makepad-vector 1.0.0

Makepad vector api
Documentation
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
use crate::geometry::{LineSegment, Point, Trapezoid};
use crate::internal_iter::InternalIterator;
use crate::path::{LinePathCommand, LinePathIterator};
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::mem;
use std::ops::Range;

/// Converts a sequence of line path commands to a sequence of trapezoids. The line path commands
/// should define a set of closed contours.
#[derive(Clone, Debug, Default)]
pub struct Trapezoidator {
    event_queue: BinaryHeap<Event>,
    active_segments: Vec<ActiveSegment>,
}

impl Trapezoidator {
    /// Creates a new trapezoidator.
    pub fn new() -> Trapezoidator {
        Trapezoidator::default()
    }

    /// Returns an iterator over trapezoids corresponding to the given iterator over line path
    /// commands.
    pub fn trapezoidate<P: LinePathIterator>(&mut self, path: P) -> Option<Trapezoidate> {
        let mut initial_point = None;
        let mut current_point = None;
        if !path.for_each(&mut |command| {
            match command {
                LinePathCommand::MoveTo(p) => {
                    initial_point = Some(p);
                    current_point = Some(p);
                }
                LinePathCommand::LineTo(p) => {
                    let p0 = current_point.replace(p).unwrap();
                    if !self.push_events_for_segment(LineSegment::new(p0, p)) {
                        return false;
                    }
                }
                LinePathCommand::Close => {
                    let p = initial_point.take().unwrap();
                    let p0 = current_point.replace(p).unwrap();
                    if !self.push_events_for_segment(LineSegment::new(p0, p)) {
                        return false;
                    }
                }
            }
            true
        }){
            return None
        };
        Some(Trapezoidate {
            trapezoidator: self,
        })
    }

    /// Adds events for the given segment to the event queue.
    fn push_events_for_segment(&mut self, segment: LineSegment) -> bool {
        // Determine the winding, the leftmost point, and the rightmost point of the segment.
        //
        // The winding is used to determine which regions are considered inside, and which are
        // considered outside. A region is considered inside if its winding is non-zero.
        // Conceptually, the winding of a region is determined by casting an imaginary ray from
        // any point inside the region to infinity in any direction, and adding the windings of
        // all segments that are intersected by the ray. The winding of a segment is +1 if it
        // intersects the ray from left to right, and -1 if it intersects the ray from right to
        // left.
        let (winding, p0, p1) = match segment.p0.partial_cmp(&segment.p1) {
            None => {
                // The endpoints of the segment cannot be compared, so the segment is invalid.
                // This can happen if the semgent has NaN coordinates. In this case, the input
                // as a whole is invalid, so we bail out early.
                return false;
            },
            Some(Ordering::Less) => (1, segment.p0, segment.p1),
            Some(Ordering::Equal) => {
                // The endpoints of the segment are equal, so the segment is empty. Empty segments
                // do not affect the output of the trapezoidation algorithm, so they can safely be
                // ignored.
                return true;
            }
            Some(Ordering::Greater) => (-1, segment.p1, segment.p0),
        };
        // Add an event to the event queue for the leftmost point of the segment. This is where
        // the segment starts intersecting the sweepline.
        self.event_queue.push(Event {
            point: p0,
            pending_segment: Some(PendingSegment { winding, p1 }),
        });
        // Add an event to the event queue for the rightmost point of the segment. This is where
        // the segment stops intersecting the sweepline.
        self.event_queue.push(Event {
            point: p1,
            pending_segment: None,
        });
        true
    }

    /// Removes all events at the next point where an event occurs from the event queue.
    /// 
    /// Returns the point at which the events occur, or `None` if the event queue is empty.
    /// Appends the pending segments that start intersecting the sweepline at this point to
    /// `pending_segments`.
    fn pop_events_for_next_point(
        &mut self,
        pending_segments: &mut Vec<PendingSegment>,
    ) -> Option<Point> {
        // Pop an event from the event queue. This will be the first event at the next point.
        self.event_queue.pop().map(|event| {
            // If there is a segment that starts intersecting the sweepline at this point, add it
            // to `pending_segments`.
            if let Some(pending_segment) = event.pending_segment {
                pending_segments.push(pending_segment)
            }
            // Keep popping events while they occur at the same point as the first one.
            while let Some(&next_event) = self.event_queue.peek() {
                if next_event != event {
                    break;
                }
                self.event_queue.pop();
                // If there is a segment that starts intersecting the sweepline at this point, add
                // it to `pending_segments`.
                if let Some(pending_segment) = next_event.pending_segment {
                    pending_segments.push(pending_segment);
                }
            }
            event.point
        })
    }

    /// Handle all events that occur at the given point. `right_segments` is a list of segments that
    /// start intersecting the sweepline at this point. `trapezoid_segments` is scratch space for a
    /// list of segments for which we potentially have to generate trapezoids.
    fn handle_events_for_point<F>(
        &mut self,
        point: Point,
        right_segments: &mut Vec<PendingSegment>,
        trapezoid_segments: &mut Vec<ActiveSegment>,
        f: &mut F,
    ) -> bool
    where
        F: FnMut(Trapezoid) -> bool,
    {
        // Find the range of active segments that are incident with the given point.
        let mut incident_segment_range = self.find_incident_segment_range(point);
        // If there is an active segment that lies below the current point, and the region below it
        // is considered outside, then this segment is the lower boundary of a trapezoid. We split
        // the segment where it intersects the sweepline, adding the part on the left to the list of
        // trapezoid segments, while keeping the part on the right in the list of active segments.
        if let Some(trapezoid_segment) =
            self.find_trapezoid_segment_below(point, incident_segment_range.start)
        {
            trapezoid_segments.push(trapezoid_segment);
        }
        // If there are any active segments that are incident with the given point, we remove them
        // from the list of active segments, and then split each segment where it intersects the
        // sweepline, adding the part on the left to the list of trapezoid segments, while adding
        // the part on the right to the list of right segments.
        self.remove_incident_segments(
            point,
            &mut incident_segment_range,
            right_segments,
            trapezoid_segments,
        );
        // Sort the right segments by their slope.
        self.sort_right_segments(point, right_segments);
        // Insert the right segments into the list of active segments, updating the range of
        // active segments that are incident with the given point accordingly.
        self.insert_right_segments(point, &mut incident_segment_range, right_segments);
        // If there is an active segment that lies above the current point, and the region below it
        // is considered inside, then this segment is the upper boundary of a trapezoid. We split the
        // the segment where it intersects the sweepline, adding the part on the left to the list of
        // trapezoid segments, while generating an event for the part on the right.
        if let Some(trapezoid_segment) =
            self.find_trapezoid_segment_above(point, incident_segment_range.end)
        {
            trapezoid_segments.push(trapezoid_segment);
        }
        // At this point, `trapezoid_segments` contains a list of segments that stop intersecting the
        // sweepline at the current point, and that potentially form trapezoid boundaries. We generate
        // trapezoids for these segments, and pass them to the given closure.
        self.generate_trapezoids(trapezoid_segments, f)
    }

    /// Finds the range of active segments that are incident with the given point.
    fn find_incident_segment_range(&self, point: Point) -> Range<usize> {
        Range {
            // Find the index of the first active segment that does not lie below the given point.
            start: self
                .active_segments
                .iter()
                .position(|active_segment| {
                    active_segment.segment.compare_to_point(point).unwrap() != Ordering::Less
                })
                .unwrap_or(self.active_segments.len()),
            // Find the index of the first active segment that lies above the given point.
            end: self
                .active_segments
                .iter()
                .rposition(|active_segment| {
                    active_segment.segment.compare_to_point(point).unwrap() != Ordering::Greater
                })
                .map_or(0, |index| index + 1),
        }
    }

    // Finds the first active segment that lies below the given point. If such a segment exists,
    // and the region below it is considered outside, then this segment is the lower boundary of a
    // trapezoid. We split the segment where it intersects the sweepline, keeping the part on the
    // right in the list of active segments, and returning the part on the left.
    fn find_trapezoid_segment_below(
        &mut self,
        point: Point,
        incident_segment_start: usize,
    ) -> Option<ActiveSegment> {
        if incident_segment_start == 0
            || !self.active_segments[incident_segment_start - 1].region_above.is_inside {
            return None;
        }
        let intersection = self.active_segments[incident_segment_start - 1]
            .segment
            .intersect_with_vertical_line(point.x)
            .unwrap_or(point);
        self.active_segments[incident_segment_start - 1].split_left_mut(intersection)
    }

    // Removes all active segments that are incident with the given point from the list of active
    // segments, and then splits each segment where it intersects the sweepline, adding the part
    // on the left to the list of trapezoid segments, while adding the part on the right to the
    // list of right segments.
    fn remove_incident_segments(
        &mut self,
        point: Point,
        incident_segment_range: &mut Range<usize>,
        right_segments: &mut Vec<PendingSegment>,
        trapezoid_segments: &mut Vec<ActiveSegment>,
    ) {
        trapezoid_segments.extend(
            Iterator::map(
                self.active_segments.drain(incident_segment_range.clone()),
                |mut active_segment| {
                    if let Some(pending_segment) = active_segment.split_right_mut(point) {
                        right_segments.push(pending_segment);
                    }
                    active_segment
                },
            )
            .filter(|active_segment| active_segment.segment.p0.x != active_segment.segment.p1.x),
        );
        incident_segment_range.end = incident_segment_range.start;
    }

    /// Sorts the given list of right segments by their slope, using the given point as the leftmost
    /// endpoint.
    fn sort_right_segments(&mut self, point: Point, right_segments: &mut Vec<PendingSegment>) {
        right_segments.sort_by(|&right_segment_0, &right_segment_1| {
            right_segment_0.compare(right_segment_1, point).unwrap()
        });
        let mut index_0 = 0;
        for index_1 in 1..right_segments.len() {
            let right_segment_1 = right_segments[index_1];
            let right_segment_0 = &mut right_segments[index_0];
            if right_segment_0.overlaps(right_segment_1, point) {
                if let Some(event) = right_segment_0.splice_mut(right_segment_1) {
                    self.event_queue.push(event);
                }
            } else {
                index_0 += 1;
                right_segments[index_0] = right_segment_1;
            }
        }
        right_segments.truncate(index_0 + 1);
    }

    // Inserts the given right segments into the list of active segments, updating the range of
    // active segments that are incident with the given point accordingly.
    fn insert_right_segments(
        &mut self,
        point: Point,
        incident_segment_range: &mut Range<usize>,
        right_segments: &[PendingSegment],
    ) {
        let mut lower_region = if incident_segment_range.end == 0 {
            Region {
                is_inside: false,
                winding: 0,
            }
        } else {
            self.active_segments[incident_segment_range.end - 1].region_above
        };
        self.active_segments.splice(
            incident_segment_range.end..incident_segment_range.end,
            Iterator::map(right_segments.iter(), |right_segment| {
                let upper_region = {
                    let winding = lower_region.winding + right_segment.winding;
                    Region {
                        is_inside: winding != 0,
                        winding,
                    }
                };
                let right_segment = ActiveSegment {
                    winding: right_segment.winding,
                    segment: LineSegment::new(point, right_segment.p1),
                    region_above: upper_region,
                };
                lower_region = upper_region;
                right_segment
            }),
        );
        incident_segment_range.end += right_segments.len();
    }

    // Finds the first active segment that lies above the given point. If such a segment exists,
    // and the region below it is considered inside, then this segment is the upper boundary of a
    // trapezoid. We split the segment where it intersects the sweepline, generating an event for
    // the part on the right, and returning the part on the left.
    fn find_trapezoid_segment_above(
        &mut self,
        point: Point,
        incident_segment_end: usize,
    ) -> Option<ActiveSegment> {
        if incident_segment_end == self.active_segments.len()
            || incident_segment_end == 0 
            || !self.active_segments[incident_segment_end - 1].region_above.is_inside
        {
            return None;
        }
        let intersection = self.active_segments[incident_segment_end]
            .segment
            .intersect_with_vertical_line(point.x)
            .unwrap();
        if let Some(pending_segment) =
            self.active_segments[incident_segment_end].split_right_mut(intersection)
        {
            self.event_queue.push(Event {
                point: intersection,
                pending_segment: Some(pending_segment),
            });
        }
        Some(self.active_segments[incident_segment_end])
    }

    fn generate_trapezoids<F>(&self, trapezoid_segments: &[ActiveSegment], f: &mut F) -> bool
    where
        F: FnMut(Trapezoid) -> bool,
    {
        for trapezoid_segment_pair in trapezoid_segments.windows(2) {
            if !trapezoid_segment_pair[0].region_above.is_inside {
                continue;
            }
            let lower_segment = trapezoid_segment_pair[0].segment;
            let upper_segment = trapezoid_segment_pair[1].segment;
            if !f(Trapezoid {
                xs: [lower_segment.p0.x as f32, lower_segment.p1.x as f32],
                ys: [
                    lower_segment.p0.y as f32,
                    lower_segment.p1.y as f32,
                    upper_segment.p0.y as f32,
                    upper_segment.p1.y as f32,
                ],
            }) {
                return false;
            }
        }
        true
    }
}

/// An iterator over trapezoids corresponding to the given iterator over line path commands.
#[derive(Debug)]
pub struct Trapezoidate<'a> {
    trapezoidator: &'a mut Trapezoidator,
}

impl<'a> InternalIterator for Trapezoidate<'a> {
    type Item = Trapezoid;

    fn for_each<F>(self, f: &mut F) -> bool
    where
        F: FnMut(Trapezoid) -> bool,
    {
        let mut right_segments = Vec::new();
        let mut trapezoid_segments = Vec::new();
        while let Some(point) = self.trapezoidator.pop_events_for_next_point(&mut right_segments) {
            let ok = self.trapezoidator.handle_events_for_point(
                point,
                &mut right_segments,
                &mut trapezoid_segments,
                f,
            );
            right_segments.clear();
            trapezoid_segments.clear();
            if !ok {
                return false;
            }
        }
        true
    }
}

// An event in the event queue.
#[derive(Clone, Copy, Debug)]
struct Event {
    // The point at which the event occurs.
    point: Point,
    // The pending segment that starts intersecting the sweepline at this point, if any.
    pending_segment: Option<PendingSegment>,
}

impl Eq for Event {}

impl Ord for Event {
    fn cmp(&self, other: &Event) -> Ordering {
        self.point.partial_cmp(&other.point).unwrap().reverse()
    }
}

impl PartialEq for Event {
    fn eq(&self, other: &Event) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

impl PartialOrd for Event {
    fn partial_cmp(&self, other: &Event) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// A segment that is pending insertion into the list of active segments.
///
/// We only store the rightmost endpoint for these segments, since the leftmost endpoint is
/// determined implicitly by the point at which the event that inserts the segment occurs.
#[derive(Clone, Copy, Debug, PartialEq)]
struct PendingSegment {
    // The winding of the segment.
    winding: i32,
    // The rightmost endpoint of the segment.
    p1: Point,
}

impl PendingSegment {
    fn to_segment(self, p0: Point) -> LineSegment {
        LineSegment::new(p0, self.p1)
    }

    fn overlaps(self, other: PendingSegment, p0: Point) -> bool {
        self.compare(other, p0) == Some(Ordering::Equal)
    }

    fn compare(self, other: PendingSegment, p0: Point) -> Option<Ordering> {
        if self.p1 <= other.p1 {
            other
                .to_segment(p0)
                .compare_to_point(self.p1)
                .map(|ordering| ordering.reverse())
        } else {
            self.to_segment(p0).compare_to_point(other.p1)
        }
    }

    fn splice_mut(&mut self, mut other: Self) -> Option<Event> {
        if other.p1 < self.p1 {
            mem::swap(self, &mut other);
        }
        self.winding += other.winding;
        if self.p1 == other.p1 {
            return None;
        }
        Some(Event {
            point: self.p1,
            pending_segment: Some(other),
        })
    }
}

/// A segment that currently intersects the sweepline,
#[derive(Clone, Copy, Debug, PartialEq)]
struct ActiveSegment {
    winding: i32,
    segment: LineSegment,
    region_above: Region,
}

impl ActiveSegment {
    // Splits this segment at the given point, returning the part on the left.
    fn split_left_mut(&mut self, p: Point) -> Option<ActiveSegment> {
        let p0 = self.segment.p0;
        if p == p0 {
            return None;
        }
        self.segment.p0 = p;
        Some(ActiveSegment {
            winding: self.winding,
            segment: LineSegment::new(p0, p),
            region_above: self.region_above,
        })
    }

    // Splits this segment at the given point, returning the part on the right.
    fn split_right_mut(&mut self, p: Point) -> Option<PendingSegment> {
        let p1 = self.segment.p1;
        if p == p1 {
            return None;
        }
        self.segment.p1 = p;
        Some(PendingSegment {
            winding: self.winding,
            p1,
        })
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct Region {
    is_inside: bool,
    winding: i32,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::path::{Path, PathIterator};

    #[test]
    fn test_square() {
        let mut path = Path::new();
        path.move_to(Point::new(0.0, 0.0));
        path.line_to(Point::new(1.0, 0.0));
        path.line_to(Point::new(1.0, 1.0));
        path.line_to(Point::new(0.0, 1.0));
        path.close();
        let mut trapezoidator = Trapezoidator::new();
        let trapezoids: Vec<_> = trapezoidator
            .trapezoidate(path.commands().linearize(0.1))
            .unwrap()
            .collect();
        assert_eq!(trapezoids, [
            Trapezoid { xs: [0.0, 1.0], ys: [0.0, 0.0, 1.0, 1.0] }
        ]);
    }
}