CalibrationYaml.hh
Go to the documentation of this file.
1 
35 #ifndef CALIBRATION_YAML_HH
36 #define CALIBRATION_YAML_HH
37 
38 #include <stdint.h>
39 #include <iostream>
40 #include <string>
41 #include <vector>
42 
43 template<typename T>
44 std::ostream& writeMatrix (std::ostream& stream, std::string const& name, uint32_t rows, uint32_t columns, T const* data)
45 {
46  stream << name << ": !!opencv-matrix\n";
47  stream << " rows: " << rows << "\n";
48  stream << " cols: " << columns << "\n";
49  stream << " dt: d\n";
50  stream << " data: [ ";
51 
52  stream.precision (17);
53  stream << std::scientific;
54  for (uint32_t i = 0; i < rows; i++)
55  {
56  if (i != 0)
57  {
58  stream << ",\n";
59  stream << " ";
60  }
61  for (uint32_t j = 0; j < columns; j++)
62  {
63  if (j != 0)
64  {
65  stream << ", ";
66  }
67  stream << data[i * columns + j];
68  }
69  }
70  stream << " ]\n";
71  return stream;
72 }
73 
74 class Expect
75 {
76 private:
77  std::string m_value;
78 
79 public:
80  Expect (std::string const& value) :
81  m_value (value)
82  {
83  }
84 
85  std::string const& value () const
86  {
87  return this->m_value;
88  }
89 };
90 
91 std::istream& operator >> (std::istream& stream, Expect const& expect)
92 {
93  stream >> std::ws;
94 
95  for (std::string::const_iterator iter = expect.value ().begin (); iter != expect.value ().end (); ++iter)
96  {
97  if (*iter == ' ')
98  {
99  stream >> std::ws;
100  continue;
101  }
102  if (stream.get () != *iter)
103  {
104  stream.clear (std::ios_base::failbit);
105  break;
106  }
107  }
108 
109  return stream;
110 }
111 
112 template<typename T>
113 std::istream& operator >> (std::istream& stream, std::vector<T>& data)
114 {
115  char input;
116  while (stream.good ())
117  {
118  input = 0;
119  stream >> input;
120  if (input == '[')
121  {
122  stream >> data;
123  }
124  else
125  {
126  stream.putback (input);
127 
128  T value;
129  stream >> value;
130  data.push_back (value);
131  }
132 
133  input = 0;
134  stream >> input;
135  if (input == ']')
136  {
137  break;
138  }
139  else if (input != ',')
140  {
141  stream.clear (std::ios_base::failbit);
142  break;
143  }
144  }
145 
146  return stream;
147 }
148 
149 std::istream& parseYaml (std::istream& stream, std::map<std::string, std::vector<float> >& data)
150 {
151  char input;
152  while (stream.good ())
153  {
154  input = 0;
155  stream >> input;
156  if (input == '%')
157  {
158  std::string comment;
159  std::getline (stream, comment);
160  continue;
161  }
162  stream.putback (input);
163 
164  std::string name;
165  stream >> name;
166  if (name.empty ())
167  {
168  break;
169  }
170  if (name[name.size () - 1] != ':')
171  {
172  stream.clear (std::ios_base::failbit);
173  break;
174  }
175  name.resize (name.size () - 1);
176 
177  std::vector<float> arrayContents;
178  arrayContents.clear ();
179 
180  input = 0;
181  stream >> input;
182  if (input == '[')
183  {
184  stream >> arrayContents;
185  }
186  else
187  {
188  stream.putback (input);
189 
190  uint32_t rows = 0;
191  uint32_t columns = 0;
192  stream >> Expect ("!!opencv-matrix");
193  stream >> Expect ("rows:") >> rows;
194  stream >> Expect ("cols:") >> columns;
195  stream >> Expect ("dt: d");
196  stream >> Expect ("data: [") >> arrayContents;
197  }
198 
199  if (stream.good())
200  {
201  data.insert (std::make_pair(name, arrayContents));
202  }
203  else
204  {
205  fprintf (stderr, "Error parsing data near array \"%s\"", name.c_str());
206  }
207  }
208 
209  return stream;
210 }
211 
212 #endif //CALIBRATION_YAML_HH
213 
std::istream & operator>>(std::istream &stream, Expect const &expect)
std::ostream & writeMatrix(std::ostream &stream, std::string const &name, uint32_t rows, uint32_t columns, T const *data)
std::string m_value
std::string const & value() const
std::istream & parseYaml(std::istream &stream, std::map< std::string, std::vector< float > > &data)
Expect(std::string const &value)


multisense_lib
Author(s):
autogenerated on Sat Apr 6 2019 02:16:46