test_occupancy_grid.cpp
Go to the documentation of this file.
1 // Copyright 2023 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 <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 
18 #include <cstddef>
19 #include <vector>
20 
21 #include <range/v3/range/conversion.hpp>
22 
23 #include <sophus/common.hpp>
24 #include <sophus/se2.hpp>
25 
27 
28 namespace {
29 
31 
32 TEST(OccupancyGrid2, FreeAtCell) {
33  constexpr double kResolution = 1.;
34 
35  const auto grid = StaticOccupancyGrid<5, 5>{
36  {false, false, false, false, true, false, false, false, true, false, false, false, true,
37  false, false, false, true, false, false, false, true, false, false, false, false},
38  kResolution};
39 
40  EXPECT_TRUE(grid.free_at(0));
41  EXPECT_FALSE(grid.free_at(8));
42  EXPECT_FALSE(grid.free_at(2, 2));
43  EXPECT_TRUE(grid.free_at(3, 2));
44  EXPECT_FALSE(grid.free_at(Eigen::Vector2i(0, 4)));
45  EXPECT_TRUE(grid.free_at(Eigen::Vector2i(1, 4)));
46 }
47 
48 TEST(OccupancyGrid2, FreeNearCell) {
49  constexpr double kResolution = 1.;
50 
51  const auto grid = StaticOccupancyGrid<5, 5>{
52  {false, false, false, false, true, false, false, false, true, false, false, false, true,
53  false, false, false, true, false, false, false, true, false, false, false, false},
54  kResolution};
55 
56  EXPECT_FALSE(grid.free_near(3.25, 1.75));
57  EXPECT_TRUE(grid.free_near(4, 4.25));
58  EXPECT_FALSE(grid.free_near(Eigen::Vector2d(3.25, 1.75)));
59  EXPECT_TRUE(grid.free_near(Eigen::Vector2d(4, 4.25)));
60 }
61 
62 TEST(OccupancyGrid2, GlobalCoordinatesAtCell) {
63  constexpr double kResolution = 1.;
64  const auto origin = Sophus::SE2d{Sophus::SO2d{Sophus::Constants<double>::pi() / 2.}, Eigen::Vector2d{1., 1.}};
65  const auto grid = StaticOccupancyGrid<5, 5>{
66  {false, false, false, false, true, false, false, false, true, false, false, false, true,
67  false, false, false, true, false, false, false, true, false, false, false, false},
68  kResolution,
69  origin};
70 
71  constexpr auto kFrame = StaticOccupancyGrid<5, 5>::Frame::kGlobal;
72  EXPECT_TRUE(grid.coordinates_at(grid.index_at(2, 2), kFrame).isApprox(Eigen::Vector2d(-1.5, 3.5)));
73 }
74 
75 TEST(OccupancyGrid2, AllFreeCells) {
76  const auto grid = StaticOccupancyGrid<2, 5>{{false, false, false, false, true, false, false, false, true, false}};
77 
78  const auto expected_free_cells = std::vector<std::size_t>{0, 1, 2, 3, 5, 6, 7, 9};
79  ASSERT_THAT(grid.free_cells() | ranges::to<std::vector>, testing::Pointwise(testing::Eq(), expected_free_cells));
80 }
81 
82 TEST(OccupancyGrid2, ObstacleData) {
83  const auto grid = StaticOccupancyGrid<5, 2>{{false, false, false, false, true, false, false, false, true, false}};
84 
85 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111118
86 #pragma GCC diagnostic push
87 #pragma GCC diagnostic ignored "-Warray-bounds"
88 #pragma GCC diagnostic ignored "-Wstringop-overflow"
89  // Data and obstacle data are equivalent in this case
90  ASSERT_THAT(grid.obstacle_data() | ranges::to<std::vector>, testing::Pointwise(testing::Eq(), grid.data()));
91 #pragma GCC diagnostic pop
92 }
93 
94 TEST(OccupancyGrid2, GlobalCoordinatesForCells) {
95  constexpr double kResolution = 1.;
96  const auto origin = Sophus::SE2d{Sophus::SO2d{Sophus::Constants<double>::pi() / 2.}, Eigen::Vector2d{1., 1.}};
97  const auto grid = StaticOccupancyGrid<2, 3>{{false, true, false, true, false, true}, kResolution, origin};
98 
99  constexpr auto kFrame = StaticOccupancyGrid<2, 3>::Frame::kGlobal;
100  const auto coordinates = grid.coordinates_for(grid.free_cells(), kFrame) | ranges::to<std::vector>;
101  EXPECT_EQ(coordinates.size(), 3);
102  EXPECT_TRUE(coordinates[0].isApprox(Eigen::Vector2d{0.5, 1.5}));
103  EXPECT_TRUE(coordinates[1].isApprox(Eigen::Vector2d{0.5, 3.5}));
104  EXPECT_TRUE(coordinates[2].isApprox(Eigen::Vector2d{-0.5, 2.5}));
105 }
106 
107 } // namespace
Sophus::SO2
static_occupancy_grid.hpp
se2.hpp
common.hpp
Sophus::SE2
beluga::testing::StaticOccupancyGrid
Definition: static_occupancy_grid.hpp:28
Sophus::Constants::pi
static SOPHUS_FUNC Scalar pi()
beluga::TEST
TEST(Bresenham, MultiPassGuarantee)
Definition: test_bresenham.cpp:27


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