use super::HalfEdgeImpl;
use crate::{
halfedge::HalfEdgeImplMeshType,
mesh::{EdgeBasics, HalfEdge, MeshBasics},
};
impl<T: HalfEdgeImplMeshType> HalfEdgeImpl<T> {
#[inline(always)]
pub fn edges_face_mut<'a>(&'a self, mesh: &'a mut T::Mesh) -> ForwardEdgeIteratorMut<'a, T> {
ForwardEdgeIteratorMut::new(self.id(), mesh)
}
}
#[derive(Clone)]
pub struct ForwardEdgeIterator<'a, T: HalfEdgeImplMeshType + 'a> {
is_first: bool,
first: T::E,
current: HalfEdgeImpl<T>,
mesh: &'a T::Mesh,
}
impl<'a, T: HalfEdgeImplMeshType> ForwardEdgeIterator<'a, T> {
pub fn new(first: HalfEdgeImpl<T>, mesh: &'a T::Mesh) -> Self {
Self {
first: first.id(),
current: first,
mesh,
is_first: true,
}
}
}
impl<'a, T: HalfEdgeImplMeshType> Iterator for ForwardEdgeIterator<'a, T> {
type Item = T::Edge;
#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
if self.is_first {
self.is_first = false;
return Some(self.current.clone());
}
let next = self.current.next(self.mesh);
if next.id() == self.first {
return None;
} else {
self.current = next.clone();
return Some(next);
}
}
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let mut curr = self.current.clone();
let mut len = 1;
while curr.next(self.mesh).id() != self.first {
len += 1;
curr = curr.next(self.mesh);
}
(len, Some(len))
}
}
impl<'a, T: HalfEdgeImplMeshType> ExactSizeIterator for ForwardEdgeIterator<'a, T> {}
pub struct BackwardEdgeIterator<'a, T: HalfEdgeImplMeshType + 'a> {
is_first: bool,
first: T::E,
current: HalfEdgeImpl<T>,
mesh: &'a T::Mesh,
}
impl<'a, T: HalfEdgeImplMeshType> BackwardEdgeIterator<'a, T> {
pub fn new(first: HalfEdgeImpl<T>, mesh: &'a T::Mesh) -> Self {
Self {
first: first.id(),
current: first,
mesh,
is_first: true,
}
}
}
impl<'a, T: HalfEdgeImplMeshType> Iterator for BackwardEdgeIterator<'a, T> {
type Item = HalfEdgeImpl<T>;
#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
if self.is_first {
self.is_first = false;
return Some(self.current.clone());
}
let prev = self.current.prev(self.mesh);
if prev.id() == self.first {
return None;
} else {
self.current = prev.clone();
return Some(prev);
}
}
}
pub struct ForwardEdgeIteratorMut<'a, T: HalfEdgeImplMeshType + 'a> {
is_first: bool,
first: T::E,
current: T::E,
mesh: &'a mut T::Mesh,
}
impl<'a, T: HalfEdgeImplMeshType> ForwardEdgeIteratorMut<'a, T> {
pub fn new(first: T::E, mesh: &'a mut T::Mesh) -> Self {
Self {
first,
current: first,
mesh,
is_first: true,
}
}
}
impl<'a, T: HalfEdgeImplMeshType> Iterator for ForwardEdgeIteratorMut<'a, T> {
type Item = &'a mut HalfEdgeImpl<T>;
fn next(&mut self) -> Option<Self::Item> {
unsafe {
if self.is_first {
self.is_first = false;
let edge_ptr = self.mesh.edge_mut(self.current) as *mut HalfEdgeImpl<T>;
return Some(&mut *edge_ptr);
}
let next = self.mesh.edge(self.current).next(self.mesh);
if next.id() == self.first {
return None;
} else {
self.current = next.id();
let edge_ptr = self.mesh.edge_mut(next.id()) as *mut HalfEdgeImpl<T>;
return Some(&mut *edge_ptr);
}
}
}
}