phys_geom/
lib.rs

1// Copyright (C) 2020-2025 phys-geom authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # Physics Geometry Library
16//!
17//! A comprehensive geometry processing library for 3D physics simulations.
18//!
19//! This library provides a set of geometric shapes and basic operations commonly used in
20//! physics engines, including volume computation and bounding box calculations.
21//!
22//! ## Features
23//!
24//! - **Primitive Shapes**: Sphere, Capsule, Cuboid, Cylinder, Cone, Triangle, Tetrahedron,
25//!   InfinitePlane
26//! - **Bounding Volumes**: Axis-Aligned Bounding Boxes (AABB) and Oriented Bounding Boxes (OBB)
27//! - **Volume Computation**: Calculate volumes of various geometric shapes
28//! - **Precision Control**: Switch between f32 and f64 using feature flags
29//! - **Serialization**: Optional serde support for data persistence
30//!
31//! ## Usage
32//!
33//! ```rust
34//! use phys_geom::{shape::Sphere, shape::Cuboid, volume::ComputeVolume, aabb3::ComputeAabb3};
35//!
36//! // Create a sphere with radius 1.0
37//! let sphere = Sphere::new(1.0);
38//!
39//! // Compute its volume
40//! let volume = sphere.compute_volume();
41//!
42//! // Create a cuboid (box) with given dimensions
43//! let cuboid = Cuboid::new([2.0, 3.0, 1.0]);
44//!
45//! // Compute its axis-aligned bounding box
46//! let aabb = cuboid.compute_aabb();
47//! ```
48//!
49//! ```rust
50//! use phys_geom::math::*;
51//!
52//! // These types change based on the `f64` feature flag
53//! let vector: Vec3 = Vec3::new(1.0, 2.0, 3.0);
54//! let point: Point3 = Point3::new(1.0, 2.0, 3.0);
55//! let rotation: UnitQuat = UnitQuat::identity();
56//! ```
57
58#![deny(warnings)]
59#![deny(clippy::style)]
60pub mod aabb2;
61pub mod aabb3;
62mod interval;
63pub mod obb3;
64mod ray;
65pub mod shape;
66pub mod volume;
67
68pub use aabb2::Aabb2;
69pub use aabb3::{Aabb3, ComputeAabb3};
70pub use interval::Interval;
71pub use ray::Ray;
72
73extern crate nalgebra as na;
74
75#[cfg(feature = "f64")]
76pub type Real = f64;
77#[cfg(not(feature = "f64"))]
78pub type Real = f32;
79
80pub mod math {
81    pub use super::Real;
82    pub type Vec2 = na::Vector2<Real>;
83    pub type Vec3 = na::Vector3<Real>;
84    pub type Point2 = na::Point2<Real>;
85    pub type Point3 = na::Point3<Real>;
86    pub type UnitQuat = na::UnitQuaternion<Real>;
87    pub type UnitVec3 = na::UnitVector3<Real>;
88    pub type Isometry3 = na::Isometry3<Real>;
89
90    #[cfg(feature = "f64")]
91    pub const PI: Real = std::f64::consts::PI;
92    #[cfg(not(feature = "f64"))]
93    pub const PI: Real = std::f32::consts::PI;
94
95    #[inline]
96    pub const fn point3(x: Real, y: Real, z: Real) -> Point3 {
97        Point3::new(x, y, z)
98    }
99
100    #[inline]
101    pub const fn vec3(x: Real, y: Real, z: Real) -> Vec3 {
102        Vec3::new(x, y, z)
103    }
104
105    #[inline]
106    pub fn unit_vec3(x: Real, y: Real, z: Real) -> UnitVec3 {
107        UnitVec3::new_normalize(vec3(x, y, z))
108    }
109
110    pub trait Vec3Ext {
111        fn normalize_to_unit(self) -> UnitVec3;
112        fn to_array(&self) -> [Real; 3];
113    }
114
115    impl Vec3Ext for Vec3 {
116        #[inline]
117        fn normalize_to_unit(self) -> UnitVec3 {
118            UnitVec3::new_normalize(self)
119        }
120
121        #[inline]
122        fn to_array(&self) -> [Real; 3] {
123            [self.x, self.y, self.z]
124        }
125    }
126
127    pub trait Point3Ext {
128        fn to_array(&self) -> [Real; 3];
129
130        #[inline]
131        fn as_vec3(&self) -> Vec3 {
132            Vec3::from(self.to_array())
133        }
134    }
135
136    impl Point3Ext for Point3 {
137        #[inline]
138        fn to_array(&self) -> [Real; 3] {
139            [self.x, self.y, self.z]
140        }
141    }
142}