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 2011 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_MIN
54 # define VTK_DBL_MIN 2.2250738585072014e-308
55 #else // DBL_MIN
56 # define VTK_DBL_MIN DBL_MIN
57 #endif // DBL_MIN
58 
59 #ifndef DBL_EPSILON
60 # define VTK_DBL_EPSILON 2.2204460492503131e-16
61 #else // DBL_EPSILON
62 # define VTK_DBL_EPSILON DBL_EPSILON
63 #endif // DBL_EPSILON
64 #include "vtkMathConfigure.h" // For VTK_HAS_ISINF and VTK_HAS_ISNAN
65 
66 #include <assert.h> // assert() in inline implementations.
67 
68 #ifndef VTK_DBL_EPSILON
69 # ifndef DBL_EPSILON
70 # define VTK_DBL_EPSILON 2.2204460492503131e-16
71 # else // DBL_EPSILON
72 # define VTK_DBL_EPSILON DBL_EPSILON
73 # endif // DBL_EPSILON
74 #endif // VTK_DBL_EPSILON
75 
76 class vtkDataArray;
77 class vtkPoints;
78 class vtkMathInternal;
81 
83 {
84 public:
85  static vtkMath *New();
86  vtkTypeMacro(vtkMath,vtkObject);
87  void PrintSelf(ostream& os, vtkIndent indent);
88 
90  static float Pi() { return 3.14159265358979f; };
91 
94  static double DoubleTwoPi() { return 6.283185307179586; };
95 
98  static double DoublePi() { return 3.1415926535897932384626; };
99 
101 
102  static float RadiansFromDegrees( float degrees);
103  static double RadiansFromDegrees( double degrees);
105 
107 
108  static float DegreesFromRadians( float radians);
109  static double DegreesFromRadians( double radians);
111 
113 
114  static int Round(float f) {
115  return static_cast<int>( f + ( f >= 0 ? 0.5 : -0.5 ) ); }
116  static int Round(double f) {
117  return static_cast<int>( f + ( f >= 0 ? 0.5 : -0.5 ) ); }
119 
122  static int Floor(double x);
123 
126  static int Ceil(double x);
127 
130  static vtkTypeInt64 Factorial( int N );
131 
135  static vtkTypeInt64 Binomial( int m, int n );
136 
143  static int* BeginCombination( int m, int n );
144 
151  static int NextCombination( int m, int n, int* combination );
152 
154  static void FreeCombination( int* combination);
155 
167  static void RandomSeed(int s);
168 
177  static int GetSeed();
178 
188  static double Random();
189 
198  static double Random( double min, double max );
199 
208  static double Gaussian();
209 
219  static double Gaussian( double mean, double std );
220 
222 
223  static void Add(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 
231  static void Add(const double a[3], const double b[3], double c[3]) {
232  for (int i = 0; i < 3; ++i)
233  c[i] = a[i] + b[i];
234  }
236 
238 
240  static void Subtract(const float a[3], const float b[3], float c[3]) {
241  for (int i = 0; i < 3; ++i)
242  c[i] = a[i] - b[i];
243  }
245 
247 
249  static void Subtract(const double a[3], const double b[3], double c[3]) {
250  for (int i = 0; i < 3; ++i)
251  c[i] = a[i] - b[i];
252  }
254 
256 
258  static void MultiplyScalar(float a[3], float s) {
259  for (int i = 0; i < 3; ++i)
260  a[i] *= s;
261  }
263 
265 
267  static void MultiplyScalar2D(float a[2], float s) {
268  for (int i = 0; i < 2; ++i)
269  a[i] *= s;
270  }
272 
274 
276  static void MultiplyScalar(double a[3], double s) {
277  for (int i = 0; i < 3; ++i)
278  a[i] *= s;
279  }
281 
283 
285  static void MultiplyScalar2D(double a[2], double s) {
286  for (int i = 0; i < 2; ++i)
287  a[i] *= s;
288  }
290 
292 
293  static float Dot(const float x[3], const float y[3]) {
294  return ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] );};
296 
298 
299  static double Dot(const double x[3], const double y[3]) {
300  return ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] );};
302 
304 
305  static void Outer(const float x[3], const float y[3], float A[3][3]) {
306  for (int i=0; i < 3; i++)
307  for (int j=0; j < 3; j++)
308  A[i][j] = x[i] * y[j];
309  }
311 
312 
313  static void Outer(const double x[3], const double y[3], double A[3][3]) {
314  for (int i=0; i < 3; i++)
315  for (int j=0; j < 3; j++)
316  A[i][j] = x[i] * y[j];
317  }
319 
321  static void Cross(const float x[3], const float y[3], float z[3]);
322 
325  static void Cross(const double x[3], const double y[3], double z[3]);
326 
328 
329  static float Norm(const float* x, int n);
330  static double Norm(const double* x, int n);
332 
334 
335  static float Norm(const float x[3]) {
336  return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] ) );};
338 
340 
341  static double Norm(const double x[3]) {
342  return sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] );};
344 
346  static float Normalize(float x[3]);
347 
350  static double Normalize(double x[3]);
351 
353 
358  static void Perpendiculars(const double x[3], double y[3], double z[3],
359  double theta);
360  static void Perpendiculars(const float x[3], float y[3], float z[3],
361  double theta);
363 
365 
368  static bool ProjectVector(const float a[3], const float b[3], float projection[3]);
369  static bool ProjectVector(const double a[3], const double b[3], double projection[3]);
371 
373 
377  static bool ProjectVector2D(const float a[2], const float b[2], float projection[2]);
378  static bool ProjectVector2D(const double a[2], const double b[2], double projection[2]);
380 
382  static float Distance2BetweenPoints(const float x[3], const float y[3]);
383 
386  static double Distance2BetweenPoints(const double x[3], const double y[3]);
387 
391  static double GaussianAmplitude(const double variance, const double distanceFromMean);
392 
396  static double GaussianAmplitude(const double mean, const double variance, const double position);
397 
402  static double GaussianWeight(const double variance, const double distanceFromMean);
403 
408  static double GaussianWeight(const double mean, const double variance, const double position);
409 
411 
412  static float Dot2D(const float x[2], const float y[2]) {
413  return ( x[0] * y[0] + x[1] * y[1] );};
415 
417 
418  static double Dot2D(const double x[2], const double y[2]) {
419  return ( x[0] * y[0] + x[1] * y[1] );};
421 
423 
424  static void Outer2D(const float x[2], const float y[2], float A[2][2])
425  {
426  for (int i=0; i < 2; i++)
427  {
428  for (int j=0; j < 2; j++)
429  {
430  A[i][j] = x[i] * y[j];
431  }
432  }
433  }
435 
436 
437  static void Outer2D(const double x[2], const double y[2], double A[2][2])
438  {
439  for (int i=0; i < 2; i++)
440  {
441  for (int j=0; j < 2; j++)
442  {
443  A[i][j] = x[i] * y[j];
444  }
445  }
446  }
448 
450 
451  static float Norm2D(const float x[2]) {
452  return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] ) );};
454 
456 
457  static double Norm2D(const double x[2]) {
458  return sqrt( x[0] * x[0] + x[1] * x[1] );};
460 
462  static float Normalize2D(float x[2]);
463 
466  static double Normalize2D(double x[2]);
467 
469 
470  static float Determinant2x2(const float c1[2], const float c2[2]) {
471  return (c1[0] * c2[1] - c2[0] * c1[1] );};
473 
475 
476  static double Determinant2x2(double a, double b, double c, double d) {
477  return (a * d - b * c);};
478  static double Determinant2x2(const double c1[2], const double c2[2]) {
479  return (c1[0] * c2[1] - c2[0] * c1[1] );};
481 
483 
484  static void LUFactor3x3(float A[3][3], int index[3]);
485  static void LUFactor3x3(double A[3][3], int index[3]);
487 
489 
490  static void LUSolve3x3(const float A[3][3], const int index[3],
491  float x[3]);
492  static void LUSolve3x3(const double A[3][3], const int index[3],
493  double x[3]);
495 
497 
499  static void LinearSolve3x3(const float A[3][3], const float x[3],
500  float y[3]);
501  static void LinearSolve3x3(const double A[3][3], const double x[3],
502  double y[3]);
504 
506 
507  static void Multiply3x3(const float A[3][3], const float in[3],
508  float out[3]);
509  static void Multiply3x3(const double A[3][3], const double in[3],
510  double out[3]);
512 
514 
515  static void Multiply3x3(const float A[3][3], const float B[3][3],
516  float C[3][3]);
517  static void Multiply3x3(const double A[3][3], const double B[3][3],
518  double C[3][3]);
520 
522 
524  static void MultiplyMatrix(const double **A, const double **B,
525  unsigned int rowA, unsigned int colA,
526  unsigned int rowB, unsigned int colB,
527  double **C);
529 
531 
533  static void Transpose3x3(const float A[3][3], float AT[3][3]);
534  static void Transpose3x3(const double A[3][3], double AT[3][3]);
536 
538 
540  static void Invert3x3(const float A[3][3], float AI[3][3]);
541  static void Invert3x3(const double A[3][3], double AI[3][3]);
543 
545 
546  static void Identity3x3(float A[3][3]);
547  static void Identity3x3(double A[3][3]);
549 
551 
552  static double Determinant3x3(float A[3][3]);
553  static double Determinant3x3(double A[3][3]);
555 
557 
558  static float Determinant3x3(const float c1[3],
559  const float c2[3],
560  const float c3[3]);
562 
564 
565  static double Determinant3x3(const double c1[3],
566  const double c2[3],
567  const double c3[3]);
569 
571 
573  static double Determinant3x3(double a1, double a2, double a3,
574  double b1, double b2, double b3,
575  double c1, double c2, double c3);
577 
579 
581  static void QuaternionToMatrix3x3(const float quat[4], float A[3][3]);
582  static void QuaternionToMatrix3x3(const double quat[4], double A[3][3]);
584 
586 
589  static void Matrix3x3ToQuaternion(const float A[3][3], float quat[4]);
590  static void Matrix3x3ToQuaternion(const double A[3][3], double quat[4]);
592 
594 
595  static void MultiplyQuaternion( const float q1[4], const float q2[4], float q[4] );
596  static void MultiplyQuaternion( const double q1[4], const double q2[4], double q[4] );
598 
600 
603  static void Orthogonalize3x3(const float A[3][3], float B[3][3]);
604  static void Orthogonalize3x3(const double A[3][3], double B[3][3]);
606 
608 
612  static void Diagonalize3x3(const float A[3][3], float w[3], float V[3][3]);
613  static void Diagonalize3x3(const double A[3][3],double w[3],double V[3][3]);
615 
617 
624  static void SingularValueDecomposition3x3(const float A[3][3],
625  float U[3][3], float w[3],
626  float VT[3][3]);
627  static void SingularValueDecomposition3x3(const double A[3][3],
628  double U[3][3], double w[3],
629  double VT[3][3]);
631 
636  static int SolveLinearSystem(double **A, double *x, int size);
637 
641  static int InvertMatrix(double **A, double **AI, int size);
642 
644 
646  static int InvertMatrix(double **A, double **AI, int size,
647  int *tmp1Size, double *tmp2Size);
649 
655  static int LUFactorLinearSystem(double **A, int *index, int size);
656 
658 
660  static int LUFactorLinearSystem(double **A, int *index, int size,
661  double *tmpSize);
663 
665 
671  static void LUSolveLinearSystem(double **A, int *index,
672  double *x, int size);
674 
682  static double EstimateMatrixCondition(double **A, int size);
683 
685 
689  static int Jacobi(float **a, float *w, float **v);
690  static int Jacobi(double **a, double *w, double **v);
692 
694 
699  static int JacobiN(float **a, int n, float *w, float **v);
700  static int JacobiN(double **a, int n, double *w, double **v);
702 
710  VTK_LEGACY(static double* SolveCubic(double c0, double c1, double c2, double c3));
711 
719  VTK_LEGACY(static double* SolveQuadratic(double c0, double c1, double c2));
720 
726  VTK_LEGACY(static double* SolveLinear(double c0, double c1));
727 
729 
742  VTK_LEGACY(static int SolveCubic(double c0, double c1, double c2, double c3,
743  double *r1, double *r2, double *r3, int *num_roots));
745 
747 
752  VTK_LEGACY(static int SolveQuadratic(double c0, double c1, double c2,
753  double *r1, double *r2, int *num_roots));
755 
762  VTK_LEGACY(static int SolveQuadratic( double* c, double* r, int* m ));
763 
769  VTK_LEGACY(static int SolveLinear(double c0, double c1, double *r1, int *num_roots));
770 
772 
782  static int SolveHomogeneousLeastSquares(int numberOfSamples, double **xt, int xOrder,
783  double **mt);
785 
786 
788 
799  static int SolveLeastSquares(int numberOfSamples, double **xt, int xOrder,
800  double **yt, int yOrder, double **mt, int checkHomogeneous=1);
802 
804 
808  static void RGBToHSV(const float rgb[3], float hsv[3])
809  { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
810  static void RGBToHSV(float r, float g, float b, float *h, float *s, float *v);
811  static double* RGBToHSV(const double rgb[3]);
812  static double* RGBToHSV(double r, double g, double b);
813  static void RGBToHSV(const double rgb[3], double hsv[3])
814  { RGBToHSV(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2); }
815  static void RGBToHSV(double r, double g, double b, double *h, double *s, double *v);
817 
819 
821  static void HSVToRGB(const float hsv[3], float rgb[3])
822  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
823  static void HSVToRGB(float h, float s, float v, float *r, float *g, float *b);
824  static double* HSVToRGB(const double hsv[3]);
825  static double* HSVToRGB(double h, double s, double v);
826  static void HSVToRGB(const double hsv[3], double rgb[3])
827  { HSVToRGB(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2); }
828  static void HSVToRGB(double h, double s, double v, double *r, double *g, double *b);
830 
832 
833  static void LabToXYZ(const double lab[3], double xyz[3]) {
834  LabToXYZ(lab[0], lab[1], lab[2], xyz+0, xyz+1, xyz+2);
835  }
836  static void LabToXYZ(double L, double a, double b,
837  double *x, double *y, double *z);
838  static double *LabToXYZ(const double lab[3]);
840 
842 
843  static void XYZToLab(const double xyz[3], double lab[3]) {
844  XYZToLab(xyz[0], xyz[1], xyz[2], lab+0, lab+1, lab+2);
845  }
846  static void XYZToLab(double x, double y, double z,
847  double *L, double *a, double *b);
848  static double *XYZToLab(const double xyz[3]);
850 
852 
853  static void XYZToRGB(const double xyz[3], double rgb[3]) {
854  XYZToRGB(xyz[0], xyz[1], xyz[2], rgb+0, rgb+1, rgb+2);
855  }
856  static void XYZToRGB(double x, double y, double z,
857  double *r, double *g, double *b);
858  static double *XYZToRGB(const double xyz[3]);
860 
862 
863  static void RGBToXYZ(const double rgb[3], double xyz[3]) {
864  RGBToXYZ(rgb[0], rgb[1], rgb[2], xyz+0, xyz+1, xyz+2);
865  }
866  static void RGBToXYZ(double r, double g, double b,
867  double *x, double *y, double *z);
868  static double *RGBToXYZ(const double rgb[3]);
870 
872 
875  static void RGBToLab(const double rgb[3], double lab[3]) {
876  RGBToLab(rgb[0], rgb[1], rgb[2], lab+0, lab+1, lab+2);
877  }
878  static void RGBToLab(double red, double green, double blue,
879  double *L, double *a, double *b);
880  static double *RGBToLab(const double rgb[3]);
882 
884 
885  static void LabToRGB(const double lab[3], double rgb[3]) {
886  LabToRGB(lab[0], lab[1], lab[2], rgb+0, rgb+1, rgb+2);
887  }
888  static void LabToRGB(double L, double a, double b,
889  double *red, double *green, double *blue);
890  static double *LabToRGB(const double lab[3]);
892 
894 
895  static void UninitializeBounds(double bounds[6]){
896  bounds[0] = 1.0;
897  bounds[1] = -1.0;
898  bounds[2] = 1.0;
899  bounds[3] = -1.0;
900  bounds[4] = 1.0;
901  bounds[5] = -1.0;
902  }
904 
906 
907  static int AreBoundsInitialized(double bounds[6]){
908  if ( bounds[1]-bounds[0]<0.0 )
909  {
910  return 0;
911  }
912  return 1;
913  }
915 
917 
919  static void ClampValue(double *value, const double range[2]);
920  static void ClampValue(double value, const double range[2], double *clamped_value);
921  static void ClampValues(
922  double *values, int nb_values, const double range[2]);
923  static void ClampValues(
924  const double *values, int nb_values, const double range[2], double *clamped_values);
926 
928 
931  static double ClampAndNormalizeValue(double value,
932  const double range[2]);
934 
936 
942  static int GetScalarTypeFittingRange(
943  double range_min, double range_max,
944  double scale = 1.0, double shift = 0.0);
946 
948 
954  static int GetAdjustedScalarRange(
955  vtkDataArray *array, int comp, double range[2]);
957 
960  static int ExtentIsWithinOtherExtent(int extent1[6], int extent2[6]);
961 
965  static int BoundsIsWithinOtherBounds(double bounds1[6], double bounds2[6], double delta[3]);
966 
970  static int PointIsWithinBounds(double point[3], double bounds[6], double delta[3]);
971 
979  static double Solve3PointCircle(const double p1[3], const double p2[3], const double p3[3], double center[3]);
980 
982  static double Inf();
983 
985  static double NegInf();
986 
988  static double Nan();
989 
992  static int IsInf(double x);
993 
996  static int IsNan(double x);
997 
998 protected:
999  vtkMath() {};
1000  ~vtkMath() {};
1001 
1002  static vtkMathInternal Internal;
1003 private:
1004  vtkMath(const vtkMath&); // Not implemented.
1005  void operator=(const vtkMath&); // Not implemented.
1006 };
1007 
1008 //----------------------------------------------------------------------------
1009 inline float vtkMath::RadiansFromDegrees( float x )
1010 {
1011  return x * 0.017453292f;
1012 }
1013 
1014 //----------------------------------------------------------------------------
1015 inline double vtkMath::RadiansFromDegrees( double x )
1016 {
1017  return x * 0.017453292519943295;
1018 }
1019 
1020 //----------------------------------------------------------------------------
1021 inline float vtkMath::DegreesFromRadians( float x )
1022 {
1023  return x * 57.2957795131f;
1024 }
1025 
1026 //----------------------------------------------------------------------------
1027 inline double vtkMath::DegreesFromRadians( double x )
1028 {
1029  return x * 57.29577951308232;
1030 }
1031 
1032 //----------------------------------------------------------------------------
1033 inline vtkTypeInt64 vtkMath::Factorial( int N )
1034 {
1035  vtkTypeInt64 r = 1;
1036  while ( N > 1 )
1037  {
1038  r *= N--;
1039  }
1040  return r;
1041 }
1042 
1043 //----------------------------------------------------------------------------
1044 // Modify the trunc() operation provided by static_cast<int>() to get floor(),
1045 // if x<0 (condition g) and x!=trunc(x) (condition n) then floor(x)=trunc(x)-1
1046 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1047 inline int vtkMath::Floor(double x)
1048 {
1049  const int r = static_cast<int>(x);
1050  const int n = ( x != static_cast<double>(r) );
1051  const int g = ( x < 0 );
1052  return r - ( n & g );
1053 }
1054 
1055 //----------------------------------------------------------------------------
1056 // Modify the trunc() operation provided by static_cast<int>() to get ceil(),
1057 // if x>=0 (condition g) and x!=trunc(x) (condition n) then ceil(x)=trunc(x)+1
1058 // Note that in C++ conditions evaluate to values of 1 or 0 (true or false).
1059 inline int vtkMath::Ceil(double x)
1060 {
1061  const int r = static_cast<int>(x);
1062  const int n = ( x != static_cast<double>(r) );
1063  const int g = ( x >= 0 );
1064  return r + ( n & g );
1065 }
1066 
1067 //----------------------------------------------------------------------------
1068 inline float vtkMath::Normalize(float x[3])
1069 {
1070  float den;
1071  if ( ( den = vtkMath::Norm( x ) ) != 0.0 )
1072  {
1073  for (int i=0; i < 3; i++)
1074  {
1075  x[i] /= den;
1076  }
1077  }
1078  return den;
1079 }
1080 
1081 //----------------------------------------------------------------------------
1082 inline double vtkMath::Normalize(double x[3])
1083 {
1084  double den;
1085  if ( ( den = vtkMath::Norm( x ) ) != 0.0 )
1086  {
1087  for (int i=0; i < 3; i++)
1088  {
1089  x[i] /= den;
1090  }
1091  }
1092  return den;
1093 }
1094 
1095 //----------------------------------------------------------------------------
1096 inline float vtkMath::Normalize2D(float x[3])
1097 {
1098  float den;
1099  if ( ( den = vtkMath::Norm2D( x ) ) != 0.0 )
1100  {
1101  for (int i=0; i < 2; i++)
1102  {
1103  x[i] /= den;
1104  }
1105  }
1106  return den;
1107 }
1108 
1109 //----------------------------------------------------------------------------
1110 inline double vtkMath::Normalize2D(double x[3])
1111 {
1112  double den;
1113  if ( ( den = vtkMath::Norm2D( x ) ) != 0.0 )
1114  {
1115  for (int i=0; i < 2; i++)
1116  {
1117  x[i] /= den;
1118  }
1119  }
1120  return den;
1121 }
1122 
1123 //----------------------------------------------------------------------------
1124 inline float vtkMath::Determinant3x3(const float c1[3],
1125  const float c2[3],
1126  const float c3[3])
1127 {
1128  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1129  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1130 }
1131 
1132 //----------------------------------------------------------------------------
1133 inline double vtkMath::Determinant3x3(const double c1[3],
1134  const double c2[3],
1135  const double c3[3])
1136 {
1137  return c1[0] * c2[1] * c3[2] + c2[0] * c3[1] * c1[2] + c3[0] * c1[1] * c2[2] -
1138  c1[0] * c3[1] * c2[2] - c2[0] * c1[1] * c3[2] - c3[0] * c2[1] * c1[2];
1139 }
1140 
1141 //----------------------------------------------------------------------------
1142 inline double vtkMath::Determinant3x3(double a1, double a2, double a3,
1143  double b1, double b2, double b3,
1144  double c1, double c2, double c3)
1145 {
1146  return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
1147  - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
1148  + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
1149 }
1150 
1151 //----------------------------------------------------------------------------
1152 inline float vtkMath::Distance2BetweenPoints(const float x[3],
1153  const float y[3])
1154 {
1155  return ( ( x[0] - y[0] ) * ( x[0] - y[0] )
1156  + ( x[1] - y[1] ) * ( x[1] - y[1] )
1157  + ( x[2] - y[2] ) * ( x[2] - y[2] ) );
1158 }
1159 
1160 //----------------------------------------------------------------------------
1161 inline double vtkMath::Distance2BetweenPoints(const double x[3],
1162  const double y[3])
1163 {
1164  return ( ( x[0] - y[0] ) * ( x[0] - y[0] )
1165  + ( x[1] - y[1] ) * ( x[1] - y[1] )
1166  + ( x[2] - y[2] ) * ( x[2] - y[2] ) );
1167 }
1168 
1169 //----------------------------------------------------------------------------
1170 // Cross product of two 3-vectors. Result (a x b) is stored in z[3].
1171 inline void vtkMath::Cross(const float x[3], const float y[3], float z[3])
1172 {
1173  float Zx = x[1] * y[2] - x[2] * y[1];
1174  float Zy = x[2] * y[0] - x[0] * y[2];
1175  float Zz = x[0] * y[1] - x[1] * y[0];
1176  z[0] = Zx; z[1] = Zy; z[2] = Zz;
1177 }
1178 
1179 //----------------------------------------------------------------------------
1180 // Cross product of two 3-vectors. Result (a x b) is stored in z[3].
1181 inline void vtkMath::Cross(const double x[3], const double y[3], double z[3])
1182 {
1183  double Zx = x[1] * y[2] - x[2] * y[1];
1184  double Zy = x[2] * y[0] - x[0] * y[2];
1185  double Zz = x[0] * y[1] - x[1] * y[0];
1186  z[0] = Zx; z[1] = Zy; z[2] = Zz;
1187 }
1188 
1189 //BTX
1190 //----------------------------------------------------------------------------
1191 template<class T>
1192 inline double vtkDeterminant3x3(T A[3][3])
1193 {
1194  return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] +
1195  A[2][0] * A[0][1] * A[1][2] - A[0][0] * A[2][1] * A[1][2] -
1196  A[1][0] * A[0][1] * A[2][2] - A[2][0] * A[1][1] * A[0][2];
1197 }
1198 //ETX
1199 
1200 //----------------------------------------------------------------------------
1201 inline double vtkMath::Determinant3x3(float A[3][3])
1202 {
1203  return vtkDeterminant3x3( A );
1204 }
1205 
1206 //----------------------------------------------------------------------------
1207 inline double vtkMath::Determinant3x3(double A[3][3])
1208 {
1209  return vtkDeterminant3x3( A );
1210 }
1211 
1212 #ifndef VTK_LEGACY_REMOVE
1213 //----------------------------------------------------------------------------
1214 inline double* vtkMath::SolveCubic(double c0, double c1, double c2, double c3)
1215 {
1216  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveCubic, "VTK 5.8",
1218  return vtkPolynomialSolversUnivariate::SolveCubic( c0, c1, c2, c3 );
1219 }
1220 
1221 //----------------------------------------------------------------------------
1222 inline double* vtkMath::SolveQuadratic(double c0, double c1, double c2)
1223 {
1224  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveQuadratic, "VTK 5.8",
1227 }
1228 
1229 //----------------------------------------------------------------------------
1230 inline double* vtkMath::SolveLinear(double c0, double c1)
1231 {
1232  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveLinear, "VTK 5.8",
1235 }
1236 
1237 //----------------------------------------------------------------------------
1238 inline int vtkMath::SolveCubic(double c0, double c1, double c2, double c3,
1239  double *r1, double *r2, double *r3, int *num_roots)
1240 {
1241  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveCubic, "VTK 5.8",
1243  return vtkPolynomialSolversUnivariate::SolveCubic( c0, c1, c2, c3, r1, r2, r3, num_roots );
1244 }
1245 
1246 //----------------------------------------------------------------------------
1247 inline int vtkMath::SolveQuadratic(double c0, double c1, double c2,
1248  double *r1, double *r2, int *num_roots)
1249 {
1250  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveQuadratic, "VTK 5.8",
1252  return vtkPolynomialSolversUnivariate::SolveQuadratic( c0, c1, c2, r1, r2, num_roots );
1253 }
1254 
1255 //----------------------------------------------------------------------------
1256 inline int vtkMath::SolveQuadratic( double* c, double* r, int* m )
1257 {
1258  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveQuadratic, "VTK 5.8",
1261 }
1262 
1263 //----------------------------------------------------------------------------
1264 inline int vtkMath::SolveLinear(double c0, double c1, double *r1, int *num_roots)
1265 {
1266  VTK_LEGACY_REPLACED_BODY(vtkMath::SolveLinear, "VTK 5.8",
1268  return vtkPolynomialSolversUnivariate::SolveLinear( c0, c1, r1, num_roots );
1269 }
1270 #endif
1271 
1272 //----------------------------------------------------------------------------
1273 inline void vtkMath::ClampValue(double *value, const double range[2])
1274 {
1275  if (value && range)
1276  {
1277  if (*value < range[0])
1278  {
1279  *value = range[0];
1280  }
1281  else if (*value > range[1])
1282  {
1283  *value = range[1];
1284  }
1285  }
1286 }
1287 
1288 //----------------------------------------------------------------------------
1290  double value, const double range[2], double *clamped_value)
1291 {
1292  if (range && clamped_value)
1293  {
1294  if (value < range[0])
1295  {
1296  *clamped_value = range[0];
1297  }
1298  else if (value > range[1])
1299  {
1300  *clamped_value = range[1];
1301  }
1302  else
1303  {
1304  *clamped_value = value;
1305  }
1306  }
1307 }
1308 
1309 // ---------------------------------------------------------------------------
1311  const double range[2])
1312 {
1313  assert("pre: valid_range" && range[0]<=range[1]);
1314 
1315  double result;
1316  if(range[0]==range[1])
1317  {
1318  result=0.0;
1319  }
1320  else
1321  {
1322  // clamp
1323  if(value<range[0])
1324  {
1325  result=range[0];
1326  }
1327  else
1328  {
1329  if(value>range[1])
1330  {
1331  result=range[1];
1332  }
1333  else
1334  {
1335  result=value;
1336  }
1337  }
1338 
1339  // normalize
1340  result=( result - range[0] ) / ( range[1] - range[0] );
1341  }
1342 
1343  assert("post: valid_result" && result>=0.0 && result<=1.0);
1344 
1345  return result;
1346 }
1347 
1348 #if defined(VTK_HAS_ISINF)
1349 #ifndef __WRAP__ // Fixes bug 13443 for VTK 5.10
1350 //-----------------------------------------------------------------------------
1351 inline int vtkMath::IsInf(double x)
1352 {
1353  return (isinf(x) ? 1 : 0);
1354 }
1355 #endif
1356 #endif
1357 
1358 #if defined(VTK_HAS_ISNAN)
1359 #ifndef __WRAP__ // Fixes bug 13443 for VTK 5.10
1360 //-----------------------------------------------------------------------------
1361 inline int vtkMath::IsNan(double x)
1362 {
1363  return (isnan(x) ? 1 : 0);
1364 }
1365 #endif
1366 #endif
1367 
1368 #endif