From d603a9140d0195659c795bc6c19634e370c84cd8 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Wed, 8 Jan 2020 00:17:57 +0100 Subject: [PATCH] more iterators --- fractional/src/easel.rs | 82 +++++++++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 23 deletions(-) diff --git a/fractional/src/easel.rs b/fractional/src/easel.rs index 85a8e34..504cf64 100644 --- a/fractional/src/easel.rs +++ b/fractional/src/easel.rs @@ -56,7 +56,7 @@ pub struct Coordinate(pub i32, pub i32, pub T); #[derive(Debug, Clone)] pub struct Coordinates(pub Vec>); -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct LineIterator where T: Debug { a :Option> , b :Coordinate @@ -74,6 +74,7 @@ where T: Add + Debug + Copy + From { type Item = Coordinate; fn next(&mut self) -> Option { + println!("== LineIterator next: {:?}", self); match self.a { None => None, Some(a) => { @@ -263,37 +264,70 @@ pub struct VertexIterator<'a,T> where T: Debug { p :&'a Polygon, top :usize, current :Option, - inner :Option>, + edge :Option>, direction :Direction, } +impl<'a,T> VertexIterator<'a,T> +where T: Add + Sub + Div + + Debug + Copy + From { + + fn new(p :&'a Polygon, direction :Direction) -> Self { + let mut v = VertexIterator { p: p + , top: p.vert_min() + , current: Some(p.vert_min()) + , edge: None + , direction: direction }; + v.next_edge(); + println!("== new: {:?}", v); + v + } + + // if this yields "None" we are finished. + fn next_edge(&mut self) -> Option> { + let current = self.current?; + let next = self.p.next_y(current, self.direction)?; + let mut edge = self.p.vertex(current).edge_iter(&self.p.vertex(next)); + + println!("== next: {:?} {:?} {:?}", current, next, edge); + + match edge.next() { + // It should be impossible that a new edge iterator has no values + // at all… anyway, just in case I handle it here. + None => self.next_edge(), + Some(_) => { + self.current = Some(next); + self.edge = Some(edge); + self.edge + }, + } + } +} + impl<'a,T> Iterator for VertexIterator<'a,T> where T: Add + Sub + Div + Debug + Copy + From { type Item = Coordinate; fn next(&mut self) -> Option { - let inner = match self.inner { - Some(i) => i, - None => { - let current = self.current?; - let next = self.p.next_y(current, self.direction)?; - self.p.vertex(current).edge_iter(&self.p.vertex(next)) - }, - } + // if for whatever reason edge is "None" finish this iterator. + let next = self.edge?.next(); - match self.current { - None => None, - Some(c) => { - let r = self.p.vertex(c); - self.current = self.p.next_y(c, self.direction); - Some(r) + println!("== next: {:?}", next); + + match next { + Some(_) => next, + None => { + self.next_edge()?; + self.next() }, } } } -impl Polygon where T: Copy + Debug { +impl Polygon +where T: Add + Sub + Div + + Copy + Debug + From { fn vert_min<'a>(&'a self) -> usize { let Polygon(Coordinates(cs)) = self; @@ -313,11 +347,7 @@ impl Polygon where T: Copy + Debug { } fn left_vertices(&self) -> VertexIterator { - VertexIterator { p: &self - , top: self.vert_min() - , current: Some(self.vert_min()) - , inner: None - , direction: Direction::Left } + VertexIterator::new(self, Direction::Left) } fn left(&self, v :usize) -> usize { @@ -353,7 +383,8 @@ impl Polygon where T: Copy + Debug { , c :usize , n :usize , d :Direction) -> Option - where T: Copy + Debug { + where T: Add + Sub + Div + + Copy + Debug + From { if c == n { None } else { @@ -362,6 +393,8 @@ impl Polygon where T: Copy + Debug { match ny.cmp(&cy) { cmp::Ordering::Less => None, + // TODO On equal we need to find out which one of both to + // keep in the list… (the outermost) cmp::Ordering::Equal => inner(p, c, p.step(n, d), d), cmp::Ordering::Greater => Some(n), } @@ -446,7 +479,10 @@ where T: Add + Sub + Div r = append_edge(&mut r_edge, r, Direction::Right); } + let l_edge2 :Vec> = self.left_vertices().collect(); + println!("== [{}] {:?}", l_edge.len(), l_edge); + println!("== [{}] {:?}", l_edge2.len(), l_edge2); println!("== [{}] {:?}", r_edge.len(), r_edge); // TODO we always miss the last scanline…