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 in embedded and WASM environments
37//! - **Generic types**: All types are generic over scalar types (f32, f64, i32, i64)
38//! - **Column-major matrices**: Compatible with OpenGL and similar APIs
39//! - **Comprehensive operations**: Full suite of vector, matrix, and quaternion operations
40//! - **Geometric primitives**: Rays, planes, triangles, spheres, and more
41//!
42//! ## Quick Start
43//!
44//! ```
45//! use rs_math3d::{Vector, Vector3, Matrix4, Quat, EPS_F32};
46//! use rs_math3d::transforms;
47//!
48//! // Create vectors
49//! let v1 = Vector3::new(1.0, 2.0, 3.0);
50//! let v2 = Vector3::new(4.0, 5.0, 6.0);
51//! let dot_product = Vector3::dot(&v1, &v2);
52//!
53//! // Create transformation matrices
54//! let translation = transforms::translate(Vector3::new(10.0, 0.0, 0.0));
55//! let rotation = transforms::rotation_from_axis_angle(
56//! &Vector3::new(0.0, 1.0, 0.0),
57//! std::f32::consts::PI / 4.0,
58//! EPS_F32,
59//! ).unwrap();
60//!
61//! // Use quaternions for rotations
62//! let q = Quat::of_axis_angle(
63//! &Vector3::new(0.0, 0.0, 1.0),
64//! std::f32::consts::PI / 2.0,
65//! EPS_F32,
66//! ).unwrap();
67//! let rotation_matrix = q.mat4();
68//! ```
69//!
70//! ## Modules
71//!
72//! - [`vector`]: 2D, 3D, and 4D vectors with arithmetic operations
73//! - [`matrix`]: 2x2, 3x3, and 4x4 matrices with linear algebra operations
74//! - [`quaternion`]: Quaternions for 3D rotations
75//! - [`transforms`]: Common transformation matrices (translate, rotate, scale, project)
76//! - [`primitives`]: Geometric primitives (rays, planes, triangles, boxes, spheres)
77//! - [`queries`]: Intersection and distance queries between primitives
78//! - [`basis`]: Coordinate system basis representation
79//! - [`scalar`]: Traits for generic numeric operations
80
81#![no_std]
82#![allow(unused_imports)]
83#[cfg(any(test, feature = "std"))]
84extern crate std;
85pub mod basis;
86pub mod matrix;
87pub mod primitives;
88/// Quaternion operations and conversions.
89pub mod quaternion;
90/// Intersection and distance query traits and helpers.
91pub mod queries;
92pub mod scalar;
93pub mod transforms;
94pub mod vector;
95
96pub use basis::*;
97pub use matrix::{Matrix2, Matrix3, Matrix4};
98pub use primitives::*;
99pub use quaternion::Quat;
100pub use queries::*;
101pub use scalar::{FloatScalar, Scalar, EPS_F32, EPS_F64};
102pub use transforms::*;
103pub use vector::{
104 CrossProduct, FloatVector, Swizzle2, Swizzle3, Vector, Vector2, Vector3, Vector4,
105};
106
107/// 4-component byte color (RGBA).
108pub type Color4b = Vector4<u8>;
109
110/// 2D integer vector.
111pub type Vec2i = Vector2<i32>;
112/// 3D integer vector.
113pub type Vec3i = Vector3<i32>;
114/// 4D integer vector.
115pub type Vec4i = Vector4<i32>;
116
117/// 2D single-precision float vector.
118pub type Vec2f = Vector2<f32>;
119/// 3D single-precision float vector.
120pub type Vec3f = Vector3<f32>;
121/// 4D single-precision float vector.
122pub type Vec4f = Vector4<f32>;
123
124/// 2D double-precision float vector.
125pub type Vec2d = Vector2<f64>;
126/// 3D double-precision float vector.
127pub type Vec3d = Vector3<f64>;
128/// 4D double-precision float vector.
129pub type Vec4d = Vector4<f64>;
130
131/// Single-precision quaternion.
132pub type Quatf = Quat<f32>;
133/// Double-precision quaternion.
134pub type Quatd = Quat<f64>;
135
136/// 2x2 single-precision matrix.
137pub type Mat2f = Matrix2<f32>;
138/// 3x3 single-precision matrix.
139pub type Mat3f = Matrix3<f32>;
140/// 4x4 single-precision matrix.
141pub type Mat4f = Matrix4<f32>;
142
143/// 2x2 double-precision matrix.
144pub type Mat2d = Matrix2<f64>;
145/// 3x3 double-precision matrix.
146pub type Mat3d = Matrix3<f64>;
147/// 4x4 double-precision matrix.
148pub type Mat4d = Matrix4<f64>;
149
150/// Integer rectangle.
151pub type Recti = Rect<i32>;
152/// Single-precision rectangle.
153pub type Rectf = Rect<f32>;
154/// Double-precision rectangle.
155pub type Rectd = Rect<f64>;
156
157/// Integer dimensions.
158pub type Dimensioni = Dimension<i32>;
159/// Single-precision dimensions.
160pub type Dimensionf = Dimension<f32>;
161
162/// 2D single-precision line.
163pub type Line2f = Line<f32, Vec2f>;
164/// 2D double-precision line.
165pub type Line2d = Line<f64, Vec2d>;
166/// 3D single-precision line.
167pub type Line3f = Line<f32, Vec3f>;
168/// 3D double-precision line.
169pub type Line3d = Line<f64, Vec3d>;
170
171/// 2D single-precision line segment.
172pub type Segment2f = Segment<f32, Vec2f>;
173/// 2D double-precision line segment.
174pub type Segment2d = Segment<f64, Vec2d>;
175/// 3D single-precision line segment.
176pub type Segment3f = Segment<f32, Vec3f>;
177/// 3D double-precision line segment.
178pub type Segment3d = Segment<f64, Vec3d>;
179
180/// Single-precision plane.
181pub type Planef = Plane<f32>;
182/// Double-precision plane.
183pub type Planed = Plane<f64>;
184
185/// 3D single-precision ray.
186pub type Ray3f = Ray<f32, Vec3f>;
187/// 3D double-precision ray.
188pub type Ray3d = Ray<f64, Vec3d>;
189
190/// 3D single-precision axis-aligned bounding box.
191pub type Box3f = Box3<f32>;
192/// 3D double-precision axis-aligned bounding box.
193pub type Box3d = Box3<f64>;
194
195/// Single-precision basis.
196pub type Basisf = Basis<f32>;
197/// Double-precision basis.
198pub type Basisd = Basis<f64>;
199
200/// Single-precision parametric plane.
201pub type ParametricPlanef = ParametricPlane<f32>;
202/// Double-precision parametric plane.
203pub type ParametricPlaned = ParametricPlane<f64>;
204
205/// Creates an RGBA color from byte components.
206pub fn color4b(r: u8, g: u8, b: u8, a: u8) -> Color4b {
207 Color4b {
208 x: r,
209 y: g,
210 z: b,
211 w: a,
212 }
213}