23 #include "gmock/gmock.h" 26 namespace mapping_3d {
29 TEST(HybridGridTest, ApplyOdds) {
30 HybridGrid hybrid_grid(1.f);
32 EXPECT_FALSE(hybrid_grid.IsKnown(Eigen::Array3i(0, 0, 0)));
33 EXPECT_FALSE(hybrid_grid.IsKnown(Eigen::Array3i(0, 1, 0)));
34 EXPECT_FALSE(hybrid_grid.IsKnown(Eigen::Array3i(1, 0, 0)));
35 EXPECT_FALSE(hybrid_grid.IsKnown(Eigen::Array3i(1, 1, 0)));
36 EXPECT_FALSE(hybrid_grid.IsKnown(Eigen::Array3i(0, 0, 1)));
37 EXPECT_FALSE(hybrid_grid.IsKnown(Eigen::Array3i(0, 1, 1)));
38 EXPECT_FALSE(hybrid_grid.IsKnown(Eigen::Array3i(1, 0, 1)));
39 EXPECT_FALSE(hybrid_grid.IsKnown(Eigen::Array3i(1, 1, 1)));
41 hybrid_grid.SetProbability(Eigen::Array3i(1, 0, 1), 0.5f);
43 hybrid_grid.StartUpdate();
44 hybrid_grid.ApplyLookupTable(
45 Eigen::Array3i(1, 0, 1),
47 EXPECT_GT(hybrid_grid.GetProbability(Eigen::Array3i(1, 0, 1)), 0.5f);
49 hybrid_grid.SetProbability(Eigen::Array3i(0, 1, 0), 0.5f);
51 hybrid_grid.StartUpdate();
52 hybrid_grid.ApplyLookupTable(
53 Eigen::Array3i(0, 1, 0),
55 EXPECT_LT(hybrid_grid.GetProbability(Eigen::Array3i(0, 1, 0)), 0.5f);
58 hybrid_grid.StartUpdate();
59 hybrid_grid.ApplyLookupTable(
60 Eigen::Array3i(1, 1, 1),
62 EXPECT_NEAR(hybrid_grid.GetProbability(Eigen::Array3i(1, 1, 1)), 0.42f, 1e-4);
65 hybrid_grid.ApplyLookupTable(
66 Eigen::Array3i(1, 1, 1),
68 EXPECT_NEAR(hybrid_grid.GetProbability(Eigen::Array3i(1, 1, 1)), 0.42f, 1e-4);
69 hybrid_grid.StartUpdate();
70 hybrid_grid.ApplyLookupTable(
71 Eigen::Array3i(1, 1, 1),
73 EXPECT_GT(hybrid_grid.GetProbability(Eigen::Array3i(1, 1, 1)), 0.42f);
76 TEST(HybridGridTest, GetProbability) {
77 HybridGrid hybrid_grid(1.f);
79 hybrid_grid.SetProbability(
80 hybrid_grid.GetCellIndex(Eigen::Vector3f(0.f, 1.f, 1.f)),
82 EXPECT_NEAR(hybrid_grid.GetProbability(
83 hybrid_grid.GetCellIndex(Eigen::Vector3f(0.f, 1.f, 1.f))),
85 for (
const Eigen::Array3i& index :
86 {hybrid_grid.GetCellIndex(Eigen::Vector3f(0.f, 2.f, 1.f)),
87 hybrid_grid.GetCellIndex(Eigen::Vector3f(1.f, 1.f, 1.f)),
88 hybrid_grid.GetCellIndex(Eigen::Vector3f(1.f, 2.f, 1.f))}) {
89 EXPECT_FALSE(hybrid_grid.IsKnown(index));
93 MATCHER_P(AllCwiseEqual, index,
"") {
return (arg == index).all(); }
95 TEST(HybridGridTest, GetCellIndex) {
96 HybridGrid hybrid_grid(2.f);
98 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(0.f, 0.f, 0.f)),
99 AllCwiseEqual(Eigen::Array3i(0, 0, 0)));
100 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(0.f, 26.f, 10.f)),
101 AllCwiseEqual(Eigen::Array3i(0, 13, 5)));
102 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(14.f, 0.f, 10.f)),
103 AllCwiseEqual(Eigen::Array3i(7, 0, 5)));
104 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(14.f, 26.f, 0.f)),
105 AllCwiseEqual(Eigen::Array3i(7, 13, 0)));
108 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(8.5f, 11.5f, 0.5f)),
109 AllCwiseEqual(Eigen::Array3i(4, 6, 0)));
110 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(7.5f, 12.5f, 1.5f)),
111 AllCwiseEqual(Eigen::Array3i(4, 6, 1)));
112 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(6.5f, 14.5f, 2.5f)),
113 AllCwiseEqual(Eigen::Array3i(3, 7, 1)));
114 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(5.5f, 13.5f, 3.5f)),
115 AllCwiseEqual(Eigen::Array3i(3, 7, 2)));
118 TEST(HybridGridTest, GetCenterOfCell) {
119 HybridGrid hybrid_grid(2.f);
121 const Eigen::Array3i index(3, 2, 1);
122 const Eigen::Vector3f center = hybrid_grid.GetCenterOfCell(index);
123 EXPECT_NEAR(6.f, center.x(), 1e-6);
124 EXPECT_NEAR(4.f, center.y(), 1e-6);
125 EXPECT_NEAR(2.f, center.z(), 1e-6);
126 EXPECT_THAT(hybrid_grid.GetCellIndex(center), AllCwiseEqual(index));
129 class RandomHybridGridTest :
public ::testing::Test {
132 std::mt19937 rng(1285120005);
133 std::uniform_real_distribution<float> value_distribution(
135 std::uniform_int_distribution<int> xyz_distribution(-3000, 2999);
136 for (
int i = 0; i < 10000; ++i) {
137 const auto x = xyz_distribution(rng);
138 const auto y = xyz_distribution(rng);
139 const auto z = xyz_distribution(rng);
140 values_.emplace(std::make_tuple(x, y, z), value_distribution(rng));
143 for (
const auto& pair :
values_) {
144 const Eigen::Array3i cell_index(std::get<0>(pair.first),
145 std::get<1>(pair.first),
146 std::get<2>(pair.first));
153 using ValueMap = std::map<std::tuple<int, int, int>,
float>;
157 TEST_F(RandomHybridGridTest, TestIteration) {
159 const Eigen::Array3i cell_index = it.GetCellIndex();
160 const float iterator_probability =
162 EXPECT_EQ(iterator_probability,
hybrid_grid_.GetProbability(cell_index));
163 const std::tuple<int, int, int> key =
164 std::make_tuple(cell_index[0], cell_index[1], cell_index[2]);
165 EXPECT_TRUE(values_.count(key));
166 EXPECT_NEAR(values_[key], iterator_probability, 1e-4);
173 ASSERT_FALSE(it.Done());
174 EXPECT_THAT(cell.first, AllCwiseEqual(it.GetCellIndex()));
175 EXPECT_EQ(cell.second, it.GetValue());
180 for (
const auto& pair : values_) {
181 const Eigen::Array3i cell_index(std::get<0>(pair.first),
182 std::get<1>(pair.first),
183 std::get<2>(pair.first));
184 ADD_FAILURE() << cell_index <<
" Probability: " << pair.second;
188 TEST_F(RandomHybridGridTest,
ToProto) {
190 EXPECT_EQ(
hybrid_grid_.resolution(), proto.resolution());
192 ASSERT_EQ(proto.x_indices_size(), proto.y_indices_size());
193 ASSERT_EQ(proto.x_indices_size(), proto.z_indices_size());
194 ASSERT_EQ(proto.x_indices_size(), proto.values_size());
197 for (
int i = 0; i < proto.x_indices_size(); ++i) {
198 proto_map[std::make_tuple(proto.x_indices(i), proto.y_indices(i),
199 proto.z_indices(i))] = proto.values(i);
203 ValueMap hybrid_grid_map;
205 hybrid_grid_map[std::make_tuple(i.first.x(), i.first.y(), i.first.z())] =
209 EXPECT_EQ(proto_map, hybrid_grid_map);
214 struct EigenComparator {
215 bool operator()(
const Eigen::Vector3i& lhs,
const Eigen::Vector3i& rhs) {
216 return std::forward_as_tuple(lhs.x(), lhs.y(), lhs.z()) <
217 std::forward_as_tuple(rhs.x(), rhs.y(), rhs.z());
223 TEST_F(RandomHybridGridTest,
FromProto) {
226 std::map<Eigen::Vector3i, float, EigenComparator> member_map(
229 std::map<Eigen::Vector3i, float, EigenComparator> constructed_map(
230 constructed_grid.begin(), constructed_grid.end());
232 EXPECT_EQ(member_map, constructed_map);
std::vector< uint16 > ComputeLookupTableToApplyOdds(const float odds)
constexpr float kMinProbability
float ValueToProbability(const uint16 value)
constexpr float kMaxProbability
float Odds(float probability)
typename Grid< uint16 >::Iterator Iterator
RangeData FromProto(const proto::RangeData &proto)
proto::HybridGrid ToProto(const HybridGrid &grid)