AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
agxUtil::StepEventCallback Class Reference

Helper to use lambdas as preCollide, pre, post and last step events. More...

#include <StepEventCallback.h>

+ Inheritance diagram for agxUtil::StepEventCallback:

Classes

struct  DefaultIsPreCollide
 Used as argument in timed callbacks (callAt, callFor et al.) where it might not matter as much when the callback is performed. More...
 

Static Public Member Functions

template<typename FuncT , typename... Args>
static void callAt (agx::Real timeStamp, DefaultIsPreCollide callbackType, FuncT &&callback, agxSDK::Simulation *simulation, Args &&... args)
 Register callback to be called at a given simulation time.
 
template<typename FuncT , typename... Args>
static void callFor (agx::Real seconds, DefaultIsPreCollide callbackType, FuncT &&callback, agxSDK::Simulation *simulation, Args &&... args)
 Executes given callback in seconds seconds from the current time stamp of the given simulation.
 
template<typename FuncT , typename... Args>
static void last (FuncT &&callback, agxSDK::Simulation *simulation, Args &&... args)
 Register a last step event callback.
 
template<typename FuncT , typename... Args>
static void post (FuncT &&callback, agxSDK::Simulation *simulation, Args &&... args)
 Register a post step event callback.
 
template<typename FuncT , typename... Args>
static void pre (FuncT &&callback, agxSDK::Simulation *simulation, Args &&... args)
 Register a pre step event callback.
 
template<typename FuncT , typename... Args>
static void preCollide (FuncT &&callback, agxSDK::Simulation *simulation, Args &&... args)
 Register a pre-collide step event callback.
 
static void removeMe (agxSDK::Simulation *simulation)
 Call this from within a callback to be removed from receiving any more calls.
 
static void uninitialize (agxSDK::Simulation *simulation)
 Removes internal step event listener from the simulation (if created).
 
- Static Public Member Functions inherited from agxSDK::StepEventListener
static agxStream::Serializablecreate (agxStream::InputArchive &)
 
- Static Public Member Functions inherited from agxSDK::EventListener
static agxStream::Serializablecreate (agxStream::InputArchive &)
 
- Static Public Member Functions inherited from agx::Component
static Component_load (TiXmlElement *eComponent, Device *device)
 
static agx::ModelClassModel ()
 
static Componentload (const Path &path, const Name &implementation, Device *device, const Path &_namespace=Path())
 
static Componentload (const String &path, const Path &_namespace=Path())
 
static Componentload (const String &path, Device *device, const Path &_namespace=Path())
 
static Componentload (TiXmlElement *eComponent, Device *device)
 
- Static Public Member Functions inherited from agx::Object
static agx::ModelClassModel ()
 
static agx::String generateName (const agx::String &bindPath)
 
static const HashSet< Object * > & getActiveObjects ()
 
static bool getEnableRebind ()
 Should rebinding be allowed? Right now a global setting.
 
static Objectload (const String &path, Device *device, const String &type, const String &attribute, const String &value)
 
template<typename T >
static T * load (const String &path, Device *device, const String &type, const String &attribute, const String &value)
 
static Objectload (const String &path, Device *device, const String &type="", size_t instance=0)
 
template<typename T >
static T * load (const String &path, Device *device, const String &type="", size_t instance=0)
 
static void load (const String &path, Device *device, ObjectPtrVector &loadedObjects)
 
static Objectload (TiXmlElement *eObject, Device *device)
 
template<typename T >
static T * load (TiXmlElement *eObject, Device *device)
 
static TiXmlDocument openDocument (const String &path)
 
static void setEnableRebind (bool flag)
 Sets if rebinding of objects should be allowed (Right now a global setting).
 
- Static Public Member Functions inherited from agx::Referenced
template<typename T >
static bool ValidateCast (const Referenced *object)
 
- Static Public Member Functions inherited from agxStream::Serializable
static void setEnableUuidGeneration (bool flag)
 Specify if there should be UUID:s generated for each new Serializable object. By default it is enabled.
 

Protected Member Functions

virtual ~StepEventCallback ()
 Reference counted object, protected destructor.
 
virtual void addNotification () override
 Add notification callback.
 
virtual void last (const agx::TimeStamp &t) override
 Called after a step is taken in the simulation Implement this method in the derived class to get callbacks.
 
virtual void post (const agx::TimeStamp &t) override
 Called after a step is taken in the simulation Implement this method in the derived class to get callbacks.
 
virtual void pre (const agx::TimeStamp &t) override
 Called before a step is taken in the simulation Implement this method in the derived class to get callbacks.
 
virtual void preCollide (const agx::TimeStamp &t) override
 Called before collision detection is performed in the simulation Implement this method in the derived class to get callbacks.
 
virtual void removeNotification () override
 Remove notification callback - will unregister this from the static instances.
 
- Protected Member Functions inherited from agxSDK::StepEventListener
virtual ~StepEventListener ()
 Destructor.
 
- Protected Member Functions inherited from agxSDK::EventListener
 EventListener (Type type=UNDEFINED_TYPE)
 Default constructor.
 
virtual ~EventListener ()
 Destructor.
 
void setSimulation (Simulation *simulation)
 Set the associated simulation for this EventListener.
 
- Protected Member Functions inherited from agx::Component
virtual ~Component ()
 
String expandAutoCompletionMatch (const String &query, const StringVector &matchingNames) const
 
void setDevice (Device *device)
 
- Protected Member Functions inherited from agx::Object
virtual ~Object ()
 
- Protected Member Functions inherited from agx::Referenced
virtual ~Referenced ()
 Destructor.
 
void allocateObserverVector () const
 
void deleteUsingDeleteHandler () const
 
- Protected Member Functions inherited from agxStream::Serializable
 Serializable ()
 Default constructor.
 
 Serializable (const Serializable &other)
 Copy constructor.
 
void generateUuid ()
 

Additional Inherited Members

- Public Types inherited from agxSDK::StepEventListener
enum  ActivationMask {
  STEP_NONE = 0x0 , PRE_COLLIDE = 0x1 , PRE_STEP = 0x2 , POST_STEP = 0x4 ,
  LAST_STEP = 0x8 , DEFAULT = PRE_STEP | POST_STEP , ALL = PRE_COLLIDE | PRE_STEP | POST_STEP | LAST_STEP
}
 Defines the event states for which a listener will be activated, a mask can be bitwise OR:ed from these members. More...
 
- Public Types inherited from agxSDK::EventListener
enum  Type { UNDEFINED_TYPE , STEP_EVENT_LISTENER , CONTACT_EVENT_LISTENER , GUI_EVENT_LISTENER }
 Type of EventListener. More...
 
- Public Types inherited from agx::Component
typedef Event2< Component *, Object * > ObjectEvent
 Event when adding removing child objects.
 
typedef Callback1< Object * > TraverseCallback
 
- Public Types inherited from agx::Object
typedef agx::observer_ptr< const EventEventConstObserver
 
typedef agx::ref_ptr< const EventEventConstRef
 
typedef agx::observer_ptr< EventEventObserver
 
typedef agx::Vector< EventObserverEventObserverVector
 
typedef agx::VectorPOD< Event * > EventPtrVector
 
typedef agx::ref_ptr< EventEventRef
 
typedef agx::Vector< EventRefEventRefVector
 
- Public Member Functions inherited from agxSDK::StepEventListener
 StepEventListener (int mask=DEFAULT)
 Default constructor, sets the default activation mask to all (POST_STEP and PRE_STEP) events.
 
 AGXSTREAM_DECLARE_SERIALIZABLE_BASE (agxSDK::StepEventListener)
 
virtual void last (const agx::TimeStamp &time)
 Called after a step is taken in the simulation Implement this method in the derived class to get callbacks.
 
virtual void post (const agx::TimeStamp &time)
 Called after a step is taken in the simulation Implement this method in the derived class to get callbacks.
 
virtual void pre (const agx::TimeStamp &time)
 Called before a step is taken in the simulation Implement this method in the derived class to get callbacks.
 
virtual void preCollide (const agx::TimeStamp &time)
 Called before collision detection is performed in the simulation Implement this method in the derived class to get callbacks.
 
virtual void setMask (int mask) override
 Specifies a bitmask which determines which event types will activate this listener.
 
- Public Member Functions inherited from agxSDK::EventListener
virtual void addNotification ()
 Called when this listener is added to the simulation.
 
 AGXSTREAM_DECLARE_SERIALIZABLE_BASE (agxSDK::EventListener)
 
int getMask () const
 
agx::UInt32 getPriority () const
 
SimulationgetSimulation ()
 
const SimulationgetSimulation () const
 
Type getType () const
 
bool isEnabled () const
 
virtual void removeNotification ()
 Called when this listener is removed from the simulation.
 
void setEnable (bool enable)
 Specify whether the listener should react at all at events.
 
virtual void setMask (int f)
 Specifies a bit mask which determines which event types that will activate this listener.
 
- Public Member Functions inherited from agx::Component
 Component (const agx::Name &name=agx::Name(), agx::Model *model=agx::Component::ClassModel(), agx::Device *device=nullptr)
 
virtual void addObject (agx::Object *object, bool assignContext=true)
 Add an object to the component.
 
virtual String autoComplete (const String &partialName, StringVector &matchingNames) const
 
virtual void buildNavigationTree (agxJson::Value &eNode) const override
 
void configure (Model *model)
 
virtual void configure (TiXmlElement *eComponent) override
 
agx::DevicegetDevice ()
 
template<typename T >
T * getDevice ()
 
const agx::DevicegetDevice () const
 
template<typename T >
const T * getDevice () const
 
size_t getNumObjects (const agx::Name &name) const
 
agx::ObjectgetObject (const agx::Name &name, agx::Model *model, size_t index=0)
 
const agx::ObjectgetObject (const agx::Name &name, agx::Model *model, size_t index=0) const
 
agx::ObjectgetObject (const agx::Name &name, size_t index=0)
 
template<typename T >
T * getObject (const agx::Name &name, size_t index=0)
 
const agx::ObjectgetObject (const agx::Name &name, size_t index=0) const
 
template<typename T >
const T * getObject (const agx::Name &name, size_t index=0) const
 
const agx::ObjectRefVectorgetObjects () const
 
template<typename T >
void getObjects (agx::ObjectPtrVector &result, bool recursive=false) const
 Returns All child objects of a specified type in the result container.
 
void getObjects (agx::ObjectPtrVector &result, const agx::Model *model, bool recursive=false) const
 
virtual ObjectgetResourceImpl (const Path &path, agx::Model *model) override
 
virtual void printSubtree () const override
 
void printSubtree (std::ostream &stream, int depth=0) const
 
virtual void rebind () override
 
void removeAllObjects ()
 Remove all components.
 
virtual void removeObject (agx::Object *object)
 Remove an object from the component.
 
void removeObject (const agx::Name &name, size_t index=0)
 Remove an object with a specified name (and optional index) from the component.
 
virtual void snapshot (TiXmlNode *eParent, const String &directory) const override
 
void traverse (ComponentVisitor *)
 
void traverse (const TraverseCallback &callback)
 
- Public Member Functions inherited from agx::Object
 Object (const Name &name=Name(), Model *model=Object::ClassModel())
 
void addListener (EventListener *listener)
 
virtual void buildNavigationTree (agxJson::Value &eNode) const
 
virtual void configure (TiXmlElement *)
 
agx::String fullName () const
 
agx::String fullPath () const
 
agx::String fullPath (const agx::Object *root) const
 
template<typename T >
T * getAutoScopedResource (const agx::Path &path)
 
template<typename T >
const T * getAutoScopedResource (const agx::Path &path) const
 
agx::ObjectgetAutoScopedResource (const agx::Path &path, agx::Model *model=nullptr)
 
const agx::ObjectgetAutoScopedResource (const agx::Path &path, agx::Model *model=nullptr) const
 
agx::ObjectgetContext ()
 
template<typename T >
T * getContext ()
 
const agx::ObjectgetContext () const
 
template<typename T >
const T * getContext () const
 
agx::UInt32 getId () const
 
const agx::NamegetImplementationName () const
 
agx::ModelgetModel ()
 
const agx::ModelgetModel () const
 
const agx::NamegetName () const
 
agx::ObjectgetNextSibling ()
 
const agx::ObjectgetNextSibling () const
 
agx::Path getPath () const
 
agx::Path getPath (const agx::Object *root) const
 
template<typename T >
T * getResource (const agx::Path &path)
 
template<typename T >
const T * getResource (const agx::Path &path) const
 
agx::ObjectgetResource (const agx::Path &path, agx::Model *model=nullptr)
 
const agx::ObjectgetResource (const agx::Path &path, agx::Model *model=nullptr) const
 
virtual agx::ObjectgetResourceImpl (const agx::Path &path, agx::Model *model)
 
agx::ObjectgetRootContext ()
 
const agx::ObjectgetRootContext () const
 
bool hasListener (EventListener *listener)
 
bool hasParent (const agx::Object *node) const
 
bool isUnique () const
 
virtual void printSubtree () const
 
virtual void rebind ()
 
void removeListener (EventListener *listener)
 
virtual void save (TiXmlElement *) const
 
virtual void setContext (agx::Object *context)
 
void setId (UInt32 id)
 
void setModel (agx::Model *model)
 Set the object model (handled automatically).
 
void setName (const agx::Name &name)
 Set the name of the object.
 
virtual void snapshot (TiXmlNode *, const String &) const
 
- Public Member Functions inherited from agx::Referenced
 Referenced ()
 Default constructor.
 
 Referenced (const Referenced &)
 
template<typename T >
T * as ()
 Subclass casting.
 
template<typename T >
const T * as () const
 
template<typename T >
T * asSafe ()
 Safe subclass casting, return nullptr if template type does not match.
 
template<typename T >
const T * asSafe () const
 
int getReferenceCount () const
 
template<typename T >
bool is () const
 Subclass test.
 
Referencedoperator= (const Referenced &)
 Assignment operator. Will increment the number of references to the referenced object.
 
void reference (void *ptr=nullptr) const
 Explicitly increment the reference count by one, indicating that this object has another pointer which is referencing it.
 
void unreference (void *ptr=nullptr) const
 Decrement the reference count by one, indicating that a pointer to this object is referencing it.
 
void unreference_nodelete () const
 Decrement the reference count by one, indicating that a pointer to this object is referencing it.
 
- Public Member Functions inherited from agxStream::Serializable
virtual ~Serializable ()
 Destructor for normal C++ use but hidden from SWIG bindings.
 
virtual const char * getClassName () const
 
bool getEnableSerialization () const
 
bool getEnableUuidGeneration ()
 
agx::UInt32 getIndex () const
 This index is given at creation of this object.
 
virtual StorageAgent * getStorageAgent () const =0
 
agx::Uuid getUuid () const
 
bool isFinished () const
 
void setEnableSerialization (bool flag)
 Set to false to disable serialization of this object.
 
void setFinished ()
 Tells this class that it is restored correctly and should not be deleted during destruction of an Archive.
 
void setUuid (const agx::Uuid &uuid)
 Explicitly set a Uuid on a serializable object.
 
- Public Attributes inherited from agx::Component
ObjectEvent addObjectEvent
 
ObjectEvent removeObjectEvent
 
- Static Protected Member Functions inherited from agx::Referenced
static DeleteHandlergetDeleteHandler ()
 
static void setDeleteHandler (DeleteHandler *handler)
 Internal: Set a DeleteHandler to which deletion of all referenced counted objects will be delegated to.
 
- Protected Attributes inherited from agxSDK::EventListener
bool m_enabled
 
int m_mask
 
agx::UInt32 m_priority
 
Type m_type
 
- Protected Attributes inherited from agx::Referenced
Mutex m_mutex
 
ObserverContainer m_observers
 
AtomicValue m_refCount
 

Detailed Description

Helper to use lambdas as preCollide, pre, post and last step events.

The arguments after the lambda are bind to the arguments. A pointer to the simulation is mandatory. Usage: Pre-collide callback that prints time. agxUtil::StepEventCallback::preCollide( []( agxSDK::Simulation* simulation ) { std::cout << "Simulation time: " << simulation->getTimeStamp() << " s." << std::endl; }, simulation );

Post-step callback with a counter. agxUtil::StepEventCallback::post( []( agxSDK::Simulation*, int& counter ) { std::cout << "# time steps: " << ++counter << std::endl; }, simulation, int{} );

Note
The internally created agxSDK::StepEventListener has to be removed either implicitly when calling agxSDK::Simulation::cleanup or explicitly by doing agxUtil::StepEventCallback::uninitialize( simulation ).

Definition at line 45 of file StepEventCallback.h.

Constructor & Destructor Documentation

◆ ~StepEventCallback()

virtual agxUtil::StepEventCallback::~StepEventCallback ( )
protectedvirtual

Reference counted object, protected destructor.

Member Function Documentation

◆ addNotification()

virtual void agxUtil::StepEventCallback::addNotification ( )
overrideprotectedvirtual

Add notification callback.

Reimplemented from agxSDK::EventListener.

◆ callAt()

template<typename FuncT , typename... Args>
void agxUtil::StepEventCallback::callAt ( agx::Real  timeStamp,
DefaultIsPreCollide  callbackType,
FuncT &&  callback,
agxSDK::Simulation simulation,
Args &&...  args 
)
static

Register callback to be called at a given simulation time.

callbackType is the step event type the callback will be executed in, e.g., {} (default is PRE_COLLIDE), agxSDK::StepEventCallback::POST_STEP or agxSDK::StepEventCallback::PRE_COLLIDE | agxSDK::StepEventCallback::POST_STEP for both pre-collide and post step that time.

Note
The time stamp has changed in LAST_STEP, so if callbackType includes LAST_STEP and some/all other (PRE_COLLIDE, PRE_STEP, POST_STEP) are included LAST_STEP will be called "first", i.e., last of the previous step.
Parameters
timeStamp- simulation time stamp for the callback to be executed
callbackType- activation mask, default PRE_COLLIDE
callback- the callback function
simulation- the simulation the callback should be coupled with
args- rest of the arguments to the callback

Definition at line 392 of file StepEventCallback.h.

◆ callFor()

template<typename FuncT , typename... Args>
void agxUtil::StepEventCallback::callFor ( agx::Real  seconds,
DefaultIsPreCollide  callbackType,
FuncT &&  callback,
agxSDK::Simulation simulation,
Args &&...  args 
)
static

Executes given callback in seconds seconds from the current time stamp of the given simulation.

Parameters
seconds- number of simulation seconds the callback should be executed before removed
callbackType- activation mask, default PRE_COLLIDE
callback- the callback function
simulation- the simulation the callback should be coupled with
args- rest of the arguments to the callback

Definition at line 408 of file StepEventCallback.h.

References agxSDK::Simulation::getTimeStamp().

◆ last() [1/2]

virtual void agxUtil::StepEventCallback::last ( const agx::TimeStamp time)
overrideprotectedvirtual

Called after a step is taken in the simulation Implement this method in the derived class to get callbacks.

Note
In this call, the simulation time is the new updated time.
Parameters
time- The new updated simulation time incremented by the time step.

Reimplemented from agxSDK::StepEventListener.

◆ last() [2/2]

template<typename FuncT , typename... Args>
void agxUtil::StepEventCallback::last ( FuncT &&  callback,
agxSDK::Simulation simulation,
Args &&...  args 
)
static

Register a last step event callback.

Parameters
callback- the callback function
simulation- the simulation the callback should be coupled with
args- rest of the arguments to the callback

Definition at line 379 of file StepEventCallback.h.

◆ post() [1/2]

virtual void agxUtil::StepEventCallback::post ( const agx::TimeStamp time)
overrideprotectedvirtual

Called after a step is taken in the simulation Implement this method in the derived class to get callbacks.

Parameters
time- the current simulation time

Reimplemented from agxSDK::StepEventListener.

◆ post() [2/2]

template<typename FuncT , typename... Args>
void agxUtil::StepEventCallback::post ( FuncT &&  callback,
agxSDK::Simulation simulation,
Args &&...  args 
)
static

Register a post step event callback.

Parameters
callback- the callback function
simulation- the simulation the callback should be coupled with
args- rest of the arguments to the callback

Definition at line 366 of file StepEventCallback.h.

◆ pre() [1/2]

virtual void agxUtil::StepEventCallback::pre ( const agx::TimeStamp time)
overrideprotectedvirtual

Called before a step is taken in the simulation Implement this method in the derived class to get callbacks.

Parameters
time- the current simulation time

Reimplemented from agxSDK::StepEventListener.

◆ pre() [2/2]

template<typename FuncT , typename... Args>
void agxUtil::StepEventCallback::pre ( FuncT &&  callback,
agxSDK::Simulation simulation,
Args &&...  args 
)
static

Register a pre step event callback.

Parameters
callback- the callback function
simulation- the simulation the callback should be coupled with
args- rest of the arguments to the callback

Definition at line 353 of file StepEventCallback.h.

◆ preCollide() [1/2]

virtual void agxUtil::StepEventCallback::preCollide ( const agx::TimeStamp time)
overrideprotectedvirtual

Called before collision detection is performed in the simulation Implement this method in the derived class to get callbacks.

Parameters
time- the current simulation time

Reimplemented from agxSDK::StepEventListener.

◆ preCollide() [2/2]

template<typename FuncT , typename... Args>
void agxUtil::StepEventCallback::preCollide ( FuncT &&  callback,
agxSDK::Simulation simulation,
Args &&...  args 
)
static

Register a pre-collide step event callback.

Parameters
callback- the callback function
simulation- the simulation the callback should be coupled with
args- rest of the arguments to the callback

Definition at line 340 of file StepEventCallback.h.

◆ removeMe()

static void agxUtil::StepEventCallback::removeMe ( agxSDK::Simulation simulation)
static

Call this from within a callback to be removed from receiving any more calls.

E.g.,

agxUtil::StepEventCallback::preCollide( []( agxSDK::Simulation* simulation, ... ) { if ( simulation->getTimeStamp() > 0.5 ) { agxUtil::StepEventCallback::removeMe( simulation ); return; } }, ... );

◆ removeNotification()

virtual void agxUtil::StepEventCallback::removeNotification ( )
overrideprotectedvirtual

Remove notification callback - will unregister this from the static instances.

Reimplemented from agxSDK::EventListener.

◆ uninitialize()

static void agxUtil::StepEventCallback::uninitialize ( agxSDK::Simulation simulation)
static

Removes internal step event listener from the simulation (if created).

Note
It's in general not necessary to call this as long as agxSDK::Simulation::cleanup is called with flag ALL.

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