overlay: don't create ratlines for connected nodes

This commit is contained in:
Mikolaj Wielgus 2024-04-23 21:39:16 +02:00
parent 5ce35a5357
commit 133d14b8bc
1 changed files with 18 additions and 1 deletions

View File

@ -3,7 +3,8 @@ use geo::Point;
use petgraph::{ use petgraph::{
data::FromElements, data::FromElements,
stable_graph::{NodeIndex, StableUnGraph}, stable_graph::{NodeIndex, StableUnGraph},
visit::{self, EdgeRef, NodeIndexable}, unionfind::UnionFind,
visit::{self, EdgeRef, IntoEdgeReferences, NodeIndexable},
}; };
use spade::{HasPosition, InsertionError, Point2}; use spade::{HasPosition, InsertionError, Point2};
@ -89,6 +90,22 @@ impl Ratsnest {
this.graph = this.graph =
StableUnGraph::from_elements(petgraph::algo::min_spanning_tree(&triangulation)); StableUnGraph::from_elements(petgraph::algo::min_spanning_tree(&triangulation));
let mut unionfind = UnionFind::new(layout.drawing().geometry().graph().node_bound());
for edge in layout.drawing().geometry().graph().edge_references() {
unionfind.union(edge.source(), edge.target());
}
this.graph.retain_edges(|g, i| {
if let Some((from, to)) = g.edge_endpoints(i) {
let from_index = g.node_weight(from).unwrap().vertex_index().node_index();
let to_index = g.node_weight(to).unwrap().vertex_index().node_index();
!unionfind.equiv(from_index, to_index)
} else {
true
}
});
Ok(this) Ok(this)
} }