AGX Dynamics  2.37.2.0
RigidBodyEmitter.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 
18 #ifndef AGX_RIGIDBODY_EMITTER_H
19 #define AGX_RIGIDBODY_EMITTER_H
20 
21 #include <agx/Emitter.h>
22 #include <agx/InternalData.h>
23 #include <agx/BitState.h>
24 #include <agx/ICloneable.h>
25 #include <agx/ObserverFrame.h>
26 
27 namespace agxSDK
28 {
29  class Simulation;
30 }
31 
32 
33 namespace agx
34 {
37 
44  {
45  private:
50  struct RigidBodyEmitterInternalData : public agx::ICloneable
51  {
52  enum EState
53  {
54  CREATED_BY_RIGIDBODYEMITTER = 1 << 0,
55  CREATED_BY_SNAPSHOT_LOADING = 1 << 1,
56  IS_BODYMODEL_TEMPLATE = 1 << 2
57  };
58  typedef agx::BitState< EState, agx::Int32 > State;
59 
60  RigidBodyEmitterInternalData() : state() {}
61 
62  State state;
63  protected:
64  virtual ~RigidBodyEmitterInternalData() {}
65 
66  private:
67  virtual agx::ICloneableRef clone(agx::ICloneable* child) override
68  {
69  if (child == nullptr)
70  child = new RigidBodyEmitterInternalData();
71 
72  child->as<RigidBodyEmitterInternalData>()->state = this->state;
73 
74  return agx::ICloneableRef(child);
75  }
76  };
77  public:
78 
79 
80  AGX_DECLARE_POINTER_TYPES( DistributionModel );
81 
83  Event emitEvent;
84 
85  public:
86 
91  RigidBodyEmitter(Quantity quantity = QUANTITY_COUNT);
92 
100  static agx::RigidBodyRefVector spawnBodiesInBound(const agx::Bound3& bound,
101  agx::Emitter::DistributionTable* sourceTable,
102  const agx::Vec3& spacing,
103  agx::Real jitterfactor = agx::Real(0));
104 
108  static bool rigidBodyIsEmitted( const agx::RigidBody * body );
109 
113  static bool rigidBodyIsLoaded( const agx::RigidBody * body );
114 
120  static bool rigidBodyIsTemplate(const agx::RigidBody* body);
121 
128  static void setRigidBodyIsEmitted( agx::RigidBody * body, bool isEmitted );
129 
136  static void setRigidBodyIsLoaded( agx::RigidBody * body, bool isLoaded );
137 
148  static void setRigidBodyIsTemplate(agx::RigidBody* body, bool isTemplate);
149 
154  static agx::RigidBodyPtrVector getEmittedBodiesInSimulation(agxSDK::Simulation* simulation);
155 
157 
158  static void storePersistentData(const agx::RigidBody* rb, agxStream::OutputArchive& out);
159 
160  static void restorePersistentData(agx::RigidBody* rb, agxStream::InputArchive& in);
161 
163 
164  private:
165  static RigidBodyEmitterInternalData* getRigidBodyEmitterData(const agx::RigidBody* obj);
166 
167  static RigidBodyEmitterInternalData* getOrCreateRigidBodyEmitterData(agx::RigidBody* obj);
168 
170 
171  protected:
172 
174  virtual ~RigidBodyEmitter();
175 
176  virtual void emitBody(const Vec3& position,
177  const Quat& rotation,
178  const Vec3& velocity,
179  Emitter::DistributionModel *model) override;
180  virtual bool preEmit() override;
181  virtual void postEmit() override;
182 
183  virtual void transfer(agxSDK::Simulation *simulation) override;
184 
185  private:
186  // Creates and initializes the parameters.
187  void init();
188 
189 
190 
191  private:
192  agxSDK::Simulation *m_simulation;
193  };
194 
198  class AGXPHYSICS_EXPORT RigidBodyEmitter::DistributionModel : public Emitter::DistributionModel
199  {
200  public:
204  DistributionModel();
205 
217  DistributionModel(agx::RigidBody *bodyTemplate,
218  agx::Real probabilityWeight,
219  agx::ObserverFrame* placementObserver=nullptr);
220 
224  agx::RigidBody *getBodyTemplate();
225 
230  agx::ObserverFrame* getPlacementObserver() const;
231 
233 
234  AGXSTREAM_DECLARE_SERIALIZABLE(agx::RigidBodyEmitter::DistributionModel);
235 
237 
238  protected:
239  virtual ~DistributionModel();
240  virtual agx::Real calculateVolume() override;
241  virtual agx::Real calculateMass() override;
242 
243  private:
244  agx::RigidBodyRef m_bodyTemplate;
245  agx::ObserverFrameRef m_placementObserver;
246  };
247 }
248 
249 #endif
InputRef< T > in(const char *name, T &obj)
Create an object with a name and a reference to the object that should be restored (usually a pointer...
Definition: InputArchive.h:400
Event3< RigidBodyEmitter *, agx::RigidBody *, DistributionModel * > Event
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive...
friend class DistributionTable
Definition: Emitter.h:388
#define AGXPHYSICS_EXPORT
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
agx::ref_ptr< ICloneable > ICloneableRef
Definition: ICloneable.h:24
Spawns new bodys inside a given volume.
Definition: Emitter.h:44
T * as()
Subclass casting.
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
#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
Interface class for a cloneable reference counted object.
Definition: ICloneable.h:29
Spawns new Rigid Bodies inside a volume given to the Emitter.
double Real
Definition: Real.h:42
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
With this class you can attach an ObserverFrame object relative to a RigidBody.
Definition: ObserverFrame.h:43