-
Notifications
You must be signed in to change notification settings - Fork 226
/
aerialmap_display.cpp
586 lines (492 loc) · 16.8 KB
/
aerialmap_display.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* Copyright 2014 Gareth Cross
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include <unordered_map>
#include <QtGlobal>
#include <QImage>
#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <OGRE/OgreManualObject.h>
#include <OGRE/OgreMaterialManager.h>
#include <OGRE/OgreSceneManager.h>
#include <OGRE/OgreSceneNode.h>
#include <OGRE/OgreTextureManager.h>
#include <OGRE/OgreImageCodec.h>
#include <OGRE/OgreVector3.h>
#include "rviz/frame_manager.h"
#include "rviz/ogre_helpers/grid.h"
#include "rviz/properties/enum_property.h"
#include "rviz/properties/float_property.h"
#include "rviz/properties/int_property.h"
#include "rviz/properties/property.h"
#include "rviz/properties/quaternion_property.h"
#include "rviz/properties/ros_topic_property.h"
#include "rviz/properties/vector_property.h"
#include "rviz/validate_floats.h"
#include "rviz/display_context.h"
#include "aerialmap_display.h"
#include "General.h"
int constexpr FRAME_CONVENTION_XYZ_ENU = 0; // X -> East, Y -> North
int constexpr FRAME_CONVENTION_XYZ_NED = 1; // X -> North, Y -> East
int constexpr FRAME_CONVENTION_XYZ_NWU = 2; // X -> North, Y -> West
namespace rviz
{
AerialMapDisplay::AerialMapDisplay() : Display(), dirty_(false), received_msg_(false)
{
topic_property_ =
new RosTopicProperty("Topic", "", QString::fromStdString(ros::message_traits::datatype<sensor_msgs::NavSatFix>()),
"sensor_msgs::NavSatFix topic to subscribe to.", this, SLOT(updateTopic()));
alpha_property_ =
new FloatProperty("Alpha", 0.7, "Amount of transparency to apply to the map.", this, SLOT(updateAlpha()));
alpha_ = alpha_property_->getValue().toFloat();
alpha_property_->setMin(0);
alpha_property_->setMax(1);
alpha_property_->setShouldBeSaved(true);
draw_under_property_ = new Property("Draw Behind", false,
"Rendering option, controls whether or not the map is always"
" drawn behind everything else.",
this, SLOT(updateDrawUnder()));
draw_under_property_->setShouldBeSaved(true);
draw_under_ = draw_under_property_->getValue().toBool();
// output, resolution of the map in meters/pixel
resolution_property_ = new FloatProperty("Resolution", 0, "Resolution of the map. (Read only)", this);
resolution_property_->setReadOnly(true);
// properties for map
tile_url_property_ =
new StringProperty("Object URI", "", "URL from which to retrieve map tiles.", this, SLOT(updateTileUrl()));
tile_url_property_->setShouldBeSaved(true);
tile_url_ = tile_url_property_->getStdString();
QString const zoom_desc = QString::fromStdString("Zoom level (0 - " + std::to_string(maxZoom) + ")");
zoom_property_ = new IntProperty("Zoom", 16, zoom_desc, this, SLOT(updateZoom()));
zoom_property_->setShouldBeSaved(true);
zoom_property_->setMin(0);
zoom_property_->setMax(maxZoom);
zoom_ = zoom_property_->getInt();
QString const blocks_desc = QString::fromStdString("Adjacent blocks (0 - " + std::to_string(maxBlocks) + ")");
blocks_property_ = new IntProperty("Blocks", 3, blocks_desc, this, SLOT(updateBlocks()));
blocks_property_->setShouldBeSaved(true);
blocks_property_->setMin(0);
blocks_property_->setMax(maxBlocks);
frame_convention_property_ =
new EnumProperty("Frame Convention", "XYZ -> ENU", "Convention for mapping cartesian frame to the compass", this,
SLOT(updateFrameConvention()));
frame_convention_property_->addOptionStd("XYZ -> ENU", FRAME_CONVENTION_XYZ_ENU);
frame_convention_property_->addOptionStd("XYZ -> NED", FRAME_CONVENTION_XYZ_NED);
frame_convention_property_->addOptionStd("XYZ -> NWU", FRAME_CONVENTION_XYZ_NWU);
// updating one triggers reload
updateBlocks();
}
AerialMapDisplay::~AerialMapDisplay()
{
unsubscribe();
clear();
}
void AerialMapDisplay::onEnable()
{
lastFixedFrame_ = context_->getFrameManager()->getFixedFrame();
subscribe();
}
void AerialMapDisplay::onDisable()
{
unsubscribe();
clear();
context_->getFrameManager()->setFixedFrame(lastFixedFrame_);
}
void AerialMapDisplay::subscribe()
{
if (!isEnabled())
{
return;
}
if (!topic_property_->getTopic().isEmpty())
{
try
{
ROS_INFO("Subscribing to %s", topic_property_->getTopicStd().c_str());
coord_sub_ = update_nh_.subscribe(topic_property_->getTopicStd(), 1, &AerialMapDisplay::navFixCallback, this);
setStatus(StatusProperty::Ok, "Topic", "OK");
}
catch (ros::Exception& e)
{
setStatus(StatusProperty::Error, "Topic", QString("Error subscribing: ") + e.what());
}
}
}
void AerialMapDisplay::unsubscribe()
{
coord_sub_.shutdown();
ROS_INFO("Unsubscribing.");
}
void AerialMapDisplay::updateAlpha()
{
alpha_ = alpha_property_->getFloat();
dirty_ = true;
ROS_INFO("Changing alpha to %f", alpha_);
}
void AerialMapDisplay::updateDrawUnder()
{
// @todo figure out why this property only applies to some objects
draw_under_ = draw_under_property_->getValue().toBool();
dirty_ = true; // force update
ROS_INFO("Changing draw_under to %s", ((draw_under_) ? "true" : "false"));
}
void AerialMapDisplay::updateTileUrl()
{
tile_url_ = tile_url_property_->getStdString();
}
void AerialMapDisplay::updateZoom()
{
int const zoom = std::max(0, std::min(maxZoom, zoom_property_->getInt()));
if (zoom != zoom_)
{
zoom_ = zoom;
clear();
}
}
void AerialMapDisplay::updateBlocks()
{
int const blocks = std::max(0, std::min(maxBlocks, blocks_property_->getInt()));
if (blocks != blocks_)
{
blocks_ = blocks;
clear();
}
}
void AerialMapDisplay::updateFrameConvention()
{
transformAerialMap();
}
void AerialMapDisplay::updateTopic()
{
unsubscribe();
clear();
subscribe();
}
void AerialMapDisplay::clear()
{
setStatus(StatusProperty::Warn, "Message", "No map received");
clearGeometry();
}
void AerialMapDisplay::clearGeometry()
{
for (MapObject& obj : objects_)
{
// destroy object
scene_node_->detachObject(obj.object);
scene_manager_->destroyManualObject(obj.object);
// destroy material
if (!obj.material.isNull())
{
Ogre::MaterialManager::getSingleton().remove(obj.material->getName());
}
}
objects_.clear();
dirty_ = true;
}
void AerialMapDisplay::createGeometry()
{
for (int block = 0; block < (2 * blocks_ + 1) * (2 * blocks_ + 1); ++block)
{
// generate an unique name
static int count = 0;
std::string const name_suffix = std::to_string(count);
++count;
// one material per texture
Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create(
"satellite_material_" + name_suffix, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
material->setReceiveShadows(false);
material->getTechnique(0)->setLightingEnabled(false);
material->setDepthBias(-16.0f, 0.0f);
material->setCullingMode(Ogre::CULL_NONE);
material->setDepthWriteEnabled(false);
// create texture and initialize it
Ogre::TextureUnitState* tex_unit = material->getTechnique(0)->getPass(0)->createTextureUnitState();
tex_unit->setTextureFiltering(Ogre::TFO_BILINEAR);
// create an object
Ogre::ManualObject* obj = scene_manager_->createManualObject("satellite_object_" + name_suffix);
obj->setVisible(false);
scene_node_->attachObject(obj);
assert(!material.isNull());
objects_.emplace_back(obj, material);
}
}
void AerialMapDisplay::update(float, float)
{
// create all geometry, if necessary
assembleScene();
// draw
context_->queueRender();
}
void AerialMapDisplay::navFixCallback(sensor_msgs::NavSatFixConstPtr const& msg)
{
ref_fix_ = *msg;
// re-load imagery
received_msg_ = true;
loadImagery();
transformAerialMap();
}
void AerialMapDisplay::loadImagery()
{
if (!isEnabled())
{
return;
}
// When the plugin starts, the properties from the config are set.
// This will call the callbacks and these will call this function.
// By checking if a message was received, we prevent to update the
// images when Rviz loads the config.
if (!received_msg_)
{
return;
}
if (tile_url_.empty())
{
setStatus(StatusProperty::Error, "Message", "Tile URL is not set");
return;
}
TileId tileId{ tile_url_, fromWGSCoordinate({ ref_fix_.latitude, ref_fix_.longitude }, zoom_), zoom_ };
if (!lastTileId_ || !(tileId == *lastTileId_))
{
lastTileId_ = tileId;
try
{
tileCache_.request({ tileId, blocks_ });
dirty_ = true;
}
catch (std::exception& e)
{
setStatus(StatusProperty::Error, "Message", QString(e.what()));
return;
}
}
// the following error rate thresholds are randomly chosen
float const errorRate = tileCache_.getTileServerErrorRate(tile_url_);
if (errorRate > 0.95)
{
setStatus(StatusProperty::Level::Error, "Message", "Few or no tiles received");
}
else if (errorRate > 0.3)
{
setStatus(StatusProperty::Level::Warn, "Message",
"Not all requested tiles have been received. Possibly the server is throttling?");
}
else
{
setStatus(StatusProperty::Level::Ok, "Message", "OK");
}
}
void AerialMapDisplay::assembleScene()
{
if (!isEnabled() || !dirty_ || !lastTileId_)
{
return;
}
dirty_ = false;
// was clearGeometry() called?
if (objects_.empty())
{
createGeometry();
// e.g. when the number of blocks got bigger, the new tiles have to be loaded
tileCache_.request({ *lastTileId_, blocks_ });
}
TileId tileId{ tile_url_, fromWGSCoordinate({ ref_fix_.latitude, ref_fix_.longitude }, zoom_), zoom_ };
Area area(tileId, blocks_);
TileCacheGuard guard(tileCache_);
bool loadedAllTiles = true;
auto it = objects_.begin();
for (int xx = area.leftTop.x; xx <= area.rightBottom.x; ++xx)
{
for (int yy = area.leftTop.y; yy <= area.rightBottom.y; ++yy)
{
auto obj = it->object;
auto& material = it->material;
assert(!material.isNull());
++it;
TileId const toFind{ tileId.tileServer, { xx, yy }, tileId.zoom };
OgreTile const* tile = tileCache_.ready(toFind);
if (!tile)
{
// don't show tiles with old textures
obj->setVisible(false);
loadedAllTiles = false;
continue;
}
obj->setVisible(true);
// update texture
Ogre::TextureUnitState* tex_unit = material->getTechnique(0)->getPass(0)->getTextureUnitState(0);
tex_unit->setTextureName(tile->texture->getName());
// configure depth & alpha properties
if (alpha_ >= 0.9998)
{
material->setDepthWriteEnabled(!draw_under_);
material->setSceneBlending(Ogre::SBT_REPLACE);
}
else
{
material->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
material->setDepthWriteEnabled(false);
}
if (draw_under_)
{
// render under everything else
obj->setRenderQueueGroup(Ogre::RENDER_QUEUE_3);
}
else
{
obj->setRenderQueueGroup(Ogre::RENDER_QUEUE_MAIN);
}
tex_unit->setAlphaOperation(Ogre::LBX_SOURCE1, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, alpha_);
// tile width/ height in meter
double const tile_w_h_m = getTileWH();
// determine position of this tile relative to the robot's position
// note: We invert the y-axis so that positive y corresponds to north. See transformAerialMap().
double const x = (xx - lastTileId_->coord.x) * tile_w_h_m;
double const y = -(yy - lastTileId_->coord.y) * tile_w_h_m;
// create a quad for this tile
// note: We have to create the vertices everytime and cannot reuse the old vertices: tile_w_h_m depends on the
// latitude.
obj->clear();
obj->begin(material->getName(), Ogre::RenderOperation::OT_TRIANGLE_LIST);
// bottom left
obj->position(x, y, 0.0f);
obj->textureCoord(0.0f, 0.0f);
obj->normal(0.0f, 0.0f, 1.0f);
// top right
obj->position(x + tile_w_h_m, y + tile_w_h_m, 0.0f);
obj->textureCoord(1.0f, 1.0f);
obj->normal(0.0f, 0.0f, 1.0f);
// top left
obj->position(x, y + tile_w_h_m, 0.0f);
obj->textureCoord(0.0f, 1.0f);
obj->normal(0.0f, 0.0f, 1.0f);
// bottom left
obj->position(x, y, 0.0f);
obj->textureCoord(0.0f, 0.0f);
obj->normal(0.0f, 0.0f, 1.0f);
// bottom right
obj->position(x + tile_w_h_m, y, 0.0f);
obj->textureCoord(1.0f, 0.0f);
obj->normal(0.0f, 0.0f, 1.0f);
// top right
obj->position(x + tile_w_h_m, y + tile_w_h_m, 0.0f);
obj->textureCoord(1.0f, 1.0f);
obj->normal(0.0f, 0.0f, 1.0f);
obj->end();
}
}
// since not all tiles were loaded, this function has to be called again
if (!loadedAllTiles)
{
dirty_ = true;
}
tileCache_.purge({ tileId, blocks_ });
}
void AerialMapDisplay::transformAerialMap()
{
if (!isEnabled())
{
return;
}
// the parent frame of this scene node
static std::string const mapFrame = "map";
// the robot's frame
std::string const frame = ref_fix_.header.frame_id;
// note: the orientation is not used on purpose
Ogre::Quaternion orientation;
Ogre::Vector3 position;
// can mapFrame be used as a fixed frame?
std::string errMsg;
if (context_->getFrameManager()->frameHasProblems(mapFrame, ros::Time{}, errMsg) ||
context_->getFrameManager()->transformHasProblems(mapFrame, ros::Time{}, errMsg))
{
context_->getFrameManager()->setFixedFrame(lastFixedFrame_);
setStatus(StatusProperty::Error, "Transform", QString::fromStdString(errMsg));
return;
}
// Set the pseudo fixed frame to map so that we can align the tiles to north just by setting identity as the
// orientation
context_->getFrameManager()->setFixedFrame(mapFrame);
// Get the latest transform between the robot frame and fixed frame from the FrameManager
if (!context_->getFrameManager()->getTransform(frame, ros::Time(), position, orientation))
{
// display error
std::string error;
if (context_->getFrameManager()->transformHasProblems(frame, ros::Time(), error))
{
setStatus(StatusProperty::Error, "Transform", QString::fromStdString(error));
}
else
{
setStatus(StatusProperty::Error, "Transform",
"Could not transform from [" + QString::fromStdString(frame) + "] to Fixed Frame [" + fixed_frame_ +
"] for an unknown reason");
}
return;
}
if (position.isNaN())
{
// This can occur if an invalid TF is published.
// Show an error and don't apply anything, so OGRE does not throw an assertion.
setStatus(StatusProperty::Error, "Transform", "Received invalid transform");
return;
}
setStatus(StatusProperty::Ok, "Transform", "Transform OK");
// apply transform and add offset from origin
// decimal places are needed here to calculate the offset
auto const center = fromWGSCoordinate<double>({ ref_fix_.latitude, ref_fix_.longitude }, zoom_);
auto const originOffsetX = center.x - std::floor(center.x);
auto const originOffsetY = 1 - center.y + std::floor(center.y); // y coord is flipped
double const tile_w_h_m = getTileWH();
position.x -= originOffsetX * tile_w_h_m;
position.y -= originOffsetY * tile_w_h_m;
scene_node_->setPosition(position);
int const convention = frame_convention_property_->getOptionInt();
if (convention == FRAME_CONVENTION_XYZ_ENU)
{
// ENU corresponds to our default drawing method
scene_node_->setOrientation(Ogre::Quaternion::IDENTITY);
}
else if (convention == FRAME_CONVENTION_XYZ_NED)
{
// XYZ->NED will cause the map to appear reversed when viewed from above (from +z).
// clang-format off
Ogre::Matrix3 const xyz_R_ned(0, 1, 0,
1, 0, 0,
0, 0, -1);
// clang-format on
scene_node_->setOrientation(xyz_R_ned.Transpose());
}
else if (convention == FRAME_CONVENTION_XYZ_NWU)
{
// clang-format off
Ogre::Matrix3 const xyz_R_nwu(0, -1, 0,
1, 0, 0,
0, 0, 1);
// clang-format on
scene_node_->setOrientation(xyz_R_nwu.Transpose());
}
else
{
ROS_ERROR_STREAM("Invalid convention code: " << convention);
}
}
void AerialMapDisplay::fixedFrameChanged()
{
transformAerialMap();
}
void AerialMapDisplay::reset()
{
Display::reset();
// unsub,clear,resub
updateTopic();
}
} // namespace rviz
#include <pluginlib/class_list_macros.h>
PLUGINLIB_EXPORT_CLASS(rviz::AerialMapDisplay, rviz::Display)