5#ifndef _USE_MATH_DEFINES
6 #define _USE_MATH_DEFINES 1
58 template <
class rot_mat_type>
65 const base_type& vec()
const {
return *
this; }
85 template <
class rot_mat_type>
86 void set(
const rot_mat_type &M)
90 (rot_mat_type::nrows() == 3 && (rot_mat_type::ncols()==3 || rot_mat_type::ncols()==4))
91 || (rot_mat_type::nrows() == 4 && rot_mat_type::ncols() == 4),
"matrix layout not supported"
95 constexpr typename rot_mat_type::value_type _1o4 = .25;
97 this->
w() = sqrt((T)plus(_1o4*( M(0, 0) + M(1, 1) + M(2, 2) + 1)));
98 this->
x() = sqrt((T)plus(_1o4*( M(0, 0) - M(1, 1) - M(2, 2) + 1)));
99 this->
y() = sqrt((T)plus(_1o4*(-M(0, 0) + M(1, 1) - M(2, 2) + 1)));
100 this->
z() = sqrt((T)plus(_1o4*(-M(0, 0) - M(1, 1) + M(2, 2) + 1)));
101 if (this->
w() >= this->
x() && this->
w() >= this->
y() && this->
w() >= this->
z()) {
102 this->
x() *= (T)cgv::math::sign(M(2, 1) - M(1, 2));
103 this->
y() *= (T)cgv::math::sign(M(0, 2) - M(2, 0));
104 this->
z() *= (T)cgv::math::sign(M(1, 0) - M(0, 1));
106 else if (this->
x() >= this->
y() && this->
x() >= this->
z()) {
107 this->
w() *= (T)cgv::math::sign(M(2, 1) - M(1, 2));
108 this->
y() *= (T)cgv::math::sign(M(0, 1) + M(1, 0));
109 this->
z() *= (T)cgv::math::sign(M(2, 0) + M(0, 2));
111 else if (this->
y() >= this->
z()) {
112 this->
w() *= (T)cgv::math::sign(M(0, 2) - M(2, 0));
113 this->
x() *= (T)cgv::math::sign(M(0, 1) + M(1, 0));
114 this->
z() *= (T)cgv::math::sign(M(1, 2) + M(2, 1));
117 this->
w() *= (T)cgv::math::sign(M(1, 0) - M(0, 1));
118 this->
x() *= (T)cgv::math::sign(M(0, 2) + M(2, 0));
119 this->
y() *= (T)cgv::math::sign(M(1, 2) + M(2, 1));
141 M(0, 0) = 1 - 2 * this->
y()*this->
y() - 2 * this->
z()*this->
z();
142 M(0, 1) = 2 * this->
x()*this->
y() - 2 * this->
w()*this->
z();
143 M(0, 2) = 2 * this->
x()*this->
z() + 2 * this->
w()*this->
y();
144 M(1, 0) = 2 * this->
x()*this->
y() + 2 * this->
w()*this->
z();
145 M(1, 1) = 1 - 2 * this->
x()*this->
x() - 2 * this->
z()*this->
z();
146 M(1, 2) = 2 * this->
y()*this->
z() - 2 * this->
w()*this->
x();
147 M(2, 0) = 2 * this->
x()*this->
z() - 2 * this->
w()*this->
y();
148 M(2, 1) = 2 * this->
y()*this->
z() + 2 * this->
w()*this->
x();
149 M(2, 2) = 1-2*this->
x()*this->
x()-2*this->
y()*this->
y();
156 M(0, 0) = 1 - 2 * this->
y()*this->
y() - 2 * this->
z()*this->
z();
157 M(0, 1) = 2 * this->
x()*this->
y() - 2 * this->
w()*this->
z();
158 M(0, 2) = 2 * this->
x()*this->
z() + 2 * this->
w()*this->
y();
159 M(1, 0) = 2 * this->
x()*this->
y() + 2 * this->
w()*this->
z();
160 M(1, 1) = 1 - 2 * this->
x()*this->
x() - 2 * this->
z()*this->
z();
161 M(1, 2) = 2 * this->
y()*this->
z() - 2 * this->
w()*this->
x();
162 M(2, 0) = 2 * this->
x()*this->
z() - 2 * this->
w()*this->
y();
163 M(2, 1) = 2 * this->
y()*this->
z() + 2 * this->
w()*this->
x();
164 M(2, 2) = 1 - 2 * this->
x()*this->
x() - 2 * this->
y()*this->
y();
165 M(3,0) = M(3,1) = M(3,2) = M(0,3) = M(1, 3) = M(2, 3) = M(3, 3) = 0;
174 if (fabs(cosfac)<EPSILON)
177 cosfac = sqrt(cosfac);
181 set(fac*sinfac/cosfac, 0, -n.
z()*tmp, n.
y()*tmp);
187 n[0] = 1-2*(this->
y()*this->
y()+ this->
z()*this->
z());
188 n[1] = 2*(this->
w()*this->
z() + this->
x()*this->
y());
189 n[2] = 2*(this->
x()*this->
z() - this->
w()*this->
y());
194 image = cross(
im(), preimage);
195 image = dot(preimage,
im())*
im() +
re()*(
re()*preimage + (
coord_type)2*image) + cross(
im(),image);
213 preimage = cross(-
im(), image);
214 preimage = dot(image,-
im())*(-
im()) +
re()*(
re()*image + (
coord_type)2*preimage) + cross(-
im(),preimage);
230 void conjugate() { this->
x() = -this->
x(); this->
y() = -this->
y(); this->
z() = -this->
z(); }
238 if (sn < EPSILON*EPSILON)
249 if ( ( 1 + cosom) > EPSILON ) {
250 if ( ( 1 - cosom) > EPSILON ) {
251 omega = acos( cosom );
252 sinom = sin ( omega );
253 sclp = sin ( (1-t) * omega ) / sinom;
254 sclq = sin ( t*omega ) / sinom;
261 set(sclp*p.
w() + sclq*q.
w(), sclp*p.
x() + sclq*q.
x(), sclp*p.
y() + sclq*q.
y(), sclp*p.
z() + sclq*q.
z());
264 sclp = (T)sin ( (1-t)*M_PI/2 );
265 sclq = (T)sin ( t * M_PI/2 );
266 set(p.
z(), sclp*p.
x() - sclq*p.
y(), sclp*p.
y() + sclq*p.
x(), sclp*p.
z() - sclq*p.
w());
278 s[0] = (this->
z()-this->
y())*(q.
y()-q.
z());
279 s[1] = (this->
w()+this->
x())*(q.
w()+q.
x());
280 s[2] = (this->
w()-this->
x())*(q.
y()+q.
z());
281 s[3] = (this->
z()+this->
y())*(q.
w()-q.
x());
282 s[4] = (this->
z()-this->
x())*(q.
x()-q.
y());
283 s[5] = (this->
z()+this->
x())*(q.
x()+q.
y());
284 s[6] = (this->
w()+this->
y())*(q.
w()-q.
z());
285 s[7] = (this->
w()-this->
y())*(q.
w()+q.
z());
286 s[8] = s[5]+s[6]+s[7];
288 set(s[0]+t-s[5], s[1]+t-s[8], s[2]+t-s[7], s[3]+t-s[6]);
303 coord_type&
re() {
return this->
w(); }
305 void put_im(vec_type& vector)
const { vector.
x() = this->
x(); vector.
y() = this->
y(); vector.
z() = this->
z(); }
343 if (sinTimesR < EPSILON)
355#define CGV_MATH_QUATERNION_DECLARED
359quaternion<T> operator * (
const T& s,
const quaternion<T>& v)
361 quaternion<T> r = v; r *= s;
return r;
matrix of fixed size dimensions
fvec< T, N > & col(unsigned j)
reference a column of the matrix as a vector
void set_col(unsigned j, const fvec< T, N > &v)
set column j of the matrix to vector v
A vector with zero based index.
T normalize()
normalize the vector using the L2-Norm and return the length
fvec< T, N > & operator*=(const T &s)
in place multiplication with s
fvec< T, N > & operator/=(const T &s)
in place division by scalar s
T sqr_length() const
square length of vector
T length() const
length of the vector L2-Norm
fvec & operator=(const fvec< T, N > &rhs)
assign vector rhs, if vector and rhs have different sizes, vector has been resized to match the size ...
vec_type im() const
return this as vector
quaternion(const vec_type &axis, coord_type angle)
construct quaternion from axis and rotation angle
quaternion< T > & operator=(const quaternion< T > &quat)
assignement operator
quaternion(const rot_mat_type &matrix)
construct quaternion from 3x3 rotation or rotational part of homogeneous 3x4 / 4x4 matrix
void put_im(vec_type &vector) const
put imaginary part
T coord_type
coordinate type
fvec< T, 3 > vec_type
type of 3d axis
vec_type get_rotated(const vec_type &v) const
return rotated vector
quaternion(AxisEnum axis, coord_type angle)
construct quaternion from coordinate axis and rotation angle
coord_type re() const
return real part
quaternion< T > operator-() const
negation operator
void put_normal(coord_type *n)
extract normal vector
void inverse_rotate(vec_type &image) const
rotate vector according to the inverse quaternion
quaternion(const quaternion< S > &q)
copy constructor with type conversion
void put_image(const vec_type &preimage, vec_type &image) const
rotate preimage according to quaternion into image
quaternion< T > exp() const
exponential map
coord_type & re()
return reference to real part
void rotate(mat_type &m) const
Rotate a frame according to quaternion.
void affin(coord_type s, const quaternion< T > &q)
compute affin combination with angular interpolation
void set(const rot_mat_type &M)
set quaternion from 3x3 rotation or rotational part of homogeneous 3x4 / 4x4 matrix
coord_type put_axis(vec_type &v) const
put rotation axis and return rotation angle
void set(coord_type re, const vec_type &im)
set quaternion from real part and vector
fmat< T, 3, 3 > mat_type
type of 3x3 matrix
quaternion< T > & operator*=(const quaternion< T > &q)
field multiplication
hmat_type get_homogeneous_matrix() const
return equivalent 4x4 rotation matrix
void conjugate()
compute conjugate
quaternion(coord_type re, const vec_type &im)
construct quaternion from real part and vector
void set(AxisEnum axis, coord_type angle)
set quaternion from coordinate axis and rotation angle
fmat< T, 4, 4 > hmat_type
type of 4x4 matrix
void set_normal(const vec_type &n)
initialize quaternion from normal vector
mat_type get_matrix() const
return equivalent 3x3 rotation matrix
mat_type get_rotated(const mat_type &M) const
Rotate source frame s into destination frame d.
quaternion< T > inverse() const
return the inverse
vec_type apply(const vec_type &v) const
rotate vector according to quaternion
void affin(const quaternion< T > &p, coord_type t, const quaternion< T > &q)
compute affin combination with angular interpolation
quaternion< T > log() const
logarithmic map
void put_preimage(const vec_type &image, vec_type &preimage) const
rotate image according to quaternion into preimage
void set(coord_type re, coord_type ix, coord_type iy, coord_type iz)
set quaternion directly
AxisEnum
enumeration of the three coordinate axes
quaternion(const quaternion< T > &quat)
copy constructor
quaternion< T > conj() const
return the conjugate
void rotate(vec_type &v) const
rotate vector according to quaternion
quaternion< T > operator*(const quaternion< T > &q) const
field multiplication
void invert()
compute inverse
fvec< T, 4 > base_type
base class type
void set(const quaternion< T > &quat)
setter from quaternion
void put_homogeneous_matrix(hmat_type &M) const
compute equivalent homogeneous 4x4 rotation matrix
quaternion< T > negated() const
return the negated quaternion
void set(const vec_type &axis, coord_type angle)
set quaternion from axis and rotation angle
void put_matrix(mat_type &M) const
compute equivalent 3x3 rotation matrix
quaternion(coord_type w, coord_type x, coord_type y, coord_type z)
construct quaternion directly
quaternion()
standard constructor initializes to unit quaternion
void negate()
negate the quaternion
cgv::math::quaternion< double > dquat
declare type of double quaternion
cgv::math::quaternion< float > quat
declare type of quaternion