module Geospatial::Interleave

Public Class Methods

map(index, bits) click to toggle source

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-|
# 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) click to toggle source
# 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