FOUND
Loading...
Searching...
No Matches
converters.hpp
1#ifndef SRC_PROVIDERS_CONVERTERS_HPP_
2#define SRC_PROVIDERS_CONVERTERS_HPP_
3
4#include <stb_image/stb_image.h>
5
6#include <string>
7#include <memory>
8#include <fstream>
9#include <sstream>
10
11#include "common/logging.hpp"
12
13#include "common/spatial/attitude-utils.hpp"
14#include "common/style.hpp"
15#include "common/decimal.hpp"
16#include "datafile/datafile.hpp"
17#include "datafile/serialization.hpp"
18
19// NOTE: Throwing exceptions is allowed in this file, as these functions
20// must successfully parse data for any pipeline to function properly.
21// If they fail, the pipeline should not be run, and an exception should be thrown.
22// This is preferable than continuing with invalid data and outputting an unintended
23// result.
24
25namespace found {
26
38inline unsigned char strtouc(const std::string &str) {
39 return static_cast<unsigned char>(std::strtoul(str.c_str(), nullptr, 10));
40}
41
42inline size_t strtosize(const std::string &str) {
43 return static_cast<size_t>(atoi(str.c_str()));
44}
45
56inline decimal strtodecimal(const std::string &str) {
57 return STR_TO_DECIMAL(str);
58}
59
71inline EulerAngles strtoea(const std::string &str) {
72 char delimiter = str.find(" ") != std::string::npos ? ' ' : ',';
73 decimal result[3];
74
75 size_t start = 0;
76 size_t end = str.find(delimiter);
77 size_t index = 0;
78
79 while (index != 2 && end != std::string::npos) {
80 result[index++] = strtodecimal(str.substr(start, end - start));
81 start = end + 1;
82 end = str.find(delimiter, start);
83 }
84
85 result[index++] = strtodecimal(str.substr(start));
86
87 while (index != 3) result[index++] = 0;
88
89 return EulerAngles(DegToRad(result[0]), DegToRad(result[1]), DegToRad(result[2]));
90}
91
100inline bool strtobool(const std::string &str) {
101 return str.size() != 0 && str != "0" && str != "false";
102}
103
115inline Image strtoimage(const std::string &str) {
116 Image image;
117 image.image = stbi_load(str.c_str(), &image.width, &image.height, &image.channels, 0);
118 if (!image.image) {
119 throw std::runtime_error("Could not load image " + str + ": " + stbi_failure_reason());
120 }
121 return image;
122}
123
127inline DataFile strtodf(const std::string &str) {
128 std::ifstream stream(str);
129 return deserializeDataFile(stream, str);
130}
131
143inline LocationRecords strtolr(const std::string &str) {
144 if (str.size() >= 6) {
145 if (str.substr(str.size() - 6) == ".found") {
146 LOG_INFO("Getting Position Data from Data File (*.found)");
147 DataFile data = strtodf(str);
148 return LocationRecords(data.positions.get(), data.positions.get() + data.header.num_positions);
149 }
150 }
151
152 LOG_INFO("Getting Position Data from non-Data File (not *.found)");
153 LocationRecords records;
154 std::ifstream file(str);
155 if (!file.is_open()) {
156 throw std::runtime_error("Could not open file " + str);
157 }
158
159 std::string line;
160 while (std::getline(file, line)) {
161 std::istringstream iss(line);
162 LocationRecord record;
163 if (!(iss >> record.timestamp >> record.position.x >> record.position.y >> record.position.z)) {
164 file.close();
165 throw std::runtime_error("Invalid format for file " + str + ": " + line);
166 }
167 records.push_back(record);
168 }
169
170 file.close();
171 return records;
172}
173
174} // namespace found
175
176#endif // SRC_PROVIDERS_CONVERTERS_HPP_
Declares data structures for serialized spatial data files, including headers, location records,...
unsigned char * image
The image contents.
Definition style.hpp:49