Ignition Gazebo

API Reference

5.0.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  public: Scene3D();
85 
87  public: ~Scene3D() override;
88 
89  // Documentation inherited
90  public: void LoadConfig(const tinyxml2::XMLElement *_pluginElem) override;
91 
92  // Documentation inherited
93  public: void Update(const UpdateInfo &_info,
94  EntityComponentManager &_ecm) override;
95 
100  public slots: void OnDropped(const QString &_drop,
101  int _mouseX, int _mouseY);
102 
106  public slots: void OnHovered(int _mouseX, int _mouseY);
107 
110  public slots: void OnFocusWindow();
111 
112  // Documentation inherited
113  protected: bool eventFilter(QObject *_obj, QEvent *_event) override;
114 
119  private: bool OnTransformMode(const msgs::StringMsg &_msg,
120  msgs::Boolean &_res);
121 
126  private: bool OnRecordVideo(const msgs::VideoRecord &_msg,
127  msgs::Boolean &_res);
128 
133  private: bool OnMoveTo(const msgs::StringMsg &_msg,
134  msgs::Boolean &_res);
135 
140  private: bool OnFollow(const msgs::StringMsg &_msg,
141  msgs::Boolean &_res);
142 
147  private: bool OnFollowOffset(const msgs::Vector3d &_msg,
148  msgs::Boolean &_res);
149 
154  private: bool OnViewAngle(const msgs::Vector3d &_msg,
155  msgs::Boolean &_res);
156 
161  private: bool OnMoveToPose(const msgs::GUICamera &_msg,
162  msgs::Boolean &_res);
163 
168  private: bool OnViewCollisions(const msgs::StringMsg &_msg,
169  msgs::Boolean &_res);
170 
173  private: std::unique_ptr<Scene3DPrivate> dataPtr;
174  };
175 
176  class RenderSync;
177 
184  class IgnRenderer : public QObject
185  {
186  Q_OBJECT
187 
189  public: IgnRenderer();
190 
192  public: ~IgnRenderer() override;
193 
197  public: void Render(RenderSync *_renderSync);
198 
200  public: void Initialize();
201 
203  public: void Destroy();
204 
206  public: class RenderUtil *RenderUtil() const;
207 
210  public: void SetTransformMode(const std::string &_mode);
211 
214  public: void SetModel(const std::string &_model);
215 
218  public: void SetModelPath(const std::string &_filePath);
219 
223  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
224 
229  public: void SetRecordVideo(bool _record, const std::string &_format,
230  const std::string &_savePath);
231 
234  public: void SetRecordVideoUseSimTime(bool _useSimTime);
235 
238  public: void SetRecordVideoLockstep(bool _lockstep);
239 
242  public: void SetRecordVideoBitrate(unsigned int _bitrate);
243 
246  public: void SetMoveTo(const std::string &_target);
247 
253  public: void SetFollowTarget(const std::string &_target,
254  bool _waitForTarget = false);
255 
260  public: void SetViewAngle(const math::Vector3d &_direction);
261 
264  public: void SetMoveToPose(const math::Pose3d &_pose);
265 
268  public: void SetViewCollisionsTarget(const std::string &_target);
269 
272  public: void SetFollowPGain(double _gain);
273 
277  public: void SetFollowWorldFrame(bool _worldFrame);
278 
281  public: void SetFollowOffset(const math::Vector3d &_offset);
282 
285  public: std::string FollowTarget() const;
286 
289  public: bool FollowWorldFrame() const;
290 
293  public: math::Vector3d FollowOffset() const;
294 
297  public: void SetInitCameraPose(const math::Pose3d &_pose);
298 
302  public: void NewMouseEvent(const common::MouseEvent &_e,
303  const math::Vector2d &_drag = math::Vector2d::Zero);
304 
307  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
308 
311  public: void HandleKeyPress(QKeyEvent *_e);
312 
315  public: void HandleKeyRelease(QKeyEvent *_e);
316 
319  public: void SetXYZSnap(const math::Vector3d &_xyz);
320 
323  public: math::Vector3d XYZSnap() const;
324 
327  public: void SetRPYSnap(const math::Vector3d &_rpy);
328 
331  public: math::Vector3d RPYSnap() const;
332 
335  public: void SetScaleSnap(const math::Vector3d &_scale);
336 
339  public: math::Vector3d ScaleSnap() const;
340 
348  public: void SnapPoint(
349  math::Vector3d &_point,
350  math::Vector3d &_snapVals, double _sensitivity = 0.4) const;
351 
357  public: void RequestSelectionChange(Entity _selectedEntity,
358  bool _deselectAll, bool _sendEvent);
359 
368  private: double SnapValue(
369  double _coord, double _interval, double _sensitivity) const;
370 
373  private: void XYZConstraint(math::Vector3d &_axis);
374 
376  private: void HandleMouseEvent();
377 
379  private: void HandleMouseContextMenu();
380 
382  private: void HandleMouseViewControl();
383 
385  private: void HandleMouseTransformControl();
386 
388  private: void HandleEntitySelection();
389 
391  private: void HandleModelPlacement();
392 
394  private: void BroadcastHoverPos();
395 
397  private: void BroadcastLeftClick();
398 
400  private: void BroadcastRightClick();
401 
404  private: Entity UniqueId();
405 
409  public: bool GeneratePreview(const sdf::Root &_sdf);
410 
412  public: void TerminateSpawnPreview();
413 
418  public: math::Vector3d ScreenToPlane(const math::Vector2i &_screenPos)
419  const;
420 
425  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
426  const;
427 
430  public: math::Pose3d CameraPose() const;
431 
433  private: void OnMoveToComplete();
434 
436  private: void OnViewAngleComplete();
437 
439  private: void OnMoveToPoseComplete();
440 
445  private: void UpdateSelectedEntity(const rendering::NodePtr &_node,
446  bool _sendEvent);
447 
451  private: void DeselectAllEntities(bool _sendEvent);
452 
454  signals: void ContextMenuRequested(QString _entity);
455 
460  signals: void FollowTargetChanged(const std::string &_target,
461  bool _waitForTarget);
462 
467  public: GLuint textureId;
468 
470  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
471 
474 
476  public: uint32_t visibilityMask = 0xFFFFFFFFu;
477 
479  public: bool initialized = false;
480 
482  public: QSize textureSize = QSize(1024, 1024);
483 
485  public: bool textureDirty = false;
486 
489  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
490  };
491 
493  class RenderThread : public QThread
494  {
495  Q_OBJECT
496 
498  public: RenderThread();
499 
503  public slots: void RenderNext(RenderSync *renderSync);
504 
506  public slots: void ShutDown();
507 
509  public slots: void SizeChanged();
510 
515  signals: void TextureReady(uint _id, const QSize &_size);
516 
518  public: QOffscreenSurface *surface = nullptr;
519 
521  public: QOpenGLContext *context = nullptr;
522 
525  };
526 
527 
529  class RenderWindowItem : public QQuickItem
530  {
531  Q_OBJECT
532 
535  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
536 
538  public: ~RenderWindowItem() override;
539 
541  public: class RenderUtil *RenderUtil() const;
542 
545  public: void SetCameraPose(const math::Pose3d &_pose);
546 
549  public: math::Pose3d CameraPose() const;
550 
553  public: void SetInitCameraPose(const math::Pose3d &_pose);
554 
557  public: void SetVisibilityMask(uint32_t _mask);
558 
561  public: void SetTransformMode(const std::string &_mode);
562 
565  public: void SetModel(const std::string &_model);
566 
569  public: void SetModelPath(const std::string &_filePath);
570 
574  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
575 
580  public: void SetRecordVideo(bool _record, const std::string &_format,
581  const std::string &_savePath);
582 
585  public: void SetRecordVideoUseSimTime(bool _useSimTime);
586 
589  public: void SetRecordVideoLockstep(bool _lockstep);
590 
593  public: void SetRecordVideoBitrate(unsigned int _bitrate);
594 
597  public: void SetMoveTo(const std::string &_target);
598 
604  public Q_SLOTS: void SetFollowTarget(const std::string &_target,
605  bool _waitForTarget = false);
606 
611  public: void SetViewAngle(const math::Vector3d &_direction);
612 
615  public: void SetMoveToPose(const math::Pose3d &_pose);
616 
619  public: void SetViewCollisionsTarget(const std::string &_target);
620 
623  public: void SetFollowPGain(double _gain);
624 
628  public: void SetFollowWorldFrame(bool _worldFrame);
629 
632  public: void SetFollowOffset(const math::Vector3d &_offset);
633 
636  public: void SetWorldName(const std::string &_name);
637 
648  public: void UpdateSelectedEntity(Entity _entity,
649  bool _sendEvent);
650 
655  public: void DeselectAllEntities(bool _sendEvent);
656 
659  public: void SetXYZSnap(const math::Vector3d &_xyz);
660 
663  public: void SetRPYSnap(const math::Vector3d &_rpy);
664 
667  public: void SetScaleSnap(const math::Vector3d &_scale);
668 
673  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos);
674 
678  public: void OnHovered(const ignition::math::Vector2i &_hoverPos);
679 
684  public: bool RendererInitialized() const;
685 
687  public Q_SLOTS: void Ready();
688 
691  public: void HandleKeyPress(QKeyEvent *_e);
692 
695  public: void HandleKeyRelease(QKeyEvent *_e);
696 
697  // Documentation inherited
698  protected: void mousePressEvent(QMouseEvent *_e) override;
699 
700  // Documentation inherited
701  protected: void mouseReleaseEvent(QMouseEvent *_e) override;
702 
703  // Documentation inherited
704  protected: void mouseMoveEvent(QMouseEvent *_e) override;
705 
706  // Documentation inherited
707  protected: void wheelEvent(QWheelEvent *_e) override;
708 
715  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
716  QQuickItem::UpdatePaintNodeData *_data) override;
717 
722  signals: void openContextMenu(QString _entity); // NOLINT
723 
726  public slots: void OnContextMenuRequested(QString _entity);
727 
731  };
732 
734  class TextureNode : public QObject, public QSGSimpleTextureNode
735  {
736  Q_OBJECT
737 
742  public: explicit TextureNode(QQuickWindow *_window,
743  RenderSync &_renderSync);
744 
746  public: ~TextureNode() override;
747 
752  public slots: void NewTexture(uint _id, const QSize &_size);
753 
756  public slots: void PrepareNode();
757 
761  signals: void TextureInUse(RenderSync *_renderSync);
762 
765  signals: void PendingNewTexture();
766 
768  public: uint id = 0;
769 
771  public: QSize size = QSize(0, 0);
772 
774  public: QMutex mutex;
775 
777  public: RenderSync &renderSync;
778 
780  public: QSGTexture *texture = nullptr;
781 
783  public: QQuickWindow *window = nullptr;
784  };
785 }
786 }
787 }
788 
789 #endif
std::string worldName
Name of the world.
Definition: Scene3D.hh:473
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: Scene3D.hh:524
Texture node for displaying the render texture from ign-renderer.
Definition: Scene3D.hh:734
A QQUickItem that manages the render window.
Definition: Scene3D.hh:529
Information passed to systems on the update callback.
Definition: Types.hh:36
QMutex mutex
Mutex to protect the texture variables.
Definition: Scene3D.hh:774
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:184
Pose3< double > Pose3d
static const Vector2< T > Zero
Rendering thread.
Definition: Scene3D.hh:493
RenderSync & renderSync
See RenderSync.
Definition: Scene3D.hh:777
GLuint textureId
Render texture id Values is constantly constantly cycled/swapped/changed from a worker thread Don&#39;t r...
Definition: Scene3D.hh:467
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