From 10f8bb9eedfdd15093365425f45876abefc8b94c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 8 Mar 2022 14:59:40 +0100 Subject: [PATCH] Add API for adding surfaces to `Shape` The surfaces aren't actually stored yet, as that is not necessary right now. Going through this API paves the way for future additions though. --- src/kernel/algorithms/approximation.rs | 3 ++- src/kernel/algorithms/sweep.rs | 10 +++++++--- src/kernel/algorithms/transform.rs | 4 +++- src/kernel/shape/mod.rs | 8 +++++++- src/kernel/shape/surfaces.rs | 13 +++++++++++++ src/kernel/shapes/circle.rs | 6 ++---- src/kernel/shapes/sketch.rs | 3 ++- src/kernel/topology/faces.rs | 2 +- 8 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/kernel/shape/surfaces.rs diff --git a/src/kernel/algorithms/approximation.rs b/src/kernel/algorithms/approximation.rs index 6b01c7912..f070f63f9 100644 --- a/src/kernel/algorithms/approximation.rs +++ b/src/kernel/algorithms/approximation.rs @@ -266,8 +266,9 @@ mod tests { edges: vec![ab, bc, cd, da], }); + let surface = shape.surfaces().add(Surface::x_y_plane()); let face = Face::Face { - surface: Surface::x_y_plane(), + surface, cycles: vec![abcd], }; diff --git a/src/kernel/algorithms/sweep.rs b/src/kernel/algorithms/sweep.rs index b5e123b01..7908311d4 100644 --- a/src/kernel/algorithms/sweep.rs +++ b/src/kernel/algorithms/sweep.rs @@ -129,10 +129,14 @@ mod tests { edges: vec![ab, bc, ca], }); + let surface = + shape + .surfaces() + .add(Surface::Swept(Swept::plane_from_points( + [a, b, c].map(|vertex| vertex.point()), + ))); let abc = Face::Face { - surface: Surface::Swept(Swept::plane_from_points( - [a, b, c].map(|vertex| vertex.point()), - )), + surface, cycles: vec![cycles], }; diff --git a/src/kernel/algorithms/transform.rs b/src/kernel/algorithms/transform.rs index 31ab73d17..4fa66e9a7 100644 --- a/src/kernel/algorithms/transform.rs +++ b/src/kernel/algorithms/transform.rs @@ -65,9 +65,11 @@ pub fn transform_face( cycles_trans.push(shape.cycles().add(Cycle { edges })); } + let surface = shape.surfaces().add(surface.transform(transform)); + Face::Face { cycles: cycles_trans, - surface: surface.transform(transform), + surface, } } Face::Triangles(mut triangles) => { diff --git a/src/kernel/shape/mod.rs b/src/kernel/shape/mod.rs index 265fab1a4..7e7b600aa 100644 --- a/src/kernel/shape/mod.rs +++ b/src/kernel/shape/mod.rs @@ -2,6 +2,7 @@ pub mod cycles; pub mod edges; pub mod faces; pub mod handle; +pub mod surfaces; pub mod vertices; use crate::math::Scalar; @@ -10,7 +11,7 @@ use super::topology::{edges::Cycle, faces::Face, vertices::Vertex}; use self::{ cycles::Cycles, edges::Edges, faces::Faces, handle::Storage, - vertices::Vertices, + surfaces::Surfaces, vertices::Vertices, }; /// The boundary representation of a shape @@ -56,6 +57,11 @@ impl Shape { self } + /// Access the shape's surfaces + pub fn surfaces(&mut self) -> Surfaces { + Surfaces + } + /// Access the shape's vertices pub fn vertices(&mut self) -> Vertices { Vertices { diff --git a/src/kernel/shape/surfaces.rs b/src/kernel/shape/surfaces.rs new file mode 100644 index 000000000..2de37daff --- /dev/null +++ b/src/kernel/shape/surfaces.rs @@ -0,0 +1,13 @@ +use crate::kernel::geometry::Surface; + +use super::handle::{Handle, Storage}; + +/// API to access the surfaces of a shape +pub struct Surfaces; + +impl Surfaces { + /// Add a surface to the shape + pub fn add(&mut self, surface: Surface) -> Handle { + Storage::new(surface).handle() + } +} diff --git a/src/kernel/shapes/circle.rs b/src/kernel/shapes/circle.rs index 3cf0a37bc..11f1d7283 100644 --- a/src/kernel/shapes/circle.rs +++ b/src/kernel/shapes/circle.rs @@ -26,10 +26,8 @@ impl ToShape for fj::Circle { shape.cycles().add(Cycle { edges: vec![edge] }); let cycles = shape.cycles().all().collect(); - shape.faces().add(Face::Face { - cycles, - surface: Surface::x_y_plane(), - }); + let surface = shape.surfaces().add(Surface::x_y_plane()); + shape.faces().add(Face::Face { cycles, surface }); shape } diff --git a/src/kernel/shapes/sketch.rs b/src/kernel/shapes/sketch.rs index de13b0a1e..a6f2c42c5 100644 --- a/src/kernel/shapes/sketch.rs +++ b/src/kernel/shapes/sketch.rs @@ -47,9 +47,10 @@ impl ToShape for fj::Sketch { shape.cycles().add(Cycle { edges }); }; + let surface = shape.surfaces().add(Surface::x_y_plane()); let face = Face::Face { cycles: shape.cycles().all().collect(), - surface: Surface::x_y_plane(), + surface, }; shape.faces().add(face); diff --git a/src/kernel/topology/faces.rs b/src/kernel/topology/faces.rs index 6aebcfcf2..bfe394287 100644 --- a/src/kernel/topology/faces.rs +++ b/src/kernel/topology/faces.rs @@ -26,7 +26,7 @@ pub enum Face { /// surface. Face { /// The surface that defines this face - surface: Surface, + surface: Handle, /// The cycles that bound the face ///