Parameterizable.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  * ------------------------------------------------------------------------- */
6 
8 #include <mrpt/core/exceptions.h>
9 
10 #include <sstream>
11 #include <stdexcept>
12 
13 using namespace mp2p_icp;
14 
16 {
17  for (auto& p : obj.declaredParameters()) //
18  attachedDeclParameters_.insert(&p);
19 
20  obj.attachedSource_ = this;
21 }
22 
24 {
25  std::string s;
26  for (const auto& [name, value] : variables_)
27  {
28  s += name;
29  s += "=";
30  s += std::to_string(value);
31  s += " ";
32  }
33 
34  return s;
35 }
36 
38 {
39  // Here comes the beef:
40  // 1) Compile uncompiled expressions,
41  // 2) Eval them all and store the results in their target.
42 
43  // 1) compile?
44  for (auto& p : attachedDeclParameters_)
45  {
46  if (p->is_constant) continue;
47  if (p->compiled.has_value()) continue; // already done:
48 
49  auto& expr = p->compiled.emplace();
50  expr.compile(p->expression, variables_);
51  }
52 
53  // 2) Evaluate and store:
54  for (auto& p : attachedDeclParameters_)
55  {
56  if (p->is_constant) continue;
57 
58  const double val = p->compiled->eval();
59 
60  std::visit(
61  [val](auto&& arg) {
62  using T = std::decay_t<decltype(arg)>;
63 
64  if constexpr (std::is_same_v<T, std::monostate>)
65  {
66  throw std::runtime_error(
67  "[ParameterSource] Attached parameter target is "
68  "monostate!");
69  }
70  else if constexpr (std::is_same_v<T, double*>)
71  {
72  *arg = val;
73  }
74  else if constexpr (std::is_same_v<T, float*>)
75  {
76  *arg = static_cast<float>(val);
77  }
78  else if constexpr (std::is_same_v<T, uint32_t*>)
79  {
80  *arg = static_cast<uint32_t>(val);
81  }
82  },
83  p->target);
84 
85  p->has_been_evaluated = true;
86  }
87 }
88 
89 template <typename T>
91  const std::string& value, T& target)
92 {
93  internal::InfoPerParam& ipm = declParameters_.emplace_back();
94 
95  ipm.expression = value;
96  ipm.target = &target;
97 
98  // Try to evaluate the expression now:
99  // If it does not contain unknown variables, it will remain
100  // constant and will need not updates from "realize()":
101  try
102  {
103  mrpt::expr::CRuntimeCompiledExpression e;
104  e.compile(value);
105  // Yes, it was successful: mark as constant and store value:
106  ipm.is_constant = true;
107  ipm.has_been_evaluated = true;
108  // Store result:
109  target = static_cast<T>(e.eval());
110  ipm.compiled = std::move(e);
111  }
112  catch (const std::exception&)
113  {
114  // We probably need variables, not defined yet.
115  // Ignore this exception at this moment. Will trigger if
116  // raised again in realize() after defining the variables.
117  }
118 }
119 
121  const std::string& value, double& target)
122 {
123  parseAndDeclareParameter_impl(value, target);
124 }
125 
127  const std::string& value, float& target)
128 {
129  parseAndDeclareParameter_impl(value, target);
130 }
131 
133  const std::string& value, uint32_t& target)
134 {
135  parseAndDeclareParameter_impl(value, target);
136 }
137 
139 {
140  std::stringstream errors;
141  for (auto& p : declParameters_)
142  {
143  if (p.has_been_evaluated) continue;
144  errors << "- '" << p.expression << "'"
145  << "\n";
146  }
147 
148  const auto sErrs = errors.str();
149  if (sErrs.empty()) return; // all is ok.
150 
151  THROW_EXCEPTION_FMT(
152  "The following parameter expressions have not been correctly "
153  "initialized:\n%s",
154  sErrs.c_str());
155 }
156 
158 {
159  for (auto& p : declParameters_)
160  {
161  if (p.is_constant) continue;
162  p.has_been_evaluated = false;
163  }
164 }
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::ParameterSource::printVariableValues
std::string printVariableValues() const
Definition: Parameterizable.cpp:23
mp2p_icp::ParameterSource::attach
void attach(Parameterizable &obj)
Definition: Parameterizable.cpp:15
s
XmlRpcServer s
mp2p_icp::internal::InfoPerParam::expression
std::string expression
Definition: Parameterizable.h:24
mp2p_icp::Parameterizable::declaredParameters
auto & declaredParameters()
Definition: Parameterizable.h:98
mp2p_icp::internal::InfoPerParam
Definition: Parameterizable.h:22
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::Parameterizable
Definition: Parameterizable.h:85
mp2p_icp::ParameterSource::attachedDeclParameters_
std::set< internal::InfoPerParam * > attachedDeclParameters_
Definition: Parameterizable.h:77
mp2p_icp::internal::InfoPerParam::compiled
std::optional< mrpt::expr::CRuntimeCompiledExpression > compiled
Compiled expression.
Definition: Parameterizable.h:26
Parameterizable.h
mp2p_icp::ParameterSource::variables_
std::map< std::string, double > variables_
Definition: Parameterizable.h:76
mp2p_icp::Parameterizable::parseAndDeclareParameter
void parseAndDeclareParameter(const std::string &value, double &target)
Definition: Parameterizable.cpp:120
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::internal::InfoPerParam::is_constant
bool is_constant
Definition: Parameterizable.h:28


mp2p_icp
Author(s): Jose-Luis Blanco-Claraco
autogenerated on Tue Jul 2 2024 02:47:25