From be94ba164198de80db9eaf90e2ecab641397b73f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 12 Oct 2022 15:38:34 +0200 Subject: [PATCH 1/6] Refactor --- crates/fj-kernel/src/partial/objects/edge.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index eccdc378a..31b4385d7 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -65,8 +65,9 @@ impl PartialHalfEdge { mut self, vertices: Option<[impl Into>; 2]>, ) -> Self { + let vertices = vertices.map(|vertices| vertices.map(Into::into)); if let Some(vertices) = vertices { - self.vertices = Some(vertices.map(Into::into)); + self.vertices = Some(vertices); } self } From d5efbd18025d80a5e936e0c93f35e6ba2da9c4af Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 12 Oct 2022 15:40:05 +0200 Subject: [PATCH 2/6] Refactor --- crates/fj-kernel/src/partial/objects/edge.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index 31b4385d7..0ce110e0d 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -107,11 +107,12 @@ impl PartialHalfEdge { .with_position(Some(point_curve)) .with_curve(Some(curve.clone())) .with_surface_form(Some(surface_form.clone())) + .into() }) }; self.curve = Some(curve.into()); - self.vertices = Some(vertices.map(Into::into)); + self.vertices = Some(vertices); self } From c2a28e0644027cb6295b1db25f040a5d2307cf73 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 12 Oct 2022 15:46:10 +0200 Subject: [PATCH 3/6] Shorten panic messages --- crates/fj-kernel/src/partial/objects/cycle.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/fj-kernel/src/partial/objects/cycle.rs b/crates/fj-kernel/src/partial/objects/cycle.rs index 41c84963b..be37c6a04 100644 --- a/crates/fj-kernel/src/partial/objects/cycle.rs +++ b/crates/fj-kernel/src/partial/objects/cycle.rs @@ -48,17 +48,17 @@ impl PartialCycle { .half_edges .last() .map(|half_edge| { - let [_, last] = half_edge.vertices().expect( - "Need half-edge vertices to extend cycle with poly-chain", - ); - let last = last.surface_form().expect( - "Need surface vertex to extend cycle with poly-chain", - ); + let [_, last] = half_edge + .vertices() + .expect("Need half-edge vertices to extend cycle"); + let last = last + .surface_form() + .expect("Need surface vertex to extend cycle"); let vertex = last.clone(); - let position = last.position().expect( - "Need surface position to extend cycle with poly-chain", - ); + let position = last + .position() + .expect("Need surface position to extend cycle"); (position, Some(vertex)) }) From 67d351cea077fd4845f2a8e57d1cab28f85fcc3b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 12 Oct 2022 15:47:18 +0200 Subject: [PATCH 4/6] Make method return type more flexible This prepares for an upcoming change. --- crates/fj-kernel/src/partial/maybe_partial.rs | 12 +++++++++--- crates/fj-kernel/src/partial/objects/cycle.rs | 10 ++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/crates/fj-kernel/src/partial/maybe_partial.rs b/crates/fj-kernel/src/partial/maybe_partial.rs index b931e11dd..9b968b747 100644 --- a/crates/fj-kernel/src/partial/maybe_partial.rs +++ b/crates/fj-kernel/src/partial/maybe_partial.rs @@ -106,10 +106,16 @@ impl MaybePartial { impl MaybePartial { /// Access the vertices - pub fn vertices(&self) -> Option<[MaybePartial; 2]> { + pub fn vertices(&self) -> [Option>; 2] { match self { - Self::Full(full) => Some(full.vertices().clone().map(Into::into)), - Self::Partial(partial) => partial.vertices.clone(), + Self::Full(full) => { + full.vertices().clone().map(|vertex| Some(vertex.into())) + } + Self::Partial(partial) => partial + .vertices + .clone() + .map(|vertices| vertices.map(Some)) + .unwrap_or([None, None]), } } } diff --git a/crates/fj-kernel/src/partial/objects/cycle.rs b/crates/fj-kernel/src/partial/objects/cycle.rs index be37c6a04..39b7dfbd8 100644 --- a/crates/fj-kernel/src/partial/objects/cycle.rs +++ b/crates/fj-kernel/src/partial/objects/cycle.rs @@ -48,9 +48,9 @@ impl PartialCycle { .half_edges .last() .map(|half_edge| { - let [_, last] = half_edge - .vertices() - .expect("Need half-edge vertices to extend cycle"); + let [_, last] = half_edge.vertices().map(|vertex| { + vertex.expect("Need half-edge vertices to extend cycle") + }); let last = last .surface_form() .expect("Need surface vertex to extend cycle"); @@ -129,7 +129,9 @@ impl PartialCycle { let vertices = [first, last].map(|option| { option.map(|half_edge| { - half_edge.vertices().expect("Need vertices to close cycle") + half_edge + .vertices() + .map(|vertex| vertex.expect("Need vertices to close cycle")) }) }); From 41317b13beb88c6baccdf81f8364bbbf2021687a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 12 Oct 2022 15:48:05 +0200 Subject: [PATCH 5/6] Make struct field type more flexible --- crates/fj-kernel/src/partial/maybe_partial.rs | 6 +-- crates/fj-kernel/src/partial/objects/edge.rs | 41 +++++++++---------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/crates/fj-kernel/src/partial/maybe_partial.rs b/crates/fj-kernel/src/partial/maybe_partial.rs index 9b968b747..19a2c31f4 100644 --- a/crates/fj-kernel/src/partial/maybe_partial.rs +++ b/crates/fj-kernel/src/partial/maybe_partial.rs @@ -111,11 +111,7 @@ impl MaybePartial { Self::Full(full) => { full.vertices().clone().map(|vertex| Some(vertex.into())) } - Self::Partial(partial) => partial - .vertices - .clone() - .map(|vertices| vertices.map(Some)) - .unwrap_or([None, None]), + Self::Partial(partial) => partial.vertices.clone(), } } } diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index 0ce110e0d..e8048ce2f 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -21,7 +21,7 @@ pub struct PartialHalfEdge { pub curve: Option>>, /// The vertices that bound this [`HalfEdge`] in the [`Curve`] - pub vertices: Option<[MaybePartial; 2]>, + pub vertices: [Option>; 2], /// The global form of the [`HalfEdge`] /// @@ -66,8 +66,8 @@ impl PartialHalfEdge { vertices: Option<[impl Into>; 2]>, ) -> Self { let vertices = vertices.map(|vertices| vertices.map(Into::into)); - if let Some(vertices) = vertices { - self.vertices = Some(vertices); + if let Some([a, b]) = vertices { + self.vertices = [Some(a), Some(b)]; } self } @@ -90,7 +90,7 @@ impl PartialHalfEdge { .with_surface(self.surface.clone()) .as_circle_from_radius(radius); - let vertices = { + let [a, b] = { let [a_curve, b_curve] = [Scalar::ZERO, Scalar::TAU].map(|coord| Point::from([coord])); @@ -112,7 +112,7 @@ impl PartialHalfEdge { }; self.curve = Some(curve.into()); - self.vertices = Some(vertices); + self.vertices = [Some(a), Some(b)]; self } @@ -135,10 +135,9 @@ impl PartialHalfEdge { /// Update partial half-edge as a line segment, reusing existing vertices pub fn as_line_segment(mut self) -> Self { - let [from, to] = self - .vertices - .clone() - .expect("Can't infer line segment without vertices"); + let [from, to] = self.vertices.clone().map(|vertex| { + vertex.expect("Can't infer line segment without vertices") + }); let [from_surface, to_surface] = [&from, &to].map(|vertex| { vertex .surface_form() @@ -163,7 +162,7 @@ impl PartialHalfEdge { .with_surface(Some(surface)) .as_line_from_points(points); - let vertices = [(from, 0.), (to, 1.)].map(|(vertex, position)| { + let [a, b] = [(from, 0.), (to, 1.)].map(|(vertex, position)| { vertex.update_partial(|vertex| { vertex .with_position(Some([position])) @@ -172,7 +171,7 @@ impl PartialHalfEdge { }); self.curve = Some(curve.into()); - self.vertices = Some(vertices); + self.vertices = [Some(a), Some(b)]; self } @@ -185,16 +184,12 @@ impl PartialHalfEdge { .expect("Can't build `HalfEdge` without curve") .update_partial(|curve| curve.with_surface(surface)) .into_full(objects); - let vertices = self - .vertices - .expect("Can't build `HalfEdge` without vertices") - .map(|vertex| { - vertex - .update_partial(|vertex| { - vertex.with_curve(Some(curve.clone())) - }) - .into_full(objects) - }); + let vertices = self.vertices.map(|vertex| { + vertex + .expect("Can't build `HalfEdge` without vertices") + .update_partial(|vertex| vertex.with_curve(Some(curve.clone()))) + .into_full(objects) + }); let global_form = self .global_form @@ -210,10 +205,12 @@ impl PartialHalfEdge { impl From<&HalfEdge> for PartialHalfEdge { fn from(half_edge: &HalfEdge) -> Self { + let [a, b] = half_edge.vertices().clone().map(Into::into); + Self { surface: Some(half_edge.curve().surface().clone()), curve: Some(half_edge.curve().clone().into()), - vertices: Some(half_edge.vertices().clone().map(Into::into)), + vertices: [Some(a), Some(b)], global_form: Some(half_edge.global_form().clone().into()), } } From 844d1c08a1a86d63fb0ead3e77392ba812850140 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 12 Oct 2022 15:50:31 +0200 Subject: [PATCH 6/6] Add methods to override single vertices --- crates/fj-kernel/src/partial/objects/edge.rs | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index e8048ce2f..32688a371 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -60,6 +60,30 @@ impl PartialHalfEdge { self } + /// Update the partial half-edge with the given from vertex + pub fn with_from_vertex( + mut self, + vertex: Option>>, + ) -> Self { + if let Some(vertex) = vertex { + let [from, _] = &mut self.vertices; + *from = Some(vertex.into()); + } + self + } + + /// Update the partial half-edge with the given from vertex + pub fn with_to_vertex( + mut self, + vertex: Option>>, + ) -> Self { + if let Some(vertex) = vertex { + let [_, to] = &mut self.vertices; + *to = Some(vertex.into()); + } + self + } + /// Update the partial half-edge with the given vertices pub fn with_vertices( mut self,