Skip to content

Commit 4ba94f7

Browse files
committed
helix_orient: normalize input in angle_error_deg (codex P2)
encode() accepts non-unit input, but the round-trip angular error is cos⁻¹ of the dot of two UNIT directions. decode() already returns a unit vector; normalize the input too, else a scaled vector (e.g. [2,0,0]) clamps the dot to 1.0 → reports 0° and short vectors report inflated errors, making the codec-quality diagnostic meaningless. Lib tests (incl. three_byte_is_sub_tenth_degree) green on 1.95.
1 parent eb0b7df commit 4ba94f7

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/hpc/splat3d/helix_orient.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,15 @@ pub fn quat_from_normal(n: [f32; 3]) -> [f32; 4] {
156156
/// Round-trip angular error in degrees (test/diagnostic helper).
157157
pub fn angle_error_deg(normal: [f32; 3], levels: usize) -> f64 {
158158
let d = decode(&encode(normal, levels)[..levels.clamp(1, 3)]);
159-
let dd = (f64::from(normal[0]) * f64::from(d[0])
160-
+ f64::from(normal[1]) * f64::from(d[1])
161-
+ f64::from(normal[2]) * f64::from(d[2]))
159+
// `encode` accepts non-unit input, but the angular error is cos⁻¹ of the dot of
160+
// two UNIT directions. `decode` already returns a unit vector; normalize `normal`
161+
// too, else a scaled input (e.g. [2,0,0]) clamps to 1.0 → reports 0°, and short
162+
// vectors report inflated errors — making the diagnostic meaningless.
163+
let nm = (f64::from(normal[0]).powi(2) + f64::from(normal[1]).powi(2) + f64::from(normal[2]).powi(2)).sqrt();
164+
let inv = if nm > 0.0 { 1.0 / nm } else { 0.0 };
165+
let dd = (f64::from(normal[0]) * inv * f64::from(d[0])
166+
+ f64::from(normal[1]) * inv * f64::from(d[1])
167+
+ f64::from(normal[2]) * inv * f64::from(d[2]))
162168
.clamp(-1.0, 1.0);
163169
dd.acos().to_degrees()
164170
}

0 commit comments

Comments
 (0)