47 #ifndef VTK_LEGACY_REMOVE
54 # define VTK_DBL_EPSILON 2.2204460492503131e-16
56 # define VTK_DBL_EPSILON DBL_EPSILON
61 class vtkMathInternal;
73 static float Pi() {
return 3.14159265358979f; };
81 static double DoublePi() {
return 3.1415926535897932384626; };
85 static float RadiansFromDegrees(
float degrees);
86 static double RadiansFromDegrees(
double degrees);
91 static float DegreesFromRadians(
float radians);
92 static double DegreesFromRadians(
double radians);
98 return static_cast<int>( f + ( f >= 0 ? 0.5 : -0.5 ) ); }
100 return static_cast<int>( f + ( f >= 0 ? 0.5 : -0.5 ) ); }
105 static int Floor(
double x);
109 static int Ceil(
double x);
113 static vtkTypeInt64 Factorial(
int N );
118 static vtkTypeInt64 Binomial(
int m,
int n );
126 static int* BeginCombination(
int m,
int n );
134 static int NextCombination(
int m,
int n,
int* combination );
137 static void FreeCombination(
int* combination);
150 static void RandomSeed(
int s);
160 static int GetSeed();
171 static double Random();
181 static double Random(
double min,
double max );
191 static double Gaussian();
202 static double Gaussian(
double mean,
double std );
206 static void Add(
const float a[3],
const float b[3],
float c[3]) {
207 for (
int i = 0; i < 3; ++i)
214 static void Add(
const double a[3],
const double b[3],
double c[3]) {
215 for (
int i = 0; i < 3; ++i)
223 static void Subtract(
const float a[3],
const float b[3],
float c[3]) {
224 for (
int i = 0; i < 3; ++i)
232 static void Subtract(
const double a[3],
const double b[3],
double c[3]) {
233 for (
int i = 0; i < 3; ++i)
242 for (
int i = 0; i < 3; ++i)
251 for (
int i = 0; i < 2; ++i)
260 for (
int i = 0; i < 3; ++i)
269 for (
int i = 0; i < 2; ++i)
276 static float Dot(
const float x[3],
const float y[3]) {
277 return ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] );};
282 static double Dot(
const double x[3],
const double y[3]) {
283 return ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] );};
288 static void Outer(
const float x[3],
const float y[3],
float A[3][3]) {
289 for (
int i=0; i < 3; i++)
290 for (
int j=0; j < 3; j++)
291 A[i][j] = x[i] * y[j];
295 static void Outer(
const double x[3],
const double y[3],
double A[3][3]) {
296 for (
int i=0; i < 3; i++)
297 for (
int j=0; j < 3; j++)
298 A[i][j] = x[i] * y[j];
303 static void Cross(
const float x[3],
const float y[3],
float z[3]);
307 static void Cross(
const double x[3],
const double y[3],
double z[3]);
311 static float Norm(
const float* x,
int n);
312 static double Norm(
const double* x,
int n);
317 static float Norm(
const float x[3]) {
318 return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] ) );};
323 static double Norm(
const double x[3]) {
324 return sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] );};
328 static float Normalize(
float x[3]);
332 static double Normalize(
double x[3]);
340 static void Perpendiculars(
const double x[3],
double y[3],
double z[3],
342 static void Perpendiculars(
const float x[3],
float y[3],
float z[3],
350 static bool ProjectVector(
const float a[3],
const float b[3],
float projection[3]);
351 static bool ProjectVector(
const double a[3],
const double b[3],
double projection[3]);
359 static bool ProjectVector2D(
const float a[2],
const float b[2],
float projection[2]);
360 static bool ProjectVector2D(
const double a[2],
const double b[2],
double projection[2]);
364 static float Distance2BetweenPoints(
const float x[3],
const float y[3]);
368 static double Distance2BetweenPoints(
const double x[3],
const double y[3]);
373 static double GaussianAmplitude(
const double variance,
const double distanceFromMean);
378 static double GaussianAmplitude(
const double mean,
const double variance,
const double position);
384 static double GaussianWeight(
const double variance,
const double distanceFromMean);
390 static double GaussianWeight(
const double mean,
const double variance,
const double position);
394 static float Dot2D(
const float x[2],
const float y[2]) {
395 return ( x[0] * y[0] + x[1] * y[1] );};
400 static double Dot2D(
const double x[2],
const double y[2]) {
401 return ( x[0] * y[0] + x[1] * y[1] );};
406 static void Outer2D(
const float x[2],
const float y[2],
float A[2][2])
408 for (
int i=0; i < 2; i++)
410 for (
int j=0; j < 2; j++)
412 A[i][j] = x[i] * y[j];
418 static void Outer2D(
const double x[2],
const double y[2],
double A[2][2])
420 for (
int i=0; i < 2; i++)
422 for (
int j=0; j < 2; j++)
424 A[i][j] = x[i] * y[j];
433 return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
438 static double Norm2D(
const double x[2]) {
439 return sqrt( x[0] * x[0] + x[1] * x[1] );};
443 static float Normalize2D(
float x[2]);
447 static double Normalize2D(
double x[2]);
452 return (c1[0] * c2[1] - c2[0] * c1[1] );};
458 return (a * d - b * c);};
460 return (c1[0] * c2[1] - c2[0] * c1[1] );};
465 static void LUFactor3x3(
float A[3][3],
int index[3]);
466 static void LUFactor3x3(
double A[3][3],
int index[3]);
471 static void LUSolve3x3(
const float A[3][3],
const int index[3],
473 static void LUSolve3x3(
const double A[3][3],
const int index[3],
480 static void LinearSolve3x3(
const float A[3][3],
const float x[3],
482 static void LinearSolve3x3(
const double A[3][3],
const double x[3],
488 static void Multiply3x3(
const float A[3][3],
const float in[3],
490 static void Multiply3x3(
const double A[3][3],
const double in[3],
496 static void Multiply3x3(
const float A[3][3],
const float B[3][3],
498 static void Multiply3x3(
const double A[3][3],
const double B[3][3],
505 static void MultiplyMatrix(
const double **A,
const double **B,
506 unsigned int rowA,
unsigned int colA,
507 unsigned int rowB,
unsigned int colB,
514 static void Transpose3x3(
const float A[3][3],
float AT[3][3]);
515 static void Transpose3x3(
const double A[3][3],
double AT[3][3]);
521 static void Invert3x3(
const float A[3][3],
float AI[3][3]);
522 static void Invert3x3(
const double A[3][3],
double AI[3][3]);
527 static void Identity3x3(
float A[3][3]);
528 static void Identity3x3(
double A[3][3]);
533 static double Determinant3x3(
float A[3][3]);
534 static double Determinant3x3(
double A[3][3]);
539 static float Determinant3x3(
const float c1[3],
546 static double Determinant3x3(
const double c1[3],
554 static double Determinant3x3(
double a1,
double a2,
double a3,
555 double b1,
double b2,
double b3,
556 double c1,
double c2,
double c3);
562 static void QuaternionToMatrix3x3(
const float quat[4],
float A[3][3]);
563 static void QuaternionToMatrix3x3(
const double quat[4],
double A[3][3]);
570 static void Matrix3x3ToQuaternion(
const float A[3][3],
float quat[4]);
571 static void Matrix3x3ToQuaternion(
const double A[3][3],
double quat[4]);
578 static void Orthogonalize3x3(
const float A[3][3],
float B[3][3]);
579 static void Orthogonalize3x3(
const double A[3][3],
double B[3][3]);
587 static void Diagonalize3x3(
const float A[3][3],
float w[3],
float V[3][3]);
588 static void Diagonalize3x3(
const double A[3][3],
double w[3],
double V[3][3]);
599 static void SingularValueDecomposition3x3(
const float A[3][3],
600 float U[3][3],
float w[3],
602 static void SingularValueDecomposition3x3(
const double A[3][3],
603 double U[3][3],
double w[3],
611 static int SolveLinearSystem(
double **A,
double *x,
int size);
616 static int InvertMatrix(
double **A,
double **AI,
int size);
621 static int InvertMatrix(
double **A,
double **AI,
int size,
622 int *tmp1Size,
double *tmp2Size);
630 static int LUFactorLinearSystem(
double **A,
int *index,
int size);
635 static int LUFactorLinearSystem(
double **A,
int *index,
int size,
646 static void LUSolveLinearSystem(
double **A,
int *index,
647 double *x,
int size);
657 static double EstimateMatrixCondition(
double **A,
int size);
664 static int Jacobi(
float **a,
float *w,
float **v);
665 static int Jacobi(
double **a,
double *w,
double **v);
674 static int JacobiN(
float **a,
int n,
float *w,
float **v);
675 static int JacobiN(
double **a,
int n,
double *w,
double **v);
685 VTK_LEGACY(
static double* SolveCubic(
double c0,
double c1,
double c2,
double c3));
694 VTK_LEGACY(
static double* SolveQuadratic(
double c0,
double c1,
double c2));
701 VTK_LEGACY(
static double* SolveLinear(
double c0,
double c1));
717 VTK_LEGACY(
static int SolveCubic(
double c0,
double c1,
double c2,
double c3,
718 double *r1,
double *r2,
double *r3,
int *num_roots));
727 VTK_LEGACY(
static int SolveQuadratic(
double c0,
double c1,
double c2,
728 double *r1,
double *r2,
int *num_roots));
737 VTK_LEGACY(
static int SolveQuadratic(
double* c,
double* r,
int* m ));
744 VTK_LEGACY(
static int SolveLinear(
double c0,
double c1,
double *r1,
int *num_roots));
757 static int SolveHomogeneousLeastSquares(
int numberOfSamples,
double **xt,
int xOrder,
774 static int SolveLeastSquares(
int numberOfSamples,
double **xt,
int xOrder,
775 double **yt,
int yOrder,
double **mt,
int checkHomogeneous=1);
781 static void RGBToHSV(
const float rgb[3],
float hsv[3])
782 { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
783 static void RGBToHSV(
float r,
float g,
float b,
float *h,
float *s,
float *v);
784 static double* RGBToHSV(
const double rgb[3]);
785 static double* RGBToHSV(
double r,
double g,
double b);
786 static void RGBToHSV(
const double rgb[3],
double hsv[3])
787 { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
788 static void RGBToHSV(
double r,
double g,
double b,
double *h,
double *s,
double *v);
794 static void HSVToRGB(
const float hsv[3],
float rgb[3])
795 { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
796 static void HSVToRGB(
float h,
float s,
float v,
float *r,
float *g,
float *b);
797 static double* HSVToRGB(
const double hsv[3]);
798 static double* HSVToRGB(
double h,
double s,
double v);
799 static void HSVToRGB(
const double hsv[3],
double rgb[3])
800 { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
801 static void HSVToRGB(
double h,
double s,
double v,
double *r,
double *g,
double *b);
806 static void LabToXYZ(
const double lab[3],
double xyz[3]) {
807 LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
809 static void LabToXYZ(
double L,
double a,
double b,
810 double *x,
double *y,
double *z);
811 static double *LabToXYZ(
const double lab[3]);
816 static void XYZToLab(
const double xyz[3],
double lab[3]) {
817 XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
819 static void XYZToLab(
double x,
double y,
double z,
820 double *L,
double *a,
double *b);
821 static double *XYZToLab(
const double xyz[3]);
826 static void XYZToRGB(
const double xyz[3],
double rgb[3]) {
827 XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
829 static void XYZToRGB(
double x,
double y,
double z,
830 double *r,
double *g,
double *b);
831 static double *XYZToRGB(
const double xyz[3]);
836 static void RGBToXYZ(
const double rgb[3],
double xyz[3]) {
837 RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
839 static void RGBToXYZ(
double r,
double g,
double b,
840 double *x,
double *y,
double *z);
841 static double *RGBToXYZ(
const double rgb[3]);
846 static void RGBToLab(
const double rgb[3],
double lab[3]) {
847 RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
849 static void RGBToLab(
double red,
double green,
double blue,
850 double *L,
double *a,
double *b);
851 static double *RGBToLab(
const double rgb[3]);
856 static void LabToRGB(
const double lab[3],
double rgb[3]) {
857 LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
859 static void LabToRGB(
double L,
double a,
double b,
860 double *red,
double *green,
double *blue);
861 static double *LabToRGB(
const double lab[3]);
879 if ( bounds[1]-bounds[0]<0.0 )
890 static void ClampValue(
double *value,
const double range[2]);
891 static void ClampValue(
double value,
const double range[2],
double *clamped_value);
892 static void ClampValues(
893 double *values,
int nb_values,
const double range[2]);
894 static void ClampValues(
895 const double *values,
int nb_values,
const double range[2],
double *clamped_values);
902 static double ClampAndNormalizeValue(
double value,
903 const double range[2]);
913 static int GetScalarTypeFittingRange(
914 double range_min,
double range_max,
915 double scale = 1.0,
double shift = 0.0);
925 static int GetAdjustedScalarRange(
931 static int ExtentIsWithinOtherExtent(
int extent1[6],
int extent2[6]);
936 static int BoundsIsWithinOtherBounds(
double bounds1[6],
double bounds2[6],
double delta[3]);
941 static int PointIsWithinBounds(
double point[3],
double bounds[6],
double delta[3]);
950 static double Solve3PointCircle(
const double p1[3],
const double p2[3],
const double p3[3],
double center[3]);
956 static double NegInf();
963 static int IsInf(
double x);
966 static int IsNan(
double x);
972 static vtkMathInternal Internal;
975 void operator=(
const vtkMath&);
981 return x * 0.017453292f;
987 return x * 0.017453292519943295;
993 return x * 57.2957795131f;
999 return x * 57.29577951308232;
1016 const int r =
static_cast<int>(x);
1017 const int n = ( x !=
static_cast<double>(r) );
1018 const int g = ( x < 0 );
1019 return r - ( n & g );
1025 const int r =
static_cast<int>(x);
1026 const int n = ( x !=
static_cast<double>(r) );
1027 const int g = ( x >= 0 );
1028 return r + ( n & g );
1037 for (
int i=0; i < 3; i++)
1051 for (
int i=0; i < 3; i++)
1065 for (
int i=0; i < 2; i++)
1079 for (
int i=0; i < 2; i++)
1092 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1093 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1101 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1102 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1107 double b1,
double b2,
double b3,
1108 double c1,
double c2,
double c3)
1119 return ( ( x[0] - y[0] ) * ( x[0] - y[0] )
1120 + ( x[1] - y[1] ) * ( x[1] - y[1] )
1121 + ( x[2] - y[2] ) * ( x[2] - y[2] ) );
1128 return ( ( x[0] - y[0] ) * ( x[0] - y[0] )
1129 + ( x[1] - y[1] ) * ( x[1] - y[1] )
1130 + ( x[2] - y[2] ) * ( x[2] - y[2] ) );
1137 float Zx = x[1] * y[2] - x[2] * y[1];
1138 float Zy = x[2] * y[0] - x[0] * y[2];
1139 float Zz = x[0] * y[1] - x[1] * y[0];
1140 z[0] = Zx; z[1] = Zy; z[2] = Zz;
1147 double Zx = x[1] * y[2] - x[2] * y[1];
1148 double Zy = x[2] * y[0] - x[0] * y[2];
1149 double Zz = x[0] * y[1] - x[1] * y[0];
1150 z[0] = Zx; z[1] = Zy; z[2] = Zz;
1158 return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1159 A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1160 A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1176 #ifndef VTK_LEGACY_REMOVE
1203 double *r1,
double *r2,
double *r3,
int *num_roots)
1212 double *r1,
double *r2,
int *num_roots)
1241 if (*value < range[0])
1245 else if (*value > range[1])
1254 double value,
const double range[2],
double *clamped_value)
1256 if (range && clamped_value)
1258 if (value < range[0])
1260 *clamped_value = range[0];
1262 else if (value > range[1])
1264 *clamped_value = range[1];
1268 *clamped_value = value;
1275 const double range[2])
1277 assert(
"pre: valid_range" && range[0]<=range[1]);
1280 if(range[0]==range[1])
1304 result=( result - range[0] ) / ( range[1] - range[0] );
1307 assert(
"post: valid_result" && result>=0.0 && result<=1.0);
static void MultiplyScalar2D(float a[2], float s)
static float Dot2D(const float x[2], const float y[2])
static void Cross(const float x[3], const float y[3], float z[3])
abstract base class for most VTK objects
static void LabToXYZ(const double lab[3], double xyz[3])
static double Norm(const double x[3])
static void ClampValue(double *value, const double range[2])
static void Outer(const float x[3], const float y[3], float A[3][3])
static float Determinant2x2(const float c1[2], const float c2[2])
static vtkTypeInt64 Factorial(int N)
static double * SolveLinear(double c0, double c1)
static void RGBToHSV(const double rgb[3], double hsv[3])
static int Round(float f)
static void RGBToHSV(const float rgb[3], float hsv[3])
static int AreBoundsInitialized(double bounds[6])
static double ClampAndNormalizeValue(double value, const double range[2])
static double * SolveQuadratic(double c0, double c1, double c2)
static void Outer(const double x[3], const double y[3], double A[3][3])
static void Add(const double a[3], const double b[3], double c[3])
static void XYZToRGB(const double xyz[3], double rgb[3])
static float Norm2D(const float x[2])
static float Normalize2D(float x[2])
static void UninitializeBounds(double bounds[6])
virtual void PrintSelf(ostream &os, vtkIndent indent)
static void RGBToXYZ(const double rgb[3], double xyz[3])
a simple class to control print indentation
static float Dot(const float x[3], const float y[3])
static void Subtract(const float a[3], const float b[3], float c[3])
static void Subtract(const double a[3], const double b[3], double c[3])
static int Floor(double x)
abstract superclass for arrays of numeric data
static double DoubleTwoPi()
static double Determinant2x2(double a, double b, double c, double d)
static float RadiansFromDegrees(float degrees)
static void HSVToRGB(const double hsv[3], double rgb[3])
Park and Miller Sequence of pseudo random numbers.
static void MultiplyScalar(double a[3], double s)
static double Determinant3x3(float A[3][3])
static float Normalize(float x[3])
static void RGBToLab(const double rgb[3], double lab[3])
static float DegreesFromRadians(float radians)
static void Outer2D(const double x[2], const double y[2], double A[2][2])
static void Outer2D(const float x[2], const float y[2], float A[2][2])
static int Ceil(double x)
performs common math operations
static double Dot2D(const double x[2], const double y[2])
static double Dot(const double x[3], const double y[3])
static double * SolveQuadratic(double c0, double c1, double c2)
static void MultiplyScalar(float a[3], float s)
static void HSVToRGB(const float hsv[3], float rgb[3])
static double * SolveCubic(double c0, double c1, double c2, double c3)
static double Norm2D(const double x[2])
static int Round(double f)
static void MultiplyScalar2D(double a[2], double s)
static double * SolveCubic(double c0, double c1, double c2, double c3)
static void LabToRGB(const double lab[3], double rgb[3])
double vtkDeterminant3x3(T A[3][3])
static float Norm(const float *x, int n)
static float Distance2BetweenPoints(const float x[3], const float y[3])
Gaussian sequence of pseudo random numbers implemented with the Box-Mueller transform.
static double * SolveLinear(double c0, double c1)
static double Determinant2x2(const double c1[2], const double c2[2])
static float Norm(const float x[3])
static void Add(const float a[3], const float b[3], float c[3])
represent and manipulate 3D points
static void XYZToLab(const double xyz[3], double lab[3])