47 #include "vtkCommonCoreModule.h"
50 #include "vtkMathConfigure.h"
55 # define VTK_DBL_MIN 2.2250738585072014e-308
57 # define VTK_DBL_MIN DBL_MIN
61 # define VTK_DBL_EPSILON 2.2204460492503131e-16
63 # define VTK_DBL_EPSILON DBL_EPSILON
66 #ifndef VTK_DBL_EPSILON
68 # define VTK_DBL_EPSILON 2.2204460492503131e-16
70 # define VTK_DBL_EPSILON DBL_EPSILON
71 # endif // DBL_EPSILON
72 #endif // VTK_DBL_EPSILON
76 class vtkMathInternal;
88 static double Pi() {
return 3.141592653589793; };
91 VTK_LEGACY(
static double DoublePi());
94 VTK_LEGACY(
static double DoubleTwoPi());
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);
127 static int CeilLog2(vtkTypeUInt64 x);
130 static bool IsPowerOfTwo(vtkTypeUInt64 x);
135 static int NearestPowerOfTwo(
int x);
139 static vtkTypeInt64 Factorial(
int N );
144 static vtkTypeInt64 Binomial(
int m,
int n );
152 static int* BeginCombination(
int m,
int n );
160 static int NextCombination(
int m,
int n,
int* combination );
163 static void FreeCombination(
int* combination);
176 static void RandomSeed(
int s);
186 static int GetSeed();
197 static double Random();
207 static double Random(
double min,
double max );
217 static double Gaussian();
228 static double Gaussian(
double mean,
double std );
232 static void Add(
const float a[3],
const float b[3],
float c[3]) {
233 for (
int i = 0; i < 3; ++i)
240 static void Add(
const double a[3],
const double b[3],
double c[3]) {
241 for (
int i = 0; i < 3; ++i)
249 static void Subtract(
const float a[3],
const float b[3],
float c[3]) {
250 for (
int i = 0; i < 3; ++i)
258 static void Subtract(
const double a[3],
const double b[3],
double c[3]) {
259 for (
int i = 0; i < 3; ++i)
268 for (
int i = 0; i < 3; ++i)
277 for (
int i = 0; i < 2; ++i)
286 for (
int i = 0; i < 3; ++i)
295 for (
int i = 0; i < 2; ++i)
302 static float Dot(
const float x[3],
const float y[3]) {
303 return ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] );};
308 static double Dot(
const double x[3],
const double y[3]) {
309 return ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] );};
314 static void Outer(
const float x[3],
const float y[3],
float A[3][3]) {
315 for (
int i=0; i < 3; i++)
316 for (
int j=0; j < 3; j++)
317 A[i][j] = x[i] * y[j];
322 static void Outer(
const double x[3],
const double y[3],
double A[3][3]) {
323 for (
int i=0; i < 3; i++)
324 for (
int j=0; j < 3; j++)
325 A[i][j] = x[i] * y[j];
330 static void Cross(
const float x[3],
const float y[3],
float z[3]);
334 static void Cross(
const double x[3],
const double y[3],
double z[3]);
338 static float Norm(
const float* x,
int n);
339 static double Norm(
const double* x,
int n);
344 static float Norm(
const float x[3]) {
345 return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] ) );};
350 static double Norm(
const double x[3]) {
351 return sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] );};
355 static float Normalize(
float x[3]);
359 static double Normalize(
double x[3]);
367 static void Perpendiculars(
const double x[3],
double y[3],
double z[3],
369 static void Perpendiculars(
const float x[3],
float y[3],
float z[3],
377 static bool ProjectVector(
const float a[3],
const float b[3],
float projection[3]);
378 static bool ProjectVector(
const double a[3],
const double b[3],
double projection[3]);
386 static bool ProjectVector2D(
const float a[2],
const float b[2],
float projection[2]);
387 static bool ProjectVector2D(
const double a[2],
const double b[2],
double projection[2]);
391 static float Distance2BetweenPoints(
const float x[3],
const float y[3]);
395 static double Distance2BetweenPoints(
const double x[3],
const double y[3]);
400 static double GaussianAmplitude(
const double variance,
const double distanceFromMean);
405 static double GaussianAmplitude(
const double mean,
const double variance,
const double position);
411 static double GaussianWeight(
const double variance,
const double distanceFromMean);
417 static double GaussianWeight(
const double mean,
const double variance,
const double position);
421 static float Dot2D(
const float x[2],
const float y[2]) {
422 return ( x[0] * y[0] + x[1] * y[1] );};
427 static double Dot2D(
const double x[2],
const double y[2]) {
428 return ( x[0] * y[0] + x[1] * y[1] );};
433 static void Outer2D(
const float x[2],
const float y[2],
float 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];
446 static void Outer2D(
const double x[2],
const double y[2],
double A[2][2])
448 for (
int i=0; i < 2; i++)
450 for (
int j=0; j < 2; j++)
452 A[i][j] = x[i] * y[j];
461 return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
466 static double Norm2D(
const double x[2]) {
467 return sqrt( x[0] * x[0] + x[1] * x[1] );};
471 static float Normalize2D(
float x[2]);
475 static double Normalize2D(
double x[2]);
480 return (c1[0] * c2[1] - c2[0] * c1[1] );};
486 return (a * d - b * c);};
488 return (c1[0] * c2[1] - c2[0] * c1[1] );};
493 static void LUFactor3x3(
float A[3][3],
int index[3]);
494 static void LUFactor3x3(
double A[3][3],
int index[3]);
499 static void LUSolve3x3(
const float A[3][3],
const int index[3],
501 static void LUSolve3x3(
const double A[3][3],
const int index[3],
508 static void LinearSolve3x3(
const float A[3][3],
const float x[3],
510 static void LinearSolve3x3(
const double A[3][3],
const double x[3],
516 static void Multiply3x3(
const float A[3][3],
const float in[3],
518 static void Multiply3x3(
const double A[3][3],
const double in[3],
524 static void Multiply3x3(
const float A[3][3],
const float B[3][3],
526 static void Multiply3x3(
const double A[3][3],
const double B[3][3],
533 static void MultiplyMatrix(
const double **A,
const double **B,
534 unsigned int rowA,
unsigned int colA,
535 unsigned int rowB,
unsigned int colB,
542 static void Transpose3x3(
const float A[3][3],
float AT[3][3]);
543 static void Transpose3x3(
const double A[3][3],
double AT[3][3]);
549 static void Invert3x3(
const float A[3][3],
float AI[3][3]);
550 static void Invert3x3(
const double A[3][3],
double AI[3][3]);
555 static void Identity3x3(
float A[3][3]);
556 static void Identity3x3(
double A[3][3]);
561 static double Determinant3x3(
float A[3][3]);
562 static double Determinant3x3(
double A[3][3]);
567 static float Determinant3x3(
const float c1[3],
574 static double Determinant3x3(
const double c1[3],
582 static double Determinant3x3(
double a1,
double a2,
double a3,
583 double b1,
double b2,
double b3,
584 double c1,
double c2,
double c3);
592 static void QuaternionToMatrix3x3(
const float quat[4],
float A[3][3]);
593 static void QuaternionToMatrix3x3(
const double quat[4],
double A[3][3]);
602 static void Matrix3x3ToQuaternion(
const float A[3][3],
float quat[4]);
603 static void Matrix3x3ToQuaternion(
const double A[3][3],
double quat[4]);
610 static void MultiplyQuaternion(
const float q1[4],
const float q2[4],
float q[4] );
611 static void MultiplyQuaternion(
const double q1[4],
const double q2[4],
double q[4] );
618 static void Orthogonalize3x3(
const float A[3][3],
float B[3][3]);
619 static void Orthogonalize3x3(
const double A[3][3],
double B[3][3]);
627 static void Diagonalize3x3(
const float A[3][3],
float w[3],
float V[3][3]);
628 static void Diagonalize3x3(
const double A[3][3],
double w[3],
double V[3][3]);
639 static void SingularValueDecomposition3x3(
const float A[3][3],
640 float U[3][3],
float w[3],
642 static void SingularValueDecomposition3x3(
const double A[3][3],
643 double U[3][3],
double w[3],
651 static int SolveLinearSystem(
double **A,
double *x,
int size);
656 static int InvertMatrix(
double **A,
double **AI,
int size);
661 static int InvertMatrix(
double **A,
double **AI,
int size,
662 int *tmp1Size,
double *tmp2Size);
670 static int LUFactorLinearSystem(
double **A,
int *index,
int size);
675 static int LUFactorLinearSystem(
double **A,
int *index,
int size,
686 static void LUSolveLinearSystem(
double **A,
int *index,
687 double *x,
int size);
697 static double EstimateMatrixCondition(
double **A,
int size);
704 static int Jacobi(
float **a,
float *w,
float **v);
705 static int Jacobi(
double **a,
double *w,
double **v);
714 static int JacobiN(
float **a,
int n,
float *w,
float **v);
715 static int JacobiN(
double **a,
int n,
double *w,
double **v);
729 static int SolveHomogeneousLeastSquares(
int numberOfSamples,
double **xt,
int xOrder,
746 static int SolveLeastSquares(
int numberOfSamples,
double **xt,
int xOrder,
747 double **yt,
int yOrder,
double **mt,
int checkHomogeneous=1);
755 static void RGBToHSV(
const float rgb[3],
float hsv[3])
756 { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
757 static void RGBToHSV(
float r,
float g,
float b,
float *h,
float *s,
float *v);
758 static double* RGBToHSV(
const double rgb[3]);
759 static double* RGBToHSV(
double r,
double g,
double b);
760 static void RGBToHSV(
const double rgb[3],
double hsv[3])
761 { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
762 static void RGBToHSV(
double r,
double g,
double b,
double *h,
double *s,
double *v);
770 static void HSVToRGB(
const float hsv[3],
float rgb[3])
771 { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
772 static void HSVToRGB(
float h,
float s,
float v,
float *r,
float *g,
float *b);
773 static double* HSVToRGB(
const double hsv[3]);
774 static double* HSVToRGB(
double h,
double s,
double v);
775 static void HSVToRGB(
const double hsv[3],
double rgb[3])
776 { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
777 static void HSVToRGB(
double h,
double s,
double v,
double *r,
double *g,
double *b);
782 static void LabToXYZ(
const double lab[3],
double xyz[3]) {
783 LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
785 static void LabToXYZ(
double L,
double a,
double b,
786 double *x,
double *y,
double *z);
787 static double *LabToXYZ(
const double lab[3]);
792 static void XYZToLab(
const double xyz[3],
double lab[3]) {
793 XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
795 static void XYZToLab(
double x,
double y,
double z,
796 double *L,
double *a,
double *b);
797 static double *XYZToLab(
const double xyz[3]);
802 static void XYZToRGB(
const double xyz[3],
double rgb[3]) {
803 XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
805 static void XYZToRGB(
double x,
double y,
double z,
806 double *r,
double *g,
double *b);
807 static double *XYZToRGB(
const double xyz[3]);
812 static void RGBToXYZ(
const double rgb[3],
double xyz[3]) {
813 RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
815 static void RGBToXYZ(
double r,
double g,
double b,
816 double *x,
double *y,
double *z);
817 static double *RGBToXYZ(
const double rgb[3]);
824 static void RGBToLab(
const double rgb[3],
double lab[3]) {
825 RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
827 static void RGBToLab(
double red,
double green,
double blue,
828 double *L,
double *a,
double *b);
829 static double *RGBToLab(
const double rgb[3]);
834 static void LabToRGB(
const double lab[3],
double rgb[3]) {
835 LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
837 static void LabToRGB(
double L,
double a,
double b,
838 double *red,
double *green,
double *blue);
839 static double *LabToRGB(
const double lab[3]);
857 if ( bounds[1]-bounds[0]<0.0 )
868 static void ClampValue(
double *value,
const double range[2]);
869 static void ClampValue(
double value,
const double range[2],
double *clamped_value);
870 static void ClampValues(
871 double *values,
int nb_values,
const double range[2]);
872 static void ClampValues(
873 const double *values,
int nb_values,
const double range[2],
double *clamped_values);
880 static double ClampAndNormalizeValue(
double value,
881 const double range[2]);
891 static int GetScalarTypeFittingRange(
892 double range_min,
double range_max,
893 double scale = 1.0,
double shift = 0.0);
903 static int GetAdjustedScalarRange(
909 static int ExtentIsWithinOtherExtent(
int extent1[6],
int extent2[6]);
914 static int BoundsIsWithinOtherBounds(
double bounds1[6],
double bounds2[6],
double delta[3]);
919 static int PointIsWithinBounds(
double point[3],
double bounds[6],
double delta[3]);
928 static double Solve3PointCircle(
const double p1[3],
const double p2[3],
const double p3[3],
double center[3]);
934 static double NegInf();
941 static int IsInf(
double x);
945 static int IsNan(
double x);
949 static bool IsFinite(
double x);
955 static vtkMathInternal Internal;
958 void operator=(
const vtkMath&);
964 return x * 0.017453292f;
970 return x * 0.017453292519943295;
976 return x * 57.2957795131f;
982 return x * 57.29577951308232;
999 return ((x != 0) & ((x & (x - 1)) == 0));
1006 unsigned int z = ((x > 0) ? x - 1 : 0);
1012 return static_cast<int>(z + 1);
1020 int i =
static_cast<int>(x);
1021 return i - ( i > x );
1029 int i =
static_cast<int>(x);
1030 return i + ( i < x );
1039 for (
int i=0; i < 3; i++)
1053 for (
int i=0; i < 3; i++)
1067 for (
int i=0; i < 2; i++)
1081 for (
int i=0; i < 2; i++)
1094 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1095 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1103 return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1104 c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1109 double b1,
double b2,
double b3,
1110 double c1,
double c2,
double c3)
1121 return ( ( x[0] - y[0] ) * ( x[0] - y[0] )
1122 + ( x[1] - y[1] ) * ( x[1] - y[1] )
1123 + ( x[2] - y[2] ) * ( x[2] - y[2] ) );
1130 return ( ( x[0] - y[0] ) * ( x[0] - y[0] )
1131 + ( x[1] - y[1] ) * ( x[1] - y[1] )
1132 + ( x[2] - y[2] ) * ( x[2] - y[2] ) );
1139 float Zx = x[1] * y[2] - x[2] * y[1];
1140 float Zy = x[2] * y[0] - x[0] * y[2];
1141 float Zz = x[0] * y[1] - x[1] * y[0];
1142 z[0] = Zx; z[1] = Zy; z[2] = Zz;
1149 double Zx = x[1] * y[2] - x[2] * y[1];
1150 double Zy = x[2] * y[0] - x[0] * y[2];
1151 double Zz = x[0] * y[1] - x[1] * y[0];
1152 z[0] = Zx; z[1] = Zy; z[2] = Zz;
1160 return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1161 A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1162 A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1183 if (*value < range[0])
1187 else if (*value > range[1])
1196 double value,
const double range[2],
double *clamped_value)
1198 if (range && clamped_value)
1200 if (value < range[0])
1202 *clamped_value = range[0];
1204 else if (value > range[1])
1206 *clamped_value = range[1];
1210 *clamped_value = value;
1217 const double range[2])
1219 assert(
"pre: valid_range" && range[0]<=range[1]);
1222 if(range[0]==range[1])
1246 result=( result - range[0] ) / ( range[1] - range[0] );
1249 assert(
"post: valid_result" && result>=0.0 && result<=1.0);
1255 #if defined(VTK_HAS_ISINF) || defined(VTK_HAS_STD_ISINF)
1256 #define VTK_MATH_ISINF_IS_INLINE
1259 using namespace std;
1260 return (isinf(x) != 0);
1265 #if defined(VTK_HAS_ISNAN) || defined(VTK_HAS_STD_ISNAN)
1266 #define VTK_MATH_ISNAN_IS_INLINE
1269 using namespace std;
1270 return (isnan(x) != 0);
1275 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE) || defined(VTK_HAS_FINITE)
1276 #define VTK_MATH_ISFINITE_IS_INLINE
1279 #if defined(VTK_HAS_ISFINITE) || defined(VTK_HAS_STD_ISFINITE)
1280 using namespace std;
1281 return (isfinite(x) != 0);
1283 return (finite(x) != 0);
static void MultiplyScalar2D(float a[2], float s)
static bool IsFinite(double x)
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])
static int IsInf(double x)
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 bool IsPowerOfTwo(vtkTypeUInt64 x)
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 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 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])
static int NearestPowerOfTwo(int x)
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 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 void MultiplyScalar(float a[3], float s)
static void HSVToRGB(const float hsv[3], float rgb[3])
static double Norm2D(const double x[2])
static int Round(double f)
VTKWRAPPINGJAVA_EXPORT jlong q(JNIEnv *env, jobject obj)
static void MultiplyScalar2D(double a[2], double s)
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])
static int IsNan(double x)
Gaussian sequence of pseudo random numbers implemented with the Box-Mueller transform.
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])