ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/math/SquareMatrix3.hpp
Revision: 1630
Committed: Thu Oct 21 21:31:39 2004 UTC (19 years, 8 months ago) by tim
File size: 16427 byte(s)
Log Message:
Snapshot and SnapshotManager in progress

File Contents

# User Rev Content
1 tim 1563 /*
2     * Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project
3     *
4     * Contact: oopse@oopse.org
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public License
8     * as published by the Free Software Foundation; either version 2.1
9     * of the License, or (at your option) any later version.
10     * All we ask is that proper credit is given for our work, which includes
11     * - but is not limited to - adding the above copyright notice to the beginning
12     * of your source code files, and to any copyright notice that you may distribute
13     * with programs based on this work.
14     *
15     * This program is distributed in the hope that it will be useful,
16     * but WITHOUT ANY WARRANTY; without even the implied warranty of
17     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     * GNU Lesser General Public License for more details.
19     *
20     * You should have received a copy of the GNU Lesser General Public License
21     * along with this program; if not, write to the Free Software
22     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23     *
24     */
25    
26     /**
27     * @file SquareMatrix3.hpp
28     * @author Teng Lin
29     * @date 10/11/2004
30     * @version 1.0
31     */
32 tim 1616 #ifndef MATH_SQUAREMATRIX3_HPP
33 tim 1592 #define MATH_SQUAREMATRIX3_HPP
34 tim 1563
35 tim 1586 #include "Quaternion.hpp"
36 tim 1563 #include "SquareMatrix.hpp"
37 tim 1586 #include "Vector3.hpp"
38    
39 tim 1563 namespace oopse {
40    
41     template<typename Real>
42     class SquareMatrix3 : public SquareMatrix<Real, 3> {
43     public:
44 tim 1630
45     typedef Real ElemType;
46     typedef Real* ElemPoinerType;
47 tim 1563
48     /** default constructor */
49     SquareMatrix3() : SquareMatrix<Real, 3>() {
50     }
51    
52     /** copy constructor */
53     SquareMatrix3(const SquareMatrix<Real, 3>& m) : SquareMatrix<Real, 3>(m) {
54     }
55    
56 tim 1586 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 tim 1606 setupRotMat(q);
66    
67 tim 1586 }
68    
69     SquareMatrix3(Real w, Real x, Real y, Real z) {
70 tim 1606 setupRotMat(w, x, y, z);
71 tim 1586 }
72    
73 tim 1563 /** 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 tim 1594 return *this;
79 tim 1563 }
80 tim 1569
81     /**
82     * Sets this matrix to a rotation matrix by three euler angles
83     * @ param euler
84     */
85 tim 1586 void setupRotMat(const Vector3<Real>& eulerAngles) {
86     setupRotMat(eulerAngles[0], eulerAngles[1], eulerAngles[2]);
87     }
88 tim 1569
89     /**
90     * Sets this matrix to a rotation matrix by three euler angles
91     * @param phi
92     * @param theta
93     * @psi theta
94     */
95 tim 1586 void setupRotMat(Real phi, Real theta, Real psi) {
96     Real sphi, stheta, spsi;
97     Real cphi, ctheta, cpsi;
98 tim 1569
99 tim 1586 sphi = sin(phi);
100     stheta = sin(theta);
101     spsi = sin(psi);
102     cphi = cos(phi);
103     ctheta = cos(theta);
104     cpsi = cos(psi);
105 tim 1569
106 tim 1586 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 tim 1569 /**
121     * Sets this matrix to a rotation matrix by quaternion
122     * @param quat
123     */
124 tim 1586 void setupRotMat(const Quaternion<Real>& quat) {
125 tim 1606 setupRotMat(quat.w(), quat.x(), quat.y(), quat.z());
126 tim 1586 }
127 tim 1569
128     /**
129     * Sets this matrix to a rotation matrix by quaternion
130 tim 1586 * @param w the first element
131     * @param x the second element
132     * @param y the third element
133 tim 1594 * @param z the fourth element
134 tim 1569 */
135 tim 1586 void setupRotMat(Real w, Real x, Real y, Real z) {
136     Quaternion<Real> q(w, x, y, z);
137     *this = q.toRotationMatrix3();
138     }
139 tim 1569
140     /**
141     * Returns the quaternion from this rotation matrix
142     * @return the quaternion from this rotation matrix
143     * @exception invalid rotation matrix
144     */
145 tim 1586 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 tim 1569
151 tim 1586 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 tim 1569 /**
192     * Returns the euler angles from this rotation matrix
193 tim 1586 * @return the euler angles in a vector
194 tim 1569 * @exception invalid rotation matrix
195 tim 1586 * 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 tim 1569 */
201 tim 1586 Vector3<Real> toEulerAngles() {
202 tim 1606 Vector3<Real> myEuler;
203 tim 1586 Real phi,theta,psi,eps;
204     Real ctheta,stheta;
205    
206     // set the tolerance for Euler angles and rotation elements
207    
208 tim 1606 theta = acos(std::min(1.0, std::max(-1.0,data_[2][2])));
209 tim 1586 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 tim 1563
244 tim 1594 /** 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 tim 1563 /**
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 tim 1594 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 tim 1563
268 tim 1594 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 tim 1592 }
281 tim 1616 /**
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 tim 1569
297 tim 1616 Program: Visualization Toolkit
298     Module: $RCSfile: SquareMatrix3.hpp,v $
299 tim 1592
300 tim 1616 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 tim 1594
304 tim 1616 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 tim 1594
308 tim 1616 =========================================================================*/
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 tim 1594
316 tim 1616 // 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 tim 1594
324 tim 1616 // 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 tim 1594
347 tim 1616 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 tim 1594
356 tim 1616 // re-orthogonalize the other two eigenvectors
357     j = (maxI+1)%3;
358     k = (maxI+2)%3;
359 tim 1594
360 tim 1616 v(j, 0) = 0.0;
361     v(j, 1) = 0.0;
362     v(j, 2) = 0.0;
363     v(j, j) = 1.0;
364 tim 1594
365 tim 1616 /** @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 tim 1594
374    
375 tim 1616 // transpose vectors back to columns
376     v = v.transpose();
377     return;
378     }
379     }
380 tim 1594
381 tim 1616 // the three eigenvalues are different, just sort the eigenvectors
382     // to align them with the x, y, and z axes
383 tim 1594
384 tim 1616 // 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 tim 1594
395 tim 1616 // 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 tim 1594
410 tim 1616 // 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 tim 1592 }
417 tim 1616 }
418 tim 1563
419 tim 1616 // 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 tim 1592 typedef SquareMatrix3<double> Mat3x3d;
431     typedef SquareMatrix3<double> RotMat3x3d;
432 tim 1586
433     } //namespace oopse
434     #endif // MATH_SQUAREMATRIX_HPP
435 tim 1616