Skip to content

Commit

Permalink
Structurally validate edges
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Mar 9, 2022
1 parent 51aac84 commit 0445620
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/kernel/shape/edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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<Edge> {
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();

Expand Down Expand Up @@ -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]);
}
}
1 change: 1 addition & 0 deletions src/kernel/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl Shape {
pub fn edges(&mut self) -> Edges {
Edges {
curves: Curves,
vertices: &mut self.vertices,
edges: &mut self.edges,
}
}
Expand Down

0 comments on commit 0445620

Please sign in to comment.