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 "vtkSmartPointer.h" // For vtkSmartPointer.
45 #include "vtkTypeTraits.h" // For type traits
46 
47 #include "vtkMathConfigure.h" // For <cmath> and VTK_HAS_ISNAN etc.
48 
49 #include <algorithm> // for std::clamp
50 #include <cassert> // assert() in inline implementations.
51 
52 #ifndef DBL_MIN
53 #define VTK_DBL_MIN 2.2250738585072014e-308
54 #else // DBL_MIN
55 #define VTK_DBL_MIN DBL_MIN
56 #endif // DBL_MIN
57 
58 #ifndef DBL_EPSILON
59 #define VTK_DBL_EPSILON 2.2204460492503131e-16
60 #else // DBL_EPSILON
61 #define VTK_DBL_EPSILON DBL_EPSILON
62 #endif // DBL_EPSILON
63 
64 #ifndef VTK_DBL_EPSILON
65 #ifndef DBL_EPSILON
66 #define VTK_DBL_EPSILON 2.2204460492503131e-16
67 #else // DBL_EPSILON
68 #define VTK_DBL_EPSILON DBL_EPSILON
69 #endif // DBL_EPSILON
70 #endif // VTK_DBL_EPSILON
71 
72 class vtkDataArray;
73 class vtkPoints;
74 class vtkMathInternal;
77 
78 namespace vtk_detail
79 {
80 // forward declaration
81 template <typename OutT>
82 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret);
83 } // end namespace vtk_detail
84 
85 class VTKCOMMONCORE_EXPORT vtkMath : public vtkObject
86 {
87 public:
88  static vtkMath* New();
89  vtkTypeMacro(vtkMath, vtkObject);
90  void PrintSelf(ostream& os, vtkIndent indent) override;
91 
95  static double Pi() { return 3.141592653589793; }
96 
98 
101  static float RadiansFromDegrees(float degrees);
102  static double RadiansFromDegrees(double degrees);
104 
106 
109  static float DegreesFromRadians(float radians);
110  static double DegreesFromRadians(double radians);
112 
116 #if 1
117  static int Round(float f) { return static_cast<int>(f + (f >= 0.0 ? 0.5 : -0.5)); }
118  static int Round(double f) { return static_cast<int>(f + (f >= 0.0 ? 0.5 : -0.5)); }
119 #endif
120 
125  template <typename OutT>
126  static void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
127  {
128  // Can't specialize template methods in a template class, so we move the
129  // implementations to a external namespace.
131  }
132 
138  static int Floor(double x);
139 
145  static int Ceil(double x);
146 
152  static int CeilLog2(vtkTypeUInt64 x);
153 
158  template <class T>
159  static T Min(const T& a, const T& b);
160 
165  template <class T>
166  static T Max(const T& a, const T& b);
167 
171  static bool IsPowerOfTwo(vtkTypeUInt64 x);
172 
178  static int NearestPowerOfTwo(int x);
179 
184  static vtkTypeInt64 Factorial(int N);
185 
191  static vtkTypeInt64 Binomial(int m, int n);
192 
204  static int* BeginCombination(int m, int n);
205 
216  static int NextCombination(int m, int n, int* combination);
217 
221  static void FreeCombination(int* combination);
222 
238  static void RandomSeed(int s);
239 
251  static int GetSeed();
252 
266  static double Random();
267 
280  static double Random(double min, double max);
281 
294  static double Gaussian();
295 
308  static double Gaussian(double mean, double std);
309 
313  static void Add(const float a[3], const float b[3], float c[3])
314  {
315  for (int i = 0; i < 3; ++i)
316  {
317  c[i] = a[i] + b[i];
318  }
319  }
320 
324  static void Add(const double a[3], const double b[3], double c[3])
325  {
326  for (int i = 0; i < 3; ++i)
327  {
328  c[i] = a[i] + b[i];
329  }
330  }
331 
335  static void Subtract(const float a[3], const float b[3], float c[3])
336  {
337  for (int i = 0; i < 3; ++i)
338  {
339  c[i] = a[i] - b[i];
340  }
341  }
342 
346  static void Subtract(const double a[3], const double b[3], double c[3])
347  {
348  for (int i = 0; i < 3; ++i)
349  {
350  c[i] = a[i] - b[i];
351  }
352  }
353 
358  static void MultiplyScalar(float a[3], float s)
359  {
360  for (int i = 0; i < 3; ++i)
361  {
362  a[i] *= s;
363  }
364  }
365 
370  static void MultiplyScalar2D(float a[2], float s)
371  {
372  for (int i = 0; i < 2; ++i)
373  {
374  a[i] *= s;
375  }
376  }
377 
382  static void MultiplyScalar(double a[3], double s)
383  {
384  for (int i = 0; i < 3; ++i)
385  {
386  a[i] *= s;
387  }
388  }
389 
394  static void MultiplyScalar2D(double a[2], double s)
395  {
396  for (int i = 0; i < 2; ++i)
397  {
398  a[i] *= s;
399  }
400  }
401 
405  static float Dot(const float a[3], const float b[3])
406  {
407  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
408  }
409 
413  static double Dot(const double a[3], const double b[3])
414  {
415  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
416  }
417 
421  static void Outer(const float a[3], const float b[3], float c[3][3])
422  {
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 
435  static void Outer(const double a[3], const double b[3], double c[3][3])
436  {
437  for (int i = 0; i < 3; ++i)
438  {
439  for (int j = 0; j < 3; ++j)
440  {
441  c[i][j] = a[i] * b[j];
442  }
443  }
444  }
445 
450  static void Cross(const float a[3], const float b[3], float c[3]);
451 
456  static void Cross(const double a[3], const double b[3], double c[3]);
457 
459 
462  static float Norm(const float* x, int n);
463  static double Norm(const double* x, int n);
465 
469  static float Norm(const float v[3]) { return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); }
470 
474  static double Norm(const double v[3])
475  {
476  return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
477  }
478 
483  static float Normalize(float v[3]);
484 
489  static double Normalize(double v[3]);
490 
492 
499  static void Perpendiculars(const double v1[3], double v2[3], double v3[3], double theta);
500  static void Perpendiculars(const float v1[3], float v2[3], float v3[3], double theta);
502 
504 
509  static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
510  static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
512 
514 
520  static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
521  static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
523 
528  static float Distance2BetweenPoints(const float p1[3], const float p2[3]);
529 
534  static double Distance2BetweenPoints(const double p1[3], const double p2[3]);
535 
539  static double AngleBetweenVectors(const double v1[3], const double v2[3]);
540 
545  static double GaussianAmplitude(const double variance, const double distanceFromMean);
546 
551  static double GaussianAmplitude(const double mean, const double variance, const double position);
552 
558  static double GaussianWeight(const double variance, const double distanceFromMean);
559 
565  static double GaussianWeight(const double mean, const double variance, const double position);
566 
570  static float Dot2D(const float x[2], const float y[2]) { return x[0] * y[0] + x[1] * y[1]; }
571 
575  static double Dot2D(const double x[2], const double y[2]) { return x[0] * y[0] + x[1] * y[1]; }
576 
580  static void Outer2D(const float x[2], const float y[2], float A[2][2])
581  {
582  for (int i = 0; i < 2; ++i)
583  {
584  for (int j = 0; j < 2; ++j)
585  {
586  A[i][j] = x[i] * y[j];
587  }
588  }
589  }
590 
594  static void Outer2D(const double x[2], const double y[2], double A[2][2])
595  {
596  for (int i = 0; i < 2; ++i)
597  {
598  for (int j = 0; j < 2; ++j)
599  {
600  A[i][j] = x[i] * y[j];
601  }
602  }
603  }
604 
609  static float Norm2D(const float x[2]) { return std::sqrt(x[0] * x[0] + x[1] * x[1]); }
610 
615  static double Norm2D(const double x[2]) { return std::sqrt(x[0] * x[0] + x[1] * x[1]); }
616 
621  static float Normalize2D(float v[2]);
622 
627  static double Normalize2D(double v[2]);
628 
632  static float Determinant2x2(const float c1[2], const float c2[2])
633  {
634  return c1[0] * c2[1] - c2[0] * c1[1];
635  }
636 
638 
641  static double Determinant2x2(double a, double b, double c, double d) { return a * d - b * c; }
642  static double Determinant2x2(const double c1[2], const double c2[2])
643  {
644  return c1[0] * c2[1] - c2[0] * c1[1];
645  }
647 
649 
652  static void LUFactor3x3(float A[3][3], int index[3]);
653  static void LUFactor3x3(double A[3][3], int index[3]);
655 
657 
660  static void LUSolve3x3(const float A[3][3], const int index[3], float x[3]);
661  static void LUSolve3x3(const double A[3][3], const int index[3], double x[3]);
663 
665 
669  static void LinearSolve3x3(const float A[3][3], const float x[3], float y[3]);
670  static void LinearSolve3x3(const double A[3][3], const double x[3], double y[3]);
672 
674 
677  static void Multiply3x3(const float A[3][3], const float in[3], float out[3]);
678  static void Multiply3x3(const double A[3][3], const double in[3], double out[3]);
680 
682 
685  static void Multiply3x3(const float A[3][3], const float B[3][3], float C[3][3]);
686  static void Multiply3x3(const double A[3][3], const double B[3][3], double C[3][3]);
688 
694  static void MultiplyMatrix(const double* const* A, const double* const* B, unsigned int rowA,
695  unsigned int colA, unsigned int rowB, unsigned int colB, double** C);
696 
698 
702  static void Transpose3x3(const float A[3][3], float AT[3][3]);
703  static void Transpose3x3(const double A[3][3], double AT[3][3]);
705 
707 
711  static void Invert3x3(const float A[3][3], float AI[3][3]);
712  static void Invert3x3(const double A[3][3], double AI[3][3]);
714 
716 
719  static void Identity3x3(float A[3][3]);
720  static void Identity3x3(double A[3][3]);
722 
724 
727  static double Determinant3x3(const float A[3][3]);
728  static double Determinant3x3(const double A[3][3]);
730 
734  static float Determinant3x3(const float c1[3], const float c2[3], const float c3[3]);
735 
739  static double Determinant3x3(const double c1[3], const double c2[3], const double c3[3]);
740 
747  static double Determinant3x3(double a1, double a2, double a3, double b1, double b2, double b3,
748  double c1, double c2, double c3);
749 
751 
758  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
759  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
761 
763 
772  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
773  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
775 
777 
783  static void MultiplyQuaternion(const float q1[4], const float q2[4], float q[4]);
784  static void MultiplyQuaternion(const double q1[4], const double q2[4], double q[4]);
786 
788 
792  static void RotateVectorByNormalizedQuaternion(const float v[3], const float q[4], float r[3]);
793  static void RotateVectorByNormalizedQuaternion(const double v[3], const double q[4], double r[3]);
795 
797 
801  static void RotateVectorByWXYZ(const float v[3], const float q[4], float r[3]);
802  static void RotateVectorByWXYZ(const double v[3], const double q[4], double r[3]);
804 
806 
811  static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
812  static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
814 
816 
822  static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
823  static void Diagonalize3x3(const double A[3][3], double w[3], double V[3][3]);
825 
827 
836  static void SingularValueDecomposition3x3(
837  const float A[3][3], float U[3][3], float w[3], float VT[3][3]);
838  static void SingularValueDecomposition3x3(
839  const double A[3][3], double U[3][3], double w[3], double VT[3][3]);
841 
848  static vtkTypeBool SolveLinearSystem(double** A, double* x, int size);
849 
856  static vtkTypeBool InvertMatrix(double** A, double** AI, int size);
857 
863  static vtkTypeBool InvertMatrix(
864  double** A, double** AI, int size, int* tmp1Size, double* tmp2Size);
865 
888  static vtkTypeBool LUFactorLinearSystem(double** A, int* index, int size);
889 
895  static vtkTypeBool LUFactorLinearSystem(double** A, int* index, int size, double* tmpSize);
896 
905  static void LUSolveLinearSystem(double** A, int* index, double* x, int size);
906 
915  static double EstimateMatrixCondition(const double* const* A, int size);
916 
918 
926  static vtkTypeBool Jacobi(float** a, float* w, float** v);
927  static vtkTypeBool Jacobi(double** a, double* w, double** v);
929 
931 
940  static vtkTypeBool JacobiN(float** a, int n, float* w, float** v);
941  static vtkTypeBool JacobiN(double** a, int n, double* w, double** v);
943 
957  static vtkTypeBool SolveHomogeneousLeastSquares(
958  int numberOfSamples, double** xt, int xOrder, double** mt);
959 
974  static vtkTypeBool SolveLeastSquares(int numberOfSamples, double** xt, int xOrder, double** yt,
975  int yOrder, double** mt, int checkHomogeneous = 1);
976 
978 
985  static void RGBToHSV(const float rgb[3], float hsv[3])
986  {
987  RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
988  }
989  static void RGBToHSV(float r, float g, float b, float* h, float* s, float* v);
990  static void RGBToHSV(const double rgb[3], double hsv[3])
991  {
992  RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
993  }
994  static void RGBToHSV(double r, double g, double b, double* h, double* s, double* v);
996 
998 
1005  static void HSVToRGB(const float hsv[3], float rgb[3])
1006  {
1007  HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
1008  }
1009  static void HSVToRGB(float h, float s, float v, float* r, float* g, float* b);
1010  static void HSVToRGB(const double hsv[3], double rgb[3])
1011  {
1012  HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
1013  }
1014  static void HSVToRGB(double h, double s, double v, double* r, double* g, double* b);
1016 
1018 
1021  static void LabToXYZ(const double lab[3], double xyz[3])
1022  {
1023  LabToXYZ(lab[0], lab[1], lab[2], xyz + 0, xyz + 1, xyz + 2);
1024  }
1025  static void LabToXYZ(double L, double a, double b, double* x, double* y, double* z);
1027 
1029 
1032  static void XYZToLab(const double xyz[3], double lab[3])
1033  {
1034  XYZToLab(xyz[0], xyz[1], xyz[2], lab + 0, lab + 1, lab + 2);
1035  }
1036  static void XYZToLab(double x, double y, double z, double* L, double* a, double* b);
1038 
1040 
1043  static void XYZToRGB(const double xyz[3], double rgb[3])
1044  {
1045  XYZToRGB(xyz[0], xyz[1], xyz[2], rgb + 0, rgb + 1, rgb + 2);
1046  }
1047  static void XYZToRGB(double x, double y, double z, double* r, double* g, double* b);
1049 
1051 
1054  static void RGBToXYZ(const double rgb[3], double xyz[3])
1055  {
1056  RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz + 0, xyz + 1, xyz + 2);
1057  }
1058  static void RGBToXYZ(double r, double g, double b, double* x, double* y, double* z);
1060 
1062 
1068  static void RGBToLab(const double rgb[3], double lab[3])
1069  {
1070  RGBToLab(rgb[0], rgb[1], rgb[2], lab + 0, lab + 1, lab + 2);
1071  }
1072  static void RGBToLab(double red, double green, double blue, double* L, double* a, double* b);
1074 
1076 
1079  static void LabToRGB(const double lab[3], double rgb[3])
1080  {
1081  LabToRGB(lab[0], lab[1], lab[2], rgb + 0, rgb + 1, rgb + 2);
1082  }
1083  static void LabToRGB(double L, double a, double b, double* red, double* green, double* blue);
1085 
1087 
1090  static void UninitializeBounds(double bounds[6])
1091  {
1092  bounds[0] = 1.0;
1093  bounds[1] = -1.0;
1094  bounds[2] = 1.0;
1095  bounds[3] = -1.0;
1096  bounds[4] = 1.0;
1097  bounds[5] = -1.0;
1098  }
1100 
1102 
1105  static vtkTypeBool AreBoundsInitialized(const double bounds[6])
1106  {
1107  if (bounds[1] - bounds[0] < 0.0)
1108  {
1109  return 0;
1110  }
1111  return 1;
1112  }
1114 
1119  template <class T>
1120  static T ClampValue(const T& value, const T& min, const T& max);
1121 
1123 
1127  static void ClampValue(double* value, const double range[2]);
1128  static void ClampValue(double value, const double range[2], double* clamped_value);
1129  static void ClampValues(double* values, int nb_values, const double range[2]);
1130  static void ClampValues(
1131  const double* values, int nb_values, const double range[2], double* clamped_values);
1133 
1140  static double ClampAndNormalizeValue(double value, const double range[2]);
1141 
1146  template <class T1, class T2>
1147  static void TensorFromSymmetricTensor(const T1 symmTensor[6], T2 tensor[9]);
1148 
1154  template <class T>
1155  static void TensorFromSymmetricTensor(T tensor[9]);
1156 
1165  static int GetScalarTypeFittingRange(
1166  double range_min, double range_max, double scale = 1.0, double shift = 0.0);
1167 
1176  static vtkTypeBool GetAdjustedScalarRange(vtkDataArray* array, int comp, double range[2]);
1177 
1182  static vtkTypeBool ExtentIsWithinOtherExtent(const int extent1[6], const int extent2[6]);
1183 
1189  static vtkTypeBool BoundsIsWithinOtherBounds(
1190  const double bounds1[6], const double bounds2[6], const double delta[3]);
1191 
1197  static vtkTypeBool PointIsWithinBounds(
1198  const double point[3], const double bounds[6], const double delta[3]);
1199 
1209  static int PlaneIntersectsAABB(
1210  const double bounds[6], const double normal[3], const double point[3]);
1211 
1221  static double Solve3PointCircle(
1222  const double p1[3], const double p2[3], const double p3[3], double center[3]);
1223 
1227  static double Inf();
1228 
1232  static double NegInf();
1233 
1237  static double Nan();
1238 
1242  static vtkTypeBool IsInf(double x);
1243 
1247  static vtkTypeBool IsNan(double x);
1248 
1253  static bool IsFinite(double x);
1254 
1255 protected:
1256  vtkMath() {}
1257  ~vtkMath() override {}
1258 
1260 
1261 private:
1262  vtkMath(const vtkMath&) = delete;
1263  void operator=(const vtkMath&) = delete;
1264 };
1265 
1266 //----------------------------------------------------------------------------
1267 inline float vtkMath::RadiansFromDegrees(float x)
1268 {
1269  return x * 0.017453292f;
1270 }
1271 
1272 //----------------------------------------------------------------------------
1273 inline double vtkMath::RadiansFromDegrees(double x)
1274 {
1275  return x * 0.017453292519943295;
1276 }
1277 
1278 //----------------------------------------------------------------------------
1279 inline float vtkMath::DegreesFromRadians(float x)
1280 {
1281  return x * 57.2957795131f;
1282 }
1283 
1284 //----------------------------------------------------------------------------
1285 inline double vtkMath::DegreesFromRadians(double x)
1286 {
1287  return x * 57.29577951308232;
1288 }
1289 
1290 //----------------------------------------------------------------------------
1291 inline bool vtkMath::IsPowerOfTwo(vtkTypeUInt64 x)
1292 {
1293  return ((x != 0) & ((x & (x - 1)) == 0));
1294 }
1295 
1296 //----------------------------------------------------------------------------
1297 // Credit goes to Peter Hart and William Lewis on comp.lang.python 1997
1299 {
1300  unsigned int z = static_cast<unsigned int>(((x > 0) ? x - 1 : 0));
1301  z |= z >> 1;
1302  z |= z >> 2;
1303  z |= z >> 4;
1304  z |= z >> 8;
1305  z |= z >> 16;
1306  return static_cast<int>(z + 1);
1307 }
1308 
1309 //----------------------------------------------------------------------------
1310 // Modify the trunc() operation provided by static_cast<int>() to get floor(),
1311 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1312 inline int vtkMath::Floor(double x)
1313 {
1314  int i = static_cast<int>(x);
1315  return i - (i > x);
1316 }
1317 
1318 //----------------------------------------------------------------------------
1319 // Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1320 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1321 inline int vtkMath::Ceil(double x)
1322 {
1323  int i = static_cast<int>(x);
1324  return i + (i < x);
1325 }
1326 
1327 //----------------------------------------------------------------------------
1328 template <class T>
1329 inline T vtkMath::Min(const T& a, const T& b)
1330 {
1331  return (b <= a ? b : a);
1332 }
1333 
1334 //----------------------------------------------------------------------------
1335 template <class T>
1336 inline T vtkMath::Max(const T& a, const T& b)
1337 {
1338  return (b > a ? b : a);
1339 }
1340 
1341 //----------------------------------------------------------------------------
1342 inline float vtkMath::Normalize(float v[3])
1343 {
1344  float den = vtkMath::Norm(v);
1345  if (den != 0.0)
1346  {
1347  for (int i = 0; i < 3; ++i)
1348  {
1349  v[i] /= den;
1350  }
1351  }
1352  return den;
1353 }
1354 
1355 //----------------------------------------------------------------------------
1356 inline double vtkMath::Normalize(double v[3])
1357 {
1358  double den = vtkMath::Norm(v);
1359  if (den != 0.0)
1360  {
1361  for (int i = 0; i < 3; ++i)
1362  {
1363  v[i] /= den;
1364  }
1365  }
1366  return den;
1367 }
1368 
1369 //----------------------------------------------------------------------------
1370 inline float vtkMath::Normalize2D(float v[3])
1371 {
1372  float den = vtkMath::Norm2D(v);
1373  if (den != 0.0)
1374  {
1375  for (int i = 0; i < 2; ++i)
1376  {
1377  v[i] /= den;
1378  }
1379  }
1380  return den;
1381 }
1382 
1383 //----------------------------------------------------------------------------
1384 inline double vtkMath::Normalize2D(double v[3])
1385 {
1386  double den = vtkMath::Norm2D(v);
1387  if (den != 0.0)
1388  {
1389  for (int i = 0; i < 2; ++i)
1390  {
1391  v[i] /= den;
1392  }
1393  }
1394  return den;
1395 }
1396 
1397 //----------------------------------------------------------------------------
1398 inline float vtkMath::Determinant3x3(const float c1[3], const float c2[3], const float c3[3])
1399 {
1400  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1401  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1402 }
1403 
1404 //----------------------------------------------------------------------------
1405 inline double vtkMath::Determinant3x3(const double c1[3], const double c2[3], const double c3[3])
1406 {
1407  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1408  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1409 }
1410 
1411 //----------------------------------------------------------------------------
1413  double a1, double a2, double a3, double b1, double b2, double b3, double c1, double c2, double c3)
1414 {
1415  return (a1 * vtkMath::Determinant2x2(b2, b3, c2, c3) -
1416  b1 * vtkMath::Determinant2x2(a2, a3, c2, c3) + c1 * vtkMath::Determinant2x2(a2, a3, b2, b3));
1417 }
1418 
1419 //----------------------------------------------------------------------------
1420 inline float vtkMath::Distance2BetweenPoints(const float p1[3], const float p2[3])
1421 {
1422  return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1423  (p1[2] - p2[2]) * (p1[2] - p2[2]));
1424 }
1425 
1426 //----------------------------------------------------------------------------
1427 inline double vtkMath::Distance2BetweenPoints(const double p1[3], const double p2[3])
1428 {
1429  return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1430  (p1[2] - p2[2]) * (p1[2] - p2[2]));
1431 }
1432 
1433 //----------------------------------------------------------------------------
1434 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1435 inline void vtkMath::Cross(const float a[3], const float b[3], float c[3])
1436 {
1437  float Cx = a[1] * b[2] - a[2] * b[1];
1438  float Cy = a[2] * b[0] - a[0] * b[2];
1439  float Cz = a[0] * b[1] - a[1] * b[0];
1440  c[0] = Cx;
1441  c[1] = Cy;
1442  c[2] = Cz;
1443 }
1444 
1445 //----------------------------------------------------------------------------
1446 // Cross product of two 3-vectors. Result (a x b) is stored in c[3].
1447 inline void vtkMath::Cross(const double a[3], const double b[3], double c[3])
1448 {
1449  double Cx = a[1] * b[2] - a[2] * b[1];
1450  double Cy = a[2] * b[0] - a[0] * b[2];
1451  double Cz = a[0] * b[1] - a[1] * b[0];
1452  c[0] = Cx;
1453  c[1] = Cy;
1454  c[2] = Cz;
1455 }
1456 
1457 //----------------------------------------------------------------------------
1458 template <class T>
1459 inline double vtkDeterminant3x3(const T A[3][3])
1460 {
1461  return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] + A[2][0] * A[0][1] * A[1][2] -
1462  A[0][0] * A[2][1] * A[1][2] - A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1463 }
1464 
1465 //----------------------------------------------------------------------------
1466 inline double vtkMath::Determinant3x3(const float A[3][3])
1467 {
1468  return vtkDeterminant3x3(A);
1469 }
1470 
1471 //----------------------------------------------------------------------------
1472 inline double vtkMath::Determinant3x3(const double A[3][3])
1473 {
1474  return vtkDeterminant3x3(A);
1475 }
1476 
1477 //----------------------------------------------------------------------------
1478 template <class T>
1479 inline T vtkMath::ClampValue(const T& value, const T& min, const T& max)
1480 {
1481  assert("pre: valid_range" && min <= max);
1482 
1483 #if __cplusplus >= 201703L
1484  return std::clamp(value, min, max);
1485 #else
1486  // compilers are good at optimizing the ternary operator,
1487  // use '<' since it is preferred by STL for custom types
1488  T v = (min < value ? value : min);
1489  return (v < max ? v : max);
1490 #endif
1491 }
1492 
1493 //----------------------------------------------------------------------------
1494 inline void vtkMath::ClampValue(double* value, const double range[2])
1495 {
1496  if (value && range)
1497  {
1498  assert("pre: valid_range" && range[0] <= range[1]);
1499 
1500  *value = vtkMath::ClampValue(*value, range[0], range[1]);
1501  }
1502 }
1503 
1504 //----------------------------------------------------------------------------
1505 inline void vtkMath::ClampValue(double value, const double range[2], double* clamped_value)
1506 {
1507  if (range && clamped_value)
1508  {
1509  assert("pre: valid_range" && range[0] <= range[1]);
1510 
1511  *clamped_value = vtkMath::ClampValue(value, range[0], range[1]);
1512  }
1513 }
1514 
1515 // ---------------------------------------------------------------------------
1516 inline double vtkMath::ClampAndNormalizeValue(double value, const double range[2])
1517 {
1518  assert("pre: valid_range" && range[0] <= range[1]);
1519 
1520  double result;
1521  if (range[0] == range[1])
1522  {
1523  result = 0.0;
1524  }
1525  else
1526  {
1527  // clamp
1528  result = vtkMath::ClampValue(value, range[0], range[1]);
1529 
1530  // normalize
1531  result = (result - range[0]) / (range[1] - range[0]);
1532  }
1533 
1534  assert("post: valid_result" && result >= 0.0 && result <= 1.0);
1535 
1536  return result;
1537 }
1538 
1539 //-----------------------------------------------------------------------------
1540 template <class T1, class T2>
1541 inline void vtkMath::TensorFromSymmetricTensor(const T1 symmTensor[9], T2 tensor[9])
1542 {
1543  for (int i = 0; i < 3; ++i)
1544  {
1545  tensor[4 * i] = symmTensor[i];
1546  }
1547  tensor[1] = tensor[3] = symmTensor[3];
1548  tensor[2] = tensor[6] = symmTensor[5];
1549  tensor[5] = tensor[7] = symmTensor[4];
1550 }
1551 
1552 //-----------------------------------------------------------------------------
1553 template <class T>
1554 inline void vtkMath::TensorFromSymmetricTensor(T tensor[9])
1555 {
1556  tensor[6] = tensor[5]; // XZ
1557  tensor[7] = tensor[4]; // YZ
1558  tensor[8] = tensor[2]; // ZZ
1559  tensor[4] = tensor[1]; // YY
1560  tensor[5] = tensor[7]; // YZ
1561  tensor[2] = tensor[6]; // XZ
1562  tensor[1] = tensor[3]; // XY
1563 }
1564 
1565 namespace vtk_detail
1566 {
1567 // Can't specialize templates inside a template class, so we move the impl here.
1568 template <typename OutT>
1569 void RoundDoubleToIntegralIfNecessary(double val, OutT* ret)
1570 { // OutT is integral -- clamp and round
1571  double min = static_cast<double>(vtkTypeTraits<OutT>::Min());
1572  double max = static_cast<double>(vtkTypeTraits<OutT>::Max());
1573  val = vtkMath::ClampValue(val, min, max);
1574  *ret = static_cast<OutT>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
1575 }
1576 template <>
1577 inline void RoundDoubleToIntegralIfNecessary(double val, double* retVal)
1578 { // OutT is double: passthrough
1579  *retVal = val;
1580 }
1581 template <>
1582 inline void RoundDoubleToIntegralIfNecessary(double val, float* retVal)
1583 { // OutT is float -- just clamp (as doubles, then the cast to float is well-defined.)
1584  double min = static_cast<double>(vtkTypeTraits<float>::Min());
1585  double max = static_cast<double>(vtkTypeTraits<float>::Max());
1586  val = vtkMath::ClampValue(val, min, max);
1587  *retVal = static_cast<float>(val);
1588 }
1589 } // end namespace vtk_detail
1590 
1591 //-----------------------------------------------------------------------------
1592 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
1593 #define VTK_MATH_ISINF_IS_INLINE
1594 inline vtkTypeBool vtkMath::IsInf(double x)
1595 {
1596 #if defined(VTK_HAS_STD_ISINF)
1597  return std::isinf(x);
1598 #else
1599  return (isinf(x) != 0); // Force conversion to bool
1600 #endif
1601 }
1602 #endif
1603 
1604 //-----------------------------------------------------------------------------
1605 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
1606 #define VTK_MATH_ISNAN_IS_INLINE
1607 inline vtkTypeBool vtkMath::IsNan(double x)
1608 {
1609 #if defined(VTK_HAS_STD_ISNAN)
1610  return std::isnan(x);
1611 #else
1612  return (isnan(x) != 0); // Force conversion to bool
1613 #endif
1614 }
1615 #endif
1616 
1617 //-----------------------------------------------------------------------------
1618 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
1619 #define VTK_MATH_ISFINITE_IS_INLINE
1620 inline bool vtkMath::IsFinite(double x)
1621 {
1622 #if defined(VTK_HAS_STD_ISFINITE)
1623  return std::isfinite(x);
1624 #elif defined(VTK_HAS_ISFINITE)
1625  return (isfinite(x) != 0); // Force conversion to bool
1626 #else
1627  return (finite(x) != 0); // Force conversion to bool
1628 #endif
1629 }
1630 #endif
1631 
1632 #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:1321
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:1068
vtkMath::MultiplyScalar
static void MultiplyScalar(double a[3], double s)
Multiplies a 3-vector by a scalar (double version).
Definition: vtkMath.h:382
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:990
vtkMath::Floor
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
Definition: vtkMath.h:1312
vtkMath::Dot
static double Dot(const double a[3], const double b[3])
Dot product of two 3-vectors (double version).
Definition: vtkMath.h:413
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:30
vtkMath::NearestPowerOfTwo
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1298
vtkMath::Normalize
static float Normalize(float v[3])
Normalize (in place) a 3-vector.
Definition: vtkMath.h:1342
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:641
vtkX3D::scale
Definition: vtkX3D.h:235
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:985
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:226
vtkMath::UninitializeBounds
static void UninitializeBounds(double bounds[6])
Set the bounds to an uninitialized state.
Definition: vtkMath.h:1090
vtk_detail
Definition: vtkMath.h:78
vtkMath::Dot2D
static float Dot2D(const float x[2], const float y[2])
Dot product of two 2-vectors.
Definition: vtkMath.h:570
vtkMath::DegreesFromRadians
static float DegreesFromRadians(float radians)
Convert radians into degrees.
Definition: vtkMath.h:1279
vtkMath::Max
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition: vtkMath.h:1336
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:615
vtkMath::HSVToRGB
static void HSVToRGB(const double hsv[3], double rgb[3])
Definition: vtkMath.h:1010
vtkMath::RadiansFromDegrees
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
Definition: vtkMath.h:1267
vtkX3D::range
Definition: vtkX3D.h:244
vtkMath::XYZToRGB
static void XYZToRGB(const double xyz[3], double rgb[3])
Convert color from the CIE XYZ system to RGB.
Definition: vtkMath.h:1043
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:1466
vtkMath::MultiplyScalar
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
Definition: vtkMath.h:358
vtkX3D::center
Definition: vtkX3D.h:236
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:335
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:49
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:1079
vtkMath::Norm
static float Norm(const float v[3])
Compute the norm of 3-vector (float version).
Definition: vtkMath.h:469
vtk_detail::RoundDoubleToIntegralIfNecessary
void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
Definition: vtkMath.h:1569
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:1516
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:1005
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:346
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:1021
vtkMath::MultiplyScalar2D
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
Definition: vtkMath.h:370
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:324
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:435
vtkMath::~vtkMath
~vtkMath() override
Definition: vtkMath.h:1257
detail::isnan
bool isnan(T x)
Definition: vtkGenericDataArrayLookupHelper.h:49
vtkX3D::position
Definition: vtkX3D.h:267
vtkX3D::point
Definition: vtkX3D.h:242
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:1479
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:126
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:609
vtkMath::Pi
static double Pi()
A mathematical constant.
Definition: vtkMath.h:95
vtkMath::MultiplyScalar2D
static void MultiplyScalar2D(double a[2], double s)
Multiplies a 2-vector by a scalar (double version).
Definition: vtkMath.h:394
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:1329
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:1459
vtkX3D::size
Definition: vtkX3D.h:259
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:594
vtkMath::Determinant2x2
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
Definition: vtkMath.h:632
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:421
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:642
vtkMath
performs common math operations
Definition: vtkMath.h:85
vtkMath::IsPowerOfTwo
static bool IsPowerOfTwo(vtkTypeUInt64 x)
Returns true if integer is a power of two.
Definition: vtkMath.h:1291
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:1435
vtkMath::Round
static int Round(double f)
Definition: vtkMath.h:118
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:1032
vtkMath::RGBToXYZ
static void RGBToXYZ(const double rgb[3], double xyz[3])
Convert color from the RGB system to CIE XYZ.
Definition: vtkMath.h:1054
vtkMath::Round
static int Round(float f)
Rounds a float to the nearest integer.
Definition: vtkMath.h:117
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:1420
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:575
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:580
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:313
vtkMath::Norm
static double Norm(const double v[3])
Compute the norm of 3-vector (double version).
Definition: vtkMath.h:474
vtkMath::Dot
static float Dot(const float a[3], const float b[3])
Dot product of two 3-vectors (float version).
Definition: vtkMath.h:405
vtkX3D::index
Definition: vtkX3D.h:252
vtkMath::AreBoundsInitialized
static vtkTypeBool AreBoundsInitialized(const double bounds[6])
Are the bounds initialized?
Definition: vtkMath.h:1105
vtkMath::Internal
static vtkSmartPointer< vtkMathInternal > Internal
Definition: vtkMath.h:1259
vtkMath::vtkMath
vtkMath()
Definition: vtkMath.h:1256
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
h