-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGridViewer.cpp
402 lines (305 loc) · 11.1 KB
/
GridViewer.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/************************************************************/
/* NAME: David Battle */
/* ORGN: MISSION SYSTEMS PTY LTD */
/* FILE: GridViewer.cpp */
/* DATE: 17 Dec 2018 */
/************************************************************/
#include "GridViewer.h"
#include "MBUtils.h"
#include <nlohmann/json.hpp>
using namespace std;
using json = nlohmann::json;
//---------------------------------------------------------
// Constructor
GridViewer::GridViewer()
{
m_iterations = 0;
m_timewarp = 1;
m_lidar_frame = false;
m_point_size = 1;
m_resolution = 1.0f;
m_display_depth = 0;
m_down_count = 0;
m_up_count = 0;
m_auto_level = true;
// Camera params
m_camera = "Camera unset";
m_camera_update = false;
m_fov_deg = 90.0;
m_focus_x = 0.f;
m_focus_y = 0.f;
m_focus_z = 0.f;
m_pos_x = 0.f;
m_pos_y = 0.f;
m_pos_z = 0.f;
m_up_x = 0.f;
m_up_y = 0.f;
m_up_z = 1.f;
// Instantiate PCL Visualizer object
m_viewer = boost::shared_ptr<pcl::visualization::PCLVisualizer>
(new pcl::visualization::PCLVisualizer ("pGridViewer"));
m_viewer->setBackgroundColor(0, 0, 0);
m_viewer->addCoordinateSystem(1.0);
m_viewer->initCameraParameters();
// Instantiate Octree object
m_octree = boost::shared_ptr<pcl::octree::OctreePointCloudOccupancy<RGBA_point>>
(new pcl::octree::OctreePointCloudOccupancy<RGBA_point> (1.0f));
// Instantiate display cloud object
m_displayCloud = RGBA_cloud_ptr(new RGBA_cloud);
m_displayCloud->is_dense = false;
}
//---------------------------------------------------------
// Destructor
GridViewer::~GridViewer()
{
}
//---------------------------------------------------------
// Procedure: OnNewMail
bool GridViewer::OnNewMail(MOOSMSG_LIST &NewMail)
{
MOOSMSG_LIST::iterator p;
for(p=NewMail.begin(); p!=NewMail.end(); p++){
CMOOSMsg &msg = *p;
string key = msg.GetKey();
string sval = msg.GetString();
// Process lidar messages
if (strEnds(key, "_DATA")) {
size_t numBytes = msg.GetBinaryDataSize();
size_t numFloats = numBytes/sizeof(float);
size_t numPoints = numFloats/4;
// size_t numPoints = numBytes/sizeof(RGBA_point); 32 bytes!
// Working cloud object - needs to be a shared pointer
RGBA_cloud_ptr cloud (new RGBA_cloud());
cloud->points.clear();
cloud->width = numPoints;
cloud->height = 1;
cloud->is_dense = false;
cloud->points.resize(numPoints);
// Pointer to float message data
float *ptr = reinterpret_cast<float *>(msg.GetBinaryData());
// Create buffer for point data
// RGBA_point * point_ptr = reinterpret_cast<RGBA_point *>(msg.GetBinaryData());
// Copy data to point cloud
for (size_t i = 0, j = 0; i < numPoints; ++i){
cloud->points[i].x = ptr[j++];
cloud->points[i].y = ptr[j++];
cloud->points[i].z = ptr[j++];
cloud->points[i].rgba = *reinterpret_cast<uint32_t*>(&ptr[j++]);
// cloud->points[i].x = point_ptr->x;
// cloud->points[i].y = point_ptr->y;
// cloud->points[i].z = point_ptr->z;
// cloud->points[i].rgba = point_ptr->rgba;
// ++point_ptr;
}
m_octree->setInputCloud(cloud);
m_octree->addPointsFromInputCloud();
}
else if (key == "LIDAR_VIEW")
try {
// Parse the json string
json data = json::parse(sval);
// Get camera name
m_camera = data["camera"];
// Camera position
m_pos_x = data["pos"][0];
m_pos_y = data["pos"][1];
m_pos_z = data["pos"][2];
// Camera orientation
m_up_x = data["Y"][0];
m_up_y = data["Y"][1];
m_up_z = data["Y"][2];
float view_x = data["Z"][0];
float view_y = data["Z"][1];
float view_z = data["Z"][2];
// Incorrect documentation!
// The view vector is actually the focus
// point, so we need to explicitly compute
// the camera's view vector like this...
m_focus_x = m_pos_x + 100.f * view_x;
m_focus_y = m_pos_y + 100.f * view_y;
m_focus_z = m_pos_z + 100.f * view_z;
m_camera_update = true;
} catch (...) {
cout << "Warning: invalid camera data!" << endl;
m_camera_update = false;
}
}
return true;
}
//---------------------------------------------------------
// Procedure: OnConnectToServer
bool GridViewer::OnConnectToServer()
{
RegisterVariables();
return(true);
}
//---------------------------------------------------------
// Procedure: Iterate()
// happens AppTick times per second
bool GridViewer::Iterate()
{
m_iterations++;
m_displayCloud->points.clear();
// Start a display timer
double start = MOOSTime();
if (!m_auto_level) {
// Display at full resolution
m_display_depth = m_octree->getTreeDepth();
}
pcl::octree::OctreePointCloudOccupancy<RGBA_point>::Iterator it(&*m_octree);
RGBA_point pt;
while(*++it) {
// Only process voxels at the display depth
if (static_cast<int> (it.getCurrentOctreeDepth ()) != m_display_depth)
continue;
Eigen::Vector3f voxel_min, voxel_max;
m_octree->getVoxelBounds(it, voxel_min, voxel_max);
// Construct a point cloud
pt.x = (voxel_min.x() + voxel_max.x()) / 2.0f;
pt.y = (voxel_min.y() + voxel_max.y()) / 2.0f;
pt.z = (voxel_min.z() + voxel_max.z()) / 2.0f;
m_displayCloud->points.push_back(pt);
// Don't iterate any lower
it.skipChildVoxels();
}
// The code below should be faster
// It appears that we must await Version 1.91 for the FixedDepthIterator, however.
// pcl::octree::OctreePointCloudOccupancy<pcl::PointXYZ>::OctreeFixedDepthIterator it(&*m_octree, m_display_depth);
// pcl::PointXYZ pt;
// while(*++it) {
// Eigen::Vector3f voxel_min, voxel_max;
// m_octree->getVoxelBounds(it, voxel_min, voxel_max);
// // Construct a point cloud
// pt.x = (voxel_min.x() + voxel_max.x()) / 2.0f;
// pt.y = (voxel_min.y() + voxel_max.y()) / 2.0f;
// pt.z = (voxel_min.z() + voxel_max.z()) / 2.0f;
// m_displayCloud->points.push_back(pt);
// }
// The code below returns all of the leaf node centers
// Ideally, we only want to display those at a certain level, however.
// std::vector<pcl::PointXYZ, Eigen::aligned_allocator<pcl::PointXYZ> > voxelCenters;
// int Nvox = m_octree->getOccupiedVoxelCenters(voxelCenters);
// m_displayCloud->points.reserve(Nvox);
// m_displayCloud->width = Nvox;
// m_displayCloud->height = 1;
// // Copy voxel centers to display cloud
// for (int i = 0; i < Nvox; i++)
// m_displayCloud->points.push_back(voxelCenters[i]);
// Set the colour handler - not really ideal, as it needs to be inverted...
pcl::visualization::PointCloudColorHandlerGenericField<RGBA_point> color_handler(m_displayCloud,"z");
// Update display point cloud
m_viewer->updatePointCloud<RGBA_point> (m_displayCloud, color_handler, "cloud");
if (m_camera_update) {
m_viewer->setCameraPosition(m_pos_x, m_pos_y, m_pos_z,
m_focus_x, m_focus_y, m_focus_z,
m_up_x, m_up_y, m_up_z);
m_viewer->updateText(m_camera, 0, 20, 12, 1.0, 1.0, 1.0, "camera");
} else
m_viewer->updateText(m_camera + " (No data)", 0, 20, 12, 1.0, 1.0, 1.0, "camera");
m_viewer->spinOnce(1,true);
double display_time = MOOSTime() - start;
// Automatically set display depth
if (m_auto_level) {
double allowed_time = 1.0 / GetAppFreq();
// Is the display keeping up?
if (display_time > 2.0 * allowed_time)
m_down_count++;
// Decrement display depth if too slow
if (m_down_count > 10) {
m_down_count = 0;
m_display_depth--;
}
// Increment display depth if we are running fast enough
if (display_time < 0.5 * allowed_time && m_display_depth < m_octree->getTreeDepth())
m_up_count++;
if (m_up_count > 10) {
m_up_count = 0;
m_display_depth++;
}
}
cout << "Tree depth = " << m_octree->getTreeDepth() <<
", Disp depth = " << m_display_depth <<
", Disp time = " << display_time <<
", Leaf count = " << m_octree->getLeafCount() << endl;
return(true);
}
//---------------------------------------------------------
// Procedure: OnStartUp()
// happens before connection is open
bool GridViewer::OnStartUp()
{
SetAppFreq(10,100);
// SetIterateMode(COMMS_DRIVEN_ITERATE_AND_MAIL);
list<string> sParams;
m_MissionReader.EnableVerbatimQuoting(false);
if(m_MissionReader.GetConfiguration(GetAppName(), sParams)) {
list<string>::iterator p;
for(p=sParams.begin(); p!=sParams.end(); p++) {
string original_line = *p;
string param = stripBlankEnds(toupper(biteString(*p, '=')));
string value = stripBlankEnds(*p);
double dval = atof(value.c_str());
bool bval = (strcasecmp (value.c_str(), "TRUE") == 0 || dval != 0);
if (param == "LIDAR_FRAME") {
// TO DO: Handle incoming data in lidar-frame
cout << "LIDAR_FRAME = " << bval << endl;
m_lidar_frame = bval;
}
else if (param == "RESOLUTION") {
cout << "RESOLUTION = " << dval << endl;
m_resolution = dval;
}
else if (param == "POINT_SIZE") {
cout << "POINT_SIZE = " << dval << endl;
m_point_size = dval;
}
else if(param == "FOV_DEG") {
cout << "FOV_DEG = " << dval << endl;
m_fov_deg = dval;
}
else if(param == "AUTO_LEVEL") {
cout << "AUTO_LEVEL = " << bval << endl;
m_auto_level = bval;
}
}
}
m_timewarp = GetMOOSTimeWarp();
// look for latitude, longitude global variables
double latOrigin, longOrigin;
if(!m_MissionReader.GetValue("LatOrigin", latOrigin)) {
MOOSTrace("pGridViewer: LatOrigin not set in *.moos file.\n");
m_geo_ok = false;
}
else if(!m_MissionReader.GetValue("LongOrigin", longOrigin)) {
MOOSTrace("pGridViewer: LongOrigin not set in *.moos file\n");
m_geo_ok = false;
}
else {
m_geo_ok = true;
// initialize m_geodesy
if(!m_geodesy.Initialise(latOrigin, longOrigin)) {
MOOSTrace("pGridViewer: Geodesy init failed.\n");
m_geo_ok = false;
}
}
RegisterVariables();
// Set the colour handler
pcl::visualization::PointCloudColorHandlerGenericField<RGBA_point> color_handler(m_displayCloud,"z");
// Set some viewer properties
m_viewer->setCameraFieldOfView(m_fov_deg / 180.0 * M_PI);
m_viewer->addText(m_camera, 0, 20, "camera");
m_viewer->addPointCloud<RGBA_point> (m_displayCloud, color_handler, "cloud");
m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, m_point_size, "cloud");
// Override default resolution
m_octree->setResolution(m_resolution);
return true;
}
//---------------------------------------------------------
// Procedure: RegisterVariables
void GridViewer::RegisterVariables()
{
// Register for all Lidar data
Register("*_DATA", "pLidarSim", 0);
// Point cloud view position and vector
Register("LIDAR_VIEW", 0);
}