Choreonoid  1.1
EigenYaml.h
説明を見る。
1 
5 #ifndef CNOID_UTIL_YAML_EIGEN_H_INCLUDED
6 #define CNOID_UTIL_YAML_EIGEN_H_INCLUDED
7 
8 #include "YamlNodes.h"
9 #include <Eigen/Core>
10 
11 namespace cnoid {
12 
13  template<typename Derived>
14  bool read(const YamlMapping& mapping, const std::string& key, Eigen::MatrixBase<Derived>& x)
15  {
16  const YamlSequence& s = *mapping.findSequence(key);
17  if(s.isValid()){
18  const int nr = x.rows();
19  const int nc = x.cols();
20  const int n = s.size();
21  int index = 0;
22  if(n > 0){
23  for(int i=0; i < nr; ++i){
24  for(int j=0; j < nc; ++j){
25  x(i, j) = s[index++].toDouble();
26  if(index == n){
27  break;
28  }
29  }
30  }
31  }
32  return (index == nr * nc);
33  }
34  return false;
35  }
36 
37  template<typename Derived>
38  inline void readEx(const YamlMapping& mapping, const std::string& key, Eigen::MatrixBase<Derived>& x)
39  {
40  if(!read(mapping, key, x)){
41  mapping.throwKeyNotFoundException(key);
42  }
43  }
44 
45  template<typename Derived>
46  YamlSequence& write(YamlMapping& mapping, const std::string& key, const Eigen::MatrixBase<Derived>& x)
47  {
48  YamlSequence& s = *mapping.createFlowStyleSequence(key);
49  const int nr = x.rows();
50  const int nc = x.cols();
51  if(nc == 1){
52  for(int i=0; i < nr; ++i){
53  s.append(x(i));
54  }
55  } else {
56  for(int i=0; i < nr; ++i){
57  s.appendLF();
58  for(int j=0; j < nc; ++j){
59  s.append(x(i, j));
60  }
61  }
62  }
63  return s;
64  }
65 
66 }
67 
68 #endif