PluginEigen.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore::core
19  * @author Jan Issac (jan dot issac at gmx dot de)
20  * @date 2011
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 #include <Eigen/Core>
28 
29 #include <SimoxUtility/json.h>
30 #include <SimoxUtility/meta/eigen/is_eigen_array.h>
31 #include <SimoxUtility/meta/eigen/is_eigen_matrix.h>
32 
34 
35 //DefaultAsStringPlugin
37 {
38  template <class T>
39  struct DefaultAsStringPlugin<T, std::enable_if_t<simox::meta::is_eigen_array_v<T>>> :
40  std::true_type
41  {
42  static std::string
44  {
45  nlohmann::json j = pd.getDefaultValue();
46  return j.dump();
47  }
48  };
49 
50  template <class T>
51  struct DefaultAsStringPlugin<T, std::enable_if_t<simox::meta::is_eigen_matrix_v<T>>> :
52  std::true_type
53  {
54  static std::string
56  {
57  using traits = simox::meta::is_eigen_matrix<T>;
58  using eigen_t = typename traits::eigen_t;
59  using scalar_t = typename traits::scalar_t;
60  nlohmann::json j;
61 
62  // If row or column vector, make a coefficient vector and dump this as JSON.
63  if constexpr (traits::cols == 1 or traits::rows == 1)
64  {
65  eigen_t m = pd.getDefaultValue();
66  std::vector<scalar_t> coefs;
67  for (unsigned int r = 0; r < traits::rows; ++r)
68  {
69  for (unsigned int c = 0; c < traits::cols; ++c)
70  {
71  coefs.push_back(m(r, c));
72  }
73  }
74  j = coefs;
75  }
76  // Else to_json Eigen overload.
77  else
78  {
79  j = pd.getDefaultValue();
80  }
81  return j.dump();
82  }
83  };
84 } // namespace armarx::meta::properties
85 
86 //PDInitHookPlugin
88 {
89  template <class T>
90  struct PDInitHookPlugin<T, std::enable_if_t<simox::meta::is_eigen_array_v<T>>> : std::true_type
91  {
92  static void
94  {
95  using traits = simox::meta::is_eigen_array<T>;
96  using eigen_t = typename traits::eigen_t;
97 
98  const static auto parser = [&](const std::string& input) -> eigen_t
99  { return nlohmann::json::parse(input); };
100 
101  pd.setFactory(parser);
102  }
103  };
104 
105  template <class T>
106  struct PDInitHookPlugin<T, std::enable_if_t<simox::meta::is_eigen_matrix_v<T>>> : std::true_type
107  {
108  static void
110  {
111  using traits = simox::meta::is_eigen_matrix<T>;
112  using eigen_t = typename traits::eigen_t;
113  using scalar_t = typename traits::scalar_t;
114 
115  const static auto parser = [&](const std::string& input) -> eigen_t
116  {
117  // Use from_json Eigen overload (no row/col vector).
118  if constexpr (!(traits::cols == 1 or traits::rows == 1))
119  {
120  return nlohmann::json::parse(input);
121  }
122 
123  // If row or column vector, init Eigen vector from coefficient vector.
124  eigen_t m = eigen_t::Zero();
125  const std::vector<scalar_t> coefs = nlohmann::json::parse(input);
126 
127  if (static_cast<long int>(coefs.size()) != m.size())
128  {
130  throw InvalidPropertyValueException("unknown", input)
131  << "Property must be a vector with " << m.size() << " elements.";
132  }
133 
134  unsigned int r = 0;
135  unsigned int c = 0;
136  for (unsigned int i = 0; i < coefs.size(); ++i)
137  {
138  m(r, c) = coefs.at(i);
139  // Only increment r if column vector, or c if row vector.
140  if constexpr (traits::cols == 1)
141  {
142  ++r;
143  }
144  else
145  {
146  ++c;
147  }
148  }
149  return m;
150  };
151 
152  pd.setFactory(parser);
153  }
154  };
155 } // namespace armarx::meta::properties
armarx::meta::properties::DefaultAsStringPlugin< T, std::enable_if_t< simox::meta::is_eigen_matrix_v< T > > >::Str
static std::string Str(PropertyDefinition< T > &pd)
Definition: PluginEigen.h:55
armarx::meta::properties
Definition: PluginCfgStruct.h:43
armarx::meta::properties::DefaultAsStringPlugin< T, std::enable_if_t< simox::meta::is_eigen_array_v< T > > >::Str
static std::string Str(PropertyDefinition< T > &pd)
Definition: PluginEigen.h:43
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::meta::properties::PDInitHookPlugin
Definition: PropertyDefinition.h:74
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:12
armarx::PropertyDefinition::getDefaultValue
PropertyType getDefaultValue()
Definition: PropertyDefinition.hpp:302
IceStorm::parser
Parser * parser
Definition: Parser.cpp:34
armarx::exceptions::local::InvalidPropertyValueException
This exception is thrown if an invalid value was specified for a property.
Definition: InvalidPropertyValueException.h:38
std
Definition: Application.h:66
armarx::meta::properties::DefaultAsStringPlugin
Definition: PropertyDefinition.h:69
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
armarx::meta::properties::PDInitHookPlugin< T, std::enable_if_t< simox::meta::is_eigen_array_v< T > > >::Init
static void Init(PropertyDefinition< T > &pd)
Definition: PluginEigen.h:93
armarx::PropertyDefinition::setFactory
PropertyDefinition< PropertyType > & setFactory(const PropertyFactoryFunction &func)
Sets the factory function that creates the specified template type from the actual string value.
Definition: PropertyDefinition.hpp:135
armarx::PropertyDefinition
PropertyDefinition defines a property that will be available within the PropertyUser.
Definition: PropertyDefinition.h:117
armarx::meta::properties::PDInitHookPlugin< T, std::enable_if_t< simox::meta::is_eigen_matrix_v< T > > >::Init
static void Init(PropertyDefinition< T > &pd)
Definition: PluginEigen.h:109
PropertyDefinition.h