Browse Source

Complete fill with iterators.

master
Georg Hopp 6 years ago
parent
commit
42c575a948
Signed by: ghopp GPG Key ID: 4C5D226768784538
  1. 86
      fractional/src/easel.rs

86
fractional/src/easel.rs

@ -74,7 +74,6 @@ where T: Add<Output = T> + Debug + Copy + From<i32> {
type Item = Coordinate<T>; type Item = Coordinate<T>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
println!("== LineIterator next: {:?}", self);
match self.a { match self.a {
None => None, None => None,
Some(a) => { Some(a) => {
@ -273,14 +272,18 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
+ Debug + Copy + From<i32> { + Debug + Copy + From<i32> {
fn new(p :&'a Polygon<T>, direction :Direction) -> Self { fn new(p :&'a Polygon<T>, 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
let top = p.vert_min();
let next = p.next_y(top, direction);
let edge = match next {
None => None,
Some(next) => Some(p.vertex(top).edge_iter(&p.vertex(next))),
};
VertexIterator { p: p
, top: top
, current: next
, edge: edge
, direction: direction }
} }
// if this yields "None" we are finished. // if this yields "None" we are finished.
@ -289,8 +292,6 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
let next = self.p.next_y(current, self.direction)?; let next = self.p.next_y(current, self.direction)?;
let mut edge = self.p.vertex(current).edge_iter(&self.p.vertex(next)); let mut edge = self.p.vertex(current).edge_iter(&self.p.vertex(next));
println!("== next: {:?} {:?} {:?}", current, next, edge);
match edge.next() { match edge.next() {
// It should be impossible that a new edge iterator has no values // It should be impossible that a new edge iterator has no values
// at all… anyway, just in case I handle it here. // at all… anyway, just in case I handle it here.
@ -311,9 +312,7 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
// if for whatever reason edge is "None" finish this iterator. // if for whatever reason edge is "None" finish this iterator.
let next = self.edge?.next();
println!("== next: {:?}", next);
let next = self.edge.as_mut()?.next();
match next { match next {
Some(_) => next, Some(_) => next,
@ -350,6 +349,10 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
VertexIterator::new(self, Direction::Left) VertexIterator::new(self, Direction::Left)
} }
fn right_vertices(&self) -> VertexIterator<T> {
VertexIterator::new(self, Direction::Right)
}
fn left(&self, v :usize) -> usize { fn left(&self, v :usize) -> usize {
let Polygon(Coordinates(cs)) = self; let Polygon(Coordinates(cs)) = self;
@ -438,57 +441,14 @@ impl<T> Fillable<T> for Polygon<T>
where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
+ Debug + Clone + Copy + From<i32> { + Debug + Clone + Copy + From<i32> {
fn fill(&self) -> Coordinates<T> { fn fill(&self) -> Coordinates<T> {
let Polygon(Coordinates(cs)) = self;
let vert_min = self.vert_min();
println!("== vert_min: [{}] {:?}", vert_min, cs[vert_min]);
let mut r = (vert_min, self.next_y(vert_min, Direction::Right));
let mut l = (vert_min, self.next_y(vert_min, Direction::Left));
let mut l_edge :Vec<Coordinate<T>> = Vec::new();
let mut r_edge :Vec<Coordinate<T>> = Vec::new();
let append_edge = | v :&mut Vec<Coordinate<T>>
, e :(usize, Option<usize>)
, f :Direction | {
match e {
(_, None) => e,
(a, Some(b)) => {
let mut edge = cs[a].edge(&cs[b]);
v.append(&mut edge);
(b, self.next_y(b, f))
},
}
};
let print_current = |s :&str, e :(usize, Option<usize>)| {
match e.1 {
None => println!( "== {}: [{:?}] - {:?}"
, s, e.1, "None"),
Some(e) => println!( "== {}: [{:?}] - {:?}"
, s, e, cs[e]),
}
};
while l.1 != None || r.1 != None {
print_current("l", l);
l = append_edge(&mut l_edge, l, Direction::Left);
print_current("r", r);
r = append_edge(&mut r_edge, r, Direction::Right);
}
let l_edge2 :Vec<Coordinate<T>> = self.left_vertices().collect();
let scanlines = self.left_vertices().zip(self.right_vertices());
println!("== [{}] {:?}", l_edge.len(), l_edge);
println!("== [{}] {:?}", l_edge2.len(), l_edge2);
println!("== [{}] {:?}", r_edge.len(), r_edge);
// vertices is an iterator over all vertices making this polygon.
let vertices = scanlines.flat_map(|(l, r)| l.line_iter(&r));
// TODO we always miss the last scanline…
// TODO check what happend with at least 2 vertices with same y and
// different x…
// loop though edges…
// for debug only…
//let vertices :Vec<(Coordinate<T>)> = vertices.collect();
//println!("== [{}] {:?}", vertices.len(), vertices);
Coordinates(Vec::<Coordinate<T>>::new()) Coordinates(Vec::<Coordinate<T>>::new())
} }

Loading…
Cancel
Save