FilterRemoveByVoxelOccupancy.cpp
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------
2  * A repertory of multi primitive-to-primitive (MP2P) ICP algorithms in C++
3  * Copyright (C) 2018-2024 Jose Luis Blanco, University of Almeria
4  * See LICENSE for license information.
5  * ------------------------------------------------------------------------- */
16 #include <mrpt/containers/yaml.h>
17 #include <mrpt/maps/CVoxelMap.h>
18 
20 
21 using namespace mp2p_icp_filters;
22 
24  const mrpt::containers::yaml& c, FilterRemoveByVoxelOccupancy& parent)
25 {
26  MCP_LOAD_REQ(c, input_pointcloud_layer);
27  MCP_LOAD_REQ(c, input_voxel_layer);
28  MCP_LOAD_OPT(c, output_layer_static_objects);
29  MCP_LOAD_OPT(c, output_layer_dynamic_objects);
30 
32 }
33 
35 {
36  mrpt::system::COutputLogger::setLoggerName("FilterRemoveByVoxelOccupancy");
37 }
38 
39 void FilterRemoveByVoxelOccupancy::initialize(const mrpt::containers::yaml& c)
40 {
41  MRPT_START
42 
43  MRPT_LOG_DEBUG_STREAM("Loading these params:\n" << c);
44  params_.load_from_yaml(c, *this);
45 
46  MRPT_END
47 }
48 
50 {
51  MRPT_START
52 
54 
55  ASSERT_LE_(params_.occupancy_threshold, 1.0);
56  ASSERT_GE_(params_.occupancy_threshold, 0.0);
57 
58  // In pts:
59  ASSERTMSG_(
60  inOut.layers.count(params_.input_pointcloud_layer) != 0,
61  mrpt::format("Input layer '%s' not found.", params_.input_pointcloud_layer.c_str()));
62 
63  const auto mapPtr = inOut.layers.at(params_.input_pointcloud_layer);
64  ASSERT_(mapPtr);
65 
66  const auto pcPtr = mp2p_icp::MapToPointsMap(*mapPtr);
67  ASSERTMSG_(
68  pcPtr, mrpt::format(
69  "Input point cloud layer '%s' could not be converted into a "
70  "point cloud (class='%s')",
71  params_.input_pointcloud_layer.c_str(), mapPtr->GetRuntimeClass()->className));
72 
73  // In voxels:
74  ASSERTMSG_(
75  inOut.layers.count(params_.input_voxel_layer) != 0,
76  mrpt::format("Input layer '%s' not found.", params_.input_voxel_layer.c_str()));
77 
78  const auto voxelMapPtr = inOut.layers.at(params_.input_voxel_layer);
79  ASSERT_(voxelMapPtr);
80 
81  const auto voxelPtr = std::dynamic_pointer_cast<mrpt::maps::CVoxelMap>(voxelMapPtr);
82  ASSERTMSG_(
83  voxelPtr,
84  mrpt::format(
85  "Input voxel layer '%s' not of type mrpt::maps::CVoxelMap"
86  "(actual class='%s')",
87  params_.input_voxel_layer.c_str(), voxelMapPtr->GetRuntimeClass()->className));
88 
89  // Outputs:
90  // Create if new: Append to existing layer, if already existed.
91  mrpt::maps::CPointsMap::Ptr outPcStatic = GetOrCreatePointLayer(
93  /*allow empty name for nullptr*/
94  true,
95  /* create cloud of the same type */
96  pcPtr->GetRuntimeClass()->className);
97  if (outPcStatic) outPcStatic->reserve(outPcStatic->size() + pcPtr->size() / 2);
98 
99  mrpt::maps::CPointsMap::Ptr outPcDynamic = GetOrCreatePointLayer(
101  /*allow empty name for nullptr*/
102  true,
103  /* create cloud of the same type */
104  pcPtr->GetRuntimeClass()->className);
105  if (outPcDynamic) outPcDynamic->reserve(outPcDynamic->size() + pcPtr->size() / 2);
106 
107  ASSERTMSG_(
108  outPcStatic || outPcDynamic,
109  "At least one 'output_layer_static_objects' or "
110  "'output_layer_dynamic_objects' output layers must be provided.");
111 
112  // Process occupancy input value so it lies within [0,0.5]:
113  const double occFree = params_.occupancy_threshold > 0.5 ? (1.0 - params_.occupancy_threshold)
115  const double occThres = 1.0 - occFree;
116 
117  // Process:
118  const auto& xs = pcPtr->getPointsBufferRef_x();
119  const auto& ys = pcPtr->getPointsBufferRef_y();
120  const auto& zs = pcPtr->getPointsBufferRef_z();
121  const size_t N = xs.size();
122 
123  size_t nStatic = 0, nDynamic = 0;
124 
125  for (size_t i = 0; i < N; i++)
126  {
127  double prob_occupancy = 0.5;
128  bool withinMap = voxelPtr->getPointOccupancy(xs[i], ys[i], zs[i], prob_occupancy);
129  if (!withinMap) continue; // undefined! pt out of voxelmap
130 
131  mrpt::maps::CPointsMap* trgMap = nullptr;
132 
133  if (prob_occupancy > occThres)
134  {
135  trgMap = outPcStatic.get();
136  nDynamic++;
137  }
138  else if (prob_occupancy < occFree)
139  {
140  trgMap = outPcDynamic.get();
141  nStatic++;
142  }
143 
144  if (!trgMap) continue;
145 
146  trgMap->insertPointFrom(*pcPtr, i);
147  }
148 
149  MRPT_LOG_DEBUG_STREAM(
150  "Parsed " << N << " points: static=" << nStatic << ", dynamic=" << nDynamic);
151 
152  MRPT_END
153 }
mp2p_icp::Parameterizable::checkAllParametersAreRealized
void checkAllParametersAreRealized() const
Definition: Parameterizable.cpp:135
mp2p_icp_filters::FilterRemoveByVoxelOccupancy::Parameters::occupancy_threshold
double occupancy_threshold
Definition: FilterRemoveByVoxelOccupancy.h:61
mp2p_icp_filters::FilterRemoveByVoxelOccupancy::params_
Parameters params_
Definition: FilterRemoveByVoxelOccupancy.h:65
IMPLEMENTS_MRPT_OBJECT
IMPLEMENTS_MRPT_OBJECT(FilterDecimateVoxelsQuadratic, mp2p_icp_filters::FilterBase, mp2p_icp_filters) using namespace mp2p_icp_filters
DECLARE_PARAMETER_IN_REQ
#define DECLARE_PARAMETER_IN_REQ(__yaml, __variable, __object)
Definition: Parameterizable.h:155
mp2p_icp::MapToPointsMap
const mrpt::maps::CPointsMap * MapToPointsMap(const mrpt::maps::CMetricMap &map)
Definition: metricmap.cpp:624
mp2p_icp_filters::FilterBase
Definition: FilterBase.h:46
mp2p_icp_filters::GetOrCreatePointLayer
mrpt::maps::CPointsMap::Ptr GetOrCreatePointLayer(mp2p_icp::metric_map_t &m, const std::string &layerName, bool allowEmptyName=true, const std::string &classForLayerCreation="mrpt::maps::CSimplePointsMap")
Definition: GetOrCreatePointLayer.cpp:15
mp2p_icp_filters::FilterRemoveByVoxelOccupancy::Parameters::input_voxel_layer
std::string input_voxel_layer
Definition: FilterRemoveByVoxelOccupancy.h:58
mp2p_icp_filters::FilterRemoveByVoxelOccupancy::Parameters::output_layer_dynamic_objects
std::string output_layer_dynamic_objects
Definition: FilterRemoveByVoxelOccupancy.h:60
mp2p_icp_filters::FilterRemoveByVoxelOccupancy
Definition: FilterRemoveByVoxelOccupancy.h:41
FilterRemoveByVoxelOccupancy.h
Removes points from an input point cloud layer by occupancy of another input voxel layer....
mp2p_icp_filters::FilterRemoveByVoxelOccupancy::Parameters::load_from_yaml
void load_from_yaml(const mrpt::containers::yaml &c, FilterRemoveByVoxelOccupancy &parent)
Definition: FilterRemoveByVoxelOccupancy.cpp:23
mp2p_icp_filters::FilterRemoveByVoxelOccupancy::Parameters::output_layer_static_objects
std::string output_layer_static_objects
Definition: FilterRemoveByVoxelOccupancy.h:59
GetOrCreatePointLayer.h
Auxiliary function GetOrCreatePointLayer.
mp2p_icp::metric_map_t
Generic container of pointcloud(s), extracted features and other maps.
Definition: metricmap.h:55
mp2p_icp_filters::FilterRemoveByVoxelOccupancy::Parameters::input_pointcloud_layer
std::string input_pointcloud_layer
Definition: FilterRemoveByVoxelOccupancy.h:57
mp2p_icp_filters::FilterRemoveByVoxelOccupancy::FilterRemoveByVoxelOccupancy
FilterRemoveByVoxelOccupancy()
Definition: FilterRemoveByVoxelOccupancy.cpp:34
mp2p_icp::metric_map_t::layers
std::map< layer_name_t, mrpt::maps::CMetricMap::Ptr > layers
Definition: metricmap.h:82
mp2p_icp_filters
Definition: FilterAdjustTimestamps.h:19
mp2p_icp_filters::FilterRemoveByVoxelOccupancy::filter
void filter(mp2p_icp::metric_map_t &inOut) const override
Definition: FilterRemoveByVoxelOccupancy.cpp:49
mp2p_icp_filters::FilterRemoveByVoxelOccupancy::initialize
void initialize(const mrpt::containers::yaml &c) override
Definition: FilterRemoveByVoxelOccupancy.cpp:39


mp2p_icp
Author(s):
autogenerated on Mon May 26 2025 02:45:48