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) 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 
163  template<class T>
164  static T Min(const T & a, const T & b);
165 
170  template<class T>
171  static T Max(const T & a, const T & b);
172 
176  static bool IsPowerOfTwo(vtkTypeUInt64 x);
177 
183  static int NearestPowerOfTwo(int x);
184 
189  static vtkTypeInt64 Factorial( int N );
190 
196  static vtkTypeInt64 Binomial( int m, int n );
197 
208  static int* BeginCombination( int m, int n );
209 
220  static int NextCombination( int m, int n, int* combination );
221 
225  static void FreeCombination( int* combination);
226 
242  static void RandomSeed(int s);
243 
255  static int GetSeed();
256 
270  static double Random();
271 
284  static double Random( double min, double max );
285 
298  static double Gaussian();
299 
312  static double Gaussian( double mean, double std );
313 
317  static void Add(const float a[3], const float b[3], float c[3]) {
318  for (int i = 0; i < 3; ++i)
319  c[i] = a[i] + b[i];
320  }
321 
325  static void Add(const double a[3], const double b[3], double c[3]) {
326  for (int i = 0; i < 3; ++i)
327  c[i] = a[i] + b[i];
328  }
329 
333  static void Subtract(const float a[3], const float b[3], float c[3]) {
334  for (int i = 0; i < 3; ++i)
335  c[i] = a[i] - b[i];
336  }
337 
341  static void Subtract(const double a[3], const double b[3], double c[3]) {
342  for (int i = 0; i < 3; ++i)
343  c[i] = a[i] - b[i];
344  }
345 
350  static void MultiplyScalar(float a[3], float s) {
351  for (int i = 0; i < 3; ++i)
352  a[i] *= s;
353  }
354 
359  static void MultiplyScalar2D(float a[2], float s) {
360  for (int i = 0; i < 2; ++i)
361  a[i] *= s;
362  }
363 
368  static void MultiplyScalar(double a[3], double s) {
369  for (int i = 0; i < 3; ++i)
370  a[i] *= s;
371  }
372 
377  static void MultiplyScalar2D(double a[2], double s) {
378  for (int i = 0; i < 2; ++i)
379  a[i] *= s;
380  }
381 
385  static float Dot(const float a[3], const float b[3]) {
386  return ( a[0] * b[0] + a[1] * b[1] + a[2] * b[2] );};
387 
391  static double Dot(const double a[3], const double b[3]) {
392  return ( a[0] * b[0] + a[1] * b[1] + a[2] * b[2] );};
393 
397  static void Outer(const float a[3], const float b[3], float C[3][3]) {
398  for (int i=0; i < 3; i++)
399  for (int j=0; j < 3; j++)
400  C[i][j] = a[i] * b[j];
401  }
405  static void Outer(const double a[3], const double b[3], double C[3][3]) {
406  for (int i=0; i < 3; i++)
407  for (int j=0; j < 3; j++)
408  C[i][j] = a[i] * b[j];
409  }
410 
414  static void Cross(const float a[3], const float b[3], float c[3]);
415 
420  static void Cross(const double a[3], const double b[3], double c[3]);
421 
423 
426  static float Norm(const float* x, int n);
427  static double Norm(const double* x, int n);
429 
433  static float Norm(const float v[3]) {
434  return static_cast<float> (sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] ) );};
435 
439  static double Norm(const double v[3]) {
440  return sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] );};
441 
445  static float Normalize(float v[3]);
446 
451  static double Normalize(double v[3]);
452 
454 
461  static void Perpendiculars(const double v1[3], double v2[3], double v3[3],
462  double theta);
463  static void Perpendiculars(const float v1[3], float v2[3], float v3[3],
464  double theta);
466 
468 
473  static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
474  static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
476 
478 
484  static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
485  static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
487 
491  static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
492 
497  static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
498 
502  static double AngleBetweenVectors(const double v1[3], const double v2[3]);
503 
508  static double GaussianAmplitude(const double variance, const double distanceFromMean);
509 
514  static double GaussianAmplitude(const double mean, const double variance, const double position);
515 
521  static double GaussianWeight(const double variance, const double distanceFromMean);
522 
528  static double GaussianWeight(const double mean, const double variance, const double position);
529 
533  static float Dot2D(const float x[2], const float y[2]) {
534  return ( x[0] * y[0] + x[1] * y[1] );};
535 
539  static double Dot2D(const double x[2], const double y[2]) {
540  return ( x[0] * y[0] + x[1] * y[1] );};
541 
545  static void Outer2D(const float x[2], const float y[2], float A[2][2])
546  {
547  for (int i=0; i < 2; i++)
548  {
549  for (int j=0; j < 2; j++)
550  {
551  A[i][j] = x[i] * y[j];
552  }
553  }
554  }
558  static void Outer2D(const double x[2], const double y[2], double A[2][2])
559  {
560  for (int i=0; i < 2; i++)
561  {
562  for (int j=0; j < 2; j++)
563  {
564  A[i][j] = x[i] * y[j];
565  }
566  }
567  }
568 
572  static float Norm2D(const float x[2]) {
573  return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
574 
579  static double Norm2D(const double x[2]) {
580  return sqrt( x[0] * x[0] + x[1] * x[1] );};
581 
585  static float Normalize2D(float v[2]);
586 
591  static double Normalize2D(double v[2]);
592 
596  static float Determinant2x2(const float c1[2], const float c2[2]) {
597  return (c1[0] * c2[1] - c2[0] * c1[1] );};
598 
600 
603  static double Determinant2x2(double a, double b, double c, double d) {
604  return (a * d - b * c);};
605  static double Determinant2x2(const double c1[2], const double c2[2]) {
606  return (c1[0] * c2[1] - c2[0] * c1[1] );};
608 
610 
613  static void LUFactor3x3(float A[3][3], int index[3]);
614  static void LUFactor3x3(double A[3][3], int index[3]);
616 
618 
621  static void LUSolve3x3(const float A[3][3], const int index[3],
622  float x[3]);
623  static void LUSolve3x3(const double A[3][3], const int index[3],
624  double x[3]);
626 
628 
632  static void LinearSolve3x3(const float A[3][3], const float x[3],
633  float y[3]);
634  static void LinearSolve3x3(const double A[3][3], const double x[3],
635  double y[3]);
637 
639 
642  static void Multiply3x3(const float A[3][3], const float in[3],
643  float out[3]);
644  static void Multiply3x3(const double A[3][3], const double in[3],
645  double out[3]);
647 
649 
652  static void Multiply3x3(const float A[3][3], const float B[3][3],
653  float C[3][3]);
654  static void Multiply3x3(const double A[3][3], const double B[3][3],
655  double C[3][3]);
657 
663  static void MultiplyMatrix(double **A, double **B,
664  unsigned int rowA, unsigned int colA,
665  unsigned int rowB, unsigned int colB,
666  double **C);
667 
669 
673  static void Transpose3x3(const float A[3][3], float AT[3][3]);
674  static void Transpose3x3(const double A[3][3], double AT[3][3]);
676 
678 
682  static void Invert3x3(const float A[3][3], float AI[3][3]);
683  static void Invert3x3(const double A[3][3], double AI[3][3]);
685 
687 
690  static void Identity3x3(float A[3][3]);
691  static void Identity3x3(double A[3][3]);
693 
695 
698  static double Determinant3x3(float A[3][3]);
699  static double Determinant3x3(double A[3][3]);
701 
705  static float Determinant3x3(const float c1[3],
706  const float c2[3],
707  const float c3[3]);
708 
712  static double Determinant3x3(const double c1[3],
713  const double c2[3],
714  const double c3[3]);
715 
722  static double Determinant3x3(double a1, double a2, double a3,
723  double b1, double b2, double b3,
724  double c1, double c2, double c3);
725 
727 
734  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
735  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
737 
739 
747  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
748  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
750 
752 
758  static void MultiplyQuaternion( const float q1[4], const float q2[4], float q[4] );
759  static void MultiplyQuaternion( const double q1[4], const double q2[4], double q[4] );
761 
763 
767  static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3]);
768  static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3]);
770 
772 
776  static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3]);
777  static void RotateVectorByWXYZ(const double v[3], const double q[4], double r[3]);
779 
781 
786  static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
787  static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
789 
791 
797  static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
798  static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
800 
802 
811  static void SingularValueDecomposition3x3(const float A[3][3],
812  float U[3][3], float w[3],
813  float VT[3][3]);
814  static void SingularValueDecomposition3x3(const double A[3][3],
815  double U[3][3], double w[3],
816  double VT[3][3]);
818 
825  static int SolveLinearSystem(double **A, double *x, int size);
826 
833  static int InvertMatrix(double **A, double **AI, int size);
834 
840  static int InvertMatrix(double **A, double **AI, int size,
841  int *tmp1Size, double *tmp2Size);
842 
865  static int LUFactorLinearSystem(double **A, int *index, int size);
866 
872  static int LUFactorLinearSystem(double **A, int *index, int size,
873  double *tmpSize);
874 
883  static void LUSolveLinearSystem(double **A, int *index,
884  double *x, int size);
885 
894  static double EstimateMatrixCondition(double **A, int size);
895 
897 
905  static int Jacobi(float **a, float *w, float **v);
906  static int Jacobi(double **a, double *w, double **v);
908 
910 
919  static int JacobiN(float **a, int n, float *w, float **v);
920  static int JacobiN(double **a, int n, double *w, double **v);
922 
936  static int SolveHomogeneousLeastSquares(int numberOfSamples, double **xt, int xOrder,
937  double **mt);
938 
953  static int SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
954  double **yt, int yOrder, double **mt, int checkHomogeneous=1);
955 
957 
964  static void RGBToHSV(const float rgb[3], float hsv[3])
965  { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
966  static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v);
967  static double* RGBToHSV(const double rgb[3]) VTK_SIZEHINT(3);
968  static double* RGBToHSV(double r, double g, double b) VTK_SIZEHINT(3);
969  static void RGBToHSV(const double rgb[3], double hsv[3])
970  { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
971  static void RGBToHSV(double r, double g, double b, double *h, double *s, double *v);
973 
975 
982  static void HSVToRGB(const float hsv[3], float rgb[3])
983  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
984  static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b);
985  static double* HSVToRGB(const double hsv[3]) VTK_SIZEHINT(3);
986  static double* HSVToRGB(double h, double s, double v) VTK_SIZEHINT(3);
987  static void HSVToRGB(const double hsv[3], double rgb[3])
988  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
989  static void HSVToRGB(double h, double s, double v, double *r, double *g, double *b);
991 
993 
996  static void LabToXYZ(const double lab[3], double xyz[3]) {
997  LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
998  }
999  static void LabToXYZ(double L, double a, double b,
1000  double *x, double *y, double *z);
1001  static double *LabToXYZ(const double lab[3]) VTK_SIZEHINT(3);
1003 
1005 
1008  static void XYZToLab(const double xyz[3], double lab[3]) {
1009  XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
1010  }
1011  static void XYZToLab(double x, double y, double z,
1012  double *L, double *a, double *b);
1013  static double *XYZToLab(const double xyz[3]) VTK_SIZEHINT(3);
1015 
1017 
1020  static void XYZToRGB(const double xyz[3], double rgb[3]) {
1021  XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
1022  }
1023  static void XYZToRGB(double x, double y, double z,
1024  double *r, double *g, double *b);
1025  static double *XYZToRGB(const double xyz[3]) VTK_SIZEHINT(3);
1027 
1029 
1032  static void RGBToXYZ(const double rgb[3], double xyz[3]) {
1033  RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
1034  }
1035  static void RGBToXYZ(double r, double g, double b,
1036  double *x, double *y, double *z);
1037  static double *RGBToXYZ(const double rgb[3]) VTK_SIZEHINT(3);
1039 
1041 
1047  static void RGBToLab(const double rgb[3], double lab[3]) {
1048  RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
1049  }
1050  static void RGBToLab(double red, double green, double blue,
1051  double *L, double *a, double *b);
1052  static double *RGBToLab(const double rgb[3]) VTK_SIZEHINT(3);
1054 
1056 
1059  static void LabToRGB(const double lab[3], double rgb[3]) {
1060  LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
1061  }
1062  static void LabToRGB(double L, double a, double b,
1063  double *red, double *green, double *blue);
1064  static double *LabToRGB(const double lab[3]) VTK_SIZEHINT(3);
1066 
1068 
1071  static void UninitializeBounds(double bounds[6]){
1072  bounds[0] = 1.0;
1073  bounds[1] = -1.0;
1074  bounds[2] = 1.0;
1075  bounds[3] = -1.0;
1076  bounds[4] = 1.0;
1077  bounds[5] = -1.0;
1078  }
1080 
1082 
1085  static vtkTypeBool AreBoundsInitialized(const double bounds[6]){
1086  if ( bounds[1]-bounds[0]<0.0 )
1087  {
1088  return 0;
1089  }
1090  return 1;
1091  }
1093 
1098  template<class T>
1099  static T ClampValue(const T & value, const T & min, const T & max);
1100 
1102 
1106  static void ClampValue(double *value, const double range[2]);
1107  static void ClampValue(double value, const double range[2], double *clamped_value);
1108  static void ClampValues(
1109  double *values, int nb_values, const double range[2]);
1110  static void ClampValues(
1111  const double *values, int nb_values, const double range[2], double *clamped_values);
1113 
1120  static double ClampAndNormalizeValue(double value,
1121  const double range[2]);
1122 
1127  template<class T1, class T2>
1128  static void TensorFromSymmetricTensor(T1 symmTensor[6], T2 tensor[9]);
1129 
1135  template<class T>
1136  static void TensorFromSymmetricTensor(T tensor[9]);
1137 
1146  static int GetScalarTypeFittingRange(
1147  double range_min, double range_max,
1148  double scale = 1.0, double shift = 0.0);
1149 
1158  static int GetAdjustedScalarRange(
1159  vtkDataArray *array, int comp, double range[2]);
1160 
1165  static vtkTypeBool ExtentIsWithinOtherExtent(int extent1[6], int extent2[6]);
1166 
1172  static vtkTypeBool BoundsIsWithinOtherBounds(double bounds1[6], double bounds2[6], double delta[3]);
1173 
1179  static vtkTypeBool PointIsWithinBounds(double point[3], double bounds[6], double delta[3]);
1180 
1190  static int PlaneIntersectsAABB(double const bounds[6], double const normal[3],
1191  double const point[3]);
1192 
1202  static double Solve3PointCircle(const double p1[3], const double p2[3], const double p3[3], double center[3]);
1203 
1207  static double Inf();
1208 
1212  static double NegInf();
1213 
1217  static double Nan();
1218 
1222  static vtkTypeBool IsInf(double x);
1223 
1227  static vtkTypeBool IsNan(double x);
1228 
1232  static bool IsFinite(double x);
1233 protected:
1234  vtkMath() {}
1235  ~vtkMath() override {}
1236 
1238 private:
1239  vtkMath(const vtkMath&) = delete;
1240  void operator=(const vtkMath&) = delete;
1241 };
1242 
1243 //----------------------------------------------------------------------------
1244 inline float vtkMath::RadiansFromDegrees( float x )
1245 {
1246  return x * 0.017453292f;
1247 }
1248 
1249 //----------------------------------------------------------------------------
1250 inline double vtkMath::RadiansFromDegrees( double x )
1251 {
1252  return x * 0.017453292519943295;
1253 }
1254 
1255 //----------------------------------------------------------------------------
1256 inline float vtkMath::DegreesFromRadians( float x )
1257 {
1258  return x * 57.2957795131f;
1259 }
1260 
1261 //----------------------------------------------------------------------------
1262 inline double vtkMath::DegreesFromRadians( double x )
1263 {
1264  return x * 57.29577951308232;
1265 }
1266 
1267 //----------------------------------------------------------------------------
1268 inline bool vtkMath::IsPowerOfTwo(vtkTypeUInt64 x)
1269 {
1270  return ((x != 0) & ((x & (x - 1)) == 0));
1271 }
1272 
1273 //----------------------------------------------------------------------------
1274 // Credit goes to Peter Hart and William Lewis on comp.lang.python 1997
1276 {
1277  unsigned int z = ((x > 0) ? x - 1 : 0);
1278  z |= z >> 1;
1279  z |= z >> 2;
1280  z |= z >> 4;
1281  z |= z >> 8;
1282  z |= z >> 16;
1283  return static_cast<int>(z + 1);
1284 }
1285 
1286 //----------------------------------------------------------------------------
1287 // Modify the trunc() operation provided by static_cast<int>() to get floor(),
1288 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1289 inline int vtkMath::Floor(double x)
1290 {
1291  int i = static_cast<int>(x);
1292  return i - ( i > x );
1293 }
1294 
1295 //----------------------------------------------------------------------------
1296 // Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1297 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1298 inline int vtkMath::Ceil(double x)
1299 {
1300  int i = static_cast<int>(x);
1301  return i + ( i < x );
1302 }
1303 
1304 //----------------------------------------------------------------------------
1305 template<class T>
1306 inline T vtkMath::Min(const T & a, const T & b)
1307 {
1308  return (b <= a ? b : a);
1309 }
1310 
1311 //----------------------------------------------------------------------------
1312 template<class T>
1313 inline T vtkMath::Max(const T & a, const T & b)
1314 {
1315  return (b > a ? b : a);
1316 }
1317 
1318 //----------------------------------------------------------------------------
1319 inline float vtkMath::Normalize(float v[3])
1320 {
1321  float den = vtkMath::Norm( v );
1322  if ( den != 0.0 )
1323  {
1324  for (int i=0; i < 3; i++)
1325  {
1326  v[i] /= den;
1327  }
1328  }
1329  return den;
1330 }
1331 
1332 //----------------------------------------------------------------------------
1333 inline double vtkMath::Normalize(double v[3])
1334 {
1335  double den = vtkMath::Norm( v );
1336  if ( den != 0.0 )
1337  {
1338  for (int i=0; i < 3; i++)
1339  {
1340  v[i] /= den;
1341  }
1342  }
1343  return den;
1344 }
1345 
1346 //----------------------------------------------------------------------------
1347 inline float vtkMath::Normalize2D(float v[3])
1348 {
1349  float den = vtkMath::Norm2D( v );
1350  if ( den != 0.0 )
1351  {
1352  for (int i=0; i < 2; i++)
1353  {
1354  v[i] /= den;
1355  }
1356  }
1357  return den;
1358 }
1359 
1360 //----------------------------------------------------------------------------
1361 inline double vtkMath::Normalize2D(double v[3])
1362 {
1363  double den = vtkMath::Norm2D( v );
1364  if ( den != 0.0 )
1365  {
1366  for (int i=0; i < 2; i++)
1367  {
1368  v[i] /= den;
1369  }
1370  }
1371  return den;
1372 }
1373 
1374 //----------------------------------------------------------------------------
1375 inline float vtkMath::Determinant3x3(const float c1[3],
1376  const float c2[3],
1377  const float c3[3])
1378 {
1379  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1380  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1381 }
1382 
1383 //----------------------------------------------------------------------------
1384 inline double vtkMath::Determinant3x3(const double c1[3],
1385  const double c2[3],
1386  const double c3[3])
1387 {
1388  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1389  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1390 }
1391 
1392 //----------------------------------------------------------------------------
1393 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
1394  double b1, double b2, double b3,
1395  double c1, double c2, double c3)
1396 {
1397  return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
1398  - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
1399  + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
1400 }
1401 
1402 //----------------------------------------------------------------------------
1403 inline float vtkMath::Distance2BetweenPoints(const float p1[3],
1404  const float p2[3])
1405 {
1406  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1407  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1408  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1409 }
1410 
1411 //----------------------------------------------------------------------------
1412 inline double vtkMath::Distance2BetweenPoints(const double p1[3],
1413  const double p2[3])
1414 {
1415  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1416  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1417  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1418 }
1419 
1420 //----------------------------------------------------------------------------
1421 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1422 inline void vtkMath::Cross(const float a[3], const float b[3], float c[3])
1423 {
1424  float Cx = a[1] * b[2] - a[2] * b[1];
1425  float Cy = a[2] * b[0] - a[0] * b[2];
1426  float Cz = a[0] * b[1] - a[1] * b[0];
1427  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1428 }
1429 
1430 //----------------------------------------------------------------------------
1431 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1432 inline void vtkMath::Cross(const double a[3], const double b[3], double c[3])
1433 {
1434  double Cx = a[1] * b[2] - a[2] * b[1];
1435  double Cy = a[2] * b[0] - a[0] * b[2];
1436  double Cz = a[0] * b[1] - a[1] * b[0];
1437  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1438 }
1439 
1440 //----------------------------------------------------------------------------
1441 template<class T>
1442 inline double vtkDeterminant3x3(T A[3][3])
1443 {
1444  return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1445  A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1446  A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1447 }
1448 
1449 //----------------------------------------------------------------------------
1450 inline double vtkMath::Determinant3x3(float A[3][3])
1451 {
1452  return vtkDeterminant3x3( A );
1453 }
1454 
1455 //----------------------------------------------------------------------------
1456 inline double vtkMath::Determinant3x3(double A[3][3])
1457 {
1458  return vtkDeterminant3x3( A );
1459 }
1460 
1461 //----------------------------------------------------------------------------
1462 template<class T>
1463 inline T vtkMath::ClampValue(const T & value, const T & min, const T & max)
1464 {
1465  assert("pre: valid_range" && min<=max);
1466 
1467  if (value < min)
1468  {
1469  return min;
1470  }
1471 
1472  if (value > max)
1473  {
1474  return max;
1475  }
1476 
1477  return value;
1478 }
1479 
1480 //----------------------------------------------------------------------------
1481 inline void vtkMath::ClampValue(double *value, const double range[2])
1482 {
1483  if (value && range)
1484  {
1485  assert("pre: valid_range" && range[0]<=range[1]);
1486 
1487  if (*value < range[0])
1488  {
1489  *value = range[0];
1490  }
1491  else if (*value > range[1])
1492  {
1493  *value = range[1];
1494  }
1495  }
1496 }
1497 
1498 //----------------------------------------------------------------------------
1500  double value, const double range[2], double *clamped_value)
1501 {
1502  if (range && clamped_value)
1503  {
1504  assert("pre: valid_range" && range[0]<=range[1]);
1505 
1506  if (value < range[0])
1507  {
1508  *clamped_value = range[0];
1509  }
1510  else if (value > range[1])
1511  {
1512  *clamped_value = range[1];
1513  }
1514  else
1515  {
1516  *clamped_value = value;
1517  }
1518  }
1519 }
1520 
1521 // ---------------------------------------------------------------------------
1523  const double range[2])
1524 {
1525  assert("pre: valid_range" && range[0]<=range[1]);
1526 
1527  double result;
1528  if(range[0]==range[1])
1529  {
1530  result=0.0;
1531  }
1532  else
1533  {
1534  // clamp
1535  if(value<range[0])
1536  {
1537  result=range[0];
1538  }
1539  else
1540  {
1541  if(value>range[1])
1542  {
1543  result=range[1];
1544  }
1545  else
1546  {
1547  result=value;
1548  }
1549  }
1550 
1551  // normalize
1552  result=( result - range[0] ) / ( range[1] - range[0] );
1553  }
1554 
1555  assert("post: valid_result" && result>=0.0 && result<=1.0);
1556 
1557  return result;
1558 }
1559 
1560 //-----------------------------------------------------------------------------
1561 template<class T1, class T2>
1562 inline void vtkMath::TensorFromSymmetricTensor(T1 symmTensor[9], T2 tensor[9])
1563 {
1564  for (int i = 0; i < 3; i++)
1565  {
1566  tensor[4*i] = symmTensor[i];
1567  }
1568  tensor[1] = tensor[3] = symmTensor[3];
1569  tensor[2] = tensor[6] = symmTensor[5];
1570  tensor[5] = tensor[7] = symmTensor[4];
1571 }
1572 
1573 //-----------------------------------------------------------------------------
1574 template<class T>
1575 inline void vtkMath::TensorFromSymmetricTensor(T tensor[9])
1576 {
1577  tensor[6] = tensor[5]; // XZ
1578  tensor[7] = tensor[4]; // YZ
1579  tensor[8] = tensor[2]; // ZZ
1580  tensor[4] = tensor[1]; // YY
1581  tensor[5] = tensor[7]; // YZ
1582  tensor[2] = tensor[6]; // XZ
1583  tensor[1] = tensor[3]; // XY
1584 }
1585 
1586 namespace vtk_detail
1587 {
1588 // Can't specialize templates inside a template class, so we move the impl here.
1589 template <typename OutT>
1590 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
1591 { // OutT is integral -- clamp and round
1592  val = vtkMath::Max(val, static_cast<double>(vtkTypeTraits<OutT>::Min()));
1593  val = vtkMath::Min(val, static_cast<double>(vtkTypeTraits<OutT>::Max()));
1594  *ret = static_cast<OutT>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
1595 }
1596 template <>
1597 inline void RoundDoubleToIntegralIfNecessary(double val, double* retVal)
1598 { // OutT is double: passthrough
1599  *retVal = val;
1600 }
1601 template <>
1602 inline void RoundDoubleToIntegralIfNecessary(double val, float* retVal)
1603 { // OutT is float -- just clamp
1604  val = vtkMath::Max(val, static_cast<double>(vtkTypeTraits<float>::Min()));
1605  val = vtkMath::Min(val, static_cast<double>(vtkTypeTraits<float>::Max()));
1606  *retVal = static_cast<float>(val);
1607 }
1608 } // end namespace vtk_detail
1609 
1610 //-----------------------------------------------------------------------------
1611 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
1612 #define VTK_MATH_ISINF_IS_INLINE
1613 inline vtkTypeBool vtkMath::IsInf(double x)
1614 {
1615 #if defined(VTK_HAS_STD_ISINF)
1616  return std::isinf(x);
1617 #else
1618  return (isinf(x) != 0); // Force conversion to bool
1619 #endif
1620 }
1621 #endif
1622 
1623 //-----------------------------------------------------------------------------
1624 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
1625 #define VTK_MATH_ISNAN_IS_INLINE
1626 inline vtkTypeBool vtkMath::IsNan(double x)
1627 {
1628 #if defined(VTK_HAS_STD_ISNAN)
1629  return std::isnan(x);
1630 #else
1631  return (isnan(x) != 0); // Force conversion to bool
1632 #endif
1633 }
1634 #endif
1635 
1636 //-----------------------------------------------------------------------------
1637 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
1638 #define VTK_MATH_ISFINITE_IS_INLINE
1639 inline bool vtkMath::IsFinite(double x)
1640 {
1641 #if defined(VTK_HAS_STD_ISFINITE)
1642  return std::isfinite(x);
1643 #elif defined(VTK_HAS_ISFINITE)
1644  return (isfinite(x) != 0); // Force conversion to bool
1645 #else
1646  return (finite(x) != 0); // Force conversion to bool
1647 #endif
1648 }
1649 #endif
1650 
1651 #endif
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
Definition: vtkMath.h:359
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:533
~vtkMath() override
Definition: vtkMath.h:1235
static float Dot(const float a[3], const float b[3])
Dot product of two 3-vectors (float version).
Definition: vtkMath.h:385
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:996
static double Pi()
A mathematical constant.
Definition: vtkMath.h:100
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:1268
void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
Definition: vtkMath.h:1590
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
Definition: vtkMath.h:596
static vtkSmartPointer< vtkMathInternal > Internal
Definition: vtkMath.h:1237
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:969
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:1234
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:964
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:1522
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:325
static double Dot(const double a[3], const double b[3])
Dot product of two 3-vectors (double-precision version).
Definition: vtkMath.h:391
static void XYZToRGB(const double xyz[3], double rgb[3])
Convert color from the CIE XYZ system to RGB.
Definition: vtkMath.h:1020
static float Norm2D(const float x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:572
static void UninitializeBounds(double bounds[6])
Set the bounds to an uninitialized state.
Definition: vtkMath.h:1071
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1275
static void RGBToXYZ(const double rgb[3], double xyz[3])
Convert color from the RGB system to CIE XYZ.
Definition: vtkMath.h:1032
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1306
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:333
static void Subtract(const double a[3], const double b[3], double c[3])
Subtraction of two 3-vectors (double version).
Definition: vtkMath.h:341
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
Definition: vtkMath.h:1289
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:603
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
Definition: vtkMath.h:1244
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:987
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:368
#define VTK_SIZEHINT(...)
static double Determinant3x3(float A[3][3])
Return the determinant of a 3x3 matrix.
Definition: vtkMath.h:1450
static void RGBToLab(const double rgb[3], double lab[3])
Convert color from the RGB system to CIE-L*ab.
Definition: vtkMath.h:1047
static float DegreesFromRadians(float radians)
Convert radians into degrees.
Definition: vtkMath.h:1256
static float Normalize(float v[3])
Normalize (in place) a 3-vector.
Definition: vtkMath.h:1319
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:558
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:545
static int Ceil(double x)
Rounds a double to the nearest integer not less than itself.
Definition: vtkMath.h:1298
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:539
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:433
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
Definition: vtkMath.h:350
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:982
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:405
static T ClampValue(const T &value, const T &min, const T &max)
Clamp some value against a range, return the result.
Definition: vtkMath.h:1463
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:579
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:397
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:439
static void MultiplyScalar2D(double a[2], double s)
Multiplies a 2-vector by a scalar (double version).
Definition: vtkMath.h:377
static float Distance2BetweenPoints(const float p1[3], const float p2[3])
Compute distance squared between two points p1 and p2.
Definition: vtkMath.h:1403
static void LabToRGB(const double lab[3], double rgb[3])
Convert color from the CIE-L*ab system to RGB.
Definition: vtkMath.h:1059
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:1442
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:1422
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:605
static void Add(const float a[3], const float b[3], float c[3])
Addition of two 3-vectors (float version).
Definition: vtkMath.h:317
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:1085
#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:1313
static void XYZToLab(const double xyz[3], double lab[3])
Convert Color from the CIE XYZ system to CIE-L*ab.
Definition: vtkMath.h:1008