geolib2
test_matrix.cpp
Go to the documentation of this file.
1 #include <geolib/math_types.h>
3 #include <iostream>
4 
5 #include <profiling/Timer.h>
6 
7 //#define USE_TF
8 
9 #include <tf/transform_datatypes.h>
10 
11 #include <stdlib.h> /* srand, rand */
12 #include <time.h> /* time */
13 
14 // ----------------------------------------------------------------------------------------------------
15 
16 double random(double min, double max) {
17  return ((double)rand() / RAND_MAX) * (max - min) + min;
18 }
19 
20 // ----------------------------------------------------------------------------------------------------
21 
23  tf::Matrix3x3 b;
24  b.setEulerYPR(random(0, 6.283), random(0, 6.283), random(0, 6.283));
25  return tf::Transform(b, tf::Vector3(random(-10, 10), random(-10, 10), random(-10, 10)));
26 }
27 
28 // ----------------------------------------------------------------------------------------------------
29 
30 void print(const tf::Matrix3x3& m) {
31  std::cout << "[ " << m[0][0] << " " << m[0][1] << " " << m[0][2] << std::endl;
32  std::cout << " " << m[1][0] << " " << m[1][1] << " " << m[1][2] << std::endl;
33  std::cout << " " << m[2][0] << " " << m[2][1] << " " << m[2][2] << " ]" << std::endl;
34 }
35 void print(const tf::Vector3& v) {
36  std::cout << "[ " << v[0] << " " << v[1] << " " << v[2] << " ]" << std::endl;
37 }
38 
39 void print(const tf::Transform& t) {
40  print(t.getBasis());
41  print(t.getOrigin());
42 }
43 
44 // ----------------------------------------------------------------------------------------------------
45 
46 bool equals(double a, double b) {
47  return std::abs(a - b) < 1e-10;
48 }
49 
50 bool equals(const geo::Transform3& T, const tf::Transform& T_tf) {
51  if ( !equals(T.t.x, T_tf.getOrigin().getX()) ||
52  !equals(T.t.y, T_tf.getOrigin().getY()) ||
53  !equals(T.t.z, T_tf.getOrigin().getZ())) {
54  return false;
55  }
56 
58  tf::Quaternion q_tf = T_tf.getRotation();
59 
60  if ( !equals(q.x, q_tf.getX()) ||
61  !equals(q.y, q_tf.getY()) ||
62  !equals(q.z, q_tf.getZ()) ||
63  !equals(q.w, q_tf.getW())) {
64  return false;
65  }
66 
67  return true;
68 }
69 
70 bool equals(const geo::Transform3& T, const tf::Transform& T_tf, const std::string& msg) {
71  if (!equals(T, T_tf)) {
72  std::cout << "ERROR\t" << msg << std::endl;
73  std::cout << "TF: " << std::endl;
74  print(T_tf);
76  std::cout << "GEOLIB:" << std::endl << T << std::endl;
77  return false;
78  } else {
79  std::cout << "OK\t" << msg << std::endl;
80  return true;
81  }
82 }
83 
84 // ----------------------------------------------------------------------------------------------------
85 
86 int main() {
87 
88  // initialize random seed
89  srand (time(NULL));
90 
93 
94  geo::Transform3 t1, t2;
95  geo::convert(t1_tf, t1);
96  geo::convert(t2_tf, t2);
97 
98  if ( !equals(t1, t1_tf, "equals(Transform, Transform)") ||
99  !equals(t1 * t2, t1_tf * t2_tf, "Transform * Transform") ||
100  !equals(t1.inverse() * t2, t1_tf.inverse() * t2_tf, "Transform.inverse() * Transform") ||
101  !equals(t1.inverseTimes(t2), t1_tf.inverseTimes(t2_tf), "Transform.inverseTimes(Transform)") ||
102  !equals(geo::Transform3::identity() * t1, t1_tf, "Transform.identity() * Transform")) {
103 
104  std::cout << "ERROR" << std::endl;
105  return -1;
106  }
107 
108  std::cout << "All OK" << std::endl;
109 
110  return 0;
111 }
math_types.h
geo::Vector3
Vec3 Vector3
Definition: datatypes.h:32
geo::Transform3T::getQuaternion
QuaternionT< T > getQuaternion() const
Definition: math_types.h:734
geo::QuaternionT
Definition: math_types.h:322
std::string
t
Timer t
a
void a()
geo::Transform3T::identity
static Transform3T identity()
Definition: math_types.h:778
geo::Transform3T
Definition: math_types.h:19
iostream
geo::QuaternionT::w
T w
Definition: math_types.h:391
std::cout
geo::Transform3T::inverse
Transform3T inverse() const
Definition: math_types.h:747
tf_conversions.h
geo::Transform3T::inverseTimes
Transform3T inverseTimes(const Transform3T &tr) const
Definition: math_types.h:725
geo::QuaternionT::y
T y
Definition: math_types.h:391
geo::QuaternionT::x
T x
Definition: math_types.h:391
geo::Transform3T::t
Vec3T< T > t
Definition: math_types.h:794
Timer.h
randomTransform
tf::Transform randomTransform()
Definition: test_matrix.cpp:22
random
double random(double min, double max)
Definition: test_matrix.cpp:16
main
int main()
Definition: test_matrix.cpp:86
b
void b()
std::endl
T endl(T... args)
equals
bool equals(double a, double b)
Definition: test_matrix.cpp:46
geo::convert
void convert(const geo::Vector3 &v, geometry_msgs::Point &msg)
converting geo::Vector3 to geometry_msgs::Point message
Definition: msg_conversions.h:23
geo::QuaternionT::z
T z
Definition: math_types.h:391
geo::Transform
Transform3 Transform
Definition: datatypes.h:28
print
void print(const tf::Matrix3x3 &m)
Definition: test_matrix.cpp:30
geo::Quaternion
QuaternionT< real > Quaternion
Definition: datatypes.h:34