AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParticleSystem.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 AGX_PARTICLESYSTEM_H
18 #define AGX_PARTICLESYSTEM_H
19 
20 #include <agx/Referenced.h>
21 #include <agx/Component.h>
22 #include <agx/Bound.h>
26 #include <agx/Material.h>
27 #include <agx/Emitter.h>
28 #include <agxStream/Serializable.h>
29 #include <agx/Task.h>
30 #include <agxCollide/Geometry.h>
31 #include <agxCollide/Space.h>
32 #include <typeinfo>
33 #include <agx/Logger.h>
34 
35 namespace agxData
36 {
38 }
39 
40 namespace agxIO
41 {
42  class Image;
43 }
44 
45 namespace agxSDK
46 {
47  class Simulation;
48 }
49 
50 AGX_TYPE_BINDING( agxData::ParticlePair, "ParticlePair" )
51 
52 namespace agx
53 {
56 
66  {
67  public:
68  static Model* ClassModel();
69 
73  virtual bool setUpdateTask( Task* updateTask );
74 
78  Task* getUpdateTask();
79 
83  virtual void addNotification( agxSDK::Simulation* simulation );
84 
88  Physics::ParticlePtr createParticle();
89 
93  agxData::EntityRange createParticles( size_t numParticles );
94 
98  Physics::ParticlePtr getParticle( agx::Index particleId );
99 
103  void destroyParticle( Physics::ParticlePtr particle );
104 
108  void destroyParticles( const Physics::ParticlePtrVector& particles );
109  void destroyParticles( const agxData::EntityPtrVector& particles );
110 
114  void destroyParticles( agxData::EntityRange particles );
115 
123  void removeParticle( Physics::ParticlePtr particle );
124 
128  bool isValid( Physics::ParticlePtr particle ) const;
129 
133  bool isParticleGeometryContactValid( Physics::ParticleGeometryContactPtr contact );
134 
138  bool isParticlePairContactValid( Physics::ParticlePairContactPtr contact );
139 
140 #if (!defined SWIG ) || ( defined SWIGCSHARP || defined SWIGJAVA)
141 
144  const agx::Physics::ParticlePtrArray getParticles() const;
145  agx::Physics::ParticlePtrArray getParticles();
146 #endif
147 
149  agxData::EntityRange spawnParticles( const agx::Vec3Vector& positions );
150 
158  agx::Physics::ParticlePtrVector spawnParticlesInBound( const agx::Bound3& bound, Real radius, const agx::Vec3& spacing, agx::Real jitterFactor = agx::Real( 0 ) );
159 
168  agx::Physics::ParticlePtrVector spawnParticlesInBound( const agx::Bound3& bound, agx::Emitter::DistributionTable* sourceTable, const agx::Vec3& spacing, agx::Real jitterFactor = agx::Real( 0 ) );
169 
178  agx::Physics::ParticlePtrVector spawnParticlesInBoundHCP( const agx::Bound3& bound, Real radius, const agx::Vec3& spacing, agx::Real jitterFactor = agx::Real( 0 ) );
179 
188  agx::Physics::ParticlePtrVector spawnParticlesInBoundHCP( const agx::Bound3& bound, agx::Emitter::DistributionTable* sourceTable, const agx::Vec3& spacing, agx::Real jitterFactor = agx::Real( 0 ) );
189 
197  agx::Physics::ParticlePtrVector spawnParticlesInGeometry( agxCollide::Geometry* geometry, Real radius, const agx::Vec3& spacing, agx::Real jitterFactor = agx::Real( 0 ) );
198 
206  agx::Physics::ParticlePtrVector spawnParticlesInGeometryHCP( agxCollide::Geometry* geometry, Real radius, const agx::Vec3& spacing, agx::Real jitterFactor = agx::Real( 0 ) );
207 
216  agx::Physics::ParticlePtrVector spawnParticlesInGeometry( agxCollide::Geometry* geometry, agx::Emitter::DistributionTable* sourceTable, const agx::Vec3& spacing, agx::Real jitterFactor = agx::Real( 0 ) );
217 
226  agx::Physics::ParticlePtrVector spawnParticlesInGeometryHCP( agxCollide::Geometry* geometry, agx::Emitter::DistributionTable* sourceTable, const agx::Vec3& spacing, agx::Real jitterFactor = agx::Real( 0 ) );
227 
233  void colorParticlesWithLevelsInBound( const agx::Bound3& bound, const agx::Vec4fVector& colors );
234 
238  size_t getNumParticles() const;
239 
243  Real getParticleRadius() const;
244 
248  Real getParticleMass() const;
249 
258  void setParticleRadius( Real radius, bool updateExistingParticles = false );
259 
268  virtual void setParticleMass( Real mass, bool updateExistingParticles = false );
269 
278  void setMaterial( Material* material, bool updateExistingParticles = false );
279 
283  Material* getMaterial();
284  const Material* getMaterial() const;
285 
289  void evaluateParticleDefaultMass();
290 
294  agxData::EntityStorage* getParticleStorage();
295 
299  const agxData::EntityStorage* getParticleStorage() const;
300 
304  agxData::EntityStorage* getParticlePairContactStorage();
305 
309  agxData::EntityStorage* getGeometryParticleContactListStorage();
310 
314  agxData::EntityStorage* getParticleGeometryContactStorage();
315 
319  agx::Vector<agx::Physics::ParticleGeometryContactPtr> getParticleGeometryContacts();
320 
324  agx::Vector<agx::Physics::ParticlePairContactPtr> getParticleParticleContacts();
325 
330  Vec3 sumForceMagnitudes( RigidBody* body );
331 
332  // Returns the total contact forces between the particles in the system
333  agx::Real getTotalParticleParticleContactForces();
334 
335  // Returns the total particle-geometry contact forces in the system.
336  agx::Real getTotalParticleGeometryContactForces();
337 
338  // Calculates the total particle volume in the particle system
339  agx::Real calculateTotalParticleVolume() const;
340 
342  agx::Real calculateTotalParticleMass() const;
343 
345 
349  void setEnableCollisions( const agxCollide::Geometry* geometry, bool flag );
350 
354  bool getEnableCollisions( const agxCollide::Geometry* geometry ) const;
355 
359  const agxCollide::GeometryHashVector& getDisabledCollisions() const;
360 
361 
370  // bool canCollide( const agxCollide::Geometry* geometry ) const;
371 
380  void addCollisionGroup( agx::UInt32 id, bool updateExistingParticles = true );
381  void addCollisionGroup( const agx::Name& id, bool updateExistingParticles = true );
382 
387  void removeCollisionGroup( agx::UInt32 id, bool updateExistingParticles = true );
388  void removeCollisionGroup( const agx::Name& id, bool updateExistingParticles = true );
389 
398  void addCollisionGroupParticle( agx::UInt32 id, agx::Physics::ParticlePtr ptr );
399  void addCollisionGroupParticle( const agx::Name& id, agx::Physics::ParticlePtr ptr );
400 
405  void removeCollisionGroupParticle( agx::UInt32 id, agx::Physics::ParticlePtr ptr );
406  void removeCollisionGroupParticle( const agx::Name& id, agx::Physics::ParticlePtr ptr );
407 
414  bool hasCollisionGroupParticle( agx::UInt32 id, agx::Physics::ParticlePtr ptr ) const;
415  bool hasCollisionGroupParticle( const agx::Name& id, agx::Physics::ParticlePtr ptr ) const;
416 
423  bool hasCollisionGroup( agx::UInt32 id ) const;
424  bool hasCollisionGroup( const agx::Name& id ) const;
425 
429  agx::Physics::CollisionGroupSetPtr getCollisionGroupSet() const;
430 
431  const agx::Bound3& getParticleBound();
432 
436  bool hasCustomBuffer( const agx::String& buffername, const agx::String& formatName );
437 
443  void addCustomBufferReal( const agx::String& buffername );
444 
451  void setCustomBufferValueReal( agx::Index id, const agx::String& buffername, agx::Real val );
452 
459  agx::Real getCustomBufferValueReal( agx::Index id, const agx::String& buffername );
460 
466  void addCustomBufferInt( const agx::String& buffername );
467 
474  void setCustomBufferValueInt( agx::Index id, const agx::String& buffername, agx::Int val );
475 
482  agx::Int getCustomBufferValueInt( agx::Index id, const agx::String& buffername );
483 
489  void addCustomBufferReal32( const agx::String& buffername );
490 
497  void setCustomBufferValueReal32( agx::Index id, const agx::String& buffername, agx::Real32 val );
498 
505  agx::Real32 getCustomBufferValueReal32( agx::Index id, const agx::String& buffername );
506 
512  void addCustomBufferVec3( const agx::String& buffername );
513 
520  void setCustomBufferValueVec3( agx::Index id, const agx::String& buffername, agx::Vec3 val );
521 
528  agx::Vec3 getCustomBufferValueVec3( agx::Index id, const agx::String& buffername );
529 
531  agx::Physics::ParticleGeometryContactPtr createParticleGeometryContact( agx::Physics::ParticlePtr particle, agx::Physics::GeometryPtr geometry );
532 
534  virtual void destroySingleParticle( agx::Index particleId );
535 
537  virtual void setSingleParticleRadius( agx::Index particleId, agx::Real radius );
538 
540  virtual agx::Real getSingleParticleRadius( agx::Index particleId );
541 
543  virtual void setSingleParticleMass( agx::Index particleId, agx::Real mass );
544 
546  virtual agx::Real getSingleParticleMass( agx::Index particleId );
547 
549  virtual void setSingleParticleColor( agx::Index particleId, const agx::Vec4f& color );
550 
552  virtual agx::Vec4f getSingleParticleColor( agx::Index particleId );
553 
555  virtual void setSingleParticleVelocity( agx::Index particleId, const agx::Vec3& velocity );
556 
558  virtual agx::Vec3 getSingleParticleVelocity( agx::Index particleId );
559 
561  virtual agx::Vec3 getSingleParticlePosition( agx::Index particleId );
562 
564  virtual void setSingleParticleForce( agx::Index particleId, const agx::Vec3& force );
565 
567  virtual agx::Vec3 getSingleParticleForce( agx::Index particleId );
568 
570  void applyAirForceDragSimple( agx::Real cd, agx::Real cr );
571 
573  bool particleExistsWithId( agx::Index id );
574 
581  agx::Index getParticleIdFromIndex( agx::Index index ) const;
582 
586  void clearAllParticles();
587 
592  void updateShouldRender();
593 
597  void commitColorBuffer();
598 
603  void commitAllBuffers();
604 
608  void attach();
609 
614  void detach();
615 
616  // private
617  void _setGravityTask( Task* task );
618  void setSpace( agxCollide::Space* space );
619  void clearContactData();
620 
622 
623  agx::Vector<agx::Physics::ParticlePtr> createParticlesFromImage( agxIO::Image* image, agx::Real particleRadius, const agx::Vec3& center, agx::Real radiusJitter );
624 
626  static Vec3u calculateFilledBoundPacking( agx::Vec3Vector& result, agx::Bound3 bound, const agx::Vec3& spacing, agx::Real boundarySpacing = agx::Real( 0 ), agx::Real jitterFactor = agx::Real( 0 ) );
627  static size_t calculateFilledBoundPackingHCP( agx::Vec3Vector& result, agx::Bound3 bound, const agx::Vec3& distance, agx::Real boundarySpacing = agx::Real( 0 ), agx::Real jitterFactor = agx::Real( 0 ) );
628  static void calculateFilledGeometryPacking( agx::Vec3Vector& result, agxCollide::Geometry* geometry, const agx::Vec3& spacing, agx::Real boundarySpacing = agx::Real( 0 ), agx::Real jitterFactor = agx::Real( 0 ) );
629  static void calculateFilledGeometryPackingHCP( agx::Vec3Vector& result, agxCollide::Geometry* geometry, const agx::Vec3& spacing, agx::Real boundarySpacing = agx::Real( 0 ), agx::Real jitterFactor = agx::Real( 0 ) );
630 
631  template<typename T>
632  void addCustomBufferT( const agx::String& name );
633 
634  template<typename T>
635  void setCustomBufferValueT( agx::Index id, const agx::String& buffername, const T valueT );
636 
637  template<typename T>
638  T getCustomBufferValueT( agx::Index id, const agx::String& buffername );
639 
640  agxSDK::Simulation* getSimulation() const;
641 
642  agx::Physics::ParticlePtrVector convertRangeToParticlePtrVector( agxData::EntityRange& entityRange );
643 
645 
647 
648  protected:
649  ParticleSystem( const String& model, Device* device = CpuDevice::instance() );
650  ParticleSystem(); // Only used for restoring from serialization
651 
652  virtual ~ParticleSystem();
653 
654  void init( bool createMaterial = true );
655  void setCollisionGroupSet( agx::Physics::CollisionGroupSetPtr );
656  void invalidLicenseCallback();
658  void setRadiusOnParticles( agxData::EntityRange& entityRange, agx::Real radius );
659 
660  protected:
668  class AGXPHYSICS_EXPORT MaterialUuidBufferListener : public agxData::Buffer::EventListener, public agx::Referenced
669  {
670  public:
672  bool isJournalPlayback();
673  void setSimulation( agxSDK::Simulation* sim );
674  virtual void updateCallback( agxData::Buffer* buffer ) override;
675  virtual void updateCallback( agxData::Buffer* buffer, agx::Index index ) override;
676  virtual void updateCallback( agxData::Buffer* buffer, agx::IndexRange range ) override;
677  virtual void updateCallback( agxData::Buffer* buffer, agxData::Array< agx::Index > indexSet ) override;
678 
679  protected:
680  virtual ~MaterialUuidBufferListener();
682  // Variables
684  private:
686  agxSDK::Simulation* m_simulation;
687  };
688 
689  protected:
691  private:
692  agxData::EntityStorageRef m_particleCellStorage;
693  agxData::EntityStorageRef m_cellBlockStorage;
694  agxData::EntityStorageRef m_particleZoneStorage;
695  agxData::EntityStorageRef m_collisionParticleStorage;
696  agxData::EntityStorageRef m_particleGeometryContactStorage;
697  agxData::EntityStorageRef m_particlePairContactStorage;
698  agxData::EntityStorageRef m_geometryParticleContactListStorage;
699 
700  agxData::BufferRef m_materialUuidBuffer;
701  agxData::BufferRef m_cellBlockPermutation;
702  agxData::BufferRef m_deadCellBlocks;
703  agxData::BufferRef m_particlesWithMissingTargetCell;
704  agxData::BufferRef m_cellBlockOverlapPairs;
705  agxData::ValueRef m_particleBound;
706  agxData::ValueRef m_collisionGroupSet;
707  agxData::ValueRef m_defaultRadius;
708  agxData::ValueRef m_defaultMass;
709  agxData::ValueRef m_defaultMaterial;
710 
711  MaterialRef m_material;
712  TaskRef m_gravityTask;
713  bool m_initialized;
714  bool m_dirtyBound;
715 
716  agx::TaskRef m_updateBoundTask;
717 
718  agxCollide::GeometryHashVector m_disabledCollisionsHash;
719  agxCollide::SpaceRef m_space;
720 
721  MaterialUuidBufferListenerRef m_uuidMaterialListenerRef;
722 
723  agxData::ValueObserver m_invMassValue;
724  void massChanged( agxData::Value* massValue );
725  agxData::Value::Event::CallbackType m_massChangedCallback;
726  };
727 
728  /* Implementation */
729  AGX_FORCE_INLINE Task* ParticleSystem::getUpdateTask() { return m_updateTask; }
730 
731  AGX_FORCE_INLINE const agx::Physics::ParticlePtrArray ParticleSystem::getParticles() const
732  {
733  return m_particleStorage->getInstances<Physics::ParticlePtr>();
734  }
735 
736  AGX_FORCE_INLINE agx::Physics::ParticlePtrArray ParticleSystem::getParticles()
737  {
738  return m_particleStorage->getInstances<Physics::ParticlePtr>();
739  }
740 
741  AGX_FORCE_INLINE agxData::EntityStorage* ParticleSystem::getParticleStorage() { return m_particleStorage; }
742  AGX_FORCE_INLINE const agxData::EntityStorage* ParticleSystem::getParticleStorage() const { return m_particleStorage; }
743  AGX_FORCE_INLINE agxData::EntityStorage* ParticleSystem::getParticleGeometryContactStorage() { return m_particleGeometryContactStorage; }
744  AGX_FORCE_INLINE agxData::EntityStorage* ParticleSystem::getGeometryParticleContactListStorage() { return m_geometryParticleContactListStorage; }
745  AGX_FORCE_INLINE agxData::EntityStorage* ParticleSystem::getParticlePairContactStorage() { return m_particlePairContactStorage; }
746 
747  AGX_FORCE_INLINE const agxCollide::GeometryHashVector& ParticleSystem::getDisabledCollisions() const { return m_disabledCollisionsHash; }
748  // AGX_FORCE_INLINE bool ParticleSystem::hasCollisionGroup( agx::UInt32 id ) const { return m_collisionGroups.contains(id); }
749  inline agx::Physics::CollisionGroupSetPtr ParticleSystem::getCollisionGroupSet() const { return m_collisionGroupSet->get<Physics::CollisionGroupSetPtr>(); }
750 
751  // Real
752  AGX_FORCE_INLINE void ParticleSystem::addCustomBufferReal( const agx::String& buffername ) { addCustomBufferT<agx::Real>( buffername ); }
753  AGX_FORCE_INLINE void ParticleSystem::setCustomBufferValueReal( agx::Index id, const agx::String& buffername, agx::Real val ) { setCustomBufferValueT<agx::Real>( id, buffername, val ); }
754  AGX_FORCE_INLINE agx::Real ParticleSystem::getCustomBufferValueReal( agx::Index id, const agx::String& buffername ) { return getCustomBufferValueT<agx::Real>( id, buffername ); }
755 
756  // Int
757  AGX_FORCE_INLINE void ParticleSystem::addCustomBufferInt( const agx::String& buffername ) { addCustomBufferT<agx::Int>( buffername ); }
758  AGX_FORCE_INLINE void ParticleSystem::setCustomBufferValueInt( agx::Index id, const agx::String& buffername, agx::Int val ) { setCustomBufferValueT<agx::Int>( id, buffername, val ); }
759  AGX_FORCE_INLINE agx::Int ParticleSystem::getCustomBufferValueInt( agx::Index id, const agx::String& buffername ) { return getCustomBufferValueT<agx::Int>( id, buffername ); }
760 
761  // Real32
762  AGX_FORCE_INLINE void ParticleSystem::addCustomBufferReal32( const agx::String& buffername ) { addCustomBufferT<agx::Real32>( buffername ); }
763  AGX_FORCE_INLINE void ParticleSystem::setCustomBufferValueReal32( agx::Index id, const agx::String& buffername, agx::Real32 val ) { setCustomBufferValueT<agx::Real32>( id, buffername, val ); }
764  AGX_FORCE_INLINE agx::Real32 ParticleSystem::getCustomBufferValueReal32( agx::Index id, const agx::String& buffername ) { return getCustomBufferValueT<agx::Real32>( id, buffername ); }
765 
766  // Vec3
767  AGX_FORCE_INLINE void ParticleSystem::addCustomBufferVec3( const agx::String& buffername ) { addCustomBufferT<agx::Vec3>( buffername ); }
768  AGX_FORCE_INLINE void ParticleSystem::setCustomBufferValueVec3( agx::Index id, const agx::String& buffername, agx::Vec3 val ) { setCustomBufferValueT<agx::Vec3>( id, buffername, val ); }
769  AGX_FORCE_INLINE agx::Vec3 ParticleSystem::getCustomBufferValueVec3( agx::Index id, const agx::String& buffername ) { return getCustomBufferValueT<agx::Vec3>( id, buffername ); }
770 }
771 
772 // AGX_PARTICLESYSTEM_H
773 #endif
float Real32
Definition: Real.h:44
void AGXPHYSICS_EXPORT init()
Initialize agx.
Pointer to a entity instance of type Physics.ParticleGeometryContact.
TaskGroupRef m_updateTask
Pointer to a entity instance of type Physics.ParticlePairContact.
Smart pointer for observed objects, that automatically set pointers to them to null when they deleted...
Definition: observer_ptr.h:60
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:73
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:38
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
Abstract representation of a value.
Definition: Value.h:42
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
#define AGX_TYPE_BINDING(_Type, _Name)
Definition: Type.h:179
friend class DistributionTable
Definition: Emitter.h:278
#define AGXPHYSICS_EXPORT
int32_t Int
Definition: Integer.h:35
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
A representation of a generic task.
Definition: Task.h:57
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:62
GLenum GLuint id
Definition: gl.h:5539
Type-specific Array used for fast access into the data held by a Buffer.
Definition: agxData.h:30
Pointer to a entity instance of type Physics.CollisionGroupSet.
Base class providing referencing counted objects.
Definition: Referenced.h:113
The geometry representation used by the collision detection engine.
Definition: Geometry.h:88
agxData::EntityStorageRef m_particleStorage
A basic particle system that contains all the storages and buffers required for basic operation and a...
This class contains all Geometries and performs Broad Phase and Narrow Phase collision detection to c...
Definition: Space.h:81
Templated callback with one argument.
Definition: Callback.h:100
Pointer to a entity instance of type Physics.Geometry.
Abstract representation of a data buffer.
Definition: Buffer.h:55
UInt32 Index
Definition: Integer.h:52
agx::SymmetricPair< agx::Physics::ParticlePtr > ParticlePair
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:52
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:384
AGXPHYSICS_EXPORT bool setEnableCollisions(agx::RigidBody *rb1, agx::RigidBody *rb2, bool enable)
Enable or disable the collision between all geometries in RigidBody rb1 and RigidBody rb2...
static agx::Model * ClassModel()
#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
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:121
double Real
Definition: Real.h:42
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
Class to represent Images.
Definition: Image.h:32
An agx::Device is an abstract representation of a device on which data can be stored and processed...
Definition: Device.h:57
Pointer to a entity instance of type Physics.Particle.
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