Skip to main content

rs_math3d/
lib.rs

1// Copyright 2020-Present (c) Raja Lehtihet & Wael El Oraiby
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
5//
6// 1. Redistributions of source code must retain the above copyright notice,
7// this list of conditions and the following disclaimer.
8//
9// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its contributors
14// may be used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28
29//! # rs-math3d
30//!
31//! A `no_std` 3D mathematics library for Rust, providing vectors, matrices,
32//! quaternions, and geometric primitives for computer graphics applications.
33//!
34//! ## Features
35//!
36//! - **No standard library required**: Works without `std`, with float math supplied by the
37//!   `std`, `libm`, or `system-libm` backend
38//! - **Generic storage types**: Vectors, boxes, rectangles, and basic matrix arithmetic support
39//!   `f32`, `f64`, `i32`, and `i64`
40//! - **Float-only analytic math**: Normalization, inverses, quaternions, transforms, rays,
41//!   planes, and geometric queries are restricted to floating-point scalars
42//! - **Column-major matrices**: Compatible with OpenGL and similar APIs
43//! - **Comprehensive float math**: Full suite of vector, matrix, and quaternion operations for
44//!   `f32` and `f64`
45//! - **Geometric primitives**: Rays, planes, triangles, spheres, and more
46//!
47//! ## Quick Start
48//!
49//! ```
50//! use rs_math3d::{Vector, Vector3, Matrix4, Quat, EPS_F32};
51//! use rs_math3d::transforms;
52//! use core::f32::consts::{FRAC_PI_2, PI};
53//!
54//! // Create vectors
55//! let v1 = Vector3::new(1.0, 2.0, 3.0);
56//! let v2 = Vector3::new(4.0, 5.0, 6.0);
57//! let dot_product = Vector3::dot(&v1, &v2);
58//!
59//! // Create transformation matrices
60//! let translation = transforms::translate(Vector3::new(10.0, 0.0, 0.0));
61//! let rotation = transforms::rotation_from_axis_angle(
62//!     &Vector3::new(0.0, 1.0, 0.0),
63//!     PI / 4.0,
64//!     EPS_F32,
65//! ).unwrap();
66//!
67//! // Use quaternions for rotations
68//! let q = Quat::of_axis_angle(
69//!     &Vector3::new(0.0, 0.0, 1.0),
70//!     FRAC_PI_2,
71//!     EPS_F32,
72//! ).unwrap();
73//! let rotation_matrix = q.mat4();
74//! ```
75//!
76//! ## Math Backends
77//!
78//! Floating-point transcendental functions are selected through one backend:
79//!
80//! - `std`: use Rust's standard-library float methods
81//! - `libm`: use the pure-software `libm` crate
82//! - `system-libm`: call the target's C math library
83//!
84//! Library builds without any of these features fall back to `system-libm`.
85//! Test builds without an explicit backend use the `std` backend.
86//! If more than one backend feature is enabled, precedence is `std`, then `libm`,
87//! then `system-libm`.
88//!
89//! ## Modules
90//!
91//! - [`vector`]: 2D, 3D, and 4D vectors with arithmetic operations
92//! - [`matrix`]: 2x2, 3x3, and 4x4 matrices with linear algebra operations
93//! - [`quaternion`]: Quaternions for 3D rotations
94//! - [`transforms`]: Common transformation matrices (translate, rotate, scale, project)
95//! - [`primitives`]: Geometric primitives (rays, planes, triangles, boxes, spheres)
96//! - [`queries`]: Intersection and distance queries between primitives
97//! - [`basis`]: Coordinate system basis representation
98//! - [`scalar`]: Traits for generic numeric operations
99
100#![no_std]
101#[cfg(any(test, feature = "std"))]
102extern crate std;
103pub mod basis;
104mod math;
105pub mod matrix;
106pub mod primitives;
107/// Quaternion operations and conversions.
108pub mod quaternion;
109/// Intersection and distance query traits and helpers.
110pub mod queries;
111pub mod scalar;
112pub mod transforms;
113pub mod vector;
114
115pub use basis::*;
116pub use matrix::{Matrix2, Matrix3, Matrix4};
117pub use primitives::*;
118pub use quaternion::Quat;
119pub use queries::*;
120pub use scalar::{FloatScalar, Scalar, EPS_F32, EPS_F64};
121pub use transforms::*;
122pub use vector::{
123    CrossProduct, FloatVector, Swizzle2, Swizzle3, Vector, Vector2, Vector3, Vector4,
124};
125
126/// 4-component byte color (RGBA).
127pub type Color4b = Vector4<u8>;
128
129/// 2D integer vector.
130pub type Vec2i = Vector2<i32>;
131/// 3D integer vector.
132pub type Vec3i = Vector3<i32>;
133/// 4D integer vector.
134pub type Vec4i = Vector4<i32>;
135
136/// 2D single-precision float vector.
137pub type Vec2f = Vector2<f32>;
138/// 3D single-precision float vector.
139pub type Vec3f = Vector3<f32>;
140/// 4D single-precision float vector.
141pub type Vec4f = Vector4<f32>;
142
143/// 2D double-precision float vector.
144pub type Vec2d = Vector2<f64>;
145/// 3D double-precision float vector.
146pub type Vec3d = Vector3<f64>;
147/// 4D double-precision float vector.
148pub type Vec4d = Vector4<f64>;
149
150/// Single-precision quaternion.
151pub type Quatf = Quat<f32>;
152/// Double-precision quaternion.
153pub type Quatd = Quat<f64>;
154
155/// 2x2 single-precision matrix.
156pub type Mat2f = Matrix2<f32>;
157/// 3x3 single-precision matrix.
158pub type Mat3f = Matrix3<f32>;
159/// 4x4 single-precision matrix.
160pub type Mat4f = Matrix4<f32>;
161
162/// 2x2 double-precision matrix.
163pub type Mat2d = Matrix2<f64>;
164/// 3x3 double-precision matrix.
165pub type Mat3d = Matrix3<f64>;
166/// 4x4 double-precision matrix.
167pub type Mat4d = Matrix4<f64>;
168
169/// Integer rectangle.
170pub type Recti = Rect<i32>;
171/// Single-precision rectangle.
172pub type Rectf = Rect<f32>;
173/// Double-precision rectangle.
174pub type Rectd = Rect<f64>;
175
176/// Integer dimensions.
177pub type Dimensioni = Dimension<i32>;
178/// Single-precision dimensions.
179pub type Dimensionf = Dimension<f32>;
180
181/// 2D single-precision line.
182pub type Line2f = Line<f32, Vec2f>;
183/// 2D double-precision line.
184pub type Line2d = Line<f64, Vec2d>;
185/// 3D single-precision line.
186pub type Line3f = Line<f32, Vec3f>;
187/// 3D double-precision line.
188pub type Line3d = Line<f64, Vec3d>;
189
190/// 2D single-precision line segment.
191pub type Segment2f = Segment<f32, Vec2f>;
192/// 2D double-precision line segment.
193pub type Segment2d = Segment<f64, Vec2d>;
194/// 3D single-precision line segment.
195pub type Segment3f = Segment<f32, Vec3f>;
196/// 3D double-precision line segment.
197pub type Segment3d = Segment<f64, Vec3d>;
198
199/// Single-precision plane.
200pub type Planef = Plane<f32>;
201/// Double-precision plane.
202pub type Planed = Plane<f64>;
203
204/// 3D single-precision ray.
205pub type Ray3f = Ray<f32, Vec3f>;
206/// 3D double-precision ray.
207pub type Ray3d = Ray<f64, Vec3d>;
208
209/// 3D single-precision axis-aligned bounding box.
210pub type Box3f = Box3<f32>;
211/// 3D double-precision axis-aligned bounding box.
212pub type Box3d = Box3<f64>;
213
214/// Single-precision basis.
215pub type Basisf = Basis<f32>;
216/// Double-precision basis.
217pub type Basisd = Basis<f64>;
218
219/// Single-precision parametric plane.
220pub type ParametricPlanef = ParametricPlane<f32>;
221/// Double-precision parametric plane.
222pub type ParametricPlaned = ParametricPlane<f64>;
223
224/// Creates an RGBA color from byte components.
225pub fn color4b(r: u8, g: u8, b: u8, a: u8) -> Color4b {
226    Color4b {
227        x: r,
228        y: g,
229        z: b,
230        w: a,
231    }
232}