Ignition Gazebo

API Reference

3.9.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 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 OnViewAngle(const msgs::Vector3d &_msg,
156  msgs::Boolean &_res);
157 
162  private: bool OnMoveToPose(const msgs::GUICamera &_msg,
163  msgs::Boolean &_res);
164 
169  private: bool OnViewCollisions(const msgs::StringMsg &_msg,
170  msgs::Boolean &_res);
171 
174  public: Q_INVOKABLE QString ErrorPopupText() const;
175 
178  public: Q_INVOKABLE void SetErrorPopupText(const QString &_errorTxt);
179 
181  signals: void ErrorPopupTextChanged();
182 
186  signals: void popupError();
187 
190  private: std::unique_ptr<Scene3DPrivate> dataPtr;
191  };
192 
199  class IgnRenderer : public QObject
200  {
201  Q_OBJECT
202 
204  public: IgnRenderer();
205 
207  public: ~IgnRenderer() override;
208 
210  public: void Render();
211 
213  public: void Initialize();
214 
216  public: void Destroy();
217 
219  public: class RenderUtil *RenderUtil() const;
220 
223  public: void SetTransformMode(const std::string &_mode);
224 
227  public: void SetModel(const std::string &_model);
228 
231  public: void SetModelPath(const std::string &_filePath);
232 
236  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
237 
242  public: void SetRecordVideo(bool _record, const std::string &_format,
243  const std::string &_savePath);
244 
247  public: void SetRecordVideoUseSimTime(bool _useSimTime);
248 
251  public: void SetRecordVideoLockstep(bool _lockstep);
252 
255  public: void SetRecordVideoBitrate(unsigned int _bitrate);
256 
259  public: void SetMoveTo(const std::string &_target);
260 
266  public: void SetFollowTarget(const std::string &_target,
267  bool _waitForTarget = false);
268 
273  public: void SetViewAngle(const math::Vector3d &_direction);
274 
277  public: void SetMoveToPose(const math::Pose3d &_pose);
278 
281  public: void SetViewCollisionsTarget(const std::string &_target);
282 
285  public: void SetFollowPGain(double _gain);
286 
290  public: void SetFollowWorldFrame(bool _worldFrame);
291 
294  public: void SetFollowOffset(const math::Vector3d &_offset);
295 
298  public: std::string FollowTarget() const;
299 
302  public: bool FollowWorldFrame() const;
303 
306  public: math::Vector3d FollowOffset() const;
307 
310  public: void SetInitCameraPose(const math::Pose3d &_pose);
311 
315  public: void NewMouseEvent(const common::MouseEvent &_e,
316  const math::Vector2d &_drag = math::Vector2d::Zero);
317 
320  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
321 
324  public: void HandleKeyPress(QKeyEvent *_e);
325 
328  public: void HandleKeyRelease(QKeyEvent *_e);
329 
332  public: void SetXYZSnap(const math::Vector3d &_xyz);
333 
336  public: math::Vector3d XYZSnap() const;
337 
340  public: void SetRPYSnap(const math::Vector3d &_rpy);
341 
344  public: math::Vector3d RPYSnap() const;
345 
348  public: void SetScaleSnap(const math::Vector3d &_scale);
349 
352  public: math::Vector3d ScaleSnap() const;
353 
361  public: void SnapPoint(
362  math::Vector3d &_point,
363  math::Vector3d &_snapVals, double _sensitivity = 0.4) const;
364 
370  public: void RequestSelectionChange(Entity _selectedEntity,
371  bool _deselectAll, bool _sendEvent);
372 
381  private: double SnapValue(
382  double _coord, double _interval, double _sensitivity) const;
383 
386  private: void XYZConstraint(math::Vector3d &_axis);
387 
389  private: void HandleMouseEvent();
390 
392  private: void HandleMouseContextMenu();
393 
395  private: void HandleMouseViewControl();
396 
398  private: void HandleMouseTransformControl();
399 
401  private: void HandleEntitySelection();
402 
404  private: void HandleModelPlacement();
405 
407  private: void BroadcastHoverPos();
408 
410  private: void BroadcastLeftClick();
411 
413  private: void BroadcastRightClick();
414 
417  private: Entity UniqueId();
418 
422  public: bool GeneratePreview(const sdf::Root &_sdf);
423 
425  public: void TerminateSpawnPreview();
426 
431  public: math::Vector3d ScreenToPlane(const math::Vector2i &_screenPos)
432  const;
433 
438  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
439  const;
440 
443  public: math::Pose3d CameraPose() const;
444 
446  private: void OnMoveToComplete();
447 
449  private: void OnViewAngleComplete();
450 
452  private: void OnMoveToPoseComplete();
453 
458  private: void UpdateSelectedEntity(const rendering::NodePtr &_node,
459  bool _sendEvent);
460 
464  private: void DeselectAllEntities(bool _sendEvent);
465 
467  signals: void ContextMenuRequested(QString _entity);
468 
473  signals: void FollowTargetChanged(const std::string &_target,
474  bool _waitForTarget);
475 
477  public: GLuint textureId = 0u;
478 
480  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
481 
484 
486  public: bool initialized = false;
487 
489  public: QSize textureSize = QSize(1024, 1024);
490 
492  public: bool textureDirty = false;
493 
496  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
497  };
498 
500  class RenderThread : public QThread
501  {
502  Q_OBJECT
503 
505  public: RenderThread();
506 
508  public slots: void RenderNext();
509 
511  public slots: void ShutDown();
512 
514  public slots: void SizeChanged();
515 
520  signals: void TextureReady(int _id, const QSize &_size);
521 
523  public: QOffscreenSurface *surface = nullptr;
524 
526  public: QOpenGLContext *context = nullptr;
527 
530  };
531 
532 
534  class RenderWindowItem : public QQuickItem
535  {
536  Q_OBJECT
537 
540  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
541 
543  public: ~RenderWindowItem() override;
544 
546  public: class RenderUtil *RenderUtil() const;
547 
550  public: void SetCameraPose(const math::Pose3d &_pose);
551 
554  public: math::Pose3d CameraPose() const;
555 
558  public: void SetInitCameraPose(const math::Pose3d &_pose);
559 
562  public: void SetTransformMode(const std::string &_mode);
563 
566  public: void SetModel(const std::string &_model);
567 
570  public: void SetModelPath(const std::string &_filePath);
571 
575  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
576 
581  public: void SetRecordVideo(bool _record, const std::string &_format,
582  const std::string &_savePath);
583 
586  public: void SetRecordVideoUseSimTime(bool _useSimTime);
587 
590  public: void SetRecordVideoLockstep(bool _lockstep);
591 
594  public: void SetRecordVideoBitrate(unsigned int _bitrate);
595 
598  public: void SetMoveTo(const std::string &_target);
599 
605  public Q_SLOTS: void SetFollowTarget(const std::string &_target,
606  bool _waitForTarget = false);
607 
612  public: void SetViewAngle(const math::Vector3d &_direction);
613 
616  public: void SetMoveToPose(const math::Pose3d &_pose);
617 
620  public: void SetViewCollisionsTarget(const std::string &_target);
621 
624  public: void SetFollowPGain(double _gain);
625 
629  public: void SetFollowWorldFrame(bool _worldFrame);
630 
633  public: void SetFollowOffset(const math::Vector3d &_offset);
634 
637  public: void SetWorldName(const std::string &_name);
638 
649  public: void UpdateSelectedEntity(Entity _entity,
650  bool _sendEvent);
651 
656  public: void DeselectAllEntities(bool _sendEvent);
657 
660  public: void SetXYZSnap(const math::Vector3d &_xyz);
661 
664  public: void SetRPYSnap(const math::Vector3d &_rpy);
665 
668  public: void SetScaleSnap(const math::Vector3d &_scale);
669 
674  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos);
675 
679  public: void OnHovered(const ignition::math::Vector2i &_hoverPos);
680 
685  public: bool RendererInitialized() const;
686 
688  public Q_SLOTS: void Ready();
689 
692  public: void HandleKeyPress(QKeyEvent *_e);
693 
696  public: void HandleKeyRelease(QKeyEvent *_e);
697 
698  // Documentation inherited
699  protected: void mousePressEvent(QMouseEvent *_e) override;
700 
701  // Documentation inherited
702  protected: void mouseReleaseEvent(QMouseEvent *_e) override;
703 
704  // Documentation inherited
705  protected: void mouseMoveEvent(QMouseEvent *_e) override;
706 
707  // Documentation inherited
708  protected: void wheelEvent(QWheelEvent *_e) override;
709 
716  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
717  QQuickItem::UpdatePaintNodeData *_data) override;
718 
723  signals: void openContextMenu(QString _entity); // NOLINT
724 
727  public slots: void OnContextMenuRequested(QString _entity);
728 
732  };
733 
735  class TextureNode : public QObject, public QSGSimpleTextureNode
736  {
737  Q_OBJECT
738 
741  public: explicit TextureNode(QQuickWindow *_window);
742 
744  public: ~TextureNode() override;
745 
750  public slots: void NewTexture(int _id, const QSize &_size);
751 
754  public slots: void PrepareNode();
755 
758  signals: void TextureInUse();
759 
762  signals: void PendingNewTexture();
763 
765  public: int id = 0;
766 
768  public: QSize size = QSize(0, 0);
769 
771  public: QMutex mutex;
772 
774  public: QSGTexture *texture = nullptr;
775 
777  public: QQuickWindow *window = nullptr;
778  };
779 }
780 }
781 }
782 
783 #endif
std::string worldName
Name of the world.
Definition: Scene3D.hh:483
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: Scene3D.hh:529
Texture node for displaying the render texture from ign-renderer.
Definition: Scene3D.hh:735
STL namespace.
A QQUickItem that manages the render window.
Definition: Scene3D.hh:534
Information passed to systems on the update callback.
Definition: Types.hh:37
QMutex mutex
Mutex to protect the texture variables.
Definition: Scene3D.hh:771
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:64
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:199
Pose3< double > Pose3d
static const Vector2< T > Zero
Rendering thread.
Definition: Scene3D.hh:500
Vector3< double > Vector3d
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