1 | //! Closed shape thick segment iterator. |
2 | |
3 | use crate::{ |
4 | geometry::Point, |
5 | primitives::common::{LineJoin, StrokeOffset, ThickSegment}, |
6 | }; |
7 | |
8 | /// Closed shape thick segments iterator. |
9 | /// |
10 | /// Iterates over all line segments in the polyline, returning a 6-sided shape as a [`ThickSegment`] |
11 | /// for each segment. These are tessellated and are used to produce scanline intersections. |
12 | /// |
13 | /// Unlike [`ThickSegmentIter`], this iterator closes the shape with a final line between the |
14 | /// start and end points. |
15 | /// |
16 | /// [`ThickSegment`]: super::thick_segment::ThickSegment |
17 | /// [`ThickSegmentIter`]: super::thick_segment_iter::ThickSegmentIter |
18 | #[derive (Clone, Debug)] |
19 | #[cfg_attr (feature = "defmt" , derive(::defmt::Format))] |
20 | pub struct ClosedThickSegmentIter<'a> { |
21 | windows: core::slice::Windows<'a, Point>, |
22 | first_join: LineJoin, |
23 | start_join: LineJoin, |
24 | width: u32, |
25 | stroke_offset: StrokeOffset, |
26 | points: &'a [Point], |
27 | stop: bool, |
28 | idx: usize, |
29 | } |
30 | |
31 | static EMPTY: &[Point; 0] = &[]; |
32 | |
33 | impl<'a> ClosedThickSegmentIter<'a> { |
34 | /// Create a new thick segments iterator. |
35 | pub fn new(points: &'a [Point], width: u32, stroke_offset: StrokeOffset) -> Self { |
36 | if let [start, end] = points { |
37 | // Single line segment. |
38 | let start_join = LineJoin::start(*start, *end, width, stroke_offset); |
39 | |
40 | Self { |
41 | windows: EMPTY.windows(3), |
42 | start_join, |
43 | width, |
44 | stroke_offset, |
45 | points, |
46 | stop: false, |
47 | first_join: start_join, |
48 | idx: 1, |
49 | } |
50 | } else if points.is_empty() { |
51 | Self::empty() |
52 | } else { |
53 | let windows = points.windows(3); |
54 | |
55 | let start_join = LineJoin::from_points( |
56 | *points.last().unwrap(), |
57 | points[0], |
58 | points[1], |
59 | width, |
60 | stroke_offset, |
61 | ); |
62 | |
63 | Self { |
64 | windows, |
65 | start_join, |
66 | width, |
67 | stroke_offset, |
68 | points, |
69 | stop: false, |
70 | first_join: start_join, |
71 | idx: 1, |
72 | } |
73 | } |
74 | } |
75 | |
76 | /// Empty |
77 | fn empty() -> Self { |
78 | Self { |
79 | windows: EMPTY.windows(3), |
80 | start_join: LineJoin::empty(), |
81 | width: 0, |
82 | stroke_offset: StrokeOffset::None, |
83 | points: EMPTY, |
84 | stop: true, |
85 | first_join: LineJoin::empty(), |
86 | idx: 1, |
87 | } |
88 | } |
89 | } |
90 | |
91 | impl<'a> Iterator for ClosedThickSegmentIter<'a> { |
92 | type Item = ThickSegment; |
93 | |
94 | fn next(&mut self) -> Option<Self::Item> { |
95 | if self.stop { |
96 | return None; |
97 | } |
98 | |
99 | self.idx += 1; |
100 | |
101 | let end_join = if let Some([start, mid, end]) = self.windows.next() { |
102 | LineJoin::from_points(*start, *mid, *end, self.width, self.stroke_offset) |
103 | } else if self.idx == self.points.len() { |
104 | // The join at the end of the line. This will become the start join of the closing |
105 | // segment. |
106 | let start = self.points.get(self.points.len() - 2)?; |
107 | let mid = self.points.last()?; |
108 | let end = self.points.first()?; |
109 | |
110 | LineJoin::from_points(*start, *mid, *end, self.width, self.stroke_offset) |
111 | } else { |
112 | // Final closing line between start/end. |
113 | self.stop = true; |
114 | |
115 | self.first_join |
116 | }; |
117 | |
118 | let segment = ThickSegment::new(self.start_join, end_join); |
119 | |
120 | self.start_join = end_join; |
121 | |
122 | Some(segment) |
123 | } |
124 | } |
125 | |