Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / brep / fingerprint.lucb

## A B-rep's fingerprint: FNV-1a (64-bit) over the bytes of its tolerances
## and of every column, in one fixed order, with each column's length. Two
## models with equal fingerprints hold the same numbers bit for bit; the
## hash has no seed, so a fingerprint is the same on every run, thread and
## OS. Derived data (records, meshes) is not part of it. The robustness
## harness (tests/robustness) compares fingerprints to prove an operation
## deterministic.

let fnv_offset: u64 = 14695981039346656037
let fnv_prime: u64 = 1099511628211

func fnv_bytes(h: u64, data: const u8*, count: usize) -> u64:
    var r = h
    for at in 0..<count: r = (r ^ (u64)data[at]) *% fnv_prime
    return r

## `values`' bytes and their count mixed into `h`.
func fnv_column[T](h: u64, values: const T[]) -> u64:
    let count = (u64)values.length
    let r = fnv_bytes(h, (const u8*)&count, memory.size_of(u64))
    return fnv_bytes(r, (const u8*)values.data, values.length * memory.size_of(T))

func fnv_texts(h: u64, texts: const FaceTexts*) -> u64!:
    var bytes = Growing[u8]()
    defer bytes.close()
    var offsets = Growing[i32]()
    defer offsets.close()
    try texts.pack(&bytes, &offsets)
    return fnv_column[i32](fnv_column[u8](h, bytes.view()), offsets.view())

extend BrepModel:
    pub func fingerprint() -> u64!:
        let tolerances = self.tolerances
        var h = fnv_bytes(fnv_offset, (const u8*)&tolerances, memory.size_of(CadTolerances))
        h = fnv_column[Vector3](h, self.vertices.view())
        h = fnv_column[u8](h, self.curve_kind.view())
        h = fnv_column[i32](h, self.curve_ends.view())
        h = fnv_column[f64](h, self.curve_frame.view())
        h = fnv_column[i32](h, self.curve_degree.view())
        h = fnv_column[u8](h, self.curve_sense.view())
        h = fnv_column[i32](h, self.curve_point_offsets.view())
        h = fnv_column[Vector3](h, self.curve_points.view())
        h = fnv_column[i32](h, self.curve_weight_offsets.view())
        h = fnv_column[f64](h, self.curve_weights.view())
        h = fnv_column[i32](h, self.curve_knot_offsets.view())
        h = fnv_column[f64](h, self.curve_knots.view())
        h = fnv_column[f64](h, self.edge_precision.view())
        h = fnv_column[f64](h, self.vertex_precision.view())
        h = fnv_column[u8](h, self.surface_kind.view())
        h = fnv_column[u8](h, self.surface_edited.view())
        h = fnv_column[f64](h, self.surface_frame.view())
        h = fnv_column[i32](h, self.surface_dims.view())
        h = fnv_column[i32](h, self.surface_cv_offsets.view())
        h = fnv_column[Vector3](h, self.cv_positions.view())
        h = fnv_column[i32](h, self.surface_weight_offsets.view())
        h = fnv_column[f64](h, self.cv_weights.view())
        h = fnv_column[i32](h, self.surface_knot_offsets.view())
        h = fnv_column[f64](h, self.surface_knots.view())
        h = fnv_column[i32](h, self.face_surface.view())
        h = fnv_column[u8](h, self.face_sense.view())
        h = fnv_column[i32](h, self.face_use_offsets.view())
        h = fnv_column[i32](h, self.face_uses.view())
        h = fnv_column[i32](h, self.face_loop_offsets.view())
        h = fnv_column[i32](h, self.loop_sizes.view())
        h = fnv_column[Matrix4](h, self.face_placement.view())
        h = fnv_column[Vector3](h, self.face_color.view())
        h = fnv_column[u8](h, self.face_colored.view())
        h = try fnv_texts(h, &self.face_paths)
        return try fnv_texts(h, &self.face_names)