Ignition Gazebo

API Reference

7.0.0~pre1
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 IGNITION_GAZEBO_GUI_SCENE3D_HH_
19 #define IGNITION_GAZEBO_GUI_SCENE3D_HH_
20 
26 
27 #include <string>
28 #include <memory>
29 #include <mutex>
30 
31 #include <sdf/Root.hh>
32 
33 #include <ignition/math/Color.hh>
34 #include <ignition/math/Pose3.hh>
35 #include <ignition/math/Vector2.hh>
36 #include <ignition/math/Vector3.hh>
37 
40 
41 #include <ignition/rendering/Camera.hh>
42 
44 
45 #include "ignition/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 
80  {
81  Q_OBJECT
82 
84  Q_PROPERTY(
85  QString errorPopupText
86  READ ErrorPopupText
87  WRITE SetErrorPopupText
88  NOTIFY ErrorPopupTextChanged
89  )
90 
91 
92  public: Scene3D();
93 
95  public: ~Scene3D() override;
96 
97  // Documentation inherited
98  public: void LoadConfig(const tinyxml2::XMLElement *_pluginElem) override;
99 
100  // Documentation inherited
101  public: void Update(const UpdateInfo &_info,
102  EntityComponentManager &_ecm) override;
103 
108  public slots: void OnDropped(const QString &_drop,
109  int _mouseX, int _mouseY);
110 
114  public slots: void OnHovered(int _mouseX, int _mouseY);
115 
118  public slots: void OnFocusWindow();
119 
120  // Documentation inherited
121  protected: bool eventFilter(QObject *_obj, QEvent *_event) override;
122 
127  private: bool OnTransformMode(const msgs::StringMsg &_msg,
128  msgs::Boolean &_res);
129 
134  private: bool OnRecordVideo(const msgs::VideoRecord &_msg,
135  msgs::Boolean &_res);
136 
141  private: bool OnMoveTo(const msgs::StringMsg &_msg,
142  msgs::Boolean &_res);
143 
148  private: bool OnFollow(const msgs::StringMsg &_msg,
149  msgs::Boolean &_res);
150 
155  private: bool OnFollowOffset(const msgs::Vector3d &_msg,
156  msgs::Boolean &_res);
157 
162  private: bool OnViewAngle(const msgs::Vector3d &_msg,
163  msgs::Boolean &_res);
164 
169  private: bool OnMoveToPose(const msgs::GUICamera &_msg,
170  msgs::Boolean &_res);
171 
177  private: bool OnViewTransparent(const msgs::StringMsg &_msg,
178  msgs::Boolean &_res);
179 
185  private: bool OnViewCOM(const msgs::StringMsg &_msg,
186  msgs::Boolean &_res);
187 
192  private: bool OnViewInertia(const msgs::StringMsg &_msg,
193  msgs::Boolean &_res);
194 
199  private: bool OnViewJoints(const msgs::StringMsg &_msg,
200  msgs::Boolean &_res);
201 
206  private: bool OnViewWireframes(const msgs::StringMsg &_msg,
207  msgs::Boolean &_res);
208 
213  private: bool OnViewCollisions(const msgs::StringMsg &_msg,
214  msgs::Boolean &_res);
215 
218  public: Q_INVOKABLE QString ErrorPopupText() const;
219 
222  public: Q_INVOKABLE void SetErrorPopupText(const QString &_errorTxt);
223 
225  signals: void ErrorPopupTextChanged();
226 
230  signals: void popupError();
231 
236  private: bool OnViewControl(const msgs::StringMsg &_msg,
237  msgs::Boolean &_res);
238 
241  private: std::unique_ptr<Scene3DPrivate> dataPtr;
242  };
243 
244  class RenderSync;
245 
252  class IgnRenderer : public QObject
253  {
254  Q_OBJECT
255 
257  public: IgnRenderer();
258 
260  public: ~IgnRenderer() override;
261 
265  public: void Render(RenderSync *_renderSync);
266 
268  public: void Initialize();
269 
271  public: void Destroy();
272 
274  public: class RenderUtil *RenderUtil() const;
275 
278  public: void SetTransformMode(const std::string &_mode);
279 
282  public: void SetModel(const std::string &_model);
283 
286  public: void SetModelPath(const std::string &_filePath);
287 
291  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
292 
297  public: void SetRecordVideo(bool _record, const std::string &_format,
298  const std::string &_savePath);
299 
302  public: void SetRecordVideoUseSimTime(bool _useSimTime);
303 
306  public: void SetRecordVideoLockstep(bool _lockstep);
307 
310  public: void SetRecordVideoBitrate(unsigned int _bitrate);
311 
314  public: void SetMoveTo(const std::string &_target);
315 
321  public: void SetFollowTarget(const std::string &_target,
322  bool _waitForTarget = false);
323 
328  public: void SetViewAngle(const math::Vector3d &_direction);
329 
332  public: void SetMoveToPose(const math::Pose3d &_pose);
333 
336  public: void SetViewTransparentTarget(const std::string &_target);
337 
340  public: void SetViewCOMTarget(const std::string &_target);
341 
344  public: void SetViewInertiaTarget(const std::string &_target);
345 
348  public: void SetViewJointsTarget(const std::string &_target);
349 
352  public: void SetViewWireframesTarget(const std::string &_target);
353 
356  public: void SetViewCollisionsTarget(const std::string &_target);
357 
360  public: void SetViewController(const std::string &_viewController);
361 
364  public: void SetFollowPGain(double _gain);
365 
369  public: void SetFollowWorldFrame(bool _worldFrame);
370 
373  public: void SetFollowOffset(const math::Vector3d &_offset);
374 
377  public: std::string FollowTarget() const;
378 
381  public: bool FollowWorldFrame() const;
382 
385  public: math::Vector3d FollowOffset() const;
386 
389  public: void SetInitCameraPose(const math::Pose3d &_pose);
390 
394  public: void NewMouseEvent(const common::MouseEvent &_e,
395  const math::Vector2d &_drag = math::Vector2d::Zero);
396 
399  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
400 
403  public: void HandleKeyPress(QKeyEvent *_e);
404 
407  public: void HandleKeyRelease(QKeyEvent *_e);
408 
411  public: void SetXYZSnap(const math::Vector3d &_xyz);
412 
415  public: math::Vector3d XYZSnap() const;
416 
419  public: void SetRPYSnap(const math::Vector3d &_rpy);
420 
423  public: math::Vector3d RPYSnap() const;
424 
427  public: void SetScaleSnap(const math::Vector3d &_scale);
428 
431  public: math::Vector3d ScaleSnap() const;
432 
440  public: void SnapPoint(
441  math::Vector3d &_point,
442  math::Vector3d &_snapVals, double _sensitivity = 0.4) const;
443 
449  public: void RequestSelectionChange(Entity _selectedEntity,
450  bool _deselectAll, bool _sendEvent);
451 
460  private: double SnapValue(
461  double _coord, double _interval, double _sensitivity) const;
462 
465  private: void XYZConstraint(math::Vector3d &_axis);
466 
468  private: void HandleMouseEvent();
469 
471  private: void HandleMouseContextMenu();
472 
474  private: void HandleMouseViewControl();
475 
477  private: void HandleMouseTransformControl();
478 
480  private: void HandleEntitySelection();
481 
483  private: void HandleModelPlacement();
484 
486  private: void BroadcastHoverPos();
487 
489  private: void BroadcastLeftClick();
490 
492  private: void BroadcastRightClick();
493 
496  private: Entity UniqueId();
497 
501  public: bool GeneratePreview(const sdf::Root &_sdf);
502 
504  public: void TerminateSpawnPreview();
505 
510  public: math::Vector3d ScreenToPlane(const math::Vector2i &_screenPos)
511  const;
512 
517  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
518  const;
519 
522  public: math::Pose3d CameraPose() const;
523 
525  private: void OnMoveToComplete();
526 
528  private: void OnViewAngleComplete();
529 
531  private: void OnMoveToPoseComplete();
532 
537  private: void UpdateSelectedEntity(const rendering::NodePtr &_node,
538  bool _sendEvent);
539 
543  private: void DeselectAllEntities(bool _sendEvent);
544 
546  signals: void ContextMenuRequested(QString _entity);
547 
552  signals: void FollowTargetChanged(const std::string &_target,
553  bool _waitForTarget);
554 
559  public: GLuint textureId;
560 
562  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
563 
566 
568  public: uint32_t visibilityMask = 0xFFFFFFFFu;
569 
571  public: bool initialized = false;
572 
574  public: QSize textureSize = QSize(1024, 1024);
575 
577  public: bool textureDirty = false;
578 
581  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
582  };
583 
585  class RenderThread : public QThread
586  {
587  Q_OBJECT
588 
590  public: RenderThread();
591 
595  public slots: void RenderNext(RenderSync *renderSync);
596 
598  public slots: void ShutDown();
599 
601  public slots: void SizeChanged();
602 
607  signals: void TextureReady(uint _id, const QSize &_size);
608 
610  public: QOffscreenSurface *surface = nullptr;
611 
613  public: QOpenGLContext *context = nullptr;
614 
617  };
618 
619 
621  class RenderWindowItem : public QQuickItem
622  {
623  Q_OBJECT
624 
627  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
628 
630  public: ~RenderWindowItem() override;
631 
633  public: class RenderUtil *RenderUtil() const;
634 
637  public: void SetCameraPose(const math::Pose3d &_pose);
638 
641  public: math::Pose3d CameraPose() const;
642 
645  public: void SetInitCameraPose(const math::Pose3d &_pose);
646 
649  public: void SetVisibilityMask(uint32_t _mask);
650 
653  public: void SetTransformMode(const std::string &_mode);
654 
657  public: void SetModel(const std::string &_model);
658 
661  public: void SetModelPath(const std::string &_filePath);
662 
666  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
667 
672  public: void SetRecordVideo(bool _record, const std::string &_format,
673  const std::string &_savePath);
674 
677  public: void SetRecordVideoUseSimTime(bool _useSimTime);
678 
681  public: void SetRecordVideoLockstep(bool _lockstep);
682 
685  public: void SetRecordVideoBitrate(unsigned int _bitrate);
686 
689  public: void SetMoveTo(const std::string &_target);
690 
696  public Q_SLOTS: void SetFollowTarget(const std::string &_target,
697  bool _waitForTarget = false);
698 
703  public: void SetViewAngle(const math::Vector3d &_direction);
704 
707  public: void SetMoveToPose(const math::Pose3d &_pose);
708 
711  public: void SetViewTransparentTarget(const std::string &_target);
712 
715  public: void SetViewCOMTarget(const std::string &_target);
716 
719  public: void SetViewInertiaTarget(const std::string &_target);
720 
723  public: void SetViewJointsTarget(const std::string &_target);
724 
727  public: void SetViewWireframesTarget(const std::string &_target);
728 
731  public: void SetViewCollisionsTarget(const std::string &_target);
732 
735  public: void SetViewController(const std::string &_viewController);
736 
739  public: void SetFollowPGain(double _gain);
740 
744  public: void SetFollowWorldFrame(bool _worldFrame);
745 
748  public: void SetFollowOffset(const math::Vector3d &_offset);
749 
752  public: void SetWorldName(const std::string &_name);
753 
764  public: void UpdateSelectedEntity(Entity _entity,
765  bool _sendEvent);
766 
771  public: void DeselectAllEntities(bool _sendEvent);
772 
775  public: void SetXYZSnap(const math::Vector3d &_xyz);
776 
779  public: void SetRPYSnap(const math::Vector3d &_rpy);
780 
783  public: void SetScaleSnap(const math::Vector3d &_scale);
784 
789  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos);
790 
794  public: void OnHovered(const ignition::math::Vector2i &_hoverPos);
795 
800  public: bool RendererInitialized() const;
801 
803  public Q_SLOTS: void Ready();
804 
807  public: void HandleKeyPress(QKeyEvent *_e);
808 
811  public: void HandleKeyRelease(QKeyEvent *_e);
812 
813  // Documentation inherited
814  protected: void mousePressEvent(QMouseEvent *_e) override;
815 
816  // Documentation inherited
817  protected: void mouseReleaseEvent(QMouseEvent *_e) override;
818 
819  // Documentation inherited
820  protected: void mouseMoveEvent(QMouseEvent *_e) override;
821 
822  // Documentation inherited
823  protected: void wheelEvent(QWheelEvent *_e) override;
824 
831  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
832  QQuickItem::UpdatePaintNodeData *_data) override;
833 
838  signals: void openContextMenu(QString _entity); // NOLINT
839 
842  public slots: void OnContextMenuRequested(QString _entity);
843 
847  };
848 
850  class TextureNode : public QObject, public QSGSimpleTextureNode
851  {
852  Q_OBJECT
853 
858  public: explicit TextureNode(QQuickWindow *_window,
859  RenderSync &_renderSync);
860 
862  public: ~TextureNode() override;
863 
868  public slots: void NewTexture(uint _id, const QSize &_size);
869 
872  public slots: void PrepareNode();
873 
877  signals: void TextureInUse(RenderSync *_renderSync);
878 
881  signals: void PendingNewTexture();
882 
884  public: uint id = 0;
885 
887  public: QSize size = QSize(0, 0);
888 
890  public: QMutex mutex;
891 
893  public: RenderSync &renderSync;
894 
896  public: QSGTexture *texture = nullptr;
897 
899  public: QQuickWindow *window = nullptr;
900  };
901 }
902 }
903 }
904 
905 #endif
std::string worldName
Name of the world.
Definition: Scene3D.hh:565
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: Scene3D.hh:616
Texture node for displaying the render texture from ign-renderer.
Definition: Scene3D.hh:850
STL namespace.
A QQUickItem that manages the render window.
Definition: Scene3D.hh:621
Information passed to systems on the update callback.
Definition: Types.hh:38
QMutex mutex
Mutex to protect the texture variables.
Definition: Scene3D.hh:890
STL class.
The EntityComponentManager constructs, deletes, and returns components and entities. A component can be of any class which inherits from components::BaseComponent.
Definition: EntityComponentManager.hh:66
Creates a new ignition rendering scene or adds a user-camera to an existing scene. It is possible to orbit the camera around the scene with the mouse. Use other plugins to manage objects in the scene.
Definition: Scene3D.hh:79
Ign-rendering renderer. All ign-rendering calls should be performed inside this class as it makes sur...
Definition: Scene3D.hh:252
Pose3< double > Pose3d
static const Vector2< T > Zero
Rendering thread.
Definition: Scene3D.hh:585
Vector3< double > Vector3d
RenderSync & renderSync
See RenderSync.
Definition: Scene3D.hh:893
GLuint textureId
Render texture id Values is constantly constantly cycled/swapped/changed from a worker thread Don&#39;t r...
Definition: Scene3D.hh:559
This library is part of the Ignition Robotics project.
Base class for a GUI System.
Definition: GuiSystem.hh:44
uint64_t Entity
An Entity identifies a single object in simulation such as a model, link, or light. At its core, an Entity is just an identifier.
Definition: Entity.hh:59
Definition: RenderUtil.hh:44