From 062d51de357c79cbea4d89050c81277bd93eddb8 Mon Sep 17 00:00:00 2001 From: Bas <16050771+Basnederveen@users.noreply.github.com> Date: Sun, 1 Nov 2026 12:20:49 +0100 Subject: [PATCH 1/2] fix(query): validate_orientation catches faces whose material normal 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 Claude-Session: https://claude.ai/code/session_01VFZvqx1RmXzPtbL9VXP4H7 --- main/opengeometry/src/query/measure.rs | 6 +- main/opengeometry/src/query/mod.rs | 2 + main/opengeometry/src/query/orientation.rs | 115 +++++++++++++++++++++ main/opengeometry/src/query/tests.rs | 77 +++++++++++++- main/opengeometry/tests/cases/builders.rs | 3 + main/opengeometry/tests/cases/matrix.rs | 4 +- 6 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 main/opengeometry/src/query/orientation.rs diff --git a/main/opengeometry/src/query/measure.rs b/main/opengeometry/src/query/measure.rs index a5101c23..9ba83795 100644 --- a/main/opengeometry/src/query/measure.rs +++ b/main/opengeometry/src/query/measure.rs @@ -1,3 +1,4 @@ +use super::orientation::validate_orientation_validated; use crate::brep::{BodyType, BrepEnvelope, GeometryError}; #[derive(Clone, Debug, PartialEq)] @@ -9,7 +10,10 @@ pub struct Validity { } pub fn validity(body: &BrepEnvelope) -> Validity { - match body.body_type() { + let checked = body + .body_type() + .and_then(|body_type| validate_orientation_validated(body).map(|()| body_type)); + match checked { Ok(body_type) => Validity { valid: true, closed: closed(body, body_type), diff --git a/main/opengeometry/src/query/mod.rs b/main/opengeometry/src/query/mod.rs index 88746abc..af82e880 100644 --- a/main/opengeometry/src/query/mod.rs +++ b/main/opengeometry/src/query/mod.rs @@ -2,6 +2,7 @@ mod classification; mod classify; mod face_trim; mod measure; +mod orientation; mod ray_roots; #[cfg(test)] mod tests; @@ -11,3 +12,4 @@ pub use classify::classify_point; pub(super) use classify::classify_point_in_shell; pub(super) use face_trim::face_contains_uv; pub use measure::{validity, Validity}; +pub use orientation::validate_orientation; diff --git a/main/opengeometry/src/query/orientation.rs b/main/opengeometry/src/query/orientation.rs new file mode 100644 index 00000000..e3ae788c --- /dev/null +++ b/main/opengeometry/src/query/orientation.rs @@ -0,0 +1,115 @@ +use super::classification::PointClassification; +use super::classify::classify_point_validated; +use super::face_trim::face_contains_uv; +use crate::brep::{unit, BrepEnvelope, Face, GeometryError, Surface, UV}; +use crate::math::{add, norm, scale, sub}; + +const GRID: usize = 6; +const PROBE_OFFSET: f64 = 100.0; +const RELIABLE_TOLERANCE: f64 = 1e-12; + +pub fn validate_orientation(brep: &BrepEnvelope) -> Result<(), GeometryError> { + brep.validate()?; + validate_orientation_validated(brep) +} + +pub(super) fn validate_orientation_validated(brep: &BrepEnvelope) -> Result<(), GeometryError> { + if brep.solids.is_empty() { + return Ok(()); + } + match check_faces(brep) { + Err(GeometryError::Math(_)) => Ok(()), + outcome => outcome, + } +} + +fn check_faces(brep: &BrepEnvelope) -> Result<(), GeometryError> { + let Some(bounds) = brep.bounds_unchecked()? else { + return Ok(()); + }; + let diagonal = norm(std::array::from_fn(|axis| bounds.axes[axis].width())); + let coordinates = bounds + .axes + .iter() + .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 { + return Ok(()); + } + for face in &brep.topology.faces { + if face_is_inverted(brep, face)? { + return Err(GeometryError::InvalidTopology(format!( + "face {} ('{}') has its material normal pointing into the solid", + face.id, face.key + ))); + } + } + Ok(()) +} + +fn face_is_inverted(brep: &BrepEnvelope, face: &Face) -> Result { + let [first, second] = probe_points(brep, face)?; + let Some(first) = first else { + return Ok(false); + }; + if probe(brep, face, first)? != Some(true) { + return Ok(false); + } + let Some(second) = second else { + return Ok(true); + }; + Ok(probe(brep, face, second)? == Some(true)) +} + +fn probe(brep: &BrepEnvelope, face: &Face, uv: UV) -> Result, GeometryError> { + let surface = brep.geometry.surface(face.surface)?; + let Ok(normal) = surface.normal_at(uv) else { + return Ok(None); + }; + let step = scale( + unit(normal)?, + 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))?; + let back = classify_point_validated(brep, sub(on_face, step))?; + Ok(match (front, back) { + (PointClassification::Inside, PointClassification::Outside) => Some(true), + (PointClassification::Outside, PointClassification::Inside) => Some(false), + _ => None, + }) +} + +fn probe_points(brep: &BrepEnvelope, face: &Face) -> Result<[Option; 2], GeometryError> { + let cell = + |i: usize, j: usize| -> UV { [i, j].map(|index| (index as f64 + 0.5) / GRID as f64) }; + let at = |fraction: UV| -> UV { + std::array::from_fn(|axis| { + let bounds = face.trim.uv_bounds[axis]; + bounds.lo + bounds.width() * fraction[axis] + }) + }; + let mut inside = [[false; GRID]; GRID]; + for (i, row) in inside.iter_mut().enumerate() { + for (j, value) in row.iter_mut().enumerate() { + *value = face_contains_uv(brep, face, at(cell(i, j)))? == Some(true); + } + } + let mut ranked: Vec<(usize, usize, usize)> = (0..GRID * GRID) + .map(|index| (index / GRID, index % GRID)) + .filter(|&(i, j)| inside[i][j]) + .map(|(i, j)| (depth(&inside, i, j), i, j)) + .collect(); + ranked.sort_by_key(|&(depth, i, j)| (std::cmp::Reverse(depth), i, j)); + Ok([0, 1].map(|rank| ranked.get(rank).map(|&(_, i, j)| at(cell(i, j))))) +} + +fn depth(inside: &[[bool; GRID]; GRID], i: usize, j: usize) -> usize { + let border = [i + 1, j + 1, GRID - i, GRID - j].into_iter().min(); + let outside = (0..GRID * GRID) + .map(|index| (index / GRID, index % GRID)) + .filter(|&(k, l)| !inside[k][l]) + .map(|(k, l)| i.abs_diff(k).max(j.abs_diff(l))); + border.into_iter().chain(outside).min().unwrap_or(0) +} diff --git a/main/opengeometry/src/query/tests.rs b/main/opengeometry/src/query/tests.rs index a1330868..58d255dd 100644 --- a/main/opengeometry/src/query/tests.rs +++ b/main/opengeometry/src/query/tests.rs @@ -1,6 +1,9 @@ use super::classification::PointClassification; use super::classify::{classify_point, classify_point_validated}; -use crate::brep::{coarse_accuracy, unit, Accuracy, BrepEnvelope, Frame3}; +use super::{validate_orientation, validity}; +use crate::brep::{ + coarse_accuracy, fine_accuracy, unit, Accuracy, BrepEnvelope, Frame3, GeometryError, +}; use crate::math::{add, cross, dot, scale, Point3}; use crate::primitives; @@ -431,3 +434,75 @@ fn cuboid_corners_edges_and_face_points_are_boundary() { .collect(), ); } + +fn assert_inverted(body: &BrepEnvelope, face: u32) { + body.validate().unwrap(); + let key = &body.topology.faces[face as usize].key; + match validate_orientation(body) { + Err(GeometryError::InvalidTopology(message)) => { + assert!(message.contains(&format!("'{key}'")), "{message}") + } + other => panic!("face {face} not reported: {other:?}"), + } + assert!(!validity(body).valid); +} + +#[test] +fn a_face_whose_material_normal_points_into_the_solid_is_rejected() { + let accuracy = coarse_accuracy(1e-5); + let mut cube = primitives::cuboid("cube".into(), Frame3::IDENTITY, [1.0; 3], accuracy).unwrap(); + validate_orientation(&cube).unwrap(); + cube.topology.faces[5].sense = cube.topology.faces[5].sense.reversed(); + assert_inverted(&cube, 5); + + let mut sphere = primitives::sphere("ball".into(), Frame3::IDENTITY, 1.0, accuracy).unwrap(); + for face in &mut sphere.topology.faces { + face.sense = face.sense.reversed(); + } + assert_inverted(&sphere, 0); +} + +#[test] +fn reversed_bores_thin_plates_and_curved_solids_pass_the_orientation_check() { + let accuracy = coarse_accuracy(1e-5); + let bodies = [ + primitives::annular_cylinder("tube".into(), Frame3::IDENTITY, 0.5, 1.0, 2.0, accuracy), + primitives::cuboid( + "plate".into(), + Frame3::IDENTITY, + [1000.0, 500.0, 1.0], + Accuracy::STANDARD, + ), + primitives::torus("ring".into(), Frame3::IDENTITY, 2.0, 0.5, accuracy), + primitives::cone("cone".into(), Frame3::IDENTITY, 1.0, 2.0, accuracy), + ]; + for body in bodies { + let body = body.unwrap(); + validate_orientation(&body).unwrap(); + assert!(validity(&body).valid, "{}", body.id); + } +} + +#[test] +fn the_orientation_check_stands_aside_where_classification_is_unreliable() { + let near_cylinder = primitives::frustum( + "near".into(), + Frame3::IDENTITY, + 1.0, + 1.0 + 2.0_f64.powi(-48), + 1.0, + fine_accuracy(), + ) + .unwrap(); + validate_orientation(&near_cylinder).unwrap(); + for (radius, geometric) in [(1e300, 1e290), (1e-200, 1e-210)] { + let accuracy = Accuracy { + geometric, + intersection: geometric / 4.0, + ..fine_accuracy() + }; + let sphere = + primitives::sphere("extreme".into(), Frame3::IDENTITY, radius, accuracy).unwrap(); + validate_orientation(&sphere).unwrap(); + } +} diff --git a/main/opengeometry/tests/cases/builders.rs b/main/opengeometry/tests/cases/builders.rs index 9ea68654..19d5fe78 100644 --- a/main/opengeometry/tests/cases/builders.rs +++ b/main/opengeometry/tests/cases/builders.rs @@ -1,5 +1,6 @@ use opengeometry::brep::{Accuracy, BodyType, CurveGeometry, Frame3}; use opengeometry::primitives; +use opengeometry::query::validate_orientation; use opengeometry_test_support::stored; use serde_json::json; @@ -91,6 +92,7 @@ fn primitive_builders_match_stored_json() { ), ]; for (name, body) in bodies { + validate_orientation(&body).unwrap(); assert_eq!( body.body_type().unwrap(), if name == "circle" { @@ -184,6 +186,7 @@ fn extrusions_match_stored_json() { ), ]; for (name, body) in bodies { + validate_orientation(&body).unwrap(); assert_eq!(body.body_type().unwrap(), BodyType::Solid); if stored::compared() { let expected = match name { diff --git a/main/opengeometry/tests/cases/matrix.rs b/main/opengeometry/tests/cases/matrix.rs index d9c3eeec..01a5c433 100644 --- a/main/opengeometry/tests/cases/matrix.rs +++ b/main/opengeometry/tests/cases/matrix.rs @@ -3,6 +3,7 @@ use opengeometry::operations::modifying::boolean::{ boolean_brep_outcome_with_handlers, boolean_brep_with_handlers, BooleanOp, }; use opengeometry::primitives; +use opengeometry::query::validate_orientation; use opengeometry_test_support::{stored, volume}; use serde_json::json; @@ -56,6 +57,7 @@ fn boolean_fixtures_match_stored_brep_and_handlers() { ($name:literal, $a:expr, $b:expr, $operation:expr) => {{ let (result, handlers) = boolean_brep_with_handlers($a, $b, $operation, $name.into()).unwrap(); + validate_orientation(&result.brep).unwrap(); let json = result.brep.to_json().unwrap(); if stored::compared() { assert_eq!( @@ -110,7 +112,7 @@ fn boolean_support_matrix_matches_stored_outcomes_and_handlers() { boolean_brep_outcome_with_handlers(&a, &b, operation, format!("matrix-{name}")); let actual = match result { Ok(result) => { - result.brep.validate().unwrap(); + validate_orientation(&result.brep).unwrap(); let expected_volume = match name { "box-union" => Some(27.0), "box-intersection" => Some(8.0), From 02c2a60bdb3b86bafdc61cf0d63154afff88c01b Mon Sep 17 00:00:00 2001 From: Bas <16050771+Basnederveen@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:25:25 +0100 Subject: [PATCH 2/2] fix(query): validate_orientation per review; validity() unchanged - 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 --- .../modifying/boolean/tests/generic.rs | 17 +- main/opengeometry/src/query/measure.rs | 6 +- main/opengeometry/src/query/orientation.rs | 169 +++++++++--------- main/opengeometry/src/query/tests/mod.rs | 77 ++++++-- 4 files changed, 163 insertions(+), 106 deletions(-) diff --git a/main/opengeometry/src/operations/modifying/boolean/tests/generic.rs b/main/opengeometry/src/operations/modifying/boolean/tests/generic.rs index c2b761eb..bb608623 100644 --- a/main/opengeometry/src/operations/modifying/boolean/tests/generic.rs +++ b/main/opengeometry/src/operations/modifying/boolean/tests/generic.rs @@ -1,8 +1,9 @@ use super::support::{cylinder, sphere}; -use crate::brep::{Accuracy, CurveGeometry, FaceRole, Frame3}; +use crate::brep::{Accuracy, BrepEnvelope, CurveGeometry, FaceRole, Frame3, GeometryError}; use crate::operations::modifying::boolean::dispatch::boolean_brep; use crate::operations::modifying::boolean::types::BooleanOp; use crate::primitives; +use crate::query::validate_orientation; use crate::tessellation::tessellate; #[test] @@ -75,6 +76,15 @@ fn provably_disjoint_surface_families_use_exact_regularized_results() { ); } +fn assert_known_inversion(body: &BrepEnvelope, key: &str) { + match validate_orientation(body) { + Err(GeometryError::InvalidTopology(message)) => { + assert!(message.contains(&format!("'{key}'")), "{message}") + } + other => panic!("'{key}' is no longer reported inverted: {other:?}"), + } +} + #[test] fn generic_closed_loop_imprints_an_off_axis_sphere_cone_intersection() { let accuracy = cylinder("accuracy", 0.0, 1.0, false).accuracy; @@ -115,6 +125,10 @@ fn generic_closed_loop_imprints_an_off_axis_sphere_cone_intersection() { .iter() .all(|face| !face.provenance.sources.is_empty())); result.brep.validate().unwrap(); + match operation { + BooleanOp::Intersection => validate_orientation(&result.brep).unwrap(), + _ => assert_known_inversion(&result.brep, "cone:lateral"), + } tessellate(&result.brep, 0.01, 2_000_000) .unwrap_or_else(|error| panic!("{operation:?} tessellation: {error}")); } @@ -136,6 +150,7 @@ fn generic_closed_loop_handles_periodic_winding() { .unwrap(); let result = boolean_brep(&cone, &sphere, BooleanOp::Intersection, "winding".into()).unwrap(); result.brep.validate().unwrap(); + assert_known_inversion(&result.brep, "lateral:intersection-patch"); tessellate(&result.brep, 0.01, 2_000_000).unwrap(); } diff --git a/main/opengeometry/src/query/measure.rs b/main/opengeometry/src/query/measure.rs index 9ba83795..a5101c23 100644 --- a/main/opengeometry/src/query/measure.rs +++ b/main/opengeometry/src/query/measure.rs @@ -1,4 +1,3 @@ -use super::orientation::validate_orientation_validated; use crate::brep::{BodyType, BrepEnvelope, GeometryError}; #[derive(Clone, Debug, PartialEq)] @@ -10,10 +9,7 @@ pub struct Validity { } pub fn validity(body: &BrepEnvelope) -> Validity { - let checked = body - .body_type() - .and_then(|body_type| validate_orientation_validated(body).map(|()| body_type)); - match checked { + match body.body_type() { Ok(body_type) => Validity { valid: true, closed: closed(body, body_type), diff --git a/main/opengeometry/src/query/orientation.rs b/main/opengeometry/src/query/orientation.rs index e3ae788c..6d7fffcc 100644 --- a/main/opengeometry/src/query/orientation.rs +++ b/main/opengeometry/src/query/orientation.rs @@ -1,114 +1,109 @@ use super::classification::PointClassification; -use super::classify::classify_point_validated; -use super::face_trim::face_contains_uv; +use super::classify::Classifier; +use super::face_trim::FaceTrim; use crate::brep::{unit, BrepEnvelope, Face, GeometryError, Surface, UV}; -use crate::math::{add, norm, scale, sub}; +use crate::math::{add, scale, sub, Point3}; -const GRID: usize = 6; +const GRIDS: [usize; 2] = [6, 24]; const PROBE_OFFSET: f64 = 100.0; -const RELIABLE_TOLERANCE: f64 = 1e-12; +const SPACINGS_PER_OFFSET: f64 = 256.0; pub fn validate_orientation(brep: &BrepEnvelope) -> Result<(), GeometryError> { brep.validate()?; - validate_orientation_validated(brep) -} - -pub(super) fn validate_orientation_validated(brep: &BrepEnvelope) -> Result<(), GeometryError> { - if brep.solids.is_empty() { - return Ok(()); - } - match check_faces(brep) { - Err(GeometryError::Math(_)) => Ok(()), - outcome => outcome, - } -} - -fn check_faces(brep: &BrepEnvelope) -> Result<(), GeometryError> { - let Some(bounds) = brep.bounds_unchecked()? else { - return Ok(()); - }; - let diagonal = norm(std::array::from_fn(|axis| bounds.axes[axis].width())); - let coordinates = bounds - .axes + let offset = PROBE_OFFSET * brep.accuracy.geometric; + let frames_resolve = brep + .geometry + .surfaces .iter() - .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 { + .all(|surface| resolves(offset, surface.frame().origin)); + if brep.solids.is_empty() || !frames_resolve { return Ok(()); } - for face in &brep.topology.faces { - if face_is_inverted(brep, face)? { - return Err(GeometryError::InvalidTopology(format!( - "face {} ('{}') has its material normal pointing into the solid", - face.id, face.key - ))); - } + let classifier = Classifier::new(brep); + match brep + .topology + .faces + .iter() + .find(|face| face_is_inverted(brep, &classifier, face)) + { + Some(face) => Err(GeometryError::InvalidTopology(format!( + "face {} ('{}') has its material normal pointing into the solid", + face.id, face.key + ))), + None => Ok(()), } - Ok(()) } -fn face_is_inverted(brep: &BrepEnvelope, face: &Face) -> Result { - let [first, second] = probe_points(brep, face)?; - let Some(first) = first else { - return Ok(false); - }; - if probe(brep, face, first)? != Some(true) { - return Ok(false); - } - let Some(second) = second else { - return Ok(true); +fn face_is_inverted(brep: &BrepEnvelope, classifier: &Classifier, face: &Face) -> bool { + let Ok(trim) = FaceTrim::new(brep, face) else { + return false; }; - Ok(probe(brep, face, second)? == Some(true)) + probe_points(brep, face, &trim).is_some_and(|points| { + points + .into_iter() + .all(|uv| probe(brep, classifier, face, uv) == Some(true)) + }) } -fn probe(brep: &BrepEnvelope, face: &Face, uv: UV) -> Result, GeometryError> { - let surface = brep.geometry.surface(face.surface)?; - let Ok(normal) = surface.normal_at(uv) else { - return Ok(None); - }; - let step = scale( - unit(normal)?, - 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))?; - let back = classify_point_validated(brep, sub(on_face, step))?; - Ok(match (front, back) { +fn probe(brep: &BrepEnvelope, classifier: &Classifier, face: &Face, uv: UV) -> Option { + let surface = brep.geometry.surface(face.surface).ok()?; + let normal = unit(surface.normal_at(uv).ok()?).ok()?; + let on_face = surface.point_at(uv).ok()?; + let offset = PROBE_OFFSET * brep.accuracy.geometric; + if !resolves(offset, on_face) { + return None; + } + let step = scale(normal, face.sense.multiplier() * offset); + let front = classifier.classify(add(on_face, step)).ok()?; + let back = classifier.classify(sub(on_face, step)).ok()?; + match (front, back) { (PointClassification::Inside, PointClassification::Outside) => Some(true), (PointClassification::Outside, PointClassification::Inside) => Some(false), _ => None, - }) + } } -fn probe_points(brep: &BrepEnvelope, face: &Face) -> Result<[Option; 2], GeometryError> { - let cell = - |i: usize, j: usize| -> UV { [i, j].map(|index| (index as f64 + 0.5) / GRID as f64) }; - let at = |fraction: UV| -> UV { - std::array::from_fn(|axis| { - let bounds = face.trim.uv_bounds[axis]; - bounds.lo + bounds.width() * fraction[axis] - }) - }; - let mut inside = [[false; GRID]; GRID]; - for (i, row) in inside.iter_mut().enumerate() { - for (j, value) in row.iter_mut().enumerate() { - *value = face_contains_uv(brep, face, at(cell(i, j)))? == Some(true); +fn resolves(offset: f64, at: Point3) -> bool { + let reach = at + .iter() + .fold(0.0_f64, |reach, value| reach.max(value.abs())); + offset > SPACINGS_PER_OFFSET * f64::EPSILON * reach +} + +fn probe_points(brep: &BrepEnvelope, face: &Face, trim: &FaceTrim) -> Option<[UV; 2]> { + GRIDS.into_iter().find_map(|grid| { + let at = |i: usize, j: usize| -> UV { + std::array::from_fn(|axis| { + let bounds = face.trim.uv_bounds[axis]; + let index = [i, j][axis] as f64; + bounds.lo + bounds.width() * (index + 0.5) / grid as f64 + }) + }; + let inside: Vec> = (0..grid) + .map(|i| { + (0..grid) + .map(|j| trim.contains(brep, face, at(i, j)) == Ok(Some(true))) + .collect() + }) + .collect(); + let mut ranked: Vec<(usize, usize, usize)> = (0..grid * grid) + .map(|index| (index / grid, index % grid)) + .filter(|&(i, j)| inside[i][j]) + .map(|(i, j)| (depth(&inside, i, j), i, j)) + .collect(); + ranked.sort_by_key(|&(depth, i, j)| (std::cmp::Reverse(depth), i, j)); + match ranked[..] { + [(_, i, j), (_, k, l), ..] => Some([at(i, j), at(k, l)]), + _ => None, } - } - let mut ranked: Vec<(usize, usize, usize)> = (0..GRID * GRID) - .map(|index| (index / GRID, index % GRID)) - .filter(|&(i, j)| inside[i][j]) - .map(|(i, j)| (depth(&inside, i, j), i, j)) - .collect(); - ranked.sort_by_key(|&(depth, i, j)| (std::cmp::Reverse(depth), i, j)); - Ok([0, 1].map(|rank| ranked.get(rank).map(|&(_, i, j)| at(cell(i, j))))) + }) } -fn depth(inside: &[[bool; GRID]; GRID], i: usize, j: usize) -> usize { - let border = [i + 1, j + 1, GRID - i, GRID - j].into_iter().min(); - let outside = (0..GRID * GRID) - .map(|index| (index / GRID, index % GRID)) +fn depth(inside: &[Vec], i: usize, j: usize) -> usize { + let grid = inside.len(); + let border = [i + 1, j + 1, grid - i, grid - j].into_iter().min(); + let outside = (0..grid * grid) + .map(|index| (index / grid, index % grid)) .filter(|&(k, l)| !inside[k][l]) .map(|(k, l)| i.abs_diff(k).max(j.abs_diff(l))); border.into_iter().chain(outside).min().unwrap_or(0) diff --git a/main/opengeometry/src/query/tests/mod.rs b/main/opengeometry/src/query/tests/mod.rs index 5f862fa2..2bf79368 100644 --- a/main/opengeometry/src/query/tests/mod.rs +++ b/main/opengeometry/src/query/tests/mod.rs @@ -1,10 +1,11 @@ use super::classification::PointClassification; use super::classify::{classify_point, classify_point_validated}; -use super::{validate_orientation, validity}; +use super::validate_orientation; use crate::brep::{ coarse_accuracy, fine_accuracy, unit, Accuracy, BrepEnvelope, Frame3, GeometryError, }; use crate::math::{add, cross, dot, scale, Point3}; +use crate::operations::modifying::boolean::{boolean_brep, BooleanOp}; use crate::primitives; mod face_trim; @@ -446,22 +447,34 @@ fn assert_inverted(body: &BrepEnvelope, face: u32) { } other => panic!("face {face} not reported: {other:?}"), } - assert!(!validity(body).valid); +} + +fn flipped(body: &BrepEnvelope, faces: impl IntoIterator) -> BrepEnvelope { + let mut body = body.clone(); + for face in faces { + let face = &mut body.topology.faces[face as usize]; + face.sense = face.sense.reversed(); + } + body +} + +fn cube_at(id: &str, origin: Point3, size: [f64; 3], accuracy: Accuracy) -> BrepEnvelope { + let frame = Frame3 { + origin, + ..Frame3::IDENTITY + }; + primitives::cuboid(id.into(), frame, size, accuracy).unwrap() } #[test] fn a_face_whose_material_normal_points_into_the_solid_is_rejected() { let accuracy = coarse_accuracy(1e-5); - let mut cube = primitives::cuboid("cube".into(), Frame3::IDENTITY, [1.0; 3], accuracy).unwrap(); + let cube = cube_at("cube", [0.0; 3], [1.0; 3], accuracy); validate_orientation(&cube).unwrap(); - cube.topology.faces[5].sense = cube.topology.faces[5].sense.reversed(); - assert_inverted(&cube, 5); + assert_inverted(&flipped(&cube, [5]), 5); - let mut sphere = primitives::sphere("ball".into(), Frame3::IDENTITY, 1.0, accuracy).unwrap(); - for face in &mut sphere.topology.faces { - face.sense = face.sense.reversed(); - } - assert_inverted(&sphere, 0); + let sphere = primitives::sphere("ball".into(), Frame3::IDENTITY, 1.0, accuracy).unwrap(); + assert_inverted(&flipped(&sphere, 0..sphere.topology.faces.len() as u32), 0); } #[test] @@ -479,12 +492,43 @@ fn reversed_bores_thin_plates_and_curved_solids_pass_the_orientation_check() { primitives::cone("cone".into(), Frame3::IDENTITY, 1.0, 2.0, accuracy), ]; for body in bodies { - let body = body.unwrap(); - validate_orientation(&body).unwrap(); - assert!(validity(&body).valid, "{}", body.id); + validate_orientation(&body.unwrap()).unwrap(); } } +#[test] +fn a_flipped_face_far_from_the_origin_is_rejected() { + for origin in [[1e4, 2e4, 3e4], [2e5, -3e5, 1e5], [6.5e5, 5.8e6, 2.0e2]] { + let cube = cube_at("site", origin, [3.0, 2.0, 1.0], Accuracy::STANDARD); + validate_orientation(&cube).unwrap(); + assert_inverted(&flipped(&cube, [2]), 2); + } +} + +#[test] +fn a_flipped_face_in_a_cavity_shell_or_a_second_solid_is_rejected() { + let accuracy = coarse_accuracy(1e-5); + let host = cube_at("host", [0.0; 3], [3.0; 3], accuracy); + let pocket = cube_at("pocket", [1.0; 3], [1.0; 3], accuracy); + let cavity = boolean_brep(&host, &pocket, BooleanOp::Subtraction, "cavity".into()) + .unwrap() + .brep; + let shell = cavity.solids[0].cavity_shells[0]; + let inner = cavity.topology.shells[shell as usize].faces[0]; + validate_orientation(&cavity).unwrap(); + assert_inverted(&flipped(&cavity, [inner]), inner); + + let blade = cube_at("blade", [1.0, -1.0, -1.0], [1.0, 5.0, 5.0], accuracy); + let split = boolean_brep(&host, &blade, BooleanOp::Subtraction, "split".into()) + .unwrap() + .brep; + assert_eq!(split.solids.len(), 2); + let shell = split.solids[1].outer_shell; + let second = split.topology.shells[shell as usize].faces[0]; + validate_orientation(&split).unwrap(); + assert_inverted(&flipped(&split, [second]), second); +} + #[test] fn the_orientation_check_stands_aside_where_classification_is_unreliable() { let near_cylinder = primitives::frustum( @@ -507,4 +551,11 @@ fn the_orientation_check_stands_aside_where_classification_is_unreliable() { primitives::sphere("extreme".into(), Frame3::IDENTITY, radius, accuracy).unwrap(); validate_orientation(&sphere).unwrap(); } + let beyond = cube_at( + "beyond", + [3e7, 1e7, 0.0], + [3.0, 2.0, 1.0], + Accuracy::STANDARD, + ); + validate_orientation(&flipped(&beyond, [2])).unwrap(); }