Scene.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 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 GAZEBO_RENDERING_SCENE_HH_
19 #define GAZEBO_RENDERING_SCENE_HH_
20 
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include <boost/enable_shared_from_this.hpp>
26 #include <boost/shared_ptr.hpp>
27 
28 #include <OGRE/OgrePrerequisites.h>
29 
30 #include <sdf/sdf.hh>
31 
32 #include <ignition/math/Color.hh>
33 #include <ignition/math/Vector2.hh>
34 #include <ignition/math/Vector3.hh>
35 
36 #include "gazebo/common/Events.hh"
37 #include "gazebo/gazebo_config.h"
38 #include "gazebo/msgs/msgs.hh"
42 #include "gazebo/util/system.hh"
43 
44 namespace SkyX
45 {
46  class SkyX;
47 }
48 
49 namespace gazebo
50 {
51  namespace rendering
52  {
53  class Visual;
54  class Grid;
55  class Heightmap;
56  class ScenePrivate;
57 
60 
92  class GZ_RENDERING_VISIBLE Scene :
93  public boost::enable_shared_from_this<Scene>
94  {
95  public: enum SkyXMode {
96  GZ_SKYX_ALL = 0x0FFFFFFF,
97  GZ_SKYX_CLOUDS = 0x0000001,
98  GZ_SKYX_MOON = 0x0000002,
99  GZ_SKYX_NONE = 0
100  };
101 
103  private: Scene();
104 
110  public: Scene(const std::string &_name,
111  const bool _enableVisualizations = false,
112  const bool _isServer = false);
113 
115  public: virtual ~Scene();
116 
119  public: void Load(sdf::ElementPtr _scene);
120 
122  public: void Load();
123 
125  public: void Init();
126 
128  public: void PreRender();
129 
133  public: bool WaitForRenderRequest(double _timeoutsec);
134 
137  public: Ogre::SceneManager *OgreSceneManager() const;
138 
141  public: std::string Name() const;
142 
145  public: void SetAmbientColor(const ignition::math::Color &_color);
146 
149  public: ignition::math::Color AmbientColor() const;
150 
153  public: void SetBackgroundColor(const ignition::math::Color &_color);
154 
157  public: ignition::math::Color BackgroundColor() const;
158 
163  public: void CreateGrid(const uint32_t _cellCount,
164  const float _cellLength, const ignition::math::Color &_color);
165 
169  public: Grid *GetGrid(uint32_t _index) const;
170 
173  public: uint32_t GridCount() const;
174 
180  public: CameraPtr CreateCamera(const std::string &_name,
181  const bool _autoRender = true);
182 
188  public: WideAngleCameraPtr CreateWideAngleCamera(const std::string &_name,
189  const bool _autoRender = true);
190 
191 #ifdef HAVE_OCULUS
192  public: OculusCameraPtr CreateOculusCamera(const std::string &_name);
196 
199  public: uint32_t OculusCameraCount() const;
200 #endif
201 
207  public: DepthCameraPtr CreateDepthCamera(const std::string &_name,
208  const bool _autoRender = true);
209 
215  public: GpuLaserPtr CreateGpuLaser(const std::string &_name,
216  const bool _autoRender = true);
217 
220  public: uint32_t CameraCount() const;
221 
226  public: CameraPtr GetCamera(const uint32_t _index) const;
227 
231  public: CameraPtr GetCamera(const std::string &_name) const;
232 
240  public: UserCameraPtr CreateUserCamera(const std::string &_name,
241  const bool _stereoEnabled = false);
242 
245  public: uint32_t UserCameraCount() const;
246 
252  public: UserCameraPtr GetUserCamera(const uint32_t _index) const;
253 
256  public: void RemoveCamera(const std::string &_name);
257 
260  public: uint32_t LightCount() const;
261 
266  public: LightPtr GetLight(const std::string &_name) const
267  GAZEBO_DEPRECATED(9.1);
268 
274  public: LightPtr GetLight(const uint32_t _index) const
275  GAZEBO_DEPRECATED(9.1);
276 
280  public: LightPtr LightByName(const std::string &_name) const;
281 
286  public: LightPtr LightByIndex(const uint32_t _index) const;
287 
291  public: LightPtr LightById(const uint32_t _id) const;
292 
296  public: VisualPtr GetVisual(const std::string &_name) const;
297 
301  public: VisualPtr GetVisual(const uint32_t _id) const;
302 
306  public: void SelectVisual(const std::string &_name,
307  const std::string &_mode);
308 
315  public: VisualPtr VisualAt(CameraPtr _camera,
316  const ignition::math::Vector2i &_mousePos,
317  std::string &_mod);
318 
321  public: void SnapVisualToNearestBelow(const std::string &_visualName);
322 
328  public: VisualPtr VisualAt(CameraPtr _camera,
329  const ignition::math::Vector2i &_mousePos);
330 
337  const ignition::math::Vector2i &_mousePos);
338 
342  public: VisualPtr VisualBelow(const std::string &_visualName);
343 
348  public: void VisualsBelowPoint(const ignition::math::Vector3d &_pt,
349  std::vector<VisualPtr> &_visuals);
350 
355  public: double HeightBelowPoint(const ignition::math::Vector3d &_pt);
356 
362  public: bool FirstContact(CameraPtr _camera,
363  const ignition::math::Vector2i &_mousePos,
364  ignition::math::Vector3d &_position);
365 
367  public: void PrintSceneGraph();
368 
373  public: void SetVisible(const std::string &_name, const bool _visible);
374 
379  public: void DrawLine(const ignition::math::Vector3d &_start,
380  const ignition::math::Vector3d &_end,
381  const std::string &_name);
382 
390  public: void SetFog(const std::string &_type,
391  const ignition::math::Color &_color,
392  const double _density, const double _start,
393  const double _end);
394 
397  public: uint32_t Id() const;
398 
401  public: std::string IdString() const;
402 
405  public: void SetShadowsEnabled(const bool _value);
406 
409  public: bool ShadowsEnabled() const;
410 
415  public: bool SetShadowTextureSize(const unsigned int _size);
416 
419  public: unsigned int ShadowTextureSize() const;
420 
423  public: void AddVisual(VisualPtr _vis);
424 
427  public: void RemoveVisual(VisualPtr _vis);
428 
431  public: void RemoveVisual(const uint32_t _id);
432 
438  public: void SetVisualId(VisualPtr _vis, const uint32_t _id);
439 
442  public: void AddLight(LightPtr _light);
443 
446  public: void RemoveLight(LightPtr _light);
447 
450  public: void SetGrid(const bool _enabled);
451 
454  public: void ShowOrigin(const bool _show);
455 
458  public: VisualPtr WorldVisual() const;
459 
463  public: std::string StripSceneName(const std::string &_name) const;
464 
467  public: Heightmap *GetHeightmap() const;
468 
472  public: void SetHeightmapLOD(const unsigned int _value);
473 
477  public: unsigned int HeightmapLOD() const;
478 
482  public: void SetHeightmapSkirtLength(const double _value);
483 
487  public: double HeightmapSkirtLength() const;
488 
490  public: void Clear();
491 
495  public: VisualPtr SelectedVisual() const;
496 
499  public: void SetWireframe(const bool _show);
500 
503  public: bool Wireframe() const;
504 
507  public: void SetTransparent(const bool _show);
508 
511  public: void ShowCOMs(const bool _show);
512 
515  public: void ShowInertias(const bool _show);
516 
519  public: void ShowLinkFrames(const bool _show);
520 
523  public: void ShowSkeleton(const bool _show);
524 
527  public: void ShowJoints(const bool _show);
528 
531  public: void ShowCollisions(const bool _show);
532 
535  public: void ShowContacts(const bool _show);
536 
539  public: void ShowClouds(const bool _show);
540 
543  public: bool ShowClouds() const;
544 
549  public: void SetSkyXMode(const unsigned int _mode);
550 
553  public: SkyX::SkyX *GetSkyX() const;
554 
557  public: bool Initialized() const;
558 
564  public: common::Time SimTime() const;
565 
569  public: void UpdatePoses(const msgs::PosesStamped& _msg);
570 
573  public: uint32_t VisualCount() const;
574 
576  public: void RemoveProjectors();
577 
582  public: void ToggleLayer(const int32_t _layer);
583 
589  public: bool LayerState(const int32_t _layer) const;
590 
595  public: bool HasLayer(const int32_t _layer) const;
596 
601  public: void EnableVisualizations(const bool _enable);
602 
606  public: bool EnableVisualizations() const;
607 
609  private: void SetSky();
610 
612  private: void InitDeferredShading();
613 
620  private: Ogre::Entity *OgreEntityAt(CameraPtr _camera,
621  const ignition::math::Vector2i &_mousePos,
622  const bool _ignoreSelectionObj);
623 
633  // Code found in Wiki: www.ogre3d.org/wiki/index.php/RetrieveVertexData
634  private: void MeshInformation(const Ogre::Mesh *_mesh,
635  size_t &_vertexCount,
636  Ogre::Vector3* &_vertices,
637  size_t &_indexCount,
638  uint64_t* &_indices,
639  const ignition::math::Vector3d &_position,
640  const ignition::math::Quaterniond &_orient,
641  const ignition::math::Vector3d &_scale);
642 
646  private: void PrintSceneGraphHelper(const std::string &_prefix,
647  Ogre::Node *_node);
648 
652  private: void OnScene(ConstScenePtr &_msg);
653 
656  private: void OnResponse(ConstResponsePtr &_msg);
657 
660  private: void OnRequest(ConstRequestPtr &_msg);
661 
664  private: void OnJointMsg(ConstJointPtr &_msg);
665 
668  private: bool ProcessSensorMsg(ConstSensorPtr &_msg);
669 
672  private: bool ProcessJointMsg(ConstJointPtr &_msg);
673 
676  private: bool ProcessLinkMsg(ConstLinkPtr &_msg);
677 
680  private: bool ProcessSceneMsg(ConstScenePtr &_msg);
681 
684  private: bool ProcessModelMsg(const msgs::Model &_msg);
685 
688  private: void OnSensorMsg(ConstSensorPtr &_msg);
689 
692  private: void OnVisualMsg(ConstVisualPtr &_msg);
693 
698  private: bool ProcessVisualMsg(ConstVisualPtr &_msg,
700 
703  private: void OnLightFactoryMsg(ConstLightPtr &_msg);
704 
707  private: void OnLightModifyMsg(ConstLightPtr &_msg);
708 
711  private: bool ProcessLightFactoryMsg(ConstLightPtr &_msg);
712 
715  private: bool ProcessLightModifyMsg(ConstLightPtr &_msg);
716 
719  private: void ProcessRequestMsg(ConstRequestPtr &_msg);
720 
723  private: void OnSkyMsg(ConstSkyPtr &_msg);
724 
727  private: void OnModelMsg(ConstModelPtr &_msg);
728 
731  private: void OnPoseMsg(ConstPosesStampedPtr &_msg);
732 
735  private: void OnSkeletonPoseMsg(ConstPoseAnimationPtr &_msg);
736 
739  private: void OnRoadMsg(ConstRoadPtr &_msg);
740 
744  private: void CreateCOMVisual(ConstLinkPtr &_msg, VisualPtr _linkVisual);
745 
749  private: void CreateCOMVisual(sdf::ElementPtr _elem,
750  VisualPtr _linkVisual);
751 
755  private: void CreateInertiaVisual(ConstLinkPtr &_msg,
756  VisualPtr _linkVisual);
757 
761  private: void CreateInertiaVisual(sdf::ElementPtr _elem,
762  VisualPtr _linkVisual);
763 
767  private: void CreateLinkFrameVisual(ConstLinkPtr &_msg,
768  VisualPtr _linkVisual);
769 
773  private: void RemoveVisualizations(VisualPtr _vis);
774 
777  private: std::unique_ptr<ScenePrivate> dataPtr;
778  };
780  }
781 }
782 #endif
bool FirstContact(CameraPtr _camera, const ignition::math::Vector2i &_mousePos, ignition::math::Vector3d &_position)
Get the world pos of a the first contact at a pixel location.
void ShowClouds(const bool _show)
Display clouds in the sky.
void ShowOrigin(const bool _show)
Show/hide the world origin indicator.
LightPtr GetLight(const uint32_t _index) const GAZEBO_DEPRECATED(9.1)
Get a light based on an index.
void SetBackgroundColor(const ignition::math::Color &_color)
Set the background color.
void SetTransparent(const bool _show)
Enable or disable transparency for all visuals.
void SetHeightmapLOD(const unsigned int _value)
Set the Level Of Detail (LOD) value for the heightmap.
Scene(const std::string &_name, const bool _enableVisualizations=false, const bool _isServer=false)
Constructor.
boost::shared_ptr< UserCamera > UserCameraPtr
Definition: RenderTypes.hh:94
bool ShowClouds() const
Get whether or not clouds are displayed.
ignition::math::Color BackgroundColor() const
Get the background color.
std::string IdString() const
Get the scene Id as a string.
void Load()
Load the scene with default parameters.
void SetAmbientColor(const ignition::math::Color &_color)
Set the ambient color.
LightPtr LightById(const uint32_t _id) const
Get a light by id.
double HeightBelowPoint(const ignition::math::Vector3d &_pt)
Get the Z-value of the first object below the given point.
CameraPtr GetCamera(const std::string &_name) const
Get a camera by name.
Representation of an entire scene graph.
Definition: Scene.hh:94
Forward declarations for the common classes.
Definition: Animation.hh:27
Heightmap * GetHeightmap() const
Get a pointer to the heightmap.
Rendering a terrain using heightmap information.
Definition: Heightmap.hh:63
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:48
bool Wireframe() const
Get whether wireframe is enabled for all visuals.
boost::shared_ptr< GpuLaser > GpuLaserPtr
Definition: RenderTypes.hh:106
@ VT_ENTITY
Entity visual.
Definition: Visual.hh:67
UserCameraPtr CreateUserCamera(const std::string &_name, const bool _stereoEnabled=false)
Create a user camera.
bool EnableVisualizations() const
Check whether visualizations are enabled or not.
common::Time SimTime() const
Get the scene simulation time.
VisualPtr VisualAt(CameraPtr _camera, const ignition::math::Vector2i &_mousePos, std::string &_mod)
Get an entity at a pixel location using a camera.
bool WaitForRenderRequest(double _timeoutsec)
Wait until a render request occurs.
Forward declarations for transport.
void VisualsBelowPoint(const ignition::math::Vector3d &_pt, std::vector< VisualPtr > &_visuals)
Get a visual directly below a point.
void SetVisible(const std::string &_name, const bool _visible)
Hide or show a visual.
Grid * GetGrid(uint32_t _index) const
Get a grid based on an index.
uint32_t Id() const
Get the scene ID.
void CreateGrid(const uint32_t _cellCount, const float _cellLength, const ignition::math::Color &_color)
Create a square grid of cells.
uint32_t VisualCount() const
Get the number of visuals.
void ShowJoints(const bool _show)
Enable or disable joint visualization.
void SetSkyXMode(const unsigned int _mode)
Set SkyX mode to enable/disable skyx components such as clouds and moon.
unsigned int HeightmapLOD() const
Get the Level Of Detail (LOD) value for the heightmap.
DepthCameraPtr CreateDepthCamera(const std::string &_name, const bool _autoRender=true)
Create depth camera.
void PrintSceneGraph()
Print the scene graph to std_out.
void Clear()
Clear rendering::Scene.
VisualPtr VisualAt(CameraPtr _camera, const ignition::math::Vector2i &_mousePos)
Get a visual at a mouse position.
virtual ~Scene()
Destructor.
void ShowSkeleton(const bool _show)
Enable or disable skeleton visualization.
void ShowInertias(const bool _show)
Enable or disable inertia visualization.
void SetHeightmapSkirtLength(const double _value)
Set the skirt length value for the heightmap LOD tiles.
void SetGrid(const bool _enabled)
Set the grid on or off.
double HeightmapSkirtLength() const
Get the skirt length value for the heightmap LOD tiles.
void Load(sdf::ElementPtr _scene)
Load the scene from a set of parameters.
void EnableVisualizations(const bool _enable)
Enable visualizations, currently only applies to sensor visuals.
uint32_t CameraCount() const
Get the number of cameras in this scene.
void UpdatePoses(const msgs::PosesStamped &_msg)
Update Poses of objects in the scene via direct API call instead of transport.
@ GZ_SKYX_NONE
Definition: Scene.hh:99
@ GZ_SKYX_ALL
Definition: Scene.hh:96
void RemoveProjectors()
Remove all projectors.
Displays a grid of cells, drawn with lines.
Definition: Grid.hh:54
void SetFog(const std::string &_type, const ignition::math::Color &_color, const double _density, const double _start, const double _end)
Set the fog parameters.
rendering
Definition: RenderEngine.hh:31
void RemoveCamera(const std::string &_name)
Remove a camera from the scene.
VisualType
Type of visual.
Definition: Visual.hh:65
uint32_t LightCount() const
Get the count of the lights.
LightPtr LightByName(const std::string &_name) const
Get a light by name.
void RemoveVisual(const uint32_t _id)
Remove a visual from the scene.
void ShowContacts(const bool _show)
Enable or disable contact visualization.
void SetVisualId(VisualPtr _vis, const uint32_t _id)
boost::shared_ptr< DepthCamera > DepthCameraPtr
Definition: RenderTypes.hh:98
CameraPtr GetCamera(const uint32_t _index) const
Get a camera based on an index.
void ShowCollisions(const bool _show)
Enable or disable collision visualization.
SkyX::SkyX * GetSkyX() const
Get the sky in the scene.
VisualPtr WorldVisual() const
Get the top level world visual.
@ GZ_SKYX_MOON
Definition: Scene.hh:98
void SetShadowsEnabled(const bool _value)
Set whether shadows are on or off.
bool Initialized() const
Return true if the Scene has been initialized.
GpuLaserPtr CreateGpuLaser(const std::string &_name, const bool _autoRender=true)
Create laser that generates data from rendering.
void RemoveVisual(VisualPtr _vis)
Remove a visual from the scene.
void SetWireframe(const bool _show)
Enable or disable wireframe for all visuals.
LightPtr GetLight(const std::string &_name) const GAZEBO_DEPRECATED(9.1)
Get a light by name.
UserCameraPtr GetUserCamera(const uint32_t _index) const
Get a user camera by index.
VisualPtr SelectedVisual() const
Get the currently selected visual.
bool ShadowsEnabled() const
Get whether shadows are on or off.
uint32_t UserCameraCount() const
Get the number of user cameras in this scene.
void AddLight(LightPtr _light)
Add a light to the scene.
SkyXMode
Definition: Scene.hh:95
bool SetShadowTextureSize(const unsigned int _size)
Set the shadow texture size.
void ToggleLayer(const int32_t _layer)
Toggle layer visilibility.
uint32_t GridCount() const
Get the number of grids.
@ GZ_SKYX_CLOUDS
Definition: Scene.hh:97
VisualPtr ModelVisualAt(CameraPtr _camera, const ignition::math::Vector2i &_mousePos)
Get a model's visual at a mouse position.
void Init()
Init rendering::Scene.
LightPtr LightByIndex(const uint32_t _index) const
Get a light based on an index.
void AddVisual(VisualPtr _vis)
Add a visual to the scene.
void RemoveLight(LightPtr _light)
Remove a light to the scene.
Definition: Scene.hh:45
#define GAZEBO_DEPRECATED(version)
Definition: system.hh:328
bool HasLayer(const int32_t _layer) const
Return true if the layer exits.
void ShowLinkFrames(const bool _show)
Enable or disable link frame visualization.
void SelectVisual(const std::string &_name, const std::string &_mode)
Select a visual by name.
VisualPtr GetVisual(const std::string &_name) const
Get a visual by name.
boost::shared_ptr< Light > LightPtr
Definition: RenderTypes.hh:86
Ogre::SceneManager * OgreSceneManager() const
Get the OGRE scene manager.
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:114
std::string Name() const
Get the name of the scene.
unsigned int ShadowTextureSize() const
Get the shadow texture size.
VisualPtr GetVisual(const uint32_t _id) const
Get a visual by id.
bool LayerState(const int32_t _layer) const
Return whether a layer is on or off.
VisualPtr VisualBelow(const std::string &_visualName)
Get the closest visual below a given visual.
boost::shared_ptr< Camera > CameraPtr
Definition: RenderTypes.hh:90
boost::shared_ptr< WideAngleCamera > WideAngleCameraPtr
Definition: RenderTypes.hh:102
void PreRender()
Process all received messages.
void DrawLine(const ignition::math::Vector3d &_start, const ignition::math::Vector3d &_end, const std::string &_name)
Draw a named line.
WideAngleCameraPtr CreateWideAngleCamera(const std::string &_name, const bool _autoRender=true)
Create a wide-angle camera.
void SnapVisualToNearestBelow(const std::string &_visualName)
Move the visual to be ontop of the nearest visual below it.
ignition::math::Color AmbientColor() const
Get the ambient color.
std::string StripSceneName(const std::string &_name) const
Remove the name of scene from a string.
void ShowCOMs(const bool _show)
Enable or disable center of mass visualization.
CameraPtr CreateCamera(const std::string &_name, const bool _autoRender=true)
Create a camera.