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 
130 
134  public: bool WaitForRenderRequest(double _timeoutsec);
135 
138  public: Ogre::SceneManager *OgreSceneManager() const;
139 
142  public: std::string Name() const;
143 
146  public: void SetAmbientColor(const ignition::math::Color &_color);
147 
150  public: ignition::math::Color AmbientColor() const;
151 
154  public: void SetBackgroundColor(const ignition::math::Color &_color);
155 
158  public: ignition::math::Color BackgroundColor() const;
159 
164  public: void CreateGrid(const uint32_t _cellCount,
165  const float _cellLength, const ignition::math::Color &_color);
166 
170  public: Grid *GetGrid(uint32_t _index) const;
171 
174  public: uint32_t GridCount() const;
175 
181  public: CameraPtr CreateCamera(const std::string &_name,
182  const bool _autoRender = true);
183 
189  public: WideAngleCameraPtr CreateWideAngleCamera(const std::string &_name,
190  const bool _autoRender = true);
191 
192 #ifdef HAVE_OCULUS
196  public: OculusCameraPtr CreateOculusCamera(const std::string &_name);
197 
200  public: uint32_t OculusCameraCount() const;
201 #endif
202 
208  public: DepthCameraPtr CreateDepthCamera(const std::string &_name,
209  const bool _autoRender = true);
210 
216  public: GpuLaserPtr CreateGpuLaser(const std::string &_name,
217  const bool _autoRender = true);
218 
221  public: uint32_t CameraCount() const;
222 
227  public: CameraPtr GetCamera(const uint32_t _index) const;
228 
232  public: CameraPtr GetCamera(const std::string &_name) const;
233 
241  public: UserCameraPtr CreateUserCamera(const std::string &_name,
242  const bool _stereoEnabled = false);
243 
246  public: uint32_t UserCameraCount() const;
247 
253  public: UserCameraPtr GetUserCamera(const uint32_t _index) const;
254 
257  public: void RemoveCamera(const std::string &_name);
258 
261  public: uint32_t LightCount() const;
262 
267  public: LightPtr GetLight(const std::string &_name) const
268  GAZEBO_DEPRECATED(9.1);
269 
275  public: LightPtr GetLight(const uint32_t _index) const
276  GAZEBO_DEPRECATED(9.1);
277 
281  public: LightPtr LightByName(const std::string &_name) const;
282 
287  public: LightPtr LightByIndex(const uint32_t _index) const;
288 
292  public: LightPtr LightById(const uint32_t _id) const;
293 
297  public: VisualPtr GetVisual(const std::string &_name) const;
298 
302  public: VisualPtr GetVisual(const uint32_t _id) const;
303 
307  public: void SelectVisual(const std::string &_name,
308  const std::string &_mode);
309 
316  public: VisualPtr VisualAt(CameraPtr _camera,
317  const ignition::math::Vector2i &_mousePos,
318  std::string &_mod);
319 
322  public: void SnapVisualToNearestBelow(const std::string &_visualName);
323 
329  public: VisualPtr VisualAt(CameraPtr _camera,
330  const ignition::math::Vector2i &_mousePos);
331 
338  const ignition::math::Vector2i &_mousePos);
339 
343  public: VisualPtr VisualBelow(const std::string &_visualName);
344 
349  public: void VisualsBelowPoint(const ignition::math::Vector3d &_pt,
350  std::vector<VisualPtr> &_visuals);
351 
356  public: double HeightBelowPoint(const ignition::math::Vector3d &_pt);
357 
363  public: bool FirstContact(CameraPtr _camera,
364  const ignition::math::Vector2i &_mousePos,
365  ignition::math::Vector3d &_position);
366 
368  public: void PrintSceneGraph();
369 
374  public: void SetVisible(const std::string &_name, const bool _visible);
375 
380  public: void DrawLine(const ignition::math::Vector3d &_start,
381  const ignition::math::Vector3d &_end,
382  const std::string &_name);
383 
384 
392  public: void SetFog(const std::string &_type,
393  const ignition::math::Color &_color,
394  const double _density, const double _start,
395  const double _end);
396 
399  public: uint32_t Id() const;
400 
403  public: std::string IdString() const;
404 
407  public: void SetShadowsEnabled(const bool _value);
408 
411  public: bool IsServer() const;
412 
415  public: bool ShadowsEnabled() const;
416 
421  public: bool SetShadowTextureSize(const unsigned int _size);
422 
425  public: unsigned int ShadowTextureSize() const;
426 
429  public: std::string ShadowCasterMaterialName() const;
430 
433  public: bool ShadowCasterRenderBackFaces() const;
434 
437  public: void AddVisual(VisualPtr _vis);
438 
441  public: void RemoveVisual(VisualPtr _vis);
442 
445  public: void RemoveVisual(const uint32_t _id);
446 
452  public: void SetVisualId(VisualPtr _vis, const uint32_t _id);
453 
456  public: void AddLight(LightPtr _light);
457 
460  public: void RemoveLight(LightPtr _light);
461 
464  public: void SetGrid(const bool _enabled);
465 
468  public: void ShowOrigin(const bool _show);
469 
472  public: VisualPtr WorldVisual() const;
473 
477  public: std::string StripSceneName(const std::string &_name) const;
478 
481  public: Heightmap *GetHeightmap() const;
482 
486  public: void SetHeightmapLOD(const unsigned int _value);
487 
491  public: unsigned int HeightmapLOD() const;
492 
496  public: void SetHeightmapSkirtLength(const double _value);
497 
501  public: double HeightmapSkirtLength() const;
502 
504  public: void Clear();
505 
509  public: VisualPtr SelectedVisual() const;
510 
513  public: void SetWireframe(const bool _show);
514 
517  public: bool Wireframe() const;
518 
521  public: void SetTransparent(const bool _show);
522 
525  public: void ShowCOMs(const bool _show);
526 
529  public: void ShowInertias(const bool _show);
530 
533  public: void ShowLinkFrames(const bool _show);
534 
537  public: void ShowSkeleton(const bool _show);
538 
541  public: void ShowJoints(const bool _show);
542 
545  public: void ShowCollisions(const bool _show);
546 
549  public: void ShowContacts(const bool _show);
550 
553  public: void ShowClouds(const bool _show);
554 
557  public: bool ShowClouds() const;
558 
563  public: void SetSkyXMode(const unsigned int _mode);
564 
567  public: SkyX::SkyX *GetSkyX() const;
568 
571  public: bool Initialized() const;
572 
578  public: common::Time SimTime() const;
579 
583  public: void UpdatePoses(const msgs::PosesStamped& _msg);
584 
587  public: uint32_t VisualCount() const;
588 
590  public: void RemoveProjectors();
591 
596  public: void ToggleLayer(const int32_t _layer);
597 
603  public: bool LayerState(const int32_t _layer) const;
604 
609  public: bool HasLayer(const int32_t _layer) const;
610 
615  public: void EnableVisualizations(const bool _enable);
616 
620  public: bool EnableVisualizations() const;
621 
623  private: void SetSky();
624 
626  private: void InitDeferredShading();
627 
634  private: Ogre::Entity *OgreEntityAt(CameraPtr _camera,
635  const ignition::math::Vector2i &_mousePos,
636  const bool _ignoreSelectionObj);
637 
647  // Code found in Wiki: www.ogre3d.org/wiki/index.php/RetrieveVertexData
648  private: void MeshInformation(const Ogre::Mesh *_mesh,
649  size_t &_vertexCount,
650  Ogre::Vector3* &_vertices,
651  size_t &_indexCount,
652  uint64_t* &_indices,
653  const ignition::math::Vector3d &_position,
654  const ignition::math::Quaterniond &_orient,
655  const ignition::math::Vector3d &_scale);
656 
660  private: void PrintSceneGraphHelper(const std::string &_prefix,
661  Ogre::Node *_node);
662 
666  private: void OnScene(ConstScenePtr &_msg);
667 
672  private: void OnSceneInfo(const msgs::Scene &_msg, const bool _result);
673 
676  private: void OnResponse(ConstResponsePtr &_msg);
677 
680  private: void OnRequest(ConstRequestPtr &_msg);
681 
684  private: void OnJointMsg(ConstJointPtr &_msg);
685 
688  private: bool ProcessSensorMsg(ConstSensorPtr &_msg);
689 
692  private: bool ProcessJointMsg(ConstJointPtr &_msg);
693 
696  private: bool ProcessLinkMsg(ConstLinkPtr &_msg);
697 
700  private: bool ProcessSceneMsg(ConstScenePtr &_msg);
701 
704  private: bool ProcessModelMsg(const msgs::Model &_msg);
705 
708  private: void OnSensorMsg(ConstSensorPtr &_msg);
709 
712  private: void OnVisualMsg(ConstVisualPtr &_msg);
713 
718  private: bool ProcessVisualMsg(ConstVisualPtr &_msg,
720 
723  private: void OnLightFactoryMsg(ConstLightPtr &_msg);
724 
727  private: void OnLightModifyMsg(ConstLightPtr &_msg);
728 
731  private: bool ProcessLightFactoryMsg(ConstLightPtr &_msg);
732 
735  private: bool ProcessLightModifyMsg(ConstLightPtr &_msg);
736 
739  private: void ProcessRequestMsg(ConstRequestPtr &_msg);
740 
743  private: void OnSkyMsg(ConstSkyPtr &_msg);
744 
747  private: void OnModelMsg(ConstModelPtr &_msg);
748 
751  private: void OnPoseMsg(ConstPosesStampedPtr &_msg);
752 
755  private: void OnSkeletonPoseMsg(ConstPoseAnimationPtr &_msg);
756 
759  private: void OnRoadMsg(ConstRoadPtr &_msg);
760 
764  private: void CreateCOMVisual(ConstLinkPtr &_msg, VisualPtr _linkVisual);
765 
769  private: void CreateCOMVisual(sdf::ElementPtr _elem,
770  VisualPtr _linkVisual);
771 
775  private: void CreateInertiaVisual(ConstLinkPtr &_msg,
776  VisualPtr _linkVisual);
777 
781  private: void CreateInertiaVisual(sdf::ElementPtr _elem,
782  VisualPtr _linkVisual);
783 
787  private: void CreateLinkFrameVisual(ConstLinkPtr &_msg,
788  VisualPtr _linkVisual);
789 
793  private: void RemoveVisualizations(VisualPtr _vis);
794 
797  private: std::unique_ptr<ScenePrivate> dataPtr;
798  };
800  }
801 }
802 #endif
rendering
Definition: RenderEngine.hh:31
Forward declarations for transport.
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:48
Displays a grid of cells, drawn with lines.
Definition: Grid.hh:54
Rendering a terrain using heightmap information.
Definition: Heightmap.hh:63
Representation of an entire scene graph.
Definition: Scene.hh:94
LightPtr GetLight(const std::string &_name) const GAZEBO_DEPRECATED(9.1)
Get a light by name.
uint32_t VisualCount() const
Get the number of visuals.
ignition::math::Color BackgroundColor() const
Get the background color.
LightPtr LightById(const uint32_t _id) const
Get a light by id.
void AddVisual(VisualPtr _vis)
Add a visual to the scene.
void Init()
Init rendering::Scene.
void AddLight(LightPtr _light)
Add a light to the scene.
bool WaitForRenderRequest(double _timeoutsec)
Wait until a render request occurs.
VisualPtr ModelVisualAt(CameraPtr _camera, const ignition::math::Vector2i &_mousePos)
Get a model's visual at a mouse position.
uint32_t LightCount() const
Get the count of the lights.
void SetSkyXMode(const unsigned int _mode)
Set SkyX mode to enable/disable skyx components such as clouds and moon.
void ToggleLayer(const int32_t _layer)
Toggle layer visilibility.
VisualPtr VisualAt(CameraPtr _camera, const ignition::math::Vector2i &_mousePos, std::string &_mod)
Get an entity at a pixel location using a camera.
void EnableVisualizations(const bool _enable)
Enable visualizations, currently only applies to sensor visuals.
void CreateGrid(const uint32_t _cellCount, const float _cellLength, const ignition::math::Color &_color)
Create a square grid of cells.
unsigned int ShadowTextureSize() const
Get the shadow texture size.
SkyX::SkyX * GetSkyX() const
Get the sky in the scene.
double HeightmapSkirtLength() const
Get the skirt length value for the heightmap LOD tiles.
void SelectVisual(const std::string &_name, const std::string &_mode)
Select a visual by name.
void RemoveVisual(const uint32_t _id)
Remove a visual from the scene.
Heightmap * GetHeightmap() const
Get a pointer to the heightmap.
void ShowSkeleton(const bool _show)
Enable or disable skeleton visualization.
Ogre::SceneManager * OgreSceneManager() const
Get the OGRE scene manager.
std::string Name() const
Get the name of the scene.
bool ShowClouds() const
Get whether or not clouds are displayed.
void ShowOrigin(const bool _show)
Show/hide the world origin indicator.
bool SetShadowTextureSize(const unsigned int _size)
Set the shadow texture size.
void ShowCOMs(const bool _show)
Enable or disable center of mass visualization.
VisualPtr SelectedVisual() const
Get the currently selected visual.
void SetHeightmapLOD(const unsigned int _value)
Set the Level Of Detail (LOD) value for the heightmap.
unsigned int HeightmapLOD() const
Get the Level Of Detail (LOD) value for the heightmap.
void ShowCollisions(const bool _show)
Enable or disable collision visualization.
void RemoveLight(LightPtr _light)
Remove a light to the scene.
LightPtr LightByName(const std::string &_name) const
Get a light by name.
void SetHeightmapSkirtLength(const double _value)
Set the skirt length value for the heightmap LOD tiles.
void Load(sdf::ElementPtr _scene)
Load the scene from a set of parameters.
void ShowContacts(const bool _show)
Enable or disable contact visualization.
uint32_t CameraCount() const
Get the number of cameras in this scene.
LightPtr LightByIndex(const uint32_t _index) const
Get a light based on an index.
bool HasLayer(const int32_t _layer) const
Return true if the layer exits.
void ShowJoints(const bool _show)
Enable or disable joint visualization.
CameraPtr GetCamera(const uint32_t _index) const
Get a camera based on an index.
void SetBackgroundColor(const ignition::math::Color &_color)
Set the background color.
void SetFog(const std::string &_type, const ignition::math::Color &_color, const double _density, const double _start, const double _end)
Set the fog parameters.
uint32_t UserCameraCount() const
Get the number of user cameras in this scene.
bool LayerState(const int32_t _layer) const
Return whether a layer is on or off.
LightPtr GetLight(const uint32_t _index) const GAZEBO_DEPRECATED(9.1)
Get a light based on an index.
std::string ShadowCasterMaterialName() const
Get the shadow caster material name.
uint32_t GridCount() const
Get the number of grids.
void RemoveCamera(const std::string &_name)
Remove a camera from the scene.
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.
bool ShadowsEnabled() const
Get whether shadows are on or off.
void ShowInertias(const bool _show)
Enable or disable inertia visualization.
Scene(const std::string &_name, const bool _enableVisualizations=false, const bool _isServer=false)
Constructor.
void Clear()
Clear rendering::Scene.
VisualPtr GetVisual(const uint32_t _id) const
Get a visual by id.
void SetGrid(const bool _enabled)
Set the grid on or off.
void ShowClouds(const bool _show)
Display clouds in the sky.
VisualPtr VisualAt(CameraPtr _camera, const ignition::math::Vector2i &_mousePos)
Get a visual at a mouse position.
VisualPtr WorldVisual() const
Get the top level world visual.
UserCameraPtr CreateUserCamera(const std::string &_name, const bool _stereoEnabled=false)
Create a user camera.
void SetVisible(const std::string &_name, const bool _visible)
Hide or show a visual.
virtual ~Scene()
Destructor.
WideAngleCameraPtr CreateWideAngleCamera(const std::string &_name, const bool _autoRender=true)
Create a wide-angle camera.
void SetWireframe(const bool _show)
Enable or disable wireframe for all visuals.
CameraPtr CreateCamera(const std::string &_name, const bool _autoRender=true)
Create a camera.
Grid * GetGrid(uint32_t _index) const
Get a grid based on an index.
GpuLaserPtr CreateGpuLaser(const std::string &_name, const bool _autoRender=true)
Create laser that generates data from rendering.
void SnapVisualToNearestBelow(const std::string &_visualName)
Move the visual to be ontop of the nearest visual below it.
void PrintSceneGraph()
Print the scene graph to std_out.
void RemoveProjectors()
Remove all projectors.
void VisualsBelowPoint(const ignition::math::Vector3d &_pt, std::vector< VisualPtr > &_visuals)
Get a visual directly below a point.
double HeightBelowPoint(const ignition::math::Vector3d &_pt)
Get the Z-value of the first object below the given point.
ignition::math::Color AmbientColor() const
Get the ambient color.
bool IsServer() const
Get whether this scene is in the server.
UserCameraPtr GetUserCamera(const uint32_t _index) const
Get a user camera by index.
uint32_t Id() const
Get the scene ID.
void RemoveVisual(VisualPtr _vis)
Remove a visual from the scene.
std::string StripSceneName(const std::string &_name) const
Remove the name of scene from a string.
void UpdatePoses(const msgs::PosesStamped &_msg)
Update Poses of objects in the scene via direct API call instead of transport.
void SetAmbientColor(const ignition::math::Color &_color)
Set the ambient color.
void Load()
Load the scene with default parameters.
bool Initialized() const
Return true if the Scene has been initialized.
std::string IdString() const
Get the scene Id as a string.
void SetShadowsEnabled(const bool _value)
Set whether shadows are on or off.
common::Time SimTime() const
Get the scene simulation time.
DepthCameraPtr CreateDepthCamera(const std::string &_name, const bool _autoRender=true)
Create depth camera.
void SetTransparent(const bool _show)
Enable or disable transparency for all visuals.
CameraPtr GetCamera(const std::string &_name) const
Get a camera by name.
bool Wireframe() const
Get whether wireframe is enabled for all visuals.
VisualPtr GetVisual(const std::string &_name) const
Get a visual by name.
VisualPtr VisualBelow(const std::string &_visualName)
Get the closest visual below a given visual.
bool EnableVisualizations() const
Check whether visualizations are enabled or not.
void SetVisualId(VisualPtr _vis, const uint32_t _id)
bool ShadowCasterRenderBackFaces() const
Get the shadow caster render back faces.
void ShowLinkFrames(const bool _show)
Enable or disable link frame visualization.
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.
SkyXMode
Definition: Scene.hh:95
VisualType
Type of visual.
Definition: Visual.hh:65
@ VT_ENTITY
Entity visual.
Definition: Visual.hh:67
Definition: Scene.hh:45
boost::shared_ptr< Light > LightPtr
Definition: RenderTypes.hh:86
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:114
boost::shared_ptr< DepthCamera > DepthCameraPtr
Definition: RenderTypes.hh:98
boost::shared_ptr< Camera > CameraPtr
Definition: RenderTypes.hh:90
boost::shared_ptr< WideAngleCamera > WideAngleCameraPtr
Definition: RenderTypes.hh:102
boost::shared_ptr< UserCamera > UserCameraPtr
Definition: RenderTypes.hh:94
boost::shared_ptr< GpuLaser > GpuLaserPtr
Definition: RenderTypes.hh:106
Forward declarations for the common classes.
Definition: Animation.hh:27
#define GAZEBO_DEPRECATED(version)
Definition: system.hh:328