-
-
Notifications
You must be signed in to change notification settings - Fork 119
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
Add Cycle validation #1686
Merged
+142
−2
Merged
Add Cycle validation #1686
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d7efec
Basic cycle validation
A-Walrus 7ab3e71
Make clippy happy
A-Walrus 20bc74b
Cleanup unwrap
A-Walrus 7d1be64
Change minimum half_edge count to 1
A-Walrus ae2c0f2
Make requested changes
A-Walrus 77bb3f5
Make clippy happy
A-Walrus f0423c6
Remove obselete `end_position` function
A-Walrus 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 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 |
---|---|---|
@@ -1,12 +1,147 @@ | ||
use crate::objects::Cycle; | ||
use crate::objects::HalfEdge; | ||
use fj_math::Point; | ||
use fj_math::Scalar; | ||
use itertools::Itertools; | ||
|
||
use super::{Validate, ValidationConfig, ValidationError}; | ||
|
||
impl Validate for Cycle { | ||
fn validate_with_config( | ||
&self, | ||
_: &ValidationConfig, | ||
_: &mut Vec<ValidationError>, | ||
config: &ValidationConfig, | ||
errors: &mut Vec<ValidationError>, | ||
) { | ||
CycleValidationError::check_half_edges_disconnected( | ||
self, config, errors, | ||
); | ||
CycleValidationError::check_enough_half_edges(self, config, errors); | ||
} | ||
} | ||
|
||
/// [`Cycle`] validation failed | ||
#[derive(Clone, Debug, thiserror::Error)] | ||
pub enum CycleValidationError { | ||
/// [`Cycle`]'s half-edges are not connected | ||
#[error( | ||
"Adjacent `HalfEdge`s are distinct\n\ | ||
- End position of first `HalfEdge`: {end_of_first:?}\n\ | ||
- Start position of second `HalfEdge`: {start_of_second:?}\n\ | ||
- `HalfEdge`s: {half_edges:#?}" | ||
)] | ||
HalfEdgesDisconnected { | ||
/// The end position of the first [`HalfEdge`] | ||
end_of_first: Point<2>, | ||
|
||
/// The start position of the second [`HalfEdge`] | ||
start_of_second: Point<2>, | ||
|
||
/// The distance between the two vertices | ||
distance: Scalar, | ||
|
||
/// The half-edge | ||
half_edges: Box<(HalfEdge, HalfEdge)>, | ||
}, | ||
#[error("Expected at least one `HalfEdge`\n")] | ||
NotEnoughHalfEdges, | ||
} | ||
|
||
impl CycleValidationError { | ||
fn check_enough_half_edges( | ||
cycle: &Cycle, | ||
_config: &ValidationConfig, | ||
errors: &mut Vec<ValidationError>, | ||
) { | ||
// If there are no half edges | ||
if cycle.half_edges().next().is_none() { | ||
errors.push(Self::NotEnoughHalfEdges.into()); | ||
} | ||
} | ||
|
||
fn check_half_edges_disconnected( | ||
cycle: &Cycle, | ||
config: &ValidationConfig, | ||
errors: &mut Vec<ValidationError>, | ||
) { | ||
for (first, second) in cycle.half_edges().circular_tuple_windows() { | ||
let end_of_first = { | ||
let [_, end] = first.boundary(); | ||
first.curve().point_from_path_coords(end) | ||
}; | ||
let start_of_second = second.start_position(); | ||
|
||
let distance = (end_of_first - start_of_second).magnitude(); | ||
|
||
if distance > config.identical_max_distance { | ||
errors.push( | ||
Self::HalfEdgesDisconnected { | ||
end_of_first, | ||
start_of_second, | ||
distance, | ||
half_edges: Box::new(( | ||
first.clone_object(), | ||
second.clone_object(), | ||
)), | ||
} | ||
.into(), | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use crate::{ | ||
builder::{CycleBuilder, HalfEdgeBuilder}, | ||
objects::Cycle, | ||
services::Services, | ||
validate::{cycle::CycleValidationError, Validate, ValidationError}, | ||
}; | ||
|
||
#[test] | ||
fn half_edges_connected() -> anyhow::Result<()> { | ||
let mut services = Services::new(); | ||
|
||
let valid = Cycle::new([]) | ||
.update_as_polygon_from_points( | ||
[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]], | ||
&mut services.objects, | ||
) | ||
.0; | ||
|
||
valid.validate_and_return_first_error()?; | ||
|
||
let disconnected = { | ||
let first = | ||
HalfEdgeBuilder::line_segment([[0., 0.], [1., 0.]], None) | ||
.build(&mut services.objects); | ||
let second = | ||
HalfEdgeBuilder::line_segment([[0., 0.], [1., 0.]], None) | ||
.build(&mut services.objects); | ||
|
||
Cycle::new([]) | ||
.add_half_edge(first, &mut services.objects) | ||
.0 | ||
.add_half_edge(second, &mut services.objects) | ||
.0 | ||
}; | ||
assert!(matches!( | ||
disconnected.validate_and_return_first_error(), | ||
Err(ValidationError::Cycle( | ||
CycleValidationError::HalfEdgesDisconnected { .. } | ||
)) | ||
)); | ||
|
||
let empty = Cycle::new([]); | ||
assert!(matches!( | ||
empty.validate_and_return_first_error(), | ||
Err(ValidationError::Cycle( | ||
CycleValidationError::NotEnoughHalfEdges | ||
)) | ||
)); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains 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
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.
Not strictly necessary, but it would be nicer to also have a test that checks for the second validation error (
NotEnoughHalfEdges
).