geolib2
test_lrf.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
4 #include <geolib/datatypes.h>
5 
6 #include <cmath>
7 
8 
9 class TestLRF : public testing::Test
10 {
11 protected:
12  TestLRF(double angle_min_=-M_PI, double angle_max_=M_PI, double range_min_=0.2, double range_max_=60., uint n_beams_=9) :
13  angle_min(angle_min_), angle_max(angle_max_), range_min(range_min_), range_max(range_max_), n_beams(n_beams_)
14  {
15  }
16 
17  virtual ~TestLRF()
18  {
19  }
20 
21  void SetUp() override
22  {
26 
28  }
29 
31 
32  const double angle_min;
33  const double angle_max;
34  const double range_min;
35  const double range_max;
36  const uint n_beams;
37 
39 };
40 
41 class TestLRF2 : public TestLRF
42 {
43 protected:
44  TestLRF2() : TestLRF(-M_PI_2, M_PI_2)
45  {
46  }
47 
48  virtual ~TestLRF2()
49  {
50  }
51 };
52 
53 class TestLRF3 : public TestLRF
54 {
55 protected:
56  TestLRF3() : TestLRF(-M_PI_2, M_PI_2, 0.2, 60., 30)
57  {
58  }
59 
60  virtual ~TestLRF3()
61  {
62  }
63 };
64 
65 TEST_F(TestLRF, renderLineFront)
66 {
67  geo::Vec2d p1(1.0, -1.0);
68  geo::Vec2d p2(1.0, 1.0);
69 
70  lrf.renderLine(p1, p2, ranges);
71  ASSERT_EQ(lrf.getAngleUpperIndex(0.0) -1, 4);
72  ASSERT_EQ(ranges[lrf.getAngleUpperIndex(0.0) -1], 1);
73 }
74 
75 TEST_F(TestLRF, renderLineLeft)
76 {
77  geo::Vec2d p1(1.0, 1.0);
78  geo::Vec2d p2(-1.0, 1.0);
79 
80  lrf.renderLine(p1, p2, ranges);
81  ASSERT_EQ(ranges[lrf.getAngleUpperIndex(M_PI_2) -1], 1.0);
82 }
83 
84 TEST_F(TestLRF, renderLineRight)
85 {
86  geo::Vec2d p1(-1.0, -1.0);
87  geo::Vec2d p2(1.0, -1.0);
88 
89  lrf.renderLine(p1, p2, ranges);
90  ASSERT_EQ(ranges[lrf.getAngleUpperIndex(-M_PI_2) -1], 1.0);
91 }
92 
93 TEST_F(TestLRF, renderLineBack)
94 {
95  geo::Vec2d p1(-1.0, 1.0);
96  geo::Vec2d p2(-1.0, -1.0);
97 
98  lrf.renderLine(p1, p2, ranges);
99  ASSERT_EQ(ranges[lrf.getAngleUpperIndex(-M_PI) -1], 1.0);
100  ASSERT_EQ(ranges[lrf.getAngleUpperIndex(M_PI) - 1], 1.0);
101 }
102 
103 TEST_F(TestLRF3, renderLineWeird)
104 {
105  double a1 = angle_max - 0.1; // Point in view of the robot -> angle_min < a1 < angle_max
106  double a2 = angle_min - 1.0; // Point in the blind spot of the robot but with a positive angle -> a2 > 0
107 
108  // The line connecting these two points should pass behind the robot -> a1 - a2 > M_PI
109  // However the difference between the a1 and the lower angle limit is less than half a circle -> a1 + a_max < M_PI
110  geo::Vec2d p1(cos(a1), sin(a1));
111  geo::Vec2d p2(cos(a2), sin(a2));
112 
113  lrf.renderLine(p1, p2, ranges);
114 
115  uint upper_index_a1 = lrf.getAngleUpperIndex(a1);
116  // The rendered line passes behind the robot. But not through the blindspot. Therefore the first index should remain untouched
117  for (uint i = 0; i<upper_index_a1-1; ++i)
118  {
119  ASSERT_EQ(ranges[i], range_max) << "Range at index [" << i << "] should not be rendered. Instead is rendered to " << ranges[i];
120  }
121  // At a1 we should see the switch between rendered and not rendered. indices above a1 should be rendered. Because points lie on a unit circle they should be <= 1
122  for (uint i = upper_index_a1; i<n_beams; ++i)
123  {
124  ASSERT_LE(ranges[i], 1.) << "Range at index [" << i << "] should be rendered to <=1. Instead its value is " << ranges[i];
125  }
126 }
127 
128 TEST_F(TestLRF3, renderLineRight2)
129 {
130  geo::Vec2d p1(-1.0, -1.0);
131  geo::Vec2d p2(1.0, -1.0);
132 
133  lrf.renderLine(p1, p2, ranges);
134 
135  uint upper_index_p2 = lrf.getAngleUpperIndex(p2.x, p2.y);
136  // The rendered line passes the robot on the right and ends in the blindspot.
137  for (uint i = 0; i<upper_index_p2-1; ++i)
138  {
139  ASSERT_LE(ranges[i], sqrt(2)) << "Range at index [" << i << "] should be rendered to <=" << sqrt(2) << ". Instead its value is " << ranges[i];
140  }
141  for (uint i = upper_index_p2; i<n_beams; ++i)
142  {
143  ASSERT_EQ(ranges[i], range_max) << "Range at index [" << i << "] should not be rendered. Instead is rendered to " << ranges[i];
144  }
145 }
146 
147 TEST_F(TestLRF2, getAngleUpperIndexAngle)
148 {
149  ASSERT_EQ(lrf.getAngleUpperIndex(1.5*angle_min), 0);
150  ASSERT_EQ(lrf.getAngleUpperIndex(angle_min), 1);
151  ASSERT_EQ(lrf.getAngleUpperIndex(0.5*angle_min + 0.5*angle_max), std::floor(0.5*n_beams-0.5)+1);
152  ASSERT_EQ(lrf.getAngleUpperIndex(angle_max), n_beams);
153  ASSERT_EQ(lrf.getAngleUpperIndex(1.5*angle_max), n_beams);
154 }
155 
156 TEST_F(TestLRF2, getAngleUpperIndexXY)
157 {
158  ASSERT_EQ(lrf.getAngleUpperIndex(cos(1.5*angle_min), sin(1.5*angle_min)), 0);
159  ASSERT_EQ(lrf.getAngleUpperIndex(cos(angle_min), sin(angle_min)), 1);
160  ASSERT_EQ(lrf.getAngleUpperIndex(cos(0.5*angle_min + 0.5*angle_max), sin(0.5*angle_min + 0.5*angle_max)), std::floor(0.5*n_beams - 0.5) + 1);
161  ASSERT_EQ(lrf.getAngleUpperIndex(cos(angle_max), sin(angle_max)), n_beams);
162  ASSERT_EQ(lrf.getAngleUpperIndex(cos(1.5*angle_max), sin(1.5*angle_max)), n_beams);
163 }
164 
165 TEST_F(TestLRF, getAngleUpperIndexUnitCircle)
166 {
167  ASSERT_EQ(lrf.getAngleUpperIndex(-1.0, 0.0)-1, 8);
168  ASSERT_EQ(lrf.getAngleUpperIndex(-1.0, -1.0)-1, 1);
169  ASSERT_EQ(lrf.getAngleUpperIndex(0.0, -1.0)-1, 2);
170  ASSERT_EQ(lrf.getAngleUpperIndex(1.0, -1.0)-1, 3);
171  ASSERT_EQ(lrf.getAngleUpperIndex(1.0, 0.0)-1, 4);
172  ASSERT_EQ(lrf.getAngleUpperIndex(1.0, 1.0)-1, 5);
173  ASSERT_EQ(lrf.getAngleUpperIndex(0.0, 1.0)-1, 6);
174  ASSERT_EQ(lrf.getAngleUpperIndex(-1.0, 1.0)-1, 7);
175  ASSERT_EQ(lrf.getAngleUpperIndex(0.0, 0.0), 5);
176 }
177 
178 int main(int argc, char **argv)
179 {
180  testing::InitGoogleTest(&argc, argv);
181  return RUN_ALL_TESTS();
182 }
geo::LaserRangeFinder::setAngleLimits
void setAngleLimits(double min, double max)
Definition: LaserRangeFinder.cpp:285
std::vector::resize
T resize(T... args)
std::floor
T floor(T... args)
TestLRF::ranges
std::vector< double > ranges
Definition: test_lrf.cpp:38
TestLRF3::~TestLRF3
virtual ~TestLRF3()
Definition: test_lrf.cpp:60
TestLRF2::TestLRF2
TestLRF2()
Definition: test_lrf.cpp:44
geo::LaserRangeFinder::setRangeLimits
void setRangeLimits(double min, double max)
Definition: LaserRangeFinder.h:67
geo::Vec2T::y
T y
Definition: math_types.h:114
std::vector< double >
TEST_F
TEST_F(TestLRF, renderLineFront)
Definition: test_lrf.cpp:65
geo::Vec2T::x
T x
Definition: math_types.h:114
cmath
TestLRF::~TestLRF
virtual ~TestLRF()
Definition: test_lrf.cpp:17
datatypes.h
TestLRF::SetUp
void SetUp() override
Definition: test_lrf.cpp:21
TestLRF::angle_max
const double angle_max
Definition: test_lrf.cpp:33
TestLRF::TestLRF
TestLRF(double angle_min_=-M_PI, double angle_max_=M_PI, double range_min_=0.2, double range_max_=60., uint n_beams_=9)
Definition: test_lrf.cpp:12
TestLRF::range_max
const double range_max
Definition: test_lrf.cpp:35
TestLRF::angle_min
const double angle_min
Definition: test_lrf.cpp:32
main
int main(int argc, char **argv)
Definition: test_lrf.cpp:178
TestLRF2::~TestLRF2
virtual ~TestLRF2()
Definition: test_lrf.cpp:48
geo::LaserRangeFinder::setNumBeams
void setNumBeams(uint n)
Definition: LaserRangeFinder.cpp:293
geo::LaserRangeFinder
Definition: LaserRangeFinder.h:13
TestLRF2
Definition: test_lrf.cpp:41
TestLRF
Definition: test_lrf.cpp:9
TestLRF3::TestLRF3
TestLRF3()
Definition: test_lrf.cpp:56
TestLRF::lrf
geo::LaserRangeFinder lrf
Definition: test_lrf.cpp:30
TestLRF::range_min
const double range_min
Definition: test_lrf.cpp:34
TestLRF::n_beams
const uint n_beams
Definition: test_lrf.cpp:36
TestLRF3
Definition: test_lrf.cpp:53
LaserRangeFinder.h
geo::Vec2T
Definition: math_types.h:24