00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00035 #ifndef __vtkMath_h
00036 #define __vtkMath_h
00037
00038 #include "vtkObject.h"
00039
00040 class VTK_COMMON_EXPORT vtkMath : public vtkObject
00041 {
00042 public:
00043 static vtkMath *New();
00044 vtkTypeRevisionMacro(vtkMath,vtkObject);
00045
00047
00048 static float Pi() {return 3.14159265358979f;};
00049 static float DegreesToRadians() {return 0.017453292f;};
00050 static float RadiansToDegrees() {return 57.2957795131f;};
00052
00054
00055 static double DoubleDegreesToRadians() {return 0.017453292519943295;};
00056 static double DoublePi() {return 3.1415926535897932384626;};
00057 static double DoubleRadiansToDegrees() {return 57.29577951308232;};
00059
00061
00062 static int Round(float f) {
00063 return static_cast<int>(f + (f >= 0 ? 0.5 : -0.5)); }
00064 static int Round(double f) {
00065 return static_cast<int>(f + (f >= 0 ? 0.5 : -0.5)); }
00067
00069
00070 static float Dot(const float x[3], const float y[3]) {
00071 return (x[0]*y[0] + x[1]*y[1] + x[2]*y[2]);};
00073
00075
00076 static double Dot(const double x[3], const double y[3]) {
00077 return (x[0]*y[0] + x[1]*y[1] + x[2]*y[2]);};
00079
00081 static void Cross(const float x[3], const float y[3], float z[3]);
00082
00085 static void Cross(const double x[3], const double y[3], double z[3]);
00086
00088 static float Norm(const float* x, int n);
00089
00091
00092 static float Norm(const float x[3]) {
00093 return static_cast<float> (sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]));};
00095
00097
00098 static double Norm(const double x[3]) {
00099 return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);};
00101
00103 static float Normalize(float x[3]);
00104
00107 static double Normalize(double x[3]);
00108
00110
00115 static void Perpendiculars(const double x[3], double y[3], double z[3],
00116 double theta);
00117 static void Perpendiculars(const float x[3], float y[3], float z[3],
00118 double theta);
00120
00122 static float Distance2BetweenPoints(const float x[3], const float y[3]);
00123
00126 static double Distance2BetweenPoints(const double x[3], const double y[3]);
00127
00129
00130 static float Dot2D(const float x[3], const float y[3]) {
00131 return (x[0]*y[0] + x[1]*y[1]);};
00133
00135
00137 static double Dot2D(const double x[3], const double y[3]) {
00138 return (x[0]*y[0] + x[1]*y[1]);};
00140
00142
00143 static float Norm2D(const float x[3]) {
00144 return static_cast<float> (sqrt(x[0]*x[0] + x[1]*x[1]));};
00146
00148
00150 static double Norm2D(const double x[3]) {
00151 return sqrt(x[0]*x[0] + x[1]*x[1]);};
00153
00156 static float Normalize2D(float x[3]);
00157
00160 static double Normalize2D(double x[3]);
00161
00163
00164 static float Determinant2x2(const float c1[2], const float c2[2]) {
00165 return (c1[0]*c2[1] - c2[0]*c1[1]);};
00167
00169
00170 static double Determinant2x2(double a, double b, double c, double d) {
00171 return (a * d - b * c);};
00173
00175
00177 static void LUFactor3x3(float A[3][3], int index[3]);
00178 static void LUFactor3x3(double A[3][3], int index[3]);
00180
00182
00184 static void LUSolve3x3(const float A[3][3], const int index[3],
00185 float x[3]);
00186 static void LUSolve3x3(const double A[3][3], const int index[3],
00187 double x[3]);
00189
00191
00193 static void LinearSolve3x3(const float A[3][3], const float x[3],
00194 float y[3]);
00195 static void LinearSolve3x3(const double A[3][3], const double x[3],
00196 double y[3]);
00198
00200
00201 static void Multiply3x3(const float A[3][3], const float in[3],
00202 float out[3]);
00203 static void Multiply3x3(const double A[3][3], const double in[3],
00204 double out[3]);
00206
00208
00209 static void Multiply3x3(const float A[3][3], const float B[3][3],
00210 float C[3][3]);
00211 static void Multiply3x3(const double A[3][3], const double B[3][3],
00212 double C[3][3]);
00214
00216
00217 static void Transpose3x3(const float A[3][3], float AT[3][3]);
00218 static void Transpose3x3(const double A[3][3], double AT[3][3]);
00220
00222
00223 static void Invert3x3(const float A[3][3], float AI[3][3]);
00224 static void Invert3x3(const double A[3][3], double AI[3][3]);
00226
00228
00229 static void Identity3x3(float A[3][3]);
00230 static void Identity3x3(double A[3][3]);
00232
00234
00235 static double Determinant3x3(float A[3][3]);
00236 static double Determinant3x3(double A[3][3]);
00238
00240
00241 static float Determinant3x3(const float c1[3],
00242 const float c2[3],
00243 const float c3[3]);
00245
00247
00249 static double Determinant3x3(double a1, double a2, double a3,
00250 double b1, double b2, double b3,
00251 double c1, double c2, double c3);
00253
00255
00257 static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
00258 static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
00260
00262
00265 static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
00266 static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
00268
00270
00273 static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
00274 static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
00276
00278
00282 static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
00283 static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
00285
00287
00294 static void SingularValueDecomposition3x3(const float A[3][3],
00295 float U[3][3], float w[3],
00296 float VT[3][3]);
00297 static void SingularValueDecomposition3x3(const double A[3][3],
00298 double U[3][3], double w[3],
00299 double VT[3][3]);
00301
00306 static int SolveLinearSystem(double **A, double *x, int size);
00307
00311 static int InvertMatrix(double **A, double **AI, int size);
00312
00314
00316 static int InvertMatrix(double **A, double **AI, int size,
00317 int *tmp1Size, double *tmp2Size);
00319
00325 static int LUFactorLinearSystem(double **A, int *index, int size);
00326
00328
00330 static int LUFactorLinearSystem(double **A, int *index, int size,
00331 double *tmpSize);
00333
00335
00341 static void LUSolveLinearSystem(double **A, int *index,
00342 double *x, int size);
00344
00352 static double EstimateMatrixCondition(double **A, int size);
00353
00359 static void RandomSeed(long s);
00360
00363 static float Random();
00364
00366 static float Random(float min, float max);
00367
00369
00373 static int Jacobi(float **a, float *w, float **v);
00374 static int Jacobi(double **a, double *w, double **v);
00376
00378
00383 static int JacobiN(float **a, int n, float *w, float **v);
00384 static int JacobiN(double **a, int n, double *w, double **v);
00386
00393 static double* SolveCubic(double c0, double c1, double c2, double c3);
00394
00401 static double* SolveQuadratic(double c0, double c1, double c2);
00402
00406 static double* SolveLinear(double c0, double c1);
00407
00409
00420 static int SolveCubic(double c0, double c1, double c2, double c3,
00421 double *r1, double *r2, double *r3, int *num_roots);
00423
00425
00429 static int SolveQuadratic(double c0, double c1, double c2,
00430 double *r1, double *r2, int *num_roots);
00432
00437 static int SolveLinear(double c0, double c1, double *r1, int *num_roots);
00438
00439
00441
00448 static int SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
00449 double **yt, int yOrder, double **mt);
00451
00453
00455 static void RGBToHSV(float rgb[3], float hsv[3])
00456 {
00457 RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
00458 }
00459 static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v);
00461
00463
00465 static void HSVToRGB(float hsv[3], float rgb[3])
00466 {
00467 HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
00468 }
00469 static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b);
00471
00472 protected:
00473 vtkMath() {};
00474 ~vtkMath() {};
00475
00476 static long Seed;
00477 private:
00478 vtkMath(const vtkMath&);
00479 void operator=(const vtkMath&);
00480 };
00481
00482 inline float vtkMath::Normalize(float x[3])
00483 {
00484 float den;
00485 if ( (den = vtkMath::Norm(x)) != 0.0 )
00486 {
00487 for (int i=0; i < 3; i++)
00488 {
00489 x[i] /= den;
00490 }
00491 }
00492 return den;
00493 }
00494 inline double vtkMath::Normalize(double x[3])
00495 {
00496 double den;
00497 if ( (den = vtkMath::Norm(x)) != 0.0 )
00498 {
00499 for (int i=0; i < 3; i++)
00500 {
00501 x[i] /= den;
00502 }
00503 }
00504 return den;
00505 }
00506
00507 inline float vtkMath::Normalize2D(float x[3])
00508 {
00509 float den;
00510 if ( (den = vtkMath::Norm2D(x)) != 0.0 )
00511 {
00512 for (int i=0; i < 2; i++)
00513 {
00514 x[i] /= den;
00515 }
00516 }
00517 return den;
00518 }
00519
00520 inline double vtkMath::Normalize2D(double x[3])
00521 {
00522 double den;
00523 if ( (den = vtkMath::Norm2D(x)) != 0.0 )
00524 {
00525 for (int i=0; i < 2; i++)
00526 {
00527 x[i] /= den;
00528 }
00529 }
00530 return den;
00531 }
00532
00533 inline float vtkMath::Determinant3x3(const float c1[3],
00534 const float c2[3],
00535 const float c3[3])
00536 {
00537 return c1[0]*c2[1]*c3[2] + c2[0]*c3[1]*c1[2] + c3[0]*c1[1]*c2[2] -
00538 c1[0]*c3[1]*c2[2] - c2[0]*c1[1]*c3[2] - c3[0]*c2[1]*c1[2];
00539 }
00540
00541 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
00542 double b1, double b2, double b3,
00543 double c1, double c2, double c3)
00544 {
00545 return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
00546 - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
00547 + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
00548 }
00549
00550 inline float vtkMath::Distance2BetweenPoints(const float x[3],
00551 const float y[3])
00552 {
00553 return ((x[0]-y[0])*(x[0]-y[0]) + (x[1]-y[1])*(x[1]-y[1]) +
00554 (x[2]-y[2])*(x[2]-y[2]));
00555 }
00556 inline double vtkMath::Distance2BetweenPoints(const double x[3],
00557 const double y[3])
00558 {
00559 return ((x[0]-y[0])*(x[0]-y[0]) + (x[1]-y[1])*(x[1]-y[1]) +
00560 (x[2]-y[2])*(x[2]-y[2]));
00561 }
00562
00563 inline float vtkMath::Random(float min, float max)
00564 {
00565 return (min + vtkMath::Random()*(max-min));
00566 }
00567
00568
00569 inline void vtkMath::Cross(const float x[3], const float y[3], float z[3])
00570 {
00571 float Zx = x[1]*y[2] - x[2]*y[1];
00572 float Zy = x[2]*y[0] - x[0]*y[2];
00573 float Zz = x[0]*y[1] - x[1]*y[0];
00574 z[0] = Zx; z[1] = Zy; z[2] = Zz;
00575 }
00576
00577
00578 inline void vtkMath::Cross(const double x[3], const double y[3], double z[3])
00579 {
00580 double Zx = x[1]*y[2] - x[2]*y[1];
00581 double Zy = x[2]*y[0] - x[0]*y[2];
00582 double Zz = x[0]*y[1] - x[1]*y[0];
00583 z[0] = Zx; z[1] = Zy; z[2] = Zz;
00584 }
00585
00586
00587
00588 template<class T>
00589 inline double vtkDeterminant3x3(T A[3][3])
00590 {
00591 return A[0][0]*A[1][1]*A[2][2] + A[1][0]*A[2][1]*A[0][2] +
00592 A[2][0]*A[0][1]*A[1][2] - A[0][0]*A[2][1]*A[1][2] -
00593 A[1][0]*A[0][1]*A[2][2] - A[2][0]*A[1][1]*A[0][2];
00594 }
00595
00596
00597 inline double vtkMath::Determinant3x3(float A[3][3])
00598 {
00599 return vtkDeterminant3x3(A);
00600 }
00601
00602 inline double vtkMath::Determinant3x3(double A[3][3])
00603 {
00604 return vtkDeterminant3x3(A);
00605 }
00606
00607
00608 #endif