Gazebo Gazebo

API Reference

3.15.2
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 OnViewAngle(const msgs::Vector3d &_msg,
167  msgs::Boolean &_res);
168 
173  private: bool OnMoveToPose(const msgs::GUICamera &_msg,
174  msgs::Boolean &_res);
175 
180  private: bool OnViewCollisions(const msgs::StringMsg &_msg,
181  msgs::Boolean &_res);
182 
185  public: Q_INVOKABLE QString ErrorPopupText() const;
186 
189  public: Q_INVOKABLE void SetErrorPopupText(const QString &_errorTxt);
190 
192  signals: void ErrorPopupTextChanged();
193 
197  signals: void popupError();
198 
201  public: Q_INVOKABLE QString LoadingError() const;
202 
205  public: Q_INVOKABLE void SetLoadingError(const QString &_loadingError);
206 
208  signals: void LoadingErrorChanged();
209 
211  public: QString loadingError;
212 
215  private: std::unique_ptr<Scene3DPrivate> dataPtr;
216  };
217 
224  class IgnRenderer : public QObject
225  {
226  Q_OBJECT
227 
229  public: IgnRenderer();
230 
232  public: ~IgnRenderer() override;
233 
235  public: void Render();
236 
240  public: std::string Initialize();
241 
243  public: void Destroy();
244 
246  public: class RenderUtil *RenderUtil() const;
247 
250  public: void SetTransformMode(const std::string &_mode);
251 
254  public: void SetModel(const std::string &_model);
255 
258  public: void SetModelPath(const std::string &_filePath);
259 
263  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
264 
269  public: void SetRecordVideo(bool _record, const std::string &_format,
270  const std::string &_savePath);
271 
274  public: void SetRecordVideoUseSimTime(bool _useSimTime);
275 
278  public: void SetRecordVideoLockstep(bool _lockstep);
279 
282  public: void SetRecordVideoBitrate(unsigned int _bitrate);
283 
286  public: void SetMoveTo(const std::string &_target);
287 
293  public: void SetFollowTarget(const std::string &_target,
294  bool _waitForTarget = false);
295 
300  public: void SetViewAngle(const math::Vector3d &_direction);
301 
304  public: void SetMoveToPose(const math::Pose3d &_pose);
305 
308  public: void SetViewCollisionsTarget(const std::string &_target);
309 
312  public: void SetFollowPGain(double _gain);
313 
317  public: void SetFollowWorldFrame(bool _worldFrame);
318 
321  public: void SetFollowOffset(const math::Vector3d &_offset);
322 
325  public: std::string FollowTarget() const;
326 
329  public: bool FollowWorldFrame() const;
330 
333  public: math::Vector3d FollowOffset() const;
334 
337  public: void SetInitCameraPose(const math::Pose3d &_pose);
338 
342  public: void NewMouseEvent(const common::MouseEvent &_e,
343  const math::Vector2d &_drag = math::Vector2d::Zero);
344 
347  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
348 
351  public: void HandleKeyPress(QKeyEvent *_e);
352 
355  public: void HandleKeyRelease(QKeyEvent *_e);
356 
359  public: void SetXYZSnap(const math::Vector3d &_xyz);
360 
363  public: math::Vector3d XYZSnap() const;
364 
367  public: void SetRPYSnap(const math::Vector3d &_rpy);
368 
371  public: math::Vector3d RPYSnap() const;
372 
375  public: void SetScaleSnap(const math::Vector3d &_scale);
376 
379  public: math::Vector3d ScaleSnap() const;
380 
388  public: void SnapPoint(
389  math::Vector3d &_point,
390  math::Vector3d &_snapVals, double _sensitivity = 0.4) const;
391 
397  public: void RequestSelectionChange(Entity _selectedEntity,
398  bool _deselectAll, bool _sendEvent);
399 
408  private: double SnapValue(
409  double _coord, double _interval, double _sensitivity) const;
410 
413  private: void XYZConstraint(math::Vector3d &_axis);
414 
416  private: void HandleMouseEvent();
417 
419  private: void HandleMouseContextMenu();
420 
422  private: void HandleMouseViewControl();
423 
425  private: void HandleMouseTransformControl();
426 
428  private: void HandleEntitySelection();
429 
431  private: void HandleModelPlacement();
432 
434  private: void BroadcastHoverPos();
435 
437  private: void BroadcastLeftClick();
438 
440  private: void BroadcastRightClick();
441 
444  private: Entity UniqueId();
445 
449  public: bool GeneratePreview(const sdf::Root &_sdf);
450 
452  public: void TerminateSpawnPreview();
453 
458  public: math::Vector3d ScreenToPlane(const math::Vector2i &_screenPos)
459  const;
460 
465  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
466  const;
467 
470  public: math::Pose3d CameraPose() const;
471 
473  private: void OnMoveToComplete();
474 
476  private: void OnViewAngleComplete();
477 
479  private: void OnMoveToPoseComplete();
480 
485  private: void UpdateSelectedEntity(const rendering::NodePtr &_node,
486  bool _sendEvent);
487 
491  private: void DeselectAllEntities(bool _sendEvent);
492 
494  signals: void ContextMenuRequested(QString _entity);
495 
500  signals: void FollowTargetChanged(const std::string &_target,
501  bool _waitForTarget);
502 
504  public: GLuint textureId = 0u;
505 
507  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
508 
511 
513  public: bool initialized = false;
514 
516  public: QSize textureSize = QSize(1024, 1024);
517 
519  public: bool textureDirty = false;
520 
523  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
524  };
525 
527  class RenderThread : public QThread
528  {
529  Q_OBJECT
530 
532  public: RenderThread();
533 
535  public slots: void RenderNext();
536 
538  public slots: void ShutDown();
539 
541  public slots: void SizeChanged();
542 
547  signals: void TextureReady(int _id, const QSize &_size);
548 
551  public: void SetErrorCb(std::function<void(const QString &)> _cb);
552 
554  public: std::function<void(const QString &)> errorCb;
555 
557  public: QOffscreenSurface *surface = nullptr;
558 
560  public: QOpenGLContext *context = nullptr;
561 
564  };
565 
566 
568  class RenderWindowItem : public QQuickItem
569  {
570  Q_OBJECT
571 
574  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
575 
577  public: ~RenderWindowItem() override;
578 
580  public: class RenderUtil *RenderUtil() const;
581 
584  public: void SetCameraPose(const math::Pose3d &_pose);
585 
588  public: math::Pose3d CameraPose() const;
589 
592  public: void SetInitCameraPose(const math::Pose3d &_pose);
593 
596  public: void SetTransformMode(const std::string &_mode);
597 
600  public: void SetModel(const std::string &_model);
601 
604  public: void SetModelPath(const std::string &_filePath);
605 
609  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
610 
615  public: void SetRecordVideo(bool _record, const std::string &_format,
616  const std::string &_savePath);
617 
620  public: void SetRecordVideoUseSimTime(bool _useSimTime);
621 
624  public: void SetRecordVideoLockstep(bool _lockstep);
625 
628  public: void SetRecordVideoBitrate(unsigned int _bitrate);
629 
632  public: void SetMoveTo(const std::string &_target);
633 
639  public Q_SLOTS: void SetFollowTarget(const std::string &_target,
640  bool _waitForTarget = false);
641 
646  public: void SetViewAngle(const math::Vector3d &_direction);
647 
650  public: void SetMoveToPose(const math::Pose3d &_pose);
651 
654  public: void SetViewCollisionsTarget(const std::string &_target);
655 
658  public: void SetFollowPGain(double _gain);
659 
663  public: void SetFollowWorldFrame(bool _worldFrame);
664 
667  public: void SetFollowOffset(const math::Vector3d &_offset);
668 
671  public: void SetWorldName(const std::string &_name);
672 
683  public: void UpdateSelectedEntity(Entity _entity,
684  bool _sendEvent);
685 
690  public: void DeselectAllEntities(bool _sendEvent);
691 
694  public: void SetXYZSnap(const math::Vector3d &_xyz);
695 
698  public: void SetRPYSnap(const math::Vector3d &_rpy);
699 
702  public: void SetScaleSnap(const math::Vector3d &_scale);
703 
708  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos);
709 
713  public: void OnHovered(const gz::math::Vector2i &_hoverPos);
714 
719  public: bool RendererInitialized() const;
720 
722  public Q_SLOTS: void Ready();
723 
726  public: void HandleKeyPress(QKeyEvent *_e);
727 
730  public: void HandleKeyRelease(QKeyEvent *_e);
731 
732  // Documentation inherited
733  protected: void mousePressEvent(QMouseEvent *_e) override;
734 
735  // Documentation inherited
736  protected: void mouseReleaseEvent(QMouseEvent *_e) override;
737 
738  // Documentation inherited
739  protected: void mouseMoveEvent(QMouseEvent *_e) override;
740 
741  // Documentation inherited
742  protected: void wheelEvent(QWheelEvent *_e) override;
743 
750  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
751  QQuickItem::UpdatePaintNodeData *_data) override;
752 
757  signals: void openContextMenu(QString _entity); // NOLINT
758 
761  public slots: void OnContextMenuRequested(QString _entity);
762 
765  public: void SetErrorCb(std::function<void(const QString &)> _cb);
766 
770  };
771 
773  class TextureNode : public QObject, public QSGSimpleTextureNode
774  {
775  Q_OBJECT
776 
779  public: explicit TextureNode(QQuickWindow *_window);
780 
782  public: ~TextureNode() override;
783 
788  public slots: void NewTexture(int _id, const QSize &_size);
789 
792  public slots: void PrepareNode();
793 
796  signals: void TextureInUse();
797 
800  signals: void PendingNewTexture();
801 
803  public: int id = 0;
804 
806  public: QSize size = QSize(0, 0);
807 
809  public: QMutex mutex;
810 
812  public: QSGTexture *texture = nullptr;
813 
815  public: QQuickWindow *window = nullptr;
816  };
817 }
818 }
819 }
820 
821 #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:59
Q_INVOKABLE void SetErrorPopupText(const QString &_errorTxt)
Set the text for the popup error message.
This library is part of the Ignition Robotics project.
STL class.
The EntityComponentManager constructs, deletes, and returns components and entities....
Definition: gz/sim/EntityComponentManager.hh:65
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:809
Texture node for displaying the render texture from ign-renderer.
Definition: Scene3D.hh:773
Rendering thread.
Definition: Scene3D.hh:527
Ign-rendering renderer. All ign-rendering calls should be performed inside this class as it makes sur...
Definition: Scene3D.hh:224
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:37
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:44
std::string worldName
Name of the world.
Definition: Scene3D.hh:510
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...
STL namespace.
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: Scene3D.hh:563
A QQUickItem that manages the render window.
Definition: Scene3D.hh:568
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:554
Vector3< double > Vector3d
Q_INVOKABLE void SetLoadingError(const QString &_loadingError)
Set the loading error message.