dymokomi/luce-geocore
Source of release 0.7.9
0.7.9 / src / geometries / topology / topology_hash.lucb
## 64-bit fingerprint of polygon connectivity: point count, face sizes and
## corner indices, not positions or display triangles. Editors compare it to
## guard recipes that address elements by index. The face sizes and corners
## are hashed in fixed chunks in parallel and the chunk hashes combined in
## order, so the value depends on neither the thread count nor how the arrays
## are stored.
from luce_std import parallel
from core.hash import mix
import core.ranges
## core.limits (8M faces, 32M corners) keep the chunks under 1024.
let chunk: usize = 65536
## Never zero: callers use zero for "no guard recorded". `offsets` has one
## entry per face and a final one; face f's size is offsets[f + 1] - offsets[f].
pub func topology_hash(points: usize, offsets: const i32[], corners: const i32[]) -> u64:
let faces = offsets.length - 1 if offsets.length > 0 else 0
let total = faces + corners.length
let chunks = (total + chunk - 1) // chunk
var results: u64[1024] = ---
var work = Work(offsets, corners, faces, total, results[..<chunks])
ranges.each_range(chunks, 1, (void*)&work, chunk_hashes)
var state = mix(mix(0x6A09E667F3BCC908, (u64)points), (u64)faces)
for value in results[..<chunks]: state = mix(state, value)
# fmix64 avalanche so nearby topologies spread over all 64 bits.
state = (state ^ (state >> 33)) *% 0xFF51AFD7ED558CCD
state = (state ^ (state >> 33)) *% 0xC4CEB9FE1A85EC53
state = state ^ (state >> 33)
return state if state != 0 else 1
struct Work:
let offsets: const i32[]
let corners: const i32[]
let faces: usize
let total: usize
let results: u64[]
## Each chunk of the sequence face sizes, then corners.
func chunk_hashes(context: void*, first: usize, end: usize, worker: usize):
let work = (Work*)context
for index in first..<end:
let start = index * chunk
let stop = start + chunk if work.total - start > chunk else work.total
var state = mix(0x243F6A8885A308D3, (u64)index)
for at in start..<stop:
let value = (u64)(work.offsets[at + 1] - work.offsets[at]) if at < work.faces else (u64)work.corners[at - work.faces]
state = mix(state, value)
work.results[index] = state