World.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <Box2D/Dynamics/b2Body.h>
13 #include <Box2D/Dynamics/b2World.h>
14 #include <mrpt/core/bits_math.h>
15 #include <mrpt/core/format.h>
16 #include <mrpt/gui/CDisplayWindowGUI.h>
17 #include <mrpt/img/TColor.h>
18 #include <mrpt/math/TPoint3D.h>
19 #include <mrpt/obs/CObservation.h>
20 #include <mrpt/system/COutputLogger.h>
21 #include <mrpt/system/CTicTac.h>
22 #include <mrpt/system/CTimeLogger.h>
23 #include <mvsim/Block.h>
24 #include <mvsim/Comms/Client.h>
26 #include <mvsim/VehicleBase.h>
28 
29 #include <list>
30 
31 namespace mvsim
32 {
41 class World : public mrpt::system::COutputLogger
42 {
43  public:
46  World();
47  ~World();
48 
51  void clear_all();
52 
60  void load_from_XML(
61  const std::string& xml_text,
62  const std::string& fileNameForPath = std::string("."));
68  double get_simul_time() const
69  {
70  return m_simul_time;
71  }
72 
73  double get_simul_timestep() const
74  {
75  return m_simul_timestep;
76  }
77  void set_simul_timestep(double timestep)
78  {
79  m_simul_timestep = timestep;
80  }
81 
82  double get_gravity() const
83  {
84  return m_gravity;
85  }
86  void set_gravity(double accel)
88  {
89  m_gravity = accel;
90  }
91 
98  void run_simulation(double dt);
99 
102  {
103  int keycode = 0;
104  bool modifierShift = false;
105  bool modifierCtrl = false;
106  bool modifierAlt = false;
107  bool modifierSuper = false;
108 
109  TGUIKeyEvent() = default;
110  };
111 
113  {
115  std::string msg_lines;
116 
117  TUpdateGUIParams() = default;
118  };
119 
127  void update_GUI(TUpdateGUIParams* params = nullptr);
128 
129  const mrpt::gui::CDisplayWindowGUI::Ptr& gui_window() const
130  {
131  return m_gui.gui_win;
132  }
133 
135  mrpt::opengl::COpenGLScene::Ptr& gl_scene);
136  void internal_GUI_thread();
138 
139  std::string m_gui_msg_lines;
141 
142  std::thread m_gui_thread;
143 
144  std::atomic_bool m_gui_thread_running = false;
145  std::atomic_bool m_gui_thread_must_close = false;
147 
148  void enqueue_task_to_run_in_gui_thread(const std::function<void(void)>& f)
149  {
151  m_gui_user_pending_tasks.emplace_back(f);
153  }
154 
155  std::vector<std::function<void(void)>> m_gui_user_pending_tasks;
157 
159  std::atomic_bool m_lastKeyEventValid = false;
160  std::mutex m_lastKeyEvent_mtx;
161 
162  bool is_GUI_open() const;
163 
165  void close_GUI();
166 
173  using VehicleList = std::multimap<std::string, VehicleBase::Ptr>;
174 
176  using WorldElementList = std::list<WorldElementBase::Ptr>;
177 
179  using BlockList = std::multimap<std::string, Block::Ptr>;
180 
183  using SimulableList = std::multimap<std::string, Simulable::Ptr>;
184 
189  std::unique_ptr<b2World>& getBox2DWorld() { return m_box2d_world; }
190  const std::unique_ptr<b2World>& getBox2DWorld() const
191  {
192  return m_box2d_world;
193  }
195  const VehicleList& getListOfVehicles() const { return m_vehicles; }
197  const BlockList& getListOfBlocks() const { return m_blocks; }
200  {
201  return m_world_elements;
202  }
203 
206  {
207  return m_simulableObjects;
208  }
209 
210  mrpt::system::CTimeLogger& getTimeLogger() { return m_timlogger; }
213  std::string resolvePath(const std::string& in_path) const;
214 
215  std::string xmlPathToActualPath(const std::string& modelURI) const;
216 
222  using vehicle_visitor_t = std::function<void(VehicleBase&)>;
223  using world_element_visitor_t = std::function<void(WorldElementBase&)>;
224 
227 
230 
235  virtual void onNewObservation(
236  [[maybe_unused]] const VehicleBase& veh,
237  [[maybe_unused]] const mrpt::obs::CObservation* obs)
238  {
239  /* default: do nothing */
240  }
245  void connectToServer();
246 
248  const mvsim::Client& commsClient() const { return m_client; }
249 
250  private:
251  friend class VehicleBase;
252  friend class Block;
253 
255 
256  // -------- World Params ----------
259  double m_gravity = 9.81;
260 
262  double m_simul_timestep = 10e-3;
263 
266 
268  {"gravity", {"%lf", &m_gravity}},
269  {"simul_timestep", {"%lf", &m_simul_timestep}},
270  {"b2d_vel_iters", {"%i", &m_b2d_vel_iters}},
271  {"b2d_pos_iters", {"%i", &m_b2d_pos_iters}},
272  };
273 
276  double m_simul_time = 0;
277 
279  std::string m_base_path{"."};
280 
281  // ------- GUI options -----
283  {
284  unsigned int win_w = 800, win_h = 600;
285  bool start_maximized = true;
286  int refresh_fps = 20;
287  bool ortho = false;
288  bool show_forces = false;
289  double force_scale = 0.01;
290  double camera_distance = 80.0;
291  double fov_deg = 60.0;
293  std::string follow_vehicle;
294 
296  {"win_w", {"%u", &win_w}},
297  {"win_h", {"%u", &win_h}},
298  {"ortho", {"%bool", &ortho}},
299  {"show_forces", {"%bool", &show_forces}},
300  {"force_scale", {"%lf", &force_scale}},
301  {"fov_deg", {"%lf", &fov_deg}},
302  {"follow_vehicle", {"%s", &follow_vehicle}},
303  {"start_maximized", {"%bool", &start_maximized}},
304  {"refresh_fps", {"%i", &refresh_fps}},
305  };
306 
307  TGUI_Options() = default;
308  void parse_from(const rapidxml::xml_node<char>& node);
309  };
310 
312 
314  // -------- World contents ----------
316  std::recursive_mutex m_world_cs;
317 
319  std::unique_ptr<b2World> m_box2d_world;
320 
323 
327 
328  // List of all objects above (vehicles, world_elements, blocks), but as
329  // shared_ptr to their Simulable interfaces, so we can easily iterate on
330  // this list only for common tasks:
332 
334  void internal_one_timestep(double dt);
335 
337 
339  struct GUI
340  {
341  GUI(World& parent) : m_parent(parent) {}
342 
343  mrpt::gui::CDisplayWindowGUI::Ptr gui_win;
344  nanogui::Label* lbCpuUsage = nullptr;
345  std::vector<nanogui::Label*> lbStatuses;
346  nanogui::Button* btnReplaceObject = nullptr;
347 
349  {
350  nanogui::CheckBox* cb = nullptr;
352  VisualObject* visual = nullptr;
353  };
354 
355  // Buttons that must be {dis,en}abled when there is a selected object:
356  std::vector<nanogui::Widget*> btns_selectedOps;
357  std::vector<InfoPerObject> gui_cbObjects;
359 
360  mrpt::math::TPoint3D clickedPt{0, 0, 0};
361 
362  void prepare_top_menu();
363  void prepare_status_window();
364  void prepare_editor_window();
365 
366  void handle_mouse_operations();
367 
368  private:
370  };
371  GUI m_gui{*this};
372  // end GUI stuff
374 
375  mrpt::system::CTimeLogger m_timlogger;
376  mrpt::system::CTicTac m_timer_iteration;
377 
379  void insertBlock(const Block::Ptr& block);
380 };
381 } // namespace mvsim
const mvsim::Client & commsClient() const
Definition: World.h:248
const GLdouble * v
mvsim::Client m_client
Definition: World.h:254
SimulableList m_simulableObjects
Definition: World.h:331
std::shared_ptr< Simulable > Ptr
Definition: Simulable.h:27
void set_simul_timestep(double timestep)
Simulation fixed-time interval for numerical integration.
Definition: World.h:77
const std::unique_ptr< b2World > & getBox2DWorld() const
Definition: World.h:190
virtual void onNewObservation([[maybe_unused]] const VehicleBase &veh, [[maybe_unused]] const mrpt::obs::CObservation *obs)
Definition: World.h:235
int m_b2d_pos_iters
Definition: World.h:265
std::recursive_mutex m_world_cs
first time the GUI window is created.
Definition: World.h:316
std::map< std::string, TParamEntry > TParameterDefinitions
std::multimap< std::string, Simulable::Ptr > SimulableList
Definition: World.h:183
~World()
Dtor.
Definition: World.cpp:33
WorldElementList m_world_elements
Definition: World.h:325
mrpt::system::CTicTac m_timer_iteration
Definition: World.h:376
const VehicleList & getListOfVehicles() const
Definition: World.h:195
std::vector< InfoPerObject > gui_cbObjects
Definition: World.h:357
std::string xmlPathToActualPath(const std::string &modelURI) const
Definition: World.cpp:140
std::vector< std::function< void(void)> > m_gui_user_pending_tasks
Definition: World.h:155
double m_simul_time
Definition: World.h:276
std::mutex m_gui_msg_lines_mtx
Definition: World.h:140
mrpt::system::CTimeLogger m_timlogger
Definition: World.h:375
void runVisitorOnWorldElements(const world_element_visitor_t &v)
Definition: World.cpp:199
std::multimap< std::string, VehicleBase::Ptr > VehicleList
Definition: World.h:173
InfoPerObject gui_selectedObject
Definition: World.h:358
std::string follow_vehicle
Definition: World.h:293
void internalUpdate3DSceneObjects(mrpt::opengl::COpenGLScene::Ptr &gl_scene)
Definition: World_gui.cpp:549
std::unique_ptr< b2World > & getBox2DWorld()
Definition: World.h:189
mrpt::gui::CDisplayWindowGUI::Ptr gui_win
Definition: World.h:343
const SimulableList & getListOfSimulableObjects() const
Definition: World.h:205
std::shared_ptr< Block > Ptr
Definition: Block.h:36
Simulable::Ptr simulable
Definition: World.h:351
std::function< void(VehicleBase &)> vehicle_visitor_t
Definition: World.h:222
std::multimap< std::string, Block::Ptr > BlockList
Definition: World.h:179
VehicleList m_vehicles
Definition: World.h:324
void internal_one_timestep(double dt)
Definition: World.cpp:97
std::unique_ptr< b2World > m_box2d_world
Definition: World.h:319
void insertBlock(const Block::Ptr &block)
Definition: World.cpp:311
TGUIKeyEvent keyevent
Keystrokes in the window are returned here.
Definition: World.h:114
double get_gravity() const
Definition: World.h:82
const mrpt::gui::CDisplayWindowGUI::Ptr & gui_window() const
Definition: World.h:129
double get_simul_timestep() const
Simulation fixed-time interval for numerical integration.
Definition: World.h:73
void clear_all()
Definition: World.cpp:48
void internal_GUI_thread()
Definition: World_gui.cpp:339
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
GUI m_gui
gui state
Definition: World.h:371
void set_gravity(double accel)
Definition: World.h:87
std::string m_gui_msg_lines
Definition: World.h:139
std::mutex m_simulationStepRunningMtx
Definition: World.h:336
bool is_GUI_open() const
Forces closing the GUI window, if any.
Definition: World_gui.cpp:42
std::string resolvePath(const std::string &in_path) const
Definition: World.cpp:162
b2Body * m_b2_ground_body
Definition: World.h:322
std::atomic_bool m_lastKeyEventValid
Definition: World.h:159
TGUI_Options m_gui_options
Definition: World.h:311
std::string msg_lines
Messages to show.
Definition: World.h:115
std::vector< nanogui::Label * > lbStatuses
Definition: World.h:345
std::function< void(WorldElementBase &)> world_element_visitor_t
Definition: World.h:223
std::atomic_bool m_gui_thread_must_close
Definition: World.h:145
std::thread m_gui_thread
Definition: World.h:142
BlockList m_blocks
Definition: World.h:326
std::vector< nanogui::Widget * > btns_selectedOps
Definition: World.h:356
std::mutex m_lastKeyEvent_mtx
Definition: World.h:160
void load_from_XML(const std::string &xml_text, const std::string &fileNameForPath=std::string("."))
std::atomic_bool m_gui_thread_running
Definition: World.h:144
int m_b2d_vel_iters
Definition: World.h:265
const WorldElementList & getListOfWorldElements() const
Definition: World.h:199
mrpt::system::CTimeLogger & getTimeLogger()
Definition: World.h:210
void runVisitorOnVehicles(const vehicle_visitor_t &v)
Definition: World.cpp:192
b2Body * getBox2DGroundBody()
Definition: World.h:194
GLfloat * params
VehicleList & getListOfVehicles()
Definition: World.h:196
double m_simul_timestep
Definition: World.h:262
TGUIKeyEvent m_lastKeyEvent
Definition: World.h:158
std::mutex m_gui_user_pending_tasks_mtx
Definition: World.h:156
BlockList & getListOfBlocks()
Definition: World.h:198
World()
Default ctor: inits an empty world.
Definition: World.cpp:27
void connectToServer()
Definition: World.cpp:205
void run_simulation(double dt)
Definition: World.cpp:71
std::list< WorldElementBase::Ptr > WorldElementList
Definition: World.h:176
std::string m_base_path
Definition: World.h:279
mvsim::Client & commsClient()
Definition: World.h:247
void process_load_walls(const rapidxml::xml_node< char > &node)
void update_GUI(TUpdateGUIParams *params=nullptr)
Definition: World_gui.cpp:633
double m_gravity
Definition: World.h:259
World & m_parent
Definition: World.h:369
double get_simul_time() const
Simulation wall-clock time.
Definition: World.h:68
void internal_process_pending_gui_user_tasks()
Definition: World_gui.cpp:536
std::mutex m_gui_thread_start_mtx
Definition: World.h:146
const TParameterDefinitions m_other_world_params
Definition: World.h:267
SimulableList & getListOfSimulableObjects()
Definition: World.h:204
const BlockList & getListOfBlocks() const
Definition: World.h:197
int keycode
0=no Key. Otherwise, ASCII code.
Definition: World.h:103
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f
GUI(World &parent)
Definition: World.h:341
void enqueue_task_to_run_in_gui_thread(const std::function< void(void)> &f)
Definition: World.h:148
void close_GUI()
a previous call to update_GUI()
Definition: World_gui.cpp:44


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51