CalibrationYaml.hh
Go to the documentation of this file.
00001 
00035 #ifndef CALIBRATION_YAML_HH
00036 #define CALIBRATION_YAML_HH
00037 
00038 #include <stdint.h>
00039 #include <iostream>
00040 #include <string>
00041 #include <vector>
00042 
00043 template<typename T>
00044 std::ostream& writeMatrix (std::ostream& stream, std::string const& name, uint32_t rows, uint32_t columns, T const* data)
00045 {
00046     stream << name << ": !!opencv-matrix\n";
00047     stream << "   rows: " << rows << "\n";
00048     stream << "   cols: " << columns << "\n";
00049     stream << "   dt: d\n";
00050     stream << "   data: [ ";
00051 
00052         stream.precision (17);
00053         stream << std::scientific;
00054         for (uint32_t i = 0; i < rows; i++)
00055     {
00056         if (i != 0)
00057         {
00058             stream << ",\n";
00059             stream << "       ";
00060         }
00061         for (uint32_t j = 0; j < columns; j++)
00062         {
00063             if (j != 0)
00064             {
00065                 stream << ", ";
00066             }
00067             stream << data[i * columns + j];
00068         }
00069     }
00070     stream << " ]\n";
00071     return stream;
00072 }
00073 
00074 class Expect
00075 {
00076 private:
00077     std::string m_value;
00078 
00079 public:
00080     Expect (std::string const& value) :
00081         m_value (value)
00082     {
00083     }
00084 
00085     std::string const& value () const
00086     {
00087         return this->m_value;
00088     }
00089 };
00090 
00091 std::istream& operator >> (std::istream& stream, Expect const& expect)
00092 {
00093     stream >> std::ws;
00094 
00095     for (std::string::const_iterator iter = expect.value ().begin (); iter != expect.value ().end (); ++iter)
00096     {
00097         if (*iter == ' ')
00098         {
00099             stream >> std::ws;
00100             continue;
00101         }
00102         if (stream.get () != *iter)
00103         {
00104             stream.clear (std::ios_base::failbit);
00105             break;
00106         }
00107     }
00108 
00109     return stream;
00110 }
00111 
00112 template<typename T>
00113 std::istream& operator >> (std::istream& stream, std::vector<T>& data)
00114 {
00115     char input;
00116     while (stream.good ())
00117     {
00118         input = 0;
00119         stream >> input;
00120         if (input == '[')
00121         {
00122             stream >> data;
00123         }
00124         else
00125         {
00126             stream.putback (input);
00127 
00128             T value;
00129             stream >> value;
00130             data.push_back (value);
00131         }
00132 
00133         input = 0;
00134         stream >> input;
00135         if (input == ']')
00136         {
00137             break;
00138         }
00139         else if (input != ',')
00140         {
00141             stream.clear (std::ios_base::failbit);
00142             break;
00143         }
00144     }
00145 
00146     return stream;
00147 }
00148 
00149 std::istream& parseYaml (std::istream& stream, std::map<std::string, std::vector<float> >& data)
00150 {
00151     char input;
00152     while (stream.good ())
00153     {
00154         input = 0;
00155         stream >> input;
00156         if (input == '%')
00157         {
00158             std::string comment;
00159             std::getline (stream, comment);
00160             continue;
00161         }
00162         stream.putback (input);
00163 
00164         std::string name;
00165         stream >> name;
00166         if (name.empty ())
00167         {
00168             break;
00169         }
00170         if (name[name.size () - 1] != ':')
00171         {
00172             stream.clear (std::ios_base::failbit);
00173             break;
00174         }
00175         name.resize (name.size () - 1);
00176 
00177         std::vector<float> arrayContents;
00178         arrayContents.clear ();
00179 
00180         input = 0;
00181         stream >> input;
00182         if (input == '[')
00183         {
00184             stream >> arrayContents;
00185         }
00186         else
00187         {
00188             stream.putback (input);
00189 
00190             uint32_t rows = 0;
00191             uint32_t columns = 0;
00192             stream >> Expect ("!!opencv-matrix");
00193             stream >> Expect ("rows:") >> rows;
00194             stream >> Expect ("cols:") >> columns;
00195             stream >> Expect ("dt: d");
00196             stream >> Expect ("data: [") >> arrayContents;
00197         }
00198 
00199                 if (stream.good())
00200                 {
00201                         data.insert (std::make_pair(name, arrayContents));
00202                 }
00203                 else
00204                 {
00205                         fprintf (stderr, "Error parsing data near array \"%s\"", name.c_str());
00206                 }
00207     }
00208 
00209     return stream;
00210 }
00211 
00212 #endif //CALIBRATION_YAML_HH
00213 


multisense_lib
Author(s):
autogenerated on Thu Aug 27 2015 14:01:11