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

Update half-edge geometry in geometry layer in more places #2274

Merged
merged 17 commits into from
Mar 20, 2024
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
18 changes: 14 additions & 4 deletions crates/fj-core/src/operations/build/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
objects::{Curve, Face, HalfEdge, Shell, Surface, Vertex},
operations::{
build::{BuildFace, BuildHalfEdge, BuildSurface, Polygon},
geometry::UpdateHalfEdgeGeometry,
insert::{Insert, IsInserted, IsInsertedNo, IsInsertedYes},
join::JoinCycle,
reverse::ReverseCurveCoordinateSystems,
Expand Down Expand Up @@ -91,13 +92,22 @@ pub trait BuildShell {
.zip_ext([[a, b], [b, c], [c, a]])
.zip_ext(curves_and_boundaries)
.map(|((vertex, positions), (curve, boundary))| {
HalfEdge::line_segment(
let half_edge = HalfEdge::line_segment(
positions,
Some(boundary.reverse().inner),
core,
)
.update_start_vertex(|_, _| vertex, core)
.update_curve(|_, _| curve, core)
);
half_edge
.update_start_vertex(|_, _| vertex, core)
.update_curve(|_, _| curve, core)
.insert(core)
.set_path(
core.layers
.geometry
.of_half_edge(&half_edge)
.path,
&mut core.layers.geometry,
)
})
};

Expand Down
23 changes: 20 additions & 3 deletions crates/fj-core/src/operations/geometry/half_edge.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use fj_math::Point;

use crate::{
geometry::{CurveBoundary, HalfEdgeGeometry, SurfacePath},
geometry::{CurveBoundary, Geometry, HalfEdgeGeometry, SurfacePath},
layers::Layer,
objects::HalfEdge,
operations::insert::Insert,
storage::Handle,
Expand All @@ -10,15 +11,22 @@ use crate::{

/// Update the geometry of a [`HalfEdge`]
pub trait UpdateHalfEdgeGeometry {
/// Update the path of the edge
/// Set the path of the half-edge
fn set_path(
self,
path: SurfacePath,
geometry: &mut Layer<Geometry>,
) -> Self;

/// Update the path of the half-edge
#[must_use]
fn update_path(
&self,
update: impl FnOnce(SurfacePath) -> SurfacePath,
core: &mut Core,
) -> Self;

/// Update the boundary of the edge
/// Update the boundary of the half-edge
#[must_use]
fn update_boundary(
&self,
Expand All @@ -28,6 +36,15 @@ pub trait UpdateHalfEdgeGeometry {
}

impl UpdateHalfEdgeGeometry for Handle<HalfEdge> {
fn set_path(
self,
path: SurfacePath,
geometry: &mut Layer<Geometry>,
) -> Self {
geometry.define_half_edge(self.clone(), HalfEdgeGeometry { path });
self
}

fn update_path(
&self,
update: impl FnOnce(SurfacePath) -> SurfacePath,
Expand Down
32 changes: 25 additions & 7 deletions crates/fj-core/src/operations/join/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
objects::{Cycle, HalfEdge},
operations::{
build::BuildHalfEdge,
geometry::UpdateHalfEdgeGeometry,
insert::Insert,
update::{UpdateCycle, UpdateHalfEdge},
},
Expand Down Expand Up @@ -129,8 +130,8 @@ impl JoinCycle for Cycle {
cycle
.update_half_edge(
self.half_edges().nth_circular(index),
|edge, core| {
[edge
|half_edge, core| {
[half_edge
.update_curve(
|_, _| edge_other.curve().clone(),
core,
Expand All @@ -144,17 +145,34 @@ impl JoinCycle for Cycle {
.clone()
},
core,
)
.insert(core)
.set_path(
core.layers
.geometry
.of_half_edge(half_edge)
.path,
&mut core.layers.geometry,
)]
},
core,
)
.update_half_edge(
self.half_edges().nth_circular(index + 1),
|edge, core| {
[edge.update_start_vertex(
|_, _| edge_other.start_vertex().clone(),
core,
)]
|half_edge, core| {
[half_edge
.update_start_vertex(
|_, _| edge_other.start_vertex().clone(),
core,
)
.insert(core)
.set_path(
core.layers
.geometry
.of_half_edge(half_edge)
.path,
&mut core.layers.geometry,
)]
},
core,
)
Expand Down
17 changes: 14 additions & 3 deletions crates/fj-core/src/operations/split/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use fj_math::Point;
use crate::{
objects::{HalfEdge, Shell},
operations::{
derive::DeriveFrom, insert::Insert, replace::ReplaceHalfEdge,
split::SplitHalfEdge, update::UpdateHalfEdge,
derive::DeriveFrom, geometry::UpdateHalfEdgeGeometry, insert::Insert,
replace::ReplaceHalfEdge, split::SplitHalfEdge, update::UpdateHalfEdge,
},
queries::SiblingOfHalfEdge,
storage::Handle,
Expand Down Expand Up @@ -55,10 +55,21 @@ impl SplitEdge for Shell {
)
.insert(core);
[sibling_a, sibling_b].map(|half_edge| {
half_edge.insert(core).derive_from(&sibling, core)
half_edge.insert(core).derive_from(&sibling, core).set_path(
core.layers.geometry.of_half_edge(&sibling).path,
&mut core.layers.geometry,
)
})
};

let [half_edge_a, half_edge_b] =
[half_edge_a, half_edge_b].map(|half_edge_part| {
half_edge_part.set_path(
core.layers.geometry.of_half_edge(half_edge).path,
&mut core.layers.geometry,
)
});

let shell = self
.replace_half_edge(
half_edge,
Expand Down
22 changes: 15 additions & 7 deletions crates/fj-core/src/operations/split/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
operations::{
build::{BuildCycle, BuildHalfEdge},
derive::DeriveFrom,
geometry::UpdateHalfEdgeGeometry,
insert::Insert,
split::SplitEdge,
update::{
Expand Down Expand Up @@ -101,13 +102,20 @@ impl SplitFace for Shell {
.expect("Updated shell must contain updated face");

// Build the edge that's going to divide the new faces.
let dividing_half_edge_a_to_d = HalfEdge::line_segment(
[b.start_position(), d.start_position()],
None,
core,
)
.update_start_vertex(|_, _| b.start_vertex().clone(), core)
.insert(core);
let dividing_half_edge_a_to_d = {
let half_edge = HalfEdge::line_segment(
[b.start_position(), d.start_position()],
None,
core,
);
half_edge
.update_start_vertex(|_, _| b.start_vertex().clone(), core)
.insert(core)
.set_path(
core.layers.geometry.of_half_edge(&half_edge).path,
&mut core.layers.geometry,
)
};
let dividing_half_edge_c_to_b = HalfEdge::from_sibling(
&dividing_half_edge_a_to_d,
d.start_vertex().clone(),
Expand Down
25 changes: 15 additions & 10 deletions crates/fj-core/src/operations/sweep/half_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
objects::{Cycle, Face, HalfEdge, Region, Vertex},
operations::{
build::{BuildCycle, BuildHalfEdge},
geometry::UpdateHalfEdgeGeometry,
insert::Insert,
presentation::SetColor,
update::{UpdateCycle, UpdateHalfEdge},
Expand Down Expand Up @@ -113,26 +114,30 @@ impl SweepHalfEdge for HalfEdge {
.zip_ext(vertices)
.zip_ext(curves)
.map(|((((boundary, start), end), start_vertex), curve)| {
let edge = {
let edge = HalfEdge::line_segment(
let half_edge = {
let line_segment = HalfEdge::line_segment(
[start, end],
Some(boundary),
core,
)
.update_start_vertex(|_, _| start_vertex, core);
);
let half_edge = line_segment
.update_start_vertex(|_, _| start_vertex, core);

let edge = if let Some(curve) = curve {
edge.update_curve(|_, _| curve, core)
let half_edge = if let Some(curve) = curve {
half_edge.update_curve(|_, _| curve, core)
} else {
edge
half_edge
};

edge.insert(core)
half_edge.insert(core).set_path(
core.layers.geometry.of_half_edge(&line_segment).path,
&mut core.layers.geometry,
)
};

exterior = exterior.add_half_edges([edge.clone()], core);
exterior = exterior.add_half_edges([half_edge.clone()], core);

edge
half_edge
});

let exterior = exterior.insert(core);
Expand Down
60 changes: 53 additions & 7 deletions crates/fj-core/src/validate/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ mod tests {
operations::{
build::BuildShell,
geometry::UpdateHalfEdgeGeometry,
insert::Insert,
update::{
UpdateCycle, UpdateFace, UpdateHalfEdge, UpdateRegion,
UpdateShell,
Expand All @@ -430,15 +431,51 @@ mod tests {
|cycle, core| {
cycle.update_half_edge(
cycle.half_edges().nth_circular(0),
|edge, core| {
[edge
|half_edge, core| {
// This is going to be weird:
//
// - That first call to `update_path` is
// going to reverse a path and insert
// a new object with the reversed
// path.
// - The next call to `update_boundary`
// relies on that, because it inserts
// an object too, and that would cause
// a validation failure without the
// first call.
// - But the new object created from
// those two operations doesn't
// actually have its geometry set in
// the geometry layer, because that
// happens in `update_path`, for an
// earlier version of the object.
// - So we need a last `set_path`, which
// sets the path again.
//
// This is very weird, but good new is,
// it's just an artifact of the
// transition from a unified object
// graph to separate topology and
// geometry layers. This should clear up
// again, once the separation is
// finished, and all APIs can adapt to
// the new reality.
[half_edge
.update_path(
|path| path.reverse(),
core,
)
.update_boundary(
|boundary| boundary.reverse(),
core,
)
.set_path(
core.layers
.geometry
.of_half_edge(half_edge)
.path
.reverse(),
&mut core.layers.geometry,
)]
},
core,
Expand Down Expand Up @@ -508,11 +545,20 @@ mod tests {
|cycle, core| {
cycle.update_half_edge(
cycle.half_edges().nth_circular(0),
|edge, core| {
[edge.update_curve(
|_, _| Curve::new(),
core,
)]
|half_edge, core| {
[half_edge
.update_curve(
|_, _| Curve::new(),
core,
)
.insert(core)
.set_path(
core.layers
.geometry
.of_half_edge(half_edge)
.path,
&mut core.layers.geometry,
)]
},
core,
)
Expand Down