22 #include "gtest/gtest.h" 29 using Eigen::Vector2f;
31 TEST(ProbabilityGridTest, ProtoConstructor) {
33 const MapLimits limits(1., {2., 3.}, CellLimits(4., 5.));
34 *proto.mutable_limits() =
ToProto(limits);
35 for (
int i = 6; i < 12; ++i) {
36 proto.mutable_cells()->Add(static_cast<uint16>(i));
38 proto.mutable_known_cells_box()->set_max_x(19);
39 proto.mutable_known_cells_box()->set_max_y(20);
40 proto.mutable_known_cells_box()->set_min_x(21);
41 proto.mutable_known_cells_box()->set_min_y(22);
42 proto.mutable_probability_grid_2d();
44 ProbabilityGrid grid(proto);
45 EXPECT_EQ(proto.limits().DebugString(),
ToProto(grid.limits()).DebugString());
52 ProbabilityGrid probability_grid(
53 MapLimits(1., Eigen::Vector2d(1., 1.), CellLimits(2, 2)));
55 const auto proto = probability_grid.ToProto();
56 EXPECT_EQ(
ToProto(probability_grid.limits()).DebugString(),
57 proto.limits().DebugString());
63 TEST(ProbabilityGridTest, ApplyOdds) {
64 ProbabilityGrid probability_grid(
65 MapLimits(1., Eigen::Vector2d(1., 1.), CellLimits(2, 2)));
66 const MapLimits& limits = probability_grid.limits();
68 EXPECT_TRUE(limits.Contains(Array2i(0, 0)));
69 EXPECT_TRUE(limits.Contains(Array2i(0, 1)));
70 EXPECT_TRUE(limits.Contains(Array2i(1, 0)));
71 EXPECT_TRUE(limits.Contains(Array2i(1, 1)));
72 EXPECT_FALSE(probability_grid.IsKnown(Array2i(0, 0)));
73 EXPECT_FALSE(probability_grid.IsKnown(Array2i(0, 1)));
74 EXPECT_FALSE(probability_grid.IsKnown(Array2i(1, 0)));
75 EXPECT_FALSE(probability_grid.IsKnown(Array2i(1, 1)));
77 probability_grid.SetProbability(Array2i(1, 0), 0.5);
79 probability_grid.ApplyLookupTable(
82 probability_grid.FinishUpdate();
83 EXPECT_GT(probability_grid.GetProbability(Array2i(1, 0)), 0.5);
85 probability_grid.SetProbability(Array2i(0, 1), 0.5);
86 probability_grid.ApplyLookupTable(
89 probability_grid.FinishUpdate();
90 EXPECT_LT(probability_grid.GetProbability(Array2i(0, 1)), 0.5);
93 probability_grid.ApplyLookupTable(
96 EXPECT_NEAR(probability_grid.GetProbability(Array2i(1, 1)), 0.42, 1e-4);
99 probability_grid.ApplyLookupTable(
102 EXPECT_NEAR(probability_grid.GetProbability(Array2i(1, 1)), 0.42, 1e-4);
103 probability_grid.FinishUpdate();
104 probability_grid.ApplyLookupTable(
107 EXPECT_GT(probability_grid.GetProbability(Array2i(1, 1)), 0.42);
110 TEST(ProbabilityGridTest, GetProbability) {
111 ProbabilityGrid probability_grid(
112 MapLimits(1., Eigen::Vector2d(1., 2.), CellLimits(2, 2)));
114 const MapLimits& limits = probability_grid.limits();
115 EXPECT_EQ(1., limits.max().x());
116 EXPECT_EQ(2., limits.max().y());
118 const CellLimits& cell_limits = limits.cell_limits();
119 ASSERT_EQ(2, cell_limits.num_x_cells);
120 ASSERT_EQ(2, cell_limits.num_y_cells);
122 probability_grid.SetProbability(limits.GetCellIndex(Vector2f(-0.5f, 0.5f)),
124 EXPECT_NEAR(probability_grid.GetProbability(
125 limits.GetCellIndex(Vector2f(-0.5f, 0.5f))),
127 for (
const Array2i& xy_index : {limits.GetCellIndex(Vector2f(-0.5f, 1.5f)),
128 limits.GetCellIndex(Vector2f(0.5f, 0.5f)),
129 limits.GetCellIndex(Vector2f(0.5f, 1.5f))}) {
130 EXPECT_TRUE(limits.Contains(xy_index));
131 EXPECT_FALSE(probability_grid.IsKnown(xy_index));
135 TEST(ProbabilityGridTest, GetCellIndex) {
136 ProbabilityGrid probability_grid(
137 MapLimits(2., Eigen::Vector2d(8., 14.), CellLimits(14, 8)));
139 const MapLimits& limits = probability_grid.limits();
140 const CellLimits& cell_limits = limits.cell_limits();
141 ASSERT_EQ(14, cell_limits.num_x_cells);
142 ASSERT_EQ(8, cell_limits.num_y_cells);
144 (Array2i(0, 0) == limits.GetCellIndex(Vector2f(7.f, 13.f))).all());
146 (Array2i(13, 0) == limits.GetCellIndex(Vector2f(7.f, -13.f))).all());
148 (Array2i(0, 7) == limits.GetCellIndex(Vector2f(-7.f, 13.f))).all());
150 (Array2i(13, 7) == limits.GetCellIndex(Vector2f(-7.f, -13.f))).all());
154 (Array2i(6, 3) == limits.GetCellIndex(Vector2f(0.5f, 0.5f))).all());
156 (Array2i(6, 3) == limits.GetCellIndex(Vector2f(1.5f, 1.5f))).all());
158 (Array2i(7, 3) == limits.GetCellIndex(Vector2f(0.5f, -0.5f))).all());
160 (Array2i(6, 4) == limits.GetCellIndex(Vector2f(-0.5f, 0.5f))).all());
162 (Array2i(7, 4) == limits.GetCellIndex(Vector2f(-0.5f, -0.5f))).all());
165 TEST(ProbabilityGridTest, CorrectCropping) {
167 std::mt19937 rng(42);
168 std::uniform_real_distribution<float> value_distribution(
kMinProbability,
170 ProbabilityGrid probability_grid(
171 MapLimits(0.05, Eigen::Vector2d(10., 10.), CellLimits(400, 400)));
172 for (
const Array2i& xy_index :
173 XYIndexRangeIterator(Array2i(100, 100), Array2i(299, 299))) {
174 probability_grid.SetProbability(xy_index, value_distribution(rng));
178 probability_grid.ComputeCroppedLimits(&offset, &limits);
179 EXPECT_TRUE((offset == Array2i(100, 100)).all());
180 EXPECT_EQ(limits.num_x_cells, 200);
181 EXPECT_EQ(limits.num_y_cells, 200);
std::vector< uint16 > ComputeLookupTableToApplyCorrespondenceCostOdds(float odds)
constexpr float kMinProbability
constexpr float kMaxProbability
float Odds(float probability)
proto::MapLimits ToProto(const MapLimits &map_limits)
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)