Ignition Gazebo

API Reference

3.5.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 OnViewAngle(const msgs::Vector3d &_msg,
148  msgs::Boolean &_res);
149 
154  private: bool OnMoveToPose(const msgs::GUICamera &_msg,
155  msgs::Boolean &_res);
156 
159  private: std::unique_ptr<Scene3DPrivate> dataPtr;
160  };
161 
168  class IgnRenderer : public QObject
169  {
170  Q_OBJECT
171 
173  public: IgnRenderer();
174 
176  public: ~IgnRenderer() override;
177 
179  public: void Render();
180 
182  public: void Initialize();
183 
185  public: void Destroy();
186 
188  public: class RenderUtil *RenderUtil() const;
189 
192  public: void SetTransformMode(const std::string &_mode);
193 
196  public: void SetModel(const std::string &_model);
197 
200  public: void SetModelPath(const std::string &_filePath);
201 
205  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
206 
211  public: void SetRecordVideo(bool _record, const std::string &_format,
212  const std::string &_savePath);
213 
216  public: void SetRecordVideoUseSimTime(bool _useSimTime);
217 
220  public: void SetRecordVideoLockstep(bool _lockstep);
221 
224  public: void SetRecordVideoBitrate(unsigned int _bitrate);
225 
228  public: void SetMoveTo(const std::string &_target);
229 
235  public: void SetFollowTarget(const std::string &_target,
236  bool _waitForTarget = false);
237 
242  public: void SetViewAngle(const math::Vector3d &_direction);
243 
246  public: void SetMoveToPose(const math::Pose3d &_pose);
247 
250  public: void SetFollowPGain(double _gain);
251 
255  public: void SetFollowWorldFrame(bool _worldFrame);
256 
259  public: void SetFollowOffset(const math::Vector3d &_offset);
260 
263  public: std::string FollowTarget() const;
264 
267  public: bool FollowWorldFrame() const;
268 
271  public: math::Vector3d FollowOffset() const;
272 
275  public: void SetInitCameraPose(const math::Pose3d &_pose);
276 
280  public: void NewMouseEvent(const common::MouseEvent &_e,
281  const math::Vector2d &_drag = math::Vector2d::Zero);
282 
285  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
286 
289  public: void HandleKeyPress(QKeyEvent *_e);
290 
293  public: void HandleKeyRelease(QKeyEvent *_e);
294 
297  public: void SetXYZSnap(const math::Vector3d &_xyz);
298 
301  public: math::Vector3d XYZSnap() const;
302 
305  public: void SetRPYSnap(const math::Vector3d &_rpy);
306 
309  public: math::Vector3d RPYSnap() const;
310 
313  public: void SetScaleSnap(const math::Vector3d &_scale);
314 
317  public: math::Vector3d ScaleSnap() const;
318 
326  public: void SnapPoint(
327  math::Vector3d &_point,
328  math::Vector3d &_snapVals, double _sensitivity = 0.4) const;
329 
335  public: void RequestSelectionChange(Entity _selectedEntity,
336  bool _deselectAll, bool _sendEvent);
337 
346  private: double SnapValue(
347  double _coord, double _interval, double _sensitivity) const;
348 
351  private: void XYZConstraint(math::Vector3d &_axis);
352 
354  private: void HandleMouseEvent();
355 
357  private: void HandleMouseContextMenu();
358 
360  private: void HandleMouseViewControl();
361 
363  private: void HandleMouseTransformControl();
364 
366  private: void HandleEntitySelection();
367 
369  private: void HandleModelPlacement();
370 
372  private: void BroadcastHoverPos();
373 
375  private: void BroadcastLeftClick();
376 
378  private: void BroadcastRightClick();
379 
382  private: Entity UniqueId();
383 
387  public: bool GeneratePreview(const sdf::Root &_sdf);
388 
390  public: void TerminateSpawnPreview();
391 
396  public: math::Vector3d ScreenToPlane(const math::Vector2i &_screenPos)
397  const;
398 
403  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
404  const;
405 
408  public: math::Pose3d CameraPose() const;
409 
411  private: void OnMoveToComplete();
412 
414  private: void OnViewAngleComplete();
415 
417  private: void OnMoveToPoseComplete();
418 
423  private: void UpdateSelectedEntity(const rendering::NodePtr &_node,
424  bool _sendEvent);
425 
429  private: void DeselectAllEntities(bool _sendEvent);
430 
432  signals: void ContextMenuRequested(QString _entity);
433 
438  signals: void FollowTargetChanged(const std::string &_target,
439  bool _waitForTarget);
440 
442  public: GLuint textureId = 0u;
443 
445  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
446 
449 
451  public: bool initialized = false;
452 
454  public: QSize textureSize = QSize(1024, 1024);
455 
457  public: bool textureDirty = false;
458 
461  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
462  };
463 
465  class RenderThread : public QThread
466  {
467  Q_OBJECT
468 
470  public: RenderThread();
471 
473  public slots: void RenderNext();
474 
476  public slots: void ShutDown();
477 
479  public slots: void SizeChanged();
480 
485  signals: void TextureReady(int _id, const QSize &_size);
486 
488  public: QOffscreenSurface *surface = nullptr;
489 
491  public: QOpenGLContext *context = nullptr;
492 
495  };
496 
497 
499  class RenderWindowItem : public QQuickItem
500  {
501  Q_OBJECT
502 
505  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
506 
508  public: ~RenderWindowItem() override;
509 
511  public: class RenderUtil *RenderUtil() const;
512 
515  public: void SetCameraPose(const math::Pose3d &_pose);
516 
519  public: math::Pose3d CameraPose() const;
520 
523  public: void SetInitCameraPose(const math::Pose3d &_pose);
524 
527  public: void SetTransformMode(const std::string &_mode);
528 
531  public: void SetModel(const std::string &_model);
532 
535  public: void SetModelPath(const std::string &_filePath);
536 
540  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
541 
546  public: void SetRecordVideo(bool _record, const std::string &_format,
547  const std::string &_savePath);
548 
551  public: void SetRecordVideoUseSimTime(bool _useSimTime);
552 
555  public: void SetRecordVideoLockstep(bool _lockstep);
556 
559  public: void SetRecordVideoBitrate(unsigned int _bitrate);
560 
563  public: void SetMoveTo(const std::string &_target);
564 
570  public Q_SLOTS: void SetFollowTarget(const std::string &_target,
571  bool _waitForTarget = false);
572 
577  public: void SetViewAngle(const math::Vector3d &_direction);
578 
581  public: void SetMoveToPose(const math::Pose3d &_pose);
582 
585  public: void SetFollowPGain(double _gain);
586 
590  public: void SetFollowWorldFrame(bool _worldFrame);
591 
594  public: void SetFollowOffset(const math::Vector3d &_offset);
595 
598  public: void SetWorldName(const std::string &_name);
599 
610  public: void UpdateSelectedEntity(Entity _entity,
611  bool _sendEvent);
612 
617  public: void DeselectAllEntities(bool _sendEvent);
618 
621  public: void SetXYZSnap(const math::Vector3d &_xyz);
622 
625  public: void SetRPYSnap(const math::Vector3d &_rpy);
626 
629  public: void SetScaleSnap(const math::Vector3d &_scale);
630 
635  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos);
636 
640  public: void OnHovered(const ignition::math::Vector2i &_hoverPos);
641 
646  public: bool RendererInitialized() const;
647 
649  public Q_SLOTS: void Ready();
650 
653  public: void HandleKeyPress(QKeyEvent *_e);
654 
657  public: void HandleKeyRelease(QKeyEvent *_e);
658 
659  // Documentation inherited
660  protected: void mousePressEvent(QMouseEvent *_e) override;
661 
662  // Documentation inherited
663  protected: void mouseReleaseEvent(QMouseEvent *_e) override;
664 
665  // Documentation inherited
666  protected: void mouseMoveEvent(QMouseEvent *_e) override;
667 
668  // Documentation inherited
669  protected: void wheelEvent(QWheelEvent *_e) override;
670 
677  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
678  QQuickItem::UpdatePaintNodeData *_data) override;
679 
684  signals: void openContextMenu(QString _entity); // NOLINT
685 
688  public slots: void OnContextMenuRequested(QString _entity);
689 
693  };
694 
696  class TextureNode : public QObject, public QSGSimpleTextureNode
697  {
698  Q_OBJECT
699 
702  public: explicit TextureNode(QQuickWindow *_window);
703 
705  public: ~TextureNode() override;
706 
711  public slots: void NewTexture(int _id, const QSize &_size);
712 
715  public slots: void PrepareNode();
716 
719  signals: void TextureInUse();
720 
723  signals: void PendingNewTexture();
724 
726  public: int id = 0;
727 
729  public: QSize size = QSize(0, 0);
730 
732  public: QMutex mutex;
733 
735  public: QSGTexture *texture = nullptr;
736 
738  public: QQuickWindow *window = nullptr;
739  };
740 }
741 }
742 }
743 
744 #endif
std::string worldName
Name of the world.
Definition: Scene3D.hh:448
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: Scene3D.hh:494
Texture node for displaying the render texture from ign-renderer.
Definition: Scene3D.hh:696
A QQUickItem that manages the render window.
Definition: Scene3D.hh:499
Information passed to systems on the update callback.
Definition: Types.hh:37
QMutex mutex
Mutex to protect the texture variables.
Definition: Scene3D.hh:732
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:168
Pose3< double > Pose3d
static const Vector2< T > Zero
Rendering thread.
Definition: Scene3D.hh:465
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