FOUND
Loading...
Searching...
No Matches
converters.hpp
Go to the documentation of this file.
1#ifndef CONVERTERS_H
2#define CONVERTERS_H
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
14#include "common/style.hpp"
15#include "common/decimal.hpp"
16#include "datafile/datafile.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 // CONVERTERS_H
An EulerAngle is a mutable Object representing Euler Angles of a 3D point.
Definition attitude-utils.hpp:165
decimal z
The z coordinate.
Definition attitude-utils.hpp:54
decimal y
The y coordinate.
Definition attitude-utils.hpp:52
decimal x
The x coordinate.
Definition attitude-utils.hpp:50
Declares data structures for serialized spatial data files, including headers, location records,...
double decimal
Definition decimal.hpp:15
#define STR_TO_DECIMAL(x)
Definition decimal.hpp:16
#define LOG_INFO(msg)
Definition logging.hpp:115
Definition calibrate.cpp:7
DataFile strtodf(const std::string &str)
Definition converters.hpp:127
constexpr decimal DegToRad(decimal deg)
Converts an angle in degrees to radians.
Definition attitude-utils.hpp:329
LocationRecords strtolr(const std::string &str)
Converts a string to a vector of location records.
Definition converters.hpp:143
std::vector< LocationRecord > LocationRecords
A collection of Location Records.
Definition style.hpp:105
Image strtoimage(const std::string &str)
Converts a string to an image.
Definition converters.hpp:115
size_t strtosize(const std::string &str)
Definition converters.hpp:42
decimal strtodecimal(const std::string &str)
Converts a string to a decimal.
Definition converters.hpp:56
unsigned char strtouc(const std::string &str)
Converts a string to an unsigned char.
Definition converters.hpp:38
bool strtobool(const std::string &str)
Converts the string to a bool.
Definition converters.hpp:100
DataFile deserializeDataFile(std::istream &stream)
Deserializes a DataFile object from an input stream.
Definition serialization.cpp:263
EulerAngles strtoea(const std::string &str)
Converts a string to euler angles.
Definition converters.hpp:71
uint32_t num_positions
Number of position entries (Vec3 elements) in the file.
Definition datafile.hpp:48
Represents a complete serialized data file.
Definition datafile.hpp:61
std::unique_ptr< LocationRecord[]> positions
Collection of location records in the file.
Definition datafile.hpp:75
DataFileHeader header
Metadata header for the file (includes magic, version, and CRC).
Definition datafile.hpp:65
Represents an image.
Definition style.hpp:34
unsigned char * image
The image contents.
Definition style.hpp:49
int channels
The image channels.
Definition style.hpp:40
int height
The image height.
Definition style.hpp:38
int width
The image width.
Definition style.hpp:36
Represents a single spatial data point with position and timestamp.
Definition style.hpp:91
Vec3 position
3D position of the recorded data point.
Definition style.hpp:100
uint64_t timestamp
Timestamp associated with the position, in microseconds or appropriate units.
Definition style.hpp:95