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 2012 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(1.10);
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  private: void SetSky();
444 
446  private: void InitDeferredShading();
447 
454  private: Ogre::Entity *GetOgreEntityAt(CameraPtr _camera,
455  const math::Vector2i &_mousePos,
456  bool _ignorSelectionObj);
457 
467  // Code found in Wiki: www.ogre3d.org/wiki/index.php/RetrieveVertexData
468  private: void GetMeshInformation(const Ogre::Mesh *_mesh,
469  size_t &_vertexCount,
470  Ogre::Vector3* &_vertices,
471  size_t &_indexCount,
472  uint64_t* &_indices,
473  const Ogre::Vector3 &_position,
474  const Ogre::Quaternion &_orient,
475  const Ogre::Vector3 &_scale);
476 
480  private: void PrintSceneGraphHelper(const std::string &_prefix,
481  Ogre::Node *_node);
482 
486  private: void OnScene(ConstScenePtr &_msg);
487 
490  private: void OnResponse(ConstResponsePtr &_msg);
491 
494  private: void OnRequest(ConstRequestPtr &_msg);
495 
498  private: void OnJointMsg(ConstJointPtr &_msg);
499 
502  private: bool ProcessSensorMsg(ConstSensorPtr &_msg);
503 
506  private: bool ProcessJointMsg(ConstJointPtr &_msg);
507 
510  private: bool ProcessLinkMsg(ConstLinkPtr &_msg);
511 
514  private: bool ProcessSceneMsg(ConstScenePtr &_msg);
515 
518  private: bool ProcessModelMsg(const msgs::Model &_msg);
519 
522  private: void OnSensorMsg(ConstSensorPtr &_msg);
523 
526  private: void OnVisualMsg(ConstVisualPtr &_msg);
527 
530  private: bool ProcessVisualMsg(ConstVisualPtr &_msg);
531 
534  private: void OnLightMsg(ConstLightPtr &_msg);
535 
538  private: bool ProcessLightMsg(ConstLightPtr &_msg);
539 
542  private: void ProcessRequestMsg(ConstRequestPtr &_msg);
543 
546  private: void OnSelectionMsg(ConstSelectionPtr &_msg);
547 
550  private: void OnSkyMsg(ConstSkyPtr &_msg);
551 
554  private: void OnModelMsg(ConstModelPtr &_msg);
555 
558  private: void OnPoseMsg(ConstPosesStampedPtr &_msg);
559 
562  private: void OnSkeletonPoseMsg(ConstPoseAnimationPtr &_msg);
563 
567  private: void CreateCOMVisual(ConstLinkPtr &_msg, VisualPtr _linkVisual);
568 
572  private: void CreateCOMVisual(sdf::ElementPtr _elem,
573  VisualPtr _linkVisual);
574 
576  private: std::string name;
577 
579  private: sdf::ElementPtr sdf;
580 
582  private: std::vector<CameraPtr> cameras;
583 
585  private: std::vector<UserCameraPtr> userCameras;
586 
588  private: Ogre::SceneManager *manager;
589 
591  private: Ogre::RaySceneQuery *raySceneQuery;
592 
594  private: std::vector<Grid *> grids;
595 
597  private: static uint32_t idCounter;
598 
600  private: uint32_t id;
601 
603  private: std::string idString;
604 
607  typedef std::list<boost::shared_ptr<msgs::Visual const> > VisualMsgs_L;
608 
610  private: VisualMsgs_L visualMsgs;
611 
614  typedef std::list<boost::shared_ptr<msgs::Light const> > LightMsgs_L;
615 
617  private: LightMsgs_L lightMsgs;
618 
621  typedef std::map<uint32_t, msgs::Pose> PoseMsgs_M;
622 
624  private: PoseMsgs_M poseMsgs;
625 
628  typedef std::list<boost::shared_ptr<msgs::Scene const> > SceneMsgs_L;
629 
631  private: SceneMsgs_L sceneMsgs;
632 
635  typedef std::list<boost::shared_ptr<msgs::Joint const> > JointMsgs_L;
636 
638  private: JointMsgs_L jointMsgs;
639 
642  typedef std::list<boost::shared_ptr<msgs::Link const> > LinkMsgs_L;
643 
645  private: LinkMsgs_L linkMsgs;
646 
649  typedef std::list<boost::shared_ptr<msgs::Model const> > ModelMsgs_L;
651  private: ModelMsgs_L modelMsgs;
652 
655  typedef std::list<boost::shared_ptr<msgs::Sensor const> > SensorMsgs_L;
656 
658  private: SensorMsgs_L sensorMsgs;
659 
662  typedef std::list<boost::shared_ptr<msgs::Request const> > RequestMsgs_L;
664  private: RequestMsgs_L requestMsgs;
665 
668  typedef std::map<uint32_t, VisualPtr> Visual_M;
669 
671  private: Visual_M visuals;
672 
675  typedef std::map<std::string, LightPtr> Light_M;
676 
678  private: Light_M lights;
679 
682  typedef std::list<boost::shared_ptr<msgs::PoseAnimation const> >
683  SkeletonPoseMsgs_L;
685  private: SkeletonPoseMsgs_L skeletonPoseMsgs;
686 
688  private: boost::shared_ptr<msgs::Selection const> selectionMsg;
689 
691  private: boost::mutex *receiveMutex;
692 
694  private: boost::recursive_mutex poseMsgMutex;
695 
697  private: transport::NodePtr node;
698 
700  private: transport::SubscriberPtr sensorSub;
701 
703  private: transport::SubscriberPtr sceneSub;
704 
706  private: transport::SubscriberPtr requestSub;
707 
709  private: transport::SubscriberPtr visSub;
710 
712  private: transport::SubscriberPtr lightSub;
713 
715  private: transport::SubscriberPtr poseSub;
716 
718  private: transport::SubscriberPtr jointSub;
719 
721  private: transport::SubscriberPtr selectionSub;
722 
724  private: transport::SubscriberPtr responseSub;
725 
727  private: transport::SubscriberPtr skeletonPoseSub;
728 
730  private: transport::SubscriberPtr skySub;
731 
733  private: transport::SubscriberPtr modelInfoSub;
734 
736  private: transport::PublisherPtr lightPub;
737 
739  private: transport::PublisherPtr responsePub;
740 
742  private: transport::PublisherPtr requestPub;
743 
745  private: std::vector<event::ConnectionPtr> connections;
746 
748  private: VisualPtr worldVisual;
749 
751  private: VisualPtr selectedVis;
752 
758  private: std::string selectionMode;
759 
761  private: msgs::Request *requestMsg;
762 
764  private: bool enableVisualizations;
765 
767  private: Heightmap *terrain;
768 
770  private: std::map<std::string, Projector *> projectors;
771 
773  public: SkyX::SkyX *skyx;
774 
776  private: SkyX::BasicController *skyxController;
777 
779  private: bool showCOMs;
780 
782  private: bool showCollisions;
783 
785  private: bool showJoints;
786 
788  private: bool transparent;
789 
791  private: bool wireframe;
792 
794  private: bool initialized;
795 
798  private: common::Time sceneSimTimePosesReceived;
799 
802  private: common::Time sceneSimTimePosesApplied;
803 
805  private: uint32_t contactVisId;
806 
809  typedef boost::unordered_map<std::string,
810  boost::shared_ptr<msgs::Joint const> > JointMsgs_M;
811 
813  private: JointMsgs_M joints;
814  };
816  }
817 }
818 #endif