Ignition Gazebo

API Reference

3.7.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 
161  private: bool OnViewCollisions(const msgs::StringMsg &_msg,
162  msgs::Boolean &_res);
163 
166  private: std::unique_ptr<Scene3DPrivate> dataPtr;
167  };
168 
175  class IgnRenderer : public QObject
176  {
177  Q_OBJECT
178 
180  public: IgnRenderer();
181 
183  public: ~IgnRenderer() override;
184 
186  public: void Render();
187 
189  public: void Initialize();
190 
192  public: void Destroy();
193 
195  public: class RenderUtil *RenderUtil() const;
196 
199  public: void SetTransformMode(const std::string &_mode);
200 
203  public: void SetModel(const std::string &_model);
204 
207  public: void SetModelPath(const std::string &_filePath);
208 
212  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
213 
218  public: void SetRecordVideo(bool _record, const std::string &_format,
219  const std::string &_savePath);
220 
223  public: void SetRecordVideoUseSimTime(bool _useSimTime);
224 
227  public: void SetRecordVideoLockstep(bool _lockstep);
228 
231  public: void SetRecordVideoBitrate(unsigned int _bitrate);
232 
235  public: void SetMoveTo(const std::string &_target);
236 
242  public: void SetFollowTarget(const std::string &_target,
243  bool _waitForTarget = false);
244 
249  public: void SetViewAngle(const math::Vector3d &_direction);
250 
253  public: void SetMoveToPose(const math::Pose3d &_pose);
254 
257  public: void SetViewCollisionsTarget(const std::string &_target);
258 
261  public: void SetFollowPGain(double _gain);
262 
266  public: void SetFollowWorldFrame(bool _worldFrame);
267 
270  public: void SetFollowOffset(const math::Vector3d &_offset);
271 
274  public: std::string FollowTarget() const;
275 
278  public: bool FollowWorldFrame() const;
279 
282  public: math::Vector3d FollowOffset() const;
283 
286  public: void SetInitCameraPose(const math::Pose3d &_pose);
287 
291  public: void NewMouseEvent(const common::MouseEvent &_e,
292  const math::Vector2d &_drag = math::Vector2d::Zero);
293 
296  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
297 
300  public: void HandleKeyPress(QKeyEvent *_e);
301 
304  public: void HandleKeyRelease(QKeyEvent *_e);
305 
308  public: void SetXYZSnap(const math::Vector3d &_xyz);
309 
312  public: math::Vector3d XYZSnap() const;
313 
316  public: void SetRPYSnap(const math::Vector3d &_rpy);
317 
320  public: math::Vector3d RPYSnap() const;
321 
324  public: void SetScaleSnap(const math::Vector3d &_scale);
325 
328  public: math::Vector3d ScaleSnap() const;
329 
337  public: void SnapPoint(
338  math::Vector3d &_point,
339  math::Vector3d &_snapVals, double _sensitivity = 0.4) const;
340 
346  public: void RequestSelectionChange(Entity _selectedEntity,
347  bool _deselectAll, bool _sendEvent);
348 
357  private: double SnapValue(
358  double _coord, double _interval, double _sensitivity) const;
359 
362  private: void XYZConstraint(math::Vector3d &_axis);
363 
365  private: void HandleMouseEvent();
366 
368  private: void HandleMouseContextMenu();
369 
371  private: void HandleMouseViewControl();
372 
374  private: void HandleMouseTransformControl();
375 
377  private: void HandleEntitySelection();
378 
380  private: void HandleModelPlacement();
381 
383  private: void BroadcastHoverPos();
384 
386  private: void BroadcastLeftClick();
387 
389  private: void BroadcastRightClick();
390 
393  private: Entity UniqueId();
394 
398  public: bool GeneratePreview(const sdf::Root &_sdf);
399 
401  public: void TerminateSpawnPreview();
402 
407  public: math::Vector3d ScreenToPlane(const math::Vector2i &_screenPos)
408  const;
409 
414  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
415  const;
416 
419  public: math::Pose3d CameraPose() const;
420 
422  private: void OnMoveToComplete();
423 
425  private: void OnViewAngleComplete();
426 
428  private: void OnMoveToPoseComplete();
429 
434  private: void UpdateSelectedEntity(const rendering::NodePtr &_node,
435  bool _sendEvent);
436 
440  private: void DeselectAllEntities(bool _sendEvent);
441 
443  signals: void ContextMenuRequested(QString _entity);
444 
449  signals: void FollowTargetChanged(const std::string &_target,
450  bool _waitForTarget);
451 
453  public: GLuint textureId = 0u;
454 
456  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
457 
460 
462  public: bool initialized = false;
463 
465  public: QSize textureSize = QSize(1024, 1024);
466 
468  public: bool textureDirty = false;
469 
472  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
473  };
474 
476  class RenderThread : public QThread
477  {
478  Q_OBJECT
479 
481  public: RenderThread();
482 
484  public slots: void RenderNext();
485 
487  public slots: void ShutDown();
488 
490  public slots: void SizeChanged();
491 
496  signals: void TextureReady(int _id, const QSize &_size);
497 
499  public: QOffscreenSurface *surface = nullptr;
500 
502  public: QOpenGLContext *context = nullptr;
503 
506  };
507 
508 
510  class RenderWindowItem : public QQuickItem
511  {
512  Q_OBJECT
513 
516  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
517 
519  public: ~RenderWindowItem() override;
520 
522  public: class RenderUtil *RenderUtil() const;
523 
526  public: void SetCameraPose(const math::Pose3d &_pose);
527 
530  public: math::Pose3d CameraPose() const;
531 
534  public: void SetInitCameraPose(const math::Pose3d &_pose);
535 
538  public: void SetTransformMode(const std::string &_mode);
539 
542  public: void SetModel(const std::string &_model);
543 
546  public: void SetModelPath(const std::string &_filePath);
547 
551  public: void SetDropdownMenuEnabled(bool _enableDropdownMenu);
552 
557  public: void SetRecordVideo(bool _record, const std::string &_format,
558  const std::string &_savePath);
559 
562  public: void SetRecordVideoUseSimTime(bool _useSimTime);
563 
566  public: void SetRecordVideoLockstep(bool _lockstep);
567 
570  public: void SetRecordVideoBitrate(unsigned int _bitrate);
571 
574  public: void SetMoveTo(const std::string &_target);
575 
581  public Q_SLOTS: void SetFollowTarget(const std::string &_target,
582  bool _waitForTarget = false);
583 
588  public: void SetViewAngle(const math::Vector3d &_direction);
589 
592  public: void SetMoveToPose(const math::Pose3d &_pose);
593 
596  public: void SetViewCollisionsTarget(const std::string &_target);
597 
600  public: void SetFollowPGain(double _gain);
601 
605  public: void SetFollowWorldFrame(bool _worldFrame);
606 
609  public: void SetFollowOffset(const math::Vector3d &_offset);
610 
613  public: void SetWorldName(const std::string &_name);
614 
625  public: void UpdateSelectedEntity(Entity _entity,
626  bool _sendEvent);
627 
632  public: void DeselectAllEntities(bool _sendEvent);
633 
636  public: void SetXYZSnap(const math::Vector3d &_xyz);
637 
640  public: void SetRPYSnap(const math::Vector3d &_rpy);
641 
644  public: void SetScaleSnap(const math::Vector3d &_scale);
645 
650  public: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos);
651 
655  public: void OnHovered(const ignition::math::Vector2i &_hoverPos);
656 
661  public: bool RendererInitialized() const;
662 
664  public Q_SLOTS: void Ready();
665 
668  public: void HandleKeyPress(QKeyEvent *_e);
669 
672  public: void HandleKeyRelease(QKeyEvent *_e);
673 
674  // Documentation inherited
675  protected: void mousePressEvent(QMouseEvent *_e) override;
676 
677  // Documentation inherited
678  protected: void mouseReleaseEvent(QMouseEvent *_e) override;
679 
680  // Documentation inherited
681  protected: void mouseMoveEvent(QMouseEvent *_e) override;
682 
683  // Documentation inherited
684  protected: void wheelEvent(QWheelEvent *_e) override;
685 
692  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
693  QQuickItem::UpdatePaintNodeData *_data) override;
694 
699  signals: void openContextMenu(QString _entity); // NOLINT
700 
703  public slots: void OnContextMenuRequested(QString _entity);
704 
708  };
709 
711  class TextureNode : public QObject, public QSGSimpleTextureNode
712  {
713  Q_OBJECT
714 
717  public: explicit TextureNode(QQuickWindow *_window);
718 
720  public: ~TextureNode() override;
721 
726  public slots: void NewTexture(int _id, const QSize &_size);
727 
730  public slots: void PrepareNode();
731 
734  signals: void TextureInUse();
735 
738  signals: void PendingNewTexture();
739 
741  public: int id = 0;
742 
744  public: QSize size = QSize(0, 0);
745 
747  public: QMutex mutex;
748 
750  public: QSGTexture *texture = nullptr;
751 
753  public: QQuickWindow *window = nullptr;
754  };
755 }
756 }
757 }
758 
759 #endif
std::string worldName
Name of the world.
Definition: Scene3D.hh:459
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: Scene3D.hh:505
Texture node for displaying the render texture from ign-renderer.
Definition: Scene3D.hh:711
A QQUickItem that manages the render window.
Definition: Scene3D.hh:510
Information passed to systems on the update callback.
Definition: Types.hh:37
QMutex mutex
Mutex to protect the texture variables.
Definition: Scene3D.hh:747
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:175
Pose3< double > Pose3d
static const Vector2< T > Zero
Rendering thread.
Definition: Scene3D.hh:476
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