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

Comparing trunk/OOPSE-4/src/math/SquareMatrix3.hpp (file contents):
Revision 1594 by tim, Mon Oct 18 23:13:23 2004 UTC vs.
Revision 1930 by gezelter, Wed Jan 12 22:41:40 2005 UTC

# Line 1 | Line 1
1 < /*
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.
1 > /*
2 > * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3   *
4 + * 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   */
41 <
41 >
42   /**
43   * @file SquareMatrix3.hpp
44   * @author Teng Lin
45   * @date 10/11/2004
46   * @version 1.0
47   */
48 < #ifndef MATH_SQUAREMATRIX3_HPP
48 > #ifndef MATH_SQUAREMATRIX3_HPP
49   #define  MATH_SQUAREMATRIX3_HPP
50  
51   #include "Quaternion.hpp"
# Line 41 | Line 57 | namespace oopse {
57      template<typename Real>
58      class SquareMatrix3 : public SquareMatrix<Real, 3> {
59          public:
60 +
61 +            typedef Real ElemType;
62 +            typedef Real* ElemPoinerType;
63              
64              /** default constructor */
65              SquareMatrix3() : SquareMatrix<Real, 3>() {
66              }
67  
68 +            /** Constructs and initializes every element of this matrix to a scalar */
69 +            SquareMatrix3(Real s) : SquareMatrix<Real,3>(s){
70 +            }
71 +
72 +            /** Constructs and initializes from an array */
73 +            SquareMatrix3(Real* array) : SquareMatrix<Real,3>(array){
74 +            }
75 +
76 +
77              /** copy  constructor */
78              SquareMatrix3(const SquareMatrix<Real, 3>& m)  : SquareMatrix<Real, 3>(m) {
79              }
80 <
80 >            
81              SquareMatrix3( const Vector3<Real>& eulerAngles) {
82                  setupRotMat(eulerAngles);
83              }
# Line 59 | Line 87 | namespace oopse {
87              }
88  
89              SquareMatrix3(const Quaternion<Real>& q) {
90 <                *this = q.toRotationMatrix3();
90 >                setupRotMat(q);
91 >
92              }
93  
94              SquareMatrix3(Real w, Real x, Real y, Real z) {
95 <                Quaternion<Real> q(w, x, y, z);
67 <                *this = q.toRotationMatrix3();
95 >                setupRotMat(w, x, y, z);
96              }
97              
98              /** copy assignment operator */
# Line 75 | Line 103 | namespace oopse {
103                   return *this;
104              }
105  
106 +
107 +            SquareMatrix3<Real>& operator =(const Quaternion<Real>& q) {
108 +                this->setupRotMat(q);
109 +                return *this;
110 +            }
111 +
112              /**
113               * Sets this matrix to a rotation matrix by three euler angles
114               * @ param euler
# Line 119 | Line 153 | namespace oopse {
153               * @param quat
154              */
155              void setupRotMat(const Quaternion<Real>& quat) {
156 <                *this = quat.toRotationMatrix3();
156 >                setupRotMat(quat.w(), quat.x(), quat.y(), quat.z());
157              }
158  
159              /**
# Line 196 | Line 230 | namespace oopse {
230               * z-axis (again).
231              */            
232              Vector3<Real> toEulerAngles() {
233 <                Vector<Real> myEuler;
234 <                Real phi,theta,psi,eps;
235 <                Real ctheta,stheta;
233 >                Vector3<Real> myEuler;
234 >                Real phi;
235 >                Real theta;
236 >                Real psi;
237 >                Real ctheta;
238 >                Real stheta;
239                  
240                  // set the tolerance for Euler angles and rotation elements
241  
242 <                theta = acos(min(1.0,max(-1.0,data_[2][2])));
242 >                theta = acos(std::min(1.0, std::max(-1.0,data_[2][2])));
243                  ctheta = data_[2][2];
244                  stheta = sqrt(1.0 - ctheta * ctheta);
245  
# Line 248 | Line 285 | namespace oopse {
285  
286                  return(x + y + z);
287              }            
288 +
289 +            /** Returns the trace of this matrix. */
290 +            Real trace() const {
291 +                return data_[0][0] + data_[1][1] + data_[2][2];
292 +            }
293              
294              /**
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() {
299 >            SquareMatrix3<Real>  inverse() const {
300                  SquareMatrix3<Real> m;
301                  double det = determinant();
302                  if (fabs(det) <= oopse::epsilon) {
# Line 275 | Line 317 | namespace oopse {
317                  m /= det;
318                  return m;
319              }
320 +            /**
321 +             * Extract the eigenvalues and eigenvectors from a 3x3 matrix.
322 +             * The eigenvectors (the columns of V) will be normalized.
323 +             * The eigenvectors are aligned optimally with the x, y, and z
324 +             * axes respectively.
325 +             * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is
326 +             *     overwritten            
327 +             * @param w will contain the eigenvalues of the matrix On return of this function
328 +             * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are
329 +             *    normalized and mutually orthogonal.              
330 +             * @warning a will be overwritten
331 +             */
332 +            static void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v);
333 +    };
334 + /*=========================================================================
335  
336 <            void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v) {
337 <                int i,j,k,maxI;
281 <                Real tmp, maxVal;
282 <                Vector3<Real> v_maxI, v_k, v_j;
336 >  Program:   Visualization Toolkit
337 >  Module:    $RCSfile: SquareMatrix3.hpp,v $
338  
339 <                // diagonalize using Jacobi
340 <                jacobi(a, w, v);
339 >  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
340 >  All rights reserved.
341 >  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
342  
343 <                // if all the eigenvalues are the same, return identity matrix
344 <                if (w[0] == w[1] && w[0] == w[2] ){
345 <                      v = SquareMatrix3<Real>::identity();
290 <                      return
291 <                }
343 >     This software is distributed WITHOUT ANY WARRANTY; without even
344 >     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
345 >     PURPOSE.  See the above copyright notice for more information.
346  
347 <                // transpose temporarily, it makes it easier to sort the eigenvectors
348 <                v = v.tanspose();
349 <                
350 <                // if two eigenvalues are the same, re-orthogonalize to optimally line
351 <                // up the eigenvectors with the x, y, and z axes
352 <                for (i = 0; i < 3; i++) {
353 <                    if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same
300 <                    // find maximum element of the independant eigenvector
301 <                    maxVal = fabs(v(i, 0));
302 <                    maxI = 0;
303 <                    for (j = 1; j < 3; j++) {
304 <                        if (maxVal < (tmp = fabs(v(i, j)))){
305 <                            maxVal = tmp;
306 <                            maxI = j;
307 <                        }
308 <                    }
309 <                    
310 <                    // swap the eigenvector into its proper position
311 <                    if (maxI != i) {
312 <                        tmp = w(maxI);
313 <                        w(maxI) = w(i);
314 <                        w(i) = tmp;
347 > =========================================================================*/
348 >    template<typename Real>
349 >    void SquareMatrix3<Real>::diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w,
350 >                                                                           SquareMatrix3<Real>& v) {
351 >        int i,j,k,maxI;
352 >        Real tmp, maxVal;
353 >        Vector3<Real> v_maxI, v_k, v_j;
354  
355 <                        v.swapRow(i, maxI);
356 <                    }
357 <                    // maximum element of eigenvector should be positive
358 <                    if (v(maxI, maxI) < 0) {
359 <                        v(maxI, 0) = -v(maxI, 0);
360 <                        v(maxI, 1) = -v(maxI, 1);
361 <                        v(maxI, 2) = -v(maxI, 2);
323 <                    }
355 >        // diagonalize using Jacobi
356 >        jacobi(a, w, v);
357 >        // if all the eigenvalues are the same, return identity matrix
358 >        if (w[0] == w[1] && w[0] == w[2] ) {
359 >              v = SquareMatrix3<Real>::identity();
360 >              return;
361 >        }
362  
363 <                    // re-orthogonalize the other two eigenvectors
364 <                    j = (maxI+1)%3;
365 <                    k = (maxI+2)%3;
366 <
367 <                    v(j, 0) = 0.0;
368 <                    v(j, 1) = 0.0;
369 <                    v(j, 2) = 0.0;
370 <                    v(j, j) = 1.0;
363 >        // transpose temporarily, it makes it easier to sort the eigenvectors
364 >        v = v.transpose();
365 >        
366 >        // if two eigenvalues are the same, re-orthogonalize to optimally line
367 >        // up the eigenvectors with the x, y, and z axes
368 >        for (i = 0; i < 3; i++) {
369 >            if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same
370 >            // find maximum element of the independant eigenvector
371 >            maxVal = fabs(v(i, 0));
372 >            maxI = 0;
373 >            for (j = 1; j < 3; j++) {
374 >                if (maxVal < (tmp = fabs(v(i, j)))){
375 >                    maxVal = tmp;
376 >                    maxI = j;
377 >                }
378 >            }
379 >            
380 >            // swap the eigenvector into its proper position
381 >            if (maxI != i) {
382 >                tmp = w(maxI);
383 >                w(maxI) = w(i);
384 >                w(i) = tmp;
385  
386 <                    /** @todo */
387 <                    v_maxI = v.getRow(maxI);
388 <                    v_j = v.getRow(j);
389 <                    v_k = cross(v_maxI, v_j);
390 <                    v_k.normailze();
391 <                    v_j = cross(v_k, v_maxI);
392 <                    v.setRow(j, v_j);
393 <                    v.setRow(k, v_k);
386 >                v.swapRow(i, maxI);
387 >            }
388 >            // maximum element of eigenvector should be positive
389 >            if (v(maxI, maxI) < 0) {
390 >                v(maxI, 0) = -v(maxI, 0);
391 >                v(maxI, 1) = -v(maxI, 1);
392 >                v(maxI, 2) = -v(maxI, 2);
393 >            }
394  
395 +            // re-orthogonalize the other two eigenvectors
396 +            j = (maxI+1)%3;
397 +            k = (maxI+2)%3;
398  
399 <                    // transpose vectors back to columns
400 <                    v = v.transpose();
401 <                    return;
402 <                    }
348 <                }
399 >            v(j, 0) = 0.0;
400 >            v(j, 1) = 0.0;
401 >            v(j, 2) = 0.0;
402 >            v(j, j) = 1.0;
403  
404 <                // the three eigenvalues are different, just sort the eigenvectors
405 <                // to align them with the x, y, and z axes
404 >            /** @todo */
405 >            v_maxI = v.getRow(maxI);
406 >            v_j = v.getRow(j);
407 >            v_k = cross(v_maxI, v_j);
408 >            v_k.normalize();
409 >            v_j = cross(v_k, v_maxI);
410 >            v.setRow(j, v_j);
411 >            v.setRow(k, v_k);
412  
353                // find the vector with the largest x element, make that vector
354                // the first vector
355                maxVal = fabs(v(0, 0));
356                maxI = 0;
357                for (i = 1; i < 3; i++) {
358                    if (maxVal < (tmp = fabs(v(i, 0)))) {
359                        maxVal = tmp;
360                        maxI = i;
361                    }
362                }
413  
414 <                // swap eigenvalue and eigenvector
415 <                if (maxI != 0) {
416 <                    tmp = w(maxI);
417 <                    w(maxI) = w(0);
418 <                    w(0) = tmp;
369 <                    v.swapRow(maxI, 0);
370 <                }
371 <                // do the same for the y element
372 <                if (fabs(v(1, 1)) < fabs(v(2, 1))) {
373 <                    tmp = w(2);
374 <                    w(2) = w(1);
375 <                    w(1) = tmp;
376 <                    v.swapRow(2, 1);
377 <                }
414 >            // transpose vectors back to columns
415 >            v = v.transpose();
416 >            return;
417 >            }
418 >        }
419  
420 <                // ensure that the sign of the eigenvectors is correct
421 <                for (i = 0; i < 2; i++) {
381 <                    if (v(i, i) < 0) {
382 <                        v(i, 0) = -v(i, 0);
383 <                        v(i, 1) = -v(i, 1);
384 <                        v(i, 2) = -v(i, 2);
385 <                    }
386 <                }
420 >        // the three eigenvalues are different, just sort the eigenvectors
421 >        // to align them with the x, y, and z axes
422  
423 <                // set sign of final eigenvector to ensure that determinant is positive
424 <                if (determinant(v) < 0) {
425 <                    v(2, 0) = -v(2, 0);
426 <                    v(2, 1) = -v(2, 1);
427 <                    v(2, 2) = -v(2, 2);
428 <                }
423 >        // find the vector with the largest x element, make that vector
424 >        // the first vector
425 >        maxVal = fabs(v(0, 0));
426 >        maxI = 0;
427 >        for (i = 1; i < 3; i++) {
428 >            if (maxVal < (tmp = fabs(v(i, 0)))) {
429 >                maxVal = tmp;
430 >                maxI = i;
431 >            }
432 >        }
433  
434 <                // transpose the eigenvectors back again
435 <                v = v.transpose();
436 <                return ;
434 >        // swap eigenvalue and eigenvector
435 >        if (maxI != 0) {
436 >            tmp = w(maxI);
437 >            w(maxI) = w(0);
438 >            w(0) = tmp;
439 >            v.swapRow(maxI, 0);
440 >        }
441 >        // do the same for the y element
442 >        if (fabs(v(1, 1)) < fabs(v(2, 1))) {
443 >            tmp = w(2);
444 >            w(2) = w(1);
445 >            w(1) = tmp;
446 >            v.swapRow(2, 1);
447 >        }
448 >
449 >        // ensure that the sign of the eigenvectors is correct
450 >        for (i = 0; i < 2; i++) {
451 >            if (v(i, i) < 0) {
452 >                v(i, 0) = -v(i, 0);
453 >                v(i, 1) = -v(i, 1);
454 >                v(i, 2) = -v(i, 2);
455              }
456 <    };
456 >        }
457  
458 +        // set sign of final eigenvector to ensure that determinant is positive
459 +        if (v.determinant() < 0) {
460 +            v(2, 0) = -v(2, 0);
461 +            v(2, 1) = -v(2, 1);
462 +            v(2, 2) = -v(2, 2);
463 +        }
464 +
465 +        // transpose the eigenvectors back again
466 +        v = v.transpose();
467 +        return ;
468 +    }
469 +
470 +    /**
471 +    * Return the multiplication of two matrixes  (m1 * m2).
472 +    * @return the multiplication of two matrixes
473 +    * @param m1 the first matrix
474 +    * @param m2 the second matrix
475 +    */
476 +    template<typename Real>
477 +    inline SquareMatrix3<Real> operator *(const SquareMatrix3<Real>& m1, const SquareMatrix3<Real>& m2) {
478 +        SquareMatrix3<Real> result;
479 +
480 +            for (unsigned int i = 0; i < 3; i++)
481 +                for (unsigned int j = 0; j < 3; j++)
482 +                    for (unsigned int k = 0; k < 3; k++)
483 +                        result(i, j)  += m1(i, k) * m2(k, j);                
484 +
485 +        return result;
486 +    }
487 +
488 +    template<typename Real>
489 +    inline SquareMatrix3<Real> outProduct(const Vector3<Real>& v1, const Vector3<Real>& v2) {
490 +        SquareMatrix3<Real> result;
491 +
492 +            for (unsigned int i = 0; i < 3; i++) {
493 +                for (unsigned int j = 0; j < 3; j++) {
494 +                        result(i, j)  = v1[i] * v2[j];                
495 +                }
496 +            }
497 +            
498 +        return result;        
499 +    }
500 +
501 +    
502      typedef SquareMatrix3<double> Mat3x3d;
503      typedef SquareMatrix3<double> RotMat3x3d;
504  
505   } //namespace oopse
506   #endif // MATH_SQUAREMATRIX_HPP
507 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines