FilterByRing.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  * ------------------------------------------------------------------------- */
15 #include <mrpt/containers/yaml.h>
16 
18 
19 using namespace mp2p_icp_filters;
20 
21 void FilterByRing::Parameters::load_from_yaml(const mrpt::containers::yaml& c)
22 {
23  MCP_LOAD_REQ(c, input_pointcloud_layer);
24 
25  MCP_LOAD_OPT(c, output_layer_selected);
26  MCP_LOAD_OPT(c, output_layer_non_selected);
27 
28  selected_ring_ids.clear();
29 
30  auto cfgIn = c["selected_ring_ids"];
31  if (cfgIn.isScalar())
32  {
33  // only one:
34  selected_ring_ids.insert(cfgIn.as<int>());
35  }
36  else
37  {
38  ASSERTMSG_(
39  cfgIn.isSequence(),
40  "YAML configuration must have an entry `selected_ring_ids` "
41  "with a scalar or sequence.");
42 
43  for (const auto& n : cfgIn.asSequenceRange()) selected_ring_ids.insert(n.as<int>());
44  }
45  ASSERT_(!selected_ring_ids.empty());
46 }
47 
48 FilterByRing::FilterByRing() = default;
49 
50 void FilterByRing::initialize(const mrpt::containers::yaml& c)
51 {
52  MRPT_LOG_DEBUG_STREAM("Loading these params:\n" << c);
54 }
55 
57 {
58  MRPT_START
59 
60  // In:
61  const auto& pcPtr = inOut.point_layer(params_.input_pointcloud_layer);
62  ASSERTMSG_(
63  pcPtr,
64  mrpt::format(
65  "Input point cloud layer '%s' was not found.", params_.input_pointcloud_layer.c_str()));
66 
67  const auto& pc = *pcPtr;
68 
69  // Outputs:
70  // Create if new: Append to existing layer, if already existed.
71  mrpt::maps::CPointsMap::Ptr outSelected = GetOrCreatePointLayer(
72  inOut, params_.output_layer_selected, true /*allow empty for nullptr*/,
73  /* create cloud of the same type */
74  pcPtr->GetRuntimeClass()->className);
75 
76  if (outSelected) outSelected->reserve(outSelected->size() + pc.size() / 10);
77 
78  // Create if new: Append to existing layer, if already existed.
79  mrpt::maps::CPointsMap::Ptr outNonSel = GetOrCreatePointLayer(
80  inOut, params_.output_layer_non_selected, true /*allow empty for nullptr*/,
81  /* create cloud of the same type */
82  pcPtr->GetRuntimeClass()->className);
83 
84  if (outNonSel) outNonSel->reserve(outNonSel->size() + pc.size() / 10);
85 
86  ASSERTMSG_(
87  outSelected || outNonSel,
88  "At least one of 'output_layer_selected' or "
89  "'output_layer_non_selected' must be provided.");
90 
91  const auto& xs = pc.getPointsBufferRef_x();
92  // const auto& ys = pc.getPointsBufferRef_y();
93  // const auto& zs = pc.getPointsBufferRef_z();
94  const auto* ptrR = pc.getPointsBufferRef_ring();
95  if (!ptrR || ptrR->empty())
96  {
97  THROW_EXCEPTION_FMT(
98  "Error: this filter needs the input layer '%s' to has an "
99  "'ring' point channel.",
101  }
102 
103  const auto& Rs = *ptrR;
104  ASSERT_EQUAL_(Rs.size(), xs.size());
105  const size_t N = xs.size();
106 
107  size_t countSel = 0, countNon = 0;
108 
109  for (size_t i = 0; i < N; i++)
110  {
111  const auto R = Rs[i];
112 
113  mrpt::maps::CPointsMap* trg = nullptr;
114 
115  if (params_.selected_ring_ids.count(R) != 0)
116  {
117  trg = outSelected.get();
118  ++countSel;
119  }
120  else
121  {
122  trg = outNonSel.get();
123  ++countNon;
124  }
125 
126  if (trg) trg->insertPointFrom(pc, i);
127  }
128 
129  MRPT_LOG_DEBUG_STREAM(
130  "[FilterByRing] Input points=" << N << " selected=" << countSel
131  << " non-selected=" << countNon);
132 
133  MRPT_END
134 }
mp2p_icp_filters::FilterByRing::params_
Parameters params_
Definition: FilterByRing.h:56
mp2p_icp_filters::FilterByRing::Parameters::selected_ring_ids
std::set< int > selected_ring_ids
Definition: FilterByRing.h:52
mp2p_icp_filters::FilterByRing::FilterByRing
FilterByRing()
mp2p_icp_filters::FilterByRing::filter
void filter(mp2p_icp::metric_map_t &inOut) const override
Definition: FilterByRing.cpp:56
mp2p_icp_filters::FilterByRing::Parameters::input_pointcloud_layer
std::string input_pointcloud_layer
Definition: FilterByRing.h:42
mp2p_icp_filters::FilterByRing::initialize
void initialize(const mrpt::containers::yaml &c) override
Definition: FilterByRing.cpp:50
IMPLEMENTS_MRPT_OBJECT
IMPLEMENTS_MRPT_OBJECT(FilterDecimateVoxelsQuadratic, mp2p_icp_filters::FilterBase, mp2p_icp_filters) using namespace mp2p_icp_filters
FilterByRing.h
Keeps only a given subset of an input cloud by LiDAR "ring_id".
mp2p_icp_filters::FilterBase
Definition: FilterBase.h:46
mp2p_icp::metric_map_t::point_layer
mrpt::maps::CPointsMap::Ptr point_layer(const layer_name_t &name) const
Definition: metricmap.cpp:607
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::FilterByRing::Parameters::load_from_yaml
void load_from_yaml(const mrpt::containers::yaml &c)
Definition: FilterByRing.cpp:21
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
Definition: FilterAdjustTimestamps.h:19
mp2p_icp_filters::FilterByRing::Parameters::output_layer_selected
std::string output_layer_selected
Definition: FilterByRing.h:46
mp2p_icp_filters::FilterByRing
Definition: FilterByRing.h:26
mp2p_icp_filters::FilterByRing::Parameters::output_layer_non_selected
std::string output_layer_non_selected
Definition: FilterByRing.h:50


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