27 #include "vtkCommonCoreModule.h"
28 #include "vtkMathPrivate.hxx"
34 #include "vtkMathConfigure.h"
38 #include <type_traits>
41 #define VTK_DBL_MIN 2.2250738585072014e-308
43 #define VTK_DBL_MIN DBL_MIN
47 #define VTK_DBL_EPSILON 2.2204460492503131e-16
49 #define VTK_DBL_EPSILON DBL_EPSILON
52 #ifndef VTK_DBL_EPSILON
54 #define VTK_DBL_EPSILON 2.2204460492503131e-16
56 #define VTK_DBL_EPSILON DBL_EPSILON
58 #endif // VTK_DBL_EPSILON
60 VTK_ABI_NAMESPACE_BEGIN
63 class vtkMathInternal;
70 VTK_ABI_NAMESPACE_BEGIN
72 template <
typename OutT>
77 VTK_ABI_NAMESPACE_BEGIN
86 template <
class VectorT,
class =
void>
87 struct VectorImplementsSize : std::false_type
91 template <
class VectorT>
92 struct VectorImplementsSize<VectorT, decltype((void)
std::declval<VectorT>().
size(), void())>
100 template <
class VectorT>
101 using EnableIfVectorImplementsSize =
118 static constexpr
double Pi() {
return 3.141592653589793; }
124 static float RadiansFromDegrees(
float degrees);
125 static double RadiansFromDegrees(
double degrees);
132 static float DegreesFromRadians(
float radians);
133 static double DegreesFromRadians(
double radians);
140 static int Round(
float f) {
return static_cast<int>(f + (f >= 0.0 ? 0.5 : -0.5)); }
141 static int Round(
double f) {
return static_cast<int>(f + (f >= 0.0 ? 0.5 : -0.5)); }
148 template <
typename OutT>
161 static int Floor(
double x);
168 static int Ceil(
double x);
175 static int CeilLog2(vtkTypeUInt64 x);
182 static T Min(
const T& a,
const T& b);
189 static T Max(
const T& a,
const T& b);
194 static bool IsPowerOfTwo(vtkTypeUInt64 x);
201 static int NearestPowerOfTwo(
int x);
207 static vtkTypeInt64 Factorial(
int N);
214 static vtkTypeInt64 Binomial(
int m,
int n);
227 static int* BeginCombination(
int m,
int n);
239 static int NextCombination(
int m,
int n,
int* combination);
244 static void FreeCombination(
int* combination);
261 static void RandomSeed(
int s);
274 static int GetSeed();
289 static double Random();
303 static double Random(
double min,
double max);
317 static double Gaussian();
331 static double Gaussian(
double mean,
double std);
337 template <
class VectorT1,
class VectorT2>
338 static void Assign(
const VectorT1& a, VectorT2&& b)
348 static void Assign(
const double a[3],
double b[3]) { vtkMath::Assign<>(a, b); }
353 static void Add(
const float a[3],
const float b[3],
float c[3])
355 for (
int i = 0; i < 3; ++i)
364 static void Add(
const double a[3],
const double b[3],
double c[3])
366 for (
int i = 0; i < 3; ++i)
377 template <
class VectorT1,
class VectorT2,
class VectorT3>
378 static void Add(VectorT1&& a, VectorT2&& b, VectorT3& c)
380 for (
int i = 0; i < 3; ++i)
389 static void Subtract(
const float a[3],
const float b[3],
float c[3])
391 for (
int i = 0; i < 3; ++i)
400 static void Subtract(
const double a[3],
const double b[3],
double c[3])
402 for (
int i = 0; i < 3; ++i)
413 template <
class VectorT1,
class VectorT2,
class VectorT3>
414 static void Subtract(
const VectorT1& a,
const VectorT2& b, VectorT3&& c)
427 for (
int i = 0; i < 3; ++i)
439 for (
int i = 0; i < 2; ++i)
451 for (
int i = 0; i < 3; ++i)
463 for (
int i = 0; i < 2; ++i)
472 static float Dot(
const float a[3],
const float b[3])
474 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
480 static double Dot(
const double a[3],
const double b[3])
482 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
500 template <
typename ReturnTypeT = double,
typename TupleRangeT1,
typename TupleRangeT2,
503 TupleRangeT1, TupleRangeT2>::type::value_type>
504 static ReturnTypeT
Dot(
const TupleRangeT1& a,
const TupleRangeT2& b)
506 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
512 static void Outer(
const float a[3],
const float b[3],
float c[3][3])
514 for (
int i = 0; i < 3; ++i)
516 for (
int j = 0; j < 3; ++j)
518 c[i][j] = a[i] * b[j];
526 static void Outer(
const double a[3],
const double b[3],
double c[3][3])
528 for (
int i = 0; i < 3; ++i)
530 for (
int j = 0; j < 3; ++j)
532 c[i][j] = a[i] * b[j];
542 template <
class VectorT1,
class VectorT2,
class VectorT3>
543 static void Cross(VectorT1&& a, VectorT2&& b, VectorT3& c);
549 static void Cross(
const float a[3],
const float b[3],
float c[3]);
555 static void Cross(
const double a[3],
const double b[3],
double c[3]);
561 static float Norm(
const float* x,
int n);
562 static double Norm(
const double* x,
int n);
568 static float Norm(
const float v[3]) {
return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); }
573 static double Norm(
const double v[3])
575 return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
587 template <
typename ReturnTypeT =
double,
typename TupleRangeT>
590 return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
597 static inline float Normalize(
float v[3]);
603 static inline double Normalize(
double v[3]);
613 static void Perpendiculars(
const double v1[3],
double v2[3],
double v3[3],
double theta);
614 static void Perpendiculars(
const float v1[3],
float v2[3],
float v3[3],
double theta);
623 static bool ProjectVector(
const float a[3],
const float b[3],
float projection[3]);
624 static bool ProjectVector(
const double a[3],
const double b[3],
double projection[3]);
634 static bool ProjectVector2D(
const float a[2],
const float b[2],
float projection[2]);
635 static bool ProjectVector2D(
const double a[2],
const double b[2],
double projection[2]);
653 template <
typename ReturnTypeT = double,
typename TupleRangeT1,
typename TupleRangeT2,
656 TupleRangeT1, TupleRangeT2>::type::value_type>
657 static ReturnTypeT Distance2BetweenPoints(
const TupleRangeT1& p1,
const TupleRangeT2& p2);
663 static float Distance2BetweenPoints(
const float p1[3],
const float p2[3]);
669 static double Distance2BetweenPoints(
const double p1[3],
const double p2[3]);
680 static double AngleBetweenVectors(
const double v1[3],
const double v2[3]);
685 static double SignedAngleBetweenVectors(
686 const double v1[3],
const double v2[3],
const double vn[3]);
692 static double GaussianAmplitude(
double variance,
double distanceFromMean);
698 static double GaussianAmplitude(
double mean,
double variance,
double position);
705 static double GaussianWeight(
double variance,
double distanceFromMean);
712 static double GaussianWeight(
double mean,
double variance,
double position);
717 static float Dot2D(
const float x[2],
const float y[2]) {
return x[0] * y[0] + x[1] * y[1]; }
722 static double Dot2D(
const double x[2],
const double y[2]) {
return x[0] * y[0] + x[1] * y[1]; }
727 static void Outer2D(
const float x[2],
const float y[2],
float A[2][2])
729 for (
int i = 0; i < 2; ++i)
731 for (
int j = 0; j < 2; ++j)
733 A[i][j] = x[i] * y[j];
741 static void Outer2D(
const double x[2],
const double y[2],
double A[2][2])
743 for (
int i = 0; i < 2; ++i)
745 for (
int j = 0; j < 2; ++j)
747 A[i][j] = x[i] * y[j];
756 static float Norm2D(
const float x[2]) {
return std::sqrt(x[0] * x[0] + x[1] * x[1]); }
762 static double Norm2D(
const double x[2]) {
return std::sqrt(x[0] * x[0] + x[1] * x[1]); }
768 static float Normalize2D(
float v[2]);
774 static double Normalize2D(
double v[2]);
781 return c1[0] * c2[1] - c2[0] * c1[1];
788 static double Determinant2x2(
double a,
double b,
double c,
double d) {
return a * d - b * c; }
791 return c1[0] * c2[1] - c2[0] * c1[1];
799 static void LUFactor3x3(
float A[3][3],
int index[3]);
800 static void LUFactor3x3(
double A[3][3],
int index[3]);
807 static void LUSolve3x3(
const float A[3][3],
const int index[3],
float x[3]);
808 static void LUSolve3x3(
const double A[3][3],
const int index[3],
double x[3]);
816 static void LinearSolve3x3(
const float A[3][3],
const float x[3],
float y[3]);
817 static void LinearSolve3x3(
const double A[3][3],
const double x[3],
double y[3]);
824 static void Multiply3x3(
const float A[3][3],
const float v[3],
float u[3]);
825 static void Multiply3x3(
const double A[3][3],
const double v[3],
double u[3]);
832 static void Multiply3x3(
const float A[3][3],
const float B[3][3],
float C[3][3]);
833 static void Multiply3x3(
const double A[3][3],
const double B[3][3],
double C[3][3]);
859 template <
int RowsT,
int MidDimT,
int ColsT,
860 class LayoutT1 = vtkMatrixUtilities::Layout::Identity,
861 class LayoutT2 = vtkMatrixUtilities::Layout::Identity,
class MatrixT1,
class MatrixT2,
865 vtkMathPrivate::MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2>::Compute(
866 std::forward<MatrixT1>(M1), std::forward<MatrixT2>(M2), std::forward<MatrixT3>(M3));
889 template <
int RowsT,
int ColsT,
class LayoutT = vtkMatrixUtilities::Layout::Identity,
890 class MatrixT,
class VectorT1,
class VectorT2>
893 vtkMathPrivate::MultiplyMatrix<RowsT, ColsT, 1, LayoutT>::Compute(
894 std::forward<MatrixT>(M), std::forward<VectorT1>(X), std::forward<VectorT2>(Y));
902 template <
class ScalarT,
int SizeT,
class VectorT1,
class VectorT2,
904 static ScalarT
Dot(VectorT1&& x, VectorT2&& y)
906 return vtkMathPrivate::ContractRowWithCol<ScalarT, 1, SizeT, 1, 0, 0,
907 vtkMatrixUtilities::Layout::Identity,
908 vtkMatrixUtilities::Layout::Transpose>::Compute(std::forward<VectorT1>(x),
909 std::forward<VectorT2>(y));
918 template <
class ScalarT,
int SizeT,
class VectorT1,
class VectorT2,
920 class = EnableIfVectorImplementsSize<VectorT1>>
921 static ScalarT
Dot(VectorT1&& x, VectorT2&& y)
924 using SizeType = decltype(std::declval<VectorT1>().
size());
925 for (SizeType dim = 0; dim < x.size(); ++dim)
927 dot += x[dim] * y[dim];
939 template <
int SizeT,
class VectorT>
944 return vtkMath::Dot<Scalar, SizeT>(std::forward<VectorT>(x), std::forward<VectorT>(x));
963 template <
int SizeT,
class LayoutT = vtkMatrixUtilities::Layout::Identity,
class MatrixT>
967 return vtkMathPrivate::Determinant<SizeT, LayoutT>::Compute(std::forward<MatrixT>(M));
985 template <
int SizeT,
class LayoutT = vtkMatrixUtilities::Layout::Identity,
class MatrixT1,
989 vtkMathPrivate::InvertMatrix<SizeT, LayoutT>::Compute(
990 std::forward<MatrixT1>(M1), std::forward<MatrixT2>(M2));
1006 template <
int RowsT,
int ColsT,
class LayoutT = vtkMatrixUtilities::Layout::Identity,
1007 class MatrixT,
class VectorT1,
class VectorT2>
1010 vtkMathPrivate::LinearSolve<RowsT, ColsT, LayoutT>::Compute(
1011 std::forward<MatrixT>(M), std::forward<VectorT1>(x), std::forward<VectorT2>(y));
1028 template <
class ScalarT,
int SizeT,
class LayoutT = vtkMatrixUtilities::Layout::Identity,
1029 class VectorT1,
class MatrixT,
class VectorT2,
1031 static ScalarT
Dot(VectorT1&& x, MatrixT&& M, VectorT2&& y)
1034 vtkMathPrivate::MultiplyMatrix<SizeT, SizeT, 1, LayoutT>::Compute(
1035 std::forward<MatrixT>(M), std::forward<VectorT2>(y), tmp);
1036 return vtkMathPrivate::ContractRowWithCol<ScalarT, 1, SizeT, 1, 0, 0,
1037 vtkMatrixUtilities::Layout::Identity,
1038 vtkMatrixUtilities::Layout::Transpose>::Compute(std::forward<VectorT1>(x), tmp);
1046 static void MultiplyMatrix(
const double*
const* A,
const double*
const* B,
unsigned int rowA,
1047 unsigned int colA,
unsigned int rowB,
unsigned int colB,
double** C);
1054 static void Transpose3x3(
const float A[3][3],
float AT[3][3]);
1055 static void Transpose3x3(
const double A[3][3],
double AT[3][3]);
1063 static void Invert3x3(
const float A[3][3],
float AI[3][3]);
1064 static void Invert3x3(
const double A[3][3],
double AI[3][3]);
1071 static void Identity3x3(
float A[3][3]);
1072 static void Identity3x3(
double A[3][3]);
1079 static double Determinant3x3(
const float A[3][3]);
1080 static double Determinant3x3(
const double A[3][3]);
1086 static float Determinant3x3(
const float c1[3],
const float c2[3],
const float c3[3]);
1091 static double Determinant3x3(
const double c1[3],
const double c2[3],
const double c3[3]);
1099 static double Determinant3x3(
double a1,
double a2,
double a3,
double b1,
double b2,
double b3,
1100 double c1,
double c2,
double c3);
1110 static void QuaternionToMatrix3x3(
const float quat[4],
float A[3][3]);
1111 static void QuaternionToMatrix3x3(
const double quat[4],
double A[3][3]);
1112 template <
class QuaternionT,
class MatrixT,
1113 class EnableT =
typename std::enable_if<!vtkMatrixUtilities::MatrixIs2DArray<MatrixT>()>::
type>
1114 static void QuaternionToMatrix3x3(QuaternionT&& q, MatrixT&& A);
1127 static void Matrix3x3ToQuaternion(
const float A[3][3],
float quat[4]);
1128 static void Matrix3x3ToQuaternion(
const double A[3][3],
double quat[4]);
1129 template <
class MatrixT,
class QuaternionT,
1130 class EnableT =
typename std::enable_if<!vtkMatrixUtilities::MatrixIs2DArray<MatrixT>()>::
type>
1131 static void Matrix3x3ToQuaternion(MatrixT&& A, QuaternionT&& q);
1141 static void MultiplyQuaternion(
const float q1[4],
const float q2[4],
float q[4]);
1142 static void MultiplyQuaternion(
const double q1[4],
const double q2[4],
double q[4]);
1150 static void RotateVectorByNormalizedQuaternion(
const float v[3],
const float q[4],
float r[3]);
1151 static void RotateVectorByNormalizedQuaternion(
const double v[3],
const double q[4],
double r[3]);
1159 static void RotateVectorByWXYZ(
const float v[3],
const float q[4],
float r[3]);
1160 static void RotateVectorByWXYZ(
const double v[3],
const double q[4],
double r[3]);
1169 static void Orthogonalize3x3(
const float A[3][3],
float B[3][3]);
1170 static void Orthogonalize3x3(
const double A[3][3],
double B[3][3]);
1180 static void Diagonalize3x3(
const float A[3][3],
float w[3],
float V[3][3]);
1181 static void Diagonalize3x3(
const double A[3][3],
double w[3],
double V[3][3]);
1194 static void SingularValueDecomposition3x3(
1195 const float A[3][3],
float U[3][3],
float w[3],
float VT[3][3]);
1196 static void SingularValueDecomposition3x3(
1197 const double A[3][3],
double U[3][3],
double w[3],
double VT[3][3]);
1208 double a00,
double a01,
double a10,
double a11,
double b0,
double b1,
double& x0,
double& x1);
1218 static vtkTypeBool SolveLinearSystem(
double** A,
double* x,
int size);
1234 double** A,
double** AI,
int size,
int* tmp1Size,
double* tmp2Size);
1275 static void LUSolveLinearSystem(
double** A,
int*
index,
double* x,
int size);
1285 static double EstimateMatrixCondition(
const double*
const* A,
int size);
1296 static vtkTypeBool Jacobi(
float** a,
float* w,
float** v);
1297 static vtkTypeBool Jacobi(
double** a,
double* w,
double** v);
1310 static vtkTypeBool JacobiN(
float** a,
int n,
float* w,
float** v);
1311 static vtkTypeBool JacobiN(
double** a,
int n,
double* w,
double** v);
1328 int numberOfSamples,
double** xt,
int xOrder,
double**
mt);
1344 static vtkTypeBool SolveLeastSquares(
int numberOfSamples,
double** xt,
int xOrder,
double** yt,
1345 int yOrder,
double**
mt,
int checkHomogeneous = 1);
1357 RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
1359 static void RGBToHSV(
float r,
float g,
float b,
float* h,
float* s,
float* v);
1360 static void RGBToHSV(
const double rgb[3],
double hsv[3])
1362 RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv + 1, hsv + 2);
1364 static void RGBToHSV(
double r,
double g,
double b,
double* h,
double* s,
double* v);
1377 HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
1379 static void HSVToRGB(
float h,
float s,
float v,
float* r,
float* g,
float* b);
1380 static void HSVToRGB(
const double hsv[3],
double rgb[3])
1382 HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb + 1, rgb + 2);
1384 static void HSVToRGB(
double h,
double s,
double v,
double* r,
double* g,
double* b);
1394 ProLabToXYZ(prolab[0], prolab[1], prolab[2], xyz + 0, xyz + 1, xyz + 2);
1396 static void ProLabToXYZ(
double L,
double a,
double b,
double* x,
double* y,
double* z);
1406 XYZToProLab(xyz[0], xyz[1], xyz[2], prolab + 0, prolab + 1, prolab + 2);
1408 static void XYZToProLab(
double x,
double y,
double z,
double* L,
double* a,
double* b);
1415 static void LabToXYZ(
const double lab[3],
double xyz[3])
1417 LabToXYZ(lab[0], lab[1], lab[2], xyz + 0, xyz + 1, xyz + 2);
1419 static void LabToXYZ(
double L,
double a,
double b,
double* x,
double* y,
double* z);
1426 static void XYZToLab(
const double xyz[3],
double lab[3])
1428 XYZToLab(xyz[0], xyz[1], xyz[2], lab + 0, lab + 1, lab + 2);
1430 static void XYZToLab(
double x,
double y,
double z,
double* L,
double* a,
double* b);
1437 static void XYZToRGB(
const double xyz[3],
double rgb[3])
1439 XYZToRGB(xyz[0], xyz[1], xyz[2], rgb + 0, rgb + 1, rgb + 2);
1441 static void XYZToRGB(
double x,
double y,
double z,
double* r,
double* g,
double* b);
1448 static void RGBToXYZ(
const double rgb[3],
double xyz[3])
1450 RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz + 0, xyz + 1, xyz + 2);
1452 static void RGBToXYZ(
double r,
double g,
double b,
double* x,
double* y,
double* z);
1463 static void RGBToLab(
const double rgb[3],
double lab[3])
1465 RGBToLab(rgb[0], rgb[1], rgb[2], lab + 0, lab + 1, lab + 2);
1467 static void RGBToLab(
double red,
double green,
double blue,
double* L,
double* a,
double* b);
1476 ProLabToRGB(prolab[0], prolab[1], prolab[2], rgb + 0, rgb + 1, rgb + 2);
1478 static void ProLabToRGB(
double L,
double a,
double b,
double* red,
double* green,
double* blue);
1491 RGBToProLab(rgb[0], rgb[1], rgb[2], prolab + 0, prolab + 1, prolab + 2);
1493 static void RGBToProLab(
double red,
double green,
double blue,
double* L,
double* a,
double* b);
1500 static void LabToRGB(
const double lab[3],
double rgb[3])
1502 LabToRGB(lab[0], lab[1], lab[2], rgb + 0, rgb + 1, rgb + 2);
1504 static void LabToRGB(
double L,
double a,
double b,
double* red,
double* green,
double* blue);
1528 if (bounds[1] - bounds[0] < 0.0)
1541 static T ClampValue(
const T&
value,
const T& min,
const T&
max);
1548 static void ClampValue(
double*
value,
const double range[2]);
1549 static void ClampValue(
double value,
const double range[2],
double* clamped_value);
1550 static void ClampValues(
double* values,
int nb_values,
const double range[2]);
1551 static void ClampValues(
1552 const double* values,
int nb_values,
const double range[2],
double* clamped_values);
1561 static double ClampAndNormalizeValue(
double value,
const double range[2]);
1567 template <
class T1,
class T2>
1568 static void TensorFromSymmetricTensor(
const T1 symmTensor[6], T2 tensor[9]);
1576 static void TensorFromSymmetricTensor(T tensor[9]);
1586 static int GetScalarTypeFittingRange(
1587 double range_min,
double range_max,
double scale = 1.0,
double shift = 0.0);
1603 static vtkTypeBool ExtentIsWithinOtherExtent(
const int extent1[6],
const int extent2[6]);
1611 const double bounds1[6],
const double bounds2[6],
const double delta[3]);
1619 const double point[3],
const double bounds[6],
const double delta[3]);
1630 static int PlaneIntersectsAABB(
1631 const double bounds[6],
const double normal[3],
const double point[3]);
1642 static double Solve3PointCircle(
1643 const double p1[3],
const double p2[3],
const double p3[3],
double center[3]);
1648 static double Inf();
1653 static double NegInf();
1658 static double Nan();
1674 static bool IsFinite(
double x);
1680 static int QuadraticRoot(
double a,
double b,
double c,
double min,
double max,
double* u);
1721 template <
class Iter1,
class Iter2,
class Iter3>
1722 static void Convolve1D(Iter1 beginSample, Iter1 endSample, Iter2 beginKernel, Iter2 endKernel,
1725 int sampleSize = std::distance(beginSample, endSample);
1726 int kernelSize = std::distance(beginKernel, endKernel);
1727 int outSize = std::distance(beginOut, endOut);
1729 if (sampleSize <= 0 || kernelSize <= 0 || outSize <= 0)
1739 case ConvolutionMode::SAME:
1740 begin =
static_cast<int>(std::ceil((std::min)(sampleSize, kernelSize) / 2.0)) - 1;
1741 end = begin + (
std::max)(sampleSize, kernelSize);
1743 case ConvolutionMode::VALID:
1744 begin = (std::min)(sampleSize, kernelSize) - 1;
1745 end = begin + std::abs(sampleSize - kernelSize) + 1;
1747 case ConvolutionMode::FULL:
1752 for (
int i = begin; i < end; i++)
1754 Iter3 out = beginOut + i - begin;
1756 for (
int j = (
std::max)(i - sampleSize + 1, 0); j <= (std::min)(i, kernelSize - 1); j++)
1758 *out += *(beginSample + (i - j)) * *(beginKernel + j);
1769 double directionVector[3] = { p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2] };
1771 result[0] = p2[0] + (offset * directionVector[0]);
1772 result[1] = p2[1] + (offset * directionVector[1]);
1773 result[2] = p2[2] + (offset * directionVector[2]);
1778 ~
vtkMath()
override =
default;
1784 void operator=(
const vtkMath&) =
delete;
1790 return x * 0.017453292f;
1796 return x * 0.017453292519943295;
1802 return x * 57.2957795131f;
1808 return x * 57.29577951308232;
1814 return ((x != 0) & ((x & (x - 1)) == 0));
1821 unsigned int z =
static_cast<unsigned int>(((x > 0) ? x - 1 : 0));
1827 return static_cast<int>(z + 1);
1835 int i =
static_cast<int>(x);
1844 int i =
static_cast<int>(x);
1852 return (b <= a ? b : a);
1859 return (b > a ? b : a);
1868 for (
int i = 0; i < 3; ++i)
1882 for (
int i = 0; i < 3; ++i)
1896 for (
int i = 0; i < 2; ++i)
1910 for (
int i = 0; i < 2; ++i)
1921 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1922 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1928 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1929 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1934 double a1,
double a2,
double a3,
double b1,
double b2,
double b3,
double c1,
double c2,
double c3)
1943 return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1944 (p1[2] - p2[2]) * (p1[2] - p2[2]));
1950 return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1951 (p1[2] - p2[2]) * (p1[2] - p2[2]));
1955 template <
typename ReturnTypeT,
typename TupleRangeT1,
typename TupleRangeT2,
typename EnableT>
1958 return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) +
1959 (p1[2] - p2[2]) * (p1[2] - p2[2]));
1965 return ((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]));
1969 template <
class VectorT1,
class VectorT2,
class VectorT3>
1973 ValueType Cx = a[1] * b[2] - a[2] * b[1];
1974 ValueType Cy = a[2] * b[0] - a[0] * b[2];
1975 ValueType Cz = a[0] * b[1] - a[1] * b[0];
1985 float Cx = a[1] * b[2] - a[2] * b[1];
1986 float Cy = a[2] * b[0] - a[0] * b[2];
1987 float Cz = a[0] * b[1] - a[1] * b[0];
1997 double Cx = a[1] * b[2] - a[2] * b[1];
1998 double Cy = a[2] * b[0] - a[0] * b[2];
1999 double Cz = a[0] * b[1] - a[1] * b[0];
2009 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] -
2010 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];
2029 assert(
"pre: valid_range" && min <= max);
2030 return std::clamp(value, min, max);
2038 assert(
"pre: valid_range" && range[0] <= range[1]);
2047 if (range && clamped_value)
2049 assert(
"pre: valid_range" && range[0] <= range[1]);
2058 assert(
"pre: valid_range" && range[0] <= range[1]);
2061 if (range[0] == range[1])
2071 result = (result - range[0]) / (range[1] - range[0]);
2074 assert(
"post: valid_result" && result >= 0.0 && result <= 1.0);
2080 template <
class T1,
class T2>
2083 for (
int i = 0; i < 3; ++i)
2085 tensor[4 * i] = symmTensor[i];
2087 tensor[1] = tensor[3] = symmTensor[3];
2088 tensor[2] = tensor[6] = symmTensor[5];
2089 tensor[5] = tensor[7] = symmTensor[4];
2096 tensor[6] = tensor[5];
2097 tensor[7] = tensor[4];
2098 tensor[8] = tensor[2];
2099 tensor[4] = tensor[1];
2100 tensor[5] = tensor[7];
2101 tensor[2] = tensor[6];
2102 tensor[1] = tensor[3];
2104 VTK_ABI_NAMESPACE_END
2108 template <
class QuaternionT,
class MatrixT>
2109 inline void vtkQuaternionToMatrix3x3(QuaternionT&& quat, MatrixT&& A)
2113 Scalar
ww = quat[0] * quat[0];
2114 Scalar wx = quat[0] * quat[1];
2115 Scalar wy = quat[0] * quat[2];
2116 Scalar wz = quat[0] * quat[3];
2118 Scalar xx = quat[1] * quat[1];
2119 Scalar yy = quat[2] * quat[2];
2120 Scalar zz = quat[3] * quat[3];
2122 Scalar xy = quat[1] * quat[2];
2123 Scalar xz = quat[1] * quat[3];
2124 Scalar yz = quat[2] * quat[3];
2126 Scalar rr = xx + yy + zz;
2128 Scalar f = 1 / (ww + rr);
2129 Scalar s = (ww - rr) * f;
2135 Wrapper::template Get<0, 0>(Ar) = xx * f + s;
2136 Wrapper::template Get<1, 0>(Ar) = (xy + wz) * f;
2137 Wrapper::template Get<2, 0>(Ar) = (xz - wy) * f;
2139 Wrapper::template Get<0, 1>(Ar) = (xy - wz) * f;
2140 Wrapper::template Get<1, 1>(Ar) = yy * f + s;
2141 Wrapper::template Get<2, 1>(Ar) = (yz + wx) * f;
2143 Wrapper::template Get<0, 2>(Ar) = (xz + wy) * f;
2144 Wrapper::template Get<1, 2>(Ar) = (yz - wx) * f;
2145 Wrapper::template Get<2, 2>(Ar) = zz * f + s;
2149 VTK_ABI_NAMESPACE_BEGIN
2153 vtkQuaternionToMatrix3x3(quat, A);
2159 vtkQuaternionToMatrix3x3(quat, A);
2163 template <
class QuaternionT,
class MatrixT,
class EnableT>
2166 vtkQuaternionToMatrix3x3(std::forward<QuaternionT>(q), std::forward<MatrixT>(A));
2168 VTK_ABI_NAMESPACE_END
2177 template <
class MatrixT,
class QuaternionT>
2178 inline void vtkMatrix3x3ToQuaternion(MatrixT&& A, QuaternionT&& quat)
2189 N[0][0] = Wrapper::template Get<0, 0>(Ar) + Wrapper::template Get<1, 1>(Ar) +
2190 Wrapper::template Get<2, 2>(Ar);
2191 N[1][1] = Wrapper::template Get<0, 0>(Ar) - Wrapper::template Get<1, 1>(Ar) -
2192 Wrapper::template Get<2, 2>(Ar);
2193 N[2][2] = -Wrapper::template Get<0, 0>(Ar) + Wrapper::template Get<1, 1>(Ar) -
2194 Wrapper::template Get<2, 2>(Ar);
2195 N[3][3] = -Wrapper::template Get<0, 0>(Ar) - Wrapper::template Get<1, 1>(Ar) +
2196 Wrapper::template Get<2, 2>(Ar);
2199 N[0][1] = N[1][0] = Wrapper::template Get<2, 1>(Ar) - Wrapper::template Get<1, 2>(Ar);
2200 N[0][2] = N[2][0] = Wrapper::template Get<0, 2>(Ar) - Wrapper::template Get<2, 0>(Ar);
2201 N[0][3] = N[3][0] = Wrapper::template Get<1, 0>(Ar) - Wrapper::template Get<0, 1>(Ar);
2203 N[1][2] = N[2][1] = Wrapper::template Get<1, 0>(Ar) + Wrapper::template Get<0, 1>(Ar);
2204 N[1][3] = N[3][1] = Wrapper::template Get<0, 2>(Ar) + Wrapper::template Get<2, 0>(Ar);
2205 N[2][3] = N[3][2] = Wrapper::template Get<2, 1>(Ar) + Wrapper::template Get<1, 2>(Ar);
2207 Scalar eigenvectors[4][4], eigenvalues[4];
2211 Scalar *NTemp[4], *eigenvectorsTemp[4];
2212 for (
int i = 0; i < 4; ++i)
2215 eigenvectorsTemp[i] = eigenvectors[i];
2220 quat[0] = eigenvectors[0][0];
2221 quat[1] = eigenvectors[1][0];
2222 quat[2] = eigenvectors[2][0];
2223 quat[3] = eigenvectors[3][0];
2227 VTK_ABI_NAMESPACE_BEGIN
2231 vtkMatrix3x3ToQuaternion(A, quat);
2237 vtkMatrix3x3ToQuaternion(A, quat);
2241 template <
class MatrixT,
class QuaternionT,
class EnableT>
2244 vtkMatrix3x3ToQuaternion(std::forward<MatrixT>(A), std::forward<QuaternionT>(q));
2246 VTK_ABI_NAMESPACE_END
2250 VTK_ABI_NAMESPACE_BEGIN
2252 template <
typename OutT>
2260 *ret =
static_cast<OutT
>((val >= 0.0) ? (val + 0.5) : (val - 0.5));
2280 *retVal =
static_cast<float>(val);
2282 VTK_ABI_NAMESPACE_END
2285 VTK_ABI_NAMESPACE_BEGIN
2287 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
2288 #define VTK_MATH_ISINF_IS_INLINE
2291 #if defined(VTK_HAS_STD_ISINF)
2292 return std::isinf(x);
2294 return (isinf(x) != 0);
2300 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
2301 #define VTK_MATH_ISNAN_IS_INLINE
2304 #if defined(VTK_HAS_STD_ISNAN)
2307 return (
isnan(x) != 0);
2313 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
2314 #define VTK_MATH_ISFINITE_IS_INLINE
2317 #if defined(VTK_HAS_STD_ISFINITE)
2318 return std::isfinite(x);
2319 #elif defined(VTK_HAS_ISFINITE)
2320 return (isfinite(x) != 0);
2322 return (finite(x) != 0);
2327 VTK_ABI_NAMESPACE_END
static void MultiplyScalar2D(float a[2], float s)
Multiplies a 2-vector by a scalar (float version).
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.
static float Dot(const float a[3], const float b[3])
Dot product of two 3-vectors (float version).
static void ProLabToRGB(const double prolab[3], double rgb[3])
Convert color from the ProLab system to RGB.
static void TensorFromSymmetricTensor(const T1 symmTensor[6], T2 tensor[9])
Convert a 6-Component symmetric tensor into a 9-Component tensor, no allocation performed.
static void GetPointAlongLine(double result[3], double p1[3], double p2[3], const double offset)
Get the coordinates of a point along a line defined by p1 and p2, at a specified offset relative to p...
abstract base class for most VTK objects
static void LabToXYZ(const double lab[3], double xyz[3])
Convert color from the CIE-L*ab system to CIE XYZ.
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.
static void RGBToProLab(const double rgb[3], double prolab[3])
Convert color from the RGB system to Prolab The input RGB must be values in the range [0...
static void Outer(const double a[3], const double b[3], double c[3][3])
Outer product of two 3-vectors (double version).
void RoundDoubleToIntegralIfNecessary(double val, OutT *ret)
static void MultiplyMatrixWithVector(MatrixT &&M, VectorT1 &&X, VectorT2 &&Y)
Multiply matrix M with vector Y such that Y = M x X.
static float Determinant2x2(const float c1[2], const float c2[2])
Compute determinant of 2x2 matrix.
static vtkSmartPointer< vtkMathInternal > Internal
static vtkMatrixUtilities::ScalarTypeExtractor< MatrixT >::value_type Determinant(MatrixT &&M)
Computes the determinant of input square SizeT x SizeT matrix M.
static double Distance2BetweenPoints2D(const double p1[2], const double p2[2])
Compute distance squared between two 2D points p1 and p2.
static void QuaternionToMatrix3x3(const float quat[4], float A[3][3])
Convert a quaternion to a 3x3 rotation matrix.
static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4])
Convert a 3x3 matrix into a quaternion.
static vtkTypeBool IsInf(double x)
Test if a number is equal to the special floating point value infinity.
static void InvertMatrix(MatrixT1 &&M1, MatrixT2 &&M2)
Computes the inverse of input matrix M1 into M2.
static constexpr double Pi()
A mathematical constant.
static void RGBToHSV(const double rgb[3], double hsv[3])
Convert color in RGB format (Red, Green, Blue) to HSV format (Hue, Saturation, Value).
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.
static void RGBToHSV(const float rgb[3], float hsv[3])
Convert color in RGB format (Red, Green, Blue) to HSV format (Hue, Saturation, Value).
static double ClampAndNormalizeValue(double value, const double range[2])
Clamp a value against a range and then normalize it between 0 and 1.
static void ProLabToXYZ(const double prolab[3], double xyz[3])
Convert color from the ProLAB system to CIE XYZ.
static void MultiplyMatrix(MatrixT1 &&M1, MatrixT2 &&M2, MatrixT3 &&M3)
Multiply matrices such that M3 = M1 x M2.
static void Outer(const float a[3], const float b[3], float c[3][3])
Outer product of two 3-vectors (float version).
static ScalarT Dot(VectorT1 &&x, VectorT2 &&y)
Computes the dot product between 2 vectors x and y.
static void Add(const double a[3], const double b[3], double c[3])
Addition of two 3-vectors (double version).
static double Dot(const double a[3], const double b[3])
Dot product of two 3-vectors (double version).
static void XYZToRGB(const double xyz[3], double rgb[3])
Convert color from the CIE XYZ system to RGB.
static float Norm2D(const float x[2])
Compute the norm of a 2-vector.
static void UninitializeBounds(double bounds[6])
Set the bounds to an uninitialized state.
static constexpr int DYNAMIC_VECTOR_SIZE()
When this value is passed to a select templated functions in vtkMath, the computation can be performe...
double vtkDeterminant3x3(const T A[3][3])
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
static void RGBToXYZ(const double rgb[3], double xyz[3])
Convert color from the RGB system to CIE XYZ.
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
a simple class to control print indentation
static float Normalize2D(float v[2])
Normalize (in place) a 2-vector.
#define Distance2BetweenPoints2D(p1, p2)
static void Subtract(const float a[3], const float b[3], float c[3])
Subtraction of two 3-vectors (float version).
static void Subtract(const double a[3], const double b[3], double c[3])
Subtraction of two 3-vectors (double version).
static int Floor(double x)
Rounds a double to the nearest integer not greater than itself.
static double Determinant3x3(const float A[3][3])
Return the determinant of a 3x3 matrix.
abstract superclass for arrays of numeric data
static ReturnTypeT SquaredNorm(const TupleRangeT &v)
Compute the squared norm of a 3-vector.
static void Convolve1D(Iter1 beginSample, Iter1 endSample, Iter2 beginKernel, Iter2 endKernel, Iter3 beginOut, Iter3 endOut, ConvolutionMode mode=ConvolutionMode::FULL)
Compute the convolution of a sampled 1D signal by a given kernel.
static double Determinant2x2(double a, double b, double c, double d)
Calculate the determinant of a 2x2 matrix: | a b | | c d |.
static float RadiansFromDegrees(float degrees)
Convert degrees into radians.
static void HSVToRGB(const double hsv[3], double rgb[3])
Convert color in HSV format (Hue, Saturation, Value) to RGB format (Red, Green, Blue).
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).
static void RGBToLab(const double rgb[3], double lab[3])
Convert color from the RGB system to CIE-L*ab.
static float DegreesFromRadians(float radians)
Convert radians into degrees.
static float Normalize(float v[3])
Normalize (in place) a 3-vector.
static void Outer2D(const double x[2], const double y[2], double A[2][2])
Outer product of two 2-vectors (double version).
static void Outer2D(const float x[2], const float y[2], float A[2][2])
Outer product of two 2-vectors (float version).
static int Ceil(double x)
Rounds a double to the nearest integer not less than itself.
static void Cross(VectorT1 &&a, VectorT2 &&b, VectorT3 &c)
Cross product of two 3-vectors.
static ScalarT Dot(VectorT1 &&x, MatrixT &&M, VectorT2 &&y)
Computes the dot product x^T M y, where x and y are vectors and M is a metric matrix.
performs common math operations
static double Dot2D(const double x[2], const double y[2])
Dot product of two 2-vectors.
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...
static vtkTypeBool JacobiN(float **a, int n, float *w, float **v)
JacobiN iteration for the solution of eigenvectors/eigenvalues of a nxn real symmetric matrix...
static float Norm(const float v[3])
Compute the norm of 3-vector (float version).
static ScalarT Dot(VectorT1 &&x, VectorT2 &&y)
Computes the dot product between 2 vectors x and y.
static void MultiplyScalar(float a[3], float s)
Multiplies a 3-vector by a scalar (float version).
static void HSVToRGB(const float hsv[3], float rgb[3])
Convert color in HSV format (Hue, Saturation, Value) to RGB format (Red, Green, Blue).
static void Subtract(const VectorT1 &a, const VectorT2 &b, VectorT3 &&c)
Subtraction of two 3-vectors (templated version).
static T ClampValue(const T &value, const T &min, const T &max)
Clamp some value against a range, return the result.
static void LinearSolve(MatrixT &&M, VectorT1 &&x, VectorT2 &&y)
This method solves linear systems M * x = y.
static double Norm2D(const double x[2])
Compute the norm of a 2-vector.
static int Round(double f)
static void Add(VectorT1 &&a, VectorT2 &&b, VectorT3 &c)
Addition of two 3-vectors (double version).
static ReturnTypeT Dot(const TupleRangeT1 &a, const TupleRangeT2 &b)
Compute dot product between two points p1 and p2.
static double Norm(const double v[3])
Compute the norm of 3-vector (double version).
static void MultiplyScalar2D(double a[2], double s)
Multiplies a 2-vector by a scalar (double version).
static void LabToRGB(const double lab[3], double rgb[3])
Convert color from the CIE-L*ab system to RGB.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
static float Norm(const float *x, int n)
Compute the norm of n-vector.
static vtkIdType ComputeGCD(vtkIdType m, vtkIdType n)
Compute the greatest common divisor (GCD) of two positive integers m and n.
static ReturnTypeT Distance2BetweenPoints(const TupleRangeT1 &p1, const TupleRangeT2 &p2)
Compute distance squared between two points p1 and p2.
ConvolutionMode
Support the convolution operations.
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 |.
static void Add(const float a[3], const float b[3], float c[3])
Addition of two 3-vectors (float version).
Template defining traits of native types used by VTK.
static vtkTypeBool AreBoundsInitialized(const double bounds[6])
Are the bounds initialized?
represent and manipulate 3D points
static void Assign(const VectorT1 &a, VectorT2 &&b)
Assign values to a 3-vector (templated version).
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
static void XYZToProLab(const double xyz[3], double prolab[3])
Convert Color from the CIE XYZ system to ProLAB.
static vtkMatrixUtilities::ScalarTypeExtractor< VectorT >::value_type SquaredNorm(VectorT &&x)
Computes the dot product between 2 vectors x and y.
static void Assign(const double a[3], double b[3])
Assign values to a 3-vector (double version).
static void XYZToLab(const double xyz[3], double lab[3])
Convert Color from the CIE XYZ system to CIE-L*ab.