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)
52  {
53  variables_[variable] = value;
54  }
55 
58  const std::vector<std::pair<std::string, double>>& nameValuePairs)
59  {
60  for (const auto& [name, value] : nameValuePairs)
61  updateVariable(name, value);
62  }
63 
64  void realize();
65 
67 
69  auto getVariableValues() const -> std::map<std::string, double>
70  {
71  return variables_;
72  }
73 
74  private:
75  // Attached clients.
76  std::map<std::string, double> variables_;
77  std::set<internal::InfoPerParam*> attachedDeclParameters_;
78 };
79 
86 {
87  friend class ParameterSource;
88 
89  public:
94  {
95  source.attach(*this);
96  }
97 
98  auto& declaredParameters() { return declParameters_; }
99  const auto& declaredParameters() const { return declParameters_; }
100 
102  const ParameterSource* attachedSource() const { return attachedSource_; }
103 
109  void checkAllParametersAreRealized() const;
110 
112  void unrealizeParameters();
113 
114  protected:
122  void parseAndDeclareParameter(const std::string& value, double& target);
123 
125  void parseAndDeclareParameter(const std::string& value, float& target);
127  void parseAndDeclareParameter(const std::string& value, uint32_t& target);
128 
129  private:
131  std::vector<internal::InfoPerParam> declParameters_;
133 
134  template <typename T>
135  void parseAndDeclareParameter_impl(const std::string& value, T& target);
136 };
137 
139 template <typename T>
141  std::vector<std::shared_ptr<T>>& setObjects, ParameterSource& source)
142 {
143  for (auto& objPtr : setObjects)
144  {
145  auto o = std::dynamic_pointer_cast<Parameterizable>(objPtr);
146  if (!o) continue;
147  o->attachToParameterSource(source);
148  }
149 }
150 
155 {
156  o.attachToParameterSource(source);
157 }
158 
159 #define DECLARE_PARAMETER_IN_OPT(__yaml, __variable, __object) \
160  __object.mp2p_icp::Parameterizable::parseAndDeclareParameter( \
161  __yaml.getOrDefault(#__variable, std::to_string(__variable)), \
162  __variable);
163 
164 #define DECLARE_PARAMETER_OPT(__yaml, __variable) \
165  DECLARE_PARAMETER_IN_OPT(__yaml, __variable, (*this))
166 
167 #define DECLARE_PARAMETER_IN_REQ(__yaml, __variable, __object) \
168  if (!__yaml.has(#__variable)) \
169  throw std::invalid_argument(mrpt::format( \
170  "Required parameter `%s` not an existing key in dictionary.", \
171  #__variable)); \
172  __object.mp2p_icp::Parameterizable::parseAndDeclareParameter( \
173  __yaml[#__variable].as<std::string>(), __variable);
174 
175 #define DECLARE_PARAMETER_REQ(__yaml, __variable) \
176  DECLARE_PARAMETER_IN_REQ(__yaml, __variable, (*this))
177 
178 } // namespace mp2p_icp
mp2p_icp::Parameterizable::attachedSource_
ParameterSource * attachedSource_
Definition: Parameterizable.h:132
mp2p_icp::Parameterizable::checkAllParametersAreRealized
void checkAllParametersAreRealized() const
Definition: Parameterizable.cpp:138
mp2p_icp
Definition: covariance.h:17
mp2p_icp::Parameterizable::attachedSource
const ParameterSource * attachedSource() const
Definition: Parameterizable.h:102
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:99
mp2p_icp::Parameterizable::declaredParameters
auto & declaredParameters()
Definition: Parameterizable.h:98
mp2p_icp::internal::InfoPerParam
Definition: Parameterizable.h:22
mp2p_icp::ParameterSource::getVariableValues
auto getVariableValues() const -> std::map< std::string, double >
Definition: Parameterizable.h:69
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:90
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:85
mp2p_icp::ParameterSource::attachedDeclParameters_
std::set< internal::InfoPerParam * > attachedDeclParameters_
Definition: Parameterizable.h:77
mp2p_icp::Parameterizable::attachedSource
ParameterSource * attachedSource()
Definition: Parameterizable.h:101
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:76
mp2p_icp::ParameterSource::updateVariables
void updateVariables(const std::vector< std::pair< std::string, double >> &nameValuePairs)
Definition: Parameterizable.h:57
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:93
std
mp2p_icp::Parameterizable::unrealizeParameters
void unrealizeParameters()
Mark all non-constant parameters as non-evaluated again.
Definition: Parameterizable.cpp:157
mp2p_icp::Parameterizable::declParameters_
std::vector< internal::InfoPerParam > declParameters_
List of declared parameters:
Definition: Parameterizable.h:131
mp2p_icp::AttachToParameterSource
void AttachToParameterSource(std::vector< std::shared_ptr< T >> &setObjects, ParameterSource &source)
Definition: Parameterizable.h:140
mp2p_icp::internal::InfoPerParam::is_constant
bool is_constant
Definition: Parameterizable.h:28


mp2p_icp
Author(s): Jose-Luis Blanco-Claraco
autogenerated on Wed Jun 26 2024 02:47:09