104 template<
typename FuncT,
typename... Args>
105 static void preCollide( FuncT&& callback,
agxSDK::Simulation* simulation, Args&&... args );
113 template<
typename FuncT,
typename... Args>
122 template<
typename FuncT,
typename... Args>
131 template<
typename FuncT,
typename... Args>
148 template<
typename FuncT,
typename... Args>
164 template<
typename FuncT,
typename... Args>
207 using Callback = std::function<void()>;
208 struct TimedCallbackData
211 CallbackType callbackType = PRE_COLLIDE_TYPE;
216 using StepCallbackVector = std::vector<Callback>;
217 using TimedCallbackVector = std::vector<TimedCallbackData>;
220 using StepCallbacks = std::array<StepCallbackVector, (size_t)NUM_CALLBACK_TYPES>;
221 using TimedCallbacks = std::array<TimedCallbackVector, (size_t)NUM_CALLBACK_TYPES>;
228 EXECUTING_CALLBACKS = 1 << 0,
229 REMOVE_CURRENT_REQUEST = 1 << 1,
231 using Flags = agx::BitState<Flag, std::underlying_type_t<Flag>>;
234 CallbackType currCallbackType = NUM_CALLBACK_TYPES;
235 StepCallbackVector deferredStepCallbacks;
236 TimedCallbackVector deferredTimedCallbacks;
238 inline InternalState()
243 inline bool removeRequestedAndResetState()
245 const auto removeRequested = flags.Is( REMOVE_CURRENT_REQUEST );
246 flags.update( REMOVE_CURRENT_REQUEST,
false );
247 return removeRequested;
250 inline bool isExecutingCallbacks( CallbackType callbackType )
const
252 return currCallbackType != NUM_CALLBACK_TYPES && callbackType == currCallbackType;
255 inline void begin( CallbackType callbackType )
257 agxAssert( currCallbackType == NUM_CALLBACK_TYPES );
258 agxAssert( !flags.Is( EXECUTING_CALLBACKS ) );
259 agxAssert( deferredStepCallbacks.empty() );
260 agxAssert( deferredTimedCallbacks.empty() );
261 flags.update( EXECUTING_CALLBACKS,
true );
262 currCallbackType = callbackType;
265 inline void end( StepCallbackVector& stepCallbacks, TimedCallbackVector& timedCallbacks )
267 stepCallbacks.insert( stepCallbacks.end(),
268 deferredStepCallbacks.begin(),
269 deferredStepCallbacks.end() );
270 deferredStepCallbacks.clear();
272 timedCallbacks.insert( timedCallbacks.end(),
273 deferredTimedCallbacks.begin(),
274 deferredTimedCallbacks.end() );
275 deferredTimedCallbacks.clear();
277 flags.update( EXECUTING_CALLBACKS,
false );
278 currCallbackType = NUM_CALLBACK_TYPES;
301 static void registerCallback(
const Callback& callback,
302 CallbackType callbackType,
314 static void registerCallback(
agx::Real startTime,
316 DefaultIsPreCollide callbackType,
317 const Callback& callback,
330 void executeCallbacks(
const agx::TimeStamp& t, CallbackType callbackType );
333 static InstanceType s_instances;
334 StepCallbacks m_stepCallbacks;
335 TimedCallbacks m_timedCallbacks;
336 InternalState m_state;
339 template<
typename FuncT,
typename... Args>
342 if ( simulation ==
nullptr )
345 registerCallback( std::bind( std::forward<FuncT>( callback ),
347 std::forward<Args>( args )... ),
352 template<
typename FuncT,
typename... Args>
355 if ( simulation ==
nullptr )
358 registerCallback( std::bind( std::forward<FuncT>( callback ),
360 std::forward<Args>( args )... ),
365 template<
typename FuncT,
typename... Args>
368 if ( simulation ==
nullptr )
371 registerCallback( std::bind( std::forward<FuncT>( callback ),
373 std::forward<Args>( args )... ),
378 template<
typename FuncT,
typename... Args>
381 if ( simulation ==
nullptr )
384 registerCallback( std::bind( std::forward<FuncT>( callback ),
386 std::forward<Args>( args )... ),
391 template<
typename FuncT,
typename... Args>
398 registerCallback( timeStamp,
401 std::bind( std::forward<FuncT>( callback ),
403 std::forward<Args>( args )... ),
407 template<
typename FuncT,
typename... Args>
414 if ( simulation ==
nullptr )
420 std::bind( std::forward<FuncT>( callback ),
422 std::forward<Args>( args )... ),
#define AGXPHYSICS_EXPORT
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
agx::Real getTimeStamp() const
Derive from this class to implement a listener for simulation step events.
ActivationMask
Defines the event states for which a listener will be activated, a mask can be bitwise OR:ed from the...
@ PRE_COLLIDE
Triggered just BEFORE a simulation step is taken.
Helper to use lambdas as preCollide, pre, post and last step events.
virtual void addNotification() override
Add notification callback.
static void removeMe(agxSDK::Simulation *simulation)
Call this from within a callback to be removed from receiving any more calls.
virtual ~StepEventCallback()
Reference counted object, protected destructor.
static void last(FuncT &&callback, agxSDK::Simulation *simulation, Args &&... args)
Register a last step event callback.
static void preCollide(FuncT &&callback, agxSDK::Simulation *simulation, Args &&... args)
Register a pre-collide step event callback.
static void pre(FuncT &&callback, agxSDK::Simulation *simulation, Args &&... args)
Register a pre step event 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 call...
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 cal...
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 call...
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.
static void post(FuncT &&callback, agxSDK::Simulation *simulation, Args &&... args)
Register a post step event callback.
virtual void removeNotification() override
Remove notification callback - will unregister this from the static instances.
virtual void preCollide(const agx::TimeStamp &t) override
Called before collision detection is performed in the simulation Implement this method in the derived...
static void uninitialize(agxSDK::Simulation *simulation)
Removes internal step event listener from the simulation (if created).
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.
Inheritance with partial specialization due to bug with ref_ptr containers.
Smart pointer for handling referenced counted objects.
Flags
Flags for specifying what is enabled in the debug rendering system.
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
The agxUtil namespace contain classes and methods for utility functionality.
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
Used as argument in timed callbacks (callAt, callFor et al.) where it might not matter as much when t...
DefaultIsPreCollide(int activation=agxSDK::StepEventListener::PRE_COLLIDE)
Construct given activation mask, default PRE_COLLIDE.