@@ -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
224236Vector3dVector 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}
0 commit comments