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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! Outline rasterizer.
//!
//! Port of `agg_rasterizer_outline.h` — simple wireframe rasterizer that feeds
//! vertices directly to a renderer (no scanline conversion). Suitable for
//! previewing paths or drawing non-anti-aliased outlines.
use crate::basics::{is_closed, is_end_poly, is_move_to, is_stop, VertexSource};
// ============================================================================
// RendererPrimitivesLike trait
// ============================================================================
/// Trait for renderers that can draw primitive lines.
///
/// Matches the subset of `RendererPrimitives` API used by `RasterizerOutline`.
pub trait RendererPrimitivesLike {
type Color: Clone;
fn coord(c: f64) -> i32;
fn move_to(&mut self, x: i32, y: i32);
fn line_to(&mut self, x: i32, y: i32, last: bool);
fn set_line_color(&mut self, c: Self::Color);
}
// ============================================================================
// RasterizerOutline
// ============================================================================
/// Outline rasterizer.
///
/// Reads vertices from a `VertexSource` and feeds them directly to a
/// primitive renderer. No anti-aliasing or scanline conversion — just
/// Bresenham lines.
///
/// Port of C++ `rasterizer_outline<Renderer>`.
pub struct RasterizerOutline<'a, Ren: RendererPrimitivesLike> {
ren: &'a mut Ren,
start_x: i32,
start_y: i32,
vertices: u32,
}
impl<'a, Ren: RendererPrimitivesLike> RasterizerOutline<'a, Ren> {
pub fn new(ren: &'a mut Ren) -> Self {
Self {
ren,
start_x: 0,
start_y: 0,
vertices: 0,
}
}
/// Move to a new position (subpixel coordinates).
pub fn move_to(&mut self, x: i32, y: i32) {
self.vertices = 1;
self.start_x = x;
self.start_y = y;
self.ren.move_to(x, y);
}
/// Draw a line to (x, y) (subpixel coordinates).
pub fn line_to(&mut self, x: i32, y: i32) {
self.vertices += 1;
self.ren.line_to(x, y, false);
}
/// Move to a floating-point position (converts to subpixel).
pub fn move_to_d(&mut self, x: f64, y: f64) {
self.move_to(Ren::coord(x), Ren::coord(y));
}
/// Draw a line to a floating-point position (converts to subpixel).
pub fn line_to_d(&mut self, x: f64, y: f64) {
self.line_to(Ren::coord(x), Ren::coord(y));
}
/// Close the current polygon by drawing a line back to the start.
pub fn close(&mut self) {
if self.vertices > 2 {
self.line_to(self.start_x, self.start_y);
}
self.vertices = 0;
}
/// Add a single vertex with a path command.
pub fn add_vertex(&mut self, x: f64, y: f64, cmd: u32) {
if is_move_to(cmd) {
self.move_to_d(x, y);
} else if is_end_poly(cmd) {
if is_closed(cmd) {
self.close();
}
} else {
self.line_to_d(x, y);
}
}
/// Add all vertices from a vertex source.
pub fn add_path<VS: VertexSource>(&mut self, vs: &mut VS, path_id: u32) {
vs.rewind(path_id);
let mut x = 0.0;
let mut y = 0.0;
loop {
let cmd = vs.vertex(&mut x, &mut y);
if is_stop(cmd) {
break;
}
self.add_vertex(x, y, cmd);
}
}
/// Render multiple paths with different colors.
pub fn render_all_paths<VS: VertexSource>(
&mut self,
vs: &mut VS,
colors: &[Ren::Color],
path_ids: &[u32],
) {
for i in 0..colors.len().min(path_ids.len()) {
self.ren.set_line_color(colors[i].clone());
self.add_path(vs, path_ids[i]);
}
}
}
// ============================================================================
// Tests
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
use crate::basics::{VertexSource, PATH_CMD_LINE_TO, PATH_CMD_MOVE_TO, PATH_CMD_STOP};
#[derive(Default)]
struct MockRenderer {
moves: Vec<(i32, i32)>,
lines: Vec<(i32, i32)>,
}
impl RendererPrimitivesLike for MockRenderer {
type Color = u32;
fn coord(c: f64) -> i32 {
(c * 256.0) as i32
}
fn move_to(&mut self, x: i32, y: i32) {
self.moves.push((x, y));
}
fn line_to(&mut self, x: i32, y: i32, _last: bool) {
self.lines.push((x, y));
}
fn set_line_color(&mut self, _c: u32) {}
}
#[test]
fn test_move_and_line_to() {
let mut ren = MockRenderer::default();
let mut ras = RasterizerOutline::new(&mut ren);
ras.move_to(10, 20);
ras.line_to(30, 40);
assert_eq!(ren.moves.len(), 1);
assert_eq!(ren.moves[0], (10, 20));
assert_eq!(ren.lines.len(), 1);
assert_eq!(ren.lines[0], (30, 40));
}
#[test]
fn test_close() {
let mut ren = MockRenderer::default();
let mut ras = RasterizerOutline::new(&mut ren);
ras.move_to(0, 0);
ras.line_to(100, 0);
ras.line_to(100, 100);
ras.close();
// Should draw line back to (0, 0)
assert_eq!(ren.lines.len(), 3);
assert_eq!(ren.lines[2], (0, 0));
}
#[test]
fn test_close_with_fewer_than_3_vertices() {
let mut ren = MockRenderer::default();
let mut ras = RasterizerOutline::new(&mut ren);
ras.move_to(0, 0);
ras.line_to(100, 0);
ras.close();
// Only 2 vertices — close() should not add a line
assert_eq!(ren.lines.len(), 1);
}
#[test]
fn test_add_path() {
struct TrianglePath {
idx: usize,
}
impl VertexSource for TrianglePath {
fn rewind(&mut self, _path_id: u32) {
self.idx = 0;
}
fn vertex(&mut self, x: &mut f64, y: &mut f64) -> u32 {
let verts: [(f64, f64, u32); 4] = [
(0.0, 0.0, PATH_CMD_MOVE_TO),
(10.0, 0.0, PATH_CMD_LINE_TO),
(5.0, 10.0, PATH_CMD_LINE_TO),
(0.0, 0.0, PATH_CMD_STOP),
];
if self.idx >= verts.len() {
return PATH_CMD_STOP;
}
let v = verts[self.idx];
*x = v.0;
*y = v.1;
self.idx += 1;
v.2
}
}
let mut ren = MockRenderer::default();
let mut ras = RasterizerOutline::new(&mut ren);
let mut path = TrianglePath { idx: 0 };
ras.add_path(&mut path, 0);
assert_eq!(ren.moves.len(), 1);
assert_eq!(ren.lines.len(), 2);
}
#[test]
fn test_move_to_d() {
let mut ren = MockRenderer::default();
let mut ras = RasterizerOutline::new(&mut ren);
ras.move_to_d(1.5, 2.5);
// coord(1.5) = 384, coord(2.5) = 640
assert_eq!(ren.moves[0], (384, 640));
}
}