AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EventSensor.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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  {
66  SENSOR_LAST_STEP
67  };
68 
69  protected:
70 
73  /*
74  * This is an internal class handling the geometry contact triggers of the simulation
75  */
77  {
78  public:
79 
81 
84  virtual void separation(const agx::TimeStamp& , agxCollide::GeometryPair&);
85 
86  friend class EventSensor;
87 
88  protected:
90 
92  };
93 
96 
97  /*
98  * This is an internal class handling the geometry contact triggers of the simulation
99  */
101  {
102  public:
103 
105 
106  void setFilter(agxSDK::CollisionGroupFilter* filter);
107 
108  agxSDK::CollisionGroupFilter* getFilter();
109  const agxSDK::CollisionGroupFilter* getFilter() const;
110 
111  virtual void contactCallback(agx::Physics::ParticleGeometryContactInstance contact,
112  agx::Physics::ParticleData& particleData,
113  agx::Physics::GeometryData& geometryData);
114 
115  friend class EventSensor;
116 
117  protected:
118  virtual ~ParticleContactListener();
119 
120  protected:
123  };
124 
125  public:
126 
128  EventSensor();
129 
134  EventSensor(int priority);
135 
141 
147  EventSensor(agxCollide::Geometry* geometry, int priority);
148 
152  void addGroup(const agx::Name& group);
153  void addGroup(agx::UInt32 group);
154  void removeGroup(const agx::Name& group);
155  void removeGroup(agx::UInt32 group);
156  void removeAllGroups();
157  bool hasGroup(const agx::Name& group);
158  bool hasGroup(agx::UInt32 group);
159 
165  bool addSensorEvent(SensorEvent* sEvent, int priority = 0);
166 
171  bool removeSensorEvent(SensorEvent* sEvent);
172 
177  bool containsSensorEvent(SensorEvent *sEvent);
178 
183  SensorEventVector getSensorEvents();
184 
188  void setGeometry(agxCollide::Geometry* geometry);
189 
193  agxCollide::Geometry* getGeometry();
194  agxCollide::Geometry* getGeometry() const;
195 
199  void setTriggerFlag(int flag);
200 
204  int getTriggerFlag();
205 
209  void setCollisionMask(int mask);
210 
214  int getCollisionMask();
215 
219  bool isForbiddenGeometry(agxCollide::Geometry* geom);
220 
224  bool isForbiddenParticle(agx::UInt32 particle);
225 
231  void setEnableBlocker(bool isBlocker);
232 
236  bool getEnableBlocker();
237 
243  virtual void preCollide(const agx::TimeStamp& time) override;
244 
250  virtual void pre(const agx::TimeStamp& time) override;
251 
257  virtual void post(const agx::TimeStamp& time) override;
258 
264  virtual void last(const agx::TimeStamp& time) override;
265 
270  virtual void addNotification() override;
271 
276  virtual void removeNotification() override;
277 
281  const ParticlePairContactVector& getParticlePairContactsInSensor();
282 
283  /*
284  * Returns the particle id:s currently in contact with the sensor geometry
285  */
286  agx::UInt32Vector getContactingParticleIds();
287 
288  /*
289  * Returns the particle id:s whose midpoint is currently _inside_ the sensor geometry
290  */
291  agx::UInt32Vector getParticlesInsideSensor();
292 
293  /*
294  * Returns a vector containing RigidBodies inside the sensor volume
295  */
296  agx::RigidBodyPtrVector getRigidBodiesInsideSensor(bool onlyRigidBodyDEM = false);
297 
298  /*
299  * Returns the number of particles currently contacting the sensor
300  */
301  agx::Real getNumParticlesInSensor() const;
302 
303  /*
304  * Returns the number of particle-particle contacts in the sensor
305  */
306  agx::Real getNumParticleParticleContactsInSensor();
307 
311  SensorEvent* getSensorEvent(const agx::Uuid& uuid);
312 
314  bool hasOnlyReference();
315 
317 
319  // Dummy method to allow EventSensor to pass makeCallable compilation.
320  void setRealDummyMethod(agx::Real /*inVal*/) { }
322 
323  public:
324 
325  template <typename T>
327  {
328  public:
329  EventPrioHolder( ): m_priority( 0 ) {}
330  EventPrioHolder( T obj, int priority ) : m_obj( obj ), m_priority( priority ) {}
331  inline int priority() const {
332  return m_priority;
333  }
334  inline operator T () {
335  return m_obj;
336  }
337  inline operator const T () const {
338  return m_obj;
339  }
340  inline operator int () const {
341  return m_priority;
342  }
343  inline T sensorEvent() {
344  return m_obj;
345  }
346  inline const T sensorEvent() const {
347  return m_obj;
348  }
349 
350 
351  protected:
352  T m_obj;
354  };
355 
356  typedef std::map<agx::Uuid, EventPrioHolder<SensorEventRef> > SensorEvents;
358 
361 
362  protected:
363 
364  /*
365  * Placeholder for information regarding the geometry contacts
366  */
368  {
370  :t(tin),cd(cdin)
371  {
372  }
373 
376  private:
377  ContactInformation& operator=(const ContactInformation&);
378  };
379 
384  {
386  agx::Physics::ParticleGeometryContactInstance contactIn,
387  agx::Physics::ParticleData& particleDataIn,
388  agx::Physics::GeometryData& geometryDataIn)
389  :t(tIn),contact(contactIn), particleData(particleDataIn), geometryData(geometryDataIn)
390  {
391  }
393  agx::Physics::ParticleGeometryContactInstance contact;
394  agx::Physics::ParticleData& particleData;
395  agx::Physics::GeometryData& geometryData;
396  private:
398  };
399 
402 
403  protected:
404 
405  void init();
406 
407  void constructSortedEventList();
408 
409  SensorEvent* getSensorEventFromName(const agx::Name& name);
410 
411  bool contactIsValid(agxCollide::GeometryContact* cd);
412 
413  void clearContactInformation();
414 
415  void refreshParticlesInsideSensorData();
416 
417  void refreshRigidBodiesInsideSensorData();
418 
419  void updateSensorEvents(const agx::TimeStamp& t);
420 
421  void executeSensorEvents(const agx::TimeStamp& t);
422 
423  void triggerContactEvents();
424 
425  void triggerEvents();
426 
427  void triggerPostContactHandling();
428 
429  void triggerEventAddNotification();
430 
431  agx::RigidBody * getContactingRigidBody(agxCollide::GeometryContact* cd) const;
432 
433  void signalGeometryContact(const agx::TimeStamp& t, agxCollide::GeometryContact* cd);
434  void signalParticleContact(const agx::TimeStamp& t,
435  agx::Physics::ParticleGeometryContactInstance contact,
436  agx::Physics::ParticleData& particleData,
437  agx::Physics::GeometryData& geometryData);
438 
439  void triggerParticleEvents();
440 
441  void tryTriggerEventsOnContact(const agx::TimeStamp& t, agxCollide::GeometryContact* cd);
442  void tryTriggerParticleEventsOnContact( const agx::TimeStamp& t,
443  agx::Physics::ParticleGeometryContactInstance contact,
444  agx::Physics::ParticleData& particleData,
445  agx::Physics::GeometryData& geometryData);
446 
447  void triggerEventsOnContact(const agx::TimeStamp& t, agxCollide::GeometryContact* cd);
448  void triggerParticleEventsOnContact( const agx::TimeStamp& t,
449  agx::Physics::ParticleGeometryContactInstance contact,
450  agx::Physics::ParticleData& particleData,
451  agx::Physics::GeometryData& geometryData);
452 
453  bool particleContactIsValid( const agx::TimeStamp& t,
454  agx::Physics::ParticleGeometryContactInstance contact,
455  agx::Physics::ParticleData& particleData,
456  agx::Physics::GeometryData& geometryData);
457 
458  protected:
459 
461  virtual ~EventSensor();
462 
465 
468 
471 
474 
477 
481 
485 
488 
491 
494 
497 
500 
504 
508  };
509 }
510 
511 #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.
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:383
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:63
bool m_particleCollision
Flags for doing when there is a collision for a particle or geometry.
Definition: EventSensor.h:502
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:499
#define AGXPHYSICS_EXPORT
agxCollide::GeometryContact * cd
Definition: EventSensor.h:375
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
int m_triggerFlag
Flag for when the events are supposed to be executed during the time-step.
Definition: EventSensor.h:464
bool hasComputedParticlePairContactsInSensor
Vector that stores particle pair contacts. Will only be computed if the user asks for it (getParticle...
Definition: EventSensor.h:506
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:385
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:360
The geometry representation used by the collision detection engine.
Definition: Geometry.h:88
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:479
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:484
ParticleContactQueue m_particleContactQueue
Definition: EventSensor.h:480
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:496
agxSDK::CollisionGroupFilterRef m_filter
The collision group filter coupled with the ContactEventListener.
Definition: EventSensor.h:476
std::map< agx::Uuid, EventPrioHolder< SensorEventRef > > SensorEvents
Definition: EventSensor.h:356
static agx::UInt32Vector g_forbiddenParticles
Definition: EventSensor.h:58
KeepContactPolicy
Defines whether contact should be kept or removed after contact/impact methods returns.
typedef void(GLAD_API_PTR *GLDEBUGPROC)(GLenum source
agx::Physics::ParticleGeometryContactInstance contact
Definition: EventSensor.h:393
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:52
ContactInformation(const agx::TimeStamp tin, agxCollide::GeometryContact *cdin)
Definition: EventSensor.h:369
#define CALLABLE
Definition: Object.h:76
agx::VectorPOD< RigidBodyPtr > RigidBodyPtrVector
agx::List< EventPrioHolder< SensorEventRef > > SensorEventList
Definition: EventSensor.h:359
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:206
agxCollide::Geometry * m_geometry
The coupled geometry of the sensor.
Definition: EventSensor.h:473
int m_collisionMask
Get the collision mask for the sensor.
Definition: EventSensor.h:470
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:400
agx::HashSet< agx::RigidBody * > m_rigidBodiesContactingSensor
Holds pointers to the current rigid bodies (DEM Objects) in the sensor.
Definition: EventSensor.h:493
agx::HashSet< agx::UInt32 > m_particlesInSensor
Holds a set of the current particle id:s that are present in the sensor.
Definition: EventSensor.h:487
agxSDK::CollisionGroupFilter * m_filter
Definition: EventSensor.h:122
AGX_DECLARE_VECTOR_TYPES(EventSensor)
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:507
int m_priority
Priority of the sensor over all the other sensors.
Definition: EventSensor.h:467
SensorEvents m_sensorEvents
Definition: EventSensor.h:357
double Real
Definition: Real.h:42
agx::Vector< ContactInformation > GeometryContactQueue
Definition: EventSensor.h:401
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:101
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:100
agx::HashSet< agx::UInt32 > m_particlesInsideSensor
Holds a set of the current particle id:s whose midpoint is inside the sensor.
Definition: EventSensor.h:490
GeometryContactEventListenerRef m_contactListener
Listeners that are coupled with the sensor.
Definition: EventSensor.h:483