Skip to content

Commit

Permalink
Reference Surface from Sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Apr 25, 2024
1 parent de4cdc9 commit 7fc8a18
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 36 deletions.
4 changes: 2 additions & 2 deletions crates/fj-core/src/operations/build/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(), [])
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/fj-core/src/operations/replace/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-core/src/operations/replace/half_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/operations/replace/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -317,7 +317,7 @@ impl ReplaceVertex for Handle<Sketch> {
}

if replacement_happened {
ReplaceOutput::Updated(Sketch::new(regions))
ReplaceOutput::Updated(Sketch::new(self.surface().clone(), regions))
} else {
ReplaceOutput::Original(self.clone())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/operations/update/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, R>(
Expand All @@ -69,6 +69,6 @@ impl UpdateSketch for Sketch {
}),
)
.expect("Region not found");
Sketch::new(regions)
Sketch::new(self.surface().clone(), regions)
}
}
14 changes: 12 additions & 2 deletions crates/fj-core/src/topology/objects/sketch.rs
Original file line number Diff line number Diff line change
@@ -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<Surface>,
regions: ObjectSet<Region>,
}

impl Sketch {
/// Construct an empty instance of `Sketch`
pub fn new(regions: impl IntoIterator<Item = Handle<Region>>) -> Self {
pub fn new(
surface: Handle<Surface>,
regions: impl IntoIterator<Item = Handle<Region>>,
) -> Self {
Self {
surface,
regions: regions.into_iter().collect(),
}
}

/// Access the surface of the sketch
pub fn surface(&self) -> &Handle<Surface> {
&self.surface
}

/// Access the regions of the sketch
pub fn regions(&self) -> &ObjectSet<Region> {
&self.regions
Expand Down
67 changes: 41 additions & 26 deletions crates/fj-core/src/validate/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,21 @@ mod tests {

let region = <Region as BuildRegion>::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,
Expand All @@ -165,18 +172,24 @@ 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();
let cloned_edges: Vec<_> =
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,
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 7fc8a18

Please sign in to comment.