cgv
polar.h
1 #pragma once
2 
3 #include <cgv/math/mat.h>
4 #include <cgv/math/inv.h>
5 #include <cgv/math/constants.h>
6 #include <limits>
7 #include <algorithm>
8 
9 
10 namespace cgv {
11 namespace math {
12 
13 
14 
15 
19 template <typename T>
20 void polar(const mat<T> &c, mat<T> &r, mat<T> &a,int num_iter=15)
21 {
22  r = c;
23  for(int i =0; i < num_iter;i++)
24  r = ((T)0.5)*(r+transpose(inv(r)));
25 
26  a = inv(r)*c;
27  /*
28  * Alternative way using svd:
29  * c = u*d*v^t
30  * r = u*v^t, a=v*d*v^t
31  */
32 }
33 
34 
37 template <typename T>
39  cgv::math::vec<T>& axis,
40  T& angle)
41 {
42  assert(R.nrows() == 3 && R.ncols() == 3);
43 
44 
45  mat<T> A = (T)0.5*(R - transpose(R));
46  mat<T> S = (T)0.5*(R + transpose(R));
47 
48 
49  T abssina = (T)sqrt(0.5*(double)(A.frobenius_norm()*A.frobenius_norm()));
50  T cosa = 0.5*(trace(S)-1.0);
51 
52 
53 
54  angle =(T)(asin(abssina)*180.0/PI);
55  if(cosa < 0)
56  angle = (T)180.0-angle;
57 
58  if(angle == 0 || angle == 180.0)
59  return false;
60 
61  axis = (T)1.0/abssina*cgv::math::vec<T>(A(2,1),A(0,2),A(1,0));
62 
63  return true;
64 }
65 
66 
67 
68 
69 }
70 
71 }
cgv::math::inv
diag_mat< T > inv(const diag_mat< T > &m)
returns the inverse of a diagonal matrix
Definition: inv.h:11
cgv::math::mat
Definition: mat.h:14
cgv::math::vec
A column vector class.
Definition: fvec.h:13
cgv::math::transpose
fmat< T, N, N > transpose(const fmat< T, N, N > &m)
return the transposed of a square matrix
Definition: fmat.h:196
cgv::math::polar
void polar(const mat< T > &c, mat< T > &r, mat< T > &a, int num_iter=15)
Definition: polar.h:20
cgv::math::mat::frobenius_norm
T frobenius_norm() const
returns the frobenius norm of matrix m
Definition: mat.h:987
cgv::math::mat::nrows
unsigned nrows() const
number of rows
Definition: mat.h:541
cgv::math::mat::ncols
unsigned ncols() const
number of columns
Definition: mat.h:547
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::math::decompose_rotation
bool decompose_rotation(const cgv::math::mat< T > &R, cgv::math::vec< T > &axis, T &angle)
Definition: polar.h:38