World.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 /* Desc: The world; all models are collected here
00018  * Author: Andrew Howard and Nate Koenig
00019  * Date: 3 Apr 2007
00020  */
00021 
00022 #ifndef WORLD_HH
00023 #define WORLD_HH
00024 
00025 #include <vector>
00026 #include <list>
00027 #include <string>
00028 #include <boost/enable_shared_from_this.hpp>
00029 #include <boost/shared_ptr.hpp>
00030 
00031 #include "transport/TransportTypes.hh"
00032 
00033 #include "msgs/msgs.h"
00034 
00035 #include "sensors/SensorTypes.hh"
00036 
00037 #include "common/CommonTypes.hh"
00038 #include "common/Event.hh"
00039 
00040 #include "physics/PhysicsTypes.hh"
00041 #include "sdf/sdf.h"
00042 
00043 namespace boost
00044 {
00045   class thread;
00046   class mutex;
00047   class recursive_mutex;
00048 }
00049 
00050 namespace gazebo
00051 {
00052     namespace physics
00053   {
00056 
00057 
00059     /*
00060      * The world class keps a list of all models, handles loading and saving,
00061      * object dynamics and collision detection for contact joints
00062      */
00063     class World : public boost::enable_shared_from_this<World>
00064     {
00066       public: World(const std::string &_name="");
00067     
00069       public: ~World();
00070     
00073       public: void Load( sdf::ElementPtr _sdf );
00074     
00075       public: void Save(const std::string &_filename);
00076 
00078       public: void Init();
00079     
00081       public: void Run();
00082 
00084       public: void Stop();
00085  
00087       public: void Fini();
00088     
00090       public: void Clear();
00091     
00093       public: std::string GetName() const;
00094     
00096       public: unsigned int GetParamCount() const;
00097     
00099       public: common::Param *GetParam(unsigned int index) const;
00100     
00103       public: PhysicsEnginePtr GetPhysicsEngine() const;
00104    
00106       public: unsigned int GetModelCount() const;
00107     
00109       public: ModelPtr GetModel(unsigned int index);
00110     
00112       public: void Reset(bool _resetTime=true);
00113     
00115       public: EntityPtr GetSelectedEntity() const;
00116 
00118       public: void PrintEntityTree();
00119     
00122       public: common::Time GetSimTime() const;
00123     
00125       public: void SetSimTime(common::Time t);
00126     
00129       public: common::Time GetPauseTime() const;
00130     
00133       public: common::Time GetStartTime() const;
00134     
00137       public: common::Time GetRealTime() const;
00138     
00140       public: bool IsPaused() const;
00141     
00143       public: void SetPaused(bool p);
00144     
00146       public: BasePtr GetByName(const std::string &name);
00147 
00149       public: ModelPtr GetModelById(unsigned int _id);
00150 
00152       public: ModelPtr GetModelByName(const std::string &name);
00153 
00155       public: EntityPtr GetEntityByName(const std::string &_name);
00156 
00158       public: ModelPtr GetModelBelowPoint(const math::Vector3 &_pt);
00159 
00161       public: EntityPtr GetEntityBelowPoint(const math::Vector3 &_pt);
00162 
00166       private: void LoadEntities( sdf::ElementPtr &_sdf , BasePtr parent);
00167 
00169       private: ModelPtr LoadModel( sdf::ElementPtr &_sdf, BasePtr parent);
00170  
00172       private: void RunLoop();
00173     
00175       private: void Update();
00176  
00178       private: void OnPause(bool p);
00179     
00181       private: void OnStep();
00182 
00183       private: void OnControl( ConstWorldControlPtr &data );
00184     
00185       private: void OnRequest( ConstRequestPtr &_msg );
00186 
00189       private: void DeleteEntityCB(const std::string &name);
00190     
00192       private: void SetSelectedEntityCB( const std::string &name );
00193     
00194       private: void OnEntitiesRequest( ConstRequestPtr &_data );
00195 
00197       private: void BuildSceneMsg(msgs::Scene &scene, BasePtr entity);
00198 
00199       private: void JointLog(ConstJointPtr &msg);
00200 
00201       private: void OnFactoryMsg(
00202                    ConstFactoryPtr &data);
00203       private: void OnModelMsg(
00204                    ConstModelPtr &_msg);
00205 
00207       private: void ModelUpdateTBB();
00208 
00210       private: void ModelUpdateSingleLoop();
00211 
00212       private: void LoadPlugin( sdf::ElementPtr &_sdf );
00213 
00214       private: void FillModelMsg(msgs::Model &_msg, ModelPtr &_model);
00215 
00216       private: void ProcessEntityMsgs();
00217       private: void ProcessRequestMsgs();
00218       private: void ProcessFactoryMsgs();
00219       private: void ProcessModelMsgs();
00220 
00222       private: PhysicsEnginePtr physicsEngine;
00223     
00224       private: BasePtr rootElement;
00225  
00227       private: boost::thread *thread;
00228     
00229       private: bool stop;
00230  
00232       protected: common::Param_V parameters;
00233     
00235       private: EntityPtr selectedEntity;
00236     
00237       private: std::vector<google::protobuf::Message> messages;
00238     
00239       private: std::string name;
00240                
00242       private: common::Time simTime, pauseTime, startTime;
00243       private: bool pause;
00244       private: bool stepInc;
00245     
00246       private: event::Connection_V connections;
00247 
00248       private: transport::NodePtr node;    
00249       private: transport::PublisherPtr selectionPub;
00250       private: transport::PublisherPtr statPub, responsePub, modelPub;
00251       private: transport::PublisherPtr guiPub, scenePub;
00252 
00253       private: transport::SubscriberPtr controlSub;
00254       private: transport::SubscriberPtr factorySub, jointSub;
00255       private: transport::SubscriberPtr modelSub, requestSub;
00256 
00257       private: msgs::WorldStatistics worldStatsMsg;
00258       private: msgs::Scene sceneMsg;
00259 
00260       private: void (World::*modelUpdateFunc)();
00261 
00262       private: common::Time statPeriod;
00263       private: common::Time prevStatTime;
00264       private: common::Time pauseStartTime;
00265       private: common::Time realTimeOffset;
00266 
00267       private: boost::mutex *receiveMutex;
00268 
00271       public: boost::recursive_mutex *modelWorldPoseUpdateMutex;
00272 
00273       private: sdf::ElementPtr sdf;
00274 
00275       private: std::vector<WorldPluginPtr> plugins;
00276       private: std::list<std::string> deleteEntity;
00277 
00278       public: std::list<Entity*> dirtyPoses;
00279 
00280       private: std::list<msgs::Request> requestMsgs;
00281       private: std::list<msgs::Factory> factoryMsgs;
00282       private: std::list<msgs::Model> modelMsgs;
00283 
00284       private: bool needsReset;
00285     };
00286 
00288   }
00289 }
00290 #endif