cgv
Loading...
Searching...
No Matches
geom.h
1#pragma once
2
3#include "fmat.h"
4
5namespace cgv {
6 namespace math {
8
9 template <typename T>
11 {
12 cgv::math::fvec<T, 3> vn = dot(n, v)*n;
13 return vn + cos(a)*(v - vn) + sin(a)*cross(n, v);
14 }
16
17 template <typename T>
18 void compute_rotation_axis_and_angle_from_vector_pair(const cgv::math::fvec<T, 3>& v0, const cgv::math::fvec<T, 3>& v1, cgv::math::fvec<T, 3>& axis, T& angle)
19 {
20 axis = cross(v0, v1);
21 T len = axis.length();
22 if (len > 2 * std::numeric_limits<T>::epsilon())
23 axis *= T(1) / len;
24 else
25 axis[1] = T(1);
26 angle = atan2(len, dot(v0, v1));
27 }
29
36 template <typename T>
37 int decompose_rotation_to_axis_and_angle(const cgv::math::fmat<T, 3, 3>& R, cgv::math::fvec<T, 3>& axis, T& angle)
38 {
39 axis(0) = R(2, 1) - R(1, 2);
40 axis(1) = R(0, 2) - R(2, 0);
41 axis(2) = R(1, 0) - R(0, 1);
42 T len = axis.length();
43 T tra = R.trace();
44 if (len < 2 * std::numeric_limits<T>::epsilon()) {
45 if (tra < 0) {
46 for (unsigned c = 0; c<3; ++c)
47 if (R(c, c) > 0) {
48 axis(c) = T(1);
49 angle = M_PI;
50 break;
51 }
52 return 1;
53 }
54 else {
55 axis(1) = T(1);
56 angle = 0;
57 return 2;
58 }
59 }
60 else {
61 axis *= T(1) / len;
62 angle = atan2(len, tra - T(1));
63 return 0;
64 }
65 }
67
75 template <typename T>
76 cgv::math::fmat<T, 3, 3> build_orthogonal_frame(const cgv::math::fvec<T, 3>& v0, const cgv::math::fvec<T, 3>& v1)
77 {
79
80 auto &x = /*(cgv::math::fvec<T, 3>&)O(0, 0)*/O.col(0);
81 auto &y = /*(cgv::math::fvec<T, 3>&)O(0, 1)*/O.col(1);
82 auto &z = /*(cgv::math::fvec<T, 3>&)O(0, 2)*/O.col(2);
83
84 x = v0;
85 x.normalize();
86 z = cross(v0, v1);
87 z.normalize();
88 y = cross(z, x);
89
90 return O;
91 }
92
93 }
94}
matrix of fixed size dimensions
Definition fmat.h:23
fvec< T, N > & col(unsigned j)
reference a column of the matrix as a vector
Definition fmat.h:209
T trace() const
returns the trace
Definition fmat.h:222
A vector with zero based index.
Definition fvec.h:26
T normalize()
normalize the vector using the L2-Norm and return the length
Definition fvec.h:302
T length() const
length of the vector L2-Norm
Definition fvec.h:249
the cgv namespace
Definition print.h:11