46 #include "vtkCommonCoreModule.h"
52 # define VTK_DBL_MIN 2.2250738585072014e-308
54 # define VTK_DBL_MIN DBL_MIN
58 # define VTK_DBL_EPSILON 2.2204460492503131e-16
60 # define VTK_DBL_EPSILON DBL_EPSILON
62 #include "vtkMathConfigure.h"
64 #ifndef VTK_DBL_EPSILON
66 # define VTK_DBL_EPSILON 2.2204460492503131e-16
68 # define VTK_DBL_EPSILON DBL_EPSILON
69 # endif // DBL_EPSILON
70 #endif // VTK_DBL_EPSILON
74 class vtkMathInternal;
86 static float Pi() {
return 3.14159265358979f; };
94 static double DoublePi() {
return 3.1415926535897932384626; };
98 static float RadiansFromDegrees(
float degrees);
99 static double RadiansFromDegrees(
double degrees);
104 static float DegreesFromRadians(
float radians);
105 static double DegreesFromRadians(
double radians);
111 return static_cast<int>( f + ( f >= 0 ? 0.5 : -0.5 ) ); }
113 return static_cast<int>( f + ( f >= 0 ? 0.5 : -0.5 ) ); }
118 static int Floor(
double x);
122 static int Ceil(
double x);
126 static vtkTypeInt64 Factorial(
int N );
131 static vtkTypeInt64 Binomial(
int m,
int n );
139 static int* BeginCombination(
int m,
int n );
147 static int NextCombination(
int m,
int n,
int* combination );
150 static void FreeCombination(
int* combination);
163 static void RandomSeed(
int s);
173 static int GetSeed();
184 static double Random();
194 static double Random(
double min,
double max );
204 static double Gaussian();
215 static double Gaussian(
double mean,
double std );
219 static void Add(
const float a[3],
const float b[3],
float c[3]) {
220 for (
int i = 0; i < 3; ++i)
227 static void Add(
const double a[3],
const double b[3],
double c[3]) {
228 for (
int i = 0; i < 3; ++i)
236 static void Subtract(
const float a[3],
const float b[3],
float c[3]) {
237 for (
int i = 0; i < 3; ++i)
245 static void Subtract(
const double a[3],
const double b[3],
double c[3]) {
246 for (
int i = 0; i < 3; ++i)
255 for (
int i = 0; i < 3; ++i)
264 for (
int i = 0; i < 2; ++i)
273 for (
int i = 0; i < 3; ++i)
282 for (
int i = 0; i < 2; ++i)
289 static float Dot(
const float x[3],
const float y[3]) {
290 return ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] );};
295 static double Dot(
const double x[3],
const double y[3]) {
296 return ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] );};
301 static void Outer(
const float x[3],
const float y[3],
float A[3][3]) {
302 for (
int i=0; i < 3; i++)
303 for (
int j=0; j < 3; j++)
304 A[i][j] = x[i] * y[j];
309 static void Outer(
const double x[3],
const double y[3],
double A[3][3]) {
310 for (
int i=0; i < 3; i++)
311 for (
int j=0; j < 3; j++)
312 A[i][j] = x[i] * y[j];
317 static void Cross(
const float x[3],
const float y[3],
float z[3]);
321 static void Cross(
const double x[3],
const double y[3],
double z[3]);
325 static float Norm(
const float* x,
int n);
326 static double Norm(
const double* x,
int n);
331 static float Norm(
const float x[3]) {
332 return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] ) );};
337 static double Norm(
const double x[3]) {
338 return sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] );};
342 static float Normalize(
float x[3]);
346 static double Normalize(
double x[3]);
354 static void Perpendiculars(
const double x[3],
double y[3],
double z[3],
356 static void Perpendiculars(
const float x[3],
float y[3],
float z[3],
364 static bool ProjectVector(
const float a[3],
const float b[3],
float projection[3]);
365 static bool ProjectVector(
const double a[3],
const double b[3],
double projection[3]);
373 static bool ProjectVector2D(
const float a[2],
const float b[2],
float projection[2]);
374 static bool ProjectVector2D(
const double a[2],
const double b[2],
double projection[2]);
378 static float Distance2BetweenPoints(
const float x[3],
const float y[3]);
382 static double Distance2BetweenPoints(
const double x[3],
const double y[3]);
387 static double GaussianAmplitude(
const double variance,
const double distanceFromMean);
392 static double GaussianAmplitude(
const double mean,
const double variance,
const double position);
398 static double GaussianWeight(
const double variance,
const double distanceFromMean);
404 static double GaussianWeight(
const double mean,
const double variance,
const double position);
408 static float Dot2D(
const float x[2],
const float y[2]) {
409 return ( x[0] * y[0] + x[1] * y[1] );};
414 static double Dot2D(
const double x[2],
const double y[2]) {
415 return ( x[0] * y[0] + x[1] * y[1] );};
420 static void Outer2D(
const float x[2],
const float y[2],
float A[2][2])
422 for (
int i=0; i < 2; i++)
424 for (
int j=0; j < 2; j++)
426 A[i][j] = x[i] * y[j];
433 static void Outer2D(
const double x[2],
const double y[2],
double A[2][2])
435 for (
int i=0; i < 2; i++)
437 for (
int j=0; j < 2; j++)
439 A[i][j] = x[i] * y[j];
448 return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
453 static double Norm2D(
const double x[2]) {
454 return sqrt( x[0] * x[0] + x[1] * x[1] );};
458 static float Normalize2D(
float x[2]);
462 static double Normalize2D(
double x[2]);
467 return (c1[0] * c2[1] - c2[0] * c1[1] );};
473 return (a * d - b * c);};
475 return (c1[0] * c2[1] - c2[0] * c1[1] );};
480 static void LUFactor3x3(
float A[3][3],
int index[3]);
481 static void LUFactor3x3(
double A[3][3],
int index[3]);
486 static void LUSolve3x3(
const float A[3][3],
const int index[3],
488 static void LUSolve3x3(
const double A[3][3],
const int index[3],
495 static void LinearSolve3x3(
const float A[3][3],
const float x[3],
497 static void LinearSolve3x3(
const double A[3][3],
const double x[3],
503 static void Multiply3x3(
const float A[3][3],
const float in[3],
505 static void Multiply3x3(
const double A[3][3],
const double in[3],
511 static void Multiply3x3(
const float A[3][3],
const float B[3][3],
513 static void Multiply3x3(
const double A[3][3],
const double B[3][3],
520 static void MultiplyMatrix(
const double **A,
const double **B,
521 unsigned int rowA,
unsigned int colA,
522 unsigned int rowB,
unsigned int colB,
529 static void Transpose3x3(
const float A[3][3],
float AT[3][3]);
530 static void Transpose3x3(
const double A[3][3],
double AT[3][3]);
536 static void Invert3x3(
const float A[3][3],
float AI[3][3]);
537 static void Invert3x3(
const double A[3][3],
double AI[3][3]);
542 static void Identity3x3(
float A[3][3]);
543 static void Identity3x3(
double A[3][3]);
548 static double Determinant3x3(
float A[3][3]);
549 static double Determinant3x3(
double A[3][3]);
554 static float Determinant3x3(
const float c1[3],
561 static double Determinant3x3(
const double c1[3],
569 static double Determinant3x3(
double a1,
double a2,
double a3,
570 double b1,
double b2,
double b3,
571 double c1,
double c2,
double c3);
577 static void QuaternionToMatrix3x3(
const float quat[4],
float A[3][3]);
578 static void QuaternionToMatrix3x3(
const double quat[4],
double A[3][3]);
585 static void Matrix3x3ToQuaternion(
const float A[3][3],
float quat[4]);
586 static void Matrix3x3ToQuaternion(
const double A[3][3],
double quat[4]);
591 static void MultiplyQuaternion(
const float q1[4],
const float q2[4],
float q[4] );
592 static void MultiplyQuaternion(
const double q1[4],
const double q2[4],
double q[4] );
599 static void Orthogonalize3x3(
const float A[3][3],
float B[3][3]);
600 static void Orthogonalize3x3(
const double A[3][3],
double B[3][3]);
608 static void Diagonalize3x3(
const float A[3][3],
float w[3],
float V[3][3]);
609 static void Diagonalize3x3(
const double A[3][3],
double w[3],
double V[3][3]);
620 static void SingularValueDecomposition3x3(
const float A[3][3],
621 float U[3][3],
float w[3],
623 static void SingularValueDecomposition3x3(
const double A[3][3],
624 double U[3][3],
double w[3],
632 static int SolveLinearSystem(
double **A,
double *x,
int size);
637 static int InvertMatrix(
double **A,
double **AI,
int size);
642 static int InvertMatrix(
double **A,
double **AI,
int size,
643 int *tmp1Size,
double *tmp2Size);
651 static int LUFactorLinearSystem(
double **A,
int *
index,
int size);
656 static int LUFactorLinearSystem(
double **A,
int *
index,
int size,
667 static void LUSolveLinearSystem(
double **A,
int *
index,
668 double *x,
int size);
678 static double EstimateMatrixCondition(
double **A,
int size);
685 static int Jacobi(
float **a,
float *w,
float **v);
686 static int Jacobi(
double **a,
double *w,
double **v);
695 static int JacobiN(
float **a,
int n,
float *w,
float **v);
696 static int JacobiN(
double **a,
int n,
double *w,
double **v);
710 static int SolveHomogeneousLeastSquares(
int numberOfSamples,
double **xt,
int xOrder,
727 static int SolveLeastSquares(
int numberOfSamples,
double **xt,
int xOrder,
728 double **yt,
int yOrder,
double **mt,
int checkHomogeneous=1);
736 static void RGBToHSV(
const float rgb[3],
float hsv[3])
737 {
RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
738 static void RGBToHSV(
float r,
float g,
float b,
float *h,
float *s,
float *v);
739 static double* RGBToHSV(
const double rgb[3]);
740 static double* RGBToHSV(
double r,
double g,
double b);
741 static void RGBToHSV(
const double rgb[3],
double hsv[3])
742 {
RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
743 static void RGBToHSV(
double r,
double g,
double b,
double *h,
double *s,
double *v);
749 static void HSVToRGB(
const float hsv[3],
float rgb[3])
750 {
HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
751 static void HSVToRGB(
float h,
float s,
float v,
float *r,
float *g,
float *b);
752 static double* HSVToRGB(
const double hsv[3]);
753 static double* HSVToRGB(
double h,
double s,
double v);
754 static void HSVToRGB(
const double hsv[3],
double rgb[3])
755 {
HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
756 static void HSVToRGB(
double h,
double s,
double v,
double *r,
double *g,
double *b);
761 static void LabToXYZ(
const double lab[3],
double xyz[3]) {
762 LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
764 static void LabToXYZ(
double L,
double a,
double b,
765 double *x,
double *y,
double *z);
766 static double *LabToXYZ(
const double lab[3]);
771 static void XYZToLab(
const double xyz[3],
double lab[3]) {
772 XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
774 static void XYZToLab(
double x,
double y,
double z,
775 double *L,
double *a,
double *b);
776 static double *XYZToLab(
const double xyz[3]);
781 static void XYZToRGB(
const double xyz[3],
double rgb[3]) {
782 XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
784 static void XYZToRGB(
double x,
double y,
double z,
785 double *r,
double *g,
double *b);
786 static double *XYZToRGB(
const double xyz[3]);
791 static void RGBToXYZ(
const double rgb[3],
double xyz[3]) {
792 RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
794 static void RGBToXYZ(
double r,
double g,
double b,
795 double *x,
double *y,
double *z);
796 static double *RGBToXYZ(
const double rgb[3]);
803 static void RGBToLab(
const double rgb[3],
double lab[3]) {
804 RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
806 static void RGBToLab(
double red,
double green,
double blue,
807 double *L,
double *a,
double *b);
808 static double *RGBToLab(
const double rgb[3]);
813 static void LabToRGB(
const double lab[3],
double rgb[3]) {
814 LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
816 static void LabToRGB(
double L,
double a,
double b,
817 double *red,
double *green,
double *blue);
818 static double *LabToRGB(
const double lab[3]);
836 if ( bounds[1]-bounds[0]<0.0 )
847 static void ClampValue(
double *
value,
const double range[2]);
848 static void ClampValue(
double value,
const double range[2],
double *clamped_value);
849 static void ClampValues(
850 double *values,
int nb_values,
const double range[2]);
851 static void ClampValues(
852 const double *values,
int nb_values,
const double range[2],
double *clamped_values);
859 static double ClampAndNormalizeValue(
double value,
860 const double range[2]);
870 static int GetScalarTypeFittingRange(
871 double range_min,
double range_max,
872 double scale = 1.0,
double shift = 0.0);
882 static int GetAdjustedScalarRange(
888 static int ExtentIsWithinOtherExtent(
int extent1[6],
int extent2[6]);
893 static int BoundsIsWithinOtherBounds(
double bounds1[6],
double bounds2[6],
double delta[3]);
898 static int PointIsWithinBounds(
double point[3],
double bounds[6],
double delta[3]);
907 static double Solve3PointCircle(
const double p1[3],
const double p2[3],
const double p3[3],
double center[3]);
913 static double NegInf();
920 static int IsInf(
double x);
924 static int IsNan(
double x);
930 static vtkMathInternal Internal;
933 void operator=(
const vtkMath&);
939 return x * 0.017453292f;
945 return x * 0.017453292519943295;
951 return x * 57.2957795131f;
957 return x * 57.29577951308232;
977 const int r =
static_cast<int>(x);
978 const int n = ( x !=
static_cast<double>(r) );
979 const int g = ( x < 0 );
980 return r - ( n & g );
989 const int r =
static_cast<int>(x);
990 const int n = ( x !=
static_cast<double>(r) );
991 const int g = ( x >= 0 );
992 return r + ( n & g );
1001 for (
int i=0; i < 3; i++)
1015 for (
int i=0; i < 3; i++)
1029 for (
int i=0; i < 2; i++)
1043 for (
int i=0; i < 2; i++)
1056 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1057 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1065 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1066 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1071 double b1,
double b2,
double b3,
1072 double c1,
double c2,
double c3)
1083 return ( ( x[0] - y[0] ) * ( x[0] - y[0] )
1084 + ( x[1] - y[1] ) * ( x[1] - y[1] )
1085 + ( x[2] - y[2] ) * ( x[2] - y[2] ) );
1092 return ( ( x[0] - y[0] ) * ( x[0] - y[0] )
1093 + ( x[1] - y[1] ) * ( x[1] - y[1] )
1094 + ( x[2] - y[2] ) * ( x[2] - y[2] ) );
1101 float Zx = x[1] * y[2] - x[2] * y[1];
1102 float Zy = x[2] * y[0] - x[0] * y[2];
1103 float Zz = x[0] * y[1] - x[1] * y[0];
1104 z[0] = Zx; z[1] = Zy; z[2] = Zz;
1111 double Zx = x[1] * y[2] - x[2] * y[1];
1112 double Zy = x[2] * y[0] - x[0] * y[2];
1113 double Zz = x[0] * y[1] - x[1] * y[0];
1114 z[0] = Zx; z[1] = Zy; z[2] = Zz;
1122 return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1123 A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1124 A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1145 if (*value < range[0])
1149 else if (*value > range[1])
1158 double value,
const double range[2],
double *clamped_value)
1160 if (range && clamped_value)
1162 if (value < range[0])
1164 *clamped_value = range[0];
1166 else if (value > range[1])
1168 *clamped_value = range[1];
1172 *clamped_value =
value;
1179 const double range[2])
1181 assert(
"pre: valid_range" && range[0]<=range[1]);
1184 if(range[0]==range[1])
1208 result=( result - range[0] ) / ( range[1] - range[0] );
1211 assert(
"post: valid_result" && result>=0.0 && result<=1.0);
1216 #if defined(VTK_HAS_ISINF)
1220 return (isinf(x) ? 1 : 0);
1224 #if defined(VTK_HAS_ISNAN)
1228 return (isnan(x) ? 1 : 0);