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