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 
57 #ifndef DBL_MIN
58 # define VTK_DBL_MIN 2.2250738585072014e-308
59 #else // DBL_MIN
60 # define VTK_DBL_MIN DBL_MIN
61 #endif // DBL_MIN
62 
63 #ifndef DBL_EPSILON
64 # define VTK_DBL_EPSILON 2.2204460492503131e-16
65 #else // DBL_EPSILON
66 # define VTK_DBL_EPSILON DBL_EPSILON
67 #endif // DBL_EPSILON
68 
69 #ifndef VTK_DBL_EPSILON
70 # ifndef DBL_EPSILON
71 # define VTK_DBL_EPSILON 2.2204460492503131e-16
72 # else // DBL_EPSILON
73 # define VTK_DBL_EPSILON DBL_EPSILON
74 # endif // DBL_EPSILON
75 #endif // VTK_DBL_EPSILON
76 
77 class vtkDataArray;
78 class vtkPoints;
79 class vtkMathInternal;
82 
83 namespace vtk_detail
84 {
85 // forward declaration
86 template <typename OutT>
87 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret);
88 } // end namespace vtk_detail
89 
90 class VTKCOMMONCORE_EXPORT vtkMath : public vtkObject
91 {
92 public:
93  static vtkMath *New();
94  vtkTypeMacro(vtkMath,vtkObject);
95  void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
96 
100  static double Pi() { return 3.141592653589793; };
101 
103 
106  static float RadiansFromDegrees( float degrees);
107  static double RadiansFromDegrees( double degrees);
109 
111 
114  static float DegreesFromRadians( float radians);
115  static double DegreesFromRadians( double radians);
117 
121  static int Round(float f) {
122  return static_cast<int>( f + ( f >= 0.0 ? 0.5 : -0.5 ) ); }
123  static int Round(double f) {
124  return static_cast<int>( f + ( f >= 0.0 ? 0.5 : -0.5 ) ); }
125 
130  template <typename OutT>
131  static void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
132  {
133  // Can't specialize template methods in a template class, so we move the
134  // implementations to a external namespace.
136  }
137 
143  static int Floor(double x);
144 
150  static int Ceil(double x);
151 
157  static int CeilLog2(vtkTypeUInt64 x);
158 
162  template<class T>
163  static T Min(const T & a, const T & b);
164 
168  template<class T>
169  static T Max(const T & a, const T & b);
170 
174  static bool IsPowerOfTwo(vtkTypeUInt64 x);
175 
181  static int NearestPowerOfTwo(int x);
182 
187  static vtkTypeInt64 Factorial( int N );
188 
194  static vtkTypeInt64 Binomial( int m, int n );
195 
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 
315  static void Add(const float a[3], const float b[3], float c[3]) {
316  for (int i = 0; i < 3; ++i)
317  c[i] = a[i] + b[i];
318  }
319 
323  static void Add(const double a[3], const double b[3], double c[3]) {
324  for (int i = 0; i < 3; ++i)
325  c[i] = a[i] + b[i];
326  }
327 
331  static void Subtract(const float a[3], const float b[3], float c[3]) {
332  for (int i = 0; i < 3; ++i)
333  c[i] = a[i] - b[i];
334  }
335 
339  static void Subtract(const double a[3], const double b[3], double c[3]) {
340  for (int i = 0; i < 3; ++i)
341  c[i] = a[i] - b[i];
342  }
343 
348  static void MultiplyScalar(float a[3], float s) {
349  for (int i = 0; i < 3; ++i)
350  a[i] *= s;
351  }
352 
357  static void MultiplyScalar2D(float a[2], float s) {
358  for (int i = 0; i < 2; ++i)
359  a[i] *= s;
360  }
361 
366  static void MultiplyScalar(double a[3], double s) {
367  for (int i = 0; i < 3; ++i)
368  a[i] *= s;
369  }
370 
375  static void MultiplyScalar2D(double a[2], double s) {
376  for (int i = 0; i < 2; ++i)
377  a[i] *= s;
378  }
379 
383  static float Dot(const float a[3], const float b[3]) {
384  return ( a[0] * b[0] + a[1] * b[1] + a[2] * b[2] );};
385 
389  static double Dot(const double a[3], const double b[3]) {
390  return ( a[0] * b[0] + a[1] * b[1] + a[2] * b[2] );};
391 
395  static void Outer(const float a[3], const float b[3], float C[3][3]) {
396  for (int i=0; i < 3; i++)
397  for (int j=0; j < 3; j++)
398  C[i][j] = a[i] * b[j];
399  }
403  static void Outer(const double a[3], const double b[3], double C[3][3]) {
404  for (int i=0; i < 3; i++)
405  for (int j=0; j < 3; j++)
406  C[i][j] = a[i] * b[j];
407  }
408 
412  static void Cross(const float a[3], const float b[3], float c[3]);
413 
418  static void Cross(const double a[3], const double b[3], double c[3]);
419 
421 
424  static float Norm(const float* x, int n);
425  static double Norm(const double* x, int n);
427 
431  static float Norm(const float v[3]) {
432  return static_cast<float> (sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] ) );};
433 
437  static double Norm(const double v[3]) {
438  return sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] );};
439 
443  static float Normalize(float v[3]);
444 
449  static double Normalize(double v[3]);
450 
452 
459  static void Perpendiculars(const double v1[3], double v2[3], double v3[3],
460  double theta);
461  static void Perpendiculars(const float v1[3], float v2[3], float v3[3],
462  double theta);
464 
466 
471  static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
472  static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
474 
476 
482  static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
483  static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
485 
489  static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
490 
495  static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
496 
500  static double AngleBetweenVectors(const double v1[3], const double v2[3]);
501 
506  static double GaussianAmplitude(const double variance, const double distanceFromMean);
507 
512  static double GaussianAmplitude(const double mean, const double variance, const double position);
513 
519  static double GaussianWeight(const double variance, const double distanceFromMean);
520 
526  static double GaussianWeight(const double mean, const double variance, const double position);
527 
531  static float Dot2D(const float x[2], const float y[2]) {
532  return ( x[0] * y[0] + x[1] * y[1] );};
533 
537  static double Dot2D(const double x[2], const double y[2]) {
538  return ( x[0] * y[0] + x[1] * y[1] );};
539 
543  static void Outer2D(const float x[2], const float y[2], float A[2][2])
544  {
545  for (int i=0; i < 2; i++)
546  {
547  for (int j=0; j < 2; j++)
548  {
549  A[i][j] = x[i] * y[j];
550  }
551  }
552  }
556  static void Outer2D(const double x[2], const double y[2], double A[2][2])
557  {
558  for (int i=0; i < 2; i++)
559  {
560  for (int j=0; j < 2; j++)
561  {
562  A[i][j] = x[i] * y[j];
563  }
564  }
565  }
566 
570  static float Norm2D(const float x[2]) {
571  return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
572 
577  static double Norm2D(const double x[2]) {
578  return sqrt( x[0] * x[0] + x[1] * x[1] );};
579 
583  static float Normalize2D(float v[2]);
584 
589  static double Normalize2D(double v[2]);
590 
594  static float Determinant2x2(const float c1[2], const float c2[2]) {
595  return (c1[0] * c2[1] - c2[0] * c1[1] );};
596 
598 
601  static double Determinant2x2(double a, double b, double c, double d) {
602  return (a * d - b * c);};
603  static double Determinant2x2(const double c1[2], const double c2[2]) {
604  return (c1[0] * c2[1] - c2[0] * c1[1] );};
606 
608 
611  static void LUFactor3x3(float A[3][3], int index[3]);
612  static void LUFactor3x3(double A[3][3], int index[3]);
614 
616 
619  static void LUSolve3x3(const float A[3][3], const int index[3],
620  float x[3]);
621  static void LUSolve3x3(const double A[3][3], const int index[3],
622  double x[3]);
624 
626 
630  static void LinearSolve3x3(const float A[3][3], const float x[3],
631  float y[3]);
632  static void LinearSolve3x3(const double A[3][3], const double x[3],
633  double y[3]);
635 
637 
640  static void Multiply3x3(const float A[3][3], const float in[3],
641  float out[3]);
642  static void Multiply3x3(const double A[3][3], const double in[3],
643  double out[3]);
645 
647 
650  static void Multiply3x3(const float A[3][3], const float B[3][3],
651  float C[3][3]);
652  static void Multiply3x3(const double A[3][3], const double B[3][3],
653  double C[3][3]);
655 
661  static void MultiplyMatrix(double **A, double **B,
662  unsigned int rowA, unsigned int colA,
663  unsigned int rowB, unsigned int colB,
664  double **C);
665 
667 
671  static void Transpose3x3(const float A[3][3], float AT[3][3]);
672  static void Transpose3x3(const double A[3][3], double AT[3][3]);
674 
676 
680  static void Invert3x3(const float A[3][3], float AI[3][3]);
681  static void Invert3x3(const double A[3][3], double AI[3][3]);
683 
685 
688  static void Identity3x3(float A[3][3]);
689  static void Identity3x3(double A[3][3]);
691 
693 
696  static double Determinant3x3(float A[3][3]);
697  static double Determinant3x3(double A[3][3]);
699 
703  static float Determinant3x3(const float c1[3],
704  const float c2[3],
705  const float c3[3]);
706 
710  static double Determinant3x3(const double c1[3],
711  const double c2[3],
712  const double c3[3]);
713 
720  static double Determinant3x3(double a1, double a2, double a3,
721  double b1, double b2, double b3,
722  double c1, double c2, double c3);
723 
725 
732  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
733  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
735 
737 
745  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
746  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
748 
750 
756  static void MultiplyQuaternion( const float q1[4], const float q2[4], float q[4] );
757  static void MultiplyQuaternion( const double q1[4], const double q2[4], double q[4] );
759 
761 
766  static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
767  static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
769 
771 
777  static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
778  static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
780 
782 
791  static void SingularValueDecomposition3x3(const float A[3][3],
792  float U[3][3], float w[3],
793  float VT[3][3]);
794  static void SingularValueDecomposition3x3(const double A[3][3],
795  double U[3][3], double w[3],
796  double VT[3][3]);
798 
805  static int SolveLinearSystem(double **A, double *x, int size);
806 
813  static int InvertMatrix(double **A, double **AI, int size);
814 
820  static int InvertMatrix(double **A, double **AI, int size,
821  int *tmp1Size, double *tmp2Size);
822 
845  static int LUFactorLinearSystem(double **A, int *index, int size);
846 
852  static int LUFactorLinearSystem(double **A, int *index, int size,
853  double *tmpSize);
854 
863  static void LUSolveLinearSystem(double **A, int *index,
864  double *x, int size);
865 
874  static double EstimateMatrixCondition(double **A, int size);
875 
877 
885  static int Jacobi(float **a, float *w, float **v);
886  static int Jacobi(double **a, double *w, double **v);
888 
890 
899  static int JacobiN(float **a, int n, float *w, float **v);
900  static int JacobiN(double **a, int n, double *w, double **v);
902 
916  static int SolveHomogeneousLeastSquares(int numberOfSamples, double **xt, int xOrder,
917  double **mt);
918 
933  static int SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
934  double **yt, int yOrder, double **mt, int checkHomogeneous=1);
935 
937 
944  static void RGBToHSV(const float rgb[3], float hsv[3])
945  { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
946  static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v);
947  static double* RGBToHSV(const double rgb[3]);
948  static double* RGBToHSV(double r, double g, double b);
949  static void RGBToHSV(const double rgb[3], double hsv[3])
950  { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
951  static void RGBToHSV(double r, double g, double b, double *h, double *s, double *v);
953 
955 
962  static void HSVToRGB(const float hsv[3], float rgb[3])
963  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
964  static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b);
965  static double* HSVToRGB(const double hsv[3]);
966  static double* HSVToRGB(double h, double s, double v);
967  static void HSVToRGB(const double hsv[3], double rgb[3])
968  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
969  static void HSVToRGB(double h, double s, double v, double *r, double *g, double *b);
971 
973 
976  static void LabToXYZ(const double lab[3], double xyz[3]) {
977  LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
978  }
979  static void LabToXYZ(double L, double a, double b,
980  double *x, double *y, double *z);
981  static double *LabToXYZ(const double lab[3]);
983 
985 
988  static void XYZToLab(const double xyz[3], double lab[3]) {
989  XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
990  }
991  static void XYZToLab(double x, double y, double z,
992  double *L, double *a, double *b);
993  static double *XYZToLab(const double xyz[3]);
995 
997 
1000  static void XYZToRGB(const double xyz[3], double rgb[3]) {
1001  XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
1002  }
1003  static void XYZToRGB(double x, double y, double z,
1004  double *r, double *g, double *b);
1005  static double *XYZToRGB(const double xyz[3]);
1007 
1009 
1012  static void RGBToXYZ(const double rgb[3], double xyz[3]) {
1013  RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
1014  }
1015  static void RGBToXYZ(double r, double g, double b,
1016  double *x, double *y, double *z);
1017  static double *RGBToXYZ(const double rgb[3]);
1019 
1021 
1027  static void RGBToLab(const double rgb[3], double lab[3]) {
1028  RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
1029  }
1030  static void RGBToLab(double red, double green, double blue,
1031  double *L, double *a, double *b);
1032  static double *RGBToLab(const double rgb[3]);
1034 
1036 
1039  static void LabToRGB(const double lab[3], double rgb[3]) {
1040  LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
1041  }
1042  static void LabToRGB(double L, double a, double b,
1043  double *red, double *green, double *blue);
1044  static double *LabToRGB(const double lab[3]);
1046 
1048 
1051  static void UninitializeBounds(double bounds[6]){
1052  bounds[0] = 1.0;
1053  bounds[1] = -1.0;
1054  bounds[2] = 1.0;
1055  bounds[3] = -1.0;
1056  bounds[4] = 1.0;
1057  bounds[5] = -1.0;
1058  }
1060 
1062 
1065  static vtkTypeBool AreBoundsInitialized(double bounds[6]){
1066  if ( bounds[1]-bounds[0]<0.0 )
1067  {
1068  return 0;
1069  }
1070  return 1;
1071  }
1073 
1078  template<class T>
1079  static T ClampValue(const T & value, const T & min, const T & max);
1080 
1082 
1086  static void ClampValue(double *value, const double range[2]);
1087  static void ClampValue(double value, const double range[2], double *clamped_value);
1088  static void ClampValues(
1089  double *values, int nb_values, const double range[2]);
1090  static void ClampValues(
1091  const double *values, int nb_values, const double range[2], double *clamped_values);
1093 
1100  static double ClampAndNormalizeValue(double value,
1101  const double range[2]);
1102 
1107  template<class T1, class T2>
1108  static void TensorFromSymmetricTensor(T1 symmTensor[6], T2 tensor[9]);
1109 
1115  template<class T>
1116  static void TensorFromSymmetricTensor(T tensor[9]);
1117 
1126  static int GetScalarTypeFittingRange(
1127  double range_min, double range_max,
1128  double scale = 1.0, double shift = 0.0);
1129 
1138  static int GetAdjustedScalarRange(
1139  vtkDataArray *array, int comp, double range[2]);
1140 
1145  static vtkTypeBool ExtentIsWithinOtherExtent(int extent1[6], int extent2[6]);
1146 
1152  static vtkTypeBool BoundsIsWithinOtherBounds(double bounds1[6], double bounds2[6], double delta[3]);
1153 
1159  static vtkTypeBool PointIsWithinBounds(double point[3], double bounds[6], double delta[3]);
1160 
1170  static double Solve3PointCircle(const double p1[3], const double p2[3], const double p3[3], double center[3]);
1171 
1175  static double Inf();
1176 
1180  static double NegInf();
1181 
1185  static double Nan();
1186 
1190  static vtkTypeBool IsInf(double x);
1191 
1195  static vtkTypeBool IsNan(double x);
1196 
1200  static bool IsFinite(double x);
1201 protected:
1202  vtkMath() {}
1203  ~vtkMath() VTK_OVERRIDE {}
1204 
1206 private:
1207  vtkMath(const vtkMath&) VTK_DELETE_FUNCTION;
1208  void operator=(const vtkMath&) VTK_DELETE_FUNCTION;
1209 };
1210 
1211 //----------------------------------------------------------------------------
1212 inline float vtkMath::RadiansFromDegrees( float x )
1213 {
1214  return x * 0.017453292f;
1215 }
1216 
1217 //----------------------------------------------------------------------------
1218 inline double vtkMath::RadiansFromDegrees( double x )
1219 {
1220  return x * 0.017453292519943295;
1221 }
1222 
1223 //----------------------------------------------------------------------------
1224 inline float vtkMath::DegreesFromRadians( float x )
1225 {
1226  return x * 57.2957795131f;
1227 }
1228 
1229 //----------------------------------------------------------------------------
1230 inline double vtkMath::DegreesFromRadians( double x )
1231 {
1232  return x * 57.29577951308232;
1233 }
1234 
1235 //----------------------------------------------------------------------------
1236 inline bool vtkMath::IsPowerOfTwo(vtkTypeUInt64 x)
1237 {
1238  return ((x != 0) & ((x & (x - 1)) == 0));
1239 }
1240 
1241 //----------------------------------------------------------------------------
1242 // Credit goes to Peter Hart and William Lewis on comp.lang.python 1997
1244 {
1245  unsigned int z = ((x > 0) ? x - 1 : 0);
1246  z |= z >> 1;
1247  z |= z >> 2;
1248  z |= z >> 4;
1249  z |= z >> 8;
1250  z |= z >> 16;
1251  return static_cast<int>(z + 1);
1252 }
1253 
1254 //----------------------------------------------------------------------------
1255 // Modify the trunc() operation provided by static_cast<int>() to get floor(),
1256 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1257 inline int vtkMath::Floor(double x)
1258 {
1259  int i = static_cast<int>(x);
1260  return i - ( i > x );
1261 }
1262 
1263 //----------------------------------------------------------------------------
1264 // Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1265 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1266 inline int vtkMath::Ceil(double x)
1267 {
1268  int i = static_cast<int>(x);
1269  return i + ( i < x );
1270 }
1271 
1272 //----------------------------------------------------------------------------
1273 template<class T>
1274 inline T vtkMath::Min(const T & a, const T & b)
1275 {
1276  return (a < b ? a : b);
1277 }
1278 
1279 //----------------------------------------------------------------------------
1280 template<class T>
1281 inline T vtkMath::Max(const T & a, const T & b)
1282 {
1283  return (a > b ? a : b);
1284 }
1285 
1286 //----------------------------------------------------------------------------
1287 inline float vtkMath::Normalize(float v[3])
1288 {
1289  float den = vtkMath::Norm( v );
1290  if ( den != 0.0 )
1291  {
1292  for (int i=0; i < 3; i++)
1293  {
1294  v[i] /= den;
1295  }
1296  }
1297  return den;
1298 }
1299 
1300 //----------------------------------------------------------------------------
1301 inline double vtkMath::Normalize(double v[3])
1302 {
1303  double den = vtkMath::Norm( v );
1304  if ( den != 0.0 )
1305  {
1306  for (int i=0; i < 3; i++)
1307  {
1308  v[i] /= den;
1309  }
1310  }
1311  return den;
1312 }
1313 
1314 //----------------------------------------------------------------------------
1315 inline float vtkMath::Normalize2D(float v[3])
1316 {
1317  float den = vtkMath::Norm2D( v );
1318  if ( den != 0.0 )
1319  {
1320  for (int i=0; i < 2; i++)
1321  {
1322  v[i] /= den;
1323  }
1324  }
1325  return den;
1326 }
1327 
1328 //----------------------------------------------------------------------------
1329 inline double vtkMath::Normalize2D(double v[3])
1330 {
1331  double den = vtkMath::Norm2D( v );
1332  if ( den != 0.0 )
1333  {
1334  for (int i=0; i < 2; i++)
1335  {
1336  v[i] /= den;
1337  }
1338  }
1339  return den;
1340 }
1341 
1342 //----------------------------------------------------------------------------
1343 inline float vtkMath::Determinant3x3(const float c1[3],
1344  const float c2[3],
1345  const float c3[3])
1346 {
1347  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1348  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1349 }
1350 
1351 //----------------------------------------------------------------------------
1352 inline double vtkMath::Determinant3x3(const double c1[3],
1353  const double c2[3],
1354  const double c3[3])
1355 {
1356  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1357  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1358 }
1359 
1360 //----------------------------------------------------------------------------
1361 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
1362  double b1, double b2, double b3,
1363  double c1, double c2, double c3)
1364 {
1365  return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
1366  - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
1367  + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
1368 }
1369 
1370 //----------------------------------------------------------------------------
1371 inline float vtkMath::Distance2BetweenPoints(const float p1[3],
1372  const float p2[3])
1373 {
1374  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1375  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1376  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1377 }
1378 
1379 //----------------------------------------------------------------------------
1380 inline double vtkMath::Distance2BetweenPoints(const double p1[3],
1381  const double p2[3])
1382 {
1383  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1384  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1385  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1386 }
1387 
1388 //----------------------------------------------------------------------------
1389 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1390 inline void vtkMath::Cross(const float a[3], const float b[3], float c[3])
1391 {
1392  float Cx = a[1] * b[2] - a[2] * b[1];
1393  float Cy = a[2] * b[0] - a[0] * b[2];
1394  float Cz = a[0] * b[1] - a[1] * b[0];
1395  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1396 }
1397 
1398 //----------------------------------------------------------------------------
1399 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1400 inline void vtkMath::Cross(const double a[3], const double b[3], double c[3])
1401 {
1402  double Cx = a[1] * b[2] - a[2] * b[1];
1403  double Cy = a[2] * b[0] - a[0] * b[2];
1404  double Cz = a[0] * b[1] - a[1] * b[0];
1405  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1406 }
1407 
1408 //----------------------------------------------------------------------------
1409 template<class T>
1410 inline double vtkDeterminant3x3(T A[3][3])
1411 {
1412  return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1413  A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1414  A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1415 }
1416 
1417 //----------------------------------------------------------------------------
1418 inline double vtkMath::Determinant3x3(float A[3][3])
1419 {
1420  return vtkDeterminant3x3( A );
1421 }
1422 
1423 //----------------------------------------------------------------------------
1424 inline double vtkMath::Determinant3x3(double A[3][3])
1425 {
1426  return vtkDeterminant3x3( A );
1427 }
1428 
1429 //----------------------------------------------------------------------------
1430 template<class T>
1431 inline T vtkMath::ClampValue(const T & value, const T & min, const T & max)
1432 {
1433  assert("pre: valid_range" && min<=max);
1434 
1435  if (value < min)
1436  {
1437  return min;
1438  }
1439 
1440  if (value > max)
1441  {
1442  return max;
1443  }
1444 
1445  return value;
1446 }
1447 
1448 //----------------------------------------------------------------------------
1449 inline void vtkMath::ClampValue(double *value, const double range[2])
1450 {
1451  if (value && range)
1452  {
1453  assert("pre: valid_range" && range[0]<=range[1]);
1454 
1455  if (*value < range[0])
1456  {
1457  *value = range[0];
1458  }
1459  else if (*value > range[1])
1460  {
1461  *value = range[1];
1462  }
1463  }
1464 }
1465 
1466 //----------------------------------------------------------------------------
1468  double value, const double range[2], double *clamped_value)
1469 {
1470  if (range && clamped_value)
1471  {
1472  assert("pre: valid_range" && range[0]<=range[1]);
1473 
1474  if (value < range[0])
1475  {
1476  *clamped_value = range[0];
1477  }
1478  else if (value > range[1])
1479  {
1480  *clamped_value = range[1];
1481  }
1482  else
1483  {
1484  *clamped_value = value;
1485  }
1486  }
1487 }
1488 
1489 // ---------------------------------------------------------------------------
1490 inline double vtkMath::ClampAndNormalizeValue(double value,
1491  const double range[2])
1492 {
1493  assert("pre: valid_range" && range[0]<=range[1]);
1494 
1495  double result;
1496  if(range[0]==range[1])
1497  {
1498  result=0.0;
1499  }
1500  else
1501  {
1502  // clamp
1503  if(value<range[0])
1504  {
1505  result=range[0];
1506  }
1507  else
1508  {
1509  if(value>range[1])
1510  {
1511  result=range[1];
1512  }
1513  else
1514  {
1515  result=value;
1516  }
1517  }
1518 
1519  // normalize
1520  result=( result - range[0] ) / ( range[1] - range[0] );
1521  }
1522 
1523  assert("post: valid_result" && result>=0.0 && result<=1.0);
1524 
1525  return result;
1526 }
1527 
1528 //-----------------------------------------------------------------------------
1529 template<class T1, class T2>
1530 inline void vtkMath::TensorFromSymmetricTensor(T1 symmTensor[9], T2 tensor[9])
1531 {
1532  for (int i = 0; i < 3; i++)
1533  {
1534  tensor[4*i] = symmTensor[i];
1535  }
1536  tensor[1] = tensor[3] = symmTensor[3];
1537  tensor[2] = tensor[6] = symmTensor[5];
1538  tensor[5] = tensor[7] = symmTensor[4];
1539 }
1540 
1541 //-----------------------------------------------------------------------------
1542 template<class T>
1543 inline void vtkMath::TensorFromSymmetricTensor(T tensor[9])
1544 {
1545  tensor[6] = tensor[5]; // XZ
1546  tensor[7] = tensor[4]; // YZ
1547  tensor[8] = tensor[2]; // ZZ
1548  tensor[4] = tensor[1]; // YY
1549  tensor[5] = tensor[7]; // YZ
1550  tensor[2] = tensor[6]; // XZ
1551  tensor[1] = tensor[3]; // XY
1552 }
1553 
1554 namespace vtk_detail
1555 {
1556 // Can't specialize templates inside a template class, so we move the impl here.
1557 template <typename OutT>
1558 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
1559 { // OutT is integral -- clamp and round
1560  val = vtkMath::Max(val, static_cast<double>(vtkTypeTraits<OutT>::Min()));
1561  val = vtkMath::Min(val, static_cast<double>(vtkTypeTraits<OutT>::Max()));
1562  *ret = static_cast<OutT>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
1563 }
1564 template <>
1565 inline void RoundDoubleToIntegralIfNecessary(double val, double* retVal)
1566 { // OutT is double: passthrough
1567  *retVal = val;
1568 }
1569 template <>
1570 inline void RoundDoubleToIntegralIfNecessary(double val, float* retVal)
1571 { // OutT is float -- just clamp
1572  val = vtkMath::Max(val, static_cast<double>(vtkTypeTraits<float>::Min()));
1573  val = vtkMath::Min(val, static_cast<double>(vtkTypeTraits<float>::Max()));
1574  *retVal = static_cast<float>(val);
1575 }
1576 } // end namespace vtk_detail
1577 
1578 //-----------------------------------------------------------------------------
1579 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
1580 #define VTK_MATH_ISINF_IS_INLINE
1581 inline vtkTypeBool vtkMath::IsInf(double x)
1582 {
1583 #if defined(VTK_HAS_STD_ISINF)
1584  return std::isinf(x);
1585 #else
1586  return (isinf(x) != 0); // Force conversion to bool
1587 #endif
1588 }
1589 #endif
1590 
1591 //-----------------------------------------------------------------------------
1592 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
1593 #define VTK_MATH_ISNAN_IS_INLINE
1594 inline vtkTypeBool vtkMath::IsNan(double x)
1595 {
1596 #if defined(VTK_HAS_STD_ISNAN)
1597  return std::isnan(x);
1598 #else
1599  return (isnan(x) != 0); // Force conversion to bool
1600 #endif
1601 }
1602 #endif
1603 
1604 //-----------------------------------------------------------------------------
1605 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
1606 #define VTK_MATH_ISFINITE_IS_INLINE
1607 inline bool vtkMath::IsFinite(double x)
1608 {
1609 #if defined(VTK_HAS_STD_ISFINITE)
1610  return std::isfinite(x);
1611 #elif defined(VTK_HAS_ISFINITE)
1612  return (isfinite(x) != 0); // Force conversion to bool
1613 #else
1614  return (finite(x) != 0); // Force conversion to bool
1615 #endif
1616 }
1617 #endif
1618 
1619 #endif
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
Definition: vtkMath.h:357
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:531
~vtkMath() override
Definition: vtkMath.h:1203
static float Dot(const float a[3], const float b[3])
Dot product of two 3-vectors (float version).
Definition: vtkMath.h:383
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:976
static double Pi()
A mathematical constant.
Definition: vtkMath.h:100
static bool IsPowerOfTwo(vtkTypeUInt64 x)
Returns true if integer is a power of two.
Definition: vtkMath.h:1236
void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
Definition: vtkMath.h:1558
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
Definition: vtkMath.h:594
static vtkSmartPointer< vtkMathInternal > Internal
Definition: vtkMath.h:1205
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:949
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:121
vtkMath()
Definition: vtkMath.h:1202
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:944
static double ClampAndNormalizeValue(double value, const double range[2])
Clamp a value against a range and then normalized it between 0 and 1.
Definition: vtkMath.h:1490
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:323
static double Dot(const double a[3], const double b[3])
Dot product of two 3-vectors (double-precision version).
Definition: vtkMath.h:389
static void XYZToRGB(const double xyz[3], double rgb[3])
Convert color from the CIE XYZ system to RGB.
Definition: vtkMath.h:1000
static float Norm2D(const float x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:570
static void UninitializeBounds(double bounds[6])
Set the bounds to an uninitialized state.
Definition: vtkMath.h:1051
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1243
static void RGBToXYZ(const double rgb[3], double xyz[3])
Convert color from the RGB system to CIE XYZ.
Definition: vtkMath.h:1012
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1274
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:331
static void Subtract(const double a[3], const double b[3], double c[3])
Subtraction of two 3-vectors (double version).
Definition: vtkMath.h:339
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
Definition: vtkMath.h:1257
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:601
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
Definition: vtkMath.h:1212
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:967
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:366
static double Determinant3x3(float A[3][3])
Return the determinant of a 3x3 matrix.
Definition: vtkMath.h:1418
static void RGBToLab(const double rgb[3], double lab[3])
Convert color from the RGB system to CIE-L*ab.
Definition: vtkMath.h:1027
static float DegreesFromRadians(float radians)
Convert radians into degrees.
Definition: vtkMath.h:1224
static float Normalize(float v[3])
Normalize (in place) a 3-vector.
Definition: vtkMath.h:1287
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:556
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:543
static int Ceil(double x)
Rounds a double to the nearest integer not less than itself.
Definition: vtkMath.h:1266
performs common math operations
Definition: vtkMath.h:90
static double Dot2D(const double x[2], const double y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:537
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:131
static float Norm(const float v[3])
Compute the norm of 3-vector.
Definition: vtkMath.h:431
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
Definition: vtkMath.h:348
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:962
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:403
static T ClampValue(const T &value, const T &min, const T &max)
Clamp some value against a range, return the result.
Definition: vtkMath.h:1431
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:577
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:395
static int Round(double f)
Definition: vtkMath.h:123
static double Norm(const double v[3])
Compute the norm of 3-vector (double-precision version).
Definition: vtkMath.h:437
static void MultiplyScalar2D(double a[2], double s)
Multiplies a 2-vector by a scalar (double version).
Definition: vtkMath.h:375
static float Distance2BetweenPoints(const float p1[3], const float p2[3])
Compute distance squared between two points p1 and p2.
Definition: vtkMath.h:1371
static void LabToRGB(const double lab[3], double rgb[3])
Convert color from the CIE-L*ab system to RGB.
Definition: vtkMath.h:1039
double vtkDeterminant3x3(T A[3][3])
Definition: vtkMath.h:1410
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:1390
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:603
static void Add(const float a[3], const float b[3], float c[3])
Addition of two 3-vectors (float version).
Definition: vtkMath.h:315
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:32
#define max(a, b)
represent and manipulate 3D points
Definition: vtkPoints.h:39
static vtkTypeBool AreBoundsInitialized(double bounds[6])
Are the bounds initialized?
Definition: vtkMath.h:1065
static T Max(const T &a, const T &b)
Returns the maximum of the two arugments provided.
Definition: vtkMath.h:1281
static void XYZToLab(const double xyz[3], double lab[3])
Convert Color from the CIE XYZ system to CIE-L*ab.
Definition: vtkMath.h:988