Skip to content

Instantly share code, notes, and snippets.

@visualzhou
Created October 23, 2014 22:19
Show Gist options
  • Save visualzhou/e9792acdfc93b44a313f to your computer and use it in GitHub Desktop.
Save visualzhou/e9792acdfc93b44a313f to your computer and use it in GitHub Desktop.
import re
import pprint
import math
foo = """
field #0['loc']: [BinData(128, 611325C000000000), BinData(128, 611325FFFFFFFFFF)], [BinData(128, 6113274000000000), BinData(128, 6113277FFFFFFFFF)], [BinData(128, 611327C000000000), BinData(128, 611327FFFFFFFFFF)], [BinData(128, 61132D0000000000), BinData(128, 61132DFFFFFFFFFF)], [BinData(128, 61132F0000000000), BinData(128, 61132FFFFFFFFFFF)], [BinData(128, 6113300000000000), BinData(128, 611333FFFFFFFFFF)], [BinData(128, 6113340000000000), BinData(128, 611334FFFFFFFFFF)], [BinData(128, 6113360000000000), BinData(128, 611336FFFFFFFFFF)], [BinData(128, 6113380000000000), BinData(128, 61133BFFFFFFFFFF)], [BinData(128, 61133C0000000000), BinData(128, 61133CFFFFFFFFFF)], [BinData(128, 61133E0000000000), BinData(128, 61133EFFFFFFFFFF)], [BinData(128, 6113854000000000), BinData(128, 6113857FFFFFFFFF)], [BinData(128, 611385C000000000), BinData(128, 611385FFFFFFFFFF)], [BinData(128, 6113900000000000), BinData(128, 611390FFFFFFFFFF)], [BinData(128, 6113910000000000), BinData(128, 611391FFFFFFFFFF)], [BinData(128, 6113940000000000), BinData(128, 611394FFFFFFFFFF)]
"""
SCALING_FACTOR = 2 ** 32 / 360.0
## Initialized hashedToNormal array
hashedToNormal = [0] * 256
for i in range(16):
fixed = 0
for j in range(4):
if (i & (i << j)) > 0:
fixed |= 1 << (j * 2)
hashedToNormal[fixed] = i
class HashBox(object):
def __init__(self, min_point, max_point):
super(HashBox, self).__init__()
self.min_point = min_point
self.max_point = max_point
def getHashes(s):
hashes = []
hashStrs = []
for match in re.finditer('BinData\(128, ([0-9A-F]+)\)', s):
hashStrs.append(match.group(1))
for i in range(0, len(hashStrs), 2):
start = int(hashStrs[i], 16)
end = int(hashStrs[i+1], 16)
level = get_bits(start, end)
edge_size = 2.0 ** level / SCALING_FACTOR
min_point = unhash_fast(start)
max_point = { "x": min_point["x"] + edge_size, "y": min_point["y"]}
hashes.append(HashBox(min_point, max_point))
return hashes
# returns { x: Number, y: Number }
def unhash_fast(_hash):
x = 0
y = 0
for i in range(8):
c = _hash % 256
_hash /= 256
t = c & 0x55
y |= hashedToNormal[t] << (4 * i)
t = (c >> 1) & 0x55
x |= hashedToNormal[t] << (4 * i)
# scaling code
x /= SCALING_FACTOR
x -= 180
y /= SCALING_FACTOR
y -= 180
return {'x': x, 'y': y}
def get_bits(start, end):
bitHash = end - start;
return int(math.log(bitHash, 2)) / 2
hash_boxes = getHashes(foo)
for box in hash_boxes:
print box.min_point, box.max_point
# pprint.pprint(map(unhash_fast, hashes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment