From 783815ab48d9f0b303923cb08648c43cfe410283 Mon Sep 17 00:00:00 2001 From: Demarius Chrite Date: Fri, 13 Feb 2015 11:59:14 -0500 Subject: [PATCH 1/9] Graycode inverse. --- index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/index.js b/index.js index 787115e..d93a379 100644 --- a/index.js +++ b/index.js @@ -197,6 +197,24 @@ function rotate3d(level, x, y, z) { // :: Int -> Int -> Int -> Int -> [Int, Int, } } +function grayInverse (g) { + var m = bits(g), inverse = g, j = 1 + while (j < m) { + inverse = inverse ^ (g >> j) + j++ + } + return inverse +} + +function bits (n) { + var ret = 0 + while (n > 0) { + ret++ + n = n >> 1 + } + return ret +} + exports.xy2d = function (x, y, height) { height = height || 2 return convert2dPointToDistance(new Point(x, y), height) From 78f20e4d85114bea6ede2246260f8e267364d9ec Mon Sep 17 00:00:00 2001 From: Demarius Chrite Date: Fri, 13 Feb 2015 15:04:59 -0500 Subject: [PATCH 2/9] Working on Hilbert indexes. --- index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d93a379..b08cf3d 100644 --- a/index.js +++ b/index.js @@ -197,7 +197,8 @@ function rotate3d(level, x, y, z) { // :: Int -> Int -> Int -> Int -> [Int, Int, } } -function grayInverse (g) { +// returns a non-negative int 'inverse' such that graycode(inverse) = g +function grayInverse (g) { // : Int -> Int var m = bits(g), inverse = g, j = 1 while (j < m) { inverse = inverse ^ (g >> j) @@ -206,7 +207,8 @@ function grayInverse (g) { return inverse } -function bits (n) { +// Returns the number of bits required to store an integer +function bits (n) { // :: Int > Int var ret = 0 while (n > 0) { ret++ @@ -215,6 +217,48 @@ function bits (n) { return ret } +function bitwiseRotateRight (x, n) { + var y = (x >> n) & ~(-1 << (32 - n)) + var z = x << (32 - n) + return y | z +} + +function bitwiseRotateLeft (x, n) { + var y = (x << n) & ~(-1 >> (32 - n)) + var z = x >> (32 - n) + return y | z +} + +function grayTransform (entry, direction, x) { + return bitwiseRotateRight((x ^ entry), direction + 1) +} + +function hilbertIndex(dim, precision, point) { + var index = 0, entry = 0, direction = 0, i = precision - 1, mask, code + + while (i >= 0) { + var arr = point.toArray() + var bits = 0 + + for (var k = 0; k < arr.length; k++) { + mask = arr[arr.length - k] << (i - 1) + bits |= mask + console.log(bits) + } + + bits = grayTransform(entry, direction, bits) + code = grayInverse(bits) + + entry = entry ^ bitwiseRotateLeft((entry * code), direction + 1) + direction = direction + (direction * code) + (1 % dim) + index = (index << dim) | code + + i-- + } + + return index +} + exports.xy2d = function (x, y, height) { height = height || 2 return convert2dPointToDistance(new Point(x, y), height) @@ -227,3 +271,6 @@ exports.xyz2d = function(x, y, z, height) { } exports.d2xy = convertDistanceTo2dPoint exports.d2xyz = convertDistanceTo3dPoint +exports.hilbert = function (dim, m, x, y, z) { + return hilbertIndex(dim, m, new Point(x, y, z)) +} From ad66e81592077ed012b79b6ac8e38c9dc51393c6 Mon Sep 17 00:00:00 2001 From: Demarius Chrite Date: Sat, 14 Feb 2015 09:13:22 -0400 Subject: [PATCH 3/9] Managing bits. --- index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index b08cf3d..77f6b23 100644 --- a/index.js +++ b/index.js @@ -238,15 +238,14 @@ function hilbertIndex(dim, precision, point) { while (i >= 0) { var arr = point.toArray() - var bits = 0 + var bits = '' for (var k = 0; k < arr.length; k++) { - mask = arr[arr.length - k] << (i - 1) - bits |= mask - console.log(bits) + bits = bits.concat(arr[arr.length - k] << (i - 1)) } - bits = grayTransform(entry, direction, bits) + console.log(parseInt(bits)) + bits = grayTransform(entry, direction, parseInt(bits)) code = grayInverse(bits) entry = entry ^ bitwiseRotateLeft((entry * code), direction + 1) From e56e8de042b2abc7ea643e5c0c7ef95ec2f64efd Mon Sep 17 00:00:00 2001 From: Demarius Chrite Date: Sat, 14 Feb 2015 17:34:19 -0400 Subject: [PATCH 4/9] Fix radix on parseInt(). --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 77f6b23..4fe79ff 100644 --- a/index.js +++ b/index.js @@ -244,8 +244,8 @@ function hilbertIndex(dim, precision, point) { bits = bits.concat(arr[arr.length - k] << (i - 1)) } - console.log(parseInt(bits)) - bits = grayTransform(entry, direction, parseInt(bits)) + console.log(parseInt(bits, 2)) + bits = grayTransform(entry, direction, parseInt(bits, 2)) code = grayInverse(bits) entry = entry ^ bitwiseRotateLeft((entry * code), direction + 1) From de91f3b78f0e9b76279cb2f2fb08d33124040514 Mon Sep 17 00:00:00 2001 From: Demarius Chrite Date: Mon, 16 Feb 2015 11:50:20 -0500 Subject: [PATCH 5/9] More bit-fiddling. --- index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 4fe79ff..1cac3af 100644 --- a/index.js +++ b/index.js @@ -238,14 +238,17 @@ function hilbertIndex(dim, precision, point) { while (i >= 0) { var arr = point.toArray() - var bits = '' + var bits = 0 for (var k = 0; k < arr.length; k++) { - bits = bits.concat(arr[arr.length - k] << (i - 1)) + if (arr[arr.length - k] & (1 << (i - 1))) { + bits |= 1 << (i - 1) + } else { + bits &= ~(1 << (i-1)) + } } - console.log(parseInt(bits, 2)) - bits = grayTransform(entry, direction, parseInt(bits, 2)) + bits = grayTransform(entry, direction, bits) code = grayInverse(bits) entry = entry ^ bitwiseRotateLeft((entry * code), direction + 1) From b75a84c009d942f865a1b6cd10cc01765d114657 Mon Sep 17 00:00:00 2001 From: Demarius Chrite Date: Tue, 17 Feb 2015 22:27:18 -0500 Subject: [PATCH 6/9] Fix bitwise rotate and order bit gathering. --- index.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 1cac3af..0d26504 100644 --- a/index.js +++ b/index.js @@ -224,9 +224,7 @@ function bitwiseRotateRight (x, n) { } function bitwiseRotateLeft (x, n) { - var y = (x << n) & ~(-1 >> (32 - n)) - var z = x >> (32 - n) - return y | z + return (x << n) | (x >> (32 - n)) & ~(-1 << n) } function grayTransform (entry, direction, x) { @@ -234,20 +232,21 @@ function grayTransform (entry, direction, x) { } function hilbertIndex(dim, precision, point) { - var index = 0, entry = 0, direction = 0, i = precision - 1, mask, code + var index = 0, entry = 0, direction = 0, i = precision - 1, code while (i >= 0) { var arr = point.toArray() var bits = 0 for (var k = 0; k < arr.length; k++) { - if (arr[arr.length - k] & (1 << (i - 1))) { - bits |= 1 << (i - 1) - } else { - bits &= ~(1 << (i-1)) + if (arr[arr.length - (k+1)] & (1 << (i - 1))) { + // need to set kth bit, not ith + bits |= 1 << k } } + console.log(bits.toString(2)) + bits = grayTransform(entry, direction, bits) code = grayInverse(bits) From 3491f62401a18140ff4a11005ae02ae577a1d51a Mon Sep 17 00:00:00 2001 From: Joshua Pelletier Date: Sat, 21 Feb 2015 08:00:29 -0800 Subject: [PATCH 7/9] Add test case to step through. --- t/xy/2dto1d.t.js | 1 + 1 file changed, 1 insertion(+) diff --git a/t/xy/2dto1d.t.js b/t/xy/2dto1d.t.js index 58dd62d..ad0d509 100755 --- a/t/xy/2dto1d.t.js +++ b/t/xy/2dto1d.t.js @@ -7,4 +7,5 @@ require('proof')(3, function (assert) { assert(hilbert.xy2d(16, 2, 2), 8) assert(hilbert.xy2d(0, 0, 2), 0) assert(hilbert.xy2d(0, 1, 3), 1) + hilbert.hilbert(2, 3, 5, 6) }) From e4a14fc8a0506592ff0465f37651484f6b7756d8 Mon Sep 17 00:00:00 2001 From: Joshua Pelletier Date: Sun, 22 Feb 2015 08:04:25 -0800 Subject: [PATCH 8/9] Add comments. --- index.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 0d26504..c026c6e 100644 --- a/index.js +++ b/index.js @@ -198,7 +198,7 @@ function rotate3d(level, x, y, z) { // :: Int -> Int -> Int -> Int -> [Int, Int, } // returns a non-negative int 'inverse' such that graycode(inverse) = g -function grayInverse (g) { // : Int -> Int +exports.grey = function grayInverse (g) { // : Int -> Int var m = bits(g), inverse = g, j = 1 while (j < m) { inverse = inverse ^ (g >> j) @@ -227,29 +227,45 @@ function bitwiseRotateLeft (x, n) { return (x << n) | (x >> (32 - n)) & ~(-1 << n) } -function grayTransform (entry, direction, x) { +exports.gc = function grayTransform (entry, direction, x) { return bitwiseRotateRight((x ^ entry), direction + 1) } function hilbertIndex(dim, precision, point) { + // dim = n + // precision = m + // p = point (5,6) + // vvv this transformation needs to happen + // l = [ bit [ 101, 110 ] <- not exactly, this is takes a single bit + // ^^ this may be reverse order + // entry = e + // direction = d + // code = w + // index = h var index = 0, entry = 0, direction = 0, i = precision - 1, code + // while (i >= 0) { - var arr = point.toArray() + + var arr = point.toArray()// transform the points to binary. + // l = [bit(p sub n-1 ; i), bit(p sub n 0 ; i)] [11], [10], [01] var bits = 0 + // what is this vvv? for (var k = 0; k < arr.length; k++) { if (arr[arr.length - (k+1)] & (1 << (i - 1))) { // need to set kth bit, not ith bits |= 1 << k } } + // ^^^ what is this ^^^? console.log(bits.toString(2)) + // vv look into each of these variables as well as the functions + bits = grayTransform(entry, direction, bits) // transform <- 3, 2, 1 + code = grayInverse(bits) // 2, 3, 1 - bits = grayTransform(entry, direction, bits) - code = grayInverse(bits) - + // vvv new entry direction and index entry = entry ^ bitwiseRotateLeft((entry * code), direction + 1) direction = direction + (direction * code) + (1 % dim) index = (index << dim) | code @@ -257,7 +273,7 @@ function hilbertIndex(dim, precision, point) { i-- } - return index + return index // set B subscript M } exports.xy2d = function (x, y, height) { From 92065c6140df85eed1e4223c81f1f4f863107917 Mon Sep 17 00:00:00 2001 From: Joshua Pelletier Date: Mon, 23 Feb 2015 10:24:46 -0800 Subject: [PATCH 9/9] Add comments. --- index.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c026c6e..d759120 100644 --- a/index.js +++ b/index.js @@ -236,10 +236,8 @@ function hilbertIndex(dim, precision, point) { // precision = m // p = point (5,6) // vvv this transformation needs to happen - // l = [ bit [ 101, 110 ] <- not exactly, this is takes a single bit - // ^^ this may be reverse order // entry = e - // direction = d + // direction = d <- this is n-1. Interesting its set as 0. // code = w // index = h var index = 0, entry = 0, direction = 0, i = precision - 1, code @@ -262,13 +260,14 @@ function hilbertIndex(dim, precision, point) { console.log(bits.toString(2)) // vv look into each of these variables as well as the functions + // does bits go in as a string? bits = grayTransform(entry, direction, bits) // transform <- 3, 2, 1 code = grayInverse(bits) // 2, 3, 1 // vvv new entry direction and index - entry = entry ^ bitwiseRotateLeft((entry * code), direction + 1) - direction = direction + (direction * code) + (1 % dim) - index = (index << dim) | code + entry = entry ^ bitwiseRotateLeft((entry * code), direction + 1)//0,3,3 + direction = direction + (direction * code) + (1 % dim) //<- 1,0,0 + index = (index << dim) | code // <-2, 11, 45 i-- }