| 29 |  | * @date 10/11/2004 | 
| 30 |  | * @version 1.0 | 
| 31 |  | */ | 
| 32 | < | #ifndef MATH_SQUAREMATRIX3_HPP | 
| 32 | > | #ifndef MATH_SQUAREMATRIX3_HPP | 
| 33 |  | #define  MATH_SQUAREMATRIX3_HPP | 
| 34 |  |  | 
| 35 |  | #include "Quaternion.hpp" | 
| 59 |  | } | 
| 60 |  |  | 
| 61 |  | SquareMatrix3(const Quaternion<Real>& q) { | 
| 62 | < | *this = q.toRotationMatrix3(); | 
| 62 | > | setupRotMat(q); | 
| 63 | > |  | 
| 64 |  | } | 
| 65 |  |  | 
| 66 |  | SquareMatrix3(Real w, Real x, Real y, Real z) { | 
| 67 | < | Quaternion<Real> q(w, x, y, z); | 
| 67 | < | *this = q.toRotationMatrix3(); | 
| 67 | > | setupRotMat(w, x, y, z); | 
| 68 |  | } | 
| 69 |  |  | 
| 70 |  | /** copy assignment operator */ | 
| 119 |  | * @param quat | 
| 120 |  | */ | 
| 121 |  | void setupRotMat(const Quaternion<Real>& quat) { | 
| 122 | < | *this = quat.toRotationMatrix3(); | 
| 122 | > | setupRotMat(quat.w(), quat.x(), quat.y(), quat.z()); | 
| 123 |  | } | 
| 124 |  |  | 
| 125 |  | /** | 
| 196 |  | * z-axis (again). | 
| 197 |  | */ | 
| 198 |  | Vector3<Real> toEulerAngles() { | 
| 199 | < | Vector<Real> myEuler; | 
| 199 | > | Vector3<Real> myEuler; | 
| 200 |  | Real phi,theta,psi,eps; | 
| 201 |  | Real ctheta,stheta; | 
| 202 |  |  | 
| 203 |  | // set the tolerance for Euler angles and rotation elements | 
| 204 |  |  | 
| 205 | < | theta = acos(min(1.0,max(-1.0,data_[2][2]))); | 
| 205 | > | theta = acos(std::min(1.0, std::max(-1.0,data_[2][2]))); | 
| 206 |  | ctheta = data_[2][2]; | 
| 207 |  | stheta = sqrt(1.0 - ctheta * ctheta); | 
| 208 |  |  | 
| 275 |  | m /= det; | 
| 276 |  | return m; | 
| 277 |  | } | 
| 278 | < |  | 
| 279 | < | void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v) { | 
| 280 | < | int i,j,k,maxI; | 
| 281 | < | Real tmp, maxVal; | 
| 282 | < | Vector3<Real> v_maxI, v_k, v_j; | 
| 278 | > | /** | 
| 279 | > | * Extract the eigenvalues and eigenvectors from a 3x3 matrix. | 
| 280 | > | * The eigenvectors (the columns of V) will be normalized. | 
| 281 | > | * The eigenvectors are aligned optimally with the x, y, and z | 
| 282 | > | * axes respectively. | 
| 283 | > | * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is | 
| 284 | > | *     overwritten | 
| 285 | > | * @param w will contain the eigenvalues of the matrix On return of this function | 
| 286 | > | * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are | 
| 287 | > | *    normalized and mutually orthogonal. | 
| 288 | > | * @warning a will be overwritten | 
| 289 | > | */ | 
| 290 | > | static void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v); | 
| 291 | > | }; | 
| 292 | > | /*========================================================================= | 
| 293 |  |  | 
| 294 | < | // diagonalize using Jacobi | 
| 295 | < | jacobi(a, w, v); | 
| 294 | > | Program:   Visualization Toolkit | 
| 295 | > | Module:    $RCSfile: SquareMatrix3.hpp,v $ | 
| 296 |  |  | 
| 297 | < | // if all the eigenvalues are the same, return identity matrix | 
| 298 | < | if (w[0] == w[1] && w[0] == w[2] ){ | 
| 299 | < | v = SquareMatrix3<Real>::identity(); | 
| 300 | < | return | 
| 297 | > | Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen | 
| 298 | > | All rights reserved. | 
| 299 | > | See Copyright.txt or http://www.kitware.com/Copyright.htm for details. | 
| 300 | > |  | 
| 301 | > | This software is distributed WITHOUT ANY WARRANTY; without even | 
| 302 | > | the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | 
| 303 | > | PURPOSE.  See the above copyright notice for more information. | 
| 304 | > |  | 
| 305 | > | =========================================================================*/ | 
| 306 | > | template<typename Real> | 
| 307 | > | void SquareMatrix3<Real>::diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, | 
| 308 | > | SquareMatrix3<Real>& v) { | 
| 309 | > | int i,j,k,maxI; | 
| 310 | > | Real tmp, maxVal; | 
| 311 | > | Vector3<Real> v_maxI, v_k, v_j; | 
| 312 | > |  | 
| 313 | > | // diagonalize using Jacobi | 
| 314 | > | jacobi(a, w, v); | 
| 315 | > | // if all the eigenvalues are the same, return identity matrix | 
| 316 | > | if (w[0] == w[1] && w[0] == w[2] ) { | 
| 317 | > | v = SquareMatrix3<Real>::identity(); | 
| 318 | > | return; | 
| 319 | > | } | 
| 320 | > |  | 
| 321 | > | // transpose temporarily, it makes it easier to sort the eigenvectors | 
| 322 | > | v = v.transpose(); | 
| 323 | > |  | 
| 324 | > | // if two eigenvalues are the same, re-orthogonalize to optimally line | 
| 325 | > | // up the eigenvectors with the x, y, and z axes | 
| 326 | > | for (i = 0; i < 3; i++) { | 
| 327 | > | if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same | 
| 328 | > | // find maximum element of the independant eigenvector | 
| 329 | > | maxVal = fabs(v(i, 0)); | 
| 330 | > | maxI = 0; | 
| 331 | > | for (j = 1; j < 3; j++) { | 
| 332 | > | if (maxVal < (tmp = fabs(v(i, j)))){ | 
| 333 | > | maxVal = tmp; | 
| 334 | > | maxI = j; | 
| 335 |  | } | 
| 336 | + | } | 
| 337 | + |  | 
| 338 | + | // swap the eigenvector into its proper position | 
| 339 | + | if (maxI != i) { | 
| 340 | + | tmp = w(maxI); | 
| 341 | + | w(maxI) = w(i); | 
| 342 | + | w(i) = tmp; | 
| 343 |  |  | 
| 344 | < | // transpose temporarily, it makes it easier to sort the eigenvectors | 
| 345 | < | v = v.tanspose(); | 
| 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 | 
| 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; | 
| 315 | < |  | 
| 316 | < | v.swapRow(i, maxI); | 
| 317 | < | } | 
| 318 | < | // maximum element of eigenvector should be positive | 
| 319 | < | if (v(maxI, maxI) < 0) { | 
| 320 | < | v(maxI, 0) = -v(maxI, 0); | 
| 321 | < | v(maxI, 1) = -v(maxI, 1); | 
| 322 | < | v(maxI, 2) = -v(maxI, 2); | 
| 323 | < | } | 
| 324 | < |  | 
| 325 | < | // re-orthogonalize the other two eigenvectors | 
| 326 | < | j = (maxI+1)%3; | 
| 327 | < | k = (maxI+2)%3; | 
| 328 | < |  | 
| 329 | < | v(j, 0) = 0.0; | 
| 330 | < | v(j, 1) = 0.0; | 
| 331 | < | v(j, 2) = 0.0; | 
| 332 | < | v(j, j) = 1.0; | 
| 333 | < |  | 
| 334 | < | /** @todo */ | 
| 335 | < | v_maxI = v.getRow(maxI); | 
| 336 | < | v_j = v.getRow(j); | 
| 337 | < | v_k = cross(v_maxI, v_j); | 
| 338 | < | v_k.normailze(); | 
| 339 | < | v_j = cross(v_k, v_maxI); | 
| 340 | < | v.setRow(j, v_j); | 
| 341 | < | v.setRow(k, v_k); | 
| 344 | > | v.swapRow(i, maxI); | 
| 345 | > | } | 
| 346 | > | // maximum element of eigenvector should be positive | 
| 347 | > | if (v(maxI, maxI) < 0) { | 
| 348 | > | v(maxI, 0) = -v(maxI, 0); | 
| 349 | > | v(maxI, 1) = -v(maxI, 1); | 
| 350 | > | v(maxI, 2) = -v(maxI, 2); | 
| 351 | > | } | 
| 352 |  |  | 
| 353 | + | // re-orthogonalize the other two eigenvectors | 
| 354 | + | j = (maxI+1)%3; | 
| 355 | + | k = (maxI+2)%3; | 
| 356 |  |  | 
| 357 | < | // transpose vectors back to columns | 
| 358 | < | v = v.transpose(); | 
| 359 | < | return; | 
| 360 | < | } | 
| 348 | < | } | 
| 357 | > | v(j, 0) = 0.0; | 
| 358 | > | v(j, 1) = 0.0; | 
| 359 | > | v(j, 2) = 0.0; | 
| 360 | > | v(j, j) = 1.0; | 
| 361 |  |  | 
| 362 | < | // the three eigenvalues are different, just sort the eigenvectors | 
| 363 | < | // to align them with the x, y, and z axes | 
| 362 | > | /** @todo */ | 
| 363 | > | v_maxI = v.getRow(maxI); | 
| 364 | > | v_j = v.getRow(j); | 
| 365 | > | v_k = cross(v_maxI, v_j); | 
| 366 | > | v_k.normalize(); | 
| 367 | > | v_j = cross(v_k, v_maxI); | 
| 368 | > | v.setRow(j, v_j); | 
| 369 | > | v.setRow(k, v_k); | 
| 370 |  |  | 
| 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 | – | } | 
| 371 |  |  | 
| 372 | < | // swap eigenvalue and eigenvector | 
| 373 | < | if (maxI != 0) { | 
| 374 | < | tmp = w(maxI); | 
| 375 | < | w(maxI) = w(0); | 
| 376 | < | 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 | < | } | 
| 372 | > | // transpose vectors back to columns | 
| 373 | > | v = v.transpose(); | 
| 374 | > | return; | 
| 375 | > | } | 
| 376 | > | } | 
| 377 |  |  | 
| 378 | < | // ensure that the sign of the eigenvectors is correct | 
| 379 | < | 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 | < | } | 
| 378 | > | // the three eigenvalues are different, just sort the eigenvectors | 
| 379 | > | // to align them with the x, y, and z axes | 
| 380 |  |  | 
| 381 | < | // set sign of final eigenvector to ensure that determinant is positive | 
| 382 | < | if (determinant(v) < 0) { | 
| 383 | < | v(2, 0) = -v(2, 0); | 
| 384 | < | v(2, 1) = -v(2, 1); | 
| 385 | < | v(2, 2) = -v(2, 2); | 
| 386 | < | } | 
| 381 | > | // find the vector with the largest x element, make that vector | 
| 382 | > | // the first vector | 
| 383 | > | maxVal = fabs(v(0, 0)); | 
| 384 | > | maxI = 0; | 
| 385 | > | for (i = 1; i < 3; i++) { | 
| 386 | > | if (maxVal < (tmp = fabs(v(i, 0)))) { | 
| 387 | > | maxVal = tmp; | 
| 388 | > | maxI = i; | 
| 389 | > | } | 
| 390 | > | } | 
| 391 |  |  | 
| 392 | < | // transpose the eigenvectors back again | 
| 393 | < | v = v.transpose(); | 
| 394 | < | return ; | 
| 392 | > | // swap eigenvalue and eigenvector | 
| 393 | > | if (maxI != 0) { | 
| 394 | > | tmp = w(maxI); | 
| 395 | > | w(maxI) = w(0); | 
| 396 | > | w(0) = tmp; | 
| 397 | > | v.swapRow(maxI, 0); | 
| 398 | > | } | 
| 399 | > | // do the same for the y element | 
| 400 | > | if (fabs(v(1, 1)) < fabs(v(2, 1))) { | 
| 401 | > | tmp = w(2); | 
| 402 | > | w(2) = w(1); | 
| 403 | > | w(1) = tmp; | 
| 404 | > | v.swapRow(2, 1); | 
| 405 | > | } | 
| 406 | > |  | 
| 407 | > | // ensure that the sign of the eigenvectors is correct | 
| 408 | > | for (i = 0; i < 2; i++) { | 
| 409 | > | if (v(i, i) < 0) { | 
| 410 | > | v(i, 0) = -v(i, 0); | 
| 411 | > | v(i, 1) = -v(i, 1); | 
| 412 | > | v(i, 2) = -v(i, 2); | 
| 413 |  | } | 
| 414 | < | }; | 
| 414 | > | } | 
| 415 |  |  | 
| 416 | + | // set sign of final eigenvector to ensure that determinant is positive | 
| 417 | + | if (v.determinant() < 0) { | 
| 418 | + | v(2, 0) = -v(2, 0); | 
| 419 | + | v(2, 1) = -v(2, 1); | 
| 420 | + | v(2, 2) = -v(2, 2); | 
| 421 | + | } | 
| 422 | + |  | 
| 423 | + | // transpose the eigenvectors back again | 
| 424 | + | v = v.transpose(); | 
| 425 | + | return ; | 
| 426 | + | } | 
| 427 |  | typedef SquareMatrix3<double> Mat3x3d; | 
| 428 |  | typedef SquareMatrix3<double> RotMat3x3d; | 
| 429 |  |  | 
| 430 |  | } //namespace oopse | 
| 431 |  | #endif // MATH_SQUAREMATRIX_HPP | 
| 432 | + |  |