bounds_test.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 #include <gtest/gtest.h>
35 #include <nav_2d_utils/bounds.h>
37 #include <vector>
38 
41 
50  unsigned int& match, unsigned int& missed, unsigned int& multiple)
51 {
52  match = 0;
53  missed = 0;
54  multiple = 0;
55 
56  nav_grid::NavGridInfo info = grid.getInfo();
57 
58  // No iterator to avoid tricky depenencies
59  for (unsigned int x = 0; x < info.width; x++)
60  {
61  for (unsigned int y = 0; y < info.height; y++)
62  {
63  switch (grid(x, y))
64  {
65  case 0:
66  missed++;
67  break;
68  case 1:
69  match++;
70  break;
71  default:
72  multiple++;
73  break;
74  }
75  }
76  }
77 }
78 
79 TEST(DivideBounds, zeroes)
80 {
81  UIntBounds bounds(2, 2, 5, 5);
82  // Number of rows/cols has to be positive
83  EXPECT_THROW(divideBounds(bounds, 0, 2), std::invalid_argument);
84  EXPECT_THROW(divideBounds(bounds, 2, 0), std::invalid_argument);
85  EXPECT_THROW(divideBounds(bounds, 0, 0), std::invalid_argument);
86  EXPECT_NO_THROW(divideBounds(bounds, 2, 2));
87 
88  bounds.reset();
89  // check for errors with empty bounds
90  EXPECT_NO_THROW(divideBounds(bounds, 2, 2));
91 }
92 
101 TEST(DivideBounds, iterative_tests)
102 {
105 
106  // count variables
107  unsigned int match, missed, multiple;
108 
109  for (info.width = 1; info.width < 15; info.width++)
110  {
111  for (info.height = 1; info.height < 15; info.height++)
112  {
113  full_grid.setInfo(info);
114  UIntBounds full_bounds = nav_2d_utils::getFullUIntBounds(info);
115  for (unsigned int rows = 1; rows < 11u; rows++)
116  {
117  for (unsigned int cols = 1; cols < 11u; cols++)
118  {
119  full_grid.reset();
120  std::vector<UIntBounds> divided = divideBounds(full_bounds, cols, rows);
121  ASSERT_LE(divided.size(), rows * cols) << info.width << "x" << info.height << " " << rows << "x" << cols;
122  for (const UIntBounds& sub : divided)
123  {
124  EXPECT_FALSE(sub.isEmpty());
125  // Can't use nav_grid_iterator for circular dependencies
126  for (unsigned int x = sub.getMinX(); x <= sub.getMaxX(); x++)
127  {
128  for (unsigned int y = sub.getMinY(); y <= sub.getMaxY(); y++)
129  {
130  full_grid.setValue(x, y, full_grid(x, y) + 1);
131  }
132  }
133  }
134 
135  countValues(full_grid, match, missed, multiple);
136  ASSERT_EQ(match, info.width * info.height) << "Full grid: " << info.width << "x" << info.height
137  << " Requested divisions: " << rows << "x" << cols;
138  EXPECT_EQ(missed, 0u);
139  EXPECT_EQ(multiple, 0u);
140  }
141  }
142  }
143  }
144 }
145 
151 TEST(DivideBounds, recursive_tests)
152 {
155  info.width = 100;
156  info.height = 100;
157  full_grid.setInfo(info);
158 
159  UIntBounds full_bounds = nav_2d_utils::getFullUIntBounds(info);
160 
161  std::vector<UIntBounds> level_one = divideBounds(full_bounds, 2, 2);
162  ASSERT_EQ(level_one.size(), 4u);
163  for (const UIntBounds& sub : level_one)
164  {
165  std::vector<UIntBounds> level_two = divideBounds(sub, 2, 2);
166  ASSERT_EQ(level_two.size(), 4u);
167  for (const UIntBounds& subsub : level_two)
168  {
169  EXPECT_GE(subsub.getMinX(), sub.getMinX());
170  EXPECT_LE(subsub.getMaxX(), sub.getMaxX());
171  EXPECT_GE(subsub.getMinY(), sub.getMinY());
172  EXPECT_LE(subsub.getMaxY(), sub.getMaxY());
173  // Can't use nav_grid_iterator for circular dependencies
174  for (unsigned int x = subsub.getMinX(); x <= subsub.getMaxX(); x++)
175  {
176  for (unsigned int y = subsub.getMinY(); y <= subsub.getMaxY(); y++)
177  {
178  full_grid.setValue(x, y, full_grid(x, y) + 1);
179  }
180  }
181  }
182  }
183 
184  // Count values
185  unsigned int match = 0,
186  missed = 0,
187  multiple = 0;
188  countValues(full_grid, match, missed, multiple);
189  ASSERT_EQ(match, info.width * info.height);
190  EXPECT_EQ(missed, 0u);
191  EXPECT_EQ(multiple, 0u);
192 }
193 
194 
195 int main(int argc, char** argv)
196 {
197  testing::InitGoogleTest(&argc, argv);
198  return RUN_ALL_TESTS();
199 }
std::vector< nav_core2::UIntBounds > divideBounds(const nav_core2::UIntBounds &original_bounds, unsigned int n_cols, unsigned int n_rows)
divide the given bounds up into sub-bounds of roughly equal size
Definition: bounds.cpp:71
void countValues(const nav_grid::VectorNavGrid< unsigned char > &grid, unsigned int &match, unsigned int &missed, unsigned int &multiple)
Count the values in a grid.
Definition: bounds_test.cpp:49
TFSIMD_FORCE_INLINE const tfScalar & y() const
void reset() override
void setInfo(const NavGridInfo &new_info) override
TFSIMD_FORCE_INLINE const tfScalar & x() const
void setValue(const unsigned int x, const unsigned int y, const T &value) override
TEST(DivideBounds, zeroes)
Definition: bounds_test.cpp:79
int main(int argc, char **argv)
NavGridInfo getInfo() const
nav_core2::UIntBounds getFullUIntBounds(const nav_grid::NavGridInfo &info)
return an integral bounds that covers the entire NavGrid
Definition: bounds.cpp:49


nav_2d_utils
Author(s):
autogenerated on Sun Jan 10 2021 04:08:32