mirror of https://codeberg.org/topola/topola.git
Wrap `FixedVertexHandle` in a `VertexIndex` struct
This commit is contained in:
parent
9196d95546
commit
d1571c99f6
55
src/mesh.rs
55
src/mesh.rs
|
|
@ -12,13 +12,18 @@ use spade::{
|
||||||
|
|
||||||
use crate::{graph::DotIndex, layout::Layout, router::Router};
|
use crate::{graph::DotIndex, layout::Layout, router::Router};
|
||||||
|
|
||||||
struct MeshVertex {
|
struct Vertex {
|
||||||
pub index: DotIndex,
|
pub dot: DotIndex,
|
||||||
x: f64,
|
x: f64,
|
||||||
y: f64,
|
y: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasPosition for MeshVertex {
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
|
pub struct VertexIndex {
|
||||||
|
handle: FixedVertexHandle,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasPosition for Vertex {
|
||||||
type Scalar = f64;
|
type Scalar = f64;
|
||||||
fn position(&self) -> Point2<Self::Scalar> {
|
fn position(&self) -> Point2<Self::Scalar> {
|
||||||
Point2::new(self.x, self.y)
|
Point2::new(self.x, self.y)
|
||||||
|
|
@ -26,7 +31,7 @@ impl HasPosition for MeshVertex {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Mesh {
|
pub struct Mesh {
|
||||||
triangulation: DelaunayTriangulation<MeshVertex>,
|
triangulation: DelaunayTriangulation<Vertex>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mesh {
|
impl Mesh {
|
||||||
|
|
@ -42,8 +47,8 @@ impl Mesh {
|
||||||
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
|
self.triangulation
|
||||||
.insert(MeshVertex {
|
.insert(Vertex {
|
||||||
index: dot,
|
dot,
|
||||||
x: center.x(),
|
x: center.x(),
|
||||||
y: center.y(),
|
y: center.y(),
|
||||||
})
|
})
|
||||||
|
|
@ -51,14 +56,14 @@ impl Mesh {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn position(&self, handle: FixedVertexHandle) -> Point {
|
pub fn position(&self, index: VertexIndex) -> Point {
|
||||||
let position = self.triangulation.vertex(handle).position();
|
let position = self.triangulation.vertex(index.handle).position();
|
||||||
point! {x: position.x, y: position.y}
|
point! {x: position.x, y: position.y}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl visit::GraphBase for Mesh {
|
impl visit::GraphBase for Mesh {
|
||||||
type NodeId = FixedVertexHandle;
|
type NodeId = VertexIndex;
|
||||||
type EdgeId = FixedDirectedEdgeHandle;
|
type EdgeId = FixedDirectedEdgeHandle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,9 +91,9 @@ pub trait IndexHolder {
|
||||||
fn index(&self) -> usize;
|
fn index(&self) -> usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IndexHolder for FixedVertexHandle {
|
impl IndexHolder for VertexIndex {
|
||||||
fn index(&self) -> usize {
|
fn index(&self) -> usize {
|
||||||
self.index()
|
self.handle.index()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -122,20 +127,24 @@ impl visit::Data for Mesh {
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct MeshEdgeReference<'a> {
|
pub struct MeshEdgeReference<'a> {
|
||||||
handle: DirectedEdgeHandle<'a, MeshVertex, (), (), ()>,
|
handle: DirectedEdgeHandle<'a, Vertex, (), (), ()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> visit::EdgeRef for MeshEdgeReference<'a> {
|
impl<'a> visit::EdgeRef for MeshEdgeReference<'a> {
|
||||||
type NodeId = FixedVertexHandle;
|
type NodeId = VertexIndex;
|
||||||
type EdgeId = FixedDirectedEdgeHandle;
|
type EdgeId = FixedDirectedEdgeHandle;
|
||||||
type Weight = ();
|
type Weight = ();
|
||||||
|
|
||||||
fn source(&self) -> Self::NodeId {
|
fn source(&self) -> Self::NodeId {
|
||||||
self.handle.from().fix()
|
VertexIndex {
|
||||||
|
handle: self.handle.from().fix(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn target(&self) -> Self::NodeId {
|
fn target(&self) -> Self::NodeId {
|
||||||
self.handle.to().fix()
|
VertexIndex {
|
||||||
|
handle: self.handle.to().fix(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn weight(&self) -> &Self::Weight {
|
fn weight(&self) -> &Self::Weight {
|
||||||
|
|
@ -148,7 +157,7 @@ impl<'a> visit::EdgeRef for MeshEdgeReference<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MeshEdgeReferences<'a> {
|
pub struct MeshEdgeReferences<'a> {
|
||||||
iter: DirectedEdgeIterator<'a, MeshVertex, (), (), ()>,
|
iter: DirectedEdgeIterator<'a, Vertex, (), (), ()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Iterator for MeshEdgeReferences<'a> {
|
impl<'a> Iterator for MeshEdgeReferences<'a> {
|
||||||
|
|
@ -172,15 +181,17 @@ impl<'a> visit::IntoEdgeReferences for &'a Mesh {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MeshNeighbors<'a> {
|
pub struct MeshNeighbors<'a> {
|
||||||
iter: Box<dyn Iterator<Item = DirectedEdgeHandle<'a, MeshVertex, (), (), ()>> + 'a>,
|
iter: Box<dyn Iterator<Item = DirectedEdgeHandle<'a, Vertex, (), (), ()>> + 'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Iterator for MeshNeighbors<'a> {
|
impl<'a> Iterator for MeshNeighbors<'a> {
|
||||||
type Item = FixedVertexHandle;
|
type Item = VertexIndex;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let handle = self.iter.next()?;
|
let handle = self.iter.next()?;
|
||||||
Some(handle.to().fix())
|
Some(VertexIndex {
|
||||||
|
handle: handle.to().fix(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -189,13 +200,13 @@ impl<'a> visit::IntoNeighbors for &'a Mesh {
|
||||||
|
|
||||||
fn neighbors(self, a: Self::NodeId) -> Self::Neighbors {
|
fn neighbors(self, a: Self::NodeId) -> Self::Neighbors {
|
||||||
MeshNeighbors {
|
MeshNeighbors {
|
||||||
iter: Box::new(self.triangulation.vertex(a).out_edges()),
|
iter: Box::new(self.triangulation.vertex(a.handle).out_edges()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MeshEdges<'a> {
|
pub struct MeshEdges<'a> {
|
||||||
iter: Box<dyn Iterator<Item = DirectedEdgeHandle<'a, MeshVertex, (), (), ()>> + 'a>,
|
iter: Box<dyn Iterator<Item = DirectedEdgeHandle<'a, Vertex, (), (), ()>> + 'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Iterator for MeshEdges<'a> {
|
impl<'a> Iterator for MeshEdges<'a> {
|
||||||
|
|
@ -212,7 +223,7 @@ impl<'a> visit::IntoEdges for &'a Mesh {
|
||||||
|
|
||||||
fn edges(self, a: Self::NodeId) -> Self::Edges {
|
fn edges(self, a: Self::NodeId) -> Self::Edges {
|
||||||
MeshEdges {
|
MeshEdges {
|
||||||
iter: Box::new(self.triangulation.vertex(a).out_edges()),
|
iter: Box::new(self.triangulation.vertex(a.handle).out_edges()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue