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" |
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; |
301 |
< |
} |
302 |
< |
|
303 |
< |
// transpose temporarily, it makes it easier to sort the eigenvectors |
294 |
< |
v = v.transpose(); |
295 |
< |
|
296 |
< |
// if two eigenvalues are the same, re-orthogonalize to optimally line |
297 |
< |
// up the eigenvectors with the x, y, and z axes |
298 |
< |
for (i = 0; i < 3; i++) { |
299 |
< |
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; |
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 |
< |
v.swapRow(i, maxI); |
306 |
< |
} |
307 |
< |
// maximum element of eigenvector should be positive |
308 |
< |
if (v(maxI, maxI) < 0) { |
309 |
< |
v(maxI, 0) = -v(maxI, 0); |
310 |
< |
v(maxI, 1) = -v(maxI, 1); |
311 |
< |
v(maxI, 2) = -v(maxI, 2); |
323 |
< |
} |
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 |
< |
// re-orthogonalize the other two eigenvectors |
314 |
< |
j = (maxI+1)%3; |
315 |
< |
k = (maxI+2)%3; |
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 |
< |
v(j, 0) = 0.0; |
322 |
< |
v(j, 1) = 0.0; |
323 |
< |
v(j, 2) = 0.0; |
324 |
< |
v(j, j) = 1.0; |
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 |
< |
/** @todo */ |
345 |
< |
v_maxI = v.getRow(maxI); |
346 |
< |
v_j = v.getRow(j); |
347 |
< |
v_k = cross(v_maxI, v_j); |
348 |
< |
v_k.normalize(); |
349 |
< |
v_j = cross(v_k, v_maxI); |
350 |
< |
v.setRow(j, v_j); |
351 |
< |
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 |
+ |
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 |
< |
// transpose vectors back to columns |
363 |
< |
v = v.transpose(); |
364 |
< |
return; |
365 |
< |
} |
366 |
< |
} |
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 |
|
|
350 |
– |
// the three eigenvalues are different, just sort the eigenvectors |
351 |
– |
// to align them with the x, y, and z axes |
371 |
|
|
372 |
< |
// find the vector with the largest x element, make that vector |
373 |
< |
// the first vector |
374 |
< |
maxVal = fabs(v(0, 0)); |
375 |
< |
maxI = 0; |
376 |
< |
for (i = 1; i < 3; i++) { |
358 |
< |
if (maxVal < (tmp = fabs(v(i, 0)))) { |
359 |
< |
maxVal = tmp; |
360 |
< |
maxI = i; |
361 |
< |
} |
362 |
< |
} |
372 |
> |
// transpose vectors back to columns |
373 |
> |
v = v.transpose(); |
374 |
> |
return; |
375 |
> |
} |
376 |
> |
} |
377 |
|
|
378 |
< |
// swap eigenvalue and eigenvector |
379 |
< |
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 |
< |
} |
378 |
> |
// the three eigenvalues are different, just sort the eigenvectors |
379 |
> |
// to align them with the x, y, and z axes |
380 |
|
|
381 |
< |
// ensure that the sign of the eigenvectors is correct |
382 |
< |
for (i = 0; i < 2; i++) { |
383 |
< |
if (v(i, i) < 0) { |
384 |
< |
v(i, 0) = -v(i, 0); |
385 |
< |
v(i, 1) = -v(i, 1); |
386 |
< |
v(i, 2) = -v(i, 2); |
387 |
< |
} |
388 |
< |
} |
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 |
< |
// set sign of final eigenvector to ensure that determinant is positive |
393 |
< |
if (v.determinant() < 0) { |
394 |
< |
v(2, 0) = -v(2, 0); |
395 |
< |
v(2, 1) = -v(2, 1); |
396 |
< |
v(2, 2) = -v(2, 2); |
397 |
< |
} |
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 |
< |
// transpose the eigenvectors back again |
408 |
< |
v = v.transpose(); |
409 |
< |
return ; |
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 |
+ |
|