use crate::{
geometry::Point,
primitives::common::{LineJoin, StrokeOffset, ThickSegment},
};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub struct ClosedThickSegmentIter<'a> {
windows: core::slice::Windows<'a, Point>,
first_join: LineJoin,
start_join: LineJoin,
width: u32,
stroke_offset: StrokeOffset,
points: &'a [Point],
stop: bool,
idx: usize,
}
static EMPTY: &[Point; 0] = &[];
impl<'a> ClosedThickSegmentIter<'a> {
pub fn new(points: &'a [Point], width: u32, stroke_offset: StrokeOffset) -> Self {
if let [start, end] = points {
let start_join = LineJoin::start(*start, *end, width, stroke_offset);
Self {
windows: EMPTY.windows(3),
start_join,
width,
stroke_offset,
points,
stop: false,
first_join: start_join,
idx: 1,
}
} else if points.is_empty() {
Self::empty()
} else {
let windows = points.windows(3);
let start_join = LineJoin::from_points(
*points.last().unwrap(),
points[0],
points[1],
width,
stroke_offset,
);
Self {
windows,
start_join,
width,
stroke_offset,
points,
stop: false,
first_join: start_join,
idx: 1,
}
}
}
fn empty() -> Self {
Self {
windows: EMPTY.windows(3),
start_join: LineJoin::empty(),
width: 0,
stroke_offset: StrokeOffset::None,
points: EMPTY,
stop: true,
first_join: LineJoin::empty(),
idx: 1,
}
}
}
impl<'a> Iterator for ClosedThickSegmentIter<'a> {
type Item = ThickSegment;
fn next(&mut self) -> Option<Self::Item> {
if self.stop {
return None;
}
self.idx += 1;
let end_join = if let Some([start, mid, end]) = self.windows.next() {
LineJoin::from_points(*start, *mid, *end, self.width, self.stroke_offset)
} else if self.idx == self.points.len() {
let start = self.points.get(self.points.len() - 2)?;
let mid = self.points.last()?;
let end = self.points.first()?;
LineJoin::from_points(*start, *mid, *end, self.width, self.stroke_offset)
} else {
self.stop = true;
self.first_join
};
let segment = ThickSegment::new(self.start_join, end_join);
self.start_join = end_join;
Some(segment)
}
}