00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00048 #ifndef __vtkMath_h
00049 #define __vtkMath_h
00050
00051 #include "vtkObject.h"
00052
00053 class VTK_COMMON_EXPORT vtkMath : public vtkObject
00054 {
00055 public:
00056 static vtkMath *New();
00057 vtkTypeRevisionMacro(vtkMath,vtkObject);
00058
00060
00061 static float Pi() {return 3.14159265358979f;};
00062 static float DegreesToRadians() {return 0.017453292f;};
00063 static float RadiansToDegrees() {return 57.2957795131f;};
00065
00067
00068 static double DoubleDegreesToRadians() {return 0.017453292519943295;};
00069 static double DoublePi() {return 3.1415926535897932384626;};
00070 static double DoubleRadiansToDegrees() {return 57.29577951308232;};
00072
00074
00075 static int Round(float f) {
00076 return static_cast<int>(f + (f >= 0 ? 0.5 : -0.5)); }
00077 static int Round(double f) {
00078 return static_cast<int>(f + (f >= 0 ? 0.5 : -0.5)); }
00080
00082
00083 static float Dot(const float x[3], const float y[3]) {
00084 return (x[0]*y[0] + x[1]*y[1] + x[2]*y[2]);};
00086
00088
00089 static double Dot(const double x[3], const double y[3]) {
00090 return (x[0]*y[0] + x[1]*y[1] + x[2]*y[2]);};
00092
00094 static void Cross(const float x[3], const float y[3], float z[3]);
00095
00098 static void Cross(const double x[3], const double y[3], double z[3]);
00099
00101 static float Norm(const float* x, int n);
00102
00104
00105 static float Norm(const float x[3]) {
00106 return static_cast<float> (sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]));};
00108
00110
00111 static double Norm(const double x[3]) {
00112 return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);};
00114
00116 static float Normalize(float x[3]);
00117
00120 static double Normalize(double x[3]);
00121
00123
00128 static void Perpendiculars(const double x[3], double y[3], double z[3],
00129 double theta);
00130 static void Perpendiculars(const float x[3], float y[3], float z[3],
00131 double theta);
00133
00135 static float Distance2BetweenPoints(const float x[3], const float y[3]);
00136
00139 static double Distance2BetweenPoints(const double x[3], const double y[3]);
00140
00142
00143 static float Dot2D(const float x[3], const float y[3]) {
00144 return (x[0]*y[0] + x[1]*y[1]);};
00146
00148
00150 static double Dot2D(const double x[3], const double y[3]) {
00151 return (x[0]*y[0] + x[1]*y[1]);};
00153
00155
00156 static float Norm2D(const float x[3]) {
00157 return static_cast<float> (sqrt(x[0]*x[0] + x[1]*x[1]));};
00159
00161
00163 static double Norm2D(const double x[3]) {
00164 return sqrt(x[0]*x[0] + x[1]*x[1]);};
00166
00169 static float Normalize2D(float x[3]);
00170
00173 static double Normalize2D(double x[3]);
00174
00176
00177 static float Determinant2x2(const float c1[2], const float c2[2]) {
00178 return (c1[0]*c2[1] - c2[0]*c1[1]);};
00180
00182
00183 static double Determinant2x2(double a, double b, double c, double d) {
00184 return (a * d - b * c);};
00186
00188
00190 static void LUFactor3x3(float A[3][3], int index[3]);
00191 static void LUFactor3x3(double A[3][3], int index[3]);
00193
00195
00197 static void LUSolve3x3(const float A[3][3], const int index[3],
00198 float x[3]);
00199 static void LUSolve3x3(const double A[3][3], const int index[3],
00200 double x[3]);
00202
00204
00206 static void LinearSolve3x3(const float A[3][3], const float x[3],
00207 float y[3]);
00208 static void LinearSolve3x3(const double A[3][3], const double x[3],
00209 double y[3]);
00211
00213
00214 static void Multiply3x3(const float A[3][3], const float in[3],
00215 float out[3]);
00216 static void Multiply3x3(const double A[3][3], const double in[3],
00217 double out[3]);
00219
00221
00222 static void Multiply3x3(const float A[3][3], const float B[3][3],
00223 float C[3][3]);
00224 static void Multiply3x3(const double A[3][3], const double B[3][3],
00225 double C[3][3]);
00227
00229
00230 static void Transpose3x3(const float A[3][3], float AT[3][3]);
00231 static void Transpose3x3(const double A[3][3], double AT[3][3]);
00233
00235
00236 static void Invert3x3(const float A[3][3], float AI[3][3]);
00237 static void Invert3x3(const double A[3][3], double AI[3][3]);
00239
00241
00242 static void Identity3x3(float A[3][3]);
00243 static void Identity3x3(double A[3][3]);
00245
00247
00248 static double Determinant3x3(float A[3][3]);
00249 static double Determinant3x3(double A[3][3]);
00251
00253
00254 static float Determinant3x3(const float c1[3],
00255 const float c2[3],
00256 const float c3[3]);
00258
00260
00262 static double Determinant3x3(double a1, double a2, double a3,
00263 double b1, double b2, double b3,
00264 double c1, double c2, double c3);
00266
00268
00270 static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
00271 static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
00273
00275
00278 static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
00279 static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
00281
00283
00286 static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
00287 static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
00289
00291
00295 static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
00296 static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
00298
00300
00307 static void SingularValueDecomposition3x3(const float A[3][3],
00308 float U[3][3], float w[3],
00309 float VT[3][3]);
00310 static void SingularValueDecomposition3x3(const double A[3][3],
00311 double U[3][3], double w[3],
00312 double VT[3][3]);
00314
00319 static int SolveLinearSystem(double **A, double *x, int size);
00320
00324 static int InvertMatrix(double **A, double **AI, int size);
00325
00327
00329 static int InvertMatrix(double **A, double **AI, int size,
00330 int *tmp1Size, double *tmp2Size);
00332
00338 static int LUFactorLinearSystem(double **A, int *index, int size);
00339
00341
00343 static int LUFactorLinearSystem(double **A, int *index, int size,
00344 double *tmpSize);
00346
00348
00354 static void LUSolveLinearSystem(double **A, int *index,
00355 double *x, int size);
00357
00365 static double EstimateMatrixCondition(double **A, int size);
00366
00372 static void RandomSeed(long s);
00373
00376 static float Random();
00377
00379 static float Random(float min, float max);
00380
00382
00386 static int Jacobi(float **a, float *w, float **v);
00387 static int Jacobi(double **a, double *w, double **v);
00389
00391
00396 static int JacobiN(float **a, int n, float *w, float **v);
00397 static int JacobiN(double **a, int n, double *w, double **v);
00399
00406 static double* SolveCubic(double c0, double c1, double c2, double c3);
00407
00414 static double* SolveQuadratic(double c0, double c1, double c2);
00415
00419 static double* SolveLinear(double c0, double c1);
00420
00422
00433 static int SolveCubic(double c0, double c1, double c2, double c3,
00434 double *r1, double *r2, double *r3, int *num_roots);
00436
00438
00442 static int SolveQuadratic(double c0, double c1, double c2,
00443 double *r1, double *r2, int *num_roots);
00445
00450 static int SolveLinear(double c0, double c1, double *r1, int *num_roots);
00451
00452
00454
00461 static int SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
00462 double **yt, int yOrder, double **mt);
00464
00466
00468 static void RGBToHSV(float rgb[3], float hsv[3])
00469 {
00470 RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
00471 }
00472 static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v);
00474
00476
00478 static void HSVToRGB(float hsv[3], float rgb[3])
00479 {
00480 HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
00481 }
00482 static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b);
00484
00485 protected:
00486 vtkMath() {};
00487 ~vtkMath() {};
00488
00489 static long Seed;
00490 private:
00491 vtkMath(const vtkMath&);
00492 void operator=(const vtkMath&);
00493 };
00494
00495 inline float vtkMath::Normalize(float x[3])
00496 {
00497 float den;
00498 if ( (den = vtkMath::Norm(x)) != 0.0 )
00499 {
00500 for (int i=0; i < 3; i++)
00501 {
00502 x[i] /= den;
00503 }
00504 }
00505 return den;
00506 }
00507 inline double vtkMath::Normalize(double x[3])
00508 {
00509 double den;
00510 if ( (den = vtkMath::Norm(x)) != 0.0 )
00511 {
00512 for (int i=0; i < 3; i++)
00513 {
00514 x[i] /= den;
00515 }
00516 }
00517 return den;
00518 }
00519
00520 inline float vtkMath::Normalize2D(float x[3])
00521 {
00522 float 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 double vtkMath::Normalize2D(double x[3])
00534 {
00535 double den;
00536 if ( (den = vtkMath::Norm2D(x)) != 0.0 )
00537 {
00538 for (int i=0; i < 2; i++)
00539 {
00540 x[i] /= den;
00541 }
00542 }
00543 return den;
00544 }
00545
00546 inline float vtkMath::Determinant3x3(const float c1[3],
00547 const float c2[3],
00548 const float c3[3])
00549 {
00550 return c1[0]*c2[1]*c3[2] + c2[0]*c3[1]*c1[2] + c3[0]*c1[1]*c2[2] -
00551 c1[0]*c3[1]*c2[2] - c2[0]*c1[1]*c3[2] - c3[0]*c2[1]*c1[2];
00552 }
00553
00554 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
00555 double b1, double b2, double b3,
00556 double c1, double c2, double c3)
00557 {
00558 return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
00559 - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
00560 + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
00561 }
00562
00563 inline float vtkMath::Distance2BetweenPoints(const float x[3],
00564 const float y[3])
00565 {
00566 return ((x[0]-y[0])*(x[0]-y[0]) + (x[1]-y[1])*(x[1]-y[1]) +
00567 (x[2]-y[2])*(x[2]-y[2]));
00568 }
00569 inline double vtkMath::Distance2BetweenPoints(const double x[3],
00570 const double y[3])
00571 {
00572 return ((x[0]-y[0])*(x[0]-y[0]) + (x[1]-y[1])*(x[1]-y[1]) +
00573 (x[2]-y[2])*(x[2]-y[2]));
00574 }
00575
00576 inline float vtkMath::Random(float min, float max)
00577 {
00578 return (min + vtkMath::Random()*(max-min));
00579 }
00580
00581
00582 inline void vtkMath::Cross(const float x[3], const float y[3], float z[3])
00583 {
00584 float Zx = x[1]*y[2] - x[2]*y[1];
00585 float Zy = x[2]*y[0] - x[0]*y[2];
00586 float Zz = x[0]*y[1] - x[1]*y[0];
00587 z[0] = Zx; z[1] = Zy; z[2] = Zz;
00588 }
00589
00590
00591 inline void vtkMath::Cross(const double x[3], const double y[3], double z[3])
00592 {
00593 double Zx = x[1]*y[2] - x[2]*y[1];
00594 double Zy = x[2]*y[0] - x[0]*y[2];
00595 double Zz = x[0]*y[1] - x[1]*y[0];
00596 z[0] = Zx; z[1] = Zy; z[2] = Zz;
00597 }
00598
00599
00600
00601 template<class T>
00602 inline double vtkDeterminant3x3(T A[3][3])
00603 {
00604 return A[0][0]*A[1][1]*A[2][2] + A[1][0]*A[2][1]*A[0][2] +
00605 A[2][0]*A[0][1]*A[1][2] - A[0][0]*A[2][1]*A[1][2] -
00606 A[1][0]*A[0][1]*A[2][2] - A[2][0]*A[1][1]*A[0][2];
00607 }
00608
00609
00610 inline double vtkMath::Determinant3x3(float A[3][3])
00611 {
00612 return vtkDeterminant3x3(A);
00613 }
00614
00615 inline double vtkMath::Determinant3x3(double A[3][3])
00616 {
00617 return vtkDeterminant3x3(A);
00618 }
00619
00620
00621 #endif