00001 /*+-------------------------------------------------------------------------+ 00002 | MultiVehicle simulator (libmvsim) | 00003 | | 00004 | Copyright (C) 2014 Jose Luis Blanco Claraco (University of Almeria) | 00005 | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) | 00006 | Distributed under GNU General Public License version 3 | 00007 | See <http://www.gnu.org/licenses/> | 00008 +-------------------------------------------------------------------------+ */ 00009 00010 #pragma once 00011 00012 #include <mvsim/VehicleBase.h> 00013 #include <mvsim/WorldElements/WorldElementBase.h> 00014 #include <mvsim/Block.h> 00015 00016 #include <mrpt/gui/CDisplayWindow3D.h> 00017 00018 #include <mrpt/version.h> 00019 #if MRPT_VERSION<0x199 00020 #include <mrpt/utils/utils_defs.h> // mrpt::format() 00021 #include <mrpt/utils/CTimeLogger.h> 00022 #include <mrpt/utils/CTicTac.h> 00023 #include <mrpt/utils/TColor.h> 00024 using mrpt::utils::CTicTac; 00025 using mrpt::utils::CTimeLogger; 00026 using mrpt::utils::CTimeLoggerEntry; 00027 using mrpt::utils::TColor; 00028 using mrpt::utils::TColorf; 00029 using mrpt::utils::keep_max; 00030 using mrpt::utils::DEG2RAD; 00031 using mrpt::utils::RAD2DEG; 00032 #else 00033 #include <mrpt/core/format.h> 00034 #include <mrpt/core/bits_math.h> 00035 #include <mrpt/system/CTimeLogger.h> 00036 #include <mrpt/system/CTicTac.h> 00037 #include <mrpt/img/TColor.h> 00038 using mrpt::system::CTicTac; 00039 using mrpt::system::CTimeLogger; 00040 using mrpt::system::CTimeLoggerEntry; 00041 using mrpt::img::TColor; 00042 using mrpt::img::TColorf; 00043 using mrpt::keep_max; 00044 using mrpt::DEG2RAD; 00045 using mrpt::RAD2DEG; 00046 #endif 00047 00048 #include <mrpt/version.h> 00049 #if MRPT_VERSION >= 0x130 00050 #include <mrpt/obs/CObservation.h> 00051 #else 00052 #include <mrpt/slam/CObservation.h> 00053 #endif 00054 00055 #include <Box2D/Dynamics/b2World.h> 00056 #include <Box2D/Dynamics/b2Body.h> 00057 00058 #include <list> 00059 00060 namespace mvsim 00061 { 00067 class World 00068 { 00069 public: 00072 World(); 00073 ~World(); 00074 00075 void clear_all(bool acquire_mt_lock = true); 00076 00077 00078 00079 00080 00081 00082 00083 00091 void load_from_XML( 00092 const std::string& xml_text, 00093 const std::string& fileNameForPath = std::string(".")); 00099 double get_simul_time() const 00100 { 00101 return m_simul_time; 00102 } 00103 00104 double get_simul_timestep() const 00105 { 00106 return m_simul_timestep; 00107 } 00108 void set_simul_timestep(double timestep) 00109 { 00110 m_simul_timestep = timestep; 00111 } 00112 00113 double get_gravity() const 00114 { 00115 return m_gravity; 00116 } 00117 00118 void set_gravity(double accel) 00119 { 00120 m_gravity = accel; 00121 } 00122 00123 00129 void run_simulation(double dt); 00130 00132 struct TGUIKeyEvent 00133 { 00134 int keycode; 00135 mrpt::gui::mrptKeyModifier key_modifier; 00136 00137 TGUIKeyEvent() : keycode(0) {} 00138 }; 00139 00140 struct TUpdateGUIParams 00141 { 00142 TGUIKeyEvent keyevent; 00143 std::string msg_lines; 00144 00145 TUpdateGUIParams(); 00146 }; 00147 00155 void update_GUI(TUpdateGUIParams* params = NULL); 00156 00157 bool is_GUI_open() const; 00158 00159 00160 void close_GUI(); 00161 00166 typedef std::multimap<std::string, VehicleBase*> 00167 TListVehicles; 00168 00169 typedef std::list<WorldElementBase*> 00170 TListWorldElements; 00171 typedef std::multimap<std::string, Block*> 00172 TListBlocks; 00173 00174 00178 b2World* getBox2DWorld() { return m_box2d_world; } 00179 const b2World* getBox2DWorld() const { return m_box2d_world; } 00180 b2Body* getBox2DGroundBody() { return m_b2_ground_body; } 00181 const TListVehicles& getListOfVehicles() const { return m_vehicles; } 00182 TListVehicles& getListOfVehicles() { return m_vehicles; } 00183 const TListBlocks& getListOfBlocks() const { return m_blocks; } 00184 TListBlocks& getListOfBlocks() { return m_blocks; } 00185 const TListWorldElements& getListOfWorldElements() const 00186 { 00187 return m_world_elements; 00188 } 00189 00190 CTimeLogger& getTimeLogger() { return m_timlogger; } 00193 std::string resolvePath(const std::string& in_path) const; 00194 00200 struct VehicleVisitorBase 00201 { 00202 virtual void visit(VehicleBase* obj) = 0; 00203 }; 00205 struct WorldElementVisitorBase 00206 { 00207 virtual void visit(WorldElementBase* obj) = 0; 00208 }; 00209 00211 void runVisitorOnVehicles(VehicleVisitorBase& v); 00212 00214 void runVisitorOnWorldElements(WorldElementVisitorBase& v); 00215 00220 virtual void onNewObservation( 00221 const VehicleBase& veh, 00222 #if MRPT_VERSION >= 0x130 00223 const mrpt::obs::CObservation* obs 00224 #else 00225 const mrpt::slam::CObservation* obs 00226 #endif 00227 ) 00228 { 00229 /* default: do nothing */ 00230 } 00233 private: 00234 friend class VehicleBase; 00235 friend class Block; 00236 00237 // -------- World Params ---------- 00238 double m_gravity; 00239 00240 double m_simul_time; 00241 00242 00243 double m_simul_timestep; 00244 00245 int m_b2d_vel_iters, 00246 m_b2d_pos_iters; 00247 std::string m_base_path; 00248 00249 // ------- GUI options ----- 00250 struct TGUI_Options 00251 { 00252 unsigned int win_w, win_h; 00253 bool ortho; 00254 bool show_forces; 00255 double force_scale; 00256 double camera_distance; 00257 double fov_deg; 00258 std::string 00259 follow_vehicle; 00260 00261 TGUI_Options(); 00262 void parse_from(const rapidxml::xml_node<char>& node); 00263 }; 00264 00265 TGUI_Options m_gui_options; 00266 00267 00268 // -------- World contents ---------- 00269 std::mutex m_world_cs; 00270 00271 00272 b2World* m_box2d_world; 00273 b2Body* 00274 m_b2_ground_body; 00275 00276 TListVehicles m_vehicles; 00277 TListWorldElements m_world_elements; 00278 TListBlocks m_blocks; 00279 00281 void internal_one_timestep(double dt); 00282 00283 // -------- GUI stuff ---------- 00284 mrpt::gui::CDisplayWindow3D::Ptr m_gui_win; 00285 00286 CTimeLogger m_timlogger; 00287 CTicTac m_timer_iteration; 00288 }; 00289 }