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 1630 by tim, Thu Oct 21 21:31:39 2004 UTC vs.
Revision 2611 by tim, Mon Mar 13 22:42:40 2006 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.
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 <
50 > #include <vector>
51   #include "Quaternion.hpp"
52   #include "SquareMatrix.hpp"
53   #include "Vector3.hpp"
54 <
54 > #include "utils/NumericConstant.hpp"
55   namespace oopse {
56  
57 <    template<typename Real>
58 <    class SquareMatrix3 : public SquareMatrix<Real, 3> {
59 <        public:
57 >  template<typename Real>
58 >  class SquareMatrix3 : public SquareMatrix<Real, 3> {
59 >  public:
60  
61 <            typedef Real ElemType;
62 <            typedef Real* ElemPoinerType;
61 >    typedef Real ElemType;
62 >    typedef Real* ElemPoinerType;
63              
64 <            /** default constructor */
65 <            SquareMatrix3() : SquareMatrix<Real, 3>() {
66 <            }
64 >    /** default constructor */
65 >    SquareMatrix3() : SquareMatrix<Real, 3>() {
66 >    }
67  
68 <            /** copy  constructor */
69 <            SquareMatrix3(const SquareMatrix<Real, 3>& m)  : SquareMatrix<Real, 3>(m) {
70 <            }
68 >    /** Constructs and initializes every element of this matrix to a scalar */
69 >    SquareMatrix3(Real s) : SquareMatrix<Real,3>(s){
70 >    }
71  
72 <            SquareMatrix3( const Vector3<Real>& eulerAngles) {
73 <                setupRotMat(eulerAngles);
74 <            }
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              
81 <            SquareMatrix3(Real phi, Real theta, Real psi) {
82 <                setupRotMat(phi, theta, psi);
83 <            }
81 >    SquareMatrix3( const Vector3<Real>& eulerAngles) {
82 >      setupRotMat(eulerAngles);
83 >    }
84 >            
85 >    SquareMatrix3(Real phi, Real theta, Real psi) {
86 >      setupRotMat(phi, theta, psi);
87 >    }
88  
89 <            SquareMatrix3(const Quaternion<Real>& q) {
90 <                setupRotMat(q);
89 >    SquareMatrix3(const Quaternion<Real>& q) {
90 >      setupRotMat(q);
91  
92 <            }
92 >    }
93  
94 <            SquareMatrix3(Real w, Real x, Real y, Real z) {
95 <                setupRotMat(w, x, y, z);
96 <            }
94 >    SquareMatrix3(Real w, Real x, Real y, Real z) {
95 >      setupRotMat(w, x, y, z);
96 >    }
97              
98 <            /** 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 <            }
98 >    /** 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  
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            }
106  
107 <            /**
108 <             * Sets this matrix to a rotation matrix by three euler angles
109 <             * @param phi
110 <             * @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;
107 >    SquareMatrix3<Real>& operator =(const Quaternion<Real>& q) {
108 >      this->setupRotMat(q);
109 >      return *this;
110 >    }
111  
112 <                sphi = sin(phi);
113 <                stheta = sin(theta);
114 <                spsi = sin(psi);
115 <                cphi = cos(phi);
116 <                ctheta = cos(theta);
117 <                cpsi = cos(psi);
112 >    /**
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  
120 <                data_[0][0] = cpsi * cphi - ctheta * sphi * spsi;
121 <                data_[0][1] = cpsi * sphi + ctheta * cphi * spsi;
122 <                data_[0][2] = spsi * stheta;
123 <                
124 <                data_[1][0] = -spsi * ctheta - ctheta * sphi * cpsi;
125 <                data_[1][1] = -spsi * stheta + ctheta * cphi * cpsi;
126 <                data_[1][2] = cpsi * stheta;
120 >    /**
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  
130 <                data_[2][0] = stheta * sphi;
131 <                data_[2][1] = -stheta * cphi;
132 <                data_[2][2] = ctheta;
133 <            }
130 >      sphi = sin(phi);
131 >      stheta = sin(theta);
132 >      spsi = sin(psi);
133 >      cphi = cos(phi);
134 >      ctheta = cos(theta);
135 >      cpsi = cos(psi);
136  
137 +      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 +                
141 +      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  
145 <            /**
146 <             * Sets this matrix to a rotation matrix by quaternion
147 <             * @param quat
148 <            */
124 <            void setupRotMat(const Quaternion<Real>& quat) {
125 <                setupRotMat(quat.w(), quat.x(), quat.y(), quat.z());
126 <            }
145 >      this->data_[2][0] = stheta * sphi;
146 >      this->data_[2][1] = -stheta * cphi;
147 >      this->data_[2][2] = ctheta;
148 >    }
149  
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            }
150  
151 <            /**
152 <             * Returns the quaternion from this rotation matrix
153 <             * @return the quaternion from this rotation matrix
154 <             * @exception invalid rotation matrix
155 <            */            
156 <            Quaternion<Real> toQuaternion() {
157 <                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 ){
151 >    /**
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  
159 <                    s = 0.5 / sqrt( t );
160 <                    q[0] = 0.25 / s;
161 <                    q[1] = (data_[1][2] - data_[2][1]) * s;
162 <                    q[2] = (data_[2][0] - data_[0][2]) * s;
163 <                    q[3] = (data_[0][1] - data_[1][0]) * s;
164 <                } else {
159 >    /**
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  
171 <                    ad1 = fabs( data_[0][0] );
172 <                    ad2 = fabs( data_[1][1] );
173 <                    ad3 = fabs( data_[2][2] );
171 >    void setupSkewMat(Vector3<Real> v) {
172 >        setupSkewMat(v[0], v[1], v[2]);
173 >    }
174  
175 <                    if( ad1 >= ad2 && ad1 >= ad3 ){
175 >    void setupSkewMat(Real v1, Real v2, Real v3) {
176 >        this->data_[0][0] = 0;
177 >        this->data_[0][1] = -v3;
178 >        this->data_[0][2] = v2;
179 >        this->data_[1][0] = v3;
180 >        this->data_[1][1] = 0;
181 >        this->data_[1][2] = -v1;
182 >        this->data_[2][0] = -v2;
183 >        this->data_[2][1] = v1;
184 >        this->data_[2][2] = 0;
185 >        
186 >        
187 >    }
188  
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 {
189  
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                }            
190  
191 <                return q;
192 <                
193 <            }
191 >    /**
192 >     * Returns the quaternion from this rotation matrix
193 >     * @return the quaternion from this rotation matrix
194 >     * @exception invalid rotation matrix
195 >     */            
196 >    Quaternion<Real> toQuaternion() {
197 >      Quaternion<Real> q;
198 >      Real t, s;
199 >      Real ad1, ad2, ad3;    
200 >      t = this->data_[0][0] + this->data_[1][1] + this->data_[2][2] + 1.0;
201  
202 <            /**
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
202 >      if( t > NumericConstant::epsilon ){
203  
204 <                theta = acos(std::min(1.0, std::max(-1.0,data_[2][2])));
205 <                ctheta = data_[2][2];
206 <                stheta = sqrt(1.0 - ctheta * ctheta);
204 >        s = 0.5 / sqrt( t );
205 >        q[0] = 0.25 / s;
206 >        q[1] = (this->data_[1][2] - this->data_[2][1]) * s;
207 >        q[2] = (this->data_[2][0] - this->data_[0][2]) * s;
208 >        q[3] = (this->data_[0][1] - this->data_[1][0]) * s;
209 >      } else {
210  
211 <                // when sin(theta) is close to 0, we need to consider singularity
212 <                // In this case, we can assign an arbitary value to phi (or psi), and then determine
213 <                // 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.
211 >        ad1 = this->data_[0][0];
212 >        ad2 = this->data_[1][1];
213 >        ad3 = this->data_[2][2];
214  
215 <                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 <                }
215 >        if( ad1 >= ad2 && ad1 >= ad3 ){
216  
217 <                //wrap phi and psi, make sure they are in the range from 0 to 2*Pi
218 <                if (phi < 0)
219 <                  phi += M_PI;
217 >          s = 0.5 / sqrt( 1.0 + this->data_[0][0] - this->data_[1][1] - this->data_[2][2] );
218 >          q[0] = (this->data_[1][2] - this->data_[2][1]) * s;
219 >          q[1] = 0.25 / s;
220 >          q[2] = (this->data_[0][1] + this->data_[1][0]) * s;
221 >          q[3] = (this->data_[0][2] + this->data_[2][0]) * s;
222 >        } else if ( ad2 >= ad1 && ad2 >= ad3 ) {
223 >          s = 0.5 / sqrt( 1.0 + this->data_[1][1] - this->data_[0][0] - this->data_[2][2] );
224 >          q[0] = (this->data_[2][0] - this->data_[0][2] ) * s;
225 >          q[1] = (this->data_[0][1] + this->data_[1][0]) * s;
226 >          q[2] = 0.25 / s;
227 >          q[3] = (this->data_[1][2] + this->data_[2][1]) * s;
228 >        } else {
229  
230 <                if (psi < 0)
231 <                  psi += M_PI;
230 >          s = 0.5 / sqrt( 1.0 + this->data_[2][2] - this->data_[0][0] - this->data_[1][1] );
231 >          q[0] = (this->data_[0][1] - this->data_[1][0]) * s;
232 >          q[1] = (this->data_[0][2] + this->data_[2][0]) * s;
233 >          q[2] = (this->data_[1][2] + this->data_[2][1]) * s;
234 >          q[3] = 0.25 / s;
235 >        }
236 >      }            
237  
238 <                myEuler[0] = phi;
239 <                myEuler[1] = theta;
240 <                myEuler[2] = psi;
238 >      return q;
239 >                
240 >    }
241  
242 <                return myEuler;
243 <            }
244 <            
245 <            /** Returns the determinant of this matrix. */
246 <            Real determinant() const {
247 <                Real x,y,z;
242 >    /**
243 >     * Returns the euler angles from this rotation matrix
244 >     * @return the euler angles in a vector
245 >     * @exception invalid rotation matrix
246 >     * We use so-called "x-convention", which is the most common definition.
247 >     * In this convention, the rotation given by Euler angles (phi, theta, psi), where the first
248 >     * rotation is by an angle phi about the z-axis, the second is by an angle  
249 >     * theta (0 <= theta <= 180)about the x-axis, and thethird is by an angle psi about the
250 >     * z-axis (again).
251 >     */            
252 >    Vector3<Real> toEulerAngles() {
253 >      Vector3<Real> myEuler;
254 >      Real phi;
255 >      Real theta;
256 >      Real psi;
257 >      Real ctheta;
258 >      Real stheta;
259 >                
260 >      // set the tolerance for Euler angles and rotation elements
261  
262 <                x = data_[0][0] * (data_[1][1] * data_[2][2] - data_[1][2] * data_[2][1]);
263 <                y = data_[0][1] * (data_[1][2] * data_[2][0] - data_[1][0] * data_[2][2]);
264 <                z = data_[0][2] * (data_[1][0] * data_[2][1] - data_[1][1] * data_[2][0]);
262 >      theta = acos(std::min(1.0, std::max(-1.0,this->data_[2][2])));
263 >      ctheta = this->data_[2][2];
264 >      stheta = sqrt(1.0 - ctheta * ctheta);
265  
266 <                return(x + y + z);
267 <            }            
266 >      // when sin(theta) is close to 0, we need to consider singularity
267 >      // In this case, we can assign an arbitary value to phi (or psi), and then determine
268 >      // the psi (or phi) or vice-versa. We'll assume that phi always gets the rotation, and psi is 0
269 >      // in cases of singularity.  
270 >      // we use atan2 instead of atan, since atan2 will give us -Pi to Pi.
271 >      // Since 0 <= theta <= 180, sin(theta) will be always non-negative. Therefore, it never
272 >      // change the sign of both of the parameters passed to atan2.
273 >
274 >      if (fabs(stheta) <= oopse::epsilon){
275 >        psi = 0.0;
276 >        phi = atan2(-this->data_[1][0], this->data_[0][0]);  
277 >      }
278 >      // we only have one unique solution
279 >      else{    
280 >        phi = atan2(this->data_[2][0], -this->data_[2][1]);
281 >        psi = atan2(this->data_[0][2], this->data_[1][2]);
282 >      }
283 >
284 >      //wrap phi and psi, make sure they are in the range from 0 to 2*Pi
285 >      if (phi < 0)
286 >        phi += M_PI;
287 >
288 >      if (psi < 0)
289 >        psi += M_PI;
290 >
291 >      myEuler[0] = phi;
292 >      myEuler[1] = theta;
293 >      myEuler[2] = psi;
294 >
295 >      return myEuler;
296 >    }
297              
298 <            /**
299 <             * Sets the value of this matrix to  the inversion of itself.
300 <             * @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 <            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 <                }
298 >    /** Returns the determinant of this matrix. */
299 >    Real determinant() const {
300 >      Real x,y,z;
301  
302 <                m(0, 0) = data_[1][1]*data_[2][2] - data_[1][2]*data_[2][1];
303 <                m(1, 0) = data_[1][2]*data_[2][0] - data_[1][0]*data_[2][2];
304 <                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];
302 >      x = this->data_[0][0] * (this->data_[1][1] * this->data_[2][2] - this->data_[1][2] * this->data_[2][1]);
303 >      y = this->data_[0][1] * (this->data_[1][2] * this->data_[2][0] - this->data_[1][0] * this->data_[2][2]);
304 >      z = this->data_[0][2] * (this->data_[1][0] * this->data_[2][1] - this->data_[1][1] * this->data_[2][0]);
305  
306 <                m /= det;
307 <                return m;
306 >      return(x + y + z);
307 >    }            
308 >
309 >    /** Returns the trace of this matrix. */
310 >    Real trace() const {
311 >      return this->data_[0][0] + this->data_[1][1] + this->data_[2][2];
312 >    }
313 >            
314 >    /**
315 >     * Sets the value of this matrix to  the inversion of itself.
316 >     * @note since simple algorithm can be applied to inverse the 3 by 3 matrix, we hide the
317 >     * implementation of inverse in SquareMatrix class
318 >     */
319 >    SquareMatrix3<Real>  inverse() const {
320 >      SquareMatrix3<Real> m;
321 >      double det = determinant();
322 >      if (fabs(det) <= oopse::epsilon) {
323 >        //"The method was called on a matrix with |determinant| <= 1e-6.",
324 >        //"This is a runtime or a programming error in your application.");
325 >        std::vector<int> zeroDiagElementIndex;
326 >        for (int i =0; i < 3; ++i) {
327 >            if (fabs(this->data_[i][i]) <= oopse::epsilon) {
328 >                zeroDiagElementIndex.push_back(i);
329              }
330 <            /**
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 < /*=========================================================================
330 >        }
331  
332 +        if (zeroDiagElementIndex.size() == 2) {
333 +            int index = zeroDiagElementIndex[0];
334 +            m(index, index) = 1.0 / this->data_[index][index];
335 +        }else if (zeroDiagElementIndex.size() == 1) {
336 +
337 +            int a = (zeroDiagElementIndex[0] + 1) % 3;
338 +            int b = (zeroDiagElementIndex[0] + 2) %3;
339 +            double denom = this->data_[a][a] * this->data_[b][b] - this->data_[b][a]*this->data_[a][b];
340 +            m(a, a) = this->data_[b][b] /denom;
341 +            m(b, a) = -this->data_[b][a]/denom;
342 +
343 +            m(a,b) = -this->data_[a][b]/denom;
344 +            m(b, b) = this->data_[a][a]/denom;
345 +                
346 +        }
347 +      
348 + /*
349 +        for(std::vector<int>::iterator iter = zeroDiagElementIndex.begin(); iter != zeroDiagElementIndex.end() ++iter) {
350 +            if (this->data_[*iter][0] > oopse::epsilon || this->data_[*iter][1] ||this->data_[*iter][2] ||
351 +                this->data_[0][*iter] > oopse::epsilon || this->data_[1][*iter] ||this->data_[2][*iter] ) {
352 +                std::cout << "can not inverse matrix" << std::endl;
353 +            }
354 +        }
355 + */
356 +      } else {
357 +
358 +          m(0, 0) = this->data_[1][1]*this->data_[2][2] - this->data_[1][2]*this->data_[2][1];
359 +          m(1, 0) = this->data_[1][2]*this->data_[2][0] - this->data_[1][0]*this->data_[2][2];
360 +          m(2, 0) = this->data_[1][0]*this->data_[2][1] - this->data_[1][1]*this->data_[2][0];
361 +          m(0, 1) = this->data_[2][1]*this->data_[0][2] - this->data_[2][2]*this->data_[0][1];
362 +          m(1, 1) = this->data_[2][2]*this->data_[0][0] - this->data_[2][0]*this->data_[0][2];
363 +          m(2, 1) = this->data_[2][0]*this->data_[0][1] - this->data_[2][1]*this->data_[0][0];
364 +          m(0, 2) = this->data_[0][1]*this->data_[1][2] - this->data_[0][2]*this->data_[1][1];
365 +          m(1, 2) = this->data_[0][2]*this->data_[1][0] - this->data_[0][0]*this->data_[1][2];
366 +          m(2, 2) = this->data_[0][0]*this->data_[1][1] - this->data_[0][1]*this->data_[1][0];
367 +
368 +          m /= det;
369 +        }
370 +      return m;
371 +    }
372 +
373 +    SquareMatrix3<Real> transpose() const{
374 +      SquareMatrix3<Real> result;
375 +                
376 +      for (unsigned int i = 0; i < 3; i++)
377 +        for (unsigned int j = 0; j < 3; j++)              
378 +          result(j, i) = this->data_[i][j];
379 +
380 +      return result;
381 +    }
382 +    /**
383 +     * Extract the eigenvalues and eigenvectors from a 3x3 matrix.
384 +     * The eigenvectors (the columns of V) will be normalized.
385 +     * The eigenvectors are aligned optimally with the x, y, and z
386 +     * axes respectively.
387 +     * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is
388 +     *     overwritten            
389 +     * @param w will contain the eigenvalues of the matrix On return of this function
390 +     * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are
391 +     *    normalized and mutually orthogonal.              
392 +     * @warning a will be overwritten
393 +     */
394 +    static void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v);
395 +  };
396 +  /*=========================================================================
397 +
398    Program:   Visualization Toolkit
399    Module:    $RCSfile: SquareMatrix3.hpp,v $
400  
# Line 301 | Line 402 | namespace oopse {
402    All rights reserved.
403    See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
404  
405 <     This software is distributed WITHOUT ANY WARRANTY; without even
406 <     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
407 <     PURPOSE.  See the above copyright notice for more information.
405 >  This software is distributed WITHOUT ANY WARRANTY; without even
406 >  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
407 >  PURPOSE.  See the above copyright notice for more information.
408  
409 < =========================================================================*/
410 <    template<typename Real>
411 <    void SquareMatrix3<Real>::diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w,
412 <                                                                           SquareMatrix3<Real>& v) {
413 <        int i,j,k,maxI;
414 <        Real tmp, maxVal;
415 <        Vector3<Real> v_maxI, v_k, v_j;
409 >  =========================================================================*/
410 >  template<typename Real>
411 >  void SquareMatrix3<Real>::diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w,
412 >                                        SquareMatrix3<Real>& v) {
413 >    int i,j,k,maxI;
414 >    Real tmp, maxVal;
415 >    Vector3<Real> v_maxI, v_k, v_j;
416  
417 <        // diagonalize using Jacobi
418 <        jacobi(a, w, v);
419 <        // if all the eigenvalues are the same, return identity matrix
420 <        if (w[0] == w[1] && w[0] == w[2] ) {
421 <              v = SquareMatrix3<Real>::identity();
422 <              return;
423 <        }
417 >    // diagonalize using Jacobi
418 >    jacobi(a, w, v);
419 >    // if all the eigenvalues are the same, return identity matrix
420 >    if (w[0] == w[1] && w[0] == w[2] ) {
421 >      v = SquareMatrix3<Real>::identity();
422 >      return;
423 >    }
424  
425 <        // transpose temporarily, it makes it easier to sort the eigenvectors
426 <        v = v.transpose();
425 >    // transpose temporarily, it makes it easier to sort the eigenvectors
426 >    v = v.transpose();
427          
428 <        // if two eigenvalues are the same, re-orthogonalize to optimally line
429 <        // up the eigenvectors with the x, y, and z axes
430 <        for (i = 0; i < 3; i++) {
431 <            if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same
432 <            // find maximum element of the independant eigenvector
433 <            maxVal = fabs(v(i, 0));
434 <            maxI = 0;
435 <            for (j = 1; j < 3; j++) {
436 <                if (maxVal < (tmp = fabs(v(i, j)))){
437 <                    maxVal = tmp;
438 <                    maxI = j;
439 <                }
440 <            }
428 >    // if two eigenvalues are the same, re-orthogonalize to optimally line
429 >    // up the eigenvectors with the x, y, and z axes
430 >    for (i = 0; i < 3; i++) {
431 >      if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same
432 >        // find maximum element of the independant eigenvector
433 >        maxVal = fabs(v(i, 0));
434 >        maxI = 0;
435 >        for (j = 1; j < 3; j++) {
436 >          if (maxVal < (tmp = fabs(v(i, j)))){
437 >            maxVal = tmp;
438 >            maxI = j;
439 >          }
440 >        }
441              
442 <            // swap the eigenvector into its proper position
443 <            if (maxI != i) {
444 <                tmp = w(maxI);
445 <                w(maxI) = w(i);
446 <                w(i) = tmp;
442 >        // swap the eigenvector into its proper position
443 >        if (maxI != i) {
444 >          tmp = w(maxI);
445 >          w(maxI) = w(i);
446 >          w(i) = tmp;
447  
448 <                v.swapRow(i, maxI);
449 <            }
450 <            // maximum element of eigenvector should be positive
451 <            if (v(maxI, maxI) < 0) {
452 <                v(maxI, 0) = -v(maxI, 0);
453 <                v(maxI, 1) = -v(maxI, 1);
454 <                v(maxI, 2) = -v(maxI, 2);
455 <            }
448 >          v.swapRow(i, maxI);
449 >        }
450 >        // maximum element of eigenvector should be positive
451 >        if (v(maxI, maxI) < 0) {
452 >          v(maxI, 0) = -v(maxI, 0);
453 >          v(maxI, 1) = -v(maxI, 1);
454 >          v(maxI, 2) = -v(maxI, 2);
455 >        }
456  
457 <            // re-orthogonalize the other two eigenvectors
458 <            j = (maxI+1)%3;
459 <            k = (maxI+2)%3;
457 >        // re-orthogonalize the other two eigenvectors
458 >        j = (maxI+1)%3;
459 >        k = (maxI+2)%3;
460  
461 <            v(j, 0) = 0.0;
462 <            v(j, 1) = 0.0;
463 <            v(j, 2) = 0.0;
464 <            v(j, j) = 1.0;
461 >        v(j, 0) = 0.0;
462 >        v(j, 1) = 0.0;
463 >        v(j, 2) = 0.0;
464 >        v(j, j) = 1.0;
465  
466 <            /** @todo */
467 <            v_maxI = v.getRow(maxI);
468 <            v_j = v.getRow(j);
469 <            v_k = cross(v_maxI, v_j);
470 <            v_k.normalize();
471 <            v_j = cross(v_k, v_maxI);
472 <            v.setRow(j, v_j);
473 <            v.setRow(k, v_k);
466 >        /** @todo */
467 >        v_maxI = v.getRow(maxI);
468 >        v_j = v.getRow(j);
469 >        v_k = cross(v_maxI, v_j);
470 >        v_k.normalize();
471 >        v_j = cross(v_k, v_maxI);
472 >        v.setRow(j, v_j);
473 >        v.setRow(k, v_k);
474  
475  
476 <            // transpose vectors back to columns
477 <            v = v.transpose();
478 <            return;
479 <            }
480 <        }
476 >        // transpose vectors back to columns
477 >        v = v.transpose();
478 >        return;
479 >      }
480 >    }
481  
482 <        // the three eigenvalues are different, just sort the eigenvectors
483 <        // to align them with the x, y, and z axes
482 >    // the three eigenvalues are different, just sort the eigenvectors
483 >    // to align them with the x, y, and z axes
484  
485 <        // find the vector with the largest x element, make that vector
486 <        // the first vector
487 <        maxVal = fabs(v(0, 0));
488 <        maxI = 0;
489 <        for (i = 1; i < 3; i++) {
490 <            if (maxVal < (tmp = fabs(v(i, 0)))) {
491 <                maxVal = tmp;
492 <                maxI = i;
493 <            }
494 <        }
485 >    // find the vector with the largest x element, make that vector
486 >    // the first vector
487 >    maxVal = fabs(v(0, 0));
488 >    maxI = 0;
489 >    for (i = 1; i < 3; i++) {
490 >      if (maxVal < (tmp = fabs(v(i, 0)))) {
491 >        maxVal = tmp;
492 >        maxI = i;
493 >      }
494 >    }
495  
496 <        // swap eigenvalue and eigenvector
497 <        if (maxI != 0) {
498 <            tmp = w(maxI);
499 <            w(maxI) = w(0);
500 <            w(0) = tmp;
501 <            v.swapRow(maxI, 0);
502 <        }
503 <        // do the same for the y element
504 <        if (fabs(v(1, 1)) < fabs(v(2, 1))) {
505 <            tmp = w(2);
506 <            w(2) = w(1);
507 <            w(1) = tmp;
508 <            v.swapRow(2, 1);
509 <        }
496 >    // swap eigenvalue and eigenvector
497 >    if (maxI != 0) {
498 >      tmp = w(maxI);
499 >      w(maxI) = w(0);
500 >      w(0) = tmp;
501 >      v.swapRow(maxI, 0);
502 >    }
503 >    // do the same for the y element
504 >    if (fabs(v(1, 1)) < fabs(v(2, 1))) {
505 >      tmp = w(2);
506 >      w(2) = w(1);
507 >      w(1) = tmp;
508 >      v.swapRow(2, 1);
509 >    }
510  
511 <        // ensure that the sign of the eigenvectors is correct
512 <        for (i = 0; i < 2; i++) {
513 <            if (v(i, i) < 0) {
514 <                v(i, 0) = -v(i, 0);
515 <                v(i, 1) = -v(i, 1);
516 <                v(i, 2) = -v(i, 2);
517 <            }
518 <        }
511 >    // ensure that the sign of the eigenvectors is correct
512 >    for (i = 0; i < 2; i++) {
513 >      if (v(i, i) < 0) {
514 >        v(i, 0) = -v(i, 0);
515 >        v(i, 1) = -v(i, 1);
516 >        v(i, 2) = -v(i, 2);
517 >      }
518 >    }
519  
520 <        // set sign of final eigenvector to ensure that determinant is positive
521 <        if (v.determinant() < 0) {
522 <            v(2, 0) = -v(2, 0);
523 <            v(2, 1) = -v(2, 1);
524 <            v(2, 2) = -v(2, 2);
525 <        }
520 >    // set sign of final eigenvector to ensure that determinant is positive
521 >    if (v.determinant() < 0) {
522 >      v(2, 0) = -v(2, 0);
523 >      v(2, 1) = -v(2, 1);
524 >      v(2, 2) = -v(2, 2);
525 >    }
526  
527 <        // transpose the eigenvectors back again
528 <        v = v.transpose();
529 <        return ;
527 >    // transpose the eigenvectors back again
528 >    v = v.transpose();
529 >    return ;
530 >  }
531 >
532 >  /**
533 >   * Return the multiplication of two matrixes  (m1 * m2).
534 >   * @return the multiplication of two matrixes
535 >   * @param m1 the first matrix
536 >   * @param m2 the second matrix
537 >   */
538 >  template<typename Real>
539 >  inline SquareMatrix3<Real> operator *(const SquareMatrix3<Real>& m1, const SquareMatrix3<Real>& m2) {
540 >    SquareMatrix3<Real> result;
541 >
542 >    for (unsigned int i = 0; i < 3; i++)
543 >      for (unsigned int j = 0; j < 3; j++)
544 >        for (unsigned int k = 0; k < 3; k++)
545 >          result(i, j)  += m1(i, k) * m2(k, j);                
546 >
547 >    return result;
548 >  }
549 >
550 >  template<typename Real>
551 >  inline SquareMatrix3<Real> outProduct(const Vector3<Real>& v1, const Vector3<Real>& v2) {
552 >    SquareMatrix3<Real> result;
553 >
554 >    for (unsigned int i = 0; i < 3; i++) {
555 >      for (unsigned int j = 0; j < 3; j++) {
556 >        result(i, j)  = v1[i] * v2[j];                
557 >      }
558      }
559 <    typedef SquareMatrix3<double> Mat3x3d;
560 <    typedef SquareMatrix3<double> RotMat3x3d;
559 >            
560 >    return result;        
561 >  }
562  
563 +    
564 +  typedef SquareMatrix3<double> Mat3x3d;
565 +  typedef SquareMatrix3<double> RotMat3x3d;
566 +
567   } //namespace oopse
568   #endif // MATH_SQUAREMATRIX_HPP
569  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines