Skip to content

Commit ec4f69e

Browse files
janx8rAlexeyMerzlyakov
authored andcommitted
Fixing the issue ros-navigation#2781: raytraceLine with same start and end point (ros-navigation#2784)
* Fixing the issue ros-navigation#2781: raytraceLine with same start and end point no longer causes segmentation fault * Some whitespace modifications to make the code pass release_test * Add testcase for raytraceLine the same point Co-authored-by: Alexey Merzlyakov <60094858+AlexeyMerzlyakov@users.noreply.github.com>
1 parent f2a163c commit ec4f69e

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

‎nav2_voxel_grid/include/nav2_voxel_grid/voxel_grid.hpp‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,22 @@ class VoxelGrid
232232
if ((unsigned int)(dist) < min_length) {
233233
return;
234234
}
235-
double scale = std::min(1.0, max_length / dist);
236-
237-
// Updating starting point to the point at distance min_length from the initial point
238-
double min_x0 = x0 + (x1 - x0) / dist * min_length;
239-
double min_y0 = y0 + (y1 - y0) / dist * min_length;
240-
double min_z0 = z0 + (z1 - z0) / dist * min_length;
235+
double scale, min_x0, min_y0, min_z0;
236+
if (dist > 0.0) {
237+
scale = std::min(1.0, max_length / dist);
238+
239+
// Updating starting point to the point at distance min_length from the initial point
240+
min_x0 = x0 + (x1 - x0) / dist * min_length;
241+
min_y0 = y0 + (y1 - y0) / dist * min_length;
242+
min_z0 = z0 + (z1 - z0) / dist * min_length;
243+
} else {
244+
// dist can be 0 if [x0, y0, z0]==[x1, y1, z1].
245+
// In this case only this voxel should be processed.
246+
scale = 1.0;
247+
min_x0 = x0;
248+
min_y0 = y0;
249+
min_z0 = z0;
250+
}
241251

242252
int dx = int(x1) - int(min_x0); // NOLINT
243253
int dy = int(y1) - int(min_y0); // NOLINT

‎nav2_voxel_grid/test/voxel_grid_bresenham_3d.cpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class TestVoxel
4949
ASSERT_TRUE(off < size_);
5050
data_[off] = val;
5151
}
52+
inline unsigned int operator()(unsigned int off)
53+
{
54+
return data_[off];
55+
}
5256

5357
private:
5458
uint32_t * data_;
@@ -122,6 +126,29 @@ TEST(voxel_grid, bresenham3DBoundariesCheck)
122126
}
123127
}
124128

129+
TEST(voxel_grid, bresenham3DSamePoint)
130+
{
131+
const int sz_x = 60;
132+
const int sz_y = 60;
133+
const int sz_z = 2;
134+
const unsigned int max_length = 60;
135+
const unsigned int min_length = 0;
136+
nav2_voxel_grid::VoxelGrid vg(sz_x, sz_y, sz_z);
137+
TestVoxel tv(vg.getData(), sz_x, sz_y);
138+
139+
// Initial point
140+
const double x0 = 2.2;
141+
const double y0 = 3.8;
142+
const double z0 = 0.4;
143+
144+
unsigned int offset = int(y0) * sz_x + int(x0);
145+
unsigned int val_before = tv(offset);
146+
// Same point to check
147+
vg.raytraceLine(tv, x0, y0, z0, x0, y0, z0, max_length, min_length);
148+
unsigned int val_after = tv(offset);
149+
ASSERT_FALSE(val_before == val_after);
150+
}
151+
125152
int main(int argc, char ** argv)
126153
{
127154
testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)