1
- use crate :: core_types:: { Quat , Vector3 } ;
2
- use euclid :: { approxeq :: ApproxEq , default , Transform3D , UnknownUnit , Vector3D } ;
1
+ use crate :: core_types:: { IsEqualApprox , Quat , Vector3 } ;
2
+ use core :: ops :: Mul ;
3
3
4
4
/// A 3x3 matrix.
5
5
#[ repr( C ) ]
@@ -96,7 +96,7 @@ impl Basis {
96
96
#[ inline]
97
97
pub fn from_axis_angle ( axis : & Vector3 , phi : f32 ) -> Self {
98
98
assert ! (
99
- axis. length( ) . approx_eq ( & 1.0 ) ,
99
+ axis. length( ) . is_equal_approx ( 1.0 ) ,
100
100
"The axis Vector3 must be normalized."
101
101
) ;
102
102
@@ -147,7 +147,7 @@ impl Basis {
147
147
] ;
148
148
149
149
let det: f32 = x. x * co[ 0 ] + x. y * co[ 1 ] + x. z * co[ 2 ] ;
150
- assert ! ( !det. approx_eq ( & 0.0 ) , "Determinant was zero" ) ;
150
+ assert ! ( !det. is_equal_approx ( 0.0 ) , "Determinant was zero" ) ;
151
151
152
152
let s: f32 = 1.0 / det;
153
153
@@ -210,7 +210,7 @@ impl Basis {
210
210
#[ inline]
211
211
pub fn orthonormalize ( & mut self ) {
212
212
assert ! (
213
- !self . determinant( ) . approx_eq ( & 0.0 ) ,
213
+ !self . determinant( ) . is_equal_approx ( 0.0 ) ,
214
214
"Determinant should not be zero."
215
215
) ;
216
216
@@ -219,11 +219,11 @@ impl Basis {
219
219
let mut y = self . y ( ) ;
220
220
let mut z = self . z ( ) ;
221
221
222
- x = x. normalize ( ) ;
222
+ x = x. normalized ( ) ;
223
223
y = y - x * ( x. dot ( y) ) ;
224
- y = y. normalize ( ) ;
224
+ y = y. normalized ( ) ;
225
225
z = z - x * ( x. dot ( z) ) - y * ( y. dot ( z) ) ;
226
- z = z. normalize ( ) ;
226
+ z = z. normalized ( ) ;
227
227
228
228
self . set_x ( x) ;
229
229
self . set_y ( y) ;
@@ -241,23 +241,23 @@ impl Basis {
241
241
242
242
/// Returns `true` if `self` and `other` are approximately equal.
243
243
#[ inline]
244
- pub fn approx_eq ( & self , other : & Basis ) -> bool {
245
- self . elements [ 0 ] . approx_eq ( & other. elements [ 0 ] )
246
- && self . elements [ 1 ] . approx_eq ( & other. elements [ 1 ] )
247
- && self . elements [ 2 ] . approx_eq ( & other. elements [ 2 ] )
244
+ pub fn is_equal_approx ( & self , other : & Basis ) -> bool {
245
+ self . elements [ 0 ] . is_equal_approx ( other. elements [ 0 ] )
246
+ && self . elements [ 1 ] . is_equal_approx ( other. elements [ 1 ] )
247
+ && self . elements [ 2 ] . is_equal_approx ( other. elements [ 2 ] )
248
248
}
249
249
250
250
#[ inline]
251
251
fn is_orthogonal ( & self ) -> bool {
252
252
let identity = Self :: identity ( ) ;
253
253
let m = ( * self ) * self . transposed ( ) ;
254
- m. approx_eq ( & identity)
254
+ m. is_equal_approx ( & identity)
255
255
}
256
256
257
257
#[ inline]
258
258
fn is_rotation ( & self ) -> bool {
259
259
let det = self . determinant ( ) ;
260
- det. approx_eq ( & 1.0 ) && self . is_orthogonal ( )
260
+ det. is_equal_approx ( 1.0 ) && self . is_orthogonal ( )
261
261
}
262
262
263
263
/// Multiplies the matrix from left by the rotation matrix: M -> R.M
@@ -328,9 +328,9 @@ impl Basis {
328
328
let k = ( i + 2 ) % 3 ;
329
329
330
330
let elements_arr: [ [ f32 ; 3 ] ; 3 ] = [
331
- matrix. elements [ 0 ] . to_array ( ) ,
332
- matrix. elements [ 1 ] . to_array ( ) ,
333
- matrix. elements [ 2 ] . to_array ( ) ,
331
+ * matrix. elements [ 0 ] . as_ref ( ) ,
332
+ * matrix. elements [ 1 ] . as_ref ( ) ,
333
+ * matrix. elements [ 2 ] . as_ref ( ) ,
334
334
] ;
335
335
336
336
let mut s = ( elements_arr[ i] [ i] - elements_arr[ j] [ j] - elements_arr[ k] [ k] + 1.0 ) . sqrt ( ) ;
@@ -343,7 +343,7 @@ impl Basis {
343
343
}
344
344
345
345
let [ a, b, c, r] = temp;
346
- Quat :: quaternion ( a, b, c, r)
346
+ Quat :: new ( a, b, c, r)
347
347
}
348
348
349
349
/// Returns the scale of the matrix.
@@ -384,17 +384,17 @@ impl Basis {
384
384
/// See [`Basis::to_quat`](#method.to_quat) if you need a quaternion instead.
385
385
#[ inline]
386
386
pub fn to_euler ( & self ) -> Vector3 {
387
- let mut euler = Vector3 :: zero ( ) ;
387
+ let mut euler = Vector3 :: ZERO ;
388
388
389
389
let m12 = self . elements [ 1 ] . z ;
390
390
if m12 < 1.0 {
391
391
if m12 > -1.0 {
392
392
// is this a pure X rotation?
393
- if self . elements [ 1 ] . x . approx_eq ( & 0.0 )
394
- && self . elements [ 0 ] . y . approx_eq ( & 0.0 )
395
- && self . elements [ 0 ] . z . approx_eq ( & 0.0 )
396
- && self . elements [ 2 ] . x . approx_eq ( & 0.0 )
397
- && self . elements [ 0 ] . x . approx_eq ( & 1.0 )
393
+ if self . elements [ 1 ] . x . is_equal_approx ( 0.0 )
394
+ && self . elements [ 0 ] . y . is_equal_approx ( 0.0 )
395
+ && self . elements [ 0 ] . z . is_equal_approx ( 0.0 )
396
+ && self . elements [ 2 ] . x . is_equal_approx ( 0.0 )
397
+ && self . elements [ 0 ] . x . is_equal_approx ( 1.0 )
398
398
{
399
399
// return the simplest form (human friendlier in editor and scripts)
400
400
euler. x = ( -m12) . atan2 ( self . elements [ 1 ] . y ) ;
@@ -471,6 +471,7 @@ impl Basis {
471
471
)
472
472
}
473
473
474
+ /*
474
475
/// Creates a `Basis` from the rotation and scaling of the provided transform.
475
476
#[inline]
476
477
pub fn from_transform(transform: &default::Transform3D<f32>) -> Basis {
@@ -494,6 +495,7 @@ impl Basis {
494
495
],
495
496
}
496
497
}
498
+ */
497
499
498
500
/// Transposed dot product with the **X Axis** of the matrix.
499
501
#[ inline]
@@ -580,6 +582,15 @@ impl core::ops::Mul<Basis> for Basis {
580
582
}
581
583
}
582
584
585
+ impl Mul < Vector3 > for Basis {
586
+ type Output = Vector3 ;
587
+
588
+ #[ inline]
589
+ fn mul ( self , rhs : Self :: Output ) -> Self :: Output {
590
+ Self :: Output :: new ( self . tdotx ( rhs) , self . tdoty ( rhs) , self . tdotz ( rhs) )
591
+ }
592
+ }
593
+
583
594
#[ cfg( test) ]
584
595
#[ allow( clippy:: unreadable_literal) ]
585
596
mod tests {
@@ -620,7 +631,7 @@ mod tests {
620
631
#[ test]
621
632
fn set_is_sane ( ) {
622
633
let mut basis = Basis {
623
- elements : [ Vector3 :: zero ( ) , Vector3 :: zero ( ) , Vector3 :: zero ( ) ] ,
634
+ elements : [ Vector3 :: ZERO , Vector3 :: ZERO , Vector3 :: ZERO ] ,
624
635
} ;
625
636
626
637
basis. set_x ( Vector3 :: new ( 1.0 , 4.0 , 7.0 ) ) ;
@@ -634,7 +645,7 @@ mod tests {
634
645
635
646
fn test_inputs ( ) -> ( Basis , Basis ) {
636
647
let v = Vector3 :: new ( 37.51756 , 20.39467 , 49.96816 ) ;
637
- let vn = v. normalize ( ) ;
648
+ let vn = v. normalized ( ) ;
638
649
let b = Basis :: from_euler ( v) ;
639
650
let bn = Basis :: from_euler ( vn) ;
640
651
( b, bn)
@@ -644,14 +655,17 @@ mod tests {
644
655
fn determinant ( ) {
645
656
let ( b, _bn) = test_inputs ( ) ;
646
657
647
- assert ! ( b. determinant( ) . approx_eq( & 1.0 ) , "Determinant should be 1.0" ) ;
658
+ assert ! (
659
+ b. determinant( ) . is_equal_approx( 1.0 ) ,
660
+ "Determinant should be 1.0"
661
+ ) ;
648
662
}
649
663
650
664
#[ test]
651
665
fn euler ( ) {
652
666
let ( _b, bn) = test_inputs ( ) ;
653
667
654
- assert ! ( Vector3 :: new( 0.57079 , 0.310283 , 0.760213 ) . approx_eq ( & bn. to_euler( ) ) ) ;
668
+ assert ! ( Vector3 :: new( 0.57079 , 0.310283 , 0.760213 ) . is_equal_approx ( bn. to_euler( ) ) ) ;
655
669
}
656
670
657
671
#[ test]
@@ -663,7 +677,7 @@ mod tests {
663
677
Vector3 :: new ( -0.288147 , 0.94041 , 0.180557 ) ,
664
678
Vector3 :: new ( -0.95445 , -0.297299 , 0.025257 ) ,
665
679
] ) ;
666
- assert ! ( expected. approx_eq ( & b. orthonormalized( ) ) ) ;
680
+ assert ! ( expected. is_equal_approx ( & b. orthonormalized( ) ) ) ;
667
681
}
668
682
669
683
#[ test]
@@ -675,40 +689,40 @@ mod tests {
675
689
Vector3 :: new ( 0.012407 , -0.040492 , -0.007774 ) ,
676
690
Vector3 :: new ( -0.682131 , -0.212475 , 0.018051 ) ,
677
691
] ) ;
678
- assert ! ( expected. approx_eq ( & b. scaled( & Vector3 :: new( 0.677813 , -0.043058 , 0.714685 ) ) ) ) ;
692
+ assert ! ( expected. is_equal_approx ( & b. scaled( & Vector3 :: new( 0.677813 , -0.043058 , 0.714685 ) ) ) ) ;
679
693
}
680
694
681
695
#[ test]
682
696
fn rotated ( ) {
683
697
let ( b, _bn) = test_inputs ( ) ;
684
698
685
- let r = Vector3 :: new ( -50.167156 , 60.67781 , -70.04305 ) . normalize ( ) ;
699
+ let r = Vector3 :: new ( -50.167156 , 60.67781 , -70.04305 ) . normalized ( ) ;
686
700
let expected = Basis :: from_elements ( [
687
701
Vector3 :: new ( -0.676245 , 0.113805 , 0.727833 ) ,
688
702
Vector3 :: new ( -0.467094 , 0.697765 , -0.54309 ) ,
689
703
Vector3 :: new ( -0.569663 , -0.707229 , -0.418703 ) ,
690
704
] ) ;
691
- assert ! ( expected. approx_eq ( & b. rotated( r, 1.0 ) ) ) ;
705
+ assert ! ( expected. is_equal_approx ( & b. rotated( r, 1.0 ) ) ) ;
692
706
}
693
707
694
708
#[ test]
695
709
fn to_quat ( ) {
696
710
let ( b, _bn) = test_inputs ( ) ;
697
711
698
- assert ! ( Quat :: quaternion ( -0.167156 , 0.677813 , -0.043058 , 0.714685 ) . approx_eq ( & b. to_quat( ) ) ) ;
712
+ assert ! ( Quat :: new ( -0.167156 , 0.677813 , -0.043058 , 0.714685 ) . is_equal_approx ( & b. to_quat( ) ) ) ;
699
713
}
700
714
701
715
#[ test]
702
716
fn scale ( ) {
703
717
let ( b, _bn) = test_inputs ( ) ;
704
718
705
- assert ! ( Vector3 :: new( 1.0 , 1.0 , 1.0 ) . approx_eq ( & b. to_scale( ) ) ) ;
719
+ assert ! ( Vector3 :: new( 1.0 , 1.0 , 1.0 ) . is_equal_approx ( b. to_scale( ) ) ) ;
706
720
}
707
721
708
722
#[ test]
709
723
fn approx_eq ( ) {
710
724
let ( b, _bn) = test_inputs ( ) ;
711
- assert ! ( !b. approx_eq ( & Basis :: from_euler( Vector3 :: new( 37.517 , 20.394 , 49.968 ) ) ) ) ;
725
+ assert ! ( !b. is_equal_approx ( & Basis :: from_euler( Vector3 :: new( 37.517 , 20.394 , 49.968 ) ) ) ) ;
712
726
}
713
727
714
728
#[ test]
@@ -719,23 +733,23 @@ mod tests {
719
733
Vector3 :: new ( -0.165055 , 0.94041 , -0.297299 ) ,
720
734
Vector3 :: new ( 0.98324 , 0.180557 , 0.025257 ) ,
721
735
] ) ;
722
- assert ! ( expected. approx_eq ( & b. transposed( ) ) ) ;
736
+ assert ! ( expected. is_equal_approx ( & b. transposed( ) ) ) ;
723
737
}
724
738
725
739
#[ test]
726
740
fn xform ( ) {
727
741
let ( b, _bn) = test_inputs ( ) ;
728
742
729
743
assert ! ( Vector3 :: new( -0.273471 , 0.478102 , -0.690386 )
730
- . approx_eq ( & b. xform( Vector3 :: new( 0.5 , 0.7 , -0.2 ) ) ) ) ;
744
+ . is_equal_approx ( b. xform( Vector3 :: new( 0.5 , 0.7 , -0.2 ) ) ) ) ;
731
745
}
732
746
733
747
#[ test]
734
748
fn xform_inv ( ) {
735
749
let ( b, _bn) = test_inputs ( ) ;
736
750
737
751
assert ! ( Vector3 :: new( -0.884898 , -0.460316 , 0.071165 )
738
- . approx_eq ( & b. xform_inv( Vector3 :: new( 0.077431 , -0.165055 , 0.98324 ) ) ) ) ;
752
+ . is_equal_approx ( b. xform_inv( Vector3 :: new( 0.077431 , -0.165055 , 0.98324 ) ) ) ) ;
739
753
}
740
754
741
755
#[ test]
@@ -747,6 +761,6 @@ mod tests {
747
761
Vector3 :: new ( -0.165055 , 0.94041 , -0.297299 ) ,
748
762
Vector3 :: new ( 0.98324 , 0.180557 , 0.025257 ) ,
749
763
] ) ;
750
- assert ! ( expected. approx_eq ( & b. inverted( ) ) ) ;
764
+ assert ! ( expected. is_equal_approx ( & b. inverted( ) ) ) ;
751
765
}
752
766
}
0 commit comments