VTK  9.1.0
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 =========================================================================*/
148 #ifndef vtkMath_h
149 #define vtkMath_h
150 
151 #include "vtkCommonCoreModule.h" // For export macro
152 #include "vtkMathPrivate.hxx" // For Matrix meta-class helpers
153 #include "vtkMatrixUtilities.h" // For Matrix wrapping / mapping
154 #include "vtkObject.h"
155 #include "vtkSmartPointer.h" // For vtkSmartPointer.
156 #include "vtkTypeTraits.h" // For type traits
157 
158 #include "vtkMathConfigure.h" // For <cmath> and VTK_HAS_ISNAN etc.
159 
160 #include <algorithm> // for std::clamp
161 #include <cassert> // assert() in inline implementations.
162 
163 #ifndef DBL_MIN
164 #define VTK_DBL_MIN 2.2250738585072014e-308
165 #else // DBL_MIN
166 #define VTK_DBL_MIN DBL_MIN
167 #endif // DBL_MIN
168 
169 #ifndef DBL_EPSILON
170 #define VTK_DBL_EPSILON 2.2204460492503131e-16
171 #else // DBL_EPSILON
172 #define VTK_DBL_EPSILON DBL_EPSILON
173 #endif // DBL_EPSILON
174 
175 #ifndef VTK_DBL_EPSILON
176 #ifndef DBL_EPSILON
177 #define VTK_DBL_EPSILON 2.2204460492503131e-16
178 #else // DBL_EPSILON
179 #define VTK_DBL_EPSILON DBL_EPSILON
180 #endif // DBL_EPSILON
181 #endif // VTK_DBL_EPSILON
182 
183 class vtkDataArray;
184 class vtkPoints;
185 class vtkMathInternal;
188 
189 namespace vtk_detail
190 {
191 // forward declaration
192 template <typename OutT>
193 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret);
194 } // end namespace vtk_detail
195 
196 class VTKCOMMONCORE_EXPORT vtkMath : public vtkObject
197 {
198 public:
199  static vtkMath* New();
200  vtkTypeMacro(vtkMath, vtkObject);
201  void PrintSelf(ostream& os, vtkIndent indent) override;
202 
206  static constexpr double Pi() { return 3.141592653589793; }
207 
209 
212  static float RadiansFromDegrees(float degrees);
213  static double RadiansFromDegrees(double degrees);
215 
217 
220  static float DegreesFromRadians(float radians);
221  static double DegreesFromRadians(double radians);
223 
227 #if 1
228  static int Round(float f) { return static_cast<int>(f + (f >= 0.0 ? 0.5 : -0.5)); }
229  static int Round(double f) { return static_cast<int>(f + (f >= 0.0 ? 0.5 : -0.5)); }
230 #endif
231 
236  template <typename OutT>
237  static void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
238  {
239  // Can't specialize template methods in a template class, so we move the
240  // implementations to a external namespace.
242  }
243 
249  static int Floor(double x);
250 
256  static int Ceil(double x);
257 
263  static int CeilLog2(vtkTypeUInt64 x);
264 
269  template <class T>
270  static T Min(const T& a, const T& b);
271 
276  template <class T>
277  static T Max(const T& a, const T& b);
278 
282  static bool IsPowerOfTwo(vtkTypeUInt64 x);
283 
289  static int NearestPowerOfTwo(int x);
290 
295  static vtkTypeInt64 Factorial(int N);
296 
302  static vtkTypeInt64 Binomial(int m, int n);
303 
315  static int* BeginCombination(int m, int n);
316 
327  static int NextCombination(int m, int n, int* combination);
328 
332  static void FreeCombination(int* combination);
333 
349  static void RandomSeed(int s);
350 
362  static int GetSeed();
363 
377  static double Random();
378 
391  static double Random(double min, double max);
392 
405  static double Gaussian();
406 
419  static double Gaussian(double mean, double std);
420 
425  template <class VectorT1, class VectorT2>
426  static void Assign(const VectorT1& a, VectorT2&& b)
427  {
428  b[0] = a[0];
429  b[1] = a[1];
430  b[2] = a[2];
431  }
432 
436  static void Assign(const double a[3], double b[3]) { vtkMath::Assign<>(a, b); }
437 
441  static void Add(const float a[3], const float b[3], float c[3])
442  {
443  for (int i = 0; i < 3; ++i)
444  {
445  c[i] = a[i] + b[i];
446  }
447  }
448 
452  static void Add(const double a[3], const double b[3], double c[3])
453  {
454  for (int i = 0; i < 3; ++i)
455  {
456  c[i] = a[i] + b[i];
457  }
458  }
459 
463  static void Subtract(const float a[3], const float b[3], float c[3])
464  {
465  for (int i = 0; i < 3; ++i)
466  {
467  c[i] = a[i] - b[i];
468  }
469  }
470 
474  static void Subtract(const double a[3], const double b[3], double c[3])
475  {
476  for (int i = 0; i < 3; ++i)
477  {
478  c[i] = a[i] - b[i];
479  }
480  }
481 
487  template <class VectorT1, class VectorT2, class VectorT3>
488  static void Subtract(const VectorT1& a, const VectorT2& b, VectorT3&& c)
489  {
490  c[0] = a[0] - b[0];
491  c[1] = a[1] - b[1];
492  c[2] = a[2] - b[2];
493  }
494 
499  static void MultiplyScalar(float a[3], float s)
500  {
501  for (int i = 0; i < 3; ++i)
502  {
503  a[i] *= s;
504  }
505  }
506 
511  static void MultiplyScalar2D(float a[2], float s)
512  {
513  for (int i = 0; i < 2; ++i)
514  {
515  a[i] *= s;
516  }
517  }
518 
523  static void MultiplyScalar(double a[3], double s)
524  {
525  for (int i = 0; i < 3; ++i)
526  {
527  a[i] *= s;
528  }
529  }
530 
535  static void MultiplyScalar2D(double a[2], double s)
536  {
537  for (int i = 0; i < 2; ++i)
538  {
539  a[i] *= s;
540  }
541  }
542 
546  static float Dot(const float a[3], const float b[3])
547  {
548  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
549  }
550 
554  static double Dot(const double a[3], const double b[3])
555  {
556  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
557  }
558 
574  template <typename ReturnTypeT = double, typename TupleRangeT1, typename TupleRangeT2,
575  typename EnableT = typename std::conditional<!std::is_pointer<TupleRangeT1>::value &&
577  TupleRangeT1, TupleRangeT2>::type::value_type>
578  static ReturnTypeT Dot(const TupleRangeT1& a, const TupleRangeT2& b)
579  {
580  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
581  }
582 
586  static void Outer(const float a[3], const float b[3], float c[3][3])
587  {
588  for (int i = 0; i < 3; ++i)
589  {
590  for (int j = 0; j < 3; ++j)
591  {
592  c[i][j] = a[i] * b[j];
593  }
594  }
595  }
596 
600  static void Outer(const double a[3], const double b[3], double c[3][3])
601  {
602  for (int i = 0; i < 3; ++i)
603  {
604  for (int j = 0; j < 3; ++j)
605  {
606  c[i][j] = a[i] * b[j];
607  }
608  }
609  }
610 
615  static void Cross(const float a[3], const float b[3], float c[3]);
616 
621  static void Cross(const double a[3], const double b[3], double c[3]);
622 
624 
627  static float Norm(const float* x, int n);
628  static double Norm(const double* x, int n);
630 
634  static float Norm(const float v[3]) { return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); }
635 
639  static double Norm(const double v[3])
640  {
641  return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
642  }
643 
653  template <typename ReturnTypeT = double, typename TupleRangeT>
654  static ReturnTypeT SquaredNorm(const TupleRangeT& v)
655  {
656  return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
657  }
658 
663  static float Normalize(float v[3]);
664 
669  static double Normalize(double v[3]);
670 
672 
679  static void Perpendiculars(const double v1[3], double v2[3], double v3[3], double theta);
680  static void Perpendiculars(const float v1[3], float v2[3], float v3[3], double theta);
682 
684 
689  static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
690  static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
692 
694 
700  static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
701  static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
703 
719  template <typename ReturnTypeT = double, typename TupleRangeT1, typename TupleRangeT2,
720  typename EnableT = typename std::conditional<!std::is_pointer<TupleRangeT1>::value &&
722  TupleRangeT1, TupleRangeT2>::type::value_type>
723  static ReturnTypeT Distance2BetweenPoints(const TupleRangeT1& p1, const TupleRangeT2& p2);
724 
729  static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
730 
735  static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
736 
740  static double AngleBetweenVectors(const double v1[3], const double v2[3]);
741 
746  const double v1[3], const double v2[3], const double vn[3]);
747 
752  static double GaussianAmplitude(const double variance, const double distanceFromMean);
753 
758  static double GaussianAmplitude(const double mean, const double variance, const double position);
759 
765  static double GaussianWeight(const double variance, const double distanceFromMean);
766 
772  static double GaussianWeight(const double mean, const double variance, const double position);
773 
777  static float Dot2D(const float x[2], const float y[2]) { return x[0] * y[0] + x[1] * y[1]; }
778 
782  static double Dot2D(const double x[2], const double y[2]) { return x[0] * y[0] + x[1] * y[1]; }
783 
787  static void Outer2D(const float x[2], const float y[2], float A[2][2])
788  {
789  for (int i = 0; i < 2; ++i)
790  {
791  for (int j = 0; j < 2; ++j)
792  {
793  A[i][j] = x[i] * y[j];
794  }
795  }
796  }
797 
801  static void Outer2D(const double x[2], const double y[2], double A[2][2])
802  {
803  for (int i = 0; i < 2; ++i)
804  {
805  for (int j = 0; j < 2; ++j)
806  {
807  A[i][j] = x[i] * y[j];
808  }
809  }
810  }
811 
816  static float Norm2D(const float x[2]) { return std::sqrt(x[0] * x[0] + x[1] * x[1]); }
817 
822  static double Norm2D(const double x[2]) { return std::sqrt(x[0] * x[0] + x[1] * x[1]); }
823 
828  static float Normalize2D(float v[2]);
829 
834  static double Normalize2D(double v[2]);
835 
839  static float Determinant2x2(const float c1[2], const float c2[2])
840  {
841  return c1[0] * c2[1] - c2[0] * c1[1];
842  }
843 
845 
848  static double Determinant2x2(double a, double b, double c, double d) { return a * d - b * c; }
849  static double Determinant2x2(const double c1[2], const double c2[2])
850  {
851  return c1[0] * c2[1] - c2[0] * c1[1];
852  }
854 
856 
859  static void LUFactor3x3(float A[3][3], int index[3]);
860  static void LUFactor3x3(double A[3][3], int index[3]);
862 
864 
867  static void LUSolve3x3(const float A[3][3], const int index[3], float x[3]);
868  static void LUSolve3x3(const double A[3][3], const int index[3], double x[3]);
870 
872 
876  static void LinearSolve3x3(const float A[3][3], const float x[3], float y[3]);
877  static void LinearSolve3x3(const double A[3][3], const double x[3], double y[3]);
879 
881 
884  static void Multiply3x3(const float A[3][3], const float v[3], float u[3]);
885  static void Multiply3x3(const double A[3][3], const double v[3], double u[3]);
887 
889 
892  static void Multiply3x3(const float A[3][3], const float B[3][3], float C[3][3]);
893  static void Multiply3x3(const double A[3][3], const double B[3][3], double C[3][3]);
895 
919  template <int RowsT, int MidDimT, int ColsT,
920  class LayoutT1 = vtkMatrixUtilities::Layout::Identity,
921  class LayoutT2 = vtkMatrixUtilities::Layout::Identity, class MatrixT1, class MatrixT2,
922  class MatrixT3>
923  static void MultiplyMatrix(const MatrixT1& M1, const MatrixT2& M2, MatrixT3&& M3)
924  {
925  vtkMathPrivate::MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2>::Compute(M1, M2, M3);
926  }
927 
948  template <int RowsT, int ColsT, class LayoutT = vtkMatrixUtilities::Layout::Identity,
949  class MatrixT, class VectorT1, class VectorT2>
950  static void MultiplyMatrixWithVector(const MatrixT& M, const VectorT1& X, VectorT2&& Y)
951  {
952  vtkMathPrivate::MultiplyMatrix<RowsT, ColsT, 1, LayoutT>::Compute(M, X, Y);
953  }
954 
960  template <class ScalarT, int SizeT, class VectorT1, class VectorT2>
961  static ScalarT Dot(const VectorT1& x, const VectorT2& y)
962  {
963  return vtkMathPrivate::ContractRowWithCol<ScalarT, 1, SizeT, 1, 0, 0,
964  vtkMatrixUtilities::Layout::Identity, vtkMatrixUtilities::Layout::Transpose>::Compute(x, y);
965  }
966 
983  template <int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity, class MatrixT>
985  const MatrixT& M)
986  {
987  return vtkMathPrivate::Determinant<SizeT, LayoutT>::Compute(M);
988  }
989 
1005  template <int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity, class MatrixT1,
1006  class MatrixT2>
1007  static void InvertMatrix(const MatrixT1& M1, MatrixT2&& M2)
1008  {
1009  vtkMathPrivate::InvertMatrix<SizeT, LayoutT>::Compute(M1, M2);
1010  }
1011 
1025  template <int RowsT, int ColsT, class LayoutT = vtkMatrixUtilities::Layout::Identity,
1026  class MatrixT, class VectorT1, class VectorT2>
1027  static void LinearSolve(const MatrixT& M, const VectorT1& x, VectorT2& y)
1028  {
1029  vtkMathPrivate::LinearSolve<RowsT, ColsT, LayoutT>::Compute(M, x, y);
1030  }
1031 
1046  template <class ScalarT, int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity,
1047  class VectorT1, class MatrixT, class VectorT2>
1048  static ScalarT Dot(const VectorT1& x, const MatrixT& M, const VectorT2& y)
1049  {
1050  ScalarT tmp[SizeT];
1051  vtkMathPrivate::MultiplyMatrix<SizeT, SizeT, 1, LayoutT>::Compute(M, y, tmp);
1052  return vtkMathPrivate::ContractRowWithCol<ScalarT, 1, SizeT, 1, 0, 0,
1053  vtkMatrixUtilities::Layout::Identity, vtkMatrixUtilities::Layout::Transpose>::Compute(x, tmp);
1054  }
1055 
1061  static void MultiplyMatrix(const double* const* A, const double* const* B, unsigned int rowA,
1062  unsigned int colA, unsigned int rowB, unsigned int colB, double** C);
1063 
1065 
1069  static void Transpose3x3(const float A[3][3], float AT[3][3]);
1070  static void Transpose3x3(const double A[3][3], double AT[3][3]);
1072 
1074 
1078  static void Invert3x3(const float A[3][3], float AI[3][3]);
1079  static void Invert3x3(const double A[3][3], double AI[3][3]);
1081 
1083 
1086  static void Identity3x3(float A[3][3]);
1087  static void Identity3x3(double A[3][3]);
1089 
1091 
1094  static double Determinant3x3(const float A[3][3]);
1095  static double Determinant3x3(const double A[3][3]);
1097 
1101  static float Determinant3x3(const float c1[3], const float c2[3], const float c3[3]);
1102 
1106  static double Determinant3x3(const double c1[3], const double c2[3], const double c3[3]);
1107 
1114  static double Determinant3x3(double a1, double a2, double a3, double b1, double b2, double b3,
1115  double c1, double c2, double c3);
1116 
1118 
1125  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
1126  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
1127  template <class QuaternionT, class MatrixT,
1128  class EnableT = typename std::enable_if<!vtkMatrixUtilities::MatrixIs2DArray<MatrixT>()>::type>
1129  static void QuaternionToMatrix3x3(const QuaternionT& q, MatrixT&& A);
1131 
1133 
1142  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
1143  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
1144  template <class MatrixT, class QuaternionT,
1145  class EnableT = typename std::enable_if<!vtkMatrixUtilities::MatrixIs2DArray<MatrixT>()>::type>
1146  static void Matrix3x3ToQuaternion(const MatrixT& A, QuaternionT&& q);
1148 
1150 
1156  static void MultiplyQuaternion(const float q1[4], const float q2[4], float q[4]);
1157  static void MultiplyQuaternion(const double q1[4], const double q2[4], double q[4]);
1159 
1161 
1165  static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3]);
1166  static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3]);
1168 
1170 
1174  static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3]);
1175  static void RotateVectorByWXYZ(const double v[3], const double q[4], double r[3]);
1177 
1179 
1184  static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
1185  static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
1187 
1189 
1195  static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
1196  static void Diagonalize3x3(const double A[3][3], double w[3], double V[3][3]);
1198 
1200 
1210  const float A[3][3], float U[3][3], float w[3], float VT[3][3]);
1212  const double A[3][3], double U[3][3], double w[3], double VT[3][3]);
1214 
1223  double a00, double a01, double a10, double a11, double b0, double b1, double& x0, double& x1);
1224 
1233  static vtkTypeBool SolveLinearSystem(double** A, double* x, int size);
1234 
1241  static vtkTypeBool InvertMatrix(double** A, double** AI, int size);
1242 
1249  double** A, double** AI, int size, int* tmp1Size, double* tmp2Size);
1250 
1273  static vtkTypeBool LUFactorLinearSystem(double** A, int* index, int size);
1274 
1280  static vtkTypeBool LUFactorLinearSystem(double** A, int* index, int size, double* tmpSize);
1281 
1290  static void LUSolveLinearSystem(double** A, int* index, double* x, int size);
1291 
1300  static double EstimateMatrixCondition(const double* const* A, int size);
1301 
1303 
1311  static vtkTypeBool Jacobi(float** a, float* w, float** v);
1312  static vtkTypeBool Jacobi(double** a, double* w, double** v);
1314 
1316 
1325  static vtkTypeBool JacobiN(float** a, int n, float* w, float** v);
1326  static vtkTypeBool JacobiN(double** a, int n, double* w, double** v);
1328 
1343  int numberOfSamples, double** xt, int xOrder, double** mt);
1344 
1359  static vtkTypeBool SolveLeastSquares(int numberOfSamples, double** xt, int xOrder, double** yt,
1360  int yOrder, double** mt, int checkHomogeneous = 1);
1361 
1363 
1370  static void RGBToHSV(const float rgb[3], float hsv[3])
1371  {
1372  RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
1373  }
1374  static void RGBToHSV(float r, float g, float b, float* h, float* s, float* v);
1375  static void RGBToHSV(const double rgb[3], double hsv[3])
1376  {
1377  RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
1378  }
1379  static void RGBToHSV(double r, double g, double b, double* h, double* s, double* v);
1381 
1383 
1390  static void HSVToRGB(const float hsv[3], float rgb[3])
1391  {
1392  HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
1393  }
1394  static void HSVToRGB(float h, float s, float v, float* r, float* g, float* b);
1395  static void HSVToRGB(const double hsv[3], double rgb[3])
1396  {
1397  HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
1398  }
1399  static void HSVToRGB(double h, double s, double v, double* r, double* g, double* b);
1401 
1403 
1406  static void LabToXYZ(const double lab[3], double xyz[3])
1407  {
1408  LabToXYZ(lab[0], lab[1], lab[2], xyz + 0, xyz + 1, xyz + 2);
1409  }
1410  static void LabToXYZ(double L, double a, double b, double* x, double* y, double* z);
1412 
1414 
1417  static void XYZToLab(const double xyz[3], double lab[3])
1418  {
1419  XYZToLab(xyz[0], xyz[1], xyz[2], lab + 0, lab + 1, lab + 2);
1420  }
1421  static void XYZToLab(double x, double y, double z, double* L, double* a, double* b);
1423 
1425 
1428  static void XYZToRGB(const double xyz[3], double rgb[3])
1429  {
1430  XYZToRGB(xyz[0], xyz[1], xyz[2], rgb + 0, rgb + 1, rgb + 2);
1431  }
1432  static void XYZToRGB(double x, double y, double z, double* r, double* g, double* b);
1434 
1436 
1439  static void RGBToXYZ(const double rgb[3], double xyz[3])
1440  {
1441  RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz + 0, xyz + 1, xyz + 2);
1442  }
1443  static void RGBToXYZ(double r, double g, double b, double* x, double* y, double* z);
1445 
1447 
1453  static void RGBToLab(const double rgb[3], double lab[3])
1454  {
1455  RGBToLab(rgb[0], rgb[1], rgb[2], lab + 0, lab + 1, lab + 2);
1456  }
1457  static void RGBToLab(double red, double green, double blue, double* L, double* a, double* b);
1459 
1461 
1464  static void LabToRGB(const double lab[3], double rgb[3])
1465  {
1466  LabToRGB(lab[0], lab[1], lab[2], rgb + 0, rgb + 1, rgb + 2);
1467  }
1468  static void LabToRGB(double L, double a, double b, double* red, double* green, double* blue);
1470 
1472 
1475  static void UninitializeBounds(double bounds[6])
1476  {
1477  bounds[0] = 1.0;
1478  bounds[1] = -1.0;
1479  bounds[2] = 1.0;
1480  bounds[3] = -1.0;
1481  bounds[4] = 1.0;
1482  bounds[5] = -1.0;
1483  }
1485 
1487 
1490  static vtkTypeBool AreBoundsInitialized(const double bounds[6])
1491  {
1492  if (bounds[1] - bounds[0] < 0.0)
1493  {
1494  return 0;
1495  }
1496  return 1;
1497  }
1499 
1504  template <class T>
1505  static T ClampValue(const T& value, const T& min, const T& max);
1506 
1508 
1512  static void ClampValue(double* value, const double range[2]);
1513  static void ClampValue(double value, const double range[2], double* clamped_value);
1514  static void ClampValues(double* values, int nb_values, const double range[2]);
1515  static void ClampValues(
1516  const double* values, int nb_values, const double range[2], double* clamped_values);
1518 
1525  static double ClampAndNormalizeValue(double value, const double range[2]);
1526 
1531  template <class T1, class T2>
1532  static void TensorFromSymmetricTensor(const T1 symmTensor[6], T2 tensor[9]);
1533 
1539  template <class T>
1540  static void TensorFromSymmetricTensor(T tensor[9]);
1541 
1551  double range_min, double range_max, double scale = 1.0, double shift = 0.0);
1552 
1561  static vtkTypeBool GetAdjustedScalarRange(vtkDataArray* array, int comp, double range[2]);
1562 
1567  static vtkTypeBool ExtentIsWithinOtherExtent(const int extent1[6], const int extent2[6]);
1568 
1575  const double bounds1[6], const double bounds2[6], const double delta[3]);
1576 
1583  const double point[3], const double bounds[6], const double delta[3]);
1584 
1595  const double bounds[6], const double normal[3], const double point[3]);
1596 
1606  static double Solve3PointCircle(
1607  const double p1[3], const double p2[3], const double p3[3], double center[3]);
1608 
1612  static double Inf();
1613 
1617  static double NegInf();
1618 
1622  static double Nan();
1623 
1627  static vtkTypeBool IsInf(double x);
1628 
1632  static vtkTypeBool IsNan(double x);
1633 
1638  static bool IsFinite(double x);
1639 
1644  static int QuadraticRoot(double a, double b, double c, double min, double max, double* u);
1645 
1646 protected:
1647  vtkMath() = default;
1648  ~vtkMath() override = default;
1649 
1651 
1652 private:
1653  vtkMath(const vtkMath&) = delete;
1654  void operator=(const vtkMath&) = delete;
1655 };
1656 
1657 //----------------------------------------------------------------------------
1658 inline float vtkMath::RadiansFromDegrees(float x)
1659 {
1660  return x * 0.017453292f;
1661 }
1662 
1663 //----------------------------------------------------------------------------
1664 inline double vtkMath::RadiansFromDegrees(double x)
1665 {
1666  return x * 0.017453292519943295;
1667 }
1668 
1669 //----------------------------------------------------------------------------
1670 inline float vtkMath::DegreesFromRadians(float x)
1671 {
1672  return x * 57.2957795131f;
1673 }
1674 
1675 //----------------------------------------------------------------------------
1676 inline double vtkMath::DegreesFromRadians(double x)
1677 {
1678  return x * 57.29577951308232;
1679 }
1680 
1681 //----------------------------------------------------------------------------
1682 inline bool vtkMath::IsPowerOfTwo(vtkTypeUInt64 x)
1683 {
1684  return ((x != 0) & ((x & (x - 1)) == 0));
1685 }
1686 
1687 //----------------------------------------------------------------------------
1688 // Credit goes to Peter Hart and William Lewis on comp.lang.python 1997
1690 {
1691  unsigned int z = static_cast<unsigned int>(((x > 0) ? x - 1 : 0));
1692  z |= z >> 1;
1693  z |= z >> 2;
1694  z |= z >> 4;
1695  z |= z >> 8;
1696  z |= z >> 16;
1697  return static_cast<int>(z + 1);
1698 }
1699 
1700 //----------------------------------------------------------------------------
1701 // Modify the trunc() operation provided by static_cast<int>() to get floor(),
1702 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1703 inline int vtkMath::Floor(double x)
1704 {
1705  int i = static_cast<int>(x);
1706  return i - (i > x);
1707 }
1708 
1709 //----------------------------------------------------------------------------
1710 // Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1711 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1712 inline int vtkMath::Ceil(double x)
1713 {
1714  int i = static_cast<int>(x);
1715  return i + (i < x);
1716 }
1717 
1718 //----------------------------------------------------------------------------
1719 template <class T>
1720 inline T vtkMath::Min(const T& a, const T& b)
1721 {
1722  return (b <= a ? b : a);
1723 }
1724 
1725 //----------------------------------------------------------------------------
1726 template <class T>
1727 inline T vtkMath::Max(const T& a, const T& b)
1728 {
1729  return (b > a ? b : a);
1730 }
1731 
1732 //----------------------------------------------------------------------------
1733 inline float vtkMath::Normalize(float v[3])
1734 {
1735  float den = vtkMath::Norm(v);
1736  if (den != 0.0)
1737  {
1738  for (int i = 0; i < 3; ++i)
1739  {
1740  v[i] /= den;
1741  }
1742  }
1743  return den;
1744 }
1745 
1746 //----------------------------------------------------------------------------
1747 inline double vtkMath::Normalize(double v[3])
1748 {
1749  double den = vtkMath::Norm(v);
1750  if (den != 0.0)
1751  {
1752  for (int i = 0; i < 3; ++i)
1753  {
1754  v[i] /= den;
1755  }
1756  }
1757  return den;
1758 }
1759 
1760 //----------------------------------------------------------------------------
1761 inline float vtkMath::Normalize2D(float v[2])
1762 {
1763  float den = vtkMath::Norm2D(v);
1764  if (den != 0.0)
1765  {
1766  for (int i = 0; i < 2; ++i)
1767  {
1768  v[i] /= den;
1769  }
1770  }
1771  return den;
1772 }
1773 
1774 //----------------------------------------------------------------------------
1775 inline double vtkMath::Normalize2D(double v[2])
1776 {
1777  double den = vtkMath::Norm2D(v);
1778  if (den != 0.0)
1779  {
1780  for (int i = 0; i < 2; ++i)
1781  {
1782  v[i] /= den;
1783  }
1784  }
1785  return den;
1786 }
1787 
1788 //----------------------------------------------------------------------------
1789 inline float vtkMath::Determinant3x3(const float c1[3], const float c2[3], const float c3[3])
1790 {
1791  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1792  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1793 }
1794 
1795 //----------------------------------------------------------------------------
1796 inline double vtkMath::Determinant3x3(const double c1[3], const double c2[3], const double c3[3])
1797 {
1798  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1799  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1800 }
1801 
1802 //----------------------------------------------------------------------------
1804  double a1, double a2, double a3, double b1, double b2, double b3, double c1, double c2, double c3)
1805 {
1806  return (a1 * vtkMath::Determinant2x2(b2, b3, c2, c3) -
1807  b1 * vtkMath::Determinant2x2(a2, a3, c2, c3) + c1 * vtkMath::Determinant2x2(a2, a3, b2, b3));
1808 }
1809 
1810 //----------------------------------------------------------------------------
1811 inline float vtkMath::Distance2BetweenPoints(const float p1[3], const float p2[3])
1812 {
1813  return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1814  (p1[2] - p2[2]) * (p1[2] - p2[2]));
1815 }
1816 
1817 //----------------------------------------------------------------------------
1818 inline double vtkMath::Distance2BetweenPoints(const double p1[3], const double p2[3])
1819 {
1820  return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1821  (p1[2] - p2[2]) * (p1[2] - p2[2]));
1822 }
1823 
1824 //----------------------------------------------------------------------------
1825 template <typename ReturnTypeT, typename TupleRangeT1, typename TupleRangeT2, typename EnableT>
1826 inline ReturnTypeT vtkMath::Distance2BetweenPoints(const TupleRangeT1& p1, const TupleRangeT2& p2)
1827 {
1828  return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1829  (p1[2] - p2[2]) * (p1[2] - p2[2]));
1830 }
1831 
1832 //----------------------------------------------------------------------------
1833 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1834 inline void vtkMath::Cross(const float a[3], const float b[3], float c[3])
1835 {
1836  float Cx = a[1] * b[2] - a[2] * b[1];
1837  float Cy = a[2] * b[0] - a[0] * b[2];
1838  float Cz = a[0] * b[1] - a[1] * b[0];
1839  c[0] = Cx;
1840  c[1] = Cy;
1841  c[2] = Cz;
1842 }
1843 
1844 //----------------------------------------------------------------------------
1845 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1846 inline void vtkMath::Cross(const double a[3], const double b[3], double c[3])
1847 {
1848  double Cx = a[1] * b[2] - a[2] * b[1];
1849  double Cy = a[2] * b[0] - a[0] * b[2];
1850  double Cz = a[0] * b[1] - a[1] * b[0];
1851  c[0] = Cx;
1852  c[1] = Cy;
1853  c[2] = Cz;
1854 }
1855 
1856 //----------------------------------------------------------------------------
1857 template <class T>
1858 inline double vtkDeterminant3x3(const T A[3][3])
1859 {
1860  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] -
1861  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];
1862 }
1863 
1864 //----------------------------------------------------------------------------
1865 inline double vtkMath::Determinant3x3(const float A[3][3])
1866 {
1867  return vtkDeterminant3x3(A);
1868 }
1869 
1870 //----------------------------------------------------------------------------
1871 inline double vtkMath::Determinant3x3(const double A[3][3])
1872 {
1873  return vtkDeterminant3x3(A);
1874 }
1875 
1876 //----------------------------------------------------------------------------
1877 template <class T>
1878 inline T vtkMath::ClampValue(const T& value, const T& min, const T& max)
1879 {
1880  assert("pre: valid_range" && min <= max);
1881 
1882 #if __cplusplus >= 201703L
1883  return std::clamp(value, min, max);
1884 #else
1885  // compilers are good at optimizing the ternary operator,
1886  // use '<' since it is preferred by STL for custom types
1887  T v = (min < value ? value : min);
1888  return (v < max ? v : max);
1889 #endif
1890 }
1891 
1892 //----------------------------------------------------------------------------
1893 inline void vtkMath::ClampValue(double* value, const double range[2])
1894 {
1895  if (value && range)
1896  {
1897  assert("pre: valid_range" && range[0] <= range[1]);
1898 
1899  *value = vtkMath::ClampValue(*value, range[0], range[1]);
1900  }
1901 }
1902 
1903 //----------------------------------------------------------------------------
1904 inline void vtkMath::ClampValue(double value, const double range[2], double* clamped_value)
1905 {
1906  if (range && clamped_value)
1907  {
1908  assert("pre: valid_range" && range[0] <= range[1]);
1909 
1910  *clamped_value = vtkMath::ClampValue(value, range[0], range[1]);
1911  }
1912 }
1913 
1914 // ---------------------------------------------------------------------------
1915 inline double vtkMath::ClampAndNormalizeValue(double value, const double range[2])
1916 {
1917  assert("pre: valid_range" && range[0] <= range[1]);
1918 
1919  double result;
1920  if (range[0] == range[1])
1921  {
1922  result = 0.0;
1923  }
1924  else
1925  {
1926  // clamp
1927  result = vtkMath::ClampValue(value, range[0], range[1]);
1928 
1929  // normalize
1930  result = (result - range[0]) / (range[1] - range[0]);
1931  }
1932 
1933  assert("post: valid_result" && result >= 0.0 && result <= 1.0);
1934 
1935  return result;
1936 }
1937 
1938 //-----------------------------------------------------------------------------
1939 template <class T1, class T2>
1940 inline void vtkMath::TensorFromSymmetricTensor(const T1 symmTensor[9], T2 tensor[9])
1941 {
1942  for (int i = 0; i < 3; ++i)
1943  {
1944  tensor[4 * i] = symmTensor[i];
1945  }
1946  tensor[1] = tensor[3] = symmTensor[3];
1947  tensor[2] = tensor[6] = symmTensor[5];
1948  tensor[5] = tensor[7] = symmTensor[4];
1949 }
1950 
1951 //-----------------------------------------------------------------------------
1952 template <class T>
1953 inline void vtkMath::TensorFromSymmetricTensor(T tensor[9])
1954 {
1955  tensor[6] = tensor[5]; // XZ
1956  tensor[7] = tensor[4]; // YZ
1957  tensor[8] = tensor[2]; // ZZ
1958  tensor[4] = tensor[1]; // YY
1959  tensor[5] = tensor[7]; // YZ
1960  tensor[2] = tensor[6]; // XZ
1961  tensor[1] = tensor[3]; // XY
1962 }
1963 
1964 namespace
1965 {
1966 template <class QuaternionT, class MatrixT>
1967 inline void vtkQuaternionToMatrix3x3(const QuaternionT& quat, MatrixT& A)
1968 {
1970 
1971  Scalar ww = quat[0] * quat[0];
1972  Scalar wx = quat[0] * quat[1];
1973  Scalar wy = quat[0] * quat[2];
1974  Scalar wz = quat[0] * quat[3];
1975 
1976  Scalar xx = quat[1] * quat[1];
1977  Scalar yy = quat[2] * quat[2];
1978  Scalar zz = quat[3] * quat[3];
1979 
1980  Scalar xy = quat[1] * quat[2];
1981  Scalar xz = quat[1] * quat[3];
1982  Scalar yz = quat[2] * quat[3];
1983 
1984  Scalar rr = xx + yy + zz;
1985  // normalization factor, just in case quaternion was not normalized
1986  Scalar f = 1 / (ww + rr);
1987  Scalar s = (ww - rr) * f;
1988  f *= 2;
1989 
1991 
1992  Wrapper::template Get<0, 0>(A) = xx * f + s;
1993  Wrapper::template Get<1, 0>(A) = (xy + wz) * f;
1994  Wrapper::template Get<2, 0>(A) = (xz - wy) * f;
1995 
1996  Wrapper::template Get<0, 1>(A) = (xy - wz) * f;
1997  Wrapper::template Get<1, 1>(A) = yy * f + s;
1998  Wrapper::template Get<2, 1>(A) = (yz + wx) * f;
1999 
2000  Wrapper::template Get<0, 2>(A) = (xz + wy) * f;
2001  Wrapper::template Get<1, 2>(A) = (yz - wx) * f;
2002  Wrapper::template Get<2, 2>(A) = zz * f + s;
2003 }
2004 } // anonymous namespace
2005 
2006 //------------------------------------------------------------------------------
2007 inline void vtkMath::QuaternionToMatrix3x3(const float quat[4], float A[3][3])
2008 {
2009  vtkQuaternionToMatrix3x3(quat, A);
2010 }
2011 
2012 //------------------------------------------------------------------------------
2013 inline void vtkMath::QuaternionToMatrix3x3(const double quat[4], double A[3][3])
2014 {
2015  vtkQuaternionToMatrix3x3(quat, A);
2016 }
2017 
2018 //-----------------------------------------------------------------------------
2019 template <class QuaternionT, class MatrixT, class EnableT>
2020 inline void vtkMath::QuaternionToMatrix3x3(const QuaternionT& q, MatrixT&& A)
2021 {
2022  vtkQuaternionToMatrix3x3(q, A);
2023 }
2024 
2025 namespace
2026 {
2027 //------------------------------------------------------------------------------
2028 // The solution is based on
2029 // Berthold K. P. Horn (1987),
2030 // "Closed-form solution of absolute orientation using unit quaternions,"
2031 // Journal of the Optical Society of America A, 4:629-642
2032 template <class MatrixT, class QuaternionT>
2033 inline void vtkMatrix3x3ToQuaternion(const MatrixT& A, QuaternionT& quat)
2034 {
2036 
2037  Scalar N[4][4];
2038 
2040 
2041  // on-diagonal elements
2042  N[0][0] = Wrapper::template Get<0, 0>(A) + Wrapper::template Get<1, 1>(A) +
2043  Wrapper::template Get<2, 2>(A);
2044  N[1][1] = Wrapper::template Get<0, 0>(A) - Wrapper::template Get<1, 1>(A) -
2045  Wrapper::template Get<2, 2>(A);
2046  N[2][2] = -Wrapper::template Get<0, 0>(A) + Wrapper::template Get<1, 1>(A) -
2047  Wrapper::template Get<2, 2>(A);
2048  N[3][3] = -Wrapper::template Get<0, 0>(A) - Wrapper::template Get<1, 1>(A) +
2049  Wrapper::template Get<2, 2>(A);
2050 
2051  // off-diagonal elements
2052  N[0][1] = N[1][0] = Wrapper::template Get<2, 1>(A) - Wrapper::template Get<1, 2>(A);
2053  N[0][2] = N[2][0] = Wrapper::template Get<0, 2>(A) - Wrapper::template Get<2, 0>(A);
2054  N[0][3] = N[3][0] = Wrapper::template Get<1, 0>(A) - Wrapper::template Get<0, 1>(A);
2055 
2056  N[1][2] = N[2][1] = Wrapper::template Get<1, 0>(A) + Wrapper::template Get<0, 1>(A);
2057  N[1][3] = N[3][1] = Wrapper::template Get<0, 2>(A) + Wrapper::template Get<2, 0>(A);
2058  N[2][3] = N[3][2] = Wrapper::template Get<2, 1>(A) + Wrapper::template Get<1, 2>(A);
2059 
2060  Scalar eigenvectors[4][4], eigenvalues[4];
2061 
2062  // convert into format that JacobiN can use,
2063  // then use Jacobi to find eigenvalues and eigenvectors
2064  Scalar *NTemp[4], *eigenvectorsTemp[4];
2065  for (int i = 0; i < 4; ++i)
2066  {
2067  NTemp[i] = N[i];
2068  eigenvectorsTemp[i] = eigenvectors[i];
2069  }
2070  vtkMath::JacobiN(NTemp, 4, eigenvalues, eigenvectorsTemp);
2071 
2072  // the first eigenvector is the one we want
2073  quat[0] = eigenvectors[0][0];
2074  quat[1] = eigenvectors[1][0];
2075  quat[2] = eigenvectors[2][0];
2076  quat[3] = eigenvectors[3][0];
2077 }
2078 } // anonymous namespace
2079 
2080 //------------------------------------------------------------------------------
2081 inline void vtkMath::Matrix3x3ToQuaternion(const float A[3][3], float quat[4])
2082 {
2083  vtkMatrix3x3ToQuaternion(A, quat);
2084 }
2085 
2086 //------------------------------------------------------------------------------
2087 inline void vtkMath::Matrix3x3ToQuaternion(const double A[3][3], double quat[4])
2088 {
2089  vtkMatrix3x3ToQuaternion(A, quat);
2090 }
2091 
2092 //-----------------------------------------------------------------------------
2093 template <class MatrixT, class QuaternionT, class EnableT>
2094 inline void vtkMath::Matrix3x3ToQuaternion(const MatrixT& A, QuaternionT&& q)
2095 {
2096  vtkMatrix3x3ToQuaternion(A, q);
2097 }
2098 
2099 namespace vtk_detail
2100 {
2101 // Can't specialize templates inside a template class, so we move the impl here.
2102 template <typename OutT>
2103 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
2104 { // OutT is integral -- clamp and round
2105  if (!vtkMath::IsNan(val))
2106  {
2107  double min = static_cast<double>(vtkTypeTraits<OutT>::Min());
2108  double max = static_cast<double>(vtkTypeTraits<OutT>::Max());
2109  val = vtkMath::ClampValue(val, min, max);
2110  *ret = static_cast<OutT>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
2111  }
2112  else
2113  *ret = 0;
2114 }
2115 template <>
2116 inline void RoundDoubleToIntegralIfNecessary(double val, double* retVal)
2117 { // OutT is double: passthrough
2118  *retVal = val;
2119 }
2120 template <>
2121 inline void RoundDoubleToIntegralIfNecessary(double val, float* retVal)
2122 { // OutT is float -- just clamp (as doubles, then the cast to float is well-defined.)
2123  if (!vtkMath::IsNan(val))
2124  {
2125  double min = static_cast<double>(vtkTypeTraits<float>::Min());
2126  double max = static_cast<double>(vtkTypeTraits<float>::Max());
2127  val = vtkMath::ClampValue(val, min, max);
2128  }
2129 
2130  *retVal = static_cast<float>(val);
2131 }
2132 } // end namespace vtk_detail
2133 
2134 //-----------------------------------------------------------------------------
2135 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
2136 #define VTK_MATH_ISINF_IS_INLINE
2137 inline vtkTypeBool vtkMath::IsInf(double x)
2138 {
2139 #if defined(VTK_HAS_STD_ISINF)
2140  return std::isinf(x);
2141 #else
2142  return (isinf(x) != 0); // Force conversion to bool
2143 #endif
2144 }
2145 #endif
2146 
2147 //-----------------------------------------------------------------------------
2148 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
2149 #define VTK_MATH_ISNAN_IS_INLINE
2150 inline vtkTypeBool vtkMath::IsNan(double x)
2151 {
2152 #if defined(VTK_HAS_STD_ISNAN)
2153  return std::isnan(x);
2154 #else
2155  return (isnan(x) != 0); // Force conversion to bool
2156 #endif
2157 }
2158 #endif
2159 
2160 //-----------------------------------------------------------------------------
2161 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
2162 #define VTK_MATH_ISFINITE_IS_INLINE
2163 inline bool vtkMath::IsFinite(double x)
2164 {
2165 #if defined(VTK_HAS_STD_ISFINITE)
2166  return std::isfinite(x);
2167 #elif defined(VTK_HAS_ISFINITE)
2168  return (isfinite(x) != 0); // Force conversion to bool
2169 #else
2170  return (finite(x) != 0); // Force conversion to bool
2171 #endif
2172 }
2173 #endif
2174 
2175 #endif
Gaussian sequence of pseudo random numbers implemented with the Box-Mueller transform.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:159
vtkFrustumSelector is a vtkSelector that selects elements based on whether they are inside or interse...
a simple class to control print indentation
Definition: vtkIndent.h:113
performs common math operations
Definition: vtkMath.h:197
static ReturnTypeT Distance2BetweenPoints(const TupleRangeT1 &p1, const TupleRangeT2 &p2)
Compute distance squared between two points p1 and p2.
Definition: vtkMath.h:1826
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:554
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:228
static int * BeginCombination(int m, int n)
Start iterating over "m choose n" objects.
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:950
static double Norm2D(const double x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:822
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:463
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:787
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:1650
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:452
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:634
static ReturnTypeT Dot(const TupleRangeT1 &a, const TupleRangeT2 &b)
Compute dot product between two points p1 and p2.
Definition: vtkMath.h:578
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:1417
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:1027
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:1406
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:546
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 vtkMatrixUtilities::ScalarTypeExtractor< MatrixT >::value_type Determinant(const MatrixT &M)
Computes the determinant of input square SizeT x SizeT matrix M.
Definition: vtkMath.h:984
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:1834
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
Definition: vtkMath.h:511
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:1390
static void Assign(const double a[3], double b[3])
Assign values to a 3-vector (double version).
Definition: vtkMath.h:436
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:849
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition: vtkMath.h:1727
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:801
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:535
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:1712
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:1395
~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:961
static double Inf()
Special IEEE-754 number used to represent positive infinity.
static vtkMath * New()
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:1439
static void QuaternionToMatrix3x3(const float quat[4], float A[3][3])
Convert a quaternion to a 3x3 rotation matrix.
Definition: vtkMath.h:2007
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1689
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:1761
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:600
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:426
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
Definition: vtkMath.h:1658
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:441
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:1490
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 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:206
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:474
static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4])
Convert a 3x3 matrix into a quaternion.
Definition: vtkMath.h:2081
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:1428
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:1915
static void MultiplyScalar(double a[3], double s)
Multiplies a 3-vector by a scalar (double version).
Definition: vtkMath.h:523
static double Dot2D(const double x[2], const double y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:782
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:923
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:1453
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
Definition: vtkMath.h:1703
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:488
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:848
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:1375
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:1670
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
Definition: vtkMath.h:839
static int Round(double f)
Definition: vtkMath.h:229
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:1475
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:586
static double Norm(const double v[3])
Compute the norm of 3-vector (double version).
Definition: vtkMath.h:639
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:237
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:1682
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:1733
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:654
static double Determinant3x3(const float A[3][3])
Return the determinant of a 3x3 matrix.
Definition: vtkMath.h:1865
static float Dot2D(const float x[2], const float y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:777
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:1048
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:1370
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:1464
static float Norm2D(const float x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:816
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:1007
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
Definition: vtkMath.h:499
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1720
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:1878
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.
Matrix wrapping class.
Park and Miller Sequence of pseudo random numbers.
abstract base class for most VTK objects
Definition: vtkObject.h:73
represent and manipulate 3D points
Definition: vtkPoints.h:143
@ point
Definition: vtkX3D.h:242
@ value
Definition: vtkX3D.h:226
@ scale
Definition: vtkX3D.h:235
@ range
Definition: vtkX3D.h:244
@ center
Definition: vtkX3D.h:236
@ type
Definition: vtkX3D.h:522
@ position
Definition: vtkX3D.h:267
@ size
Definition: vtkX3D.h:259
@ index
Definition: vtkX3D.h:252
void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
Definition: vtkMath.h:2103
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.
Definition: vtkTypeTraits.h:30
int vtkTypeBool
Definition: vtkABI.h:69
double vtkDeterminant3x3(const T A[3][3])
Definition: vtkMath.h:1858
#define max(a, b)