ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/math/SquareMatrix3.hpp
Revision: 2583
Committed: Thu Feb 2 17:14:15 2006 UTC (18 years, 5 months ago) by tim
File size: 16634 byte(s)
Log Message:
adding transpose into SquareMatrix3; fix a bug in AtomStamp where orientation is not set correctly.

File Contents

# User Rev Content
1 gezelter 2204 /*
2 gezelter 1930 * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 tim 1563 *
4 gezelter 1930 * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
6     * redistribute this software in source and binary code form, provided
7     * that the following conditions are met:
8     *
9     * 1. Acknowledgement of the program authors must be made in any
10     * publication of scientific results based in part on use of the
11     * program. An acceptable form of acknowledgement is citation of
12     * the article in which the program was described (Matthew
13     * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14     * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15     * Parallel Simulation Engine for Molecular Dynamics,"
16     * J. Comput. Chem. 26, pp. 252-271 (2005))
17     *
18     * 2. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40 tim 1563 */
41 gezelter 1930
42 tim 1563 /**
43     * @file SquareMatrix3.hpp
44     * @author Teng Lin
45     * @date 10/11/2004
46     * @version 1.0
47     */
48 gezelter 2204 #ifndef MATH_SQUAREMATRIX3_HPP
49 tim 1592 #define MATH_SQUAREMATRIX3_HPP
50 tim 1563
51 tim 1586 #include "Quaternion.hpp"
52 tim 1563 #include "SquareMatrix.hpp"
53 tim 1586 #include "Vector3.hpp"
54 tim 2146 #include "utils/NumericConstant.hpp"
55 tim 1563 namespace oopse {
56    
57 gezelter 2204 template<typename Real>
58     class SquareMatrix3 : public SquareMatrix<Real, 3> {
59     public:
60 tim 1630
61 gezelter 2204 typedef Real ElemType;
62     typedef Real* ElemPoinerType;
63 tim 1563
64 gezelter 2204 /** default constructor */
65     SquareMatrix3() : SquareMatrix<Real, 3>() {
66     }
67 tim 1563
68 gezelter 2204 /** Constructs and initializes every element of this matrix to a scalar */
69     SquareMatrix3(Real s) : SquareMatrix<Real,3>(s){
70     }
71 tim 1644
72 gezelter 2204 /** Constructs and initializes from an array */
73     SquareMatrix3(Real* array) : SquareMatrix<Real,3>(array){
74     }
75 tim 1644
76    
77 gezelter 2204 /** copy constructor */
78     SquareMatrix3(const SquareMatrix<Real, 3>& m) : SquareMatrix<Real, 3>(m) {
79     }
80 gezelter 1930
81 gezelter 2204 SquareMatrix3( const Vector3<Real>& eulerAngles) {
82     setupRotMat(eulerAngles);
83     }
84 tim 1586
85 gezelter 2204 SquareMatrix3(Real phi, Real theta, Real psi) {
86     setupRotMat(phi, theta, psi);
87     }
88 tim 1586
89 gezelter 2204 SquareMatrix3(const Quaternion<Real>& q) {
90     setupRotMat(q);
91 tim 1606
92 gezelter 2204 }
93 tim 1586
94 gezelter 2204 SquareMatrix3(Real w, Real x, Real y, Real z) {
95     setupRotMat(w, x, y, z);
96     }
97 tim 1586
98 gezelter 2204 /** copy assignment operator */
99     SquareMatrix3<Real>& operator =(const SquareMatrix<Real, 3>& m) {
100     if (this == &m)
101     return *this;
102     SquareMatrix<Real, 3>::operator=(m);
103     return *this;
104     }
105 tim 1569
106 gezelter 1930
107 gezelter 2204 SquareMatrix3<Real>& operator =(const Quaternion<Real>& q) {
108     this->setupRotMat(q);
109     return *this;
110     }
111 gezelter 1930
112 gezelter 2204 /**
113     * Sets this matrix to a rotation matrix by three euler angles
114     * @ param euler
115     */
116     void setupRotMat(const Vector3<Real>& eulerAngles) {
117     setupRotMat(eulerAngles[0], eulerAngles[1], eulerAngles[2]);
118     }
119 tim 1569
120 gezelter 2204 /**
121     * Sets this matrix to a rotation matrix by three euler angles
122     * @param phi
123     * @param theta
124     * @psi theta
125     */
126     void setupRotMat(Real phi, Real theta, Real psi) {
127     Real sphi, stheta, spsi;
128     Real cphi, ctheta, cpsi;
129 tim 1569
130 gezelter 2204 sphi = sin(phi);
131     stheta = sin(theta);
132     spsi = sin(psi);
133     cphi = cos(phi);
134     ctheta = cos(theta);
135     cpsi = cos(psi);
136 tim 1569
137 gezelter 2204 this->data_[0][0] = cpsi * cphi - ctheta * sphi * spsi;
138     this->data_[0][1] = cpsi * sphi + ctheta * cphi * spsi;
139     this->data_[0][2] = spsi * stheta;
140 tim 1586
141 gezelter 2204 this->data_[1][0] = -spsi * ctheta - ctheta * sphi * cpsi;
142     this->data_[1][1] = -spsi * stheta + ctheta * cphi * cpsi;
143     this->data_[1][2] = cpsi * stheta;
144 tim 1586
145 gezelter 2204 this->data_[2][0] = stheta * sphi;
146     this->data_[2][1] = -stheta * cphi;
147     this->data_[2][2] = ctheta;
148     }
149 tim 1586
150    
151 gezelter 2204 /**
152     * Sets this matrix to a rotation matrix by quaternion
153     * @param quat
154     */
155     void setupRotMat(const Quaternion<Real>& quat) {
156     setupRotMat(quat.w(), quat.x(), quat.y(), quat.z());
157     }
158 tim 1569
159 gezelter 2204 /**
160     * Sets this matrix to a rotation matrix by quaternion
161     * @param w the first element
162     * @param x the second element
163     * @param y the third element
164     * @param z the fourth element
165     */
166     void setupRotMat(Real w, Real x, Real y, Real z) {
167     Quaternion<Real> q(w, x, y, z);
168     *this = q.toRotationMatrix3();
169     }
170 tim 1569
171 gezelter 2204 /**
172     * Returns the quaternion from this rotation matrix
173     * @return the quaternion from this rotation matrix
174     * @exception invalid rotation matrix
175     */
176     Quaternion<Real> toQuaternion() {
177     Quaternion<Real> q;
178     Real t, s;
179     Real ad1, ad2, ad3;
180     t = this->data_[0][0] + this->data_[1][1] + this->data_[2][2] + 1.0;
181 tim 1569
182 tim 2336 if( t > NumericConstant::epsilon ){
183 tim 1586
184 gezelter 2204 s = 0.5 / sqrt( t );
185     q[0] = 0.25 / s;
186     q[1] = (this->data_[1][2] - this->data_[2][1]) * s;
187     q[2] = (this->data_[2][0] - this->data_[0][2]) * s;
188     q[3] = (this->data_[0][1] - this->data_[1][0]) * s;
189     } else {
190 tim 1586
191 tim 2332 ad1 = this->data_[0][0];
192     ad2 = this->data_[1][1];
193     ad3 = this->data_[2][2];
194 tim 1586
195 gezelter 2204 if( ad1 >= ad2 && ad1 >= ad3 ){
196 tim 1586
197 gezelter 2204 s = 0.5 / sqrt( 1.0 + this->data_[0][0] - this->data_[1][1] - this->data_[2][2] );
198     q[0] = (this->data_[1][2] - this->data_[2][1]) * s;
199     q[1] = 0.25 / s;
200     q[2] = (this->data_[0][1] + this->data_[1][0]) * s;
201     q[3] = (this->data_[0][2] + this->data_[2][0]) * s;
202     } else if ( ad2 >= ad1 && ad2 >= ad3 ) {
203     s = 0.5 / sqrt( 1.0 + this->data_[1][1] - this->data_[0][0] - this->data_[2][2] );
204     q[0] = (this->data_[2][0] - this->data_[0][2] ) * s;
205     q[1] = (this->data_[0][1] + this->data_[1][0]) * s;
206     q[2] = 0.25 / s;
207     q[3] = (this->data_[1][2] + this->data_[2][1]) * s;
208     } else {
209 tim 1586
210 gezelter 2204 s = 0.5 / sqrt( 1.0 + this->data_[2][2] - this->data_[0][0] - this->data_[1][1] );
211     q[0] = (this->data_[0][1] - this->data_[1][0]) * s;
212     q[1] = (this->data_[0][2] + this->data_[2][0]) * s;
213     q[2] = (this->data_[1][2] + this->data_[2][1]) * s;
214     q[3] = 0.25 / s;
215     }
216     }
217 tim 1586
218 gezelter 2204 return q;
219 tim 1586
220 gezelter 2204 }
221 tim 1586
222 gezelter 2204 /**
223     * Returns the euler angles from this rotation matrix
224     * @return the euler angles in a vector
225     * @exception invalid rotation matrix
226     * We use so-called "x-convention", which is the most common definition.
227     * In this convention, the rotation given by Euler angles (phi, theta, psi), where the first
228     * rotation is by an angle phi about the z-axis, the second is by an angle
229     * theta (0 <= theta <= 180)about the x-axis, and thethird is by an angle psi about the
230     * z-axis (again).
231     */
232     Vector3<Real> toEulerAngles() {
233     Vector3<Real> myEuler;
234     Real phi;
235     Real theta;
236     Real psi;
237     Real ctheta;
238     Real stheta;
239 tim 1586
240 gezelter 2204 // set the tolerance for Euler angles and rotation elements
241 tim 1586
242 gezelter 2204 theta = acos(std::min(1.0, std::max(-1.0,this->data_[2][2])));
243     ctheta = this->data_[2][2];
244     stheta = sqrt(1.0 - ctheta * ctheta);
245 tim 1586
246 gezelter 2204 // when sin(theta) is close to 0, we need to consider singularity
247     // In this case, we can assign an arbitary value to phi (or psi), and then determine
248     // the psi (or phi) or vice-versa. We'll assume that phi always gets the rotation, and psi is 0
249     // in cases of singularity.
250     // we use atan2 instead of atan, since atan2 will give us -Pi to Pi.
251     // Since 0 <= theta <= 180, sin(theta) will be always non-negative. Therefore, it never
252     // change the sign of both of the parameters passed to atan2.
253 tim 1586
254 gezelter 2204 if (fabs(stheta) <= oopse::epsilon){
255     psi = 0.0;
256     phi = atan2(-this->data_[1][0], this->data_[0][0]);
257     }
258     // we only have one unique solution
259     else{
260     phi = atan2(this->data_[2][0], -this->data_[2][1]);
261     psi = atan2(this->data_[0][2], this->data_[1][2]);
262     }
263 tim 1586
264 gezelter 2204 //wrap phi and psi, make sure they are in the range from 0 to 2*Pi
265     if (phi < 0)
266     phi += M_PI;
267 tim 1586
268 gezelter 2204 if (psi < 0)
269     psi += M_PI;
270 tim 1586
271 gezelter 2204 myEuler[0] = phi;
272     myEuler[1] = theta;
273     myEuler[2] = psi;
274 tim 1586
275 gezelter 2204 return myEuler;
276     }
277 tim 1563
278 gezelter 2204 /** Returns the determinant of this matrix. */
279     Real determinant() const {
280     Real x,y,z;
281 tim 1594
282 gezelter 2204 x = this->data_[0][0] * (this->data_[1][1] * this->data_[2][2] - this->data_[1][2] * this->data_[2][1]);
283     y = this->data_[0][1] * (this->data_[1][2] * this->data_[2][0] - this->data_[1][0] * this->data_[2][2]);
284     z = this->data_[0][2] * (this->data_[1][0] * this->data_[2][1] - this->data_[1][1] * this->data_[2][0]);
285 tim 1594
286 gezelter 2204 return(x + y + z);
287     }
288 gezelter 1930
289 gezelter 2204 /** Returns the trace of this matrix. */
290     Real trace() const {
291     return this->data_[0][0] + this->data_[1][1] + this->data_[2][2];
292     }
293 tim 1594
294 gezelter 2204 /**
295     * Sets the value of this matrix to the inversion of itself.
296     * @note since simple algorithm can be applied to inverse the 3 by 3 matrix, we hide the
297     * implementation of inverse in SquareMatrix class
298     */
299     SquareMatrix3<Real> inverse() const {
300     SquareMatrix3<Real> m;
301     double det = determinant();
302     if (fabs(det) <= oopse::epsilon) {
303     //"The method was called on a matrix with |determinant| <= 1e-6.",
304     //"This is a runtime or a programming error in your application.");
305     }
306 tim 1563
307 gezelter 2204 m(0, 0) = this->data_[1][1]*this->data_[2][2] - this->data_[1][2]*this->data_[2][1];
308     m(1, 0) = this->data_[1][2]*this->data_[2][0] - this->data_[1][0]*this->data_[2][2];
309     m(2, 0) = this->data_[1][0]*this->data_[2][1] - this->data_[1][1]*this->data_[2][0];
310     m(0, 1) = this->data_[2][1]*this->data_[0][2] - this->data_[2][2]*this->data_[0][1];
311     m(1, 1) = this->data_[2][2]*this->data_[0][0] - this->data_[2][0]*this->data_[0][2];
312     m(2, 1) = this->data_[2][0]*this->data_[0][1] - this->data_[2][1]*this->data_[0][0];
313     m(0, 2) = this->data_[0][1]*this->data_[1][2] - this->data_[0][2]*this->data_[1][1];
314     m(1, 2) = this->data_[0][2]*this->data_[1][0] - this->data_[0][0]*this->data_[1][2];
315     m(2, 2) = this->data_[0][0]*this->data_[1][1] - this->data_[0][1]*this->data_[1][0];
316 tim 1594
317 gezelter 2204 m /= det;
318     return m;
319     }
320 tim 2583
321     SquareMatrix3<Real> transpose() const{
322     SquareMatrix3<Real> result;
323    
324     for (unsigned int i = 0; i < 3; i++)
325     for (unsigned int j = 0; j < 3; j++)
326     result(j, i) = this->data_[i][j];
327    
328     return result;
329     }
330 gezelter 2204 /**
331     * Extract the eigenvalues and eigenvectors from a 3x3 matrix.
332     * The eigenvectors (the columns of V) will be normalized.
333     * The eigenvectors are aligned optimally with the x, y, and z
334     * axes respectively.
335     * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is
336     * overwritten
337     * @param w will contain the eigenvalues of the matrix On return of this function
338     * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are
339     * normalized and mutually orthogonal.
340     * @warning a will be overwritten
341     */
342     static void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v);
343     };
344     /*=========================================================================
345 tim 1569
346 tim 1616 Program: Visualization Toolkit
347     Module: $RCSfile: SquareMatrix3.hpp,v $
348 tim 1592
349 tim 1616 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
350     All rights reserved.
351     See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
352 tim 1594
353 gezelter 2204 This software is distributed WITHOUT ANY WARRANTY; without even
354     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
355     PURPOSE. See the above copyright notice for more information.
356 tim 1594
357 gezelter 2204 =========================================================================*/
358     template<typename Real>
359     void SquareMatrix3<Real>::diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w,
360     SquareMatrix3<Real>& v) {
361     int i,j,k,maxI;
362     Real tmp, maxVal;
363     Vector3<Real> v_maxI, v_k, v_j;
364 tim 1594
365 gezelter 2204 // diagonalize using Jacobi
366     jacobi(a, w, v);
367     // if all the eigenvalues are the same, return identity matrix
368     if (w[0] == w[1] && w[0] == w[2] ) {
369     v = SquareMatrix3<Real>::identity();
370     return;
371     }
372 tim 1594
373 gezelter 2204 // transpose temporarily, it makes it easier to sort the eigenvectors
374     v = v.transpose();
375 tim 1616
376 gezelter 2204 // if two eigenvalues are the same, re-orthogonalize to optimally line
377     // up the eigenvectors with the x, y, and z axes
378     for (i = 0; i < 3; i++) {
379     if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same
380     // find maximum element of the independant eigenvector
381     maxVal = fabs(v(i, 0));
382     maxI = 0;
383     for (j = 1; j < 3; j++) {
384     if (maxVal < (tmp = fabs(v(i, j)))){
385     maxVal = tmp;
386     maxI = j;
387     }
388     }
389 tim 1616
390 gezelter 2204 // swap the eigenvector into its proper position
391     if (maxI != i) {
392     tmp = w(maxI);
393     w(maxI) = w(i);
394     w(i) = tmp;
395 tim 1594
396 gezelter 2204 v.swapRow(i, maxI);
397     }
398     // maximum element of eigenvector should be positive
399     if (v(maxI, maxI) < 0) {
400     v(maxI, 0) = -v(maxI, 0);
401     v(maxI, 1) = -v(maxI, 1);
402     v(maxI, 2) = -v(maxI, 2);
403     }
404 tim 1594
405 gezelter 2204 // re-orthogonalize the other two eigenvectors
406     j = (maxI+1)%3;
407     k = (maxI+2)%3;
408 tim 1594
409 gezelter 2204 v(j, 0) = 0.0;
410     v(j, 1) = 0.0;
411     v(j, 2) = 0.0;
412     v(j, j) = 1.0;
413 tim 1594
414 gezelter 2204 /** @todo */
415     v_maxI = v.getRow(maxI);
416     v_j = v.getRow(j);
417     v_k = cross(v_maxI, v_j);
418     v_k.normalize();
419     v_j = cross(v_k, v_maxI);
420     v.setRow(j, v_j);
421     v.setRow(k, v_k);
422 tim 1594
423    
424 gezelter 2204 // transpose vectors back to columns
425     v = v.transpose();
426     return;
427     }
428     }
429 tim 1594
430 gezelter 2204 // the three eigenvalues are different, just sort the eigenvectors
431     // to align them with the x, y, and z axes
432 tim 1594
433 gezelter 2204 // find the vector with the largest x element, make that vector
434     // the first vector
435     maxVal = fabs(v(0, 0));
436     maxI = 0;
437     for (i = 1; i < 3; i++) {
438     if (maxVal < (tmp = fabs(v(i, 0)))) {
439     maxVal = tmp;
440     maxI = i;
441     }
442     }
443 tim 1594
444 gezelter 2204 // swap eigenvalue and eigenvector
445     if (maxI != 0) {
446     tmp = w(maxI);
447     w(maxI) = w(0);
448     w(0) = tmp;
449     v.swapRow(maxI, 0);
450     }
451     // do the same for the y element
452     if (fabs(v(1, 1)) < fabs(v(2, 1))) {
453     tmp = w(2);
454     w(2) = w(1);
455     w(1) = tmp;
456     v.swapRow(2, 1);
457     }
458 tim 1594
459 gezelter 2204 // ensure that the sign of the eigenvectors is correct
460     for (i = 0; i < 2; i++) {
461     if (v(i, i) < 0) {
462     v(i, 0) = -v(i, 0);
463     v(i, 1) = -v(i, 1);
464     v(i, 2) = -v(i, 2);
465     }
466     }
467 tim 1563
468 gezelter 2204 // set sign of final eigenvector to ensure that determinant is positive
469     if (v.determinant() < 0) {
470     v(2, 0) = -v(2, 0);
471     v(2, 1) = -v(2, 1);
472     v(2, 2) = -v(2, 2);
473 tim 1616 }
474 gezelter 1930
475 gezelter 2204 // transpose the eigenvectors back again
476     v = v.transpose();
477     return ;
478     }
479 gezelter 1930
480 gezelter 2204 /**
481     * Return the multiplication of two matrixes (m1 * m2).
482     * @return the multiplication of two matrixes
483     * @param m1 the first matrix
484     * @param m2 the second matrix
485     */
486     template<typename Real>
487     inline SquareMatrix3<Real> operator *(const SquareMatrix3<Real>& m1, const SquareMatrix3<Real>& m2) {
488     SquareMatrix3<Real> result;
489 gezelter 1930
490 gezelter 2204 for (unsigned int i = 0; i < 3; i++)
491     for (unsigned int j = 0; j < 3; j++)
492     for (unsigned int k = 0; k < 3; k++)
493     result(i, j) += m1(i, k) * m2(k, j);
494 gezelter 1930
495 gezelter 2204 return result;
496     }
497 gezelter 1930
498 gezelter 2204 template<typename Real>
499     inline SquareMatrix3<Real> outProduct(const Vector3<Real>& v1, const Vector3<Real>& v2) {
500     SquareMatrix3<Real> result;
501    
502     for (unsigned int i = 0; i < 3; i++) {
503     for (unsigned int j = 0; j < 3; j++) {
504     result(i, j) = v1[i] * v2[j];
505     }
506     }
507 gezelter 1930
508 gezelter 2204 return result;
509     }
510 gezelter 1930
511    
512 gezelter 2204 typedef SquareMatrix3<double> Mat3x3d;
513     typedef SquareMatrix3<double> RotMat3x3d;
514 tim 1586
515     } //namespace oopse
516     #endif // MATH_SQUAREMATRIX_HPP
517 tim 1616