-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonl_reader.cpp
94 lines (88 loc) · 3.52 KB
/
jsonl_reader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "jsonl_reader.hpp"
#include <fstream>
#include <limits>
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
double JsonlReader::getSmallestTimestamp(std::string jsonlFilePath) {
std::ifstream dataFile;
dataFile.open(jsonlFilePath);
if (!dataFile.is_open()) {
assert(false && "JSONL file not found");
}
double t0 = std::numeric_limits<double>::infinity();
std::string line;
while (std::getline(dataFile, line)) {
json j = json::parse(line);
if (j.find("time") != j.end()) {
double t = j["time"].get<double>();
if (t < t0) t0 = t;
}
}
return t0;
}
void JsonlReader::read(std::string jsonlFilePath) {
std::ifstream dataFile;
dataFile.open(jsonlFilePath);
if (!dataFile.is_open()) {
assert(false && "JSONL file not found");
}
double time;
std::string line;
std::array<double, 3> sensorValues;
std::map<int, FrameParameters> frames;
std::vector<FrameParameters> framesVec;
// int framesInd;
while (std::getline(dataFile, line)) {
// std::cout << "parsing " << line << std::endl;
json j = json::parse(line);
if (j.find("sensor") != j.end()) {
time = j["time"].get<double>();
sensorValues = j["sensor"]["values"];
std::string sensorType = j["sensor"]["type"];
if (sensorType == "gyroscope") {
if (onGyroscope) onGyroscope(time, sensorValues[0], sensorValues[1], sensorValues[2]);
} else if (sensorType == "accelerometer") {
if (onAccelerometer) onAccelerometer(time, sensorValues[0], sensorValues[1], sensorValues[2]);
}
} else if (onFrames && j.find("frames") != j.end()) {
frames.clear();
time = j["time"].get<double>();
json jFrames = j["frames"];
for (json::iterator jFrame = jFrames.begin(); jFrame != jFrames.end(); ++jFrame) {
FrameParameters frame = {
/* .time = */ time
};
if (!(*jFrame)["cameraParameters"].is_null()) {
#define X(FIELD) \
if (!(*jFrame)["cameraParameters"][#FIELD].is_null()) { \
frame.FIELD = (*jFrame)["cameraParameters"][#FIELD].get<double>(); \
}
X(focalLengthX)
X(focalLengthY)
X(principalPointX)
X(principalPointY)
#undef X
bool hasDirFocal = frame.focalLengthX > 0.0 && frame.focalLengthY > 0.0;
if (!hasDirFocal && !(*jFrame)["cameraParameters"]["focalLength"].is_null()) {
double focalLength = (*jFrame)["cameraParameters"]["focalLength"].get<double>();
frame.focalLengthX = focalLength;
frame.focalLengthY = focalLength;
}
}
int cameraInd = (*jFrame)["cameraInd"].get<int>();
// Use map to allow any order of cameraInds in the JSON array.
frames.insert({cameraInd, frame});
}
if (!frames.empty()) {
size_t n = frames.size();
framesVec.clear();
// Assumes the keys of `this->frames` are successive without gaps and start from zero.
for (size_t i = 0; i < n; ++i) {
framesVec.push_back(frames.at(i));
}
onFrames(framesVec);
}
}
}
}