Parameterizable.h
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  * ------------------------------------------------------------------------- */
6 
7 #pragma once
8 
9 #include <mrpt/expr/CRuntimeCompiledExpression.h>
10 
11 #include <cstdint>
12 #include <optional>
13 #include <set>
14 #include <string>
15 #include <variant>
16 #include <vector>
17 
18 namespace mp2p_icp
19 {
20 namespace internal
21 {
23 {
26  std::optional<mrpt::expr::CRuntimeCompiledExpression> compiled;
27  std::variant<std::monostate, double*, float*, uint32_t*> target;
28  bool is_constant = false;
29  bool has_been_evaluated = false;
30 };
31 } // namespace internal
32 
33 class ParameterSource;
34 class Parameterizable;
35 
43 {
44  public:
45  ParameterSource() = default;
46 
47  void attach(Parameterizable& obj);
48 
51  void updateVariable(const std::string& variable, double value) { variables_[variable] = value; }
52 
54  void updateVariables(const std::vector<std::pair<std::string, double>>& nameValuePairs)
55  {
56  for (const auto& [name, value] : nameValuePairs) updateVariable(name, value);
57  }
58 
59  void realize();
60 
62 
64  auto getVariableValues() const -> std::map<std::string, double> { return variables_; }
65 
66  private:
67  // Attached clients.
68  std::map<std::string, double> variables_;
69  std::set<internal::InfoPerParam*> attachedDeclParameters_;
70 };
71 
78 {
79  friend class ParameterSource;
80 
81  public:
85  virtual void attachToParameterSource(ParameterSource& source) { source.attach(*this); }
86 
87  auto& declaredParameters() { return declParameters_; }
88  const auto& declaredParameters() const { return declParameters_; }
89 
91  const ParameterSource* attachedSource() const { return attachedSource_; }
92 
98  void checkAllParametersAreRealized() const;
99 
101  void unrealizeParameters();
102 
103  protected:
111  void parseAndDeclareParameter(const std::string& value, double& target);
112 
114  void parseAndDeclareParameter(const std::string& value, float& target);
116  void parseAndDeclareParameter(const std::string& value, uint32_t& target);
117 
118  private:
120  std::vector<internal::InfoPerParam> declParameters_;
122 
123  template <typename T>
124  void parseAndDeclareParameter_impl(const std::string& value, T& target);
125 };
126 
128 template <typename T>
130  std::vector<std::shared_ptr<T>>& setObjects, ParameterSource& source)
131 {
132  for (auto& objPtr : setObjects)
133  {
134  auto o = std::dynamic_pointer_cast<Parameterizable>(objPtr);
135  if (!o) continue;
136  o->attachToParameterSource(source);
137  }
138 }
139 
144 {
145  o.attachToParameterSource(source);
146 }
147 
148 #define DECLARE_PARAMETER_IN_OPT(__yaml, __variable, __object) \
149  __object.mp2p_icp::Parameterizable::parseAndDeclareParameter( \
150  __yaml.getOrDefault(#__variable, std::to_string(__variable)), __variable);
151 
152 #define DECLARE_PARAMETER_OPT(__yaml, __variable) \
153  DECLARE_PARAMETER_IN_OPT(__yaml, __variable, (*this))
154 
155 #define DECLARE_PARAMETER_IN_REQ(__yaml, __variable, __object) \
156  if (!__yaml.has(#__variable)) \
157  throw std::invalid_argument(mrpt::format( \
158  "Required parameter `%s` not an existing key in dictionary.", #__variable)); \
159  __object.mp2p_icp::Parameterizable::parseAndDeclareParameter( \
160  __yaml[#__variable].as<std::string>(), __variable);
161 
162 #define DECLARE_PARAMETER_REQ(__yaml, __variable) \
163  DECLARE_PARAMETER_IN_REQ(__yaml, __variable, (*this))
164 
165 } // namespace mp2p_icp
mp2p_icp::Parameterizable::attachedSource_
ParameterSource * attachedSource_
Definition: Parameterizable.h:121
mp2p_icp::Parameterizable::checkAllParametersAreRealized
void checkAllParametersAreRealized() const
Definition: Parameterizable.cpp:135
mp2p_icp
Definition: covariance.h:17
mp2p_icp::Parameterizable::attachedSource
const ParameterSource * attachedSource() const
Definition: Parameterizable.h:91
mp2p_icp::ParameterSource::printVariableValues
std::string printVariableValues() const
Definition: Parameterizable.cpp:23
mp2p_icp::ParameterSource
Definition: Parameterizable.h:42
mp2p_icp::ParameterSource::attach
void attach(Parameterizable &obj)
Definition: Parameterizable.cpp:15
mp2p_icp::internal::InfoPerParam::expression
std::string expression
Definition: Parameterizable.h:24
mp2p_icp::Parameterizable::declaredParameters
const auto & declaredParameters() const
Definition: Parameterizable.h:88
mp2p_icp::Parameterizable::declaredParameters
auto & declaredParameters()
Definition: Parameterizable.h:87
mp2p_icp::internal::InfoPerParam
Definition: Parameterizable.h:22
mp2p_icp::ParameterSource::getVariableValues
auto getVariableValues() const -> std::map< std::string, double >
Definition: Parameterizable.h:64
mp2p_icp::ParameterSource::realize
void realize()
Definition: Parameterizable.cpp:37
testing::internal::string
::std::string string
Definition: gtest.h:1979
mp2p_icp::internal::InfoPerParam::has_been_evaluated
bool has_been_evaluated
Definition: Parameterizable.h:29
mp2p_icp::Parameterizable::parseAndDeclareParameter_impl
void parseAndDeclareParameter_impl(const std::string &value, T &target)
Definition: Parameterizable.cpp:91
mp2p_icp::internal::InfoPerParam::target
std::variant< std::monostate, double *, float *, uint32_t * > target
Definition: Parameterizable.h:27
mp2p_icp::ParameterSource::updateVariable
void updateVariable(const std::string &variable, double value)
Definition: Parameterizable.h:51
mp2p_icp::Parameterizable
Definition: Parameterizable.h:77
mp2p_icp::ParameterSource::attachedDeclParameters_
std::set< internal::InfoPerParam * > attachedDeclParameters_
Definition: Parameterizable.h:69
mp2p_icp::Parameterizable::attachedSource
ParameterSource * attachedSource()
Definition: Parameterizable.h:90
mp2p_icp::ParameterSource::ParameterSource
ParameterSource()=default
mp2p_icp::internal::InfoPerParam::compiled
std::optional< mrpt::expr::CRuntimeCompiledExpression > compiled
Compiled expression.
Definition: Parameterizable.h:26
mp2p_icp::ParameterSource::variables_
std::map< std::string, double > variables_
Definition: Parameterizable.h:68
mp2p_icp::ParameterSource::updateVariables
void updateVariables(const std::vector< std::pair< std::string, double >> &nameValuePairs)
Definition: Parameterizable.h:54
mp2p_icp::Parameterizable::parseAndDeclareParameter
void parseAndDeclareParameter(const std::string &value, double &target)
Definition: Parameterizable.cpp:120
mp2p_icp::Parameterizable::attachToParameterSource
virtual void attachToParameterSource(ParameterSource &source)
Definition: Parameterizable.h:85
std
mp2p_icp::Parameterizable::unrealizeParameters
void unrealizeParameters()
Mark all non-constant parameters as non-evaluated again.
Definition: Parameterizable.cpp:154
mp2p_icp::Parameterizable::declParameters_
std::vector< internal::InfoPerParam > declParameters_
List of declared parameters:
Definition: Parameterizable.h:120
mp2p_icp::AttachToParameterSource
void AttachToParameterSource(std::vector< std::shared_ptr< T >> &setObjects, ParameterSource &source)
Definition: Parameterizable.h:129
mp2p_icp::internal::InfoPerParam::is_constant
bool is_constant
Definition: Parameterizable.h:28


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