Gazebo Gui

API Reference

3.12.1
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 
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 
181  private: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
182  const;
183 
185  public: GLuint textureId = 0u;
186 
188  public: std::string engineName = "ogre";
189 
191  public: std::string sceneName = "scene";
192 
194  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
195 
197  public: math::Color backgroundColor = math::Color::Black;
198 
200  public: math::Color ambientLight = math::Color(0.3f, 0.3f, 0.3f, 1.0f);
201 
203  public: bool initialized = false;
204 
206  public: QSize textureSize = QSize(1024, 1024);
207 
209  public: bool textureDirty = false;
210 
215 
219 
222 
227 
230  private: std::unique_ptr<IgnRendererPrivate> dataPtr;
231  };
232 
234  class RenderThread : public QThread
235  {
236  Q_OBJECT
237 
239  public: RenderThread();
240 
242  public slots: void RenderNext();
243 
245  public slots: void ShutDown();
246 
248  public slots: void SizeChanged();
249 
254  signals: void TextureReady(int _id, const QSize &_size);
255 
258  public: void SetErrorCb(std::function<void(const QString &)> _cb);
259 
261  public: std::function<void(const QString &)> errorCb;
262 
264  public: QOffscreenSurface *surface = nullptr;
265 
267  public: QOpenGLContext *context = nullptr;
268 
271  };
272 
273 
275  class RenderWindowItem : public QQuickItem
276  {
277  Q_OBJECT
278 
281  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
282 
284  public: virtual ~RenderWindowItem();
285 
288  public: void SetBackgroundColor(const math::Color &_color);
289 
292  public: void SetAmbientLight(const math::Color &_ambient);
293 
296  public: void SetEngineName(const std::string &_name);
297 
300  public: void SetSceneName(const std::string &_name);
301 
304  public: void SetCameraPose(const math::Pose3d &_pose);
305 
310  public: void SetSceneService(const std::string &_service);
311 
316  public: void SetPoseTopic(const std::string &_topic);
317 
322  public: void SetDeletionTopic(const std::string &_topic);
323 
327  public: void SetSceneTopic(const std::string &_topic);
328 
332  public: void OnHovered(const gz::math::Vector2i &_hoverPos);
333 
335  public Q_SLOTS: void Ready();
336 
339  public: void HandleKeyPress(QKeyEvent *_e);
340 
343  public: void HandleKeyRelease(QKeyEvent *_e);
344 
345  // Documentation inherited
346  protected: virtual void mousePressEvent(QMouseEvent *_e) override;
347 
348  // Documentation inherited
349  protected: virtual void mouseReleaseEvent(QMouseEvent *_e) override;
350 
351  // Documentation inherited
352  protected: virtual void mouseMoveEvent(QMouseEvent *_e) override;
353 
354  // Documentation inherited
355  protected: virtual void wheelEvent(QWheelEvent *_e) override;
356 
363  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
364  QQuickItem::UpdatePaintNodeData *_data) override;
365 
368  public: void SetErrorCb(std::function<void(const QString &)> _cb);
369 
373  };
374 
376  class TextureNode : public QObject, public QSGSimpleTextureNode
377  {
378  Q_OBJECT
379 
382  public: explicit TextureNode(QQuickWindow *_window);
383 
385  public: ~TextureNode() override;
386 
391  public slots: void NewTexture(int _id, const QSize &_size);
392 
395  public slots: void PrepareNode();
396 
399  signals: void TextureInUse();
400 
403  signals: void PendingNewTexture();
404 
406  public: int id = 0;
407 
409  public: QSize size = QSize(0, 0);
410 
412  public: QMutex mutex;
413 
415  public: QSGTexture *texture = nullptr;
416 
418  public: QQuickWindow *window = nullptr;
419  };
420 }
421 }
422 }
423 
424 #endif
QString loadingError
Loading error message.
Definition: Scene3D.hh:76
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: Scene3D.hh:270
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:214
std::function< void(const QString &)> errorCb
Function to be called if there are errors.
Definition: Scene3D.hh:261
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
Texture node for displaying the render texture from ign-renderer.
Definition: Scene3D.hh:376
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:218
std::string deletionTopic
Ign-transport deletion topic name.
Definition: Scene3D.hh:221
Creates an ignition rendering scene and user camera. It is possible to orbit the camera around the sc...
Definition: Scene3D.hh:66
QMutex mutex
Mutex to protect the texture variables.
Definition: Scene3D.hh:412
Rendering thread.
Definition: Scene3D.hh:234
A QQUickItem that manages the render window.
Definition: Scene3D.hh:275
STL namespace.
Ign-rendering renderer. All ign-rendering calls should be performed inside this class as it makes sur...
Definition: Scene3D.hh:125
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:226
Q_INVOKABLE QString LoadingError() const
Get the loading error string.
Q_INVOKABLE void SetLoadingError(const QString &_loadingError)
Set the loading error message.