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


multisense_lib
Author(s):
autogenerated on Thu Apr 17 2025 02:49:08