@@ -239,19 +239,42 @@ OccupancyGridMapOutlierFilterComponent::OccupancyGridMapOutlierFilterComponent(
239
239
void OccupancyGridMapOutlierFilterComponent::splitPointCloudFrontBack (
240
240
const PointCloud2::ConstSharedPtr & input_pc, PointCloud2 & front_pc, PointCloud2 & behind_pc)
241
241
{
242
- PclPointCloud tmp_behind_pc;
243
- PclPointCloud tmp_front_pc;
244
- for (sensor_msgs::PointCloud2ConstIterator<float > x (*input_pc, " x" ), y (*input_pc, " y" ),
245
- z (*input_pc, " z" );
246
- x != x.end (); ++x, ++y, ++z) {
242
+ size_t front_count = 0 ;
243
+ size_t behind_count = 0 ;
244
+
245
+ for (sensor_msgs::PointCloud2ConstIterator<float > x (*input_pc, " x" ); x != x.end (); ++x) {
247
246
if (*x < 0.0 ) {
248
- tmp_behind_pc. push_back ( pcl::PointXYZ (*x, *y, *z)) ;
247
+ behind_count++ ;
249
248
} else {
250
- tmp_front_pc. push_back ( pcl::PointXYZ (*x, *y, *z)) ;
249
+ front_count++ ;
251
250
}
252
251
}
253
- pcl::toROSMsg (tmp_front_pc, front_pc);
254
- pcl::toROSMsg (tmp_behind_pc, behind_pc);
252
+
253
+ sensor_msgs::PointCloud2Modifier front_pc_modifier (front_pc);
254
+ sensor_msgs::PointCloud2Modifier behind_pc_modifier (behind_pc);
255
+ front_pc_modifier.setPointCloud2FieldsByString (1 , " xyz" );
256
+ behind_pc_modifier.setPointCloud2FieldsByString (1 , " xyz" );
257
+ front_pc_modifier.resize (front_count);
258
+ behind_pc_modifier.resize (behind_count);
259
+
260
+ sensor_msgs::PointCloud2Iterator<float > fr_iter (front_pc, " x" );
261
+ sensor_msgs::PointCloud2Iterator<float > be_iter (behind_pc, " x" );
262
+
263
+ for (sensor_msgs::PointCloud2ConstIterator<float > in_iter (*input_pc, " x" );
264
+ in_iter != in_iter.end (); ++in_iter) {
265
+ if (*in_iter < 0.0 ) {
266
+ *be_iter = in_iter[0 ];
267
+ be_iter[1 ] = in_iter[1 ];
268
+ be_iter[2 ] = in_iter[2 ];
269
+ ++be_iter;
270
+ } else {
271
+ *fr_iter = in_iter[0 ];
272
+ fr_iter[1 ] = in_iter[1 ];
273
+ fr_iter[2 ] = in_iter[2 ];
274
+ ++fr_iter;
275
+ }
276
+ }
277
+
255
278
front_pc.header = input_pc->header ;
256
279
behind_pc.header = input_pc->header ;
257
280
}
0 commit comments