23 #include "gmock/gmock.h" 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.ApplyLookupTable(Eigen::Array3i(1, 0, 1),
45 hybrid_grid.FinishUpdate();
46 EXPECT_GT(hybrid_grid.GetProbability(Eigen::Array3i(1, 0, 1)), 0.5f);
48 hybrid_grid.SetProbability(Eigen::Array3i(0, 1, 0), 0.5f);
50 hybrid_grid.ApplyLookupTable(Eigen::Array3i(0, 1, 0),
52 hybrid_grid.FinishUpdate();
53 EXPECT_LT(hybrid_grid.GetProbability(Eigen::Array3i(0, 1, 0)), 0.5f);
56 hybrid_grid.ApplyLookupTable(Eigen::Array3i(1, 1, 1),
58 EXPECT_NEAR(hybrid_grid.GetProbability(Eigen::Array3i(1, 1, 1)), 0.42f, 1e-4);
61 hybrid_grid.ApplyLookupTable(Eigen::Array3i(1, 1, 1),
63 EXPECT_NEAR(hybrid_grid.GetProbability(Eigen::Array3i(1, 1, 1)), 0.42f, 1e-4);
64 hybrid_grid.FinishUpdate();
65 hybrid_grid.ApplyLookupTable(Eigen::Array3i(1, 1, 1),
67 EXPECT_GT(hybrid_grid.GetProbability(Eigen::Array3i(1, 1, 1)), 0.42f);
70 TEST(HybridGridTest, GetProbability) {
71 HybridGrid hybrid_grid(1.f);
73 hybrid_grid.SetProbability(
74 hybrid_grid.GetCellIndex(Eigen::Vector3f(0.f, 1.f, 1.f)),
76 EXPECT_NEAR(hybrid_grid.GetProbability(
77 hybrid_grid.GetCellIndex(Eigen::Vector3f(0.f, 1.f, 1.f))),
79 for (
const Eigen::Array3i& index :
80 {hybrid_grid.GetCellIndex(Eigen::Vector3f(0.f, 2.f, 1.f)),
81 hybrid_grid.GetCellIndex(Eigen::Vector3f(1.f, 1.f, 1.f)),
82 hybrid_grid.GetCellIndex(Eigen::Vector3f(1.f, 2.f, 1.f))}) {
83 EXPECT_FALSE(hybrid_grid.IsKnown(index));
87 MATCHER_P(AllCwiseEqual, index,
"") {
return (arg == index).all(); }
89 TEST(HybridGridTest, GetCellIndex) {
90 HybridGrid hybrid_grid(2.f);
92 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(0.f, 0.f, 0.f)),
93 AllCwiseEqual(Eigen::Array3i(0, 0, 0)));
94 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(0.f, 26.f, 10.f)),
95 AllCwiseEqual(Eigen::Array3i(0, 13, 5)));
96 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(14.f, 0.f, 10.f)),
97 AllCwiseEqual(Eigen::Array3i(7, 0, 5)));
98 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(14.f, 26.f, 0.f)),
99 AllCwiseEqual(Eigen::Array3i(7, 13, 0)));
102 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(8.5f, 11.5f, 0.5f)),
103 AllCwiseEqual(Eigen::Array3i(4, 6, 0)));
104 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(7.5f, 12.5f, 1.5f)),
105 AllCwiseEqual(Eigen::Array3i(4, 6, 1)));
106 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(6.5f, 14.5f, 2.5f)),
107 AllCwiseEqual(Eigen::Array3i(3, 7, 1)));
108 EXPECT_THAT(hybrid_grid.GetCellIndex(Eigen::Vector3f(5.5f, 13.5f, 3.5f)),
109 AllCwiseEqual(Eigen::Array3i(3, 7, 2)));
112 TEST(HybridGridTest, GetCenterOfCell) {
113 HybridGrid hybrid_grid(2.f);
115 const Eigen::Array3i index(3, 2, 1);
116 const Eigen::Vector3f center = hybrid_grid.GetCenterOfCell(index);
117 EXPECT_NEAR(6.f, center.x(), 1e-6);
118 EXPECT_NEAR(4.f, center.y(), 1e-6);
119 EXPECT_NEAR(2.f, center.z(), 1e-6);
120 EXPECT_THAT(hybrid_grid.GetCellIndex(center), AllCwiseEqual(index));
123 class RandomHybridGridTest :
public ::testing::Test {
126 std::mt19937 rng(1285120005);
127 std::uniform_real_distribution<float> value_distribution(
kMinProbability,
129 std::uniform_int_distribution<int> xyz_distribution(-3000, 2999);
130 for (
int i = 0; i < 10000; ++i) {
131 const auto x = xyz_distribution(rng);
132 const auto y = xyz_distribution(rng);
133 const auto z = xyz_distribution(rng);
134 values_.emplace(std::make_tuple(x, y, z), value_distribution(rng));
137 for (
const auto& pair :
values_) {
138 const Eigen::Array3i cell_index(std::get<0>(pair.first),
139 std::get<1>(pair.first),
140 std::get<2>(pair.first));
147 using ValueMap = std::map<std::tuple<int, int, int>,
float>;
151 TEST_F(RandomHybridGridTest, TestIteration) {
153 const Eigen::Array3i cell_index = it.GetCellIndex();
155 EXPECT_EQ(iterator_probability,
hybrid_grid_.GetProbability(cell_index));
156 const std::tuple<int, int, int> key =
157 std::make_tuple(cell_index[0], cell_index[1], cell_index[2]);
158 EXPECT_TRUE(values_.count(key));
159 EXPECT_NEAR(values_[key], iterator_probability, 1e-4);
166 ASSERT_FALSE(it.Done());
167 EXPECT_THAT(cell.first, AllCwiseEqual(it.GetCellIndex()));
168 EXPECT_EQ(cell.second, it.GetValue());
173 for (
const auto& pair : values_) {
174 const Eigen::Array3i cell_index(std::get<0>(pair.first),
175 std::get<1>(pair.first),
176 std::get<2>(pair.first));
177 ADD_FAILURE() << cell_index <<
" Probability: " << pair.second;
181 TEST_F(RandomHybridGridTest,
ToProto) {
183 EXPECT_EQ(
hybrid_grid_.resolution(), proto.resolution());
184 ASSERT_EQ(proto.x_indices_size(), proto.y_indices_size());
185 ASSERT_EQ(proto.x_indices_size(), proto.z_indices_size());
186 ASSERT_EQ(proto.x_indices_size(), proto.values_size());
189 for (
int i = 0; i < proto.x_indices_size(); ++i) {
190 proto_map[std::make_tuple(proto.x_indices(i), proto.y_indices(i),
191 proto.z_indices(i))] = proto.values(i);
195 ValueMap hybrid_grid_map;
197 hybrid_grid_map[std::make_tuple(i.first.x(), i.first.y(), i.first.z())] =
201 EXPECT_EQ(proto_map, hybrid_grid_map);
204 struct EigenComparator {
205 bool operator()(
const Eigen::Vector3i& lhs,
206 const Eigen::Vector3i& rhs)
const {
207 return std::forward_as_tuple(lhs.x(), lhs.y(), lhs.z()) <
208 std::forward_as_tuple(rhs.x(), rhs.y(), rhs.z());
212 TEST_F(RandomHybridGridTest,
FromProto) {
213 const HybridGrid constructed_grid(
hybrid_grid_.ToProto());
215 std::map<Eigen::Vector3i, float, EigenComparator> member_map(
218 std::map<Eigen::Vector3i, float, EigenComparator> constructed_map(
219 constructed_grid.begin(), constructed_grid.end());
221 EXPECT_EQ(member_map, constructed_map);
PoseGraph::Constraint::Tag FromProto(const proto::PoseGraph::Constraint::Tag &proto)
std::vector< uint16 > ComputeLookupTableToApplyOdds(const float odds)
typename GridBase< uint16 >::Iterator Iterator
constexpr float kMinProbability
float ValueToProbability(const uint16 value)
constexpr float kMaxProbability
float Odds(float probability)
MATCHER_P(Near, point, std::string(negation ? "Doesn't" :"Does")+" match.")
proto::MapLimits ToProto(const MapLimits &map_limits)
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)