如何从双坐标计算希尔伯特指数?

时间:2017-03-29 22:39:55

标签: c fractals space-filling-curve

我想将由两个双值(x,y)表示的坐标对转换为希尔伯特值。我找到了以下实现(from this link):

/*****************************************************************
 * hilbert_c2i
 * 
 * Convert coordinates of a point on a Hilbert curve to its index.
 * Inputs:
 *  nDims:      Number of coordinates.
 *  nBits:      Number of bits/coordinate.
 *  coord:      Array of n nBits-bit coordinates.
 * Outputs:
 *  index:      Output index value.  nDims*nBits bits.
 * Assumptions:
 *      nDims*nBits <= (sizeof bitmask_t) * (bits_per_byte)
 */
bitmask_t
hilbert_c2i(unsigned nDims, unsigned nBits, bitmask_t const coord[])
{
  if (nDims > 1)
    {
      unsigned const nDimsBits = nDims*nBits;
      bitmask_t index;
      unsigned d;
      bitmask_t coords = 0;
      for (d = nDims; d--; )
    {
      coords <<= nBits;
      coords |= coord[d];
    }

      if (nBits > 1)
    {
      halfmask_t const ndOnes = ones(halfmask_t,nDims);
      halfmask_t const nd1Ones= ndOnes >> 1; /* for adjust_rotation */
      unsigned b = nDimsBits;
      unsigned rotation = 0;
      halfmask_t flipBit = 0;
      bitmask_t const nthbits = ones(bitmask_t,nDimsBits) / ndOnes;
      coords = bitTranspose(nDims, nBits, coords);
      coords ^= coords >> nDims;
      index = 0;
      do
        {
          halfmask_t bits = (coords >> (b-=nDims)) & ndOnes;
          bits = rotateRight(flipBit ^ bits, rotation, nDims);
          index <<= nDims;
          index |= bits;
          flipBit = (halfmask_t)1 << rotation;
          adjust_rotation(rotation,nDims,bits);
        } while (b);
      index ^= nthbits >> 1;
    }
      else
    index = coords;
      for (d = 1; d < nDimsBits; d *= 2)
    index ^= index >> d;
      return index;
    }
  else
    return coord[0];
}

但是,这是作为输入的整数值。如何适应我的双重值?

1 个答案:

答案 0 :(得分:0)

对于任何游荡的人来说,wikipedia page都有一种非常快速的计算希尔伯特一维坐标的方法。我在多个程序中使用过Hilbert(我研究Delaunay三角测量,我们需要它来加速这个过程)我可以向你保证它是最好的。