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 =========================================================================*/
39 #ifndef vtkMath_h
40 #define vtkMath_h
41 
42 #include "vtkCommonCoreModule.h" // For export macro
43 #include "vtkMathPrivate.hxx" // For Matrix meta-class helpers
44 #include "vtkMatrixUtilities.h" // For Matrix wrapping / mapping
45 #include "vtkObject.h"
46 #include "vtkSmartPointer.h" // For vtkSmartPointer.
47 #include "vtkTypeTraits.h" // For type traits
48 
49 #include "vtkMathConfigure.h" // For <cmath> and VTK_HAS_ISNAN etc.
50 
51 #include <algorithm> // for std::clamp
52 #include <cassert> // assert() in inline implementations.
53 
54 #ifndef DBL_MIN
55 #define VTK_DBL_MIN 2.2250738585072014e-308
56 #else // DBL_MIN
57 #define VTK_DBL_MIN DBL_MIN
58 #endif // DBL_MIN
59 
60 #ifndef DBL_EPSILON
61 #define VTK_DBL_EPSILON 2.2204460492503131e-16
62 #else // DBL_EPSILON
63 #define VTK_DBL_EPSILON DBL_EPSILON
64 #endif // DBL_EPSILON
65 
66 #ifndef VTK_DBL_EPSILON
67 #ifndef DBL_EPSILON
68 #define VTK_DBL_EPSILON 2.2204460492503131e-16
69 #else // DBL_EPSILON
70 #define VTK_DBL_EPSILON DBL_EPSILON
71 #endif // DBL_EPSILON
72 #endif // VTK_DBL_EPSILON
73 
74 class vtkDataArray;
75 class vtkPoints;
76 class vtkMathInternal;
79 
80 namespace vtk_detail
81 {
82 // forward declaration
83 template <typename OutT>
84 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret);
85 } // end namespace vtk_detail
86 
87 class VTKCOMMONCORE_EXPORT vtkMath : public vtkObject
88 {
89 public:
90  static vtkMath* New();
91  vtkTypeMacro(vtkMath, vtkObject);
92  void PrintSelf(ostream& os, vtkIndent indent) override;
93 
97  static constexpr double Pi() { return 3.141592653589793; }
98 
100 
103  static float RadiansFromDegrees(float degrees);
104  static double RadiansFromDegrees(double degrees);
106 
108 
111  static float DegreesFromRadians(float radians);
112  static double DegreesFromRadians(double radians);
114 
118 #if 1
119  static int Round(float f) { return static_cast<int>(f + (f >= 0.0 ? 0.5 : -0.5)); }
120  static int Round(double f) { return static_cast<int>(f + (f >= 0.0 ? 0.5 : -0.5)); }
121 #endif
122 
127  template <typename OutT>
128  static void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
129  {
130  // Can't specialize template methods in a template class, so we move the
131  // implementations to a external namespace.
133  }
134 
140  static int Floor(double x);
141 
147  static int Ceil(double x);
148 
154  static int CeilLog2(vtkTypeUInt64 x);
155 
160  template <class T>
161  static T Min(const T& a, const T& b);
162 
167  template <class T>
168  static T Max(const T& a, const T& b);
169 
173  static bool IsPowerOfTwo(vtkTypeUInt64 x);
174 
180  static int NearestPowerOfTwo(int x);
181 
186  static vtkTypeInt64 Factorial(int N);
187 
193  static vtkTypeInt64 Binomial(int m, int n);
194 
206  static int* BeginCombination(int m, int n);
207 
218  static int NextCombination(int m, int n, int* combination);
219 
223  static void FreeCombination(int* combination);
224 
240  static void RandomSeed(int s);
241 
253  static int GetSeed();
254 
268  static double Random();
269 
282  static double Random(double min, double max);
283 
296  static double Gaussian();
297 
310  static double Gaussian(double mean, double std);
311 
316  template <class VectorT1, class VectorT2>
317  static void Assign(const VectorT1& a, VectorT2&& b)
318  {
319  b[0] = a[0];
320  b[1] = a[1];
321  b[2] = a[2];
322  }
323 
327  static void Assign(const double a[3], double b[3]) { vtkMath::Assign<>(a, b); }
328 
332  static void Add(const float a[3], const float b[3], float c[3])
333  {
334  for (int i = 0; i < 3; ++i)
335  {
336  c[i] = a[i] + b[i];
337  }
338  }
339 
343  static void Add(const double a[3], const double b[3], double c[3])
344  {
345  for (int i = 0; i < 3; ++i)
346  {
347  c[i] = a[i] + b[i];
348  }
349  }
350 
354  static void Subtract(const float a[3], const float b[3], float c[3])
355  {
356  for (int i = 0; i < 3; ++i)
357  {
358  c[i] = a[i] - b[i];
359  }
360  }
361 
365  static void Subtract(const double a[3], const double b[3], double c[3])
366  {
367  for (int i = 0; i < 3; ++i)
368  {
369  c[i] = a[i] - b[i];
370  }
371  }
372 
378  template <class VectorT1, class VectorT2, class VectorT3>
379  static void Subtract(const VectorT1& a, const VectorT2& b, VectorT3&& c)
380  {
381  c[0] = a[0] - b[0];
382  c[1] = a[1] - b[1];
383  c[2] = a[2] - b[2];
384  }
385 
390  static void MultiplyScalar(float a[3], float s)
391  {
392  for (int i = 0; i < 3; ++i)
393  {
394  a[i] *= s;
395  }
396  }
397 
402  static void MultiplyScalar2D(float a[2], float s)
403  {
404  for (int i = 0; i < 2; ++i)
405  {
406  a[i] *= s;
407  }
408  }
409 
414  static void MultiplyScalar(double a[3], double s)
415  {
416  for (int i = 0; i < 3; ++i)
417  {
418  a[i] *= s;
419  }
420  }
421 
426  static void MultiplyScalar2D(double a[2], double s)
427  {
428  for (int i = 0; i < 2; ++i)
429  {
430  a[i] *= s;
431  }
432  }
433 
437  static float Dot(const float a[3], const float b[3])
438  {
439  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
440  }
441 
445  static double Dot(const double a[3], const double b[3])
446  {
447  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
448  }
449 
465  template <typename ReturnTypeT = double, typename TupleRangeT1, typename TupleRangeT2,
466  typename EnableT = typename std::conditional<!std::is_pointer<TupleRangeT1>::value &&
468  TupleRangeT1, TupleRangeT2>::type::value_type>
469  static ReturnTypeT Dot(const TupleRangeT1& a, const TupleRangeT2& b)
470  {
471  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
472  }
473 
477  static void Outer(const float a[3], const float b[3], float c[3][3])
478  {
479  for (int i = 0; i < 3; ++i)
480  {
481  for (int j = 0; j < 3; ++j)
482  {
483  c[i][j] = a[i] * b[j];
484  }
485  }
486  }
487 
491  static void Outer(const double a[3], const double b[3], double c[3][3])
492  {
493  for (int i = 0; i < 3; ++i)
494  {
495  for (int j = 0; j < 3; ++j)
496  {
497  c[i][j] = a[i] * b[j];
498  }
499  }
500  }
501 
506  static void Cross(const float a[3], const float b[3], float c[3]);
507 
512  static void Cross(const double a[3], const double b[3], double c[3]);
513 
515 
518  static float Norm(const float* x, int n);
519  static double Norm(const double* x, int n);
521 
525  static float Norm(const float v[3]) { return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); }
526 
530  static double Norm(const double v[3])
531  {
532  return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
533  }
534 
544  template <typename ReturnTypeT = double, typename TupleRangeT>
545  static ReturnTypeT SquaredNorm(const TupleRangeT& v)
546  {
547  return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
548  }
549 
554  static float Normalize(float v[3]);
555 
560  static double Normalize(double v[3]);
561 
563 
570  static void Perpendiculars(const double v1[3], double v2[3], double v3[3], double theta);
571  static void Perpendiculars(const float v1[3], float v2[3], float v3[3], double theta);
573 
575 
580  static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
581  static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
583 
585 
591  static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
592  static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
594 
610  template <typename ReturnTypeT = double, typename TupleRangeT1, typename TupleRangeT2,
611  typename EnableT = typename std::conditional<!std::is_pointer<TupleRangeT1>::value &&
613  TupleRangeT1, TupleRangeT2>::type::value_type>
614  static ReturnTypeT Distance2BetweenPoints(const TupleRangeT1& p1, const TupleRangeT2& p2);
615 
620  static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
621 
626  static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
627 
631  static double AngleBetweenVectors(const double v1[3], const double v2[3]);
632 
637  const double v1[3], const double v2[3], const double vn[3]);
638 
643  static double GaussianAmplitude(const double variance, const double distanceFromMean);
644 
649  static double GaussianAmplitude(const double mean, const double variance, const double position);
650 
656  static double GaussianWeight(const double variance, const double distanceFromMean);
657 
663  static double GaussianWeight(const double mean, const double variance, const double position);
664 
668  static float Dot2D(const float x[2], const float y[2]) { return x[0] * y[0] + x[1] * y[1]; }
669 
673  static double Dot2D(const double x[2], const double y[2]) { return x[0] * y[0] + x[1] * y[1]; }
674 
678  static void Outer2D(const float x[2], const float y[2], float A[2][2])
679  {
680  for (int i = 0; i < 2; ++i)
681  {
682  for (int j = 0; j < 2; ++j)
683  {
684  A[i][j] = x[i] * y[j];
685  }
686  }
687  }
688 
692  static void Outer2D(const double x[2], const double y[2], double A[2][2])
693  {
694  for (int i = 0; i < 2; ++i)
695  {
696  for (int j = 0; j < 2; ++j)
697  {
698  A[i][j] = x[i] * y[j];
699  }
700  }
701  }
702 
707  static float Norm2D(const float x[2]) { return std::sqrt(x[0] * x[0] + x[1] * x[1]); }
708 
713  static double Norm2D(const double x[2]) { return std::sqrt(x[0] * x[0] + x[1] * x[1]); }
714 
719  static float Normalize2D(float v[2]);
720 
725  static double Normalize2D(double v[2]);
726 
730  static float Determinant2x2(const float c1[2], const float c2[2])
731  {
732  return c1[0] * c2[1] - c2[0] * c1[1];
733  }
734 
736 
739  static double Determinant2x2(double a, double b, double c, double d) { return a * d - b * c; }
740  static double Determinant2x2(const double c1[2], const double c2[2])
741  {
742  return c1[0] * c2[1] - c2[0] * c1[1];
743  }
745 
747 
750  static void LUFactor3x3(float A[3][3], int index[3]);
751  static void LUFactor3x3(double A[3][3], int index[3]);
753 
755 
758  static void LUSolve3x3(const float A[3][3], const int index[3], float x[3]);
759  static void LUSolve3x3(const double A[3][3], const int index[3], double x[3]);
761 
763 
767  static void LinearSolve3x3(const float A[3][3], const float x[3], float y[3]);
768  static void LinearSolve3x3(const double A[3][3], const double x[3], double y[3]);
770 
772 
775  static void Multiply3x3(const float A[3][3], const float v[3], float u[3]);
776  static void Multiply3x3(const double A[3][3], const double v[3], double u[3]);
778 
780 
783  static void Multiply3x3(const float A[3][3], const float B[3][3], float C[3][3]);
784  static void Multiply3x3(const double A[3][3], const double B[3][3], double C[3][3]);
786 
810  template <int RowsT, int MidDimT, int ColsT,
811  class LayoutT1 = vtkMatrixUtilities::Layout::Identity,
812  class LayoutT2 = vtkMatrixUtilities::Layout::Identity, class MatrixT1, class MatrixT2,
813  class MatrixT3>
814  static void MultiplyMatrix(const MatrixT1& M1, const MatrixT2& M2, MatrixT3&& M3)
815  {
816  vtkMathPrivate::MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2>::Compute(M1, M2, M3);
817  }
818 
839  template <int RowsT, int ColsT, class LayoutT = vtkMatrixUtilities::Layout::Identity,
840  class MatrixT, class VectorT1, class VectorT2>
841  static void MultiplyMatrixWithVector(const MatrixT& M, const VectorT1& X, VectorT2&& Y)
842  {
843  vtkMathPrivate::MultiplyMatrix<RowsT, ColsT, 1, LayoutT>::Compute(M, X, Y);
844  }
845 
851  template <class ScalarT, int SizeT, class VectorT1, class VectorT2>
852  static ScalarT Dot(const VectorT1& x, const VectorT2& y)
853  {
854  return vtkMathPrivate::ContractRowWithCol<ScalarT, 1, SizeT, 1, 0, 0,
855  vtkMatrixUtilities::Layout::Identity, vtkMatrixUtilities::Layout::Transpose>::Compute(x, y);
856  }
857 
874  template <int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity, class MatrixT>
876  const MatrixT& M)
877  {
878  return vtkMathPrivate::Determinant<SizeT, LayoutT>::Compute(M);
879  }
880 
896  template <int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity, class MatrixT1,
897  class MatrixT2>
898  static void InvertMatrix(const MatrixT1& M1, MatrixT2&& M2)
899  {
900  vtkMathPrivate::InvertMatrix<SizeT, LayoutT>::Compute(M1, M2);
901  }
902 
916  template <int RowsT, int ColsT, class LayoutT = vtkMatrixUtilities::Layout::Identity,
917  class MatrixT, class VectorT1, class VectorT2>
918  static void LinearSolve(const MatrixT& M, const VectorT1& x, VectorT2& y)
919  {
920  vtkMathPrivate::LinearSolve<RowsT, ColsT, LayoutT>::Compute(M, x, y);
921  }
922 
937  template <class ScalarT, int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity,
938  class VectorT1, class MatrixT, class VectorT2>
939  static ScalarT Dot(const VectorT1& x, const MatrixT& M, const VectorT2& y)
940  {
941  ScalarT tmp[SizeT];
942  vtkMathPrivate::MultiplyMatrix<SizeT, SizeT, 1, LayoutT>::Compute(M, y, tmp);
943  return vtkMathPrivate::ContractRowWithCol<ScalarT, 1, SizeT, 1, 0, 0,
944  vtkMatrixUtilities::Layout::Identity, vtkMatrixUtilities::Layout::Transpose>::Compute(x, tmp);
945  }
946 
952  static void MultiplyMatrix(const double* const* A, const double* const* B, unsigned int rowA,
953  unsigned int colA, unsigned int rowB, unsigned int colB, double** C);
954 
956 
960  static void Transpose3x3(const float A[3][3], float AT[3][3]);
961  static void Transpose3x3(const double A[3][3], double AT[3][3]);
963 
965 
969  static void Invert3x3(const float A[3][3], float AI[3][3]);
970  static void Invert3x3(const double A[3][3], double AI[3][3]);
972 
974 
977  static void Identity3x3(float A[3][3]);
978  static void Identity3x3(double A[3][3]);
980 
982 
985  static double Determinant3x3(const float A[3][3]);
986  static double Determinant3x3(const double A[3][3]);
988 
992  static float Determinant3x3(const float c1[3], const float c2[3], const float c3[3]);
993 
997  static double Determinant3x3(const double c1[3], const double c2[3], const double c3[3]);
998 
1005  static double Determinant3x3(double a1, double a2, double a3, double b1, double b2, double b3,
1006  double c1, double c2, double c3);
1007 
1009 
1016  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
1017  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
1018  template <class QuaternionT, class MatrixT,
1019  class EnableT = typename std::enable_if<!vtkMatrixUtilities::MatrixIs2DArray<MatrixT>()>::type>
1020  static void QuaternionToMatrix3x3(const QuaternionT& q, MatrixT&& A);
1022 
1024 
1033  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
1034  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
1035  template <class MatrixT, class QuaternionT,
1036  class EnableT = typename std::enable_if<!vtkMatrixUtilities::MatrixIs2DArray<MatrixT>()>::type>
1037  static void Matrix3x3ToQuaternion(const MatrixT& A, QuaternionT&& q);
1039 
1041 
1047  static void MultiplyQuaternion(const float q1[4], const float q2[4], float q[4]);
1048  static void MultiplyQuaternion(const double q1[4], const double q2[4], double q[4]);
1050 
1052 
1056  static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3]);
1057  static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3]);
1059 
1061 
1065  static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3]);
1066  static void RotateVectorByWXYZ(const double v[3], const double q[4], double r[3]);
1068 
1070 
1075  static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
1076  static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
1078 
1080 
1086  static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
1087  static void Diagonalize3x3(const double A[3][3], double w[3], double V[3][3]);
1089 
1091 
1101  const float A[3][3], float U[3][3], float w[3], float VT[3][3]);
1103  const double A[3][3], double U[3][3], double w[3], double VT[3][3]);
1105 
1114  double a00, double a01, double a10, double a11, double b0, double b1, double& x0, double& x1);
1115 
1124  static vtkTypeBool SolveLinearSystem(double** A, double* x, int size);
1125 
1132  static vtkTypeBool InvertMatrix(double** A, double** AI, int size);
1133 
1140  double** A, double** AI, int size, int* tmp1Size, double* tmp2Size);
1141 
1164  static vtkTypeBool LUFactorLinearSystem(double** A, int* index, int size);
1165 
1171  static vtkTypeBool LUFactorLinearSystem(double** A, int* index, int size, double* tmpSize);
1172 
1181  static void LUSolveLinearSystem(double** A, int* index, double* x, int size);
1182 
1191  static double EstimateMatrixCondition(const double* const* A, int size);
1192 
1194 
1202  static vtkTypeBool Jacobi(float** a, float* w, float** v);
1203  static vtkTypeBool Jacobi(double** a, double* w, double** v);
1205 
1207 
1216  static vtkTypeBool JacobiN(float** a, int n, float* w, float** v);
1217  static vtkTypeBool JacobiN(double** a, int n, double* w, double** v);
1219 
1234  int numberOfSamples, double** xt, int xOrder, double** mt);
1235 
1250  static vtkTypeBool SolveLeastSquares(int numberOfSamples, double** xt, int xOrder, double** yt,
1251  int yOrder, double** mt, int checkHomogeneous = 1);
1252 
1254 
1261  static void RGBToHSV(const float rgb[3], float hsv[3])
1262  {
1263  RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
1264  }
1265  static void RGBToHSV(float r, float g, float b, float* h, float* s, float* v);
1266  static void RGBToHSV(const double rgb[3], double hsv[3])
1267  {
1268  RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
1269  }
1270  static void RGBToHSV(double r, double g, double b, double* h, double* s, double* v);
1272 
1274 
1281  static void HSVToRGB(const float hsv[3], float rgb[3])
1282  {
1283  HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
1284  }
1285  static void HSVToRGB(float h, float s, float v, float* r, float* g, float* b);
1286  static void HSVToRGB(const double hsv[3], double rgb[3])
1287  {
1288  HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
1289  }
1290  static void HSVToRGB(double h, double s, double v, double* r, double* g, double* b);
1292 
1294 
1297  static void LabToXYZ(const double lab[3], double xyz[3])
1298  {
1299  LabToXYZ(lab[0], lab[1], lab[2], xyz + 0, xyz + 1, xyz + 2);
1300  }
1301  static void LabToXYZ(double L, double a, double b, double* x, double* y, double* z);
1303 
1305 
1308  static void XYZToLab(const double xyz[3], double lab[3])
1309  {
1310  XYZToLab(xyz[0], xyz[1], xyz[2], lab + 0, lab + 1, lab + 2);
1311  }
1312  static void XYZToLab(double x, double y, double z, double* L, double* a, double* b);
1314 
1316 
1319  static void XYZToRGB(const double xyz[3], double rgb[3])
1320  {
1321  XYZToRGB(xyz[0], xyz[1], xyz[2], rgb + 0, rgb + 1, rgb + 2);
1322  }
1323  static void XYZToRGB(double x, double y, double z, double* r, double* g, double* b);
1325 
1327 
1330  static void RGBToXYZ(const double rgb[3], double xyz[3])
1331  {
1332  RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz + 0, xyz + 1, xyz + 2);
1333  }
1334  static void RGBToXYZ(double r, double g, double b, double* x, double* y, double* z);
1336 
1338 
1344  static void RGBToLab(const double rgb[3], double lab[3])
1345  {
1346  RGBToLab(rgb[0], rgb[1], rgb[2], lab + 0, lab + 1, lab + 2);
1347  }
1348  static void RGBToLab(double red, double green, double blue, double* L, double* a, double* b);
1350 
1352 
1355  static void LabToRGB(const double lab[3], double rgb[3])
1356  {
1357  LabToRGB(lab[0], lab[1], lab[2], rgb + 0, rgb + 1, rgb + 2);
1358  }
1359  static void LabToRGB(double L, double a, double b, double* red, double* green, double* blue);
1361 
1363 
1366  static void UninitializeBounds(double bounds[6])
1367  {
1368  bounds[0] = 1.0;
1369  bounds[1] = -1.0;
1370  bounds[2] = 1.0;
1371  bounds[3] = -1.0;
1372  bounds[4] = 1.0;
1373  bounds[5] = -1.0;
1374  }
1376 
1378 
1381  static vtkTypeBool AreBoundsInitialized(const double bounds[6])
1382  {
1383  if (bounds[1] - bounds[0] < 0.0)
1384  {
1385  return 0;
1386  }
1387  return 1;
1388  }
1390 
1395  template <class T>
1396  static T ClampValue(const T& value, const T& min, const T& max);
1397 
1399 
1403  static void ClampValue(double* value, const double range[2]);
1404  static void ClampValue(double value, const double range[2], double* clamped_value);
1405  static void ClampValues(double* values, int nb_values, const double range[2]);
1406  static void ClampValues(
1407  const double* values, int nb_values, const double range[2], double* clamped_values);
1409 
1416  static double ClampAndNormalizeValue(double value, const double range[2]);
1417 
1422  template <class T1, class T2>
1423  static void TensorFromSymmetricTensor(const T1 symmTensor[6], T2 tensor[9]);
1424 
1430  template <class T>
1431  static void TensorFromSymmetricTensor(T tensor[9]);
1432 
1442  double range_min, double range_max, double scale = 1.0, double shift = 0.0);
1443 
1452  static vtkTypeBool GetAdjustedScalarRange(vtkDataArray* array, int comp, double range[2]);
1453 
1458  static vtkTypeBool ExtentIsWithinOtherExtent(const int extent1[6], const int extent2[6]);
1459 
1466  const double bounds1[6], const double bounds2[6], const double delta[3]);
1467 
1474  const double point[3], const double bounds[6], const double delta[3]);
1475 
1486  const double bounds[6], const double normal[3], const double point[3]);
1487 
1497  static double Solve3PointCircle(
1498  const double p1[3], const double p2[3], const double p3[3], double center[3]);
1499 
1503  static double Inf();
1504 
1508  static double NegInf();
1509 
1513  static double Nan();
1514 
1518  static vtkTypeBool IsInf(double x);
1519 
1523  static vtkTypeBool IsNan(double x);
1524 
1529  static bool IsFinite(double x);
1530 
1535  static int QuadraticRoot(double a, double b, double c, double min, double max, double* u);
1536 
1537 protected:
1538  vtkMath() = default;
1539  ~vtkMath() override = default;
1540 
1542 
1543 private:
1544  vtkMath(const vtkMath&) = delete;
1545  void operator=(const vtkMath&) = delete;
1546 };
1547 
1548 //----------------------------------------------------------------------------
1549 inline float vtkMath::RadiansFromDegrees(float x)
1550 {
1551  return x * 0.017453292f;
1552 }
1553 
1554 //----------------------------------------------------------------------------
1555 inline double vtkMath::RadiansFromDegrees(double x)
1556 {
1557  return x * 0.017453292519943295;
1558 }
1559 
1560 //----------------------------------------------------------------------------
1561 inline float vtkMath::DegreesFromRadians(float x)
1562 {
1563  return x * 57.2957795131f;
1564 }
1565 
1566 //----------------------------------------------------------------------------
1567 inline double vtkMath::DegreesFromRadians(double x)
1568 {
1569  return x * 57.29577951308232;
1570 }
1571 
1572 //----------------------------------------------------------------------------
1573 inline bool vtkMath::IsPowerOfTwo(vtkTypeUInt64 x)
1574 {
1575  return ((x != 0) & ((x & (x - 1)) == 0));
1576 }
1577 
1578 //----------------------------------------------------------------------------
1579 // Credit goes to Peter Hart and William Lewis on comp.lang.python 1997
1581 {
1582  unsigned int z = static_cast<unsigned int>(((x > 0) ? x - 1 : 0));
1583  z |= z >> 1;
1584  z |= z >> 2;
1585  z |= z >> 4;
1586  z |= z >> 8;
1587  z |= z >> 16;
1588  return static_cast<int>(z + 1);
1589 }
1590 
1591 //----------------------------------------------------------------------------
1592 // Modify the trunc() operation provided by static_cast<int>() to get floor(),
1593 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1594 inline int vtkMath::Floor(double x)
1595 {
1596  int i = static_cast<int>(x);
1597  return i - (i > x);
1598 }
1599 
1600 //----------------------------------------------------------------------------
1601 // Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1602 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1603 inline int vtkMath::Ceil(double x)
1604 {
1605  int i = static_cast<int>(x);
1606  return i + (i < x);
1607 }
1608 
1609 //----------------------------------------------------------------------------
1610 template <class T>
1611 inline T vtkMath::Min(const T& a, const T& b)
1612 {
1613  return (b <= a ? b : a);
1614 }
1615 
1616 //----------------------------------------------------------------------------
1617 template <class T>
1618 inline T vtkMath::Max(const T& a, const T& b)
1619 {
1620  return (b > a ? b : a);
1621 }
1622 
1623 //----------------------------------------------------------------------------
1624 inline float vtkMath::Normalize(float v[3])
1625 {
1626  float den = vtkMath::Norm(v);
1627  if (den != 0.0)
1628  {
1629  for (int i = 0; i < 3; ++i)
1630  {
1631  v[i] /= den;
1632  }
1633  }
1634  return den;
1635 }
1636 
1637 //----------------------------------------------------------------------------
1638 inline double vtkMath::Normalize(double v[3])
1639 {
1640  double den = vtkMath::Norm(v);
1641  if (den != 0.0)
1642  {
1643  for (int i = 0; i < 3; ++i)
1644  {
1645  v[i] /= den;
1646  }
1647  }
1648  return den;
1649 }
1650 
1651 //----------------------------------------------------------------------------
1652 inline float vtkMath::Normalize2D(float v[2])
1653 {
1654  float den = vtkMath::Norm2D(v);
1655  if (den != 0.0)
1656  {
1657  for (int i = 0; i < 2; ++i)
1658  {
1659  v[i] /= den;
1660  }
1661  }
1662  return den;
1663 }
1664 
1665 //----------------------------------------------------------------------------
1666 inline double vtkMath::Normalize2D(double v[2])
1667 {
1668  double den = vtkMath::Norm2D(v);
1669  if (den != 0.0)
1670  {
1671  for (int i = 0; i < 2; ++i)
1672  {
1673  v[i] /= den;
1674  }
1675  }
1676  return den;
1677 }
1678 
1679 //----------------------------------------------------------------------------
1680 inline float vtkMath::Determinant3x3(const float c1[3], const float c2[3], const float c3[3])
1681 {
1682  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1683  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1684 }
1685 
1686 //----------------------------------------------------------------------------
1687 inline double vtkMath::Determinant3x3(const double c1[3], const double c2[3], const double c3[3])
1688 {
1689  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1690  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1691 }
1692 
1693 //----------------------------------------------------------------------------
1695  double a1, double a2, double a3, double b1, double b2, double b3, double c1, double c2, double c3)
1696 {
1697  return (a1 * vtkMath::Determinant2x2(b2, b3, c2, c3) -
1698  b1 * vtkMath::Determinant2x2(a2, a3, c2, c3) + c1 * vtkMath::Determinant2x2(a2, a3, b2, b3));
1699 }
1700 
1701 //----------------------------------------------------------------------------
1702 inline float vtkMath::Distance2BetweenPoints(const float p1[3], const float p2[3])
1703 {
1704  return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1705  (p1[2] - p2[2]) * (p1[2] - p2[2]));
1706 }
1707 
1708 //----------------------------------------------------------------------------
1709 inline double vtkMath::Distance2BetweenPoints(const double p1[3], const double p2[3])
1710 {
1711  return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1712  (p1[2] - p2[2]) * (p1[2] - p2[2]));
1713 }
1714 
1715 //----------------------------------------------------------------------------
1716 template <typename ReturnTypeT, typename TupleRangeT1, typename TupleRangeT2, typename EnableT>
1717 inline ReturnTypeT vtkMath::Distance2BetweenPoints(const TupleRangeT1& p1, const TupleRangeT2& p2)
1718 {
1719  return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1720  (p1[2] - p2[2]) * (p1[2] - p2[2]));
1721 }
1722 
1723 //----------------------------------------------------------------------------
1724 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1725 inline void vtkMath::Cross(const float a[3], const float b[3], float c[3])
1726 {
1727  float Cx = a[1] * b[2] - a[2] * b[1];
1728  float Cy = a[2] * b[0] - a[0] * b[2];
1729  float Cz = a[0] * b[1] - a[1] * b[0];
1730  c[0] = Cx;
1731  c[1] = Cy;
1732  c[2] = Cz;
1733 }
1734 
1735 //----------------------------------------------------------------------------
1736 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1737 inline void vtkMath::Cross(const double a[3], const double b[3], double c[3])
1738 {
1739  double Cx = a[1] * b[2] - a[2] * b[1];
1740  double Cy = a[2] * b[0] - a[0] * b[2];
1741  double Cz = a[0] * b[1] - a[1] * b[0];
1742  c[0] = Cx;
1743  c[1] = Cy;
1744  c[2] = Cz;
1745 }
1746 
1747 //----------------------------------------------------------------------------
1748 template <class T>
1749 inline double vtkDeterminant3x3(const T A[3][3])
1750 {
1751  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] -
1752  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];
1753 }
1754 
1755 //----------------------------------------------------------------------------
1756 inline double vtkMath::Determinant3x3(const float A[3][3])
1757 {
1758  return vtkDeterminant3x3(A);
1759 }
1760 
1761 //----------------------------------------------------------------------------
1762 inline double vtkMath::Determinant3x3(const double A[3][3])
1763 {
1764  return vtkDeterminant3x3(A);
1765 }
1766 
1767 //----------------------------------------------------------------------------
1768 template <class T>
1769 inline T vtkMath::ClampValue(const T& value, const T& min, const T& max)
1770 {
1771  assert("pre: valid_range" && min <= max);
1772 
1773 #if __cplusplus >= 201703L
1774  return std::clamp(value, min, max);
1775 #else
1776  // compilers are good at optimizing the ternary operator,
1777  // use '<' since it is preferred by STL for custom types
1778  T v = (min < value ? value : min);
1779  return (v < max ? v : max);
1780 #endif
1781 }
1782 
1783 //----------------------------------------------------------------------------
1784 inline void vtkMath::ClampValue(double* value, const double range[2])
1785 {
1786  if (value && range)
1787  {
1788  assert("pre: valid_range" && range[0] <= range[1]);
1789 
1790  *value = vtkMath::ClampValue(*value, range[0], range[1]);
1791  }
1792 }
1793 
1794 //----------------------------------------------------------------------------
1795 inline void vtkMath::ClampValue(double value, const double range[2], double* clamped_value)
1796 {
1797  if (range && clamped_value)
1798  {
1799  assert("pre: valid_range" && range[0] <= range[1]);
1800 
1801  *clamped_value = vtkMath::ClampValue(value, range[0], range[1]);
1802  }
1803 }
1804 
1805 // ---------------------------------------------------------------------------
1806 inline double vtkMath::ClampAndNormalizeValue(double value, const double range[2])
1807 {
1808  assert("pre: valid_range" && range[0] <= range[1]);
1809 
1810  double result;
1811  if (range[0] == range[1])
1812  {
1813  result = 0.0;
1814  }
1815  else
1816  {
1817  // clamp
1818  result = vtkMath::ClampValue(value, range[0], range[1]);
1819 
1820  // normalize
1821  result = (result - range[0]) / (range[1] - range[0]);
1822  }
1823 
1824  assert("post: valid_result" && result >= 0.0 && result <= 1.0);
1825 
1826  return result;
1827 }
1828 
1829 //-----------------------------------------------------------------------------
1830 template <class T1, class T2>
1831 inline void vtkMath::TensorFromSymmetricTensor(const T1 symmTensor[9], T2 tensor[9])
1832 {
1833  for (int i = 0; i < 3; ++i)
1834  {
1835  tensor[4 * i] = symmTensor[i];
1836  }
1837  tensor[1] = tensor[3] = symmTensor[3];
1838  tensor[2] = tensor[6] = symmTensor[5];
1839  tensor[5] = tensor[7] = symmTensor[4];
1840 }
1841 
1842 //-----------------------------------------------------------------------------
1843 template <class T>
1844 inline void vtkMath::TensorFromSymmetricTensor(T tensor[9])
1845 {
1846  tensor[6] = tensor[5]; // XZ
1847  tensor[7] = tensor[4]; // YZ
1848  tensor[8] = tensor[2]; // ZZ
1849  tensor[4] = tensor[1]; // YY
1850  tensor[5] = tensor[7]; // YZ
1851  tensor[2] = tensor[6]; // XZ
1852  tensor[1] = tensor[3]; // XY
1853 }
1854 
1855 namespace
1856 {
1857 template <class QuaternionT, class MatrixT>
1858 inline void vtkQuaternionToMatrix3x3(const QuaternionT& quat, MatrixT& A)
1859 {
1861 
1862  Scalar ww = quat[0] * quat[0];
1863  Scalar wx = quat[0] * quat[1];
1864  Scalar wy = quat[0] * quat[2];
1865  Scalar wz = quat[0] * quat[3];
1866 
1867  Scalar xx = quat[1] * quat[1];
1868  Scalar yy = quat[2] * quat[2];
1869  Scalar zz = quat[3] * quat[3];
1870 
1871  Scalar xy = quat[1] * quat[2];
1872  Scalar xz = quat[1] * quat[3];
1873  Scalar yz = quat[2] * quat[3];
1874 
1875  Scalar rr = xx + yy + zz;
1876  // normalization factor, just in case quaternion was not normalized
1877  Scalar f = 1 / (ww + rr);
1878  Scalar s = (ww - rr) * f;
1879  f *= 2;
1880 
1882 
1883  Wrapper::template Get<0, 0>(A) = xx * f + s;
1884  Wrapper::template Get<1, 0>(A) = (xy + wz) * f;
1885  Wrapper::template Get<2, 0>(A) = (xz - wy) * f;
1886 
1887  Wrapper::template Get<0, 1>(A) = (xy - wz) * f;
1888  Wrapper::template Get<1, 1>(A) = yy * f + s;
1889  Wrapper::template Get<2, 1>(A) = (yz + wx) * f;
1890 
1891  Wrapper::template Get<0, 2>(A) = (xz + wy) * f;
1892  Wrapper::template Get<1, 2>(A) = (yz - wx) * f;
1893  Wrapper::template Get<2, 2>(A) = zz * f + s;
1894 }
1895 } // anonymous namespace
1896 
1897 //------------------------------------------------------------------------------
1898 inline void vtkMath::QuaternionToMatrix3x3(const float quat[4], float A[3][3])
1899 {
1900  vtkQuaternionToMatrix3x3(quat, A);
1901 }
1902 
1903 //------------------------------------------------------------------------------
1904 inline void vtkMath::QuaternionToMatrix3x3(const double quat[4], double A[3][3])
1905 {
1906  vtkQuaternionToMatrix3x3(quat, A);
1907 }
1908 
1909 //-----------------------------------------------------------------------------
1910 template <class QuaternionT, class MatrixT, class EnableT>
1911 inline void vtkMath::QuaternionToMatrix3x3(const QuaternionT& q, MatrixT&& A)
1912 {
1913  vtkQuaternionToMatrix3x3(q, A);
1914 }
1915 
1916 namespace
1917 {
1918 //------------------------------------------------------------------------------
1919 // The solution is based on
1920 // Berthold K. P. Horn (1987),
1921 // "Closed-form solution of absolute orientation using unit quaternions,"
1922 // Journal of the Optical Society of America A, 4:629-642
1923 template <class MatrixT, class QuaternionT>
1924 inline void vtkMatrix3x3ToQuaternion(const MatrixT& A, QuaternionT& quat)
1925 {
1927 
1928  Scalar N[4][4];
1929 
1931 
1932  // on-diagonal elements
1933  N[0][0] = Wrapper::template Get<0, 0>(A) + Wrapper::template Get<1, 1>(A) +
1934  Wrapper::template Get<2, 2>(A);
1935  N[1][1] = Wrapper::template Get<0, 0>(A) - Wrapper::template Get<1, 1>(A) -
1936  Wrapper::template Get<2, 2>(A);
1937  N[2][2] = -Wrapper::template Get<0, 0>(A) + Wrapper::template Get<1, 1>(A) -
1938  Wrapper::template Get<2, 2>(A);
1939  N[3][3] = -Wrapper::template Get<0, 0>(A) - Wrapper::template Get<1, 1>(A) +
1940  Wrapper::template Get<2, 2>(A);
1941 
1942  // off-diagonal elements
1943  N[0][1] = N[1][0] = Wrapper::template Get<2, 1>(A) - Wrapper::template Get<1, 2>(A);
1944  N[0][2] = N[2][0] = Wrapper::template Get<0, 2>(A) - Wrapper::template Get<2, 0>(A);
1945  N[0][3] = N[3][0] = Wrapper::template Get<1, 0>(A) - Wrapper::template Get<0, 1>(A);
1946 
1947  N[1][2] = N[2][1] = Wrapper::template Get<1, 0>(A) + Wrapper::template Get<0, 1>(A);
1948  N[1][3] = N[3][1] = Wrapper::template Get<0, 2>(A) + Wrapper::template Get<2, 0>(A);
1949  N[2][3] = N[3][2] = Wrapper::template Get<2, 1>(A) + Wrapper::template Get<1, 2>(A);
1950 
1951  Scalar eigenvectors[4][4], eigenvalues[4];
1952 
1953  // convert into format that JacobiN can use,
1954  // then use Jacobi to find eigenvalues and eigenvectors
1955  Scalar *NTemp[4], *eigenvectorsTemp[4];
1956  for (int i = 0; i < 4; ++i)
1957  {
1958  NTemp[i] = N[i];
1959  eigenvectorsTemp[i] = eigenvectors[i];
1960  }
1961  vtkMath::JacobiN(NTemp, 4, eigenvalues, eigenvectorsTemp);
1962 
1963  // the first eigenvector is the one we want
1964  quat[0] = eigenvectors[0][0];
1965  quat[1] = eigenvectors[1][0];
1966  quat[2] = eigenvectors[2][0];
1967  quat[3] = eigenvectors[3][0];
1968 }
1969 } // anonymous namespace
1970 
1971 //------------------------------------------------------------------------------
1972 inline void vtkMath::Matrix3x3ToQuaternion(const float A[3][3], float quat[4])
1973 {
1974  vtkMatrix3x3ToQuaternion(A, quat);
1975 }
1976 
1977 //------------------------------------------------------------------------------
1978 inline void vtkMath::Matrix3x3ToQuaternion(const double A[3][3], double quat[4])
1979 {
1980  vtkMatrix3x3ToQuaternion(A, quat);
1981 }
1982 
1983 //-----------------------------------------------------------------------------
1984 template <class MatrixT, class QuaternionT, class EnableT>
1985 inline void vtkMath::Matrix3x3ToQuaternion(const MatrixT& A, QuaternionT&& q)
1986 {
1987  vtkMatrix3x3ToQuaternion(A, q);
1988 }
1989 
1990 namespace vtk_detail
1991 {
1992 // Can't specialize templates inside a template class, so we move the impl here.
1993 template <typename OutT>
1994 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
1995 { // OutT is integral -- clamp and round
1996  if (!vtkMath::IsNan(val))
1997  {
1998  double min = static_cast<double>(vtkTypeTraits<OutT>::Min());
1999  double max = static_cast<double>(vtkTypeTraits<OutT>::Max());
2000  val = vtkMath::ClampValue(val, min, max);
2001  *ret = static_cast<OutT>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
2002  }
2003  else
2004  *ret = 0;
2005 }
2006 template <>
2007 inline void RoundDoubleToIntegralIfNecessary(double val, double* retVal)
2008 { // OutT is double: passthrough
2009  *retVal = val;
2010 }
2011 template <>
2012 inline void RoundDoubleToIntegralIfNecessary(double val, float* retVal)
2013 { // OutT is float -- just clamp (as doubles, then the cast to float is well-defined.)
2014  if (!vtkMath::IsNan(val))
2015  {
2016  double min = static_cast<double>(vtkTypeTraits<float>::Min());
2017  double max = static_cast<double>(vtkTypeTraits<float>::Max());
2018  val = vtkMath::ClampValue(val, min, max);
2019  }
2020 
2021  *retVal = static_cast<float>(val);
2022 }
2023 } // end namespace vtk_detail
2024 
2025 //-----------------------------------------------------------------------------
2026 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
2027 #define VTK_MATH_ISINF_IS_INLINE
2028 inline vtkTypeBool vtkMath::IsInf(double x)
2029 {
2030 #if defined(VTK_HAS_STD_ISINF)
2031  return std::isinf(x);
2032 #else
2033  return (isinf(x) != 0); // Force conversion to bool
2034 #endif
2035 }
2036 #endif
2037 
2038 //-----------------------------------------------------------------------------
2039 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
2040 #define VTK_MATH_ISNAN_IS_INLINE
2041 inline vtkTypeBool vtkMath::IsNan(double x)
2042 {
2043 #if defined(VTK_HAS_STD_ISNAN)
2044  return std::isnan(x);
2045 #else
2046  return (isnan(x) != 0); // Force conversion to bool
2047 #endif
2048 }
2049 #endif
2050 
2051 //-----------------------------------------------------------------------------
2052 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
2053 #define VTK_MATH_ISFINITE_IS_INLINE
2054 inline bool vtkMath::IsFinite(double x)
2055 {
2056 #if defined(VTK_HAS_STD_ISFINITE)
2057  return std::isfinite(x);
2058 #elif defined(VTK_HAS_ISFINITE)
2059  return (isfinite(x) != 0); // Force conversion to bool
2060 #else
2061  return (finite(x) != 0); // Force conversion to bool
2062 #endif
2063 }
2064 #endif
2065 
2066 #endif
Gaussian sequence of pseudo random numbers implemented with the Box-Mueller transform.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:50
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:34
performs common math operations
Definition: vtkMath.h:88
static ReturnTypeT Distance2BetweenPoints(const TupleRangeT1 &p1, const TupleRangeT2 &p2)
Compute distance squared between two points p1 and p2.
Definition: vtkMath.h:1717
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:445
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:119
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:841
static double Norm2D(const double x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:713
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:354
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:678
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:1541
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:343
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:525
static ReturnTypeT Dot(const TupleRangeT1 &a, const TupleRangeT2 &b)
Compute dot product between two points p1 and p2.
Definition: vtkMath.h:469
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:1308
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:918
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:1297
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:437
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:875
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:1725
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
Definition: vtkMath.h:402
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:1281
static void Assign(const double a[3], double b[3])
Assign values to a 3-vector (double version).
Definition: vtkMath.h:327
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:740
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition: vtkMath.h:1618
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:692
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:426
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:1603
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:1286
~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:852
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:1330
static void QuaternionToMatrix3x3(const float quat[4], float A[3][3])
Convert a quaternion to a 3x3 rotation matrix.
Definition: vtkMath.h:1898
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1580
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:1652
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:491
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:317
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
Definition: vtkMath.h:1549
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:332
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:1381
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:97
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:365
static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4])
Convert a 3x3 matrix into a quaternion.
Definition: vtkMath.h:1972
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:1319
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:1806
static void MultiplyScalar(double a[3], double s)
Multiplies a 3-vector by a scalar (double version).
Definition: vtkMath.h:414
static double Dot2D(const double x[2], const double y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:673
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:814
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:1344
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
Definition: vtkMath.h:1594
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:379
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:739
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:1266
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:1561
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
Definition: vtkMath.h:730
static int Round(double f)
Definition: vtkMath.h:120
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:1366
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:477
static double Norm(const double v[3])
Compute the norm of 3-vector (double version).
Definition: vtkMath.h:530
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:128
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:1573
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:1624
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:545
static double Determinant3x3(const float A[3][3])
Return the determinant of a 3x3 matrix.
Definition: vtkMath.h:1756
static float Dot2D(const float x[2], const float y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:668
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:939
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:1261
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:1355
static float Norm2D(const float x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:707
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:898
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
Definition: vtkMath.h:390
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1611
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:1769
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:63
represent and manipulate 3D points
Definition: vtkPoints.h:34
@ 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:1994
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:1749
#define max(a, b)