37 #include <nav2_voxel_grid/voxel_grid.hpp>
39 namespace nav2_voxel_grid
42 : logger(rclcpp::get_logger(
"voxel_grid"))
50 logger,
"Error, this implementation can only support up to 16 z values (%d)",
55 data_ =
new uint32_t[size_x_ * size_y_];
56 uint32_t unknown_col = ~((uint32_t)0) >> 16;
57 uint32_t * col = data_;
58 for (
unsigned int i = 0; i < size_x_ * size_y_; ++i) {
67 if (size_x == size_x_ && size_y == size_y_ && size_z == size_z_) {
79 logger,
"Error, this implementation can only support up to 16 z values (%d)",
84 data_ =
new uint32_t[size_x_ * size_y_];
85 uint32_t unknown_col = ~((uint32_t)0) >> 16;
86 uint32_t * col = data_;
87 for (
unsigned int i = 0; i < size_x_ * size_y_; ++i) {
93 VoxelGrid::~VoxelGrid()
98 void VoxelGrid::reset()
100 uint32_t unknown_col = ~((uint32_t)0) >> 16;
101 uint32_t * col = data_;
102 for (
unsigned int i = 0; i < size_x_ * size_y_; ++i) {
108 void VoxelGrid::markVoxelLine(
109 double x0,
double y0,
double z0,
double x1,
double y1,
double z1,
110 unsigned int max_length)
112 if (x0 >= size_x_ || y0 >= size_y_ || z0 >= size_z_ || x1 >= size_x_ || y1 >= size_y_ ||
117 "Error, line endpoint out of bounds. "
118 "(%.2f, %.2f, %.2f) to (%.2f, %.2f, %.2f), size: (%d, %d, %d)",
119 x0, y0, z0, x1, y1, z1, size_x_, size_y_, size_z_);
124 raytraceLine(mv, x0, y0, z0, x1, y1, z1, max_length);
127 void VoxelGrid::clearVoxelLine(
128 double x0,
double y0,
double z0,
double x1,
double y1,
double z1,
129 unsigned int max_length,
unsigned int min_length)
131 if (x0 >= size_x_ || y0 >= size_y_ || z0 >= size_z_ || x1 >= size_x_ || y1 >= size_y_ ||
136 "Error, line endpoint out of bounds. "
137 "(%.2f, %.2f, %.2f) to (%.2f, %.2f, %.2f), size: (%d, %d, %d)",
138 x0, y0, z0, x1, y1, z1, size_x_, size_y_, size_z_);
142 ClearVoxel cv(data_);
143 raytraceLine(cv, x0, y0, z0, x1, y1, z1, max_length, min_length);
146 void VoxelGrid::clearVoxelLineInMap(
147 double x0,
double y0,
double z0,
double x1,
double y1,
double z1,
unsigned char * map_2d,
148 unsigned int unknown_threshold,
unsigned int mark_threshold,
unsigned char free_cost,
149 unsigned char unknown_cost,
unsigned int max_length,
unsigned int min_length)
152 if (map_2d == NULL) {
153 clearVoxelLine(x0, y0, z0, x1, y1, z1, max_length, min_length);
157 if (x0 >= size_x_ || y0 >= size_y_ || z0 >= size_z_ || x1 >= size_x_ || y1 >= size_y_ ||
162 "Error, line endpoint out of bounds. "
163 "(%.2f, %.2f, %.2f) to (%.2f, %.2f, %.2f), size: (%d, %d, %d)",
164 x0, y0, z0, x1, y1, z1, size_x_, size_y_, size_z_);
168 ClearVoxelInMap cvm(data_, costmap, unknown_threshold, mark_threshold, free_cost, unknown_cost);
169 raytraceLine(cvm, x0, y0, z0, x1, y1, z1, max_length, min_length);
172 VoxelStatus VoxelGrid::getVoxel(
unsigned int x,
unsigned int y,
unsigned int z)
174 if (x >= size_x_ || y >= size_y_ || z >= size_z_) {
175 RCLCPP_DEBUG(logger,
"Error, voxel out of bounds. (%d, %d, %d)\n", x, y, z);
178 uint32_t full_mask = ((uint32_t)1 << z << 16) | (1 << z);
179 uint32_t result = data_[y * size_x_ + x] & full_mask;
180 unsigned int bits = numBits(result);
194 VoxelStatus VoxelGrid::getVoxelColumn(
195 unsigned int x,
unsigned int y,
196 unsigned int unknown_threshold,
unsigned int marked_threshold)
198 if (x >= size_x_ || y >= size_y_) {
199 RCLCPP_DEBUG(logger,
"Error, voxel out of bounds. (%d, %d)\n", x, y);
203 uint32_t * col = &data_[y * size_x_ + x];
205 unsigned int unknown_bits = uint16_t(*col >> 16) ^ uint16_t(*col);
206 unsigned int marked_bits = *col >> 16;
209 if (!bitsBelowThreshold(marked_bits, marked_threshold)) {
214 if (!bitsBelowThreshold(unknown_bits, unknown_threshold)) {
221 unsigned int VoxelGrid::sizeX()
226 unsigned int VoxelGrid::sizeY()
231 unsigned int VoxelGrid::sizeZ()
236 void VoxelGrid::printVoxelGrid()
238 for (
unsigned int z = 0; z < size_z_; z++) {
239 printf(
"Layer z = %u:\n", z);
240 for (
unsigned int y = 0; y < size_y_; y++) {
241 for (
unsigned int x = 0; x < size_x_; x++) {
242 printf((getVoxel(x, y, z)) == nav2_voxel_grid::MARKED ?
"#" :
" ");
249 void VoxelGrid::printColumnGrid()
251 printf(
"Column view:\n");
252 for (
unsigned int y = 0; y < size_y_; y++) {
253 for (
unsigned int x = 0; x < size_x_; x++) {
254 printf((getVoxelColumn(x, y, 16, 0) == nav2_voxel_grid::MARKED) ?
"#" :
" ");
void resize(unsigned int size_x, unsigned int size_y, unsigned int size_z)
Resizes a voxel grid to the desired size.
VoxelGrid(unsigned int size_x, unsigned int size_y, unsigned int size_z)
Constructor for a voxel grid.