00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00059 #ifndef __vtkMath_h
00060 #define __vtkMath_h
00061
00062 #include "vtkObject.h"
00063
00064 class VTK_COMMON_EXPORT vtkMath : public vtkObject
00065 {
00066 public:
00067 static vtkMath *New();
00068 vtkTypeMacro(vtkMath,vtkObject);
00069
00071
00072 static float Pi() {return 3.14159265358979;};
00073 static float DegreesToRadians() {return 0.017453292;};
00075
00077 static double DoubleDegreesToRadians() {return 0.017453292519943295;};
00078
00080
00081 static float Dot(const float x[3], const float y[3]) {
00082 return (x[0]*y[0] + x[1]*y[1] + x[2]*y[2]);};
00084
00086
00087 static double Dot(const double x[3], const double y[3]) {
00088 return (x[0]*y[0] + x[1]*y[1] + x[2]*y[2]);};
00090
00092 static void Cross(const float x[3], const float y[3], float z[3]);
00093
00096 static void Cross(const double x[3], const double y[3], double z[3]);
00097
00099 static float Norm(const float* x, int n);
00100
00102
00103 static float Norm(const float x[3]) {
00104 return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);};
00106
00108
00109 static double Norm(const double x[3]) {
00110 return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);};
00112
00114 static float Normalize(float x[3]);
00115
00118 static double Normalize(double x[3]);
00119
00121
00126 static void Perpendiculars(const double x[3], double y[3], double z[3],
00127 double theta);
00128 static void Perpendiculars(const float x[3], float y[3], float z[3],
00129 double theta);
00131
00133 static float Distance2BetweenPoints(const float x[3], const float y[3]);
00134
00137 static double Distance2BetweenPoints(const double x[3], const double y[3]);
00138
00140
00141 static float Dot2D(const float x[3], const float y[3]) {
00142 return (x[0]*y[0] + x[1]*y[1]);};
00144
00146
00148 static double Dot2D(const double x[3], const double y[3]) {
00149 return (x[0]*y[0] + x[1]*y[1]);};
00151
00153
00154 static float Norm2D(const float x[3]) {
00155 return sqrt(x[0]*x[0] + x[1]*x[1]);};
00157
00159
00161 static double Norm2D(const double x[3]) {
00162 return sqrt(x[0]*x[0] + x[1]*x[1]);};
00164
00167 static float Normalize2D(float x[3]);
00168
00171 static double Normalize2D(double x[3]);
00172
00174
00175 static float Determinant2x2(const float c1[2], const float c2[2]) {
00176 return (c1[0]*c2[1] - c2[0]*c1[1]);};
00178
00180
00181 static double Determinant2x2(double a, double b, double c, double d) {
00182 return (a * d - b * c);};
00184
00186
00188 static void LUFactor3x3(float A[3][3], int index[3]);
00189 static void LUFactor3x3(double A[3][3], int index[3]);
00191
00193
00195 static void LUSolve3x3(const float A[3][3], const int index[3],
00196 float x[3]);
00197 static void LUSolve3x3(const double A[3][3], const int index[3],
00198 double x[3]);
00200
00202
00204 static void LinearSolve3x3(const float A[3][3], const float x[3],
00205 float y[3]);
00206 static void LinearSolve3x3(const double A[3][3], const double x[3],
00207 double y[3]);
00209
00211
00212 static void Multiply3x3(const float A[3][3], const float in[3],
00213 float out[3]);
00214 static void Multiply3x3(const double A[3][3], const double in[3],
00215 double out[3]);
00217
00219
00220 static void Multiply3x3(const float A[3][3], const float B[3][3],
00221 float C[3][3]);
00222 static void Multiply3x3(const double A[3][3], const double B[3][3],
00223 double C[3][3]);
00225
00227
00228 static void Transpose3x3(const float A[3][3], float AT[3][3]);
00229 static void Transpose3x3(const double A[3][3], double AT[3][3]);
00231
00233
00234 static void Invert3x3(const float A[3][3], float AI[3][3]);
00235 static void Invert3x3(const double A[3][3], double AI[3][3]);
00237
00239
00240 static void Identity3x3(float A[3][3]);
00241 static void Identity3x3(double A[3][3]);
00243
00245
00246 static double Determinant3x3(float A[3][3]);
00247 static double Determinant3x3(double A[3][3]);
00249
00251
00252 static float Determinant3x3(const float c1[3],
00253 const float c2[3],
00254 const float c3[3]);
00256
00258
00260 static double Determinant3x3(double a1, double a2, double a3,
00261 double b1, double b2, double b3,
00262 double c1, double c2, double c3);
00264
00266
00268 static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
00269 static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
00271
00273
00276 static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
00277 static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
00279
00281
00284 static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
00285 static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
00287
00289
00293 static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
00294 static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
00296
00298
00305 static void SingularValueDecomposition3x3(const float A[3][3],
00306 float U[3][3], float w[3],
00307 float VT[3][3]);
00308 static void SingularValueDecomposition3x3(const double A[3][3],
00309 double U[3][3], double w[3],
00310 double VT[3][3]);
00312
00317 static int SolveLinearSystem(double **A, double *x, int size);
00318
00322 static int InvertMatrix(double **A, double **AI, int size);
00323
00325
00327 static int InvertMatrix(double **A, double **AI, int size,
00328 int *tmp1Size, double *tmp2Size);
00330
00336 static int LUFactorLinearSystem(double **A, int *index, int size);
00337
00339
00341 static int LUFactorLinearSystem(double **A, int *index, int size,
00342 double *tmpSize);
00344
00346
00352 static void LUSolveLinearSystem(double **A, int *index,
00353 double *x, int size);
00355
00363 static double EstimateMatrixCondition(double **A, int size);
00364
00370 static void RandomSeed(long s);
00371
00374 static float Random();
00375
00377 static float Random(float min, float max);
00378
00380
00384 static int Jacobi(float **a, float *w, float **v);
00385 static int Jacobi(double **a, double *w, double **v);
00387
00389
00394 static int JacobiN(float **a, int n, float *w, float **v);
00395 static int JacobiN(double **a, int n, double *w, double **v);
00397
00404 static double* SolveCubic(double c0, double c1, double c2, double c3);
00405
00412 static double* SolveQuadratic(double c0, double c1, double c2);
00413
00417 static double* SolveLinear(double c0, double c1);
00418
00420
00431 static int SolveCubic(double c0, double c1, double c2, double c3,
00432 double *r1, double *r2, double *r3, int *num_roots);
00434
00436
00440 static int SolveQuadratic(double c0, double c1, double c2,
00441 double *r1, double *r2, int *num_roots);
00443
00448 static int SolveLinear(double c0, double c1, double *r1, int *num_roots);
00449
00450
00452
00459 static int SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
00460 double **yt, int yOrder, double **mt);
00462
00463 protected:
00464 vtkMath() {};
00465 ~vtkMath() {};
00466
00467 static long Seed;
00468 private:
00469 vtkMath(const vtkMath&);
00470 void operator=(const vtkMath&);
00471 };
00472
00473 inline float vtkMath::Normalize(float x[3])
00474 {
00475 float den;
00476 if ( (den = vtkMath::Norm(x)) != 0.0 )
00477 {
00478 for (int i=0; i < 3; i++)
00479 {
00480 x[i] /= den;
00481 }
00482 }
00483 return den;
00484 }
00485 inline double vtkMath::Normalize(double x[3])
00486 {
00487 double den;
00488 if ( (den = vtkMath::Norm(x)) != 0.0 )
00489 {
00490 for (int i=0; i < 3; i++)
00491 {
00492 x[i] /= den;
00493 }
00494 }
00495 return den;
00496 }
00497
00498 inline float vtkMath::Normalize2D(float x[3])
00499 {
00500 float den;
00501 if ( (den = vtkMath::Norm2D(x)) != 0.0 )
00502 {
00503 for (int i=0; i < 2; i++)
00504 {
00505 x[i] /= den;
00506 }
00507 }
00508 return den;
00509 }
00510
00511 inline double vtkMath::Normalize2D(double x[3])
00512 {
00513 double den;
00514 if ( (den = vtkMath::Norm2D(x)) != 0.0 )
00515 {
00516 for (int i=0; i < 2; i++)
00517 {
00518 x[i] /= den;
00519 }
00520 }
00521 return den;
00522 }
00523
00524 inline float vtkMath::Determinant3x3(const float c1[3],
00525 const float c2[3],
00526 const float c3[3])
00527 {
00528 return c1[0]*c2[1]*c3[2] + c2[0]*c3[1]*c1[2] + c3[0]*c1[1]*c2[2] -
00529 c1[0]*c3[1]*c2[2] - c2[0]*c1[1]*c3[2] - c3[0]*c2[1]*c1[2];
00530 }
00531
00532 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
00533 double b1, double b2, double b3,
00534 double c1, double c2, double c3)
00535 {
00536 return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
00537 - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
00538 + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
00539 }
00540
00541 inline float vtkMath::Distance2BetweenPoints(const float x[3],
00542 const float y[3])
00543 {
00544 return ((x[0]-y[0])*(x[0]-y[0]) + (x[1]-y[1])*(x[1]-y[1]) +
00545 (x[2]-y[2])*(x[2]-y[2]));
00546 }
00547 inline double vtkMath::Distance2BetweenPoints(const double x[3],
00548 const double y[3])
00549 {
00550 return ((x[0]-y[0])*(x[0]-y[0]) + (x[1]-y[1])*(x[1]-y[1]) +
00551 (x[2]-y[2])*(x[2]-y[2]));
00552 }
00553
00554 inline float vtkMath::Random(float min, float max)
00555 {
00556 return (min + vtkMath::Random()*(max-min));
00557 }
00558
00559
00560 inline void vtkMath::Cross(const float x[3], const float y[3], float z[3])
00561 {
00562 float Zx = x[1]*y[2] - x[2]*y[1];
00563 float Zy = x[2]*y[0] - x[0]*y[2];
00564 float Zz = x[0]*y[1] - x[1]*y[0];
00565 z[0] = Zx; z[1] = Zy; z[2] = Zz;
00566 }
00567
00568
00569 inline void vtkMath::Cross(const double x[3], const double y[3], double z[3])
00570 {
00571 double Zx = x[1]*y[2] - x[2]*y[1];
00572 double Zy = x[2]*y[0] - x[0]*y[2];
00573 double Zz = x[0]*y[1] - x[1]*y[0];
00574 z[0] = Zx; z[1] = Zy; z[2] = Zz;
00575 }
00576
00577
00578
00579 template<class T>
00580 static inline double vtkDeterminant3x3(T A[3][3])
00581 {
00582 return A[0][0]*A[1][1]*A[2][2] + A[1][0]*A[2][1]*A[0][2] +
00583 A[2][0]*A[0][1]*A[1][2] - A[0][0]*A[2][1]*A[1][2] -
00584 A[1][0]*A[0][1]*A[2][2] - A[2][0]*A[1][1]*A[0][2];
00585 }
00586
00587
00588 inline double vtkMath::Determinant3x3(float A[3][3])
00589 {
00590 return vtkDeterminant3x3(A);
00591 }
00592
00593 inline double vtkMath::Determinant3x3(double A[3][3])
00594 {
00595 return vtkDeterminant3x3(A);
00596 }
00597
00598
00599 #endif