22 #include "gtest/gtest.h" 25 namespace mapping_2d {
28 TEST(ProbabilityGridTest, ProtoConstructor) {
29 proto::ProbabilityGrid proto;
30 const MapLimits limits(1., {2., 3.}, CellLimits(4., 5.));
31 *proto.mutable_limits() =
ToProto(limits);
32 for (
int i = 6; i < 12; ++i) {
33 proto.mutable_cells()->Add(static_cast<uint16>(i));
35 for (
int i = 13; i < 18; ++i) {
36 proto.mutable_update_indices()->Add(i);
43 ProbabilityGrid grid(proto);
44 EXPECT_EQ(proto.limits().DebugString(),
ToProto(grid.limits()).DebugString());
50 TEST(ProbabilityGridTest,
ToProto) {
51 ProbabilityGrid probability_grid(
52 MapLimits(1., Eigen::Vector2d(1., 1.), CellLimits(2, 2)));
54 const auto proto = probability_grid.ToProto();
55 EXPECT_EQ(
ToProto(probability_grid.limits()).DebugString(),
56 proto.limits().DebugString());
62 TEST(ProbabilityGridTest, ApplyOdds) {
63 ProbabilityGrid probability_grid(
64 MapLimits(1., Eigen::Vector2d(1., 1.), CellLimits(2, 2)));
65 const MapLimits& limits = probability_grid.limits();
67 EXPECT_TRUE(limits.Contains(Eigen::Array2i(0, 0)));
68 EXPECT_TRUE(limits.Contains(Eigen::Array2i(0, 1)));
69 EXPECT_TRUE(limits.Contains(Eigen::Array2i(1, 0)));
70 EXPECT_TRUE(limits.Contains(Eigen::Array2i(1, 1)));
71 EXPECT_FALSE(probability_grid.IsKnown(Eigen::Array2i(0, 0)));
72 EXPECT_FALSE(probability_grid.IsKnown(Eigen::Array2i(0, 1)));
73 EXPECT_FALSE(probability_grid.IsKnown(Eigen::Array2i(1, 0)));
74 EXPECT_FALSE(probability_grid.IsKnown(Eigen::Array2i(1, 1)));
76 probability_grid.SetProbability(Eigen::Array2i(1, 0), 0.5);
78 probability_grid.StartUpdate();
79 probability_grid.ApplyLookupTable(
82 EXPECT_GT(probability_grid.GetProbability(Eigen::Array2i(1, 0)), 0.5);
84 probability_grid.StartUpdate();
85 probability_grid.SetProbability(Eigen::Array2i(0, 1), 0.5);
87 probability_grid.StartUpdate();
88 probability_grid.ApplyLookupTable(
91 EXPECT_LT(probability_grid.GetProbability(Eigen::Array2i(0, 1)), 0.5);
94 probability_grid.StartUpdate();
95 probability_grid.ApplyLookupTable(
98 EXPECT_NEAR(probability_grid.GetProbability(Eigen::Array2i(1, 1)), 0.42,
102 probability_grid.ApplyLookupTable(
103 Eigen::Array2i(1, 1),
105 EXPECT_NEAR(probability_grid.GetProbability(Eigen::Array2i(1, 1)), 0.42,
107 probability_grid.StartUpdate();
108 probability_grid.ApplyLookupTable(
109 Eigen::Array2i(1, 1),
111 EXPECT_GT(probability_grid.GetProbability(Eigen::Array2i(1, 1)), 0.42);
114 TEST(ProbabilityGridTest, GetProbability) {
115 ProbabilityGrid probability_grid(
116 MapLimits(1., Eigen::Vector2d(1., 2.), CellLimits(2, 2)));
118 const MapLimits& limits = probability_grid.limits();
119 EXPECT_EQ(1., limits.max().x());
120 EXPECT_EQ(2., limits.max().y());
122 const CellLimits& cell_limits = limits.cell_limits();
123 ASSERT_EQ(2, cell_limits.num_x_cells);
124 ASSERT_EQ(2, cell_limits.num_y_cells);
126 probability_grid.StartUpdate();
127 probability_grid.SetProbability(
128 limits.GetXYIndexOfCellContainingPoint(-0.5, 0.5),
130 EXPECT_NEAR(probability_grid.GetProbability(-0.5, 0.5),
132 for (
const Eigen::Array2i& xy_index :
133 {limits.GetXYIndexOfCellContainingPoint(-0.5, 1.5),
134 limits.GetXYIndexOfCellContainingPoint(0.5, 0.5),
135 limits.GetXYIndexOfCellContainingPoint(0.5, 1.5)}) {
136 EXPECT_TRUE(limits.Contains(xy_index));
137 EXPECT_FALSE(probability_grid.IsKnown(xy_index));
141 TEST(ProbabilityGridTest, GetXYIndexOfCellContainingPoint) {
142 ProbabilityGrid probability_grid(
143 MapLimits(2., Eigen::Vector2d(8., 14.), CellLimits(14, 8)));
145 const MapLimits& limits = probability_grid.limits();
146 const CellLimits& cell_limits = limits.cell_limits();
147 ASSERT_EQ(14, cell_limits.num_x_cells);
148 ASSERT_EQ(8, cell_limits.num_y_cells);
150 (Eigen::Array2i(0, 0) == limits.GetXYIndexOfCellContainingPoint(7, 13))
153 (Eigen::Array2i(13, 0) == limits.GetXYIndexOfCellContainingPoint(7, -13))
156 (Eigen::Array2i(0, 7) == limits.GetXYIndexOfCellContainingPoint(-7, 13))
159 (Eigen::Array2i(13, 7) == limits.GetXYIndexOfCellContainingPoint(-7, -13))
164 (Eigen::Array2i(6, 3) == limits.GetXYIndexOfCellContainingPoint(0.5, 0.5))
167 (Eigen::Array2i(6, 3) == limits.GetXYIndexOfCellContainingPoint(1.5, 1.5))
169 EXPECT_TRUE((Eigen::Array2i(7, 3) ==
170 limits.GetXYIndexOfCellContainingPoint(0.5, -0.5))
172 EXPECT_TRUE((Eigen::Array2i(6, 4) ==
173 limits.GetXYIndexOfCellContainingPoint(-0.5, 0.5))
175 EXPECT_TRUE((Eigen::Array2i(7, 4) ==
176 limits.GetXYIndexOfCellContainingPoint(-0.5, -0.5))
180 TEST(ProbabilityGridTest, CorrectCropping) {
182 std::mt19937 rng(42);
183 std::uniform_real_distribution<float> value_distribution(
185 ProbabilityGrid probability_grid(
186 MapLimits(0.05, Eigen::Vector2d(10., 10.), CellLimits(400, 400)));
187 probability_grid.StartUpdate();
188 for (
const Eigen::Array2i& xy_index : XYIndexRangeIterator(
189 Eigen::Array2i(100, 100), Eigen::Array2i(299, 299))) {
190 probability_grid.SetProbability(xy_index, value_distribution(rng));
192 Eigen::Array2i offset;
194 probability_grid.ComputeCroppedLimits(&offset, &limits);
195 EXPECT_TRUE((offset == Eigen::Array2i(100, 100)).all());
196 EXPECT_EQ(limits.num_x_cells, 200);
197 EXPECT_EQ(limits.num_y_cells, 200);
std::vector< uint16 > ComputeLookupTableToApplyOdds(const float odds)
constexpr float kMinProbability
constexpr float kMaxProbability
float Odds(float probability)
proto::MapLimits ToProto(const MapLimits &map_limits)