AGX Dynamics  2.37.2.0
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 
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;
349  SensorEvents m_sensorEvents;
350 
352  SensorEventList m_sensorEventList;
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 
471  GeometryContactQueue m_geomContactQueue;
472  ParticleContactQueue m_particleContactQueue;
473 
477 
480 
483 
486 
489 
492 
496 
499  ParticlePairContactVector m_particlePairContacts;
500  };
501 }
502 
503 #endif /*AGXMODEL_EVENTSENSOR_H*/
TriggerEventStages
Defines the event execution states when the SensorEvents are executed during the timestep of the dete...
Definition: EventSensor.h:62
This class is for detecting and handling particle collisions with geometries in the simulation...
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
Derive from this class to implement a listener for Collision events.
Derive from this class to implement a listener for simulation step events.
agx::Vector< agxControl::SensorEvent *> SensorEventVector
Definition: EventSensor.h:51
Placeholder for information regarding the particle contacts.
Definition: EventSensor.h:375
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:66
bool m_particleCollision
Flags for doing when there is a collision for a particle or geometry.
Definition: EventSensor.h:494
static agxCollide::GeometryPtrVector g_forbiddenGeometries
Definition: EventSensor.h:57
A contact between two geometries.
Definition: Contacts.h:270
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
#define AGXPHYSICS_EXPORT
agxCollide::GeometryContact * cd
Definition: EventSensor.h:367
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
int m_triggerFlag
Flag for when the events are supposed to be executed during the time-step.
Definition: EventSensor.h:456
bool hasComputedParticlePairContactsInSensor
Vector that stores particle pair contacts. Will only be computed if the user asks for it (getParticle...
Definition: EventSensor.h:498
Representation of a name string.
Definition: Name.h:32
agx::Real TimeStamp
Definition: TimeStamp.h:26
ParticleContactInformation(const agx::TimeStamp tIn, agx::Physics::ParticleGeometryContactInstance contactIn, agx::Physics::ParticleData &particleDataIn, agx::Physics::GeometryData &geometryDataIn)
Definition: EventSensor.h:377
This is a sensor class that can be attached to an geometry in the simulation.
Definition: EventSensor.h:47
SensorEventList m_sensorEventList
Definition: EventSensor.h:352
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
AGXPHYSICS_EXPORT void addGroup(const agx::RigidBody *body, unsigned id)
For the specified body, add the collision group ID to its geometries.
GeometryContactQueue m_geomContactQueue
Queues for the contacts.
Definition: EventSensor.h:471
agx::Vector< agx::Physics::ParticlePairContactPtr > ParticlePairContactVector
Definition: EventSensor.h:50
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
ParticleContactListenerRef m_particleContactListener
Definition: EventSensor.h:476
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
ParticleContactQueue m_particleContactQueue
Definition: EventSensor.h:472
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
agxSDK::CollisionGroupFilterRef m_filter
The collision group filter coupled with the ContactEventListener.
Definition: EventSensor.h:468
std::map< agx::Uuid, EventPrioHolder< SensorEventRef > > SensorEvents
Definition: EventSensor.h:348
static agx::UInt32Vector g_forbiddenParticles
Definition: EventSensor.h:58
KeepContactPolicy
Defines whether contact should be kept or removed after contact/impact methods returns.
agx::Physics::ParticleGeometryContactInstance contact
Definition: EventSensor.h:385
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
This namespace contains general operation and action classes which can be used to control a agxSDK::S...
ContactInformation(const agx::TimeStamp tin, agxCollide::GeometryContact *cdin)
Definition: EventSensor.h:361
#define CALLABLE
Definition: Object.h:73
agx::List< EventPrioHolder< SensorEventRef > > SensorEventList
Definition: EventSensor.h:351
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:41
#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
agxCollide::Geometry * m_geometry
The coupled geometry of the sensor.
Definition: EventSensor.h:465
int m_collisionMask
Get the collision mask for the sensor.
Definition: EventSensor.h:462
AGXPHYSICS_EXPORT void removeGroup(const agx::RigidBody *body, unsigned id)
For the specified body, remove the collision group ID to its geometries.
An ExecuteFilter that is used to filter out contacts matching a specified criteria based on Collision...
agx::Vector< ParticleContactInformation > ParticleContactQueue
Definition: EventSensor.h:392
agx::HashSet< agx::RigidBody * > m_rigidBodiesContactingSensor
Holds pointers to the current rigid bodies (DEM Objects) in the sensor.
Definition: EventSensor.h:485
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
agxSDK::CollisionGroupFilter * m_filter
Definition: EventSensor.h:121
Triggered in a PRE solve/integration step.
Definition: EventSensor.h:64
A sensor event is an event triggered when the sensor is activated by a contact.
Definition: SensorEvent.h:48
ParticlePairContactVector m_particlePairContacts
Definition: EventSensor.h:499
int m_priority
Priority of the sensor over all the other sensors.
Definition: EventSensor.h:459
SensorEvents m_sensorEvents
Definition: EventSensor.h:349
double Real
Definition: Real.h:42
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
agx::Vector< ContactInformation > GeometryContactQueue
Definition: EventSensor.h:393
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
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
GeometryContactEventListenerRef m_contactListener
Listeners that are coupled with the sensor.
Definition: EventSensor.h:475