OSVR-Core
LoadRows.h
Go to the documentation of this file.
1 
11 // Copyright 2016 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_LoadRows_h_GUID_7FC43F97_8922_448D_7CE8_2D9EAB669BBD
26 #define INCLUDED_LoadRows_h_GUID_7FC43F97_8922_448D_7CE8_2D9EAB669BBD
27 
28 // Internal Includes
29 #include "CSVTools.h"
30 #include "UtilityFunctions.h"
31 #include <ImageProcessing.h>
32 #include <LedMeasurement.h>
33 #include <MakeHDKTrackingSystem.h>
34 
35 #include <osvr/Util/Finally.h>
36 #include <osvr/Util/TimeValue.h>
37 
38 // Library/third-party includes
39 #include <Eigen/Core>
40 #include <Eigen/Geometry>
41 #include <opencv2/core/core.hpp>
42 
43 // Standard includes
44 #include <cstddef>
45 #include <fstream>
46 #include <iostream>
47 #include <memory>
48 #include <string>
49 #include <utility>
50 #include <vector>
51 
52 namespace osvr {
53 namespace vbtracker {
54 
55  static const cv::Size IMAGE_SIZE = {640, 480};
56 
57  inline cv::Mat const &getGray() {
58  static const cv::Mat instance = cv::Mat(IMAGE_SIZE, CV_8UC1);
59  return instance;
60  }
61 
62  inline cv::Mat const &getColor() {
63  static const cv::Mat instance = cv::Mat(IMAGE_SIZE, CV_8UC3);
64  return instance;
65  }
66 
67  class LoadRow {
68  public:
71  : helper_(helper), row_(row) {}
72  ~LoadRow() {
73  if (!measurementPieces_.empty()) {
74  std::cerr << "Leftover measurement pieces on loadrow "
75  "destruction, suggests a parsing error!"
76  << std::endl;
77  }
78  }
79  bool operator()(std::string const &line, std::size_t beginPos,
80  std::size_t endPos) {
81  auto advanceFieldAfterProcessing = util::finally([&] { field_++; });
82  csvtools::StringField strField(line, beginPos, endPos);
83  // std::cout << strField.beginPos() << ":" <<
84  // strField.virtualEndPos() << std::endl;
85  if (field_ < 3) {
86  // refx/y/z
87  bool success = false;
88  double val;
89  std::tie(success, val) = helper_.getFieldAs<double>(strField);
90  if (!success) {
91  return false;
92  }
93  row_.xlate[field_] = val;
94  return true;
95  }
96  if (field_ < 7) {
97  // refqw/qx/qy/qz
98  bool success = false;
99  double val;
100  std::tie(success, val) = helper_.getFieldAs<double>(strField);
101  if (!success) {
102  return false;
103  }
104  switch (field_) {
105  case 3:
106  row_.rot.w() = val;
107  break;
108  case 4:
109  row_.rot.x() = val;
110  break;
111  case 5:
112  row_.rot.y() = val;
113  break;
114  case 6:
115  row_.rot.z() = val;
116  break;
117  }
118  return true;
119  }
120  if (field_ == 7) {
121  // sec
122  auto success = helper_.getField(strField, row_.tv.seconds);
123  return success;
124  }
125  if (field_ == 8) {
126  // usec
127  auto success = helper_.getField(strField, row_.tv.microseconds);
128  if (success) {
129  row_.ok = true;
130  }
131  return success;
132  }
133  // Now, we are looping through x, y, size for every blob.
134 
135  bool success = false;
136  float val;
137  std::tie(success, val) = helper_.getFieldAs<float>(strField);
138  if (!success) {
139  return false;
140  }
141  measurementPieces_.push_back(val);
142  if (measurementPieces_.size() == 3) {
143  // that's a new LED!
144  row_.measurements.emplace_back(
145  measurementPieces_[0], measurementPieces_[1],
146  measurementPieces_[2], IMAGE_SIZE);
147  measurementPieces_.clear();
148  }
149  return true;
150  }
151 
152  private:
155  std::size_t field_ = 0;
156  std::vector<float> measurementPieces_;
157  };
158 
159  inline MeasurementsRows loadData(std::string const &fn) {
160  MeasurementsRows ret;
161  std::ifstream csvFile(fn);
162  if (!csvFile) {
163  std::cerr << "Could not open csvFile " << fn << std::endl;
164  return ret;
165  }
166  {
167  auto headerRow = csvtools::getCleanLine(csvFile);
168  if (headerRow.empty() || !csvFile) {
171  std::cerr << "Header row was empty, that's not a good sign!"
172  << std::endl;
173  return ret;
174  }
175  }
177 
178  std::string dataLine = csvtools::getCleanLine(csvFile);
179  while (csvFile) {
180  TimestampedMeasurementsPtr newRow(new TimestampedMeasurements);
181  csvtools::iterateFields(LoadRow(helper, *newRow), dataLine);
182  // std::cout << "Done with iterate fields" << std::endl;
183  if (newRow->ok) {
184 #if 0
185  std::cout << "Row has " << newRow->measurements.size()
186  << " blobs" << std::endl;
187 #endif
188  ret.emplace_back(std::move(newRow));
189  } else {
190  std::cerr << "Something went wrong parsing that row: "
191  << dataLine << std::endl;
192  }
193 
194  dataLine = csvtools::getCleanLine(csvFile);
195  }
196  std::cout << "Total of " << ret.size() << " rows" << std::endl;
197  return ret;
198  }
199 
200  ImageOutputDataPtr
201  makeImageOutputDataFromRow(TimestampedMeasurements const &row,
202  CameraParameters const &camParams) {
203  ImageOutputDataPtr ret(new ImageProcessingOutput);
204  ret->tv = row.tv;
205  ret->ledMeasurements = row.measurements;
206  ret->camParams = camParams;
207  ret->frame = getColor();
208  ret->frameGray = getGray();
209  return ret;
210  }
211 
212 } // namespace vbtracker
213 } // namespace osvr
214 
215 #endif // INCLUDED_LoadRows_h_GUID_7FC43F97_8922_448D_7CE8_2D9EAB669BBD
Definition: UtilityFunctions.h:48
Header declaring a C++11 finally or "scope-guard" construct.
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
MeasurementsRows loadData(std::string const &fn)
Definition: LoadRows.h:159
Scalar y() const
Definition: Quaternion.h:71
Definition: CameraParameters.h:41
Definition: LoadRows.h:67
Definition: CSVTools.h:98
Scalar x() const
Definition: Quaternion.h:69
Definition: ImageProcessing.h:41
Scalar w() const
Definition: Quaternion.h:75
Header providing a C++ wrapper around TimeValueC.h.
FinalTask< F > finally(F &&f)
Creation free function for final tasks to run on scope exit.
Definition: Finally.h:87
Scalar z() const
Definition: Quaternion.h:73
Definition: CSVTools.h:230
Header.