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 {
42 TEST(CostmapBBF, ForEach)
43 {
44  CostmapBBF bbf;
45  const int w = 10, h = 5;
46  bbf.reset(CostmapBBF::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 CostmapBBF::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 }
71 TEST(CostmapBBF, Update)
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  CostmapBBF bbf;
102  bbf.reset(CostmapBBF::Vec(3, 3, 1));
103  bbf.clear();
104 
105  cm.reset(CostmapBBF::Vec(3, 3, 1));
106  for (int i = 0; i < 3; ++i)
107  for (int j = 0; j < 3; ++j)
108  cm[CostmapBBF::Vec(i, j, 0)] = costmap0[i][j];
109 
110  bbf.remember(&cm, CostmapBBF::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[CostmapBBF::Vec(i, j, 0)] = costmap1[i][j];
115 
116  bbf.remember(&cm, CostmapBBF::Vec(0, 0, 0), odds_hit, odds_miss, 1, 2);
117 
118  const auto cb = [expected_odds](const CostmapBBF::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(CostmapBBF::Vec(i, j, 0))) << "at " << i << ", " << j;
127 
128  bbf.clear();
129  const auto cb_cleared = [](const CostmapBBF::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(CostmapBBF::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 }
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:51
void reset(const CyclicVecInt< DIM, NONCYCLIC > &size)
TEST(CostmapBBF, ForEach)
char getCost(const Vec &p) const
Definition: costmap_bbf.h:69
int main(int argc, char **argv)
TFSIMD_FORCE_INLINE const tfScalar & w() const
constexpr float probabilityToOdds(const float &p)
Definition: bbf.h:42
const float MIN_ODDS
Definition: bbf.h:49
void forEach(const std::function< void(const Vec &, bbf::BinaryBayesFilter &)> cb)
Definition: costmap_bbf.cpp:93


planner_cspace
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:20:42