From 68df57930891efd5724598bfc251d9a88647aedc Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Mon, 22 Apr 2024 17:59:19 +0200 Subject: [PATCH] overlay: minimize ratsnest edge lengths --- src/overlay/ratsnest.rs | 14 +++++++------- src/triangulation.rs | 36 +++++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/overlay/ratsnest.rs b/src/overlay/ratsnest.rs index d3f5041..4150a13 100644 --- a/src/overlay/ratsnest.rs +++ b/src/overlay/ratsnest.rs @@ -15,22 +15,22 @@ use crate::{ }, geometry::primitive::PrimitiveShapeTrait, layout::Layout, - triangulation::{GetVertexIndex, Triangulation}, + triangulation::{GetVertexIndex, Triangulation, TriangulationEdgeWeight}, }; #[derive(Debug, Clone, Copy)] -pub struct TriangulationWeight { +pub struct VertexWeight { vertex: FixedDotIndex, pub pos: Point, } -impl GetVertexIndex for TriangulationWeight { +impl GetVertexIndex for VertexWeight { fn vertex_index(&self) -> FixedDotIndex { self.vertex } } -impl HasPosition for TriangulationWeight { +impl HasPosition for VertexWeight { type Scalar = f64; fn position(&self) -> Point2 { Point2::new(self.pos.x(), self.pos.y()) @@ -38,7 +38,7 @@ impl HasPosition for TriangulationWeight { } pub struct Ratsnest { - graph: StableUnGraph, + graph: StableUnGraph, } impl Ratsnest { @@ -55,7 +55,7 @@ impl Ratsnest { match node { PrimitiveIndex::FixedDot(dot) => { - triangulation.add_vertex(TriangulationWeight { + triangulation.add_vertex(VertexWeight { vertex: dot, pos: center, })?; @@ -70,7 +70,7 @@ impl Ratsnest { Ok(this) } - pub fn graph(&self) -> &StableUnGraph { + pub fn graph(&self) -> &StableUnGraph { &self.graph } } diff --git a/src/triangulation.rs b/src/triangulation.rs index c4f3b69..94343ef 100644 --- a/src/triangulation.rs +++ b/src/triangulation.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use geo::{point, Point}; +use geo::{point, EuclideanDistance, Point}; use petgraph::visit; use spade::{handles::FixedVertexHandle, DelaunayTriangulation, HasPosition, InsertionError}; @@ -75,23 +75,29 @@ impl + HasPosition + HasPosition> visit::Data for Triangulation { type NodeWeight = W; - type EdgeWeight = (); + type EdgeWeight = TriangulationEdgeWeight; } #[derive(Debug, Clone, Copy, PartialEq)] pub struct TriangulationEdgeReference { from: I, to: I, + weight: TriangulationEdgeWeight, } impl visit::EdgeRef for TriangulationEdgeReference { type NodeId = I; type EdgeId = (I, I); - type Weight = (); + type Weight = TriangulationEdgeWeight; fn source(&self) -> Self::NodeId { self.from @@ -102,7 +108,7 @@ impl visit::EdgeRef for TriangulationEdgeReference { } fn weight(&self) -> &Self::Weight { - &() + &self.weight } fn id(&self) -> Self::EdgeId { @@ -133,9 +139,14 @@ impl<'a, I: Copy + PartialEq + GetNodeIndex, W: GetVertexIndex + HasPosition< fn edge_references(self) -> Self::EdgeReferences { Box::new( spade::Triangulation::directed_edges(&self.triangulation).map(|edge| { + let from = self.vertex(edge.from().fix()); + let to = self.vertex(edge.to().fix()); TriangulationEdgeReference { - from: self.vertex(edge.from().fix()), - to: self.vertex(edge.to().fix()), + from, + to, + weight: TriangulationEdgeWeight { + length: self.position(from).euclidean_distance(&self.position(to)), + }, } }), ) @@ -151,9 +162,16 @@ impl<'a, I: Copy + PartialEq + GetNodeIndex, W: GetVertexIndex + HasPosition< Box::new( spade::Triangulation::vertex(&self.triangulation, self.handle(node)) .out_edges() - .map(|edge| TriangulationEdgeReference { - from: self.vertex(edge.from().fix()), - to: self.vertex(edge.to().fix()), + .map(|edge| { + let from = self.vertex(edge.from().fix()); + let to = self.vertex(edge.to().fix()); + TriangulationEdgeReference { + from, + to, + weight: TriangulationEdgeWeight { + length: self.position(from).euclidean_distance(&self.position(to)), + }, + } }), ) }