ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/math/SquareMatrix3.hpp
(Generate patch)

Comparing trunk/OOPSE-2.0/src/math/SquareMatrix3.hpp (file contents):
Revision 1563 by tim, Wed Oct 13 06:51:09 2004 UTC vs.
Revision 1630 by tim, Thu Oct 21 21:31:39 2004 UTC

# Line 29 | Line 29
29   * @date 10/11/2004
30   * @version 1.0
31   */
32 < #ifndef MATH_SQUAREMATRIX#_HPP
33 < #define  MATH_SQUAREMATRIX#_HPP
32 > #ifndef MATH_SQUAREMATRIX3_HPP
33 > #define  MATH_SQUAREMATRIX3_HPP
34  
35 + #include "Quaternion.hpp"
36   #include "SquareMatrix.hpp"
37 + #include "Vector3.hpp"
38 +
39   namespace oopse {
40  
41      template<typename Real>
42      class SquareMatrix3 : public SquareMatrix<Real, 3> {
43          public:
44 +
45 +            typedef Real ElemType;
46 +            typedef Real* ElemPoinerType;
47              
48              /** default constructor */
49              SquareMatrix3() : SquareMatrix<Real, 3>() {
# Line 47 | Line 53 | namespace oopse {
53              SquareMatrix3(const SquareMatrix<Real, 3>& m)  : SquareMatrix<Real, 3>(m) {
54              }
55  
56 +            SquareMatrix3( const Vector3<Real>& eulerAngles) {
57 +                setupRotMat(eulerAngles);
58 +            }
59 +            
60 +            SquareMatrix3(Real phi, Real theta, Real psi) {
61 +                setupRotMat(phi, theta, psi);
62 +            }
63 +
64 +            SquareMatrix3(const Quaternion<Real>& q) {
65 +                setupRotMat(q);
66 +
67 +            }
68 +
69 +            SquareMatrix3(Real w, Real x, Real y, Real z) {
70 +                setupRotMat(w, x, y, z);
71 +            }
72 +            
73              /** copy assignment operator */
74              SquareMatrix3<Real>& operator =(const SquareMatrix<Real, 3>& m) {
75                  if (this == &m)
76                      return *this;
77                   SquareMatrix<Real, 3>::operator=(m);
78 +                 return *this;
79              }
80 +
81 +            /**
82 +             * Sets this matrix to a rotation matrix by three euler angles
83 +             * @ param euler
84 +             */
85 +            void setupRotMat(const Vector3<Real>& eulerAngles) {
86 +                setupRotMat(eulerAngles[0], eulerAngles[1], eulerAngles[2]);
87 +            }
88 +
89 +            /**
90 +             * Sets this matrix to a rotation matrix by three euler angles
91 +             * @param phi
92 +             * @param theta
93 +             * @psi theta
94 +             */
95 +            void setupRotMat(Real phi, Real theta, Real psi) {
96 +                Real sphi, stheta, spsi;
97 +                Real cphi, ctheta, cpsi;
98 +
99 +                sphi = sin(phi);
100 +                stheta = sin(theta);
101 +                spsi = sin(psi);
102 +                cphi = cos(phi);
103 +                ctheta = cos(theta);
104 +                cpsi = cos(psi);
105 +
106 +                data_[0][0] = cpsi * cphi - ctheta * sphi * spsi;
107 +                data_[0][1] = cpsi * sphi + ctheta * cphi * spsi;
108 +                data_[0][2] = spsi * stheta;
109 +                
110 +                data_[1][0] = -spsi * ctheta - ctheta * sphi * cpsi;
111 +                data_[1][1] = -spsi * stheta + ctheta * cphi * cpsi;
112 +                data_[1][2] = cpsi * stheta;
113 +
114 +                data_[2][0] = stheta * sphi;
115 +                data_[2][1] = -stheta * cphi;
116 +                data_[2][2] = ctheta;
117 +            }
118 +
119 +
120 +            /**
121 +             * Sets this matrix to a rotation matrix by quaternion
122 +             * @param quat
123 +            */
124 +            void setupRotMat(const Quaternion<Real>& quat) {
125 +                setupRotMat(quat.w(), quat.x(), quat.y(), quat.z());
126 +            }
127 +
128 +            /**
129 +             * Sets this matrix to a rotation matrix by quaternion
130 +             * @param w the first element
131 +             * @param x the second element
132 +             * @param y the third element
133 +             * @param z the fourth element
134 +            */
135 +            void setupRotMat(Real w, Real x, Real y, Real z) {
136 +                Quaternion<Real> q(w, x, y, z);
137 +                *this = q.toRotationMatrix3();
138 +            }
139 +
140 +            /**
141 +             * Returns the quaternion from this rotation matrix
142 +             * @return the quaternion from this rotation matrix
143 +             * @exception invalid rotation matrix
144 +            */            
145 +            Quaternion<Real> toQuaternion() {
146 +                Quaternion<Real> q;
147 +                Real t, s;
148 +                Real ad1, ad2, ad3;    
149 +                t = data_[0][0] + data_[1][1] + data_[2][2] + 1.0;
150 +
151 +                if( t > 0.0 ){
152 +
153 +                    s = 0.5 / sqrt( t );
154 +                    q[0] = 0.25 / s;
155 +                    q[1] = (data_[1][2] - data_[2][1]) * s;
156 +                    q[2] = (data_[2][0] - data_[0][2]) * s;
157 +                    q[3] = (data_[0][1] - data_[1][0]) * s;
158 +                } else {
159 +
160 +                    ad1 = fabs( data_[0][0] );
161 +                    ad2 = fabs( data_[1][1] );
162 +                    ad3 = fabs( data_[2][2] );
163 +
164 +                    if( ad1 >= ad2 && ad1 >= ad3 ){
165 +
166 +                        s = 2.0 * sqrt( 1.0 + data_[0][0] - data_[1][1] - data_[2][2] );
167 +                        q[0] = (data_[1][2] + data_[2][1]) / s;
168 +                        q[1] = 0.5 / s;
169 +                        q[2] = (data_[0][1] + data_[1][0]) / s;
170 +                        q[3] = (data_[0][2] + data_[2][0]) / s;
171 +                    } else if ( ad2 >= ad1 && ad2 >= ad3 ) {
172 +                        s = sqrt( 1.0 + data_[1][1] - data_[0][0] - data_[2][2] ) * 2.0;
173 +                        q[0] = (data_[0][2] + data_[2][0]) / s;
174 +                        q[1] = (data_[0][1] + data_[1][0]) / s;
175 +                        q[2] = 0.5 / s;
176 +                        q[3] = (data_[1][2] + data_[2][1]) / s;
177 +                    } else {
178 +
179 +                        s = sqrt( 1.0 + data_[2][2] - data_[0][0] - data_[1][1] ) * 2.0;
180 +                        q[0] = (data_[0][1] + data_[1][0]) / s;
181 +                        q[1] = (data_[0][2] + data_[2][0]) / s;
182 +                        q[2] = (data_[1][2] + data_[2][1]) / s;
183 +                        q[3] = 0.5 / s;
184 +                    }
185 +                }            
186 +
187 +                return q;
188 +                
189 +            }
190 +
191 +            /**
192 +             * Returns the euler angles from this rotation matrix
193 +             * @return the euler angles in a vector
194 +             * @exception invalid rotation matrix
195 +             * We use so-called "x-convention", which is the most common definition.
196 +             * In this convention, the rotation given by Euler angles (phi, theta, psi), where the first
197 +             * rotation is by an angle phi about the z-axis, the second is by an angle  
198 +             * theta (0 <= theta <= 180)about the x-axis, and thethird is by an angle psi about the
199 +             * z-axis (again).
200 +            */            
201 +            Vector3<Real> toEulerAngles() {
202 +                Vector3<Real> myEuler;
203 +                Real phi,theta,psi,eps;
204 +                Real ctheta,stheta;
205 +                
206 +                // set the tolerance for Euler angles and rotation elements
207 +
208 +                theta = acos(std::min(1.0, std::max(-1.0,data_[2][2])));
209 +                ctheta = data_[2][2];
210 +                stheta = sqrt(1.0 - ctheta * ctheta);
211 +
212 +                // when sin(theta) is close to 0, we need to consider singularity
213 +                // In this case, we can assign an arbitary value to phi (or psi), and then determine
214 +                // the psi (or phi) or vice-versa. We'll assume that phi always gets the rotation, and psi is 0
215 +                // in cases of singularity.  
216 +                // we use atan2 instead of atan, since atan2 will give us -Pi to Pi.
217 +                // Since 0 <= theta <= 180, sin(theta) will be always non-negative. Therefore, it never
218 +                // change the sign of both of the parameters passed to atan2.
219 +
220 +                if (fabs(stheta) <= oopse::epsilon){
221 +                    psi = 0.0;
222 +                    phi = atan2(-data_[1][0], data_[0][0]);  
223 +                }
224 +                // we only have one unique solution
225 +                else{    
226 +                    phi = atan2(data_[2][0], -data_[2][1]);
227 +                    psi = atan2(data_[0][2], data_[1][2]);
228 +                }
229 +
230 +                //wrap phi and psi, make sure they are in the range from 0 to 2*Pi
231 +                if (phi < 0)
232 +                  phi += M_PI;
233 +
234 +                if (psi < 0)
235 +                  psi += M_PI;
236 +
237 +                myEuler[0] = phi;
238 +                myEuler[1] = theta;
239 +                myEuler[2] = psi;
240 +
241 +                return myEuler;
242 +            }
243              
244 +            /** Returns the determinant of this matrix. */
245 +            Real determinant() const {
246 +                Real x,y,z;
247 +
248 +                x = data_[0][0] * (data_[1][1] * data_[2][2] - data_[1][2] * data_[2][1]);
249 +                y = data_[0][1] * (data_[1][2] * data_[2][0] - data_[1][0] * data_[2][2]);
250 +                z = data_[0][2] * (data_[1][0] * data_[2][1] - data_[1][1] * data_[2][0]);
251 +
252 +                return(x + y + z);
253 +            }            
254 +            
255              /**
256               * Sets the value of this matrix to  the inversion of itself.
257               * @note since simple algorithm can be applied to inverse the 3 by 3 matrix, we hide the
258               * implementation of inverse in SquareMatrix class
259               */
260 <            void  inverse();
261 <            
260 >            SquareMatrix3<Real>  inverse() {
261 >                SquareMatrix3<Real> m;
262 >                double det = determinant();
263 >                if (fabs(det) <= oopse::epsilon) {
264 >                //"The method was called on a matrix with |determinant| <= 1e-6.",
265 >                //"This is a runtime or a programming error in your application.");
266 >                }
267 >
268 >                m(0, 0) = data_[1][1]*data_[2][2] - data_[1][2]*data_[2][1];
269 >                m(1, 0) = data_[1][2]*data_[2][0] - data_[1][0]*data_[2][2];
270 >                m(2, 0) = data_[1][0]*data_[2][1] - data_[1][1]*data_[2][0];
271 >                m(0, 1) = data_[2][1]*data_[0][2] - data_[2][2]*data_[0][1];
272 >                m(1, 1) = data_[2][2]*data_[0][0] - data_[2][0]*data_[0][2];
273 >                m(2, 1) = data_[2][0]*data_[0][1] - data_[2][1]*data_[0][0];
274 >                m(0, 2) = data_[0][1]*data_[1][2] - data_[0][2]*data_[1][1];
275 >                m(1, 2) = data_[0][2]*data_[1][0] - data_[0][0]*data_[1][2];
276 >                m(2, 2) = data_[0][0]*data_[1][1] - data_[0][1]*data_[1][0];
277 >
278 >                m /= det;
279 >                return m;
280 >            }
281              /**
282 <             * Sets the value of this matrix to  the inversion of other matrix.
283 <             * @ param m the source matrix
284 <             */        
285 <            void inverse(const SquareMatrix<Real, Dim>& m);
282 >             * Extract the eigenvalues and eigenvectors from a 3x3 matrix.
283 >             * The eigenvectors (the columns of V) will be normalized.
284 >             * The eigenvectors are aligned optimally with the x, y, and z
285 >             * axes respectively.
286 >             * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is
287 >             *     overwritten            
288 >             * @param w will contain the eigenvalues of the matrix On return of this function
289 >             * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are
290 >             *    normalized and mutually orthogonal.              
291 >             * @warning a will be overwritten
292 >             */
293 >            static void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v);
294 >    };
295 > /*=========================================================================
296  
297 +  Program:   Visualization Toolkit
298 +  Module:    $RCSfile: SquareMatrix3.hpp,v $
299 +
300 +  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
301 +  All rights reserved.
302 +  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
303 +
304 +     This software is distributed WITHOUT ANY WARRANTY; without even
305 +     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
306 +     PURPOSE.  See the above copyright notice for more information.
307 +
308 + =========================================================================*/
309 +    template<typename Real>
310 +    void SquareMatrix3<Real>::diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w,
311 +                                                                           SquareMatrix3<Real>& v) {
312 +        int i,j,k,maxI;
313 +        Real tmp, maxVal;
314 +        Vector3<Real> v_maxI, v_k, v_j;
315 +
316 +        // diagonalize using Jacobi
317 +        jacobi(a, w, v);
318 +        // if all the eigenvalues are the same, return identity matrix
319 +        if (w[0] == w[1] && w[0] == w[2] ) {
320 +              v = SquareMatrix3<Real>::identity();
321 +              return;
322 +        }
323 +
324 +        // transpose temporarily, it makes it easier to sort the eigenvectors
325 +        v = v.transpose();
326 +        
327 +        // if two eigenvalues are the same, re-orthogonalize to optimally line
328 +        // up the eigenvectors with the x, y, and z axes
329 +        for (i = 0; i < 3; i++) {
330 +            if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same
331 +            // find maximum element of the independant eigenvector
332 +            maxVal = fabs(v(i, 0));
333 +            maxI = 0;
334 +            for (j = 1; j < 3; j++) {
335 +                if (maxVal < (tmp = fabs(v(i, j)))){
336 +                    maxVal = tmp;
337 +                    maxI = j;
338 +                }
339 +            }
340 +            
341 +            // swap the eigenvector into its proper position
342 +            if (maxI != i) {
343 +                tmp = w(maxI);
344 +                w(maxI) = w(i);
345 +                w(i) = tmp;
346 +
347 +                v.swapRow(i, maxI);
348 +            }
349 +            // maximum element of eigenvector should be positive
350 +            if (v(maxI, maxI) < 0) {
351 +                v(maxI, 0) = -v(maxI, 0);
352 +                v(maxI, 1) = -v(maxI, 1);
353 +                v(maxI, 2) = -v(maxI, 2);
354 +            }
355 +
356 +            // re-orthogonalize the other two eigenvectors
357 +            j = (maxI+1)%3;
358 +            k = (maxI+2)%3;
359 +
360 +            v(j, 0) = 0.0;
361 +            v(j, 1) = 0.0;
362 +            v(j, 2) = 0.0;
363 +            v(j, j) = 1.0;
364 +
365 +            /** @todo */
366 +            v_maxI = v.getRow(maxI);
367 +            v_j = v.getRow(j);
368 +            v_k = cross(v_maxI, v_j);
369 +            v_k.normalize();
370 +            v_j = cross(v_k, v_maxI);
371 +            v.setRow(j, v_j);
372 +            v.setRow(k, v_k);
373 +
374 +
375 +            // transpose vectors back to columns
376 +            v = v.transpose();
377 +            return;
378 +            }
379 +        }
380 +
381 +        // the three eigenvalues are different, just sort the eigenvectors
382 +        // to align them with the x, y, and z axes
383 +
384 +        // find the vector with the largest x element, make that vector
385 +        // the first vector
386 +        maxVal = fabs(v(0, 0));
387 +        maxI = 0;
388 +        for (i = 1; i < 3; i++) {
389 +            if (maxVal < (tmp = fabs(v(i, 0)))) {
390 +                maxVal = tmp;
391 +                maxI = i;
392 +            }
393 +        }
394 +
395 +        // swap eigenvalue and eigenvector
396 +        if (maxI != 0) {
397 +            tmp = w(maxI);
398 +            w(maxI) = w(0);
399 +            w(0) = tmp;
400 +            v.swapRow(maxI, 0);
401 +        }
402 +        // do the same for the y element
403 +        if (fabs(v(1, 1)) < fabs(v(2, 1))) {
404 +            tmp = w(2);
405 +            w(2) = w(1);
406 +            w(1) = tmp;
407 +            v.swapRow(2, 1);
408 +        }
409 +
410 +        // ensure that the sign of the eigenvectors is correct
411 +        for (i = 0; i < 2; i++) {
412 +            if (v(i, i) < 0) {
413 +                v(i, 0) = -v(i, 0);
414 +                v(i, 1) = -v(i, 1);
415 +                v(i, 2) = -v(i, 2);
416 +            }
417 +        }
418 +
419 +        // set sign of final eigenvector to ensure that determinant is positive
420 +        if (v.determinant() < 0) {
421 +            v(2, 0) = -v(2, 0);
422 +            v(2, 1) = -v(2, 1);
423 +            v(2, 2) = -v(2, 2);
424 +        }
425 +
426 +        // transpose the eigenvectors back again
427 +        v = v.transpose();
428 +        return ;
429      }
430 +    typedef SquareMatrix3<double> Mat3x3d;
431 +    typedef SquareMatrix3<double> RotMat3x3d;
432  
433 <    };
433 > } //namespace oopse
434 > #endif // MATH_SQUAREMATRIX_HPP
435  
74 }
75 #endif // MATH_SQUAREMATRIX#_HPP

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines