VTK
vtkMath.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkMath.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================
15  Copyright 2008 Sandia Corporation.
16  Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
17  license for use of this work by or on behalf of the
18  U.S. Government. Redistribution and use in source and binary forms, with
19  or without modification, are permitted provided that this Notice and any
20  statement of authorship are reproduced on all copies.
21 
22  Contact: pppebay@sandia.gov,dcthomp@sandia.gov
23 
24 =========================================================================*/
43 #ifndef __vtkMath_h
44 #define __vtkMath_h
45 
46 #include "vtkObject.h"
47 #ifndef VTK_LEGACY_REMOVE
48 # include "vtkPolynomialSolversUnivariate.h" // For backwards compatibility of old solvers
49 #endif
50 
51 #include <assert.h> // assert() in inline implementations.
52 
53 #ifndef DBL_EPSILON
54 # define VTK_DBL_EPSILON 2.2204460492503131e-16
55 #else // DBL_EPSILON
56 # define VTK_DBL_EPSILON DBL_EPSILON
57 #endif // DBL_EPSILON
58 
59 class vtkDataArray;
60 class vtkPoints;
61 class vtkMathInternal;
64 
66 {
67 public:
68  static vtkMath *New();
69  vtkTypeMacro(vtkMath,vtkObject);
70  void PrintSelf(ostream& os, vtkIndent indent);
71 
73  static float Pi() { return 3.14159265358979f; };
74 
77  static double DoubleTwoPi() { return 6.283185307179586; };
78 
81  static double DoublePi() { return 3.1415926535897932384626; };
82 
84 
85  static float RadiansFromDegrees( float degrees);
86  static double RadiansFromDegrees( double degrees);
88 
90 
91  static float DegreesFromRadians( float radians);
92  static double DegreesFromRadians( double radians);
94 
96 
97  static int Round(float f) {
98  return static_cast<int>( f + ( f >= 0 ? 0.5 : -0.5 ) ); }
99  static int Round(double f) {
100  return static_cast<int>( f + ( f >= 0 ? 0.5 : -0.5 ) ); }
102 
105  static int Floor(double x);
106 
109  static int Ceil(double x);
110 
113  static vtkTypeInt64 Factorial( int N );
114 
118  static vtkTypeInt64 Binomial( int m, int n );
119 
126  static int* BeginCombination( int m, int n );
127 
134  static int NextCombination( int m, int n, int* combination );
135 
137  static void FreeCombination( int* combination);
138 
150  static void RandomSeed(int s);
151 
160  static int GetSeed();
161 
171  static double Random();
172 
181  static double Random( double min, double max );
182 
191  static double Gaussian();
192 
202  static double Gaussian( double mean, double std );
203 
205 
206  static void Add(const float a[3], const float b[3], float c[3]) {
207  for (int i = 0; i < 3; ++i)
208  c[i] = a[i] + b[i];
209  }
211 
213 
214  static void Add(const double a[3], const double b[3], double c[3]) {
215  for (int i = 0; i < 3; ++i)
216  c[i] = a[i] + b[i];
217  }
219 
221 
223  static void Subtract(const float a[3], const float b[3], float c[3]) {
224  for (int i = 0; i < 3; ++i)
225  c[i] = a[i] - b[i];
226  }
228 
230 
232  static void Subtract(const double a[3], const double b[3], double c[3]) {
233  for (int i = 0; i < 3; ++i)
234  c[i] = a[i] - b[i];
235  }
237 
239 
241  static void MultiplyScalar(float a[3], float s) {
242  for (int i = 0; i < 3; ++i)
243  a[i] *= s;
244  }
246 
248 
250  static void MultiplyScalar2D(float a[2], float s) {
251  for (int i = 0; i < 2; ++i)
252  a[i] *= s;
253  }
255 
257 
259  static void MultiplyScalar(double a[3], double s) {
260  for (int i = 0; i < 3; ++i)
261  a[i] *= s;
262  }
264 
266 
268  static void MultiplyScalar2D(double a[2], double s) {
269  for (int i = 0; i < 2; ++i)
270  a[i] *= s;
271  }
273 
275 
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] );};
279 
281 
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] );};
285 
287 
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];
292  }
293  // Description:
294  // Outer product of two 3-vectors (double-precision version).
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];
299  }
301 
303  static void Cross(const float x[3], const float y[3], float z[3]);
304 
307  static void Cross(const double x[3], const double y[3], double z[3]);
308 
310 
311  static float Norm(const float* x, int n);
312  static double Norm(const double* x, int n);
314 
316 
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] ) );};
320 
322 
323  static double Norm(const double x[3]) {
324  return sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] );};
326 
328  static float Normalize(float x[3]);
329 
332  static double Normalize(double x[3]);
333 
335 
340  static void Perpendiculars(const double x[3], double y[3], double z[3],
341  double theta);
342  static void Perpendiculars(const float x[3], float y[3], float z[3],
343  double theta);
345 
347 
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]);
353 
355 
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]);
362 
364  static float Distance2BetweenPoints(const float x[3], const float y[3]);
365 
368  static double Distance2BetweenPoints(const double x[3], const double y[3]);
369 
373  static double GaussianAmplitude(const double variance, const double distanceFromMean);
374 
378  static double GaussianAmplitude(const double mean, const double variance, const double position);
379 
384  static double GaussianWeight(const double variance, const double distanceFromMean);
385 
390  static double GaussianWeight(const double mean, const double variance, const double position);
391 
393 
394  static float Dot2D(const float x[2], const float y[2]) {
395  return ( x[0] * y[0] + x[1] * y[1] );};
397 
399 
400  static double Dot2D(const double x[2], const double y[2]) {
401  return ( x[0] * y[0] + x[1] * y[1] );};
403 
405 
406  static void Outer2D(const float x[2], const float y[2], float A[2][2])
407  {
408  for (int i=0; i < 2; i++)
409  {
410  for (int j=0; j < 2; j++)
411  {
412  A[i][j] = x[i] * y[j];
413  }
414  }
415  }
416  // Description:
417  // Outer product of two 2-vectors (float version).
418  static void Outer2D(const double x[2], const double y[2], double A[2][2])
419  {
420  for (int i=0; i < 2; i++)
421  {
422  for (int j=0; j < 2; j++)
423  {
424  A[i][j] = x[i] * y[j];
425  }
426  }
427  }
429 
431 
432  static float Norm2D(const float x[2]) {
433  return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
435 
437 
438  static double Norm2D(const double x[2]) {
439  return sqrt( x[0] * x[0] + x[1] * x[1] );};
441 
443  static float Normalize2D(float x[2]);
444 
447  static double Normalize2D(double x[2]);
448 
450 
451  static float Determinant2x2(const float c1[2], const float c2[2]) {
452  return (c1[0] * c2[1] - c2[0] * c1[1] );};
454 
456 
457  static double Determinant2x2(double a, double b, double c, double d) {
458  return (a * d - b * c);};
459  static double Determinant2x2(const double c1[2], const double c2[2]) {
460  return (c1[0] * c2[1] - c2[0] * c1[1] );};
462 
464 
465  static void LUFactor3x3(float A[3][3], int index[3]);
466  static void LUFactor3x3(double A[3][3], int index[3]);
468 
470 
471  static void LUSolve3x3(const float A[3][3], const int index[3],
472  float x[3]);
473  static void LUSolve3x3(const double A[3][3], const int index[3],
474  double x[3]);
476 
478 
480  static void LinearSolve3x3(const float A[3][3], const float x[3],
481  float y[3]);
482  static void LinearSolve3x3(const double A[3][3], const double x[3],
483  double y[3]);
485 
487 
488  static void Multiply3x3(const float A[3][3], const float in[3],
489  float out[3]);
490  static void Multiply3x3(const double A[3][3], const double in[3],
491  double out[3]);
493 
495 
496  static void Multiply3x3(const float A[3][3], const float B[3][3],
497  float C[3][3]);
498  static void Multiply3x3(const double A[3][3], const double B[3][3],
499  double C[3][3]);
501 
503 
505  static void MultiplyMatrix(const double **A, const double **B,
506  unsigned int rowA, unsigned int colA,
507  unsigned int rowB, unsigned int colB,
508  double **C);
510 
512 
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]);
517 
519 
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]);
524 
526 
527  static void Identity3x3(float A[3][3]);
528  static void Identity3x3(double A[3][3]);
530 
532 
533  static double Determinant3x3(float A[3][3]);
534  static double Determinant3x3(double A[3][3]);
536 
538 
539  static float Determinant3x3(const float c1[3],
540  const float c2[3],
541  const float c3[3]);
543 
545 
546  static double Determinant3x3(const double c1[3],
547  const double c2[3],
548  const double c3[3]);
550 
552 
554  static double Determinant3x3(double a1, double a2, double a3,
555  double b1, double b2, double b3,
556  double c1, double c2, double c3);
558 
560 
562  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
563  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
565 
567 
570  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
571  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
573 
575 
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]);
581 
583 
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]);
590 
592 
599  static void SingularValueDecomposition3x3(const float A[3][3],
600  float U[3][3], float w[3],
601  float VT[3][3]);
602  static void SingularValueDecomposition3x3(const double A[3][3],
603  double U[3][3], double w[3],
604  double VT[3][3]);
606 
611  static int SolveLinearSystem(double **A, double *x, int size);
612 
616  static int InvertMatrix(double **A, double **AI, int size);
617 
619 
621  static int InvertMatrix(double **A, double **AI, int size,
622  int *tmp1Size, double *tmp2Size);
624 
630  static int LUFactorLinearSystem(double **A, int *index, int size);
631 
633 
635  static int LUFactorLinearSystem(double **A, int *index, int size,
636  double *tmpSize);
638 
640 
646  static void LUSolveLinearSystem(double **A, int *index,
647  double *x, int size);
649 
657  static double EstimateMatrixCondition(double **A, int size);
658 
660 
664  static int Jacobi(float **a, float *w, float **v);
665  static int Jacobi(double **a, double *w, double **v);
667 
669 
674  static int JacobiN(float **a, int n, float *w, float **v);
675  static int JacobiN(double **a, int n, double *w, double **v);
677 
685  VTK_LEGACY(static double* SolveCubic(double c0, double c1, double c2, double c3));
686 
694  VTK_LEGACY(static double* SolveQuadratic(double c0, double c1, double c2));
695 
701  VTK_LEGACY(static double* SolveLinear(double c0, double c1));
702 
704 
717  VTK_LEGACY(static int SolveCubic(double c0, double c1, double c2, double c3,
718  double *r1, double *r2, double *r3, int *num_roots));
720 
722 
727  VTK_LEGACY(static int SolveQuadratic(double c0, double c1, double c2,
728  double *r1, double *r2, int *num_roots));
730 
737  VTK_LEGACY(static int SolveQuadratic( double* c, double* r, int* m ));
738 
744  VTK_LEGACY(static int SolveLinear(double c0, double c1, double *r1, int *num_roots));
745 
747 
757  static int SolveHomogeneousLeastSquares(int numberOfSamples, double **xt, int xOrder,
758  double **mt);
760 
761 
763 
774  static int SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
775  double **yt, int yOrder, double **mt, int checkHomogeneous=1);
777 
779 
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);
790 
792 
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);
803 
805 
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);
808  }
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]);
813 
815 
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);
818  }
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]);
823 
825 
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);
828  }
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]);
833 
835 
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);
838  }
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]);
843 
845 
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);
848  }
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]);
853 
855 
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);
858  }
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]);
863 
865 
866  static void UninitializeBounds(double bounds[6]){
867  bounds[0] = 1.0;
868  bounds[1] = -1.0;
869  bounds[2] = 1.0;
870  bounds[3] = -1.0;
871  bounds[4] = 1.0;
872  bounds[5] = -1.0;
873  }
875 
877 
878  static int AreBoundsInitialized(double bounds[6]){
879  if ( bounds[1]-bounds[0]<0.0 )
880  {
881  return 0;
882  }
883  return 1;
884  }
886 
888 
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);
897 
899 
902  static double ClampAndNormalizeValue(double value,
903  const double range[2]);
905 
907 
913  static int GetScalarTypeFittingRange(
914  double range_min, double range_max,
915  double scale = 1.0, double shift = 0.0);
917 
919 
925  static int GetAdjustedScalarRange(
926  vtkDataArray *array, int comp, double range[2]);
928 
931  static int ExtentIsWithinOtherExtent(int extent1[6], int extent2[6]);
932 
936  static int BoundsIsWithinOtherBounds(double bounds1[6], double bounds2[6], double delta[3]);
937 
941  static int PointIsWithinBounds(double point[3], double bounds[6], double delta[3]);
942 
950  static double Solve3PointCircle(const double p1[3], const double p2[3], const double p3[3], double center[3]);
951 
953  static double Inf();
954 
956  static double NegInf();
957 
959  static double Nan();
960 
963  static int IsInf(double x);
964 
965  // Test if a number is equal to the special floating point value Not-A-Number (Nan).
966  static int IsNan(double x);
967 
968 protected:
969  vtkMath() {};
970  ~vtkMath() {};
971 
972  static vtkMathInternal Internal;
973 private:
974  vtkMath(const vtkMath&); // Not implemented.
975  void operator=(const vtkMath&); // Not implemented.
976 };
977 
978 //----------------------------------------------------------------------------
979 inline float vtkMath::RadiansFromDegrees( float x )
980 {
981  return x * 0.017453292f;
982 }
983 
984 //----------------------------------------------------------------------------
985 inline double vtkMath::RadiansFromDegrees( double x )
986 {
987  return x * 0.017453292519943295;
988 }
989 
990 //----------------------------------------------------------------------------
991 inline float vtkMath::DegreesFromRadians( float x )
992 {
993  return x * 57.2957795131f;
994 }
995 
996 //----------------------------------------------------------------------------
997 inline double vtkMath::DegreesFromRadians( double x )
998 {
999  return x * 57.29577951308232;
1000 }
1001 
1002 //----------------------------------------------------------------------------
1003 inline vtkTypeInt64 vtkMath::Factorial( int N )
1004 {
1005  vtkTypeInt64 r = 1;
1006  while ( N > 1 )
1007  {
1008  r *= N--;
1009  }
1010  return r;
1011 }
1012 
1013 //----------------------------------------------------------------------------
1014 inline int vtkMath::Floor(double x)
1015 {
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 );
1020 }
1021 
1022 //----------------------------------------------------------------------------
1023 inline int vtkMath::Ceil(double x)
1024 {
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 );
1029 }
1030 
1031 //----------------------------------------------------------------------------
1032 inline float vtkMath::Normalize(float x[3])
1033 {
1034  float den;
1035  if ( ( den = vtkMath::Norm( x ) ) != 0.0 )
1036  {
1037  for (int i=0; i < 3; i++)
1038  {
1039  x[i] /= den;
1040  }
1041  }
1042  return den;
1043 }
1044 
1045 //----------------------------------------------------------------------------
1046 inline double vtkMath::Normalize(double x[3])
1047 {
1048  double den;
1049  if ( ( den = vtkMath::Norm( x ) ) != 0.0 )
1050  {
1051  for (int i=0; i < 3; i++)
1052  {
1053  x[i] /= den;
1054  }
1055  }
1056  return den;
1057 }
1058 
1059 //----------------------------------------------------------------------------
1060 inline float vtkMath::Normalize2D(float x[3])
1061 {
1062  float den;
1063  if ( ( den = vtkMath::Norm2D( x ) ) != 0.0 )
1064  {
1065  for (int i=0; i < 2; i++)
1066  {
1067  x[i] /= den;
1068  }
1069  }
1070  return den;
1071 }
1072 
1073 //----------------------------------------------------------------------------
1074 inline double vtkMath::Normalize2D(double x[3])
1075 {
1076  double den;
1077  if ( ( den = vtkMath::Norm2D( x ) ) != 0.0 )
1078  {
1079  for (int i=0; i < 2; i++)
1080  {
1081  x[i] /= den;
1082  }
1083  }
1084  return den;
1085 }
1086 
1087 //----------------------------------------------------------------------------
1088 inline float vtkMath::Determinant3x3(const float c1[3],
1089  const float c2[3],
1090  const float c3[3])
1091 {
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];
1094 }
1095 
1096 //----------------------------------------------------------------------------
1097 inline double vtkMath::Determinant3x3(const double c1[3],
1098  const double c2[3],
1099  const double c3[3])
1100 {
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];
1103 }
1104 
1105 //----------------------------------------------------------------------------
1106 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
1107  double b1, double b2, double b3,
1108  double c1, double c2, double c3)
1109 {
1110  return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
1111  - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
1112  + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
1113 }
1114 
1115 //----------------------------------------------------------------------------
1116 inline float vtkMath::Distance2BetweenPoints(const float x[3],
1117  const float y[3])
1118 {
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] ) );
1122 }
1123 
1124 //----------------------------------------------------------------------------
1125 inline double vtkMath::Distance2BetweenPoints(const double x[3],
1126  const double y[3])
1127 {
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] ) );
1131 }
1132 
1133 //----------------------------------------------------------------------------
1134 // Cross product of two 3-vectors. Result (a x b) is stored in z[3].
1135 inline void vtkMath::Cross(const float x[3], const float y[3], float z[3])
1136 {
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;
1141 }
1142 
1143 //----------------------------------------------------------------------------
1144 // Cross product of two 3-vectors. Result (a x b) is stored in z[3].
1145 inline void vtkMath::Cross(const double x[3], const double y[3], double z[3])
1146 {
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;
1151 }
1152 
1153 //BTX
1154 //----------------------------------------------------------------------------
1155 template<class T>
1156 inline double vtkDeterminant3x3(T A[3][3])
1157 {
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];
1161 }
1162 //ETX
1163 
1164 //----------------------------------------------------------------------------
1165 inline double vtkMath::Determinant3x3(float A[3][3])
1166 {
1167  return vtkDeterminant3x3( A );
1168 }
1169 
1170 //----------------------------------------------------------------------------
1171 inline double vtkMath::Determinant3x3(double A[3][3])
1172 {
1173  return vtkDeterminant3x3( A );
1174 }
1175 
1176 #ifndef VTK_LEGACY_REMOVE
1177 //----------------------------------------------------------------------------
1178 inline double* vtkMath::SolveCubic(double c0, double c1, double c2, double c3)
1179 {
1180  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveCubic, "VTK 5.8",
1182  return vtkPolynomialSolversUnivariate::SolveCubic( c0, c1, c2, c3 );
1183 }
1184 
1185 //----------------------------------------------------------------------------
1186 inline double* vtkMath::SolveQuadratic(double c0, double c1, double c2)
1187 {
1188  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveQuadratic, "VTK 5.8",
1191 }
1192 
1193 //----------------------------------------------------------------------------
1194 inline double* vtkMath::SolveLinear(double c0, double c1)
1195 {
1196  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveLinear, "VTK 5.8",
1199 }
1200 
1201 //----------------------------------------------------------------------------
1202 inline int vtkMath::SolveCubic(double c0, double c1, double c2, double c3,
1203  double *r1, double *r2, double *r3, int *num_roots)
1204 {
1205  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveCubic, "VTK 5.8",
1207  return vtkPolynomialSolversUnivariate::SolveCubic( c0, c1, c2, c3, r1, r2, r3, num_roots );
1208 }
1209 
1210 //----------------------------------------------------------------------------
1211 inline int vtkMath::SolveQuadratic(double c0, double c1, double c2,
1212  double *r1, double *r2, int *num_roots)
1213 {
1214  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveQuadratic, "VTK 5.8",
1216  return vtkPolynomialSolversUnivariate::SolveQuadratic( c0, c1, c2, r1, r2, num_roots );
1217 }
1218 
1219 //----------------------------------------------------------------------------
1220 inline int vtkMath::SolveQuadratic( double* c, double* r, int* m )
1221 {
1222  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveQuadratic, "VTK 5.8",
1225 }
1226 
1227 //----------------------------------------------------------------------------
1228 inline int vtkMath::SolveLinear(double c0, double c1, double *r1, int *num_roots)
1229 {
1230  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveLinear, "VTK 5.8",
1232  return vtkPolynomialSolversUnivariate::SolveLinear( c0, c1, r1, num_roots );
1233 }
1234 #endif
1235 
1236 //----------------------------------------------------------------------------
1237 inline void vtkMath::ClampValue(double *value, const double range[2])
1238 {
1239  if (value && range)
1240  {
1241  if (*value < range[0])
1242  {
1243  *value = range[0];
1244  }
1245  else if (*value > range[1])
1246  {
1247  *value = range[1];
1248  }
1249  }
1250 }
1251 
1252 //----------------------------------------------------------------------------
1254  double value, const double range[2], double *clamped_value)
1255 {
1256  if (range && clamped_value)
1257  {
1258  if (value < range[0])
1259  {
1260  *clamped_value = range[0];
1261  }
1262  else if (value > range[1])
1263  {
1264  *clamped_value = range[1];
1265  }
1266  else
1267  {
1268  *clamped_value = value;
1269  }
1270  }
1271 }
1272 
1273 // ---------------------------------------------------------------------------
1274 inline double vtkMath::ClampAndNormalizeValue(double value,
1275  const double range[2])
1276 {
1277  assert("pre: valid_range" && range[0]<=range[1]);
1278 
1279  double result;
1280  if(range[0]==range[1])
1281  {
1282  result=0.0;
1283  }
1284  else
1285  {
1286  // clamp
1287  if(value<range[0])
1288  {
1289  result=range[0];
1290  }
1291  else
1292  {
1293  if(value>range[1])
1294  {
1295  result=range[1];
1296  }
1297  else
1298  {
1299  result=value;
1300  }
1301  }
1302 
1303  // normalize
1304  result=( result - range[0] ) / ( range[1] - range[0] );
1305  }
1306 
1307  assert("post: valid_result" && result>=0.0 && result<=1.0);
1308 
1309  return result;
1310 }
1311 
1312 #endif
static void MultiplyScalar2D(float a[2], float s)
Definition: vtkMath.h:250
static float Dot2D(const float x[2], const float y[2])
Definition: vtkMath.h:394
static void Cross(const float x[3], const float y[3], float z[3])
Definition: vtkMath.h:1135
abstract base class for most VTK objects
Definition: vtkObject.h:60
static void LabToXYZ(const double lab[3], double xyz[3])
Definition: vtkMath.h:806
static double Norm(const double x[3])
Definition: vtkMath.h:323
static void ClampValue(double *value, const double range[2])
Definition: vtkMath.h:1237
static void Outer(const float x[3], const float y[3], float A[3][3])
Definition: vtkMath.h:288
static float Determinant2x2(const float c1[2], const float c2[2])
Definition: vtkMath.h:451
static vtkTypeInt64 Factorial(int N)
Definition: vtkMath.h:1003
static double * SolveLinear(double c0, double c1)
Definition: vtkMath.h:1194
static void RGBToHSV(const double rgb[3], double hsv[3])
Definition: vtkMath.h:786
static int Round(float f)
Definition: vtkMath.h:97
vtkMath()
Definition: vtkMath.h:969
static void RGBToHSV(const float rgb[3], float hsv[3])
Definition: vtkMath.h:781
static int AreBoundsInitialized(double bounds[6])
Definition: vtkMath.h:878
static double ClampAndNormalizeValue(double value, const double range[2])
Definition: vtkMath.h:1274
static double * SolveQuadratic(double c0, double c1, double c2)
static void Outer(const double x[3], const double y[3], double A[3][3])
Definition: vtkMath.h:295
static void Add(const double a[3], const double b[3], double c[3])
Definition: vtkMath.h:214
static void XYZToRGB(const double xyz[3], double rgb[3])
Definition: vtkMath.h:826
static float Norm2D(const float x[2])
Definition: vtkMath.h:432
static float Normalize2D(float x[2])
static void UninitializeBounds(double bounds[6])
Definition: vtkMath.h:866
virtual void PrintSelf(ostream &os, vtkIndent indent)
static void RGBToXYZ(const double rgb[3], double xyz[3])
Definition: vtkMath.h:836
a simple class to control print indentation
Definition: vtkIndent.h:37
static float Dot(const float x[3], const float y[3])
Definition: vtkMath.h:276
static void Subtract(const float a[3], const float b[3], float c[3])
Definition: vtkMath.h:223
static void Subtract(const double a[3], const double b[3], double c[3])
Definition: vtkMath.h:232
static int Floor(double x)
Definition: vtkMath.h:1014
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:53
static double DoubleTwoPi()
Definition: vtkMath.h:77
static double Determinant2x2(double a, double b, double c, double d)
Definition: vtkMath.h:457
static float RadiansFromDegrees(float degrees)
Definition: vtkMath.h:979
static void HSVToRGB(const double hsv[3], double rgb[3])
Definition: vtkMath.h:799
#define VTK_COMMON_EXPORT
Park and Miller Sequence of pseudo random numbers.
static void MultiplyScalar(double a[3], double s)
Definition: vtkMath.h:259
static double Determinant3x3(float A[3][3])
Definition: vtkMath.h:1165
static float Normalize(float x[3])
Definition: vtkMath.h:1032
static void RGBToLab(const double rgb[3], double lab[3])
Definition: vtkMath.h:846
static float DegreesFromRadians(float radians)
Definition: vtkMath.h:991
static void Outer2D(const double x[2], const double y[2], double A[2][2])
Definition: vtkMath.h:418
static void Outer2D(const float x[2], const float y[2], float A[2][2])
Definition: vtkMath.h:406
static int Ceil(double x)
Definition: vtkMath.h:1023
performs common math operations
Definition: vtkMath.h:65
static double Dot2D(const double x[2], const double y[2])
Definition: vtkMath.h:400
static double Dot(const double x[3], const double y[3])
Definition: vtkMath.h:282
static double * SolveQuadratic(double c0, double c1, double c2)
Definition: vtkMath.h:1186
static void MultiplyScalar(float a[3], float s)
Definition: vtkMath.h:241
static void HSVToRGB(const float hsv[3], float rgb[3])
Definition: vtkMath.h:794
static double * SolveCubic(double c0, double c1, double c2, double c3)
Definition: vtkMath.h:1178
static float Pi()
Definition: vtkMath.h:73
static double DoublePi()
Definition: vtkMath.h:81
~vtkMath()
Definition: vtkMath.h:970
static double Norm2D(const double x[2])
Definition: vtkMath.h:438
static int Round(double f)
Definition: vtkMath.h:99
static void MultiplyScalar2D(double a[2], double s)
Definition: vtkMath.h:268
static double * SolveCubic(double c0, double c1, double c2, double c3)
static void LabToRGB(const double lab[3], double rgb[3])
Definition: vtkMath.h:856
static vtkObject * New()
double vtkDeterminant3x3(T A[3][3])
Definition: vtkMath.h:1156
static float Norm(const float *x, int n)
static float Distance2BetweenPoints(const float x[3], const float y[3])
Definition: vtkMath.h:1116
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])
Definition: vtkMath.h:459
static float Norm(const float x[3])
Definition: vtkMath.h:317
static void Add(const float a[3], const float b[3], float c[3])
Definition: vtkMath.h:206
represent and manipulate 3D points
Definition: vtkPoints.h:38
static void XYZToLab(const double xyz[3], double lab[3])
Definition: vtkMath.h:816