-
Notifications
You must be signed in to change notification settings - Fork 99
/
jsonMessageSpec.hpp
227 lines (193 loc) · 5.71 KB
/
jsonMessageSpec.hpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#ifndef UNITYMESSAGESPEC_H
#define UNITYMESSAGESPEC_H
/**
* @file jsonMessageSpec.hpp
* @author Winter Guerra
* @brief Defines the json structure of messages going to and from Unity.
*/
// Message/state struct definition
#include <iostream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include "json.hpp"
using json = nlohmann::json;
namespace unity_outgoing
{
struct Camera_t
{
std::string ID;
std::string TF;
// Position and rotation use Unity left-handed coordinates.
// Z North, X East, Y up.
// E.G. East, Up, North.
std::vector<double> position;
std::vector<double> rotation;
// Metadata
// enum CameraShader {
// RGB = -1,
// InstanceID = 0,
// SemanticID = 1,
// DepthCompressed = 2,
// DepthMultiChannel = 3,
// SurfaceNormals =4
// grayscale=5
//
// }
int outputShaderType = -1;
// Motion blur, can takes values from [0,1], where 1.0 means that the shutter is open across the entire exposure time.
float motionBlurPercent = 0.0;
// Should this camera collision check or check for visibility?
bool hasCollisionCheck = true;
bool doesLandmarkVisCheck = false;
// Should this camera render this frame?
bool shouldRenderThisFrame = true;
// For internal use by the client
int inverseRelativeFramerate = 1;
int numberOfFramesSinceLastRender = 0;
};
// Window class for decoding the ZMQ messages.
struct Object_t
{
std::string ID;
std::string prefabID;
std::vector<double> position;
std::vector<double> rotation;
// Metadata
std::vector<double> size;
};
struct StateMessage_t
{
// Scene/Render settings
bool sceneIsInternal = true;
// Scene choices in v1.4.1
// std::string sceneFilename = "Hazelwood_Loft_Full_Night";
// std::string sceneFilename = "Hazelwood_Loft_Full_Day";
// std::string sceneFilename = "Butterfly_World";
// std::string sceneFilename = "NYC_Subway";
// std::string sceneFilename = "Museum_Day";
std::string sceneFilename = "Museum_Day_Small";
std::string obstaclePerturbationFile = "";
// Frame Metadata
int64_t ntime;
int camWidth = 1024;
int camHeight = 768;
float camFOV = 60.0f;
float camDepthScale = 10.0; // 0.xx corresponds to xx cm resolution
// Object state update
std::vector<Camera_t> cameras;
std::vector<Object_t> objects;
// std::vector<Landmark_t> landmarks;
};
// Json constructors
// StateMessage_t
inline void to_json(json &j, const StateMessage_t &o)
{
j = json{// Initializers
// {"maxFramerate", o.maxFramerate},
{"sceneIsInternal", o.sceneIsInternal},
{"sceneFilename", o.sceneFilename},
{"obstaclePerturbationFile", o.obstaclePerturbationFile},
// Frame Metadata
{"ntime", o.ntime},
{"camWidth", o.camWidth},
{"camHeight", o.camHeight},
{"camFOV", o.camFOV},
{"camDepthScale", o.camDepthScale},
// Object state update
{"cameras", o.cameras},
{"objects", o.objects}
};
}
// Camera_t
inline void to_json(json &j, const Camera_t &o)
{
j = json{{"ID", o.ID},
{"position", o.position},
{"rotation", o.rotation},
{"outputShaderType", o.outputShaderType},
{"motionBlurPercent", o.motionBlurPercent},
{"hasCollisionCheck", o.hasCollisionCheck},
{"doesLandmarkVisCheck", o.doesLandmarkVisCheck},
{"shouldRenderThisFrame", o.shouldRenderThisFrame}
};
}
// Object_t
inline void to_json(json &j, const Object_t &o)
{
j = json{
{"ID", o.ID},
{"prefabID", o.prefabID},
{"position", o.position},
{"rotation", o.rotation},
{"size", o.size}
};
}
}
// Struct for returning metadata from Unity.
namespace unity_incoming
{
// Points in the world that should be checked for visibility from some camera.
struct Landmark_t
{
std::string ID;
std::vector<double> position;
};
struct RenderMetadata_t
{
// Metadata
int64_t ntime;
int camWidth;
int camHeight;
float camDepthScale;
// Object state update
std::vector<std::string> cameraIDs;
std::vector<int> cameraIndexes;
std::vector<int> channels;
// Status update from collision detectors and raycasters.
bool hasCameraCollision = false;
std::vector<Landmark_t> landmarksInView;
float lidarReturn = 100.0f;
};
// Json Parsers
// Landmark_t
inline void from_json(const json &j, Landmark_t &o)
{
o.ID = j.at("ID").get<std::string>();
o.position = j.at("position").get<std::vector<double>>();
}
// RenderMetadata_t
inline void from_json(const json &j, RenderMetadata_t &o)
{
// Get timestamp
if (j.find("ntime") != j.end() ){
o.ntime = j.at("ntime").get<int64_t>();
} else {
// Backwards compatibility for FlightGoggles API <= v1.7.0
o.ntime = j.at("utime").get<int64_t>();
}
o.camWidth = j.at("camWidth").get<int>();
o.camHeight = j.at("camHeight").get<int>();
o.camDepthScale = j.at("camDepthScale").get<float>();
o.cameraIDs = j.at("cameraIDs").get<std::vector<std::string>>();
o.cameraIndexes = j.at("cameraIndexes").get<std::vector<int>>();
o.channels = j.at("channels").get<std::vector<int>>();
// Backwards compatibility for FlightGoggles API <= v1.7.0
if (j.find("hasCameraCollision") != j.end() ){
o.hasCameraCollision = j.at("hasCameraCollision").get<bool>();
}
if (j.find("landmarksInView") != j.end()){
o.landmarksInView = j.at("landmarksInView").get<std::vector<Landmark_t>>();
}
if (j.find("lidarReturn") != j.end()){
o.lidarReturn = j.at("lidarReturn").get<float>();
}
}
// Struct for outputting parsed received messages to handler functions
struct RenderOutput_t
{
RenderMetadata_t renderMetadata;
std::vector<cv::Mat> images;
};
}
#endif