Flatland
world.h
1 /*
2  * ______ __ __ __
3  * /\ _ \ __ /\ \/\ \ /\ \__
4  * \ \ \L\ \ __ __ /\_\ \_\ \ \ \____ ___\ \ ,_\ ____
5  * \ \ __ \/\ \/\ \\/\ \ /'_` \ \ '__`\ / __`\ \ \/ /',__\
6  * \ \ \/\ \ \ \_/ |\ \ \/\ \L\ \ \ \L\ \/\ \L\ \ \ \_/\__, `\
7  * \ \_\ \_\ \___/ \ \_\ \___,_\ \_,__/\ \____/\ \__\/\____/
8  * \/_/\/_/\/__/ \/_/\/__,_ /\/___/ \/___/ \/__/\/___/
9  * @copyright Copyright 2017 Avidbots Corp.
10  * @name world.h
11  * @brief Definition for the simulation world
12  * @author Joseph Duchesne
13  *
14  * Software License Agreement (BSD License)
15  *
16  * Copyright (c) 2017, Avidbots Corp.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  *
23  * * Redistributions of source code must retain the above copyright
24  * notice, this list of conditions and the following disclaimer.
25  * * Redistributions in binary form must reproduce the above
26  * copyright notice, this list of conditions and the following
27  * disclaimer in the documentation and/or other materials provided
28  * with the distribution.
29  * * Neither the name of the Avidbots Corp. nor the names of its
30  * contributors may be used to endorse or promote products derived
31  * from this software without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
36  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
39  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
43  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44  * POSSIBILITY OF SUCH DAMAGE.
45  */
46 
47 #ifndef FLATLAND_SERVER_WORLD_H
48 #define FLATLAND_SERVER_WORLD_H
49 
50 #include <Box2D/Box2D.h>
51 #include <flatland_server/collision_filter_registry.h>
52 #include <flatland_server/interactive_marker_manager.h>
53 #include <flatland_server/layer.h>
54 #include <flatland_server/model.h>
55 #include <flatland_server/plugin_manager.h>
56 #include <flatland_server/timekeeper.h>
57 #include <map>
58 #include <string>
59 #include <string>
60 #include <vector>
61 
62 namespace flatland_server {
63 
69 class World : public b2ContactListener {
70  public:
71  boost::filesystem::path world_yaml_dir_;
72  b2World *physics_world_;
73  b2Vec2 gravity_;
74  std::map<std::vector<std::string>, Layer *>
76  std::vector<Layer *> layers_;
77  std::vector<Model *> models_;
86 
91  World();
92 
96  ~World();
97 
102  void Update(Timekeeper &timekeeper);
103 
108  void BeginContact(b2Contact *contact) override;
109 
114  void EndContact(b2Contact *contact) override;
115 
121  void PreSolve(b2Contact *contact, const b2Manifold *oldManifold);
122 
128  void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse);
129 
130  /*
131  * @brief Load world plugins
132  * @param[in] world_plugin_reader, readin the info about the plugin
133  * @param[in] world, the world where the plugin will be applied to
134  * @param[in] world config, the yaml reader of world.yaml
135  */
136  void LoadWorldPlugins(YamlReader &world_plugin_reader, World *world,
137  YamlReader &world_config);
142  void LoadLayers(YamlReader &layers_reader);
143 
148  void LoadModels(YamlReader &models_reader);
149 
157  void LoadModel(const std::string &model_yaml_path, const std::string &ns,
158  const std::string &name, const Pose &pose);
159 
164  void DeleteModel(const std::string &name);
165 
171  void MoveModel(const std::string &name, const Pose &pose);
172 
176  void Pause();
177 
181  void Resume();
182 
186  void TogglePaused();
187 
192  bool IsPaused();
193 
200  static World *MakeWorld(const std::string &yaml_path);
201 
207  void DebugVisualize(bool update_layers = true);
208 };
209 }; // namespace flatland_server
210 #endif // FLATLAND_SERVER_WORLD_H
World()
Constructor for the world class. All data required for initialization should be passed in here...
Definition: world.cpp:61
std::map< std::vector< std::string >, Layer * > layers_name_map_
map of all layers and thier name
Definition: world.h:75
Definition: collision_filter_registry.h:63
void Update(Timekeeper &timekeeper)
trigger world update include all physics and plugins
Definition: world.cpp:101
void PreSolve(b2Contact *contact, const b2Manifold *oldManifold)
Box2D inherited presolve.
Definition: world.cpp:120
boost::filesystem::path world_yaml_dir_
directory containing world file
Definition: world.h:71
static World * MakeWorld(const std::string &yaml_path)
factory method to create a instance of the world class. Cleans all the inputs before instantiation of...
Definition: world.cpp:128
bool IsPaused()
returns true if service_paused_ is true or an interactive marker is currently being dragged ...
Definition: world.cpp:339
void Pause()
set the paused state of the simulation to true
Definition: world.cpp:333
std::vector< Layer * > layers_
list of layers
Definition: world.h:76
PluginManager plugin_manager_
for loading and updating plugins
Definition: world.h:79
b2World * physics_world_
Box2D physics world.
Definition: world.h:72
bool service_paused_
Definition: world.h:80
void BeginContact(b2Contact *contact) override
Box2D inherited begin contact.
Definition: world.cpp:112
Definition: body.h:55
Definition: layer.h:65
std::vector< Model * > models_
list of models
Definition: world.h:77
void DebugVisualize(bool update_layers=true)
Publish debug visualizations for everything.
Definition: world.cpp:343
void LoadModel(const std::string &model_yaml_path, const std::string &ns, const std::string &name, const Pose &pose)
load models into the world. Throws YAMLException.
Definition: world.cpp:244
void EndContact(b2Contact *contact) override
Box2D inherited end contact.
Definition: world.cpp:116
void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse)
Box2D inherited pre solve.
Definition: world.cpp:124
void DeleteModel(const std::string &name)
remove model with a given name
Definition: world.cpp:292
Definition: types.h:84
~World()
Destructor for the world class.
Definition: world.cpp:69
void LoadModels(YamlReader &models_reader)
load models into the world. Throws YAMLException.
Definition: world.cpp:219
void Resume()
set the paused state of the simulation to false
Definition: world.cpp:335
void TogglePaused()
toggle the paused state of the simulation
Definition: world.cpp:337
int physics_velocity_iterations_
Box2D solver param.
Definition: world.h:85
Definition: interactive_marker_manager.h:14
b2Vec2 gravity_
Box2D world gravity, always (0, 0)
Definition: world.h:73
void MoveModel(const std::string &name, const Pose &pose)
move model with a given name
Definition: world.cpp:314
InteractiveMarkerManager int_marker_manager_
for dynamically moving models from Rviz
Definition: world.h:83
Definition: plugin_manager.h:64
Definition: world.h:69
Definition: timekeeper.h:55
CollisionFilterRegistry cfr_
collision registry for layers and models
Definition: world.h:78
Definition: yaml_reader.h:76
int physics_position_iterations_
Box2D solver param.
Definition: world.h:84
void LoadLayers(YamlReader &layers_reader)
load layers into the world. Throws YAMLException.
Definition: world.cpp:167