AGX Dynamics  2.38.0.2
SensorOperations.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_SENSOROPERATIONS_H
18 #define AGXCONTROL_SENSOROPERATIONS_H
19 
20 #include <fstream>
21 #include <agx/Date.h>
22 #include <agx/String.h>
23 #include <agxData/Track.h>
24 #include <agxControl/SensorEvent.h>
25 #include <agxControl/EventSensor.h>
27 #include <agx/Journal.h>
28 #include <agxRender/Color.h>
29 
30 /*
31 This file defines some example sensor operations
32 */
33 namespace agxControl
34 {
37  {
38  public:
39  RemoveRigidBody(const agx::Name& name = agx::Name(), bool onlyRemoveEmittedBodies = false);
40 
41  void triggerEvent(const agx::TimeStamp& /*t*/, agxSDK::Simulation * simulation, agxCollide::GeometryContact *cd, agxControl::EventSensor * sensor) override;
42 
44 
45  protected:
46  virtual ~RemoveRigidBody();
47 
48  protected:
50  };
51 
54  {
55  public:
56  RemoveParticles(const agx::Name& name = agx::Name());
57 
58  void triggerParticleEvent(const agx::TimeStamp& /*t*/,
59  agxSDK::Simulation* /*simulation*/,
60  agx::Physics::ParticleGeometryContactInstance contact,
61  agx::Physics::ParticleData& particleData,
62  agx::Physics::GeometryData& /*geometryData*/,
63  agxControl::EventSensor* /*sensor*/) override;
64 
66 
67  protected:
68  virtual ~RemoveParticles();
69 
70  };
71 
74  {
75  public:
76  TagOperation(const agx::Name& name = agx::Name());
77 
78 
79  virtual void triggerEvent(const agx::TimeStamp& /*t*/, agxSDK::Simulation * /*simulation*/, agxCollide::GeometryContact * /*cd*/, agxControl::EventSensor* /*sensor*/) override
80  {
81  // Tag this operation as executed
82  m_isExecuted = true;
83  }
84 
85  virtual void triggerParticleEvent(const agx::TimeStamp& /*t*/,
86  agxSDK::Simulation* /*simulation*/,
87  agx::Physics::ParticleGeometryContactInstance /*contact*/,
88  agx::Physics::ParticleData& /*particleData*/,
89  agx::Physics::GeometryData& /*geometryData*/,
90  agxControl::EventSensor* /*sensor*/) override
91  {
92  m_isExecuted = true;
93  }
94 
96 
97  public:
98  bool isExecuted() { return m_isExecuted; }
99 
100  void setIsExecuted(bool executed) { m_isExecuted = executed; }
101 
102  protected:
104 
105  protected:
106  virtual ~TagOperation();
107 
108  };
109 
112  {
113  private:
114 
115  static agx::HashSet<agx::UInt32> s_coloredParticles;
116  static agx::TimeStamp s_lastTimeStep;
117 
118  public:
119  ColorParticles(const agx::Name& name = agx::Name(), const agx::Vec3& color = agx::Vec3(0.5, 0.5, 0.5), bool mixColors = false);
120 
121  virtual void triggerParticleEvent(const agx::TimeStamp& t,
122  agxSDK::Simulation* simulation,
123  agx::Physics::ParticleGeometryContactInstance contact,
124  agx::Physics::ParticleData& particleData,
125  agx::Physics::GeometryData& geometryData,
126  agxControl::EventSensor* sensor) override;
127 
128  bool getEnableMixColors(){ return m_mixColors; }
129  void setEnableMixColors(bool mixColors){ m_mixColors = mixColors; }
130 
132 
133  public:
134  agx::Vec3& getColor(){ return m_color; }
135 
136  protected:
139 
140  protected:
141  virtual ~ColorParticles();
142 
143  };
144 
147  {
148  public:
149 
150  ParticleContactDumper(const agx::Name& name = agx::Name());
151  ParticleContactDumper(const agx::Name& name, const agx::Name& filepath, agx::UInt32 samplingStride = 1, bool exportOnce = false);
152 
153  void createFileName(const agx::Name& filePath);
154 
155  // This function appends a date stamp at the end of the filename
156  agx::String generateDateStamp();
157 
158  virtual void triggerParticleEvent(const agx::TimeStamp& t,
159  agxSDK::Simulation* /*simulation*/,
160  agx::Physics::ParticleGeometryContactInstance contact,
161  agx::Physics::ParticleData& particleData,
162  agx::Physics::GeometryData& /*geometryData*/,
163  agxControl::EventSensor* /*sensor*/) override;
164 
165  agx::Name& getFilePath(){ return m_filepath; }
166 
167  virtual void update(const agx::TimeStamp& t) override;
168 
169  void postContactHandling(agxSDK::Simulation* simulation, EventSensor* sensor) override;
170 
172 
173  protected:
175  std::ofstream m_outStream;
183 
184  protected:
185  virtual ~ParticleContactDumper();
186 
187  };
188 
189  // Switches the material of the particle in the supplied contact
191  {
192  public:
193 
195  ReplaceParticleMaterialOperation(const agx::Name& name, const agx::Name& materialName);
196 
197  virtual void triggerParticleEvent(const agx::TimeStamp& t,
198  agxSDK::Simulation* simulation,
199  agx::Physics::ParticleGeometryContactInstance contact,
200  agx::Physics::ParticleData& particleData,
201  agx::Physics::GeometryData& geometryData,
202  agxControl::EventSensor* sensor) override;
203 
204  void triggerEvent( const agx::TimeStamp& t,
205  agxSDK::Simulation * simulation,
207  agxControl::EventSensor* sensor ) override;
208 
210 
211  void setMaterialName(const agx::String& material);
212  agx::Name getMaterialName();
213 
215  // Variables
217  protected:
219 
220  protected:
222 
223  };
224 
226  {
227  m_materialName = material;
228  }
229 
231  {
232  return m_materialName;
233  }
234 
235 
240  {
242  friend class RemoveParticles;
243  public:
244 
247  {
249  UNFREEZE_PARTICLES
250  };
251 
252  MakeParticleKinematicOperation(const agx::Name& name = agx::Name(), ParticleFreezeMode mode = FREEZE_PARTILCES);
253 
254  virtual void triggerParticleEvent(const agx::TimeStamp& t,
255  agxSDK::Simulation* simulation,
256  agx::Physics::ParticleGeometryContactInstance contact,
257  agx::Physics::ParticleData& particleData,
258  agx::Physics::GeometryData& geometryData,
259  agxControl::EventSensor* sensor) override;
260 
261  void triggerEvent( const agx::TimeStamp& /*t*/,
262  agxSDK::Simulation * simulation,
264  agxControl::EventSensor * sensor ) override;
265 
267 
268  protected:
270 
272  // Variables
274  int m_mode;
275  };
276 
282  {
283  public:
284  ResetParticleContactEnergyOperation( const agx::Name& name = agx::Name(), agx::Real resetEnergy=0 );
285 
286  void postContactHandling( agxSDK::Simulation* simulation, EventSensor* sensor ) override;
287 
289 
290  protected:
292 
294  // Variables
297  };
298 
299 
305  {
307  friend class RemoveParticles;
308  public:
309 
311  agx::Real sleepSpeedThreshold = 1.0,
312  bool colorOnSleep = false,
313  agx::Vec4f sleepColor = agxRender::Color::Yellow() );
314 
315  virtual void triggerParticleEvent( const agx::TimeStamp& t,
316  agxSDK::Simulation* simulation,
317  agx::Physics::ParticleGeometryContactInstance contact,
318  agx::Physics::ParticleData& particleData,
319  agx::Physics::GeometryData& geometryData,
320  agxControl::EventSensor* sensor ) override;
321 
323 
324  protected:
326 
328  // Variables
333  };
334 
339  {
340  public:
341 
342  SetVelocityOperation(const agx::Name& name = agx::Name());
343  SetVelocityOperation(const agx::Name& name, const agx::Vec3& velocity, const agx::Vec3& angularVelocity, bool setInLocalFrame = false);
344 
345  virtual void triggerParticleEvent(const agx::TimeStamp& t,
346  agxSDK::Simulation* simulation,
347  agx::Physics::ParticleGeometryContactInstance contact,
348  agx::Physics::ParticleData& particleData,
349  agx::Physics::GeometryData& geometryData,
350  agxControl::EventSensor* sensor) override;
351 
352  virtual void triggerEvent(const agx::TimeStamp& t,
353  agxSDK::Simulation * simulation,
355  agxControl::EventSensor* sensor) override;
356 
357  void setVelocity(const agx::Vec3& velocity);
358  const agx::Vec3& getVelocity() const;
359 
360  void setAngularVelocity(const agx::Vec3& angularVelocity);
361  const agx::Vec3& getAngularVelocity() const;
362 
364 
365  protected:
366  virtual ~SetVelocityOperation();
367 
369  // Variables
371  protected:
376  };
377 
379  {
380  m_velocity = velocity;
381  }
382 
384  {
385  return m_velocity;
386  }
387 
389  {
390  m_angularVelocity = angularVelocity;
391  }
392 
394  {
395  return m_angularVelocity;
396  }
397 
402  {
403  public:
404 
406 
407  virtual void triggerParticleEvent(const agx::TimeStamp& t,
408  agxSDK::Simulation* simulation,
409  agx::Physics::ParticleGeometryContactInstance contact,
410  agx::Physics::ParticleData& particleData,
411  agx::Physics::GeometryData& geometryData,
412  agxControl::EventSensor* sensor) override;
413 
414  virtual void triggerEvent(const agx::TimeStamp& t,
415  agxSDK::Simulation * simulation,
417  agxControl::EventSensor* sensor) override;
418 
419  virtual void update(const agx::TimeStamp& t) override;
420 
421  agx::Physics::ParticlePtrVector getParticlesInContact() const;
422 
424 
425  protected:
426  virtual ~StoreParticlesOperation();
427 
428  protected:
430  };
431 
433 
438  {
439  public:
440 
441  typedef std::pair<agx::TimeStamp, agx::Index> TimeParticleIdPair;
443 
445  EventSensor * target,
446  agx::Real teleportTimeDilation = 0,
447  bool cloneTeleportation = false,
448  agx::Frame * sourceoffset = nullptr,
449  agx::Frame * targetoffset = nullptr);
450 
451  void addNotification(EventSensor * sensor) override;
452 
453  void triggerEvent(const agx::TimeStamp& t,
454  agxSDK::Simulation * simulation,
456  agxControl::EventSensor* sensor) override;
457 
458  void triggerParticleEvent(const agx::TimeStamp& t,
459  agxSDK::Simulation* /*simulation*/,
460  agx::Physics::ParticleGeometryContactInstance contact,
461  agx::Physics::ParticleData& particleData,
462  agx::Physics::GeometryData& /*geometryData*/,
463  agxControl::EventSensor* sensor) override;
464 
465  void postContactHandling(agxSDK::Simulation *simulation, EventSensor * sensor) override;
466 
468 
469  protected:
471  void init();
472 
473  void teleportParticles(const agx::TimeStamp& t, agxControl::EventSensor * sensor, agx::Physics::GranularBodySystem * system);
474 
476  // Variables
478  protected:
487  };
488 }
489 
490 #endif
agxControl::RemoveRigidBody::m_onlyRemoveEmittedBodies
bool m_onlyRemoveEmittedBodies
Definition: SensorOperations.h:49
agxControl::SetVelocityOperation::getAngularVelocity
const agx::Vec3 & getAngularVelocity() const
Definition: SensorOperations.h:393
agxControl::ReplaceParticleMaterialOperation::setMaterialName
void setMaterialName(const agx::String &material)
Definition: SensorOperations.h:225
agxControl::MakeParticleKinematicOperation::FREEZE_PARTILCES
@ FREEZE_PARTILCES
Definition: SensorOperations.h:248
agxControl::ParticleContactDumper::m_filepath
agx::Name m_filepath
Definition: SensorOperations.h:176
agxControl::SetVelocityOperation::m_angularVelocity
agx::Vec3 m_angularVelocity
Definition: SensorOperations.h:373
agxControl::ParticleContactDumper::m_completeFilePath
agx::Name m_completeFilePath
Definition: SensorOperations.h:177
agxControl::ParticleContactDumper::m_outStream
std::ofstream m_outStream
Definition: SensorOperations.h:175
agx::Real
double Real
Definition: Real.h:42
agx::List< TimeParticleIdPair >
agxControl::RemoveRigidBody
This operation removes the rigid body given by the geometry in the contact supplied by the sensor.
Definition: SensorOperations.h:36
agxControl
This namespace contains general operation and action classes which can be used to control a agxSDK::S...
agxControl::TeleportToSensorOperation::m_teleportTimeDilation
agx::Real m_teleportTimeDilation
Definition: SensorOperations.h:479
agxControl::ParticleContactDumper::getFilePath
agx::Name & getFilePath()
Definition: SensorOperations.h:165
agxControl::TagOperation::isExecuted
bool isExecuted()
Definition: SensorOperations.h:98
agxControl::ParticleContactDumper::m_samplingStride
agx::UInt32 m_samplingStride
Definition: SensorOperations.h:179
agxControl::ColorParticles::getColor
agx::Vec3 & getColor()
Definition: SensorOperations.h:134
CALLABLE
#define CALLABLE
Definition: macros.h:72
agxControl::SensorOperation
A specific type of sensor event that triggers a change in the simulation when activated by a contact.
Definition: SensorEvent.h:117
agxControl::MakeParticleKinematicOperation::ParticleFreezeMode
ParticleFreezeMode
Enum describing what mode the operation should be in. If it should freeze or unfreeze particles.
Definition: SensorOperations.h:246
agxControl::ColorParticles::m_color
agx::Vec3 m_color
Definition: SensorOperations.h:137
agxControl::SetVelocityOperation::getVelocity
const agx::Vec3 & getVelocity() const
Definition: SensorOperations.h:383
agxControl::ParticleContactDumper
This operation dumps particle contact information to a file.
Definition: SensorOperations.h:146
agxControl::TeleportToSensorOperation::ParticleTeleportQueue
agx::List< TimeParticleIdPair > ParticleTeleportQueue
Definition: SensorOperations.h:442
agxControl::ParticleContactDumper::m_handledParticleIds
agx::HashTable< agx::Index, agx::Bool > m_handledParticleIds
Definition: SensorOperations.h:171
agxControl::TagOperation::triggerEvent
virtual void triggerEvent(const agx::TimeStamp &, agxSDK::Simulation *, agxCollide::GeometryContact *, agxControl::EventSensor *) override
Triggers the sensor event by passing contact information.
Definition: SensorOperations.h:79
agxControl::SetVelocityOperation
This sensor operation class sets a speed to the object that collides with the sensor,...
Definition: SensorOperations.h:338
agx::Vec4T
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
agxControl::StoreParticlesOperation::m_particlesInContact
agx::Physics::ParticlePtrVector m_particlesInContact
Definition: SensorOperations.h:429
Track.h
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< ParticlePtr >
agxControl::SetVelocityOperation::m_setInLocalFrame
bool m_setInLocalFrame
Definition: SensorOperations.h:374
AGXPHYSICS_EXPORT
#define AGXPHYSICS_EXPORT
agxControl::TagOperation::setIsExecuted
void setIsExecuted(bool executed)
Definition: SensorOperations.h:100
agxControl::ParticleContactDumper::m_exportOnlyOnce
bool m_exportOnlyOnce
Definition: SensorOperations.h:182
agxControl::EventSensor
This is a sensor class that can be attached to an geometry in the simulation.
Definition: EventSensor.h:47
agxControl::SetVelocityOperation::m_velocity
agx::Vec3 m_velocity
Definition: SensorOperations.h:372
agxControl::TeleportToSensorOperation
This sensor operation can teleport contacting particles to a target sensor geometry with arbitrary ti...
Definition: SensorOperations.h:437
agxControl::ReplaceParticleMaterialOperation
Definition: SensorOperations.h:190
agx::ref_ptr< Frame >
agxControl::TagOperation::m_isExecuted
bool m_isExecuted
Definition: SensorOperations.h:103
agxControl::ParticleContactDumper::m_framesSinceLastSampling
agx::UInt32 m_framesSinceLastSampling
Definition: SensorOperations.h:180
agxSDK::Simulation
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:125
agxControl::SetVelocityOperation::setAngularVelocity
void setAngularVelocity(const agx::Vec3 &angularVelocity)
Definition: SensorOperations.h:388
agxControl::TeleportToSensorOperation::m_targetOffsetFrame
agx::FrameRef m_targetOffsetFrame
Definition: SensorOperations.h:484
agxControl::ParticleKinematicSleepOperation::m_sleepColor
agx::Vec4f m_sleepColor
Definition: SensorOperations.h:332
agxControl::ColorParticles::m_mixColors
bool m_mixColors
Definition: SensorOperations.h:138
Journal.h
agxControl::TagOperation
This operation can be used to know when a sensor has been activated by a contact.
Definition: SensorOperations.h:73
SensorEvent.h
agxControl::ReplaceParticleMaterialOperation::m_materialName
agx::Name m_materialName
Definition: SensorOperations.h:218
Date.h
agx::LinearProbingHashTable
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: LinearProbingHashTable.h:926
agxControl::ParticleKinematicSleepOperation::m_sleepSpeedThreshold
agx::Real m_sleepSpeedThreshold
Definition: SensorOperations.h:330
agx::Physics::GranularBodySystem
Definition: GranularBodySystem.h:32
agx::Name
Representation of a name string.
Definition: Name.h:32
agxControl::RemoveParticles
This operations removes all the particles in the contact supplied by the sensor.
Definition: SensorOperations.h:53
agxControl::TeleportToSensorOperation::m_shouldCloneTeleport
bool m_shouldCloneTeleport
Definition: SensorOperations.h:483
agx::HashSet< agx::UInt32 >
agxControl::ResetParticleContactEnergyOperation
This operation resets accumulated impact energy to a set value for particles inside the sensor if the...
Definition: SensorOperations.h:281
agxControl::SetVelocityOperation::m_localFrame
agx::FrameRef m_localFrame
Definition: SensorOperations.h:375
agxControl::TeleportToSensorOperation::m_targetSensor
agxControl::EventSensor * m_targetSensor
Definition: SensorOperations.h:480
agxControl::ResetParticleContactEnergyOperation::m_resetEnergy
agx::Real m_resetEnergy
Definition: SensorOperations.h:296
agxControl::TeleportToSensorOperation::m_sourceOffsetFrame
agx::FrameRef m_sourceOffsetFrame
Definition: SensorOperations.h:485
agxControl::ParticleContactDumper::m_recordFrame
bool m_recordFrame
Definition: SensorOperations.h:181
agxControl::StoreParticlesOperation
This sensor operation class store particle id's of particles that are in contact with the sensor.
Definition: SensorOperations.h:401
agxControl::ParticleContactDumper::m_fileFormat
agx::Name m_fileFormat
Definition: SensorOperations.h:178
agxControl::TeleportToSensorOperation::m_particleSpawnQueue
ParticleTeleportQueue m_particleSpawnQueue
Definition: SensorOperations.h:481
agxCollide::GeometryContact
A contact between two geometries.
Definition: Contacts.h:270
agxControl::TeleportToSensorOperation::m_particleTeleportStorage
agxData::EntityStorageRef m_particleTeleportStorage
Definition: SensorOperations.h:482
agx::TimeStamp
agx::Real TimeStamp
Definition: TimeStamp.h:26
agxControl::MakeParticleKinematicOperation
This operation applies a "kinematic state" to particles that collide with the sensor.
Definition: SensorOperations.h:239
agxControl::TagOperation::triggerParticleEvent
virtual void triggerParticleEvent(const agx::TimeStamp &, agxSDK::Simulation *, agx::Physics::ParticleGeometryContactInstance, agx::Physics::ParticleData &, agx::Physics::GeometryData &, agxControl::EventSensor *) override
Triggers the sensor event by passing particle contact information.
Definition: SensorOperations.h:85
agxControl::ReplaceParticleMaterialOperation::getMaterialName
agx::Name getMaterialName()
Definition: SensorOperations.h:230
agxControl::TeleportToSensorOperation::m_previousContactingParticleIds
agx::HashSet< agx::UInt32 > m_previousContactingParticleIds
Definition: SensorOperations.h:486
String.h
agx::AgXString< std::string >
agxControl::MakeParticleKinematicOperation::m_mode
int m_mode
Definition: SensorOperations.h:274
agxControl::SetVelocityOperation::setVelocity
void setVelocity(const agx::Vec3 &velocity)
Definition: SensorOperations.h:378
agx::init
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API.
Color.h
agxControl::StoreParticlesOperation::getParticlesInContact
agx::Physics::ParticlePtrVector getParticlesInContact() const
Definition: SensorOperations.h:432
agx::Vec3T< Real >
agxControl::ColorParticles::getEnableMixColors
bool getEnableMixColors()
Definition: SensorOperations.h:128
agxControl::ColorParticles
This operation changes the color of the particles given in the contact.
Definition: SensorOperations.h:111
agxRender::Color::Yellow
static agxRender::Color Yellow()
Definition: Color.h:193
agxControl::ParticleKinematicSleepOperation
This operation applies a kinematic "sleep state" to particles inside the sensor that are under a spec...
Definition: SensorOperations.h:304
EventSensor.h
agx::UInt32
uint32_t UInt32
Definition: Integer.h:32
agxControl::TeleportToSensorOperation::TimeParticleIdPair
std::pair< agx::TimeStamp, agx::Index > TimeParticleIdPair
Definition: SensorOperations.h:441
agxControl::ColorParticles::setEnableMixColors
void setEnableMixColors(bool mixColors)
Definition: SensorOperations.h:129
agx::Frame
The object defining a frame of reference and providing transformations operations.
Definition: Frame.h:67
agxControl::ParticleKinematicSleepOperation::m_colorOnSleep
bool m_colorOnSleep
Definition: SensorOperations.h:331
GranularBodySystem.h
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58