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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::Point;
use petgraph::{Directed, Undirected};
use petgraph::visit::EdgeRef;
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Segment {
pub start: Point,
pub end: Point,
pub kind: SegmentKind,
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum SegmentKind {
Blank,
NonBlank,
}
pub type PointGraph = petgraph::Graph<Point, (), Undirected, u32>;
pub type EulerGraph = petgraph::Graph<Point, SegmentKind, Undirected, u32>;
pub type EulerCircuit = petgraph::Graph<Point, SegmentKind, Directed, u32>;
type EdgeIndex = petgraph::graph::EdgeIndex<u32>;
type NodeIndex = petgraph::graph::NodeIndex<u32>;
#[derive(Clone)]
pub struct Segments<I> {
points: I,
last_point: Option<Point>,
}
impl<I> Iterator for Segments<I>
where
I: Iterator<Item = Point>,
{
type Item = Segment;
fn next(&mut self) -> Option<Self::Item> {
while let Some(end) = self.points.next() {
let start = match self.last_point.replace(end) {
None => continue,
Some(last) => last,
};
if start == end {
continue;
}
let kind = if start.is_blank() && end.is_blank() {
SegmentKind::Blank
} else {
SegmentKind::NonBlank
};
return Some(Segment { start, end, kind })
}
None
}
}
pub fn points_to_segments<I>(points: I) -> Segments<I::IntoIter>
where
I: IntoIterator<Item = Point>,
{
let points = points.into_iter();
let last_point = None;
Segments { points, last_point }
}
pub fn segments_to_point_graph<I>(segments: I) -> PointGraph
where
I: IntoIterator<Item = Segment>,
{
#[derive(Eq, Hash, PartialEq)]
struct HashPoint {
pos: [i32; 2],
rgb: [u32; 3],
}
impl From<Point> for HashPoint {
fn from(p: Point) -> Self {
let [px, py] = p.position;
let [pr, pg, pb] = p.color;
let x = (px * std::i16::MAX as f32) as i32;
let y = (py * std::i16::MAX as f32) as i32;
let r = (pr * std::u16::MAX as f32) as u32;
let g = (pg * std::u16::MAX as f32) as u32;
let b = (pb * std::u16::MAX as f32) as u32;
let pos = [x, y];
let rgb = [r, g, b];
HashPoint { pos, rgb }
}
}
let mut g = PointGraph::default();
let mut pt_to_id = hashbrown::HashMap::new();
for seg in segments {
match seg.kind {
SegmentKind::Blank => (),
SegmentKind::NonBlank => {
let ha = HashPoint::from(seg.start);
let hb = HashPoint::from(seg.end);
let na = *pt_to_id.entry(ha).or_insert_with(|| g.add_node(seg.start));
let nb = *pt_to_id.entry(hb).or_insert_with(|| g.add_node(seg.end));
g.add_edge(na, nb, ());
}
}
}
g
}
pub fn point_graph_to_euler_graph(pg: &PointGraph) -> EulerGraph {
let ccs = petgraph::algo::kosaraju_scc(pg);
let euler_components: hashbrown::HashSet<_> = ccs.iter()
.enumerate()
.filter(|(_, cc)| cc.iter().all(|&n| pg.edges(n).count() % 2 == 0))
.map(|(i, _)| i)
.collect();
struct ToConnect {
prev: NodeIndex,
inner: Vec<NodeIndex>,
next: NodeIndex,
}
let mut to_connect = vec![];
for (i, cc) in ccs.iter().enumerate() {
if euler_components.contains(&i) {
let n = cc[0];
to_connect.push(ToConnect { prev: n, inner: vec![], next: n });
} else {
let v: Vec<_> = cc.iter().filter(|&&n| pg.edges(n).count() % 2 != 0).collect();
assert_eq!(
v.len() % 2, 0,
"expected even number of odd-degree nodes for non-Euler component",
);
let prev = *v[0];
let inner = v[1..v.len()-1].iter().map(|&&n| n).collect();
let next = *v[v.len()-1];
to_connect.push(ToConnect { prev, inner, next });
}
}
let mut pairs = vec![];
let mut iter = to_connect.iter().peekable();
while let Some(this) = iter.next() {
for ch in this.inner.chunks(2) {
pairs.push((ch[0], ch[1]));
}
match iter.peek() {
Some(next) => pairs.push((this.next, next.prev)),
None => pairs.push((this.next, to_connect[0].prev)),
}
}
let mut eg = pg.map(|_n_ix, n| n.clone(), |_e_ix, _| SegmentKind::NonBlank);
for (na, nb) in pairs {
eg.add_edge(na, nb, SegmentKind::Blank);
}
eg
}
pub fn euler_graph_to_euler_circuit(eg: &EulerGraph) -> EulerCircuit {
let start = match eg.node_indices().next() {
Some(n_ix) => n_ix,
None => return Default::default(),
};
let mut visit_order: Vec<EdgeIndex> = vec![];
let mut n = start;
loop {
let e_ref = eg.edges(n).next().expect("expected a strongly connected euler graph");
visit_order.push(e_ref.id());
if e_ref.target() == start {
break;
}
}
unimplemented!()
}