test_icp.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Fetch Robotics Inc.
3  * Author: Michael Ferguson
4  */
5 
6 #include <gtest/gtest.h>
8 
9 // TODO: move this to perception.h or something
10 void getIdealTarget(std::vector<geometry_msgs::Point>& points)
11 {
12  points.clear();
13 
14  // Each side is 100mm long, at 45 degree angle
15  for (double x = 0.0; x < 0.05 /*0.0707106*/; x += 0.01)
16  {
17  geometry_msgs::Point p;
18  p.x = x;
19  p.y = -0.15 - x;
20  p.z = 0.0;
21  points.push_back(p);
22  }
23  // Front face is 300mm long
24  for (double y = -0.15; y <= 0.15; y += 0.01)
25  {
26  geometry_msgs::Point p;
27  p.x = 0.0; //-0.00487805;
28  p.y = y;
29  p.z = 0.0;
30  points.push_back(p);
31  }
32  // Each side is 100mm long, at 45 degree angle
33  for (double x = 0.0; x < 0.05 /*0.0707106*/; x += 0.001)
34  {
35  geometry_msgs::Point p;
36  p.x = x;
37  p.y = 0.15 + x;
38  p.z = 0.0;
39  points.push_back(p);
40  }
41 }
42 
43 TEST(TestICP, test_already_aligned)
44 {
45  std::vector<geometry_msgs::Point> source;
46  std::vector<geometry_msgs::Point> target;
47 
48  getIdealTarget(source);
49  getIdealTarget(target);
50 
51  geometry_msgs::Transform transform;
52 
53  EXPECT_TRUE(icp_2d::alignICP(source, target, transform));
54  EXPECT_NEAR(0.0, transform.translation.x, 1e-6);
55  EXPECT_NEAR(0.0, transform.translation.y, 1e-6);
56  EXPECT_NEAR(0.0, transform.rotation.x, 1e-8);
57  EXPECT_NEAR(0.0, transform.rotation.y, 1e-8);
58  EXPECT_NEAR(0.0, transform.rotation.z, 1e-6);
59  EXPECT_NEAR(1.0, transform.rotation.w, 1e-6);
60 }
61 
62 TEST(TestICP, test_shifted)
63 {
64  std::vector<geometry_msgs::Point> source;
65  std::vector<geometry_msgs::Point> target;
66 
67  getIdealTarget(source);
68  getIdealTarget(target);
69  source = icp_2d::transform(source, 0.1, 0.1, 0);
70 
71  geometry_msgs::Transform transform;
72 
73  EXPECT_TRUE(icp_2d::alignICP(source, target, transform));
74  EXPECT_NEAR(-0.1, transform.translation.x, 0.002);
75  EXPECT_NEAR(-0.1, transform.translation.y, 0.002);
76  EXPECT_NEAR(0.0, transform.rotation.x, 1e-8);
77  EXPECT_NEAR(0.0, transform.rotation.y, 1e-8);
78  EXPECT_NEAR(0.0, transform.rotation.z, 1e-8);
79  EXPECT_NEAR(1.0, transform.rotation.w, 1e-8);
80 }
81 
82 TEST(TestICP, test_rotated)
83 {
84  std::vector<geometry_msgs::Point> source;
85  std::vector<geometry_msgs::Point> target;
86 
87  getIdealTarget(source);
88  getIdealTarget(target);
89  source = icp_2d::transform(source, 0, 0, 0.2);
90 
91  geometry_msgs::Transform transform;
92 
93  EXPECT_TRUE(icp_2d::alignICP(source, target, transform));
94  EXPECT_NEAR(0.0, transform.translation.x, 0.002);
95  EXPECT_NEAR(0.0, transform.translation.y, 0.002);
96  EXPECT_NEAR(0.0, transform.rotation.x, 1e-8);
97  EXPECT_NEAR(0.0, transform.rotation.y, 1e-8);
98  double theta = icp_2d::thetaFromQuaternion(transform.rotation);
99  EXPECT_NEAR(-0.2, theta, 1e-2);
100 }
101 
102 TEST(TestICP, test_shifted_rotated)
103 {
104  std::vector<geometry_msgs::Point> source, source_t;
105  std::vector<geometry_msgs::Point> target;
106 
107  getIdealTarget(source);
108  getIdealTarget(target);
109  source_t = icp_2d::transform(source, 0.1, 0.2, 0.3);
110 
111  geometry_msgs::Transform transform;
112  EXPECT_TRUE(icp_2d::alignICP(source_t, target, transform));
113  EXPECT_NEAR(0.0, transform.rotation.x, 1e-8);
114  EXPECT_NEAR(0.0, transform.rotation.y, 1e-8);
115  double theta = icp_2d::thetaFromQuaternion(transform.rotation);
116  EXPECT_NEAR(-0.3, theta, 1e-3);
117 
118  source_t = icp_2d::transform(source_t,
119  transform.translation.x,
120  transform.translation.y,
121  icp_2d::thetaFromQuaternion(transform.rotation));
122 
123  for (size_t i = 0; i < source.size(); i++)
124  {
125  EXPECT_NEAR(source[i].x, source_t[i].x, 0.002);
126  EXPECT_NEAR(source[i].y, source_t[i].y, 0.002);
127  }
128 }
129 
130 // Run all the tests that were declared with TEST()
131 int main(int argc, char **argv)
132 {
133  testing::InitGoogleTest(&argc, argv);
134  return RUN_ALL_TESTS();
135 }
void getIdealTarget(std::vector< geometry_msgs::Point > &points)
Definition: test_icp.cpp:10
std::vector< geometry_msgs::Point > transform(const std::vector< geometry_msgs::Point > &points, double x, double y, double theta)
Transform a vector of points in 2d.
Definition: icp_2d.cpp:41
TEST(TestICP, test_already_aligned)
Definition: test_icp.cpp:43
double alignICP(const std::vector< geometry_msgs::Point > source, const std::vector< geometry_msgs::Point > target, geometry_msgs::Transform &transform, size_t max_iterations=10, double min_delta_rmsd=0.000001)
Perform Iterative Closest Point (ICP) algorithm to align two point clouds in a two dimensional plane...
Definition: icp_2d.cpp:289
double thetaFromQuaternion(const geometry_msgs::Quaternion &q)
Get the 2d rotation from a quaternion.
Definition: icp_2d.cpp:28
int main(int argc, char **argv)
Definition: test_icp.cpp:131


fetch_open_auto_dock
Author(s): Michael Ferguson, Griswald Brooks
autogenerated on Mon Feb 28 2022 22:21:37