Cheetah Software  1.0
test_orientation_tools.cpp File Reference

Test orientation functions. More...

#include "Math/orientation_tools.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
+ Include dependency graph for test_orientation_tools.cpp:

Go to the source code of this file.

Functions

 TEST (Orientation, rad2deg)
 
 TEST (Orientation, almostEqual)
 
 TEST (Orientation, coordinateRotation)
 
 TEST (Orientation, skew)
 
 TEST (Orientation, quatToRotm)
 
 TEST (Orientation, quatToRpy)
 
 TEST (Orientation, quatToRpy2)
 
 TEST (Orientation, quatToRpy3)
 
 TEST (Orienation, quaternionDerivative)
 
 TEST (Orientation, quaternionProduct)
 
 TEST (Orientation, quaternionProductDirection)
 
 TEST (Orientation, quaternionIntegration)
 
 TEST (Orientation, rpyToRotMat)
 
 TEST (Orientation, allOrientationConversions)
 

Detailed Description

Test orientation functions.

Test the orientation related functions in orientation_tools.h Does not check any spatial stuff

Definition in file test_orientation_tools.cpp.

Function Documentation

TEST ( Orientation  ,
rad2deg   
)

Check radians to degrees

Definition at line 17 of file test_orientation_tools.cpp.

References ori::deg2rad(), and ori::rad2deg().

17  {
18  EXPECT_EQ(M_PI / 4., deg2rad(45.));
19  EXPECT_EQ(45, rad2deg(M_PI / 4.));
20 }
T rad2deg(T rad)
T deg2rad(T deg)

+ Here is the call graph for this function:

TEST ( Orientation  ,
almostEqual   
)

Check the "almostEqual" function for comparing two eigen quantities

Definition at line 25 of file test_orientation_tools.cpp.

References almostEqual().

25  {
26  // check matrix "almostEqual" function
27  Mat3<double> a, b;
28  a << 1, 2, 3, 4, 5, 6, 7, 8, 9.2;
29  b << 1, 2, 3, 4, 5, 6, 7, 8, 9.1;
30  EXPECT_EQ(true, almostEqual(a, b, .3));
31  EXPECT_EQ(false, almostEqual(a, b, .01));
32 
33  DVec<double> qdd(4);
34  qdd << 1, 2, 3, 4;
35 
36  DVec<double> qdd2 = qdd;
37 
38  EXPECT_TRUE(almostEqual(qdd, qdd2, 1e-6));
39  qdd2(1) += .2;
40  EXPECT_FALSE(almostEqual(qdd, qdd2, 1e-6));
41 
42  DMat<double> testDynamicMat(3, 3);
43  DMat<double> testDynamicMat2(3, 3);
44  testDynamicMat << 1, 2, 3, 4, 5, 6, 7, 8, 9.2;
45  testDynamicMat2 << 1, 2, 3, 4, 5, 6, 7, 8, 9.1;
46  EXPECT_EQ(true, almostEqual(testDynamicMat, testDynamicMat2, .3));
47  EXPECT_EQ(false, almostEqual(testDynamicMat, testDynamicMat2, .01));
48 }
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > DMat
Definition: cppTypes.h:106
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
typename Eigen::Matrix< T, Eigen::Dynamic, 1 > DVec
Definition: cppTypes.h:102

+ Here is the call graph for this function:

TEST ( Orientation  ,
coordinateRotation   
)

Check the coordinateRotation algorithm which generates rotaiton matrices for axis rotations

Definition at line 54 of file test_orientation_tools.cpp.

References ori::coordinateRotation(), ori::X, ori::Y, and ori::Z.

54  {
55  // check rotation matrices
56  double s = std::sin(.2);
57  double c = std::cos(.2);
58  Mat3<double> R_ref_x, R_ref_y, R_ref_z;
59  R_ref_x << 1, 0, 0, 0, c, s, 0, -s, c;
60  R_ref_y << c, 0, -s, 0, 1, 0, s, 0, c;
61  R_ref_z << c, s, 0, -s, c, 0, 0, 0, 1;
62  EXPECT_EQ(R_ref_x, coordinateRotation(CoordinateAxis::X, .2));
63  EXPECT_EQ(R_ref_y, coordinateRotation(CoordinateAxis::Y, .2));
64  EXPECT_EQ(R_ref_z, coordinateRotation(CoordinateAxis::Z, .2));
65 }
Mat3< T > coordinateRotation(CoordinateAxis axis, T theta)
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54

+ Here is the call graph for this function:

TEST ( Orientation  ,
skew   
)

Check the skew functions, which go between vectors and skew-symmetric matrices

Definition at line 71 of file test_orientation_tools.cpp.

References ori::matToSkewVec(), and ori::vectorToSkewMat().

71  {
72  // check skew vec->mat and mat->vec
73  Mat3<double> A, B;
74  A << 8, 1, 6, 3, 5, 7, 4, 9, 2;
75  B << 0, -3, 2, 3, 0, -1, -2, 1, 0;
76  Vec3<double> v(1, 1, 1);
77  Vec3<double> w(1, 2, 3);
78 
79  EXPECT_EQ(matToSkewVec(A), v);
80  EXPECT_EQ(B, vectorToSkewMat(w));
81  EXPECT_EQ(w, matToSkewVec(vectorToSkewMat(w)));
82  EXPECT_EQ(v, matToSkewVec(vectorToSkewMat(v)));
83 }
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
Mat3< typename T::Scalar > vectorToSkewMat(const Eigen::MatrixBase< T > &v)
Vec3< typename T::Scalar > matToSkewVec(const Eigen::MatrixBase< T > &m)

+ Here is the call graph for this function:

TEST ( Orientation  ,
quatToRotm   
)

Check the quaternion to rotation matrix and rotation matrix to quaternion functions

Definition at line 89 of file test_orientation_tools.cpp.

References almostEqual(), ori::quaternionToRotationMatrix(), and ori::rotationMatrixToQuaternion().

89  {
90  // check R -> q and q -> R
91  Quat<double> q(.9672, -0.0672, -0.1653, -0.1808);
92  Mat3<double> R;
93  R << .8799, .3720, -.2955, -.3276, .9256, .1898, .3441, -.0702, .9363;
94  Quat<double> q2 = rotationMatrixToQuaternion(R.transpose());
95  Mat3<double> R2 = quaternionToRotationMatrix(q).transpose();
96  EXPECT_TRUE(almostEqual(q2, q, .0001));
97  EXPECT_TRUE(almostEqual(R2, R, .0001));
98 }
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
Quat< typename T::Scalar > rotationMatrixToQuaternion(const Eigen::MatrixBase< T > &r1)
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
Mat3< typename T::Scalar > quaternionToRotationMatrix(const Eigen::MatrixBase< T > &q)

+ Here is the call graph for this function:

TEST ( Orientation  ,
quatToRpy   
)

Test the quaternion to roll-pitch-yaw conversion

Definition at line 103 of file test_orientation_tools.cpp.

References almostEqual(), and ori::quatToRPY().

103  {
104  // check q -> rpy
105  Quat<double> q(0.9672, -0.0672, -0.1653, -0.1808);
106  Vec3<double> rpy1(-0.0748, -0.3513, -0.3564);
107  EXPECT_TRUE(almostEqual(rpy1, quatToRPY(q), .1));
108 }
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
Vec3< typename T::Scalar > quatToRPY(const Eigen::MatrixBase< T > &q)
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23

+ Here is the call graph for this function:

TEST ( Orientation  ,
quatToRpy2   
)

Test the quaternion to roll-pitch-yaw conversion direction

Definition at line 113 of file test_orientation_tools.cpp.

References almostEqual(), ori::coordinateRotation(), ori::quatToRPY(), ori::rotationMatrixToQuaternion(), and ori::Y.

113  {
114  // check q -> rpy
115  RotMat<double> R = coordinateRotation(CoordinateAxis::Y, .5);
117  Vec3<double> rpy = quatToRPY(q);
118  Vec3<double> rpy_ref(0, .5, 0);
119  EXPECT_TRUE(almostEqual(rpy, rpy_ref, .00001));
120 }
Mat3< T > coordinateRotation(CoordinateAxis axis, T theta)
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
Vec3< typename T::Scalar > quatToRPY(const Eigen::MatrixBase< T > &q)
Quat< typename T::Scalar > rotationMatrixToQuaternion(const Eigen::MatrixBase< T > &r1)
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
typename Eigen::Matrix< T, 3, 3 > RotMat
Definition: cppTypes.h:18

+ Here is the call graph for this function:

TEST ( Orientation  ,
quatToRpy3   
)

Check that rpy is in roll-pitch-yaw order

Definition at line 125 of file test_orientation_tools.cpp.

References almostEqual(), ori::coordinateRotation(), ori::quatToRPY(), ori::rotationMatrixToQuaternion(), and ori::Z.

125  {
126  // check q -> rpy
127  RotMat<double> R = coordinateRotation(CoordinateAxis::Z, .5);
129  Vec3<double> rpy = quatToRPY(q);
130  Vec3<double> rpy_ref(0, 0, 0.5);
131  EXPECT_TRUE(almostEqual(rpy, rpy_ref, .00001));
132 }
Mat3< T > coordinateRotation(CoordinateAxis axis, T theta)
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
Vec3< typename T::Scalar > quatToRPY(const Eigen::MatrixBase< T > &q)
Quat< typename T::Scalar > rotationMatrixToQuaternion(const Eigen::MatrixBase< T > &r1)
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
typename Eigen::Matrix< T, 3, 3 > RotMat
Definition: cppTypes.h:18

+ Here is the call graph for this function:

TEST ( Orienation  ,
quaternionDerivative   
)

Check the quaternion derivative function. This isn't used and probably should be removed

Definition at line 138 of file test_orientation_tools.cpp.

References almostEqual(), and ori::quatDerivative().

138  {
139  Quat<double> ref(-10.8376, -0.6752, -2.5128, -1.3504);
140  Quat<double> q =
141  quatDerivative(Quat<double>(1, 2, 3, 4), Vec3<double>(1, 2, 3));
142  EXPECT_TRUE(almostEqual(q, ref, .0005));
143 }
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
Quat< typename T::Scalar > quatDerivative(const Eigen::MatrixBase< T > &q, const Eigen::MatrixBase< T2 > &omega)

+ Here is the call graph for this function:

TEST ( Orientation  ,
quaternionProduct   
)

Check the quaternion product

Definition at line 148 of file test_orientation_tools.cpp.

References almostEqual(), and ori::quatProduct().

148  {
149  Quat<double> q1(1, 2, 3, 4);
150  Quat<double> q2(5, 6, 7, 8);
151  Quat<double> ref(-60, 12, 30, 24);
152  EXPECT_TRUE(almostEqual(ref, quatProduct(q1, q2), .0001));
153 }
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
Quat< typename T::Scalar > quatProduct(const Eigen::MatrixBase< T > &q1, const Eigen::MatrixBase< T > &q2)
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23

+ Here is the call graph for this function:

TEST ( Orientation  ,
quaternionProductDirection   
)

Definition at line 155 of file test_orientation_tools.cpp.

References almostEqual(), ori::coordinateRotation(), ori::quaternionToRotationMatrix(), ori::quatProduct(), ori::rotationMatrixToQuaternion(), ori::X, ori::Y, and ori::Z.

155  {
156  RotMat<double> R1 = coordinateRotation(CoordinateAxis::X, 7.23) *
157  coordinateRotation(CoordinateAxis::Y, -2.343) *
158  coordinateRotation(CoordinateAxis::Z, 1.2324);
159 
160  RotMat<double> R2 = coordinateRotation(CoordinateAxis::Z, .3231) *
161  coordinateRotation(CoordinateAxis::X, -4.2332) *
162  coordinateRotation(CoordinateAxis::Y, -3.3213);
163 
164  RotMat<double> R12_ref = R2 * R1;
165 
168  Quat<double> Q12 = quatProduct(Q1, Q2);
169 
171 
172  EXPECT_TRUE(almostEqual(R12, R12_ref, .0001));
173 }
Mat3< T > coordinateRotation(CoordinateAxis axis, T theta)
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
Quat< typename T::Scalar > rotationMatrixToQuaternion(const Eigen::MatrixBase< T > &r1)
Quat< typename T::Scalar > quatProduct(const Eigen::MatrixBase< T > &q1, const Eigen::MatrixBase< T > &q2)
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
Mat3< typename T::Scalar > quaternionToRotationMatrix(const Eigen::MatrixBase< T > &q)
typename Eigen::Matrix< T, 3, 3 > RotMat
Definition: cppTypes.h:18

+ Here is the call graph for this function:

TEST ( Orientation  ,
quaternionIntegration   
)

Check that the quaternion integration goes in the correct direction

Definition at line 178 of file test_orientation_tools.cpp.

References almostEqual(), ori::coordinateRotation(), ori::integrateQuat(), ori::quaternionToRotationMatrix(), ori::rotationMatrixToQuaternion(), ori::X, ori::Y, and ori::Z.

178  {
180 
181  Vec3<double> omegaX(1, 0, 0);
182  Vec3<double> omegaY(0, -1, 0);
183  Vec3<double> omegaZ(0, 0, 1);
184 
185  RotMat<double> rot_x_ref = coordinateRotation(CoordinateAxis::X, .1);
186  RotMat<double> rot_y_ref = coordinateRotation(CoordinateAxis::Y, -.1);
187  RotMat<double> rot_z_ref = coordinateRotation(CoordinateAxis::Z, .1);
188 
190 
191  Quat<double> rot_x_quat = integrateQuat(eyeQ, omegaX, .1);
192  Quat<double> rot_y_quat = integrateQuat(eyeQ, omegaY, .1);
193  Quat<double> rot_z_quat = integrateQuat(eyeQ, omegaZ, .1);
194 
195  EXPECT_TRUE(
196  almostEqual(quaternionToRotationMatrix(rot_x_quat), rot_x_ref, .0001));
197  EXPECT_TRUE(
198  almostEqual(quaternionToRotationMatrix(rot_y_quat), rot_y_ref, .0001));
199  EXPECT_TRUE(
200  almostEqual(quaternionToRotationMatrix(rot_z_quat), rot_z_ref, .0001));
201 }
Mat3< T > coordinateRotation(CoordinateAxis axis, T theta)
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
Quat< typename T::Scalar > integrateQuat(const Eigen::MatrixBase< T > &quat, const Eigen::MatrixBase< T2 > &omega, T3 dt)
Quat< typename T::Scalar > rotationMatrixToQuaternion(const Eigen::MatrixBase< T > &r1)
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
Mat3< typename T::Scalar > quaternionToRotationMatrix(const Eigen::MatrixBase< T > &q)
typename Eigen::Matrix< T, 3, 3 > RotMat
Definition: cppTypes.h:18

+ Here is the call graph for this function:

TEST ( Orientation  ,
rpyToRotMat   
)

Check rpy to rotation matrix

Definition at line 206 of file test_orientation_tools.cpp.

References almostEqual(), ori::deg2rad(), ori::quatToRPY(), ori::rotationMatrixToQuaternion(), and ori::rpyToRotMat().

206  {
207  Vec3<double> rpy(deg2rad(20.), deg2rad(34.), deg2rad(160.));
208  Mat3<double> R = rpyToRotMat(rpy);
210  Vec3<double> rpyAgain = quatToRPY(q);
211  EXPECT_TRUE(almostEqual(rpyAgain, rpy, .001));
212  std::cout << "1: " << rpy.transpose() << "\n2: " << rpyAgain.transpose()
213  << "\n";
214 }
Mat3< typename T::Scalar > rpyToRotMat(const Eigen::MatrixBase< T > &v)
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
Vec3< typename T::Scalar > quatToRPY(const Eigen::MatrixBase< T > &q)
Quat< typename T::Scalar > rotationMatrixToQuaternion(const Eigen::MatrixBase< T > &r1)
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
T deg2rad(T deg)

+ Here is the call graph for this function:

TEST ( Orientation  ,
allOrientationConversions   
)

Check all 6 possible conversions between rotation matrix, quaternion, and rpy

Definition at line 219 of file test_orientation_tools.cpp.

References almostEqual(), ori::deg2rad(), ori::quaternionToRotationMatrix(), ori::quatToRPY(), ori::rotationMatrixToQuaternion(), ori::rotationMatrixToRPY(), ori::rpyToQuat(), and ori::rpyToRotMat().

219  {
220  Vec3<double> refRPY(deg2rad(20.), deg2rad(34.), deg2rad(160.));
221  // do all rpy -> conversions
222  Quat<double> quatFromRPY = rpyToQuat(refRPY);
223  Mat3<double> rFromRPY = rpyToRotMat(refRPY);
224 
225  // do all quat -> conversions
226  Mat3<double> rFromQuat = quaternionToRotationMatrix(quatFromRPY);
227  Vec3<double> rpyFromQuat = quatToRPY(quatFromRPY);
228 
229  // do all r -> conversions
230  Vec3<double> rpyFromR = rotationMatrixToRPY(rFromRPY);
231  Quat<double> quatFromR = rotationMatrixToQuaternion(rFromQuat);
232 
233  EXPECT_TRUE(almostEqual(refRPY, rpyFromQuat, .0001));
234  EXPECT_TRUE(almostEqual(refRPY, rpyFromR, .0001));
235 
236  EXPECT_TRUE(almostEqual(rFromQuat, rFromRPY, .0001));
237 
238  EXPECT_TRUE(almostEqual(quatFromR, quatFromRPY, .0001));
239 }
Mat3< typename T::Scalar > rpyToRotMat(const Eigen::MatrixBase< T > &v)
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
Vec3< typename T::Scalar > quatToRPY(const Eigen::MatrixBase< T > &q)
Quat< typename T::Scalar > rotationMatrixToQuaternion(const Eigen::MatrixBase< T > &r1)
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
Quat< typename T::Scalar > rpyToQuat(const Eigen::MatrixBase< T > &rpy)
T deg2rad(T deg)
Vec3< typename T::Scalar > rotationMatrixToRPY(const Eigen::MatrixBase< T > &R)
Mat3< typename T::Scalar > quaternionToRotationMatrix(const Eigen::MatrixBase< T > &q)

+ Here is the call graph for this function: