Gazebo Gazebo

API Reference

6.16.0
Scene3D.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef GZ_GAZEBO_GUI_SCENE3D_HH_
19 #define GZ_GAZEBO_GUI_SCENE3D_HH_
20 
21 #include <gz/msgs/boolean.pb.h>
22 #include <gz/msgs/gui_camera.pb.h>
23 #include <gz/msgs/stringmsg.pb.h>
24 #include <gz/msgs/vector3d.pb.h>
25 #include <gz/msgs/video_record.pb.h>
26 
27 #include <string>
28 #include <memory>
29 #include <mutex>
30 
31 #include <sdf/Root.hh>
32 
33 #include <gz/math/Color.hh>
34 #include <gz/math/Pose3.hh>
35 #include <gz/math/Vector2.hh>
36 #include <gz/math/Vector3.hh>
37 
38 #include <gz/common/MouseEvent.hh>
39 #include <gz/common/KeyEvent.hh>
40 
41 #include <gz/rendering/Camera.hh>
42 
43 #include <gz/sim/gui/GuiSystem.hh>
44 
45 #include "gz/gui/qt.h"
46 
47 
48 namespace ignition
49 {
50 namespace gazebo
51 {
52 // Inline bracket to help doxygen filtering.
53 inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
54  class IgnRendererPrivate;
55  class RenderWindowItemPrivate;
56  class Scene3DPrivate;
57  class RenderUtil;
58 
83  {
84  Q_OBJECT
85 
87  Q_PROPERTY(
88  QString errorPopupText
89  READ ErrorPopupText
90  WRITE SetErrorPopupText
91  NOTIFY ErrorPopupTextChanged
92  )
93 
94 
95  Q_PROPERTY(
96  QString loadingError
97  READ LoadingError
98  WRITE SetLoadingError
99  NOTIFY LoadingErrorChanged
100  )
101 
103  public: Scene3D();
104 
106  public: ~Scene3D() override;
107 
108  // Documentation inherited
109  public: void LoadConfig(const tinyxml2::XMLElement *_pluginElem) override;
110 
111  // Documentation inherited
112  public: void Update(const UpdateInfo &_info,
113  EntityComponentManager &_ecm) override;
114 
119  public slots: void OnDropped(const QString &_drop,
120  int _mouseX, int _mouseY);
121 
125  public slots: void OnHovered(int _mouseX, int _mouseY);
126 
129  public slots: void OnFocusWindow();
130 
131  // Documentation inherited
132  protected: bool eventFilter(QObject *_obj, QEvent *_event) override;
133 
138  private: bool OnTransformMode(const msgs::StringMsg &_msg,
139  msgs::Boolean &_res);
140 
145  private: bool OnRecordVideo(const msgs::VideoRecord &_msg,
146  msgs::Boolean &_res);
147 
152  private: bool OnMoveTo(const msgs::StringMsg &_msg,
153  msgs::Boolean &_res);
154 
159  private: bool OnFollow(const msgs::StringMsg &_msg,
160  msgs::Boolean &_res);
161 
166  private: bool OnFollowOffset(const msgs::Vector3d &_msg,
167  msgs::Boolean &_res);
168 
173  private: bool OnViewAngle(const msgs::Vector3d &_msg,
174  msgs::Boolean &_res);
175 
180  private: bool OnMoveToPose(const msgs::GUICamera &_msg,
181  msgs::Boolean &_res);
182 
188  private: bool OnViewTransparent(const msgs::StringMsg &_msg,
189  msgs::Boolean &_res);
190 
196  private: bool OnViewCOM(const msgs::StringMsg &_msg,
197  msgs::Boolean &_res);
198 
203  private: bool OnViewInertia(const msgs::StringMsg &_msg,
204  msgs::Boolean &_res);
205 
210  private: bool OnViewJoints(const msgs::StringMsg &_msg,
211  msgs::Boolean &_res);
212 
217  private: bool OnViewWireframes(const msgs::StringMsg &_msg,
218  msgs::Boolean &_res);
219 
224  private: bool OnViewCollisions(const msgs::StringMsg &_msg,
225  msgs::Boolean &_res);
226 
229  public: Q_INVOKABLE QString ErrorPopupText() const;
230 
233  public: Q_INVOKABLE void SetErrorPopupText(const QString &_errorTxt);
234 
236  signals: void ErrorPopupTextChanged();
237 
241  signals: void popupError();
242 
247  private: bool OnViewControl(const msgs::StringMsg &_msg,
248  msgs::Boolean &_res);
249 
252  public: Q_INVOKABLE QString LoadingError() const;
253 
256  public: Q_INVOKABLE void SetLoadingError(const QString &_loadingError);
257 
259  signals: void LoadingErrorChanged();
260 
262  public: QString loadingError;
263 
266  private: std::unique_ptr<Scene3DPrivate> dataPtr;
267  };
268 
269  class RenderSync;
270 
277  class IgnRenderer : public QObject
278  {
279  Q_OBJECT
280 
282  public: IgnRenderer();
283 
285  public: ~IgnRenderer() override;
286 
290  public: void Render(RenderSync *_renderSync);
291 
295  public: std::string Initialize();
296 
298  public: void Destroy();
299 
301  public: class RenderUtil *RenderUtil() const;
302 
305  public: void SetTransformMode(const std::string &_mode);
306 
309  public: void SetModel(const std::string &_model);
310 
313  public: void SetModelPath(const std::string &_filePath);
314 
318  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
319 
324  public: void SetRecordVideo(bool _record, const std::string &_format,
325  const std::string &_savePath);
326 
329  public: void SetRecordVideoUseSimTime(bool _useSimTime);
330 
333  public: void SetRecordVideoLockstep(bool _lockstep);
334 
337  public: void SetRecordVideoBitrate(unsigned int _bitrate);
338 
341  public: void SetMoveTo(const std::string &_target);
342 
348  public: void SetFollowTarget(const std::string &_target,
349  bool _waitForTarget = false);
350 
355  public: void SetViewAngle(const math::Vector3d &_direction);
356 
359  public: void SetMoveToPose(const math::Pose3d &_pose);
360 
363  public: void SetViewTransparentTarget(const std::string &_target);
364 
367  public: void SetViewCOMTarget(const std::string &_target);
368 
371  public: void SetViewInertiaTarget(const std::string &_target);
372 
375  public: void SetViewJointsTarget(const std::string &_target);
376 
379  public: void SetViewWireframesTarget(const std::string &_target);
380 
383  public: void SetViewCollisionsTarget(const std::string &_target);
384 
387  public: void SetViewController(const std::string &_viewController);
388 
391  public: void SetFollowPGain(double _gain);
392 
396  public: void SetFollowWorldFrame(bool _worldFrame);
397 
400  public: void SetFollowOffset(const math::Vector3d &_offset);
401 
404  public: std::string FollowTarget() const;
405 
408  public: bool FollowWorldFrame() const;
409 
412  public: math::Vector3d FollowOffset() const;
413 
416  public: void SetInitCameraPose(const math::Pose3d &_pose);
417 
421  public: void NewMouseEvent(const common::MouseEvent &_e,
422  const math::Vector2d &_drag = math::Vector2d::Zero);
423 
426  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
427 
430  public: void HandleKeyPress(QKeyEvent *_e);
431 
434  public: void HandleKeyRelease(QKeyEvent *_e);
435 
438  public: void SetXYZSnap(const math::Vector3d &_xyz);
439 
442  public: math::Vector3d XYZSnap() const;
443 
446  public: void SetRPYSnap(const math::Vector3d &_rpy);
447 
450  public: math::Vector3d RPYSnap() const;
451 
454  public: void SetScaleSnap(const math::Vector3d &_scale);
455 
458  public: math::Vector3d ScaleSnap() const;
459 
467  public: void SnapPoint(
468  math::Vector3d &_point,
469  math::Vector3d &_snapVals, double _sensitivity = 0.4) const;
470 
476  public: void RequestSelectionChange(Entity _selectedEntity,
477  bool _deselectAll, bool _sendEvent);
478 
487  private: double SnapValue(
488  double _coord, double _interval, double _sensitivity) const;
489 
492  private: void XYZConstraint(math::Vector3d &_axis);
493 
495  private: void HandleMouseEvent();
496 
498  private: void HandleMouseContextMenu();
499 
501  private: void HandleMouseViewControl();
502 
504  private: void HandleMouseTransformControl();
505 
507  private: void HandleEntitySelection();
508 
510  private: void HandleModelPlacement();
511 
513  private: void BroadcastHoverPos();
514 
516  private: void BroadcastLeftClick();
517 
519  private: void BroadcastRightClick();
520 
523  private: Entity UniqueId();
524 
528  public: bool GeneratePreview(const sdf::Root &_sdf);
529 
531  public: void TerminateSpawnPreview();
532 
537  public: math::Vector3d ScreenToPlane(const math::Vector2i &_screenPos)
538  const;
539 
544  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
545  const;
546 
549  public: math::Pose3d CameraPose() const;
550 
552  private: void OnMoveToComplete();
553 
555  private: void OnViewAngleComplete();
556 
558  private: void OnMoveToPoseComplete();
559 
564  private: void UpdateSelectedEntity(const rendering::NodePtr &_node,
565  bool _sendEvent);
566 
570  private: void DeselectAllEntities(bool _sendEvent);
571 
573  signals: void ContextMenuRequested(QString _entity);
574 
579  signals: void FollowTargetChanged(const std::string &_target,
580  bool _waitForTarget);
581 
586  public: GLuint textureId;
587 
589  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
590 
593 
595  public: uint32_t visibilityMask = 0xFFFFFFFFu;
596 
598  public: bool initialized = false;
599 
601  public: QSize textureSize = QSize(1024, 1024);
602 
604  public: bool textureDirty = false;
605 
608  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
609  };
610 
612  class RenderThread : public QThread
613  {
614  Q_OBJECT
615 
617  public: RenderThread();
618 
622  public slots: void RenderNext(RenderSync *renderSync);
623 
625  public slots: void ShutDown();
626 
628  public slots: void SizeChanged();
629 
634  signals: void TextureReady(uint _id, const QSize &_size);
635 
638  public: void SetErrorCb(std::function<void(const QString &)> _cb);
639 
641  public: std::function<void(const QString &)> errorCb;
642 
644  public: QOffscreenSurface *surface = nullptr;
645 
647  public: QOpenGLContext *context = nullptr;
648 
651  };
652 
653 
655  class RenderWindowItem : public QQuickItem
656  {
657  Q_OBJECT
658 
661  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
662 
664  public: ~RenderWindowItem() override;
665 
667  public: class RenderUtil *RenderUtil() const;
668 
671  public: void SetCameraPose(const math::Pose3d &_pose);
672 
675  public: math::Pose3d CameraPose() const;
676 
679  public: void SetInitCameraPose(const math::Pose3d &_pose);
680 
683  public: void SetVisibilityMask(uint32_t _mask);
684 
687  public: void SetTransformMode(const std::string &_mode);
688 
691  public: void SetModel(const std::string &_model);
692 
695  public: void SetModelPath(const std::string &_filePath);
696 
700  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
701 
706  public: void SetRecordVideo(bool _record, const std::string &_format,
707  const std::string &_savePath);
708 
711  public: void SetRecordVideoUseSimTime(bool _useSimTime);
712 
715  public: void SetRecordVideoLockstep(bool _lockstep);
716 
719  public: void SetRecordVideoBitrate(unsigned int _bitrate);
720 
723  public: void SetMoveTo(const std::string &_target);
724 
730  public Q_SLOTS: void SetFollowTarget(const std::string &_target,
731  bool _waitForTarget = false);
732 
737  public: void SetViewAngle(const math::Vector3d &_direction);
738 
741  public: void SetMoveToPose(const math::Pose3d &_pose);
742 
745  public: void SetViewTransparentTarget(const std::string &_target);
746 
749  public: void SetViewCOMTarget(const std::string &_target);
750 
753  public: void SetViewInertiaTarget(const std::string &_target);
754 
757  public: void SetViewJointsTarget(const std::string &_target);
758 
761  public: void SetViewWireframesTarget(const std::string &_target);
762 
765  public: void SetViewCollisionsTarget(const std::string &_target);
766 
769  public: void SetViewController(const std::string &_viewController);
770 
773  public: void SetFollowPGain(double _gain);
774 
778  public: void SetFollowWorldFrame(bool _worldFrame);
779 
782  public: void SetFollowOffset(const math::Vector3d &_offset);
783 
786  public: void SetWorldName(const std::string &_name);
787 
798  public: void UpdateSelectedEntity(Entity _entity,
799  bool _sendEvent);
800 
805  public: void DeselectAllEntities(bool _sendEvent);
806 
809  public: void SetXYZSnap(const math::Vector3d &_xyz);
810 
813  public: void SetRPYSnap(const math::Vector3d &_rpy);
814 
817  public: void SetScaleSnap(const math::Vector3d &_scale);
818 
823  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos);
824 
828  public: void OnHovered(const gz::math::Vector2i &_hoverPos);
829 
834  public: bool RendererInitialized() const;
835 
837  public Q_SLOTS: void Ready();
838 
841  public: void HandleKeyPress(QKeyEvent *_e);
842 
845  public: void HandleKeyRelease(QKeyEvent *_e);
846 
847  // Documentation inherited
848  protected: void mousePressEvent(QMouseEvent *_e) override;
849 
850  // Documentation inherited
851  protected: void mouseReleaseEvent(QMouseEvent *_e) override;
852 
853  // Documentation inherited
854  protected: void mouseMoveEvent(QMouseEvent *_e) override;
855 
856  // Documentation inherited
857  protected: void wheelEvent(QWheelEvent *_e) override;
858 
865  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
866  QQuickItem::UpdatePaintNodeData *_data) override;
867 
872  signals: void openContextMenu(QString _entity); // NOLINT
873 
876  public slots: void OnContextMenuRequested(QString _entity);
877 
880  public: void SetErrorCb(std::function<void(const QString &)> _cb);
881 
885  };
886 
888  class TextureNode : public QObject, public QSGSimpleTextureNode
889  {
890  Q_OBJECT
891 
896  public: explicit TextureNode(QQuickWindow *_window,
897  RenderSync &_renderSync);
898 
900  public: ~TextureNode() override;
901 
906  public slots: void NewTexture(uint _id, const QSize &_size);
907 
910  public slots: void PrepareNode();
911 
915  signals: void TextureInUse(RenderSync *_renderSync);
916 
919  signals: void PendingNewTexture();
920 
922  public: uint id = 0;
923 
925  public: QSize size = QSize(0, 0);
926 
928  public: QMutex mutex;
929 
931  public: RenderSync &renderSync;
932 
934  public: QSGTexture *texture = nullptr;
935 
937  public: QQuickWindow *window = nullptr;
938  };
939 }
940 }
941 }
942 
943 #endif
Base class for a GUI System.
Definition: gz/sim/gui/GuiSystem.hh:44
uint64_t Entity
An Entity identifies a single object in simulation such as a model, link, or light....
Definition: gz/sim/Entity.hh:58
Q_INVOKABLE void SetErrorPopupText(const QString &_errorTxt)
Set the text for the popup error message.
This library is part of the Gazebo project.
STL class.
The EntityComponentManager constructs, deletes, and returns components and entities....
Definition: gz/sim/EntityComponentManager.hh:66
Q_INVOKABLE QString LoadingError() const
Get the loading error string.
void OnFocusWindow()
Callback when the mouse enters the render window to focus the window for mouse/key events.
QMutex mutex
Mutex to protect the texture variables.
Definition: Scene3D.hh:928
Texture node for displaying the render texture from ign-renderer.
Definition: Scene3D.hh:888
Rendering thread.
Definition: Scene3D.hh:612
GLuint textureId
Render texture id Values is constantly constantly cycled/swapped/changed from a worker thread Don't r...
Definition: Scene3D.hh:586
Ign-rendering renderer. All ign-rendering calls should be performed inside this class as it makes sur...
Definition: Scene3D.hh:277
Creates an ignition rendering scene and user camera. It is possible to orbit the camera around the sc...
Definition: Scene3D.hh:82
void OnHovered(int _mouseX, int _mouseY)
Callback when the mouse hovers to a new position.
Q_INVOKABLE QString ErrorPopupText() const
Get the text for the popup error message.
Pose3< double > Pose3d
bool eventFilter(QObject *_obj, QEvent *_event) override
Information passed to systems on the update callback.
Definition: include/gz/sim/Types.hh:38
QString loadingError
Loading error message.
Definition: Scene3D.hh:100
static const Vector2< T > Zero
QString errorPopupText
Text for popup error.
Definition: Scene3D.hh:92
Definition: gz/sim/rendering/RenderUtil.hh:45
std::string worldName
Name of the world.
Definition: Scene3D.hh:592
void ErrorPopupTextChanged()
Notify the popup error text has changed.
void OnDropped(const QString &_drop, int _mouseX, int _mouseY)
Callback when receives a drop event.
void popupError()
Notify that an error has occurred (opens popup) Note that the function name needs to start with lower...
void Update(const UpdateInfo &_info, EntityComponentManager &_ecm) override
Update callback called every time the system is stepped. This is called at an Ignition transport thre...
RenderSync & renderSync
See RenderSync.
Definition: Scene3D.hh:931
STL namespace.
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: Scene3D.hh:650
A QQUickItem that manages the render window.
Definition: Scene3D.hh:655
void LoadConfig(const tinyxml2::XMLElement *_pluginElem) override
void LoadingErrorChanged()
Notify that loading error has changed.
std::function< void(const QString &)> errorCb
Function to be called if there are errors.
Definition: Scene3D.hh:641
Vector3< double > Vector3d
Q_INVOKABLE void SetLoadingError(const QString &_loadingError)
Set the loading error message.