sensor_data_test.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <functional>
4 
5 #include "../../../src/core/states/sensor_data.h"
6 
7 #include "../mock_grid_cell.h"
8 #include "../../../src/core/maps/plain_grid_map.h"
9 #include "../../../src/utils/data_generation/map_primitives.h"
10 #include "../../../src/utils/data_generation/grid_map_patcher.h"
11 #include "../../../src/utils/data_generation/laser_scan_generator.h"
12 
13 
14 //----------------------------------------------------------------------------//
15 // ScanPoint2D
16 
17 class ScanPoint2DTest : public ::testing::Test {
18 protected: // types
20 protected: // consts
21  static constexpr double Acc_Error = 0.001;
22 protected: // methods
23 
25  const double range, const double angle_rad,
26  const double x, const double y) {
27  ScanPoint2D sp;
28  switch (type) {
29  case SPPT::Polar:
30  sp = ScanPoint2D{type, range, angle_rad, true};
31  break;
32  case SPPT::Cartesian:
33  sp = ScanPoint2D{type, x, y, true};
34  break;
35  default:
36  assert(0 && "Unknown ScanPoint2D type");
37  }
38 
39  ASSERT_NEAR(range, sp.range(), Acc_Error);
40  ASSERT_NEAR(angle_rad, sp.angle(), Acc_Error);
41  ASSERT_NEAR(x, sp.x(), Acc_Error);
42  ASSERT_NEAR(y, sp.y(), Acc_Error);
43  }
44 
45  void assert_scan_point(const ScanPoint2D &expected,
46  const ScanPoint2D &actual) {
47  ASSERT_NEAR(expected.range(), actual.range(), Acc_Error);
48  ASSERT_NEAR(expected.angle(), actual.angle(), Acc_Error);
49  ASSERT_NEAR(expected.x(), actual.x(), Acc_Error);
50  ASSERT_NEAR(expected.y(), actual.y(), Acc_Error);
51  ASSERT_EQ(expected.is_occupied(), actual.is_occupied());
52  }
53 };
54 
55 TEST_F(ScanPoint2DTest, polar1Quad) {
56  smoke_check_point(SPPT::Polar, 5, deg2rad(30), 4.3301, 2.5);
57 }
58 
59 TEST_F(ScanPoint2DTest, polar2Quad) {
60  smoke_check_point(SPPT::Polar, 4, deg2rad(120), -2, 3.4641);
61 }
62 
63 TEST_F(ScanPoint2DTest, polar3Quad) {
64  smoke_check_point(SPPT::Polar, 2, deg2rad(-150), -1.7320, -1);
65 }
66 
67 TEST_F(ScanPoint2DTest, polar4Quad) {
68  smoke_check_point(SPPT::Polar, 4, deg2rad(-45), 2.8284, -2.8284);
69 }
70 
71 TEST_F(ScanPoint2DTest, cartesian1Quad) {
72  smoke_check_point(SPPT::Cartesian, 2.8284, deg2rad(45), 2, 2);
73 }
74 
75 TEST_F(ScanPoint2DTest, cartesian2Quad) {
76  smoke_check_point(SPPT::Cartesian, 3.6055, deg2rad(146.309), -3, 2);
77 }
78 
79 TEST_F(ScanPoint2DTest, cartesian3Quad) {
80  smoke_check_point(SPPT::Cartesian, 3.6055, deg2rad(-123.69), -2, -3);
81 }
82 
83 TEST_F(ScanPoint2DTest, cartesian4Quad) {
84  smoke_check_point(SPPT::Cartesian, 3.1622, deg2rad(-71.565), 1, -3);
85 }
86 
87 TEST_F(ScanPoint2DTest, convertFromPolarNoChanges) {
88  auto polar_sp = ScanPoint2D{SPPT::Polar, 5, deg2rad(57), false};
89 
90  assert_scan_point(polar_sp, polar_sp.to_cartesian(0, 0));
91  assert_scan_point(polar_sp, polar_sp.to_polar(0, 0));
92 }
93 
94 TEST_F(ScanPoint2DTest, convertToCartesianFromPolarWithAltering) {
95  auto polar_sp = ScanPoint2D{SPPT::Polar, 5, deg2rad(57), true};
96  const double D_Angle = deg2rad(3), D_Range = -0.1;
97  auto cartesian_sp = polar_sp.to_cartesian(D_Angle, D_Range);
98 
99  ASSERT_NEAR(polar_sp.range() + D_Range, cartesian_sp.range(), Acc_Error);
100  ASSERT_NEAR(polar_sp.angle() + D_Angle, cartesian_sp.angle(), Acc_Error);
101  ASSERT_EQ(polar_sp.is_occupied(), cartesian_sp.is_occupied());
102 }
103 
104 TEST_F(ScanPoint2DTest, convertFromCartesianNoChanges) {
105  auto cartesian_sp = ScanPoint2D{SPPT::Cartesian, 6, 3, true};
106 
107  assert_scan_point(cartesian_sp, cartesian_sp.to_cartesian(0, 0));
108  assert_scan_point(cartesian_sp, cartesian_sp.to_polar(0, 0));
109 }
110 
111 TEST_F(ScanPoint2DTest, convertToPolarFromCartesianWithAltering) {
112  auto cartesian_sp = ScanPoint2D{SPPT::Cartesian, 1, -8, false};
113  const double D_X = -8, D_Y = 4;
114  auto polar_sp = cartesian_sp.to_polar(D_X, D_Y);
115 
116  ASSERT_NEAR(cartesian_sp.x() + D_X, polar_sp.x(), Acc_Error);
117  ASSERT_NEAR(cartesian_sp.y() + D_Y, polar_sp.y(), Acc_Error);
118  ASSERT_EQ(cartesian_sp.is_occupied(), polar_sp.is_occupied());
119 }
120 
121 TEST_F(ScanPoint2DTest, convertToCartesianWithProvider) {
122  auto polar_sp = ScanPoint2D{SPPT::Polar, 4, deg2rad(10), true};
123 
124  const double D_Angle = deg2rad(23);
125  auto rtp = std::make_shared<RawTrigonometryProvider>();
126  rtp->set_base_angle(D_Angle);
127  auto cartesian_sp = polar_sp.to_cartesian(rtp);
128 
129  ASSERT_NEAR(polar_sp.range(), cartesian_sp.range(), Acc_Error);
130  ASSERT_NEAR(polar_sp.angle() + D_Angle, cartesian_sp.angle(), Acc_Error);
131  ASSERT_EQ(polar_sp.is_occupied(), cartesian_sp.is_occupied());
132 }
133 
134 //----------------------------------------------------------------------------//
135 // LaserScan2D
136 
137 class LaserScan2DTest : public ::testing::Test {
138 protected: // consts
139  static constexpr double Acc_Error = 0.001;
140 protected: // methods
141  void check_sp_rotation(double d_angle, const ScanPoint2D &expected,
142  const ScanPoint2D &actual) {
143  ASSERT_NEAR(expected.range(), actual.range(), Acc_Error);
144  ASSERT_NEAR(expected.angle() + d_angle, actual.angle(), Acc_Error);
145  ASSERT_EQ(expected.is_occupied(), actual.is_occupied());
146  }
147 };
148 
149 TEST_F(LaserScan2DTest, toCartesianWithRotation) {
150  auto map = UnboundedPlainGridMap{std::make_shared<MockGridCell>(),
151  GridMapParams{100, 100, 1}};
152  const auto Top_Bnd_Pos = CecumTextRasterMapPrimitive::BoundPosition::Top;
153  auto cecum_mp = CecumTextRasterMapPrimitive{7, 4, Top_Bnd_Pos};
154  GridMapPatcher{}.apply_text_raster(map, cecum_mp.to_stream(), {}, 1, 1);
155  auto mp_free_space = cecum_mp.free_space()[0];
156 
157  auto pose = RobotPose{mp_free_space.left() + mp_free_space.hside_len() / 2,
158  mp_free_space.bot() + mp_free_space.vside_len() / 2,
159  deg2rad(90)};
160 
161  auto lsg = LaserScanGenerator{{30, deg2rad(30.0), deg2rad(135.0)}};
162  auto scan = lsg.laser_scan_2D(map, pose, 1);
163 
164  const auto D_Angle = deg2rad(7);
165  auto rotated_scan = scan.to_cartesian(D_Angle);
166 
167  ASSERT_EQ(scan.points().size(), rotated_scan.points().size());
168  for (std::size_t i = 0; i < scan.points().size(); ++i) {
169  check_sp_rotation(D_Angle, scan.points()[i], rotated_scan.points()[i]);
170  }
171 }
172 
173 //------------------------------------------------------------------------------
174 
175 int main (int argc, char *argv[]) {
176  ::testing::InitGoogleTest(&argc, argv);
177  return RUN_ALL_TESTS();
178 }
int main(int argc, char *argv[])
bool is_occupied() const
Definition: sensor_data.h:72
double x() const
Definition: sensor_data.h:60
void assert_scan_point(const ScanPoint2D &expected, const ScanPoint2D &actual)
void apply_text_raster(MapType &dst_map, std::istream &src_raster, const DiscretePoint2D &dst_offset, int w_zoom=1, int h_zoom=1)
double angle() const
Definition: sensor_data.h:54
void check_sp_rotation(double d_angle, const ScanPoint2D &expected, const ScanPoint2D &actual)
static constexpr double Acc_Error
void smoke_check_point(SPPT type, const double range, const double angle_rad, const double x, const double y)
double range() const
Definition: sensor_data.h:47
constexpr double deg2rad(double angle_deg)
Definition: math_utils.h:56
double y() const
Definition: sensor_data.h:66
TEST_F(ScanPoint2DTest, polar1Quad)


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25