VTK  9.2.6
vtkMath.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkMath.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================
15 Copyright 2011 Sandia Corporation.
16 Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
17 license for use of this work by or on behalf of the
18 U.S. Government. Redistribution and use in source and binary forms, with
19 or without modification, are permitted provided that this Notice and any
20 statement of authorship are reproduced on all copies.
21
22 Contact: pppebay@sandia.gov,dcthomp@sandia.gov
23
24=========================================================================*/
54#ifndef vtkMath_h
55#define vtkMath_h
56
57#include "vtkCommonCoreModule.h" // For export macro
58#include "vtkMathPrivate.hxx" // For Matrix meta-class helpers
59#include "vtkMatrixUtilities.h" // For Matrix wrapping / mapping
60#include "vtkObject.h"
61#include "vtkSmartPointer.h" // For vtkSmartPointer.
62#include "vtkTypeTraits.h" // For type traits
63
64#include "vtkMathConfigure.h" // For <cmath> and VTK_HAS_ISNAN etc.
65
66#include <algorithm> // for std::clamp
67#include <cassert> // assert() in inline implementations.
68
69#ifndef DBL_MIN
70#define VTK_DBL_MIN 2.2250738585072014e-308
71#else // DBL_MIN
72#define VTK_DBL_MIN DBL_MIN
73#endif // DBL_MIN
74
75#ifndef DBL_EPSILON
76#define VTK_DBL_EPSILON 2.2204460492503131e-16
77#else // DBL_EPSILON
78#define VTK_DBL_EPSILON DBL_EPSILON
79#endif // DBL_EPSILON
80
81#ifndef VTK_DBL_EPSILON
82#ifndef DBL_EPSILON
83#define VTK_DBL_EPSILON 2.2204460492503131e-16
84#else // DBL_EPSILON
85#define VTK_DBL_EPSILON DBL_EPSILON
86#endif // DBL_EPSILON
87#endif // VTK_DBL_EPSILON
88
89class vtkDataArray;
90class vtkPoints;
91class vtkMathInternal;
94
95namespace vtk_detail
96{
97// forward declaration
98template <typename OutT>
99void RoundDoubleToIntegralIfNecessary(double val, OutT* ret);
100} // end namespace vtk_detail
101
102class VTKCOMMONCORE_EXPORT vtkMath : public vtkObject
103{
104public:
105 static vtkMath* New();
106 vtkTypeMacro(vtkMath, vtkObject);
107 void PrintSelf(ostream& os, vtkIndent indent) override;
108
112 static constexpr double Pi() { return 3.141592653589793; }
113
115
118 static float RadiansFromDegrees(float degrees);
119 static double RadiansFromDegrees(double degrees);
121
123
126 static float DegreesFromRadians(float radians);
127 static double DegreesFromRadians(double radians);
129
133#if 1
134 static int Round(float f) { return static_cast<int>(f + (f >= 0.0 ? 0.5 : -0.5)); }
135 static int Round(double f) { return static_cast<int>(f + (f >= 0.0 ? 0.5 : -0.5)); }
136#endif
137
142 template <typename OutT>
143 static void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
144 {
145 // Can't specialize template methods in a template class, so we move the
146 // implementations to a external namespace.
148 }
149
155 static int Floor(double x);
156
162 static int Ceil(double x);
163
169 static int CeilLog2(vtkTypeUInt64 x);
170
175 template <class T>
176 static T Min(const T& a, const T& b);
177
182 template <class T>
183 static T Max(const T& a, const T& b);
184
188 static bool IsPowerOfTwo(vtkTypeUInt64 x);
189
195 static int NearestPowerOfTwo(int x);
196
201 static vtkTypeInt64 Factorial(int N);
202
208 static vtkTypeInt64 Binomial(int m, int n);
209
221 static int* BeginCombination(int m, int n);
222
233 static int NextCombination(int m, int n, int* combination);
234
238 static void FreeCombination(int* combination);
239
255 static void RandomSeed(int s);
256
268 static int GetSeed();
269
283 static double Random();
284
297 static double Random(double min, double max);
298
311 static double Gaussian();
312
325 static double Gaussian(double mean, double std);
326
331 template <class VectorT1, class VectorT2>
332 static void Assign(const VectorT1& a, VectorT2&& b)
333 {
334 b[0] = a[0];
335 b[1] = a[1];
336 b[2] = a[2];
337 }
338
342 static void Assign(const double a[3], double b[3]) { vtkMath::Assign<>(a, b); }
343
347 static void Add(const float a[3], const float b[3], float c[3])
348 {
349 for (int i = 0; i < 3; ++i)
350 {
351 c[i] = a[i] + b[i];
352 }
353 }
354
358 static void Add(const double a[3], const double b[3], double c[3])
359 {
360 for (int i = 0; i < 3; ++i)
361 {
362 c[i] = a[i] + b[i];
363 }
364 }
365
369 static void Subtract(const float a[3], const float b[3], float c[3])
370 {
371 for (int i = 0; i < 3; ++i)
372 {
373 c[i] = a[i] - b[i];
374 }
375 }
376
380 static void Subtract(const double a[3], const double b[3], double c[3])
381 {
382 for (int i = 0; i < 3; ++i)
383 {
384 c[i] = a[i] - b[i];
385 }
386 }
387
393 template <class VectorT1, class VectorT2, class VectorT3>
394 static void Subtract(const VectorT1& a, const VectorT2& b, VectorT3&& c)
395 {
396 c[0] = a[0] - b[0];
397 c[1] = a[1] - b[1];
398 c[2] = a[2] - b[2];
399 }
400
405 static void MultiplyScalar(float a[3], float s)
406 {
407 for (int i = 0; i < 3; ++i)
408 {
409 a[i] *= s;
410 }
411 }
412
417 static void MultiplyScalar2D(float a[2], float s)
418 {
419 for (int i = 0; i < 2; ++i)
420 {
421 a[i] *= s;
422 }
423 }
424
429 static void MultiplyScalar(double a[3], double s)
430 {
431 for (int i = 0; i < 3; ++i)
432 {
433 a[i] *= s;
434 }
435 }
436
441 static void MultiplyScalar2D(double a[2], double s)
442 {
443 for (int i = 0; i < 2; ++i)
444 {
445 a[i] *= s;
446 }
447 }
448
452 static float Dot(const float a[3], const float b[3])
453 {
454 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
455 }
456
460 static double Dot(const double a[3], const double b[3])
461 {
462 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
463 }
464
480 template <typename ReturnTypeT = double, typename TupleRangeT1, typename TupleRangeT2,
481 typename EnableT = typename std::conditional<!std::is_pointer<TupleRangeT1>::value &&
482 !std::is_array<TupleRangeT1>::value,
483 TupleRangeT1, TupleRangeT2>::type::value_type>
484 static ReturnTypeT Dot(const TupleRangeT1& a, const TupleRangeT2& b)
485 {
486 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
487 }
488
492 static void Outer(const float a[3], const float b[3], float c[3][3])
493 {
494 for (int i = 0; i < 3; ++i)
495 {
496 for (int j = 0; j < 3; ++j)
497 {
498 c[i][j] = a[i] * b[j];
499 }
500 }
501 }
502
506 static void Outer(const double a[3], const double b[3], double c[3][3])
507 {
508 for (int i = 0; i < 3; ++i)
509 {
510 for (int j = 0; j < 3; ++j)
511 {
512 c[i][j] = a[i] * b[j];
513 }
514 }
515 }
516
521 static void Cross(const float a[3], const float b[3], float c[3]);
522
527 static void Cross(const double a[3], const double b[3], double c[3]);
528
530
533 static float Norm(const float* x, int n);
534 static double Norm(const double* x, int n);
536
540 static float Norm(const float v[3]) { return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); }
541
545 static double Norm(const double v[3])
546 {
547 return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
548 }
549
559 template <typename ReturnTypeT = double, typename TupleRangeT>
560 static ReturnTypeT SquaredNorm(const TupleRangeT& v)
561 {
562 return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
563 }
564
569 static float Normalize(float v[3]);
570
575 static double Normalize(double v[3]);
576
578
585 static void Perpendiculars(const double v1[3], double v2[3], double v3[3], double theta);
586 static void Perpendiculars(const float v1[3], float v2[3], float v3[3], double theta);
588
590
595 static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
596 static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
598
600
606 static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
607 static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
609
625 template <typename ReturnTypeT = double, typename TupleRangeT1, typename TupleRangeT2,
626 typename EnableT = typename std::conditional<!std::is_pointer<TupleRangeT1>::value &&
627 !std::is_array<TupleRangeT1>::value,
628 TupleRangeT1, TupleRangeT2>::type::value_type>
629 static ReturnTypeT Distance2BetweenPoints(const TupleRangeT1& p1, const TupleRangeT2& p2);
630
635 static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
636
641 static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
642
646 static double AngleBetweenVectors(const double v1[3], const double v2[3]);
647
652 const double v1[3], const double v2[3], const double vn[3]);
653
658 static double GaussianAmplitude(const double variance, const double distanceFromMean);
659
664 static double GaussianAmplitude(const double mean, const double variance, const double position);
665
671 static double GaussianWeight(const double variance, const double distanceFromMean);
672
678 static double GaussianWeight(const double mean, const double variance, const double position);
679
683 static float Dot2D(const float x[2], const float y[2]) { return x[0] * y[0] + x[1] * y[1]; }
684
688 static double Dot2D(const double x[2], const double y[2]) { return x[0] * y[0] + x[1] * y[1]; }
689
693 static void Outer2D(const float x[2], const float y[2], float A[2][2])
694 {
695 for (int i = 0; i < 2; ++i)
696 {
697 for (int j = 0; j < 2; ++j)
698 {
699 A[i][j] = x[i] * y[j];
700 }
701 }
702 }
703
707 static void Outer2D(const double x[2], const double y[2], double A[2][2])
708 {
709 for (int i = 0; i < 2; ++i)
710 {
711 for (int j = 0; j < 2; ++j)
712 {
713 A[i][j] = x[i] * y[j];
714 }
715 }
716 }
717
722 static float Norm2D(const float x[2]) { return std::sqrt(x[0] * x[0] + x[1] * x[1]); }
723
728 static double Norm2D(const double x[2]) { return std::sqrt(x[0] * x[0] + x[1] * x[1]); }
729
734 static float Normalize2D(float v[2]);
735
740 static double Normalize2D(double v[2]);
741
745 static float Determinant2x2(const float c1[2], const float c2[2])
746 {
747 return c1[0] * c2[1] - c2[0] * c1[1];
748 }
749
751
754 static double Determinant2x2(double a, double b, double c, double d) { return a * d - b * c; }
755 static double Determinant2x2(const double c1[2], const double c2[2])
756 {
757 return c1[0] * c2[1] - c2[0] * c1[1];
758 }
760
762
765 static void LUFactor3x3(float A[3][3], int index[3]);
766 static void LUFactor3x3(double A[3][3], int index[3]);
768
770
773 static void LUSolve3x3(const float A[3][3], const int index[3], float x[3]);
774 static void LUSolve3x3(const double A[3][3], const int index[3], double x[3]);
776
778
782 static void LinearSolve3x3(const float A[3][3], const float x[3], float y[3]);
783 static void LinearSolve3x3(const double A[3][3], const double x[3], double y[3]);
785
787
790 static void Multiply3x3(const float A[3][3], const float v[3], float u[3]);
791 static void Multiply3x3(const double A[3][3], const double v[3], double u[3]);
793
795
798 static void Multiply3x3(const float A[3][3], const float B[3][3], float C[3][3]);
799 static void Multiply3x3(const double A[3][3], const double B[3][3], double C[3][3]);
801
825 template <int RowsT, int MidDimT, int ColsT,
826 class LayoutT1 = vtkMatrixUtilities::Layout::Identity,
827 class LayoutT2 = vtkMatrixUtilities::Layout::Identity, class MatrixT1, class MatrixT2,
828 class MatrixT3>
829 static void MultiplyMatrix(const MatrixT1& M1, const MatrixT2& M2, MatrixT3&& M3)
830 {
831 vtkMathPrivate::MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2>::Compute(M1, M2, M3);
832 }
833
854 template <int RowsT, int ColsT, class LayoutT = vtkMatrixUtilities::Layout::Identity,
855 class MatrixT, class VectorT1, class VectorT2>
856 static void MultiplyMatrixWithVector(const MatrixT& M, const VectorT1& X, VectorT2&& Y)
857 {
858 vtkMathPrivate::MultiplyMatrix<RowsT, ColsT, 1, LayoutT>::Compute(M, X, Y);
859 }
860
866 template <class ScalarT, int SizeT, class VectorT1, class VectorT2>
867 static ScalarT Dot(const VectorT1& x, const VectorT2& y)
868 {
869 return vtkMathPrivate::ContractRowWithCol<ScalarT, 1, SizeT, 1, 0, 0,
870 vtkMatrixUtilities::Layout::Identity, vtkMatrixUtilities::Layout::Transpose>::Compute(x, y);
871 }
872
889 template <int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity, class MatrixT>
891 const MatrixT& M)
892 {
893 return vtkMathPrivate::Determinant<SizeT, LayoutT>::Compute(M);
894 }
895
911 template <int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity, class MatrixT1,
912 class MatrixT2>
913 static void InvertMatrix(const MatrixT1& M1, MatrixT2&& M2)
914 {
915 vtkMathPrivate::InvertMatrix<SizeT, LayoutT>::Compute(M1, M2);
916 }
917
931 template <int RowsT, int ColsT, class LayoutT = vtkMatrixUtilities::Layout::Identity,
932 class MatrixT, class VectorT1, class VectorT2>
933 static void LinearSolve(const MatrixT& M, const VectorT1& x, VectorT2& y)
934 {
935 vtkMathPrivate::LinearSolve<RowsT, ColsT, LayoutT>::Compute(M, x, y);
936 }
937
952 template <class ScalarT, int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity,
953 class VectorT1, class MatrixT, class VectorT2>
954 static ScalarT Dot(const VectorT1& x, const MatrixT& M, const VectorT2& y)
955 {
956 ScalarT tmp[SizeT];
957 vtkMathPrivate::MultiplyMatrix<SizeT, SizeT, 1, LayoutT>::Compute(M, y, tmp);
958 return vtkMathPrivate::ContractRowWithCol<ScalarT, 1, SizeT, 1, 0, 0,
959 vtkMatrixUtilities::Layout::Identity, vtkMatrixUtilities::Layout::Transpose>::Compute(x, tmp);
960 }
961
967 static void MultiplyMatrix(const double* const* A, const double* const* B, unsigned int rowA,
968 unsigned int colA, unsigned int rowB, unsigned int colB, double** C);
969
971
975 static void Transpose3x3(const float A[3][3], float AT[3][3]);
976 static void Transpose3x3(const double A[3][3], double AT[3][3]);
978
980
984 static void Invert3x3(const float A[3][3], float AI[3][3]);
985 static void Invert3x3(const double A[3][3], double AI[3][3]);
987
989
992 static void Identity3x3(float A[3][3]);
993 static void Identity3x3(double A[3][3]);
995
997
1000 static double Determinant3x3(const float A[3][3]);
1001 static double Determinant3x3(const double A[3][3]);
1003
1007 static float Determinant3x3(const float c1[3], const float c2[3], const float c3[3]);
1008
1012 static double Determinant3x3(const double c1[3], const double c2[3], const double c3[3]);
1013
1020 static double Determinant3x3(double a1, double a2, double a3, double b1, double b2, double b3,
1021 double c1, double c2, double c3);
1022
1024
1031 static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
1032 static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
1033 template <class QuaternionT, class MatrixT,
1034 class EnableT = typename std::enable_if<!vtkMatrixUtilities::MatrixIs2DArray<MatrixT>()>::type>
1035 static void QuaternionToMatrix3x3(const QuaternionT& q, MatrixT&& A);
1037
1039
1048 static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
1049 static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
1050 template <class MatrixT, class QuaternionT,
1051 class EnableT = typename std::enable_if<!vtkMatrixUtilities::MatrixIs2DArray<MatrixT>()>::type>
1052 static void Matrix3x3ToQuaternion(const MatrixT& A, QuaternionT&& q);
1054
1056
1062 static void MultiplyQuaternion(const float q1[4], const float q2[4], float q[4]);
1063 static void MultiplyQuaternion(const double q1[4], const double q2[4], double q[4]);
1065
1067
1071 static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3]);
1072 static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3]);
1074
1076
1080 static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3]);
1081 static void RotateVectorByWXYZ(const double v[3], const double q[4], double r[3]);
1083
1085
1090 static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
1091 static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
1093
1095
1101 static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
1102 static void Diagonalize3x3(const double A[3][3], double w[3], double V[3][3]);
1104
1106
1116 const float A[3][3], float U[3][3], float w[3], float VT[3][3]);
1118 const double A[3][3], double U[3][3], double w[3], double VT[3][3]);
1120
1129 double a00, double a01, double a10, double a11, double b0, double b1, double& x0, double& x1);
1130
1139 static vtkTypeBool SolveLinearSystem(double** A, double* x, int size);
1140
1147 static vtkTypeBool InvertMatrix(double** A, double** AI, int size);
1148
1155 double** A, double** AI, int size, int* tmp1Size, double* tmp2Size);
1156
1179 static vtkTypeBool LUFactorLinearSystem(double** A, int* index, int size);
1180
1186 static vtkTypeBool LUFactorLinearSystem(double** A, int* index, int size, double* tmpSize);
1187
1196 static void LUSolveLinearSystem(double** A, int* index, double* x, int size);
1197
1206 static double EstimateMatrixCondition(const double* const* A, int size);
1207
1209
1217 static vtkTypeBool Jacobi(float** a, float* w, float** v);
1218 static vtkTypeBool Jacobi(double** a, double* w, double** v);
1220
1222
1231 static vtkTypeBool JacobiN(float** a, int n, float* w, float** v);
1232 static vtkTypeBool JacobiN(double** a, int n, double* w, double** v);
1234
1249 int numberOfSamples, double** xt, int xOrder, double** mt);
1250
1265 static vtkTypeBool SolveLeastSquares(int numberOfSamples, double** xt, int xOrder, double** yt,
1266 int yOrder, double** mt, int checkHomogeneous = 1);
1267
1269
1276 static void RGBToHSV(const float rgb[3], float hsv[3])
1277 {
1278 RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
1279 }
1280 static void RGBToHSV(float r, float g, float b, float* h, float* s, float* v);
1281 static void RGBToHSV(const double rgb[3], double hsv[3])
1282 {
1283 RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
1284 }
1285 static void RGBToHSV(double r, double g, double b, double* h, double* s, double* v);
1287
1289
1296 static void HSVToRGB(const float hsv[3], float rgb[3])
1297 {
1298 HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
1299 }
1300 static void HSVToRGB(float h, float s, float v, float* r, float* g, float* b);
1301 static void HSVToRGB(const double hsv[3], double rgb[3])
1302 {
1303 HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
1304 }
1305 static void HSVToRGB(double h, double s, double v, double* r, double* g, double* b);
1307
1309
1312 static void LabToXYZ(const double lab[3], double xyz[3])
1313 {
1314 LabToXYZ(lab[0], lab[1], lab[2], xyz + 0, xyz + 1, xyz + 2);
1315 }
1316 static void LabToXYZ(double L, double a, double b, double* x, double* y, double* z);
1318
1320
1323 static void XYZToLab(const double xyz[3], double lab[3])
1324 {
1325 XYZToLab(xyz[0], xyz[1], xyz[2], lab + 0, lab + 1, lab + 2);
1326 }
1327 static void XYZToLab(double x, double y, double z, double* L, double* a, double* b);
1329
1331
1334 static void XYZToRGB(const double xyz[3], double rgb[3])
1335 {
1336 XYZToRGB(xyz[0], xyz[1], xyz[2], rgb + 0, rgb + 1, rgb + 2);
1337 }
1338 static void XYZToRGB(double x, double y, double z, double* r, double* g, double* b);
1340
1342
1345 static void RGBToXYZ(const double rgb[3], double xyz[3])
1346 {
1347 RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz + 0, xyz + 1, xyz + 2);
1348 }
1349 static void RGBToXYZ(double r, double g, double b, double* x, double* y, double* z);
1351
1353
1359 static void RGBToLab(const double rgb[3], double lab[3])
1360 {
1361 RGBToLab(rgb[0], rgb[1], rgb[2], lab + 0, lab + 1, lab + 2);
1362 }
1363 static void RGBToLab(double red, double green, double blue, double* L, double* a, double* b);
1365
1367
1370 static void LabToRGB(const double lab[3], double rgb[3])
1371 {
1372 LabToRGB(lab[0], lab[1], lab[2], rgb + 0, rgb + 1, rgb + 2);
1373 }
1374 static void LabToRGB(double L, double a, double b, double* red, double* green, double* blue);
1376
1378
1381 static void UninitializeBounds(double bounds[6])
1382 {
1383 bounds[0] = 1.0;
1384 bounds[1] = -1.0;
1385 bounds[2] = 1.0;
1386 bounds[3] = -1.0;
1387 bounds[4] = 1.0;
1388 bounds[5] = -1.0;
1389 }
1391
1393
1396 static vtkTypeBool AreBoundsInitialized(const double bounds[6])
1397 {
1398 if (bounds[1] - bounds[0] < 0.0)
1399 {
1400 return 0;
1401 }
1402 return 1;
1403 }
1405
1410 template <class T>
1411 static T ClampValue(const T& value, const T& min, const T& max);
1412
1414
1418 static void ClampValue(double* value, const double range[2]);
1419 static void ClampValue(double value, const double range[2], double* clamped_value);
1420 static void ClampValues(double* values, int nb_values, const double range[2]);
1421 static void ClampValues(
1422 const double* values, int nb_values, const double range[2], double* clamped_values);
1424
1431 static double ClampAndNormalizeValue(double value, const double range[2]);
1432
1437 template <class T1, class T2>
1438 static void TensorFromSymmetricTensor(const T1 symmTensor[6], T2 tensor[9]);
1439
1445 template <class T>
1446 static void TensorFromSymmetricTensor(T tensor[9]);
1447
1457 double range_min, double range_max, double scale = 1.0, double shift = 0.0);
1458
1467 static vtkTypeBool GetAdjustedScalarRange(vtkDataArray* array, int comp, double range[2]);
1468
1473 static vtkTypeBool ExtentIsWithinOtherExtent(const int extent1[6], const int extent2[6]);
1474
1481 const double bounds1[6], const double bounds2[6], const double delta[3]);
1482
1489 const double point[3], const double bounds[6], const double delta[3]);
1490
1501 const double bounds[6], const double normal[3], const double point[3]);
1502
1512 static double Solve3PointCircle(
1513 const double p1[3], const double p2[3], const double p3[3], double center[3]);
1514
1518 static double Inf();
1519
1523 static double NegInf();
1524
1528 static double Nan();
1529
1533 static vtkTypeBool IsInf(double x);
1534
1538 static vtkTypeBool IsNan(double x);
1539
1544 static bool IsFinite(double x);
1545
1550 static int QuadraticRoot(double a, double b, double c, double min, double max, double* u);
1551
1553 {
1554 FULL,
1555 SAME,
1556 VALID
1557 };
1558
1581 template <class Iter1, class Iter2, class Iter3>
1582 static void Convolve1D(Iter1 beginSample, Iter1 endSample, Iter2 beginKernel, Iter2 endKernel,
1583 Iter3 beginOut, Iter3 endOut, ConvolutionMode mode = ConvolutionMode::FULL)
1584 {
1585 int sampleSize = std::distance(beginSample, endSample);
1586 int kernelSize = std::distance(beginKernel, endKernel);
1587 int outSize = std::distance(beginOut, endOut);
1588
1589 if (sampleSize <= 0 || kernelSize <= 0 || outSize <= 0)
1590 {
1591 return;
1592 }
1593
1594 int begin = 0;
1595 int end = outSize;
1596
1597 switch (mode)
1598 {
1599 case ConvolutionMode::SAME:
1600 begin = static_cast<int>(std::ceil(std::min(sampleSize, kernelSize) / 2.0)) - 1;
1601 end = begin + std::max(sampleSize, kernelSize);
1602 break;
1603 case ConvolutionMode::VALID:
1604 begin = std::min(sampleSize, kernelSize) - 1;
1605 end = begin + std::abs(sampleSize - kernelSize) + 1;
1606 break;
1607 case ConvolutionMode::FULL:
1608 default:
1609 break;
1610 }
1611
1612 for (int i = begin; i < end; i++)
1613 {
1614 Iter3 out = beginOut + i - begin;
1615 *out = 0;
1616 for (int j = std::max(i - sampleSize + 1, 0); j <= std::min(i, kernelSize - 1); j++)
1617 {
1618 *out += *(beginSample + (i - j)) * *(beginKernel + j);
1619 }
1620 }
1621 }
1622
1623protected:
1624 vtkMath() = default;
1625 ~vtkMath() override = default;
1626
1628
1629private:
1630 vtkMath(const vtkMath&) = delete;
1631 void operator=(const vtkMath&) = delete;
1632};
1633
1634//----------------------------------------------------------------------------
1635inline float vtkMath::RadiansFromDegrees(float x)
1636{
1637 return x * 0.017453292f;
1638}
1639
1640//----------------------------------------------------------------------------
1641inline double vtkMath::RadiansFromDegrees(double x)
1642{
1643 return x * 0.017453292519943295;
1644}
1645
1646//----------------------------------------------------------------------------
1647inline float vtkMath::DegreesFromRadians(float x)
1648{
1649 return x * 57.2957795131f;
1650}
1651
1652//----------------------------------------------------------------------------
1653inline double vtkMath::DegreesFromRadians(double x)
1654{
1655 return x * 57.29577951308232;
1656}
1657
1658//----------------------------------------------------------------------------
1659inline bool vtkMath::IsPowerOfTwo(vtkTypeUInt64 x)
1660{
1661 return ((x != 0) & ((x & (x - 1)) == 0));
1662}
1663
1664//----------------------------------------------------------------------------
1665// Credit goes to Peter Hart and William Lewis on comp.lang.python 1997
1667{
1668 unsigned int z = static_cast<unsigned int>(((x > 0) ? x - 1 : 0));
1669 z |= z >> 1;
1670 z |= z >> 2;
1671 z |= z >> 4;
1672 z |= z >> 8;
1673 z |= z >> 16;
1674 return static_cast<int>(z + 1);
1675}
1676
1677//----------------------------------------------------------------------------
1678// Modify the trunc() operation provided by static_cast<int>() to get floor(),
1679// Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1680inline int vtkMath::Floor(double x)
1681{
1682 int i = static_cast<int>(x);
1683 return i - (i > x);
1684}
1685
1686//----------------------------------------------------------------------------
1687// Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1688// Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1689inline int vtkMath::Ceil(double x)
1690{
1691 int i = static_cast<int>(x);
1692 return i + (i < x);
1693}
1694
1695//----------------------------------------------------------------------------
1696template <class T>
1697inline T vtkMath::Min(const T& a, const T& b)
1698{
1699 return (b <= a ? b : a);
1700}
1701
1702//----------------------------------------------------------------------------
1703template <class T>
1704inline T vtkMath::Max(const T& a, const T& b)
1705{
1706 return (b > a ? b : a);
1707}
1708
1709//----------------------------------------------------------------------------
1710inline float vtkMath::Normalize(float v[3])
1711{
1712 float den = vtkMath::Norm(v);
1713 if (den != 0.0)
1714 {
1715 for (int i = 0; i < 3; ++i)
1716 {
1717 v[i] /= den;
1718 }
1719 }
1720 return den;
1721}
1722
1723//----------------------------------------------------------------------------
1724inline double vtkMath::Normalize(double v[3])
1725{
1726 double den = vtkMath::Norm(v);
1727 if (den != 0.0)
1728 {
1729 for (int i = 0; i < 3; ++i)
1730 {
1731 v[i] /= den;
1732 }
1733 }
1734 return den;
1735}
1736
1737//----------------------------------------------------------------------------
1738inline float vtkMath::Normalize2D(float v[2])
1739{
1740 float den = vtkMath::Norm2D(v);
1741 if (den != 0.0)
1742 {
1743 for (int i = 0; i < 2; ++i)
1744 {
1745 v[i] /= den;
1746 }
1747 }
1748 return den;
1749}
1750
1751//----------------------------------------------------------------------------
1752inline double vtkMath::Normalize2D(double v[2])
1753{
1754 double den = vtkMath::Norm2D(v);
1755 if (den != 0.0)
1756 {
1757 for (int i = 0; i < 2; ++i)
1758 {
1759 v[i] /= den;
1760 }
1761 }
1762 return den;
1763}
1764
1765//----------------------------------------------------------------------------
1766inline float vtkMath::Determinant3x3(const float c1[3], const float c2[3], const float c3[3])
1767{
1768 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1769 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1770}
1771
1772//----------------------------------------------------------------------------
1773inline double vtkMath::Determinant3x3(const double c1[3], const double c2[3], const double c3[3])
1774{
1775 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1776 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1777}
1778
1779//----------------------------------------------------------------------------
1781 double a1, double a2, double a3, double b1, double b2, double b3, double c1, double c2, double c3)
1782{
1783 return (a1 * vtkMath::Determinant2x2(b2, b3, c2, c3) -
1784 b1 * vtkMath::Determinant2x2(a2, a3, c2, c3) + c1 * vtkMath::Determinant2x2(a2, a3, b2, b3));
1785}
1786
1787//----------------------------------------------------------------------------
1788inline float vtkMath::Distance2BetweenPoints(const float p1[3], const float p2[3])
1789{
1790 return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1791 (p1[2] - p2[2]) * (p1[2] - p2[2]));
1792}
1793
1794//----------------------------------------------------------------------------
1795inline double vtkMath::Distance2BetweenPoints(const double p1[3], const double p2[3])
1796{
1797 return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1798 (p1[2] - p2[2]) * (p1[2] - p2[2]));
1799}
1800
1801//----------------------------------------------------------------------------
1802template <typename ReturnTypeT, typename TupleRangeT1, typename TupleRangeT2, typename EnableT>
1803inline ReturnTypeT vtkMath::Distance2BetweenPoints(const TupleRangeT1& p1, const TupleRangeT2& p2)
1804{
1805 return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1806 (p1[2] - p2[2]) * (p1[2] - p2[2]));
1807}
1808
1809//----------------------------------------------------------------------------
1810// Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1811inline void vtkMath::Cross(const float a[3], const float b[3], float c[3])
1812{
1813 float Cx = a[1] * b[2] - a[2] * b[1];
1814 float Cy = a[2] * b[0] - a[0] * b[2];
1815 float Cz = a[0] * b[1] - a[1] * b[0];
1816 c[0] = Cx;
1817 c[1] = Cy;
1818 c[2] = Cz;
1819}
1820
1821//----------------------------------------------------------------------------
1822// Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1823inline void vtkMath::Cross(const double a[3], const double b[3], double c[3])
1824{
1825 double Cx = a[1] * b[2] - a[2] * b[1];
1826 double Cy = a[2] * b[0] - a[0] * b[2];
1827 double Cz = a[0] * b[1] - a[1] * b[0];
1828 c[0] = Cx;
1829 c[1] = Cy;
1830 c[2] = Cz;
1831}
1832
1833//----------------------------------------------------------------------------
1834template <class T>
1835inline double vtkDeterminant3x3(const T A[3][3])
1836{
1837 return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] + A[2][0] * A[0][1] * A[1][2] -
1838 A[0][0] * A[2][1] * A[1][2] - A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1839}
1840
1841//----------------------------------------------------------------------------
1842inline double vtkMath::Determinant3x3(const float A[3][3])
1843{
1844 return vtkDeterminant3x3(A);
1845}
1846
1847//----------------------------------------------------------------------------
1848inline double vtkMath::Determinant3x3(const double A[3][3])
1849{
1850 return vtkDeterminant3x3(A);
1851}
1852
1853//----------------------------------------------------------------------------
1854template <class T>
1855inline T vtkMath::ClampValue(const T& value, const T& min, const T& max)
1856{
1857 assert("pre: valid_range" && min <= max);
1858
1859#if __cplusplus >= 201703L
1860 return std::clamp(value, min, max);
1861#else
1862 // compilers are good at optimizing the ternary operator,
1863 // use '<' since it is preferred by STL for custom types
1864 T v = (min < value ? value : min);
1865 return (v < max ? v : max);
1866#endif
1867}
1868
1869//----------------------------------------------------------------------------
1870inline void vtkMath::ClampValue(double* value, const double range[2])
1871{
1872 if (value && range)
1873 {
1874 assert("pre: valid_range" && range[0] <= range[1]);
1875
1876 *value = vtkMath::ClampValue(*value, range[0], range[1]);
1877 }
1878}
1879
1880//----------------------------------------------------------------------------
1881inline void vtkMath::ClampValue(double value, const double range[2], double* clamped_value)
1882{
1883 if (range && clamped_value)
1884 {
1885 assert("pre: valid_range" && range[0] <= range[1]);
1886
1887 *clamped_value = vtkMath::ClampValue(value, range[0], range[1]);
1888 }
1889}
1890
1891// ---------------------------------------------------------------------------
1892inline double vtkMath::ClampAndNormalizeValue(double value, const double range[2])
1893{
1894 assert("pre: valid_range" && range[0] <= range[1]);
1895
1896 double result;
1897 if (range[0] == range[1])
1898 {
1899 result = 0.0;
1900 }
1901 else
1902 {
1903 // clamp
1904 result = vtkMath::ClampValue(value, range[0], range[1]);
1905
1906 // normalize
1907 result = (result - range[0]) / (range[1] - range[0]);
1908 }
1909
1910 assert("post: valid_result" && result >= 0.0 && result <= 1.0);
1911
1912 return result;
1913}
1914
1915//-----------------------------------------------------------------------------
1916template <class T1, class T2>
1917inline void vtkMath::TensorFromSymmetricTensor(const T1 symmTensor[9], T2 tensor[9])
1918{
1919 for (int i = 0; i < 3; ++i)
1920 {
1921 tensor[4 * i] = symmTensor[i];
1922 }
1923 tensor[1] = tensor[3] = symmTensor[3];
1924 tensor[2] = tensor[6] = symmTensor[5];
1925 tensor[5] = tensor[7] = symmTensor[4];
1926}
1927
1928//-----------------------------------------------------------------------------
1929template <class T>
1931{
1932 tensor[6] = tensor[5]; // XZ
1933 tensor[7] = tensor[4]; // YZ
1934 tensor[8] = tensor[2]; // ZZ
1935 tensor[4] = tensor[1]; // YY
1936 tensor[5] = tensor[7]; // YZ
1937 tensor[2] = tensor[6]; // XZ
1938 tensor[1] = tensor[3]; // XY
1939}
1940
1941namespace
1942{
1943template <class QuaternionT, class MatrixT>
1944inline void vtkQuaternionToMatrix3x3(const QuaternionT& quat, MatrixT& A)
1945{
1947
1948 Scalar ww = quat[0] * quat[0];
1949 Scalar wx = quat[0] * quat[1];
1950 Scalar wy = quat[0] * quat[2];
1951 Scalar wz = quat[0] * quat[3];
1952
1953 Scalar xx = quat[1] * quat[1];
1954 Scalar yy = quat[2] * quat[2];
1955 Scalar zz = quat[3] * quat[3];
1956
1957 Scalar xy = quat[1] * quat[2];
1958 Scalar xz = quat[1] * quat[3];
1959 Scalar yz = quat[2] * quat[3];
1960
1961 Scalar rr = xx + yy + zz;
1962 // normalization factor, just in case quaternion was not normalized
1963 Scalar f = 1 / (ww + rr);
1964 Scalar s = (ww - rr) * f;
1965 f *= 2;
1966
1968
1969 Wrapper::template Get<0, 0>(A) = xx * f + s;
1970 Wrapper::template Get<1, 0>(A) = (xy + wz) * f;
1971 Wrapper::template Get<2, 0>(A) = (xz - wy) * f;
1972
1973 Wrapper::template Get<0, 1>(A) = (xy - wz) * f;
1974 Wrapper::template Get<1, 1>(A) = yy * f + s;
1975 Wrapper::template Get<2, 1>(A) = (yz + wx) * f;
1976
1977 Wrapper::template Get<0, 2>(A) = (xz + wy) * f;
1978 Wrapper::template Get<1, 2>(A) = (yz - wx) * f;
1979 Wrapper::template Get<2, 2>(A) = zz * f + s;
1980}
1981} // anonymous namespace
1982
1983//------------------------------------------------------------------------------
1984inline void vtkMath::QuaternionToMatrix3x3(const float quat[4], float A[3][3])
1985{
1986 vtkQuaternionToMatrix3x3(quat, A);
1987}
1988
1989//------------------------------------------------------------------------------
1990inline void vtkMath::QuaternionToMatrix3x3(const double quat[4], double A[3][3])
1991{
1992 vtkQuaternionToMatrix3x3(quat, A);
1993}
1994
1995//-----------------------------------------------------------------------------
1996template <class QuaternionT, class MatrixT, class EnableT>
1997inline void vtkMath::QuaternionToMatrix3x3(const QuaternionT& q, MatrixT&& A)
1998{
1999 vtkQuaternionToMatrix3x3(q, A);
2000}
2001
2002namespace
2003{
2004//------------------------------------------------------------------------------
2005// The solution is based on
2006// Berthold K. P. Horn (1987),
2007// "Closed-form solution of absolute orientation using unit quaternions,"
2008// Journal of the Optical Society of America A, 4:629-642
2009template <class MatrixT, class QuaternionT>
2010inline void vtkMatrix3x3ToQuaternion(const MatrixT& A, QuaternionT& quat)
2011{
2013
2014 Scalar N[4][4];
2015
2017
2018 // on-diagonal elements
2019 N[0][0] = Wrapper::template Get<0, 0>(A) + Wrapper::template Get<1, 1>(A) +
2020 Wrapper::template Get<2, 2>(A);
2021 N[1][1] = Wrapper::template Get<0, 0>(A) - Wrapper::template Get<1, 1>(A) -
2022 Wrapper::template Get<2, 2>(A);
2023 N[2][2] = -Wrapper::template Get<0, 0>(A) + Wrapper::template Get<1, 1>(A) -
2024 Wrapper::template Get<2, 2>(A);
2025 N[3][3] = -Wrapper::template Get<0, 0>(A) - Wrapper::template Get<1, 1>(A) +
2026 Wrapper::template Get<2, 2>(A);
2027
2028 // off-diagonal elements
2029 N[0][1] = N[1][0] = Wrapper::template Get<2, 1>(A) - Wrapper::template Get<1, 2>(A);
2030 N[0][2] = N[2][0] = Wrapper::template Get<0, 2>(A) - Wrapper::template Get<2, 0>(A);
2031 N[0][3] = N[3][0] = Wrapper::template Get<1, 0>(A) - Wrapper::template Get<0, 1>(A);
2032
2033 N[1][2] = N[2][1] = Wrapper::template Get<1, 0>(A) + Wrapper::template Get<0, 1>(A);
2034 N[1][3] = N[3][1] = Wrapper::template Get<0, 2>(A) + Wrapper::template Get<2, 0>(A);
2035 N[2][3] = N[3][2] = Wrapper::template Get<2, 1>(A) + Wrapper::template Get<1, 2>(A);
2036
2037 Scalar eigenvectors[4][4], eigenvalues[4];
2038
2039 // convert into format that JacobiN can use,
2040 // then use Jacobi to find eigenvalues and eigenvectors
2041 Scalar *NTemp[4], *eigenvectorsTemp[4];
2042 for (int i = 0; i < 4; ++i)
2043 {
2044 NTemp[i] = N[i];
2045 eigenvectorsTemp[i] = eigenvectors[i];
2046 }
2047 vtkMath::JacobiN(NTemp, 4, eigenvalues, eigenvectorsTemp);
2048
2049 // the first eigenvector is the one we want
2050 quat[0] = eigenvectors[0][0];
2051 quat[1] = eigenvectors[1][0];
2052 quat[2] = eigenvectors[2][0];
2053 quat[3] = eigenvectors[3][0];
2054}
2055} // anonymous namespace
2056
2057//------------------------------------------------------------------------------
2058inline void vtkMath::Matrix3x3ToQuaternion(const float A[3][3], float quat[4])
2059{
2060 vtkMatrix3x3ToQuaternion(A, quat);
2061}
2062
2063//------------------------------------------------------------------------------
2064inline void vtkMath::Matrix3x3ToQuaternion(const double A[3][3], double quat[4])
2065{
2066 vtkMatrix3x3ToQuaternion(A, quat);
2067}
2068
2069//-----------------------------------------------------------------------------
2070template <class MatrixT, class QuaternionT, class EnableT>
2071inline void vtkMath::Matrix3x3ToQuaternion(const MatrixT& A, QuaternionT&& q)
2072{
2073 vtkMatrix3x3ToQuaternion(A, q);
2074}
2075
2076namespace vtk_detail
2077{
2078// Can't specialize templates inside a template class, so we move the impl here.
2079template <typename OutT>
2080void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
2081{ // OutT is integral -- clamp and round
2082 if (!vtkMath::IsNan(val))
2083 {
2084 double min = static_cast<double>(vtkTypeTraits<OutT>::Min());
2085 double max = static_cast<double>(vtkTypeTraits<OutT>::Max());
2086 val = vtkMath::ClampValue(val, min, max);
2087 *ret = static_cast<OutT>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
2088 }
2089 else
2090 *ret = 0;
2091}
2092template <>
2093inline void RoundDoubleToIntegralIfNecessary(double val, double* retVal)
2094{ // OutT is double: passthrough
2095 *retVal = val;
2096}
2097template <>
2098inline void RoundDoubleToIntegralIfNecessary(double val, float* retVal)
2099{ // OutT is float -- just clamp (as doubles, then the cast to float is well-defined.)
2100 if (!vtkMath::IsNan(val))
2101 {
2102 double min = static_cast<double>(vtkTypeTraits<float>::Min());
2103 double max = static_cast<double>(vtkTypeTraits<float>::Max());
2104 val = vtkMath::ClampValue(val, min, max);
2105 }
2106
2107 *retVal = static_cast<float>(val);
2108}
2109} // end namespace vtk_detail
2110
2111//-----------------------------------------------------------------------------
2112#if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
2113#define VTK_MATH_ISINF_IS_INLINE
2114inline vtkTypeBool vtkMath::IsInf(double x)
2115{
2116#if defined(VTK_HAS_STD_ISINF)
2117 return std::isinf(x);
2118#else
2119 return (isinf(x) != 0); // Force conversion to bool
2120#endif
2121}
2122#endif
2123
2124//-----------------------------------------------------------------------------
2125#if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
2126#define VTK_MATH_ISNAN_IS_INLINE
2127inline vtkTypeBool vtkMath::IsNan(double x)
2128{
2129#if defined(VTK_HAS_STD_ISNAN)
2130 return std::isnan(x);
2131#else
2132 return (isnan(x) != 0); // Force conversion to bool
2133#endif
2134}
2135#endif
2136
2137//-----------------------------------------------------------------------------
2138#if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
2139#define VTK_MATH_ISFINITE_IS_INLINE
2140inline bool vtkMath::IsFinite(double x)
2141{
2142#if defined(VTK_HAS_STD_ISFINITE)
2143 return std::isfinite(x);
2144#elif defined(VTK_HAS_ISFINITE)
2145 return (isfinite(x) != 0); // Force conversion to bool
2146#else
2147 return (finite(x) != 0); // Force conversion to bool
2148#endif
2149}
2150#endif
2151
2152#endif
Gaussian sequence of pseudo random numbers implemented with the Box-Mueller transform.
abstract superclass for arrays of numeric data
a simple class to control print indentation
Definition vtkIndent.h:49
performs common math operations
Definition vtkMath.h:103
static ReturnTypeT Distance2BetweenPoints(const TupleRangeT1 &p1, const TupleRangeT2 &p2)
Compute distance squared between two points p1 and p2.
Definition vtkMath.h:1803
static void Multiply3x3(const float A[3][3], const float B[3][3], float C[3][3])
Multiply one 3x3 matrix by another according to C = AB.
static double Dot(const double a[3], const double b[3])
Dot product of two 3-vectors (double version).
Definition vtkMath.h:460
static int GetScalarTypeFittingRange(double range_min, double range_max, double scale=1.0, double shift=0.0)
Return the scalar type that is most likely to have enough precision to store a given range of data on...
static void RGBToXYZ(double r, double g, double b, double *x, double *y, double *z)
Convert color from the RGB system to CIE XYZ.
static void Multiply3x3(const double A[3][3], const double B[3][3], double C[3][3])
Multiply one 3x3 matrix by another according to C = AB.
static double Norm(const double *x, int n)
Compute the norm of n-vector.
static int Round(float f)
Rounds a float to the nearest integer.
Definition vtkMath.h:134
static void MultiplyMatrixWithVector(const MatrixT &M, const VectorT1 &X, VectorT2 &&Y)
Multiply matrix M with vector Y such that Y = M x X.
Definition vtkMath.h:856
static double Norm2D(const double x[2])
Compute the norm of a 2-vector.
Definition vtkMath.h:728
static void XYZToRGB(double x, double y, double z, double *r, double *g, double *b)
Convert color from the CIE XYZ system to RGB.
static void Subtract(const float a[3], const float b[3], float c[3])
Subtraction of two 3-vectors (float version).
Definition vtkMath.h:369
static void LUSolve3x3(const double A[3][3], const int index[3], double x[3])
LU back substitution for a 3x3 matrix.
static vtkTypeBool SolveHomogeneousLeastSquares(int numberOfSamples, double **xt, int xOrder, double **mt)
Solves for the least squares best fit matrix for the homogeneous equation X'M' = 0'.
static void Outer2D(const float x[2], const float y[2], float A[2][2])
Outer product of two 2-vectors (float version).
Definition vtkMath.h:693
static bool ProjectVector(const double a[3], const double b[3], double projection[3])
Compute the projection of vector a on vector b and return it in projection[3].
static vtkSmartPointer< vtkMathInternal > Internal
Definition vtkMath.h:1627
static float Norm(const float *x, int n)
Compute the norm of n-vector.
static vtkTypeBool ExtentIsWithinOtherExtent(const int extent1[6], const int extent2[6])
Return true if first 3D extent is within second 3D extent Extent is x-min, x-max, y-min,...
static void Add(const double a[3], const double b[3], double c[3])
Addition of two 3-vectors (double version).
Definition vtkMath.h:358
static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v)
Convert color in RGB format (Red, Green, Blue) to HSV format (Hue, Saturation, Value).
static float Norm(const float v[3])
Compute the norm of 3-vector (float version).
Definition vtkMath.h:540
static ReturnTypeT Dot(const TupleRangeT1 &a, const TupleRangeT2 &b)
Compute dot product between two points p1 and p2.
Definition vtkMath.h:484
static vtkTypeBool Jacobi(double **a, double *w, double **v)
Jacobi iteration for the solution of eigenvectors/eigenvalues of a 3x3 real symmetric matrix.
static void XYZToLab(const double xyz[3], double lab[3])
Convert Color from the CIE XYZ system to CIE-L*ab.
Definition vtkMath.h:1323
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static vtkTypeInt64 Factorial(int N)
Compute N factorial, N! = N*(N-1) * (N-2)...*3*2*1.
static vtkTypeInt64 Binomial(int m, int n)
The number of combinations of n objects from a pool of m objects (m>n).
static double Random()
Generate pseudo-random numbers distributed according to the uniform distribution between 0....
static void LinearSolve(const MatrixT &M, const VectorT1 &x, VectorT2 &y)
This method solves linear systems M * x = y.
Definition vtkMath.h:933
static void Identity3x3(float A[3][3])
Set A to the identity matrix.
static double GaussianAmplitude(const double variance, const double distanceFromMean)
Compute the amplitude of a Gaussian function with mean=0 and specified variance.
static void SingularValueDecomposition3x3(const float A[3][3], float U[3][3], float w[3], float VT[3][3])
Perform singular value decomposition on a 3x3 matrix.
static double Nan()
Special IEEE-754 number used to represent Not-A-Number (Nan).
static void Perpendiculars(const float v1[3], float v2[3], float v3[3], double theta)
Given a unit vector v1, find two unit vectors v2 and v3 such that v1 cross v2 = v3 (i....
static double Gaussian(double mean, double std)
Generate pseudo-random numbers distributed according to the Gaussian distribution with mean mean and ...
static bool IsFinite(double x)
Test if a number has finite value i.e.
static void LUSolveLinearSystem(double **A, int *index, double *x, int size)
Solve linear equations Ax = b using LU decomposition A = LU where L is lower triangular matrix and U ...
static double EstimateMatrixCondition(const double *const *A, int size)
Estimate the condition number of a LU factored matrix.
static void LUFactor3x3(float A[3][3], int index[3])
LU Factorization of a 3x3 matrix.
static void FreeCombination(int *combination)
Free the "iterator" array created by vtkMath::BeginCombination.
static double Random(double min, double max)
Generate pseudo-random numbers distributed according to the uniform distribution between min and max.
static void TensorFromSymmetricTensor(const T1 symmTensor[6], T2 tensor[9])
Convert a 6-Component symmetric tensor into a 9-Component tensor, no allocation performed.
static void LabToXYZ(const double lab[3], double xyz[3])
Convert color from the CIE-L*ab system to CIE XYZ.
Definition vtkMath.h:1312
static double Solve3PointCircle(const double p1[3], const double p2[3], const double p3[3], double center[3])
In Euclidean space, there is a unique circle passing through any given three non-collinear points P1,...
static vtkTypeBool PointIsWithinBounds(const double point[3], const double bounds[6], const double delta[3])
Return true if point is within the given 3D bounds Bounds is x-min, x-max, y-min, y-max,...
static float Dot(const float a[3], const float b[3])
Dot product of two 3-vectors (float version).
Definition vtkMath.h:452
static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3])
Diagonalize a symmetric 3x3 matrix and return the eigenvalues in w and the eigenvectors in the column...
static void LabToXYZ(double L, double a, double b, double *x, double *y, double *z)
Convert color from the CIE-L*ab system to CIE XYZ.
static vtkTypeBool GetAdjustedScalarRange(vtkDataArray *array, int comp, double range[2])
Get a vtkDataArray's scalar range for a given component.
static bool ProjectVector(const float a[3], const float b[3], float projection[3])
Compute the projection of vector a on vector b and return it in projection[3].
static void Cross(const float a[3], const float b[3], float c[3])
Cross product of two 3-vectors.
Definition vtkMath.h:1811
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
Definition vtkMath.h:417
static void HSVToRGB(const float hsv[3], float rgb[3])
Convert color in HSV format (Hue, Saturation, Value) to RGB format (Red, Green, Blue).
Definition vtkMath.h:1296
static void Assign(const double a[3], double b[3])
Assign values to a 3-vector (double version).
Definition vtkMath.h:342
static double Determinant2x2(const double c1[2], const double c2[2])
Calculate the determinant of a 2x2 matrix: | a b | | c d |.
Definition vtkMath.h:755
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition vtkMath.h:1704
static void Outer2D(const double x[2], const double y[2], double A[2][2])
Outer product of two 2-vectors (double version).
Definition vtkMath.h:707
static void RandomSeed(int s)
Initialize seed value.
static double NegInf()
Special IEEE-754 number used to represent negative infinity.
static void MultiplyScalar2D(double a[2], double s)
Multiplies a 2-vector by a scalar (double version).
Definition vtkMath.h:441
static void LabToRGB(double L, double a, double b, double *red, double *green, double *blue)
Convert color from the CIE-L*ab system to RGB.
static double Gaussian()
Generate pseudo-random numbers distributed according to the standard normal distribution.
static int Ceil(double x)
Rounds a double to the nearest integer not less than itself.
Definition vtkMath.h:1689
static void HSVToRGB(const double hsv[3], double rgb[3])
Convert color in HSV format (Hue, Saturation, Value) to RGB format (Red, Green, Blue).
Definition vtkMath.h:1301
~vtkMath() override=default
static ScalarT Dot(const VectorT1 &x, const VectorT2 &y)
Computes the dot product between 2 vectors x and y.
Definition vtkMath.h:867
static double Inf()
Special IEEE-754 number used to represent positive infinity.
static double GaussianAmplitude(const double mean, const double variance, const double position)
Compute the amplitude of a Gaussian function with specified mean and variance.
static vtkTypeBool Jacobi(float **a, float *w, float **v)
Jacobi iteration for the solution of eigenvectors/eigenvalues of a 3x3 real symmetric matrix.
static int PlaneIntersectsAABB(const double bounds[6], const double normal[3], const double point[3])
Implements Plane / Axis-Aligned Bounding-Box intersection as described in Graphics Gems IV,...
static void RGBToXYZ(const double rgb[3], double xyz[3])
Convert color from the RGB system to CIE XYZ.
Definition vtkMath.h:1345
static void QuaternionToMatrix3x3(const float quat[4], float A[3][3])
Convert a quaternion to a 3x3 rotation matrix.
Definition vtkMath.h:1984
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition vtkMath.h:1666
static void HSVToRGB(double h, double s, double v, double *r, double *g, double *b)
Convert color in HSV format (Hue, Saturation, Value) to RGB format (Red, Green, Blue).
static void SingularValueDecomposition3x3(const double A[3][3], double U[3][3], double w[3], double VT[3][3])
Perform singular value decomposition on a 3x3 matrix.
static double SignedAngleBetweenVectors(const double v1[3], const double v2[3], const double vn[3])
Compute signed angle in radians between two vectors with regard to a third orthogonal vector.
static float Normalize2D(float v[2])
Normalize (in place) a 2-vector.
Definition vtkMath.h:1738
static void Invert3x3(const double A[3][3], double AI[3][3])
Invert a 3x3 matrix.
static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b)
Convert color in HSV format (Hue, Saturation, Value) to RGB format (Red, Green, Blue).
static void MultiplyQuaternion(const double q1[4], const double q2[4], double q[4])
Multiply two quaternions.
static void Multiply3x3(const double A[3][3], const double v[3], double u[3])
Multiply a vector by a 3x3 matrix.
static void Outer(const double a[3], const double b[3], double c[3][3])
Outer product of two 3-vectors (double version).
Definition vtkMath.h:506
static vtkTypeBool InvertMatrix(double **A, double **AI, int size, int *tmp1Size, double *tmp2Size)
Thread safe version of InvertMatrix method.
static vtkTypeBool InvertMatrix(double **A, double **AI, int size)
Invert input square matrix A into matrix AI.
static void LUSolve3x3(const float A[3][3], const int index[3], float x[3])
LU back substitution for a 3x3 matrix.
static int GetSeed()
Return the current seed used by the random number generator.
static void Assign(const VectorT1 &a, VectorT2 &&b)
Assign values to a 3-vector (templated version).
Definition vtkMath.h:332
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
Definition vtkMath.h:1635
static void Convolve1D(Iter1 beginSample, Iter1 endSample, Iter2 beginKernel, Iter2 endKernel, Iter3 beginOut, Iter3 endOut, ConvolutionMode mode=ConvolutionMode::FULL)
Compute the convolution of a sampled 1D signal by a given kernel.
Definition vtkMath.h:1582
static void RotateVectorByWXYZ(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 Add(const float a[3], const float b[3], float c[3])
Addition of two 3-vectors (float version).
Definition vtkMath.h:347
static int CeilLog2(vtkTypeUInt64 x)
Gives the exponent of the lowest power of two not less than x.
static vtkTypeBool AreBoundsInitialized(const double bounds[6])
Are the bounds initialized?
Definition vtkMath.h:1396
static bool ProjectVector2D(const double a[2], const double b[2], double projection[2])
Compute the projection of 2D vector a on 2D vector b and returns the result in projection[2].
static vtkTypeBool JacobiN(float **a, int n, float *w, float **v)
JacobiN iteration for the solution of eigenvectors/eigenvalues of a nxn real symmetric matrix.
static vtkMatrixUtilities::ScalarTypeExtractor< MatrixT >::value_type Determinant(const MatrixT &M)
Computes the determinant of input square SizeT x SizeT matrix M.
Definition vtkMath.h:890
static int NextCombination(int m, int n, int *combination)
Given m, n, and a valid combination of n integers in the range [0,m[, this function alters the intege...
static constexpr double Pi()
A mathematical constant.
Definition vtkMath.h:112
static double GaussianWeight(const double variance, const double distanceFromMean)
Compute the amplitude of an unnormalized Gaussian function with mean=0 and specified variance.
static void Multiply3x3(const float A[3][3], const float v[3], float u[3])
Multiply a vector by a 3x3 matrix.
static void Subtract(const double a[3], const double b[3], double c[3])
Subtraction of two 3-vectors (double version).
Definition vtkMath.h:380
static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4])
Convert a 3x3 matrix into a quaternion.
Definition vtkMath.h:2058
static vtkMath * New()
static void Orthogonalize3x3(const double A[3][3], double B[3][3])
Orthogonalize a 3x3 matrix and put the result in B.
static void XYZToRGB(const double xyz[3], double rgb[3])
Convert color from the CIE XYZ system to RGB.
Definition vtkMath.h:1334
static double ClampAndNormalizeValue(double value, const double range[2])
Clamp a value against a range and then normalize it between 0 and 1.
Definition vtkMath.h:1892
static void MultiplyScalar(double a[3], double s)
Multiplies a 3-vector by a scalar (double version).
Definition vtkMath.h:429
static double Dot2D(const double x[2], const double y[2])
Dot product of two 2-vectors.
Definition vtkMath.h:688
static void LinearSolve3x3(const float A[3][3], const float x[3], float y[3])
Solve Ay = x for y and place the result in y.
static void MultiplyMatrix(const MatrixT1 &M1, const MatrixT2 &M2, MatrixT3 &&M3)
Multiply matrices such that M3 = M1 x M2.
Definition vtkMath.h:829
static vtkTypeBool IsNan(double x)
Test if a number is equal to the special floating point value Not-A-Number (Nan).
static void Diagonalize3x3(const double A[3][3], double w[3], double V[3][3])
Diagonalize a symmetric 3x3 matrix and return the eigenvalues in w and the eigenvectors in the column...
static void RGBToLab(const double rgb[3], double lab[3])
Convert color from the RGB system to CIE-L*ab.
Definition vtkMath.h:1359
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
Definition vtkMath.h:1680
static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3])
rotate a vector by a normalized quaternion using // https://en.wikipedia.org/wiki/Rodrigues%27_rotati...
static void Subtract(const VectorT1 &a, const VectorT2 &b, VectorT3 &&c)
Subtraction of two 3-vectors (templated version).
Definition vtkMath.h:394
static vtkTypeBool BoundsIsWithinOtherBounds(const double bounds1[6], const double bounds2[6], const double delta[3])
Return true if first 3D bounds is within the second 3D bounds Bounds is x-min, x-max,...
static double Determinant2x2(double a, double b, double c, double d)
Calculate the determinant of a 2x2 matrix: | a b | | c d |.
Definition vtkMath.h:754
static void RGBToHSV(const double rgb[3], double hsv[3])
Convert color in RGB format (Red, Green, Blue) to HSV format (Hue, Saturation, Value).
Definition vtkMath.h:1281
static vtkTypeBool JacobiN(double **a, int n, double *w, double **v)
JacobiN iteration for the solution of eigenvectors/eigenvalues of a nxn real symmetric matrix.
static double AngleBetweenVectors(const double v1[3], const double v2[3])
Compute angle in radians between two vectors.
static void MultiplyMatrix(const double *const *A, const double *const *B, unsigned int rowA, unsigned int colA, unsigned int rowB, unsigned int colB, double **C)
General matrix multiplication.
static float DegreesFromRadians(float radians)
Convert radians into degrees.
Definition vtkMath.h:1647
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
Definition vtkMath.h:745
static int Round(double f)
Definition vtkMath.h:135
static vtkTypeBool IsInf(double x)
Test if a number is equal to the special floating point value infinity.
static void UninitializeBounds(double bounds[6])
Set the bounds to an uninitialized state.
Definition vtkMath.h:1381
vtkMath()=default
static void RGBToHSV(double r, double g, double b, double *h, double *s, double *v)
Convert color in RGB format (Red, Green, Blue) to HSV format (Hue, Saturation, Value).
static void Outer(const float a[3], const float b[3], float c[3][3])
Outer product of two 3-vectors (float version).
Definition vtkMath.h:492
static int * BeginCombination(int m, int n)
Start iterating over "m choose n" objects.
static double Norm(const double v[3])
Compute the norm of 3-vector (double version).
Definition vtkMath.h:545
static void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
Round a double to type OutT if OutT is integral, otherwise simply clamp the value to the output range...
Definition vtkMath.h:143
static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3])
rotate a vector by WXYZ using // https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
static bool IsPowerOfTwo(vtkTypeUInt64 x)
Returns true if integer is a power of two.
Definition vtkMath.h:1659
static void Invert3x3(const float A[3][3], float AI[3][3])
Invert a 3x3 matrix.
static float Normalize(float v[3])
Normalize (in place) a 3-vector.
Definition vtkMath.h:1710
static void Transpose3x3(const double A[3][3], double AT[3][3])
Transpose a 3x3 matrix.
static ReturnTypeT SquaredNorm(const TupleRangeT &v)
Compute the squared norm of a 3-vector.
Definition vtkMath.h:560
static double Determinant3x3(const float A[3][3])
Return the determinant of a 3x3 matrix.
Definition vtkMath.h:1842
static float Dot2D(const float x[2], const float y[2])
Dot product of two 2-vectors.
Definition vtkMath.h:683
ConvolutionMode
Definition vtkMath.h:1553
static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3])
rotate a vector by a normalized quaternion using // https://en.wikipedia.org/wiki/Rodrigues%27_rotati...
static ScalarT Dot(const VectorT1 &x, const MatrixT &M, const VectorT2 &y)
Computes the dot product x^T M y, where x and y are vectors and M is a metric matrix.
Definition vtkMath.h:954
static void RGBToHSV(const float rgb[3], float hsv[3])
Convert color in RGB format (Red, Green, Blue) to HSV format (Hue, Saturation, Value).
Definition vtkMath.h:1276
static void Orthogonalize3x3(const float A[3][3], float B[3][3])
Orthogonalize a 3x3 matrix and put the result in B.
static bool ProjectVector2D(const float a[2], const float b[2], float projection[2])
Compute the projection of 2D vector a on 2D vector b and returns the result in projection[2].
static vtkTypeBool SolveLinearSystemGEPP2x2(double a00, double a01, double a10, double a11, double b0, double b1, double &x0, double &x1)
Solve linear equation Ax = b using Gaussian Elimination with Partial Pivoting for a 2x2 system.
static vtkTypeBool SolveLinearSystem(double **A, double *x, int size)
Solve linear equations Ax = b using Crout's method.
static void LabToRGB(const double lab[3], double rgb[3])
Convert color from the CIE-L*ab system to RGB.
Definition vtkMath.h:1370
static float Norm2D(const float x[2])
Compute the norm of a 2-vector.
Definition vtkMath.h:722
static double GaussianWeight(const double mean, const double variance, const double position)
Compute the amplitude of an unnormalized Gaussian function with specified mean and variance.
static vtkTypeBool LUFactorLinearSystem(double **A, int *index, int size, double *tmpSize)
Thread safe version of LUFactorLinearSystem method.
static void LinearSolve3x3(const double A[3][3], const double x[3], double y[3])
Solve Ay = x for y and place the result in y.
static void XYZToLab(double x, double y, double z, double *L, double *a, double *b)
Convert Color from the CIE XYZ system to CIE-L*ab.
static void InvertMatrix(const MatrixT1 &M1, MatrixT2 &&M2)
Computes the inverse of input matrix M1 into M2.
Definition vtkMath.h:913
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
Definition vtkMath.h:405
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition vtkMath.h:1697
static void Perpendiculars(const double v1[3], double v2[3], double v3[3], double theta)
Given a unit vector v1, find two unit vectors v2 and v3 such that v1 cross v2 = v3 (i....
static T ClampValue(const T &value, const T &min, const T &max)
Clamp some value against a range, return the result.
Definition vtkMath.h:1855
static vtkTypeBool SolveLeastSquares(int numberOfSamples, double **xt, int xOrder, double **yt, int yOrder, double **mt, int checkHomogeneous=1)
Solves for the least squares best fit matrix for the equation X'M' = Y'.
static void Identity3x3(double A[3][3])
Set A to the identity matrix.
static void LUFactor3x3(double A[3][3], int index[3])
LU Factorization of a 3x3 matrix.
static vtkTypeBool LUFactorLinearSystem(double **A, int *index, int size)
Factor linear equations Ax = b using LU decomposition into the form A = LU where L is a unit lower tr...
static void RGBToLab(double red, double green, double blue, double *L, double *a, double *b)
Convert color from the RGB system to CIE-L*ab.
static void MultiplyQuaternion(const float q1[4], const float q2[4], float q[4])
Multiply two quaternions.
static void ClampValues(const double *values, int nb_values, const double range[2], double *clamped_values)
Clamp some values against a range The method without 'clamped_values' will perform in-place clamping.
static void Transpose3x3(const float A[3][3], float AT[3][3])
Transpose a 3x3 matrix.
static void ClampValues(double *values, int nb_values, const double range[2])
Clamp some values against a range The method without 'clamped_values' will perform in-place clamping.
static int QuadraticRoot(double a, double b, double c, double min, double max, double *u)
find roots of ax^2+bx+c=0 in the interval min,max.
Park and Miller Sequence of pseudo random numbers.
abstract base class for most VTK objects
Definition vtkObject.h:72
represent and manipulate 3D points
Definition vtkPoints.h:49
Computes the portion of a dataset which is inside a selection.
Hold a reference to a vtkObjectBase instance.
void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
Definition vtkMath.h:2080
detail::ScalarTypeExtractor< std::is_array< DerefContainer >::value||std::is_pointer< DerefContainer >::value, ContainerT >::value_type value_type
Template defining traits of native types used by VTK.
int vtkTypeBool
Definition vtkABI.h:69
double vtkDeterminant3x3(const T A[3][3])
Definition vtkMath.h:1835
#define max(a, b)