probability_grid_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <random>
20 #include <vector>
21 
22 #include "gtest/gtest.h"
23 
24 namespace cartographer {
25 namespace mapping_2d {
26 namespace {
27 
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));
34  }
35  for (int i = 13; i < 18; ++i) {
36  proto.mutable_update_indices()->Add(i);
37  }
38  proto.set_max_x(19);
39  proto.set_max_y(20);
40  proto.set_min_x(21);
41  proto.set_min_y(22);
42 
43  ProbabilityGrid grid(proto);
44  EXPECT_EQ(proto.limits().DebugString(), ToProto(grid.limits()).DebugString());
45 
46  // TODO(macmason): Figure out how to test the contents of cells_,
47  // update_indices_, and {min, max}_{x, y}_ gracefully.
48 }
49 
50 TEST(ProbabilityGridTest, ToProto) {
51  ProbabilityGrid probability_grid(
52  MapLimits(1., Eigen::Vector2d(1., 1.), CellLimits(2, 2)));
53 
54  const auto proto = probability_grid.ToProto();
55  EXPECT_EQ(ToProto(probability_grid.limits()).DebugString(),
56  proto.limits().DebugString());
57 
58  // TODO(macmason): Figure out how to test the contents of cells_,
59  // update_indices_, and {min, max}_{x, y}_ gracefully.
60 }
61 
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();
66 
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)));
75 
76  probability_grid.SetProbability(Eigen::Array2i(1, 0), 0.5);
77 
78  probability_grid.StartUpdate();
79  probability_grid.ApplyLookupTable(
80  Eigen::Array2i(1, 0),
82  EXPECT_GT(probability_grid.GetProbability(Eigen::Array2i(1, 0)), 0.5);
83 
84  probability_grid.StartUpdate();
85  probability_grid.SetProbability(Eigen::Array2i(0, 1), 0.5);
86 
87  probability_grid.StartUpdate();
88  probability_grid.ApplyLookupTable(
89  Eigen::Array2i(0, 1),
91  EXPECT_LT(probability_grid.GetProbability(Eigen::Array2i(0, 1)), 0.5);
92 
93  // Tests adding odds to an unknown cell.
94  probability_grid.StartUpdate();
95  probability_grid.ApplyLookupTable(
96  Eigen::Array2i(1, 1),
98  EXPECT_NEAR(probability_grid.GetProbability(Eigen::Array2i(1, 1)), 0.42,
99  1e-4);
100 
101  // Tests that further updates are ignored if StartUpdate() isn't called.
102  probability_grid.ApplyLookupTable(
103  Eigen::Array2i(1, 1),
105  EXPECT_NEAR(probability_grid.GetProbability(Eigen::Array2i(1, 1)), 0.42,
106  1e-4);
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);
112 }
113 
114 TEST(ProbabilityGridTest, GetProbability) {
115  ProbabilityGrid probability_grid(
116  MapLimits(1., Eigen::Vector2d(1., 2.), CellLimits(2, 2)));
117 
118  const MapLimits& limits = probability_grid.limits();
119  EXPECT_EQ(1., limits.max().x());
120  EXPECT_EQ(2., limits.max().y());
121 
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);
125 
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));
138  }
139 }
140 
141 TEST(ProbabilityGridTest, GetXYIndexOfCellContainingPoint) {
142  ProbabilityGrid probability_grid(
143  MapLimits(2., Eigen::Vector2d(8., 14.), CellLimits(14, 8)));
144 
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);
149  EXPECT_TRUE(
150  (Eigen::Array2i(0, 0) == limits.GetXYIndexOfCellContainingPoint(7, 13))
151  .all());
152  EXPECT_TRUE(
153  (Eigen::Array2i(13, 0) == limits.GetXYIndexOfCellContainingPoint(7, -13))
154  .all());
155  EXPECT_TRUE(
156  (Eigen::Array2i(0, 7) == limits.GetXYIndexOfCellContainingPoint(-7, 13))
157  .all());
158  EXPECT_TRUE(
159  (Eigen::Array2i(13, 7) == limits.GetXYIndexOfCellContainingPoint(-7, -13))
160  .all());
161 
162  // Check around the origin.
163  EXPECT_TRUE(
164  (Eigen::Array2i(6, 3) == limits.GetXYIndexOfCellContainingPoint(0.5, 0.5))
165  .all());
166  EXPECT_TRUE(
167  (Eigen::Array2i(6, 3) == limits.GetXYIndexOfCellContainingPoint(1.5, 1.5))
168  .all());
169  EXPECT_TRUE((Eigen::Array2i(7, 3) ==
170  limits.GetXYIndexOfCellContainingPoint(0.5, -0.5))
171  .all());
172  EXPECT_TRUE((Eigen::Array2i(6, 4) ==
173  limits.GetXYIndexOfCellContainingPoint(-0.5, 0.5))
174  .all());
175  EXPECT_TRUE((Eigen::Array2i(7, 4) ==
176  limits.GetXYIndexOfCellContainingPoint(-0.5, -0.5))
177  .all());
178 }
179 
180 TEST(ProbabilityGridTest, CorrectCropping) {
181  // Create a probability grid with random values.
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));
191  }
192  Eigen::Array2i offset;
193  CellLimits limits;
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);
198 }
199 
200 } // namespace
201 } // namespace mapping_2d
202 } // namespace cartographer
std::vector< uint16 > ComputeLookupTableToApplyOdds(const float odds)
constexpr float kMinProbability
constexpr float kMaxProbability
float Odds(float probability)
proto::MapLimits ToProto(const MapLimits &map_limits)
Definition: map_limits.h:93


cartographer
Author(s):
autogenerated on Wed Jun 5 2019 21:57:58