VTK
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 =========================================================================*/
45 #ifndef vtkMath_h
46 #define vtkMath_h
47 
48 #include "vtkCommonCoreModule.h" // For export macro
49 #include "vtkObject.h"
50 #include "vtkTypeTraits.h" // For type traits
51 #include "vtkSmartPointer.h" // For vtkSmartPointer.
52 
53 #include "vtkMathConfigure.h" // For <cmath> and VTK_HAS_ISNAN etc.
54 
55 #include <cassert> // assert() in inline implementations.
56 #include <algorithm> // for std::clamp
57 
58 #ifndef DBL_MIN
59 # define VTK_DBL_MIN 2.2250738585072014e-308
60 #else // DBL_MIN
61 # define VTK_DBL_MIN DBL_MIN
62 #endif // DBL_MIN
63 
64 #ifndef DBL_EPSILON
65 # define VTK_DBL_EPSILON 2.2204460492503131e-16
66 #else // DBL_EPSILON
67 # define VTK_DBL_EPSILON DBL_EPSILON
68 #endif // DBL_EPSILON
69 
70 #ifndef VTK_DBL_EPSILON
71 # ifndef DBL_EPSILON
72 # define VTK_DBL_EPSILON 2.2204460492503131e-16
73 # else // DBL_EPSILON
74 # define VTK_DBL_EPSILON DBL_EPSILON
75 # endif // DBL_EPSILON
76 #endif // VTK_DBL_EPSILON
77 
78 class vtkDataArray;
79 class vtkPoints;
80 class vtkMathInternal;
83 
84 namespace vtk_detail
85 {
86 // forward declaration
87 template <typename OutT>
88 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret);
89 } // end namespace vtk_detail
90 
91 class VTKCOMMONCORE_EXPORT vtkMath : public vtkObject
92 {
93 public:
94  static vtkMath *New();
95  vtkTypeMacro(vtkMath,vtkObject);
96  void PrintSelf(ostream& os, vtkIndent indent) override;
97 
101  static double Pi() { return 3.141592653589793; }
102 
104 
107  static float RadiansFromDegrees( float degrees);
108  static double RadiansFromDegrees( double degrees);
110 
112 
115  static float DegreesFromRadians( float radians);
116  static double DegreesFromRadians( double radians);
118 
122 #if 1
123  static int Round(float f) {
124  return static_cast<int>( f + ( f >= 0.0 ? 0.5 : -0.5 ) ); }
125  static int Round(double f) {
126  return static_cast<int>( f + ( f >= 0.0 ? 0.5 : -0.5 ) ); }
127 #endif
128 
133  template <typename OutT>
134  static void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
135  {
136  // Can't specialize template methods in a template class, so we move the
137  // implementations to a external namespace.
139  }
140 
146  static int Floor(double x);
147 
153  static int Ceil(double x);
154 
160  static int CeilLog2(vtkTypeUInt64 x);
161 
166  template<class T>
167  static T Min(const T & a, const T & b);
168 
173  template<class T>
174  static T Max(const T & a, const T & b);
175 
179  static bool IsPowerOfTwo(vtkTypeUInt64 x);
180 
186  static int NearestPowerOfTwo(int x);
187 
192  static vtkTypeInt64 Factorial( int N );
193 
199  static vtkTypeInt64 Binomial( int m, int n );
200 
211  static int* BeginCombination( int m, int n );
212 
223  static int NextCombination( int m, int n, int* combination );
224 
228  static void FreeCombination( int* combination);
229 
245  static void RandomSeed(int s);
246 
258  static int GetSeed();
259 
273  static double Random();
274 
287  static double Random( double min, double max );
288 
301  static double Gaussian();
302 
315  static double Gaussian( double mean, double std );
316 
320  static void Add(const float a[3], const float b[3], float c[3]) {
321  for (int i = 0; i < 3; ++i)
322  {
323  c[i] = a[i] + b[i];
324  }
325  }
326 
330  static void Add(const double a[3], const double b[3], double c[3]) {
331  for (int i = 0; i < 3; ++i)
332  {
333  c[i] = a[i] + b[i];
334  }
335  }
336 
340  static void Subtract(const float a[3], const float b[3], float c[3]) {
341  for (int i = 0; i < 3; ++i)
342  {
343  c[i] = a[i] - b[i];
344  }
345  }
346 
350  static void Subtract(const double a[3], const double b[3], double c[3]) {
351  for (int i = 0; i < 3; ++i)
352  {
353  c[i] = a[i] - b[i];
354  }
355  }
356 
361  static void MultiplyScalar(float a[3], float s) {
362  for (int i = 0; i < 3; ++i)
363  {
364  a[i] *= s;
365  }
366  }
367 
372  static void MultiplyScalar2D(float a[2], float s) {
373  for (int i = 0; i < 2; ++i)
374  {
375  a[i] *= s;
376  }
377  }
378 
383  static void MultiplyScalar(double a[3], double s) {
384  for (int i = 0; i < 3; ++i)
385  {
386  a[i] *= s;
387  }
388  }
389 
394  static void MultiplyScalar2D(double a[2], double s) {
395  for (int i = 0; i < 2; ++i)
396  {
397  a[i] *= s;
398  }
399  }
400 
404  static float Dot(const float a[3], const float b[3]) {
405  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
406  }
407 
411  static double Dot(const double a[3], const double b[3]) {
412  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
413  }
414 
418  static void Outer(const float a[3], const float b[3], float c[3][3]) {
419  for (int i = 0; i < 3; ++i)
420  {
421  for (int j = 0; j < 3; ++j)
422  {
423  c[i][j] = a[i] * b[j];
424  }
425  }
426  }
427 
431  static void Outer(const double a[3], const double b[3], double c[3][3]) {
432  for (int i = 0; i < 3; ++i)
433  {
434  for (int j = 0; j < 3; ++j)
435  {
436  c[i][j] = a[i] * b[j];
437  }
438  }
439  }
440 
445  static void Cross(const float a[3], const float b[3], float c[3]);
446 
451  static void Cross(const double a[3], const double b[3], double c[3]);
452 
454 
457  static float Norm(const float* x, int n);
458  static double Norm(const double* x, int n);
460 
464  static float Norm(const float v[3]) {
465  return static_cast<float> (sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] ) );
466  }
467 
471  static double Norm(const double v[3]) {
472  return sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] );
473  }
474 
479  static float Normalize(float v[3]);
480 
485  static double Normalize(double v[3]);
486 
488 
495  static void Perpendiculars(const double v1[3], double v2[3], double v3[3],
496  double theta);
497  static void Perpendiculars(const float v1[3], float v2[3], float v3[3],
498  double theta);
500 
502 
507  static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
508  static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
510 
512 
518  static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
519  static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
521 
526  static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
527 
532  static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
533 
537  static double AngleBetweenVectors(const double v1[3], const double v2[3]);
538 
543  static double GaussianAmplitude(const double variance, const double distanceFromMean);
544 
549  static double GaussianAmplitude(const double mean, const double variance, const double position);
550 
556  static double GaussianWeight(const double variance, const double distanceFromMean);
557 
563  static double GaussianWeight(const double mean, const double variance, const double position);
564 
568  static float Dot2D(const float x[2], const float y[2]) {
569  return x[0] * y[0] + x[1] * y[1];
570  }
571 
575  static double Dot2D(const double x[2], const double y[2]) {
576  return x[0] * y[0] + x[1] * y[1];
577  }
578 
582  static void Outer2D(const float x[2], const float y[2], float A[2][2])
583  {
584  for (int i=0; i < 2; ++i)
585  {
586  for (int j=0; j < 2; ++j)
587  {
588  A[i][j] = x[i] * y[j];
589  }
590  }
591  }
592 
596  static void Outer2D(const double x[2], const double y[2], double A[2][2])
597  {
598  for (int i=0; i < 2; ++i)
599  {
600  for (int j=0; j < 2; ++j)
601  {
602  A[i][j] = x[i] * y[j];
603  }
604  }
605  }
606 
611  static float Norm2D(const float x[2]) {
612  return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );
613  }
614 
619  static double Norm2D(const double x[2]) {
620  return sqrt( x[0] * x[0] + x[1] * x[1] );
621  }
622 
627  static float Normalize2D(float v[2]);
628 
633  static double Normalize2D(double v[2]);
634 
638  static float Determinant2x2(const float c1[2], const float c2[2]) {
639  return c1[0] * c2[1] - c2[0] * c1[1];
640  }
641 
643 
646  static double Determinant2x2(double a, double b, double c, double d) {
647  return a * d - b * c;
648  }
649  static double Determinant2x2(const double c1[2], const double c2[2]) {
650  return c1[0] * c2[1] - c2[0] * c1[1];
651  }
653 
655 
658  static void LUFactor3x3(float A[3][3], int index[3]);
659  static void LUFactor3x3(double A[3][3], int index[3]);
661 
663 
666  static void LUSolve3x3(const float A[3][3], const int index[3],
667  float x[3]);
668  static void LUSolve3x3(const double A[3][3], const int index[3],
669  double x[3]);
671 
673 
677  static void LinearSolve3x3(const float A[3][3], const float x[3],
678  float y[3]);
679  static void LinearSolve3x3(const double A[3][3], const double x[3],
680  double y[3]);
682 
684 
687  static void Multiply3x3(const float A[3][3], const float in[3],
688  float out[3]);
689  static void Multiply3x3(const double A[3][3], const double in[3],
690  double out[3]);
692 
694 
697  static void Multiply3x3(const float A[3][3], const float B[3][3],
698  float C[3][3]);
699  static void Multiply3x3(const double A[3][3], const double B[3][3],
700  double C[3][3]);
702 
708  static void MultiplyMatrix(const double *const *A, const double *const *B,
709  unsigned int rowA, unsigned int colA,
710  unsigned int rowB, unsigned int colB,
711  double **C);
712 
714 
718  static void Transpose3x3(const float A[3][3], float AT[3][3]);
719  static void Transpose3x3(const double A[3][3], double AT[3][3]);
721 
723 
727  static void Invert3x3(const float A[3][3], float AI[3][3]);
728  static void Invert3x3(const double A[3][3], double AI[3][3]);
730 
732 
735  static void Identity3x3(float A[3][3]);
736  static void Identity3x3(double A[3][3]);
738 
740 
743  static double Determinant3x3(const float A[3][3]);
744  static double Determinant3x3(const double A[3][3]);
746 
750  static float Determinant3x3(const float c1[3],
751  const float c2[3],
752  const float c3[3]);
753 
757  static double Determinant3x3(const double c1[3],
758  const double c2[3],
759  const double c3[3]);
760 
767  static double Determinant3x3(double a1, double a2, double a3,
768  double b1, double b2, double b3,
769  double c1, double c2, double c3);
770 
772 
779  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
780  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
782 
784 
793  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
794  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
796 
798 
804  static void MultiplyQuaternion( const float q1[4], const float q2[4], float q[4] );
805  static void MultiplyQuaternion( const double q1[4], const double q2[4], double q[4] );
807 
809 
813  static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3]);
814  static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3]);
816 
818 
822  static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3]);
823  static void RotateVectorByWXYZ(const double v[3], const double q[4], double r[3]);
825 
827 
832  static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
833  static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
835 
837 
843  static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
844  static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
846 
848 
857  static void SingularValueDecomposition3x3(const float A[3][3],
858  float U[3][3], float w[3],
859  float VT[3][3]);
860  static void SingularValueDecomposition3x3(const double A[3][3],
861  double U[3][3], double w[3],
862  double VT[3][3]);
864 
871  static vtkTypeBool SolveLinearSystem(double **A, double *x, int size);
872 
879  static vtkTypeBool InvertMatrix(double **A, double **AI, int size);
880 
886  static vtkTypeBool InvertMatrix(double **A, double **AI, int size,
887  int *tmp1Size, double *tmp2Size);
888 
911  static vtkTypeBool LUFactorLinearSystem(double **A, int *index, int size);
912 
918  static vtkTypeBool LUFactorLinearSystem(double **A, int *index, int size,
919  double *tmpSize);
920 
929  static void LUSolveLinearSystem(double **A, int *index,
930  double *x, int size);
931 
940  static double EstimateMatrixCondition(const double *const *A, int size);
941 
943 
951  static vtkTypeBool Jacobi(float **a, float *w, float **v);
952  static vtkTypeBool Jacobi(double **a, double *w, double **v);
954 
956 
965  static vtkTypeBool JacobiN(float **a, int n, float *w, float **v);
966  static vtkTypeBool JacobiN(double **a, int n, double *w, double **v);
968 
982  static vtkTypeBool SolveHomogeneousLeastSquares(int numberOfSamples, double **xt,
983  int xOrder, double **mt);
984 
999  static vtkTypeBool SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
1000  double **yt, int yOrder, double **mt, int checkHomogeneous=1);
1001 
1003 
1010  static void RGBToHSV(const float rgb[3], float hsv[3]) {
1011  RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1012  }
1013  static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v);
1014  VTK_LEGACY(static double* RGBToHSV(const double rgb[3]) VTK_SIZEHINT(3));
1015  VTK_LEGACY(static double* RGBToHSV(double r, double g, double b) VTK_SIZEHINT(3));
1016  static void RGBToHSV(const double rgb[3], double hsv[3]) {
1017  RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1018  }
1019  static void RGBToHSV(double r, double g, double b, double *h, double *s, double *v);
1021 
1023 
1030  static void HSVToRGB(const float hsv[3], float rgb[3]) {
1031  HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
1032  }
1033  static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b);
1034  VTK_LEGACY(static double* HSVToRGB(const double hsv[3]) VTK_SIZEHINT(3));
1035  VTK_LEGACY(static double* HSVToRGB(double h, double s, double v) VTK_SIZEHINT(3));
1036  static void HSVToRGB(const double hsv[3], double rgb[3])
1037  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
1038  static void HSVToRGB(double h, double s, double v, double *r, double *g, double *b);
1040 
1042 
1045  static void LabToXYZ(const double lab[3], double xyz[3]) {
1046  LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
1047  }
1048  static void LabToXYZ(double L, double a, double b,
1049  double *x, double *y, double *z);
1050  VTK_LEGACY(static double *LabToXYZ(const double lab[3]) VTK_SIZEHINT(3));
1052 
1054 
1057  static void XYZToLab(const double xyz[3], double lab[3]) {
1058  XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
1059  }
1060  static void XYZToLab(double x, double y, double z,
1061  double *L, double *a, double *b);
1062  VTK_LEGACY(static double *XYZToLab(const double xyz[3]) VTK_SIZEHINT(3));
1064 
1066 
1069  static void XYZToRGB(const double xyz[3], double rgb[3]) {
1070  XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
1071  }
1072  static void XYZToRGB(double x, double y, double z,
1073  double *r, double *g, double *b);
1074  VTK_LEGACY(static double *XYZToRGB(const double xyz[3]) VTK_SIZEHINT(3));
1076 
1078 
1081  static void RGBToXYZ(const double rgb[3], double xyz[3]) {
1082  RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
1083  }
1084  static void RGBToXYZ(double r, double g, double b,
1085  double *x, double *y, double *z);
1086  VTK_LEGACY(static double *RGBToXYZ(const double rgb[3]) VTK_SIZEHINT(3));
1088 
1090 
1096  static void RGBToLab(const double rgb[3], double lab[3]) {
1097  RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
1098  }
1099  static void RGBToLab(double red, double green, double blue,
1100  double *L, double *a, double *b);
1101  VTK_LEGACY(static double *RGBToLab(const double rgb[3]) VTK_SIZEHINT(3));
1103 
1105 
1108  static void LabToRGB(const double lab[3], double rgb[3]) {
1109  LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
1110  }
1111  static void LabToRGB(double L, double a, double b,
1112  double *red, double *green, double *blue);
1113  VTK_LEGACY(static double *LabToRGB(const double lab[3]) VTK_SIZEHINT(3));
1115 
1117 
1120  static void UninitializeBounds(double bounds[6]) {
1121  bounds[0] = 1.0;
1122  bounds[1] = -1.0;
1123  bounds[2] = 1.0;
1124  bounds[3] = -1.0;
1125  bounds[4] = 1.0;
1126  bounds[5] = -1.0;
1127  }
1129 
1131 
1134  static vtkTypeBool AreBoundsInitialized(const double bounds[6]) {
1135  if ( bounds[1] - bounds[0] < 0.0 )
1136  {
1137  return 0;
1138  }
1139  return 1;
1140  }
1142 
1147  template<class T>
1148  static T ClampValue(const T & value, const T & min, const T & max);
1149 
1151 
1155  static void ClampValue(double *value, const double range[2]);
1156  static void ClampValue(double value, const double range[2], double *clamped_value);
1157  static void ClampValues(
1158  double *values, int nb_values, const double range[2]);
1159  static void ClampValues(
1160  const double *values, int nb_values, const double range[2], double *clamped_values);
1162 
1169  static double ClampAndNormalizeValue(double value,
1170  const double range[2]);
1171 
1176  template<class T1, class T2>
1177  static void TensorFromSymmetricTensor(const T1 symmTensor[6], T2 tensor[9]);
1178 
1184  template<class T>
1185  static void TensorFromSymmetricTensor(T tensor[9]);
1186 
1195  static int GetScalarTypeFittingRange(
1196  double range_min, double range_max,
1197  double scale = 1.0, double shift = 0.0);
1198 
1207  static vtkTypeBool GetAdjustedScalarRange(
1208  vtkDataArray *array, int comp, double range[2]);
1209 
1214  static vtkTypeBool ExtentIsWithinOtherExtent(const int extent1[6], const int extent2[6]);
1215 
1221  static vtkTypeBool BoundsIsWithinOtherBounds(const double bounds1[6], const double bounds2[6], const double delta[3]);
1222 
1228  static vtkTypeBool PointIsWithinBounds(const double point[3], const double bounds[6], const double delta[3]);
1229 
1239  static int PlaneIntersectsAABB(
1240  const double bounds[6],
1241  const double normal[3],
1242  const double point[3]);
1243 
1253  static double Solve3PointCircle(
1254  const double p1[3],
1255  const double p2[3],
1256  const double p3[3],
1257  double center[3]);
1258 
1262  static double Inf();
1263 
1267  static double NegInf();
1268 
1272  static double Nan();
1273 
1277  static vtkTypeBool IsInf(double x);
1278 
1282  static vtkTypeBool IsNan(double x);
1283 
1287  static bool IsFinite(double x);
1288 protected:
1289  vtkMath() {}
1290  ~vtkMath() override {}
1291 
1293 private:
1294  vtkMath(const vtkMath&) = delete;
1295  void operator=(const vtkMath&) = delete;
1296 };
1297 
1298 //----------------------------------------------------------------------------
1299 inline float vtkMath::RadiansFromDegrees( float x )
1300 {
1301  return x * 0.017453292f;
1302 }
1303 
1304 //----------------------------------------------------------------------------
1305 inline double vtkMath::RadiansFromDegrees( double x )
1306 {
1307  return x * 0.017453292519943295;
1308 }
1309 
1310 //----------------------------------------------------------------------------
1311 inline float vtkMath::DegreesFromRadians( float x )
1312 {
1313  return x * 57.2957795131f;
1314 }
1315 
1316 //----------------------------------------------------------------------------
1317 inline double vtkMath::DegreesFromRadians( double x )
1318 {
1319  return x * 57.29577951308232;
1320 }
1321 
1322 //----------------------------------------------------------------------------
1323 inline bool vtkMath::IsPowerOfTwo(vtkTypeUInt64 x)
1324 {
1325  return ((x != 0) & ((x & (x - 1)) == 0));
1326 }
1327 
1328 //----------------------------------------------------------------------------
1329 // Credit goes to Peter Hart and William Lewis on comp.lang.python 1997
1331 {
1332  unsigned int z = ((x > 0) ? x - 1 : 0);
1333  z |= z >> 1;
1334  z |= z >> 2;
1335  z |= z >> 4;
1336  z |= z >> 8;
1337  z |= z >> 16;
1338  return static_cast<int>(z + 1);
1339 }
1340 
1341 //----------------------------------------------------------------------------
1342 // Modify the trunc() operation provided by static_cast<int>() to get floor(),
1343 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1344 inline int vtkMath::Floor(double x)
1345 {
1346  int i = static_cast<int>(x);
1347  return i - ( i > x );
1348 }
1349 
1350 //----------------------------------------------------------------------------
1351 // Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1352 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1353 inline int vtkMath::Ceil(double x)
1354 {
1355  int i = static_cast<int>(x);
1356  return i + ( i < x );
1357 }
1358 
1359 //----------------------------------------------------------------------------
1360 template<class T>
1361 inline T vtkMath::Min(const T & a, const T & b)
1362 {
1363  return (b <= a ? b : a);
1364 }
1365 
1366 //----------------------------------------------------------------------------
1367 template<class T>
1368 inline T vtkMath::Max(const T & a, const T & b)
1369 {
1370  return (b > a ? b : a);
1371 }
1372 
1373 //----------------------------------------------------------------------------
1374 inline float vtkMath::Normalize(float v[3])
1375 {
1376  float den = vtkMath::Norm( v );
1377  if ( den != 0.0 )
1378  {
1379  for (int i=0; i < 3; ++i)
1380  {
1381  v[i] /= den;
1382  }
1383  }
1384  return den;
1385 }
1386 
1387 //----------------------------------------------------------------------------
1388 inline double vtkMath::Normalize(double v[3])
1389 {
1390  double den = vtkMath::Norm( v );
1391  if ( den != 0.0 )
1392  {
1393  for (int i=0; i < 3; ++i)
1394  {
1395  v[i] /= den;
1396  }
1397  }
1398  return den;
1399 }
1400 
1401 //----------------------------------------------------------------------------
1402 inline float vtkMath::Normalize2D(float v[3])
1403 {
1404  float den = vtkMath::Norm2D( v );
1405  if ( den != 0.0 )
1406  {
1407  for (int i=0; i < 2; ++i)
1408  {
1409  v[i] /= den;
1410  }
1411  }
1412  return den;
1413 }
1414 
1415 //----------------------------------------------------------------------------
1416 inline double vtkMath::Normalize2D(double v[3])
1417 {
1418  double den = vtkMath::Norm2D( v );
1419  if ( den != 0.0 )
1420  {
1421  for (int i=0; i < 2; ++i)
1422  {
1423  v[i] /= den;
1424  }
1425  }
1426  return den;
1427 }
1428 
1429 //----------------------------------------------------------------------------
1430 inline float vtkMath::Determinant3x3(const float c1[3],
1431  const float c2[3],
1432  const float c3[3])
1433 {
1434  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1435  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1436 }
1437 
1438 //----------------------------------------------------------------------------
1439 inline double vtkMath::Determinant3x3(const double c1[3],
1440  const double c2[3],
1441  const double c3[3])
1442 {
1443  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1444  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1445 }
1446 
1447 //----------------------------------------------------------------------------
1448 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
1449  double b1, double b2, double b3,
1450  double c1, double c2, double c3)
1451 {
1452  return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
1453  - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
1454  + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
1455 }
1456 
1457 //----------------------------------------------------------------------------
1458 inline float vtkMath::Distance2BetweenPoints(const float p1[3],
1459  const float p2[3])
1460 {
1461  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1462  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1463  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1464 }
1465 
1466 //----------------------------------------------------------------------------
1467 inline double vtkMath::Distance2BetweenPoints(const double p1[3],
1468  const double p2[3])
1469 {
1470  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1471  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1472  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1473 }
1474 
1475 //----------------------------------------------------------------------------
1476 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1477 inline void vtkMath::Cross(const float a[3], const float b[3], float c[3])
1478 {
1479  float Cx = a[1] * b[2] - a[2] * b[1];
1480  float Cy = a[2] * b[0] - a[0] * b[2];
1481  float Cz = a[0] * b[1] - a[1] * b[0];
1482  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1483 }
1484 
1485 //----------------------------------------------------------------------------
1486 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1487 inline void vtkMath::Cross(const double a[3], const double b[3], double c[3])
1488 {
1489  double Cx = a[1] * b[2] - a[2] * b[1];
1490  double Cy = a[2] * b[0] - a[0] * b[2];
1491  double Cz = a[0] * b[1] - a[1] * b[0];
1492  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1493 }
1494 
1495 //----------------------------------------------------------------------------
1496 template<class T>
1497 inline double vtkDeterminant3x3(const T A[3][3])
1498 {
1499  return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1500  A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1501  A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1502 }
1503 
1504 //----------------------------------------------------------------------------
1505 inline double vtkMath::Determinant3x3(const float A[3][3])
1506 {
1507  return vtkDeterminant3x3( A );
1508 }
1509 
1510 //----------------------------------------------------------------------------
1511 inline double vtkMath::Determinant3x3(const double A[3][3])
1512 {
1513  return vtkDeterminant3x3( A );
1514 }
1515 
1516 //----------------------------------------------------------------------------
1517 template<class T>
1518 inline T vtkMath::ClampValue(const T & value, const T & min, const T & max)
1519 {
1520  assert("pre: valid_range" && min<=max);
1521 
1522 #if __cplusplus >= 201703L
1523  return std::clamp(value, min, max);
1524 #else
1525  // compilers are good at optimizing the ternary operator,
1526  // use '<' since it is preferred by STL for custom types
1527  T v = (min < value ? value : min);
1528  return (v < max ? v : max);
1529 #endif
1530 }
1531 
1532 //----------------------------------------------------------------------------
1533 inline void vtkMath::ClampValue(double *value, const double range[2])
1534 {
1535  if (value && range)
1536  {
1537  assert("pre: valid_range" && range[0]<=range[1]);
1538 
1539  *value = vtkMath::ClampValue(*value, range[0], range[1]);
1540  }
1541 }
1542 
1543 //----------------------------------------------------------------------------
1545  double value, const double range[2], double *clamped_value)
1546 {
1547  if (range && clamped_value)
1548  {
1549  assert("pre: valid_range" && range[0]<=range[1]);
1550 
1551  *clamped_value = vtkMath::ClampValue(value, range[0], range[1]);
1552  }
1553 }
1554 
1555 // ---------------------------------------------------------------------------
1557  const double range[2])
1558 {
1559  assert("pre: valid_range" && range[0]<=range[1]);
1560 
1561  double result;
1562  if (range[0] == range[1])
1563  {
1564  result = 0.0;
1565  }
1566  else
1567  {
1568  // clamp
1569  result=vtkMath::ClampValue(value, range[0], range[1]);
1570 
1571  // normalize
1572  result=( result - range[0] ) / ( range[1] - range[0] );
1573  }
1574 
1575  assert("post: valid_result" && result>=0.0 && result<=1.0);
1576 
1577  return result;
1578 }
1579 
1580 //-----------------------------------------------------------------------------
1581 template<class T1, class T2>
1582 inline void vtkMath::TensorFromSymmetricTensor(const T1 symmTensor[9], T2 tensor[9])
1583 {
1584  for (int i = 0; i < 3; ++i)
1585  {
1586  tensor[4*i] = symmTensor[i];
1587  }
1588  tensor[1] = tensor[3] = symmTensor[3];
1589  tensor[2] = tensor[6] = symmTensor[5];
1590  tensor[5] = tensor[7] = symmTensor[4];
1591 }
1592 
1593 //-----------------------------------------------------------------------------
1594 template<class T>
1595 inline void vtkMath::TensorFromSymmetricTensor(T tensor[9])
1596 {
1597  tensor[6] = tensor[5]; // XZ
1598  tensor[7] = tensor[4]; // YZ
1599  tensor[8] = tensor[2]; // ZZ
1600  tensor[4] = tensor[1]; // YY
1601  tensor[5] = tensor[7]; // YZ
1602  tensor[2] = tensor[6]; // XZ
1603  tensor[1] = tensor[3]; // XY
1604 }
1605 
1606 namespace vtk_detail
1607 {
1608 // Can't specialize templates inside a template class, so we move the impl here.
1609 template <typename OutT>
1610 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
1611 { // OutT is integral -- clamp and round
1612  double min = static_cast<double>(vtkTypeTraits<OutT>::Min());
1613  double max = static_cast<double>(vtkTypeTraits<OutT>::Max());
1614  val = vtkMath::ClampValue(val, min, max);
1615  *ret = static_cast<OutT>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
1616 }
1617 template <>
1618 inline void RoundDoubleToIntegralIfNecessary(double val, double* retVal)
1619 { // OutT is double: passthrough
1620  *retVal = val;
1621 }
1622 template <>
1623 inline void RoundDoubleToIntegralIfNecessary(double val, float* retVal)
1624 { // OutT is float -- just clamp (as doubles, then the cast to float is well-defined.)
1625  double min = static_cast<double>(vtkTypeTraits<float>::Min());
1626  double max = static_cast<double>(vtkTypeTraits<float>::Max());
1627  val = vtkMath::ClampValue(val, min, max);
1628  *retVal = static_cast<float>(val);
1629 }
1630 } // end namespace vtk_detail
1631 
1632 //-----------------------------------------------------------------------------
1633 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
1634 #define VTK_MATH_ISINF_IS_INLINE
1635 inline vtkTypeBool vtkMath::IsInf(double x)
1636 {
1637 #if defined(VTK_HAS_STD_ISINF)
1638  return std::isinf(x);
1639 #else
1640  return (isinf(x) != 0); // Force conversion to bool
1641 #endif
1642 }
1643 #endif
1644 
1645 //-----------------------------------------------------------------------------
1646 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
1647 #define VTK_MATH_ISNAN_IS_INLINE
1648 inline vtkTypeBool vtkMath::IsNan(double x)
1649 {
1650 #if defined(VTK_HAS_STD_ISNAN)
1651  return std::isnan(x);
1652 #else
1653  return (isnan(x) != 0); // Force conversion to bool
1654 #endif
1655 }
1656 #endif
1657 
1658 //-----------------------------------------------------------------------------
1659 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
1660 #define VTK_MATH_ISFINITE_IS_INLINE
1661 inline bool vtkMath::IsFinite(double x)
1662 {
1663 #if defined(VTK_HAS_STD_ISFINITE)
1664  return std::isfinite(x);
1665 #elif defined(VTK_HAS_ISFINITE)
1666  return (isfinite(x) != 0); // Force conversion to bool
1667 #else
1668  return (finite(x) != 0); // Force conversion to bool
1669 #endif
1670 }
1671 #endif
1672 
1673 #endif
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
Definition: vtkMath.h:372
static bool IsFinite(double x)
Test if a number has finite value i.e.
static float Dot2D(const float x[2], const float y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:568
~vtkMath() override
Definition: vtkMath.h:1290
static float Dot(const float a[3], const float b[3])
Dot product of two 3-vectors (float version).
Definition: vtkMath.h:404
static void TensorFromSymmetricTensor(const T1 symmTensor[6], T2 tensor[9])
Convert a 6-Component symmetric tensor into a 9-Component tensor, no allocation performed.
abstract base class for most VTK objects
Definition: vtkObject.h:59
static void LabToXYZ(const double lab[3], double xyz[3])
Convert color from the CIE-L*ab system to CIE XYZ.
Definition: vtkMath.h:1045
static double Pi()
A mathematical constant.
Definition: vtkMath.h:101
static bool IsPowerOfTwo(vtkTypeUInt64 x)
Returns true if integer is a power of two.
Definition: vtkMath.h:1323
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:431
void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
Definition: vtkMath.h:1610
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
Definition: vtkMath.h:638
static vtkSmartPointer< vtkMathInternal > Internal
Definition: vtkMath.h:1292
static vtkTypeBool IsInf(double x)
Test if a number is equal to the special floating point value infinity.
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:1016
static vtkTypeBool IsNan(double x)
Test if a number is equal to the special floating point value Not-A-Number (Nan). ...
static int Round(float f)
Rounds a float to the nearest integer.
Definition: vtkMath.h:123
vtkMath()
Definition: vtkMath.h:1289
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:1010
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:1556
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:418
static float Normalize2D(float v[2])
Normalize (in place) a 2-vector.
int vtkTypeBool
Definition: vtkABI.h:69
static void Add(const double a[3], const double b[3], double c[3])
Addition of two 3-vectors (double version).
Definition: vtkMath.h:330
static double Dot(const double a[3], const double b[3])
Dot product of two 3-vectors (double version).
Definition: vtkMath.h:411
static void XYZToRGB(const double xyz[3], double rgb[3])
Convert color from the CIE XYZ system to RGB.
Definition: vtkMath.h:1069
static float Norm2D(const float x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:611
static void UninitializeBounds(double bounds[6])
Set the bounds to an uninitialized state.
Definition: vtkMath.h:1120
double vtkDeterminant3x3(const T A[3][3])
Definition: vtkMath.h:1497
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1330
static void RGBToXYZ(const double rgb[3], double xyz[3])
Convert color from the RGB system to CIE XYZ.
Definition: vtkMath.h:1081
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1361
a simple class to control print indentation
Definition: vtkIndent.h:39
static void Subtract(const float a[3], const float b[3], float c[3])
Subtraction of two 3-vectors (float version).
Definition: vtkMath.h:340
static void Subtract(const double a[3], const double b[3], double c[3])
Subtraction of two 3-vectors (double version).
Definition: vtkMath.h:350
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
Definition: vtkMath.h:1344
static double Determinant3x3(const float A[3][3])
Return the determinant of a 3x3 matrix.
Definition: vtkMath.h:1505
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:54
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:646
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
Definition: vtkMath.h:1299
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:1036
Park and Miller Sequence of pseudo random numbers.
static void MultiplyScalar(double a[3], double s)
Multiplies a 3-vector by a scalar (double version).
Definition: vtkMath.h:383
#define VTK_SIZEHINT(...)
static void RGBToLab(const double rgb[3], double lab[3])
Convert color from the RGB system to CIE-L*ab.
Definition: vtkMath.h:1096
static float DegreesFromRadians(float radians)
Convert radians into degrees.
Definition: vtkMath.h:1311
static float Normalize(float v[3])
Normalize (in place) a 3-vector.
Definition: vtkMath.h:1374
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:596
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:582
static int Ceil(double x)
Rounds a double to the nearest integer not less than itself.
Definition: vtkMath.h:1353
performs common math operations
Definition: vtkMath.h:91
static double Dot2D(const double x[2], const double y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:575
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:134
static float Norm(const float v[3])
Compute the norm of 3-vector (float version).
Definition: vtkMath.h:464
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
Definition: vtkMath.h:361
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:1030
static T ClampValue(const T &value, const T &min, const T &max)
Clamp some value against a range, return the result.
Definition: vtkMath.h:1518
static double Norm2D(const double x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:619
static int Round(double f)
Definition: vtkMath.h:125
static double Norm(const double v[3])
Compute the norm of 3-vector (double version).
Definition: vtkMath.h:471
static void MultiplyScalar2D(double a[2], double s)
Multiplies a 2-vector by a scalar (double version).
Definition: vtkMath.h:394
static float Distance2BetweenPoints(const float p1[3], const float p2[3])
Compute distance squared between two points p1 and p2.
Definition: vtkMath.h:1458
static void LabToRGB(const double lab[3], double rgb[3])
Convert color from the CIE-L*ab system to RGB.
Definition: vtkMath.h:1108
static float Norm(const float *x, int n)
Compute the norm of n-vector.
static void Cross(const float a[3], const float b[3], float c[3])
Cross product of two 3-vectors.
Definition: vtkMath.h:1477
Gaussian sequence of pseudo random numbers implemented with the Box-Mueller transform.
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:649
static void Add(const float a[3], const float b[3], float c[3])
Addition of two 3-vectors (float version).
Definition: vtkMath.h:320
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:32
static vtkTypeBool AreBoundsInitialized(const double bounds[6])
Are the bounds initialized?
Definition: vtkMath.h:1134
#define max(a, b)
represent and manipulate 3D points
Definition: vtkPoints.h:39
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition: vtkMath.h:1368
static void XYZToLab(const double xyz[3], double lab[3])
Convert Color from the CIE XYZ system to CIE-L*ab.
Definition: vtkMath.h:1057