Gazebo Gui

API Reference

6.8.0
Scene3D.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 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 GZ_GUI_PLUGINS_SCENE3D_HH_
19 #define GZ_GUI_PLUGINS_SCENE3D_HH_
20 
21 #include <string>
22 #include <memory>
23 #include <mutex>
24 
25 #include <gz/math/Color.hh>
26 #include <gz/math/Pose3.hh>
27 #include <gz/math/Vector2.hh>
28 #include <gz/math/Vector3.hh>
29 
30 #include <gz/common/MouseEvent.hh>
31 
32 #include "gz/gui/qt.h"
33 #include "gz/gui/Plugin.hh"
34 
35 namespace ignition
36 {
37 namespace gui
38 {
39 namespace plugins
40 {
41  class IgnRendererPrivate;
42  class RenderWindowItemPrivate;
43  class Scene3DPrivate;
44 
66  class Scene3D : public Plugin
67  {
68  Q_OBJECT
69 
71  Q_PROPERTY(
73  READ LoadingError
74  WRITE SetLoadingError
75  NOTIFY LoadingErrorChanged
76  )
77 
78 
79  public: Scene3D();
80 
82  public: virtual ~Scene3D();
83 
87  public slots: void OnHovered(int _mouseX, int _mouseY);
88 
91  public slots: void OnFocusWindow();
92 
93  // Documentation inherited
94  protected: bool eventFilter(QObject *_obj, QEvent *_event) override;
95 
96  // Documentation inherited
97  public: virtual void LoadConfig(const tinyxml2::XMLElement *_pluginElem)
98  override;
99 
102  public: Q_INVOKABLE QString LoadingError() const;
103 
106  public: Q_INVOKABLE void SetLoadingError(const QString &_loadingError);
107 
109  signals: void LoadingErrorChanged();
110 
112  public: QString loadingError;
113 
116  private: std::unique_ptr<Scene3DPrivate> dataPtr;
117  };
118 
125  class IgnRenderer
126  {
128  public: IgnRenderer();
129 
131  public: ~IgnRenderer();
132 
134  public: void Render();
135 
139  public: std::string Initialize();
140 
142  public: void Destroy();
143 
147  public: void NewMouseEvent(const common::MouseEvent &_e,
148  const math::Vector2d &_drag = math::Vector2d::Zero);
149 
152  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
153 
156  public: void HandleKeyPress(QKeyEvent *_e);
157 
160  public: void HandleKeyRelease(QKeyEvent *_e);
161 
163  private: void HandleMouseEvent();
164 
166  private: void HandleMouseViewControl();
167 
169  private: void BroadcastHoverPos();
170 
172  private: void BroadcastLeftClick();
173 
175  private: void BroadcastRightClick();
176 
178  private: void BroadcastKeyRelease();
179 
181  private: void BroadcastKeyPress();
182 
187  private: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
188  const;
189 
191  public: GLuint textureId = 0u;
192 
194  public: std::string engineName = "ogre";
195 
197  public: std::string sceneName = "scene";
198 
200  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
201 
203  public: math::Color backgroundColor = math::Color::Black;
204 
206  public: math::Color ambientLight = math::Color(0.3f, 0.3f, 0.3f, 1.0f);
207 
209  public: bool initialized = false;
210 
212  public: QSize textureSize = QSize(1024, 1024);
213 
215  public: bool textureDirty = false;
216 
221 
225 
228 
233 
236  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
237  };
238 
240  class RenderThread : public QThread
241  {
242  Q_OBJECT
243 
245  public: RenderThread();
246 
248  public slots: void RenderNext();
249 
251  public slots: void ShutDown();
252 
254  public slots: void SizeChanged();
255 
260  signals: void TextureReady(int _id, const QSize &_size);
261 
264  public: void SetErrorCb(std::function<void(const QString &)> _cb);
265 
267  public: std::function<void(const QString &)> errorCb;
268 
270  public: QOffscreenSurface *surface = nullptr;
271 
273  public: QOpenGLContext *context = nullptr;
274 
276  public: IgnRenderer ignRenderer;
277  };
278 
279 
281  class RenderWindowItem : public QQuickItem
282  {
283  Q_OBJECT
284 
287  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
288 
290  public: virtual ~RenderWindowItem();
291 
294  public: void SetBackgroundColor(const math::Color &_color);
295 
298  public: void SetAmbientLight(const math::Color &_ambient);
299 
302  public: void SetEngineName(const std::string &_name);
303 
306  public: void SetSceneName(const std::string &_name);
307 
310  public: void SetCameraPose(const math::Pose3d &_pose);
311 
316  public: void SetSceneService(const std::string &_service);
317 
322  public: void SetPoseTopic(const std::string &_topic);
323 
328  public: void SetDeletionTopic(const std::string &_topic);
329 
333  public: void SetSceneTopic(const std::string &_topic);
334 
338  public: void OnHovered(const gz::math::Vector2i &_hoverPos);
339 
341  public Q_SLOTS: void Ready();
342 
345  public: void HandleKeyPress(QKeyEvent *_e);
346 
349  public: void HandleKeyRelease(QKeyEvent *_e);
350 
351  // Documentation inherited
352  protected: virtual void mousePressEvent(QMouseEvent *_e) override;
353 
354  // Documentation inherited
355  protected: virtual void mouseReleaseEvent(QMouseEvent *_e) override;
356 
357  // Documentation inherited
358  protected: virtual void mouseMoveEvent(QMouseEvent *_e) override;
359 
360  // Documentation inherited
361  protected: virtual void wheelEvent(QWheelEvent *_e) override;
362 
363  // Documentation inherited
364  protected: virtual void keyPressEvent(QKeyEvent *_event) override;
365 
366  // Documentation inherited
367  protected: virtual void keyReleaseEvent(QKeyEvent *_event) override;
368 
375  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
376  QQuickItem::UpdatePaintNodeData *_data) override;
377 
380  public: void SetErrorCb(std::function<void(const QString &)> _cb);
381 
385  };
386 
388  class TextureNode : public QObject, public QSGSimpleTextureNode
389  {
390  Q_OBJECT
391 
394  public: explicit TextureNode(QQuickWindow *_window);
395 
397  public: ~TextureNode() override;
398 
403  public slots: void NewTexture(int _id, const QSize &_size);
404 
407  public slots: void PrepareNode();
408 
411  signals: void TextureInUse();
412 
415  signals: void PendingNewTexture();
416 
418  public: int id = 0;
419 
421  public: QSize size = QSize(0, 0);
422 
424  public: QMutex mutex;
425 
427  public: QSGTexture *texture = nullptr;
428 
430  public: QQuickWindow *window = nullptr;
431  };
432 }
433 }
434 }
435 
436 #endif
QString loadingError
Loading error message.
Definition: Scene3D.hh:76
STL class.
virtual void LoadConfig(const tinyxml2::XMLElement *_pluginElem) override
Load the plugin with a configuration file. Override this on custom plugins to handle custom configura...
std::string sceneService
Scene service. If not empty, a request will be made to get the scene information using this service a...
Definition: Scene3D.hh:220
void OnFocusWindow()
Callback when the mouse enters the render window to focus the window for mouse/key events.
static const Color Black
bool eventFilter(QObject *_obj, QEvent *_event) override
Pose3< double > Pose3d
static const Vector2< T > Zero
Base class for Gazebo GUI plugins.
Definition: gz/gui/Plugin.hh:54
void LoadingErrorChanged()
Notify that loading error has changed.
std::string poseTopic
Scene pose topic. If not empty, a node will subcribe to this topic to get pose updates of objects in ...
Definition: Scene3D.hh:224
std::string deletionTopic
Ign-transport deletion topic name.
Definition: Scene3D.hh:227
Creates an ignition rendering scene and user camera. It is possible to orbit the camera around the sc...
Definition: Scene3D.hh:66
Rendering thread.
Definition: MinimalScene.hh:256
STL namespace.
Ign-rendering renderer. All ign-rendering calls should be performed inside this class as it makes sur...
Definition: MinimalScene.hh:130
void OnHovered(int _mouseX, int _mouseY)
Callback when the mouse hovers to a new position.
Definition: gz/gui/Application.hh:35
std::string sceneTopic
Ign-transport scene topic name New scene messages will be published to this topic when an entities ar...
Definition: Scene3D.hh:232
Q_INVOKABLE QString LoadingError() const
Get the loading error string.
Q_INVOKABLE void SetLoadingError(const QString &_loadingError)
Set the loading error message.