Program Listing for File CMetricMapBuilderICP.h

↰ Return to documentation for file (include/mrpt/slam/CMetricMapBuilderICP.h)

/*                    _
                     | |    Mobile Robot Programming Toolkit (MRPT)
 _ __ ___  _ __ _ __ | |_
| '_ ` _ \| '__| '_ \| __|          https://www.mrpt.org/
| | | | | | |  | |_) | |_
|_| |_| |_|_|  | .__/ \__|     https://github.com/MRPT/mrpt/
               | |
               |_|

 Copyright (c) 2005-2026, Individual contributors, see AUTHORS file
 See: https://www.mrpt.org/Authors - All rights reserved.
 SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once

#include <mrpt/poses/CRobot2DPoseEstimator.h>
#include <mrpt/slam/CICP.h>
#include <mrpt/slam/CMetricMapBuilder.h>

#include <map>

namespace mrpt::slam
{
class CMetricMapBuilderICP : public mrpt::slam::CMetricMapBuilder
{
 public:
  CMetricMapBuilderICP();

  ~CMetricMapBuilderICP() override;

  struct TConfigParams : public mrpt::config::CLoadableOptions
  {
    TConfigParams(mrpt::system::VerbosityLevel& parent_verbosity_level);
    TConfigParams& operator=(const TConfigParams& other);  // Copy
    TConfigParams(const TConfigParams& other) : verbosity_level(other.verbosity_level)
    {
      *this = other;
    }

    void loadFromConfigFile(
        const mrpt::config::CConfigFileBase& source,
        const std::string& section) override;                 // See base docs
    void dumpToTextStream(std::ostream& out) const override;  // See base docs
    bool matchAgainstTheGrid;

    double insertionLinDistance;
    double insertionAngDistance;
    double localizationLinDistance;
    double localizationAngDistance;

    double minICPgoodnessToAccept;

    mrpt::system::VerbosityLevel& verbosity_level;

    mrpt::maps::TSetOfMetricMapInitializers mapInitializers;
  };

  TConfigParams ICP_options;
  CICP::TConfigParams ICP_params;

  void initialize(
      const mrpt::maps::CSimpleMap& initialMap = mrpt::maps::CSimpleMap(),
      const mrpt::poses::CPosePDF* x0 = nullptr) override;

  mrpt::poses::CPose3DPDF::Ptr getCurrentPoseEstimation() const override;

  void setCurrentMapFile(const char* mapFile);

  void processActionObservation(
      mrpt::obs::CActionCollection& action, mrpt::obs::CSensoryFrame& in_SF) override;

  void processObservation(const mrpt::obs::CObservation::Ptr& obs);

  void getCurrentlyBuiltMap(mrpt::maps::CSimpleMap& out_map) const override;

  void getCurrentMapPoints(std::vector<float>& x, std::vector<float>& y);

  const mrpt::maps::CMultiMetricMap& getCurrentlyBuiltMetricMap() const override;

  [[nodiscard]] size_t getCurrentlyBuiltMapSize() override;

  void saveCurrentEstimationToImage(const std::string& file) override;

 private:
  mrpt::maps::CSimpleMap SF_Poses_seq;

  mrpt::maps::CMultiMetricMap metricMap;

  std::string currentMapFile;

  mrpt::poses::CRobot2DPoseEstimator m_lastPoseEst;
  mrpt::math::CMatrixDouble33 m_lastPoseEst_cov;

  std::deque<mrpt::math::TPose2D> m_estRobotPath;
  mrpt::poses::CPose2D m_auxAccumOdometry;

  struct TDist
  {
    TDist() = default;
    double lin{0};  // meters
    double ang{0};  // degrees
    mrpt::math::TPose2D last_update;

    void updateDistances(const mrpt::poses::CPose2D& p);
    void updatePose(const mrpt::poses::CPose2D& p);
  };
  TDist m_distSinceLastICP;
  std::map<std::string, TDist> m_distSinceLastInsertion;
  bool m_there_has_been_an_odometry{false};

  void accumulateRobotDisplacementCounters(const mrpt::poses::CPose2D& new_pose);
  void resetRobotDisplacementCounters(const mrpt::poses::CPose2D& new_pose);
};

}  // namespace mrpt::slam