Repository navigation
fix(query): validate_orientation catches faces whose material normal points into the solid - #39
Conversation
…points into the solid validate() checks that twin half-edges run opposite, but nothing ties a face's sense to the side its material is on: a face with a flipped sense, or a whole solid turned inside-out, passes it. query::validate_orientation probes each face geometrically: a point just in front of the face along its material normal (surface normal times sense) must not be inside the solid while the point just behind it is outside (offset 100x the geometric tolerance). Probe points are the deepest inside cells of a 6x6 UV grid over the face's trim bounds. A face is only reported when its second probe agrees, since one ray can graze an edge (a hole on a folded sheet) and miscount; Boundary or Unknown answers make no claim. The check stands aside where point classification is unreliable: a tolerance below 1e-12 of the model's coordinate scale (including surface frame origins), or a Math range error. validity() runs it after body_type(), so an inverted solid is reported invalid. The case tests run it on every builder, the stored booleans and the boolean matrix. Run on every builder and boolean result of the test suite, it flags only generic_single_closed_loop_boolean on a cone and a sphere: the cone-sphere intersection patch of generic_closed_loop_handles_periodic_winding, and the cone's lateral face of the off-axis cone-sphere union and subtraction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VFZvqx1RmXzPtbL9VXP4H7
| .flat_map(|axis| [axis.lo, axis.hi]) | ||
| .chain(brep.geometry.surfaces.iter().flat_map(|s| s.frame().origin)) | ||
| .fold(diagonal, |scale, value| scale.max(value.abs())); | ||
| if !coordinates.is_finite() || brep.accuracy.geometric < RELIABLE_TOLERANCE * coordinates { |
There was a problem hiding this comment.
The check is silently skipped on large coordinates. geometric < 1e-12 * coordinates is true for anything above 1e4 at Accuracy::STANDARD, so a building in millimetres or a georeferenced model always passes.
Suggestion: compare the probe offset with the float spacing at that coordinate instead, and keep the frustum case as its own guard.
| return Ok(()); | ||
| } | ||
| match check_faces(brep) { | ||
| Err(GeometryError::Math(_)) => Ok(()), |
There was a problem hiding this comment.
Any Math error makes the check pass. Err(GeometryError::Math(_)) => Ok(()) wraps all of check_faces, so one failing face hides an inverted face after it.
Suggestion: catch the error per face (skip that face, carry on) rather than around the whole loop.
| return Ok(None); | ||
| }; | ||
| let step = scale( | ||
| unit(normal)?, |
There was a problem hiding this comment.
Probe failures make validity() report invalid. A non-Math error from unit(normal)? or the classifier now returns valid: false for a body that was only not probeable.
Suggestion: treat every probe error as "no claim", the same as Boundary and Unknown; only a confirmed inversion should be an error.
| match body.body_type() { | ||
| let checked = body | ||
| .body_type() | ||
| .and_then(|body_type| validate_orientation_validated(body).map(|()| body_type)); |
There was a problem hiding this comment.
Keep validity() as it is. The check is O(faces²) (10 s to about 2 minutes on the batch matrix), and it makes three cone and sphere boolean results on main report invalid.
Suggestion: ship query::validate_orientation as its own query and leave validity() unchanged; wire it in later, once the closed-loop boolean is fixed.
| face.sense.multiplier() * PROBE_OFFSET * brep.accuracy.geometric, | ||
| ); | ||
| let on_face = surface.point_at(uv)?; | ||
| let front = classify_point_validated(brep, add(on_face, step))?; |
There was a problem hiding this comment.
Bounds and trim polygons are rebuilt per probe.
Suggestion: one Classifier::new(brep) for the run and one FaceTrim::new(brep, face) per face, calling .contains per cell.
| if probe(brep, face, first)? != Some(true) { | ||
| return Ok(false); | ||
| } | ||
| let Some(second) = second else { |
There was a problem hiding this comment.
A face with one inside cell is judged on one probe, which is the graze case the two-probe rule guards against.
Suggestion: with fewer than two cells, sample a finer grid on that face or make no claim.
| ); | ||
| } | ||
|
|
||
| fn assert_inverted(body: &BrepEnvelope, face: u32) { |
There was a problem hiding this comment.
Tests. Add a shelled solid (cavity shell), a multi-solid body, a flipped face at a coordinate above 1e4, and a test that pins the three known cone and sphere results.
What
BrepEnvelope::validate()checks that twin half-edges run opposite, but nothing ties a face'ssenseto the side its material is on. A face with a flippedsense, or a whole solid that is inside-out (every face flipped), passesvalidate()today: the new tests build both from a cuboid and a sphere, andvalidate()accepts them.query::validate_orientation(&brep)probes each face geometrically. A point just in front of the face along its material normal (surface normal timessense) must not be inside the solid while the point just behind it is outside. The offset is 100x the geometric tolerance.face_contains_uvsays inside, ranked by their distance (in cells) from the trim and the bounds. The deepest cell is probed first.BoundaryorUnknownmakes no claim.Mathrange error (a 1e300 or 1e-200 sphere). Those bodies are also the ones the tessellator refuses.The error is
InvalidTopology("face <id> ('<key>') has its material normal pointing into the solid").query::validity()now runs it afterbody_type(), sovalidis false, with that error, for an inverted solid. It is not run insideBuilder::finish_solid: the builder sits inbrep, belowquery, in the layer table.The check costs two to four point classifications per face, so it is O(faces²).
validity()is an on-demand query, and nothing on the boolean or builder path calls it.Findings on
mainWith the check temporarily run on every
finish_solidand every boolean / batch / shell result across the whole test suite, every builder and almost every boolean passes. The three results it flags all come fromgeneric_single_closed_loop_booleanon a cone and a sphere:generic_closed_loop_handles_periodic_winding(cone ∩ sphere): face 0,lateral:intersection-patch, is inside-out.generic_closed_loop_imprints_an_off_axis_sphere_cone_intersection, union and subtraction: face 0,cone:lateral, has its material normal pointing into the solid.These tests pass today because they check
validate()and tessellation, not orientation. This PR leaves them as they are; the fixes belong with the closed-loop Boolean.Tests
query::tests::a_face_whose_material_normal_points_into_the_solid_is_rejected: a cuboid with one flipped face, and a sphere with every face flipped, passvalidate();validate_orientationreports the face andvalidity()is invalid.query::tests::reversed_bores_thin_plates_and_curved_solids_pass_the_orientation_check: a tube (reversed bore), a 1000 x 500 x 1 plate at standard accuracy, a torus and a cone.query::tests::the_orientation_check_stands_aside_where_classification_is_unreliable: the near-cylinder frustum and the 1e300 / 1e-200 spheres from the tessellation tests.tests/cases: every builder, the five stored booleans and the 18-row boolean matrix now go throughvalidate_orientation(it validates first, so it replaces the matrix'svalidate()call). The batch matrix passes too, but its walls have many faces and the O(faces²) check took the debugcasesrun from about 10 s to about 2 minutes, so it is not asserted there.cargo fmt --check,cargo clippy --all-targets -D warnings,npm run check:source,check:duplicates,check:cycles: clean.cargo test --all-targets --no-fail-fast(withCI=true, on Windows x86_64): all pass, 376 kernel unit tests (373 onmainplus 3 new) and every integration binary. Browser runs andcheck:apiwere not run; the TypeScript surface does not change.cargo run --release --example budgets: 2.6 ms here against 2.0 ms onmain, on a loaded machine (the bound is 2000 ms); the Boolean path does not call the check.Files
main/opengeometry/src/query/orientation.rs(new)main/opengeometry/src/query/mod.rsmain/opengeometry/src/query/measure.rsmain/opengeometry/src/query/tests.rsmain/opengeometry/tests/cases/builders.rs,matrix.rs🤖 Generated with Claude Code