Skip to content

fix(query): validate_orientation catches faces whose material normal points into the solid - #39

Open
Basnederveen wants to merge 4 commits into
OpenGeometry-io:mainfrom
Bas-Automation-Services:fix/validate-face-orientation
Open

Basnederveen wants to merge 4 commits into
OpenGeometry-io:mainfrom
Bas-Automation-Services:fix/validate-face-orientation

Conversation

@Basnederveen

@Basnederveen Basnederveen commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

What

BrepEnvelope::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 that is inside-out (every face flipped), passes validate() today: the new tests build both from a cuboid and a sphere, and validate() accepts them.

query::validate_orientation(&brep) 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. The offset is 100x the geometric tolerance.

  • Probe points: a 6 x 6 grid of UV cells over the face's trim bounds, kept where the face's FaceTrim says inside, ranked by their distance (in cells) from the trim and the bounds. The deepest cell is probed first. A face with fewer than two inside cells is sampled on a 24 x 24 grid; if that also has fewer than two, the face makes no claim. One Classifier serves the whole run.
  • Two probes per face: a single ray can graze an edge (for example the edge of a hole on a folded sheet) and miscount. A face is only reported when its first probe says inverted and its second probe agrees.
  • Undecided is not an error: a probe where either point classifies as Boundary or Unknown, or where anything on the way errors (trim, surface evaluation, classifier), makes no claim, and the next face is still checked.
  • Stands aside where classification is unreliable: a probe whose offset is not more than 256 float spacings at the probe point makes no claim (at Accuracy::STANDARD the check runs to coordinates of about 1.7e7). If the offset cannot resolve some surface frame origin of the body, for example the near-cylinder frustum whose cone apex is ~1e14 away, the whole check stands aside, because every probe's ray may cross that surface. A Math range error from the classifier (a 1e300 sphere) is a probe error and makes no claim.
  • Sheets and wires (no solids) are skipped.

The error is InvalidTopology("face <id> ('<key>') has its material normal pointing into the solid").

query::validity() is unchanged; validate_orientation is its own query. It is not run inside Builder::finish_solid: the builder sits in brep, below query, in the layer table.

The check costs two to four point classifications per face, so it is O(faces²). Nothing on the boolean or builder path calls it.

Findings on main

With the check temporarily run on every finish_solid and every boolean / batch / shell result across the whole test suite, every builder and almost every boolean passes. The three results it flags all come from generic_single_closed_loop_boolean on 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 now pin the three results as inverted. #41 fixes all three, so whichever of the two lands second drops those pins.

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, pass validate(); validate_orientation reports the face.
  • query::tests::a_flipped_face_far_from_the_origin_is_rejected: a cuboid at (1e4, 2e4, 3e4), (2e5, −3e5, 1e5) and (6.5e5, 5.8e6, 200) at Accuracy::STANDARD passes, and a flipped face is reported.
  • query::tests::a_flipped_face_in_a_cavity_shell_or_a_second_solid_is_rejected: a cube with a cavity and a cube split into two solids pass; a flipped face in the cavity shell or in the second solid is reported.
  • 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, the 1e300 / 1e-200 spheres from the tessellation tests, and a flipped cuboid at 3e7.
  • boolean::tests::generic: the three closed-loop cone/sphere results above are pinned as inverted.
  • tests/cases: every builder, the five stored booleans and the 18-row boolean matrix now go through validate_orientation (it validates first, so it replaces the matrix's validate() call). The batch matrix passes too, but its walls have many faces and the O(faces²) check took the debug cases run 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 on Windows x86_64: 944 pass, and the same 6 stored-fixture and digest comparisons fail as on main on this machine. With CI=true, cases, booleans and golden all pass, so every matrix row goes through validate_orientation. Browser runs and check:api were not run; the TypeScript surface does not change.
  • cargo run --release --example budgets: 2.6 ms here against 2.0 ms on main, 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.rs
  • main/opengeometry/src/query/tests/mod.rs, main/opengeometry/src/operations/modifying/boolean/tests/generic.rs
  • main/opengeometry/tests/cases/builders.rs, matrix.rs

🤖 Generated with Claude Code

…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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 02c2a60. Each probe now checks that its offset (100 × geometric) is more than 256 float spacings at the probe point. If it isn't, that probe makes no claim. At Accuracy::STANDARD the check runs up to coordinates of about 1.7e7, so a building in millimetres or a UTM-sized site (6.5e5, 5.8e6) is checked. The frustum case is its own guard: if the offset can't resolve any surface frame origin of the body, the whole check stands aside. Applying that guard per face wasn't enough, because the frustum's planar cap was reported once the classifier's ray reached the lateral cone, whose apex is ~1e14 away.

On the margin: with it set to 1 instead of 256, a cuboid, cylinder, sphere, torus, cone and tube moved out to 1e10 were still never reported. The classifier only went undecided. So 256 leaves plenty of room.

return Ok(());
}
match check_faces(brep) {
Err(GeometryError::Math(_)) => Ok(()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 02c2a60. The Math catch around the whole loop is gone. Errors are now handled per probe, and the next face is still checked (see the next thread).

return Ok(None);
};
let step = scale(
unit(normal)?,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 02c2a60. Any error while probing a face, from FaceTrim::new, contains, normal_at, unit, point_at or the classifier, means that face makes no claim. validate_orientation now returns an error only from validate() or for a confirmed inversion.

Comment thread main/opengeometry/src/query/measure.rs Outdated
match body.body_type() {
let checked = body
.body_type()
.and_then(|body_type| validate_orientation_validated(body).map(|()| body_type));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 02c2a60. measure.rs is back to main, and query::validate_orientation is its own query. The tests/cases builders and the boolean matrix still assert it directly. The batch matrix doesn't.

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))?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 02c2a60. There is one Classifier::new(brep) per run, shared by every probe, and one FaceTrim::new(brep, face) per face, with .contains called for each grid cell.

if probe(brep, face, first)? != Some(true) {
return Ok(false);
}
let Some(second) = second else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 02c2a60. A face with fewer than two inside cells on the 6 × 6 grid is sampled again on a 24 × 24 grid. If that also has fewer than two, the face makes no claim. A face is reported only when both probes say it is inverted.

);
}

fn assert_inverted(body: &BrepEnvelope, face: u32) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Basnederveen Basnederveen Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 02c2a60:

Basnederveen and others added 2 commits November 18, 2026 08:22
Conflict in tests/cases/matrix.rs: both sides added a query import; keep both. Upstream's new rotated-box polyhedra test checks orientation as well, like the rest of the matrix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Large coordinates: the probe offset is compared with the float spacing at
  the probe point (256 spacings), so a body at 1e6 is still checked at
  Accuracy::STANDARD. Surface frames whose origin the offset cannot resolve
  (the near-cylinder frustum) still make the whole check stand aside.
- Any error while probing a face, from the trim, the surface or the
  classifier, makes no claim for that face and the next face is checked.
- One Classifier per run and one FaceTrim per face.
- A face with fewer than two inside cells on the 6 x 6 grid is sampled on
  a 24 x 24 grid, and makes no claim if that also has fewer than two.
- validity() is back to upstream; validate_orientation is its own query.
- Tests: a flipped face at 1e4 to 6e6, a cavity shell, a second solid, a
  body beyond the resolvable range, and the three known closed-loop
  cone/sphere results pinned as inverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Basnederveen

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All seven points are in 02c2a60, and I've replied inline. validity() is back to main, and validate_orientation stays its own query.

Windows x86_64: fmt and clippy (-D warnings) are clean. cargo test --all-targets --no-fail-fast gives 944 passed and 6 failed. The 6 are the same stored-fixture and digest comparisons that fail on main on this machine. They are listed on #35. With CI=true, cases, booleans and golden pass, so every boolean matrix row goes through validate_orientation. check:source, check:duplicates and check:cycles are clean.

Basnederveen added a commit to Bas-Automation-Services/OpenGeometry that referenced this pull request Nov 20, 2026
…integration/3.0

OpenGeometry-io#41 is on integration, so the closed-loop pins from OpenGeometry-io#39 are left out
(generic.rs keeps integration's version). OpenGeometry-io#40's standard winding rejects a
single flipped face, so the orientation tests flip whole shells and
re-orient their loops.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants