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 
40 namespace SkyX
41 {
42  class SkyX;
43  class BasicController;
44 }
45 
46 namespace Ogre
47 {
48  class SceneManager;
49  class RaySceneQuery;
50  class Node;
51  class Entity;
52  class Mesh;
53  class Vector3;
54  class Quaternion;
55 }
56 
57 namespace boost
58 {
59  class mutex;
60 }
61 
62 namespace gazebo
63 {
64  namespace rendering
65  {
66  class Projector;
67  class Light;
68  class Visual;
69  class Grid;
70  class Heightmap;
71 
74 
79  class Scene : public boost::enable_shared_from_this<Scene>
80  {
81  public: enum SkyXMode {
82  GZ_SKYX_ALL = 0x0FFFFFFF,
83  GZ_SKYX_CLOUDS = 0x0000001,
84  GZ_SKYX_MOON = 0x0000002,
86  };
87 
89  private: Scene() {}
90 
96  public: Scene(const std::string &_name,
97  bool _enableVisualizations = false,
98  bool _isServer = false);
99 
101  public: virtual ~Scene();
102 
105  public: void Load(sdf::ElementPtr _scene);
106 
108  public: void Load();
109 
111  public: void Init();
112 
114  public: void PreRender();
115 
118  public: Ogre::SceneManager *GetManager() const;
119 
122  public: std::string GetName() const;
123 
126  public: void SetAmbientColor(const common::Color &_color);
127 
130  public: common::Color GetAmbientColor() const;
131 
134  public: void SetBackgroundColor(const common::Color &_color);
135 
138  public: common::Color GetBackgroundColor() const;
139 
145  public: void CreateGrid(uint32_t _cellCount, float _cellLength,
146  float _lineWidth, const common::Color &_color);
147 
151  public: Grid *GetGrid(uint32_t _index) const;
152 
155  public: uint32_t GetGridCount() const;
156 
162  public: CameraPtr CreateCamera(const std::string &_name,
163  bool _autoRender = true);
164 
170  public: DepthCameraPtr CreateDepthCamera(const std::string &_name,
171  bool _autoRender = true);
172 
178  public: GpuLaserPtr CreateGpuLaser(const std::string &_name,
179  bool _autoRender = true);
180 
183  public: uint32_t GetCameraCount() const;
184 
189  public: CameraPtr GetCamera(uint32_t _index) const;
190 
194  public: CameraPtr GetCamera(const std::string &_name) const;
195 
201  public: UserCameraPtr CreateUserCamera(const std::string &_name);
202 
205  public: uint32_t GetUserCameraCount() const;
206 
212  public: UserCameraPtr GetUserCamera(uint32_t _index) const;
213 
216  public: void RemoveCamera(const std::string &_name);
217 
221  public: LightPtr GetLight(const std::string &_name) const;
222 
225  public: uint32_t GetLightCount() const;
226 
231  public: LightPtr GetLight(uint32_t _index) const;
232 
236  public: VisualPtr GetVisual(const std::string &_name) const;
237 
241  public: VisualPtr GetVisual(uint32_t _id) const;
242 
246  public: void SelectVisual(const std::string &_name,
247  const std::string &_mode);
248 
255  public: VisualPtr GetVisualAt(CameraPtr _camera,
256  const math::Vector2i &_mousePos,
257  std::string &_mod);
258 
261  public: void SnapVisualToNearestBelow(const std::string &_visualName);
262 
268  public: VisualPtr GetVisualAt(CameraPtr _camera,
269  const math::Vector2i &_mousePos);
270 
276  public: VisualPtr GetModelVisualAt(CameraPtr _camera,
277  const math::Vector2i &_mousePos);
278 
279 
283  public: VisualPtr GetVisualBelow(const std::string &_visualName);
284 
289  public: void GetVisualsBelowPoint(const math::Vector3 &_pt,
290  std::vector<VisualPtr> &_visuals);
291 
292 
297  public: double GetHeightBelowPoint(const math::Vector3 &_pt);
298 
304  public: bool GetFirstContact(CameraPtr _camera,
305  const math::Vector2i &_mousePos,
306  math::Vector3 &_position);
307 
309  public: void PrintSceneGraph();
310 
315  public: void SetVisible(const std::string &_name, bool _visible);
316 
321  public: void DrawLine(const math::Vector3 &_start,
322  const math::Vector3 &_end,
323  const std::string &_name);
324 
332  public: void SetFog(const std::string &_type,
333  const common::Color &_color,
334  double _density, double _start, double _end);
335 
338  public: uint32_t GetId() const;
339 
342  public: std::string GetIdString() const;
343 
346  public: void SetShadowsEnabled(bool _value);
347 
350  public: bool GetShadowsEnabled() const;
351 
354  public: void AddVisual(VisualPtr _vis);
355 
358  public: void RemoveVisual(VisualPtr _vis);
359 
362  public: void SetGrid(bool _enabled);
363 
366  public: VisualPtr GetWorldVisual() const;
367 
371  public: std::string StripSceneName(const std::string &_name) const;
372 
375  public: Heightmap *GetHeightmap() const;
376 
378  public: void Clear();
379 
381  public: VisualPtr CloneVisual(const std::string &_visualName,
382  const std::string &_newName) GAZEBO_DEPRECATED(2.0);
383 
387  public: VisualPtr GetSelectedVisual() const;
388 
391  public: void SetWireframe(bool _show);
392 
395  public: void SetTransparent(bool _show);
396 
399  public: void ShowCOMs(bool _show);
400 
403  public: void ShowJoints(bool _show);
404 
407  public: void ShowCollisions(bool _show);
408 
411  public: void ShowContacts(bool _show);
412 
415  public: void ShowClouds(bool _show);
416 
419  public: bool GetShowClouds() const;
420 
421 
426  public: void SetSkyXMode(unsigned int _mode);
427 
429  public: bool GetInitialized() const;
430 
436  public: common::Time GetSimTime() const;
437 
440  public: uint32_t GetVisualCount() const;
441 
443  public: void RemoveProjectors();
444 
446  private: void SetSky();
447 
449  private: void InitDeferredShading();
450 
457  private: Ogre::Entity *GetOgreEntityAt(CameraPtr _camera,
458  const math::Vector2i &_mousePos,
459  bool _ignorSelectionObj);
460 
470  // Code found in Wiki: www.ogre3d.org/wiki/index.php/RetrieveVertexData
471  private: void GetMeshInformation(const Ogre::Mesh *_mesh,
472  size_t &_vertexCount,
473  Ogre::Vector3* &_vertices,
474  size_t &_indexCount,
475  uint64_t* &_indices,
476  const Ogre::Vector3 &_position,
477  const Ogre::Quaternion &_orient,
478  const Ogre::Vector3 &_scale);
479 
483  private: void PrintSceneGraphHelper(const std::string &_prefix,
484  Ogre::Node *_node);
485 
489  private: void OnScene(ConstScenePtr &_msg);
490 
493  private: void OnResponse(ConstResponsePtr &_msg);
494 
497  private: void OnRequest(ConstRequestPtr &_msg);
498 
501  private: void OnJointMsg(ConstJointPtr &_msg);
502 
505  private: bool ProcessSensorMsg(ConstSensorPtr &_msg);
506 
509  private: bool ProcessJointMsg(ConstJointPtr &_msg);
510 
513  private: bool ProcessLinkMsg(ConstLinkPtr &_msg);
514 
517  private: bool ProcessSceneMsg(ConstScenePtr &_msg);
518 
521  private: bool ProcessModelMsg(const msgs::Model &_msg);
522 
525  private: void OnSensorMsg(ConstSensorPtr &_msg);
526 
529  private: void OnVisualMsg(ConstVisualPtr &_msg);
530 
533  private: bool ProcessVisualMsg(ConstVisualPtr &_msg);
534 
537  private: void OnLightMsg(ConstLightPtr &_msg);
538 
541  private: bool ProcessLightMsg(ConstLightPtr &_msg);
542 
545  private: void ProcessRequestMsg(ConstRequestPtr &_msg);
546 
549  private: void OnSelectionMsg(ConstSelectionPtr &_msg);
550 
553  private: void OnSkyMsg(ConstSkyPtr &_msg);
554 
557  private: void OnModelMsg(ConstModelPtr &_msg);
558 
561  private: void OnPoseMsg(ConstPosesStampedPtr &_msg);
562 
565  private: void OnSkeletonPoseMsg(ConstPoseAnimationPtr &_msg);
566 
570  private: void CreateCOMVisual(ConstLinkPtr &_msg, VisualPtr _linkVisual);
571 
575  private: void CreateCOMVisual(sdf::ElementPtr _elem,
576  VisualPtr _linkVisual);
577 
579  private: std::string name;
580 
582  private: sdf::ElementPtr sdf;
583 
585  private: std::vector<CameraPtr> cameras;
586 
588  private: std::vector<UserCameraPtr> userCameras;
589 
591  private: Ogre::SceneManager *manager;
592 
594  private: Ogre::RaySceneQuery *raySceneQuery;
595 
597  private: std::vector<Grid *> grids;
598 
600  private: static uint32_t idCounter;
601 
603  private: uint32_t id;
604 
606  private: std::string idString;
607 
610  typedef std::list<boost::shared_ptr<msgs::Visual const> > VisualMsgs_L;
611 
613  private: VisualMsgs_L visualMsgs;
614 
617  typedef std::list<boost::shared_ptr<msgs::Light const> > LightMsgs_L;
618 
620  private: LightMsgs_L lightMsgs;
621 
624  typedef std::map<uint32_t, msgs::Pose> PoseMsgs_M;
625 
627  private: PoseMsgs_M poseMsgs;
628 
631  typedef std::list<boost::shared_ptr<msgs::Scene const> > SceneMsgs_L;
632 
634  private: SceneMsgs_L sceneMsgs;
635 
638  typedef std::list<boost::shared_ptr<msgs::Joint const> > JointMsgs_L;
639 
641  private: JointMsgs_L jointMsgs;
642 
645  typedef std::list<boost::shared_ptr<msgs::Link const> > LinkMsgs_L;
646 
648  private: LinkMsgs_L linkMsgs;
649 
652  typedef std::list<boost::shared_ptr<msgs::Model const> > ModelMsgs_L;
654  private: ModelMsgs_L modelMsgs;
655 
658  typedef std::list<boost::shared_ptr<msgs::Sensor const> > SensorMsgs_L;
659 
661  private: SensorMsgs_L sensorMsgs;
662 
665  typedef std::list<boost::shared_ptr<msgs::Request const> > RequestMsgs_L;
667  private: RequestMsgs_L requestMsgs;
668 
671  typedef std::map<uint32_t, VisualPtr> Visual_M;
672 
674  private: Visual_M visuals;
675 
678  typedef std::map<std::string, LightPtr> Light_M;
679 
681  private: Light_M lights;
682 
685  typedef std::list<boost::shared_ptr<msgs::PoseAnimation const> >
686  SkeletonPoseMsgs_L;
688  private: SkeletonPoseMsgs_L skeletonPoseMsgs;
689 
691  private: boost::shared_ptr<msgs::Selection const> selectionMsg;
692 
694  private: boost::mutex *receiveMutex;
695 
697  private: boost::recursive_mutex poseMsgMutex;
698 
700  private: transport::NodePtr node;
701 
703  private: transport::SubscriberPtr sensorSub;
704 
706  private: transport::SubscriberPtr sceneSub;
707 
709  private: transport::SubscriberPtr requestSub;
710 
712  private: transport::SubscriberPtr visSub;
713 
715  private: transport::SubscriberPtr lightSub;
716 
718  private: transport::SubscriberPtr poseSub;
719 
721  private: transport::SubscriberPtr jointSub;
722 
724  private: transport::SubscriberPtr selectionSub;
725 
727  private: transport::SubscriberPtr responseSub;
728 
730  private: transport::SubscriberPtr skeletonPoseSub;
731 
733  private: transport::SubscriberPtr skySub;
734 
736  private: transport::SubscriberPtr modelInfoSub;
737 
739  private: transport::PublisherPtr lightPub;
740 
742  private: transport::PublisherPtr responsePub;
743 
745  private: transport::PublisherPtr requestPub;
746 
748  private: std::vector<event::ConnectionPtr> connections;
749 
751  private: VisualPtr worldVisual;
752 
754  private: VisualPtr selectedVis;
755 
761  private: std::string selectionMode;
762 
764  private: msgs::Request *requestMsg;
765 
767  private: bool enableVisualizations;
768 
770  private: Heightmap *terrain;
771 
773  private: std::map<std::string, Projector *> projectors;
774 
776  public: SkyX::SkyX *skyx;
777 
779  private: SkyX::BasicController *skyxController;
780 
782  private: bool showCOMs;
783 
785  private: bool showCollisions;
786 
788  private: bool showJoints;
789 
791  private: bool transparent;
792 
794  private: bool wireframe;
795 
797  private: bool initialized;
798 
801  private: common::Time sceneSimTimePosesReceived;
802 
805  private: common::Time sceneSimTimePosesApplied;
806 
808  private: uint32_t contactVisId;
809 
812  typedef boost::unordered_map<std::string,
813  boost::shared_ptr<msgs::Joint const> > JointMsgs_M;
814 
816  private: JointMsgs_M joints;
817  };
819  }
820 }
821 #endif