Skip to content

Commit 612e04c

Browse files
committed
Added visualisation function to see the occupied points in the grid
-- Added unordered set for fast lookup of occupied cells -- Added logic to fast iterate the occupied points from vast grid -- Added occupancy probability computation logic
1 parent 87347e1 commit 612e04c

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/occupancy_grid.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ OccupancyGrid::OccupancyGrid(const std::array<size_t, 3>& dimensions,
1616

1717
size_t total_cells = grid_dimensions[0] * grid_dimensions[1] * grid_dimensions[2];
1818
grid_data.resize(total_cells,0.0f);
19-
19+
size_t estimated_occupancy_size = total_cells / 1000; // estimate %0.1 occupancy
20+
occupied_cells.reserve(estimated_occupancy_size);
2021
std::cout << "OccupancyGrid3D initialized:" << std::endl;
2122
std::cout << " Dimensions: " << grid_dimensions[0] << "×" << grid_dimensions[1] << "×" << grid_dimensions[2] << std::endl;
2223
std::cout << " Resolution: " << voxel_resolution << "m" << std::endl;
@@ -62,6 +63,12 @@ void OccupancyGrid::update_occupied(const Vector3i& idx) {
6263
size_t index = to_LinearIndex(idx);
6364
grid_data[index] = std::clamp(grid_data[index]+ LOG_ODDS_OCCUPIED,
6465
LOG_ODDS_MIN, LOG_ODDS_MAX);
66+
67+
// NEW: Track in auxiliary structure
68+
float prob = 1.0f / (1.0f + std::exp(-grid_data[index]));
69+
if (prob > 0.5f) { // Threshold for "occupied"
70+
occupied_cells.insert(idx);
71+
}
6572
}
6673

6774
// updates occupancy
@@ -70,6 +77,11 @@ void OccupancyGrid::update_free(const Vector3i& idx) {
7077
size_t index = to_LinearIndex(idx);
7178
grid_data[index] = std::clamp(grid_data[index]+ LOG_ODDS_FREE,
7279
LOG_ODDS_MIN, LOG_ODDS_MAX);
80+
81+
float prob = 1.0f / (1.0f + std::exp(-grid_data[index]));
82+
if (prob <= 0.5f) {
83+
occupied_cells.erase(idx);
84+
}
7385
}
7486

7587
// Simple getOccupancy for testing
@@ -222,8 +234,24 @@ void OccupancyGrid::rayTrace(const Vector3d& start, const Vector3d& end) {
222234
}
223235

224236
Vector3dVector OccupancyGrid::getOccupiedVoxels(float threshold) const {
225-
// TODO: Implement visualization extraction
226-
// PRIORITY: HIGH - Needed for final visualization
237+
// Implement visualization extraction
227238
Vector3dVector occupied_points;
239+
occupied_points.reserve(occupied_cells.size()); // Estimated occupancy
240+
241+
std::for_each(occupied_cells.begin(), occupied_cells.end(),
242+
[this, &occupied_points, threshold](const Vector3i& idx){
243+
size_t linear_idx = to_LinearIndex(idx);
244+
float prob = 1.0f / (1.0f + std::exp(-grid_data[linear_idx]));
245+
if (prob > threshold) {
246+
occupied_points.emplace_back(grid_to_world(idx));
247+
}
248+
}
249+
);
250+
251+
std::cout << "Extracted " << occupied_points.size()
252+
<< " occupied voxels from " << occupied_cells.size()
253+
<< " tracked voxels (vs " << (grid_dimensions[0] * grid_dimensions[1] * grid_dimensions[2])
254+
<< " total voxels)" << std::endl;
255+
228256
return occupied_points;
229257
}

src/occupancy_grid.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <Eigen/Core>
44
#include <array>
55
#include <vector>
6+
#include <unordered_set>
67

78
using Vector3dVector = std::vector<Eigen::Vector3d>;
89
using Vector3d = Eigen::Vector3d;
@@ -15,7 +16,19 @@ class OccupancyGrid {
1516
double voxel_resolution;
1617
// links grid coordinates with world coordinates
1718
Vector3d grid_origin;
19+
// has the occupancy grid probability data
1820
std::vector<float> grid_data;
21+
// Hash function for Vector3i
22+
struct Vector3iHash {
23+
size_t operator()(const Eigen::Vector3i& v) const {
24+
return std::hash<int>()(v.x()) ^
25+
(std::hash<int>()(v.y()) << 1) ^
26+
(std::hash<int>()(v.z()) << 2);
27+
}
28+
};
29+
// additional storage for fast lookup of occupied cells, would be updated as part of ray tracing
30+
std::unordered_set<Vector3i,Vector3iHash> occupied_cells;
31+
1932
// connects the start and end point of lidar ray in voxel map
2033
std::vector<Vector3i> draw_bresenham3d_line(const Vector3i& start, const Vector3i& end) const;
2134
// updates the log odds in the grid data for the voxel

0 commit comments

Comments
 (0)