Skip to content
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

Clean up handling of vertices in normalized order #1181

Merged
merged 4 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions crates/fj-kernel/src/objects/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ impl HalfEdge {
the half-edge's global form"
);
assert_eq!(
&normalize_vertex_order(
&VerticesInNormalizedOrder::new(
[&a, &b].map(|vertex| vertex.global_form().clone())
),
global_form.vertices_in_normalized_order(),
global_form.vertices(),
"The global forms of a half-edge's vertices must match the \
vertices of the half-edge's global form"
);
Expand Down Expand Up @@ -104,7 +104,7 @@ impl fmt::Display for HalfEdge {
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct GlobalEdge {
curve: HandleWrapper<GlobalCurve>,
vertices: [Handle<GlobalVertex>; 2],
vertices: VerticesInNormalizedOrder,
}

impl GlobalEdge {
Expand All @@ -118,7 +118,7 @@ impl GlobalEdge {
vertices: [Handle<GlobalVertex>; 2],
) -> Self {
let curve = curve.into();
let vertices = normalize_vertex_order(vertices);
let vertices = VerticesInNormalizedOrder::new(vertices);
Self { curve, vertices }
}

Expand All @@ -137,18 +137,34 @@ impl GlobalEdge {
/// and might not match the order of the vertices that were passed to
/// [`GlobalEdge::new`]. You must not rely on the vertices being in any
/// specific order.
pub fn vertices_in_normalized_order(&self) -> &[Handle<GlobalVertex>; 2] {
pub fn vertices(&self) -> &VerticesInNormalizedOrder {
&self.vertices
}
}

fn normalize_vertex_order(
[a, b]: [Handle<GlobalVertex>; 2],
) -> [Handle<GlobalVertex>; 2] {
if a < b {
[a, b]
} else {
[b, a]
/// The vertices of a [`GlobalEdge`]
///
/// Since [`GlobalEdge`] is the single global representation of an edge in
/// global space, it must normalize the order of its vertices. Otherwise, it is
/// possible to construct two [`GlobalEdge`] instances that are meant to
/// represent the same edge, but aren't equal.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct VerticesInNormalizedOrder([Handle<GlobalVertex>; 2]);

impl VerticesInNormalizedOrder {
/// Construct a new instance of `VerticesInNormalizedOrder`
///
/// The provided vertices can be in any order.
pub fn new([a, b]: [Handle<GlobalVertex>; 2]) -> Self {
let vertices = if a < b { [a, b] } else { [b, a] };
Self(vertices)
}

/// Access the vertices
///
/// The vertices in the returned array will be in normalized order.
pub fn access_in_normalized_order(&self) -> &[Handle<GlobalVertex>; 2] {
&self.0
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod vertex;
pub use self::{
curve::{Curve, GlobalCurve},
cycle::Cycle,
edge::{GlobalEdge, HalfEdge},
edge::{GlobalEdge, HalfEdge, VerticesInNormalizedOrder},
face::{Face, Faces, Handedness},
shell::Shell,
sketch::Sketch,
Expand Down
4 changes: 3 additions & 1 deletion crates/fj-kernel/src/partial/objects/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ impl From<&GlobalEdge> for PartialGlobalEdge {
fn from(global_edge: &GlobalEdge) -> Self {
Self {
curve: Some(global_edge.curve().clone().into()),
vertices: Some(global_edge.vertices_in_normalized_order().clone()),
vertices: Some(
global_edge.vertices().access_in_normalized_order().clone(),
),
}
}
}