qchull 2.0.1-beta.0

Provides quick convex hull algorithm
// Copyright (C) 2020-2025 qchull authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use smallvec::SmallVec;
use vslab::{new_type_id, Slab};

use crate::face::FaceKey;

new_type_id!(HalfEdgeKey);
pub(crate) type EdgeMap = Slab<HalfEdgeKey, HalfEdge>;
pub(crate) type EdgeIndexSmallVec = SmallVec<[HalfEdgeKey; 32]>;

#[derive(Debug)]
pub struct HalfEdge {
    pub origin: usize,
    pub destination: usize,
    twin: Option<HalfEdgeKey>,
    owner_face: Option<FaceKey>,
    prev: Option<HalfEdgeKey>,
    next: Option<HalfEdgeKey>,
    pub(crate) visited: bool,
}

impl HalfEdge {
    #[inline]
    pub(crate) fn new(origin: usize, destination: usize) -> Self {
        Self {
            origin,
            destination,
            twin: None,
            owner_face: None,
            prev: None,
            next: None,
            visited: false,
        }
    }

    /// #Panics
    ///
    /// Panic if the twin is not set
    #[inline]
    pub(crate) fn twin(&self) -> HalfEdgeKey {
        self.twin.expect("twin must be set")
    }

    #[inline]
    pub(crate) fn set_twin(&mut self, twin: HalfEdgeKey) {
        self.twin = Some(twin);
    }

    /// #Panics
    ///
    /// Panic if the `owner_face` is not set
    #[inline]
    pub(crate) fn owner_face(&self) -> FaceKey {
        self.owner_face.expect("owner_face must be set")
    }

    #[inline]
    pub(crate) fn set_owner_face(&mut self, owner_face: FaceKey) {
        self.owner_face = Some(owner_face);
    }

    /// #Panics
    ///
    /// Panic if the prev edge is not set
    #[inline]
    pub(crate) fn prev(&self) -> HalfEdgeKey {
        self.prev.expect("prev must be set")
    }

    #[inline]
    pub(crate) fn set_prev(&mut self, prev: HalfEdgeKey) {
        self.prev = Some(prev);
    }

    /// #Panics
    ///
    /// Panic if the next edge is not set
    #[inline]
    pub(crate) fn next(&self) -> HalfEdgeKey {
        self.next.expect("next must be set")
    }

    #[inline]
    pub(crate) fn set_next(&mut self, next: HalfEdgeKey) {
        self.next = Some(next);
    }
}