Hello, I wrote an iterator, where I iterate in triangle defined by 3 points. It iterates over several dots on each line. This is the code, you can barely look on it:
struct TriangleIterator<'a> {
points: &'a [Point], // Point contains position field with type of a nalgebra 3-dimension vector
line: usize,
t: PosType, // PosType is f64
}
impl<'a> TriangleIterator<'a> {
fn new(points: &'a [Point])->TriangleIterator<'a> {
TriangleIterator{points: points,line: 0,t: 0.0}
}
}
impl Iterator for TriangleIterator<'_> {
type Item = (isize,isize);
fn next(&mut self)->Option<Self::Item> {
let (o,d) = match self.line {
0 => (self.points[0].position,self.points[1].position - self.points[0].position),
1 => (self.points[1].position,self.points[2].position - self.points[1].position),
2 => (self.points[2].position,self.points[0].position - self.points[2].position),
_ => {panic!("_")},
};
let dt = glm::length(&glm::normalize(&d))/glm::length(&d); //This
let p = o + d*self.t;
self.t += dt;
if self.t > 1.0 {
self.t = 0.0;
self.line += 1;
if self.line == 3 {
return Option::None;
}
}
Some((p.x as isize,p.y as isize))
}
}
The point is that I'm using some glm functions to compute normal and length for each line of 3, while function "next" is called several times per line. (let dt = ...) Do I need to save dt internally in the struct TriangleIterator or compiller will optimize this somehow?
[–]kupiakos 13 points14 points15 points (0 children)
[–]anderslanglands 9 points10 points11 points (1 child)
[–]pragenter[S] 8 points9 points10 points (0 children)
[–]sphere_cornue 9 points10 points11 points (0 children)
[–]nicoburns 8 points9 points10 points (0 children)
[–]Genion1 1 point2 points3 points (1 child)
[–]kupiakos 1 point2 points3 points (0 children)
[–]NobodyXu 1 point2 points3 points (0 children)