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::{Vector3, Matrix4, Quat};
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//! );
59//!
60//! // Use quaternions for rotations
61//! let q = Quat::of_axis_angle(
62//! &Vector3::new(0.0, 0.0, 1.0),
63//! std::f32::consts::PI / 2.0
64//! );
65//! let rotation_matrix = q.mat4();
66//! ```
67//!
68//! ## Modules
69//!
70//! - [`vector`]: 2D, 3D, and 4D vectors with arithmetic operations
71//! - [`matrix`]: 2x2, 3x3, and 4x4 matrices with linear algebra operations
72//! - [`quaternion`]: Quaternions for 3D rotations
73//! - [`transforms`]: Common transformation matrices (translate, rotate, scale, project)
74//! - [`primitives`]: Geometric primitives (rays, planes, triangles, boxes, spheres)
75//! - [`queries`]: Intersection and distance queries between primitives
76//! - [`basis`]: Coordinate system basis representation
77//! - [`scalar`]: Traits for generic numeric operations
78
79#![no_std]
80#![allow(unused_imports)]
81pub mod basis;
82pub mod matrix;
83pub mod primitives;
84pub mod quaternion;
85pub mod queries;
86pub mod scalar;
87pub mod transforms;
88pub mod vector;
89
90pub use basis::*;
91pub use matrix::{Matrix2, Matrix3, Matrix4};
92pub use primitives::*;
93pub use quaternion::Quat;
94pub use queries::*;
95pub use scalar::{FloatScalar, Scalar};
96pub use transforms::*;
97pub use vector::{
98 CrossProduct, FloatVector, Swizzle2, Swizzle3, Vector, Vector2, Vector3, Vector4,
99};
100
101/// 4-component byte color (RGBA).
102pub type Color4b = Vector4<u8>;
103
104/// 2D integer vector.
105pub type Vec2i = Vector2<i32>;
106/// 3D integer vector.
107pub type Vec3i = Vector3<i32>;
108/// 4D integer vector.
109pub type Vec4i = Vector4<i32>;
110
111/// 2D single-precision float vector.
112pub type Vec2f = Vector2<f32>;
113/// 3D single-precision float vector.
114pub type Vec3f = Vector3<f32>;
115/// 4D single-precision float vector.
116pub type Vec4f = Vector4<f32>;
117
118/// 2D double-precision float vector.
119pub type Vec2d = Vector2<f64>;
120/// 3D double-precision float vector.
121pub type Vec3d = Vector3<f64>;
122/// 4D double-precision float vector.
123pub type Vec4d = Vector4<f64>;
124
125/// Single-precision quaternion.
126pub type Quatf = Quat<f32>;
127/// Double-precision quaternion.
128pub type Quatd = Quat<f64>;
129
130/// 2x2 single-precision matrix.
131pub type Mat2f = Matrix2<f32>;
132/// 3x3 single-precision matrix.
133pub type Mat3f = Matrix3<f32>;
134/// 4x4 single-precision matrix.
135pub type Mat4f = Matrix4<f32>;
136
137/// 2x2 double-precision matrix.
138pub type Mat2d = Matrix2<f64>;
139/// 3x3 double-precision matrix.
140pub type Mat3d = Matrix3<f64>;
141/// 4x4 double-precision matrix.
142pub type Mat4d = Matrix4<f64>;
143
144/// Integer rectangle.
145pub type Recti = Rect<i32>;
146/// Single-precision rectangle.
147pub type Rectf = Rect<f32>;
148/// Double-precision rectangle.
149pub type Rectd = Rect<f64>;
150
151/// Integer dimensions.
152pub type Dimensioni = Dimension<i32>;
153/// Single-precision dimensions.
154pub type Dimensionf = Dimension<f32>;
155
156/// 2D single-precision line.
157pub type Line2f = Line<f32, Vec2f>;
158/// 2D double-precision line.
159pub type Line2d = Line<f64, Vec2d>;
160/// 3D single-precision line.
161pub type Line3f = Line<f32, Vec3f>;
162/// 3D double-precision line.
163pub type Line3d = Line<f64, Vec3d>;
164
165/// 2D single-precision line segment.
166pub type Segment2f = Segment<f32, Vec2f>;
167/// 2D double-precision line segment.
168pub type Segment2d = Segment<f64, Vec2d>;
169/// 3D single-precision line segment.
170pub type Segment3f = Segment<f32, Vec3f>;
171/// 3D double-precision line segment.
172pub type Segment3d = Segment<f64, Vec3d>;
173
174/// Single-precision plane.
175pub type Planef = Plane<f32>;
176/// Double-precision plane.
177pub type Planed = Plane<f64>;
178
179/// 3D single-precision ray.
180pub type Ray3f = Ray<f32, Vec3f>;
181/// 3D double-precision ray.
182pub type Ray3d = Ray<f64, Vec3d>;
183
184/// 3D single-precision axis-aligned bounding box.
185pub type Box3f = Box3<f32>;
186/// 3D double-precision axis-aligned bounding box.
187pub type Box3d = Box3<f64>;
188
189pub type Basisf = Basis<f32>;
190pub type Basisd = Basis<f64>;
191
192pub type ParametricPlanef = ParametricPlane<f32>;
193pub type ParametricPlaned = ParametricPlane<f64>;
194
195pub fn color4b(r: u8, g: u8, b: u8, a: u8) -> Color4b {
196 Color4b {
197 x: r,
198 y: g,
199 z: b,
200 w: a,
201 }
202}
203