Choreonoid  1.8
EigenArchive.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_UTIL_EIGEN_ARCHIVE_H
6 #define CNOID_UTIL_EIGEN_ARCHIVE_H
7 
8 #include "ValueTree.h"
9 #include "EigenUtil.h"
10 #include <fmt/format.h>
11 #include <functional>
12 #include <initializer_list>
13 #include "exportdecl.h"
14 
15 namespace cnoid {
16 
17 template<typename Derived>
18 void readEx(const Listing* listing, Eigen::MatrixBase<Derived>& x)
19 {
20  const int nr = x.rows();
21  const int nc = x.cols();
22  if(listing->size() != nr * nc){
23  listing->throwException(
24  fmt::format("A {0} x {1} matrix / vector value is expected", nr, nc));
25  }
26  int index = 0;
27  for(int i=0; i < nr; ++i){
28  for(int j=0; j < nc; ++j){
29  x(i, j) = (*listing)[index++].toDouble();
30  }
31  }
32 }
33 
34 
35 template<typename Derived>
36 void readEx(const Listing& listing, Eigen::MatrixBase<Derived>& x)
37 {
38  readEx(&listing, x);
39 }
40 
41 
42 template<typename Derived>
43 bool read(const Mapping* mapping, const std::string& key, Eigen::MatrixBase<Derived>& x)
44 {
45  auto s = mapping->findListing(key);
46  if(!s->isValid()){
47  return false;
48  }
49  readEx(s, x);
50  return true;
51 }
52 
53 
54 template<typename Derived>
55 bool read(const Mapping* mapping, std::initializer_list<const char*> keys, Eigen::MatrixBase<Derived>& x)
56 {
57  for(auto& key : keys){
58  if(read(mapping, key, x)){
59  return true;
60  }
61  }
62  return false;
63 }
64 
65 
66 template<typename Derived>
67 bool read(const Mapping& mapping, const std::string& key, Eigen::MatrixBase<Derived>& x)
68 {
69  return read(&mapping, key, x);
70 }
71 
72 
73 template<typename Derived>
74 bool read(const Mapping& mapping, std::initializer_list<const char*> keys, Eigen::MatrixBase<Derived>& x)
75 {
76  return read(&mapping, keys, x);
77 }
78 
79 
80 template<typename Scalar, int Dim, int Mode>
81 bool read(const Mapping* mapping, const std::string& key, Eigen::Transform<Scalar, Dim, Mode>& T)
82 {
83  return read(mapping, key, T.matrix());
84 }
85 
86 
87 template<typename Scalar, int Dim, int Mode>
88 bool read(const Mapping* mapping, std::initializer_list<const char*> keys, Eigen::Transform<Scalar, Dim, Mode>& T)
89 {
90  auto M = T.matrix();
91  for(auto& key : keys){
92  if(read(*mapping, key, M)){
93  return true;
94  }
95  }
96  return false;
97 }
98 
99 
100 template<typename Scalar, int Dim, int Mode>
101 bool read(const Mapping& mapping, const std::string& key, Eigen::Transform<Scalar, Dim, Mode>& T)
102 {
103  return read(&mapping, key, T.matrix());
104 }
105 
106 
107 template<typename Scalar, int Dim, int Mode>
108 bool read(const Mapping& mapping, std::initializer_list<const char*> keys, Eigen::Transform<Scalar, Dim, Mode>& T)
109 {
110  return read(&mapping, keys, T.matrix());
111 }
112 
113 
114 template<typename Derived>
115 void readEx(const Mapping* mapping, const std::string& key, Eigen::MatrixBase<Derived>& x)
116 {
117  if(!read(mapping, key, x)){
118  mapping->throwKeyNotFoundException(key);
119  }
120 }
121 
122 
123 template<typename Derived>
124 void readEx(const Mapping* mapping, std::initializer_list<const char*> keys, Eigen::MatrixBase<Derived>& x)
125 {
126  for(auto& key : keys){
127  if(read(mapping, key, x)){
128  return;
129  }
130  }
131  if(keys.begin() != keys.end()){
132  mapping->throwKeyNotFoundException(*keys.begin());
133  }
134 }
135 
136 
137 template<typename Derived>
138 void readEx(const Mapping& mapping, const std::string& key, Eigen::MatrixBase<Derived>& x)
139 {
140  readEx(&mapping, key, x);
141 }
142 
143 
144 template<typename Derived>
145 Listing* write(Mapping* mapping, const std::string& key, const Eigen::MatrixBase<Derived>& x)
146 {
147  auto s = mapping->createFlowStyleListing(key);
148  const int nr = x.rows();
149  const int nc = x.cols();
150  if(nc == 1){
151  for(int i=0; i < nr; ++i){
152  s->append(x(i));
153  }
154  } else {
155  for(int i=0; i < nr; ++i){
156  s->appendLF();
157  for(int j=0; j < nc; ++j){
158  s->append(x(i, j));
159  }
160  }
161  }
162  return s;
163 }
164 
165 
166 template<typename Derived>
167 Listing& write(Mapping& mapping, const std::string& key, const Eigen::MatrixBase<Derived>& x)
168 {
169  return *write(&mapping, key, x);
170 }
171 
172 
173 template<typename Scalar, int Dim, int Mode>
174 Listing* write(Mapping* mapping, const std::string& key, const Eigen::Transform<Scalar, Dim, Mode>& T)
175 {
176  return write(mapping, key, T.matrix());
177 }
178 
179 
180 template<typename Scalar, int Dim, int Mode>
181 Listing& write(Mapping& mapping, const std::string& key, const Eigen::Transform<Scalar, Dim, Mode>& T)
182 {
183  return *write(&mapping, key, T.matrix());
184 }
185 
186 /*
187  The following reading functions read angle values written in degrees unless isForcedRadianMode of
188  the container list is true.
189 */
190 CNOID_EXPORT bool readAngleAxis(const Mapping* mapping, const std::string& key, Eigen::AngleAxisd& aa);
191 CNOID_EXPORT bool readAngleAxis(const Mapping* mapping, std::initializer_list<const char*> keys, Eigen::AngleAxisd& aa);
192 CNOID_EXPORT bool readAngleAxis(const Mapping& mapping, const std::string& key, Eigen::AngleAxisd& aa);
193 CNOID_EXPORT bool readDegreeAngleAxis(const Mapping& mapping, const std::string& key, Eigen::AngleAxisd& aa);
194 CNOID_EXPORT bool readRadianAngleAxis(const Mapping& mapping, const std::string& key, Eigen::AngleAxisd& aa);
195 
196 CNOID_EXPORT Listing* writeDegreeAngleAxis(Mapping* mapping, const std::string& key, const Eigen::AngleAxisd& aa);
197 CNOID_EXPORT Listing& writeDegreeAngleAxis(Mapping& mapping, const std::string& key, const Eigen::AngleAxisd& aa);
198 CNOID_EXPORT Listing* writeRadianAngleAxis_(Mapping* mapping, const std::string& key, const Eigen::AngleAxisd& aa);
199 
200 CNOID_EXPORT bool read(
201  const Mapping& mapping, const std::string& key, std::function<void(const Eigen::Vector3d& value)> setterFunc);
202 
203 [[deprecated]]
204 CNOID_EXPORT Listing* writeAngleAxis(Mapping* mapping, const std::string& key, const Eigen::AngleAxisd& aa);
205 [[deprecated]]
206 CNOID_EXPORT Listing& writeAngleAxis(Mapping& mapping, const std::string& key, const Eigen::AngleAxisd& aa);
207 
208 [[deprecated("Use readAngleAxis")]]
209 CNOID_EXPORT bool read(const Mapping& mapping, const std::string& key, Eigen::AngleAxisd& aa);
210 [[deprecated("Use writeDegreeAngleAxis")]]
211 CNOID_EXPORT Listing& write(Mapping& mapping, const std::string& key, const Eigen::AngleAxisd& aa);
212 
213 }
214 
215 #endif
cnoid::writeAngleAxis
CNOID_EXPORT Listing * writeAngleAxis(Mapping *mapping, const std::string &key, const Eigen::AngleAxisd &aa)
Definition: EigenArchive.cpp:115
cnoid::Mapping
Definition: ValueTree.h:253
EigenUtil.h
cnoid::write
Listing * write(Mapping *mapping, const std::string &key, const Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:145
cnoid::writeDegreeAngleAxis
CNOID_EXPORT Listing * writeDegreeAngleAxis(Mapping *mapping, const std::string &key, const Eigen::AngleAxisd &aa)
Definition: EigenArchive.cpp:127
cnoid::ValueNode::throwException
void throwException(const std::string &message) const
Definition: ValueTree.cpp:405
cnoid::readDegreeAngleAxis
CNOID_EXPORT bool readDegreeAngleAxis(const Mapping &mapping, const std::string &key, Eigen::AngleAxisd &aa)
Definition: EigenArchive.cpp:74
cnoid::readRadianAngleAxis
CNOID_EXPORT bool readRadianAngleAxis(const Mapping &mapping, const std::string &key, Eigen::AngleAxisd &aa)
Definition: EigenArchive.cpp:80
cnoid::readEx
void readEx(const Listing *listing, Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:18
cnoid::read
bool read(const Mapping *mapping, const std::string &key, Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:43
ValueTree.h
cnoid::Mapping::createFlowStyleListing
Listing * createFlowStyleListing(const std::string &key)
Definition: ValueTree.h:337
cnoid::writeRadianAngleAxis_
CNOID_EXPORT Listing * writeRadianAngleAxis_(Mapping *mapping, const std::string &key, const Eigen::AngleAxisd &aa)
Definition: EigenArchive.cpp:139
cnoid::Listing::size
int size() const
Definition: ValueTree.h:502
cnoid::Listing
Definition: ValueTree.h:486
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Mapping::findListing
Listing * findListing(const std::string &key) const
Definition: ValueTree.cpp:578
cnoid::Listing::append
void append(ValueNode *node)
Definition: ValueTree.h:551
cnoid::Mapping::throwKeyNotFoundException
void throwKeyNotFoundException(const std::string &key) const
Definition: ValueTree.cpp:679
cnoid::readAngleAxis
CNOID_EXPORT bool readAngleAxis(const Mapping *mapping, const std::string &key, Eigen::AngleAxisd &aa)
Definition: EigenArchive.cpp:51