rinia 0.0.11

Stable numeric foundations for graphics, animation, and simulation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use core::ops::{Add, Div, Mul, Neg, Sub};

use crate::{
    Vector,
    algebra::{
        ApproxEqAbs, ApproxEqRel, Dot, Identity, Inverse, Length, LengthSquared, Lerp, Normalize,
    },
    numeric::{Clamp, NegOne, One, Sqrt, Trigonometry, Two, Zero},
    quaternion::Quaternion,
};

// Identity inherent
impl<T> Quaternion<T>
where
    T: Zero + One,
{
    /// The identity quaternion (0, 0, 0, 1).
    pub const IDENTITY: Self = Self::new(T::ZERO, T::ZERO, T::ZERO, T::ONE);
}

// Identity trait
impl<T> Identity for Quaternion<T>
where
    T: Zero + One,
{
    const IDENTITY: Self = Quaternion::IDENTITY;
}

// Approx eq abs inherent
impl<T> Quaternion<T>
where
    T: Copy + ApproxEqAbs<Tolerance = T> + Neg<Output = T>,
{
    /// Default absolute tolerance for approximate equality checks.
    const DEFAULT_TOLERANCE_ABS: T = T::DEFAULT_TOLERANCE_ABS;

    /// Checks if two quaternions are approximately equal within a given absolute tolerance.
    #[inline]
    pub fn approx_eq_abs_tol(self, rhs: Self, tolerance: T) -> bool {
        let lhs = self.into_tuple();
        let rhs = rhs.into_tuple();

        lhs.approx_eq_abs_tol(rhs, tolerance) || lhs.approx_eq_abs_tol(-rhs, tolerance)
    }

    /// Checks if two quaternions are approximately equal within a given default absolute tolerance.
    #[inline]
    pub fn approx_eq_abs(self, rhs: Self) -> bool {
        self.approx_eq_abs_tol(rhs, Self::DEFAULT_TOLERANCE_ABS)
    }
}

// Approx eq abs trait
impl<T> ApproxEqAbs for Quaternion<T>
where
    T: Copy + ApproxEqAbs<Tolerance = T> + Neg<Output = T>,
{
    type Tolerance = T;
    const DEFAULT_TOLERANCE_ABS: Self::Tolerance = T::DEFAULT_TOLERANCE_ABS;

    #[inline]
    fn approx_eq_abs_tol(self, other: Self, tol: Self::Tolerance) -> bool {
        Quaternion::approx_eq_abs_tol(self, other, tol)
    }
}

// Approx eq rel inherent
impl<T> Quaternion<T>
where
    T: Copy + ApproxEqRel<Tolerance = T> + Neg<Output = T>,
{
    /// Default relative tolerance for approximate equality checks.
    const DEFAULT_TOLERANCE_REL: T = T::DEFAULT_TOLERANCE_REL;

    /// Checks if two quaternions are approximately equal within a given relative tolerance.
    #[inline]
    pub fn approx_eq_rel_tol(self, rhs: Self, tolerance: T) -> bool {
        let lhs = self.into_tuple();
        let rhs = rhs.into_tuple();
        lhs.approx_eq_rel_tol(rhs, tolerance) || lhs.approx_eq_rel_tol(-rhs, tolerance)
    }

    /// Checks if two quaternions are approximately equal within a given default relative tolerance.
    #[inline]
    pub fn approx_eq_rel(self, rhs: Self) -> bool {
        self.approx_eq_rel_tol(rhs, Self::DEFAULT_TOLERANCE_REL)
    }
}

// Approx eq rel trait
impl<T> ApproxEqRel for Quaternion<T>
where
    T: Copy + ApproxEqRel<Tolerance = T> + Neg<Output = T>,
{
    type Tolerance = T;
    const DEFAULT_TOLERANCE_REL: Self::Tolerance = T::DEFAULT_TOLERANCE_REL;

    #[inline]
    fn approx_eq_rel_tol(self, other: Self, tol: Self::Tolerance) -> bool {
        Quaternion::approx_eq_rel_tol(self, other, tol)
    }
}

// Length and length squared inherent
impl<T> Quaternion<T>
where
    T: Copy + Mul<Output = T> + Add<Output = T>,
{
    /// Computes the squared length of the quaternion.
    #[inline]
    pub fn length_squared(self) -> T {
        self.into_tuple().length_squared()
    }

    /// Computes the length (magnitude) of the quaternion.
    #[inline]
    pub fn length(self) -> T
    where
        T: Sqrt,
    {
        self.length_squared().sqrt()
    }

    /// Returns a normalized quaternion with length one.
    #[inline]
    pub fn normalize(self) -> Self
    where
        T: Sqrt + Div<Output = T>,
    {
        self / self.length()
    }
}

// Length squared trait
impl<T> LengthSquared for Quaternion<T>
where
    T: Copy + Mul<Output = T> + Add<Output = T>,
{
    type Output = T;

    #[inline]
    fn length_squared(self) -> Self::Output {
        Quaternion::length_squared(self)
    }
}

// Length trait
impl<T> Length for Quaternion<T>
where
    T: Copy + Mul<Output = T> + Add<Output = T> + Sqrt,
{
    #[inline]
    fn length(self) -> Self::Output {
        Quaternion::length(self)
    }
}

// Normalize trait
impl<T> Normalize for Quaternion<T>
where
    T: Copy + Mul<Output = T> + Add<Output = T> + Div<Output = T> + Sqrt,
{
    #[inline]
    fn normalize(self) -> Self {
        Quaternion::normalize(self)
    }
}

// Dot inherent
impl<T> Quaternion<T>
where
    T: Copy + Mul<Output = T> + Add<Output = T>,
{
    /// Computes the dot product of two quaternions.
    #[inline]
    pub fn dot(self, rhs: Self) -> T {
        self.into_tuple().dot(rhs.into_tuple())
    }
}

// Dot trait
impl<T> Dot for Quaternion<T>
where
    T: Copy + Mul<Output = T> + Add<Output = T>,
{
    type Output = T;

    #[inline]
    fn dot(self, rhs: Self) -> Self::Output {
        Quaternion::dot(self, rhs)
    }
}

// Conjugate inherent
impl<T> Quaternion<T>
where
    T: Copy + Neg<Output = T>,
{
    /// Returns the conjugate of the quaternion.
    #[inline]
    pub fn conjugate(self) -> Self {
        Self::new(-self.x, -self.y, -self.z, self.w)
    }
}

// Inverse inherent
impl<T> Quaternion<T>
where
    T: Copy + Mul<Output = T> + Add<Output = T> + Div<Output = T> + Neg<Output = T>,
{
    /// Returns the inverse of the quaternion.
    #[inline]
    pub fn inverse(self) -> Self {
        self.conjugate() / self.length_squared()
    }
}

// Inverse trait
impl<T> Inverse for Quaternion<T>
where
    T: Copy + Mul<Output = T> + Add<Output = T> + Div<Output = T> + Neg<Output = T>,
{
    #[inline]
    fn inverse(self) -> Self {
        Quaternion::inverse(self)
    }
}

// Compose inherent
impl<T> Quaternion<T>
where
    T: Copy + Mul<Output = T> + Add<Output = T> + Sub<Output = T>,
{
    /// Composes two quaternions, returning a quaternion representing
    /// the combined rotation.
    #[inline]
    pub fn compose(self, rhs: Self) -> Self {
        let (x1, y1, z1, w1) = (self.x, self.y, self.z, self.w);
        let (x2, y2, z2, w2) = (rhs.x, rhs.y, rhs.z, rhs.w);

        let x = w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2;
        let y = w1 * y2 - x1 * z2 + y1 * w2 + z1 * x2;
        let z = w1 * z2 + x1 * y2 - y1 * x2 + z1 * w2;
        let w = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2;

        Self::new(x, y, z, w)
    }
}

// Rotate vector inherent
impl<T> Quaternion<T>
where
    T: Copy + Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Two,
{
    /// Rotates a vector by this quaternion.
    #[inline]
    pub fn rotate_vector(self, rhs: Vector<T, 3>) -> Vector<T, 3> {
        let u = Vector::<T, 3>::new(self.x, self.y, self.z);
        let w = self.w;

        let uv = u.cross(rhs);
        let uuv = u.cross(uv);

        rhs + uv * (w * T::TWO) + uuv * T::TWO
    }
}

// Lerp inherent
impl<T> Quaternion<T>
where
    T: Copy + Sub<Output = T> + Mul<Output = T> + Add<Output = T>,
{
    /// Performs linear interpolation between two quaternions.
    ///
    /// The result is not guaranteed to be normalized.
    #[inline]
    pub fn lerp(self, rhs: Self, t: T) -> Self {
        self + (rhs - self) * t
    }
}

// Lerp trait
impl<T> Lerp for Quaternion<T>
where
    T: Copy + Sub<Output = T> + Mul<Output = T> + Add<Output = T>,
{
    type Scalar = T;

    #[inline]
    fn lerp(self, rhs: Self, t: Self::Scalar) -> Self {
        Quaternion::lerp(self, rhs, t)
    }
}

// Nlerp inherent
impl<T> Quaternion<T>
where
    T: Copy
        + Sub<Output = T>
        + Mul<Output = T>
        + Add<Output = T>
        + Div<Output = T>
        + Neg<Output = T>
        + Zero
        + Sqrt
        + PartialOrd,
{
    /// Performs normalized linear interpolation between two quaternions.
    #[inline]
    pub fn nlerp(self, mut rhs: Self, t: T) -> Self {
        if self.dot(rhs) < T::ZERO {
            rhs = -rhs;
        }

        self.lerp(rhs, t).normalize()
    }
}

impl<T> Quaternion<T>
where
    T: Copy
        + Sub<Output = T>
        + Add<Output = T>
        + Mul<Output = T>
        + Div<Output = T>
        + Neg<Output = T>
        + Sqrt
        + Trigonometry
        + Zero
        + One
        + NegOne
        + Clamp
        + PartialOrd
        + ApproxEqAbs<Tolerance = T>,
{
    /// Performs spherical linear interpolation between two quaternions.
    ///
    /// The shortest rotation path is used.
    #[inline]
    pub fn slerp(self, mut rhs: Self, t: T) -> Self {
        let mut dot = self.dot(rhs);

        // Take shortest path.
        if dot < T::ZERO {
            rhs = -rhs;
            dot = -dot;
        }

        // Protect acos from floating point drift.
        dot = dot.clamp_value(T::NEG_ONE, T::ONE);

        // Avoid instability when the angle is very small.
        if dot.approx_eq_abs(T::ONE) {
            return self.nlerp(rhs, t);
        }

        let theta = dot.acos();
        let sin_theta = theta.sin();

        let a = ((T::ONE - t) * theta).sin() / sin_theta;
        let b = (t * theta).sin() / sin_theta;

        self * a + rhs * b
    }
}

// From axis-angle inherent
impl<T> Quaternion<T>
where
    T: Copy + Mul<Output = T> + Div<Output = T> + Trigonometry + Two,
{
    /// Creates a quaternion from an axis and rotation angle.
    ///
    /// The axis is expected to be normalized.
    #[inline]
    pub fn from_axis_angle(axis: Vector<T, 3>, angle: T) -> Self {
        let half_angle = angle / T::TWO;
        let (sin, cos) = half_angle.sin_cos();

        let axis = axis * sin;

        Self::new(axis.x, axis.y, axis.z, cos)
    }
}

// To axis-angle inherent
impl<T> Quaternion<T>
where
    T: Copy
        + Mul<Output = T>
        + Div<Output = T>
        + Trigonometry
        + Zero
        + One
        + Two
        + NegOne
        + Clamp
        + ApproxEqAbs<Tolerance = T>,
{
    /// Returns the rotation axis and angle.
    ///
    /// The quaternion is expected to be normalized.
    #[inline]
    pub fn to_axis_angle(self) -> (Vector<T, 3>, T) {
        let half_angle = self.w.clamp_value(T::NEG_ONE, T::ONE).acos();
        let angle = half_angle * T::TWO;

        let sin_half_angle = half_angle.sin();

        // Identity rotation has no unique axis.
        if sin_half_angle.approx_eq_abs(T::ZERO) {
            return (Vector::<T, 3>::new(T::ONE, T::ZERO, T::ZERO), T::ZERO);
        }

        let axis = Vector::<T, 3>::new(self.x, self.y, self.z) / sin_half_angle;

        (axis, angle)
    }
}