CGR Localization
 All Classes Namespaces Files Functions Variables Macros Pages
quaternion_helper.h
Go to the documentation of this file.
1 //========================================================================
2 // This software is free: you can redistribute it and/or modify
3 // it under the terms of the GNU Lesser General Public License Version 3,
4 // as published by the Free Software Foundation.
5 //
6 // This software is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // GNU Lesser General Public License for more details.
10 //
11 // You should have received a copy of the GNU Lesser General Public License
12 // Version 3 in the file COPYING that came with this distribution.
13 // If not, see <http://www.gnu.org/licenses/>.
14 //========================================================================
20 //========================================================================
21 
22 #include "geometry.h"
23 #include <eigen3/Eigen/Dense>
24 #include <eigen3/Eigen/Geometry>
25 
26 #ifndef QUATERNION_HELPER_H
27 #define QUATERNION_HELPER_H
28 
29 namespace GVector {
30 
31  template <class num> inline GVector::vector3d<num> quat_rotate(const Eigen::Quaternion<num> &quat, const GVector::vector3d<num> &vec) __attribute__ ((warn_unused_result));
32  template <class num> inline GVector::vector3d<num> quat_rotate(const Eigen::Quaternion< num >& quat, const GVector::vector3d< num >& vec)
33  {
34  Eigen::Matrix<num,3,1> v(vec.x,vec.y,vec.z);
35  v = quat*v;
36  return GVector::vector3d<num>(v[0],v[1],v[2]);
37  }
38 
39  template <class num> void quatToMatrix(const Eigen::Quaternion<num> &quat, GVector::matrix3d<num> &M)
40  {
41  Eigen::Matrix<num,3,3> mat;
42  mat = quat.toRotationMatrix();
43  M.m11 = mat(0,0);
44  M.m12 = mat(0,1);
45  M.m13 = mat(0,2);
46  M.m14 = 0.0;
47  M.m21 = mat(1,0);
48  M.m22 = mat(1,1);
49  M.m23 = mat(1,2);
50  M.m24 = 0.0;
51  M.m31 = mat(2,0);
52  M.m32 = mat(2,1);
53  M.m33 = mat(2,2);
54  M.m34 = 0.0;
55  M.m41 = 0.0;
56  M.m42 = 0.0;
57  M.m43 = 0.0;
58  M.m44 = 1.0;
59  }
60 
61  template <class num> void transformMatrix(const Eigen::Quaternion<num> &quat, const GVector::vector3d<num> &t, GVector::matrix3d<num> &M)
62  {
63  Eigen::Matrix<num,3,3> mat;
64  mat = quat.toRotationMatrix();
65  M.m11 = mat(0,0);
66  M.m12 = mat(0,1);
67  M.m13 = mat(0,2);
68  M.m14 = t.x;
69  M.m21 = mat(1,0);
70  M.m22 = mat(1,1);
71  M.m23 = mat(1,2);
72  M.m24 = t.y;
73  M.m31 = mat(2,0);
74  M.m32 = mat(2,1);
75  M.m33 = mat(2,2);
76  M.m34 = t.z;
77  M.m41 = 0.0;
78  M.m42 = 0.0;
79  M.m43 = 0.0;
80  M.m44 = 1.0;
81  }
82 
83  template <class num> Eigen::Quaternion<num> quat_construct(const GVector::vector3d<num> &axis, const num angle)
84  {
85  Eigen::Matrix<num,3,1> axisVector(axis.x,axis.y,axis.z);
86  Eigen::AngleAxis<num> angleAxisForm(angle,axisVector);
87  return Eigen::Quaternion<num>(angleAxisForm);
88  }
89 
91  template <class num> Eigen::Quaternion<num> randRot(const num max_angle, const Eigen::Quaternion<num> &angle)
92  {
93  GVector::vector3d<num> randAxis(frand(-1.0,1.0),frand(-1.0,1.0),frand(-1.0,1.0));
94  num randAngle = frand<num>(0.0,max_angle);
95  if(randAngle>0.0 && randAxis.sqlength()>0.0){
96  randAxis.normalize();
97  return quat_construct<num>(randAxis,randAngle)*angle;
98  }
99  return angle;
100  }
101 
103  template <class num> Eigen::Quaternion<num> randRot(const num &max_angle)
104  {
105  GVector::vector3d<num> axis(frand(-1.0,1.0),frand(-1.0,1.0),frand(-1.0,1.0));
106  num angle = frand<num>(0.0,max_angle);
107  if(angle>0.0 && axis.sqlength()>0.0){
108  axis.normalize();
109  return quat_construct<num>(axis,angle)*angle;
110  }
111  return Eigen::Quaternion<num>(1.0,0.0,0.0,0.0);
112  }
113 };
114 
115 #endif //QUATERNION_HELPER_H