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 =========================================================================*/
39 #ifndef vtkMath_h
40 #define vtkMath_h
41 
42 #include "vtkCommonCoreModule.h" // For export macro
43 #include "vtkObject.h"
44 #include "vtkTypeTraits.h" // For type traits
45 #include "vtkSmartPointer.h" // For vtkSmartPointer.
46 
47 #include "vtkMathConfigure.h" // For <cmath> and VTK_HAS_ISNAN etc.
48 
49 #include <cassert> // assert() in inline implementations.
50 
51 #ifndef DBL_MIN
52 # define VTK_DBL_MIN 2.2250738585072014e-308
53 #else // DBL_MIN
54 # define VTK_DBL_MIN DBL_MIN
55 #endif // DBL_MIN
56 
57 #ifndef DBL_EPSILON
58 # define VTK_DBL_EPSILON 2.2204460492503131e-16
59 #else // DBL_EPSILON
60 # define VTK_DBL_EPSILON DBL_EPSILON
61 #endif // DBL_EPSILON
62 
63 #ifndef VTK_DBL_EPSILON
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 #endif // VTK_DBL_EPSILON
70 
71 class vtkDataArray;
72 class vtkPoints;
73 class vtkMathInternal;
76 
77 namespace vtk_detail
78 {
79 // forward declaration
80 template <typename OutT>
81 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret);
82 } // end namespace vtk_detail
83 
84 class VTKCOMMONCORE_EXPORT vtkMath : public vtkObject
85 {
86 public:
87  static vtkMath *New();
88  vtkTypeMacro(vtkMath,vtkObject);
89  void PrintSelf(ostream& os, vtkIndent indent) override;
90 
94  static double Pi() { return 3.141592653589793; };
95 
97 
100  static float RadiansFromDegrees( float degrees);
101  static double RadiansFromDegrees( double degrees);
103 
105 
108  static float DegreesFromRadians( float radians);
109  static double DegreesFromRadians( double radians);
111 
115  static int Round(float f) {
116  return static_cast<int>( f + ( f >= 0.0 ? 0.5 : -0.5 ) ); }
117  static int Round(double f) {
118  return static_cast<int>( f + ( f >= 0.0 ? 0.5 : -0.5 ) ); }
119 
124  template <typename OutT>
125  static void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
126  {
127  // Can't specialize template methods in a template class, so we move the
128  // implementations to a external namespace.
130  }
131 
137  static int Floor(double x);
138 
144  static int Ceil(double x);
145 
151  static int CeilLog2(vtkTypeUInt64 x);
152 
157  template<class T>
158  static T Min(const T & a, const T & b);
159 
164  template<class T>
165  static T Max(const T & a, const T & b);
166 
170  static bool IsPowerOfTwo(vtkTypeUInt64 x);
171 
177  static int NearestPowerOfTwo(int x);
178 
183  static vtkTypeInt64 Factorial( int N );
184 
190  static vtkTypeInt64 Binomial( int m, int n );
191 
202  static int* BeginCombination( int m, int n );
203 
214  static int NextCombination( int m, int n, int* combination );
215 
219  static void FreeCombination( int* combination);
220 
236  static void RandomSeed(int s);
237 
249  static int GetSeed();
250 
264  static double Random();
265 
278  static double Random( double min, double max );
279 
292  static double Gaussian();
293 
306  static double Gaussian( double mean, double std );
307 
311  static void Add(const float a[3], const float b[3], float c[3]) {
312  for (int i = 0; i < 3; ++i)
313  c[i] = a[i] + b[i];
314  }
315 
319  static void Add(const double a[3], const double b[3], double c[3]) {
320  for (int i = 0; i < 3; ++i)
321  c[i] = a[i] + b[i];
322  }
323 
327  static void Subtract(const float a[3], const float b[3], float c[3]) {
328  for (int i = 0; i < 3; ++i)
329  c[i] = a[i] - b[i];
330  }
331 
335  static void Subtract(const double a[3], const double b[3], double c[3]) {
336  for (int i = 0; i < 3; ++i)
337  c[i] = a[i] - b[i];
338  }
339 
344  static void MultiplyScalar(float a[3], float s) {
345  for (int i = 0; i < 3; ++i)
346  a[i] *= s;
347  }
348 
353  static void MultiplyScalar2D(float a[2], float s) {
354  for (int i = 0; i < 2; ++i)
355  a[i] *= s;
356  }
357 
362  static void MultiplyScalar(double a[3], double s) {
363  for (int i = 0; i < 3; ++i)
364  a[i] *= s;
365  }
366 
371  static void MultiplyScalar2D(double a[2], double s) {
372  for (int i = 0; i < 2; ++i)
373  a[i] *= s;
374  }
375 
379  static float Dot(const float a[3], const float b[3]) {
380  return ( a[0] * b[0] + a[1] * b[1] + a[2] * b[2] );};
381 
385  static double Dot(const double a[3], const double b[3]) {
386  return ( a[0] * b[0] + a[1] * b[1] + a[2] * b[2] );};
387 
391  static void Outer(const float a[3], const float b[3], float C[3][3]) {
392  for (int i=0; i < 3; i++)
393  for (int j=0; j < 3; j++)
394  C[i][j] = a[i] * b[j];
395  }
399  static void Outer(const double a[3], const double b[3], double C[3][3]) {
400  for (int i=0; i < 3; i++)
401  for (int j=0; j < 3; j++)
402  C[i][j] = a[i] * b[j];
403  }
404 
408  static void Cross(const float a[3], const float b[3], float c[3]);
409 
414  static void Cross(const double a[3], const double b[3], double c[3]);
415 
417 
420  static float Norm(const float* x, int n);
421  static double Norm(const double* x, int n);
423 
427  static float Norm(const float v[3]) {
428  return static_cast<float> (sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] ) );};
429 
433  static double Norm(const double v[3]) {
434  return sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] );};
435 
439  static float Normalize(float v[3]);
440 
445  static double Normalize(double v[3]);
446 
448 
455  static void Perpendiculars(const double v1[3], double v2[3], double v3[3],
456  double theta);
457  static void Perpendiculars(const float v1[3], float v2[3], float v3[3],
458  double theta);
460 
462 
467  static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
468  static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
470 
472 
478  static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
479  static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
481 
485  static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
486 
491  static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
492 
496  static double AngleBetweenVectors(const double v1[3], const double v2[3]);
497 
502  static double GaussianAmplitude(const double variance, const double distanceFromMean);
503 
508  static double GaussianAmplitude(const double mean, const double variance, const double position);
509 
515  static double GaussianWeight(const double variance, const double distanceFromMean);
516 
522  static double GaussianWeight(const double mean, const double variance, const double position);
523 
527  static float Dot2D(const float x[2], const float y[2]) {
528  return ( x[0] * y[0] + x[1] * y[1] );};
529 
533  static double Dot2D(const double x[2], const double y[2]) {
534  return ( x[0] * y[0] + x[1] * y[1] );};
535 
539  static void Outer2D(const float x[2], const float y[2], float A[2][2])
540  {
541  for (int i=0; i < 2; i++)
542  {
543  for (int j=0; j < 2; j++)
544  {
545  A[i][j] = x[i] * y[j];
546  }
547  }
548  }
552  static void Outer2D(const double x[2], const double y[2], double A[2][2])
553  {
554  for (int i=0; i < 2; i++)
555  {
556  for (int j=0; j < 2; j++)
557  {
558  A[i][j] = x[i] * y[j];
559  }
560  }
561  }
562 
566  static float Norm2D(const float x[2]) {
567  return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
568 
573  static double Norm2D(const double x[2]) {
574  return sqrt( x[0] * x[0] + x[1] * x[1] );};
575 
579  static float Normalize2D(float v[2]);
580 
585  static double Normalize2D(double v[2]);
586 
590  static float Determinant2x2(const float c1[2], const float c2[2]) {
591  return (c1[0] * c2[1] - c2[0] * c1[1] );};
592 
594 
597  static double Determinant2x2(double a, double b, double c, double d) {
598  return (a * d - b * c);};
599  static double Determinant2x2(const double c1[2], const double c2[2]) {
600  return (c1[0] * c2[1] - c2[0] * c1[1] );};
602 
604 
607  static void LUFactor3x3(float A[3][3], int index[3]);
608  static void LUFactor3x3(double A[3][3], int index[3]);
610 
612 
615  static void LUSolve3x3(const float A[3][3], const int index[3],
616  float x[3]);
617  static void LUSolve3x3(const double A[3][3], const int index[3],
618  double x[3]);
620 
622 
626  static void LinearSolve3x3(const float A[3][3], const float x[3],
627  float y[3]);
628  static void LinearSolve3x3(const double A[3][3], const double x[3],
629  double y[3]);
631 
633 
636  static void Multiply3x3(const float A[3][3], const float in[3],
637  float out[3]);
638  static void Multiply3x3(const double A[3][3], const double in[3],
639  double out[3]);
641 
643 
646  static void Multiply3x3(const float A[3][3], const float B[3][3],
647  float C[3][3]);
648  static void Multiply3x3(const double A[3][3], const double B[3][3],
649  double C[3][3]);
651 
657  static void MultiplyMatrix(double **A, double **B,
658  unsigned int rowA, unsigned int colA,
659  unsigned int rowB, unsigned int colB,
660  double **C);
661 
663 
667  static void Transpose3x3(const float A[3][3], float AT[3][3]);
668  static void Transpose3x3(const double A[3][3], double AT[3][3]);
670 
672 
676  static void Invert3x3(const float A[3][3], float AI[3][3]);
677  static void Invert3x3(const double A[3][3], double AI[3][3]);
679 
681 
684  static void Identity3x3(float A[3][3]);
685  static void Identity3x3(double A[3][3]);
687 
689 
692  static double Determinant3x3(float A[3][3]);
693  static double Determinant3x3(double A[3][3]);
695 
699  static float Determinant3x3(const float c1[3],
700  const float c2[3],
701  const float c3[3]);
702 
706  static double Determinant3x3(const double c1[3],
707  const double c2[3],
708  const double c3[3]);
709 
716  static double Determinant3x3(double a1, double a2, double a3,
717  double b1, double b2, double b3,
718  double c1, double c2, double c3);
719 
721 
728  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
729  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
731 
733 
741  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
742  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
744 
746 
752  static void MultiplyQuaternion( const float q1[4], const float q2[4], float q[4] );
753  static void MultiplyQuaternion( const double q1[4], const double q2[4], double q[4] );
755 
757 
761  static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3]);
762  static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3]);
764 
766 
770  static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3]);
771  static void RotateVectorByWXYZ(const double v[3], const double q[4], double r[3]);
773 
775 
780  static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
781  static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
783 
785 
791  static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
792  static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
794 
796 
805  static void SingularValueDecomposition3x3(const float A[3][3],
806  float U[3][3], float w[3],
807  float VT[3][3]);
808  static void SingularValueDecomposition3x3(const double A[3][3],
809  double U[3][3], double w[3],
810  double VT[3][3]);
812 
819  static int SolveLinearSystem(double **A, double *x, int size);
820 
827  static int InvertMatrix(double **A, double **AI, int size);
828 
834  static int InvertMatrix(double **A, double **AI, int size,
835  int *tmp1Size, double *tmp2Size);
836 
859  static int LUFactorLinearSystem(double **A, int *index, int size);
860 
866  static int LUFactorLinearSystem(double **A, int *index, int size,
867  double *tmpSize);
868 
877  static void LUSolveLinearSystem(double **A, int *index,
878  double *x, int size);
879 
888  static double EstimateMatrixCondition(double **A, int size);
889 
891 
899  static int Jacobi(float **a, float *w, float **v);
900  static int Jacobi(double **a, double *w, double **v);
902 
904 
913  static int JacobiN(float **a, int n, float *w, float **v);
914  static int JacobiN(double **a, int n, double *w, double **v);
916 
930  static int SolveHomogeneousLeastSquares(int numberOfSamples, double **xt, int xOrder,
931  double **mt);
932 
947  static int SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
948  double **yt, int yOrder, double **mt, int checkHomogeneous=1);
949 
951 
958  static void RGBToHSV(const float rgb[3], float hsv[3])
959  { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
960  static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v);
961  static double* RGBToHSV(const double rgb[3]) VTK_SIZEHINT(3);
962  static double* RGBToHSV(double r, double g, double b) VTK_SIZEHINT(3);
963  static void RGBToHSV(const double rgb[3], double hsv[3])
964  { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
965  static void RGBToHSV(double r, double g, double b, double *h, double *s, double *v);
967 
969 
976  static void HSVToRGB(const float hsv[3], float rgb[3])
977  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
978  static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b);
979  static double* HSVToRGB(const double hsv[3]) VTK_SIZEHINT(3);
980  static double* HSVToRGB(double h, double s, double v) VTK_SIZEHINT(3);
981  static void HSVToRGB(const double hsv[3], double rgb[3])
982  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
983  static void HSVToRGB(double h, double s, double v, double *r, double *g, double *b);
985 
987 
990  static void LabToXYZ(const double lab[3], double xyz[3]) {
991  LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
992  }
993  static void LabToXYZ(double L, double a, double b,
994  double *x, double *y, double *z);
995  static double *LabToXYZ(const double lab[3]) VTK_SIZEHINT(3);
997 
999 
1002  static void XYZToLab(const double xyz[3], double lab[3]) {
1003  XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
1004  }
1005  static void XYZToLab(double x, double y, double z,
1006  double *L, double *a, double *b);
1007  static double *XYZToLab(const double xyz[3]) VTK_SIZEHINT(3);
1009 
1011 
1014  static void XYZToRGB(const double xyz[3], double rgb[3]) {
1015  XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
1016  }
1017  static void XYZToRGB(double x, double y, double z,
1018  double *r, double *g, double *b);
1019  static double *XYZToRGB(const double xyz[3]) VTK_SIZEHINT(3);
1021 
1023 
1026  static void RGBToXYZ(const double rgb[3], double xyz[3]) {
1027  RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
1028  }
1029  static void RGBToXYZ(double r, double g, double b,
1030  double *x, double *y, double *z);
1031  static double *RGBToXYZ(const double rgb[3]) VTK_SIZEHINT(3);
1033 
1035 
1041  static void RGBToLab(const double rgb[3], double lab[3]) {
1042  RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
1043  }
1044  static void RGBToLab(double red, double green, double blue,
1045  double *L, double *a, double *b);
1046  static double *RGBToLab(const double rgb[3]) VTK_SIZEHINT(3);
1048 
1050 
1053  static void LabToRGB(const double lab[3], double rgb[3]) {
1054  LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
1055  }
1056  static void LabToRGB(double L, double a, double b,
1057  double *red, double *green, double *blue);
1058  static double *LabToRGB(const double lab[3]) VTK_SIZEHINT(3);
1060 
1062 
1065  static void UninitializeBounds(double bounds[6]){
1066  bounds[0] = 1.0;
1067  bounds[1] = -1.0;
1068  bounds[2] = 1.0;
1069  bounds[3] = -1.0;
1070  bounds[4] = 1.0;
1071  bounds[5] = -1.0;
1072  }
1074 
1076 
1079  static vtkTypeBool AreBoundsInitialized(const double bounds[6]){
1080  if ( bounds[1]-bounds[0]<0.0 )
1081  {
1082  return 0;
1083  }
1084  return 1;
1085  }
1087 
1092  template<class T>
1093  static T ClampValue(const T & value, const T & min, const T & max);
1094 
1096 
1100  static void ClampValue(double *value, const double range[2]);
1101  static void ClampValue(double value, const double range[2], double *clamped_value);
1102  static void ClampValues(
1103  double *values, int nb_values, const double range[2]);
1104  static void ClampValues(
1105  const double *values, int nb_values, const double range[2], double *clamped_values);
1107 
1114  static double ClampAndNormalizeValue(double value,
1115  const double range[2]);
1116 
1121  template<class T1, class T2>
1122  static void TensorFromSymmetricTensor(T1 symmTensor[6], T2 tensor[9]);
1123 
1129  template<class T>
1130  static void TensorFromSymmetricTensor(T tensor[9]);
1131 
1140  static int GetScalarTypeFittingRange(
1141  double range_min, double range_max,
1142  double scale = 1.0, double shift = 0.0);
1143 
1152  static int GetAdjustedScalarRange(
1153  vtkDataArray *array, int comp, double range[2]);
1154 
1159  static vtkTypeBool ExtentIsWithinOtherExtent(int extent1[6], int extent2[6]);
1160 
1166  static vtkTypeBool BoundsIsWithinOtherBounds(double bounds1[6], double bounds2[6], double delta[3]);
1167 
1173  static vtkTypeBool PointIsWithinBounds(double point[3], double bounds[6], double delta[3]);
1174 
1184  static int PlaneIntersectsAABB(double const bounds[6], double const normal[3],
1185  double const point[3]);
1186 
1196  static double Solve3PointCircle(const double p1[3], const double p2[3], const double p3[3], double center[3]);
1197 
1201  static double Inf();
1202 
1206  static double NegInf();
1207 
1211  static double Nan();
1212 
1216  static vtkTypeBool IsInf(double x);
1217 
1221  static vtkTypeBool IsNan(double x);
1222 
1226  static bool IsFinite(double x);
1227 protected:
1228  vtkMath() {}
1229  ~vtkMath() override {}
1230 
1232 private:
1233  vtkMath(const vtkMath&) = delete;
1234  void operator=(const vtkMath&) = delete;
1235 };
1236 
1237 //----------------------------------------------------------------------------
1238 inline float vtkMath::RadiansFromDegrees( float x )
1239 {
1240  return x * 0.017453292f;
1241 }
1242 
1243 //----------------------------------------------------------------------------
1244 inline double vtkMath::RadiansFromDegrees( double x )
1245 {
1246  return x * 0.017453292519943295;
1247 }
1248 
1249 //----------------------------------------------------------------------------
1250 inline float vtkMath::DegreesFromRadians( float x )
1251 {
1252  return x * 57.2957795131f;
1253 }
1254 
1255 //----------------------------------------------------------------------------
1256 inline double vtkMath::DegreesFromRadians( double x )
1257 {
1258  return x * 57.29577951308232;
1259 }
1260 
1261 //----------------------------------------------------------------------------
1262 inline bool vtkMath::IsPowerOfTwo(vtkTypeUInt64 x)
1263 {
1264  return ((x != 0) & ((x & (x - 1)) == 0));
1265 }
1266 
1267 //----------------------------------------------------------------------------
1268 // Credit goes to Peter Hart and William Lewis on comp.lang.python 1997
1270 {
1271  unsigned int z = ((x > 0) ? x - 1 : 0);
1272  z |= z >> 1;
1273  z |= z >> 2;
1274  z |= z >> 4;
1275  z |= z >> 8;
1276  z |= z >> 16;
1277  return static_cast<int>(z + 1);
1278 }
1279 
1280 //----------------------------------------------------------------------------
1281 // Modify the trunc() operation provided by static_cast<int>() to get floor(),
1282 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1283 inline int vtkMath::Floor(double x)
1284 {
1285  int i = static_cast<int>(x);
1286  return i - ( i > x );
1287 }
1288 
1289 //----------------------------------------------------------------------------
1290 // Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1291 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1292 inline int vtkMath::Ceil(double x)
1293 {
1294  int i = static_cast<int>(x);
1295  return i + ( i < x );
1296 }
1297 
1298 //----------------------------------------------------------------------------
1299 template<class T>
1300 inline T vtkMath::Min(const T & a, const T & b)
1301 {
1302  return (b <= a ? b : a);
1303 }
1304 
1305 //----------------------------------------------------------------------------
1306 template<class T>
1307 inline T vtkMath::Max(const T & a, const T & b)
1308 {
1309  return (b > a ? b : a);
1310 }
1311 
1312 //----------------------------------------------------------------------------
1313 inline float vtkMath::Normalize(float v[3])
1314 {
1315  float den = vtkMath::Norm( v );
1316  if ( den != 0.0 )
1317  {
1318  for (int i=0; i < 3; i++)
1319  {
1320  v[i] /= den;
1321  }
1322  }
1323  return den;
1324 }
1325 
1326 //----------------------------------------------------------------------------
1327 inline double vtkMath::Normalize(double v[3])
1328 {
1329  double den = vtkMath::Norm( v );
1330  if ( den != 0.0 )
1331  {
1332  for (int i=0; i < 3; i++)
1333  {
1334  v[i] /= den;
1335  }
1336  }
1337  return den;
1338 }
1339 
1340 //----------------------------------------------------------------------------
1341 inline float vtkMath::Normalize2D(float v[3])
1342 {
1343  float den = vtkMath::Norm2D( v );
1344  if ( den != 0.0 )
1345  {
1346  for (int i=0; i < 2; i++)
1347  {
1348  v[i] /= den;
1349  }
1350  }
1351  return den;
1352 }
1353 
1354 //----------------------------------------------------------------------------
1355 inline double vtkMath::Normalize2D(double v[3])
1356 {
1357  double den = vtkMath::Norm2D( v );
1358  if ( den != 0.0 )
1359  {
1360  for (int i=0; i < 2; i++)
1361  {
1362  v[i] /= den;
1363  }
1364  }
1365  return den;
1366 }
1367 
1368 //----------------------------------------------------------------------------
1369 inline float vtkMath::Determinant3x3(const float c1[3],
1370  const float c2[3],
1371  const float c3[3])
1372 {
1373  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1374  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1375 }
1376 
1377 //----------------------------------------------------------------------------
1378 inline double vtkMath::Determinant3x3(const double c1[3],
1379  const double c2[3],
1380  const double c3[3])
1381 {
1382  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1383  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1384 }
1385 
1386 //----------------------------------------------------------------------------
1387 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
1388  double b1, double b2, double b3,
1389  double c1, double c2, double c3)
1390 {
1391  return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
1392  - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
1393  + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
1394 }
1395 
1396 //----------------------------------------------------------------------------
1397 inline float vtkMath::Distance2BetweenPoints(const float p1[3],
1398  const float p2[3])
1399 {
1400  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1401  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1402  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1403 }
1404 
1405 //----------------------------------------------------------------------------
1406 inline double vtkMath::Distance2BetweenPoints(const double p1[3],
1407  const double p2[3])
1408 {
1409  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1410  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1411  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1412 }
1413 
1414 //----------------------------------------------------------------------------
1415 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1416 inline void vtkMath::Cross(const float a[3], const float b[3], float c[3])
1417 {
1418  float Cx = a[1] * b[2] - a[2] * b[1];
1419  float Cy = a[2] * b[0] - a[0] * b[2];
1420  float Cz = a[0] * b[1] - a[1] * b[0];
1421  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1422 }
1423 
1424 //----------------------------------------------------------------------------
1425 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1426 inline void vtkMath::Cross(const double a[3], const double b[3], double c[3])
1427 {
1428  double Cx = a[1] * b[2] - a[2] * b[1];
1429  double Cy = a[2] * b[0] - a[0] * b[2];
1430  double Cz = a[0] * b[1] - a[1] * b[0];
1431  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1432 }
1433 
1434 //----------------------------------------------------------------------------
1435 template<class T>
1436 inline double vtkDeterminant3x3(T A[3][3])
1437 {
1438  return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1439  A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1440  A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1441 }
1442 
1443 //----------------------------------------------------------------------------
1444 inline double vtkMath::Determinant3x3(float A[3][3])
1445 {
1446  return vtkDeterminant3x3( A );
1447 }
1448 
1449 //----------------------------------------------------------------------------
1450 inline double vtkMath::Determinant3x3(double A[3][3])
1451 {
1452  return vtkDeterminant3x3( A );
1453 }
1454 
1455 //----------------------------------------------------------------------------
1456 template<class T>
1457 inline T vtkMath::ClampValue(const T & value, const T & min, const T & max)
1458 {
1459  assert("pre: valid_range" && min<=max);
1460 
1461  if (value < min)
1462  {
1463  return min;
1464  }
1465 
1466  if (value > max)
1467  {
1468  return max;
1469  }
1470 
1471  return value;
1472 }
1473 
1474 //----------------------------------------------------------------------------
1475 inline void vtkMath::ClampValue(double *value, const double range[2])
1476 {
1477  if (value && range)
1478  {
1479  assert("pre: valid_range" && range[0]<=range[1]);
1480 
1481  if (*value < range[0])
1482  {
1483  *value = range[0];
1484  }
1485  else if (*value > range[1])
1486  {
1487  *value = range[1];
1488  }
1489  }
1490 }
1491 
1492 //----------------------------------------------------------------------------
1494  double value, const double range[2], double *clamped_value)
1495 {
1496  if (range && clamped_value)
1497  {
1498  assert("pre: valid_range" && range[0]<=range[1]);
1499 
1500  if (value < range[0])
1501  {
1502  *clamped_value = range[0];
1503  }
1504  else if (value > range[1])
1505  {
1506  *clamped_value = range[1];
1507  }
1508  else
1509  {
1510  *clamped_value = value;
1511  }
1512  }
1513 }
1514 
1515 // ---------------------------------------------------------------------------
1517  const double range[2])
1518 {
1519  assert("pre: valid_range" && range[0]<=range[1]);
1520 
1521  double result;
1522  if(range[0]==range[1])
1523  {
1524  result=0.0;
1525  }
1526  else
1527  {
1528  // clamp
1529  if(value<range[0])
1530  {
1531  result=range[0];
1532  }
1533  else
1534  {
1535  if(value>range[1])
1536  {
1537  result=range[1];
1538  }
1539  else
1540  {
1541  result=value;
1542  }
1543  }
1544 
1545  // normalize
1546  result=( result - range[0] ) / ( range[1] - range[0] );
1547  }
1548 
1549  assert("post: valid_result" && result>=0.0 && result<=1.0);
1550 
1551  return result;
1552 }
1553 
1554 //-----------------------------------------------------------------------------
1555 template<class T1, class T2>
1556 inline void vtkMath::TensorFromSymmetricTensor(T1 symmTensor[9], T2 tensor[9])
1557 {
1558  for (int i = 0; i < 3; i++)
1559  {
1560  tensor[4*i] = symmTensor[i];
1561  }
1562  tensor[1] = tensor[3] = symmTensor[3];
1563  tensor[2] = tensor[6] = symmTensor[5];
1564  tensor[5] = tensor[7] = symmTensor[4];
1565 }
1566 
1567 //-----------------------------------------------------------------------------
1568 template<class T>
1569 inline void vtkMath::TensorFromSymmetricTensor(T tensor[9])
1570 {
1571  tensor[6] = tensor[5]; // XZ
1572  tensor[7] = tensor[4]; // YZ
1573  tensor[8] = tensor[2]; // ZZ
1574  tensor[4] = tensor[1]; // YY
1575  tensor[5] = tensor[7]; // YZ
1576  tensor[2] = tensor[6]; // XZ
1577  tensor[1] = tensor[3]; // XY
1578 }
1579 
1580 namespace vtk_detail
1581 {
1582 // Can't specialize templates inside a template class, so we move the impl here.
1583 template <typename OutT>
1584 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
1585 { // OutT is integral -- clamp and round
1586  val = vtkMath::Max(val, static_cast<double>(vtkTypeTraits<OutT>::Min()));
1587  val = vtkMath::Min(val, static_cast<double>(vtkTypeTraits<OutT>::Max()));
1588  *ret = static_cast<OutT>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
1589 }
1590 template <>
1591 inline void RoundDoubleToIntegralIfNecessary(double val, double* retVal)
1592 { // OutT is double: passthrough
1593  *retVal = val;
1594 }
1595 template <>
1596 inline void RoundDoubleToIntegralIfNecessary(double val, float* retVal)
1597 { // OutT is float -- just clamp
1598  val = vtkMath::Max(val, static_cast<double>(vtkTypeTraits<float>::Min()));
1599  val = vtkMath::Min(val, static_cast<double>(vtkTypeTraits<float>::Max()));
1600  *retVal = static_cast<float>(val);
1601 }
1602 } // end namespace vtk_detail
1603 
1604 //-----------------------------------------------------------------------------
1605 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
1606 #define VTK_MATH_ISINF_IS_INLINE
1607 inline vtkTypeBool vtkMath::IsInf(double x)
1608 {
1609 #if defined(VTK_HAS_STD_ISINF)
1610  return std::isinf(x);
1611 #else
1612  return (isinf(x) != 0); // Force conversion to bool
1613 #endif
1614 }
1615 #endif
1616 
1617 //-----------------------------------------------------------------------------
1618 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
1619 #define VTK_MATH_ISNAN_IS_INLINE
1620 inline vtkTypeBool vtkMath::IsNan(double x)
1621 {
1622 #if defined(VTK_HAS_STD_ISNAN)
1623  return std::isnan(x);
1624 #else
1625  return (isnan(x) != 0); // Force conversion to bool
1626 #endif
1627 }
1628 #endif
1629 
1630 //-----------------------------------------------------------------------------
1631 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
1632 #define VTK_MATH_ISFINITE_IS_INLINE
1633 inline bool vtkMath::IsFinite(double x)
1634 {
1635 #if defined(VTK_HAS_STD_ISFINITE)
1636  return std::isfinite(x);
1637 #elif defined(VTK_HAS_ISFINITE)
1638  return (isfinite(x) != 0); // Force conversion to bool
1639 #else
1640  return (finite(x) != 0); // Force conversion to bool
1641 #endif
1642 }
1643 #endif
1644 
1645 #endif
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
Definition: vtkMath.h:353
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:527
~vtkMath() override
Definition: vtkMath.h:1229
static float Dot(const float a[3], const float b[3])
Dot product of two 3-vectors (float version).
Definition: vtkMath.h:379
abstract base class for most VTK objects
Definition: vtkObject.h:53
static void LabToXYZ(const double lab[3], double xyz[3])
Convert color from the CIE-L*ab system to CIE XYZ.
Definition: vtkMath.h:990
static double Pi()
A mathematical constant.
Definition: vtkMath.h:94
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static bool IsPowerOfTwo(vtkTypeUInt64 x)
Returns true if integer is a power of two.
Definition: vtkMath.h:1262
void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
Definition: vtkMath.h:1584
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
Definition: vtkMath.h:590
static vtkSmartPointer< vtkMathInternal > Internal
Definition: vtkMath.h:1231
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:963
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:115
vtkMath()
Definition: vtkMath.h:1228
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:958
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:1516
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:319
static double Dot(const double a[3], const double b[3])
Dot product of two 3-vectors (double-precision version).
Definition: vtkMath.h:385
static void XYZToRGB(const double xyz[3], double rgb[3])
Convert color from the CIE XYZ system to RGB.
Definition: vtkMath.h:1014
static float Norm2D(const float x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:566
static void UninitializeBounds(double bounds[6])
Set the bounds to an uninitialized state.
Definition: vtkMath.h:1065
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1269
static void RGBToXYZ(const double rgb[3], double xyz[3])
Convert color from the RGB system to CIE XYZ.
Definition: vtkMath.h:1026
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1300
a simple class to control print indentation
Definition: vtkIndent.h:33
static void Subtract(const float a[3], const float b[3], float c[3])
Subtraction of two 3-vectors (float version).
Definition: vtkMath.h:327
static void Subtract(const double a[3], const double b[3], double c[3])
Subtraction of two 3-vectors (double version).
Definition: vtkMath.h:335
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
Definition: vtkMath.h:1283
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:48
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:597
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
Definition: vtkMath.h:1238
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:981
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:362
#define VTK_SIZEHINT(...)
static double Determinant3x3(float A[3][3])
Return the determinant of a 3x3 matrix.
Definition: vtkMath.h:1444
static void RGBToLab(const double rgb[3], double lab[3])
Convert color from the RGB system to CIE-L*ab.
Definition: vtkMath.h:1041
static float DegreesFromRadians(float radians)
Convert radians into degrees.
Definition: vtkMath.h:1250
static float Normalize(float v[3])
Normalize (in place) a 3-vector.
Definition: vtkMath.h:1313
static void Outer2D(const double x[2], const double y[2], double A[2][2])
Outer product of two 2-vectors (float version).
Definition: vtkMath.h:552
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:539
static int Ceil(double x)
Rounds a double to the nearest integer not less than itself.
Definition: vtkMath.h:1292
performs common math operations
Definition: vtkMath.h:84
static double Dot2D(const double x[2], const double y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:533
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:125
static float Norm(const float v[3])
Compute the norm of 3-vector.
Definition: vtkMath.h:427
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
Definition: vtkMath.h:344
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:976
static void Outer(const double a[3], const double b[3], double C[3][3])
Outer product of two 3-vectors (double-precision version).
Definition: vtkMath.h:399
static T ClampValue(const T &value, const T &min, const T &max)
Clamp some value against a range, return the result.
Definition: vtkMath.h:1457
static void TensorFromSymmetricTensor(T1 symmTensor[6], T2 tensor[9])
Convert a 6-Component symmetric tensor into a 9-Component tensor, no allocation performed.
static double Norm2D(const double x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:573
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:391
static int Round(double f)
Definition: vtkMath.h:117
static double Norm(const double v[3])
Compute the norm of 3-vector (double-precision version).
Definition: vtkMath.h:433
static void MultiplyScalar2D(double a[2], double s)
Multiplies a 2-vector by a scalar (double version).
Definition: vtkMath.h:371
static float Distance2BetweenPoints(const float p1[3], const float p2[3])
Compute distance squared between two points p1 and p2.
Definition: vtkMath.h:1397
static void LabToRGB(const double lab[3], double rgb[3])
Convert color from the CIE-L*ab system to RGB.
Definition: vtkMath.h:1053
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
double vtkDeterminant3x3(T A[3][3])
Definition: vtkMath.h:1436
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:1416
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:599
static void Add(const float a[3], const float b[3], float c[3])
Addition of two 3-vectors (float version).
Definition: vtkMath.h:311
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:29
static vtkTypeBool AreBoundsInitialized(const double bounds[6])
Are the bounds initialized?
Definition: vtkMath.h:1079
#define max(a, b)
represent and manipulate 3D points
Definition: vtkPoints.h:33
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition: vtkMath.h:1307
static void XYZToLab(const double xyz[3], double lab[3])
Convert Color from the CIE XYZ system to CIE-L*ab.
Definition: vtkMath.h:1002