utils.h
Go to the documentation of this file.
1 #ifndef TUW_GEOMETRY_UTILS
2 #define TUW_GEOMETRY_UTILS
3 
4 #include <string>
5 #include <opencv2/core/mat.hpp>
6 #include <opencv2/core/matx.hpp>
7 
8 namespace tuw
9 {
10 typedef cv::Matx<double, 3, 3> Tf2D;
11 
19 inline cv::Vec<double, 3> append ( const cv::Vec<double, 2> &src, double value = 1.0 )
20 {
21  return cv::Vec<double, 3> ( src.val[0], src.val[1], value );
22 }
23 
31 inline cv::Vec<double, 4> append ( const cv::Vec<double, 3> &src, double value = 1.0 )
32 {
33  return cv::Vec<double, 4> ( src.val[0], src.val[1], src.val[2], value );
34 }
35 
43 inline double angle_normalize ( double angle, double min_angle = -M_PI, double max_angle = +M_PI )
44 {
45  while ( angle > max_angle ) {
46  angle -= ( 2.*M_PI );
47  }
48  while ( angle < min_angle ) {
49  angle += ( 2.*M_PI );
50  }
51  return angle;
52 }
59 inline double angle_difference ( double alpha0, double angle1 )
60 {
61  return atan2 ( sin ( alpha0-angle1 ), cos ( alpha0-angle1 ) );
62 }
63 
68 template <typename Quaternion>
69 void EulerPitchToQuaternion ( double pitch, Quaternion& q )
70 {
71  double cp = cos ( pitch * 0.5 );
72  double sp = sin ( pitch * 0.5 );
73 
74  q.w = cp;
75  q.x = 0;
76  q.y = sp;
77  q.z = 0;
78 
79 }
80 
85 template <typename Quaternion>
86 void EulerToQuaternion ( double roll, const Quaternion& q )
87 {
88  double cr = cos ( roll * 0.5 );
89  double sr = sin ( roll * 0.5 );
90 
91  q.w = cr;
92  q.x = sr;
93  q.y = 0.;
94  q.z = 0.;
95 }
96 
101 template <typename Quaternion>
102 void EulerYawToQuaternion ( double yaw, Quaternion& q )
103 {
104  double cy = cos ( yaw * 0.5 );
105  double sy = sin ( yaw * 0.5 );
106 
107  q.w = cy;
108  q.x = 0.;
109  q.y = 0.;
110  q.z = sy;
111 }
112 
117 template <typename Quaternion>
118 void EulerToQuaternion ( double pitch, double roll, double yaw, const Quaternion& q )
119 {
120  double cy = cos ( yaw * 0.5 );
121  double sy = sin ( yaw * 0.5 );
122  double cr = cos ( roll * 0.5 );
123  double sr = sin ( roll * 0.5 );
124  double cp = cos ( pitch * 0.5 );
125  double sp = sin ( pitch * 0.5 );
126 
127  q.w = cy * cr * cp + sy * sr * sp;
128  q.x = cy * sr * cp - sy * cr * sp;
129  q.y = cy * cr * sp + sy * sr * cp;
130  q.z = sy * cr * cp - cy * sr * sp;
131  return q;
132 }
133 
138 template <typename Quaternion>
139 void QuaternionToRoll ( const Quaternion& q, double& roll )
140 {
141  // roll (x-axis rotation)
142  double sinr = +2.0 * ( q.w * q.x + q.y * q.z );
143  double cosr = +1.0 - 2.0 * ( q.x * q.x + q.y * q.y );
144  roll = atan2 ( sinr, cosr );
145 }
150 template <typename Quaternion>
151 void QuaternionToPitch ( const Quaternion& q, double& pitch )
152 {
153  // pitch (y-axis rotation)
154  double sinp = +2.0 * ( q.w * q.y - q.z * q.x );
155  if ( fabs ( sinp ) >= 1 ) {
156  pitch = copysign ( M_PI / 2, sinp ); // use 90 degrees if out of range
157  } else {
158  pitch = asin ( sinp );
159  }
160 }
165 template <typename Quaternion>
166 void QuaternionToYaw ( const Quaternion& q, double& yaw )
167 {
168  // yaw (z-axis rotation)
169  double siny = +2.0 * ( q.w * q.z + q.x * q.y );
170  double cosy = +1.0 - 2.0 * ( q.y * q.y + q.z * q.z );
171  yaw = atan2 ( siny, cosy );
172 }
173 
178 template <typename Quaternion>
179 void QuaternionToEuler ( const Quaternion& q, double& roll, double& pitch, double& yaw )
180 {
181  QuaternionToRoll ( q, roll ), QuaternionToPitch ( q, pitch ), QuaternionToYaw ( q, yaw );
182 }
183 
184 std::string format ( const cv::Mat_<int8_t> &m );
185 std::string format ( const cv::Mat_<int> &m );
186 std::string format ( const cv::Mat_<float> &m );
187 std::string format ( const cv::Mat_<double> &m );
188 std::string format ( const cv::Matx33d &m );
189 }
190 
191 #endif // TUW_GEOMETRY_UTILS
double angle_normalize(double angle, double min_angle=-M_PI, double max_angle=+M_PI)
Definition: utils.h:43
void QuaternionToYaw(const Quaternion &q, double &yaw)
Definition: utils.h:166
cv::Vec< double, 3 > append(const cv::Vec< double, 2 > &src, double value=1.0)
Definition: utils.h:19
void QuaternionToRoll(const Quaternion &q, double &roll)
Definition: utils.h:139
Definition: command.h:8
void QuaternionToEuler(const Quaternion &q, double &roll, double &pitch, double &yaw)
Definition: utils.h:179
void QuaternionToPitch(const Quaternion &q, double &pitch)
Definition: utils.h:151
void EulerYawToQuaternion(double yaw, Quaternion &q)
Definition: utils.h:102
double angle_difference(double alpha0, double angle1)
Definition: utils.h:59
void EulerToQuaternion(double roll, const Quaternion &q)
Definition: utils.h:86
cv::Matx< double, 3, 3 > Tf2D
Definition: utils.h:10
std::string format(const cv::Mat_< int8_t > &m)
Definition: utils.cpp:11
void EulerPitchToQuaternion(double pitch, Quaternion &q)
Definition: utils.h:69


tuw_geometry
Author(s): Markus Bader
autogenerated on Mon Jun 10 2019 15:33:09