use crate::{
halfedge::HalfEdgeImplMeshType,
math::IndexType,
mesh::{EdgeBasics, HalfEdge, MeshBasics},
};
pub struct IncidentToVertexIterator<'a, T: HalfEdgeImplMeshType + 'a> {
is_first: bool,
first: T::E,
current: T::E,
mesh: &'a T::Mesh,
}
impl<'a, T: HalfEdgeImplMeshType> IncidentToVertexIterator<'a, T> {
pub fn new(first: T::Edge, mesh: &'a T::Mesh) -> Self {
Self {
first: first.id(),
current: first.id(),
mesh,
is_first: true,
}
}
pub fn empty(mesh: &'a T::Mesh) -> Self {
Self {
first: IndexType::max(),
current: IndexType::max(),
mesh,
is_first: true,
}
}
}
impl<'a, T: HalfEdgeImplMeshType> Iterator for IncidentToVertexIterator<'a, T> {
type Item = T::Edge;
fn next(&mut self) -> Option<Self::Item> {
if self.current == IndexType::max() {
return None;
}
let current = self.mesh.edge(self.current);
if self.is_first {
self.is_first = false;
return Some(current.clone());
}
let next = current.twin(self.mesh).next(self.mesh);
debug_assert!(
next.origin_id() == self.mesh.edge(self.first).origin_id(),
"The edge wheel around vertex {} is not closed. The mesh is invalid.",
next.origin_id()
);
if next.id() == self.first {
return None;
} else {
self.current = next.id();
return Some(next);
}
}
}