Provide means of converting dot index to vertex index

This commit is contained in:
Mikolaj Wielgus 2023-08-27 05:00:15 +02:00
parent d1571c99f6
commit 270a7e857c
1 changed files with 19 additions and 8 deletions

View File

@ -7,7 +7,7 @@ use petgraph::{
use spade::{ use spade::{
handles::{DirectedEdgeHandle, FixedDirectedEdgeHandle, FixedVertexHandle}, handles::{DirectedEdgeHandle, FixedDirectedEdgeHandle, FixedVertexHandle},
iterators::DirectedEdgeIterator, iterators::DirectedEdgeIterator,
DelaunayTriangulation, HasPosition, Point2, Triangulation, DelaunayTriangulation, HasPosition, InsertionError, Point2, Triangulation,
}; };
use crate::{graph::DotIndex, layout::Layout, router::Router}; use crate::{graph::DotIndex, layout::Layout, router::Router};
@ -32,32 +32,43 @@ impl HasPosition for Vertex {
pub struct Mesh { pub struct Mesh {
triangulation: DelaunayTriangulation<Vertex>, triangulation: DelaunayTriangulation<Vertex>,
dot_to_vertex: Vec<Option<VertexIndex>>,
} }
impl Mesh { impl Mesh {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
triangulation: DelaunayTriangulation::new(), triangulation: DelaunayTriangulation::new(),
dot_to_vertex: Vec::new(),
} }
} }
pub fn triangulate(&mut self, layout: &Layout) { pub fn triangulate(&mut self, layout: &Layout) -> Result<(), InsertionError> {
self.triangulation.clear(); self.triangulation.clear();
self.dot_to_vertex = Vec::new();
self.dot_to_vertex.resize(layout.graph.node_bound(), None);
for dot in layout.dots() { for dot in layout.dots() {
let center = layout.primitive(dot).shape().center(); let center = layout.primitive(dot).shape().center();
self.triangulation
.insert(Vertex { self.dot_to_vertex[dot.index.index()] = Some(VertexIndex {
handle: self.triangulation.insert(Vertex {
dot, dot,
x: center.x(), x: center.x(),
y: center.y(), y: center.y(),
}) })?,
.unwrap(); // TODO. });
} }
Ok(())
} }
pub fn position(&self, index: VertexIndex) -> Point { pub fn vertex(&self, dot: DotIndex) -> VertexIndex {
let position = self.triangulation.vertex(index.handle).position(); self.dot_to_vertex[dot.index.index()].unwrap()
}
pub fn position(&self, vertex: VertexIndex) -> Point {
let position = self.triangulation.vertex(vertex.handle).position();
point! {x: position.x, y: position.y} point! {x: position.x, y: position.y}
} }
} }