AGX Dynamics  2.35.0.1
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 
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 
106 
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:
118 
119  protected:
122  };
123 
124  public:
125 
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);
156  bool hasGroup(const agx::Name& group);
157  bool hasGroup(agx::UInt32 group);
158 
164  bool addSensorEvent(SensorEvent* sEvent, int priority = 0);
165 
171 
177 
183 
188 
194 
198  void setTriggerFlag(int flag);
199 
204 
208  void setCollisionMask(int mask);
209 
214 
219 
224 
230  void setEnableBlocker(bool isBlocker);
231 
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 
274 
275  /*
276  * Returns the particle id:s currently in contact with the sensor geometry
277  */
279 
280  /*
281  * Returns the particle id:s whose midpoint is currently _inside_ the sensor geometry
282  */
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  */
294 
295  /*
296  * Returns the number of particle-particle contacts in the sensor
297  */
299 
304 
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 
400 
402 
404 
406 
408 
410 
412 
414 
416 
418 
420 
422 
424 
427  agx::Physics::ParticleGeometryContactInstance contact,
428  agx::Physics::ParticleData& particleData,
429  agx::Physics::GeometryData& geometryData);
430 
432 
435  agx::Physics::ParticleGeometryContactInstance contact,
436  agx::Physics::ParticleData& particleData,
437  agx::Physics::GeometryData& geometryData);
438 
441  agx::Physics::ParticleGeometryContactInstance contact,
442  agx::Physics::ParticleData& particleData,
443  agx::Physics::GeometryData& geometryData);
444 
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*/
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#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
#define AGXPHYSICS_EXPORT
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
A contact between two geometries.
Definition: Contacts.h:270
The geometry representation used by the collision detection engine.
Definition: Geometry.h:93
virtual KeepContactPolicy impact(const agx::TimeStamp &, agxCollide::GeometryContact *)
Called upon impact event if getFilter() contain IMPACT.
virtual KeepContactPolicy contact(const agx::TimeStamp &, agxCollide::GeometryContact *)
Called upon contact event if getFilter() contain CONTACT.
virtual void separation(const agx::TimeStamp &, agxCollide::GeometryPair &)
Called upon separation event if getFilter() contain SEPARATION.
void setFilter(agxSDK::CollisionGroupFilter *filter)
virtual void contactCallback(agx::Physics::ParticleGeometryContactInstance contact, agx::Physics::ParticleData &particleData, agx::Physics::GeometryData &geometryData)
agxSDK::CollisionGroupFilter * m_filter
Definition: EventSensor.h:121
agxSDK::CollisionGroupFilter * getFilter()
const agxSDK::CollisionGroupFilter * getFilter() const
This is a sensor class that can be attached to an geometry in the simulation.
Definition: EventSensor.h:48
SensorEventVector getSensorEvents()
Returns the SensorEvents currently active in the Sensor in the order of execution priority.
agx::Vector< ParticleContactInformation > ParticleContactQueue
Definition: EventSensor.h:392
bool contactIsValid(agxCollide::GeometryContact *cd)
void setTriggerFlag(int flag)
Set/get the trigger flag to control when the events will be executed during the time-step of contact ...
agxCollide::Geometry * m_geometry
The coupled geometry of the sensor.
Definition: EventSensor.h:465
bool addSensorEvent(SensorEvent *sEvent, int priority=0)
Sets a sensor event to the sensor with a priority.
virtual void pre(const agx::TimeStamp &time) override
Called before a step is taken in the simulation Implement this method in the derived class to get cal...
void removeGroup(agx::UInt32 group)
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
SensorEvent * getSensorEventFromName(const agx::Name &name)
void tryTriggerParticleEventsOnContact(const agx::TimeStamp &t, agx::Physics::ParticleGeometryContactInstance contact, agx::Physics::ParticleData &particleData, agx::Physics::GeometryData &geometryData)
GeometryContactQueue m_geomContactQueue
Queues for the contacts.
Definition: EventSensor.h:471
int m_triggerFlag
Flag for when the events are supposed to be executed during the time-step.
Definition: EventSensor.h:456
agx::List< EventPrioHolder< SensorEventRef > > SensorEventList
Definition: EventSensor.h:351
void tryTriggerEventsOnContact(const agx::TimeStamp &t, agxCollide::GeometryContact *cd)
SensorEvent * getSensorEvent(const agx::Uuid &uuid)
Returns a sensor event with the given uuid if there exists one in the sensor.
bool hasGroup(agx::UInt32 group)
void refreshParticlesInsideSensorData()
void triggerEventsOnContact(const agx::TimeStamp &t, agxCollide::GeometryContact *cd)
agxCollide::Geometry * getGeometry()
Get the geometry of the sensor.
void removeGroup(const agx::Name &group)
bool hasOnlyReference()
Checks if the sensor keeps the only reference to its internal contact listener. Used in unittest.
agx::UInt32Vector getContactingParticleIds()
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
virtual void removeNotification() override
This function will execute when the sensor is removed form the simulation.
agx::HashSet< agx::RigidBody * > m_rigidBodiesContactingSensor
Holds pointers to the current rigid bodies (DEM Objects) in the sensor.
Definition: EventSensor.h:485
void setCollisionMask(int mask)
Set/get the collision mask for the sensor.
virtual void preCollide(const agx::TimeStamp &time) override
Called before collision detection is performed in the simulation Implement this method in the derived...
std::map< agx::Uuid, EventPrioHolder< SensorEventRef > > SensorEvents
Definition: EventSensor.h:348
bool particleContactIsValid(const agx::TimeStamp &t, agx::Physics::ParticleGeometryContactInstance contact, agx::Physics::ParticleData &particleData, agx::Physics::GeometryData &geometryData)
void executeSensorEvents(const agx::TimeStamp &t)
static agxCollide::GeometryPtrVector g_forbiddenGeometries
Definition: EventSensor.h:57
GeometryContactEventListenerRef m_contactListener
Listeners that are coupled with the sensor.
Definition: EventSensor.h:475
agx::RigidBodyPtrVector getRigidBodiesInsideSensor(bool onlyRigidBodyDEM=false)
virtual void post(const agx::TimeStamp &time) override
Called after a step is taken in the simulation Implement this method in the derived class to get call...
agx::Vector< agx::Physics::ParticlePairContactPtr > ParticlePairContactVector
Definition: EventSensor.h:50
ParticleContactQueue m_particleContactQueue
Definition: EventSensor.h:472
ParticleContactListenerRef m_particleContactListener
Definition: EventSensor.h:476
agx::Real getNumParticleParticleContactsInSensor()
agx::RigidBody * getContactingRigidBody(agxCollide::GeometryContact *cd) const
void refreshRigidBodiesInsideSensorData()
EventSensor(agxCollide::Geometry *geometry, int priority)
Constructor that takes the geometry to couple the sensor too.
EventSensor(agxCollide::Geometry *geometry)
Constructor that takes the geometry to couple the sensor too.
SensorEventList m_sensorEventList
Definition: EventSensor.h:352
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
bool containsSensorEvent(SensorEvent *sEvent)
Checks if a sensor event already exists in the sensor.
void triggerParticleEventsOnContact(const agx::TimeStamp &t, agx::Physics::ParticleGeometryContactInstance contact, agx::Physics::ParticleData &particleData, agx::Physics::GeometryData &geometryData)
agx::UInt32Vector getParticlesInsideSensor()
EventSensor()
Default constructor.
bool m_particleCollision
Flags for doing when there is a collision for a particle or geometry.
Definition: EventSensor.h:494
const ParticlePairContactVector & getParticlePairContactsInSensor()
Returns the current particle-particle contacts in the sensor geometry.
void setGeometry(agxCollide::Geometry *geometry)
Sets the geometry of the sensor.
void setEnableBlocker(bool isBlocker)
Set the sensor to block the contact from other sensors.
TriggerEventStages
Defines the event execution states when the SensorEvents are executed during the timestep of the dete...
Definition: EventSensor.h:63
@ SENSOR_PRE_STEP
Triggered in a PRE solve/integration step.
Definition: EventSensor.h:64
EventSensor(int priority)
This constructor takes the priority of the internal contact sensors.
bool hasComputedParticlePairContactsInSensor
Vector that stores particle pair contacts. Will only be computed if the user asks for it (getParticle...
Definition: EventSensor.h:498
agx::Real getNumParticlesInSensor() const
bool removeSensorEvent(SensorEvent *sEvent)
Removes a sensor event in the sensor.
int m_collisionMask
Get the collision mask for the sensor.
Definition: EventSensor.h:462
agx::Vector< ContactInformation > GeometryContactQueue
Definition: EventSensor.h:393
agx::Vector< agxControl::SensorEvent * > SensorEventVector
Definition: EventSensor.h:51
virtual void addNotification() override
This will execute when the sensor is inserted in the simulation.
agxCollide::Geometry * getGeometry() const
bool isForbiddenGeometry(agxCollide::Geometry *geom)
Check if the geometry is marked as forbidden by a sensor in this time step.
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
void updateSensorEvents(const agx::TimeStamp &t)
void addGroup(agx::UInt32 group)
agxSDK::CollisionGroupFilterRef m_filter
The collision group filter coupled with the ContactEventListener.
Definition: EventSensor.h:468
void signalGeometryContact(const agx::TimeStamp &t, agxCollide::GeometryContact *cd)
bool hasGroup(const agx::Name &group)
void addGroup(const agx::Name &group)
Functions for adding and removing collision groups.
SensorEvents m_sensorEvents
Definition: EventSensor.h:349
bool isForbiddenParticle(agx::UInt32 particle)
Check if the particle is marked as forbidden by a sensor in this time step.
void signalParticleContact(const agx::TimeStamp &t, agx::Physics::ParticleGeometryContactInstance contact, agx::Physics::ParticleData &particleData, agx::Physics::GeometryData &geometryData)
static agx::UInt32Vector g_forbiddenParticles
Definition: EventSensor.h:58
ParticlePairContactVector m_particlePairContacts
Definition: EventSensor.h:499
int m_priority
Priority of the sensor over all the other sensors.
Definition: EventSensor.h:459
virtual ~EventSensor()
Destructor.
A sensor event is an event triggered when the sensor is activated by a contact.
Definition: SensorEvent.h:49
An ExecuteFilter that is used to filter out contacts matching a specified criteria based on Collision...
Derive from this class to implement a listener for Collision events.
KeepContactPolicy
Defines whether contact should be kept or removed after contact/impact methods returns.
Derive from this class to implement a listener for simulation step events.
Representation of a name string.
Definition: Name.h:33
This class is for detecting and handling particle collisions with geometries in the simulation.
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:42
Vector containing 'raw' data.
Definition: agx/Vector.h:246
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define CALLABLE
Definition: macros.h:72
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
This namespace contains general operation and action classes which can be used to control a agxSDK::S...
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:66
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:40
double Real
Definition: Real.h:42
agx::Real TimeStamp
Definition: TimeStamp.h:26
ContactInformation(const agx::TimeStamp tin, agxCollide::GeometryContact *cdin)
Definition: EventSensor.h:361
agxCollide::GeometryContact * cd
Definition: EventSensor.h:367
Placeholder for information regarding the particle contacts.
Definition: EventSensor.h:376
agx::Physics::ParticleGeometryContactInstance contact
Definition: EventSensor.h:385
ParticleContactInformation(const agx::TimeStamp tIn, agx::Physics::ParticleGeometryContactInstance contactIn, agx::Physics::ParticleData &particleDataIn, agx::Physics::GeometryData &geometryDataIn)
Definition: EventSensor.h:377