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
use crate::fill::is_after;
use crate::math::{point, Point};
use crate::Side;
use crate::{FillGeometryBuilder, VertexId};

/// Helper class that generates a triangulation from a sequence of vertices describing a monotone
/// polygon (used internally by the `FillTessellator`).
pub(crate) struct BasicMonotoneTessellator {
    stack: Vec<MonotoneVertex>,
    previous: MonotoneVertex,
    triangles: Vec<(VertexId, VertexId, VertexId)>,
}

#[derive(Copy, Clone, Debug)]
struct MonotoneVertex {
    pos: Point,
    id: VertexId,
    side: Side,
}

impl BasicMonotoneTessellator {
    pub fn new() -> Self {
        BasicMonotoneTessellator {
            stack: Vec::new(),
            triangles: Vec::new(),
            // Some placeholder value that will be replaced right away.
            previous: MonotoneVertex {
                pos: Point::new(0.0, 0.0),
                id: VertexId(0),
                side: Side::Left,
            },
        }
    }

    pub fn begin(&mut self, pos: Point, id: VertexId) {
        debug_assert!(id != VertexId::INVALID);
        let first = MonotoneVertex {
            pos,
            id,
            side: Side::Left,
        };
        self.previous = first;

        self.triangles.clear();
        self.triangles.reserve(16);

        self.stack.clear();
        self.stack.reserve(16);
        self.stack.push(first);
    }

    #[inline]
    pub fn vertex(&mut self, pos: Point, id: VertexId, side: Side) {
        self.monotone_vertex(MonotoneVertex { pos, id, side });
    }

    fn monotone_vertex(&mut self, current: MonotoneVertex) {
        debug_assert!(current.id != VertexId::INVALID);
        // cf. test_fixed_to_f32_precision
        debug_assert!(current.pos.y >= self.previous.pos.y);
        debug_assert!(!self.stack.is_empty());

        let changed_side = current.side != self.previous.side;

        if changed_side {
            for i in 0..(self.stack.len() - 1) {
                let mut a = self.stack[i];
                let mut b = self.stack[i + 1];

                let winding = (a.pos - b.pos).cross(current.pos - b.pos) >= 0.0;

                if !winding {
                    std::mem::swap(&mut a, &mut b);
                }

                self.push_triangle(&a, &b, &current);
            }
            self.stack.clear();
            self.stack.push(self.previous);
        } else {
            let mut last_popped = self.stack.pop();
            while !self.stack.is_empty() {
                let mut a = last_popped.unwrap();
                let mut b = *self.stack.last().unwrap();

                if current.side.is_right() {
                    std::mem::swap(&mut a, &mut b);
                }

                let cross = (current.pos - b.pos).cross(a.pos - b.pos);
                if cross >= 0.0 {
                    self.push_triangle(&b, &a, &current);
                    last_popped = self.stack.pop();
                } else {
                    break;
                }
            }
            if let Some(item) = last_popped {
                self.stack.push(item);
            }
        }

        self.stack.push(current);
        self.previous = current;
    }

    pub fn end(&mut self, pos: Point, id: VertexId) {
        let side = self.previous.side.opposite();
        self.vertex(pos, id, side);
        self.stack.clear();
    }

    #[inline]
    fn push_triangle(&mut self, a: &MonotoneVertex, b: &MonotoneVertex, c: &MonotoneVertex) {
        let threshold = -0.0625; // Floating point errors stroke again :(
        debug_assert!((a.pos - b.pos).cross(c.pos - b.pos) >= threshold);

        self.push_triangle_ids(a.id, b.id, c.id);
    }

    fn push_triangle_ids(&mut self, a: VertexId, b: VertexId, c: VertexId) {
        debug_assert!(a != b);
        debug_assert!(b != c);
        debug_assert!(a != c);
        debug_assert!(a != VertexId::INVALID);
        debug_assert!(b != VertexId::INVALID);
        debug_assert!(c != VertexId::INVALID);

        self.triangles.push((a, b, c));
    }

    pub fn flush(&mut self, output: &mut dyn FillGeometryBuilder) {
        for &(a, b, c) in &self.triangles {
            output.add_triangle(a, b, c);
        }
        self.triangles.clear();
    }
}

#[test]
fn test_monotone_tess() {
    println!(" ------------ ");
    {
        let mut tess = BasicMonotoneTessellator::new();
        tess.begin(point(0.0, 0.0), VertexId(0));
        tess.vertex(point(-1.0, 1.0), VertexId(1), Side::Left);
        tess.end(point(1.0, 2.0), VertexId(2));
        assert_eq!(tess.triangles.len(), 1);
    }
    println!(" ------------ ");
    {
        let mut tess = BasicMonotoneTessellator::new();
        tess.begin(point(0.0, 0.0), VertexId(0));
        tess.vertex(point(1.0, 1.0), VertexId(1), Side::Right);
        tess.vertex(point(-1.5, 2.0), VertexId(2), Side::Left);
        tess.vertex(point(-1.0, 3.0), VertexId(3), Side::Left);
        tess.vertex(point(1.0, 4.0), VertexId(4), Side::Right);
        tess.end(point(0.0, 5.0), VertexId(5));
        assert_eq!(tess.triangles.len(), 4);
    }
    println!(" ------------ ");
    {
        let mut tess = BasicMonotoneTessellator::new();
        tess.begin(point(0.0, 0.0), VertexId(0));
        tess.vertex(point(1.0, 1.0), VertexId(1), Side::Right);
        tess.vertex(point(3.0, 2.0), VertexId(2), Side::Right);
        tess.vertex(point(1.0, 3.0), VertexId(3), Side::Right);
        tess.vertex(point(1.0, 4.0), VertexId(4), Side::Right);
        tess.vertex(point(4.0, 5.0), VertexId(5), Side::Right);
        tess.end(point(0.0, 6.0), VertexId(6));
        assert_eq!(tess.triangles.len(), 5);
    }
    println!(" ------------ ");
    {
        let mut tess = BasicMonotoneTessellator::new();
        tess.begin(point(0.0, 0.0), VertexId(0));
        tess.vertex(point(-1.0, 1.0), VertexId(1), Side::Left);
        tess.vertex(point(-3.0, 2.0), VertexId(2), Side::Left);
        tess.vertex(point(-1.0, 3.0), VertexId(3), Side::Left);
        tess.vertex(point(-1.0, 4.0), VertexId(4), Side::Left);
        tess.vertex(point(-4.0, 5.0), VertexId(5), Side::Left);
        tess.end(point(0.0, 6.0), VertexId(6));
        assert_eq!(tess.triangles.len(), 5);
    }
    println!(" ------------ ");
}

struct SideEvents {
    // We decide whether we have to flush a convex vertex chain based on
    // whether the two sides are far apart. reference_point.x contains the
    // center-most x coordinate of the current chain of vertex. It is not
    // enough because a previous chain on one side can still interfere with
    // a chain of vertex on the opposite side that is still current, so we
    // also keep track of a conservative reference x value which is not not
    // relaxed until the opposite side has is flushed. See issue #623.
    reference_point: Point,
    conservative_reference_x: f32,
    // A convex chain of vertex events for this side that can be tessellated
    // without interference from the other side.
    events: Vec<VertexId>,
    prev: Point,
    last: MonotoneVertex,
}

impl SideEvents {
    #[inline]
    fn push(&mut self, vertex: MonotoneVertex) {
        self.events.push(vertex.id);
        self.prev = self.last.pos;
        self.last = vertex;
    }
}

pub struct AdvancedMonotoneTessellator {
    tess: BasicMonotoneTessellator,
    left: SideEvents,
    right: SideEvents,
    flushing: bool,
}

impl AdvancedMonotoneTessellator {
    pub fn new() -> Self {
        let zero = point(0.0, 0.0);
        let dummy_vtx = MonotoneVertex {
            pos: zero,
            id: VertexId(0),
            side: Side::Left,
        };
        AdvancedMonotoneTessellator {
            left: SideEvents {
                events: Vec::with_capacity(16),
                reference_point: zero,
                conservative_reference_x: 0.0,
                prev: zero,
                last: dummy_vtx,
            },
            right: SideEvents {
                events: Vec::with_capacity(16),
                reference_point: zero,
                conservative_reference_x: 0.0,
                prev: zero,
                last: dummy_vtx,
            },
            tess: BasicMonotoneTessellator::new(),
            flushing: false,
        }
    }

    pub fn begin(&mut self, pos: Point, id: VertexId) {
        self.tess.begin(pos, id);
        self.left.reference_point = pos;
        self.left.conservative_reference_x = pos.x;
        self.right.reference_point = pos;
        self.right.conservative_reference_x = pos.x;
        self.left.prev = pos;
        self.right.prev = pos;
        self.flushing = false;

        self.left.events.clear();
        self.right.events.clear();
        self.left.push(MonotoneVertex {
            pos,
            id,
            side: Side::Left,
        });
        self.right.push(MonotoneVertex {
            pos,
            id,
            side: Side::Right,
        });
    }

    pub fn vertex(&mut self, pos: Point, id: VertexId, side: Side) {
        match side {
            Side::Left => {
                self.left.reference_point.x = self.left.reference_point.x.max(pos.x);
                self.left.conservative_reference_x = self
                    .left
                    .conservative_reference_x
                    .max(self.left.reference_point.x);
            }
            Side::Right => {
                self.right.reference_point.x = self.right.reference_point.x.min(pos.x);
                self.right.conservative_reference_x = self
                    .right
                    .conservative_reference_x
                    .min(self.right.reference_point.x);
            }
        }

        let dx = self.right.conservative_reference_x - self.left.conservative_reference_x;

        let (side_ev, opposite_side_ev) = match side {
            Side::Left => (&mut self.left, &mut self.right),
            Side::Right => (&mut self.right, &mut self.left),
        };

        let dy = pos.y - side_ev.reference_point.y;
        let sides_are_close = dx < dy * 0.1;

        let len = side_ev.events.len();
        let outward_turn = if !sides_are_close && len >= 2 {
            let sign = match side {
                Side::Left => 1.0,
                Side::Right => -1.0,
            };
            let prev = side_ev.prev;
            let last = side_ev.last.pos;
            (prev - last).cross(pos - last) * sign < 0.0
        } else {
            false
        };

        if outward_turn || sides_are_close {
            // To ensure that we don't break the ordering of the vertices forwarded to
            // the inner tessellator we have to flush the opposite side as well
            let must_flush_opposite_side = is_after(side_ev.last.pos, opposite_side_ev.last.pos);

            if must_flush_opposite_side {
                if let Some(v) = flush_side(opposite_side_ev, side.opposite(), &mut self.tess) {
                    self.tess.monotone_vertex(v);
                    side_ev.conservative_reference_x = side_ev.reference_point.x;
                }
            }

            if let Some(v) = flush_side(side_ev, side, &mut self.tess) {
                self.tess.monotone_vertex(v);
                opposite_side_ev.conservative_reference_x = opposite_side_ev.reference_point.x;
            }
        }

        side_ev.push(MonotoneVertex { pos, id, side });
    }

    pub fn end(&mut self, pos: Point, id: VertexId) {
        let a = flush_side(&mut self.left, Side::Left, &mut self.tess);
        let b = flush_side(&mut self.right, Side::Right, &mut self.tess);
        match (a, b) {
            (Some(v), None) | (None, Some(v)) => {
                self.tess.monotone_vertex(v);
            }
            (Some(mut v1), Some(mut v2)) => {
                if is_after(v1.pos, v2.pos) {
                    std::mem::swap(&mut v1, &mut v2);
                }
                self.tess.monotone_vertex(v1);
                self.tess.monotone_vertex(v2);
            }
            (None, None) => {}
        }

        self.tess.end(pos, id);
    }

    pub fn flush(&mut self, output: &mut dyn FillGeometryBuilder) {
        self.tess.flush(output);
    }
}

#[inline(never)]
fn flush_side(
    side: &mut SideEvents,
    s: Side,
    tess: &mut BasicMonotoneTessellator,
) -> Option<MonotoneVertex> {
    let len = side.events.len();
    if len < 2 {
        return None;
    }

    let mut step = 1;
    while step * 2 < len {
        let mut last_index = 0;
        let imax = (len - 1) / (2 * step);
        for i in 0..imax {
            let mut a = i * 2 * step;
            let mut b = a + step;
            last_index = b + step;
            if s == Side::Right {
                std::mem::swap(&mut a, &mut b);
            }
            tess.push_triangle_ids(side.events[a], side.events[b], side.events[last_index]);
        }

        if last_index + step < len {
            let mut b = last_index;
            let mut c = last_index + step;
            if s == Side::Right {
                std::mem::swap(&mut b, &mut c);
            }

            tess.push_triangle_ids(side.events[0], side.events[b], side.events[c]);
        }

        step *= 2;
    }

    side.events.clear();
    side.push(side.last);
    side.reference_point = side.last.pos;

    Some(side.last)
}

pub type MonotoneTessellator = AdvancedMonotoneTessellator;