VTK
vtkMath.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkMath.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================
15  Copyright 2011 Sandia Corporation.
16  Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
17  license for use of this work by or on behalf of the
18  U.S. Government. Redistribution and use in source and binary forms, with
19  or without modification, are permitted provided that this Notice and any
20  statement of authorship are reproduced on all copies.
21 
22  Contact: pppebay@sandia.gov,dcthomp@sandia.gov
23 
24 =========================================================================*/
39 #ifndef vtkMath_h
40 #define vtkMath_h
41 
42 #include "vtkCommonCoreModule.h" // For export macro
43 #include "vtkObject.h"
44 #include "vtkTypeTraits.h" // For type traits
45 #include "vtkSmartPointer.h" // For vtkSmartPointer.
46 
47 #include "vtkMathConfigure.h" // For <cmath> and VTK_HAS_ISNAN etc.
48 
49 #include <cassert> // assert() in inline implementations.
50 
51 #ifndef DBL_MIN
52 # define VTK_DBL_MIN 2.2250738585072014e-308
53 #else // DBL_MIN
54 # define VTK_DBL_MIN DBL_MIN
55 #endif // DBL_MIN
56 
57 #ifndef DBL_EPSILON
58 # define VTK_DBL_EPSILON 2.2204460492503131e-16
59 #else // DBL_EPSILON
60 # define VTK_DBL_EPSILON DBL_EPSILON
61 #endif // DBL_EPSILON
62 
63 #ifndef VTK_DBL_EPSILON
64 # ifndef DBL_EPSILON
65 # define VTK_DBL_EPSILON 2.2204460492503131e-16
66 # else // DBL_EPSILON
67 # define VTK_DBL_EPSILON DBL_EPSILON
68 # endif // DBL_EPSILON
69 #endif // VTK_DBL_EPSILON
70 
71 class vtkDataArray;
72 class vtkPoints;
73 class vtkMathInternal;
76 
77 namespace vtk_detail
78 {
79 // forward declaration
80 template <typename OutT>
81 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret);
82 } // end namespace vtk_detail
83 
84 class VTKCOMMONCORE_EXPORT vtkMath : public vtkObject
85 {
86 public:
87  static vtkMath *New();
88  vtkTypeMacro(vtkMath,vtkObject);
89  void PrintSelf(ostream& os, vtkIndent indent) override;
90 
94  static double Pi() { return 3.141592653589793; }
95 
97 
100  static float RadiansFromDegrees( float degrees);
101  static double RadiansFromDegrees( double degrees);
103 
105 
108  static float DegreesFromRadians( float radians);
109  static double DegreesFromRadians( double radians);
111 
115  static int Round(float f) {
116  return static_cast<int>( f + ( f >= 0.0 ? 0.5 : -0.5 ) ); }
117  static int Round(double f) {
118  return static_cast<int>( f + ( f >= 0.0 ? 0.5 : -0.5 ) ); }
119 
124  template <typename OutT>
125  static void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
126  {
127  // Can't specialize template methods in a template class, so we move the
128  // implementations to a external namespace.
130  }
131 
137  static int Floor(double x);
138 
144  static int Ceil(double x);
145 
151  static int CeilLog2(vtkTypeUInt64 x);
152 
157  template<class T>
158  static T Min(const T & a, const T & b);
159 
164  template<class T>
165  static T Max(const T & a, const T & b);
166 
170  static bool IsPowerOfTwo(vtkTypeUInt64 x);
171 
177  static int NearestPowerOfTwo(int x);
178 
183  static vtkTypeInt64 Factorial( int N );
184 
190  static vtkTypeInt64 Binomial( int m, int n );
191 
202  static int* BeginCombination( int m, int n );
203 
214  static int NextCombination( int m, int n, int* combination );
215 
219  static void FreeCombination( int* combination);
220 
236  static void RandomSeed(int s);
237 
249  static int GetSeed();
250 
264  static double Random();
265 
278  static double Random( double min, double max );
279 
292  static double Gaussian();
293 
306  static double Gaussian( double mean, double std );
307 
311  static void Add(const float a[3], const float b[3], float c[3]) {
312  for (int i = 0; i < 3; ++i)
313  {
314  c[i] = a[i] + b[i];
315  }
316  }
317 
321  static void Add(const double a[3], const double b[3], double c[3]) {
322  for (int i = 0; i < 3; ++i)
323  {
324  c[i] = a[i] + b[i];
325  }
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  {
334  c[i] = a[i] - b[i];
335  }
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  {
344  c[i] = a[i] - b[i];
345  }
346  }
347 
352  static void MultiplyScalar(float a[3], float s) {
353  for (int i = 0; i < 3; ++i)
354  {
355  a[i] *= s;
356  }
357  }
358 
363  static void MultiplyScalar2D(float a[2], float s) {
364  for (int i = 0; i < 2; ++i)
365  {
366  a[i] *= s;
367  }
368  }
369 
374  static void MultiplyScalar(double a[3], double s) {
375  for (int i = 0; i < 3; ++i)
376  {
377  a[i] *= s;
378  }
379  }
380 
385  static void MultiplyScalar2D(double a[2], double s) {
386  for (int i = 0; i < 2; ++i)
387  {
388  a[i] *= s;
389  }
390  }
391 
395  static float Dot(const float a[3], const float b[3]) {
396  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
397  }
398 
402  static double Dot(const double a[3], const double b[3]) {
403  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
404  }
405 
409  static void Outer(const float a[3], const float b[3], float c[3][3]) {
410  for (int i = 0; i < 3; ++i)
411  {
412  for (int j = 0; j < 3; ++j)
413  {
414  c[i][j] = a[i] * b[j];
415  }
416  }
417  }
418 
422  static void Outer(const double a[3], const double b[3], double c[3][3]) {
423  for (int i = 0; i < 3; ++i)
424  {
425  for (int j = 0; j < 3; ++j)
426  {
427  c[i][j] = a[i] * b[j];
428  }
429  }
430  }
431 
436  static void Cross(const float a[3], const float b[3], float c[3]);
437 
442  static void Cross(const double a[3], const double b[3], double c[3]);
443 
445 
448  static float Norm(const float* x, int n);
449  static double Norm(const double* x, int n);
451 
455  static float Norm(const float v[3]) {
456  return static_cast<float> (sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] ) );
457  }
458 
462  static double Norm(const double v[3]) {
463  return sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] );
464  }
465 
470  static float Normalize(float v[3]);
471 
476  static double Normalize(double v[3]);
477 
479 
486  static void Perpendiculars(const double v1[3], double v2[3], double v3[3],
487  double theta);
488  static void Perpendiculars(const float v1[3], float v2[3], float v3[3],
489  double theta);
491 
493 
498  static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
499  static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
501 
503 
509  static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
510  static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
512 
517  static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
518 
523  static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
524 
528  static double AngleBetweenVectors(const double v1[3], const double v2[3]);
529 
534  static double GaussianAmplitude(const double variance, const double distanceFromMean);
535 
540  static double GaussianAmplitude(const double mean, const double variance, const double position);
541 
547  static double GaussianWeight(const double variance, const double distanceFromMean);
548 
554  static double GaussianWeight(const double mean, const double variance, const double position);
555 
559  static float Dot2D(const float x[2], const float y[2]) {
560  return x[0] * y[0] + x[1] * y[1];
561  }
562 
566  static double Dot2D(const double x[2], const double y[2]) {
567  return x[0] * y[0] + x[1] * y[1];
568  }
569 
573  static void Outer2D(const float x[2], const float y[2], float A[2][2])
574  {
575  for (int i=0; i < 2; ++i)
576  {
577  for (int j=0; j < 2; ++j)
578  {
579  A[i][j] = x[i] * y[j];
580  }
581  }
582  }
583 
587  static void Outer2D(const double x[2], const double y[2], double A[2][2])
588  {
589  for (int i=0; i < 2; ++i)
590  {
591  for (int j=0; j < 2; ++j)
592  {
593  A[i][j] = x[i] * y[j];
594  }
595  }
596  }
597 
602  static float Norm2D(const float x[2]) {
603  return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );
604  }
605 
610  static double Norm2D(const double x[2]) {
611  return sqrt( x[0] * x[0] + x[1] * x[1] );
612  }
613 
618  static float Normalize2D(float v[2]);
619 
624  static double Normalize2D(double v[2]);
625 
629  static float Determinant2x2(const float c1[2], const float c2[2]) {
630  return c1[0] * c2[1] - c2[0] * c1[1];
631  }
632 
634 
637  static double Determinant2x2(double a, double b, double c, double d) {
638  return a * d - b * c;
639  }
640  static double Determinant2x2(const double c1[2], const double c2[2]) {
641  return c1[0] * c2[1] - c2[0] * c1[1];
642  }
644 
646 
649  static void LUFactor3x3(float A[3][3], int index[3]);
650  static void LUFactor3x3(double A[3][3], int index[3]);
652 
654 
657  static void LUSolve3x3(const float A[3][3], const int index[3],
658  float x[3]);
659  static void LUSolve3x3(const double A[3][3], const int index[3],
660  double x[3]);
662 
664 
668  static void LinearSolve3x3(const float A[3][3], const float x[3],
669  float y[3]);
670  static void LinearSolve3x3(const double A[3][3], const double x[3],
671  double y[3]);
673 
675 
678  static void Multiply3x3(const float A[3][3], const float in[3],
679  float out[3]);
680  static void Multiply3x3(const double A[3][3], const double in[3],
681  double out[3]);
683 
685 
688  static void Multiply3x3(const float A[3][3], const float B[3][3],
689  float C[3][3]);
690  static void Multiply3x3(const double A[3][3], const double B[3][3],
691  double C[3][3]);
693 
699  static void MultiplyMatrix(const double *const *A, const double *const *B,
700  unsigned int rowA, unsigned int colA,
701  unsigned int rowB, unsigned int colB,
702  double **C);
703 
705 
709  static void Transpose3x3(const float A[3][3], float AT[3][3]);
710  static void Transpose3x3(const double A[3][3], double AT[3][3]);
712 
714 
718  static void Invert3x3(const float A[3][3], float AI[3][3]);
719  static void Invert3x3(const double A[3][3], double AI[3][3]);
721 
723 
726  static void Identity3x3(float A[3][3]);
727  static void Identity3x3(double A[3][3]);
729 
731 
734  static double Determinant3x3(const float A[3][3]);
735  static double Determinant3x3(const double A[3][3]);
737 
741  static float Determinant3x3(const float c1[3],
742  const float c2[3],
743  const float c3[3]);
744 
748  static double Determinant3x3(const double c1[3],
749  const double c2[3],
750  const double c3[3]);
751 
758  static double Determinant3x3(double a1, double a2, double a3,
759  double b1, double b2, double b3,
760  double c1, double c2, double c3);
761 
763 
770  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
771  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
773 
775 
784  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
785  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
787 
789 
795  static void MultiplyQuaternion( const float q1[4], const float q2[4], float q[4] );
796  static void MultiplyQuaternion( const double q1[4], const double q2[4], double q[4] );
798 
800 
804  static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3]);
805  static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3]);
807 
809 
813  static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3]);
814  static void RotateVectorByWXYZ(const double v[3], const double q[4], double r[3]);
816 
818 
823  static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
824  static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
826 
828 
834  static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
835  static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
837 
839 
848  static void SingularValueDecomposition3x3(const float A[3][3],
849  float U[3][3], float w[3],
850  float VT[3][3]);
851  static void SingularValueDecomposition3x3(const double A[3][3],
852  double U[3][3], double w[3],
853  double VT[3][3]);
855 
862  static vtkTypeBool SolveLinearSystem(double **A, double *x, int size);
863 
870  static vtkTypeBool InvertMatrix(double **A, double **AI, int size);
871 
877  static vtkTypeBool InvertMatrix(double **A, double **AI, int size,
878  int *tmp1Size, double *tmp2Size);
879 
902  static vtkTypeBool LUFactorLinearSystem(double **A, int *index, int size);
903 
909  static vtkTypeBool LUFactorLinearSystem(double **A, int *index, int size,
910  double *tmpSize);
911 
920  static void LUSolveLinearSystem(double **A, int *index,
921  double *x, int size);
922 
931  static double EstimateMatrixCondition(const double *const *A, int size);
932 
934 
942  static vtkTypeBool Jacobi(float **a, float *w, float **v);
943  static vtkTypeBool Jacobi(double **a, double *w, double **v);
945 
947 
956  static vtkTypeBool JacobiN(float **a, int n, float *w, float **v);
957  static vtkTypeBool JacobiN(double **a, int n, double *w, double **v);
959 
973  static vtkTypeBool SolveHomogeneousLeastSquares(int numberOfSamples, double **xt,
974  int xOrder, double **mt);
975 
990  static vtkTypeBool SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
991  double **yt, int yOrder, double **mt, int checkHomogeneous=1);
992 
994 
1001  static void RGBToHSV(const float rgb[3], float hsv[3]) {
1002  RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1003  }
1004  static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v);
1005  VTK_LEGACY(static double* RGBToHSV(const double rgb[3]) VTK_SIZEHINT(3));
1006  VTK_LEGACY(static double* RGBToHSV(double r, double g, double b) VTK_SIZEHINT(3));
1007  static void RGBToHSV(const double rgb[3], double hsv[3]) {
1008  RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1009  }
1010  static void RGBToHSV(double r, double g, double b, double *h, double *s, double *v);
1012 
1014 
1021  static void HSVToRGB(const float hsv[3], float rgb[3]) {
1022  HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
1023  }
1024  static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b);
1025  VTK_LEGACY(static double* HSVToRGB(const double hsv[3]) VTK_SIZEHINT(3));
1026  VTK_LEGACY(static double* HSVToRGB(double h, double s, double v) VTK_SIZEHINT(3));
1027  static void HSVToRGB(const double hsv[3], double rgb[3])
1028  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
1029  static void HSVToRGB(double h, double s, double v, double *r, double *g, double *b);
1031 
1033 
1036  static void LabToXYZ(const double lab[3], double xyz[3]) {
1037  LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
1038  }
1039  static void LabToXYZ(double L, double a, double b,
1040  double *x, double *y, double *z);
1041  VTK_LEGACY(static double *LabToXYZ(const double lab[3]) VTK_SIZEHINT(3));
1043 
1045 
1048  static void XYZToLab(const double xyz[3], double lab[3]) {
1049  XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
1050  }
1051  static void XYZToLab(double x, double y, double z,
1052  double *L, double *a, double *b);
1053  VTK_LEGACY(static double *XYZToLab(const double xyz[3]) VTK_SIZEHINT(3));
1055 
1057 
1060  static void XYZToRGB(const double xyz[3], double rgb[3]) {
1061  XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
1062  }
1063  static void XYZToRGB(double x, double y, double z,
1064  double *r, double *g, double *b);
1065  VTK_LEGACY(static double *XYZToRGB(const double xyz[3]) VTK_SIZEHINT(3));
1067 
1069 
1072  static void RGBToXYZ(const double rgb[3], double xyz[3]) {
1073  RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
1074  }
1075  static void RGBToXYZ(double r, double g, double b,
1076  double *x, double *y, double *z);
1077  VTK_LEGACY(static double *RGBToXYZ(const double rgb[3]) VTK_SIZEHINT(3));
1079 
1081 
1087  static void RGBToLab(const double rgb[3], double lab[3]) {
1088  RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
1089  }
1090  static void RGBToLab(double red, double green, double blue,
1091  double *L, double *a, double *b);
1092  VTK_LEGACY(static double *RGBToLab(const double rgb[3]) VTK_SIZEHINT(3));
1094 
1096 
1099  static void LabToRGB(const double lab[3], double rgb[3]) {
1100  LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
1101  }
1102  static void LabToRGB(double L, double a, double b,
1103  double *red, double *green, double *blue);
1104  VTK_LEGACY(static double *LabToRGB(const double lab[3]) VTK_SIZEHINT(3));
1106 
1108 
1111  static void UninitializeBounds(double bounds[6]) {
1112  bounds[0] = 1.0;
1113  bounds[1] = -1.0;
1114  bounds[2] = 1.0;
1115  bounds[3] = -1.0;
1116  bounds[4] = 1.0;
1117  bounds[5] = -1.0;
1118  }
1120 
1122 
1125  static vtkTypeBool AreBoundsInitialized(const double bounds[6]) {
1126  if ( bounds[1] - bounds[0] < 0.0 )
1127  {
1128  return 0;
1129  }
1130  return 1;
1131  }
1133 
1138  template<class T>
1139  static T ClampValue(const T & value, const T & min, const T & max);
1140 
1142 
1146  static void ClampValue(double *value, const double range[2]);
1147  static void ClampValue(double value, const double range[2], double *clamped_value);
1148  static void ClampValues(
1149  double *values, int nb_values, const double range[2]);
1150  static void ClampValues(
1151  const double *values, int nb_values, const double range[2], double *clamped_values);
1153 
1160  static double ClampAndNormalizeValue(double value,
1161  const double range[2]);
1162 
1167  template<class T1, class T2>
1168  static void TensorFromSymmetricTensor(const T1 symmTensor[6], T2 tensor[9]);
1169 
1175  template<class T>
1176  static void TensorFromSymmetricTensor(T tensor[9]);
1177 
1186  static int GetScalarTypeFittingRange(
1187  double range_min, double range_max,
1188  double scale = 1.0, double shift = 0.0);
1189 
1198  static vtkTypeBool GetAdjustedScalarRange(
1199  vtkDataArray *array, int comp, double range[2]);
1200 
1205  static vtkTypeBool ExtentIsWithinOtherExtent(const int extent1[6], const int extent2[6]);
1206 
1212  static vtkTypeBool BoundsIsWithinOtherBounds(const double bounds1[6], const double bounds2[6], const double delta[3]);
1213 
1219  static vtkTypeBool PointIsWithinBounds(const double point[3], const double bounds[6], const double delta[3]);
1220 
1230  static int PlaneIntersectsAABB(
1231  const double bounds[6],
1232  const double normal[3],
1233  const double point[3]);
1234 
1244  static double Solve3PointCircle(
1245  const double p1[3],
1246  const double p2[3],
1247  const double p3[3],
1248  double center[3]);
1249 
1253  static double Inf();
1254 
1258  static double NegInf();
1259 
1263  static double Nan();
1264 
1268  static vtkTypeBool IsInf(double x);
1269 
1273  static vtkTypeBool IsNan(double x);
1274 
1278  static bool IsFinite(double x);
1279 protected:
1280  vtkMath() {}
1281  ~vtkMath() override {}
1282 
1284 private:
1285  vtkMath(const vtkMath&) = delete;
1286  void operator=(const vtkMath&) = delete;
1287 };
1288 
1289 //----------------------------------------------------------------------------
1290 inline float vtkMath::RadiansFromDegrees( float x )
1291 {
1292  return x * 0.017453292f;
1293 }
1294 
1295 //----------------------------------------------------------------------------
1296 inline double vtkMath::RadiansFromDegrees( double x )
1297 {
1298  return x * 0.017453292519943295;
1299 }
1300 
1301 //----------------------------------------------------------------------------
1302 inline float vtkMath::DegreesFromRadians( float x )
1303 {
1304  return x * 57.2957795131f;
1305 }
1306 
1307 //----------------------------------------------------------------------------
1308 inline double vtkMath::DegreesFromRadians( double x )
1309 {
1310  return x * 57.29577951308232;
1311 }
1312 
1313 //----------------------------------------------------------------------------
1314 inline bool vtkMath::IsPowerOfTwo(vtkTypeUInt64 x)
1315 {
1316  return ((x != 0) & ((x & (x - 1)) == 0));
1317 }
1318 
1319 //----------------------------------------------------------------------------
1320 // Credit goes to Peter Hart and William Lewis on comp.lang.python 1997
1322 {
1323  unsigned int z = ((x > 0) ? x - 1 : 0);
1324  z |= z >> 1;
1325  z |= z >> 2;
1326  z |= z >> 4;
1327  z |= z >> 8;
1328  z |= z >> 16;
1329  return static_cast<int>(z + 1);
1330 }
1331 
1332 //----------------------------------------------------------------------------
1333 // Modify the trunc() operation provided by static_cast<int>() to get floor(),
1334 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1335 inline int vtkMath::Floor(double x)
1336 {
1337  int i = static_cast<int>(x);
1338  return i - ( i > x );
1339 }
1340 
1341 //----------------------------------------------------------------------------
1342 // Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1343 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1344 inline int vtkMath::Ceil(double x)
1345 {
1346  int i = static_cast<int>(x);
1347  return i + ( i < x );
1348 }
1349 
1350 //----------------------------------------------------------------------------
1351 template<class T>
1352 inline T vtkMath::Min(const T & a, const T & b)
1353 {
1354  return (b <= a ? b : a);
1355 }
1356 
1357 //----------------------------------------------------------------------------
1358 template<class T>
1359 inline T vtkMath::Max(const T & a, const T & b)
1360 {
1361  return (b > a ? b : a);
1362 }
1363 
1364 //----------------------------------------------------------------------------
1365 inline float vtkMath::Normalize(float v[3])
1366 {
1367  float den = vtkMath::Norm( v );
1368  if ( den != 0.0 )
1369  {
1370  for (int i=0; i < 3; ++i)
1371  {
1372  v[i] /= den;
1373  }
1374  }
1375  return den;
1376 }
1377 
1378 //----------------------------------------------------------------------------
1379 inline double vtkMath::Normalize(double v[3])
1380 {
1381  double den = vtkMath::Norm( v );
1382  if ( den != 0.0 )
1383  {
1384  for (int i=0; i < 3; ++i)
1385  {
1386  v[i] /= den;
1387  }
1388  }
1389  return den;
1390 }
1391 
1392 //----------------------------------------------------------------------------
1393 inline float vtkMath::Normalize2D(float v[3])
1394 {
1395  float den = vtkMath::Norm2D( v );
1396  if ( den != 0.0 )
1397  {
1398  for (int i=0; i < 2; ++i)
1399  {
1400  v[i] /= den;
1401  }
1402  }
1403  return den;
1404 }
1405 
1406 //----------------------------------------------------------------------------
1407 inline double vtkMath::Normalize2D(double v[3])
1408 {
1409  double den = vtkMath::Norm2D( v );
1410  if ( den != 0.0 )
1411  {
1412  for (int i=0; i < 2; ++i)
1413  {
1414  v[i] /= den;
1415  }
1416  }
1417  return den;
1418 }
1419 
1420 //----------------------------------------------------------------------------
1421 inline float vtkMath::Determinant3x3(const float c1[3],
1422  const float c2[3],
1423  const float c3[3])
1424 {
1425  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1426  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1427 }
1428 
1429 //----------------------------------------------------------------------------
1430 inline double vtkMath::Determinant3x3(const double c1[3],
1431  const double c2[3],
1432  const double c3[3])
1433 {
1434  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1435  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1436 }
1437 
1438 //----------------------------------------------------------------------------
1439 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
1440  double b1, double b2, double b3,
1441  double c1, double c2, double c3)
1442 {
1443  return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
1444  - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
1445  + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
1446 }
1447 
1448 //----------------------------------------------------------------------------
1449 inline float vtkMath::Distance2BetweenPoints(const float p1[3],
1450  const float p2[3])
1451 {
1452  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1453  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1454  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1455 }
1456 
1457 //----------------------------------------------------------------------------
1458 inline double vtkMath::Distance2BetweenPoints(const double p1[3],
1459  const double p2[3])
1460 {
1461  return ( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] )
1462  + ( p1[1] - p2[1] ) * ( p1[1] - p2[1] )
1463  + ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );
1464 }
1465 
1466 //----------------------------------------------------------------------------
1467 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1468 inline void vtkMath::Cross(const float a[3], const float b[3], float c[3])
1469 {
1470  float Cx = a[1] * b[2] - a[2] * b[1];
1471  float Cy = a[2] * b[0] - a[0] * b[2];
1472  float Cz = a[0] * b[1] - a[1] * b[0];
1473  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1474 }
1475 
1476 //----------------------------------------------------------------------------
1477 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1478 inline void vtkMath::Cross(const double a[3], const double b[3], double c[3])
1479 {
1480  double Cx = a[1] * b[2] - a[2] * b[1];
1481  double Cy = a[2] * b[0] - a[0] * b[2];
1482  double Cz = a[0] * b[1] - a[1] * b[0];
1483  c[0] = Cx; c[1] = Cy; c[2] = Cz;
1484 }
1485 
1486 //----------------------------------------------------------------------------
1487 template<class T>
1488 inline double vtkDeterminant3x3(const T A[3][3])
1489 {
1490  return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1491  A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1492  A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1493 }
1494 
1495 //----------------------------------------------------------------------------
1496 inline double vtkMath::Determinant3x3(const float A[3][3])
1497 {
1498  return vtkDeterminant3x3( A );
1499 }
1500 
1501 //----------------------------------------------------------------------------
1502 inline double vtkMath::Determinant3x3(const double A[3][3])
1503 {
1504  return vtkDeterminant3x3( A );
1505 }
1506 
1507 //----------------------------------------------------------------------------
1508 template<class T>
1509 inline T vtkMath::ClampValue(const T & value, const T & min, const T & max)
1510 {
1511  assert("pre: valid_range" && min<=max);
1512 
1513 #if __cplusplus >= 201703L
1514  return std::clamp(value, min, max);
1515 #else
1516  if (value < min)
1517  {
1518  return min;
1519  }
1520 
1521  if (max < value)
1522  {
1523  return max;
1524  }
1525 
1526  return value;
1527 #endif
1528 }
1529 
1530 //----------------------------------------------------------------------------
1531 inline void vtkMath::ClampValue(double *value, const double range[2])
1532 {
1533  if (value && range)
1534  {
1535  assert("pre: valid_range" && range[0]<=range[1]);
1536 
1537  *value = vtkMath::ClampValue(*value, range[0], range[1]);
1538  }
1539 }
1540 
1541 //----------------------------------------------------------------------------
1543  double value, const double range[2], double *clamped_value)
1544 {
1545  if (range && clamped_value)
1546  {
1547  assert("pre: valid_range" && range[0]<=range[1]);
1548 
1549  *clamped_value = vtkMath::ClampValue(value, range[0], range[1]);
1550  }
1551 }
1552 
1553 // ---------------------------------------------------------------------------
1555  const double range[2])
1556 {
1557  assert("pre: valid_range" && range[0]<=range[1]);
1558 
1559  double result;
1560  if (range[0] == range[1])
1561  {
1562  result = 0.0;
1563  }
1564  else
1565  {
1566  // clamp
1567  result=vtkMath::ClampValue(value, range[0], range[1]);
1568 
1569  // normalize
1570  result=( result - range[0] ) / ( range[1] - range[0] );
1571  }
1572 
1573  assert("post: valid_result" && result>=0.0 && result<=1.0);
1574 
1575  return result;
1576 }
1577 
1578 //-----------------------------------------------------------------------------
1579 template<class T1, class T2>
1580 inline void vtkMath::TensorFromSymmetricTensor(const T1 symmTensor[9], T2 tensor[9])
1581 {
1582  for (int i = 0; i < 3; ++i)
1583  {
1584  tensor[4*i] = symmTensor[i];
1585  }
1586  tensor[1] = tensor[3] = symmTensor[3];
1587  tensor[2] = tensor[6] = symmTensor[5];
1588  tensor[5] = tensor[7] = symmTensor[4];
1589 }
1590 
1591 //-----------------------------------------------------------------------------
1592 template<class T>
1593 inline void vtkMath::TensorFromSymmetricTensor(T tensor[9])
1594 {
1595  tensor[6] = tensor[5]; // XZ
1596  tensor[7] = tensor[4]; // YZ
1597  tensor[8] = tensor[2]; // ZZ
1598  tensor[4] = tensor[1]; // YY
1599  tensor[5] = tensor[7]; // YZ
1600  tensor[2] = tensor[6]; // XZ
1601  tensor[1] = tensor[3]; // XY
1602 }
1603 
1604 namespace vtk_detail
1605 {
1606 // Can't specialize templates inside a template class, so we move the impl here.
1607 template <typename OutT>
1608 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
1609 { // OutT is integral -- clamp and round
1610  double min = static_cast<double>(vtkTypeTraits<OutT>::Min());
1611  double max = static_cast<double>(vtkTypeTraits<OutT>::Max());
1612  val = vtkMath::ClampValue(val, min, max);
1613  *ret = static_cast<OutT>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
1614 }
1615 template <>
1616 inline void RoundDoubleToIntegralIfNecessary(double val, double* retVal)
1617 { // OutT is double: passthrough
1618  *retVal = val;
1619 }
1620 template <>
1621 inline void RoundDoubleToIntegralIfNecessary(double val, float* retVal)
1622 { // OutT is float -- just clamp (as doubles, then the cast to float is well-defined.)
1623  double min = static_cast<double>(vtkTypeTraits<float>::Min());
1624  double max = static_cast<double>(vtkTypeTraits<float>::Max());
1625  val = vtkMath::ClampValue(val, min, max);
1626  *retVal = static_cast<float>(val);
1627 }
1628 } // end namespace vtk_detail
1629 
1630 //-----------------------------------------------------------------------------
1631 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
1632 #define VTK_MATH_ISINF_IS_INLINE
1633 inline vtkTypeBool vtkMath::IsInf(double x)
1634 {
1635 #if defined(VTK_HAS_STD_ISINF)
1636  return std::isinf(x);
1637 #else
1638  return (isinf(x) != 0); // Force conversion to bool
1639 #endif
1640 }
1641 #endif
1642 
1643 //-----------------------------------------------------------------------------
1644 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
1645 #define VTK_MATH_ISNAN_IS_INLINE
1646 inline vtkTypeBool vtkMath::IsNan(double x)
1647 {
1648 #if defined(VTK_HAS_STD_ISNAN)
1649  return std::isnan(x);
1650 #else
1651  return (isnan(x) != 0); // Force conversion to bool
1652 #endif
1653 }
1654 #endif
1655 
1656 //-----------------------------------------------------------------------------
1657 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
1658 #define VTK_MATH_ISFINITE_IS_INLINE
1659 inline bool vtkMath::IsFinite(double x)
1660 {
1661 #if defined(VTK_HAS_STD_ISFINITE)
1662  return std::isfinite(x);
1663 #elif defined(VTK_HAS_ISFINITE)
1664  return (isfinite(x) != 0); // Force conversion to bool
1665 #else
1666  return (finite(x) != 0); // Force conversion to bool
1667 #endif
1668 }
1669 #endif
1670 
1671 #endif
vtkPoints
represent and manipulate 3D points
Definition: vtkPoints.h:33
vtkMath::Ceil
static int Ceil(double x)
Rounds a double to the nearest integer not less than itself.
Definition: vtkMath.h:1344
vtkMath::RGBToLab
static void RGBToLab(const double rgb[3], double lab[3])
Convert color from the RGB system to CIE-L*ab.
Definition: vtkMath.h:1087
vtkMath::MultiplyScalar
static void MultiplyScalar(double a[3], double s)
Multiplies a 3-vector by a scalar (double version).
Definition: vtkMath.h:374
vtkMath::Norm
static float Norm(const float *x, int n)
Compute the norm of n-vector.
vtkMath::RGBToHSV
static void RGBToHSV(const double rgb[3], double hsv[3])
Definition: vtkMath.h:1007
vtkMath::Floor
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
Definition: vtkMath.h:1335
vtkMath::Dot
static double Dot(const double a[3], const double b[3])
Dot product of two 3-vectors (double version).
Definition: vtkMath.h:402
vtkMinimalStandardRandomSequence
Park and Miller Sequence of pseudo random numbers.
Definition: vtkMinimalStandardRandomSequence.h:41
vtkMath::IsNan
static vtkTypeBool IsNan(double x)
Test if a number is equal to the special floating point value Not-A-Number (Nan).
vtkTypeTraits
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:29
vtkMath::NearestPowerOfTwo
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1321
vtkMath::Normalize
static float Normalize(float v[3])
Normalize (in place) a 3-vector.
Definition: vtkMath.h:1365
vtkMath::Determinant2x2
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:637
vtkX3D::scale
Definition: vtkX3D.h:229
vtkMath::RGBToHSV
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:1001
vtkMath::IsInf
static vtkTypeBool IsInf(double x)
Test if a number is equal to the special floating point value infinity.
vtkX3D::value
Definition: vtkX3D.h:220
vtkMath::UninitializeBounds
static void UninitializeBounds(double bounds[6])
Set the bounds to an uninitialized state.
Definition: vtkMath.h:1111
vtk_detail
Definition: vtkMath.h:77
vtkMath::Dot2D
static float Dot2D(const float x[2], const float y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:559
vtkMath::DegreesFromRadians
static float DegreesFromRadians(float radians)
Convert radians into degrees.
Definition: vtkMath.h:1302
vtkMath::Max
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition: vtkMath.h:1359
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkMath::Norm2D
static double Norm2D(const double x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:610
vtkMath::HSVToRGB
static void HSVToRGB(const double hsv[3], double rgb[3])
Definition: vtkMath.h:1027
vtkMath::RadiansFromDegrees
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
Definition: vtkMath.h:1290
vtkX3D::range
Definition: vtkX3D.h:238
vtkMath::XYZToRGB
static void XYZToRGB(const double xyz[3], double rgb[3])
Convert color from the CIE XYZ system to RGB.
Definition: vtkMath.h:1060
vtkSmartPointer< vtkMathInternal >
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:53
vtkMath::Determinant3x3
static double Determinant3x3(const float A[3][3])
Return the determinant of a 3x3 matrix.
Definition: vtkMath.h:1496
vtkMath::MultiplyScalar
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
Definition: vtkMath.h:352
vtkX3D::center
Definition: vtkX3D.h:230
vtkMath::Subtract
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
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:48
vtkMath::LabToRGB
static void LabToRGB(const double lab[3], double rgb[3])
Convert color from the CIE-L*ab system to RGB.
Definition: vtkMath.h:1099
vtkMath::Norm
static float Norm(const float v[3])
Compute the norm of 3-vector (float version).
Definition: vtkMath.h:455
vtk_detail::RoundDoubleToIntegralIfNecessary
void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
Definition: vtkMath.h:1608
vtkMath::ClampAndNormalizeValue
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:1554
vtkMath::HSVToRGB
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:1021
vtkMath::Subtract
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
max
#define max(a, b)
Definition: vtkX3DExporterFIWriterHelper.h:31
vtkMath::LabToXYZ
static void LabToXYZ(const double lab[3], double xyz[3])
Convert color from the CIE-L*ab system to CIE XYZ.
Definition: vtkMath.h:1036
vtkMath::MultiplyScalar2D
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
Definition: vtkMath.h:363
vtkMath::Add
static void Add(const double a[3], const double b[3], double c[3])
Addition of two 3-vectors (double version).
Definition: vtkMath.h:321
vtkMath::Outer
static void Outer(const double a[3], const double b[3], double c[3][3])
Outer product of two 3-vectors (double version).
Definition: vtkMath.h:422
vtkMath::~vtkMath
~vtkMath() override
Definition: vtkMath.h:1281
detail::isnan
bool isnan(T x)
Definition: vtkGenericDataArrayLookupHelper.h:56
vtkX3D::position
Definition: vtkX3D.h:261
vtkX3D::point
Definition: vtkX3D.h:236
vtkMath::ClampValue
static T ClampValue(const T &value, const T &min, const T &max)
Clamp some value against a range, return the result.
Definition: vtkMath.h:1509
VTK_SIZEHINT
#define VTK_SIZEHINT(...)
Definition: vtkWrappingHints.h:42
vtkTypeTraits.h
vtkMath::RoundDoubleToIntegralIfNecessary
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
vtkMath::Normalize2D
static float Normalize2D(float v[2])
Normalize (in place) a 2-vector.
vtkMath::Norm2D
static float Norm2D(const float x[2])
Compute the norm of a 2-vector.
Definition: vtkMath.h:602
vtkMath::Pi
static double Pi()
A mathematical constant.
Definition: vtkMath.h:94
vtkMath::MultiplyScalar2D
static void MultiplyScalar2D(double a[2], double s)
Multiplies a 2-vector by a scalar (double version).
Definition: vtkMath.h:385
vtkMath::TensorFromSymmetricTensor
static void TensorFromSymmetricTensor(const T1 symmTensor[6], T2 tensor[9])
Convert a 6-Component symmetric tensor into a 9-Component tensor, no allocation performed.
vtkMath::Min
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1352
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkSmartPointer.h
vtkDeterminant3x3
double vtkDeterminant3x3(const T A[3][3])
Definition: vtkMath.h:1488
vtkX3D::size
Definition: vtkX3D.h:253
vtkObject::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkMath::Outer2D
static void Outer2D(const double x[2], const double y[2], double A[2][2])
Outer product of two 2-vectors (double version).
Definition: vtkMath.h:587
vtkMath::Determinant2x2
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
Definition: vtkMath.h:629
vtkObject.h
vtkMath::Outer
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:409
vtkBoxMuellerRandomSequence
Gaussian sequence of pseudo random numbers implemented with the Box-Mueller transform.
Definition: vtkBoxMuellerRandomSequence.h:32
vtkMath::Determinant2x2
static double Determinant2x2(const double c1[2], const double c2[2])
Definition: vtkMath.h:640
vtkMath
performs common math operations
Definition: vtkMath.h:84
vtkMath::IsPowerOfTwo
static bool IsPowerOfTwo(vtkTypeUInt64 x)
Returns true if integer is a power of two.
Definition: vtkMath.h:1314
vtkMath::Cross
static void Cross(const float a[3], const float b[3], float c[3])
Cross product of two 3-vectors.
Definition: vtkMath.h:1468
vtkMath::Round
static int Round(double f)
Definition: vtkMath.h:117
vtkMath::XYZToLab
static void XYZToLab(const double xyz[3], double lab[3])
Convert Color from the CIE XYZ system to CIE-L*ab.
Definition: vtkMath.h:1048
vtkMath::RGBToXYZ
static void RGBToXYZ(const double rgb[3], double xyz[3])
Convert color from the RGB system to CIE XYZ.
Definition: vtkMath.h:1072
vtkMath::Round
static int Round(float f)
Rounds a float to the nearest integer.
Definition: vtkMath.h:115
vtkMath::Distance2BetweenPoints
static float Distance2BetweenPoints(const float p1[3], const float p2[3])
Compute distance squared between two points p1 and p2.
Definition: vtkMath.h:1449
vtkMath::IsFinite
static bool IsFinite(double x)
Test if a number has finite value i.e.
vtkMath::Dot2D
static double Dot2D(const double x[2], const double y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:566
vtkMath::Outer2D
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:573
vtkMath::Add
static void Add(const float a[3], const float b[3], float c[3])
Addition of two 3-vectors (float version).
Definition: vtkMath.h:311
vtkMath::Norm
static double Norm(const double v[3])
Compute the norm of 3-vector (double version).
Definition: vtkMath.h:462
vtkMath::Dot
static float Dot(const float a[3], const float b[3])
Dot product of two 3-vectors (float version).
Definition: vtkMath.h:395
vtkX3D::index
Definition: vtkX3D.h:246
vtkMath::AreBoundsInitialized
static vtkTypeBool AreBoundsInitialized(const double bounds[6])
Are the bounds initialized?
Definition: vtkMath.h:1125
vtkMath::Internal
static vtkSmartPointer< vtkMathInternal > Internal
Definition: vtkMath.h:1283
vtkMath::vtkMath
vtkMath()
Definition: vtkMath.h:1280
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
h