@@ -14,9 +14,9 @@ use glam::Mat3;
14
14
pub struct Basis {
15
15
/// Matrix rows. These are **not** the basis vectors!
16
16
///
17
- /// This is a transposed view for performance. <br>
18
- /// To read basis vectors, see [`x ()`][Self::x ], [`y ()`][Self::y ], [`z ()`][Self::z]. <br>
19
- /// To write them, see [`set_x ()`][Self::set_x ], [`set_y ()`][Self::set_x ], [`set_z ()`][Self::set_x ].
17
+ /// This is a transposed view for performance.<br>
18
+ /// To read basis vectors, see [`a ()`][Self::a ], [`b ()`][Self::b ], [`c ()`][Self::c]. <br>
19
+ /// To write them, see [`set_a ()`][Self::set_a ], [`set_b ()`][Self::set_b ], [`set_c ()`][Self::set_c ].
20
20
pub elements : [ Vector3 ; 3 ] ,
21
21
}
22
22
@@ -51,7 +51,7 @@ impl Basis {
51
51
/// Constructs a basis matrix from 3 linearly independent basis vectors (matrix columns).
52
52
///
53
53
/// This is the typical way to construct a basis. If you want to fill in the elements one-by-one,
54
- /// consider using [`Self::from_rows()`] instead .
54
+ /// consider using [`Self::from_rows()`].
55
55
#[ inline]
56
56
pub const fn from_basis_vectors ( a : Vector3 , b : Vector3 , c : Vector3 ) -> Self {
57
57
Self {
@@ -78,7 +78,8 @@ impl Basis {
78
78
/// Vector3::new(a.z, b.z, c.z),
79
79
/// );
80
80
/// ```
81
- /// In that case, the vectors `a`, `b` and `c` are the basis vectors.
81
+ /// The vectors `a`, `b` and `c` are the basis vectors.<br>
82
+ /// In this particular case, you could also use [`Self::from_basis_vectors(a, b, c)`][Self::from_basis_vectors()] instead.
82
83
#[ inline]
83
84
pub const fn from_rows (
84
85
x_components : Vector3 ,
@@ -221,13 +222,13 @@ impl Basis {
221
222
222
223
let s: f32 = 1.0 / det;
223
224
224
- self . set_x ( Vector3 :: new ( co[ 0 ] * s, co[ 1 ] * s, co[ 2 ] * s) ) ;
225
- self . set_y ( Vector3 :: new (
225
+ self . set_a ( Vector3 :: new ( co[ 0 ] * s, co[ 1 ] * s, co[ 2 ] * s) ) ;
226
+ self . set_b ( Vector3 :: new (
226
227
( x. z * z. y - x. y * z. z ) * s,
227
228
( x. x * z. z - x. z * z. x ) * s,
228
229
( x. y * z. x - x. x * z. y ) * s,
229
230
) ) ;
230
- self . set_z ( Vector3 :: new (
231
+ self . set_c ( Vector3 :: new (
231
232
( x. y * y. z - x. z * y. y ) * s,
232
233
( x. z * y. x - x. x * y. z ) * s,
233
234
( x. x * y. y - x. y * y. x ) * s,
@@ -287,19 +288,19 @@ impl Basis {
287
288
) ;
288
289
289
290
// Gram-Schmidt Process
290
- let mut x = self . x ( ) ;
291
- let mut y = self . y ( ) ;
292
- let mut z = self . z ( ) ;
291
+ let mut x = self . a ( ) ;
292
+ let mut y = self . b ( ) ;
293
+ let mut z = self . c ( ) ;
293
294
294
295
x = x. normalized ( ) ;
295
296
y = y - x * ( x. dot ( y) ) ;
296
297
y = y. normalized ( ) ;
297
298
z = z - x * ( x. dot ( z) ) - y * ( y. dot ( z) ) ;
298
299
z = z. normalized ( ) ;
299
300
300
- self . set_x ( x) ;
301
- self . set_y ( y) ;
302
- self . set_z ( z) ;
301
+ self . set_a ( x) ;
302
+ self . set_b ( y) ;
303
+ self . set_c ( z) ;
303
304
}
304
305
305
306
#[ inline]
@@ -308,9 +309,7 @@ impl Basis {
308
309
m. is_equal_approx ( & Self :: IDENTITY )
309
310
}
310
311
311
- /// Returns an orthonormalized version of the matrix.
312
- ///
313
- /// See [`Basis::orthonormalize()`](#method.orthonormalize)
312
+ /// Returns an orthonormalized version of the matrix: 3 orthogonal basis vectors of unit length.
314
313
#[ inline]
315
314
pub fn orthonormalized ( & self ) -> Self {
316
315
let mut copy = * self ;
@@ -326,25 +325,27 @@ impl Basis {
326
325
&& self . elements [ 2 ] . is_equal_approx ( other. elements [ 2 ] )
327
326
}
328
327
329
- /// Multiplies the matrix from left by the rotation matrix: M -> R. M
328
+ /// Multiplies the matrix from left with the rotation matrix: M -> R· M
330
329
///
331
330
/// The main use of `Basis` is as a `Transform.basis`, which is used as the transformation matrix
332
331
/// of the 3D object. `rotated()` here refers to rotation of the object (which is `R * self`), not the matrix itself.
333
332
#[ inline]
334
333
pub fn rotated ( & self , axis : Vector3 , phi : f32 ) -> Self {
335
- let rot = Basis :: from_axis_angle ( axis, phi) ;
336
- rot * ( * self )
334
+ let mut copy = * self ;
335
+ copy. rotate ( axis, phi) ;
336
+ copy
337
337
}
338
338
339
339
/// Rotates the matrix.
340
340
///
341
341
/// If object rotation is needed, see [`Basis::rotated()`]
342
342
#[ inline]
343
- #[ allow( unused) ] // useful to have around, if more methods are added
344
343
fn rotate ( & mut self , axis : Vector3 , phi : f32 ) {
345
- * self = self . rotated ( axis, phi) ;
344
+ let rot = Self :: from_axis_angle ( axis, phi) ;
345
+ * self = rot * * self ;
346
346
}
347
347
348
+ /// Returns true if this basis represents a rotation matrix (orthogonal and no scaling).
348
349
#[ inline]
349
350
fn is_rotation ( & self ) -> bool {
350
351
let det = self . determinant ( ) ;
@@ -356,11 +357,8 @@ impl Basis {
356
357
pub fn scale ( & self ) -> Vector3 {
357
358
let det = self . determinant ( ) ;
358
359
let det_sign = if det < 0.0 { -1.0 } else { 1.0 } ;
359
- Vector3 :: new (
360
- Vector3 :: new ( self . elements [ 0 ] . x , self . elements [ 1 ] . x , self . elements [ 2 ] . x ) . length ( ) ,
361
- Vector3 :: new ( self . elements [ 0 ] . y , self . elements [ 1 ] . y , self . elements [ 2 ] . y ) . length ( ) ,
362
- Vector3 :: new ( self . elements [ 0 ] . z , self . elements [ 1 ] . z , self . elements [ 2 ] . z ) . length ( ) ,
363
- ) * det_sign
360
+
361
+ Vector3 :: new ( self . a ( ) . length ( ) , self . b ( ) . length ( ) , self . c ( ) . length ( ) ) * det_sign
364
362
}
365
363
366
364
/// Introduce an additional scaling specified by the given 3D scaling factor.
@@ -371,7 +369,7 @@ impl Basis {
371
369
copy
372
370
}
373
371
374
- /// Multiplies the matrix from left by the scaIling matrix: M -> S. M
372
+ /// Multiplies the matrix from left with the scaling matrix: M -> S· M
375
373
///
376
374
/// See the comment for [Basis::rotated](#method.rotated) for further explanation.
377
375
#[ inline]
@@ -511,68 +509,64 @@ impl Basis {
511
509
/// Note: This results in a multiplication by the inverse of the matrix only if it represents a rotation-reflection.
512
510
#[ inline]
513
511
pub fn xform_inv ( & self , v : Vector3 ) -> Vector3 {
514
- Vector3 :: new (
515
- ( self . elements [ 0 ] . x * v. x ) + ( self . elements [ 1 ] . x * v. y ) + ( self . elements [ 2 ] . x * v. z ) ,
516
- ( self . elements [ 0 ] . y * v. x ) + ( self . elements [ 1 ] . y * v. y ) + ( self . elements [ 2 ] . y * v. z ) ,
517
- ( self . elements [ 0 ] . z * v. x ) + ( self . elements [ 1 ] . z * v. y ) + ( self . elements [ 2 ] . z * v. z ) ,
518
- )
512
+ Vector3 :: new ( self . a ( ) . dot ( v) , self . b ( ) . dot ( v) , self . c ( ) . dot ( v) )
519
513
}
520
514
521
515
/// Transposed dot product with the **X basis vector** of the matrix.
522
516
#[ inline]
523
517
pub ( crate ) fn tdotx ( & self , v : Vector3 ) -> f32 {
524
- self . x ( ) . dot ( v)
518
+ self . a ( ) . dot ( v)
525
519
}
526
520
527
521
/// Transposed dot product with the **Y basis vector** of the matrix.
528
522
#[ inline]
529
523
pub ( crate ) fn tdoty ( & self , v : Vector3 ) -> f32 {
530
- self . y ( ) . dot ( v)
524
+ self . b ( ) . dot ( v)
531
525
}
532
526
533
527
/// Transposed dot product with the **Z basis vector** of the matrix.
534
528
#[ inline]
535
529
pub ( crate ) fn tdotz ( & self , v : Vector3 ) -> f32 {
536
- self . z ( ) . dot ( v)
530
+ self . c ( ) . dot ( v)
537
531
}
538
532
539
- /// Get the **X basis vector** (first column vector of the matrix).
533
+ /// Get the **1st basis vector** (first column vector of the matrix).
540
534
#[ inline]
541
- pub fn x ( & self ) -> Vector3 {
535
+ pub fn a ( & self ) -> Vector3 {
542
536
Vector3 :: new ( self . elements [ 0 ] . x , self . elements [ 1 ] . x , self . elements [ 2 ] . x )
543
537
}
544
538
545
- /// Set the **X basis vector** (first column vector of the matrix).
539
+ /// Set the **1st basis vector** (first column vector of the matrix).
546
540
#[ inline]
547
- pub fn set_x ( & mut self , v : Vector3 ) {
541
+ pub fn set_a ( & mut self , v : Vector3 ) {
548
542
self . elements [ 0 ] . x = v. x ;
549
543
self . elements [ 1 ] . x = v. y ;
550
544
self . elements [ 2 ] . x = v. z ;
551
545
}
552
546
553
- /// Get the **Y basis vector** (second column vector of the matrix).
547
+ /// Get the **2nd basis vector** (second column vector of the matrix).
554
548
#[ inline]
555
- pub fn y ( & self ) -> Vector3 {
549
+ pub fn b ( & self ) -> Vector3 {
556
550
Vector3 :: new ( self . elements [ 0 ] . y , self . elements [ 1 ] . y , self . elements [ 2 ] . y )
557
551
}
558
552
559
- /// Set the **Y basis vector** (second column vector of the matrix).
553
+ /// Set the **2nd basis vector** (second column vector of the matrix).
560
554
#[ inline]
561
- pub fn set_y ( & mut self , v : Vector3 ) {
555
+ pub fn set_b ( & mut self , v : Vector3 ) {
562
556
self . elements [ 0 ] . y = v. x ;
563
557
self . elements [ 1 ] . y = v. y ;
564
558
self . elements [ 2 ] . y = v. z ;
565
559
}
566
560
567
- /// Get the **Z basis vector** (third column vector of the matrix).
561
+ /// Get the **3rd basis vector** (third column vector of the matrix).
568
562
#[ inline]
569
- pub fn z ( & self ) -> Vector3 {
563
+ pub fn c ( & self ) -> Vector3 {
570
564
Vector3 :: new ( self . elements [ 0 ] . z , self . elements [ 1 ] . z , self . elements [ 2 ] . z )
571
565
}
572
566
573
- /// Set the **Z basis vector** (third column vector of the matrix).
567
+ /// Set the **3rd basis vector** (third column vector of the matrix).
574
568
#[ inline]
575
- pub fn set_z ( & mut self , v : Vector3 ) {
569
+ pub fn set_c ( & mut self , v : Vector3 ) {
576
570
self . elements [ 0 ] . z = v. x ;
577
571
self . elements [ 1 ] . z = v. y ;
578
572
self . elements [ 2 ] . z = v. z ;
@@ -657,9 +651,9 @@ mod tests {
657
651
] ,
658
652
} ;
659
653
660
- assert ! ( basis. x ( ) == Vector3 :: new( 1.0 , 4.0 , 7.0 ) ) ;
661
- assert ! ( basis. y ( ) == Vector3 :: new( 2.0 , 5.0 , 8.0 ) ) ;
662
- assert ! ( basis. z ( ) == Vector3 :: new( 3.0 , 6.0 , 9.0 ) ) ;
654
+ assert ! ( basis. a ( ) == Vector3 :: new( 1.0 , 4.0 , 7.0 ) ) ;
655
+ assert ! ( basis. b ( ) == Vector3 :: new( 2.0 , 5.0 , 8.0 ) ) ;
656
+ assert ! ( basis. c ( ) == Vector3 :: new( 3.0 , 6.0 , 9.0 ) ) ;
663
657
}
664
658
665
659
#[ test]
@@ -668,9 +662,9 @@ mod tests {
668
662
elements : [ Vector3 :: ZERO , Vector3 :: ZERO , Vector3 :: ZERO ] ,
669
663
} ;
670
664
671
- basis. set_x ( Vector3 :: new ( 1.0 , 4.0 , 7.0 ) ) ;
672
- basis. set_y ( Vector3 :: new ( 2.0 , 5.0 , 8.0 ) ) ;
673
- basis. set_z ( Vector3 :: new ( 3.0 , 6.0 , 9.0 ) ) ;
665
+ basis. set_a ( Vector3 :: new ( 1.0 , 4.0 , 7.0 ) ) ;
666
+ basis. set_b ( Vector3 :: new ( 2.0 , 5.0 , 8.0 ) ) ;
667
+ basis. set_c ( Vector3 :: new ( 3.0 , 6.0 , 9.0 ) ) ;
674
668
675
669
assert ! ( basis. elements[ 0 ] == Vector3 :: new( 1.0 , 2.0 , 3.0 ) ) ;
676
670
assert ! ( basis. elements[ 1 ] == Vector3 :: new( 4.0 , 5.0 , 6.0 ) ) ;
0 commit comments