OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / SGI / util / SGI / include / vtk / vtkMath.h
1 /*=========================================================================
2
3   Program:   Visualization Toolkit
4   Module:    $RCSfile: vtkMath.h,v $
5   Language:  C++
6   Date:      $Date: 2002/02/01 06:30:41 $
7   Version:   $Revision: 1.1.1.1 $
8
9
10 Copyright (c) 1993-1998 Ken Martin, Will Schroeder, Bill Lorensen.
11
12 This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
13 The following terms apply to all files associated with the software unless
14 explicitly disclaimed in individual files. This copyright specifically does
15 not apply to the related textbook "The Visualization Toolkit" ISBN
16 013199837-4 published by Prentice Hall which is covered by its own copyright.
17
18 The authors hereby grant permission to use, copy, and distribute this
19 software and its documentation for any purpose, provided that existing
20 copyright notices are retained in all copies and that this notice is included
21 verbatim in any distributions. Additionally, the authors grant permission to
22 modify this software and its documentation for any purpose, provided that
23 such modifications are not distributed without the explicit consent of the
24 authors and that existing copyright notices are retained in all copies. Some
25 of the algorithms implemented by this software are patented, observe all
26 applicable patent law.
27
28 IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
29 DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
30 OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
31 EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
34 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
35 PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN
36 "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
37 MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
38
39
40 =========================================================================*/
41 // .NAME vtkMath - performs common math operations
42 // .SECTION Description
43 // vtkMath is provides methods to perform common math operations. These 
44 // include providing constants such as Pi; conversion from degrees to 
45 // radians; vector operations such as dot and cross products and vector 
46 // norm; matrix determinant for 2x2 and 3x3 matrices; and random 
47 // number generation.
48
49 #ifndef __vtkMath_h
50 #define __vtkMath_h
51
52 #include <math.h>
53 #include "vtkWin32Header.h"
54
55 class VTK_EXPORT vtkMath
56 {
57 public:
58   vtkMath() {};
59   static vtkMath *New() {return new vtkMath;};
60   virtual const char *GetClassName() {return "vtkMath";};
61   virtual void Delete(); //delete a vtk object.
62   
63 #ifdef _WIN32
64   // avoid dll boundary problems
65   void* operator new( size_t tSize, const char *, int);
66   void* operator new( size_t tSize );
67   void operator delete( void* p );
68 #endif 
69
70   // constants
71   static float Pi() {return 3.14159265358979;};
72   static float DegreesToRadians() {return 0.017453292;};
73
74   // some common methods
75   static float Dot(float x[3], float y[3]);
76   static double Dot(double x[3], double y[3]);
77   static void Cross(float x[3], float y[3], float z[3]);
78   static float Norm(float x[3]);
79   static float Normalize(float x[3]);
80   static float Distance2BetweenPoints(float x[3], float y[3]);
81
82   // special methods for 2D operations
83   static float Dot2D(float x[3], float y[3]);
84   static double Dot2D(double x[3], double y[3]);
85   static float Norm2D(float x[3]);
86   static float Normalize2D(float x[3]);
87
88   // matrix stuff
89   static float Determinant2x2(float c1[2], float c2[2]);
90   static double Determinant2x2(double a, double b, double c, double d);
91   static float Determinant3x3(float c1[3], float c2[3], float c3[3]);
92   static double Determinant3x3(double a1, double a2, double a3, 
93                                double b1, double b2, double b3, 
94                                double c1, double c2, double c3);
95   static int SolveLinearSystem(double **A, double *x, int size);
96   static int InvertMatrix(double **A, double **AI, int size);
97   static int LUFactorLinearSystem(double **A, int *index, int size);
98   static void LUSolveLinearSystem(double **A, int *index, double *x, int size);
99   static double EstimateMatrixCondition(double **A, int size);
100
101   // Random number generation
102   static void RandomSeed(long s);  
103   static float Random();  
104   static float Random(float min, float max);
105
106   // Eigenvalue/vector extraction for 3x3 matrices
107   static int Jacobi(float **a, float *d, float **v);
108
109   // Roots of polinomial equations
110   // 
111   static double* SolveCubic( double c0, double c1, double c2, double c3 );
112   static double* SolveQuadratic( double c0, double c1, double c2 );
113
114   static void SolveCubic( double c0, double c1, double c2, double c3, 
115                            double *r1, double *r2, double *r3, int *num_roots );
116   static void SolveQuadratic( double c0, double c1, double c2, 
117                         double *r1, double *r2, int *num_roots );
118
119 protected:
120   static long Seed;
121 };
122
123 // Description:
124 // Dot product of two 3-vectors (float version).
125 inline float vtkMath::Dot(float x[3], float y[3]) 
126 {
127   return (x[0]*y[0] + x[1]*y[1] + x[2]*y[2]);
128 }
129
130 // Description:
131 // Dot product of two 3-vectors (double-precision version).
132 inline double vtkMath::Dot(double x[3], double y[3]) 
133 {
134   return (x[0]*y[0] + x[1]*y[1] + x[2]*y[2]);
135 }
136
137 // Description:
138 // Dot product of two 2-vectors. The third (z) component is ignored.
139 inline float vtkMath::Dot2D(float x[3], float y[3]) 
140 {
141   return (x[0]*y[0] + x[1]*y[1]);
142 }
143
144 // Description:
145 // Dot product of two 2-vectors. The third (z) component is ignored. (Double-precision
146 // version.)
147 inline double vtkMath::Dot2D(double x[3], double y[3]) 
148 {
149   return (x[0]*y[0] + x[1]*y[1]);
150 }
151
152 // Description:
153 // Compute the norm of 3-vector.
154 inline float vtkMath::Norm(float x[3])
155 {
156   return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);
157 }
158
159 // Description:
160 // Compute the norm of a 2-vector. Ignores z-component.
161 inline float vtkMath::Norm2D(float x[3])
162 {
163   return sqrt(x[0]*x[0] + x[1]*x[1]);
164 }
165
166 // Description:
167 // Normalize (in place) a 3-vector. Returns norm of vector.
168 inline float vtkMath::Normalize(float x[3])
169 {
170   float den; 
171   if ( (den = vtkMath::Norm(x)) != 0.0 ) for (int i=0; i < 3; i++) x[i] /= den;
172   return den;
173 }
174
175 // Description:
176 // Normalize (in place) a 2-vector. Returns norm of vector. Ignores z-component.
177 inline float vtkMath::Normalize2D(float x[3])
178 {
179   float den; 
180   if ( (den = vtkMath::Norm2D(x)) != 0.0 ) for (int i=0; i < 2; i++) x[i] /= den;
181   return den;
182 }
183
184 // Description:
185 // Compute determinant of 2x2 matrix. Two columns of matrix are input.
186 inline float vtkMath::Determinant2x2(float c1[2], float c2[2])
187 {
188   return (c1[0]*c2[1] - c2[0]*c1[1]);
189 }
190
191 // Description:
192 // Calculate the determinant of a 2x2 matrix: | a b |
193 //                                            | c d |
194 inline double vtkMath::Determinant2x2(double a, double b, double c, double d)
195 {
196   return (a * d - b * c);
197 }
198
199 // Description:
200 // Compute determinant of 3x3 matrix. Three columns of matrix are input.
201 inline float vtkMath::Determinant3x3(float c1[3], float c2[3], float c3[3])
202 {
203   return c1[0]*c2[1]*c3[2] + c2[0]*c3[1]*c1[2] + c3[0]*c1[1]*c2[2] -
204          c1[0]*c3[1]*c2[2] - c2[0]*c1[1]*c3[2] - c3[0]*c2[1]*c1[2];
205 }
206
207 // Description:
208 // Calculate the determinent of a 3x3 matrix in the form:
209 //     | a1,  b1,  c1 |
210 //     | a2,  b2,  c2 |
211 //     | a3,  b3,  c3 |
212 inline double vtkMath::Determinant3x3(double a1, double a2, double a3, 
213                                      double b1, double b2, double b3, 
214                                      double c1, double c2, double c3)
215 {
216     return ( a1 * vtkMath::Determinant2x2( b2, b3, c2, c3 )
217             - b1 * vtkMath::Determinant2x2( a2, a3, c2, c3 )
218             + c1 * vtkMath::Determinant2x2( a2, a3, b2, b3 ) );
219 }
220
221 // Description:
222 // Compute distance squared between two points.
223 inline float vtkMath::Distance2BetweenPoints(float x[3], float y[3])
224 {
225   return ((x[0]-y[0])*(x[0]-y[0]) + (x[1]-y[1])*(x[1]-y[1]) +
226           (x[2]-y[2])*(x[2]-y[2]));
227 }
228
229 // Description:
230 // Generate random number between (min,max).
231 inline float vtkMath::Random(float min, float max)
232 {
233   return (min + vtkMath::Random()*(max-min));
234 }
235
236 #endif