All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Scene.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 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 #ifndef _SCENE_HH_
18 #define _SCENE_HH_
19 
20 #include <vector>
21 #include <map>
22 #include <string>
23 #include <list>
24 #include <boost/enable_shared_from_this.hpp>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/unordered/unordered_map.hpp>
27 #include <boost/thread/recursive_mutex.hpp>
28 
29 #include <sdf/sdf.hh>
30 
31 #include "gazebo/msgs/msgs.hh"
32 
34 
36 #include "gazebo/common/Events.hh"
37 #include "gazebo/common/Color.hh"
38 #include "gazebo/math/Vector2i.hh"
39 #include "gazebo/util/system.hh"
40 
41 namespace SkyX
42 {
43  class SkyX;
44  class BasicController;
45 }
46 
47 namespace Ogre
48 {
49  class SceneManager;
50  class RaySceneQuery;
51  class Node;
52  class Entity;
53  class Mesh;
54  class Vector3;
55  class Quaternion;
56 }
57 
58 namespace boost
59 {
60  class mutex;
61 }
62 
63 namespace gazebo
64 {
65  namespace rendering
66  {
67  class Projector;
68  class Light;
69  class Visual;
70  class Grid;
71  class Heightmap;
72 
75 
80  class GAZEBO_VISIBLE Scene : public boost::enable_shared_from_this<Scene>
81  {
82  public: enum SkyXMode {
83  GZ_SKYX_ALL = 0x0FFFFFFF,
84  GZ_SKYX_CLOUDS = 0x0000001,
85  GZ_SKYX_MOON = 0x0000002,
86  GZ_SKYX_NONE = 0
87  };
88 
90  private: Scene() {}
91 
97  public: Scene(const std::string &_name,
98  bool _enableVisualizations = false,
99  bool _isServer = false);
100 
102  public: virtual ~Scene();
103 
106  public: void Load(sdf::ElementPtr _scene);
107 
109  public: void Load();
110 
112  public: void Init();
113 
115  public: void PreRender();
116 
119  public: Ogre::SceneManager *GetManager() const;
120 
123  public: std::string GetName() const;
124 
127  public: void SetAmbientColor(const common::Color &_color);
128 
131  public: common::Color GetAmbientColor() const;
132 
135  public: void SetBackgroundColor(const common::Color &_color);
136 
139  public: common::Color GetBackgroundColor() const;
140 
146  public: void CreateGrid(uint32_t _cellCount, float _cellLength,
147  float _lineWidth, const common::Color &_color);
148 
152  public: Grid *GetGrid(uint32_t _index) const;
153 
156  public: uint32_t GetGridCount() const;
157 
163  public: CameraPtr CreateCamera(const std::string &_name,
164  bool _autoRender = true);
165 
171  public: DepthCameraPtr CreateDepthCamera(const std::string &_name,
172  bool _autoRender = true);
173 
179  public: GpuLaserPtr CreateGpuLaser(const std::string &_name,
180  bool _autoRender = true);
181 
184  public: uint32_t GetCameraCount() const;
185 
190  public: CameraPtr GetCamera(uint32_t _index) const;
191 
195  public: CameraPtr GetCamera(const std::string &_name) const;
196 
202  public: UserCameraPtr CreateUserCamera(const std::string &_name);
203 
206  public: uint32_t GetUserCameraCount() const;
207 
213  public: UserCameraPtr GetUserCamera(uint32_t _index) const;
214 
217  public: void RemoveCamera(const std::string &_name);
218 
222  public: LightPtr GetLight(const std::string &_name) const;
223 
226  public: uint32_t GetLightCount() const;
227 
232  public: LightPtr GetLight(uint32_t _index) const;
233 
237  public: VisualPtr GetVisual(const std::string &_name) const;
238 
242  public: VisualPtr GetVisual(uint32_t _id) const;
243 
247  public: void SelectVisual(const std::string &_name,
248  const std::string &_mode);
249 
256  public: VisualPtr GetVisualAt(CameraPtr _camera,
257  const math::Vector2i &_mousePos,
258  std::string &_mod);
259 
262  public: void SnapVisualToNearestBelow(const std::string &_visualName);
263 
269  public: VisualPtr GetVisualAt(CameraPtr _camera,
270  const math::Vector2i &_mousePos);
271 
277  public: VisualPtr GetModelVisualAt(CameraPtr _camera,
278  const math::Vector2i &_mousePos);
279 
280 
284  public: VisualPtr GetVisualBelow(const std::string &_visualName);
285 
290  public: void GetVisualsBelowPoint(const math::Vector3 &_pt,
291  std::vector<VisualPtr> &_visuals);
292 
293 
298  public: double GetHeightBelowPoint(const math::Vector3 &_pt);
299 
305  public: bool GetFirstContact(CameraPtr _camera,
306  const math::Vector2i &_mousePos,
307  math::Vector3 &_position);
308 
310  public: void PrintSceneGraph();
311 
316  public: void SetVisible(const std::string &_name, bool _visible);
317 
322  public: void DrawLine(const math::Vector3 &_start,
323  const math::Vector3 &_end,
324  const std::string &_name);
325 
333  public: void SetFog(const std::string &_type,
334  const common::Color &_color,
335  double _density, double _start, double _end);
336 
339  public: uint32_t GetId() const;
340 
343  public: std::string GetIdString() const;
344 
347  public: void SetShadowsEnabled(bool _value);
348 
351  public: bool GetShadowsEnabled() const;
352 
355  public: void AddVisual(VisualPtr _vis);
356 
359  public: void RemoveVisual(VisualPtr _vis);
360 
363  public: void SetGrid(bool _enabled);
364 
367  public: VisualPtr GetWorldVisual() const;
368 
372  public: std::string StripSceneName(const std::string &_name) const;
373 
376  public: Heightmap *GetHeightmap() const;
377 
379  public: void Clear();
380 
382  public: VisualPtr CloneVisual(const std::string &_visualName,
383  const std::string &_newName) GAZEBO_DEPRECATED(2.0);
384 
388  public: VisualPtr GetSelectedVisual() const;
389 
392  public: void SetWireframe(bool _show);
393 
396  public: void SetTransparent(bool _show);
397 
400  public: void ShowCOMs(bool _show);
401 
404  public: void ShowJoints(bool _show);
405 
408  public: void ShowCollisions(bool _show);
409 
412  public: void ShowContacts(bool _show);
413 
416  public: void ShowClouds(bool _show);
417 
420  public: bool GetShowClouds() const;
421 
422 
427  public: void SetSkyXMode(unsigned int _mode);
428 
430  public: bool GetInitialized() const;
431 
437  public: common::Time GetSimTime() const;
438 
441  public: uint32_t GetVisualCount() const;
442 
444  public: void RemoveProjectors();
445 
447  private: void SetSky();
448 
450  private: void InitDeferredShading();
451 
458  private: Ogre::Entity *GetOgreEntityAt(CameraPtr _camera,
459  const math::Vector2i &_mousePos,
460  bool _ignorSelectionObj);
461 
471  // Code found in Wiki: www.ogre3d.org/wiki/index.php/RetrieveVertexData
472  private: void GetMeshInformation(const Ogre::Mesh *_mesh,
473  size_t &_vertexCount,
474  Ogre::Vector3* &_vertices,
475  size_t &_indexCount,
476  uint64_t* &_indices,
477  const Ogre::Vector3 &_position,
478  const Ogre::Quaternion &_orient,
479  const Ogre::Vector3 &_scale);
480 
484  private: void PrintSceneGraphHelper(const std::string &_prefix,
485  Ogre::Node *_node);
486 
490  private: void OnScene(ConstScenePtr &_msg);
491 
494  private: void OnResponse(ConstResponsePtr &_msg);
495 
498  private: void OnRequest(ConstRequestPtr &_msg);
499 
502  private: void OnJointMsg(ConstJointPtr &_msg);
503 
506  private: bool ProcessSensorMsg(ConstSensorPtr &_msg);
507 
510  private: bool ProcessJointMsg(ConstJointPtr &_msg);
511 
514  private: bool ProcessLinkMsg(ConstLinkPtr &_msg);
515 
518  private: bool ProcessSceneMsg(ConstScenePtr &_msg);
519 
522  private: bool ProcessModelMsg(const msgs::Model &_msg);
523 
526  private: void OnSensorMsg(ConstSensorPtr &_msg);
527 
530  private: void OnVisualMsg(ConstVisualPtr &_msg);
531 
534  private: bool ProcessVisualMsg(ConstVisualPtr &_msg);
535 
538  private: void OnLightMsg(ConstLightPtr &_msg);
539 
542  private: bool ProcessLightMsg(ConstLightPtr &_msg);
543 
546  private: void ProcessRequestMsg(ConstRequestPtr &_msg);
547 
550  private: void OnSelectionMsg(ConstSelectionPtr &_msg);
551 
554  private: void OnSkyMsg(ConstSkyPtr &_msg);
555 
558  private: void OnModelMsg(ConstModelPtr &_msg);
559 
562  private: void OnPoseMsg(ConstPosesStampedPtr &_msg);
563 
566  private: void OnSkeletonPoseMsg(ConstPoseAnimationPtr &_msg);
567 
571  private: void CreateCOMVisual(ConstLinkPtr &_msg, VisualPtr _linkVisual);
572 
576  private: void CreateCOMVisual(sdf::ElementPtr _elem,
577  VisualPtr _linkVisual);
578 
580  private: std::string name;
581 
583  private: sdf::ElementPtr sdf;
584 
586  private: std::vector<CameraPtr> cameras;
587 
589  private: std::vector<UserCameraPtr> userCameras;
590 
592  private: Ogre::SceneManager *manager;
593 
595  private: Ogre::RaySceneQuery *raySceneQuery;
596 
598  private: std::vector<Grid *> grids;
599 
601  private: static uint32_t idCounter;
602 
604  private: uint32_t id;
605 
607  private: std::string idString;
608 
611  typedef std::list<boost::shared_ptr<msgs::Visual const> > VisualMsgs_L;
612 
614  private: VisualMsgs_L visualMsgs;
615 
618  typedef std::list<boost::shared_ptr<msgs::Light const> > LightMsgs_L;
619 
621  private: LightMsgs_L lightMsgs;
622 
625  typedef std::map<uint32_t, msgs::Pose> PoseMsgs_M;
626 
628  private: PoseMsgs_M poseMsgs;
629 
632  typedef std::list<boost::shared_ptr<msgs::Scene const> > SceneMsgs_L;
633 
635  private: SceneMsgs_L sceneMsgs;
636 
639  typedef std::list<boost::shared_ptr<msgs::Joint const> > JointMsgs_L;
640 
642  private: JointMsgs_L jointMsgs;
643 
646  typedef std::list<boost::shared_ptr<msgs::Link const> > LinkMsgs_L;
647 
649  private: LinkMsgs_L linkMsgs;
650 
653  typedef std::list<boost::shared_ptr<msgs::Model const> > ModelMsgs_L;
655  private: ModelMsgs_L modelMsgs;
656 
659  typedef std::list<boost::shared_ptr<msgs::Sensor const> > SensorMsgs_L;
660 
662  private: SensorMsgs_L sensorMsgs;
663 
666  typedef std::list<boost::shared_ptr<msgs::Request const> > RequestMsgs_L;
668  private: RequestMsgs_L requestMsgs;
669 
672  typedef std::map<uint32_t, VisualPtr> Visual_M;
673 
675  private: Visual_M visuals;
676 
679  typedef std::map<std::string, LightPtr> Light_M;
680 
682  private: Light_M lights;
683 
686  typedef std::list<boost::shared_ptr<msgs::PoseAnimation const> >
687  SkeletonPoseMsgs_L;
689  private: SkeletonPoseMsgs_L skeletonPoseMsgs;
690 
692  private: boost::shared_ptr<msgs::Selection const> selectionMsg;
693 
695  private: boost::mutex *receiveMutex;
696 
698  private: boost::recursive_mutex poseMsgMutex;
699 
701  private: transport::NodePtr node;
702 
704  private: transport::SubscriberPtr sensorSub;
705 
707  private: transport::SubscriberPtr sceneSub;
708 
710  private: transport::SubscriberPtr requestSub;
711 
713  private: transport::SubscriberPtr visSub;
714 
716  private: transport::SubscriberPtr lightSub;
717 
719  private: transport::SubscriberPtr poseSub;
720 
722  private: transport::SubscriberPtr jointSub;
723 
725  private: transport::SubscriberPtr selectionSub;
726 
728  private: transport::SubscriberPtr responseSub;
729 
731  private: transport::SubscriberPtr skeletonPoseSub;
732 
734  private: transport::SubscriberPtr skySub;
735 
737  private: transport::SubscriberPtr modelInfoSub;
738 
740  private: transport::PublisherPtr lightPub;
741 
743  private: transport::PublisherPtr responsePub;
744 
746  private: transport::PublisherPtr requestPub;
747 
749  private: std::vector<event::ConnectionPtr> connections;
750 
752  private: VisualPtr worldVisual;
753 
755  private: VisualPtr selectedVis;
756 
762  private: std::string selectionMode;
763 
765  private: msgs::Request *requestMsg;
766 
768  private: bool enableVisualizations;
769 
771  private: Heightmap *terrain;
772 
774  private: std::map<std::string, Projector *> projectors;
775 
777  public: SkyX::SkyX *skyx;
778 
780  private: SkyX::BasicController *skyxController;
781 
783  private: bool showCOMs;
784 
786  private: bool showCollisions;
787 
789  private: bool showJoints;
790 
792  private: bool transparent;
793 
795  private: bool wireframe;
796 
798  private: bool initialized;
799 
802  private: common::Time sceneSimTimePosesReceived;
803 
806  private: common::Time sceneSimTimePosesApplied;
807 
809  private: uint32_t contactVisId;
810 
813  typedef boost::unordered_map<std::string,
814  boost::shared_ptr<msgs::Joint const> > JointMsgs_M;
815 
817  private: JointMsgs_M joints;
818  };
820  }
821 }
822 #endif