AGX Dynamics  2.37.2.0
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  {
284  friend class RemoveParticles;
285  public:
286 
288  agx::Real sleepSpeedThreshold = 1.0,
289  bool colorOnSleep = false,
290  agx::Vec4f sleepColor = agxRender::Color::Yellow() );
291 
292  virtual void triggerParticleEvent( const agx::TimeStamp& t,
293  agxSDK::Simulation* simulation,
294  agx::Physics::ParticleGeometryContactInstance contact,
295  agx::Physics::ParticleData& particleData,
296  agx::Physics::GeometryData& geometryData,
297  agxControl::EventSensor* sensor ) override;
298 
300 
301  protected:
303 
305  // Variables
310  };
311 
316  {
317  public:
318 
319  SetVelocityOperation(const agx::Name& name = agx::Name());
320  SetVelocityOperation(const agx::Name& name, const agx::Vec3& velocity, const agx::Vec3& angularVelocity, bool setInLocalFrame = false);
321 
322  virtual void triggerParticleEvent(const agx::TimeStamp& t,
323  agxSDK::Simulation* simulation,
324  agx::Physics::ParticleGeometryContactInstance contact,
325  agx::Physics::ParticleData& particleData,
326  agx::Physics::GeometryData& geometryData,
327  agxControl::EventSensor* sensor) override;
328 
329  virtual void triggerEvent(const agx::TimeStamp& t,
330  agxSDK::Simulation * simulation,
332  agxControl::EventSensor* sensor) override;
333 
334  void setVelocity(const agx::Vec3& velocity);
335  const agx::Vec3& getVelocity() const;
336 
337  void setAngularVelocity(const agx::Vec3& angularVelocity);
338  const agx::Vec3& getAngularVelocity() const;
339 
341 
342  protected:
343  virtual ~SetVelocityOperation();
344 
346  // Variables
348  protected:
353  };
354 
356  {
357  m_velocity = velocity;
358  }
359 
361  {
362  return m_velocity;
363  }
364 
366  {
367  m_angularVelocity = angularVelocity;
368  }
369 
371  {
372  return m_angularVelocity;
373  }
374 
379  {
380  public:
381 
383 
384  virtual void triggerParticleEvent(const agx::TimeStamp& t,
385  agxSDK::Simulation* simulation,
386  agx::Physics::ParticleGeometryContactInstance contact,
387  agx::Physics::ParticleData& particleData,
388  agx::Physics::GeometryData& geometryData,
389  agxControl::EventSensor* sensor) override;
390 
391  virtual void triggerEvent(const agx::TimeStamp& t,
392  agxSDK::Simulation * simulation,
394  agxControl::EventSensor* sensor) override;
395 
396  virtual void update(const agx::TimeStamp& t) override;
397 
398  agx::Physics::ParticlePtrVector getParticlesInContact() const;
399 
401 
402  protected:
403  virtual ~StoreParticlesOperation();
404 
405  protected:
407  };
408 
410 
415  {
416  public:
417 
418  typedef std::pair<agx::TimeStamp, agx::Index> TimeParticleIdPair;
420 
422  EventSensor * target,
423  agx::Real teleportTimeDilation = 0,
424  bool cloneTeleportation = false,
425  agx::Frame * sourceoffset = nullptr,
426  agx::Frame * targetoffset = nullptr);
427 
428  void addNotification(EventSensor * sensor) override;
429 
430  void triggerEvent(const agx::TimeStamp& t,
431  agxSDK::Simulation * simulation,
433  agxControl::EventSensor* sensor) override;
434 
435  void triggerParticleEvent(const agx::TimeStamp& t,
436  agxSDK::Simulation* /*simulation*/,
437  agx::Physics::ParticleGeometryContactInstance contact,
438  agx::Physics::ParticleData& particleData,
439  agx::Physics::GeometryData& /*geometryData*/,
440  agxControl::EventSensor* sensor) override;
441 
442  void postContactHandling(agxSDK::Simulation *simulation, EventSensor * sensor) override;
443 
445 
446  protected:
448  void init();
449 
450  void teleportParticles(const agx::TimeStamp& t, agxControl::EventSensor * sensor, agx::Physics::GranularBodySystem * system);
451 
453  // Variables
455  protected:
458  ParticleTeleportQueue m_particleSpawnQueue;
464  };
465 }
466 
467 #endif
agx::Physics::ParticlePtrVector getParticlesInContact() const
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
This operation can be used to know when a sensor has been activated by a contact. ...
This operations removes all the particles in the contact supplied by the sensor.
agx::HashTable< agx::Index, agx::Bool > m_handledParticleIds
const agx::Vec3 & getAngularVelocity() const
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
This operation applies a "kinematic state" to particles that collide with the sensor.
A contact between two geometries.
Definition: Contacts.h:270
#define AGXPHYSICS_EXPORT
agx::Physics::ParticlePtrVector m_particlesInContact
virtual void triggerEvent(const agx::TimeStamp &, agxSDK::Simulation *, agxCollide::GeometryContact *, agxControl::EventSensor *) override
Triggers the sensor event by passing contact information.
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
void setIsExecuted(bool executed)
agx::Real TimeStamp
Definition: TimeStamp.h:26
This is a sensor class that can be attached to an geometry in the simulation.
Definition: EventSensor.h:47
This sensor operation class sets a speed to the object that collides with the sensor, in both linear and angular direction.
const agx::Vec3 & getVelocity() const
This sensor operation class store particle id&#39;s of particles that are in contact with the sensor...
void setMaterialName(const agx::String &material)
agxData::EntityStorageRef m_particleTeleportStorage
ParticleTeleportQueue m_particleSpawnQueue
This sensor operation can teleport contacting particles to a target sensor geometry with arbitrary ti...
void setAngularVelocity(const agx::Vec3 &angularVelocity)
ParticleFreezeMode
Enum describing what mode the operation should be in. If it should freeze or unfreeze particles...
static agxRender::Color Yellow()
Definition: Color.h:193
agxControl::EventSensor * m_targetSensor
This namespace contains general operation and action classes which can be used to control a agxSDK::S...
This operation dumps particle contact information to a file.
#define CALLABLE
Definition: Object.h:73
std::pair< agx::TimeStamp, agx::Index > TimeParticleIdPair
The object defining a frame of reference and providing transformations operations.
Definition: Frame.h:67
#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
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
This operation removes the rigid body given by the geometry in the contact supplied by the sensor...
void setEnableMixColors(bool mixColors)
void setVelocity(const agx::Vec3 &velocity)
double Real
Definition: Real.h:42
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.
A specific type of sensor event that triggers a change in the simulation when activated by a contact...
Definition: SensorEvent.h:117
agx::List< TimeParticleIdPair > ParticleTeleportQueue
Inheritance with partial specialization due to bug with ref_ptr containers.
agx::HashSet< agx::UInt32 > m_previousContactingParticleIds
This operation applies a kinematic "sleep state" to particles inside the sensor that are under a spec...
uint32_t UInt32
Definition: Integer.h:40
This operation changes the color of the particles given in the contact.