FilterVoxelSlice.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 #include <mrpt/maps/COccupancyGridMap2D.h>
17 #include <mrpt/maps/CVoxelMap.h>
18 #include <mrpt/maps/CVoxelMapRGB.h>
19 #include <mrpt/math/TPose3D.h>
20 #include <mrpt/obs/CObservationPointCloud.h>
21 
23 
24 using namespace mp2p_icp_filters;
25 
27  const mrpt::containers::yaml& c, FilterVoxelSlice& parent)
28 {
29  MCP_LOAD_REQ(c, input_layer);
30  MCP_LOAD_REQ(c, output_layer);
31 
34 }
35 
37 {
38  mrpt::system::COutputLogger::setLoggerName("FilterVoxelSlice");
39 }
40 
41 void FilterVoxelSlice::initialize(const mrpt::containers::yaml& c)
42 {
43  MRPT_START
44 
45  MRPT_LOG_DEBUG_STREAM("Loading these params:\n" << c);
46  params_.load_from_yaml(c, *this);
47 
48  MRPT_END
49 }
50 
52 {
53  MRPT_START
54 
56 
58 
59  // In:
60  ASSERT_(!params_.input_layer.empty());
61  ASSERTMSG_(
62  inOut.layers.count(params_.input_layer),
63  mrpt::format("Input layer '%s' was not found.", params_.input_layer.c_str()));
64 
65  const auto in = inOut.layers.at(params_.input_layer);
66  auto inVoxelMap = std::dynamic_pointer_cast<mrpt::maps::CVoxelMap>(in);
67  auto inVoxelMapRGB = std::dynamic_pointer_cast<mrpt::maps::CVoxelMapRGB>(in);
68 
69  if (!inVoxelMap && !inVoxelMapRGB)
70  {
71  THROW_EXCEPTION_FMT(
72  "Input layer is of class '%s', expected one of: "
73  "mrpt::maps::CVoxelMap, mrpt::maps::CVoxelMapRGB",
74  in->GetRuntimeClass()->className);
75  }
76 
77  // Out:
78  ASSERT_(!params_.output_layer.empty());
79 
80  auto occGrid = mrpt::maps::COccupancyGridMap2D::Create();
81  inOut.layers[params_.output_layer] = occGrid;
82 
83  // Set the grid "height" (z):
84  occGrid->insertionOptions.mapAltitude = 0.5 * (params_.slice_z_max + params_.slice_z_min);
85 
86  // make the conversion:
87  if (inVoxelMap)
88  {
89  auto& grid =
90  const_cast<Bonxai::VoxelGrid<mrpt::maps::CVoxelMap::voxel_node_t>&>(inVoxelMap->grid());
91 
92  const mrpt::math::TBoundingBoxf bbox = inVoxelMap->boundingBox();
93 
94  occGrid->setSize(bbox.min.x, bbox.max.x, bbox.min.y, bbox.max.y, grid.resolution);
95 
96  const auto zCoordMin =
97  Bonxai::PosToCoord({0., 0., params_.slice_z_min}, grid.inv_resolution);
98  const auto zCoordMax =
99  Bonxai::PosToCoord({0., 0., params_.slice_z_max}, grid.inv_resolution);
100 
101  // Go thru all voxels:
102  auto lmbdPerVoxel =
103  [&](mrpt::maps::CVoxelMap::voxel_node_t& data, const Bonxai::CoordT& coord)
104  {
105  // are we at the correct height?
106  if (coord.z < zCoordMin.z || coord.z > zCoordMax.z) return;
107  const auto pt = Bonxai::CoordToPos(coord, grid.resolution);
108 
109  const double freeness = inVoxelMap->l2p(data.occupancy);
110 
111  // Bayesian fuse information:
112  occGrid->updateCell(occGrid->x2idx(pt.x), occGrid->y2idx(pt.y), freeness);
113  }; // end lambda for each voxel
114 
115  grid.forEachCell(lmbdPerVoxel);
116  }
117  else if (inVoxelMapRGB)
118  {
119  // ...
120  }
121 
122  MRPT_END
123 }
mp2p_icp_filters::FilterVoxelSlice::FilterVoxelSlice
FilterVoxelSlice()
Definition: FilterVoxelSlice.cpp:36
mp2p_icp::Parameterizable::checkAllParametersAreRealized
void checkAllParametersAreRealized() const
Definition: Parameterizable.cpp:135
mp2p_icp_filters::FilterVoxelSlice::Parameters::load_from_yaml
void load_from_yaml(const mrpt::containers::yaml &c, FilterVoxelSlice &parent)
Definition: FilterVoxelSlice.cpp:26
FilterVoxelSlice.h
Takes an input layer of type CVoxelMap (Bonxai) and extracts one 2D slice as an occupancy gridmap.
mp2p_icp_filters::FilterVoxelSlice::filter
void filter(mp2p_icp::metric_map_t &inOut) const override
Definition: FilterVoxelSlice.cpp:51
mp2p_icp_filters::FilterVoxelSlice::initialize
void initialize(const mrpt::containers::yaml &c) override
Definition: FilterVoxelSlice.cpp:41
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_filters::FilterBase
Definition: FilterBase.h:46
mp2p_icp_filters::FilterVoxelSlice
Definition: FilterVoxelSlice.h:30
boost::posix_time
mp2p_icp::metric_map_t
Generic container of pointcloud(s), extracted features and other maps.
Definition: metricmap.h:55
mp2p_icp_filters::FilterVoxelSlice::Parameters::slice_z_min
double slice_z_min
Definition: FilterVoxelSlice.h:49
mp2p_icp_filters::FilterVoxelSlice::Parameters::output_layer
std::string output_layer
Definition: FilterVoxelSlice.h:47
mp2p_icp_filters::FilterVoxelSlice::params_
Parameters params_
Definition: FilterVoxelSlice.h:54
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::FilterVoxelSlice::Parameters::slice_z_max
double slice_z_max
Definition: FilterVoxelSlice.h:50
mp2p_icp_filters::FilterVoxelSlice::Parameters::input_layer
std::string input_layer
Definition: FilterVoxelSlice.h:46


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