cgv
Loading...
Searching...
No Matches
rigid_transform.h
1#pragma once
2
3#include <cgv/math/fvec.h>
4#include <cgv/math/quaternion.h>
5#include <cgv/math/fmat.h>
6
7namespace cgv {
8 namespace math {
9
12template <typename T>
14{
15public:
26protected:
31public:
33 rigid_transform() : q(1,0,0,0), t(0,0,0) {}
35 rigid_transform(const quat_type& _q, const vec_type& _t) : q(_q), t(_t) {}
37 void transform_vector(vec_type& v) const { q.rotate(v); }
39 void transform_point(vec_type& p) const { q.rotate(p); p += t; }
43 rigid_transform<T>& operator += (const rigid_transform<T>& T) { q += T.q; t += T.t; return *this; }
45 rigid_transform<T>& operator *= (T s) { q.vec() *= s; t *= s; return *this; }
47 rigid_transform<T> operator * (T s) const { rigid_transform<T> r(*this); r *= s; return r; }
49 void normalize() { q.normalize(); }
51 rigid_transform<T> inverse() const { return rigid_transform<T>(q.inverse(), q.inverse().apply(-t)); }
53 vec_type get_transformed_vector(const vec_type& v) const { return q.apply(v); }
55 vec_type get_transformed_point(const vec_type& p) const { return q.apply(p)+t; }
57 const vec_type& get_translation() const { return t; }
59 vec_type& ref_translation() { return t; }
61 const quat_type& get_quaternion() const { return q; }
63 quat_type& ref_quaternion() { return q; }
66 T M[9];
67 q.put_matrix(M);
68 hmat_type H;
69 H.set_col(0, hvec_type(M[0], M[3], M[6], 0));
70 H.set_col(1, hvec_type(M[1], M[4], M[7], 0));
71 H.set_col(2, hvec_type(M[2], M[5], M[8], 0));
72 H.set_col(3, hvec_type(t(0), t(1), t(2), 1));
73 return H;
74 }
77 T M[9];
78 q.put_matrix(M);
79 hmat_type H;
80 H.set_row(0, hvec_type(M[0], M[3], M[6], 0));
81 H.set_row(1, hvec_type(M[1], M[4], M[7], 0));
82 H.set_row(2, hvec_type(M[2], M[5], M[8], 0));
83 H.set_row(3, hvec_type(t(0), t(1), t(2), 1));
84 return H;
85 }
86};
87
88 }
89}
matrix of fixed size dimensions
Definition fmat.h:23
void set_col(unsigned j, const fvec< T, N > &v)
set column j of the matrix to vector v
Definition fmat.h:217
void set_row(unsigned i, const fvec< T, M > &v)
set row i of the matrix to vector v
Definition fmat.h:204
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
implements a quaternion.
Definition quaternion.h:21
quaternion< T > inverse() const
return the inverse
Definition quaternion.h:232
vec_type apply(const vec_type &v) const
rotate vector according to quaternion
Definition quaternion.h:219
void rotate(vec_type &v) const
rotate vector according to quaternion
Definition quaternion.h:198
void put_matrix(mat_type &M) const
compute equivalent 3x3 rotation matrix
Definition quaternion.h:139
implementation of a rigid body transformation with a quaternion and a translation vector.
vec_type & ref_translation()
return the translational part
const vec_type & get_translation() const
return the translational part
rigid_transform(const quat_type &_q, const vec_type &_t)
construct from quaternion and translation vector to the transformation that first rotates and then tr...
cgv::math::fmat< T, 4, 4 > hmat_type
type of 4x4 matrix
vec_type get_transformed_vector(const vec_type &v) const
apply transformation to vector
void transform_point(vec_type &p) const
apply transformation to point
rigid_transform< T > & operator+=(const rigid_transform< T > &T)
multiply quaternion and translation with scalar
vec_type t
and translation vector
rigid_transform< T > operator*(const rigid_transform< T > &M) const
concatenate two rigid transformations
hmat_type get_transposed_hmat() const
convert transformation to transpose of homogeneous transformation
const quat_type & get_quaternion() const
return the rotation as quaternion
cgv::math::fvec< T, 4 > hvec_type
type of homogenous vector
rigid_transform< T > & operator*=(T s)
multiply quaternion and translation with scalar
vec_type get_transformed_point(const vec_type &p) const
apply transformation to point
void transform_vector(vec_type &v) const
apply transformation to vector
cgv::math::quaternion< T > quat_type
type of quaternions
hmat_type get_hmat() const
convert transformation to homogeneous transformation
cgv::math::fmat< T, 3, 3 > mat_type
type of 3x3 matrix
rigid_transform< T > inverse() const
return the inverse transformation
quat_type q
store transformation as quaternion
rigid_transform()
construct identity
cgv::math::fvec< T, 3 > vec_type
type of 3d vector
quat_type & ref_quaternion()
return the rotation as quaternion
the cgv namespace
Definition print.h:11