AGXDynamics  2.35.0.3
EventSensor.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXCONTROL_EVENTSENSOR_H
18 #define AGXCONTROL_EVENTSENSOR_H
19 #include <agx/config.h>
20 
21 #include <agx/agxPhysics_export.h>
23 #include <agx/TimeStamp.h>
24 #include <agxCollide/Geometry.h>
27 #include <agx/HashSet.h>
29 #include <map>
30 #include <agxControl/SensorEvent.h>
32 
33 namespace agxControl
34 {
35 
36  class SensorEvent;
37 
40 
48  {
49  public:
52 
53  protected:
54  // This is a static list that contains forbidden geometries. And exclusive sensor can mark
55  // geometries as forbidden after they have been handled, which prevents other sensors form detecting them.
56  // The same goes for particles.
59 
60  public:
63  {
65  SENSOR_POST_STEP
66  };
67 
68  protected:
69 
72  /*
73  * This is an internal class handling the geometry contact triggers of the simulation
74  */
76  {
77  public:
78 
80 
83  virtual void separation(const agx::TimeStamp& , agxCollide::GeometryPair&);
84 
85  friend class EventSensor;
86 
87  protected:
89 
91  };
92 
95 
96  /*
97  * This is an internal class handling the geometry contact triggers of the simulation
98  */
100  {
101  public:
102 
104 
105  void setFilter(agxSDK::CollisionGroupFilter* filter);
106 
107  agxSDK::CollisionGroupFilter* getFilter();
108  const agxSDK::CollisionGroupFilter* getFilter() const;
109 
110  virtual void contactCallback(agx::Physics::ParticleGeometryContactInstance contact,
111  agx::Physics::ParticleData& particleData,
112  agx::Physics::GeometryData& geometryData);
113 
114  friend class EventSensor;
115 
116  protected:
117  virtual ~ParticleContactListener();
118 
119  protected:
122  };
123 
124  public:
125 
127  EventSensor();
128 
133  EventSensor(int priority);
134 
140 
146  EventSensor(agxCollide::Geometry* geometry, int priority);
147 
151  void addGroup(const agx::Name& group);
152  void addGroup(agx::UInt32 group);
153  void removeGroup(const agx::Name& group);
154  void removeGroup(agx::UInt32 group);
155  void removeAllGroups();
156  bool hasGroup(const agx::Name& group);
157  bool hasGroup(agx::UInt32 group);
158 
164  bool addSensorEvent(SensorEvent* sEvent, int priority = 0);
165 
170  bool removeSensorEvent(SensorEvent* sEvent);
171 
176  bool containsSensorEvent(SensorEvent *sEvent);
177 
182  SensorEventVector getSensorEvents();
183 
187  void setGeometry(agxCollide::Geometry* geometry);
188 
192  agxCollide::Geometry* getGeometry();
193  agxCollide::Geometry* getGeometry() const;
194 
198  void setTriggerFlag(int flag);
199 
203  int getTriggerFlag();
204 
208  void setCollisionMask(int mask);
209 
213  int getCollisionMask();
214 
218  bool isForbiddenGeometry(agxCollide::Geometry* geom);
219 
223  bool isForbiddenParticle(agx::UInt32 particle);
224 
230  void setEnableBlocker(bool isBlocker);
231 
235  bool getEnableBlocker();
236 
242  virtual void preCollide(const agx::TimeStamp& time) override;
243 
249  virtual void pre(const agx::TimeStamp& time) override;
250 
256  virtual void post(const agx::TimeStamp& time) override;
257 
262  virtual void addNotification() override;
263 
268  virtual void removeNotification() override;
269 
273  const ParticlePairContactVector& getParticlePairContactsInSensor();
274 
275  /*
276  * Returns the particle id:s currently in contact with the sensor geometry
277  */
278  agx::UInt32Vector getContactingParticleIds();
279 
280  /*
281  * Returns the particle id:s whose midpoint is currently _inside_ the sensor geometry
282  */
283  agx::UInt32Vector getParticlesInsideSensor();
284 
285  /*
286  * Returns a vector containing RigidBodies inside the sensor volume
287  */
288  agx::RigidBodyPtrVector getRigidBodiesInsideSensor(bool onlyRigidBodyDEM = false);
289 
290  /*
291  * Returns the number of particles currently contacting the sensor
292  */
293  agx::Real getNumParticlesInSensor() const;
294 
295  /*
296  * Returns the number of particle-particle contacts in the sensor
297  */
298  agx::Real getNumParticleParticleContactsInSensor();
299 
303  SensorEvent* getSensorEvent(const agx::Uuid& uuid);
304 
306  bool hasOnlyReference();
307 
309 
311  // Dummy method to allow EventSensor to pass makeCallable compilation.
312  void setRealDummyMethod(agx::Real /*inVal*/) { }
314 
315  public:
316 
317  template <typename T>
319  {
320  public:
321  EventPrioHolder( ): m_priority( 0 ) {}
322  EventPrioHolder( T obj, int priority ) : m_obj( obj ), m_priority( priority ) {}
323  inline int priority() const {
324  return m_priority;
325  }
326  inline operator T () {
327  return m_obj;
328  }
329  inline operator const T () const {
330  return m_obj;
331  }
332  inline operator int () const {
333  return m_priority;
334  }
335  inline T sensorEvent() {
336  return m_obj;
337  }
338  inline const T sensorEvent() const {
339  return m_obj;
340  }
341 
342 
343  protected:
344  T m_obj;
346  };
347 
348  typedef std::map<agx::Uuid, EventPrioHolder<SensorEventRef> > SensorEvents;
350 
353 
354  protected:
355 
356  /*
357  * Placeholder for information regarding the geometry contacts
358  */
360  {
362  :t(tin),cd(cdin)
363  {
364  }
365 
368  private:
369  ContactInformation& operator=(const ContactInformation&);
370  };
371 
376  {
378  agx::Physics::ParticleGeometryContactInstance contactIn,
379  agx::Physics::ParticleData& particleDataIn,
380  agx::Physics::GeometryData& geometryDataIn)
381  :t(tIn),contact(contactIn), particleData(particleDataIn), geometryData(geometryDataIn)
382  {
383  }
385  agx::Physics::ParticleGeometryContactInstance contact;
386  agx::Physics::ParticleData& particleData;
387  agx::Physics::GeometryData& geometryData;
388  private:
390  };
391 
394 
395  protected:
396 
397  void init();
398 
399  void constructSortedEventList();
400 
401  SensorEvent* getSensorEventFromName(const agx::Name& name);
402 
403  bool contactIsValid(agxCollide::GeometryContact* cd);
404 
405  void clearContactInformation();
406 
407  void refreshParticlesInsideSensorData();
408 
409  void refreshRigidBodiesInsideSensorData();
410 
411  void updateSensorEvents(const agx::TimeStamp& t);
412 
413  void executeSensorEvents(const agx::TimeStamp& t);
414 
415  void triggerContactEvents();
416 
417  void triggerEvents();
418 
419  void triggerPostContactHandling();
420 
421  void triggerEventAddNotification();
422 
423  agx::RigidBody * getContactingRigidBody(agxCollide::GeometryContact* cd) const;
424 
425  void signalGeometryContact(const agx::TimeStamp& t, agxCollide::GeometryContact* cd);
426  void signalParticleContact(const agx::TimeStamp& t,
427  agx::Physics::ParticleGeometryContactInstance contact,
428  agx::Physics::ParticleData& particleData,
429  agx::Physics::GeometryData& geometryData);
430 
431  void triggerParticleEvents();
432 
433  void tryTriggerEventsOnContact(const agx::TimeStamp& t, agxCollide::GeometryContact* cd);
434  void tryTriggerParticleEventsOnContact( const agx::TimeStamp& t,
435  agx::Physics::ParticleGeometryContactInstance contact,
436  agx::Physics::ParticleData& particleData,
437  agx::Physics::GeometryData& geometryData);
438 
439  void triggerEventsOnContact(const agx::TimeStamp& t, agxCollide::GeometryContact* cd);
440  void triggerParticleEventsOnContact( const agx::TimeStamp& t,
441  agx::Physics::ParticleGeometryContactInstance contact,
442  agx::Physics::ParticleData& particleData,
443  agx::Physics::GeometryData& geometryData);
444 
445  bool particleContactIsValid( const agx::TimeStamp& t,
446  agx::Physics::ParticleGeometryContactInstance contact,
447  agx::Physics::ParticleData& particleData,
448  agx::Physics::GeometryData& geometryData);
449 
450  protected:
451 
453  virtual ~EventSensor();
454 
457 
460 
463 
466 
469 
473 
477 
480 
483 
486 
489 
492 
496 
500  };
501 }
502 
503 #endif /*AGXMODEL_EVENTSENSOR_H*/
agxPhysics_export.h
agxControl::EventSensor::ParticleContactListener::m_sensor
EventSensor * m_sensor
Definition: EventSensor.h:120
agxControl::EventSensor::m_rigidBodiesInsideSensor
agx::HashSet< agx::RigidBody * > m_rigidBodiesInsideSensor
Holds pointers to the current rigid bodies (DEM Objects) where the midpoint is INSIDE the sensor.
Definition: EventSensor.h:488
agx::Real
double Real
Definition: Real.h:42
agxControl::EventSensor::m_particlePairContacts
ParticlePairContactVector m_particlePairContacts
Definition: EventSensor.h:499
agx::List
Definition: List.h:107
agxSDK::StepEventListener
Derive from this class to implement a listener for simulation step events.
Definition: StepEventListener.h:37
agxData::GeometryPair
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:66
agxControl::EventSensor::EventPrioHolder::m_obj
T m_obj
Definition: EventSensor.h:344
agxControl::EventSensor::ParticleContactInformation::contact
agx::Physics::ParticleGeometryContactInstance contact
Definition: EventSensor.h:385
ParticleContactSensor.h
agx::Vector< agx::Physics::ParticlePairContactPtr >
agxControl
This namespace contains general operation and action classes which can be used to control a agxSDK::S...
agxControl::EventSensor::m_particleContactListener
ParticleContactListenerRef m_particleContactListener
Definition: EventSensor.h:476
agxControl::EventSensor::ParticleContactInformation::geometryData
agx::Physics::GeometryData & geometryData
Definition: EventSensor.h:387
agx::RigidBody
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
CALLABLE
#define CALLABLE
Definition: macros.h:72
agxControl::EventSensor::m_geometry
agxCollide::Geometry * m_geometry
The coupled geometry of the sensor.
Definition: EventSensor.h:465
agxControl::EventSensor::ParticleContactListener::m_filter
agxSDK::CollisionGroupFilter * m_filter
Definition: EventSensor.h:121
DOXYGEN_END_INTERNAL_BLOCK
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
agxControl::EventSensor::EventPrioHolder::EventPrioHolder
EventPrioHolder()
Definition: EventSensor.h:321
agxControl::EventSensor::ParticleContactInformation
Placeholder for information regarding the particle contacts.
Definition: EventSensor.h:375
agxControl::EventSensor::ParticleContactInformation::ParticleContactInformation
ParticleContactInformation(const agx::TimeStamp tIn, agx::Physics::ParticleGeometryContactInstance contactIn, agx::Physics::ParticleData &particleDataIn, agx::Physics::GeometryData &geometryDataIn)
Definition: EventSensor.h:377
Geometry.h
agxControl::EventSensor::ContactInformation::cd
agxCollide::GeometryContact * cd
Definition: EventSensor.h:367
CollisionGroupFilter.h
DOXYGEN_START_INTERNAL_BLOCK
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
agxControl::EventSensor::ParticleContactListener
Definition: EventSensor.h:99
agxControl::EventSensor::TriggerEventStages
TriggerEventStages
Defines the event execution states when the SensorEvents are executed during the timestep of the dete...
Definition: EventSensor.h:62
agxControl::EventSensor::SensorEventVector
agx::Vector< agxControl::SensorEvent * > SensorEventVector
Definition: EventSensor.h:51
agxControl::EventSensor::EventPrioHolder::EventPrioHolder
EventPrioHolder(T obj, int priority)
Definition: EventSensor.h:322
agxControl::EventSensor::m_particlesInSensor
agx::HashSet< agx::UInt32 > m_particlesInSensor
Holds a set of the current particle id:s that are present in the sensor.
Definition: EventSensor.h:479
agxUtil::addGroup
AGXPHYSICS_EXPORT void addGroup(const agx::RigidBody *body, unsigned id)
For the specified body, add the collision group ID to its geometries.
agxControl::EventSensor::g_forbiddenParticles
static agx::UInt32Vector g_forbiddenParticles
Definition: EventSensor.h:58
agxControl::EventSensor::SensorEventList
agx::List< EventPrioHolder< SensorEventRef > > SensorEventList
Definition: EventSensor.h:351
StepEventListener.h
agxControl::EventSensor::m_contactListener
GeometryContactEventListenerRef m_contactListener
Listeners that are coupled with the sensor.
Definition: EventSensor.h:475
AGXSTREAM_DECLARE_SERIALIZABLE
#define AGXSTREAM_DECLARE_SERIALIZABLE(T)
Use this in a Serializable class to add the required methods Important: Use full namespace in the dec...
Definition: Serializable.h:207
agx::VectorPOD
Vector containing 'raw' data.
Definition: Vector.h:245
agxControl::EventSensor::ContactInformation
Definition: EventSensor.h:359
agxControl::EventSensor::m_filter
agxSDK::CollisionGroupFilterRef m_filter
The collision group filter coupled with the ContactEventListener.
Definition: EventSensor.h:468
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
agx::ParticleContactSensor
This class is for detecting and handling particle collisions with geometries in the simulation.
Definition: ParticleContactSensor.h:42
AGXPHYSICS_EXPORT
#define AGXPHYSICS_EXPORT
agxControl::EventSensor::m_rigidBodiesContactingSensor
agx::HashSet< agx::RigidBody * > m_rigidBodiesContactingSensor
Holds pointers to the current rigid bodies (DEM Objects) in the sensor.
Definition: EventSensor.h:485
agxControl::EventSensor::GeometryContactEventListener
Definition: EventSensor.h:75
agxControl::EventSensor::SensorEvents
std::map< agx::Uuid, EventPrioHolder< SensorEventRef > > SensorEvents
Definition: EventSensor.h:348
agxControl::EventSensor::EventPrioHolder::sensorEvent
const T sensorEvent() const
Definition: EventSensor.h:338
agxControl::EventSensor::ParticleContactInformation::t
const agx::TimeStamp t
Definition: EventSensor.h:384
agxControl::EventSensor::m_priority
int m_priority
Priority of the sensor over all the other sensors.
Definition: EventSensor.h:459
agxControl::EventSensor::m_geometryCollision
bool m_geometryCollision
Definition: EventSensor.h:495
agxControl::EventSensor::ParticleContactInformation::particleData
agx::Physics::ParticleData & particleData
Definition: EventSensor.h:386
agxControl::EventSensor
This is a sensor class that can be attached to an geometry in the simulation.
Definition: EventSensor.h:47
AGX_DECLARE_VECTOR_TYPES
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
agxControl::EventSensor::m_isBlocker
bool m_isBlocker
True if the sensor should block the contact from other sensors in the scene under the same timestep.
Definition: EventSensor.h:491
agxControl::EventSensor::m_triggerFlag
int m_triggerFlag
Flag for when the events are supposed to be executed during the time-step.
Definition: EventSensor.h:456
agx::ref_ptr< CollisionGroupFilter >
agxControl::EventSensor::hasComputedParticlePairContactsInSensor
bool hasComputedParticlePairContactsInSensor
Vector that stores particle pair contacts. Will only be computed if the user asks for it (getParticle...
Definition: EventSensor.h:498
agxControl::EventSensor::EventPrioHolder::priority
int priority() const
Definition: EventSensor.h:323
agxControl::EventSensor::ParticleContactQueue
agx::Vector< ParticleContactInformation > ParticleContactQueue
Definition: EventSensor.h:392
agxSDK::ContactEventListener::KeepContactPolicy
KeepContactPolicy
Defines whether contact should be kept or removed after contact/impact methods returns.
Definition: ContactEventListener.h:59
SensorEvent.h
agxControl::EventSensor::m_geomContactQueue
GeometryContactQueue m_geomContactQueue
Queues for the contacts.
Definition: EventSensor.h:471
agxControl::EventSensor::g_forbiddenGeometries
static agxCollide::GeometryPtrVector g_forbiddenGeometries
Definition: EventSensor.h:57
agxControl::EventSensor::EventPrioHolder::m_priority
int m_priority
Definition: EventSensor.h:345
agxCollide::Geometry
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
agx::Name
Representation of a name string.
Definition: Name.h:32
agxControl::EventSensor::ParticlePairContactVector
agx::Vector< agx::Physics::ParticlePairContactPtr > ParticlePairContactVector
Definition: EventSensor.h:50
agx::HashSet< agx::UInt32 >
agxControl::EventSensor::ContactInformation::ContactInformation
ContactInformation(const agx::TimeStamp tin, agxCollide::GeometryContact *cdin)
Definition: EventSensor.h:361
agxControl::EventSensor::EventPrioHolder
Definition: EventSensor.h:318
HashSet.h
agxControl::EventSensor::GeometryContactEventListener::m_sensor
EventSensor * m_sensor
Definition: EventSensor.h:90
ParticlePairContactEntity.h
AGX_DECLARE_POINTER_TYPES
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
agxUtil::removeGroup
AGXPHYSICS_EXPORT void removeGroup(const agx::RigidBody *body, unsigned id)
For the specified body, remove the collision group ID to its geometries.
ContactEventListener.h
agxControl::EventSensor::m_particlesInsideSensor
agx::HashSet< agx::UInt32 > m_particlesInsideSensor
Holds a set of the current particle id:s whose midpoint is inside the sensor.
Definition: EventSensor.h:482
agxCollide::GeometryContact
A contact between two geometries.
Definition: Contacts.h:269
agx::TimeStamp
agx::Real TimeStamp
Definition: TimeStamp.h:26
agxControl::SensorEvent
A sensor event is an event triggered when the sensor is activated by a contact.
Definition: SensorEvent.h:48
agxSDK::CollisionGroupFilter
An ExecuteFilter that is used to filter out contacts matching a specified criteria based on Collision...
Definition: CollisionGroupFilter.h:41
agx::init
void AGXPHYSICS_EXPORT init()
Initialize agx.
agx::Uuid
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:41
agxControl::EventSensor::m_sensorEvents
SensorEvents m_sensorEvents
Definition: EventSensor.h:349
agxControl::EventSensor::m_collisionMask
int m_collisionMask
Get the collision mask for the sensor.
Definition: EventSensor.h:462
agx::UInt32
uint32_t UInt32
Definition: Integer.h:40
agxControl::EventSensor::m_particleCollision
bool m_particleCollision
Flags for doing when there is a collision for a particle or geometry.
Definition: EventSensor.h:494
agxControl::EventSensor::SENSOR_PRE_STEP
@ SENSOR_PRE_STEP
Triggered in a PRE solve/integration step.
Definition: EventSensor.h:64
agxControl::EventSensor::EventPrioHolder::sensorEvent
T sensorEvent()
Definition: EventSensor.h:335
agxControl::EventSensor::GeometryContactQueue
agx::Vector< ContactInformation > GeometryContactQueue
Definition: EventSensor.h:393
TimeStamp.h
agxControl::EventSensor::m_sensorEventList
SensorEventList m_sensorEventList
Definition: EventSensor.h:352
agxControl::EventSensor::ContactInformation::t
const agx::TimeStamp t
Definition: EventSensor.h:366
agxControl::EventSensor::m_particleContactQueue
ParticleContactQueue m_particleContactQueue
Definition: EventSensor.h:472
agxSDK::ContactEventListener
Derive from this class to implement a listener for Collision events.
Definition: ContactEventListener.h:44