dataset_reader.h
Go to the documentation of this file.
1 /*
2  * OpenVINS: An Open Platform for Visual-Inertial Research
3  * Copyright (C) 2018-2023 Patrick Geneva
4  * Copyright (C) 2018-2023 Guoquan Huang
5  * Copyright (C) 2018-2023 OpenVINS Contributors
6  * Copyright (C) 2018-2019 Kevin Eckenhoff
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef OV_CORE_DATASET_READER_H
23 #define OV_CORE_DATASET_READER_H
24 
25 #include <Eigen/Eigen>
26 #include <fstream>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 
31 #include "colors.h"
32 #include "print.h"
33 
34 using namespace std;
35 
36 namespace ov_core {
37 
51 
52 public:
62  static void load_gt_file(std::string path, std::map<double, Eigen::Matrix<double, 17, 1>> &gt_states) {
63 
64  // Clear any old data
65  gt_states.clear();
66 
67  // Open the file
68  std::ifstream file;
69  std::string line;
70  file.open(path);
71 
72  // Check that it was successfull
73  if (!file) {
74  PRINT_ERROR(RED "ERROR: Unable to open groundtruth file...\n" RESET);
75  PRINT_ERROR(RED "ERROR: %s\n" RESET, path.c_str());
76  std::exit(EXIT_FAILURE);
77  }
78 
79  // Skip the first line as it is just the header
80  std::getline(file, line);
81 
82  // Loop through each line in the file
83  while (std::getline(file, line)) {
84  // Loop variables
85  int i = 0;
86  std::istringstream s(line);
87  std::string field;
88  Eigen::Matrix<double, 17, 1> temp = Eigen::Matrix<double, 17, 1>::Zero();
89  // Loop through this line
90  while (getline(s, field, ',')) {
91  // Ensure we are in the range
92  if (i > 16) {
93  PRINT_ERROR(RED "ERROR: Invalid groudtruth line, too long!\n" RESET);
94  PRINT_ERROR(RED "ERROR: %s\n" RESET, line.c_str());
95  std::exit(EXIT_FAILURE);
96  }
97  // Save our groundtruth state value
98  temp(i, 0) = std::atof(field.c_str());
99  i++;
100  }
101  // Append to our groundtruth map
102  gt_states.insert({1e-9 * temp(0, 0), temp});
103  }
104  file.close();
105  }
106 
114  static bool get_gt_state(double timestep, Eigen::Matrix<double, 17, 1> &imustate,
115  std::map<double, Eigen::Matrix<double, 17, 1>> &gt_states) {
116 
117  // Check that we even have groundtruth loaded
118  if (gt_states.empty()) {
119  PRINT_ERROR(RED "Groundtruth data loaded is empty, make sure you call load before asking for a state.\n" RESET);
120  return false;
121  }
122 
123  // Loop through gt states and find the closest time stamp
124  double closest_time = INFINITY;
125  auto it0 = gt_states.begin();
126  while (it0 != gt_states.end()) {
127  if (std::abs(it0->first - timestep) < std::abs(closest_time - timestep)) {
128  closest_time = it0->first;
129  }
130  it0++;
131  }
132 
133  // If close to this timestamp, then use it
134  if (std::abs(closest_time - timestep) < 0.10) {
135  // PRINT_DEBUG("init DT = %.4f\n", std::abs(closest_time-timestep));
136  // PRINT_DEBUG("timestamp = %.15f\n", closest_time);
137  timestep = closest_time;
138  }
139 
140  // Check that we have the timestamp in our GT file
141  if (gt_states.find(timestep) == gt_states.end()) {
142  PRINT_WARNING(YELLOW "Unable to find %.6f timestamp in GT file, wrong GT file loaded???\n" RESET, timestep);
143  return false;
144  }
145 
146  // Get the GT state vector
147  Eigen::Matrix<double, 17, 1> state = gt_states[timestep];
148 
149  // Our "fixed" state vector from the ETH GT format [q,p,v,bg,ba]
150  imustate(0, 0) = timestep; // time
151  imustate(1, 0) = state(5, 0); // quat
152  imustate(2, 0) = state(6, 0);
153  imustate(3, 0) = state(7, 0);
154  imustate(4, 0) = state(4, 0);
155  imustate(5, 0) = state(1, 0); // pos
156  imustate(6, 0) = state(2, 0);
157  imustate(7, 0) = state(3, 0);
158  imustate(8, 0) = state(8, 0); // vel
159  imustate(9, 0) = state(9, 0);
160  imustate(10, 0) = state(10, 0);
161  imustate(11, 0) = state(11, 0); // bg
162  imustate(12, 0) = state(12, 0);
163  imustate(13, 0) = state(13, 0);
164  imustate(14, 0) = state(14, 0); // ba
165  imustate(15, 0) = state(15, 0);
166  imustate(16, 0) = state(16, 0);
167 
168  // Success!
169  return true;
170  }
171 
177  static void load_simulated_trajectory(std::string path, std::vector<Eigen::VectorXd> &traj_data) {
178 
179  // Try to open our groundtruth file
180  std::ifstream file;
181  file.open(path);
182  if (!file) {
183  PRINT_ERROR(RED "ERROR: Unable to open simulation trajectory file...\n" RESET);
184  PRINT_ERROR(RED "ERROR: %s\n" RESET, path.c_str());
185  std::exit(EXIT_FAILURE);
186  }
187 
188  // Debug print
189  std::string base_filename = path.substr(path.find_last_of("/\\") + 1);
190  PRINT_DEBUG("loaded trajectory %s\n", base_filename.c_str());
191 
192  // Loop through each line of this file
193  std::string current_line;
194  while (std::getline(file, current_line)) {
195 
196  // Skip if we start with a comment
197  if (!current_line.find("#"))
198  continue;
199 
200  // Loop variables
201  int i = 0;
202  std::istringstream s(current_line);
203  std::string field;
204  Eigen::Matrix<double, 8, 1> data;
205 
206  // Loop through this line (timestamp(s) tx ty tz qx qy qz qw)
207  while (std::getline(s, field, ' ')) {
208  // Skip if empty
209  if (field.empty() || i >= data.rows())
210  continue;
211  // save the data to our vector
212  data(i) = std::atof(field.c_str());
213  i++;
214  }
215 
216  // Only a valid line if we have all the parameters
217  if (i > 7) {
218  traj_data.push_back(data);
219 
220  // std::stringstream ss;
221  // ss << std::setprecision(15) << data.transpose() << std::endl;
222  // PRINT_DEBUG(ss.str().c_str());
223  }
224  }
225 
226  // Finally close the file
227  file.close();
228 
229  // Error if we don't have any data
230  if (traj_data.empty()) {
231  PRINT_ERROR(RED "ERROR: Could not parse any data from the file!!\n" RESET);
232  PRINT_ERROR(RED "ERROR: %s\n" RESET, path.c_str());
233  std::exit(EXIT_FAILURE);
234  }
235  }
236 
237 private:
243 };
244 
245 } // namespace ov_core
246 
247 #endif /* OV_CORE_DATASET_READER_H */
ov_core::DatasetReader::load_gt_file
static void load_gt_file(std::string path, std::map< double, Eigen::Matrix< double, 17, 1 >> &gt_states)
Load a ASL format groundtruth file.
Definition: dataset_reader.h:62
ov_core::DatasetReader::load_simulated_trajectory
static void load_simulated_trajectory(std::string path, std::vector< Eigen::VectorXd > &traj_data)
This will load the trajectory into memory (space separated)
Definition: dataset_reader.h:177
s
XmlRpcServer s
PRINT_DEBUG
#define PRINT_DEBUG(x...)
Definition: print.h:97
ov_core::DatasetReader::DatasetReader
DatasetReader()
Definition: dataset_reader.h:242
PRINT_ERROR
#define PRINT_ERROR(x...)
Definition: print.h:100
ov_core::DatasetReader::get_gt_state
static bool get_gt_state(double timestep, Eigen::Matrix< double, 17, 1 > &imustate, std::map< double, Eigen::Matrix< double, 17, 1 >> &gt_states)
Gets the 17x1 groundtruth state at a given timestep.
Definition: dataset_reader.h:114
print.h
RED
#define RED
Definition: colors.h:27
colors.h
std
RESET
#define RESET
Definition: colors.h:25
ov_core
Core algorithms for OpenVINS.
Definition: CamBase.h:30
YELLOW
#define YELLOW
Definition: colors.h:29
PRINT_WARNING
#define PRINT_WARNING(x...)
Definition: print.h:99
ov_core::DatasetReader
Helper functions to read in dataset files.
Definition: dataset_reader.h:50


ov_core
Author(s): Patrick Geneva , Kevin Eckenhoff , Guoquan Huang
autogenerated on Mon Dec 16 2024 03:06:46