Simulation Systems

Components that are more tailored to the ECS architecture adapt data for simulation systems that run during the simulation loop, separate of each entity. These systems are all expected to track time as well to ensure the simulation retains time synchronization while enabling flexibility to call systems whenever relevant.

TrimeshCollisionSystem

class rtd.sim.systems.collision.CollisionObject(meshes: Trimesh | list[Trimesh], parent: int = None)[source]

Bases: object

A class for storing the mesh of an object to handle its collision

inCollision(other: CollisionObject) tuple[bool, tuple[int, int]][source]

Checks if this object is in collision with another object. Returns a bool as well as its and its collided pair’s parent. Note that this should only be used for quick collision checks with a single other object

Parameters:

other (CollisionObject) – the object to check collision against

Returns:

  • collided (bool) – whether self is in collision with other

  • pair (tuple[int, int]) – pair of self.parent and other.parent

class rtd.sim.systems.collision.DynamicCollisionObject[source]

Bases: object

Base class for generating a CollisionObject of a dynamic object at a specified time

abstract getCollisionObject(**options) CollisionObject[source]

Abstract method which needs to be implemented to return a CollisionObject with mesh at a given time (and any other options)

Parameters:

**options – Usually arguments such as time

Returns:

resolved – resolved dynamic collision object

Return type:

CollisionObject

class rtd.sim.systems.collision.TrimeshCollisionSystem(static_objects: CollisionObject | list[rtd.sim.systems.collision.CollisionObject.CollisionObject] = None, dynamic_objects: DynamicCollisionObject | list[rtd.sim.systems.collision.DynamicCollisionObject.DynamicCollisionObject] = None, **options)[source]

Bases: SimulationSystem, Options

Takes in a list of CollisionObjects and DynamicCollisionObjects and handles their collision detection

addObjects(static: CollisionObject | list[rtd.sim.systems.collision.CollisionObject.CollisionObject] = None, dynamic: DynamicCollisionObject | list[rtd.sim.systems.collision.DynamicCollisionObject.DynamicCollisionObject] = None)[source]

Takes in a collision object or a list of collision objects and adds them to the corresponding list

Parameters:
checkCollisionAtTime(time: float) dict[source]

Check for every collision at a given time and return a bool if any collision happened, as well as a set of collided pair’s parents and the number of pairs

Parameters:

time (float) – time to check collision at

Returns:

  • info (dict) – with keys:

  • <time> (float) – input time

  • <collided> (bool) – whether a collision occured

  • <n_pairs> (int) – number of collided pairs

  • <pairs> (set[tuple[int, int]]) – set of collided object pairs

checkCollisionObject(collision_obj: CollisionObject) dict[source]

Check for every collision against collision_obj (does not check for internal collision) at the most recent time

Parameters:

collision_obj (CollisionObject) – object to check collision against

Returns:

  • info (dict) – with keys:

  • <time> (float) – input time

  • <collided> (bool) – whether a collision occured

  • <n_pairs> (int) – number of collided pairs

  • <pairs> (set[tuple[int, int]]) – set of collided object pairs

static defaultoptions() dict[source]
Returns:

options – default options of collision system

Return type:

dict

remove(*objects: CollisionObject | DynamicCollisionObject)[source]

Takes in a collision object or a list of collision objects and removes them from static and dynamic objects list. Assumes the object is already in either lists

Parameters:

*objects (CollisionObject | DynamicCollisionObject) – objects to remove from the system

reset(**options)[source]

Abstract method which needs to be implemented to reset the system

updateCollision(t_update: float) tuple[bool, set[tuple[int, int]]][source]

Appends t_update to time and checks for any collision for t_update time

Parameters:

t_update (float) – duration to update for

Returns:

  • collided (bool) – whether a collision occured

  • pairs (set[tuple[int, int]]) – set of collided objects pairs

PyvistaVisualSystem

class rtd.sim.systems.visual.ClientVisualObject[source]

Bases: object

An object that can be extended to control the rendering of an entity

abstract create_plot_data(**options) MeshData | list[rtd.sim.websocket.meshdata.MeshData][source]

Abstract method which needs to be implemented to initialize self.plot_data to a Trimesh mesh (or a list of meshes), and return the corresponding MeshData(s)

Parameters:

**options – options for creating the plot data

Returns:

plot_obj – Websocket MeshData(s) to plot

Return type:

MeshData | list[MeshData]

isPlotDataValid() bool[source]

Checks if plot_data is a Trimesh mesh

Returns:

is_valid – whether the plot_data is a valid Trimesh mesh

Return type:

bool

abstract plot(**options) tuple[str, dict, dict] | list[tuple[str, dict, dict]][source]

Abstract method which needs to be implemented to return the transformation and rotation matrices for the plot_data animation

Returns:

move_msg – a tuple of mesh guid, transformation matrix, and rotation matrix for every object in plot_data

Return type:

MoveMsg | list[MoveMsg]

class rtd.sim.systems.visual.ClientVisualSystem(static_objects: ClientVisualObject | list[rtd.sim.systems.visual.ClientVisualObject.ClientVisualObject] = None, dynamic_objects: ClientVisualObject | list[rtd.sim.systems.visual.ClientVisualObject.ClientVisualObject] = None, **options)[source]

Bases: ClientSimulationSystem, Options

Takes in a list of static and dynamic objects and handles their rendering

addObjects(static: ClientVisualObject | list[rtd.sim.systems.visual.ClientVisualObject.ClientVisualObject] = None, dynamic: ClientVisualObject | list[rtd.sim.systems.visual.ClientVisualObject.ClientVisualObject] = None)[source]

Takes in a visual object or a list of visual objects and adds them to the corresponding list

Parameters:
animate(t_span: list[float, float] = None, time_discretization: float = None, pause_time: float = None)[source]

Animates from time = t_span[0] to t_span[1], dividing it into frames of length time_discretization and waiting pause_time seconds before rendering the next frame. A lower discretization will result in a smoother animation, while a pause_time lower than the discretization will result in a faster animation speed. Defaults to 1 second per t

Parameters:
  • t_span (Bound) – range of time to animate

  • time_discretization (float) – time difference between frames

  • pause_time (float) – pause time in seconds between frames

static defaultoptions() dict[source]
Returns:

options – default options of visual system

Return type:

dict

static get_discretization_and_pause(framerate: float = 30, speed: float = 1) tuple[float, float][source]

Returns the time_discretization and pause_time to get the desired framerate and speed. Speed of 2 means 1 second = 2 time

Parameters:
  • framerate (float) – desired framerate

  • speed (float) – desired animation speed ratio

Returns:

  • time_discretization (float) – time difference between frames

  • pause_time (float) – pause time in seconds between frames

remove(*objects: ClientVisualObject)[source]

Takes in a visual object or a list of visual objects and removes them from static and dynamic objects list

Parameters:

*objects (ClientVisualObject) – objects to remove from the system

reset(**options)[source]

Connects to the server and resets the system

updateVisual(t_update: float)[source]

Appends t_update to time and updates the dynamic objects on the current figure for t_update time

Parameters:

t_update (float) – duration to update for

class rtd.sim.systems.visual.PyvistaVisualObject[source]

Bases: object

An object that can be extended to control the rendering of an entity

abstract create_plot_data(**options) Actor | list[Actor][source]

Abstract method which needs to be implemented to generate and return the plot data

Parameters:

**options – options for creating the plot data

Returns:

plot_obj – Pyvista actor(s) to plot

Return type:

Actor | list[Actor]

isPlotDataValid() bool[source]

Checks if plot_data is a pyvista actor object

Returns:

is_valid – whether the plot_data is a valid Pyvista actor

Return type:

bool

abstract plot(**options)[source]

Abstract method which needs to be implemented to update the generated plot data

class rtd.sim.systems.visual.PyvistaVisualSystem(static_objects: PyvistaVisualObject | list[rtd.sim.systems.visual.PyvistaVisualObject.PyvistaVisualObject] = None, dynamic_objects: PyvistaVisualObject | list[rtd.sim.systems.visual.PyvistaVisualObject.PyvistaVisualObject] = None, **options)[source]

Bases: SimulationSystem, Options

Takes in a list of static and dynamic objects and handles their rendering

addObjects(static: PyvistaVisualObject | list[rtd.sim.systems.visual.PyvistaVisualObject.PyvistaVisualObject] = None, dynamic: PyvistaVisualObject | list[rtd.sim.systems.visual.PyvistaVisualObject.PyvistaVisualObject] = None)[source]

Takes in a visual object or a list of visual objects and adds them to the corresponding list

Parameters:
animate(t_span: list[float, float] = None, time_discretization: float = None, pause_time: float = None, axlim: list = None)[source]

Animates from time = t_span[0] to t_span[1], dividing it into frames of length time_discretization and waiting pause_time seconds before rendering the next frame. A lower discretization will result in a smoother animation, while a pause_time lower than the discretization will result in a faster animation speed. Defaults to 1 second per t

Parameters:
  • t_span (Bound) – range of time to animate

  • time_discretization (float) – time difference between frames

  • pause_time (float) – pause time in seconds between frames

  • axlim (list) – list of 4 numbers for the x lim and y lim

static defaultoptions() dict[source]
Returns:

options – default options of visual system

Return type:

dict

static get_discretization_and_pause(framerate: float = 30, speed: float = 1) tuple[float, float][source]

Returns the time_discretization and pause_time to get the desired framerate and speed. Speed of 2 means 1 second = 2 time

Parameters:
  • framerate (float) – desired framerate

  • speed (float) – desired animation speed ratio

Returns:

  • time_discretization (float) – time difference between frames

  • pause_time (float) – pause time in seconds between frames

redraw(time: float = None, axlim: list = None)[source]

Recreates the plotter if necessary and adds the actors to the current plotter at the input time. Defaults to the most recent time

Parameters:
  • time (float) – time to redraw from

  • axlim (list) – list of 4 numbers for the x lim and y lim

remove(*objects: PyvistaVisualObject)[source]

Takes in a visual object or a list of visual objects and removes them from static and dynamic objects list

Parameters:

*objects (PyvistaVisualObject) – objects to remove from the system

reset(**options)[source]

Abstract method which needs to be implemented to reset the system

updateVisual(t_update: float)[source]

Appends t_update to time and updates the dynamic objects on the current figure for t_update time

Parameters:

t_update (float) – duration to update for

validateOrCreateFigure()[source]

If plotter does not exists or is no longer valid, create a new figure and save its reference to self._plotter

waituntilclose()[source]

Waits until plotter is closed before proceeding