diff --git a/src/kernel/shape/edges.rs b/src/kernel/shape/edges.rs index c6cda00e0..58294ca6c 100644 --- a/src/kernel/shape/edges.rs +++ b/src/kernel/shape/edges.rs @@ -9,12 +9,13 @@ use crate::{ use super::{ curves::Curves, handle::{Handle, Storage}, - EdgesInner, + EdgesInner, VerticesInner, }; /// The edges of a shape pub struct Edges<'r> { pub(super) curves: Curves, + pub(super) vertices: &'r mut VerticesInner, pub(super) edges: &'r mut EdgesInner, } @@ -34,6 +35,15 @@ impl Edges<'_> { /// the future, it can add the edge to the proper internal data structures, /// and validate any constraints that apply to edge creation. pub fn add(&mut self, edge: Edge) -> Handle { + for vertices in &edge.vertices { + for vertex in vertices { + assert!( + self.vertices.contains(vertex.storage()), + "Edge validation failed: {vertex:?} is not part of shape", + ); + } + } + let storage = Storage::new(edge); let handle = storage.handle(); @@ -74,3 +84,30 @@ impl Edges<'_> { }) } } + +#[cfg(test)] +mod tests { + use crate::{kernel::shape::Shape, math::Point}; + + #[test] + fn add_valid() { + let mut shape = Shape::new(); + + let a = shape.vertices().add(Point::from([0., 0., 0.])); + let b = shape.vertices().add(Point::from([1., 0., 0.])); + + shape.edges().add_line_segment([a, b]); + } + + #[test] + #[should_panic] + fn add_invalid() { + let mut shape = Shape::new(); + let mut other = Shape::new(); + + let a = other.vertices().add(Point::from([0., 0., 0.])); + let b = other.vertices().add(Point::from([1., 0., 0.])); + + shape.edges().add_line_segment([a, b]); + } +} diff --git a/src/kernel/shape/mod.rs b/src/kernel/shape/mod.rs index 83d4ccbef..454eebbaf 100644 --- a/src/kernel/shape/mod.rs +++ b/src/kernel/shape/mod.rs @@ -86,6 +86,7 @@ impl Shape { pub fn edges(&mut self) -> Edges { Edges { curves: Curves, + vertices: &mut self.vertices, edges: &mut self.edges, } }