-
Notifications
You must be signed in to change notification settings - Fork 24
/
path.rs
193 lines (163 loc) · 4.89 KB
/
path.rs
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
#![allow(dead_code)]
use crate::defs::*;
pub struct Interval {
pub a: f32,
pub b: f32,
}
pub struct PathSegment {
pub cvs: [LocalPoint; 3],
pub next: Option<usize>,
previous: Option<usize>,
}
impl PathSegment {
pub fn new(a: LocalPoint, b: LocalPoint, c: LocalPoint) -> Self {
Self {
cvs: [a, b, c],
next: None,
previous: None,
}
}
pub fn y_interval(&self) -> Interval {
Interval {
// Fatten the interval slightly to prevent artifacts by
// slightly missing a curve in a band.
a: self.cvs[0].y.min(self.cvs[1].y).min(self.cvs[2].y) - 1.0,
b: self.cvs[0].y.max(self.cvs[1].y).max(self.cvs[2].y) + 1.0,
}
}
}
#[derive(PartialEq, PartialOrd)]
struct PathScannerNode {
coord: f32,
seg: usize,
end: bool,
}
pub struct PathScanner {
pub segments: Vec<PathSegment>,
nodes: Vec<PathScannerNode>,
index: usize,
pub interval: Interval,
pub first: Option<usize>,
}
impl PathScanner {
pub fn new() -> Self {
Self {
segments: vec![],
nodes: vec![],
index: 0,
interval: Interval { a: 0.0, b: 0.0 },
first: None,
}
}
pub fn init(&mut self) {
// Close the path if necessary.
if let Some(first) = self.segments.first() {
if let Some(last) = self.segments.last() {
let start = first.cvs[0];
let end = last.cvs[2];
if start != end {
self.segments.push(PathSegment {
cvs: [end, start.lerp(end, 0.5), start],
next: None,
previous: None,
})
}
}
}
self.nodes.clear();
self.index = 0;
for i in 0..self.segments.len() {
let y_interval = self.segments[i].y_interval();
self.nodes.push(PathScannerNode {
coord: y_interval.a,
seg: i,
end: false,
});
self.nodes.push(PathScannerNode {
coord: y_interval.b,
seg: i,
end: true,
});
}
self.nodes.sort_by(|a, b| a.partial_cmp(b).unwrap());
}
pub fn begin(&mut self, cvs: &[LocalPoint]) {
self.segments.clear();
let mut i = 0;
while i < cvs.len() - 2 {
self.segments.push(PathSegment {
cvs: [cvs[i], cvs[i + 1], cvs[i + 2]],
next: None,
previous: None,
});
i += 2;
}
self.init();
}
pub fn next(&mut self) -> bool {
let y = self.nodes[self.index].coord;
self.interval.a = y;
let n = self.nodes.len();
while self.index < n && self.nodes[self.index].coord == y {
let node = &self.nodes[self.index];
assert!(node.seg < self.segments.len());
if node.end {
if let Some(prev) = self.segments[node.seg].previous {
self.segments[prev].next = self.segments[node.seg].next;
}
if let Some(next) = self.segments[node.seg].next {
self.segments[next].previous = self.segments[node.seg].previous;
}
if self.first == Some(node.seg) {
self.first = self.segments[node.seg].next
}
self.segments[node.seg].next = None;
self.segments[node.seg].previous = None;
} else {
self.segments[node.seg].next = self.first;
if let Some(first) = self.first {
self.segments[first].previous = Some(node.seg);
}
self.first = Some(node.seg);
}
self.index += 1;
}
if self.index < n {
self.interval.b = self.nodes[self.index].coord
}
self.index < n
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_path_scanner() {
let mut scan = PathScanner::new();
let cvs = vec![
[1.0, 0.0].into(),
[1.0, 1.0].into(),
[0.0, 1.0].into(),
[-1.0, 1.0].into(),
[-1.0, 0.0].into(),
[-1.0, -1.0].into(),
[0.0, -1.0].into(),
[1.0, -1.0].into(),
[1.0, 0.0].into(),
];
scan.begin(&cvs);
assert_eq!(scan.segments.len(), 4);
while scan.next() {
print!(
"interval {:?} {:?} active: ",
scan.interval.a, scan.interval.b
);
let mut index = scan.first;
while let Some(i) = index {
print!("{:?} ", i);
index = scan.segments[i].next;
}
println!();
}
}
}