forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkMath.h
1643 lines (1452 loc) · 53.4 KB
/
vtkMath.h
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMath.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================
Copyright 2011 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
license for use of this work by or on behalf of the
U.S. Government. Redistribution and use in source and binary forms, with
or without modification, are permitted provided that this Notice and any
statement of authorship are reproduced on all copies.
Contact: pppebay@sandia.gov,dcthomp@sandia.gov
=========================================================================*/
/**
* @class vtkMath
* @brief performs common math operations
*
* vtkMath provides methods to perform common math operations. These
* include providing constants such as Pi; conversion from degrees to
* radians; vector operations such as dot and cross products and vector
* norm; matrix determinant for 2x2 and 3x3 matrices; univariate polynomial
* solvers; and for random number generation (for backward compatibility only).
* @sa
* vtkMinimalStandardRandomSequence, vtkBoxMuellerRandomSequence,
* vtkQuaternion
*/
#ifndef vtkMath_h
#define vtkMath_h
#include "vtkCommonCoreModule.h" // For export macro
#include "vtkObject.h"
#include "vtkTypeTraits.h" // For type traits
#include "vtkSmartPointer.h" // For vtkSmartPointer.
#include "vtkMathConfigure.h" // For <cmath> and VTK_HAS_ISNAN etc.
#include <cassert> // assert() in inline implementations.
#ifndef DBL_MIN
# define VTK_DBL_MIN 2.2250738585072014e-308
#else // DBL_MIN
# define VTK_DBL_MIN DBL_MIN
#endif // DBL_MIN
#ifndef DBL_EPSILON
# define VTK_DBL_EPSILON 2.2204460492503131e-16
#else // DBL_EPSILON
# define VTK_DBL_EPSILON DBL_EPSILON
#endif // DBL_EPSILON
#ifndef VTK_DBL_EPSILON
# ifndef DBL_EPSILON
# define VTK_DBL_EPSILON 2.2204460492503131e-16
# else // DBL_EPSILON
# define VTK_DBL_EPSILON DBL_EPSILON
# endif // DBL_EPSILON
#endif // VTK_DBL_EPSILON
class vtkDataArray;
class vtkPoints;
class vtkMathInternal;
class vtkMinimalStandardRandomSequence;
class vtkBoxMuellerRandomSequence;
namespace vtk_detail
{
// forward declaration
template <typename OutT>
void RoundDoubleToIntegralIfNecessary(double val, OutT* ret);
} // end namespace vtk_detail
class VTKCOMMONCORE_EXPORT vtkMath : public vtkObject
{
public:
static vtkMath *New();
vtkTypeMacro(vtkMath,vtkObject);
void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
/**
* A mathematical constant. This version is atan(1.0) * 4.0
*/
static double Pi() { return 3.141592653589793; };
//@{
/**
* Convert degrees into radians
*/
static float RadiansFromDegrees( float degrees);
static double RadiansFromDegrees( double degrees);
//@}
//@{
/**
* Convert radians into degrees
*/
static float DegreesFromRadians( float radians);
static double DegreesFromRadians( double radians);
//@}
/**
* Rounds a float to the nearest integer.
*/
static int Round(float f) {
return static_cast<int>( f + ( f >= 0.0 ? 0.5 : -0.5 ) ); }
static int Round(double f) {
return static_cast<int>( f + ( f >= 0.0 ? 0.5 : -0.5 ) ); }
/**
* Round a double to type OutT if OutT is integral, otherwise simply clamp
* the value to the output range.
*/
template <typename OutT>
static void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
{
// Can't specialize template methods in a template class, so we move the
// implementations to a external namespace.
vtk_detail::RoundDoubleToIntegralIfNecessary(val, ret);
}
/**
* Rounds a double to the nearest integer not greater than itself.
* This is faster than floor() but provides undefined output on
* overflow.
*/
static int Floor(double x);
/**
* Rounds a double to the nearest integer not less than itself.
* This is faster than ceil() but provides undefined output on
* overflow.
*/
static int Ceil(double x);
/**
* Gives the exponent of the lowest power of two not less than x.
* Or in mathspeak, return the smallest "i" for which 2^i >= x.
* If x is zero, then the return value will be zero.
*/
static int CeilLog2(vtkTypeUInt64 x);
/**
* Returns the minimum of the two arguments provided.
*/
template<class T>
static T Min(const T & a, const T & b);
/**
* Returns the maximum of the two arugments provided.
*/
template<class T>
static T Max(const T & a, const T & b);
/**
* Returns true if integer is a power of two.
*/
static bool IsPowerOfTwo(vtkTypeUInt64 x);
/**
* Compute the nearest power of two that is not less than x.
* The return value is 1 if x is less than or equal to zero,
* and is VTK_INT_MIN if result is too large to fit in an int.
*/
static int NearestPowerOfTwo(int x);
/**
* Compute N factorial, N! = N*(N-1) * (N-2)...*3*2*1.
* 0! is taken to be 1.
*/
static vtkTypeInt64 Factorial( int N );
/**
* The number of combinations of n objects from a pool of m objects (m>n).
* This is commonly known as "m choose n" and sometimes denoted \f$_mC_n\f$
* or \f$\left(\begin{array}{c}m \\ n\end{array}\right)\f$.
*/
static vtkTypeInt64 Binomial( int m, int n );
/**
* Start iterating over "m choose n" objects.
* This function returns an array of n integers, each from 0 to m-1.
* These integers represent the n items chosen from the set [0,m[.
* You are responsible for calling vtkMath::FreeCombination() once the iterator is no longer needed.
* Warning: this gets large very quickly, especially when n nears m/2!
* (Hint: think of Pascal's triangle.)
*/
static int* BeginCombination( int m, int n );
/**
* Given \a m, \a n, and a valid \a combination of \a n integers in
* the range [0,m[, this function alters the integers into the next
* combination in a sequence of all combinations of \a n items from
* a pool of \a m.
* If the \a combination is the last item in the sequence on input,
* then \a combination is unaltered and 0 is returned.
* Otherwise, 1 is returned and \a combination is updated.
*/
static int NextCombination( int m, int n, int* combination );
/**
* Free the "iterator" array created by vtkMath::BeginCombination.
*/
static void FreeCombination( int* combination);
/**
* Initialize seed value. NOTE: Random() has the bad property that
* the first random number returned after RandomSeed() is called
* is proportional to the seed value! To help solve this, call
* RandomSeed() a few times inside seed. This doesn't ruin the
* repeatability of Random().
* DON'T USE Random(), RandomSeed(), GetSeed(), Gaussian()
* THIS IS STATIC SO THIS IS PRONE TO ERRORS (SPECIALLY FOR REGRESSION TESTS)
* THIS IS HERE FOR BACKWARD COMPATIBILITY ONLY.
* Instead, for a sequence of random numbers with a uniform distribution
* create a vtkMinimalStandardRandomSequence object.
* For a sequence of random numbers with a gaussian/normal distribution
* create a vtkBoxMuellerRandomSequence object.
*/
static void RandomSeed(int s);
/**
* Return the current seed used by the random number generator.
* DON'T USE Random(), RandomSeed(), GetSeed(), Gaussian()
* THIS IS STATIC SO THIS IS PRONE TO ERRORS (SPECIALLY FOR REGRESSION TESTS)
* THIS IS HERE FOR BACKWARD COMPATIBILITY ONLY.
* Instead, for a sequence of random numbers with a uniform distribution
* create a vtkMinimalStandardRandomSequence object.
* For a sequence of random numbers with a gaussian/normal distribution
* create a vtkBoxMuellerRandomSequence object.
*/
static int GetSeed();
/**
* Generate pseudo-random numbers distributed according to the uniform
* distribution between 0.0 and 1.0.
* This is used to provide portability across different systems.
* DON'T USE Random(), RandomSeed(), GetSeed(), Gaussian()
* THIS IS STATIC SO THIS IS PRONE TO ERRORS (SPECIALLY FOR REGRESSION TESTS)
* THIS IS HERE FOR BACKWARD COMPATIBILITY ONLY.
* Instead, for a sequence of random numbers with a uniform distribution
* create a vtkMinimalStandardRandomSequence object.
* For a sequence of random numbers with a gaussian/normal distribution
* create a vtkBoxMuellerRandomSequence object.
*/
static double Random();
/**
* Generate pseudo-random numbers distributed according to the uniform
* distribution between \a min and \a max.
* DON'T USE Random(), RandomSeed(), GetSeed(), Gaussian()
* THIS IS STATIC SO THIS IS PRONE TO ERRORS (SPECIALLY FOR REGRESSION TESTS)
* THIS IS HERE FOR BACKWARD COMPATIBILITY ONLY.
* Instead, for a sequence of random numbers with a uniform distribution
* create a vtkMinimalStandardRandomSequence object.
* For a sequence of random numbers with a gaussian/normal distribution
* create a vtkBoxMuellerRandomSequence object.
*/
static double Random( double min, double max );
/**
* Generate pseudo-random numbers distributed according to the standard
* normal distribution.
* DON'T USE Random(), RandomSeed(), GetSeed(), Gaussian()
* THIS IS STATIC SO THIS IS PRONE TO ERRORS (SPECIALLY FOR REGRESSION TESTS)
* THIS IS HERE FOR BACKWARD COMPATIBILITY ONLY.
* Instead, for a sequence of random numbers with a uniform distribution
* create a vtkMinimalStandardRandomSequence object.
* For a sequence of random numbers with a gaussian/normal distribution
* create a vtkBoxMuellerRandomSequence object.
*/
static double Gaussian();
/**
* Generate pseudo-random numbers distributed according to the Gaussian
* distribution with mean \a mean and standard deviation \a std.
* DON'T USE Random(), RandomSeed(), GetSeed(), Gaussian()
* THIS IS STATIC SO THIS IS PRONE TO ERRORS (SPECIALLY FOR REGRESSION TESTS)
* THIS IS HERE FOR BACKWARD COMPATIBILITY ONLY.
* Instead, for a sequence of random numbers with a uniform distribution
* create a vtkMinimalStandardRandomSequence object.
* For a sequence of random numbers with a gaussian/normal distribution
* create a vtkBoxMuellerRandomSequence object.
*/
static double Gaussian( double mean, double std );
/**
* Addition of two 3-vectors (float version). Result is stored in c.
*/
static void Add(const float a[3], const float b[3], float c[3]) {
for (int i = 0; i < 3; ++i)
c[i] = a[i] + b[i];
}
/**
* Addition of two 3-vectors (double version). Result is stored in c.
*/
static void Add(const double a[3], const double b[3], double c[3]) {
for (int i = 0; i < 3; ++i)
c[i] = a[i] + b[i];
}
/**
* Subtraction of two 3-vectors (float version). Result is stored in c according to c = a - b.
*/
static void Subtract(const float a[3], const float b[3], float c[3]) {
for (int i = 0; i < 3; ++i)
c[i] = a[i] - b[i];
}
/**
* Subtraction of two 3-vectors (double version). Result is stored in c according to c = a - b.
*/
static void Subtract(const double a[3], const double b[3], double c[3]) {
for (int i = 0; i < 3; ++i)
c[i] = a[i] - b[i];
}
/**
* Multiplies a 3-vector by a scalar (float version).
* This modifies the input 3-vector.
*/
static void MultiplyScalar(float a[3], float s) {
for (int i = 0; i < 3; ++i)
a[i] *= s;
}
/**
* Multiplies a 2-vector by a scalar (float version).
* This modifies the input 2-vector.
*/
static void MultiplyScalar2D(float a[2], float s) {
for (int i = 0; i < 2; ++i)
a[i] *= s;
}
/**
* Multiplies a 3-vector by a scalar (double version).
* This modifies the input 3-vector.
*/
static void MultiplyScalar(double a[3], double s) {
for (int i = 0; i < 3; ++i)
a[i] *= s;
}
/**
* Multiplies a 2-vector by a scalar (double version).
* This modifies the input 2-vector.
*/
static void MultiplyScalar2D(double a[2], double s) {
for (int i = 0; i < 2; ++i)
a[i] *= s;
}
/**
* Dot product of two 3-vectors (float version).
*/
static float Dot(const float a[3], const float b[3]) {
return ( a[0] * b[0] + a[1] * b[1] + a[2] * b[2] );};
/**
* Dot product of two 3-vectors (double-precision version).
*/
static double Dot(const double a[3], const double b[3]) {
return ( a[0] * b[0] + a[1] * b[1] + a[2] * b[2] );};
/**
* Outer product of two 3-vectors (float version).
*/
static void Outer(const float a[3], const float b[3], float C[3][3]) {
for (int i=0; i < 3; i++)
for (int j=0; j < 3; j++)
C[i][j] = a[i] * b[j];
}
/**
* Outer product of two 3-vectors (double-precision version).
*/
static void Outer(const double a[3], const double b[3], double C[3][3]) {
for (int i=0; i < 3; i++)
for (int j=0; j < 3; j++)
C[i][j] = a[i] * b[j];
}
/**
* Cross product of two 3-vectors. Result (a x b) is stored in c.
*/
static void Cross(const float a[3], const float b[3], float c[3]);
/**
* Cross product of two 3-vectors. Result (a x b) is stored in c.
* (double-precision version)
*/
static void Cross(const double a[3], const double b[3], double c[3]);
//@{
/**
* Compute the norm of n-vector. x is the vector, n is its length.
*/
static float Norm(const float* x, int n);
static double Norm(const double* x, int n);
//@}
/**
* Compute the norm of 3-vector.
*/
static float Norm(const float v[3]) {
return static_cast<float> (sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] ) );};
/**
* Compute the norm of 3-vector (double-precision version).
*/
static double Norm(const double v[3]) {
return sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] );};
/**
* Normalize (in place) a 3-vector. Returns norm of vector.
*/
static float Normalize(float v[3]);
/**
* Normalize (in place) a 3-vector. Returns norm of vector
* (double-precision version).
*/
static double Normalize(double v[3]);
//@{
/**
* Given a unit vector v1, find two unit vectors v2 and v3 such that
* v1 cross v2 = v3 (i.e. the vectors are perpendicular to each other).
* There is an infinite number of such vectors, specify an angle theta
* to choose one set. If you want only one perpendicular vector,
* specify NULL for v3.
*/
static void Perpendiculars(const double v1[3], double v2[3], double v3[3],
double theta);
static void Perpendiculars(const float v1[3], float v2[3], float v3[3],
double theta);
//@}
//@{
/**
* Compute the projection of vector a on vector b and return it in projection[3].
* If b is a zero vector, the function returns false and 'projection' is invalid.
* Otherwise, it returns true.
*/
static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
//@}
//@{
/**
* Compute the projection of 2D vector a on 2D vector b and returns the result
* in projection[2].
* If b is a zero vector, the function returns false and 'projection' is invalid.
* Otherwise, it returns true.
*/
static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
//@}
/**
* Compute distance squared between two points p1 and p2.
*/
static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
/**
* Compute distance squared between two points p1 and p2
* (double precision version).
*/
static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
/**
* Compute angle in radians between two vectors.
*/
static double AngleBetweenVectors(const double v1[3], const double v2[3]);
/**
* Compute the amplitude of a Gaussian function with mean=0 and specified variance.
* That is, 1./(sqrt(2 Pi * variance)) * exp(-distanceFromMean^2/(2.*variance)).
*/
static double GaussianAmplitude(const double variance, const double distanceFromMean);
/**
* Compute the amplitude of a Gaussian function with specified mean and variance.
* That is, 1./(sqrt(2 Pi * variance)) * exp(-(position - mean)^2/(2.*variance)).
*/
static double GaussianAmplitude(const double mean, const double variance, const double position);
/**
* Compute the amplitude of an unnormalized Gaussian function with mean=0 and specified variance.
* That is, exp(-distanceFromMean^2/(2.*variance)). When distanceFromMean = 0, this function
* returns 1.
*/
static double GaussianWeight(const double variance, const double distanceFromMean);
/**
* Compute the amplitude of an unnormalized Gaussian function with specified mean and variance.
* That is, exp(-(position - mean)^2/(2.*variance)). When the distance from 'position' to 'mean'
* is 0, this function returns 1.
*/
static double GaussianWeight(const double mean, const double variance, const double position);
/**
* Dot product of two 2-vectors.
*/
static float Dot2D(const float x[2], const float y[2]) {
return ( x[0] * y[0] + x[1] * y[1] );};
/**
* Dot product of two 2-vectors. (double-precision version).
*/
static double Dot2D(const double x[2], const double y[2]) {
return ( x[0] * y[0] + x[1] * y[1] );};
/**
* Outer product of two 2-vectors (float version).
*/
static void Outer2D(const float x[2], const float y[2], float A[2][2])
{
for (int i=0; i < 2; i++)
{
for (int j=0; j < 2; j++)
{
A[i][j] = x[i] * y[j];
}
}
}
/**
* Outer product of two 2-vectors (float version).
*/
static void Outer2D(const double x[2], const double y[2], double A[2][2])
{
for (int i=0; i < 2; i++)
{
for (int j=0; j < 2; j++)
{
A[i][j] = x[i] * y[j];
}
}
}
/**
* Compute the norm of a 2-vector.
*/
static float Norm2D(const float x[2]) {
return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
/**
* Compute the norm of a 2-vector.
* (double-precision version).
*/
static double Norm2D(const double x[2]) {
return sqrt( x[0] * x[0] + x[1] * x[1] );};
/**
* Normalize (in place) a 2-vector. Returns norm of vector.
*/
static float Normalize2D(float v[2]);
/**
* Normalize (in place) a 2-vector. Returns norm of vector.
* (double-precision version).
*/
static double Normalize2D(double v[2]);
/**
* Compute determinant of 2x2 matrix. Two columns of matrix are input.
*/
static float Determinant2x2(const float c1[2], const float c2[2]) {
return (c1[0] * c2[1] - c2[0] * c1[1] );};
//@{
/**
* Calculate the determinant of a 2x2 matrix: | a b | | c d |
*/
static double Determinant2x2(double a, double b, double c, double d) {
return (a * d - b * c);};
static double Determinant2x2(const double c1[2], const double c2[2]) {
return (c1[0] * c2[1] - c2[0] * c1[1] );};
//@}
//@{
/**
* LU Factorization of a 3x3 matrix.
*/
static void LUFactor3x3(float A[3][3], int index[3]);
static void LUFactor3x3(double A[3][3], int index[3]);
//@}
//@{
/**
* LU back substitution for a 3x3 matrix.
*/
static void LUSolve3x3(const float A[3][3], const int index[3],
float x[3]);
static void LUSolve3x3(const double A[3][3], const int index[3],
double x[3]);
//@}
//@{
/**
* Solve Ay = x for y and place the result in y. The matrix A is
* destroyed in the process.
*/
static void LinearSolve3x3(const float A[3][3], const float x[3],
float y[3]);
static void LinearSolve3x3(const double A[3][3], const double x[3],
double y[3]);
//@}
//@{
/**
* Multiply a vector by a 3x3 matrix. The result is placed in out.
*/
static void Multiply3x3(const float A[3][3], const float in[3],
float out[3]);
static void Multiply3x3(const double A[3][3], const double in[3],
double out[3]);
//@}
//@{
/**
* Multiply one 3x3 matrix by another according to C = AB.
*/
static void Multiply3x3(const float A[3][3], const float B[3][3],
float C[3][3]);
static void Multiply3x3(const double A[3][3], const double B[3][3],
double C[3][3]);
//@}
/**
* General matrix multiplication. You must allocate output storage.
* colA == rowB
* and matrix C is rowA x colB
*/
static void MultiplyMatrix(double **A, double **B,
unsigned int rowA, unsigned int colA,
unsigned int rowB, unsigned int colB,
double **C);
//@{
/**
* Transpose a 3x3 matrix. The input matrix is A. The output
* is stored in AT.
*/
static void Transpose3x3(const float A[3][3], float AT[3][3]);
static void Transpose3x3(const double A[3][3], double AT[3][3]);
//@}
//@{
/**
* Invert a 3x3 matrix. The input matrix is A. The output is
* stored in AI.
*/
static void Invert3x3(const float A[3][3], float AI[3][3]);
static void Invert3x3(const double A[3][3], double AI[3][3]);
//@}
//@{
/**
* Set A to the identity matrix.
*/
static void Identity3x3(float A[3][3]);
static void Identity3x3(double A[3][3]);
//@}
//@{
/**
* Return the determinant of a 3x3 matrix.
*/
static double Determinant3x3(float A[3][3]);
static double Determinant3x3(double A[3][3]);
//@}
/**
* Compute determinant of 3x3 matrix. Three columns of matrix are input.
*/
static float Determinant3x3(const float c1[3],
const float c2[3],
const float c3[3]);
/**
* Compute determinant of 3x3 matrix. Three columns of matrix are input.
*/
static double Determinant3x3(const double c1[3],
const double c2[3],
const double c3[3]);
/**
* Calculate the determinant of a 3x3 matrix in the form:
* | a1, b1, c1 |
* | a2, b2, c2 |
* | a3, b3, c3 |
*/
static double Determinant3x3(double a1, double a2, double a3,
double b1, double b2, double b3,
double c1, double c2, double c3);
//@{
/**
* Convert a quaternion to a 3x3 rotation matrix. The quaternion
* does not have to be normalized beforehand.
* The quaternion must be in the form [w, x, y, z].
* @sa Matrix3x3ToQuaternion() MultiplyQuaternion()
* @sa vtkQuaternion
*/
static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
//@}
//@{
/**
* Convert a 3x3 matrix into a quaternion. This will provide the
* best possible answer even if the matrix is not a pure rotation matrix.
* The quaternion is in the form [w, x, y, z].
* The method used is that of B.K.P. Horn.
* @sa QuaternionToMatrix3x3() MultiplyQuaternion()
* @sa vtkQuaternion
*/
static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
//@}
//@{
/**
* Multiply two quaternions. This is used to concatenate rotations.
* Quaternions are in the form [w, x, y, z].
* @sa Matrix3x3ToQuaternion() QuaternionToMatrix3x3()
* @sa vtkQuaternion
*/
static void MultiplyQuaternion( const float q1[4], const float q2[4], float q[4] );
static void MultiplyQuaternion( const double q1[4], const double q2[4], double q[4] );
//@}
//@{
/**
* rotate a vector by a normalized quaternion
* using // https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
*/
static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3]);
static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3]);
//@}
//@{
/**
* rotate a vector by WXYZ
* using // https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
*/
static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3]);
static void RotateVectorByWXYZ(const double v[3], const double q[4], double r[3]);
//@}
//@{
/**
* Orthogonalize a 3x3 matrix and put the result in B. If matrix A
* has a negative determinant, then B will be a rotation plus a flip
* i.e. it will have a determinant of -1.
*/
static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
//@}
//@{
/**
* Diagonalize a symmetric 3x3 matrix and return the eigenvalues in
* w and the eigenvectors in the columns of V. The matrix V will
* have a positive determinant, and the three eigenvectors will be
* aligned as closely as possible with the x, y, and z axes.
*/
static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
//@}
//@{
/**
* Perform singular value decomposition on a 3x3 matrix. This is not
* done using a conventional SVD algorithm, instead it is done using
* Orthogonalize3x3 and Diagonalize3x3. Both output matrices U and VT
* will have positive determinants, and the w values will be arranged
* such that the three rows of VT are aligned as closely as possible
* with the x, y, and z axes respectively. If the determinant of A is
* negative, then the three w values will be negative.
*/
static void SingularValueDecomposition3x3(const float A[3][3],
float U[3][3], float w[3],
float VT[3][3]);
static void SingularValueDecomposition3x3(const double A[3][3],
double U[3][3], double w[3],
double VT[3][3]);
//@}
/**
* Solve linear equations Ax = b using Crout's method. Input is square
* matrix A and load vector x. Solution x is written over load vector. The
* dimension of the matrix is specified in size. If error is found, method
* returns a 0.
*/
static int SolveLinearSystem(double **A, double *x, int size);
/**
* Invert input square matrix A into matrix AI.
* Note that A is modified during
* the inversion. The size variable is the dimension of the matrix. Returns 0
* if inverse not computed.
*/
static int InvertMatrix(double **A, double **AI, int size);
/**
* Thread safe version of InvertMatrix method.
* Working memory arrays tmp1SIze and tmp2Size
* of length size must be passed in.
*/
static int InvertMatrix(double **A, double **AI, int size,
int *tmp1Size, double *tmp2Size);
/**
* Factor linear equations Ax = b using LU decomposition into the form
* A = LU where L is a unit lower triangular matrix and U is upper triangular
* matrix.
* The input is a square matrix A, an integer array of pivot indices index[0->n-1],
* and the size, n, of the square matrix.
* The output is provided by overwriting the input A with a matrix of the same size as
* A containing all of the information about L and U. If the output matrix is
* \f$ A* = \left( \begin{array}{cc}
* a & b \\ c & d \end{array} \right)\f$
* then L and U can be obtained as:
* \f$ L = \left( \begin{array}{cc}
* 1 & 0 \\ c & 1 \end{array} \right)\f$
* \f$ U = \left( \begin{array}{cc}
* a & b \\ 0 & d \end{array} \right)\f$
* That is, the diagonal of the resulting A* is the diagonal of U. The upper right
* triangle of A is the upper right triangle of U. The lower left triangle of A is
* the lower left triangle of L (and since L is unit lower triangular, the diagonal
* of L is all 1's).
* If an error is found, the function returns 0.
*/
static int LUFactorLinearSystem(double **A, int *index, int size);
/**
* Thread safe version of LUFactorLinearSystem method.
* Working memory array tmpSize of length size
* must be passed in.
*/
static int LUFactorLinearSystem(double **A, int *index, int size,
double *tmpSize);
/**
* Solve linear equations Ax = b using LU decomposition A = LU where L is
* lower triangular matrix and U is upper triangular matrix. Input is
* factored matrix A=LU, integer array of pivot indices index[0->n-1],
* load vector x[0->n-1], and size of square matrix n. Note that A=LU and
* index[] are generated from method LUFactorLinearSystem). Also, solution
* vector is written directly over input load vector.
*/
static void LUSolveLinearSystem(double **A, int *index,
double *x, int size);
/**
* Estimate the condition number of a LU factored matrix. Used to judge the
* accuracy of the solution. The matrix A must have been previously factored
* using the method LUFactorLinearSystem. The condition number is the ratio
* of the infinity matrix norm (i.e., maximum value of matrix component)
* divided by the minimum diagonal value. (This works for triangular matrices
* only: see Conte and de Boor, Elementary Numerical Analysis.)
*/
static double EstimateMatrixCondition(double **A, int size);
//@{
/**
* Jacobi iteration for the solution of eigenvectors/eigenvalues of a 3x3
* real symmetric matrix. Square 3x3 matrix a; output eigenvalues in w;
* and output eigenvectors in v arranged column-wise. Resulting
* eigenvalues/vectors are sorted in decreasing order; the most positive
* eigenvectors are selected for consistency; eigenvectors are normalized.
* NOTE: the input matrix a is modified during the solution
*/
static int Jacobi(float **a, float *w, float **v);
static int Jacobi(double **a, double *w, double **v);
//@}
//@{
/**
* JacobiN iteration for the solution of eigenvectors/eigenvalues of a nxn
* real symmetric matrix. Square nxn matrix a; size of matrix in n; output
* eigenvalues in w; and output eigenvectors in v arranged column-wise.
* Resulting eigenvalues/vectors are sorted in decreasing order; the most
* positive eigenvectors are selected for consistency; and eigenvectors are
* normalized. w and v need to be allocated previously.
* NOTE: the input matrix a is modified during the solution
*/
static int JacobiN(float **a, int n, float *w, float **v);
static int JacobiN(double **a, int n, double *w, double **v);
//@}
/**
* Solves for the least squares best fit matrix for the homogeneous equation X'M' = 0'.
* Uses the method described on pages 40-41 of Computer Vision by
* Forsyth and Ponce, which is that the solution is the eigenvector
* associated with the minimum eigenvalue of T(X)X, where T(X) is the
* transpose of X.
* The inputs and output are transposed matrices.
* Dimensions: X' is numberOfSamples by xOrder,
* M' dimension is xOrder by yOrder.
* M' should be pre-allocated. All matrices are row major. The resultant
* matrix M' should be pre-multiplied to X' to get 0', or transposed and
* then post multiplied to X to get 0
*/
static int SolveHomogeneousLeastSquares(int numberOfSamples, double **xt, int xOrder,
double **mt);
/**
* Solves for the least squares best fit matrix for the equation X'M' = Y'.
* Uses pseudoinverse to get the ordinary least squares.
* The inputs and output are transposed matrices.
* Dimensions: X' is numberOfSamples by xOrder,
* Y' is numberOfSamples by yOrder,
* M' dimension is xOrder by yOrder.
* M' should be pre-allocated. All matrices are row major. The resultant
* matrix M' should be pre-multiplied to X' to get Y', or transposed and
* then post multiplied to X to get Y
* By default, this method checks for the homogeneous condition where Y==0, and
* if so, invokes SolveHomogeneousLeastSquares. For better performance when
* the system is known not to be homogeneous, invoke with checkHomogeneous=0.
*/
static int SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
double **yt, int yOrder, double **mt, int checkHomogeneous=1);
//@{
/**
* Convert color in RGB format (Red, Green, Blue) to HSV format
* (Hue, Saturation, Value). The input color is not modified.
* The input RGB must be float values in the range [0,1].
* The output ranges are hue [0, 1], saturation [0, 1], and
* value [0, 1].
*/
static void RGBToHSV(const float rgb[3], float hsv[3])
{ RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v);
static double* RGBToHSV(const double rgb[3]);
static double* RGBToHSV(double r, double g, double b);
static void RGBToHSV(const double rgb[3], double hsv[3])
{ RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
static void RGBToHSV(double r, double g, double b, double *h, double *s, double *v);
//@}
//@{
/**
* Convert color in HSV format (Hue, Saturation, Value) to RGB
* format (Red, Green, Blue). The input color is not modified.
* The input 'hsv' must be float values in the range [0,1].
* The elements of each component of the output 'rgb' are in
* the range [0, 1].
*/
static void HSVToRGB(const float hsv[3], float rgb[3])
{ HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b);
static double* HSVToRGB(const double hsv[3]);
static double* HSVToRGB(double h, double s, double v);
static void HSVToRGB(const double hsv[3], double rgb[3])
{ HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
static void HSVToRGB(double h, double s, double v, double *r, double *g, double *b);
//@}
//@{
/**
* Convert color from the CIE-L*ab system to CIE XYZ.
*/
static void LabToXYZ(const double lab[3], double xyz[3]) {
LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
}
static void LabToXYZ(double L, double a, double b,
double *x, double *y, double *z);
static double *LabToXYZ(const double lab[3]);
//@}
//@{
/**
* Convert Color from the CIE XYZ system to CIE-L*ab.
*/
static void XYZToLab(const double xyz[3], double lab[3]) {