From c329bd8a429a6d1ffb8b37094be4a0b1d1e29135 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 12:19:32 +0200 Subject: [PATCH 01/16] Implement `Default` for `MaybePartial` --- crates/fj-kernel/src/partial/maybe_partial.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/fj-kernel/src/partial/maybe_partial.rs b/crates/fj-kernel/src/partial/maybe_partial.rs index 84052a742..d31ca522c 100644 --- a/crates/fj-kernel/src/partial/maybe_partial.rs +++ b/crates/fj-kernel/src/partial/maybe_partial.rs @@ -72,6 +72,16 @@ impl MaybePartial { } } +impl Default for MaybePartial +where + T: HasPartial, + T::Partial: Default, +{ + fn default() -> Self { + Self::Partial(T::Partial::default()) + } +} + impl From> for MaybePartial where T: HasPartial, From 301dae436911239295219a64b82e8e3e1b9eda37 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 12:20:28 +0200 Subject: [PATCH 02/16] Simplify field of `PartialGlobalVertex` --- crates/fj-kernel/src/algorithms/transform/vertex.rs | 5 +---- crates/fj-kernel/src/partial/objects/vertex.rs | 12 +++++------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform/vertex.rs b/crates/fj-kernel/src/algorithms/transform/vertex.rs index a13287695..68ac9911c 100644 --- a/crates/fj-kernel/src/algorithms/transform/vertex.rs +++ b/crates/fj-kernel/src/algorithms/transform/vertex.rs @@ -53,10 +53,7 @@ impl TransformObject for PartialSurfaceVertex { .surface .map(|surface| surface.transform(transform, objects)) .transpose()?; - let global_form = self - .global_form - .map(|global_form| global_form.transform(transform, objects)) - .transpose()?; + let global_form = self.global_form.transform(transform, objects)?; // Don't need to transform `self.position`, as that is in surface // coordinates and thus transforming the surface takes care of it. diff --git a/crates/fj-kernel/src/partial/objects/vertex.rs b/crates/fj-kernel/src/partial/objects/vertex.rs index 49c81e9be..986251b36 100644 --- a/crates/fj-kernel/src/partial/objects/vertex.rs +++ b/crates/fj-kernel/src/partial/objects/vertex.rs @@ -152,7 +152,7 @@ pub struct PartialSurfaceVertex { /// /// Can be provided, if already available, or computed from the position on /// the [`Surface`]. - pub global_form: Option>, + pub global_form: MaybePartial, } impl PartialSurfaceVertex { @@ -181,7 +181,7 @@ impl PartialSurfaceVertex { global_form: Option>>, ) -> Self { if let Some(global_form) = global_form { - self.global_form = Some(global_form.into()); + self.global_form = global_form.into(); } self } @@ -206,10 +206,8 @@ impl PartialSurfaceVertex { let global_form = self .global_form - .unwrap_or_else(|| { - GlobalVertex::partial() - .from_surface_and_position(&surface, position) - .into() + .update_partial(|global_form| { + global_form.from_surface_and_position(&surface, position) }) .into_full(objects)?; @@ -226,7 +224,7 @@ impl From<&SurfaceVertex> for PartialSurfaceVertex { Self { position: Some(surface_vertex.position()), surface: Some(surface_vertex.surface().clone()), - global_form: Some((surface_vertex.global_form().clone()).into()), + global_form: (surface_vertex.global_form().clone()).into(), } } } From 43361153b40b6b823063bd87dc9fd8d92a85b714 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:22:23 +0200 Subject: [PATCH 03/16] Simplify `PartialVertex` field --- crates/fj-kernel/src/algorithms/transform/vertex.rs | 10 +++------- crates/fj-kernel/src/partial/maybe_partial.rs | 2 +- crates/fj-kernel/src/partial/objects/vertex.rs | 9 ++++----- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform/vertex.rs b/crates/fj-kernel/src/algorithms/transform/vertex.rs index 68ac9911c..30f7d509a 100644 --- a/crates/fj-kernel/src/algorithms/transform/vertex.rs +++ b/crates/fj-kernel/src/algorithms/transform/vertex.rs @@ -20,13 +20,9 @@ impl TransformObject for PartialVertex { .transpose()?; let surface_form = self .surface_form - .map(|surface_form| -> Result<_, ValidationError> { - Ok(surface_form - .into_partial() - .transform(transform, objects)? - .into()) - }) - .transpose()?; + .into_partial() + .transform(transform, objects)? + .into(); let global_form = self .global_form .map(|global_form| global_form.transform(transform, objects)) diff --git a/crates/fj-kernel/src/partial/maybe_partial.rs b/crates/fj-kernel/src/partial/maybe_partial.rs index d31ca522c..b5a917854 100644 --- a/crates/fj-kernel/src/partial/maybe_partial.rs +++ b/crates/fj-kernel/src/partial/maybe_partial.rs @@ -185,7 +185,7 @@ impl MaybePartial { pub fn surface_form(&self) -> Option> { match self { Self::Full(full) => Some(full.surface_form().clone().into()), - Self::Partial(partial) => partial.surface_form.clone(), + Self::Partial(partial) => Some(partial.surface_form.clone()), } } } diff --git a/crates/fj-kernel/src/partial/objects/vertex.rs b/crates/fj-kernel/src/partial/objects/vertex.rs index 986251b36..8ffb8793d 100644 --- a/crates/fj-kernel/src/partial/objects/vertex.rs +++ b/crates/fj-kernel/src/partial/objects/vertex.rs @@ -2,7 +2,7 @@ use fj_math::Point; use crate::{ objects::{Curve, GlobalVertex, Objects, Surface, SurfaceVertex, Vertex}, - partial::{HasPartial, MaybePartial}, + partial::MaybePartial, storage::Handle, validate::ValidationError, }; @@ -26,7 +26,7 @@ pub struct PartialVertex { /// /// Can be provided, if already available, or computed from the position on /// the [`Curve`]. - pub surface_form: Option>, + pub surface_form: MaybePartial, /// The global form of the [`Vertex`] /// @@ -64,7 +64,7 @@ impl PartialVertex { surface_form: Option>>, ) -> Self { if let Some(surface_form) = surface_form { - self.surface_form = Some(surface_form.into()); + self.surface_form = surface_form.into(); } self } @@ -101,7 +101,6 @@ impl PartialVertex { let surface_form = self .surface_form - .unwrap_or_else(|| SurfaceVertex::partial().into()) .update_partial(|partial| { let position = partial.position.unwrap_or_else(|| { curve.path().point_from_path_coords(position) @@ -127,7 +126,7 @@ impl From<&Vertex> for PartialVertex { Self { position: Some(vertex.position()), curve: Some(vertex.curve().clone().into()), - surface_form: Some(vertex.surface_form().clone().into()), + surface_form: vertex.surface_form().clone().into(), global_form: Some((vertex.global_form().clone()).into()), } } From ddeb32cda776ec5c46efacb719450b4fd55ef2c4 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:26:52 +0200 Subject: [PATCH 04/16] Simplify method return value --- crates/fj-kernel/src/partial/maybe_partial.rs | 6 +++--- crates/fj-kernel/src/partial/objects/cycle.rs | 9 +++------ crates/fj-kernel/src/partial/objects/edge.rs | 7 ++----- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/crates/fj-kernel/src/partial/maybe_partial.rs b/crates/fj-kernel/src/partial/maybe_partial.rs index b5a917854..0f488ff60 100644 --- a/crates/fj-kernel/src/partial/maybe_partial.rs +++ b/crates/fj-kernel/src/partial/maybe_partial.rs @@ -182,10 +182,10 @@ impl MaybePartial { impl MaybePartial { /// Access the surface form - pub fn surface_form(&self) -> Option> { + pub fn surface_form(&self) -> MaybePartial { match self { - Self::Full(full) => Some(full.surface_form().clone().into()), - Self::Partial(partial) => Some(partial.surface_form.clone()), + Self::Full(full) => full.surface_form().clone().into(), + Self::Partial(partial) => partial.surface_form.clone(), } } } diff --git a/crates/fj-kernel/src/partial/objects/cycle.rs b/crates/fj-kernel/src/partial/objects/cycle.rs index 0279fe245..01a505efa 100644 --- a/crates/fj-kernel/src/partial/objects/cycle.rs +++ b/crates/fj-kernel/src/partial/objects/cycle.rs @@ -53,7 +53,6 @@ impl PartialCycle { vertex.expect("Need half-edge vertices to extend cycle") }); last.surface_form() - .expect("Need surface vertex to extend cycle") }) .into_iter() .chain(vertices); @@ -142,7 +141,6 @@ impl PartialCycle { let vertices = [last, first].map(|vertex| { vertex .surface_form() - .expect("Need surface vertex to close cycle") .position() .expect("Need surface position to close cycle") }); @@ -174,10 +172,9 @@ impl PartialCycle { .and_then(|half_edge| { half_edge.front().map(|vertex| (half_edge, vertex)) }) - .and_then(|(half_edge, vertex)| { - vertex.surface_form().map(|surface_vertex| { - (half_edge, vertex, surface_vertex) - }) + .map(|(half_edge, vertex)| { + let surface_vertex = vertex.surface_form(); + (half_edge, vertex, surface_vertex) }) .map(|(half_edge, vertex, surface_vertex)| -> Result<_, ValidationError> diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index 15f86fb99..663f19250 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -187,11 +187,8 @@ impl PartialHalfEdge { 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() - .expect("Can't infer line segment without two surface vertices") - }); + let [from_surface, to_surface] = + [&from, &to].map(|vertex| vertex.surface_form()); let surface = self .surface From 9f94d0317e8c6d5d630847c2a5d08445f3437c20 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:29:56 +0200 Subject: [PATCH 05/16] Make variable name more explicit --- crates/fj-kernel/src/partial/objects/edge.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index 663f19250..7934ef58b 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -252,8 +252,8 @@ impl PartialHalfEdge { }; vertices.zip_ext(global_forms).map(|(vertex, global_form)| { - vertex.update_partial(|partial| { - partial.with_global_form(global_form) + vertex.update_partial(|vertex| { + vertex.with_global_form(global_form) }) }) }; From af004869737a8242b43d8288df58baf724b42191 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:33:40 +0200 Subject: [PATCH 06/16] Remove redundant code --- .../src/algorithms/transform/vertex.rs | 5 ----- crates/fj-kernel/src/partial/objects/edge.rs | 6 +++++- .../fj-kernel/src/partial/objects/vertex.rs | 19 ------------------- 3 files changed, 5 insertions(+), 25 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform/vertex.rs b/crates/fj-kernel/src/algorithms/transform/vertex.rs index 30f7d509a..ca40ac0b6 100644 --- a/crates/fj-kernel/src/algorithms/transform/vertex.rs +++ b/crates/fj-kernel/src/algorithms/transform/vertex.rs @@ -23,10 +23,6 @@ impl TransformObject for PartialVertex { .into_partial() .transform(transform, objects)? .into(); - let global_form = self - .global_form - .map(|global_form| global_form.transform(transform, objects)) - .transpose()?; // Don't need to transform `self.position`, as that is in curve // coordinates and thus transforming the curve takes care of it. @@ -34,7 +30,6 @@ impl TransformObject for PartialVertex { position: self.position, curve, surface_form, - global_form, }) } } diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index 7934ef58b..f04e527aa 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -253,7 +253,11 @@ impl PartialHalfEdge { vertices.zip_ext(global_forms).map(|(vertex, global_form)| { vertex.update_partial(|vertex| { - vertex.with_global_form(global_form) + vertex.clone().with_surface_form(Some( + vertex.surface_form.update_partial(|surface_vertex| { + surface_vertex.with_global_form(global_form) + }), + )) }) }) }; diff --git a/crates/fj-kernel/src/partial/objects/vertex.rs b/crates/fj-kernel/src/partial/objects/vertex.rs index 8ffb8793d..3c6bf69ad 100644 --- a/crates/fj-kernel/src/partial/objects/vertex.rs +++ b/crates/fj-kernel/src/partial/objects/vertex.rs @@ -27,12 +27,6 @@ pub struct PartialVertex { /// Can be provided, if already available, or computed from the position on /// the [`Curve`]. pub surface_form: MaybePartial, - - /// The global form of the [`Vertex`] - /// - /// Can be provided, if already available, or acquired through the surface - /// form. - pub global_form: Option>, } impl PartialVertex { @@ -69,17 +63,6 @@ impl PartialVertex { self } - /// Provide a global form for the partial vertex - pub fn with_global_form( - mut self, - global_form: Option>>, - ) -> Self { - if let Some(global_form) = global_form { - self.global_form = Some(global_form.into()); - } - self - } - /// Build a full [`Vertex`] from the partial vertex /// /// # Panics @@ -109,7 +92,6 @@ impl PartialVertex { partial .with_position(Some(position)) .with_surface(Some(curve.surface().clone())) - .with_global_form(self.global_form) }) .into_full(objects)?; @@ -127,7 +109,6 @@ impl From<&Vertex> for PartialVertex { position: Some(vertex.position()), curve: Some(vertex.curve().clone().into()), surface_form: vertex.surface_form().clone().into(), - global_form: Some((vertex.global_form().clone()).into()), } } } From 88c7d299b8c536939ada734b1bf5623a945e0661 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:42:15 +0200 Subject: [PATCH 07/16] Simplify `PartialVertex` field --- crates/fj-kernel/src/algorithms/transform/vertex.rs | 5 +---- crates/fj-kernel/src/partial/objects/vertex.rs | 11 ++++------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform/vertex.rs b/crates/fj-kernel/src/algorithms/transform/vertex.rs index ca40ac0b6..32e11f798 100644 --- a/crates/fj-kernel/src/algorithms/transform/vertex.rs +++ b/crates/fj-kernel/src/algorithms/transform/vertex.rs @@ -14,10 +14,7 @@ impl TransformObject for PartialVertex { transform: &Transform, objects: &Objects, ) -> Result { - let curve = self - .curve - .map(|curve| curve.transform(transform, objects)) - .transpose()?; + let curve = self.curve.transform(transform, objects)?; let surface_form = self .surface_form .into_partial() diff --git a/crates/fj-kernel/src/partial/objects/vertex.rs b/crates/fj-kernel/src/partial/objects/vertex.rs index 3c6bf69ad..929a06d39 100644 --- a/crates/fj-kernel/src/partial/objects/vertex.rs +++ b/crates/fj-kernel/src/partial/objects/vertex.rs @@ -20,7 +20,7 @@ pub struct PartialVertex { /// The curve that the [`Vertex`] is defined in /// /// Must be provided before [`PartialVertex::build`] is called. - pub curve: Option>, + pub curve: MaybePartial, /// The surface form of the [`Vertex`] /// @@ -47,7 +47,7 @@ impl PartialVertex { curve: Option>>, ) -> Self { if let Some(curve) = curve { - self.curve = Some(curve.into()); + self.curve = curve.into(); } self } @@ -77,10 +77,7 @@ impl PartialVertex { let position = self .position .expect("Cant' build `Vertex` without position"); - let curve = self - .curve - .expect("Can't build `Vertex` without `Curve`") - .into_full(objects)?; + let curve = self.curve.into_full(objects)?; let surface_form = self .surface_form @@ -107,7 +104,7 @@ impl From<&Vertex> for PartialVertex { fn from(vertex: &Vertex) -> Self { Self { position: Some(vertex.position()), - curve: Some(vertex.curve().clone().into()), + curve: vertex.curve().clone().into(), surface_form: vertex.surface_form().clone().into(), } } From 635bd2817705340407e0395309ba85dfab6bb5f9 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:44:24 +0200 Subject: [PATCH 08/16] Simplify `PartialHalfEdge` field --- .../src/algorithms/transform/edge.rs | 20 ++++++++----------- crates/fj-kernel/src/partial/objects/edge.rs | 11 +++++----- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform/edge.rs b/crates/fj-kernel/src/algorithms/transform/edge.rs index 2b2df9d5e..68d7a334d 100644 --- a/crates/fj-kernel/src/algorithms/transform/edge.rs +++ b/crates/fj-kernel/src/algorithms/transform/edge.rs @@ -41,18 +41,14 @@ impl TransformObject for PartialHalfEdge { }) .transpose() })?; - let global_form = self - .global_form - .map(|global_form| -> Result<_, ValidationError> { - Ok(global_form - .into_partial() - .transform(transform, objects)? - .with_curve(curve.as_ref().and_then( - |curve: &MaybePartial| curve.global_form(), - )) - .into()) - }) - .transpose()?; + let global_form = + self.global_form + .into_partial() + .transform(transform, objects)? + .with_curve(curve.as_ref().and_then( + |curve: &MaybePartial| curve.global_form(), + )) + .into(); Ok(Self { surface, diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index f04e527aa..894dd2821 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -28,7 +28,7 @@ pub struct PartialHalfEdge { /// The global form of the [`HalfEdge`] /// /// Can be computed by [`PartialHalfEdge::build`], if not available. - pub global_form: Option>, + pub global_form: MaybePartial, } impl PartialHalfEdge { @@ -38,14 +38,14 @@ impl PartialHalfEdge { pub fn extract_global_curve(&self) -> Option> { let global_curve_from_curve = || self.curve.as_ref()?.global_form(); let global_curve_from_global_form = - || self.global_form.as_ref()?.curve().cloned(); + || self.global_form.curve().cloned(); global_curve_from_curve().or_else(global_curve_from_global_form) } /// Access the vertices of the global form, if available pub fn extract_global_vertices(&self) -> Option<[Handle; 2]> { - self.global_form.as_ref()?.vertices().cloned() + self.global_form.vertices().cloned() } /// Update the partial half-edge with the given surface @@ -109,7 +109,7 @@ impl PartialHalfEdge { global_form: Option>>, ) -> Self { if let Some(global_form) = global_form { - self.global_form = Some(global_form.into()); + self.global_form = global_form.into(); } self } @@ -288,7 +288,6 @@ impl PartialHalfEdge { let global_form = self .global_form - .unwrap_or_else(|| GlobalEdge::partial().into()) .update_partial(|partial| { partial.from_curve_and_vertices(&curve, &vertices) }) @@ -309,7 +308,7 @@ impl From<&HalfEdge> for PartialHalfEdge { surface: Some(half_edge.curve().surface().clone()), curve: Some(half_edge.curve().clone().into()), vertices: [Some(back_vertex), Some(front_vertex)], - global_form: Some(half_edge.global_form().clone().into()), + global_form: half_edge.global_form().clone().into(), } } } From 25e4025b896c6f126f62772239a6b0c286878f82 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:46:50 +0200 Subject: [PATCH 09/16] Fix variable name --- crates/fj-kernel/src/algorithms/transform/edge.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform/edge.rs b/crates/fj-kernel/src/algorithms/transform/edge.rs index 68d7a334d..bb98cc6f9 100644 --- a/crates/fj-kernel/src/algorithms/transform/edge.rs +++ b/crates/fj-kernel/src/algorithms/transform/edge.rs @@ -30,8 +30,8 @@ impl TransformObject for PartialHalfEdge { .into()) }) .transpose()?; - let vertices = self.vertices.clone().try_map_ext(|vertices| { - vertices + let vertices = self.vertices.clone().try_map_ext(|vertex| { + vertex .map(|vertex| -> Result<_, ValidationError> { Ok(vertex .into_partial() From ef4e09ac8d4f7f4cbd7e91527c9ce635ea9a82da Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:52:36 +0200 Subject: [PATCH 10/16] Simplify `PartialHalfEdge` field --- .../src/algorithms/transform/edge.rs | 20 +++++++++---------- crates/fj-kernel/src/partial/maybe_partial.rs | 6 +++--- crates/fj-kernel/src/partial/objects/cycle.rs | 8 +++----- crates/fj-kernel/src/partial/objects/edge.rs | 19 ++++++++---------- 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform/edge.rs b/crates/fj-kernel/src/algorithms/transform/edge.rs index bb98cc6f9..bd85f3d87 100644 --- a/crates/fj-kernel/src/algorithms/transform/edge.rs +++ b/crates/fj-kernel/src/algorithms/transform/edge.rs @@ -30,17 +30,15 @@ impl TransformObject for PartialHalfEdge { .into()) }) .transpose()?; - let vertices = self.vertices.clone().try_map_ext(|vertex| { - vertex - .map(|vertex| -> Result<_, ValidationError> { - Ok(vertex - .into_partial() - .transform(transform, objects)? - .with_curve(curve.clone()) - .into()) - }) - .transpose() - })?; + let vertices = self.vertices.clone().try_map_ext( + |vertex| -> Result<_, ValidationError> { + Ok(vertex + .into_partial() + .transform(transform, objects)? + .with_curve(curve.clone()) + .into()) + }, + )?; let global_form = self.global_form .into_partial() diff --git a/crates/fj-kernel/src/partial/maybe_partial.rs b/crates/fj-kernel/src/partial/maybe_partial.rs index 0f488ff60..9f2575fb6 100644 --- a/crates/fj-kernel/src/partial/maybe_partial.rs +++ b/crates/fj-kernel/src/partial/maybe_partial.rs @@ -135,7 +135,7 @@ impl MaybePartial { Self::Full(full) => Some(full.back().clone().into()), Self::Partial(partial) => { let [back, _] = &partial.vertices; - back.clone() + Some(back.clone()) } } } @@ -146,7 +146,7 @@ impl MaybePartial { Self::Full(full) => Some(full.front().clone().into()), Self::Partial(partial) => { let [_, front] = &partial.vertices; - front.clone() + Some(front.clone()) } } } @@ -157,7 +157,7 @@ impl MaybePartial { Self::Full(full) => { full.vertices().clone().map(|vertex| Some(vertex.into())) } - Self::Partial(partial) => partial.vertices.clone(), + Self::Partial(partial) => partial.vertices.clone().map(Some), } } } diff --git a/crates/fj-kernel/src/partial/objects/cycle.rs b/crates/fj-kernel/src/partial/objects/cycle.rs index 01a505efa..9d6c2b794 100644 --- a/crates/fj-kernel/src/partial/objects/cycle.rs +++ b/crates/fj-kernel/src/partial/objects/cycle.rs @@ -208,15 +208,13 @@ impl PartialCycle { let half_edge = half_edge .update_partial(|half_edge| { let [back, _] = half_edge.vertices.clone(); - let back = back.map(|vertex| { - vertex.update_partial(|partial| { - partial.with_surface_form(previous_vertex) - }) + let back = back.update_partial(|partial| { + partial.with_surface_form(previous_vertex) }); half_edge .with_surface(Some(surface_for_edges.clone())) - .with_back_vertex(back) + .with_back_vertex(Some(back)) }) .into_full(objects)?; diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index 894dd2821..2c18b5ced 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -23,7 +23,7 @@ pub struct PartialHalfEdge { pub curve: Option>, /// The vertices that bound this [`HalfEdge`] in the [`Curve`] - pub vertices: [Option>; 2], + pub vertices: [MaybePartial; 2], /// The global form of the [`HalfEdge`] /// @@ -74,7 +74,7 @@ impl PartialHalfEdge { ) -> Self { if let Some(vertex) = vertex { let [from, _] = &mut self.vertices; - *from = Some(vertex.into()); + *from = vertex.into(); } self } @@ -86,7 +86,7 @@ impl PartialHalfEdge { ) -> Self { if let Some(vertex) = vertex { let [_, to] = &mut self.vertices; - *to = Some(vertex.into()); + *to = vertex.into(); } self } @@ -98,7 +98,7 @@ impl PartialHalfEdge { ) -> Self { let vertices = vertices.map(|vertices| vertices.map(Into::into)); if let Some([back, front]) = vertices { - self.vertices = [Some(back), Some(front)]; + self.vertices = [back, front]; } self } @@ -160,7 +160,7 @@ impl PartialHalfEdge { }); self.curve = Some(curve.into()); - self.vertices = [Some(back), Some(front)]; + self.vertices = [back, front]; Ok(self) } @@ -184,9 +184,7 @@ 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().map(|vertex| { - vertex.expect("Can't infer line segment without vertices") - }); + let [from, to] = self.vertices.clone(); let [from_surface, to_surface] = [&from, &to].map(|vertex| vertex.surface_form()); @@ -263,7 +261,7 @@ impl PartialHalfEdge { }; self.curve = Some(curve.into()); - self.vertices = [Some(back), Some(front)]; + self.vertices = [back, front]; self } @@ -281,7 +279,6 @@ impl PartialHalfEdge { .into_full(objects)?; let vertices = self.vertices.try_map_ext(|vertex| { vertex - .expect("Can't build `HalfEdge` without vertices") .update_partial(|vertex| vertex.with_curve(Some(curve.clone()))) .into_full(objects) })?; @@ -307,7 +304,7 @@ impl From<&HalfEdge> for PartialHalfEdge { Self { surface: Some(half_edge.curve().surface().clone()), curve: Some(half_edge.curve().clone().into()), - vertices: [Some(back_vertex), Some(front_vertex)], + vertices: [back_vertex, front_vertex], global_form: half_edge.global_form().clone().into(), } } From e7dc1f54b06158e840fc5d1d44c558c72a171e45 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:53:36 +0200 Subject: [PATCH 11/16] Remove unused code --- crates/fj-kernel/src/partial/maybe_partial.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/crates/fj-kernel/src/partial/maybe_partial.rs b/crates/fj-kernel/src/partial/maybe_partial.rs index 9f2575fb6..75b7bfc64 100644 --- a/crates/fj-kernel/src/partial/maybe_partial.rs +++ b/crates/fj-kernel/src/partial/maybe_partial.rs @@ -129,17 +129,6 @@ impl MaybePartial { } impl MaybePartial { - /// Access the back vertex - pub fn back(&self) -> Option> { - match self { - Self::Full(full) => Some(full.back().clone().into()), - Self::Partial(partial) => { - let [back, _] = &partial.vertices; - Some(back.clone()) - } - } - } - /// Access the front vertex pub fn front(&self) -> Option> { match self { From 507cc2f48970f10a659b6b63edc148686310d8c3 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:56:07 +0200 Subject: [PATCH 12/16] Simplify method return value --- crates/fj-kernel/src/partial/maybe_partial.rs | 6 +++--- crates/fj-kernel/src/partial/objects/cycle.rs | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/fj-kernel/src/partial/maybe_partial.rs b/crates/fj-kernel/src/partial/maybe_partial.rs index 75b7bfc64..b26287a46 100644 --- a/crates/fj-kernel/src/partial/maybe_partial.rs +++ b/crates/fj-kernel/src/partial/maybe_partial.rs @@ -130,12 +130,12 @@ impl MaybePartial { impl MaybePartial { /// Access the front vertex - pub fn front(&self) -> Option> { + pub fn front(&self) -> MaybePartial { match self { - Self::Full(full) => Some(full.front().clone().into()), + Self::Full(full) => full.front().clone().into(), Self::Partial(partial) => { let [_, front] = &partial.vertices; - Some(front.clone()) + front.clone() } } } diff --git a/crates/fj-kernel/src/partial/objects/cycle.rs b/crates/fj-kernel/src/partial/objects/cycle.rs index 9d6c2b794..e0b2ac819 100644 --- a/crates/fj-kernel/src/partial/objects/cycle.rs +++ b/crates/fj-kernel/src/partial/objects/cycle.rs @@ -169,8 +169,9 @@ impl PartialCycle { let last_vertex = self .half_edges .last_mut() - .and_then(|half_edge| { - half_edge.front().map(|vertex| (half_edge, vertex)) + .map(|half_edge| { + let vertex = half_edge.front(); + (half_edge, vertex) }) .map(|(half_edge, vertex)| { let surface_vertex = vertex.surface_form(); From e1b0625d2a154a4031b70d92fb56d26ea06cd859 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:58:14 +0200 Subject: [PATCH 13/16] Simplify method return value --- crates/fj-kernel/src/partial/maybe_partial.rs | 6 +++--- crates/fj-kernel/src/partial/objects/cycle.rs | 13 +++---------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/crates/fj-kernel/src/partial/maybe_partial.rs b/crates/fj-kernel/src/partial/maybe_partial.rs index b26287a46..3501b768c 100644 --- a/crates/fj-kernel/src/partial/maybe_partial.rs +++ b/crates/fj-kernel/src/partial/maybe_partial.rs @@ -141,12 +141,12 @@ impl MaybePartial { } /// Access the vertices - pub fn vertices(&self) -> [Option>; 2] { + pub fn vertices(&self) -> [MaybePartial; 2] { match self { Self::Full(full) => { - full.vertices().clone().map(|vertex| Some(vertex.into())) + full.vertices().clone().map(|vertex| vertex.into()) } - Self::Partial(partial) => partial.vertices.clone().map(Some), + Self::Partial(partial) => partial.vertices.clone(), } } } diff --git a/crates/fj-kernel/src/partial/objects/cycle.rs b/crates/fj-kernel/src/partial/objects/cycle.rs index e0b2ac819..7ec5cf40b 100644 --- a/crates/fj-kernel/src/partial/objects/cycle.rs +++ b/crates/fj-kernel/src/partial/objects/cycle.rs @@ -49,9 +49,7 @@ impl PartialCycle { .half_edges .last() .map(|half_edge| { - let [_, last] = half_edge.vertices().map(|vertex| { - vertex.expect("Need half-edge vertices to extend cycle") - }); + let [_, last] = half_edge.vertices(); last.surface_form() }) .into_iter() @@ -129,13 +127,8 @@ impl PartialCycle { let first = self.half_edges.first(); let last = self.half_edges.last(); - let vertices = [first, last].map(|option| { - option.map(|half_edge| { - half_edge - .vertices() - .map(|vertex| vertex.expect("Need vertices to close cycle")) - }) - }); + let vertices = [first, last] + .map(|option| option.map(|half_edge| half_edge.vertices())); if let [Some([first, _]), Some([_, last])] = vertices { let vertices = [last, first].map(|vertex| { From 2648fc81f091081f916937ffd4f012bfc92132c4 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 20:58:44 +0200 Subject: [PATCH 14/16] Refactor --- crates/fj-kernel/src/partial/maybe_partial.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/fj-kernel/src/partial/maybe_partial.rs b/crates/fj-kernel/src/partial/maybe_partial.rs index 3501b768c..647038d4e 100644 --- a/crates/fj-kernel/src/partial/maybe_partial.rs +++ b/crates/fj-kernel/src/partial/maybe_partial.rs @@ -143,9 +143,7 @@ impl MaybePartial { /// Access the vertices pub fn vertices(&self) -> [MaybePartial; 2] { match self { - Self::Full(full) => { - full.vertices().clone().map(|vertex| vertex.into()) - } + Self::Full(full) => full.vertices().clone().map(Into::into), Self::Partial(partial) => partial.vertices.clone(), } } From a0fdfd260d0b74598d37f0a16425d018e9f4cbad Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 21:03:12 +0200 Subject: [PATCH 15/16] Simplify `PartialHalfEdge` field --- .../src/algorithms/transform/edge.rs | 32 ++++++++----------- crates/fj-kernel/src/partial/objects/edge.rs | 13 ++++---- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform/edge.rs b/crates/fj-kernel/src/algorithms/transform/edge.rs index bd85f3d87..0a997a3c4 100644 --- a/crates/fj-kernel/src/algorithms/transform/edge.rs +++ b/crates/fj-kernel/src/algorithms/transform/edge.rs @@ -2,7 +2,7 @@ use fj_interop::ext::ArrayExt; use fj_math::Transform; use crate::{ - objects::{Curve, Objects}, + objects::Objects, partial::{MaybePartial, PartialGlobalEdge, PartialHalfEdge}, validate::ValidationError, }; @@ -19,34 +19,28 @@ impl TransformObject for PartialHalfEdge { .surface .map(|surface| surface.transform(transform, objects)) .transpose()?; - let curve = self + let curve: MaybePartial<_> = self .curve .clone() - .map(|curve| -> Result<_, ValidationError> { - Ok(curve - .into_partial() - .transform(transform, objects)? - .with_surface(surface.clone()) - .into()) - }) - .transpose()?; + .into_partial() + .transform(transform, objects)? + .with_surface(surface.clone()) + .into(); let vertices = self.vertices.clone().try_map_ext( |vertex| -> Result<_, ValidationError> { Ok(vertex .into_partial() .transform(transform, objects)? - .with_curve(curve.clone()) + .with_curve(Some(curve.clone())) .into()) }, )?; - let global_form = - self.global_form - .into_partial() - .transform(transform, objects)? - .with_curve(curve.as_ref().and_then( - |curve: &MaybePartial| curve.global_form(), - )) - .into(); + let global_form = self + .global_form + .into_partial() + .transform(transform, objects)? + .with_curve(curve.global_form()) + .into(); Ok(Self { surface, diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index 2c18b5ced..1fc6f122e 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -20,7 +20,7 @@ pub struct PartialHalfEdge { pub surface: Option>, /// The curve that the [`HalfEdge`] is defined in - pub curve: Option>, + pub curve: MaybePartial, /// The vertices that bound this [`HalfEdge`] in the [`Curve`] pub vertices: [MaybePartial; 2], @@ -36,7 +36,7 @@ impl PartialHalfEdge { /// /// If a global curve is available through both, the curve is preferred. pub fn extract_global_curve(&self) -> Option> { - let global_curve_from_curve = || self.curve.as_ref()?.global_form(); + let global_curve_from_curve = || self.curve.global_form(); let global_curve_from_global_form = || self.global_form.curve().cloned(); @@ -62,7 +62,7 @@ impl PartialHalfEdge { curve: Option>>, ) -> Self { if let Some(curve) = curve { - self.curve = Some(curve.into()); + self.curve = curve.into(); } self } @@ -159,7 +159,7 @@ impl PartialHalfEdge { .into() }); - self.curve = Some(curve.into()); + self.curve = curve.into(); self.vertices = [back, front]; Ok(self) @@ -260,7 +260,7 @@ impl PartialHalfEdge { }) }; - self.curve = Some(curve.into()); + self.curve = curve.into(); self.vertices = [back, front]; self @@ -274,7 +274,6 @@ impl PartialHalfEdge { let surface = self.surface; let curve = self .curve - .expect("Can't build `HalfEdge` without curve") .update_partial(|curve| curve.with_surface(surface)) .into_full(objects)?; let vertices = self.vertices.try_map_ext(|vertex| { @@ -303,7 +302,7 @@ impl From<&HalfEdge> for PartialHalfEdge { Self { surface: Some(half_edge.curve().surface().clone()), - curve: Some(half_edge.curve().clone().into()), + curve: half_edge.curve().clone().into(), vertices: [back_vertex, front_vertex], global_form: half_edge.global_form().clone().into(), } From 113f4957ff46b46a31c30de38349b3589cbc5f17 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 28 Oct 2022 21:04:14 +0200 Subject: [PATCH 16/16] Refactor --- crates/fj-kernel/src/partial/objects/edge.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/fj-kernel/src/partial/objects/edge.rs b/crates/fj-kernel/src/partial/objects/edge.rs index 1fc6f122e..669ca7986 100644 --- a/crates/fj-kernel/src/partial/objects/edge.rs +++ b/crates/fj-kernel/src/partial/objects/edge.rs @@ -36,11 +36,9 @@ impl PartialHalfEdge { /// /// If a global curve is available through both, the curve is preferred. pub fn extract_global_curve(&self) -> Option> { - let global_curve_from_curve = || self.curve.global_form(); - let global_curve_from_global_form = - || self.global_form.curve().cloned(); - - global_curve_from_curve().or_else(global_curve_from_global_form) + self.curve + .global_form() + .or_else(|| self.global_form.curve().cloned()) } /// Access the vertices of the global form, if available