1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//! [`Route`]: an orthogonal connector route through a [`RoutingGraph`].
use alloc::vec::Vec;
use crate::geom::{Direction, Point};
use crate::graph::{RoutingGraph, VertexId};
/// An orthogonal route through a routing graph.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Route {
/// Every graph vertex the route passes, from source to target. Consecutive vertices
/// are graph neighbours. A route between coincident ports has one vertex.
pub vertices: Vec<VertexId>,
/// The route as a polyline: source, bend points, target. Collinear points are
/// merged, so consecutive segments alternate between horizontal and vertical.
pub points: Vec<Point>,
/// The total length (Manhattan, which equals Euclidean for orthogonal routes).
pub length: u64,
/// The number of bends.
pub bends: u32,
/// The cost the route was optimised for, in the units of the objective used.
pub cost: u64,
}
impl Route {
/// Builds a route from its vertex sequence, computing points, length and bends.
///
/// # Panics
///
/// Panics if `vertices` is empty or two consecutive vertices are not aligned.
#[must_use]
pub fn from_vertices(graph: &RoutingGraph, vertices: Vec<VertexId>, cost: u64) -> Self {
let raw: Vec<Point> = vertices.iter().map(|&v| graph.point(v)).collect();
let points = simplify(&raw);
let length = raw.windows(2).map(|w| w[0].manhattan(w[1])).sum();
let bends = u32::try_from(points.len().saturating_sub(2)).expect("fewer than 2^32 bends");
Self {
vertices,
points,
length,
bends,
cost,
}
}
/// Builds a route from a polyline whose straight runs follow graph edges, walking
/// the graph to recover every vertex.
///
/// Returns `None` if a point is not a vertex or a run is not a chain of edges.
#[must_use]
pub fn from_points(graph: &RoutingGraph, points: &[Point], cost: u64) -> Option<Self> {
let first = graph.vertex_at(*points.first()?)?;
let mut vertices = alloc::vec![first];
let mut v = first;
for pair in points.windows(2) {
if pair[0] == pair[1] {
continue;
}
let dir = Direction::between(pair[0], pair[1])?;
while graph.point(v) != pair[1] {
v = graph.neighbor(v, dir)?;
vertices.push(v);
}
}
Some(Self::from_vertices(graph, vertices, cost))
}
/// The direction of the first move, or `None` for a single-vertex route.
#[must_use]
pub fn first_direction(&self) -> Option<Direction> {
Direction::between(*self.points.first()?, *self.points.get(1)?)
}
/// The direction of the last move, or `None` for a single-vertex route.
#[must_use]
pub fn last_direction(&self) -> Option<Direction> {
let n = self.points.len();
Direction::between(*self.points.get(n.checked_sub(2)?)?, self.points[n - 1])
}
}
/// Removes repeated points and merges collinear runs of an orthogonal polyline.
#[must_use]
pub fn simplify(points: &[Point]) -> Vec<Point> {
let mut out: Vec<Point> = Vec::with_capacity(points.len());
for &p in points {
if out.last() == Some(&p) {
continue;
}
if out.len() >= 2 {
let a = out[out.len() - 2];
let b = out[out.len() - 1];
if (a.x == b.x && b.x == p.x) || (a.y == b.y && b.y == p.y) {
out.pop();
}
}
out.push(p);
}
out
}
#[cfg(test)]
mod tests {
//! Unit tests for polyline simplification.
use super::*;
/// Collinear and repeated points disappear; bends stay.
#[test]
fn simplify_merges_collinear() {
let pts = [
Point::new(0, 0),
Point::new(2, 0),
Point::new(2, 0),
Point::new(5, 0),
Point::new(5, 3),
Point::new(5, 7),
];
assert_eq!(
simplify(&pts),
[Point::new(0, 0), Point::new(5, 0), Point::new(5, 7)]
);
}
}