test_costmap_bbf.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, the neonavigation authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <map>
31 #include <cmath>
32 
33 #include <gtest/gtest.h>
34 
36 #include <planner_cspace/bbf.h>
37 
38 namespace planner_cspace
39 {
40 namespace planner_3d
41 {
43 {
44  CostmapBBFImpl bbf;
45  const int w = 10, h = 5;
46  bbf.reset(CostmapBBFImpl::Vec(w, h, 1));
47  bool called[w][h];
48  for (int i = 0; i < w; i++)
49  for (int j = 0; j < h; j++)
50  called[i][j] = false;
51 
52  const auto cb = [&called, w, h](const CostmapBBFImpl::Vec& p, bbf::BinaryBayesFilter& /* bbf */)
53  {
54  ASSERT_LT(p[0], w);
55  ASSERT_LT(p[1], h);
56  ASSERT_GE(p[0], 0);
57  ASSERT_GE(p[1], 0);
58  ASSERT_EQ(0, p[2]);
59  called[p[0]][p[1]] = true;
60  };
61  bbf.forEach(cb);
62 
63  int cnt_called = 0;
64  for (int i = 0; i < w; i++)
65  for (int j = 0; j < h; j++)
66  if (called[i][j])
67  cnt_called++;
68 
69  ASSERT_EQ(w * h, cnt_called);
70 }
72 {
73  const float odds_hit = bbf::probabilityToOdds(0.8);
74  const float odds_miss = bbf::probabilityToOdds(0.3);
75  const char costmap0[3][3] =
76  {
77  {10, 100, 100},
78  {100, 100, 100},
79  {100, 100, 100},
80  };
81  const char costmap1[3][3] =
82  {
83  {0, -1, 0},
84  {100, 0, 0},
85  {0, 0, 100},
86  };
87  const float expected_odds[3][3] =
88  {
89  {bbf::MIN_ODDS, bbf::MIN_ODDS * odds_hit, bbf::MIN_ODDS * odds_hit * odds_miss},
90  {bbf::MIN_ODDS * odds_hit * odds_hit, bbf::MIN_ODDS * odds_hit * odds_miss, bbf::MIN_ODDS},
91  {bbf::MIN_ODDS * odds_hit * odds_miss, bbf::MIN_ODDS, bbf::MIN_ODDS},
92  };
93  const char expected_cost[3][3] =
94  {
95  {0, 26, 8},
96  {68, 8, 0},
97  {8, 0, 0},
98  };
100 
101  CostmapBBFImpl bbf;
102  bbf.reset(CostmapBBFImpl::Vec(3, 3, 1));
103  bbf.clear();
104 
105  cm.reset(CostmapBBFImpl::Vec(3, 3, 1));
106  for (int i = 0; i < 3; ++i)
107  for (int j = 0; j < 3; ++j)
108  cm[CostmapBBFImpl::Vec(i, j, 0)] = costmap0[i][j];
109 
110  bbf.remember(&cm, CostmapBBFImpl::Vec(0, 0, 0), odds_hit, odds_miss, 1, 2);
111 
112  for (int i = 0; i < 3; ++i)
113  for (int j = 0; j < 3; ++j)
114  cm[CostmapBBFImpl::Vec(i, j, 0)] = costmap1[i][j];
115 
116  bbf.remember(&cm, CostmapBBFImpl::Vec(0, 0, 0), odds_hit, odds_miss, 1, 2);
117 
118  const auto cb = [expected_odds](const CostmapBBFImpl::Vec& p, bbf::BinaryBayesFilter& b)
119  {
120  EXPECT_FLOAT_EQ(expected_odds[p[0]][p[1]], b.get()) << "at " << p[0] << ", " << p[1];
121  };
122  bbf.forEach(cb);
123  bbf.updateCostmap();
124  for (int i = 0; i < 3; ++i)
125  for (int j = 0; j < 3; ++j)
126  EXPECT_EQ(expected_cost[i][j], bbf.getCost(CostmapBBFImpl::Vec(i, j, 0))) << "at " << i << ", " << j;
127 
128  bbf.clear();
129  const auto cb_cleared = [](const CostmapBBFImpl::Vec& p, bbf::BinaryBayesFilter& b)
130  {
131  EXPECT_FLOAT_EQ(bbf::MIN_ODDS, b.get()) << "at " << p[0] << ", " << p[1];
132  };
133  bbf.forEach(cb_cleared);
134  for (int i = 0; i < 3; ++i)
135  for (int j = 0; j < 3; ++j)
136  EXPECT_EQ(0, bbf.getCost(CostmapBBFImpl::Vec(i, j, 0))) << "at " << i << ", " << j;
137 }
138 } // namespace planner_3d
139 } // namespace planner_cspace
140 
141 int main(int argc, char** argv)
142 {
143  testing::InitGoogleTest(&argc, argv);
144 
145  return RUN_ALL_TESTS();
146 }
planner_cspace::planner_3d::CostmapBBFImpl::remember
void remember(const BlockMemGridmapBase< char, 3, 2 > *const costmap, const Vec &center, const float remember_hit_odds, const float remember_miss_odds, const int range_min, const int range_max)
Definition: costmap_bbf.cpp:53
planner_cspace
Definition: bbf.h:33
planner_cspace::planner_3d::CostmapBBFImpl::updateCostmap
void updateCostmap()
Definition: costmap_bbf.cpp:41
planner_cspace::planner_3d::CostmapBBFImpl::getCost
char getCost(const Vec &p) const
Definition: costmap_bbf.h:91
planner_cspace::planner_3d::CostmapBBFImpl
Definition: costmap_bbf.h:62
bbf.h
planner_cspace::bbf::MIN_ODDS
const float MIN_ODDS
Definition: bbf.h:49
planner_cspace::BlockMemGridmap
Definition: blockmem_gridmap.h:67
planner_cspace::planner_3d::TEST
TEST(CostmapBBFImpl, ForEach)
Definition: test_costmap_bbf.cpp:42
planner_cspace::bbf::probabilityToOdds
constexpr float probabilityToOdds(const float &p)
Definition: bbf.h:42
costmap_bbf.h
planner_cspace::planner_3d::CostmapBBFImpl::forEach
void forEach(const std::function< void(const Vec &, bbf::BinaryBayesFilter &)> cb)
Definition: costmap_bbf.cpp:102
planner_cspace::bbf::BinaryBayesFilter
Definition: bbf.h:52
planner_cspace::planner_3d::CostmapBBFImpl::clear
void clear()
Definition: costmap_bbf.h:84
planner_cspace::planner_3d::CostmapBBFImpl::reset
void reset(const Vec &size)
Definition: costmap_bbf.h:77
planner_cspace::BlockMemGridmap::reset
void reset(const CyclicVecInt< DIM, NONCYCLIC > &size) override
Definition: blockmem_gridmap.h:195
main
int main(int argc, char **argv)
Definition: test_costmap_bbf.cpp:141
planner_cspace::CyclicVecBase
Definition: cyclic_vec.h:78


planner_cspace
Author(s): Atsushi Watanabe
autogenerated on Fri May 16 2025 02:15:23