forked from gazebosim/gz-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualizationCapabilities.cc
2920 lines (2537 loc) · 89.9 KB
/
VisualizationCapabilities.cc
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 (C) 2021 Open Source Robotics Foundation
*
* 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 "VisualizationCapabilities.hh"
#include <gz/msgs/boolean.pb.h>
#include <gz/msgs/stringmsg.pb.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <QQmlProperty>
#include <gz/common/Console.hh>
#include <gz/common/geospatial/Dem.hh>
#include <gz/common/geospatial/HeightmapData.hh>
#include <gz/common/geospatial/ImageHeightmap.hh>
#include <gz/common/MeshManager.hh>
#include <gz/common/Profiler.hh>
#include <gz/common/StringUtils.hh>
#include <gz/common/Uuid.hh>
#include <gz/gui/Application.hh>
#include <gz/gui/GuiEvents.hh>
#include <gz/gui/Helpers.hh>
#include <gz/gui/MainWindow.hh>
#include <gz/plugin/Register.hh>
#include "gz/rendering/AxisVisual.hh"
#include "gz/rendering/Capsule.hh"
#include <gz/rendering/COMVisual.hh>
#include <gz/rendering/Heightmap.hh>
#include <gz/rendering/InertiaVisual.hh>
#include <gz/rendering/JointVisual.hh>
#include <gz/rendering/Visual.hh>
#include <gz/rendering/RenderingIface.hh>
#include <gz/rendering/Scene.hh>
#include <gz/rendering/Text.hh>
#include <gz/rendering/WireBox.hh>
#include <gz/transport/Node.hh>
#include <sdf/Capsule.hh>
#include <sdf/Cone.hh>
#include <sdf/Ellipsoid.hh>
#include <sdf/Joint.hh>
#include <sdf/Heightmap.hh>
#include <sdf/Mesh.hh>
#include <sdf/Pbr.hh>
#include <sdf/Polyline.hh>
#include <sdf/Root.hh>
#include "gz/sim/components/CastShadows.hh"
#include "gz/sim/components/ChildLinkName.hh"
#include "gz/sim/components/Collision.hh"
#include "gz/sim/components/Geometry.hh"
#include "gz/sim/components/Inertial.hh"
#include "gz/sim/components/Joint.hh"
#include "gz/sim/components/JointAxis.hh"
#include "gz/sim/components/JointType.hh"
#include "gz/sim/components/Link.hh"
#include "gz/sim/components/Model.hh"
#include "gz/sim/components/Name.hh"
#include "gz/sim/components/ParentEntity.hh"
#include "gz/sim/components/ParentLinkName.hh"
#include "gz/sim/components/Pose.hh"
#include "gz/sim/components/Scene.hh"
#include "gz/sim/components/Transparency.hh"
#include "gz/sim/components/Visibility.hh"
#include "gz/sim/components/Visual.hh"
#include "gz/sim/components/World.hh"
#include "gz/sim/Util.hh"
#include "gz/sim/rendering/RenderUtil.hh"
#include "gz/sim/rendering/SceneManager.hh"
namespace gz::sim
{
class VisualizationCapabilitiesPrivate
{
/// \brief Update the 3D scene.
public: void OnRender();
/// \brief Helper function to get all child links of a model entity.
/// \param[in] _entity Entity to find child links
/// \return Vector of child links found for the parent entity
public: std::vector<gz::sim::Entity>
FindChildLinks(const gz::sim::Entity &_entity);
/// \brief Helper function to get all children of an entity that have a
/// pose.
/// \param[in] _entity Entity to find children
/// \return Vector of children found for the parent entity
public: std::unordered_set<gz::sim::Entity>
FindChildFrames(const gz::sim::Entity &_entity);
/// \brief Finds the links (collision parent) that are used to create child
/// collision visuals in RenderUtil::Update
/// \param[in] _ecm The entity-component manager
public: void FindCollisionLinks(const EntityComponentManager &_ecm);
/// \brief Finds the child links for given entity from the ECM
/// \param[in] _ecm The entity-component manager
/// \param[in] _entity Entity to find child links
/// \return A vector of child links found for the entity
public: std::vector<gz::sim::Entity> FindChildLinksFromECM(
const gz::sim::EntityComponentManager &_ecm,
const gz::sim::Entity &_entity);
/// \brief Finds the links (visual parent) that are used to toggle wireframe
/// and transparent view for visuals in RenderUtil::Update
/// \param[in] _ecm The entity-component manager
public: void PopulateViewModeVisualLinks(
const gz::sim::EntityComponentManager &_ecm);
/// \brief Finds the links (inertial parent) that are used to create child
/// inertia and center of mass visuals in RenderUtil::Update
/// \param[in] _ecm The entity-component manager
public: void FindInertialLinks(const EntityComponentManager &_ecm);
/// \brief Retrieve visual based on its Gazebo entity. Note that this is
/// different from gz-rendering's internal ID for the visual.
/// \param[in] _entity Gazebo entity
/// \return Pointer to requested visual, null if not found.
public: rendering::VisualPtr VisualByEntity(Entity _entity);
/// \brief Create a collision visual from an SDF visual element.
/// \param[in] _id Entity which the visual corresponds to
/// \param[in] _visual SDF describing the visual.
/// \param[in] _parent Parent link's visual
/// \return Pointer to created visual
public: rendering::VisualPtr CreateCollisionVisual(
gz::sim::Entity _id,
const sdf::Visual &_visual,
rendering::VisualPtr &_parent);
/// \brief Create a geometry from an SDF element.
/// \param[in] _geom SDF describing the geometry.
/// \param[out] _scale Geometry's scale
/// \param[out] _localPose Geometry's local pose
/// \return Pointer to created geometry
public: rendering::GeometryPtr CreateGeometry(
const sdf::Geometry &_geom, math::Vector3d &_scale,
math::Pose3d &_localPose);
/// \brief Create a material from an SDF element.
/// \param[in] _material SDF describing the material.
/// \return Pointer to created material
public: rendering::MaterialPtr CreateMaterial(
const sdf::Material &_material);
/////////////////////////////////////////////////
// Transparent
/////////////////////////////////////////////////
/// \brief View an entity as transparent
/// \param[in] _entity Entity to view as transparent
public: void ViewTransparent(const gz::sim::Entity &_entity);
/// \brief Callback for view as transparent request
/// \param[in] _msg Request message to set the target to view as
/// transparent
/// \param[in] _res Response data
/// \return True if the request is received
public: bool OnViewTransparent(const msgs::StringMsg &_msg,
msgs::Boolean &_res);
/////////////////////////////////////////////////
// Wireframes
/////////////////////////////////////////////////
/// \brief View wireframes of specified entity
/// \param[in] _entity Entity to view wireframes
public: void ViewWireframes(const gz::sim::Entity &_entity);
/// \brief Callback for view wireframes request
/// \param[in] _msg Request message to set the target to view wireframes
/// \param[in] _res Response data
/// \return True if the request is received
public: bool OnViewWireframes(const msgs::StringMsg &_msg,
msgs::Boolean &_res);
/////////////////////////////////////////////////
// Collision
/////////////////////////////////////////////////
/// \brief View collisions of specified entity which are shown in orange
/// \param[in] _entity Entity to view collisions
public: void ViewCollisions(const Entity &_entity);
/// \brief Callback for view collisions request
/// \param[in] _msg Request message to set the target to view collisions
/// \param[in] _res Response data
/// \return True if the request is received
public: bool OnViewCollisions(const msgs::StringMsg &_msg,
msgs::Boolean &_res);
/// \brief Create the collision visual
/// \param[in] _id Collision entity
/// \param[in] _collision SDF description of collision
/// \param[in] _parent Parent link's visual
public: rendering::VisualPtr CreateCollision(
gz::sim::Entity _id,
const sdf::Collision &_collision,
gz::rendering::VisualPtr &_parent);
/////////////////////////////////////////////////
// COM
/////////////////////////////////////////////////
/// \brief View center of mass of specified entity
/// \param[in] _entity Entity to view center of mass
public: void ViewCOM(const Entity &_entity);
/// \brief Callback for view center of mass request
/// \param[in] _msg Request message to set the target to view center of
/// mass
/// \param[in] _res Response data
/// \return True if the request is received
public: bool OnViewCOM(const msgs::StringMsg &_msg,
msgs::Boolean &_res);
/// \brief Create a center of mass visual
/// \param[in] _id Unique visual id
/// \param[in] _inertial Inertial component of the link
/// \param[in] _parent Visual parent
/// \return Visual (center of mass) object created from the inertial
public: gz::rendering::VisualPtr CreateCOMVisual(
gz::sim::Entity _id,
const math::Inertiald &_inertia,
gz::rendering::VisualPtr &_parent);
/////////////////////////////////////////////////
// Inertia
/////////////////////////////////////////////////
/// \brief View inertia of specified entity
/// \param[in] _entity Entity to view inertia
public: void ViewInertia(const Entity &_entity);
/// \brief Callback for view inertia request
/// \param[in] _msg Request message to set the target to view inertia
/// \param[in] _res Response data
/// \return True if the request is received
public: bool OnViewInertia(const msgs::StringMsg &_msg,
msgs::Boolean &_res);
/// \brief Create an inertia visual
/// \param[in] _id Unique visual id
/// \param[in] _inertial Inertial component of the link
/// \param[in] _parent Visual parent
/// \return Visual (center of mass) object created from the inertial
public: gz::rendering::VisualPtr CreateInertiaVisual(
gz::sim::Entity _id,
const math::Inertiald &_inertia,
gz::rendering::VisualPtr &_parent);
/////////////////////////////////////////////////
// Joints
/////////////////////////////////////////////////
/// \brief Callback for view joints request
/// \param[in] _msg Request message to set the target to view center of
/// mass
/// \param[in] _res Response data
/// \return True if the request is received
public: bool OnViewJoints(const msgs::StringMsg &_msg,
msgs::Boolean &_res);
/// \brief View joints of specified entity
/// \param[in] _entity Entity to view joints
public: void ViewJoints(const Entity &_entity);
/// \brief Create a joint visual
/// \param[in] _id Unique visual id
/// \param[in] _joint Joint sdf dom
/// \param[in] _childId Joint child id
/// \param[in] _parentId Joint parent id
/// \return Visual (joint) object created from the sdf dom
public: rendering::VisualPtr CreateJointVisual(Entity _id,
const sdf::Joint &_joint, Entity _childId = 0,
Entity _parentId = 0);
/// \brief Updates the world pose of joint parent visual
/// according to its child.
/// \param[in] _jointId Joint visual id.
public: void UpdateJointParentPose(Entity _jointId);
/////////////////////////////////////////////////
// Frames
/////////////////////////////////////////////////
/// \brief View frame of specified entity
/// \param[in] _entity Entity to view frame
public: void ViewFrames(const Entity &_entity);
/// \brief Callback for view frame request
/// \param[in] _msg Request message to set the target to view frame
/// \param[in] _res Response data
/// \return True if the request is received
public: bool OnViewFrames(const msgs::StringMsg &_msg,
msgs::Boolean &_res);
/// \brief Create a frame visual
/// \param[in] _id Unique visual id to be used internally by gz-rendering.
/// This is NOT a Gazebo Entity ID.
/// \param[in] _parent Visual parent
/// \return Visual (frame) object created
public: rendering::VisualPtr CreateFrameVisual(
unsigned int _id,
rendering::VisualPtr &_parent);
/////////////////////////////////////////////////
/// \brief Gazebo communication node.
public: transport::Node node;
/// \brief Keep track of world ID, which is equivalent to the scene's
/// root visual.
/// Defaults to kNullEntity.
public: Entity worldId{kNullEntity};
/// \brief Pointer to the rendering scene
public: gz::rendering::ScenePtr scene{nullptr};
/// \brief Scene manager
public: gz::sim::SceneManager sceneManager;
/// True if the rendering component is initialized
public: bool initialized = false;
/// \brief A map of model entities and their corresponding children links
public: std::map<Entity, std::vector<Entity>> modelToLinkEntities;
/// \brief A map of model entities and their corresponding children models
public: std::map<Entity, std::vector<Entity>> modelToModelEntities;
/// \brief New wireframe visuals to be toggled
public: std::vector<gz::sim::Entity> newTransparentEntities;
/// \brief A map of link entities and their corresponding children visuals
public: std::map<gz::sim::Entity,
std::vector<gz::sim::Entity>> linkToVisualEntities;
/// \brief Map of visual entity in Gazebo to visual pointers.
public: std::map<Entity, rendering::VisualPtr> visuals;
/////////////////////////////////////////////////
// Transparent
/////////////////////////////////////////////////
/// \brief Target to view as transparent
public: std::string viewTransparentTarget;
/// \brief A list of links used to toggle transparent mode for visuals
public: std::vector<Entity> newTransparentVisualLinks;
/// \brief A map of created transparent visuals and if they are currently
/// visible
public: std::map<gz::sim::Entity, bool> viewingTransparent;
/// \brief View transparent service
public: std::string viewTransparentService;
/////////////////////////////////////////////////
// Wireframes
/////////////////////////////////////////////////
/// \brief Target to view wireframes
public: std::string viewWireframesTarget;
/// \brief New wireframe visuals to be toggled
public: std::vector<Entity> newWireframes;
/// \brief A map of created wireframe visuals and if they are currently
/// visible
public: std::map<Entity, bool> viewingWireframes;
/// \brief A list of links used to toggle wireframe mode for visuals
public: std::vector<Entity> newWireframeVisualLinks;
/// \brief View transparent service
public: std::string viewWireframesService;
/////////////////////////////////////////////////
// COM
/////////////////////////////////////////////////
/// \brief New center of mass visuals to be created
public: std::vector<Entity> newCOMVisuals;
/// \brief A list of links used to create new center of mass visuals
public: std::vector<Entity> newCOMLinks;
/// \brief A map of link entities and if their center of mass visuals
/// are currently visible
public: std::map<Entity, bool> viewingCOM;
/// \brief A map of links and their center of mass visuals
public: std::map<Entity, Entity> linkToCOMVisuals;
/// \brief Target to view center of mass
public: std::string viewCOMTarget;
/// \brief View center of mass service
public: std::string viewCOMService;
/////////////////////////////////////////////////
// Inertia
/////////////////////////////////////////////////
/// \brief View inertia service
public: std::string viewInertiaService;
/// \brief A map of entity ids and their inertials
public: std::map<Entity, math::Inertiald> entityInertials;
/// \brief A map of links and their inertia visuals
public: std::map<Entity, Entity> linkToInertiaVisuals;
/// \brief A map of link entities and if their inertias are currently
/// visible
public: std::map<Entity, bool> viewingInertias;
/// \brief A list of links used to create new inertia visuals
public: std::vector<Entity> newInertiaLinks;
/// \brief New inertias to be created
public: std::vector<Entity> newInertias;
/// \brief Target to view inertia
public: std::string viewInertiaTarget;
/////////////////////////////////////////////////
// Collision
/////////////////////////////////////////////////
/// \brief Target to view collisions
public: std::string viewCollisionsTarget;
/// \brief View collisions service
public: std::string viewCollisionsService;
/// \brief New collisions to be created
public: std::vector<Entity> newCollisions;
/// \brief A list of links used to create new collision visuals
public: std::vector<Entity> newCollisionLinks;
/// \brief A map of collision entity ids and their SDF DOM
public: std::map<Entity, sdf::Collision> entityCollisions;
/// \brief A map of link entities and their corresponding children
/// collisions
public: std::map<Entity, std::vector<Entity>> linkToCollisionEntities;
/// \brief A map of created collision entities and if they are currently
/// visible
public: std::map<Entity, bool> viewingCollisions;
/////////////////////////////////////////////////
// Joints
/////////////////////////////////////////////////
/// \brief View joints service name
public: std::string viewJointsService;
/// \brief Target to view joints
public: std::string viewJointsTarget;
/// \brief New joint visuals to be created
public: std::vector<Entity> newJoints;
/// \brief Finds the models (joint parent) that are used to create
/// joint visuals in RenderUtil::Update
/// \param[in] _ecm The entity-component manager
public: void FindJointModels(const EntityComponentManager &_ecm);
/// \brief A list of models used to create new joint visuals
public: std::vector<Entity> newJointModels;
/// \brief A map of joint entity ids and their SDF DOM
public: std::map<Entity, sdf::Joint> entityJoints;
/// \brief A map of model entities and their corresponding children links
public: std::map<Entity, std::vector<Entity>> modelToJointEntities;
/// \brief A map of created joint entities and if they are currently
/// visible
public: std::map<Entity, bool> viewingJoints;
/// \brief A list of joint visuals for which the parent visual poses
/// have to be updated.
public: std::vector<Entity> updateJointParentPoses;
/// \brief A map of models entities and link attributes used
/// to create joint visuals
public: std::map<Entity, std::map<std::string, Entity>>
matchLinksWithEntities;
/////////////////////////////////////////////////
// Frame
/////////////////////////////////////////////////
/// \brief A list of entities that need frame visuals. Once the frame visual
/// is created, the entity is removed from the list.
public: std::vector<Entity> newFrameEntities;
/// \brief Entities that have a pose. The key is the entity, the value its
/// parent entity. Note that not all entities with pose will have frames
/// displayed at the moment.
public: std::unordered_map<Entity, Entity> entitiesWithPose;
/// \brief A map of entities and whether their frame visuals
/// are currently visible
public: std::map<Entity, bool> viewingFrames;
/// \brief A map of entities and the gz-rendering ID of their frame visuals
public: std::map<Entity, unsigned int> entityToFrameVisuals;
/// \brief Target to view frame
public: std::string viewFramesTarget;
/// \brief View frame service
public: std::string viewFramesService;
};
}
using namespace gz;
using namespace sim;
/////////////////////////////////////////////////
void VisualizationCapabilitiesPrivate::OnRender()
{
if (nullptr == this->scene)
{
this->scene = rendering::sceneFromFirstRenderEngine();
if (nullptr == this->scene)
{
return;
}
this->sceneManager.SetScene(this->scene);
}
// create new wireframe visuals
for (const auto &link : this->newWireframeVisualLinks)
{
std::vector<Entity> visEntities =
this->linkToVisualEntities[link];
for (const auto &visEntity : visEntities)
{
if (!this->viewingWireframes[visEntity])
{
auto wireframeVisual = this->VisualByEntity(visEntity);
if (wireframeVisual)
{
wireframeVisual->SetWireframe(true);
this->viewingWireframes[visEntity] = true;
}
}
}
}
this->newWireframeVisualLinks.clear();
// update joint parent visual poses
{
for (const auto &jointEntity : this->updateJointParentPoses)
{
this->UpdateJointParentPose(jointEntity);
}
}
this->updateJointParentPoses.clear();
// create new transparent visuals
for (const auto &link : this->newTransparentVisualLinks)
{
std::vector<Entity> visEntities =
this->linkToVisualEntities[link];
for (const auto &visEntity : visEntities)
{
if (!this->viewingTransparent[visEntity])
{
auto transparencyVisual = this->VisualByEntity(visEntity);
if (transparencyVisual)
{
this->sceneManager.UpdateTransparency(transparencyVisual,
true /* transparent */);
this->viewingTransparent[visEntity] = true;
}
}
}
}
this->newTransparentVisualLinks.clear();
// create new inertia visuals
for (const auto &link : this->newInertiaLinks)
{
// create a new id for the inertia visual
auto attempts = 100000u;
for (auto i = 0u; i < attempts; ++i)
{
Entity id = i;
if (!this->scene->HasNodeId(id) && !this->scene->HasLightId(id) &&
!this->scene->HasSensorId(id) && !this->scene->HasVisualId(id) &&
!this->viewingInertias[link])
{
auto existsVisual = this->VisualByEntity(id);
auto parentInertiaVisual = this->VisualByEntity(link);
if (existsVisual == nullptr && parentInertiaVisual != nullptr)
{
this->CreateInertiaVisual(id, this->entityInertials[link],
parentInertiaVisual);
}
else
{
continue;
}
this->viewingInertias[link] = true;
this->linkToInertiaVisuals[link] = id;
break;
}
}
}
this->newInertiaLinks.clear();
// create new joint visuals
{
for (const auto &model : this->newJointModels)
{
std::vector<Entity> jointEntities =
this->modelToJointEntities[model];
for (const auto &jointEntity : jointEntities)
{
if (!this->scene->HasNodeId(jointEntity) &&
!this->scene->HasLightId(jointEntity) &&
!this->scene->HasSensorId(jointEntity) &&
!this->scene->HasVisualId(jointEntity) &&
!this->viewingInertias[jointEntity])
{
std::string childLinkName =
this->entityJoints[jointEntity].ChildName();
Entity childId =
this->matchLinksWithEntities[model][childLinkName];
std::string parentLinkName =
this->entityJoints[jointEntity].ParentName();
Entity parentId =
this->matchLinksWithEntities[model][parentLinkName];
auto joint = this->entityJoints[jointEntity];
auto vis = this->CreateJointVisual(
jointEntity, joint, childId, parentId);
this->viewingJoints[jointEntity] = true;
// Update joint parent visual pose
if (joint.Axis(1))
{
this->updateJointParentPoses.push_back(jointEntity);
}
}
}
}
}
this->newJointModels.clear();
// create new center of mass visuals
for (const auto &link : this->newCOMLinks)
{
// create a new id for the center of mass visual
auto attempts = 100000u;
for (auto i = 0u; i < attempts; ++i)
{
Entity id = i;
if (!this->scene->HasNodeId(id) && !this->scene->HasLightId(id) &&
!this->scene->HasSensorId(id) && !this->scene->HasVisualId(id) &&
!this->viewingCOM[link])
{
auto existsVisual = this->VisualByEntity(id);
auto parentInertiaVisual = this->VisualByEntity(link);
if (existsVisual == nullptr && parentInertiaVisual != nullptr)
{
this->CreateCOMVisual(id, this->entityInertials[link],
parentInertiaVisual);
}
else
{
continue;
}
this->viewingCOM[link] = true;
this->linkToCOMVisuals[link] = id;
break;
}
}
}
this->newCOMLinks.clear();
// create new collision visuals
for (const auto &link : this->newCollisionLinks)
{
std::vector<Entity> colEntities =
this->linkToCollisionEntities[link];
for (const auto &colEntity : colEntities)
{
if (!this->scene->HasNodeId(colEntity) &&
!this->scene->HasLightId(colEntity) &&
!this->scene->HasSensorId(colEntity) &&
!this->scene->HasVisualId(colEntity) &&
!this->viewingCollisions[link])
{
auto parentCollisionVisual = this->VisualByEntity(link);
if (parentCollisionVisual != nullptr)
{
auto vis = this->CreateCollision(
colEntity,
this->entityCollisions[colEntity],
parentCollisionVisual);
if (vis == nullptr)
{
continue;
}
vis->SetUserData("gui-only", static_cast<bool>(true));
this->viewingCollisions[colEntity] = true;
// add geometry material to originalEmissive map
for (auto g = 0u; g < vis->GeometryCount(); ++g)
{
auto geom = vis->GeometryByIndex(g);
// Geometry material
auto geomMat = geom->Material();
if (nullptr == geomMat)
continue;
}
}
else
{
continue;
}
}
}
}
this->newCollisionLinks.clear();
// create new frame visuals
for (const auto &entity : this->newFrameEntities)
{
if (this->viewingFrames[entity])
continue;
auto parentVisual = this->VisualByEntity(entity);
if (parentVisual == nullptr)
{
// Entities without specific visuals, like collisions and sensors,
// aren't supported yet.
continue;
}
// create a new id for the visual
auto attempts = 100000u;
for (Entity id = 0u; id < attempts; ++id)
{
if (this->scene->HasNodeId(id) || this->scene->HasLightId(id) ||
this->scene->HasSensorId(id) || this->scene->HasVisualId(id))
{
continue;
}
this->CreateFrameVisual(id, parentVisual);
this->viewingFrames[entity] = true;
this->entityToFrameVisuals[entity] = id;
break;
}
}
this->newFrameEntities.clear();
// View center of mass
{
GZ_PROFILE("VisualizationCapabilitiesPrivate::OnRender ViewCOM");
if (!this->viewCOMTarget.empty())
{
rendering::NodePtr targetNode =
scene->NodeByName(this->viewCOMTarget);
auto targetVis = std::dynamic_pointer_cast<rendering::Visual>(targetNode);
if (targetVis && targetVis->HasUserData("gazebo-entity"))
{
Entity targetEntity =
std::get<uint64_t>(targetVis->UserData("gazebo-entity"));
this->ViewCOM(targetEntity);
}
else
{
gzerr << "Unable to find node name ["
<< this->viewCOMTarget
<< "] to view center of mass" << std::endl;
}
this->viewCOMTarget.clear();
}
}
// View inertia
{
GZ_PROFILE("VisualizationCapabilitiesPrivate::OnRender ViewInertia");
if (!this->viewInertiaTarget.empty())
{
rendering::NodePtr targetNode =
scene->NodeByName(this->viewInertiaTarget);
auto targetVis = std::dynamic_pointer_cast<rendering::Visual>(targetNode);
if (targetVis && targetVis->HasUserData("gazebo-entity"))
{
Entity targetEntity =
std::get<uint64_t>(targetVis->UserData("gazebo-entity"));
this->ViewInertia(targetEntity);
}
else
{
gzerr << "Unable to find node name ["
<< this->viewInertiaTarget
<< "] to view inertia" << std::endl;
}
this->viewInertiaTarget.clear();
}
}
// view Transparent
{
GZ_PROFILE("VisualizationCapabilitiesPrivate::OnRender ViewTransparent");
if (!this->viewTransparentTarget.empty())
{
rendering::NodePtr targetNode =
this->scene->VisualByName(this->viewTransparentTarget);
auto targetVis = std::dynamic_pointer_cast<rendering::Visual>(targetNode);
if (targetVis && targetVis->HasUserData("gazebo-entity"))
{
Entity targetEntity =
std::get<uint64_t>(targetVis->UserData("gazebo-entity"));
this->ViewTransparent(targetEntity);
}
else
{
gzerr << "Unable to find node name ["
<< this->viewTransparentTarget
<< "] to view as transparent" << std::endl;
}
this->viewTransparentTarget.clear();
}
}
// View collisions
{
GZ_PROFILE("VisualizationCapabilitiesPrivate::OnRender ViewCollisions");
if (!this->viewCollisionsTarget.empty())
{
rendering::NodePtr targetNode =
scene->NodeByName(this->viewCollisionsTarget);
auto targetVis = std::dynamic_pointer_cast<rendering::Visual>(targetNode);
if (targetVis && targetVis->HasUserData("gazebo-entity"))
{
Entity targetEntity =
std::get<uint64_t>(targetVis->UserData("gazebo-entity"));
this->ViewCollisions(targetEntity);
}
else
{
gzerr << "Unable to find node name ["
<< this->viewCollisionsTarget
<< "] to view collisions" << std::endl;
}
this->viewCollisionsTarget.clear();
}
}
// View joints
{
GZ_PROFILE("VisualizationCapabilitiesPrivate::OnRender ViewJoints");
if (!this->viewJointsTarget.empty())
{
rendering::NodePtr targetNode =
scene->NodeByName(this->viewJointsTarget);
auto targetVis = std::dynamic_pointer_cast<rendering::Visual>(targetNode);
if (targetVis && targetVis->HasUserData("gazebo-entity"))
{
Entity targetEntity =
std::get<uint64_t>(targetVis->UserData("gazebo-entity"));
this->ViewJoints(targetEntity);
}
else
{
gzerr << "Unable to find node name ["
<< this->viewJointsTarget
<< "] to view joints" << std::endl;
}
this->viewJointsTarget.clear();
}
}
// View wireframes
{
GZ_PROFILE("VisualizationCapabilitiesPrivate::OnRender ViewWireframes");
if (!this->viewWireframesTarget.empty())
{
rendering::NodePtr targetNode =
scene->NodeByName(this->viewWireframesTarget);
auto targetVis = std::dynamic_pointer_cast<rendering::Visual>(targetNode);
if (targetVis && targetVis->HasUserData("gazebo-entity"))
{
Entity targetEntity =
std::get<uint64_t>(targetVis->UserData("gazebo-entity"));
this->ViewWireframes(targetEntity);
}
else
{
gzerr << "Unable to find node name ["
<< this->viewWireframesTarget
<< "] to view wireframes" << std::endl;
}
this->viewWireframesTarget.clear();
}
}
// View frames
{
GZ_PROFILE("VisualizationCapabilitiesPrivate::OnRender ViewFrames");
if (!this->viewFramesTarget.empty())
{
auto targetNode = this->scene->NodeByName(this->viewFramesTarget);
auto targetVis = std::dynamic_pointer_cast<rendering::Visual>(targetNode);
if (targetVis && targetVis->HasUserData("gazebo-entity"))
{
Entity targetEntity =
std::get<uint64_t>(targetVis->UserData("gazebo-entity"));
this->ViewFrames(targetEntity);
}
else
{
gzerr << "Unable to find node name ["
<< this->viewFramesTarget
<< "] to view frame" << std::endl;
}
this->viewFramesTarget.clear();
}
}
}
/////////////////////////////////////////////////
rendering::VisualPtr VisualizationCapabilitiesPrivate::CreateJointVisual(
Entity _id, const sdf::Joint &_joint,
Entity _childId, Entity _parentId)
{
if (!this->scene)
{
return rendering::VisualPtr();
}
if (this->visuals.find(_id) != this->visuals.end())
{
return rendering::VisualPtr();
}
rendering::VisualPtr parent;
if (_childId != this->worldId)
{
parent = this->VisualByEntity(_childId);