test_sparse_value_grid.cpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include <cstddef>
18 #include <map>
19 #include <optional>
20 #include <unordered_map>
21 
22 #include <Eigen/Core>
23 
25 
26 namespace beluga {
27 
28 template <class T>
29 class SparseGridTests : public testing::Test {};
30 
31 struct Less {
32  bool operator()(const Eigen::Vector2i& lhs, const Eigen::Vector2i& rhs) const {
33  return (lhs.x() < rhs.x()) || (lhs.y() < rhs.y());
34  }
35 };
36 
37 struct Hasher {
38  std::size_t operator()(const Eigen::Vector2i&) const { return 1; }
39 };
40 
41 using SparseGridTestCases =
42  testing::Types<std::unordered_map<Eigen::Vector2i, int, Hasher>, std::map<Eigen::Vector2i, int, Less>>;
43 
45 
46 TYPED_TEST(SparseGridTests, CanBeConstructedEmpty) {
47  const TypeParam data;
48  [[maybe_unused]] const beluga::SparseValueGrid2<TypeParam> grid{data, 0.5};
49 }
50 
52  const TypeParam data{
53  {Eigen::Vector2i{1, 2}, 1},
54  {Eigen::Vector2i{4, 2}, 1},
55  {Eigen::Vector2i{2, 2}, 1},
56  {Eigen::Vector2i{3, 2}, 1},
57  };
58 
59  const beluga::SparseValueGrid2<TypeParam> grid{data, 0.5};
60  ASSERT_EQ(grid.size(), 4);
61 }
62 
64  const TypeParam data{};
65 
66  const beluga::SparseValueGrid2<TypeParam> grid{data, 0.5};
67 
68  ASSERT_EQ(grid.data_at(Eigen::Vector2i(2, 1)), std::nullopt);
69  ASSERT_EQ(grid.data_near(Eigen::Vector2d(5, 3)), std::nullopt);
70  ASSERT_EQ(grid.data_near(Eigen::Vector2d{5, 1}), std::nullopt);
71 }
72 
74  {
75  const beluga::SparseValueGrid2<TypeParam> grid{TypeParam{}, 0.5};
76  ASSERT_DOUBLE_EQ(grid.resolution(), 0.5);
77  }
78  {
79  const beluga::SparseValueGrid2<TypeParam> grid{TypeParam{}, 0.8};
80  ASSERT_DOUBLE_EQ(grid.resolution(), 0.8);
81  }
82 }
83 
84 TYPED_TEST(SparseGridTests, DataAccessing) {
85  const TypeParam data{
86  {Eigen::Vector2i{1, 2}, 1},
87  {Eigen::Vector2i{4, 2}, 2},
88  {Eigen::Vector2i{3, 2}, 3},
89  {Eigen::Vector2i{2, 2}, 4},
90  };
91  // trivial case where resolution == 1.0
92  {
93  const beluga::SparseValueGrid2<TypeParam> grid{data, 1.0};
94  auto data = grid.data_near(Eigen::Vector2d{1.0, 2.0});
95  ASSERT_TRUE(data.has_value());
96  ASSERT_EQ(data, 1);
97  }
98  {
99  const beluga::SparseValueGrid2<TypeParam> grid{data, 1.0};
100  auto data = grid.data_near(Eigen::Vector2d{4.0, 2.0});
101  ASSERT_TRUE(data.has_value());
102  ASSERT_EQ(data, 2);
103  }
104  {
105  const beluga::SparseValueGrid2<TypeParam> grid{data, 0.5};
106  auto data = grid.data_near(Eigen::Vector2d{1.0, 2.0} * 0.5);
107  ASSERT_TRUE(data.has_value());
108  ASSERT_EQ(data, 1);
109  }
110 }
111 
112 TYPED_TEST(SparseGridTests, AllAccessorMethodsAreEquivalent) {
113  const TypeParam data{
114  {Eigen::Vector2i{1, 2}, 1}, {Eigen::Vector2i{4, 2}, 2}, {Eigen::Vector2i{3, 2}, 3}, {Eigen::Vector2i{2, 2}, 4}};
115 
116  for (const auto resolution : {0.1, 0.5, 1.2, 1.5}) {
117  const beluga::SparseValueGrid2<TypeParam> grid{data, resolution};
118  for (const auto& [coordinates, value] : data) {
119  const Eigen::Vector2d double_coords = coordinates.template cast<double>() * resolution;
120  ASSERT_TRUE(grid.cell_near(double_coords).isApprox(coordinates));
121  ASSERT_EQ(grid.data_near(double_coords), std::make_optional<int>(value));
122  ASSERT_EQ(grid.data_at(grid.cell_near(double_coords)), std::make_optional<int>(value));
123  }
124  }
125 }
126 
127 } // namespace beluga
beluga::SparseGridTests
Definition: test_sparse_value_grid.cpp:29
beluga::TYPED_TEST_SUITE
TYPED_TEST_SUITE(SparseGridTests, SparseGridTestCases,)
beluga::Less
Definition: test_sparse_value_grid.cpp:31
beluga::SparseValueGrid::data_near
std::optional< mapped_type > data_near(const Eigen::Vector< double, NDim > &coordinates) const
Gets grid data at real coordinates 'coordinates' or std::nullopt if it's not present.
Definition: sparse_value_grid.hpp:86
beluga::SparseValueGrid
Generic N dimensional sparse value regular grid.
Definition: sparse_value_grid.hpp:46
beluga::TYPED_TEST
TYPED_TEST(SparseGridTests, CanBeConstructedEmpty)
Definition: test_sparse_value_grid.cpp:46
beluga::Hasher::operator()
std::size_t operator()(const Eigen::Vector2i &) const
Definition: test_sparse_value_grid.cpp:38
sparse_value_grid.hpp
Implementation of generic sparse value regular grid.
beluga::Less::operator()
bool operator()(const Eigen::Vector2i &lhs, const Eigen::Vector2i &rhs) const
Definition: test_sparse_value_grid.cpp:32
beluga::SparseGridTestCases
testing::Types< std::unordered_map< Eigen::Vector2i, int, Hasher >, std::map< Eigen::Vector2i, int, Less > > SparseGridTestCases
Definition: test_sparse_value_grid.cpp:42
beluga::Hasher
Definition: test_sparse_value_grid.cpp:37
beluga
The main Beluga namespace.
Definition: 3d_embedding.hpp:21


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53