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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Rendering Cells
use crate::POLY_SUBPIXEL_SCALE;
use crate::POLY_SUBPIXEL_SHIFT;
use crate::POLY_SUBPIXEL_MASK;
use std::cmp::min;
use std::cmp::max;
/// Rendering Cell
///
/// Effectively Represents a Pixel
#[derive(Debug,Copy,Clone,PartialEq, Default)]
pub struct Cell { // cell_aa
/// Cell x position
pub x: i64,
/// Cell y position
pub y: i64,
/// Cell coverage
pub cover: i64,
/// Cell area
pub area: i64,
}
impl Cell {
/// Create a new Cell
///
/// Cover and Area are both 0
fn new() -> Self {
Cell { x: std::i64::MAX, y: std::i64::MAX, cover: 0, area: 0 }
}
/// Create new cell at position (x,y)
pub fn at(x: i64, y: i64) -> Self {
let mut c = Cell::new();
c.x = x;
c.y = y;
c
}
/// Compare two cell positionsx
pub fn equal(&self, x: i64, y: i64) -> bool {
self.x - x == 0 && self.y - y == 0
}
/// Test if cover and area are equal to 0
pub fn is_empty(&self) -> bool {
self.cover == 0 && self.area == 0
}
}
/// Collection of Cells
#[derive(Debug,Default)]
pub struct RasterizerCell {
/// Cells
cells: Vec<Cell>,
/// Minimum x value of current cells
pub min_x: i64,
/// Maximum x value of current cells
pub max_x: i64,
/// Minimum y value of current cells
pub min_y: i64,
/// Maximum y value of current cells
pub max_y: i64,
/// Cells sorted by y position, then x position
pub sorted_y: Vec<Vec<Cell>>,
}
impl RasterizerCell {
/// Create new Cell collection
pub fn new() -> Self {
Self { cells: vec![],
min_x: std::i64::MAX,
min_y: std::i64::MAX,
max_x: std::i64::MIN,
max_y: std::i64::MIN,
sorted_y: vec![],
}
}
/// Clear cells
pub fn reset(&mut self) {
self.max_x = std::i64::MIN;
self.max_y = std::i64::MIN;
self.min_x = std::i64::MAX;
self.min_y = std::i64::MAX;
self.sorted_y.clear(); // Not sure if this should be cleared
self.cells.clear(); // Not sure if this should be cleared
}
/// Return total number of cells
pub fn total_cells(&self) -> usize {
self.cells.len()
}
/// Sort cells into sorted_y cells
///
/// Cells are distributed into y bins, then sorted by x value
pub fn sort_cells(&mut self) {
//eprintln!("SORT_CELLS MAX_Y: {} N: {} MIN_Y: {}", self.max_y, self.cells.len(), self.min_y);
if ! self.sorted_y.is_empty() || self.max_y < 0 {
return;
}
// Distribute into
self.sorted_y = vec![vec![]; (self.max_y+1) as usize];
for c in self.cells.iter() {
if c.y >= 0 {
let y = c.y as usize;
//eprintln!("SORT_CELLS SORTING {:?}", c);
self.sorted_y[y].push(c.clone());
}
}
// Sort by the x value
for i in 0 .. self.sorted_y.len() {
//eprintln!("SORT_CELLS: y: {} len: {}", i, self.sorted_y[i].len());
self.sorted_y[i].sort_by(|a,b| (a.x).cmp(&b.x));
}
}
/// Return number of cells in a specific y row
pub fn scanline_num_cells(&self, y: i64) -> usize {
self.sorted_y[y as usize].len()
}
/// Returns the cells of a specific y row
pub fn scanline_cells(&self, y: i64) -> &[Cell] {
& self.sorted_y[y as usize]
}
//pub fn add_curr_cell(&mut self, new_cell: Cell) {
// self.cells.push( new_cell );
//}
/// Determine if the last cell is equal to (x,y) and is empty
///
// fn curr_cell_is_set(&self, x: i64, y: i64) -> bool {
// match self.cells.last() {
// None => true,
// Some(cur) => {
// //eprintln!("SET_CURR_CELL: {} {} EQUAL: {} EMPTY: {}", x, y, cur.equal(x,y), !cur.is_empty());
// ! cur.equal(x,y) && ! cur.is_empty()
// }
// }
// }
/// Determine if the current cell is located at (x,y)
fn curr_cell_not_equal(&self, x: i64, y: i64) -> bool {
match self.cells.last() {
None => true,
Some(cur) => ! cur.equal(x,y),
}
}
/// Remove last cell is cover and area are equal to 0
fn pop_last_cell_if_empty(&mut self) {
let n = self.cells.len();
if n == 0 {
return;
}
if self.cells[n-1].area == 0 && self.cells[n-1].cover == 0 {
self.cells.pop();
} else {
self.show_last_cell();
}
}
/// Print the last cell
fn show_last_cell(&self) {
if let Some(c) = self.cells.last() {
println!("ADD_CURR_CELL: {} {} area {} cover {} len {}", c.x,c.y,c.area,c.cover, self.cells.len());
}
}
/// Create new cell at (x,y)
///
/// Current cell is removed if empty (cover and area equal to 0)
/// New cell is added to cell list
fn set_curr_cell(&mut self, x: i64, y: i64) {
//eprintln!("SET_CURR_CELL: {} {}", x,y);
if self.curr_cell_not_equal(x, y) {
//eprintln!("ADD_CURR_CELL: {} {} {} ", x,y, self.cells.len()+1);
self.pop_last_cell_if_empty();
self.cells.push( Cell::at(x,y) );
}
}
/// Create and update new cells
fn render_hline(&mut self, ey: i64, x1: i64, y1: i64, x2: i64, y2: i64) {
let ex1 = x1 >> POLY_SUBPIXEL_SHIFT;
let ex2 = x2 >> POLY_SUBPIXEL_SHIFT;
let fx1 = x1 & POLY_SUBPIXEL_MASK;
let fx2 = x2 & POLY_SUBPIXEL_MASK;
// Horizontal Line
if y1 == y2 {
self.set_curr_cell(ex2, ey);
return;
}
// Single Cell
if ex1 == ex2 {
//eprintln!("RENDER_HLINE LEN: {}", self.cells.len());
let m_curr_cell = self.cells.last_mut().unwrap();
m_curr_cell.cover += y2-y1;
m_curr_cell.area += (fx1 + fx2) * (y2-y1);
/*
eprintln!("INCR0 cover {} area {} dcover {} darea {} x,y {} {}",
m_curr_cell.cover,
m_curr_cell.area,
y2-y1,
(fx1 + fx2) * (y2-y1), m_curr_cell.x, m_curr_cell.y);
*/
return;
}
//eprintln!("RENDER_HLINE ADJCENT CELLS SAME LINE {} {}", x1,x2);
// Adjacent Cells on Same Line
let (mut p, first, incr, dx) = if x2-x1 < 0 {
(fx1 * (y2-y1), 0,-1, x1-x2)
} else {
((POLY_SUBPIXEL_SCALE - fx1) * (y2-y1), POLY_SUBPIXEL_SCALE, 1, x2-x1)
};
let mut delta = p / dx;
let mut xmod = p % dx;
if xmod < 0 {
delta -= 1;
xmod += dx;
}
{
let m_curr_cell = self.cells.last_mut().unwrap();
m_curr_cell.cover += delta;
m_curr_cell.area += (fx1 + first) * delta;
/*
eprintln!("INCR1 cover {} area {} dcover {} darea {} x,y {} {}",
m_curr_cell.cover,
m_curr_cell.area,
delta,
(fx1 + first) * delta, m_curr_cell.x, m_curr_cell.y);
*/
}
let mut ex1 = ex1 + incr;
self.set_curr_cell(ex1, ey);
let mut y1 = y1 + delta;
if ex1 != ex2 {
p = POLY_SUBPIXEL_SCALE * (y2 - y1 + delta);
let mut lift = p / dx;
let mut rem = p % dx;
if rem < 0 {
lift -= 1;
rem += dx;
}
xmod -= dx;
while ex1 != ex2 {
delta = lift;
xmod += rem;
if xmod >= 0 {
xmod -= dx;
delta += 1;
}
{
let m_curr_cell = self.cells.last_mut().unwrap();
m_curr_cell.cover += delta;
m_curr_cell.area += POLY_SUBPIXEL_SCALE * delta;
/*
eprintln!("INCR2 cover {} area {} dcover {} darea {} x,y {} {}",
m_curr_cell.cover,
m_curr_cell.area,
delta,
POLY_SUBPIXEL_SCALE * delta, m_curr_cell.x, m_curr_cell.y);
*/
}
y1 += delta;
ex1 += incr;
self.set_curr_cell(ex1, ey);
}
}
delta = y2-y1;
{
let m_curr_cell = self.cells.last_mut().unwrap();
m_curr_cell.cover += delta;
m_curr_cell.area += (fx2 + POLY_SUBPIXEL_SCALE - first) * delta;
/*
eprintln!("INCR3 cover {} area {} dcover {} darea {} x,y {} {}",
m_curr_cell.cover,
m_curr_cell.area,
delta,
(fx2 + POLY_SUBPIXEL_SCALE - first) * delta, m_curr_cell.x, m_curr_cell.y);
*/
}
}
/// Draw a line from (x1,y1) to (x2,y2)
///
/// Cells are added to the cells collection with cover and area values
///
/// Input coordinates are at subpixel scale
pub fn line(&mut self, x1: i64, y1: i64, x2: i64, y2: i64) {
println!("ADD_PATH: LINE: {} {} -> {} {}", x1,y1, x2,y2);
let dx_limit = 16384 << POLY_SUBPIXEL_SHIFT;
let dx = x2 - x1;
// Split long lines in half
if dx >= dx_limit || dx <= -dx_limit {
let cx = (x1 + x2) / 2;
let cy = (y1 + y2) / 2;
self.line(x1, y1, cx, cy);
self.line(cx, cy, x2, y2);
}
let dy = y2-y1;
// Downshift
let ex1 = x1 >> POLY_SUBPIXEL_SHIFT;
let ex2 = x2 >> POLY_SUBPIXEL_SHIFT;
let ey1 = y1 >> POLY_SUBPIXEL_SHIFT;
let ey2 = y2 >> POLY_SUBPIXEL_SHIFT;
let fy1 = y1 & POLY_SUBPIXEL_MASK;
let fy2 = y2 & POLY_SUBPIXEL_MASK;
self.min_x = min(ex2, min(ex1, self.min_x));
self.min_y = min(ey2, min(ey1, self.min_y));
self.max_x = max(ex2, max(ex1, self.max_x));
self.max_y = max(ey2, max(ey1, self.max_y));
self.set_curr_cell(ex1, ey1);
//eprintln!("EY1, EY2: {} {}", ey1, ey2);
// Horizontal Line
if ey1 == ey2 {
//eprintln!("LINE EY1 = EY2");
self.render_hline(ey1, x1, fy1, x2, fy2);
let n = self.cells.len();
if self.cells[n-1].area == 0 && self.cells[n-1].cover == 0 {
self.cells.pop();
}
return;
}
if dx == 0 {
//eprintln!("LINE DX = 0");
let ex = x1 >> POLY_SUBPIXEL_SHIFT;
let two_fx = (x1 - (ex << POLY_SUBPIXEL_SHIFT)) << 1;
let (first, incr) = if dy < 0 {
(0, -1)
} else {
(POLY_SUBPIXEL_SCALE, 1)
};
//let x_from = x1;
let delta = first - fy1;
{
let m_curr_cell = self.cells.last_mut().unwrap();
m_curr_cell.cover += delta;
m_curr_cell.area += two_fx * delta;
}
let mut ey1 = ey1 + incr;
self.set_curr_cell(ex, ey1);
let delta = first + first - POLY_SUBPIXEL_SCALE;
let area = two_fx * delta;
while ey1 != ey2 {
{
let m_curr_cell = self.cells.last_mut().unwrap();
m_curr_cell.cover = delta;
m_curr_cell.area = area;
}
ey1 += incr;
self.set_curr_cell(ex, ey1);
}
let delta = fy2 - POLY_SUBPIXEL_SCALE + first;
{
let m_curr_cell = self.cells.last_mut().unwrap();
m_curr_cell.cover += delta;
m_curr_cell.area += two_fx * delta;
}
return;
}
//eprintln!("LINE RENDER MULTPLE LINES {} {}", dx, dy);
// Render Multiple Lines
let (p,first,incr, dy) = if dy < 0 {
(fy1 * dx, 0, -1, -dy)
} else {
((POLY_SUBPIXEL_SCALE - fy1) * dx, POLY_SUBPIXEL_SCALE, 1, dy)
};
let mut delta = p / dy;
let mut xmod = p % dy;
if xmod < 0 {
delta -= 1;
xmod += dy;
}
let mut x_from = x1 + delta;
self.render_hline(ey1, x1, fy1, x_from, first);
let mut ey1 = ey1 + incr;
self.set_curr_cell(x_from >> POLY_SUBPIXEL_SHIFT, ey1);
if ey1 != ey2 {
let p = POLY_SUBPIXEL_SCALE * dx;
let mut lift = p / dy;
let mut rem = p % dy;
if rem < 0 {
lift -= 1;
rem += dy;
}
xmod -= dy;
while ey1 != ey2 {
delta = lift;
xmod += rem;
if xmod >= 0 {
xmod -= dy;
delta += 1;
}
let x_to = x_from + delta;
self.render_hline(ey1, x_from, POLY_SUBPIXEL_SCALE - first, x_to, first);
x_from = x_to;
ey1 += incr;
self.set_curr_cell(x_from >> POLY_SUBPIXEL_SHIFT, ey1);
}
}
self.render_hline(ey1, x_from, POLY_SUBPIXEL_SCALE - first, x2, fy2);
self.pop_last_cell_if_empty();
}
}