Ignition Gazebo

API Reference

5.1.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 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 
176  private: bool OnViewCollisions(const msgs::StringMsg &_msg,
177  msgs::Boolean &_res);
178 
181  public: Q_INVOKABLE QString ErrorPopupText() const;
182 
185  public: Q_INVOKABLE void SetErrorPopupText(const QString &_errorTxt);
186 
188  signals: void ErrorPopupTextChanged();
189 
193  signals: void popupError();
194 
197  private: std::unique_ptr<Scene3DPrivate> dataPtr;
198  };
199 
200  class RenderSync;
201 
208  class IgnRenderer : public QObject
209  {
210  Q_OBJECT
211 
213  public: IgnRenderer();
214 
216  public: ~IgnRenderer() override;
217 
221  public: void Render(RenderSync *_renderSync);
222 
224  public: void Initialize();
225 
227  public: void Destroy();
228 
230  public: class RenderUtil *RenderUtil() const;
231 
234  public: void SetTransformMode(const std::string &_mode);
235 
238  public: void SetModel(const std::string &_model);
239 
242  public: void SetModelPath(const std::string &_filePath);
243 
247  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
248 
253  public: void SetRecordVideo(bool _record, const std::string &_format,
254  const std::string &_savePath);
255 
258  public: void SetRecordVideoUseSimTime(bool _useSimTime);
259 
262  public: void SetRecordVideoLockstep(bool _lockstep);
263 
266  public: void SetRecordVideoBitrate(unsigned int _bitrate);
267 
270  public: void SetMoveTo(const std::string &_target);
271 
277  public: void SetFollowTarget(const std::string &_target,
278  bool _waitForTarget = false);
279 
284  public: void SetViewAngle(const math::Vector3d &_direction);
285 
288  public: void SetMoveToPose(const math::Pose3d &_pose);
289 
292  public: void SetViewCollisionsTarget(const std::string &_target);
293 
296  public: void SetFollowPGain(double _gain);
297 
301  public: void SetFollowWorldFrame(bool _worldFrame);
302 
305  public: void SetFollowOffset(const math::Vector3d &_offset);
306 
309  public: std::string FollowTarget() const;
310 
313  public: bool FollowWorldFrame() const;
314 
317  public: math::Vector3d FollowOffset() const;
318 
321  public: void SetInitCameraPose(const math::Pose3d &_pose);
322 
326  public: void NewMouseEvent(const common::MouseEvent &_e,
327  const math::Vector2d &_drag = math::Vector2d::Zero);
328 
331  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
332 
335  public: void HandleKeyPress(QKeyEvent *_e);
336 
339  public: void HandleKeyRelease(QKeyEvent *_e);
340 
343  public: void SetXYZSnap(const math::Vector3d &_xyz);
344 
347  public: math::Vector3d XYZSnap() const;
348 
351  public: void SetRPYSnap(const math::Vector3d &_rpy);
352 
355  public: math::Vector3d RPYSnap() const;
356 
359  public: void SetScaleSnap(const math::Vector3d &_scale);
360 
363  public: math::Vector3d ScaleSnap() const;
364 
372  public: void SnapPoint(
373  math::Vector3d &_point,
374  math::Vector3d &_snapVals, double _sensitivity = 0.4) const;
375 
381  public: void RequestSelectionChange(Entity _selectedEntity,
382  bool _deselectAll, bool _sendEvent);
383 
392  private: double SnapValue(
393  double _coord, double _interval, double _sensitivity) const;
394 
397  private: void XYZConstraint(math::Vector3d &_axis);
398 
400  private: void HandleMouseEvent();
401 
403  private: void HandleMouseContextMenu();
404 
406  private: void HandleMouseViewControl();
407 
409  private: void HandleMouseTransformControl();
410 
412  private: void HandleEntitySelection();
413 
415  private: void HandleModelPlacement();
416 
418  private: void BroadcastHoverPos();
419 
421  private: void BroadcastLeftClick();
422 
424  private: void BroadcastRightClick();
425 
428  private: Entity UniqueId();
429 
433  public: bool GeneratePreview(const sdf::Root &_sdf);
434 
436  public: void TerminateSpawnPreview();
437 
442  public: math::Vector3d ScreenToPlane(const math::Vector2i &_screenPos)
443  const;
444 
449  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
450  const;
451 
454  public: math::Pose3d CameraPose() const;
455 
457  private: void OnMoveToComplete();
458 
460  private: void OnViewAngleComplete();
461 
463  private: void OnMoveToPoseComplete();
464 
469  private: void UpdateSelectedEntity(const rendering::NodePtr &_node,
470  bool _sendEvent);
471 
475  private: void DeselectAllEntities(bool _sendEvent);
476 
478  signals: void ContextMenuRequested(QString _entity);
479 
484  signals: void FollowTargetChanged(const std::string &_target,
485  bool _waitForTarget);
486 
491  public: GLuint textureId;
492 
494  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
495 
498 
500  public: uint32_t visibilityMask = 0xFFFFFFFFu;
501 
503  public: bool initialized = false;
504 
506  public: QSize textureSize = QSize(1024, 1024);
507 
509  public: bool textureDirty = false;
510 
513  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
514  };
515 
517  class RenderThread : public QThread
518  {
519  Q_OBJECT
520 
522  public: RenderThread();
523 
527  public slots: void RenderNext(RenderSync *renderSync);
528 
530  public slots: void ShutDown();
531 
533  public slots: void SizeChanged();
534 
539  signals: void TextureReady(uint _id, const QSize &_size);
540 
542  public: QOffscreenSurface *surface = nullptr;
543 
545  public: QOpenGLContext *context = nullptr;
546 
549  };
550 
551 
553  class RenderWindowItem : public QQuickItem
554  {
555  Q_OBJECT
556 
559  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
560 
562  public: ~RenderWindowItem() override;
563 
565  public: class RenderUtil *RenderUtil() const;
566 
569  public: void SetCameraPose(const math::Pose3d &_pose);
570 
573  public: math::Pose3d CameraPose() const;
574 
577  public: void SetInitCameraPose(const math::Pose3d &_pose);
578 
581  public: void SetVisibilityMask(uint32_t _mask);
582 
585  public: void SetTransformMode(const std::string &_mode);
586 
589  public: void SetModel(const std::string &_model);
590 
593  public: void SetModelPath(const std::string &_filePath);
594 
598  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
599 
604  public: void SetRecordVideo(bool _record, const std::string &_format,
605  const std::string &_savePath);
606 
609  public: void SetRecordVideoUseSimTime(bool _useSimTime);
610 
613  public: void SetRecordVideoLockstep(bool _lockstep);
614 
617  public: void SetRecordVideoBitrate(unsigned int _bitrate);
618 
621  public: void SetMoveTo(const std::string &_target);
622 
628  public Q_SLOTS: void SetFollowTarget(const std::string &_target,
629  bool _waitForTarget = false);
630 
635  public: void SetViewAngle(const math::Vector3d &_direction);
636 
639  public: void SetMoveToPose(const math::Pose3d &_pose);
640 
643  public: void SetViewCollisionsTarget(const std::string &_target);
644 
647  public: void SetFollowPGain(double _gain);
648 
652  public: void SetFollowWorldFrame(bool _worldFrame);
653 
656  public: void SetFollowOffset(const math::Vector3d &_offset);
657 
660  public: void SetWorldName(const std::string &_name);
661 
672  public: void UpdateSelectedEntity(Entity _entity,
673  bool _sendEvent);
674 
679  public: void DeselectAllEntities(bool _sendEvent);
680 
683  public: void SetXYZSnap(const math::Vector3d &_xyz);
684 
687  public: void SetRPYSnap(const math::Vector3d &_rpy);
688 
691  public: void SetScaleSnap(const math::Vector3d &_scale);
692 
697  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos);
698 
702  public: void OnHovered(const ignition::math::Vector2i &_hoverPos);
703 
708  public: bool RendererInitialized() const;
709 
711  public Q_SLOTS: void Ready();
712 
715  public: void HandleKeyPress(QKeyEvent *_e);
716 
719  public: void HandleKeyRelease(QKeyEvent *_e);
720 
721  // Documentation inherited
722  protected: void mousePressEvent(QMouseEvent *_e) override;
723 
724  // Documentation inherited
725  protected: void mouseReleaseEvent(QMouseEvent *_e) override;
726 
727  // Documentation inherited
728  protected: void mouseMoveEvent(QMouseEvent *_e) override;
729 
730  // Documentation inherited
731  protected: void wheelEvent(QWheelEvent *_e) override;
732 
739  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
740  QQuickItem::UpdatePaintNodeData *_data) override;
741 
746  signals: void openContextMenu(QString _entity); // NOLINT
747 
750  public slots: void OnContextMenuRequested(QString _entity);
751 
755  };
756 
758  class TextureNode : public QObject, public QSGSimpleTextureNode
759  {
760  Q_OBJECT
761 
766  public: explicit TextureNode(QQuickWindow *_window,
767  RenderSync &_renderSync);
768 
770  public: ~TextureNode() override;
771 
776  public slots: void NewTexture(uint _id, const QSize &_size);
777 
780  public slots: void PrepareNode();
781 
785  signals: void TextureInUse(RenderSync *_renderSync);
786 
789  signals: void PendingNewTexture();
790 
792  public: uint id = 0;
793 
795  public: QSize size = QSize(0, 0);
796 
798  public: QMutex mutex;
799 
801  public: RenderSync &renderSync;
802 
804  public: QSGTexture *texture = nullptr;
805 
807  public: QQuickWindow *window = nullptr;
808  };
809 }
810 }
811 }
812 
813 #endif
std::string worldName
Name of the world.
Definition: Scene3D.hh:497
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: Scene3D.hh:548
Texture node for displaying the render texture from ign-renderer.
Definition: Scene3D.hh:758
STL namespace.
A QQUickItem that manages the render window.
Definition: Scene3D.hh:553
Information passed to systems on the update callback.
Definition: Types.hh:36
QMutex mutex
Mutex to protect the texture variables.
Definition: Scene3D.hh:798
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:65
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:208
Pose3< double > Pose3d
static const Vector2< T > Zero
Rendering thread.
Definition: Scene3D.hh:517
Vector3< double > Vector3d
RenderSync & renderSync
See RenderSync.
Definition: Scene3D.hh:801
GLuint textureId
Render texture id Values is constantly constantly cycled/swapped/changed from a worker thread Don&#39;t r...
Definition: Scene3D.hh:491
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