# | Line 29 | Line 29 | |
---|---|---|
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" | |
# | Line 41 | Line 41 | namespace oopse { | |
41 | template<typename Real> | |
42 | class SquareMatrix3 : public SquareMatrix<Real, 3> { | |
43 | public: | |
44 | + | |
45 | + | typedef Real ElemType; |
46 | + | typedef Real* ElemPoinerType; |
47 | ||
48 | /** default constructor */ | |
49 | SquareMatrix3() : SquareMatrix<Real, 3>() { | |
# | Line 275 | Line 278 | namespace oopse { | |
278 | m /= det; | |
279 | return m; | |
280 | } | |
281 | < | |
282 | < | void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v) { |
283 | < | int i,j,k,maxI; |
284 | < | Real tmp, maxVal; |
285 | < | Vector3<Real> v_maxI, v_k, v_j; |
286 | < | |
287 | < | // diagonalize using Jacobi |
288 | < | jacobi(a, w, v); |
281 | > | /** |
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 | > | /*========================================================================= |
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; |
301 | < | } |
297 | > | Program: Visualization Toolkit |
298 | > | Module: $RCSfile: SquareMatrix3.hpp,v $ |
299 | > | |
300 | > | Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen |
301 | > | All rights reserved. |
302 | > | See Copyright.txt or http://www.kitware.com/Copyright.htm for details. |
303 | ||
304 | < | // transpose temporarily, it makes it easier to sort the eigenvectors |
305 | < | v = v.transpose(); |
306 | < | |
307 | < | // if two eigenvalues are the same, re-orthogonalize to optimally line |
308 | < | // up the eigenvectors with the x, y, and z axes |
309 | < | for (i = 0; i < 3; i++) { |
310 | < | if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same |
311 | < | // find maximum element of the independant eigenvector |
312 | < | maxVal = fabs(v(i, 0)); |
313 | < | maxI = 0; |
314 | < | 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; |
304 | > | This software is distributed WITHOUT ANY WARRANTY; without even |
305 | > | the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
306 | > | PURPOSE. See the above copyright notice for more information. |
307 | > | |
308 | > | =========================================================================*/ |
309 | > | template<typename Real> |
310 | > | void SquareMatrix3<Real>::diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, |
311 | > | SquareMatrix3<Real>& v) { |
312 | > | int i,j,k,maxI; |
313 | > | Real tmp, maxVal; |
314 | > | Vector3<Real> v_maxI, v_k, v_j; |
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 | < | } |
316 | > | // diagonalize using Jacobi |
317 | > | jacobi(a, w, v); |
318 | > | // if all the eigenvalues are the same, return identity matrix |
319 | > | if (w[0] == w[1] && w[0] == w[2] ) { |
320 | > | v = SquareMatrix3<Real>::identity(); |
321 | > | return; |
322 | > | } |
323 | ||
324 | < | // re-orthogonalize the other two eigenvectors |
325 | < | j = (maxI+1)%3; |
326 | < | k = (maxI+2)%3; |
324 | > | // transpose temporarily, it makes it easier to sort the eigenvectors |
325 | > | v = v.transpose(); |
326 | > | |
327 | > | // if two eigenvalues are the same, re-orthogonalize to optimally line |
328 | > | // up the eigenvectors with the x, y, and z axes |
329 | > | for (i = 0; i < 3; i++) { |
330 | > | if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same |
331 | > | // find maximum element of the independant eigenvector |
332 | > | maxVal = fabs(v(i, 0)); |
333 | > | maxI = 0; |
334 | > | for (j = 1; j < 3; j++) { |
335 | > | if (maxVal < (tmp = fabs(v(i, j)))){ |
336 | > | maxVal = tmp; |
337 | > | maxI = j; |
338 | > | } |
339 | > | } |
340 | > | |
341 | > | // swap the eigenvector into its proper position |
342 | > | if (maxI != i) { |
343 | > | tmp = w(maxI); |
344 | > | w(maxI) = w(i); |
345 | > | w(i) = tmp; |
346 | ||
347 | < | v(j, 0) = 0.0; |
348 | < | v(j, 1) = 0.0; |
349 | < | v(j, 2) = 0.0; |
350 | < | v(j, j) = 1.0; |
347 | > | v.swapRow(i, maxI); |
348 | > | } |
349 | > | // maximum element of eigenvector should be positive |
350 | > | if (v(maxI, maxI) < 0) { |
351 | > | v(maxI, 0) = -v(maxI, 0); |
352 | > | v(maxI, 1) = -v(maxI, 1); |
353 | > | v(maxI, 2) = -v(maxI, 2); |
354 | > | } |
355 | ||
356 | < | /** @todo */ |
357 | < | v_maxI = v.getRow(maxI); |
358 | < | v_j = v.getRow(j); |
337 | < | v_k = cross(v_maxI, v_j); |
338 | < | v_k.normalize(); |
339 | < | v_j = cross(v_k, v_maxI); |
340 | < | v.setRow(j, v_j); |
341 | < | v.setRow(k, v_k); |
356 | > | // re-orthogonalize the other two eigenvectors |
357 | > | j = (maxI+1)%3; |
358 | > | k = (maxI+2)%3; |
359 | ||
360 | + | v(j, 0) = 0.0; |
361 | + | v(j, 1) = 0.0; |
362 | + | v(j, 2) = 0.0; |
363 | + | v(j, j) = 1.0; |
364 | ||
365 | < | // transpose vectors back to columns |
366 | < | v = v.transpose(); |
367 | < | return; |
368 | < | } |
369 | < | } |
365 | > | /** @todo */ |
366 | > | v_maxI = v.getRow(maxI); |
367 | > | v_j = v.getRow(j); |
368 | > | v_k = cross(v_maxI, v_j); |
369 | > | v_k.normalize(); |
370 | > | v_j = cross(v_k, v_maxI); |
371 | > | v.setRow(j, v_j); |
372 | > | v.setRow(k, v_k); |
373 | ||
350 | – | // the three eigenvalues are different, just sort the eigenvectors |
351 | – | // to align them with the x, y, and z axes |
374 | ||
375 | < | // find the vector with the largest x element, make that vector |
376 | < | // the first vector |
377 | < | maxVal = fabs(v(0, 0)); |
378 | < | maxI = 0; |
379 | < | for (i = 1; i < 3; i++) { |
358 | < | if (maxVal < (tmp = fabs(v(i, 0)))) { |
359 | < | maxVal = tmp; |
360 | < | maxI = i; |
361 | < | } |
362 | < | } |
375 | > | // transpose vectors back to columns |
376 | > | v = v.transpose(); |
377 | > | return; |
378 | > | } |
379 | > | } |
380 | ||
381 | < | // swap eigenvalue and eigenvector |
382 | < | if (maxI != 0) { |
366 | < | tmp = w(maxI); |
367 | < | w(maxI) = w(0); |
368 | < | 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 | < | } |
381 | > | // the three eigenvalues are different, just sort the eigenvectors |
382 | > | // to align them with the x, y, and z axes |
383 | ||
384 | < | // ensure that the sign of the eigenvectors is correct |
385 | < | for (i = 0; i < 2; i++) { |
386 | < | if (v(i, i) < 0) { |
387 | < | v(i, 0) = -v(i, 0); |
388 | < | v(i, 1) = -v(i, 1); |
389 | < | v(i, 2) = -v(i, 2); |
390 | < | } |
391 | < | } |
384 | > | // find the vector with the largest x element, make that vector |
385 | > | // the first vector |
386 | > | maxVal = fabs(v(0, 0)); |
387 | > | maxI = 0; |
388 | > | for (i = 1; i < 3; i++) { |
389 | > | if (maxVal < (tmp = fabs(v(i, 0)))) { |
390 | > | maxVal = tmp; |
391 | > | maxI = i; |
392 | > | } |
393 | > | } |
394 | ||
395 | < | // set sign of final eigenvector to ensure that determinant is positive |
396 | < | if (v.determinant() < 0) { |
397 | < | v(2, 0) = -v(2, 0); |
398 | < | v(2, 1) = -v(2, 1); |
399 | < | v(2, 2) = -v(2, 2); |
400 | < | } |
395 | > | // swap eigenvalue and eigenvector |
396 | > | if (maxI != 0) { |
397 | > | tmp = w(maxI); |
398 | > | w(maxI) = w(0); |
399 | > | w(0) = tmp; |
400 | > | v.swapRow(maxI, 0); |
401 | > | } |
402 | > | // do the same for the y element |
403 | > | if (fabs(v(1, 1)) < fabs(v(2, 1))) { |
404 | > | tmp = w(2); |
405 | > | w(2) = w(1); |
406 | > | w(1) = tmp; |
407 | > | v.swapRow(2, 1); |
408 | > | } |
409 | ||
410 | < | // transpose the eigenvectors back again |
411 | < | v = v.transpose(); |
412 | < | return ; |
410 | > | // ensure that the sign of the eigenvectors is correct |
411 | > | for (i = 0; i < 2; i++) { |
412 | > | if (v(i, i) < 0) { |
413 | > | v(i, 0) = -v(i, 0); |
414 | > | v(i, 1) = -v(i, 1); |
415 | > | v(i, 2) = -v(i, 2); |
416 | } | |
417 | < | }; |
417 | > | } |
418 | ||
419 | + | // set sign of final eigenvector to ensure that determinant is positive |
420 | + | if (v.determinant() < 0) { |
421 | + | v(2, 0) = -v(2, 0); |
422 | + | v(2, 1) = -v(2, 1); |
423 | + | v(2, 2) = -v(2, 2); |
424 | + | } |
425 | + | |
426 | + | // transpose the eigenvectors back again |
427 | + | v = v.transpose(); |
428 | + | return ; |
429 | + | } |
430 | typedef SquareMatrix3<double> Mat3x3d; | |
431 | typedef SquareMatrix3<double> RotMat3x3d; | |
432 | ||
433 | } //namespace oopse | |
434 | #endif // MATH_SQUAREMATRIX_HPP | |
435 | + |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |