World.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2016 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 _GAZEBO_WORLD_HH_
18 #define _GAZEBO_WORLD_HH_
19 
20 #ifdef _WIN32
21  // Ensure that Winsock2.h is included before Windows.h, which can get
22  // pulled in by anybody (e.g., Boost).
23  #include <Winsock2.h>
24 #endif
25 
26 #include <vector>
27 #include <list>
28 #include <set>
29 #include <deque>
30 #include <string>
31 #include <boost/thread.hpp>
32 #include <boost/enable_shared_from_this.hpp>
33 #include <boost/shared_ptr.hpp>
34 
35 #include <sdf/sdf.hh>
36 
38 
39 #include "gazebo/msgs/msgs.hh"
40 
43 #include "gazebo/common/Event.hh"
44 
45 #include "gazebo/physics/Base.hh"
48 #include "gazebo/util/system.hh"
49 
50 namespace gazebo
51 {
52  namespace physics
53  {
55  class WorldPrivate;
56 
59 
68  class GZ_PHYSICS_VISIBLE World :
69  public boost::enable_shared_from_this<World>
70  {
74  public: explicit World(const std::string &_name = "");
75 
77  public: ~World();
78 
82  public: void Load(sdf::ElementPtr _sdf);
83 
87  public: void Save(const std::string &_filename);
88 
91  public: void Init();
92 
97  public: void Run(unsigned int _iterations = 0);
98 
101  public: bool GetRunning() const;
102 
105  public: void Stop();
106 
109  public: void Fini();
110 
114  public: void Clear();
115 
118  public: std::string GetName() const;
119 
123  public: PhysicsEnginePtr GetPhysicsEngine() const;
124 
127  public: PresetManagerPtr GetPresetManager() const;
128 
131  public: common::SphericalCoordinatesPtr GetSphericalCoordinates() const;
132 
135  public: ignition::math::Vector3d Gravity() const;
136 
139  public: void SetGravity(const ignition::math::Vector3d &_gravity);
140 
143  public: void SetGravitySDF(const ignition::math::Vector3d &_gravity);
144 
147  public: virtual ignition::math::Vector3d MagneticField() const;
148 
151  public: void SetMagneticField(const ignition::math::Vector3d &_mag);
152 
155  public: unsigned int GetModelCount() const;
156 
162  public: ModelPtr GetModel(unsigned int _index) const;
163 
166  public: Model_V GetModels() const;
167 
170  public: unsigned int LightCount() const;
171 
174  public: Light_V Lights() const;
175 
180  public: void ResetEntities(Base::EntityType _type = Base::BASE);
181 
183  public: void ResetTime();
184 
186  public: void Reset();
187 
190  public: void PrintEntityTree();
191 
195  public: common::Time GetSimTime() const;
196 
199  public: void SetSimTime(const common::Time &_t);
200 
203  public: common::Time GetPauseTime() const;
204 
207  public: common::Time GetStartTime() const;
208 
211  public: common::Time GetRealTime() const;
212 
215  public: bool IsPaused() const;
216 
219  public: void SetPaused(bool _p);
220 
226  public: BasePtr GetByName(const std::string &_name);
227 
233  public: ModelPtr GetModel(const std::string &_name);
234 
240  public: LightPtr Light(const std::string &_name);
241 
247  public: EntityPtr GetEntity(const std::string &_name);
248 
256  public: ModelPtr GetModelBelowPoint(const math::Vector3 &_pt);
257 
263  public: EntityPtr GetEntityBelowPoint(const math::Vector3 &_pt);
264 
267  public: void SetState(const WorldState &_state);
268 
272  public: void InsertModelFile(const std::string &_sdfFilename);
273 
277  public: void InsertModelString(const std::string &_sdfString);
278 
282  public: void InsertModelSDF(const sdf::SDF &_sdf);
283 
287  public: std::string StripWorldName(const std::string &_name) const;
288 
292  public: void EnableAllModels();
293 
297  public: void DisableAllModels();
298 
301  public: void Step(unsigned int _steps);
302 
307  public: void LoadPlugin(const std::string &_filename,
308  const std::string &_name,
309  sdf::ElementPtr _sdf);
310 
313  public: void RemovePlugin(const std::string &_name);
314 
317  public: boost::mutex *GetSetWorldPoseMutex() const;
318 
321  public: bool GetEnablePhysicsEngine();
322 
325  public: void EnablePhysicsEngine(bool _enable);
326 
328  public: void UpdateStateSDF();
329 
332  public: bool IsLoaded() const;
333 
336  public: void ClearModels();
337 
342  public: void PublishModelPose(physics::ModelPtr _model);
343 
348  public: void PublishModelScale(physics::ModelPtr _model);
349 
354  public: void PublishLightPose(const physics::LightPtr _light);
355 
358  public: uint32_t GetIterations() const;
359 
362  public: msgs::Scene GetSceneMsg() const;
363 
368  public: void RunBlocking(unsigned int _iterations = 0);
369 
374  public: void RemoveModel(ModelPtr _model);
375 
380  public: void RemoveModel(const std::string &_name);
381 
384  public: void ResetPhysicsStates();
385 
392  public: void _AddDirty(Entity *_entity);
393 
396  public: bool SensorsInitialized() const;
397 
402  public: void _SetSensorsInitialized(const bool _init);
403 
409  public: std::string UniqueModelName(const std::string &_name);
410 
418  private: ModelPtr GetModelById(unsigned int _id);
420 
424  private: void LoadPlugins();
425 
429  private: void LoadEntities(sdf::ElementPtr _sdf, BasePtr _parent);
430 
435  private: ModelPtr LoadModel(sdf::ElementPtr _sdf, BasePtr _parent);
436 
441  private: LightPtr LoadLight(const sdf::ElementPtr &_sdf,
442  const BasePtr &_parent);
443 
448  private: ActorPtr LoadActor(sdf::ElementPtr _sdf, BasePtr _parent);
449 
454  private: RoadPtr LoadRoad(sdf::ElementPtr _sdf, BasePtr _parent);
455 
457  private: void RunLoop();
458 
460  private: void Step();
461 
463  private: void LogStep();
464 
466  private: void Update();
467 
470  private: void OnPause(bool _p);
471 
473  private: void OnStep();
474 
477  private: void OnControl(ConstWorldControlPtr &_data);
478 
481  private: void OnPlaybackControl(ConstLogPlaybackControlPtr &_data);
482 
485  private: void OnRequest(ConstRequestPtr &_msg);
486 
491  private: void BuildSceneMsg(msgs::Scene &_scene, BasePtr _entity);
492 
495  private: void JointLog(ConstJointPtr &_msg);
496 
499  private: void OnFactoryMsg(ConstFactoryPtr &_data);
500 
503  private: void OnModelMsg(ConstModelPtr &_msg);
504 
506  private: void ModelUpdateTBB();
507 
509  private: void ModelUpdateSingleLoop();
510 
513  private: void LoadPlugin(sdf::ElementPtr _sdf);
514 
518  private: void FillModelMsg(msgs::Model &_msg, ModelPtr _model);
519 
522  private: void ProcessEntityMsgs();
523 
526  private: void ProcessRequestMsgs();
527 
530  private: void ProcessFactoryMsgs();
531 
534  private: void ProcessModelMsgs();
535 
538  private: void ProcessLightFactoryMsgs();
539 
542  private: void ProcessLightModifyMsgs();
543 
545  private: bool OnLog(std::ostringstream &_stream);
546 
548  private: void ProcessMessages();
549 
551  private: void PublishWorldStats();
552 
554  private: void LogWorker();
555 
561  private: void OnLightMsg(ConstLightPtr &_msg);
562 
566  private: void OnLightFactoryMsg(ConstLightPtr &_msg);
567 
571  private: void OnLightModifyMsg(ConstLightPtr &_msg);
572 
575  private: WorldPrivate *dataPtr;
576 
578  private: friend class DARTLink;
579 
581  private: friend class SimbodyPhysics;
582  };
584  }
585 }
586 #endif
boost::shared_ptr< Actor > ActorPtr
Definition: PhysicsTypes.hh:92
boost::shared_ptr< PresetManager > PresetManagerPtr
Definition: PhysicsTypes.hh:120
A light entity.
Definition: physics/Light.hh:33
Forward declarations for the common classes.
Definition: Animation.hh:33
std::vector< ModelPtr > Model_V
Definition: PhysicsTypes.hh:196
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:39
std::vector< LightPtr > Light_V
Definition: PhysicsTypes.hh:212
Forward declarations for transport.
default namespace for gazebo
Base type.
Definition: Base.hh:87
boost::shared_ptr< PhysicsEngine > PhysicsEnginePtr
Definition: PhysicsTypes.hh:116
The world provides access to all other object within a simulated environment.
Definition: World.hh:68
Simbody physics engine.
Definition: SimbodyPhysics.hh:42
boost::shared_ptr< SphericalCoordinates > SphericalCoordinatesPtr
Definition: CommonTypes.hh:140
Base class for all physics objects in Gazebo.
Definition: Entity.hh:58
boost::shared_ptr< Entity > EntityPtr
Definition: PhysicsTypes.hh:80
boost::shared_ptr< Road > RoadPtr
Definition: PhysicsTypes.hh:152
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:88
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
EntityType
Unique identifiers for all entity types.
Definition: Base.hh:85
boost::shared_ptr< Light > LightPtr
Definition: PhysicsTypes.hh:96
Store state information of a physics::World object.
Definition: WorldState.hh:47
boost::shared_ptr< Base > BasePtr
Definition: PhysicsTypes.hh:72
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:44