MVSim
Lightweight simulator for 2.5D vehicles and robots
Classes | Public Member Functions | Friends | List of all members
mvsim::World Class Reference

#include <World.h>

Inheritance diagram for mvsim::World:
Inheritance graph
[legend]
Collaboration diagram for mvsim::World:
Collaboration graph
[legend]

Classes

struct  GeoreferenceOptions
 
struct  GUIKeyEvent
 
struct  TUpdateGUIParams
 

Public Types

Public types
using VehicleList = std::multimap< std::string, VehicleBase::Ptr >
 
using WorldElementList = std::list< WorldElementBase::Ptr >
 
using BlockList = std::multimap< std::string, Block::Ptr >
 
using SimulableList = std::multimap< std::string, Simulable::Ptr >
 
using ActorList = std::multimap< std::string, HumanActor::Ptr >
 

Public Member Functions

void connectToServer ()
 
mvsim::ClientcommsClient ()
 
const mvsim::ClientcommsClient () const
 
void free_opengl_resources ()
 
auto & physical_objects_mtx ()
 
bool headless () const
 
void headless (bool setHeadless)
 
bool sensor_has_to_create_egl_context ()
 
const std::map< std::string, std::string > & user_defined_variables () const
 
std::optional< mvsim::TJoyStickEventgetJoystickState () const
 
bool evaluate_tag_if (const rapidxml::xml_node< char > &node) const
 
float collisionThreshold () const
 
std::set< float > getElevationsAt (const mrpt::math::TPoint2D &worldXY) const
 
float getHighestElevationUnder (const mrpt::math::TPoint3Df &queryPt) const
 
void internal_simul_pre_step_terrain_elevation ()
 
std::optional< std::any > getPropertyAt (const std::string &propertyName, const mrpt::math::TPoint3D &worldXYZ) const
 
const GeoreferenceOptionsgeoreferenceOptions () const
 
mrpt::math::TVector3D worldRenderOffset () const
 (See docs for worldRenderOffset_)
 
mrpt::math::TPose3D applyWorldRenderOffset (mrpt::math::TPose3D p) const
 
mrpt::poses::CPose3D applyWorldRenderOffset (mrpt::poses::CPose3D p) const
 
void worldRenderOffsetPropose (const mrpt::math::TVector3D &v)
 (See docs for worldRenderOffset_)
 
Initialization, simulation set-up
 World ()
 Default ctor: inits an empty world.
 
 ~World ()
 Dtor.
 
 World (const World &)=delete
 
Worldoperator= (const World &)=delete
 
 World (World &&)=delete
 
Worldoperator= (World &&)=delete
 
void clear_all ()
 
void load_from_XML_file (const std::string &xmlFileNamePath)
 
void internal_initialize ()
 
void load_from_XML (const std::string &xml_text, const std::string &fileNameForPath=std::string("."))
 
Access inner working objects
std::unique_ptr< b2World > & getBox2DWorld ()
 
const std::unique_ptr< b2World > & getBox2DWorld () const
 
b2Body * getBox2DGroundBody ()
 
const VehicleListgetListOfVehicles () const
 
VehicleListgetListOfVehicles ()
 
const BlockListgetListOfBlocks () const
 
BlockListgetListOfBlocks ()
 
const WorldElementListgetListOfWorldElements () const
 
const std::vector< WorldJoint > & getListOfJoints () const
 
const ActorListgetListOfActors () const
 
ActorListgetListOfActors ()
 
SimulableListgetListOfSimulableObjects ()
 Always lock/unlock getListOfSimulableObjectsMtx() before using this:
 
const SimulableListgetListOfSimulableObjects () const
 
auto & getListOfSimulableObjectsMtx ()
 
mrpt::system::CTimeLogger & getTimeLogger ()
 
std::string local_to_abs_path (const std::string &in_path) const
 
std::string xmlPathToActualPath (const std::string &modelURI) const
 

Friends

class VehicleBase
 
class Block
 

Visitors API

using vehicle_visitor_t = std::function< void(VehicleBase &)>
 
using world_element_visitor_t = std::function< void(WorldElementBase &)>
 
using block_visitor_t = std::function< void(Block &)>
 
void runVisitorOnVehicles (const vehicle_visitor_t &v)
 
void runVisitorOnWorldElements (const world_element_visitor_t &v)
 
void runVisitorOnBlocks (const block_visitor_t &v)
 

Optional user hooks

using on_observation_callback_t = std::function< void(const Simulable &, const mrpt::obs::CObservation::Ptr &)>
 
void registerCallbackOnObservation (const on_observation_callback_t &f)
 
void dispatchOnObservation (const Simulable &veh, const mrpt::obs::CObservation::Ptr &obs)
 

Simulation execution

mrpt::opengl::CSetOfObjects::Ptr guiUserObjectsPhysical_
 
mrpt::opengl::CSetOfObjects::Ptr guiUserObjectsViz_
 
std::mutex guiUserObjectsMtx_
 
std::mutex pendingRunSensorsOn3DSceneMtx_
 
bool pendingRunSensorsOn3DScene_ = false
 
std::string guiMsgLines_
 
std::mutex guiMsgLinesMtx_
 
std::thread gui_thread_
 
std::atomic_bool gui_thread_running_ = false
 
std::atomic_bool simulator_must_close_ = false
 
std::mutex gui_thread_start_mtx_
 
std::vector< std::function< void(void)> > guiUserPendingTasks_
 
std::mutex guiUserPendingTasksMtx_
 
GUIKeyEvent lastKeyEvent_
 
std::atomic_bool lastKeyEventValid_ = false
 
std::mutex lastKeyEventMtx_
 
double get_simul_time () const
 
void force_set_simul_time (double newSimulatedTime)
 Normally should not be called by users, for internal use only.
 
mrpt::Clock::time_point get_simul_timestamp () const
 
double get_simul_timestep () const
 Simulation fixed-time interval for numerical integration.
 
void set_simul_timestep (double timestep)
 
double get_gravity () const
 
void set_gravity (double accel)
 
void run_simulation (double dt)
 
void insert_vehicle (const VehicleBase::Ptr &veh)
 
void update_GUI (TUpdateGUIParams *params=nullptr)
 
const mrpt::gui::CDisplayWindowGUI::Ptr & gui_window () const
 
const mrpt::math::TPoint3D & gui_mouse_point () const
 
void internalGraphicsLoopTasksForSimulation ()
 
void internalRunSensorsOn3DScene (mrpt::opengl::COpenGLScene &physicalObjects)
 
void internalUpdate3DSceneObjects (mrpt::opengl::COpenGLScene &viz, mrpt::opengl::COpenGLScene &physical)
 
void internal_GUI_thread ()
 
void internal_process_pending_gui_user_tasks ()
 
void mark_as_pending_running_sensors_on_3D_scene ()
 
void clear_pending_running_sensors_on_3D_scene ()
 
bool pending_running_sensors_on_3D_scene ()
 
bool simulator_must_close () const
 
void simulator_must_close (bool value)
 
void enqueue_task_to_run_in_gui_thread (const std::function< void(void)> &f)
 
bool is_GUI_open () const
 
void close_GUI ()
 Forces closing the GUI window, if any.
 

Detailed Description

Simulation happens inside a World object. This is the central class for usage from user code, running the simulation, loading XML models, managing GUI visualization, etc. The ROS node acts as a bridge between this class and the ROS subsystem.

See: https://mvsimulator.readthedocs.io/en/latest/world.html

Member Typedef Documentation

◆ ActorList

using mvsim::World::ActorList = std::multimap<std::string, HumanActor::Ptr>

Map 'actor-name' => actor object. See getListOfActors()

◆ BlockList

using mvsim::World::BlockList = std::multimap<std::string, Block::Ptr>

Map 'block-name' => block object. See getListOfBlocks()

◆ SimulableList

using mvsim::World::SimulableList = std::multimap<std::string, Simulable::Ptr>

For convenience, all elements (vehicles, world elements, blocks) are also stored here for each look-up by name

◆ VehicleList

using mvsim::World::VehicleList = std::multimap<std::string, VehicleBase::Ptr>

Map 'vehicle-name' => vehicle object. See getListOfVehicles()

◆ WorldElementList

using mvsim::World::WorldElementList = std::list<WorldElementBase::Ptr>

See getListOfWorldElements()

Member Function Documentation

◆ clear_all()

void mvsim::World::clear_all ( )

Resets the entire simulation environment to an empty world.

◆ connectToServer()

void mvsim::World::connectToServer ( )

Connect to server, advertise topics and services, etc. per the world description loaded from XML file.

◆ dispatchOnObservation()

void mvsim::World::dispatchOnObservation ( const Simulable veh,
const mrpt::obs::CObservation::Ptr &  obs 
)

Calls all registered callbacks:

◆ get_gravity()

double mvsim::World::get_gravity ( ) const
inline

Gravity acceleration (Default=9.8 m/s^2). Used to evaluate weights for friction, etc.

◆ get_simul_time()

double mvsim::World::get_simul_time ( ) const
inline

Seconds since start of simulation.

See also
get_simul_timestamp()

◆ get_simul_timestamp()

mrpt::Clock::time_point mvsim::World::get_simul_timestamp ( ) const
inline

Get the current simulation full timestamp, computed as the real wall clock timestamp at the beginning of the simulation, plus the number of seconds simulation has run.

See also
get_simul_time()

◆ getElevationsAt()

std::set<float> mvsim::World::getElevationsAt ( const mrpt::math::TPoint2D &  worldXY) const

Returns the list of "z" coordinate or "elevations" for all simulable objects at a given world-frame 2D coordinates (x,y). If no object reports any height, the value "0.0" will be always reported by default. In multistorey worlds, for example, this will return the height of each floor for the queried point.

◆ getHighestElevationUnder()

float mvsim::World::getHighestElevationUnder ( const mrpt::math::TPoint3Df &  queryPt) const

with query points the center of a wheel, this returns the highest "ground" under it, or .0 if nothing found.

◆ getJoystickState()

std::optional<mvsim::TJoyStickEvent> mvsim::World::getJoystickState ( ) const

If joystick usage is enabled (via XML file option, for example), this will read the joystick state and return it. Otherwise (or on device error or disconnection), a null optional variable is returned.

◆ getPropertyAt()

std::optional<std::any> mvsim::World::getPropertyAt ( const std::string &  propertyName,
const mrpt::math::TPoint3D &  worldXYZ 
) const

Query all mvsim::WorldElementBase objects for a given custom property at the specific 3D location. It returns nullopt if no object defines this property.

See also
WorldElementBase::queryProperty()

◆ internalGraphicsLoopTasksForSimulation()

void mvsim::World::internalGraphicsLoopTasksForSimulation ( )

Update 3D vehicles, sensors, run render-based sensors, etc: Called from World_gui thread in normal mode, or mvsim-cli in headless mode.

◆ is_GUI_open()

bool mvsim::World::is_GUI_open ( ) const

Return true if the GUI window is open, after a previous call to update_GUI()

◆ load_from_XML()

void mvsim::World::load_from_XML ( const std::string &  xml_text,
const std::string &  fileNameForPath = std::string(".") 
)

Load an entire world description into this object from a specification in XML format.

Parameters
[in]fileNameForPathOptionally, provide the full path to an XML file from which to take relative paths.
Exceptions
std::exceptionOn any error, with what() giving a descriptive error message

◆ load_from_XML_file()

void mvsim::World::load_from_XML_file ( const std::string &  xmlFileNamePath)

Load an entire world description into this object from a specification in XML format.

Parameters
[in]xmlFileNamePathThe relative or full path to the XML file.
Exceptions
std::exceptionOn any error, with what() giving a descriptive error message

◆ local_to_abs_path()

std::string mvsim::World::local_to_abs_path ( const std::string &  in_path) const

Replace macros, prefix the base_path if input filename is relative, etc.

See also
xmlPathToActualPath

◆ run_simulation()

void mvsim::World::run_simulation ( double  dt)

Runs the simulation for a given time interval (in seconds)

Note
The minimum simulation time is the timestep set (e.g. via set_simul_timestep()), even if time advanced further than the provided "dt".

◆ runVisitorOnBlocks()

void mvsim::World::runVisitorOnBlocks ( const block_visitor_t &  v)

Run the user-provided visitor on each world block

◆ runVisitorOnVehicles()

void mvsim::World::runVisitorOnVehicles ( const vehicle_visitor_t &  v)

Run the user-provided visitor on each vehicle

◆ runVisitorOnWorldElements()

void mvsim::World::runVisitorOnWorldElements ( const world_element_visitor_t &  v)

Run the user-provided visitor on each world element

◆ set_gravity()

void mvsim::World::set_gravity ( double  accel)
inline

Gravity acceleration (Default=9.8 m/s^2). Used to evaluate weights for friction, etc.

◆ set_simul_timestep()

void mvsim::World::set_simul_timestep ( double  timestep)
inline

Simulation fixed-time interval for numerical integration 0 means auto-determine as the minimum of 50 ms and the shortest sensor sample period.

◆ update_GUI()

void mvsim::World::update_GUI ( TUpdateGUIParams params = nullptr)

Updates (or sets-up upon first call) the GUI visualization of the scene.

Parameters
[in,out]paramsOptional inputs/outputs to the GUI update process. See struct for details.
Note
This method is prepared to be called concurrently with the simulation, and doing so is recommended to assure a smooth multi-threading simulation.

◆ xmlPathToActualPath()

std::string mvsim::World::xmlPathToActualPath ( const std::string &  modelURI) const

Parses URIs in all the forms explained in RemoteResourcesManager::resolve_path(), then passes it through local_to_abs_path().

See also
local_to_abs_path

Member Data Documentation

◆ guiUserObjectsPhysical_

mrpt::opengl::CSetOfObjects::Ptr mvsim::World::guiUserObjectsPhysical_

If !=null, a set of objects to be rendered merged with the default visualization. Lock the mutex gui_user_objects_mtx_ while writing. There are two sets of objects: "viz" for visualization only, "physical" for objects which should be detected by sensors.


The documentation for this class was generated from the following file: