From 21d5f62aa5e68f374190b5775efc418a575ee6c4 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 8 Mar 2022 15:28:27 +0100 Subject: [PATCH] Track curves through `Shape` API --- src/kernel/algorithms/transform.rs | 8 ++++---- src/kernel/shape/curves.rs | 13 +++++++++++++ src/kernel/shape/edges.rs | 29 ++++++++++++++++++----------- src/kernel/shape/mod.rs | 12 +++++++++--- src/kernel/topology/edges.rs | 2 +- 5 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 src/kernel/shape/curves.rs diff --git a/src/kernel/algorithms/transform.rs b/src/kernel/algorithms/transform.rs index 4fa66e9a7..7cb6ae16f 100644 --- a/src/kernel/algorithms/transform.rs +++ b/src/kernel/algorithms/transform.rs @@ -44,6 +44,9 @@ pub fn transform_face( let mut edges = Vec::new(); for edge in &cycle.edges { + let curve = + shape.curves().add(edge.curve.transform(transform)); + let vertices = edge.vertices.clone().map(|vertices| { vertices.map(|vertex| { let point = @@ -53,10 +56,7 @@ pub fn transform_face( }) }); - let edge = Edge { - curve: edge.curve.transform(transform), - vertices, - }; + let edge = Edge { curve, vertices }; let edge = shape.edges().add(edge); edges.push(edge); diff --git a/src/kernel/shape/curves.rs b/src/kernel/shape/curves.rs new file mode 100644 index 000000000..4d891936f --- /dev/null +++ b/src/kernel/shape/curves.rs @@ -0,0 +1,13 @@ +use crate::kernel::geometry::Curve; + +use super::handle::{Handle, Storage}; + +/// API to access the curves of a shape +pub struct Curves; + +impl Curves { + /// Add a curve to the shape + pub fn add(&mut self, curve: Curve) -> Handle { + Storage::new(curve).handle() + } +} diff --git a/src/kernel/shape/edges.rs b/src/kernel/shape/edges.rs index 5ae4a5b3f..921b5250c 100644 --- a/src/kernel/shape/edges.rs +++ b/src/kernel/shape/edges.rs @@ -1,15 +1,20 @@ use crate::{ kernel::{ - geometry::{Curve, Line, Circle}, + geometry::{Circle, Curve, Line}, topology::{edges::Edge, vertices::Vertex}, }, - math::{Scalar, Vector, Point}, + math::{Point, Scalar, Vector}, }; -use super::handle::{Handle, Storage}; +use super::{ + curves::Curves, + handle::{Handle, Storage}, +}; /// The edges of a shape -pub struct Edges; +pub struct Edges { + pub(super) curves: Curves, +} impl Edges { /// Add an edge to the shape @@ -35,11 +40,12 @@ impl Edges { /// Calls [`Edges::add`] internally, and is subject to the same /// restrictions. pub fn add_circle(&mut self, radius: Scalar) -> Handle { + let curve = self.curves.add(Curve::Circle(Circle { + center: Point::origin(), + radius: Vector::from([radius, Scalar::ZERO]), + })); self.add(Edge { - curve: Curve::Circle(Circle { - center: Point::origin(), - radius: Vector::from([radius, Scalar::ZERO]), - }), + curve, vertices: None, }) } @@ -52,10 +58,11 @@ impl Edges { &mut self, vertices: [Handle; 2], ) -> Handle { + let curve = self.curves.add(Curve::Line(Line::from_points( + vertices.clone().map(|vertex| vertex.point()), + ))); self.add(Edge { - curve: Curve::Line(Line::from_points( - vertices.clone().map(|vertex| vertex.point()), - )), + curve, vertices: Some(vertices), }) } diff --git a/src/kernel/shape/mod.rs b/src/kernel/shape/mod.rs index 7e7b600aa..75fe2bfcd 100644 --- a/src/kernel/shape/mod.rs +++ b/src/kernel/shape/mod.rs @@ -1,3 +1,4 @@ +pub mod curves; pub mod cycles; pub mod edges; pub mod faces; @@ -10,8 +11,8 @@ use crate::math::Scalar; use super::topology::{edges::Cycle, faces::Face, vertices::Vertex}; use self::{ - cycles::Cycles, edges::Edges, faces::Faces, handle::Storage, - surfaces::Surfaces, vertices::Vertices, + curves::Curves, cycles::Cycles, edges::Edges, faces::Faces, + handle::Storage, surfaces::Surfaces, vertices::Vertices, }; /// The boundary representation of a shape @@ -57,6 +58,11 @@ impl Shape { self } + /// Access the shape's curves + pub fn curves(&mut self) -> Curves { + Curves + } + /// Access the shape's surfaces pub fn surfaces(&mut self) -> Surfaces { Surfaces @@ -72,7 +78,7 @@ impl Shape { /// Access the shape's edges pub fn edges(&mut self) -> Edges { - Edges + Edges { curves: Curves } } /// Access the shape's cycles diff --git a/src/kernel/topology/edges.rs b/src/kernel/topology/edges.rs index 550fc7a4a..20265ffdf 100644 --- a/src/kernel/topology/edges.rs +++ b/src/kernel/topology/edges.rs @@ -20,7 +20,7 @@ pub struct Edge { /// The edge can be a segment of the curve that is bounded by two vertices, /// or if the curve is continuous (i.e. connects to itself), the edge could /// be defined by the whole curve, and have no bounding vertices. - pub curve: Curve, + pub curve: Handle, /// Access the vertices that bound the edge on the curve ///