molgfx-math 0.3.2

glam-based math foundation: cameras, projections, bounds, splines, transport frames.
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
//! Project-owned quaternions and matrices lowered to glam operations.

use std::ops::Mul;

use bytemuck::{Pod, Zeroable};
use serde::{Deserialize, Serialize};

use super::{Vec3, Vec4};

/// A SIMD-aligned quaternion stored as `(x, y, z, w)`.
#[repr(C, align(16))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
#[serde(from = "[f32; 4]", into = "[f32; 4]")]
pub struct Quat {
    /// X component.
    pub x: f32,
    /// Y component.
    pub y: f32,
    /// Z component.
    pub z: f32,
    /// Scalar component.
    pub w: f32,
}

impl Quat {
    /// Identity rotation.
    pub const IDENTITY: Self = Self::from_xyzw(0.0, 0.0, 0.0, 1.0);
    /// Builds a quaternion.
    #[must_use]
    #[inline]
    pub const fn from_xyzw(x: f32, y: f32, z: f32, w: f32) -> Self {
        Self { x, y, z, w }
    }
    /// Builds from an array.
    #[must_use]
    #[inline]
    pub const fn from_array(value: [f32; 4]) -> Self {
        Self::from_xyzw(value[0], value[1], value[2], value[3])
    }
    /// Returns components.
    #[must_use]
    #[inline]
    pub const fn to_array(self) -> [f32; 4] {
        [self.x, self.y, self.z, self.w]
    }
    /// Rotation around X.
    #[must_use]
    #[inline]
    pub fn from_rotation_x(angle: f32) -> Self {
        glam::Quat::from_rotation_x(angle).into()
    }
    /// Rotation around Y.
    #[must_use]
    #[inline]
    pub fn from_rotation_y(angle: f32) -> Self {
        glam::Quat::from_rotation_y(angle).into()
    }
    /// Rotation around Z.
    #[must_use]
    #[inline]
    pub fn from_rotation_z(angle: f32) -> Self {
        glam::Quat::from_rotation_z(angle).into()
    }
    /// Rotation around a normalized axis.
    #[must_use]
    #[inline]
    pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
        glam::Quat::from_axis_angle(axis.into(), angle).into()
    }
    /// Shortest rotation between directions.
    #[must_use]
    #[inline]
    pub fn from_rotation_arc(from: Vec3, to: Vec3) -> Self {
        glam::Quat::from_rotation_arc(from.into(), to.into()).into()
    }
    /// Builds from a rotation matrix.
    #[must_use]
    #[inline]
    pub fn from_mat3(matrix: &Mat3) -> Self {
        glam::Quat::from_mat3(&(*matrix).into()).into()
    }
    /// Spherical interpolation.
    #[must_use]
    #[inline]
    pub fn slerp(self, rhs: Self, amount: f32) -> Self {
        glam::Quat::from(self).slerp(rhs.into(), amount).into()
    }
    /// Returns whether every component is finite.
    #[must_use]
    #[inline]
    pub fn is_finite(self) -> bool {
        glam::Quat::from(self).is_finite()
    }
    /// Squared quaternion length.
    #[must_use]
    #[inline]
    pub fn length_squared(self) -> f32 {
        glam::Quat::from(self).length_squared()
    }
    /// Returns a normalized quaternion.
    #[must_use]
    #[inline]
    pub fn normalize(self) -> Self {
        glam::Quat::from(self).normalize().into()
    }
}

impl Default for Quat {
    #[inline]
    fn default() -> Self {
        Self::IDENTITY
    }
}
impl From<[f32; 4]> for Quat {
    #[inline]
    fn from(value: [f32; 4]) -> Self {
        Self::from_array(value)
    }
}
impl From<Quat> for [f32; 4] {
    #[inline]
    fn from(value: Quat) -> Self {
        value.to_array()
    }
}
impl From<glam::Quat> for Quat {
    #[inline]
    fn from(value: glam::Quat) -> Self {
        value.to_array().into()
    }
}
impl From<Quat> for glam::Quat {
    #[inline]
    fn from(value: Quat) -> Self {
        Self::from_array(value.to_array())
    }
}
impl Mul for Quat {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: Self) -> Self {
        (glam::Quat::from(self) * glam::Quat::from(rhs)).into()
    }
}
impl Mul<Vec3> for Quat {
    type Output = Vec3;
    #[inline]
    fn mul(self, rhs: Vec3) -> Vec3 {
        (glam::Quat::from(self) * glam::Vec3::from(rhs)).into()
    }
}

/// A column-major 3×3 matrix.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
pub struct Mat3 {
    /// First column.
    pub x_axis: Vec3,
    /// Second column.
    pub y_axis: Vec3,
    /// Third column.
    pub z_axis: Vec3,
}

impl Mat3 {
    /// Identity matrix.
    pub const IDENTITY: Self = Self::from_cols(Vec3::X, Vec3::Y, Vec3::Z);
    /// Builds from columns.
    #[must_use]
    #[inline]
    pub const fn from_cols(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
        Self {
            x_axis,
            y_axis,
            z_axis,
        }
    }
    /// Extracts the linear part of a 4×4 matrix.
    #[must_use]
    #[inline]
    pub fn from_mat4(value: Mat4) -> Self {
        glam::Mat3::from_mat4(value.into()).into()
    }
    /// Builds from a quaternion.
    #[must_use]
    #[inline]
    pub fn from_quat(value: Quat) -> Self {
        glam::Mat3::from_quat(value.into()).into()
    }
    /// Matrix inverse.
    #[must_use]
    #[inline]
    pub fn inverse(self) -> Self {
        glam::Mat3::from(self).inverse().into()
    }
    /// Matrix transpose.
    #[must_use]
    #[inline]
    pub fn transpose(self) -> Self {
        glam::Mat3::from(self).transpose().into()
    }
    /// Determinant.
    #[must_use]
    #[inline]
    pub fn determinant(self) -> f32 {
        glam::Mat3::from(self).determinant()
    }
    /// Returns whether all entries are finite.
    #[must_use]
    #[inline]
    pub fn is_finite(self) -> bool {
        glam::Mat3::from(self).is_finite()
    }
}

impl From<glam::Mat3> for Mat3 {
    #[inline]
    fn from(value: glam::Mat3) -> Self {
        Self::from_cols(
            value.x_axis.into(),
            value.y_axis.into(),
            value.z_axis.into(),
        )
    }
}
impl From<Mat3> for glam::Mat3 {
    #[inline]
    fn from(value: Mat3) -> Self {
        Self::from_cols(
            value.x_axis.into(),
            value.y_axis.into(),
            value.z_axis.into(),
        )
    }
}
impl Mul for Mat3 {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: Self) -> Self {
        (glam::Mat3::from(self) * glam::Mat3::from(rhs)).into()
    }
}
impl Mul<Vec3> for Mat3 {
    type Output = Vec3;
    #[inline]
    fn mul(self, rhs: Vec3) -> Vec3 {
        (glam::Mat3::from(self) * glam::Vec3::from(rhs)).into()
    }
}

/// A SIMD-aligned column-major 4×4 matrix.
#[repr(C, align(16))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
pub struct Mat4 {
    /// First column.
    pub x_axis: Vec4,
    /// Second column.
    pub y_axis: Vec4,
    /// Third column.
    pub z_axis: Vec4,
    /// Fourth column.
    pub w_axis: Vec4,
}

impl Mat4 {
    /// Identity matrix.
    pub const IDENTITY: Self = Self::from_cols(
        Vec4::new(1.0, 0.0, 0.0, 0.0),
        Vec4::new(0.0, 1.0, 0.0, 0.0),
        Vec4::new(0.0, 0.0, 1.0, 0.0),
        Vec4::new(0.0, 0.0, 0.0, 1.0),
    );
    /// Zero matrix.
    pub const ZERO: Self = Self::from_cols(Vec4::ZERO, Vec4::ZERO, Vec4::ZERO, Vec4::ZERO);
    /// Builds from columns.
    #[must_use]
    #[inline]
    pub const fn from_cols(x_axis: Vec4, y_axis: Vec4, z_axis: Vec4, w_axis: Vec4) -> Self {
        Self {
            x_axis,
            y_axis,
            z_axis,
            w_axis,
        }
    }
    /// Builds from column-major elements.
    #[must_use]
    #[inline]
    pub fn from_cols_array(value: &[f32; 16]) -> Self {
        glam::Mat4::from_cols_array(value).into()
    }
    /// Returns column-major elements.
    #[must_use]
    #[inline]
    pub fn to_cols_array(self) -> [f32; 16] {
        glam::Mat4::from(self).to_cols_array()
    }
    /// Returns a column-major nested array.
    #[must_use]
    #[inline]
    pub fn to_cols_array_2d(self) -> [[f32; 4]; 4] {
        glam::Mat4::from(self).to_cols_array_2d()
    }
    /// Translation matrix.
    #[must_use]
    #[inline]
    pub fn from_translation(value: Vec3) -> Self {
        glam::Mat4::from_translation(value.into()).into()
    }
    /// Rotation around Y.
    #[must_use]
    #[inline]
    pub fn from_rotation_y(angle: f32) -> Self {
        glam::Mat4::from_rotation_y(angle).into()
    }
    /// Scale, rotation and translation matrix.
    #[must_use]
    #[inline]
    pub fn from_scale_rotation_translation(scale: Vec3, rotation: Quat, translation: Vec3) -> Self {
        glam::Mat4::from_scale_rotation_translation(
            scale.into(),
            rotation.into(),
            translation.into(),
        )
        .into()
    }
    /// Transforms a point.
    #[must_use]
    #[inline]
    pub fn transform_point3(self, point: Vec3) -> Vec3 {
        glam::Mat4::from(self).transform_point3(point.into()).into()
    }
    /// Transforms a direction.
    #[must_use]
    #[inline]
    pub fn transform_vector3(self, vector: Vec3) -> Vec3 {
        glam::Mat4::from(self)
            .transform_vector3(vector.into())
            .into()
    }
    /// Matrix inverse.
    #[must_use]
    #[inline]
    pub fn inverse(self) -> Self {
        glam::Mat4::from(self).inverse().into()
    }
    /// Determinant.
    #[must_use]
    #[inline]
    pub fn determinant(self) -> f32 {
        glam::Mat4::from(self).determinant()
    }
    /// Returns whether all entries are finite.
    #[must_use]
    #[inline]
    pub fn is_finite(self) -> bool {
        glam::Mat4::from(self).is_finite()
    }
    /// Returns a row.
    #[must_use]
    #[inline]
    pub fn row(self, index: usize) -> Vec4 {
        glam::Mat4::from(self).row(index).into()
    }
}

impl Default for Mat4 {
    #[inline]
    fn default() -> Self {
        Self::IDENTITY
    }
}
impl From<glam::Mat4> for Mat4 {
    #[inline]
    fn from(value: glam::Mat4) -> Self {
        Self::from_cols(
            value.x_axis.into(),
            value.y_axis.into(),
            value.z_axis.into(),
            value.w_axis.into(),
        )
    }
}
impl From<Mat4> for glam::Mat4 {
    #[inline]
    fn from(value: Mat4) -> Self {
        Self::from_cols(
            value.x_axis.into(),
            value.y_axis.into(),
            value.z_axis.into(),
            value.w_axis.into(),
        )
    }
}
impl Mul for Mat4 {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: Self) -> Self {
        (glam::Mat4::from(self) * glam::Mat4::from(rhs)).into()
    }
}
impl Mul<Vec4> for Mat4 {
    type Output = Vec4;
    #[inline]
    fn mul(self, rhs: Vec4) -> Vec4 {
        (glam::Mat4::from(self) * glam::Vec4::from(rhs)).into()
    }
}