Skip to content

Commit

Permalink
Merge pull request #277 from hannobraun/validation
Browse files Browse the repository at this point in the history
Prepare the ground for vertex validation
  • Loading branch information
hannobraun authored Mar 2, 2022
2 parents b4a33ac + b1e840f commit 9924a0b
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 30 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ approx = "0.5.1"
bytemuck = "1.7.3"
decorum = "0.3.1"
futures = "0.3.21"
kiddo = "0.2.3"
libloading = "0.7.2"
map-macro = "0.2.0"
nalgebra = "0.30.0"
Expand Down
6 changes: 3 additions & 3 deletions src/kernel/shapes/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ use super::ToShape;
impl ToShape for fj::Sketch {
fn to_shape(&self, _: Scalar, _: &mut DebugInfo) -> Shape {
let mut shape = Shape::new();
let mut vertices = Vec::new();

for [x, y] in self.to_points() {
shape.vertices().create(Point::from([x, y, 0.]));
let vertex = shape.vertices().create(Point::from([x, y, 0.]));
vertices.push(vertex);
}

shape.edges = {
let mut vertices: Vec<_> = shape.vertices().iter().collect();

if !vertices.is_empty() {
// Add the first vertex at the end again, to close the loop.
//
Expand Down
10 changes: 7 additions & 3 deletions src/kernel/topology/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ pub mod edges;
pub mod faces;
pub mod vertices;

use crate::math::Point;
use kiddo::KdTree;

use crate::math::{Point, Scalar};

use self::{edges::Edges, faces::Faces, vertices::Vertices};

Expand All @@ -14,7 +16,7 @@ use self::{edges::Edges, faces::Faces, vertices::Vertices};
/// provides. Steps have been made in that direction, but right now, the API is
/// still full of holes, forcing callers to just be careful for the time being.
pub struct Shape {
vertices: Vec<Point<3>>,
vertices: VerticesInner,

pub edges: Edges,
pub faces: Faces,
Expand All @@ -24,7 +26,7 @@ impl Shape {
/// Construct a new shape
pub fn new() -> Self {
Self {
vertices: Vec::new(),
vertices: VerticesInner::new(),
edges: Edges { cycles: Vec::new() },
faces: Faces(Vec::new()),
}
Expand All @@ -37,3 +39,5 @@ impl Shape {
}
}
}

type VerticesInner = KdTree<Scalar, Point<3>, 3>;
21 changes: 7 additions & 14 deletions src/kernel/topology/vertices.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::{
kernel::geometry::{self, Curve},
math::Point,
};
use crate::kernel::geometry::{self, Curve};

use super::VerticesInner;

/// The vertices of a shape
pub struct Vertices<'r> {
pub(super) vertices: &'r mut Vec<Point<3>>,
pub(super) vertices: &'r mut VerticesInner,
}

impl Vertices<'_> {
Expand All @@ -24,16 +23,10 @@ impl Vertices<'_> {
point: impl Into<geometry::Point<D>>,
) -> Vertex<D> {
let point = point.into();
self.vertices.push(point.canonical());
Vertex(point)
}

/// Access an iterator over all vertices
pub fn iter(&self) -> impl Iterator<Item = Vertex<3>> + '_ {
self.vertices
.iter()
.copied()
.map(|point| Vertex(geometry::Point::new(point, point)))
.add(&point.canonical().into(), point.canonical())
.expect("Error adding vertex");
Vertex(point)
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/math/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ impl<const D: usize> From<Point<D>> for [f64; D] {
}
}

impl<const D: usize> From<Point<D>> for [Scalar; D] {
fn from(point: Point<D>) -> Self {
point.coords.into()
}
}

impl<const D: usize> ops::Neg for Point<D> {
type Output = Self;

Expand Down
Loading

0 comments on commit 9924a0b

Please sign in to comment.