AGX Dynamics  2.38.0.2
EventSensor.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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 
228  bool wasParticleRemovedBySensor( agx::UInt32 particle );
229 
233  bool wasRigidBodyRemovedBySensor( agx::UInt32 rigidBody );
234 
240  void setEnableBlocker(bool isBlocker);
241 
245  bool getEnableBlocker();
246 
252  virtual void preCollide(const agx::TimeStamp& time) override;
253 
259  virtual void pre(const agx::TimeStamp& time) override;
260 
266  virtual void post(const agx::TimeStamp& time) override;
267 
272  virtual void addNotification() override;
273 
278  virtual void removeNotification() override;
279 
283  const ParticlePairContactVector& getParticlePairContactsInSensor();
284 
285  /*
286  * Returns the particle id:s currently in contact with the sensor geometry
287  */
288  agx::UInt32Vector getContactingParticleIds();
289 
290  /*
291  * Returns the particle id:s whose midpoint is currently _inside_ the sensor geometry
292  */
293  agx::UInt32Vector getParticlesInsideSensor();
294 
298  agx::UInt32Vector getRemovedParticles();
299 
300  /*
301  * Returns a vector containing RigidBodies inside the sensor volume
302  */
303  agx::RigidBodyPtrVector getRigidBodiesInsideSensor(bool onlyRigidBodyDEM = false);
304 
305  /*
306  * Returns the number of particles currently contacting the sensor
307  */
308  agx::Real getNumParticlesInSensor() const;
309 
310  /*
311  * Returns the number of particle-particle contacts in the sensor
312  */
313  agx::Real getNumParticleParticleContactsInSensor();
314 
318  agx::Real getTotalRemovedMass() const;
319 
323  agx::Real getRemovedParticleMass() const;
324 
328  agx::Real getRemovedBodyMass() const;
329 
333  agx::UInt getNumRemovedParticles() const;
334 
338  agx::UInt getNumRemovedBodies() const;
339 
343  SensorEvent* getSensorEvent(const agx::Uuid& uuid);
344 
346  bool hasOnlyReference();
347 
349 
351 
352 
356  void registerRemovedParticle( agx::Physics::ParticlePtr particle );
357 
358  void registerRemovedBody( agx::RigidBody* body );
359 
360  // Dummy method to allow EventSensor to pass makeCallable compilation.
361  void setRealDummyMethod(agx::Real /*inVal*/) { }
363 
364  public:
365 
366  template <typename T>
368  {
369  public:
370  EventPrioHolder( ): m_priority( 0 ) {}
371  EventPrioHolder( T obj, int priority ) : m_obj( obj ), m_priority( priority ) {}
372  inline int priority() const {
373  return m_priority;
374  }
375  inline operator T () {
376  return m_obj;
377  }
378  inline operator const T () const {
379  return m_obj;
380  }
381  inline operator int () const {
382  return m_priority;
383  }
384  inline T sensorEvent() {
385  return m_obj;
386  }
387  inline const T sensorEvent() const {
388  return m_obj;
389  }
390 
391 
392  protected:
393  T m_obj;
395  };
396 
397  typedef std::map<agx::Uuid, EventPrioHolder<SensorEventRef> > SensorEvents;
399 
402 
403  protected:
404 
405  /*
406  * Placeholder for information regarding the geometry contacts
407  */
409  {
411  :t(tin),cd(cdin)
412  {
413  }
414 
417  private:
418  ContactInformation& operator=(const ContactInformation&);
419  };
420 
425  {
427  agx::Physics::ParticleGeometryContactInstance contactIn,
428  agx::Physics::ParticleData& particleDataIn,
429  agx::Physics::GeometryData& geometryDataIn)
430  :t(tIn),contact(contactIn), particleData(particleDataIn), geometryData(geometryDataIn)
431  {
432  }
434  agx::Physics::ParticleGeometryContactInstance contact;
435  agx::Physics::ParticleData& particleData;
436  agx::Physics::GeometryData& geometryData;
437  private:
439  };
440 
443 
444  protected:
445 
446  void init();
447 
448  void constructSortedEventList();
449 
450  SensorEvent* getSensorEventFromName(const agx::Name& name);
451 
452  bool contactIsValid(agxCollide::GeometryContact* cd);
453 
454  void clearContactInformation();
455 
456  void refreshParticlesInsideSensorData();
457 
458  void refreshRigidBodiesInsideSensorData();
459 
460  void updateSensorEvents(const agx::TimeStamp& t);
461 
462  void executeSensorEvents(const agx::TimeStamp& t);
463 
464  void triggerContactEvents();
465 
466  void triggerEvents();
467 
468  void triggerPostContactHandling();
469 
470  void triggerEventAddNotification();
471 
472  agx::RigidBody * getContactingRigidBody(agxCollide::GeometryContact* cd) const;
473 
474  void signalGeometryContact(const agx::TimeStamp& t, agxCollide::GeometryContact* cd);
475  void signalParticleContact(const agx::TimeStamp& t,
476  agx::Physics::ParticleGeometryContactInstance contact,
477  agx::Physics::ParticleData& particleData,
478  agx::Physics::GeometryData& geometryData);
479 
480  void triggerParticleEvents();
481 
482  void tryTriggerEventsOnContact(const agx::TimeStamp& t, agxCollide::GeometryContact* cd);
483  void tryTriggerParticleEventsOnContact( const agx::TimeStamp& t,
484  agx::Physics::ParticleGeometryContactInstance contact,
485  agx::Physics::ParticleData& particleData,
486  agx::Physics::GeometryData& geometryData);
487 
488  void triggerEventsOnContact(const agx::TimeStamp& t, agxCollide::GeometryContact* cd);
489  void triggerParticleEventsOnContact( const agx::TimeStamp& t,
490  agx::Physics::ParticleGeometryContactInstance contact,
491  agx::Physics::ParticleData& particleData,
492  agx::Physics::GeometryData& geometryData);
493 
494  bool particleContactIsValid( const agx::TimeStamp& t,
495  agx::Physics::ParticleGeometryContactInstance contact,
496  agx::Physics::ParticleData& particleData,
497  agx::Physics::GeometryData& geometryData);
498 
499  protected:
500 
502  virtual ~EventSensor();
503 
506 
509 
512 
515 
518 
522 
526 
529 
532 
535 
538 
546 
549 
553 
557  };
558 }
559 
560 #endif /*AGXMODEL_EVENTSENSOR_H*/
agxPhysics_export.h
agxControl::EventSensor::m_numRemovedParticles
agx::UInt m_numRemovedParticles
Definition: EventSensor.h:544
agxControl::EventSensor::ParticleContactListener::m_sensor
EventSensor * m_sensor
Definition: EventSensor.h:120
agx::UInt
uint64_t UInt
Definition: Integer.h:27
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:537
agx::Real
double Real
Definition: Real.h:42
agxControl::EventSensor::m_particlePairContacts
ParticlePairContactVector m_particlePairContacts
Definition: EventSensor.h:556
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:65
agxControl::EventSensor::EventPrioHolder::m_obj
T m_obj
Definition: EventSensor.h:393
agxControl::EventSensor::ParticleContactInformation::contact
agx::Physics::ParticleGeometryContactInstance contact
Definition: EventSensor.h:434
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:525
agxControl::EventSensor::ParticleContactInformation::geometryData
agx::Physics::GeometryData & geometryData
Definition: EventSensor.h:436
agx::RigidBody
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:51
CALLABLE
#define CALLABLE
Definition: macros.h:72
agxControl::EventSensor::m_geometry
agxCollide::Geometry * m_geometry
The coupled geometry of the sensor.
Definition: EventSensor.h:514
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:370
agxControl::EventSensor::ParticleContactInformation
Placeholder for information regarding the particle contacts.
Definition: EventSensor.h:424
agxControl::EventSensor::ParticleContactInformation::ParticleContactInformation
ParticleContactInformation(const agx::TimeStamp tIn, agx::Physics::ParticleGeometryContactInstance contactIn, agx::Physics::ParticleData &particleDataIn, agx::Physics::GeometryData &geometryDataIn)
Definition: EventSensor.h:426
Geometry.h
agxControl::EventSensor::ContactInformation::cd
agxCollide::GeometryContact * cd
Definition: EventSensor.h:416
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::m_removedParticles
agx::HashSet< agx::UInt32 > m_removedParticles
Definition: EventSensor.h:541
agxControl::EventSensor::SensorEventVector
agx::Vector< agxControl::SensorEvent * > SensorEventVector
Definition: EventSensor.h:51
agxControl::EventSensor::EventPrioHolder::EventPrioHolder
EventPrioHolder(T obj, int priority)
Definition: EventSensor.h:371
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:528
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:400
StepEventListener.h
agxControl::EventSensor::m_contactListener
GeometryContactEventListenerRef m_contactListener
Listeners that are coupled with the sensor.
Definition: EventSensor.h:524
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:408
agxControl::EventSensor::m_filter
agxSDK::CollisionGroupFilterRef m_filter
The collision group filter coupled with the ContactEventListener.
Definition: EventSensor.h:517
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:534
agxControl::EventSensor::GeometryContactEventListener
Definition: EventSensor.h:75
agxControl::EventSensor::SensorEvents
std::map< agx::Uuid, EventPrioHolder< SensorEventRef > > SensorEvents
Definition: EventSensor.h:397
agxControl::EventSensor::EventPrioHolder::sensorEvent
const T sensorEvent() const
Definition: EventSensor.h:387
agxControl::EventSensor::ParticleContactInformation::t
const agx::TimeStamp t
Definition: EventSensor.h:433
agxControl::EventSensor::m_priority
int m_priority
Priority of the sensor over all the other sensors.
Definition: EventSensor.h:508
agxControl::EventSensor::m_geometryCollision
bool m_geometryCollision
Definition: EventSensor.h:552
agxControl::EventSensor::ParticleContactInformation::particleData
agx::Physics::ParticleData & particleData
Definition: EventSensor.h:435
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_removedBodyMass
agx::Real m_removedBodyMass
Definition: EventSensor.h:543
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:548
agxControl::EventSensor::m_triggerFlag
int m_triggerFlag
Flag for when the events are supposed to be executed during the time-step.
Definition: EventSensor.h:505
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:555
agxControl::EventSensor::EventPrioHolder::priority
int priority() const
Definition: EventSensor.h:372
agxControl::EventSensor::ParticleContactQueue
agx::Vector< ParticleContactInformation > ParticleContactQueue
Definition: EventSensor.h:441
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:520
agxControl::EventSensor::g_forbiddenGeometries
static agxCollide::GeometryPtrVector g_forbiddenGeometries
Definition: EventSensor.h:57
agxControl::EventSensor::EventPrioHolder::m_priority
int m_priority
Definition: EventSensor.h:394
agxCollide::Geometry
The geometry representation used by the collision detection engine.
Definition: Geometry.h:91
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:410
agxControl::EventSensor::EventPrioHolder
Definition: EventSensor.h:367
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:254
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:531
agxCollide::GeometryContact
A contact between two geometries.
Definition: Contacts.h:270
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 Dynamics API including thread resources and must be executed before using the AGX API.
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:398
agxControl::EventSensor::m_removedParticleMass
agx::Real m_removedParticleMass
Definition: EventSensor.h:542
agxControl::EventSensor::m_collisionMask
int m_collisionMask
Get the collision mask for the sensor.
Definition: EventSensor.h:511
agx::UInt32
uint32_t UInt32
Definition: Integer.h:32
agxControl::EventSensor::m_particleCollision
bool m_particleCollision
Flags for doing when there is a collision for a particle or geometry.
Definition: EventSensor.h:551
agxControl::EventSensor::m_numRemovedBodies
agx::UInt m_numRemovedBodies
Definition: EventSensor.h:545
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:384
agxControl::EventSensor::GeometryContactQueue
agx::Vector< ContactInformation > GeometryContactQueue
Definition: EventSensor.h:442
TimeStamp.h
agxControl::EventSensor::m_removedBodies
agx::HashSet< agx::UInt32 > m_removedBodies
Holds a set of the current particles and bodies that have been marked as removed by an Operation duri...
Definition: EventSensor.h:540
agxControl::EventSensor::m_sensorEventList
SensorEventList m_sensorEventList
Definition: EventSensor.h:401
agxControl::EventSensor::ContactInformation::t
const agx::TimeStamp t
Definition: EventSensor.h:415
agxControl::EventSensor::m_particleContactQueue
ParticleContactQueue m_particleContactQueue
Definition: EventSensor.h:521
agxSDK::ContactEventListener
Derive from this class to implement a listener for Collision events.
Definition: ContactEventListener.h:44