Scene.hh
00001 /*
00002  * Copyright 2011 Nate Koenig & Andrew Howard
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 #ifndef SCENE_HH
00018 #define SCENE_HH
00019 
00020 #include <vector>
00021 #include <map>
00022 #include <boost/shared_ptr.hpp>
00023 
00024 #include "sdf/sdf.h"
00025 #include "msgs/msgs.h"
00026 
00027 //#include "rendering/ogre.h"
00028 #include "rendering/RenderTypes.hh"
00029 
00030 #include "transport/TransportTypes.hh"
00031 #include "common/Events.hh"
00032 #include "common/Color.hh"
00033 #include "math/Vector2i.hh"
00034 
00035 
00036 namespace Ogre
00037 {
00038   class SceneManager;
00039   class RaySceneQuery;
00040   class Node;
00041   class Entity;
00042   class Mesh;
00043   class Vector3;
00044   class Quaternion;
00045 }
00046 
00047 namespace boost
00048 {
00049   class mutex;
00050 }
00051 
00052 namespace gazebo
00053 {
00054 
00055     namespace rendering
00056   {
00057     class Light;
00058     class Visual;
00059     class Grid;
00060     class Camera;
00061     class UserCamera;
00062 
00065 
00067     class Scene
00068     {
00069       public: enum SceneType {BSP, GENERIC};
00070 
00071       private: Scene() {}
00072   
00074       public: Scene(const std::string &_name, 
00075                     bool _enableVisualizations=false);
00076   
00078       public: virtual ~Scene();
00079   
00081       public: void Load(sdf::ElementPtr &_scene);
00082 
00084       public: void Load();
00085   
00087       public: void Init();
00088 
00090       public: void PreRender();
00091 
00093       public: Ogre::SceneManager *GetManager() const;
00094   
00096       public: std::string GetName() const;
00097   
00099       public: void SetAmbientColor(const common::Color &color);
00100   
00102       public: common::Color GetAmbientColor() const;
00103   
00105       public: void SetBackgroundColor(const common::Color &color);
00106 
00108       public: common::Color GetBackgroundColor() const;
00109   
00111       public: void CreateGrid(uint32_t cell_count, float cell_length, 
00112                               float line_width, const common::Color &color );
00113   
00115       public: Grid *GetGrid(unsigned int index) const;
00116   
00118       public: CameraPtr CreateCamera(const std::string &name, 
00119                                      bool _autoRender=true);
00120  
00122       public: DepthCameraPtr CreateDepthCamera( const std::string &_name, bool _autoRender=true);
00123 
00125       public: unsigned int GetCameraCount() const;
00126   
00128       public: CameraPtr GetCamera(unsigned int index) const;
00129 
00131       public: CameraPtr GetCamera( const std::string &_name ) const;
00132   
00134       public: UserCameraPtr CreateUserCamera(const std::string &name);
00135   
00137       public: unsigned int GetUserCameraCount() const;
00138   
00140       public: UserCameraPtr GetUserCamera(unsigned int index) const;
00141 
00143       public: VisualPtr GetVisual( const std::string &_name ) const;
00144 
00145       public: VisualPtr SelectVisualAt(CameraPtr camera, math::Vector2i mousePos);
00147       public: void SelectVisual( const std::string &_name ) const;
00148 
00155       public: VisualPtr GetVisualAt(CameraPtr camera, 
00156                                     math::Vector2i mousePos, 
00157                                     std::string &mod);
00158 
00160       public: VisualPtr GetVisualAt(CameraPtr camera, math::Vector2i mousePos);
00161 
00163       public: VisualPtr GetVisualBelowPoint(const math::Vector3 &_pt);
00164  
00166       private: Ogre::Entity *GetOgreEntityAt(CameraPtr _camera, 
00167                                              math::Vector2i _mousePos,
00168                                              bool _ignorSelectionObj);
00169 
00171       private: Ogre::Entity *GetOgreEntityBelowPoint(
00172                    const math::Vector3 &_mousePos, bool _ignorSelectionObj);
00173 
00175       public: math::Vector3 GetFirstContact(CameraPtr camera, 
00176                                             math::Vector2i mousePos);
00177   
00178       public: void PrintSceneGraph();
00179   
00181       public: void SetVisible(const std::string &name, bool visible);
00182   
00184       public: void DrawLine(const math::Vector3 &start, 
00185                             const math::Vector3 &end, 
00186                             const std::string &name);
00187   
00188       public: void SetFog( const std::string &_type, 
00189                            const common::Color &_color, 
00190                            double _density, double _start, double _end );
00191 
00192       // Get the scene ID
00193       public: unsigned int GetId() const;
00194   
00195       // Get the scene Id as a string
00196       public: std::string GetIdString() const;
00197  
00199       public: std::string GetUniqueName(const std::string &_prefix);
00200 
00202       public: SelectionObj *GetSelectionObj() const;
00203 
00205       private: void PrintSceneGraphHelper(const std::string &prefix, 
00206                                           Ogre::Node *node);
00207  
00209       public: void InitShadows();
00210 
00211       public: void SetSky(const std::string &_material);
00212 
00215       public: void SetShadowsEnabled(bool _value);
00216 
00218       public: bool GetShadowsEnabled() const;
00219 
00221       public: void AddVisual( VisualPtr &_vis );
00222 
00224       public: void RemoveVisual( VisualPtr _vis );
00225 
00228       public: void SetGrid( bool _enabled );
00229 
00230       public: VisualPtr GetWorldVisual() const;
00231 
00232 
00233       // \brief Get the mesh information for the given mesh.
00234       // Code found in Wiki: www.ogre3d.org/wiki/index.php/RetrieveVertexData
00235       private: void GetMeshInformation(const Ogre::Mesh *mesh,
00236                                        size_t &vertex_count,
00237                                        Ogre::Vector3* &vertices,
00238                                        size_t &index_count,
00239                                        unsigned long* &indices,
00240                                        const Ogre::Vector3 &position,
00241                                        const Ogre::Quaternion &orient,
00242                                        const Ogre::Vector3 &scale);
00243   
00244       private: void OnRequest(
00245                    ConstRequestPtr &_msg);
00246       private: void OnResponse(
00247                    ConstResponsePtr &_msg);
00248       private: void OnJointMsg(ConstJointPtr &_msg);
00249 
00250       private: void ProcessSensorMsg(
00251                    ConstSensorPtr &_msg);
00252       private: void ProcessJointMsg(
00253                    ConstJointPtr &_msg);
00254 
00255       private: void ProcessSceneMsg(
00256                    ConstScenePtr &_msg);
00257 
00258       private: void OnSceneMsg(
00259                    ConstScenePtr &_msg);
00260       private: void OnVisualMsg(
00261                    ConstVisualPtr &msg);
00262       private: void ProcessVisualMsg(
00263                    ConstVisualPtr &msg);
00264 
00265       private: void OnLightMsg(ConstLightPtr &msg);
00266       private: void ProcessLightMsg(
00267                    ConstLightPtr &msg);
00268 
00269       private: void OnSelectionMsg(
00270                    ConstSelectionPtr &msg);
00271                
00272       private: void OnPoseMsg(ConstPosePtr &msg);
00273 
00274       public: void Clear();
00275 
00276       //private: void ClearImpl();
00277       private: std::string name;
00278 
00279       private: sdf::ElementPtr sdf;
00280   
00281       private: std::vector<CameraPtr> cameras;
00282       private: std::vector<UserCameraPtr> userCameras;
00283   
00284       private: Ogre::SceneManager *manager;
00285       private: Ogre::RaySceneQuery *raySceneQuery;
00286   
00287       private: std::vector<Grid *> grids;
00288   
00289       private: static unsigned int idCounter;
00290       private: unsigned int id;
00291       private: std::string idString;
00292   
00293       typedef std::list<boost::shared_ptr<msgs::Visual const> > VisualMsgs_L;
00294       private: VisualMsgs_L visualMsgs;
00295 
00296       typedef std::list<boost::shared_ptr<msgs::Light const> > LightMsgs_L;
00297       private: LightMsgs_L lightMsgs;
00298 
00299       typedef std::list<boost::shared_ptr<msgs::Pose const> > PoseMsgs_L;
00300       private: PoseMsgs_L poseMsgs;
00301 
00302       typedef std::list<boost::shared_ptr<msgs::Scene const> > SceneMsgs_L;
00303       private: SceneMsgs_L sceneMsgs;
00304 
00305       typedef std::list<boost::shared_ptr<msgs::Joint const> > JointMsgs_L;
00306       private: JointMsgs_L jointMsgs;
00307 
00308       typedef std::list<boost::shared_ptr<msgs::Sensor const> > SensorMsgs_L;
00309       private: SensorMsgs_L sensorMsgs;
00310 
00311  
00312       typedef std::map<std::string, VisualPtr> Visual_M;
00313       private: Visual_M visuals;
00314 
00315       typedef std::map<std::string, Light*> Light_M;
00316       private: Light_M lights;
00317 
00318       private: boost::shared_ptr<msgs::Selection const> selectionMsg;
00319 
00320       private: boost::mutex *receiveMutex;
00321 
00322       private: transport::NodePtr node;  
00323       private: transport::SubscriberPtr sceneSub;
00324       private: transport::SubscriberPtr visSub;
00325       private: transport::SubscriberPtr lightSub;
00326       private: transport::SubscriberPtr poseSub;
00327       private: transport::SubscriberPtr selectionSub;
00328       private: transport::SubscriberPtr responseSub;
00329       private: transport::SubscriberPtr requestSub;
00330       private: transport::PublisherPtr requestPub;
00331 
00332       private: std::vector<event::ConnectionPtr> connections;
00333 
00334       private: SelectionObj *selectionObj;
00335 
00336       private: VisualPtr worldVisual;
00337       private: msgs::Request *requestMsg;
00338 
00339       private: bool enableVisualizations;
00340       private: bool clearAll;
00341     };
00343   }
00344 }
00345 #endif