37 #include <nav2_voxel_grid/voxel_grid.hpp>
39 #include <rclcpp/logger.hpp>
40 #include <rclcpp/logging.hpp>
42 namespace nav2_voxel_grid
45 : logger(rclcpp::get_logger(
"voxel_grid"))
53 logger,
"Error, this implementation can only support up to 16 z values (%d)",
58 data_ =
new uint32_t[size_x_ * size_y_];
59 uint32_t unknown_col = ~((uint32_t)0) >> 16;
60 uint32_t * col = data_;
61 for (
unsigned int i = 0; i < size_x_ * size_y_; ++i) {
70 if (size_x == size_x_ && size_y == size_y_ && size_z == size_z_) {
82 logger,
"Error, this implementation can only support up to 16 z values (%d)",
87 data_ =
new uint32_t[size_x_ * size_y_];
88 uint32_t unknown_col = ~((uint32_t)0) >> 16;
89 uint32_t * col = data_;
90 for (
unsigned int i = 0; i < size_x_ * size_y_; ++i) {
96 VoxelGrid::~VoxelGrid()
101 void VoxelGrid::reset()
103 uint32_t unknown_col = ~((uint32_t)0) >> 16;
104 uint32_t * col = data_;
105 for (
unsigned int i = 0; i < size_x_ * size_y_; ++i) {
111 void VoxelGrid::markVoxelLine(
112 double x0,
double y0,
double z0,
double x1,
double y1,
double z1,
113 unsigned int max_length)
115 if (x0 >= size_x_ || y0 >= size_y_ || z0 >= size_z_ || x1 >= size_x_ || y1 >= size_y_ ||
120 "Error, line endpoint out of bounds. "
121 "(%.2f, %.2f, %.2f) to (%.2f, %.2f, %.2f), size: (%d, %d, %d)",
122 x0, y0, z0, x1, y1, z1, size_x_, size_y_, size_z_);
127 raytraceLine(mv, x0, y0, z0, x1, y1, z1, max_length);
130 void VoxelGrid::clearVoxelLine(
131 double x0,
double y0,
double z0,
double x1,
double y1,
double z1,
132 unsigned int max_length,
unsigned int min_length)
134 if (x0 >= size_x_ || y0 >= size_y_ || z0 >= size_z_ || x1 >= size_x_ || y1 >= size_y_ ||
139 "Error, line endpoint out of bounds. "
140 "(%.2f, %.2f, %.2f) to (%.2f, %.2f, %.2f), size: (%d, %d, %d)",
141 x0, y0, z0, x1, y1, z1, size_x_, size_y_, size_z_);
145 ClearVoxel cv(data_);
146 raytraceLine(cv, x0, y0, z0, x1, y1, z1, max_length, min_length);
149 void VoxelGrid::clearVoxelLineInMap(
150 double x0,
double y0,
double z0,
double x1,
double y1,
double z1,
unsigned char * map_2d,
151 unsigned int unknown_threshold,
unsigned int mark_threshold,
unsigned char free_cost,
152 unsigned char unknown_cost,
unsigned int max_length,
unsigned int min_length)
155 if (map_2d == NULL) {
156 clearVoxelLine(x0, y0, z0, x1, y1, z1, max_length, min_length);
160 if (x0 >= size_x_ || y0 >= size_y_ || z0 >= size_z_ || x1 >= size_x_ || y1 >= size_y_ ||
165 "Error, line endpoint out of bounds. "
166 "(%.2f, %.2f, %.2f) to (%.2f, %.2f, %.2f), size: (%d, %d, %d)",
167 x0, y0, z0, x1, y1, z1, size_x_, size_y_, size_z_);
171 ClearVoxelInMap cvm(data_, costmap, unknown_threshold, mark_threshold, free_cost, unknown_cost);
172 raytraceLine(cvm, x0, y0, z0, x1, y1, z1, max_length, min_length);
175 VoxelStatus VoxelGrid::getVoxel(
unsigned int x,
unsigned int y,
unsigned int z)
177 if (x >= size_x_ || y >= size_y_ || z >= size_z_) {
178 RCLCPP_DEBUG(logger,
"Error, voxel out of bounds. (%d, %d, %d)\n", x, y, z);
181 uint32_t full_mask = ((uint32_t)1 << z << 16) | (1 << z);
182 uint32_t result = data_[y * size_x_ + x] & full_mask;
183 unsigned int bits = numBits(result);
197 VoxelStatus VoxelGrid::getVoxelColumn(
198 unsigned int x,
unsigned int y,
199 unsigned int unknown_threshold,
unsigned int marked_threshold)
201 if (x >= size_x_ || y >= size_y_) {
202 RCLCPP_DEBUG(logger,
"Error, voxel out of bounds. (%d, %d)\n", x, y);
206 uint32_t * col = &data_[y * size_x_ + x];
208 unsigned int unknown_bits = uint16_t(*col >> 16) ^ uint16_t(*col);
209 unsigned int marked_bits = *col >> 16;
212 if (!bitsBelowThreshold(marked_bits, marked_threshold)) {
217 if (!bitsBelowThreshold(unknown_bits, unknown_threshold)) {
224 unsigned int VoxelGrid::sizeX()
229 unsigned int VoxelGrid::sizeY()
234 unsigned int VoxelGrid::sizeZ()
239 void VoxelGrid::printVoxelGrid()
241 for (
unsigned int z = 0; z < size_z_; z++) {
242 printf(
"Layer z = %u:\n", z);
243 for (
unsigned int y = 0; y < size_y_; y++) {
244 for (
unsigned int x = 0; x < size_x_; x++) {
245 printf((getVoxel(x, y, z)) == nav2_voxel_grid::MARKED ?
"#" :
" ");
252 void VoxelGrid::printColumnGrid()
254 printf(
"Column view:\n");
255 for (
unsigned int y = 0; y < size_y_; y++) {
256 for (
unsigned int x = 0; x < size_x_; x++) {
257 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.