@@ -49,9 +49,11 @@ namespace volume_grid
4949SpatioTemporalVoxelGrid::SpatioTemporalVoxelGrid (
5050 rclcpp::Clock::SharedPtr clock,
5151 const float & voxel_size, const double & background_value,
52- const int & decay_model, const double & voxel_decay, const bool & pub_voxels)
52+ const int & decay_model, const double & voxel_decay, const double & voxel_distance_decay,
53+ const bool & pub_voxels)
5354: _clock(clock), _decay_model(decay_model), _background_value(background_value),
54- _voxel_size (voxel_size), _voxel_decay(voxel_decay), _pub_voxels(pub_voxels),
55+ _voxel_size (voxel_size), _voxel_decay(voxel_decay), _voxel_distance_decay(voxel_distance_decay),
56+ _pub_voxels(pub_voxels),
5557 _grid_points(std::make_unique<std::vector<geometry_msgs::msg::Point32>>()),
5658 _cost_map(new std::unordered_map<occupany_cell, uint>)
5759/* ****************************************************************************/
@@ -95,7 +97,7 @@ void SpatioTemporalVoxelGrid::InitializeGrid(void)
9597/* ****************************************************************************/
9698void SpatioTemporalVoxelGrid::ClearFrustums (
9799 const std::vector<observation::MeasurementReading> & clearing_readings,
98- std::unordered_set<occupany_cell> & cleared_cells)
100+ std::unordered_set<occupany_cell> & cleared_cells, openvdb::Vec3d & robot_pose_world )
99101/* ****************************************************************************/
100102{
101103 boost::unique_lock<boost::mutex> lock (_grid_lock);
@@ -113,7 +115,7 @@ void SpatioTemporalVoxelGrid::ClearFrustums(
113115 std::vector<frustum_model> obs_frustums;
114116
115117 if (clearing_readings.size () == 0 ) {
116- TemporalClearAndGenerateCostmap (obs_frustums, cleared_cells);
118+ TemporalClearAndGenerateCostmap (obs_frustums, cleared_cells, robot_pose_world );
117119 return ;
118120 }
119121
@@ -142,20 +144,22 @@ void SpatioTemporalVoxelGrid::ClearFrustums(
142144 frustum->TransformModel ();
143145 obs_frustums.emplace_back (frustum, it->_decay_acceleration );
144146 }
145- TemporalClearAndGenerateCostmap (obs_frustums, cleared_cells);
147+ TemporalClearAndGenerateCostmap (obs_frustums, cleared_cells, robot_pose_world );
146148}
147149
148150/* ****************************************************************************/
149151void SpatioTemporalVoxelGrid::TemporalClearAndGenerateCostmap (
150152 std::vector<frustum_model> & frustums,
151- std::unordered_set<occupany_cell> & cleared_cells)
153+ std::unordered_set<occupany_cell> & cleared_cells,
154+ openvdb::Vec3d & robot_pose_world)
152155/* ****************************************************************************/
153156{
154157 // sample time once for all clearing readings
155158 const double cur_time = _clock->now ().seconds ();
156159
157160 // check each point in the grid for inclusion in a frustum
158161 openvdb::DoubleGrid::ValueOnCIter cit_grid = _grid->cbeginValueOn ();
162+ double voxel_distance_decay_squared = _voxel_distance_decay * _voxel_distance_decay;
159163 for (; cit_grid.test (); ++cit_grid) {
160164 const openvdb::Coord pt_index (cit_grid.getCoord ());
161165 const openvdb::Vec3d pose_world = this ->IndexToWorld (pt_index);
@@ -164,6 +168,23 @@ void SpatioTemporalVoxelGrid::TemporalClearAndGenerateCostmap(
164168 bool frustum_cycle = false ;
165169 bool cleared_point = false ;
166170
171+ // spatial filtering
172+ if (_voxel_distance_decay > 0.0 ) {
173+ // check squared distance from robot to voxel
174+ double distance_2d_squared =
175+ (pose_world[0 ] - robot_pose_world[0 ]) * (pose_world[0 ] - robot_pose_world[0 ]) +
176+ (pose_world[1 ] - robot_pose_world[1 ]) * (pose_world[1 ] - robot_pose_world[1 ]);
177+
178+ if (distance_2d_squared > voxel_distance_decay_squared) {
179+ cleared_point = true ;
180+ if (!this ->ClearGridPoint (pt_index)) {
181+ std::cout << " Failed to clear point." << std::endl;
182+ }
183+ cleared_cells.insert (occupany_cell (pose_world[0 ], pose_world[1 ]));
184+ continue ;
185+ }
186+ }
187+
167188 const double time_since_marking = cur_time - cit_grid.getValue ();
168189 const double base_duration_to_decay = GetTemporalClearingDuration (
169190 time_since_marking);
0 commit comments