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) VTK_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 
156  template<class T>
157  static T Min(const T & a, const T & b);
158 
162  template<class T>
163  static T Max(const T & a, const T & b);
164 
168  static bool IsPowerOfTwo(vtkTypeUInt64 x);
169 
175  static int NearestPowerOfTwo(int x);
176 
181  static vtkTypeInt64 Factorial( int N );
182 
188  static vtkTypeInt64 Binomial( int m, int n );
189 
200  static int* BeginCombination( int m, int n );
201 
212  static int NextCombination( int m, int n, int* combination );
213 
217  static void FreeCombination( int* combination);
218 
234  static void RandomSeed(int s);
235 
247  static int GetSeed();
248 
262  static double Random();
263 
276  static double Random( double min, double max );
277 
290  static double Gaussian();
291 
304  static double Gaussian( double mean, double std );
305 
309  static void Add(const float a[3], const float b[3], float c[3]) {
310  for (int i = 0; i < 3; ++i)
311  c[i] = a[i] + b[i];
312  }
313 
317  static void Add(const double a[3], const double b[3], double c[3]) {
318  for (int i = 0; i < 3; ++i)
319  c[i] = a[i] + b[i];
320  }
321 
325  static void Subtract(const float a[3], const float b[3], float c[3]) {
326  for (int i = 0; i < 3; ++i)
327  c[i] = a[i] - b[i];
328  }
329 
333  static void Subtract(const double a[3], const double b[3], double c[3]) {
334  for (int i = 0; i < 3; ++i)
335  c[i] = a[i] - b[i];
336  }
337 
342  static void MultiplyScalar(float a[3], float s) {
343  for (int i = 0; i < 3; ++i)
344  a[i] *= s;
345  }
346 
351  static void MultiplyScalar2D(float a[2], float s) {
352  for (int i = 0; i < 2; ++i)
353  a[i] *= s;
354  }
355 
360  static void MultiplyScalar(double a[3], double s) {
361  for (int i = 0; i < 3; ++i)
362  a[i] *= s;
363  }
364 
369  static void MultiplyScalar2D(double a[2], double s) {
370  for (int i = 0; i < 2; ++i)
371  a[i] *= s;
372  }
373 
377  static float Dot(const float a[3], const float b[3]) {
378  return ( a[0] * b[0] + a[1] * b[1] + a[2] * b[2] );};
379 
383  static double Dot(const double a[3], const double b[3]) {
384  return ( a[0] * b[0] + a[1] * b[1] + a[2] * b[2] );};
385 
389  static void Outer(const float a[3], const float b[3], float C[3][3]) {
390  for (int i=0; i < 3; i++)
391  for (int j=0; j < 3; j++)
392  C[i][j] = a[i] * b[j];
393  }
397  static void Outer(const double a[3], const double b[3], double 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  }
402 
406  static void Cross(const float a[3], const float b[3], float c[3]);
407 
412  static void Cross(const double a[3], const double b[3], double c[3]);
413 
415 
418  static float Norm(const float* x, int n);
419  static double Norm(const double* x, int n);
421 
425  static float Norm(const float v[3]) {
426  return static_cast<float> (sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] ) );};
427 
431  static double Norm(const double v[3]) {
432  return sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] );};
433 
437  static float Normalize(float v[3]);
438 
443  static double Normalize(double v[3]);
444 
446 
453  static void Perpendiculars(const double v1[3], double v2[3], double v3[3],
454  double theta);
455  static void Perpendiculars(const float v1[3], float v2[3], float v3[3],
456  double theta);
458 
460 
465  static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
466  static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
468 
470 
476  static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
477  static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
479 
483  static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
484 
489  static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
490 
494  static double AngleBetweenVectors(const double v1[3], const double v2[3]);
495 
500  static double GaussianAmplitude(const double variance, const double distanceFromMean);
501 
506  static double GaussianAmplitude(const double mean, const double variance, const double position);
507 
513  static double GaussianWeight(const double variance, const double distanceFromMean);
514 
520  static double GaussianWeight(const double mean, const double variance, const double position);
521 
525  static float Dot2D(const float x[2], const float y[2]) {
526  return ( x[0] * y[0] + x[1] * y[1] );};
527 
531  static double Dot2D(const double x[2], const double y[2]) {
532  return ( x[0] * y[0] + x[1] * y[1] );};
533 
537  static void Outer2D(const float x[2], const float y[2], float A[2][2])
538  {
539  for (int i=0; i < 2; i++)
540  {
541  for (int j=0; j < 2; j++)
542  {
543  A[i][j] = x[i] * y[j];
544  }
545  }
546  }
550  static void Outer2D(const double x[2], const double y[2], double A[2][2])
551  {
552  for (int i=0; i < 2; i++)
553  {
554  for (int j=0; j < 2; j++)
555  {
556  A[i][j] = x[i] * y[j];
557  }
558  }
559  }
560 
564  static float Norm2D(const float x[2]) {
565  return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
566 
571  static double Norm2D(const double x[2]) {
572  return sqrt( x[0] * x[0] + x[1] * x[1] );};
573 
577  static float Normalize2D(float v[2]);
578 
583  static double Normalize2D(double v[2]);
584 
588  static float Determinant2x2(const float c1[2], const float c2[2]) {
589  return (c1[0] * c2[1] - c2[0] * c1[1] );};
590 
592 
595  static double Determinant2x2(double a, double b, double c, double d) {
596  return (a * d - b * c);};
597  static double Determinant2x2(const double c1[2], const double c2[2]) {
598  return (c1[0] * c2[1] - c2[0] * c1[1] );};
600 
602 
605  static void LUFactor3x3(float A[3][3], int index[3]);
606  static void LUFactor3x3(double A[3][3], int index[3]);
608 
610 
613  static void LUSolve3x3(const float A[3][3], const int index[3],
614  float x[3]);
615  static void LUSolve3x3(const double A[3][3], const int index[3],
616  double x[3]);
618 
620 
624  static void LinearSolve3x3(const float A[3][3], const float x[3],
625  float y[3]);
626  static void LinearSolve3x3(const double A[3][3], const double x[3],
627  double y[3]);
629 
631 
634  static void Multiply3x3(const float A[3][3], const float in[3],
635  float out[3]);
636  static void Multiply3x3(const double A[3][3], const double in[3],
637  double out[3]);
639 
641 
644  static void Multiply3x3(const float A[3][3], const float B[3][3],
645  float C[3][3]);
646  static void Multiply3x3(const double A[3][3], const double B[3][3],
647  double C[3][3]);
649 
655  static void MultiplyMatrix(double **A, double **B,
656  unsigned int rowA, unsigned int colA,
657  unsigned int rowB, unsigned int colB,
658  double **C);
659 
661 
665  static void Transpose3x3(const float A[3][3], float AT[3][3]);
666  static void Transpose3x3(const double A[3][3], double AT[3][3]);
668 
670 
674  static void Invert3x3(const float A[3][3], float AI[3][3]);
675  static void Invert3x3(const double A[3][3], double AI[3][3]);
677 
679 
682  static void Identity3x3(float A[3][3]);
683  static void Identity3x3(double A[3][3]);
685 
687 
690  static double Determinant3x3(float A[3][3]);
691  static double Determinant3x3(double A[3][3]);
693 
697  static float Determinant3x3(const float c1[3],
698  const float c2[3],
699  const float c3[3]);
700 
704  static double Determinant3x3(const double c1[3],
705  const double c2[3],
706  const double c3[3]);
707 
714  static double Determinant3x3(double a1, double a2, double a3,
715  double b1, double b2, double b3,
716  double c1, double c2, double c3);
717 
719 
726  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
727  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
729 
731 
739  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
740  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
742 
744 
750  static void MultiplyQuaternion( const float q1[4], const float q2[4], float q[4] );
751  static void MultiplyQuaternion( const double q1[4], const double q2[4], double q[4] );
753 
755 
759  static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3]);
760  static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3]);
762 
764 
768  static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3]);
769  static void RotateVectorByWXYZ(const double v[3], const double q[4], double r[3]);
771 
773 
778  static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
779  static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
781 
783 
789  static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
790  static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
792 
794 
803  static void SingularValueDecomposition3x3(const float A[3][3],
804  float U[3][3], float w[3],
805  float VT[3][3]);
806  static void SingularValueDecomposition3x3(const double A[3][3],
807  double U[3][3], double w[3],
808  double VT[3][3]);
810 
817  static int SolveLinearSystem(double **A, double *x, int size);
818 
825  static int InvertMatrix(double **A, double **AI, int size);
826 
832  static int InvertMatrix(double **A, double **AI, int size,
833  int *tmp1Size, double *tmp2Size);
834 
857  static int LUFactorLinearSystem(double **A, int *index, int size);
858 
864  static int LUFactorLinearSystem(double **A, int *index, int size,
865  double *tmpSize);
866 
875  static void LUSolveLinearSystem(double **A, int *index,
876  double *x, int size);
877 
886  static double EstimateMatrixCondition(double **A, int size);
887 
889 
897  static int Jacobi(float **a, float *w, float **v);
898  static int Jacobi(double **a, double *w, double **v);
900 
902 
911  static int JacobiN(float **a, int n, float *w, float **v);
912  static int JacobiN(double **a, int n, double *w, double **v);
914 
928  static int SolveHomogeneousLeastSquares(int numberOfSamples, double **xt, int xOrder,
929  double **mt);
930 
945  static int SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
946  double **yt, int yOrder, double **mt, int checkHomogeneous=1);
947 
949 
956  static void RGBToHSV(const float rgb[3], float hsv[3])
957  { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
958  static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v);
959  static double* RGBToHSV(const double rgb[3]);
960  static double* RGBToHSV(double r, double g, double b);
961  static void RGBToHSV(const double rgb[3], double hsv[3])
962  { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
963  static void RGBToHSV(double r, double g, double b, double *h, double *s, double *v);
965 
967 
974  static void HSVToRGB(const float hsv[3], float rgb[3])
975  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
976  static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b);
977  static double* HSVToRGB(const double hsv[3]);
978  static double* HSVToRGB(double h, double s, double v);
979  static void HSVToRGB(const double hsv[3], double rgb[3])
980  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
981  static void HSVToRGB(double h, double s, double v, double *r, double *g, double *b);
983 
985 
988  static void LabToXYZ(const double lab[3], double xyz[3]) {
989  LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
990  }
991  static void LabToXYZ(double L, double a, double b,
992  double *x, double *y, double *z);
993  static double *LabToXYZ(const double lab[3]);
995 
997 
1000  static void XYZToLab(const double xyz[3], double lab[3]) {
1001  XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
1002  }
1003  static void XYZToLab(double x, double y, double z,
1004  double *L, double *a, double *b);
1005  static double *XYZToLab(const double xyz[3]);
1007 
1009 
1012  static void XYZToRGB(const double xyz[3], double rgb[3]) {
1013  XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
1014  }
1015  static void XYZToRGB(double x, double y, double z,
1016  double *r, double *g, double *b);
1017  static double *XYZToRGB(const double xyz[3]);
1019 
1021 
1024  static void RGBToXYZ(const double rgb[3], double xyz[3]) {
1025  RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
1026  }
1027  static void RGBToXYZ(double r, double g, double b,
1028  double *x, double *y, double *z);
1029  static double *RGBToXYZ(const double rgb[3]);
1031 
1033 
1039  static void RGBToLab(const double rgb[3], double lab[3]) {
1040  RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
1041  }
1042  static void RGBToLab(double red, double green, double blue,
1043  double *L, double *a, double *b);
1044  static double *RGBToLab(const double rgb[3]);
1046 
1048 
1051  static void LabToRGB(const double lab[3], double rgb[3]) {
1052  LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
1053  }
1054  static void LabToRGB(double L, double a, double b,
1055  double *red, double *green, double *blue);
1056  static double *LabToRGB(const double lab[3]);
1058 
1060 
1063  static void UninitializeBounds(double bounds[6]){
1064  bounds[0] = 1.0;
1065  bounds[1] = -1.0;
1066  bounds[2] = 1.0;
1067  bounds[3] = -1.0;
1068  bounds[4] = 1.0;
1069  bounds[5] = -1.0;
1070  }
1072 
1074 
1077  static vtkTypeBool AreBoundsInitialized(const double bounds[6]){
1078  if ( bounds[1]-bounds[0]<0.0 )
1079  {
1080  return 0;
1081  }
1082  return 1;
1083  }
1085 
1090  template<class T>
1091  static T ClampValue(const T & value, const T & min, const T & max);
1092 
1094 
1098  static void ClampValue(double *value, const double range[2]);
1099  static void ClampValue(double value, const double range[2], double *clamped_value);
1100  static void ClampValues(
1101  double *values, int nb_values, const double range[2]);
1102  static void ClampValues(
1103  const double *values, int nb_values, const double range[2], double *clamped_values);
1105 
1112  static double ClampAndNormalizeValue(double value,
1113  const double range[2]);
1114 
1119  template<class T1, class T2>
1120  static void TensorFromSymmetricTensor(T1 symmTensor[6], T2 tensor[9]);
1121 
1127  template<class T>
1128  static void TensorFromSymmetricTensor(T tensor[9]);
1129 
1138  static int GetScalarTypeFittingRange(
1139  double range_min, double range_max,
1140  double scale = 1.0, double shift = 0.0);
1141 
1150  static int GetAdjustedScalarRange(
1151  vtkDataArray *array, int comp, double range[2]);
1152 
1157  static vtkTypeBool ExtentIsWithinOtherExtent(int extent1[6], int extent2[6]);
1158 
1164  static vtkTypeBool BoundsIsWithinOtherBounds(double bounds1[6], double bounds2[6], double delta[3]);
1165 
1171  static vtkTypeBool PointIsWithinBounds(double point[3], double bounds[6], double delta[3]);
1172 
1182  static int PlaneIntersectsAABB(double const bounds[6], double const normal[3],
1183  double const point[3]);
1184 
1194  static double Solve3PointCircle(const double p1[3], const double p2[3], const double p3[3], double center[3]);
1195 
1199  static double Inf();
1200 
1204  static double NegInf();
1205 
1209  static double Nan();
1210 
1214  static vtkTypeBool IsInf(double x);
1215 
1219  static vtkTypeBool IsNan(double x);
1220 
1224  static bool IsFinite(double x);
1225 protected:
1226  vtkMath() {}
1227  ~vtkMath() VTK_OVERRIDE {}
1228 
1230 private:
1231  vtkMath(const vtkMath&) VTK_DELETE_FUNCTION;
1232  void operator=(const vtkMath&) VTK_DELETE_FUNCTION;
1233 };
1234 
1235 //----------------------------------------------------------------------------
1236 inline float vtkMath::RadiansFromDegrees( float x )
1237 {
1238  return x * 0.017453292f;
1239 }
1240 
1241 //----------------------------------------------------------------------------
1242 inline double vtkMath::RadiansFromDegrees( double x )
1243 {
1244  return x * 0.017453292519943295;
1245 }
1246 
1247 //----------------------------------------------------------------------------
1248 inline float vtkMath::DegreesFromRadians( float x )
1249 {
1250  return x * 57.2957795131f;
1251 }
1252 
1253 //----------------------------------------------------------------------------
1254 inline double vtkMath::DegreesFromRadians( double x )
1255 {
1256  return x * 57.29577951308232;
1257 }
1258 
1259 //----------------------------------------------------------------------------
1260 inline bool vtkMath::IsPowerOfTwo(vtkTypeUInt64 x)
1261 {
1262  return ((x != 0) & ((x & (x - 1)) == 0));
1263 }
1264 
1265 //----------------------------------------------------------------------------
1266 // Credit goes to Peter Hart and William Lewis on comp.lang.python 1997
1268 {
1269  unsigned int z = ((x > 0) ? x - 1 : 0);
1270  z |= z >> 1;
1271  z |= z >> 2;
1272  z |= z >> 4;
1273  z |= z >> 8;
1274  z |= z >> 16;
1275  return static_cast<int>(z + 1);
1276 }
1277 
1278 //----------------------------------------------------------------------------
1279 // Modify the trunc() operation provided by static_cast<int>() to get floor(),
1280 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1281 inline int vtkMath::Floor(double x)
1282 {
1283  int i = static_cast<int>(x);
1284  return i - ( i > x );
1285 }
1286 
1287 //----------------------------------------------------------------------------
1288 // Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1289 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1290 inline int vtkMath::Ceil(double x)
1291 {
1292  int i = static_cast<int>(x);
1293  return i + ( i < x );
1294 }
1295 
1296 //----------------------------------------------------------------------------
1297 template<class T>
1298 inline T vtkMath::Min(const T & a, const T & b)
1299 {
1300  return (a < b ? a : b);
1301 }
1302 
1303 //----------------------------------------------------------------------------
1304 template<class T>
1305 inline T vtkMath::Max(const T & a, const T & b)
1306 {
1307  return (a > b ? a : b);
1308 }
1309 
1310 //----------------------------------------------------------------------------
1311 inline float vtkMath::Normalize(float v[3])
1312 {
1313  float den = vtkMath::Norm( v );
1314  if ( den != 0.0 )
1315  {
1316  for (int i=0; i < 3; i++)
1317  {
1318  v[i] /= den;
1319  }
1320  }
1321  return den;
1322 }
1323 
1324 //----------------------------------------------------------------------------
1325 inline double vtkMath::Normalize(double v[3])
1326 {
1327  double den = vtkMath::Norm( v );
1328  if ( den != 0.0 )
1329  {
1330  for (int i=0; i < 3; i++)
1331  {
1332  v[i] /= den;
1333  }
1334  }
1335  return den;
1336 }
1337 
1338 //----------------------------------------------------------------------------
1339 inline float vtkMath::Normalize2D(float v[3])
1340 {
1341  float den = vtkMath::Norm2D( v );
1342  if ( den != 0.0 )
1343  {
1344  for (int i=0; i < 2; i++)
1345  {
1346  v[i] /= den;
1347  }
1348  }
1349  return den;
1350 }
1351 
1352 //----------------------------------------------------------------------------
1353 inline double vtkMath::Normalize2D(double v[3])
1354 {
1355  double den = vtkMath::Norm2D( v );
1356  if ( den != 0.0 )
1357  {
1358  for (int i=0; i < 2; i++)
1359  {
1360  v[i] /= den;
1361  }
1362  }
1363  return den;
1364 }
1365 
1366 //----------------------------------------------------------------------------
1367 inline float vtkMath::Determinant3x3(const float c1[3],
1368  const float c2[3],
1369  const float c3[3])
1370 {
1371  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1372  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1373 }
1374 
1375 //----------------------------------------------------------------------------
1376 inline double vtkMath::Determinant3x3(const double c1[3],
1377  const double c2[3],
1378  const double c3[3])
1379 {
1380  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1381  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1382 }
1383 
1384 //----------------------------------------------------------------------------
1385 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
1386  double b1, double b2, double b3,
1387  double c1, double c2, double c3)
1388 {
1389  return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
1390  - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
1391  + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
1392 }
1393 
1394 //----------------------------------------------------------------------------
1395 inline float vtkMath::Distance2BetweenPoints(const float p1[3],
1396  const float p2[3])
1397 {
1398  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1399  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1400  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1401 }
1402 
1403 //----------------------------------------------------------------------------
1404 inline double vtkMath::Distance2BetweenPoints(const double p1[3],
1405  const double p2[3])
1406 {
1407  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1408  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1409  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1410 }
1411 
1412 //----------------------------------------------------------------------------
1413 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1414 inline void vtkMath::Cross(const float a[3], const float b[3], float c[3])
1415 {
1416  float Cx = a[1] * b[2] - a[2] * b[1];
1417  float Cy = a[2] * b[0] - a[0] * b[2];
1418  float Cz = a[0] * b[1] - a[1] * b[0];
1419  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1420 }
1421 
1422 //----------------------------------------------------------------------------
1423 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1424 inline void vtkMath::Cross(const double a[3], const double b[3], double c[3])
1425 {
1426  double Cx = a[1] * b[2] - a[2] * b[1];
1427  double Cy = a[2] * b[0] - a[0] * b[2];
1428  double Cz = a[0] * b[1] - a[1] * b[0];
1429  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1430 }
1431 
1432 //----------------------------------------------------------------------------
1433 template<class T>
1434 inline double vtkDeterminant3x3(T A[3][3])
1435 {
1436  return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1437  A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1438  A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1439 }
1440 
1441 //----------------------------------------------------------------------------
1442 inline double vtkMath::Determinant3x3(float A[3][3])
1443 {
1444  return vtkDeterminant3x3( A );
1445 }
1446 
1447 //----------------------------------------------------------------------------
1448 inline double vtkMath::Determinant3x3(double A[3][3])
1449 {
1450  return vtkDeterminant3x3( A );
1451 }
1452 
1453 //----------------------------------------------------------------------------
1454 template<class T>
1455 inline T vtkMath::ClampValue(const T & value, const T & min, const T & max)
1456 {
1457  assert("pre: valid_range" && min<=max);
1458 
1459  if (value < min)
1460  {
1461  return min;
1462  }
1463 
1464  if (value > max)
1465  {
1466  return max;
1467  }
1468 
1469  return value;
1470 }
1471 
1472 //----------------------------------------------------------------------------
1473 inline void vtkMath::ClampValue(double *value, const double range[2])
1474 {
1475  if (value && range)
1476  {
1477  assert("pre: valid_range" && range[0]<=range[1]);
1478 
1479  if (*value < range[0])
1480  {
1481  *value = range[0];
1482  }
1483  else if (*value > range[1])
1484  {
1485  *value = range[1];
1486  }
1487  }
1488 }
1489 
1490 //----------------------------------------------------------------------------
1492  double value, const double range[2], double *clamped_value)
1493 {
1494  if (range && clamped_value)
1495  {
1496  assert("pre: valid_range" && range[0]<=range[1]);
1497 
1498  if (value < range[0])
1499  {
1500  *clamped_value = range[0];
1501  }
1502  else if (value > range[1])
1503  {
1504  *clamped_value = range[1];
1505  }
1506  else
1507  {
1508  *clamped_value = value;
1509  }
1510  }
1511 }
1512 
1513 // ---------------------------------------------------------------------------
1515  const double range[2])
1516 {
1517  assert("pre: valid_range" && range[0]<=range[1]);
1518 
1519  double result;
1520  if(range[0]==range[1])
1521  {
1522  result=0.0;
1523  }
1524  else
1525  {
1526  // clamp
1527  if(value<range[0])
1528  {
1529  result=range[0];
1530  }
1531  else
1532  {
1533  if(value>range[1])
1534  {
1535  result=range[1];
1536  }
1537  else
1538  {
1539  result=value;
1540  }
1541  }
1542 
1543  // normalize
1544  result=( result - range[0] ) / ( range[1] - range[0] );
1545  }
1546 
1547  assert("post: valid_result" && result>=0.0 && result<=1.0);
1548 
1549  return result;
1550 }
1551 
1552 //-----------------------------------------------------------------------------
1553 template<class T1, class T2>
1554 inline void vtkMath::TensorFromSymmetricTensor(T1 symmTensor[9], T2 tensor[9])
1555 {
1556  for (int i = 0; i < 3; i++)
1557  {
1558  tensor[4*i] = symmTensor[i];
1559  }
1560  tensor[1] = tensor[3] = symmTensor[3];
1561  tensor[2] = tensor[6] = symmTensor[5];
1562  tensor[5] = tensor[7] = symmTensor[4];
1563 }
1564 
1565 //-----------------------------------------------------------------------------
1566 template<class T>
1567 inline void vtkMath::TensorFromSymmetricTensor(T tensor[9])
1568 {
1569  tensor[6] = tensor[5]; // XZ
1570  tensor[7] = tensor[4]; // YZ
1571  tensor[8] = tensor[2]; // ZZ
1572  tensor[4] = tensor[1]; // YY
1573  tensor[5] = tensor[7]; // YZ
1574  tensor[2] = tensor[6]; // XZ
1575  tensor[1] = tensor[3]; // XY
1576 }
1577 
1578 namespace vtk_detail
1579 {
1580 // Can't specialize templates inside a template class, so we move the impl here.
1581 template <typename OutT>
1582 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
1583 { // OutT is integral -- clamp and round
1584  val = vtkMath::Max(val, static_cast<double>(vtkTypeTraits<OutT>::Min()));
1585  val = vtkMath::Min(val, static_cast<double>(vtkTypeTraits<OutT>::Max()));
1586  *ret = static_cast<OutT>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
1587 }
1588 template <>
1589 inline void RoundDoubleToIntegralIfNecessary(double val, double* retVal)
1590 { // OutT is double: passthrough
1591  *retVal = val;
1592 }
1593 template <>
1594 inline void RoundDoubleToIntegralIfNecessary(double val, float* retVal)
1595 { // OutT is float -- just clamp
1596  val = vtkMath::Max(val, static_cast<double>(vtkTypeTraits<float>::Min()));
1597  val = vtkMath::Min(val, static_cast<double>(vtkTypeTraits<float>::Max()));
1598  *retVal = static_cast<float>(val);
1599 }
1600 } // end namespace vtk_detail
1601 
1602 //-----------------------------------------------------------------------------
1603 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
1604 #define VTK_MATH_ISINF_IS_INLINE
1605 inline vtkTypeBool vtkMath::IsInf(double x)
1606 {
1607 #if defined(VTK_HAS_STD_ISINF)
1608  return std::isinf(x);
1609 #else
1610  return (isinf(x) != 0); // Force conversion to bool
1611 #endif
1612 }
1613 #endif
1614 
1615 //-----------------------------------------------------------------------------
1616 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
1617 #define VTK_MATH_ISNAN_IS_INLINE
1618 inline vtkTypeBool vtkMath::IsNan(double x)
1619 {
1620 #if defined(VTK_HAS_STD_ISNAN)
1621  return std::isnan(x);
1622 #else
1623  return (isnan(x) != 0); // Force conversion to bool
1624 #endif
1625 }
1626 #endif
1627 
1628 //-----------------------------------------------------------------------------
1629 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
1630 #define VTK_MATH_ISFINITE_IS_INLINE
1631 inline bool vtkMath::IsFinite(double x)
1632 {
1633 #if defined(VTK_HAS_STD_ISFINITE)
1634  return std::isfinite(x);
1635 #elif defined(VTK_HAS_ISFINITE)
1636  return (isfinite(x) != 0); // Force conversion to bool
1637 #else
1638  return (finite(x) != 0); // Force conversion to bool
1639 #endif
1640 }
1641 #endif
1642 
1643 #endif
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
Definition: vtkMath.h:351
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:525
~vtkMath() override
Definition: vtkMath.h:1227
static float Dot(const float a[3], const float b[3])
Dot product of two 3-vectors (float version).
Definition: vtkMath.h:377
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:988
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:1260
void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
Definition: vtkMath.h:1582
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
Definition: vtkMath.h:588
static vtkSmartPointer< vtkMathInternal > Internal
Definition: vtkMath.h:1229
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:961
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:1226
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:956
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:1514
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:317
static double Dot(const double a[3], const double b[3])
Dot product of two 3-vectors (double-precision version).
Definition: vtkMath.h:383
static void XYZToRGB(const double xyz[3], double rgb[3])
Convert color from the CIE XYZ system to RGB.
Definition: vtkMath.h:1012
static float Norm2D(const float x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:564
static void UninitializeBounds(double bounds[6])
Set the bounds to an uninitialized state.
Definition: vtkMath.h:1063
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1267
static void RGBToXYZ(const double rgb[3], double xyz[3])
Convert color from the RGB system to CIE XYZ.
Definition: vtkMath.h:1024
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1298
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:325
static void Subtract(const double a[3], const double b[3], double c[3])
Subtraction of two 3-vectors (double version).
Definition: vtkMath.h:333
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
Definition: vtkMath.h:1281
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:595
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
Definition: vtkMath.h:1236
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:979
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:360
static double Determinant3x3(float A[3][3])
Return the determinant of a 3x3 matrix.
Definition: vtkMath.h:1442
static void RGBToLab(const double rgb[3], double lab[3])
Convert color from the RGB system to CIE-L*ab.
Definition: vtkMath.h:1039
static float DegreesFromRadians(float radians)
Convert radians into degrees.
Definition: vtkMath.h:1248
static float Normalize(float v[3])
Normalize (in place) a 3-vector.
Definition: vtkMath.h:1311
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:550
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:537
static int Ceil(double x)
Rounds a double to the nearest integer not less than itself.
Definition: vtkMath.h:1290
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:531
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:425
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
Definition: vtkMath.h:342
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:974
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:397
static T ClampValue(const T &value, const T &min, const T &max)
Clamp some value against a range, return the result.
Definition: vtkMath.h:1455
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:571
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:389
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:431
static void MultiplyScalar2D(double a[2], double s)
Multiplies a 2-vector by a scalar (double version).
Definition: vtkMath.h:369
static float Distance2BetweenPoints(const float p1[3], const float p2[3])
Compute distance squared between two points p1 and p2.
Definition: vtkMath.h:1395
static void LabToRGB(const double lab[3], double rgb[3])
Convert color from the CIE-L*ab system to RGB.
Definition: vtkMath.h:1051
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:1434
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:1414
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:597
static void Add(const float a[3], const float b[3], float c[3])
Addition of two 3-vectors (float version).
Definition: vtkMath.h:309
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:1077
#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 arugments provided.
Definition: vtkMath.h:1305
static void XYZToLab(const double xyz[3], double lab[3])
Convert Color from the CIE XYZ system to CIE-L*ab.
Definition: vtkMath.h:1000