Module: Geospatial::Interleave

Defined in:
lib/geospatial/interleave.rb

Class Method Summary collapse

Class Method Details

.map(index, bits) ⇒ Object

Convert a Tranpose Hilbert index into a Hilbert integer 15-bit Hilbert integer = A B C D E F G H I J K L M N O is stored as its Transpose: x = A D G J M x = B E H K N x = C F I L O

|--bits-|


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/geospatial/interleave.rb', line 30

def self.map(index, bits)
  result = 0
  
  index.each_with_index do |x, i|
    offset = index.size - (i+1)
    
    bits.times do |j|
      result |= (x & 1) << (j*index.size+offset)
      
      x >>= 1
      
      break if x == 0
    end
  end
  
  return result
end

.unmap(integral, width) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/geospatial/interleave.rb', line 48

def self.unmap(integral, width)
  result = [0] * width
  mask = (1 << width) - 1
  offset = 0
  
  while integral != 0
    # N times, look at each bit and append
    width.times do |i|
      bit = (integral >> i) & 1
      result[-1-i] |= bit << offset
    end
    
    # Pop first n bits
    integral >>= width
    
    offset += 1
  end
  
  return result
end