Ignition Gazebo

API Reference

4.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  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 
206  public: void SetRecordVideo(bool _record, const std::string &_format,
207  const std::string &_savePath);
208 
211  public: void SetMoveTo(const std::string &_target);
212 
218  public: void SetFollowTarget(const std::string &_target,
219  bool _waitForTarget = false);
220 
225  public: void SetViewAngle(const math::Vector3d &_direction);
226 
229  public: void SetMoveToPose(const math::Pose3d &_pose);
230 
233  public: void SetFollowPGain(double _gain);
234 
238  public: void SetFollowWorldFrame(bool _worldFrame);
239 
242  public: void SetFollowOffset(const math::Vector3d &_offset);
243 
246  public: std::string FollowTarget() const;
247 
250  public: bool FollowWorldFrame() const;
251 
254  public: math::Vector3d FollowOffset() const;
255 
258  public: void SetInitCameraPose(const math::Pose3d &_pose);
259 
263  public: void NewMouseEvent(const common::MouseEvent &_e,
264  const math::Vector2d &_drag = math::Vector2d::Zero);
265 
268  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
269 
272  public: void HandleKeyPress(QKeyEvent *_e);
273 
276  public: void HandleKeyRelease(QKeyEvent *_e);
277 
280  public: void SetXYZSnap(const math::Vector3d &_xyz);
281 
284  public: math::Vector3d XYZSnap() const;
285 
288  public: void SetRPYSnap(const math::Vector3d &_rpy);
289 
292  public: math::Vector3d RPYSnap() const;
293 
296  public: void SetScaleSnap(const math::Vector3d &_scale);
297 
300  public: math::Vector3d ScaleSnap() const;
301 
309  public: void SnapPoint(
310  math::Vector3d &_point,
311  math::Vector3d &_snapVals, double _sensitivity = 0.4) const;
312 
318  public: void RequestSelectionChange(Entity _selectedEntity,
319  bool _deselectAll, bool _sendEvent);
320 
329  private: double SnapValue(
330  double _coord, double _interval, double _sensitivity) const;
331 
334  private: void XYZConstraint(math::Vector3d &_axis);
335 
337  private: void HandleMouseEvent();
338 
340  private: void HandleMouseContextMenu();
341 
343  private: void HandleMouseViewControl();
344 
346  private: void HandleMouseTransformControl();
347 
349  private: void HandleEntitySelection();
350 
352  private: void HandleModelPlacement();
353 
355  private: void BroadcastHoverPos();
356 
358  private: void BroadcastLeftClick();
359 
362  private: Entity UniqueId();
363 
367  public: bool GeneratePreview(const sdf::Root &_sdf);
368 
370  public: void TerminateSpawnPreview();
371 
376  public: math::Vector3d ScreenToPlane(const math::Vector2i &_screenPos)
377  const;
378 
383  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
384  const;
385 
388  public: math::Pose3d CameraPose() const;
389 
391  private: void OnMoveToComplete();
392 
394  private: void OnViewAngleComplete();
395 
397  private: void OnMoveToPoseComplete();
398 
403  private: void UpdateSelectedEntity(const rendering::NodePtr &_node,
404  bool _sendEvent);
405 
409  private: void DeselectAllEntities(bool _sendEvent);
410 
412  signals: void ContextMenuRequested(QString _entity);
413 
418  signals: void FollowTargetChanged(const std::string &_target,
419  bool _waitForTarget);
420 
422  public: GLuint textureId = 0u;
423 
425  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
426 
429 
431  public: uint32_t visibilityMask = 0xFFFFFFFFu;
432 
434  public: bool initialized = false;
435 
437  public: QSize textureSize = QSize(1024, 1024);
438 
440  public: bool textureDirty = false;
441 
444  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
445  };
446 
448  class RenderThread : public QThread
449  {
450  Q_OBJECT
451 
453  public: RenderThread();
454 
456  public slots: void RenderNext();
457 
459  public slots: void ShutDown();
460 
462  public slots: void SizeChanged();
463 
468  signals: void TextureReady(int _id, const QSize &_size);
469 
471  public: QOffscreenSurface *surface = nullptr;
472 
474  public: QOpenGLContext *context = nullptr;
475 
478  };
479 
480 
482  class RenderWindowItem : public QQuickItem
483  {
484  Q_OBJECT
485 
488  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
489 
491  public: ~RenderWindowItem() override;
492 
494  public: class RenderUtil *RenderUtil() const;
495 
498  public: void SetCameraPose(const math::Pose3d &_pose);
499 
502  public: math::Pose3d CameraPose() const;
503 
506  public: void SetInitCameraPose(const math::Pose3d &_pose);
507 
510  public: void SetVisibilityMask(uint32_t _mask);
511 
514  public: void SetTransformMode(const std::string &_mode);
515 
518  public: void SetModel(const std::string &_model);
519 
522  public: void SetModelPath(const std::string &_filePath);
523 
528  public: void SetRecordVideo(bool _record, const std::string &_format,
529  const std::string &_savePath);
530 
533  public: void SetMoveTo(const std::string &_target);
534 
540  public Q_SLOTS: void SetFollowTarget(const std::string &_target,
541  bool _waitForTarget = false);
542 
547  public: void SetViewAngle(const math::Vector3d &_direction);
548 
551  public: void SetMoveToPose(const math::Pose3d &_pose);
552 
555  public: void SetFollowPGain(double _gain);
556 
560  public: void SetFollowWorldFrame(bool _worldFrame);
561 
564  public: void SetFollowOffset(const math::Vector3d &_offset);
565 
568  public: void SetWorldName(const std::string &_name);
569 
580  public: void UpdateSelectedEntity(Entity _entity,
581  bool _sendEvent);
582 
587  public: void DeselectAllEntities(bool _sendEvent);
588 
591  public: void SetXYZSnap(const math::Vector3d &_xyz);
592 
595  public: void SetRPYSnap(const math::Vector3d &_rpy);
596 
599  public: void SetScaleSnap(const math::Vector3d &_scale);
600 
605  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos);
606 
610  public: void OnHovered(const ignition::math::Vector2i &_hoverPos);
611 
613  public Q_SLOTS: void Ready();
614 
617  public: void HandleKeyPress(QKeyEvent *_e);
618 
621  public: void HandleKeyRelease(QKeyEvent *_e);
622 
623  // Documentation inherited
624  protected: void mousePressEvent(QMouseEvent *_e) override;
625 
626  // Documentation inherited
627  protected: void mouseReleaseEvent(QMouseEvent *_e) override;
628 
629  // Documentation inherited
630  protected: void mouseMoveEvent(QMouseEvent *_e) override;
631 
632  // Documentation inherited
633  protected: void wheelEvent(QWheelEvent *_e) override;
634 
641  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
642  QQuickItem::UpdatePaintNodeData *_data) override;
643 
648  signals: void openContextMenu(QString _entity); // NOLINT
649 
652  public slots: void OnContextMenuRequested(QString _entity);
653 
657  };
658 
660  class TextureNode : public QObject, public QSGSimpleTextureNode
661  {
662  Q_OBJECT
663 
666  public: explicit TextureNode(QQuickWindow *_window);
667 
669  public: ~TextureNode() override;
670 
675  public slots: void NewTexture(int _id, const QSize &_size);
676 
679  public slots: void PrepareNode();
680 
683  signals: void TextureInUse();
684 
687  signals: void PendingNewTexture();
688 
690  public: int id = 0;
691 
693  public: QSize size = QSize(0, 0);
694 
696  public: QMutex mutex;
697 
699  public: QSGTexture *texture = nullptr;
700 
702  public: QQuickWindow *window = nullptr;
703  };
704 }
705 }
706 }
707 
708 #endif
std::string worldName
Name of the world.
Definition: Scene3D.hh:428
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: Scene3D.hh:477
Texture node for displaying the render texture from ign-renderer.
Definition: Scene3D.hh:660
A QQUickItem that manages the render window.
Definition: Scene3D.hh:482
Information passed to systems on the update callback.
Definition: Types.hh:36
QMutex mutex
Mutex to protect the texture variables.
Definition: Scene3D.hh:696
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:448
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