diff --git a/src/router/mesh.rs b/src/router/mesh.rs index 5856bee..6154afc 100644 --- a/src/router/mesh.rs +++ b/src/router/mesh.rs @@ -31,7 +31,7 @@ pub enum VertexIndex { #[enum_dispatch(GetNodeIndex, MakePrimitive)] #[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)] -pub enum TriangulationVertexIndex { +enum TriangulationVertexIndex { FixedDot(FixedDotIndex), FixedBend(FixedBendIndex), } @@ -82,13 +82,13 @@ pub struct Mesh { } impl Mesh { - pub fn new(layout: &Drawing) -> Self { + pub fn new(drawing: &Drawing) -> Self { let mut this = Self { - triangulation: Triangulation::new(layout), + triangulation: Triangulation::new(drawing), vertex_to_triangulation_vertex: Vec::new(), }; this.vertex_to_triangulation_vertex - .resize(layout.geometry().graph().node_bound(), None); + .resize(drawing.geometry().graph().node_bound(), None); this } diff --git a/src/router/triangulation.rs b/src/router/triangulation.rs index a19f2ad..f55006f 100644 --- a/src/router/triangulation.rs +++ b/src/router/triangulation.rs @@ -2,7 +2,10 @@ use std::marker::PhantomData; use geo::{point, Point}; use petgraph::visit::{self, NodeIndexable}; -use spade::{handles::FixedVertexHandle, DelaunayTriangulation, HasPosition, InsertionError}; +use spade::{ + handles::FixedVertexHandle, iterators::VertexIterator, DelaunayTriangulation, HasPosition, + InsertionError, +}; use crate::{ drawing::{rules::RulesTrait, Drawing}, @@ -146,11 +149,8 @@ impl<'a, I: Copy + PartialEq + GetNodeIndex, W: GetVertexIndex + HasPosition< } } -impl< - 'a, - I: Copy + PartialEq + GetNodeIndex + std::fmt::Debug, - W: GetVertexIndex + HasPosition, - > visit::IntoEdges for &'a Triangulation +impl<'a, I: Copy + PartialEq + GetNodeIndex, W: GetVertexIndex + HasPosition> + visit::IntoEdges for &'a Triangulation { type Edges = Box> + 'a>; @@ -165,3 +165,73 @@ impl< ) } } + +impl<'a, I: Copy + PartialEq + GetNodeIndex, W: GetVertexIndex + HasPosition> + visit::IntoNodeIdentifiers for &'a Triangulation +{ + type NodeIdentifiers = Box + 'a>; + + fn node_identifiers(self) -> Self::NodeIdentifiers { + Box::new( + spade::Triangulation::fixed_vertices(&self.triangulation).map(|vertex| { + spade::Triangulation::s(&self.triangulation) + .vertex_data(vertex) + .vertex() + }), + ) + } +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct TriangulationVertexReference { + index: I, +} + +impl visit::NodeRef for TriangulationVertexReference { + type NodeId = I; + type Weight = (); + + fn id(&self) -> Self::NodeId { + self.index + } + + fn weight(&self) -> &Self::Weight { + &() + } +} + +impl<'a, I: Copy + PartialEq + GetNodeIndex, W: GetVertexIndex + HasPosition> + visit::IntoNodeReferences for &'a Triangulation +{ + type NodeRef = TriangulationVertexReference; + type NodeReferences = Box> + 'a>; + fn node_references(self) -> Self::NodeReferences { + Box::new( + spade::Triangulation::fixed_vertices(&self.triangulation).map(|vertex| { + TriangulationVertexReference { + index: spade::Triangulation::s(&self.triangulation) + .vertex_data(vertex) + .vertex(), + } + }), + ) + } +} + +impl<'a, I: Copy + PartialEq + GetNodeIndex, W: GetVertexIndex + HasPosition> + visit::NodeIndexable for &'a Triangulation +{ + fn node_bound(&self) -> usize { + spade::Triangulation::num_vertices(&self.triangulation) + } + + fn to_index(&self, node: I) -> usize { + node.node_index().index() + } + + fn from_index(&self, index: usize) -> I { + spade::Triangulation::s(&self.triangulation) + .vertex_data(self.vertex_to_handle[index].unwrap()) + .vertex() + } +}