Skip to content

Commit 6c4a663

Browse files
authored
Bounds Checking for SubGrid Iterator (#77)
1 parent b26cc2d commit 6c4a663

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

nav_grid_iterators/include/nav_grid_iterators/sub_grid.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ class SubGrid : public BaseIterator<SubGrid>
5454
* @param min_y Minimum index (y coordinate)
5555
* @param width Number of indexes in the x direction
5656
* @param height Number of indexes in the y direction
57-
*
58-
* @note No bounds checking is done for any of the coordinates. Assumes (min_x, min_y) and
59-
* (min_x + width - 1, min_y + height - 1) are valid in the grid.
6057
*/
6158
SubGrid(const nav_grid::NavGridInfo* info, unsigned int min_x, unsigned int min_y,
6259
unsigned int width, unsigned int height)
@@ -78,13 +75,18 @@ class SubGrid : public BaseIterator<SubGrid>
7875
* @param min_y Minimum index (y coordinate)
7976
* @param width Number of indexes in the x direction
8077
* @param height Number of indexes in the y direction
81-
*
82-
* @note No bounds checking is done for any of the coordinates. Assumes (min_x, min_y) and
83-
* (min_x + width - 1, min_y + height - 1) are valid in the grid.
8478
*/
8579
SubGrid(const nav_grid::NavGridInfo* info, const nav_grid::Index& index, unsigned int min_x, unsigned int min_y,
86-
unsigned int width, unsigned int height)
87-
: BaseIterator(info, index), min_x_(min_x), min_y_(min_y), width_(width), height_(height) {}
80+
unsigned int width, unsigned int height);
81+
82+
/**
83+
* @brief Public constructor using UIntBounds object that takes in an arbitrary index
84+
* @param info NavGridInfo for the grid to iterate over
85+
* @param index Initial index
86+
* @param bounds UIntBounds
87+
*/
88+
SubGrid(const nav_grid::NavGridInfo* info, const nav_grid::Index& index, const nav_core2::UIntBounds& bounds)
89+
: SubGrid(info, index, bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), bounds.getHeight()) {}
8890

8991
/**@name Standard BaseIterator Interface */
9092
/**@{*/

nav_grid_iterators/src/sub_grid.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,35 @@
3636

3737
namespace nav_grid_iterators
3838
{
39+
SubGrid::SubGrid(const nav_grid::NavGridInfo* info, const nav_grid::Index& index,
40+
unsigned int min_x, unsigned int min_y,
41+
unsigned int width, unsigned int height)
42+
: BaseIterator(info, index), min_x_(min_x), min_y_(min_y), width_(width), height_(height)
43+
{
44+
// If the start coordinate is entirely off the grid or the size is 0
45+
// we invalidate the entire iterator and give up immediately
46+
if (min_x_ >= info->width || min_y_ >= info->height || width_ == 0 || height_ == 0)
47+
{
48+
index_ = nav_grid::Index(0, 0);
49+
width_ = 0;
50+
height_ = 0;
51+
min_x_ = 0;
52+
min_y_ = 0;
53+
return;
54+
}
55+
56+
// If the end coordinate is off the grid, we shorten the dimensions to
57+
// cover the on-grid potion
58+
if (min_x_ + width_ > info->width)
59+
{
60+
width_ = info->width - min_x_;
61+
}
62+
if (min_y_ + height_ > info->height)
63+
{
64+
height_ = info->height - min_y_;
65+
}
66+
}
67+
3968
SubGrid SubGrid::begin() const
4069
{
4170
return SubGrid(info_, min_x_, min_y_, width_, height_);

nav_grid_iterators/test/utest.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ TEST(SubGrid, sub_grid)
117117
ASSERT_EQ(countIterations(nav_grid_iterators::SubGrid(&info, 1, 3, 2, 2)), 4);
118118
nav_core2::UIntBounds bounds(1, 3, 4, 3);
119119
ASSERT_EQ(countIterations(nav_grid_iterators::SubGrid(&info, bounds)), 4);
120+
121+
// Empty Bounds
122+
bounds.reset();
123+
ASSERT_EQ(countIterations(nav_grid_iterators::SubGrid(&info, bounds)), 0);
124+
125+
// Partially Overlapping Bounds
126+
bounds.touch(3, 2);
127+
bounds.touch(6, 3);
128+
ASSERT_EQ(countIterations(nav_grid_iterators::SubGrid(&info, bounds)), 4);
129+
130+
bounds.reset();
131+
bounds.touch(1, 6);
132+
bounds.touch(3, 9);
133+
ASSERT_EQ(countIterations(nav_grid_iterators::SubGrid(&info, bounds)), 6);
134+
135+
// Different empty bounds
136+
nav_core2::UIntBounds empty(1, 0, 0, 0);
137+
ASSERT_EQ(countIterations(nav_grid_iterators::SubGrid(&info, empty)), 0);
120138
}
121139

122140
TEST(SubGrid, equality)

0 commit comments

Comments
 (0)