polyhedron 0.1.0

A half edge and radial edge implementation.
Documentation
use std::ops::Neg;

use crate::data_container::StorageBuffer;
use crate::prelude::{half_edge_handle::HalfEdgeHandle, vert_handle::VertHandle};
use crate::{FaceData, FaceId, MeshData, MeshStorage, VertData};

/// Topological handle to a face.
pub struct FaceHandle<C: MeshStorage, const MANIFOLD: bool>
{
    pub(crate) mesh_data: *mut MeshData<C, MANIFOLD>,
    pub(crate) id: FaceId,
}

impl<C: MeshStorage, const MANIFOLD: bool> FaceHandle<C, MANIFOLD>
{
    /// Id of the handle.
    pub fn id(&self) -> FaceId { self.id }

    /// Handle to an arbitrary half edge in the face.
    pub fn half_edge(&self) -> HalfEdgeHandle<C, MANIFOLD>
    {
        unsafe {
            let half_edge_id = (*self.mesh_data).face_ref(self.id).half_edge;

            HalfEdgeHandle {
                mesh_data: self.mesh_data,
                id: half_edge_id,
            }
        }
    }

    /// Read the stored data for this face.
    pub fn read_data(&self) -> &FaceData<C>
    {
        unsafe {
            (*self.mesh_data)
                .geometry_data
                .face_data
                .read(self.id.to_index() as u64)
        }
    }

    /// Update the stored data for this face.
    pub fn update_data<F: FnMut(&mut FaceData<C>)>(&mut self, update: F)
    {
        unsafe {
            (*self.mesh_data)
                .geometry_data
                .face_data
                .update(self.id.to_index() as u64, update)
        }
    }

    /// Fast but naive method for the un-normalized normal of this face.
    pub fn naive_normal<S>(&self) -> VertData<C>
    where
        VertData<C>: linear_isomorphic::InnerSpace<S> + Neg<Output = VertData<C>>,
        S: linear_isomorphic::RealField,
    {
        use linear_isomorphic::VectorSpace;
        let e1: VertData<C> = self.half_edge().dir();
        let e2: VertData<C> = self.half_edge().face_prev().dir();

        return e2.cross(&e1);
    }

    /// Iterator over vertex handles to the vertices in the face.
    pub fn vertices(&self) -> impl Iterator<Item = VertHandle<C, MANIFOLD>>
    {
        self.half_edge().iter_face().map(|h| h.source())
    }

    /// Make a shallow copy of the handle.
    pub fn replicate(&self) -> Self
    {
        FaceHandle {
            mesh_data: self.mesh_data,
            id: self.id,
        }
    }
}