Skip to content

Instantly share code, notes, and snippets.

@InEase
Created March 30, 2023 08:15
Show Gist options
  • Save InEase/7a66e995062676cba2099ac7cd4c5c56 to your computer and use it in GitHub Desktop.
Save InEase/7a66e995062676cba2099ac7cd4c5c56 to your computer and use it in GitHub Desktop.
import numpy as np
per_level_scale = 1.381912879967776
log2_per_level_scale = np.log2(per_level_scale)
base_resolution = 16
log2_hashmap_size = 14
HashIn = np.load("./HashIn.npy")
HashOut = np.load("./HashOut.npy")[0:65536]
HashWeight = np.load("./HashWeight.npy")
HashWeight = HashWeight.reshape((-1,2))
HashTable = {}
HTOffset = [0 for i in range(17)]
for i in range(16):
HTScale = np.exp2(i * log2_per_level_scale) * base_resolution - 1
HTReso = np.ceil(HTScale) + 1
HTSize = HTReso * HTReso * HTReso
if HTSize > 2**log2_hashmap_size:
HTSize = 2**log2_hashmap_size
if HTSize % 2 == 1:
HTSize = HTSize + 1
HTOffset[i+1] = int(HTSize + HTOffset[i])
HashTable[str(i)] = HashWeight[HTOffset[i]:HTOffset[i+1]]
print(HTOffset)
print(len(HashWeight))
def HashInt(Coord: np.ndarray):
CoordInt = Coord.astype(np.int64)
CoordInt[0] = CoordInt[0] * 1
CoordInt[1] = CoordInt[1] * 2654435761
CoordInt[2] = CoordInt[2] * 805459861
XORCoord = (CoordInt[0].astype(int)^CoordInt[1].astype(int)^CoordInt[2].astype(int))
return XORCoord
def HashLocation(Coord: np.ndarray, Resolution: int, HashSize: int, Y: int, Z: int):
stride = 1
index = 0
for dim in range(0,3):
if stride > HashSize:
break
index += Coord[dim] * stride
stride *= Resolution
if HashSize < stride:
index = HashInt(Coord)
BeforeShift = int(index) % HashSize
FinalAddr = BeforeShift % (HashSize / 4)
if Y > 0:
FinalAddr = FinalAddr + HashSize / 4
if Z > 0:
FinalAddr = FinalAddr + HashSize / 2
return int(FinalAddr)
def HashFunc(Level: int, HashSize: int, Coord: np.ndarray, HashTable: np.ndarray):
LevelScale = np.exp2(Level * log2_per_level_scale) * base_resolution - 1
Resolution = np.ceil(LevelScale) + 1
Pos = Coord * LevelScale + 0.5
PosGrid = np.floor(Pos)
Pos = Pos - PosGrid
Feature = np.zeros((1,2))
for idx in range(0,8):
LocalPosGrid = np.zeros_like(Coord)
ZeroPosGrid = np.zeros_like(Coord)
weight = 1
for dim in range(0,3):
if not idx & 1 << dim:
weight *= 1 - Pos[dim]
LocalPosGrid[dim] = PosGrid[dim]
ZeroPosGrid[dim] = 0
else:
weight *= Pos[dim]
LocalPosGrid[dim] = PosGrid[dim] + 1
ZeroPosGrid[dim] = 1
HashIndex2Fetch = HashLocation(LocalPosGrid, Resolution, HashSize, ZeroPosGrid[1], ZeroPosGrid[2])
FetchedFeature = HashTable[HashIndex2Fetch]
Feature += FetchedFeature * weight
return Feature
from tqdm import trange
HashOut_lsx = np.zeros((len(HashOut), 32))
for i in trange(len(HashOut)):
for j in range(16):
HashOut_lsx[i][2*j:2*j+2] = HashFunc(j, HTOffset[j+1] - HTOffset[j], HashIn[i].transpose(), HashTable[str(j)])
import warnings
warnings.filterwarnings("error")
HashOut_diff = np.zeros((len(HashOut), 32))
HashDiffRela = np.zeros((1,32))
HashDiffSW = np.zeros((1,32))
ActiveCount = 0
for i in range(65536):
if 0 in HashOut[i]:
continue
else:
try:
ActiveCount += 1
HashDiffSW += np.divide(np.abs(HashOut_lsx[i] - HashOut[i]),HashOut[i])
except RuntimeWarning:
a = 0
HashDiffSW = HashDiffSW / ActiveCount
print(HashDiffSW)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment