phys-collision 2.0.1-beta.0

Provides collision detection ability
// Copyright (C) 2020-2025 phys-collision 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 std::sync::{LockResult, Mutex, MutexGuard};

use glam::{Isometry3, Vec3};
use once_cell::sync::Lazy;

#[derive(Clone, Debug, Default)]
pub struct ContactDebugContext {
    pub count: usize,
    pub insight_pairs: [InsightPair; 4],
}

#[derive(Clone, Debug, Default)]
pub struct InsightPair {
    pub value: [ManifoldInsight; 2],
}

#[derive(Clone, Debug, PartialEq)]
pub enum ManifoldInsight {
    Sphere {
        radius: f32,
        center: Vec3,
    },
    Box {
        half_extents: Vec3,
        center: Vec3,
        rotation: glam::Quat,
    },
    Capsule {
        radius: f32,
        half_height: f32,
        center: Vec3,
        rotation: glam::Quat,
    },
    Plane {
        normal: Vec3,
        distance: f32,
    },
    MeshInsight {
        highlight_triangles: TriangleListWithHighlight,
    },
}
impl Default for ManifoldInsight {
    fn default() -> Self {
        ManifoldInsight::Sphere {
            radius: Default::default(),
            center: Vec3::default(),
        }
    }
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct TriangleListWithHighlight {
    pub transform: Isometry3,
    pub points_id: u32,
    pub face_vertex_indices: Vec<u32>,
    pub face_indices_start: Vec<u32>,
}

static CONTACT_DEBUG_DATA: Lazy<Mutex<Vec<ContactDebugContext>>> =
    Lazy::new(|| Mutex::new(Vec::new()));

/// Returns a lock guard for the global contact debug data.
///
/// # Errors
///
/// Returns an error if the mutex is poisoned or if the lock cannot be acquired.
pub fn get_or_init() -> LockResult<MutexGuard<'static, Vec<ContactDebugContext>>> {
    CONTACT_DEBUG_DATA.lock()
}