brep_topology/props/mass_properties/identity.rs
1//! The content identity of the face set the integrator actually reads.
2//!
3//! Two `solid_mass_properties` / `solid_signed_volume` calls return the same
4//! bits exactly when they see the same geometry in the same order. This hash
5//! is that statement, made checkable: it walks precisely the fields the
6//! integrator reads and nothing else, in the order it reads them.
7//!
8//! **What goes in.** Per shell in order, per face in order: the carrier's
9//! degrees, knot vectors and control points, `same_sense`, then per loop in
10//! order and per coedge in order the pcurve's degree, knots and control
11//! points and the coedge's `forward` flag.
12//!
13//! **What stays out.** `id` and `name` on every record, and the solid's
14//! `vertices` and `edges` — `solid_props.rs` never reads them, so a solid
15//! whose edge table was renumbered integrates to the same bits and must hash
16//! the same. `genus` is topology bookkeeping the integrator never consults.
17//!
18//! **Order, not set.** Both entry points run a compensated sum over the faces
19//! in shell order; permuting the faces permutes the addends and can move the
20//! last bits. So the hash is over the SEQUENCE. "The same face set" is a
21//! station-signature statement; this is the stronger one that licenses
22//! serving a stored value.
23//!
24//! **Why the bits and not the value.** `f64::to_bits` distinguishes `-0.0`
25//! from `0.0` and makes every NaN payload its own key. Both are what the
26//! integrator sees: it can consume a negative zero (the sign survives a
27//! multiply) and it never normalizes a NaN. Hashing the value would let two
28//! inputs the integrator distinguishes share a key.
29
30use crate::{BrepSolid, FaceRecord, NurbsCurve, NurbsSurface, Vec4};
31
32/// The 128-bit content identity of a solid's face set.
33///
34/// Two hashes are equal when the two solids present the integrator with the
35/// same geometry in the same order. It is a hash, so equality is evidence and
36/// not proof — but see the width note on `absorb`: a consumer that serves a
37/// stored value on a hit is betting on 2^-128, and this file owes that bet a
38/// mixer worth the number.
39#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
40pub struct FaceSetIdentity {
41 lanes: [u64; 2],
42 /// Faces walked, carried beside the digest as a cheap first discriminator
43 /// and as the thing a profile line can print.
44 pub faces: u64,
45}
46
47impl FaceSetIdentity {
48 pub fn hex(&self) -> String {
49 format!("{:016x}{:016x}", self.lanes[0], self.lanes[1])
50 }
51}
52#[allow(unused_imports)]
53pub use crate::identity_hash::{hash_curve, hash_surface, mix64, TwoLane, SEEDS, STEPS};
54
55/// Everything the per-face tessellator reads off a face record besides its
56/// coedges' shared edge samples: the carrier, `same_sense`, and per loop and
57/// coedge in order the sense and the pcurve. `watertight_tessellation::face_memo`
58/// keys a face's mesh fragment on this plus the samples' bits.
59pub fn hash_face(lane: &mut TwoLane, face: &FaceRecord) {
60 hash_surface(lane, &face.surface);
61 lane.bool(face.same_sense);
62 lane.usize(face.loops.len());
63 for loop_record in &face.loops {
64 lane.usize(loop_record.coedges.len());
65 for coedge in &loop_record.coedges {
66 lane.bool(coedge.forward);
67 hash_curve(lane, &coedge.pcurve);
68 }
69 }
70}
71
72/// The 128-bit content identity of one coedge's USE: the face's carrier, the
73/// pcurve, the edge's curve and domain and the coedge's sense — every input
74/// of a reading that compares the trim's image with the edge (the refit's
75/// detection miss, the challenger's old-trim tracking). Two uses with equal
76/// identities read the same number, bit for bit; `reading_memo.rs` keys on it.
77pub fn coedge_use_identity(
78 surface: &NurbsSurface,
79 pcurve: &NurbsCurve,
80 edge: &crate::topology::EdgeRecord,
81 forward: bool,
82) -> [u64; 2] {
83 let mut lane = TwoLane::new();
84 hash_surface(&mut lane, surface);
85 hash_curve(&mut lane, pcurve);
86 hash_curve(&mut lane, &edge.curve);
87 lane.f64(edge.t0);
88 lane.f64(edge.t1);
89 lane.bool(edge.degenerate);
90 lane.bool(forward);
91 lane.finish()
92}
93
94/// Inputs of assembly's sampled edge-to-carrier residual. IDs, trims and
95/// tolerance are absent because the reader does not consult them. Preserve
96/// carrier order, represented interval, and every geometric bit.
97pub fn carrier_residual_identity(
98 curve: &NurbsCurve, t0: f64, t1: f64, surfaces: &[&NurbsSurface],
99) -> [u64; 2] {
100 let mut lane = TwoLane::new();
101 hash_curve(&mut lane, curve);
102 lane.f64(t0);
103 lane.f64(t1);
104 lane.usize(surfaces.len());
105 for surface in surfaces { hash_surface(&mut lane, surface); }
106 lane.finish()
107}
108
109/// `key` with a tolerance folded in: the identity of a reading that is a pure
110/// function of a use's content AND one policy value (the refit's candidacy
111/// standoff, read against `policy.model`). The same content under another
112/// model is another key.
113pub fn identity_with_model(key: [u64; 2], model: f64) -> [u64; 2] {
114 let mut lane = TwoLane::new();
115 lane.u64(key[0]);
116 lane.u64(key[1]);
117 lane.f64(model);
118 lane.finish()
119}
120
121/// The 128-bit identity of an EDGE's scope claim (`heal::edge_claim_with`):
122/// the identities of the edge's uses in the order the claim is handed them,
123/// the order in which it visits their distinct carriers (`ranks`, the first
124/// use of each carrier in that visiting order) and the model tolerance.
125pub fn edge_claim_identity(uses: &[[u64; 2]], ranks: &[usize], model: f64) -> [u64; 2] {
126 let mut lane = TwoLane::new();
127 lane.usize(uses.len());
128 for key in uses {
129 lane.u64(key[0]);
130 lane.u64(key[1]);
131 }
132 lane.usize(ranks.len());
133 for &rank in ranks {
134 lane.usize(rank);
135 }
136 lane.f64(model);
137 lane.finish()
138}
139
140/// The 128-bit content identity of one face's volume contribution about
141/// `reference` under the sign reading's correction policy: everything
142/// `face_volume_contribution_about` reads (the face as `hash_face` walks it,
143/// the reference point's bits). `reading_memo.rs` keys the boolean's
144/// assembly sign check on it.
145pub fn face_contribution_identity(face: &FaceRecord, reference: crate::Vec3) -> [u64; 2] {
146 let mut lane = TwoLane::new();
147 hash_face(&mut lane, face);
148 lane.f64(reference.x);
149 lane.f64(reference.y);
150 lane.f64(reference.z);
151 lane.finish()
152}
153
154/// Hash exactly what the integrator reads, in the order it reads it.
155pub fn face_set_identity(solid: &BrepSolid) -> FaceSetIdentity {
156 let mut lane = TwoLane::new();
157 let mut faces = 0u64;
158 lane.usize(solid.shells.len());
159 for shell in &solid.shells {
160 lane.usize(shell.faces.len());
161 for face in &shell.faces {
162 faces += 1;
163 hash_face(&mut lane, face);
164 }
165 }
166 FaceSetIdentity {
167 lanes: lane.finish(),
168 faces,
169 }
170}