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/meta/eigen/is_eigen_array.h>
30 #include <SimoxUtility/meta/eigen/is_eigen_matrix.h>
31 #include <SimoxUtility/json.h>
32 
34 
35 //DefaultAsStringPlugin
37 {
38  template<class T>
40  T,
41  std::enable_if_t <simox::meta::is_eigen_array_v<T>>
42  > : std::true_type
43  {
44  static std::string Str(PropertyDefinition<T>& pd)
45  {
46  nlohmann::json j = pd.getDefaultValue();
47  return j.dump();
48  }
49  };
50 
51  template<class T>
53  T,
54  std::enable_if_t <simox::meta::is_eigen_matrix_v<T>>
55  > : std::true_type
56  {
57  static std::string Str(PropertyDefinition<T>& pd)
58  {
59  using traits = simox::meta::is_eigen_matrix<T>;
60  using eigen_t = typename traits::eigen_t;
61  using scalar_t = typename traits::scalar_t;
62  nlohmann::json j;
63 
64  // If row or column vector, make a coefficient vector and dump this as JSON.
65  if constexpr(traits::cols == 1 or traits::rows == 1)
66  {
67  eigen_t m = pd.getDefaultValue();
68  std::vector<scalar_t> coefs;
69  for (unsigned int r = 0; r < traits::rows; ++r)
70  {
71  for (unsigned int c = 0; c < traits::cols; ++c)
72  {
73  coefs.push_back(m(r, c));
74  }
75  }
76  j = coefs;
77  }
78  // Else to_json Eigen overload.
79  else
80  {
81  j = pd.getDefaultValue();
82  }
83  return j.dump();
84  }
85  };
86 }
87 //PDInitHookPlugin
89 {
90  template<class T>
92  T,
93  std::enable_if_t <simox::meta::is_eigen_array_v<T>>
94  > : std::true_type
95  {
96  static void Init(PropertyDefinition<T>& pd)
97  {
98  using traits = simox::meta::is_eigen_array<T>;
99  using eigen_t = typename traits::eigen_t;
100 
101  const static auto parser = [&](const std::string & input) -> eigen_t
102  {
103  return nlohmann::json::parse(input);
104  };
105 
106  pd.setFactory(parser);
107  }
108  };
109 
110  template<class T>
112  T,
113  std::enable_if_t <simox::meta::is_eigen_matrix_v<T>>
114  > : std::true_type
115  {
116  static void Init(PropertyDefinition<T>& pd)
117  {
118  using traits = simox::meta::is_eigen_matrix<T>;
119  using eigen_t = typename traits::eigen_t;
120  using scalar_t = typename traits::scalar_t;
121 
122  const static auto parser = [&](const std::string & input) -> eigen_t
123  {
124  // Use from_json Eigen overload (no row/col vector).
125  if constexpr(!(traits::cols == 1 or traits::rows == 1))
126  {
127  return nlohmann::json::parse(input);
128  }
129 
130  // If row or column vector, init Eigen vector from coefficient vector.
131  eigen_t m = eigen_t::Zero();
132  const std::vector<scalar_t> coefs = nlohmann::json::parse(input);
133 
134  if (static_cast<long int>(coefs.size()) != m.size())
135  {
137  throw InvalidPropertyValueException("unknown", input)
138  << "Property must be a vector with " << m.size() << " elements.";
139  }
140 
141  unsigned int r = 0;
142  unsigned int c = 0;
143  for (unsigned int i = 0; i < coefs.size(); ++i)
144  {
145  m(r, c) = coefs.at(i);
146  // Only increment r if column vector, or c if row vector.
147  if constexpr(traits::cols == 1)
148  {
149  ++r;
150  }
151  else
152  {
153  ++c;
154  }
155  }
156  return m;
157  };
158 
159  pd.setFactory(parser);
160  }
161  };
162 }
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:57
armarx::meta::properties
Definition: PluginCfgStruct.h:42
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:44
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::meta::properties::PDInitHookPlugin
Definition: PropertyDefinition.h:66
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::PropertyDefinition::getDefaultValue
PropertyType getDefaultValue()
Definition: PropertyDefinition.hpp:321
IceStorm::parser
Parser * parser
Definition: Parser.cpp:33
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:63
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
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:96
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:140
armarx::PropertyDefinition
PropertyDefinition defines a property that will be available within the PropertyUser.
Definition: PropertyDefinition.h:107
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:116
PropertyDefinition.h