cgv
Loading...
Searching...
No Matches
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
10namespace cgv {
11namespace math {
12
13
14
15
19template <typename T>
20void 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
37template <typename T>
38bool decompose_rotation(const cgv::math::mat<T>& R,
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}
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...
Definition mat.h:208
unsigned ncols() const
number of columns
Definition mat.h:546
unsigned nrows() const
number of rows
Definition mat.h:540
A column vector class.
Definition vec.h:28
the cgv namespace
Definition print.h:11