From 7fc8a1846c87501fdc4f70bbf89012083fd1da20 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 4 Apr 2024 11:39:04 +0200 Subject: [PATCH] Reference `Surface` from `Sketch` --- crates/fj-core/src/operations/build/sketch.rs | 4 +- .../fj-core/src/operations/replace/curve.rs | 2 +- .../src/operations/replace/half_edge.rs | 2 +- .../fj-core/src/operations/replace/vertex.rs | 4 +- .../fj-core/src/operations/update/sketch.rs | 4 +- crates/fj-core/src/topology/objects/sketch.rs | 14 +++- crates/fj-core/src/validate/sketch.rs | 67 ++++++++++++------- 7 files changed, 61 insertions(+), 36 deletions(-) diff --git a/crates/fj-core/src/operations/build/sketch.rs b/crates/fj-core/src/operations/build/sketch.rs index b21a2c70d..93e8a6840 100644 --- a/crates/fj-core/src/operations/build/sketch.rs +++ b/crates/fj-core/src/operations/build/sketch.rs @@ -7,8 +7,8 @@ use crate::topology::{Sketch, Topology}; /// [module-level documentation]: super pub trait BuildSketch { /// Create a sketch with no regions - fn empty(_: &Topology) -> Sketch { - Sketch::new([]) + fn empty(topology: &Topology) -> Sketch { + Sketch::new(topology.surfaces.space_2d(), []) } } diff --git a/crates/fj-core/src/operations/replace/curve.rs b/crates/fj-core/src/operations/replace/curve.rs index 570edd170..1e5a21051 100644 --- a/crates/fj-core/src/operations/replace/curve.rs +++ b/crates/fj-core/src/operations/replace/curve.rs @@ -151,7 +151,7 @@ impl ReplaceCurve for Sketch { } if replacement_happened { - ReplaceOutput::Updated(Sketch::new(regions)) + ReplaceOutput::Updated(Sketch::new(self.surface().clone(), regions)) } else { ReplaceOutput::Original(self.clone()) } diff --git a/crates/fj-core/src/operations/replace/half_edge.rs b/crates/fj-core/src/operations/replace/half_edge.rs index 8c6b3687e..ae046861b 100644 --- a/crates/fj-core/src/operations/replace/half_edge.rs +++ b/crates/fj-core/src/operations/replace/half_edge.rs @@ -117,7 +117,7 @@ impl ReplaceHalfEdge for Sketch { } if replacement_happened { - ReplaceOutput::Updated(Sketch::new(regions)) + ReplaceOutput::Updated(Sketch::new(self.surface().clone(), regions)) } else { ReplaceOutput::Original(self.clone()) } diff --git a/crates/fj-core/src/operations/replace/vertex.rs b/crates/fj-core/src/operations/replace/vertex.rs index 11715b479..e96f41d51 100644 --- a/crates/fj-core/src/operations/replace/vertex.rs +++ b/crates/fj-core/src/operations/replace/vertex.rs @@ -153,7 +153,7 @@ impl ReplaceVertex for Sketch { } if replacement_happened { - ReplaceOutput::Updated(Sketch::new(regions)) + ReplaceOutput::Updated(Sketch::new(self.surface().clone(), regions)) } else { ReplaceOutput::Original(self.clone()) } @@ -317,7 +317,7 @@ impl ReplaceVertex for Handle { } if replacement_happened { - ReplaceOutput::Updated(Sketch::new(regions)) + ReplaceOutput::Updated(Sketch::new(self.surface().clone(), regions)) } else { ReplaceOutput::Original(self.clone()) } diff --git a/crates/fj-core/src/operations/update/sketch.rs b/crates/fj-core/src/operations/update/sketch.rs index 4f1bc6a07..843442e47 100644 --- a/crates/fj-core/src/operations/update/sketch.rs +++ b/crates/fj-core/src/operations/update/sketch.rs @@ -47,7 +47,7 @@ impl UpdateSketch for Sketch { { let regions = regions.into_iter().map(|region| region.insert(core)); let regions = self.regions().iter().cloned().chain(regions); - Sketch::new(regions) + Sketch::new(self.surface().clone(), regions) } fn update_region( @@ -69,6 +69,6 @@ impl UpdateSketch for Sketch { }), ) .expect("Region not found"); - Sketch::new(regions) + Sketch::new(self.surface().clone(), regions) } } diff --git a/crates/fj-core/src/topology/objects/sketch.rs b/crates/fj-core/src/topology/objects/sketch.rs index ba79d5de0..71555a7b4 100644 --- a/crates/fj-core/src/topology/objects/sketch.rs +++ b/crates/fj-core/src/topology/objects/sketch.rs @@ -1,22 +1,32 @@ use crate::{ storage::Handle, - topology::{ObjectSet, Region}, + topology::{ObjectSet, Region, Surface}, }; /// A 2-dimensional shape #[derive(Clone, Debug)] pub struct Sketch { + surface: Handle, regions: ObjectSet, } impl Sketch { /// Construct an empty instance of `Sketch` - pub fn new(regions: impl IntoIterator>) -> Self { + pub fn new( + surface: Handle, + regions: impl IntoIterator>, + ) -> Self { Self { + surface, regions: regions.into_iter().collect(), } } + /// Access the surface of the sketch + pub fn surface(&self) -> &Handle { + &self.surface + } + /// Access the regions of the sketch pub fn regions(&self) -> &ObjectSet { &self.regions diff --git a/crates/fj-core/src/validate/sketch.rs b/crates/fj-core/src/validate/sketch.rs index 794295b74..39170e10f 100644 --- a/crates/fj-core/src/validate/sketch.rs +++ b/crates/fj-core/src/validate/sketch.rs @@ -137,14 +137,21 @@ mod tests { let region = ::circle([0., 0.], 1., &mut core) .insert(&mut core); - let valid_sketch = Sketch::new(vec![region.clone()]).insert(&mut core); + let valid_sketch = Sketch::new( + core.layers.topology.surfaces.space_2d(), + vec![region.clone()], + ) + .insert(&mut core); valid_sketch.validate_and_return_first_error(&core.layers.geometry)?; let shared_cycle = region.exterior(); - let invalid_sketch = Sketch::new(vec![ - Region::new(shared_cycle.clone(), vec![]).insert(&mut core), - Region::new(shared_cycle.clone(), vec![]).insert(&mut core), - ]); + let invalid_sketch = Sketch::new( + core.layers.topology.surfaces.space_2d(), + vec![ + Region::new(shared_cycle.clone(), vec![]).insert(&mut core), + Region::new(shared_cycle.clone(), vec![]).insert(&mut core), + ], + ); assert_contains_err!( core, invalid_sketch, @@ -165,7 +172,11 @@ mod tests { &mut core, ) .insert(&mut core); - let valid_sketch = Sketch::new(vec![region.clone()]).insert(&mut core); + let valid_sketch = Sketch::new( + core.layers.topology.surfaces.space_2d(), + vec![region.clone()], + ) + .insert(&mut core); valid_sketch.validate_and_return_first_error(&core.layers.geometry)?; let exterior = region.exterior(); @@ -173,10 +184,12 @@ mod tests { exterior.half_edges().iter().cloned().collect(); let interior = Cycle::new(cloned_edges).insert(&mut core); - let invalid_sketch = - Sketch::new(vec![ + let invalid_sketch = Sketch::new( + core.layers.topology.surfaces.space_2d(), + vec![ Region::new(exterior.clone(), vec![interior]).insert(&mut core) - ]); + ], + ); assert_contains_err!( core, invalid_sketch, @@ -195,10 +208,10 @@ mod tests { let valid_outer_circle = HalfEdge::circle([0., 0.], 1., &mut core); let valid_exterior = Cycle::new(vec![valid_outer_circle.clone()]).insert(&mut core); - let valid_sketch = - Sketch::new(vec![ - Region::new(valid_exterior.clone(), vec![]).insert(&mut core) - ]); + let valid_sketch = Sketch::new( + core.layers.topology.surfaces.space_2d(), + vec![Region::new(valid_exterior.clone(), vec![]).insert(&mut core)], + ); valid_sketch.validate_and_return_first_error(&core.layers.geometry)?; let invalid_outer_circle = HalfEdge::from_sibling( @@ -208,10 +221,12 @@ mod tests { ); let invalid_exterior = Cycle::new(vec![invalid_outer_circle.clone()]).insert(&mut core); - let invalid_sketch = - Sketch::new(vec![ + let invalid_sketch = Sketch::new( + core.layers.topology.surfaces.space_2d(), + vec![ Region::new(invalid_exterior.clone(), vec![]).insert(&mut core) - ]); + ], + ); assert_contains_err!( core, invalid_sketch, @@ -238,20 +253,20 @@ mod tests { let valid_interior = Cycle::new(vec![cw_inner_circle.clone()]).insert(&mut core); - let valid_sketch = Sketch::new(vec![Region::new( - exterior.clone(), - vec![valid_interior], - ) - .insert(&mut core)]); + let valid_sketch = Sketch::new( + core.layers.topology.surfaces.space_2d(), + vec![Region::new(exterior.clone(), vec![valid_interior]) + .insert(&mut core)], + ); valid_sketch.validate_and_return_first_error(&core.layers.geometry)?; let invalid_interior = Cycle::new(vec![inner_circle.clone()]).insert(&mut core); - let invalid_sketch = Sketch::new(vec![Region::new( - exterior.clone(), - vec![invalid_interior], - ) - .insert(&mut core)]); + let invalid_sketch = Sketch::new( + core.layers.topology.surfaces.space_2d(), + vec![Region::new(exterior.clone(), vec![invalid_interior]) + .insert(&mut core)], + ); assert_contains_err!( core, invalid_sketch,