47 #ifndef VTK_LEGACY_REMOVE
54 # define VTK_DBL_MIN 2.2250738585072014e-308
56 # define VTK_DBL_MIN DBL_MIN
60 # define VTK_DBL_EPSILON 2.2204460492503131e-16
62 # define VTK_DBL_EPSILON DBL_EPSILON
64 #include "vtkMathConfigure.h"
68 #ifndef VTK_DBL_EPSILON
70 # define VTK_DBL_EPSILON 2.2204460492503131e-16
72 # define VTK_DBL_EPSILON DBL_EPSILON
73 # endif // DBL_EPSILON
74 #endif // VTK_DBL_EPSILON
78 class vtkMathInternal;
90 static float Pi() {
return 3.14159265358979f; };
98 static double DoublePi() {
return 3.1415926535897932384626; };
102 static float RadiansFromDegrees(
float degrees);
103 static double RadiansFromDegrees(
double degrees);
108 static float DegreesFromRadians(
float radians);
109 static double DegreesFromRadians(
double radians);
115 return static_cast<int>( f + ( f >= 0 ? 0.5 : -0.5 ) ); }
117 return static_cast<int>( f + ( f >= 0 ? 0.5 : -0.5 ) ); }
122 static int Floor(
double x);
126 static int Ceil(
double x);
130 static vtkTypeInt64 Factorial(
int N );
135 static vtkTypeInt64 Binomial(
int m,
int n );
143 static int* BeginCombination(
int m,
int n );
151 static int NextCombination(
int m,
int n,
int* combination );
154 static void FreeCombination(
int* combination);
167 static void RandomSeed(
int s);
177 static int GetSeed();
188 static double Random();
198 static double Random(
double min,
double max );
208 static double Gaussian();
219 static double Gaussian(
double mean,
double std );
223 static void Add(
const float a[3],
const float b[3],
float c[3]) {
224 for (
int i = 0; i < 3; ++i)
231 static void Add(
const double a[3],
const double b[3],
double c[3]) {
232 for (
int i = 0; i < 3; ++i)
240 static void Subtract(
const float a[3],
const float b[3],
float c[3]) {
241 for (
int i = 0; i < 3; ++i)
249 static void Subtract(
const double a[3],
const double b[3],
double c[3]) {
250 for (
int i = 0; i < 3; ++i)
259 for (
int i = 0; i < 3; ++i)
268 for (
int i = 0; i < 2; ++i)
277 for (
int i = 0; i < 3; ++i)
286 for (
int i = 0; i < 2; ++i)
293 static float Dot(
const float x[3],
const float y[3]) {
294 return ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] );};
299 static double Dot(
const double x[3],
const double y[3]) {
300 return ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] );};
305 static void Outer(
const float x[3],
const float y[3],
float A[3][3]) {
306 for (
int i=0; i < 3; i++)
307 for (
int j=0; j < 3; j++)
308 A[i][j] = x[i] * y[j];
313 static void Outer(
const double x[3],
const double y[3],
double A[3][3]) {
314 for (
int i=0; i < 3; i++)
315 for (
int j=0; j < 3; j++)
316 A[i][j] = x[i] * y[j];
321 static void Cross(
const float x[3],
const float y[3],
float z[3]);
325 static void Cross(
const double x[3],
const double y[3],
double z[3]);
329 static float Norm(
const float* x,
int n);
330 static double Norm(
const double* x,
int n);
335 static float Norm(
const float x[3]) {
336 return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] ) );};
341 static double Norm(
const double x[3]) {
342 return sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] );};
346 static float Normalize(
float x[3]);
350 static double Normalize(
double x[3]);
358 static void Perpendiculars(
const double x[3],
double y[3],
double z[3],
360 static void Perpendiculars(
const float x[3],
float y[3],
float z[3],
368 static bool ProjectVector(
const float a[3],
const float b[3],
float projection[3]);
369 static bool ProjectVector(
const double a[3],
const double b[3],
double projection[3]);
377 static bool ProjectVector2D(
const float a[2],
const float b[2],
float projection[2]);
378 static bool ProjectVector2D(
const double a[2],
const double b[2],
double projection[2]);
382 static float Distance2BetweenPoints(
const float x[3],
const float y[3]);
386 static double Distance2BetweenPoints(
const double x[3],
const double y[3]);
391 static double GaussianAmplitude(
const double variance,
const double distanceFromMean);
396 static double GaussianAmplitude(
const double mean,
const double variance,
const double position);
402 static double GaussianWeight(
const double variance,
const double distanceFromMean);
408 static double GaussianWeight(
const double mean,
const double variance,
const double position);
412 static float Dot2D(
const float x[2],
const float y[2]) {
413 return ( x[0] * y[0] + x[1] * y[1] );};
418 static double Dot2D(
const double x[2],
const double y[2]) {
419 return ( x[0] * y[0] + x[1] * y[1] );};
424 static void Outer2D(
const float x[2],
const float y[2],
float A[2][2])
426 for (
int i=0; i < 2; i++)
428 for (
int j=0; j < 2; j++)
430 A[i][j] = x[i] * y[j];
437 static void Outer2D(
const double x[2],
const double y[2],
double A[2][2])
439 for (
int i=0; i < 2; i++)
441 for (
int j=0; j < 2; j++)
443 A[i][j] = x[i] * y[j];
452 return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
457 static double Norm2D(
const double x[2]) {
458 return sqrt( x[0] * x[0] + x[1] * x[1] );};
462 static float Normalize2D(
float x[2]);
466 static double Normalize2D(
double x[2]);
471 return (c1[0] * c2[1] - c2[0] * c1[1] );};
477 return (a * d - b * c);};
479 return (c1[0] * c2[1] - c2[0] * c1[1] );};
484 static void LUFactor3x3(
float A[3][3],
int index[3]);
485 static void LUFactor3x3(
double A[3][3],
int index[3]);
490 static void LUSolve3x3(
const float A[3][3],
const int index[3],
492 static void LUSolve3x3(
const double A[3][3],
const int index[3],
499 static void LinearSolve3x3(
const float A[3][3],
const float x[3],
501 static void LinearSolve3x3(
const double A[3][3],
const double x[3],
507 static void Multiply3x3(
const float A[3][3],
const float in[3],
509 static void Multiply3x3(
const double A[3][3],
const double in[3],
515 static void Multiply3x3(
const float A[3][3],
const float B[3][3],
517 static void Multiply3x3(
const double A[3][3],
const double B[3][3],
524 static void MultiplyMatrix(
const double **A,
const double **B,
525 unsigned int rowA,
unsigned int colA,
526 unsigned int rowB,
unsigned int colB,
533 static void Transpose3x3(
const float A[3][3],
float AT[3][3]);
534 static void Transpose3x3(
const double A[3][3],
double AT[3][3]);
540 static void Invert3x3(
const float A[3][3],
float AI[3][3]);
541 static void Invert3x3(
const double A[3][3],
double AI[3][3]);
546 static void Identity3x3(
float A[3][3]);
547 static void Identity3x3(
double A[3][3]);
552 static double Determinant3x3(
float A[3][3]);
553 static double Determinant3x3(
double A[3][3]);
558 static float Determinant3x3(
const float c1[3],
565 static double Determinant3x3(
const double c1[3],
573 static double Determinant3x3(
double a1,
double a2,
double a3,
574 double b1,
double b2,
double b3,
575 double c1,
double c2,
double c3);
581 static void QuaternionToMatrix3x3(
const float quat[4],
float A[3][3]);
582 static void QuaternionToMatrix3x3(
const double quat[4],
double A[3][3]);
589 static void Matrix3x3ToQuaternion(
const float A[3][3],
float quat[4]);
590 static void Matrix3x3ToQuaternion(
const double A[3][3],
double quat[4]);
595 static void MultiplyQuaternion(
const float q1[4],
const float q2[4],
float q[4] );
596 static void MultiplyQuaternion(
const double q1[4],
const double q2[4],
double q[4] );
603 static void Orthogonalize3x3(
const float A[3][3],
float B[3][3]);
604 static void Orthogonalize3x3(
const double A[3][3],
double B[3][3]);
612 static void Diagonalize3x3(
const float A[3][3],
float w[3],
float V[3][3]);
613 static void Diagonalize3x3(
const double A[3][3],
double w[3],
double V[3][3]);
624 static void SingularValueDecomposition3x3(
const float A[3][3],
625 float U[3][3],
float w[3],
627 static void SingularValueDecomposition3x3(
const double A[3][3],
628 double U[3][3],
double w[3],
636 static int SolveLinearSystem(
double **A,
double *x,
int size);
641 static int InvertMatrix(
double **A,
double **AI,
int size);
646 static int InvertMatrix(
double **A,
double **AI,
int size,
647 int *tmp1Size,
double *tmp2Size);
655 static int LUFactorLinearSystem(
double **A,
int *
index,
int size);
660 static int LUFactorLinearSystem(
double **A,
int *
index,
int size,
671 static void LUSolveLinearSystem(
double **A,
int *
index,
672 double *x,
int size);
682 static double EstimateMatrixCondition(
double **A,
int size);
689 static int Jacobi(
float **a,
float *w,
float **v);
690 static int Jacobi(
double **a,
double *w,
double **v);
699 static int JacobiN(
float **a,
int n,
float *w,
float **v);
700 static int JacobiN(
double **a,
int n,
double *w,
double **v);
710 VTK_LEGACY(
static double* SolveCubic(
double c0,
double c1,
double c2,
double c3));
719 VTK_LEGACY(
static double* SolveQuadratic(
double c0,
double c1,
double c2));
726 VTK_LEGACY(
static double* SolveLinear(
double c0,
double c1));
742 VTK_LEGACY(
static int SolveCubic(
double c0,
double c1,
double c2,
double c3,
743 double *r1,
double *r2,
double *r3,
int *num_roots));
752 VTK_LEGACY(
static int SolveQuadratic(
double c0,
double c1,
double c2,
753 double *r1,
double *r2,
int *num_roots));
762 VTK_LEGACY(
static int SolveQuadratic(
double* c,
double* r,
int* m ));
769 VTK_LEGACY(
static int SolveLinear(
double c0,
double c1,
double *r1,
int *num_roots));
782 static int SolveHomogeneousLeastSquares(
int numberOfSamples,
double **xt,
int xOrder,
799 static int SolveLeastSquares(
int numberOfSamples,
double **xt,
int xOrder,
800 double **yt,
int yOrder,
double **mt,
int checkHomogeneous=1);
808 static void RGBToHSV(
const float rgb[3],
float hsv[3])
809 {
RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
810 static void RGBToHSV(
float r,
float g,
float b,
float *h,
float *s,
float *v);
811 static double* RGBToHSV(
const double rgb[3]);
812 static double* RGBToHSV(
double r,
double g,
double b);
813 static void RGBToHSV(
const double rgb[3],
double hsv[3])
814 {
RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
815 static void RGBToHSV(
double r,
double g,
double b,
double *h,
double *s,
double *v);
821 static void HSVToRGB(
const float hsv[3],
float rgb[3])
822 {
HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
823 static void HSVToRGB(
float h,
float s,
float v,
float *r,
float *g,
float *b);
824 static double* HSVToRGB(
const double hsv[3]);
825 static double* HSVToRGB(
double h,
double s,
double v);
826 static void HSVToRGB(
const double hsv[3],
double rgb[3])
827 {
HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
828 static void HSVToRGB(
double h,
double s,
double v,
double *r,
double *g,
double *b);
833 static void LabToXYZ(
const double lab[3],
double xyz[3]) {
834 LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
836 static void LabToXYZ(
double L,
double a,
double b,
837 double *x,
double *y,
double *z);
838 static double *LabToXYZ(
const double lab[3]);
843 static void XYZToLab(
const double xyz[3],
double lab[3]) {
844 XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
846 static void XYZToLab(
double x,
double y,
double z,
847 double *L,
double *a,
double *b);
848 static double *XYZToLab(
const double xyz[3]);
853 static void XYZToRGB(
const double xyz[3],
double rgb[3]) {
854 XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
856 static void XYZToRGB(
double x,
double y,
double z,
857 double *r,
double *g,
double *b);
858 static double *XYZToRGB(
const double xyz[3]);
863 static void RGBToXYZ(
const double rgb[3],
double xyz[3]) {
864 RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
866 static void RGBToXYZ(
double r,
double g,
double b,
867 double *x,
double *y,
double *z);
868 static double *RGBToXYZ(
const double rgb[3]);
875 static void RGBToLab(
const double rgb[3],
double lab[3]) {
876 RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
878 static void RGBToLab(
double red,
double green,
double blue,
879 double *L,
double *a,
double *b);
880 static double *RGBToLab(
const double rgb[3]);
885 static void LabToRGB(
const double lab[3],
double rgb[3]) {
886 LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
888 static void LabToRGB(
double L,
double a,
double b,
889 double *red,
double *green,
double *blue);
890 static double *LabToRGB(
const double lab[3]);
908 if ( bounds[1]-bounds[0]<0.0 )
919 static void ClampValue(
double *
value,
const double range[2]);
920 static void ClampValue(
double value,
const double range[2],
double *clamped_value);
921 static void ClampValues(
922 double *values,
int nb_values,
const double range[2]);
923 static void ClampValues(
924 const double *values,
int nb_values,
const double range[2],
double *clamped_values);
931 static double ClampAndNormalizeValue(
double value,
932 const double range[2]);
942 static int GetScalarTypeFittingRange(
943 double range_min,
double range_max,
944 double scale = 1.0,
double shift = 0.0);
954 static int GetAdjustedScalarRange(
960 static int ExtentIsWithinOtherExtent(
int extent1[6],
int extent2[6]);
965 static int BoundsIsWithinOtherBounds(
double bounds1[6],
double bounds2[6],
double delta[3]);
970 static int PointIsWithinBounds(
double point[3],
double bounds[6],
double delta[3]);
979 static double Solve3PointCircle(
const double p1[3],
const double p2[3],
const double p3[3],
double center[3]);
985 static double NegInf();
992 static int IsInf(
double x);
996 static int IsNan(
double x);
1002 static vtkMathInternal Internal;
1005 void operator=(
const vtkMath&);
1011 return x * 0.017453292f;
1017 return x * 0.017453292519943295;
1023 return x * 57.2957795131f;
1029 return x * 57.29577951308232;
1049 const int r =
static_cast<int>(x);
1050 const int n = ( x !=
static_cast<double>(r) );
1051 const int g = ( x < 0 );
1052 return r - ( n & g );
1061 const int r =
static_cast<int>(x);
1062 const int n = ( x !=
static_cast<double>(r) );
1063 const int g = ( x >= 0 );
1064 return r + ( n & g );
1073 for (
int i=0; i < 3; i++)
1087 for (
int i=0; i < 3; i++)
1101 for (
int i=0; i < 2; i++)
1115 for (
int i=0; i < 2; i++)
1128 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1129 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1137 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1138 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1143 double b1,
double b2,
double b3,
1144 double c1,
double c2,
double c3)
1155 return ( ( x[0] - y[0] ) * ( x[0] - y[0] )
1156 + ( x[1] - y[1] ) * ( x[1] - y[1] )
1157 + ( x[2] - y[2] ) * ( x[2] - y[2] ) );
1164 return ( ( x[0] - y[0] ) * ( x[0] - y[0] )
1165 + ( x[1] - y[1] ) * ( x[1] - y[1] )
1166 + ( x[2] - y[2] ) * ( x[2] - y[2] ) );
1173 float Zx = x[1] * y[2] - x[2] * y[1];
1174 float Zy = x[2] * y[0] - x[0] * y[2];
1175 float Zz = x[0] * y[1] - x[1] * y[0];
1176 z[0] = Zx; z[1] = Zy; z[2] = Zz;
1183 double Zx = x[1] * y[2] - x[2] * y[1];
1184 double Zy = x[2] * y[0] - x[0] * y[2];
1185 double Zz = x[0] * y[1] - x[1] * y[0];
1186 z[0] = Zx; z[1] = Zy; z[2] = Zz;
1194 return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1195 A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1196 A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1212 #ifndef VTK_LEGACY_REMOVE
1239 double *r1,
double *r2,
double *r3,
int *num_roots)
1248 double *r1,
double *r2,
int *num_roots)
1277 if (*value < range[0])
1281 else if (*value > range[1])
1290 double value,
const double range[2],
double *clamped_value)
1292 if (range && clamped_value)
1294 if (value < range[0])
1296 *clamped_value = range[0];
1298 else if (value > range[1])
1300 *clamped_value = range[1];
1304 *clamped_value =
value;
1311 const double range[2])
1313 assert(
"pre: valid_range" && range[0]<=range[1]);
1316 if(range[0]==range[1])
1340 result=( result - range[0] ) / ( range[1] - range[0] );
1343 assert(
"post: valid_result" && result>=0.0 && result<=1.0);
1348 #if defined(VTK_HAS_ISINF)
1349 #ifndef __WRAP__ // Fixes bug 13443 for VTK 5.10
1353 return (isinf(x) ? 1 : 0);
1358 #if defined(VTK_HAS_ISNAN)
1359 #ifndef __WRAP__ // Fixes bug 13443 for VTK 5.10
1363 return (isnan(x) ? 1 : 0);