QualityEvaluator_Voxels.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-2021 Jose Luis Blanco, University of Almeria
4  * See LICENSE for license information.
5  * ------------------------------------------------------------------------- */
14 #include <mrpt/maps/COccupancyGridMap3D.h>
15 #include <mrpt/maps/CSimplePointsMap.h>
16 #include <mrpt/version.h>
17 
19 
20 using namespace mp2p_icp;
21 
23 {
24  mrpt::system::COutputLogger::setLoggerName("QualityEvaluator_Voxels");
25 }
26 
28  const mrpt::containers::yaml& params)
29 {
30  MCP_LOAD_OPT(params, resolution);
31  MCP_LOAD_OPT(params, maxOccupancyUpdateCertainty);
32  MCP_LOAD_OPT(params, maxFreenessUpdateCertainty);
33  MCP_LOAD_OPT(params, dist2quality_scale);
34 }
35 
36 static double loss(double x, double y)
37 {
38  /*
39 
40  D=[0 0 +1;0 0.5 0; 0 1 -1;...
41  0.5 0 0; 0.5 0.5 0; 0.5 1 0;...
42  1 0 -1; 1 0.5 0; 1 1 +1];
43  x=D(:,1); Y=D(:,2); z=D(:,3);
44 
45  sf = fit([x, y],z,'poly22')
46  plot(sf,[x,y],z)
47 
48  */
49 
50  return 1.0 - 2 * x - 2 * y + 4 * x * y;
51 }
52 
54  const metric_map_t& pcGlobal, const metric_map_t& pcLocal,
55  const mrpt::poses::CPose3D& localPose,
56  [[maybe_unused]] const Pairings& pairingsFromICP) const
57 {
58  // Build both voxel maps:
59  // ----------------------------------
60  const auto r = resolution;
61 
62  mrpt::maps::COccupancyGridMap3D voxelsGlo({-r, -r, -r}, {r, r, r}, r);
63  mrpt::maps::COccupancyGridMap3D voxelsLoc({-r, -r, -r}, {r, r, r}, r);
64  // voxels default occupancy probability: 0.5
65 
66  auto& io = voxelsGlo.insertionOptions;
67 
68  io.maxOccupancyUpdateCertainty = maxOccupancyUpdateCertainty;
69  io.maxFreenessUpdateCertainty = maxFreenessUpdateCertainty;
70 
71  for (const auto& ly : pointLayers)
72  {
73  auto itG = pcGlobal.layers.find(ly);
74  auto itL = pcLocal.layers.find(ly);
75  if (itG == pcGlobal.layers.end() || itL == pcLocal.layers.end())
76  {
77  MRPT_LOG_ERROR_FMT(
78  "Layer `%s` not found in both global/local layers.",
79  ly.c_str());
80  return 0;
81  }
82 
83  auto ptsG =
84  std::dynamic_pointer_cast<mrpt::maps::CPointsMap>(itG->second);
85  auto ptsL =
86  std::dynamic_pointer_cast<mrpt::maps::CPointsMap>(itL->second);
87  ASSERT_(ptsG);
88  ASSERT_(ptsL);
89 
90  mrpt::maps::CSimplePointsMap localTransformed;
91  localTransformed.insertAnotherMap(ptsL.get(), localPose);
92 
93  // resize voxel grids?
94  MRPT_TODO("Check against current size too, for many layers");
95  {
96  const auto bb = ptsG->boundingBox();
97  voxelsGlo.resizeGrid(bb.min, bb.max);
98  }
99  {
100  const auto bb = localTransformed.boundingBox();
101  voxelsLoc.resizeGrid(bb.min, bb.max);
102  }
103  // insert:
104  voxelsGlo.insertPointCloud({0, 0, 0}, *ptsG);
105  voxelsLoc.insertPointCloud(
106  mrpt::math::TPoint3D(localPose.asTPose()), localTransformed);
107  }
108 
109  // Compare them:
110  // ----------------------------------
111  const auto& g = voxelsGlo.m_grid;
112  const auto& l = voxelsLoc.m_grid;
113 
114  // Kullback-Leibler distance:
115  double dist = 0;
116  size_t dist_cells = 0;
117 
118  for (size_t igx = 0; igx < g.getSizeX(); igx++)
119  {
120  const int ilx = l.x2idx(g.idx2x(igx));
121  if (ilx < 0 || static_cast<size_t>(ilx) >= l.getSizeX()) continue;
122 
123  for (size_t igy = 0; igy < g.getSizeY(); igy++)
124  {
125  const int ily = l.y2idx(g.idx2y(igy));
126  if (ily < 0 || static_cast<size_t>(ily) >= l.getSizeY()) continue;
127 
128  for (size_t igz = 0; igz < g.getSizeZ(); igz++)
129  {
130  const int ilz = l.z2idx(g.idx2z(igz));
131  if (ilz < 0 || static_cast<size_t>(ilz) >= l.getSizeZ())
132  continue;
133 
134  // derreferencing should be safe given all checks above: don't
135  // throw exceptions to allow compiler optimizations:
136  const auto gCell = *g.cellByIndex(igx, igy, igz);
137  const auto lCell = *l.cellByIndex(ilx, ily, ilz);
138 
139  const float gProb = voxelsGlo.l2p(gCell);
140  const float lProb = voxelsLoc.l2p(lCell);
141 
142  if (std::abs(gProb - 0.5) < 0.01 ||
143  std::abs(lProb - 0.5) < 0.01)
144  continue;
145 
146  const double d = loss(lProb, gProb);
147  dist += d;
148 
149  dist_cells++;
150  }
151  }
152  }
153 
154  double quality = 0;
155 
156  if (dist_cells)
157  {
158  dist /= dist_cells;
159  quality = 1.0 / (1.0 + std::exp(-dist2quality_scale * dist));
160  }
161 #if 0
162  MRPT_LOG_WARN_STREAM(
163  "dist: " << dist << " dist_cells: " << dist_cells
164  << " quality: " << quality);
165 #endif
166 
167  return quality;
168 }
mp2p_icp
Definition: covariance.h:17
mp2p_icp::QualityEvaluator_Voxels::maxFreenessUpdateCertainty
double maxFreenessUpdateCertainty
<[0.5,1.0]
Definition: QualityEvaluator_Voxels.h:38
mp2p_icp::QualityEvaluator_Voxels
Definition: QualityEvaluator_Voxels.h:22
mp2p_icp::Pairings
Definition: Pairings.h:94
QualityEvaluator_Voxels.h
Matching quality evaluator: comparison via voxel occupancy.
mp2p_icp::QualityEvaluator_Voxels::initialize
void initialize(const mrpt::containers::yaml &params) override
Definition: QualityEvaluator_Voxels.cpp:27
test.x
x
Definition: test.py:4
d
d
mp2p_icp::QualityEvaluator_Voxels::dist2quality_scale
double dist2quality_scale
Definition: QualityEvaluator_Voxels.h:39
mp2p_icp::QualityEvaluator_Voxels::evaluate
double evaluate(const metric_map_t &pcGlobal, const metric_map_t &pcLocal, const mrpt::poses::CPose3D &localPose, const Pairings &pairingsFromICP) const override
Definition: QualityEvaluator_Voxels.cpp:53
mp2p_icp::QualityEvaluator_Voxels::maxOccupancyUpdateCertainty
double maxOccupancyUpdateCertainty
Definition: QualityEvaluator_Voxels.h:37
loss
static double loss(double x, double y)
Definition: QualityEvaluator_Voxels.cpp:36
mp2p_icp::QualityEvaluator_Voxels::resolution
double resolution
voxel size [meters]
Definition: QualityEvaluator_Voxels.h:36
mp2p_icp::metric_map_t
Generic container of pointcloud(s), extracted features and other maps.
Definition: metricmap.h:47
IMPLEMENTS_MRPT_OBJECT
IMPLEMENTS_MRPT_OBJECT(metric_map_t, mrpt::serialization::CSerializable, mp2p_icp) using namespace mp2p_icp
mp2p_icp::metric_map_t::layers
std::map< layer_name_t, mrpt::maps::CMetricMap::Ptr > layers
Definition: metricmap.h:74
mp2p_icp::QualityEvaluator_Voxels::pointLayers
std::set< std::string > pointLayers
Definition: QualityEvaluator_Voxels.h:42
mp2p_icp::QualityEvaluator_Voxels::QualityEvaluator_Voxels
QualityEvaluator_Voxels()
Definition: QualityEvaluator_Voxels.cpp:22
mp2p_icp::QualityEvaluator
Definition: QualityEvaluator.h:26


mrpt_local_obstacles
Author(s): Jose-Luis Blanco-Claraco
autogenerated on Mon Aug 14 2023 02:09:04