1
1
import seedrandom from 'seedrandom' ;
2
2
import macro from 'vtk.js/Sources/macros' ;
3
+ import {
4
+ IDENTITY ,
5
+ IDENTITY_3X3 ,
6
+ EPSILON ,
7
+ VTK_SMALL_NUMBER ,
8
+ } from 'vtk.js/Sources/Common/Core/Math/Constants' ;
3
9
4
10
const { vtkErrorMacro, vtkWarningMacro } = macro ;
5
11
@@ -11,7 +17,6 @@ const { vtkErrorMacro, vtkWarningMacro } = macro;
11
17
// ----------------------------------------------------------------------------
12
18
let randomSeedValue = 0 ;
13
19
const VTK_MAX_ROTATIONS = 20 ;
14
- const VTK_SMALL_NUMBER = 1.0e-12 ;
15
20
16
21
function notImplemented ( method ) {
17
22
return ( ) => vtkErrorMacro ( `vtkMath::${ method } - NOT IMPLEMENTED` ) ;
@@ -43,7 +48,11 @@ function swapColumnsMatrix_nxn(matrix, n, column1, column2) {
43
48
44
49
export function createArray ( size = 3 ) {
45
50
// faster than Array.from and/or while loop
46
- return Array ( size ) . fill ( 0 ) ;
51
+ const res = Array ( size ) ;
52
+ for ( let i = 0 ; i < size ; ++ i ) {
53
+ res [ i ] = 0 ;
54
+ }
55
+ return res ;
47
56
}
48
57
49
58
export const Pi = ( ) => Math . PI ;
@@ -697,6 +706,25 @@ export function determinant3x3(mat_3x3) {
697
706
) ;
698
707
}
699
708
709
+ /**
710
+ * Returns true if elements of both arrays are equals.
711
+ * @param {Array } a an array of numbers (vector, point, matrix...)
712
+ * @param {Array } b an array of numbers (vector, point, matrix...)
713
+ * @param {Number } eps tolerance
714
+ */
715
+ export function areEquals ( a , b , eps = EPSILON ) {
716
+ if ( a . length !== b . length ) {
717
+ return false ;
718
+ }
719
+
720
+ function isEqual ( element , index ) {
721
+ return Math . abs ( element - b [ index ] ) <= eps ;
722
+ }
723
+ return a . every ( isEqual ) ;
724
+ }
725
+
726
+ export const areMatricesEqual = areEquals ;
727
+
700
728
export function identity3x3 ( mat_3x3 ) {
701
729
for ( let i = 0 ; i < 3 ; i ++ ) {
702
730
/* eslint-disable-next-line no-multi-assign */
@@ -715,6 +743,14 @@ export function identity(n, mat) {
715
743
return mat ;
716
744
}
717
745
746
+ export function isIdentity ( mat , eps = EPSILON ) {
747
+ return areMatricesEqual ( mat , IDENTITY , eps ) ;
748
+ }
749
+
750
+ export function isIdentity3x3 ( mat , eps = EPSILON ) {
751
+ return areMatricesEqual ( mat , IDENTITY_3X3 , eps ) ;
752
+ }
753
+
718
754
export function quaternionToMatrix3x3 ( quat_4 , mat_3x3 ) {
719
755
const ww = quat_4 [ 0 ] * quat_4 [ 0 ] ;
720
756
const wx = quat_4 [ 0 ] * quat_4 [ 1 ] ;
@@ -748,25 +784,6 @@ export function quaternionToMatrix3x3(quat_4, mat_3x3) {
748
784
mat_3x3 [ 8 ] = zz * f + s ;
749
785
}
750
786
751
- /**
752
- * Returns true if elements of both arrays are equals.
753
- * @param {Array } a an array of numbers (vector, point, matrix...)
754
- * @param {Array } b an array of numbers (vector, point, matrix...)
755
- * @param {Number } eps tolerance
756
- */
757
- export function areEquals ( a , b , eps = 1e-6 ) {
758
- if ( a . length !== b . length ) {
759
- return false ;
760
- }
761
-
762
- function isEqual ( element , index ) {
763
- return Math . abs ( element - b [ index ] ) <= eps ;
764
- }
765
- return a . every ( isEqual ) ;
766
- }
767
-
768
- export const areMatricesEqual = areEquals ;
769
-
770
787
export function roundNumber ( num , digits = 0 ) {
771
788
if ( ! `${ num } ` . includes ( 'e' ) ) {
772
789
return + `${ Math . round ( `${ num } e+${ digits } ` ) } e-${ digits } ` ;
@@ -1425,6 +1442,7 @@ export function solveLinearSystem(A, x, size) {
1425
1442
return 1 ;
1426
1443
}
1427
1444
1445
+ // Note that A is modified during the inversion !
1428
1446
export function invertMatrix ( A , AI , size , index = null , column = null ) {
1429
1447
const tmp1Size = index || createArray ( size ) ;
1430
1448
const tmp2Size = column || createArray ( size ) ;
@@ -1433,7 +1451,7 @@ export function invertMatrix(A, AI, size, index = null, column = null) {
1433
1451
// Note: tmp1Size returned value is used later, tmp2Size is just working
1434
1452
// memory whose values are not used in LUSolveLinearSystem
1435
1453
if ( luFactorLinearSystem ( A , tmp1Size , size , tmp2Size ) === 0 ) {
1436
- return 0 ;
1454
+ return null ;
1437
1455
}
1438
1456
1439
1457
for ( let j = 0 ; j < size ; j ++ ) {
@@ -1449,7 +1467,7 @@ export function invertMatrix(A, AI, size, index = null, column = null) {
1449
1467
}
1450
1468
}
1451
1469
1452
- return 1 ;
1470
+ return AI ;
1453
1471
}
1454
1472
1455
1473
export function estimateMatrixCondition ( A , size ) {
@@ -2210,6 +2228,8 @@ export default {
2210
2228
invert3x3,
2211
2229
identity3x3,
2212
2230
identity,
2231
+ isIdentity,
2232
+ isIdentity3x3,
2213
2233
determinant3x3,
2214
2234
quaternionToMatrix3x3,
2215
2235
areEquals,
0 commit comments