rs_math3d/
transforms.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//! 3D transformation functions for computer graphics.
29//!
30//! This module provides functions to create and manipulate transformation
31//! matrices commonly used in 3D graphics, including translation, rotation,
32//! scaling, and projection matrices.
33//!
34//! # Examples
35//!
36//! ```
37//! use rs_math3d::transforms;
38//! use rs_math3d::vector::Vector3;
39//! 
40//! // Create a translation matrix
41//! let translation = transforms::translate(Vector3::new(10.0, 5.0, 0.0));
42//! 
43//! // Create a perspective projection matrix
44//! let projection = transforms::perspective(
45//!     45.0f32.to_radians(),  // Field of view
46//!     16.0 / 9.0,            // Aspect ratio
47//!     0.1,                   // Near plane
48//!     100.0                  // Far plane
49//! );
50//! ```
51
52use crate::matrix::*;
53use crate::quaternion::*;
54use crate::scalar::*;
55use crate::vector::*;
56use num_traits::{Zero, One};
57
58/// Creates a 4x4 translation matrix.
59///
60/// The translation matrix has the form:
61/// ```text
62/// [1  0  0  tx]
63/// [0  1  0  ty]
64/// [0  0  1  tz]
65/// [0  0  0  1 ]
66/// ```
67///
68/// # Parameters
69/// - `trans`: Translation vector (tx, ty, tz)
70pub fn translate<T: Scalar>(trans: Vector3<T>) -> Matrix4<T> {
71    Matrix4::new(
72        <T as One>::one(),
73        <T as Zero>::zero(),
74        <T as Zero>::zero(),
75        <T as Zero>::zero(),
76        <T as Zero>::zero(),
77        <T as One>::one(),
78        <T as Zero>::zero(),
79        <T as Zero>::zero(),
80        <T as Zero>::zero(),
81        <T as Zero>::zero(),
82        <T as One>::one(),
83        <T as Zero>::zero(),
84        trans.x,
85        trans.y,
86        trans.z,
87        <T as One>::one(),
88    )
89}
90
91/// Creates a 4x4 scaling matrix.
92///
93/// The scaling matrix has the form:
94/// ```text
95/// [sx 0  0  0]
96/// [0  sy 0  0]
97/// [0  0  sz 0]
98/// [0  0  0  1]
99/// ```
100///
101/// # Parameters
102/// - `scale`: Scale factors for each axis
103pub fn scale<T: Scalar>(scale: Vector3<T>) -> Matrix4<T> {
104    Matrix4::new(
105        scale.x,
106        <T as Zero>::zero(),
107        <T as Zero>::zero(),
108        <T as Zero>::zero(),
109        <T as Zero>::zero(),
110        scale.y,
111        <T as Zero>::zero(),
112        <T as Zero>::zero(),
113        <T as Zero>::zero(),
114        <T as Zero>::zero(),
115        scale.z,
116        <T as Zero>::zero(),
117        <T as Zero>::zero(),
118        <T as Zero>::zero(),
119        <T as Zero>::zero(),
120        <T as One>::one(),
121    )
122}
123
124/// Creates a 4x4 rotation matrix from a quaternion.
125///
126/// Converts a unit quaternion to its equivalent rotation matrix.
127pub fn rotation_from_quat<T: FloatScalar>(q: &Quat<T>) -> Matrix4<T> {
128    Quat::mat4(q)
129}
130
131/// Creates a 4x4 rotation matrix from an axis and angle.
132///
133/// # Parameters
134/// - `axis`: The rotation axis (will be normalized)
135/// - `angle`: The rotation angle in radians
136pub fn rotation_from_axis_angle<T: FloatScalar>(axis: &Vector3<T>, angle: T) -> Matrix4<T> {
137    Quat::mat4(&Quat::of_axis_angle(axis, angle))
138}
139
140/// Transforms a 3D vector by a 4x4 matrix with perspective division.
141///
142/// The vector is treated as a point (w=1) and the result is divided by w.
143pub fn transform_vec3<T: Scalar>(m: &Matrix4<T>, v: &Vector3<T>) -> Vector3<T> {
144    let v4 = Vector4::new(v.x, v.y, v.z, <T as One>::one());
145    let vout = *m * v4;
146    Vector3::new(vout.x / vout.w, vout.y / vout.w, vout.z / vout.w)
147}
148
149/// Projects a 3D point to screen coordinates.
150///
151/// # Parameters
152/// - `world`: World transformation matrix
153/// - `persp`: Perspective projection matrix
154/// - `lb`: Screen left-bottom corner
155/// - `rt`: Screen right-top corner
156/// - `pt`: Point to project
157///
158/// # Returns
159/// Screen coordinates with z in \[0,1\] (0=near, 1=far)
160pub fn project3<T: Scalar>(
161    world: &Matrix4<T>,
162    persp: &Matrix4<T>,
163    lb: &Vector2<T>,
164    rt: &Vector2<T>,
165    pt: &Vector3<T>,
166) -> Vector3<T> {
167    let inp = Vector4::new(pt.x, pt.y, pt.z, <T as One>::one());
168    let pw = *persp * *world;
169    let mut out = pw * inp;
170
171    out.x /= out.w;
172    out.y /= out.w;
173    out.z /= out.w;
174
175    let out_x = lb.x + ((rt.x - lb.x) * (out.x + <T as One>::one()) * T::half());
176    let out_y = lb.y + ((rt.y - lb.y) * (out.y + <T as One>::one()) * T::half());
177    let out_z = (out.z + <T as One>::one()) * T::half();
178    Vector3::new(out_x, out_y, out_z)
179}
180
181/// Unprojects screen coordinates back to 3D world space.
182///
183/// # Parameters
184/// - `world`: World transformation matrix
185/// - `persp`: Perspective projection matrix
186/// - `lb`: Screen left-bottom corner
187/// - `rt`: Screen right-top corner
188/// - `pt`: Screen point with z-depth
189///
190/// # Returns
191/// The corresponding 3D world point
192pub fn unproject3<T: Scalar>(
193    world: &Matrix4<T>,
194    persp: &Matrix4<T>,
195    lb: &Vector2<T>,
196    rt: &Vector2<T>,
197    pt: &Vector3<T>,
198) -> Vector3<T> {
199    let pw = *persp * *world;
200    let inv = pw.inverse();
201    let in_x = (T::two() * (pt.x - lb.x) / (rt.x - lb.x)) - <T as One>::one();
202    let in_y = (T::two() * (pt.y - lb.y) / (rt.y - lb.y)) - <T as One>::one();
203    let in_z = (T::two() * pt.z) - <T as One>::one();
204    let in_w = <T as One>::one();
205    let inp = Vector4::new(in_x, in_y, in_z, in_w);
206    let out = inv * inp;
207    let out4 = out / out.w;
208    Vector3::new(out4.x, out4.y, out4.z)
209}
210
211/// Creates a perspective projection matrix from frustum bounds.
212///
213/// # Parameters
214/// - `lbn`: Left-bottom-near corner (x, y, z)
215/// - `rtf`: Right-top-far corner (x, y, z)
216///
217/// The frustum is defined by the near and far clipping planes.
218pub fn frustum<T: Scalar>(lbn: &Vector3<T>, rtf: &Vector3<T>) -> Matrix4<T> {
219    let width = rtf.x - lbn.x;
220    let height = rtf.y - lbn.y;
221    let depth = rtf.z - lbn.z;
222    let a = (rtf.x + lbn.x) / width;
223    let b = (rtf.y + lbn.y) / height;
224    let c = -(rtf.z + lbn.z) / depth;
225    let d = -(T::two() * rtf.z * lbn.z) / depth;
226
227    Matrix4::new(
228        T::two() * lbn.z / width,
229        <T as Zero>::zero(),
230        <T as Zero>::zero(),
231        <T as Zero>::zero(),
232        <T as Zero>::zero(),
233        T::two() * lbn.z / height,
234        <T as Zero>::zero(),
235        <T as Zero>::zero(),
236        a,
237        b,
238        c,
239        -<T as One>::one(),
240        <T as Zero>::zero(),
241        <T as Zero>::zero(),
242        d,
243        <T as Zero>::zero(),
244    )
245}
246
247/// Creates an orthographic projection matrix.
248///
249/// # Parameters
250/// - `left`, `right`: X-axis bounds
251/// - `bottom`, `top`: Y-axis bounds
252/// - `near`, `far`: Z-axis bounds (depth)
253///
254/// Objects maintain their size regardless of depth in orthographic projection.
255pub fn ortho4<T: Scalar>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Matrix4<T> {
256    let width = right - left;
257    let height = top - bottom;
258    let depth = far - near;
259    let r00 = T::two() / width;
260    let r11 = T::two() / height;
261    let r22 = -T::two() / depth;
262    let r03 = -(right + left) / width;
263    let r13 = -(top + bottom) / height;
264    let r23 = -(far + near) / depth;
265    Matrix4::new(
266        r00,
267        <T as Zero>::zero(),
268        <T as Zero>::zero(),
269        <T as Zero>::zero(),
270        <T as Zero>::zero(),
271        r11,
272        <T as Zero>::zero(),
273        <T as Zero>::zero(),
274        <T as Zero>::zero(),
275        <T as Zero>::zero(),
276        r22,
277        <T as Zero>::zero(),
278        r03,
279        r13,
280        r23,
281        <T as One>::one(),
282    )
283}
284
285/// Creates a perspective projection matrix.
286///
287/// # Parameters
288/// - `fovy`: Field of view angle in radians (vertical)
289/// - `aspect`: Aspect ratio (width / height)
290/// - `near`: Near clipping plane distance
291/// - `far`: Far clipping plane distance
292///
293/// Uses the standard OpenGL perspective projection formula.
294pub fn perspective<T: FloatScalar>(fovy: T, aspect: T, near: T, far: T) -> Matrix4<T> {
295    let f = <T as One>::one() / T::ttan(fovy * T::half());
296    let denom = near - far;
297    let a = (far + near) / denom;
298    let b = (T::two() * far * near) / denom;
299
300    Matrix4::new(
301        f / aspect,
302        <T as Zero>::zero(),
303        <T as Zero>::zero(),
304        <T as Zero>::zero(),
305        <T as Zero>::zero(),
306        f,
307        <T as Zero>::zero(),
308        <T as Zero>::zero(),
309        <T as Zero>::zero(),
310        <T as Zero>::zero(),
311        a,
312        -<T as One>::one(),
313        <T as Zero>::zero(),
314        <T as Zero>::zero(),
315        b,
316        <T as Zero>::zero(),
317    )
318}
319
320/// Creates a view matrix looking from eye position to target.
321///
322/// # Parameters
323/// - `eye`: Camera position
324/// - `dest`: Target position to look at
325/// - `up`: Up vector (typically (0, 1, 0))
326///
327/// The resulting matrix transforms from world space to view space.
328pub fn lookat<T: FloatScalar>(eye: &Vector3<T>, dest: &Vector3<T>, up: &Vector3<T>) -> Matrix4<T> {
329    let f = Vector3::normalize(&(*dest - *eye));
330    let s = Vector3::normalize(&Vector3::cross(&f, up));
331    let u = Vector3::normalize(&Vector3::cross(&s, &f));
332
333    let trans = translate(-*eye);
334
335    let m = Matrix4::new(
336        s.x,
337        u.x,
338        -f.x,
339        <T as Zero>::zero(),
340        s.y,
341        u.y,
342        -f.y,
343        <T as Zero>::zero(),
344        s.z,
345        u.z,
346        -f.z,
347        <T as Zero>::zero(),
348        <T as Zero>::zero(),
349        <T as Zero>::zero(),
350        <T as Zero>::zero(),
351        <T as One>::one(),
352    );
353    m * trans
354}
355
356/// Decomposes a transformation matrix into scale, rotation, and translation.
357///
358/// # Returns
359/// - `Some((scale, rotation, translation))` if successful
360/// - `None` if the matrix is singular or has zero scale
361///
362/// # Note
363/// This assumes the matrix represents a valid affine transformation.
364pub fn decompose<T: FloatScalar>(m: &Matrix4<T>) -> Option<(Vector3<T>, Quat<T>, Vector3<T>)> {
365    let mut col0 = Vector3::new(m.col[0].x, m.col[0].y, m.col[0].z);
366    let mut col1 = Vector3::new(m.col[1].x, m.col[1].y, m.col[1].z);
367    let mut col2 = Vector3::new(m.col[2].x, m.col[2].y, m.col[2].z);
368    let det = m.determinant();
369
370    // the scale needs to be tested
371    let mut scale = Vector3::new(
372        Vector3::length(&col0),
373        Vector3::length(&col1),
374        Vector3::length(&col2),
375    );
376    let trans = Vector3::new(m.col[3].x, m.col[3].y, m.col[3].z);
377
378    if det < <T as Zero>::zero() {
379        scale = -scale;
380    }
381
382    if scale.x != <T as Zero>::zero() {
383        col0 = col0 / scale.x;
384    } else {
385        return Option::None;
386    }
387
388    if scale.y != <T as Zero>::zero() {
389        col1 = col1 / scale.y;
390    } else {
391        return Option::None;
392    }
393
394    if scale.z != <T as Zero>::zero() {
395        col2 = col2 / scale.z;
396    } else {
397        return Option::None;
398    }
399
400    let rot_matrix = Matrix3::new(
401        col0.x, col0.y, col0.z, col1.x, col1.y, col1.z, col2.x, col2.y, col2.z,
402    );
403
404    let rot = Quat::of_matrix3(&rot_matrix);
405
406    Some((scale, rot, trans))
407}
408
409#[cfg(test)]
410mod tests {
411    use super::*;
412    #[test]
413    pub fn test_decompose() {
414        let ms = scale(Vector3::<f32>::new(4.0, 5.0, 6.0));
415        let mt = translate(Vector3::<f32>::new(1.0, 2.0, 3.0));
416        let q = Quat::<f32>::of_axis_angle(&Vector3::new(1.0, 1.0, 1.0), 1.0);
417        let mr = rotation_from_quat(&q);
418
419        let m = mt * mr * ms;
420
421        let v = decompose(&m);
422        match v {
423            None => assert_eq!(1, 2),
424            Some((s, r, t)) => {
425                assert_eq!((s.x - 4.0) < f32::epsilon(), true);
426                assert_eq!((s.y - 5.0) < f32::epsilon(), true);
427                assert_eq!((s.z - 6.0) < f32::epsilon(), true);
428
429                assert_eq!((q.x - r.x) < f32::epsilon(), true);
430                assert_eq!((q.y - r.y) < f32::epsilon(), true);
431                assert_eq!((q.z - r.z) < f32::epsilon(), true);
432                assert_eq!((q.w - r.w) < f32::epsilon(), true);
433
434                assert_eq!((t.x - 1.0) < f32::epsilon(), true);
435                assert_eq!((t.y - 2.0) < f32::epsilon(), true);
436                assert_eq!((t.z - 3.0) < f32::epsilon(), true);
437            }
438        }
439    }
440}