Repository navigation
fix(query): validate_orientation catches faces whose material normal points into the solid #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Basnederveen
wants to merge
4
commits into
OpenGeometry-io:main
Choose a base branch
from
Bas-Automation-Services:fix/validate-face-orientation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
062d51d
fix(query): validate_orientation catches faces whose material normal …
Basnederveen 6c53592
Merge upstream main into fix/validate-face-orientation
Basnederveen 93076a1
Merge upstream main into fix/validate-face-orientation
Basnederveen 02c2a60
fix(query): validate_orientation per review; validity() unchanged
Basnederveen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| use super::classification::PointClassification; | ||
| use super::classify::Classifier; | ||
| use super::face_trim::FaceTrim; | ||
| use crate::brep::{unit, BrepEnvelope, Face, GeometryError, Surface, UV}; | ||
| use crate::math::{add, scale, sub, Point3}; | ||
|
|
||
| const GRIDS: [usize; 2] = [6, 24]; | ||
| const PROBE_OFFSET: f64 = 100.0; | ||
| const SPACINGS_PER_OFFSET: f64 = 256.0; | ||
|
|
||
| pub fn validate_orientation(brep: &BrepEnvelope) -> Result<(), GeometryError> { | ||
| brep.validate()?; | ||
| let offset = PROBE_OFFSET * brep.accuracy.geometric; | ||
| let frames_resolve = brep | ||
| .geometry | ||
| .surfaces | ||
| .iter() | ||
| .all(|surface| resolves(offset, surface.frame().origin)); | ||
| if brep.solids.is_empty() || !frames_resolve { | ||
| return Ok(()); | ||
| } | ||
| 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(()), | ||
| } | ||
| } | ||
|
|
||
| fn face_is_inverted(brep: &BrepEnvelope, classifier: &Classifier, face: &Face) -> bool { | ||
| let Ok(trim) = FaceTrim::new(brep, face) else { | ||
| return false; | ||
| }; | ||
| 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, classifier: &Classifier, face: &Face, uv: UV) -> Option<bool> { | ||
| 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 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<Vec<bool>> = (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, | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| fn depth(inside: &[Vec<bool>], 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 02c2a60:
a_flipped_face_far_from_the_origin_is_rejected: a cuboid at (1e4, 2e4, 3e4), (2e5, −3e5, 1e5) and (6.5e5, 5.8e6, 200) atAccuracy::STANDARD. Each one passes, and with one face flipped it is reported.a_flipped_face_in_a_cavity_shell_or_a_second_solid_is_rejected: a cube with a cubic cavity (subtraction) and a cube split into two solids. Both pass. A flipped face in the cavity shell, or in the second solid's outer shell, is reported.the_orientation_check_stands_aside_where_classification_is_unreliablenow also checks that a flipped cuboid at 3e7 is not reported.boolean/tests/generic.rs. The off-axis sphere–cone union and subtraction are reported atcone:lateral, while the intersection passes. The periodic-winding intersection is reported atlateral:intersection-patch. fix(booleans): closed-loop booleans on spheres, cones and cylinders that came out inside-out or wrong #41 fixes all three: the intersection then builds a valid result, and the union and subtraction are refused with a seamCoverageGap. Whichever of fix(query): validate_orientation catches faces whose material normal points into the solid #39 and fix(booleans): closed-loop booleans on spheres, cones and cylinders that came out inside-out or wrong #41 lands second has to drop these pins, and fix(booleans): closed-loop booleans on spheres, cones and cylinders that came out inside-out or wrong #41 also moves these tests totests/closed_loop_windings.rs.