-
Notifications
You must be signed in to change notification settings - Fork 226
/
aerialmap_display.cpp
1258 lines (1062 loc) · 37.6 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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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 <OGRE/OgreManualObject.h>
#include <OGRE/OgreMaterialManager.h>
#include <OGRE/OgreSceneManager.h>
#include <OGRE/OgreSceneNode.h>
#include <OGRE/OgreTextureManager.h>
#include <OGRE/OgreTechnique.h>
#include <GeographicLib/UTMUPS.hpp>
#include "rviz/display_context.h"
#include "rviz/frame_manager.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/ros_topic_property.h"
#include "rviz/properties/string_property.h"
#include "rviz/properties/tf_frame_property.h"
#include <tf2/LinearMath/Vector3.h>
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
#include "aerialmap_display.h"
#include "mercator.h"
#include "position_reference_property.h"
#include <regex>
#include <unordered_map>
namespace rviz
{
/**
* @file
* The sequence of events is rather complex due to the asynchronous nature of the tile texture updates, and the
* different coordinate systems and frame transforms involved:
*
* The navSatFixCallback calls the updateCenterTile function, which then queries a texture update and calls
* transformTileToMapFrame. The latter finds and stores the transform from the NavSatFix frame to the map-frame, to
* which the tiles are rigidly attached by ENU convention and Mercator projection. On each frame, update() is called,
* which calls transformMapTileToFixedFrame, which then transforms the tile-map from the map-frame to the fixed-frame.
* Splitting this transform lookup is necessary to mitigate frame jitter.
*/
std::unordered_map<MapTransformType, QString> mapTransformTypeStrings =
{
{MapTransformType::VIA_MAP_FRAME, "NavSatFix Messages and Map Frame"},
{MapTransformType::VIA_UTM_FRAME, "Explicit UTM Frame"},
};
AerialMapDisplay::AerialMapDisplay() : Display()
{
center_tile_pose_.pose.orientation.w = 1;
topic_property_ =
new RosTopicProperty("Topic", "", QString::fromStdString(ros::message_traits::datatype<sensor_msgs::NavSatFix>()),
"sensor_msgs::NavSatFix topic to subscribe to.", this, SLOT(updateTopic()));
map_transform_type_property_ =
new EnumProperty("Map transform type", mapTransformTypeStrings[MapTransformType::VIA_MAP_FRAME],
"Whether to transform tiles via map frame and fix messages or via UTM frame",
this, SLOT(updateMapTransformType()));
map_transform_type_property_->addOption(mapTransformTypeStrings[MapTransformType::VIA_MAP_FRAME],
static_cast<int>(MapTransformType::VIA_MAP_FRAME));
map_transform_type_property_->addOption(mapTransformTypeStrings[MapTransformType::VIA_UTM_FRAME],
static_cast<int>(MapTransformType::VIA_UTM_FRAME));
map_transform_type_property_->setShouldBeSaved(true);
map_transform_type_ = static_cast<MapTransformType>(map_transform_type_property_->getOptionInt());
map_frame_property_ = new TfFrameProperty(
"Map Frame", "map", "Frame ID of the map.", this, nullptr, false, SLOT(updateMapFrame()));
map_frame_property_->setShouldBeSaved(true);
map_frame_ = map_frame_property_->getFrameStd();
utm_frame_property_ = new TfFrameProperty(
"UTM Frame", "utm", "Frame ID of the UTM frame.", this, nullptr, false, SLOT(updateUtmFrame()));
utm_frame_property_->setShouldBeSaved(true);
utm_frame_ = utm_frame_property_->getFrameStd();
utm_zone_property_ =
new IntProperty("UTM Zone", GeographicLib::UTMUPS::STANDARD, "UTM zone (-1 means autodetect).",
this, SLOT(updateUtmZone()));
utm_zone_property_->setMin(GeographicLib::UTMUPS::STANDARD);
utm_zone_property_->setMax(GeographicLib::UTMUPS::MAXZONE);
utm_zone_property_->setShouldBeSaved(true);
utm_zone_ = utm_zone_property_->getInt();
xy_reference_property_ =
new PositionReferenceProperty("XY Reference", PositionReferenceProperty::FIX_MSG_STRING,
"How to determine XY coordinates that define the displayed tiles.",
this, nullptr, SLOT(updateXYReference()));
xy_reference_property_->setShouldBeSaved(true);
xy_reference_type_ = PositionReferenceType::NAV_SAT_FIX_MESSAGE;
z_reference_property_ =
new PositionReferenceProperty("Z Reference", PositionReferenceProperty::FIX_MSG_STRING,
"How to determine height of the displayed tiles.",
this, nullptr, SLOT(updateZReference()));
z_reference_property_->setShouldBeSaved(true);
z_reference_type_ = PositionReferenceType::NAV_SAT_FIX_MESSAGE;
alpha_property_ =
new FloatProperty("Alpha", 0.7, "Amount of transparency to apply to the map.", this, SLOT(updateAlpha()));
alpha_property_->setMin(0);
alpha_property_->setMax(1);
alpha_property_->setShouldBeSaved(true);
alpha_ = alpha_property_->getValue().toFloat();
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();
// 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(MAX_ZOOM) + ")");
zoom_property_ = new IntProperty("Zoom", 16, zoom_desc, this, SLOT(updateZoom()));
zoom_property_->setMin(0);
zoom_property_->setMax(MAX_ZOOM);
zoom_property_->setShouldBeSaved(true);
zoom_ = zoom_property_->getInt();
QString const blocks_desc = QString::fromStdString("Adjacent blocks (0 - " + std::to_string(MAX_BLOCKS) + ")");
blocks_property_ = new IntProperty("Blocks", 3, blocks_desc, this, SLOT(updateBlocks()));
blocks_property_->setMin(0);
blocks_property_->setMax(MAX_BLOCKS);
blocks_property_->setShouldBeSaved(true);
blocks_ = blocks_property_->getInt();
z_offset_property_ =
new FloatProperty("Z Offset", 0.0, "Offset in Z direction (in meters).", this, SLOT(updateZOffset()));
z_offset_property_->setShouldBeSaved(true);
z_offset_ = z_offset_property_->getValue().toFloat();
tf_reference_update_duration_ = {1, 0};
tf_reference_update_timer_ = threaded_nh_.createTimer(
tf_reference_update_duration_, &AerialMapDisplay::tfReferencePeriodicUpdate, this);
}
AerialMapDisplay::~AerialMapDisplay()
{
unsubscribe();
clearAll();
}
void AerialMapDisplay::onInitialize()
{
tf_buffer_ = context_->getFrameManager()->getTF2BufferPtr();
map_frame_property_->setFrameManager(context_->getFrameManager());
utm_frame_property_->setFrameManager(context_->getFrameManager());
xy_reference_property_->setFrameManager(context_->getFrameManager());
z_reference_property_->setFrameManager(context_->getFrameManager());
updateMapTransformType();
}
void AerialMapDisplay::onEnable()
{
createTileObjects();
subscribe();
}
void AerialMapDisplay::onDisable()
{
unsubscribe();
clearAll();
}
void AerialMapDisplay::subscribe()
{
if (!isEnabled())
{
return;
}
if (!topic_property_->getTopic().isEmpty())
{
try
{
ROS_INFO("Subscribing to %s", topic_property_->getTopicStd().c_str());
navsat_fix_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()
{
navsat_fix_sub_.shutdown();
}
void AerialMapDisplay::updateAlpha()
{
// if draw_under_ texture property changed, we need to
// - repaint textures
// we don't need to
// - query textures
// - re-create tile grid geometry
// - update the center tile
// - update transforms
auto const alpha = alpha_property_->getFloat();
if (alpha == alpha_)
{
return;
}
alpha_ = alpha;
if (!isEnabled())
{
return;
}
triggerSceneAssembly();
}
void AerialMapDisplay::updateDrawUnder()
{
// if draw_under_ texture property changed, we need to
// - repaint textures
// we don't need to
// - query textures
// - re-create tile grid geometry
// - update the center tile
// - update transforms
auto const draw_under = draw_under_property_->getValue().toBool();
if (draw_under == draw_under_)
{
return;
}
draw_under_ = draw_under;
if (!isEnabled())
{
return;
}
triggerSceneAssembly();
}
void AerialMapDisplay::updateTileUrl()
{
// if the tile url changed, we need to
// - query textures
// - repaint textures
// we don't need to
// - re-create tile grid geometry
// - update the center tile
// - update transforms
auto const tile_url = tile_url_property_->getStdString();
if (tile_url == tile_url_)
{
return;
}
// Check for valid url: https://stackoverflow.com/a/38608262
if (!std::regex_match(tile_url, std::regex(R"(^(https?:\/\/).*)")))
{
ROS_ERROR("Invalid Object URI: %s", tile_url.c_str());
// Still setting the url since keeping the old is probably unexpected
}
else if (!std::regex_match(tile_url, std::regex(R"(.*\{([xyz]|lat|lon)\}.*)")))
{
ROS_ERROR("No coordinates ({lat}, {lon} or {x}, {y}, {z}) found in URI: %s", tile_url.c_str());
}
tile_url_ = tile_url;
if (!isEnabled())
{
return;
}
requestTileTextures();
}
void AerialMapDisplay::updateZoom()
{
// if the zoom changed, we need to
// - re-create tile grid geometry
// - update the center tile
// - query textures
// - repaint textures
// - update transforms
auto const zoom = zoom_property_->getInt();
if (zoom == zoom_)
{
return;
}
zoom_ = zoom;
if (!isEnabled())
{
return;
}
createTileObjects();
if (ref_fix_)
{
updateCenterTile(ref_fix_);
}
}
void AerialMapDisplay::updateBlocks()
{
// if the number of blocks changed, we need to
// - re-create tile grid geometry
// - query textures
// - repaint textures
// we don't need to
// - update the center tile
// - update transforms
auto const blocks = blocks_property_->getInt();
if (blocks == blocks_)
{
return;
}
blocks_ = blocks;
if (!isEnabled())
{
return;
}
createTileObjects();
requestTileTextures();
}
void AerialMapDisplay::updateTopic()
{
// if the NavSat topic changes, we reset everything
if (!isEnabled())
{
return;
}
unsubscribe();
clearAll();
createTileObjects();
subscribe();
}
void AerialMapDisplay::updateMapTransformType()
{
// if the map transform type changed, we need to
// - enable/disable map/utm frame inputs
// - update XY and Z reference settings to only use values valid for the given mode
// - query textures
// - repaint textures
// - reset and update the center tile
// - update transforms
// we don't need to
// - re-create tile grid geometry
auto const map_transform_type = static_cast<MapTransformType>(map_transform_type_property_->getOptionInt());
map_transform_type_ = map_transform_type;
switch (map_transform_type_)
{
case MapTransformType::VIA_MAP_FRAME:
utm_frame_property_->hide();
utm_zone_property_->hide();
map_frame_property_->show();
break;
case MapTransformType::VIA_UTM_FRAME:
utm_frame_property_->show();
utm_zone_property_->show();
map_frame_property_->hide();
break;
}
updateXYReference();
updateZReference();
if (ref_fix_)
{
updateCenterTile(ref_fix_);
}
}
void AerialMapDisplay::updateMapFrame()
{
// if the map frame changed, we need to
// - query textures
// - repaint textures
// - reset and update the center tile
// - update transforms
// we don't need to
// - re-create tile grid geometry
auto const map_frame = map_frame_property_->getFrameStd();
if (map_frame == map_frame_)
{
return;
}
map_frame_ = map_frame;
if (!isEnabled())
{
return;
}
if (ref_fix_)
{
updateCenterTile(ref_fix_);
}
}
void AerialMapDisplay::updateUtmFrame()
{
// if the UTM frame changed, we need to
// - query textures
// - repaint textures
// - reset and update the center tile
// - update transforms
// we don't need to
// - re-create tile grid geometry
auto const utm_frame = utm_frame_property_->getFrameStd();
if (utm_frame == utm_frame_)
{
return;
}
utm_frame_ = utm_frame;
if (!isEnabled())
{
return;
}
if (ref_fix_)
{
updateCenterTile(ref_fix_);
}
}
void AerialMapDisplay::updateUtmZone()
{
// if the UTM zone changed, we need to
// - query textures
// - repaint textures
// - reset and update the center tile
// - update transforms
// we don't need to
// - re-create tile grid geometry
auto const utm_zone = utm_zone_property_->getInt();
if (utm_zone == utm_zone_)
{
return;
}
utm_zone_ = utm_zone;
if (!isEnabled())
{
return;
}
if (ref_fix_)
{
updateCenterTile(ref_fix_);
}
}
void AerialMapDisplay::updateXYReference()
{
// if the XY reference changed, we need to
// - query textures
// - repaint textures
// - reset and update the center tile
// - update transforms
// we don't need to
// - re-create tile grid geometry
const auto old_reference_type = xy_reference_type_;
const auto old_reference_frame = xy_reference_frame_;
switch (map_transform_type_)
{
case MapTransformType::VIA_MAP_FRAME:
xy_reference_type_ = PositionReferenceType::NAV_SAT_FIX_MESSAGE;
xy_reference_frame_ = "";
xy_reference_property_->hide();
break;
case MapTransformType::VIA_UTM_FRAME:
xy_reference_type_ = xy_reference_property_->getPositionReferenceType();
xy_reference_frame_ = xy_reference_property_->getFrameStd();
xy_reference_property_->show();
if (xy_reference_type_ == PositionReferenceType::TF_FRAME && xy_reference_frame_ == utm_frame_)
{
ROS_WARN_THROTTLE(2.0, "Setting UTM frame '%s' as XY reference is invalid, as the computed easting and "
"northing of zero is out of bounds. Select a different frame.", utm_frame_.c_str());
}
break;
}
if (!isEnabled() || (old_reference_type == xy_reference_type_ && old_reference_frame == xy_reference_frame_))
{
return;
}
if (xy_reference_type_ != PositionReferenceType::TF_FRAME)
{
deleteStatus("UTM");
deleteStatus("XY Reference Transform");
deleteStatus("XY reference UTM conversion");
}
if (ref_fix_)
{
updateCenterTile(ref_fix_);
}
}
void AerialMapDisplay::updateZReference()
{
// if the Z reference changed, we need to
// - update transforms
// we don't need to
// - re-create tile grid geometry
// - query textures
// - repaint textures
// - reset and update the center tile
const auto old_reference_type = z_reference_type_;
const auto old_reference_frame = z_reference_frame_;
switch (map_transform_type_)
{
case MapTransformType::VIA_MAP_FRAME:
z_reference_type_ = PositionReferenceType::NAV_SAT_FIX_MESSAGE;
z_reference_frame_ = "";
z_reference_property_->hide();
break;
case MapTransformType::VIA_UTM_FRAME:
z_reference_type_ = z_reference_property_->getPositionReferenceType();
z_reference_frame_ = z_reference_property_->getFrameStd();
z_reference_property_->show();
break;
}
if (!isEnabled() || (old_reference_type == z_reference_type_ && old_reference_frame == z_reference_frame_))
{
return;
}
if (z_reference_type_ != PositionReferenceType::TF_FRAME)
{
deleteStatus("Z Reference Transform");
}
transformTileToReferenceFrame();
}
void AerialMapDisplay::updateZOffset()
{
// if the Z offset changed, we don't need to do anything as the value is directly read in each update() call
z_offset_ = z_offset_property_->getFloat();
}
void AerialMapDisplay::clearAll()
{
ref_fix_ = nullptr;
center_tile_ = boost::none;
ref_coords_ = boost::none;
destroyTileObjects();
setStatus(StatusProperty::Warn, "Message", "No NavSatFix message received yet");
}
void AerialMapDisplay::destroyTileObjects()
{
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();
}
void AerialMapDisplay::createTileObjects()
{
if (not objects_.empty())
{
destroyTileObjects();
}
for (int block = 0; block < (2 * blocks_ + 1) * (2 * blocks_ + 1); ++block)
{
// generate an unique name
static size_t 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)
{
if (!ref_fix_ or !center_tile_)
{
return;
}
// update tiles, if necessary
assembleScene();
// transform scene object into fixed frame
transformMapTileToFixedFrame();
}
void AerialMapDisplay::navFixCallback(sensor_msgs::NavSatFixConstPtr const& msg)
{
// reset the periodic update timer as we got an update right now
tf_reference_update_timer_.setPeriod(tf_reference_update_duration_);
updateCenterTile(msg);
setStatus(StatusProperty::Ok, "Message", "NavSatFix message received");
}
bool AerialMapDisplay::updateCenterTile(sensor_msgs::NavSatFixConstPtr const& msg)
{
if (!isEnabled())
{
return false;
}
WGSCoordinate reference_wgs{};
// find the WGS lat/lon of the XY reference point
switch (xy_reference_type_)
{
case PositionReferenceType::NAV_SAT_FIX_MESSAGE: {
reference_wgs = { msg->latitude, msg->longitude };
break;
}
case PositionReferenceType::TF_FRAME: {
try
{
auto const tf_reference_utm = tf_buffer_->lookupTransform(utm_frame_, xy_reference_frame_, ros::Time(0));
setStatus(::rviz::StatusProperty::Ok, "XY Reference Transform", "Transform OK.");
try
{
// If UTM zone is set to be autodetected and detection has not yet been performed, do it now
if (utm_zone_ < GeographicLib::UTMUPS::MINZONE)
{
int zone;
bool northp;
double e, n;
GeographicLib::UTMUPS::Forward(msg->latitude, msg->longitude, zone, northp, e, n, utm_zone_);
utm_zone_property_->setValue(zone);
ROS_INFO("UTM zone has been automatically determined from NavSatFix message to %s.",
GeographicLib::UTMUPS::EncodeZone(zone, northp).c_str());
}
const auto& utm_coords = tf_reference_utm.transform.translation;
GeographicLib::UTMUPS::Reverse(utm_zone_, msg->latitude >= 0, utm_coords.x, utm_coords.y, reference_wgs.lat,
reference_wgs.lon, true);
setStatus(::rviz::StatusProperty::Ok, "XY reference UTM conversion", "UTM conversion OK.");
}
catch (GeographicLib::GeographicErr& err)
{
setStatus(::rviz::StatusProperty::Error, "XY reference UTM conversion", QString::fromStdString(err.what()));
ROS_ERROR_THROTTLE(2.0, "%s", err.what());
return false;
}
}
catch (tf2::TransformException const& ex)
{
setStatus(::rviz::StatusProperty::Error, "XY Reference Transform", QString::fromStdString(ex.what()));
ROS_ERROR_THROTTLE(2.0, "%s", ex.what());
return false;
}
break;
}
}
// check if update is necessary
TileCoordinate const tile_coordinates = fromWGSCoordinate<int>(reference_wgs, zoom_);
TileId const new_center_tile_id{ tile_url_, tile_coordinates, zoom_ };
bool const center_tile_changed = (!center_tile_ || !(new_center_tile_id == *center_tile_));
if (not center_tile_changed)
{
// TODO: Maybe we should update the transform here even if the center tile did not change?
// The localization might have been updated.
return false;
}
ROS_DEBUG_NAMED("rviz_satellite", "Updating center tile to (%i, %i)", tile_coordinates.x, tile_coordinates.y);
center_tile_ = new_center_tile_id;
ref_coords_ = reference_wgs;
ref_fix_ = msg;
requestTileTextures();
transformTileToReferenceFrame();
return true;
}
void AerialMapDisplay::requestTileTextures()
{
if (!isEnabled())
{
return;
}
if (tile_url_.empty())
{
setStatus(StatusProperty::Error, "TileRequest", "Tile URL is not set");
return;
}
if (not center_tile_)
{
setStatus(StatusProperty::Error, "Message", "No NavSatFix received yet");
return;
}
try
{
ROS_DEBUG_NAMED("rviz_satellite", "Requesting new tile images from the server");
tile_cache_.request({ *center_tile_, blocks_ });
triggerSceneAssembly();
}
catch (std::exception const& e)
{
setStatus(StatusProperty::Error, "TileRequest", QString(e.what()));
return;
}
}
void AerialMapDisplay::checkRequestErrorRate()
{
// the following error rate thresholds are randomly chosen
float const error_rate = tile_cache_.getTileServerErrorRate(tile_url_);
if (error_rate > 0.95)
{
setStatus(StatusProperty::Level::Error, "TileRequest", "Few or no tiles received");
}
else if (error_rate > 0.3)
{
setStatus(StatusProperty::Level::Warn, "TileRequest",
"Not all requested tiles have been received. Possibly the server is throttling?");
}
else
{
setStatus(StatusProperty::Level::Ok, "TileRequest", "OK");
}
}
void AerialMapDisplay::triggerSceneAssembly()
{
ROS_DEBUG_NAMED("rviz_satellite", "Starting to repaint all tiles");
dirty_ = true;
}
void AerialMapDisplay::assembleScene()
{
// TODO: split this function into pieces, and only call the pieces when needed
// E.g. into: grid geometry, tile geometry, material/texture (only this is asynchronous and depends on the incoming
// tiles from the cache/server)
if (!isEnabled() || !dirty_ || !center_tile_)
{
return;
}
if (objects_.empty())
{
ROS_ERROR_THROTTLE_NAMED(5, "rviz_satellite", "No objects to draw on, call createTileObjects() first!");
return;
}
dirty_ = false;
Area area(*center_tile_, blocks_);
TileCacheGuard guard(tile_cache_);
bool all_tiles_loaded = true;
auto it = objects_.begin();
for (int xx = area.left_top.x; xx <= area.right_bottom.x; ++xx)
{
for (int yy = area.left_top.y; yy <= area.right_bottom.y; ++yy)
{
auto obj = it->object;
auto& material = it->material;
assert(!material.isNull());
++it;
TileId const to_find{ center_tile_->tile_server, { xx, yy }, center_tile_->zoom };
OgreTile const* tile = tile_cache_.ready(to_find);
if (!tile)
{
// don't show tiles with old textures
obj->setVisible(false);
all_tiles_loaded = 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(ref_coords_->lat, zoom_);
// Note: In the following we will do two things:
//
// * We flip the position's y coordinate.
// * We flip the texture's v coordinate.
//
// For more explanation see the function transformAerialMap()
// The center tile has the coordinates left-bot = (0,0) and right-top = (1,1) in the AerialMap frame.
double const x = (xx - center_tile_->coord.x) * tile_w_h_m;
// flip the y coordinate because we need to flip the tiles to align the tile's frame with the ENU "map" frame
double const y = -(yy - center_tile_->coord.y) * tile_w_h_m;
// create a quad for this tile
// note: We have to recreate the vertices 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);
// We assign the Ogre texture coordinates in a way so that we flip the
// texture along the v coordinate. For example, we assign the bottom left
//
// Note that the Ogre texture coordinate system is: (0,0) = top left of the loaded image and (1,1) = bottom right
// of the loaded image
// 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 yet, this function has to be called again
if (!all_tiles_loaded)
{
dirty_ = true;
}
else
{
ROS_DEBUG_NAMED("rviz_satellite", "Finished assembling all tiles");
}
tile_cache_.purge({ *center_tile_, blocks_ });
checkRequestErrorRate();
}
void AerialMapDisplay::transformTileToReferenceFrame()
{
switch (map_transform_type_)
{
case MapTransformType::VIA_MAP_FRAME:
transformTileToMapFrame();
break;
case MapTransformType::VIA_UTM_FRAME:
transformTileToUtmFrame();
break;
}
}
void AerialMapDisplay::transformTileToMapFrame()
{
if (!ref_fix_ or !center_tile_)
{
ROS_FATAL_THROTTLE_NAMED(2, "rviz_satellite", "ref_fix_ not set, can't create transforms");
return;
}
// We will use three frames in this function:
//