AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Emitter.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 
18 #ifndef AGX__EMITTER_H
19 #define AGX__EMITTER_H
20 
21 #include <agx/agxPhysics_export.h>
23 #include <agxStream/Serializable.h>
24 #include <agx/Task.h>
25 #include <agxCollide/Geometry.h>
26 #include <agxCollide/Space.h>
27 #include <agx/Random.h>
28 
29 namespace agx
30 {
33 
38  {
39  public:
40  enum Quantity
41  {
44  QUANTITY_MASS
45  };
46 
47  AGX_DECLARE_POINTER_TYPES(DistributionModel);
48  AGX_DECLARE_VECTOR_TYPES(DistributionModel);
49  AGX_DECLARE_POINTER_TYPES(DistributionTable);
51 
52  public:
53 
57  Emitter(Quantity quantity = QUANTITY_COUNT);
58 
62  void setGeometry(agxCollide::Geometry* geometry);
63  agxCollide::Geometry* getGeometry();
64 
65 
72  void setQuantity(Quantity quantity);
73 
77  Quantity getQuantity() const;
78 
85  void setRate(Real rate);
86 
90  Real getRate() const;
91 
96  void setVelocity(const Vec3& velocity);
97 
101  const Vec3& getVelocity() const;
102 
106  void setSeed(const agx::UInt32 seed);
107 
111  void setDistributionTable(DistributionTable *table);
112 
116  DistributionTable *getDistributionTable();
117  const DistributionTable *getDistributionTable() const;
118 
122  agx::UInt getNumEmitted() const;
123 
130  void setMaximumEmittedQuantity(const agx::Real max);
131 
135  agx::Real getEmittedQuantity() const;
136 
140  agx::Real getMaximumEmittedQuantity() const;
141 
150  void addCollisionGroup(agx::UInt32 id);
151  void addCollisionGroup(const agx::Name& name);
152 
157  void removeCollisionGroup(agx::UInt32 id);
158  void removeCollisionGroup(const agx::Name& name);
159 
166  bool hasCollisionGroup(agx::UInt32 id) const;
167  bool hasCollisionGroup(agx::Name id) const;
168 
172  agx::Physics::CollisionGroupSetPtr getCollisionGroupSet() const;
173 
174  void setName(const agx::String& name);
175 
176  agx::String getName() const;
177 
179 
180  void transfer(agxData::EntityStorage *storage);
181  void setSpace(agxCollide::Space* space);
182 
184  static agxStream::Serializable* create() { return nullptr; }
185  static agxStream::Serializable* create(agxStream::InputArchive&) { return nullptr; }
186 
188 
189  protected:
190  virtual void run() override;
191 
193  virtual ~Emitter();
194 
195  virtual void emitBody(const Vec3& position, const Vec3& velocity, Emitter::DistributionModel *model) = 0;
196  virtual bool preEmit() = 0;
197  virtual void postEmit() = 0;
198  virtual Real getDefaultElementQuantity(Quantity quantity);
199 
200  virtual void store( agxStream::OutputArchive& out ) const override;
201  virtual void restore( agxStream::InputArchive& in ) override;
202 
203  void setEmittedQuantity(agx::Real quantity);
204  void setEmittedCount(agx::UInt emittedCount);
205 
206  // Generates a random number from the generator in the emitter
207  agx::Real generateRandomNumber();
208 
209  private:
210  // Creates and initializes the parameters.
211  void init();
212 
213  // Few helper functions for randomly selecting points in space.
214  Vec3 randomPointInBox(const agxCollide::BoundingAABB* bound);
215  Vec3 randomPointInSphere(agx::Real maxRadius);
216  Vec3 randomPointInGeometry(agxCollide::Geometry* geometry, agxCollide::Geometry* proxySphere);
217 
218  void setCollisionGroupSet(agx::Physics::CollisionGroupSetPtr set);
219 
220  protected:
221  Physics::EmitterPtr m_entity;
222 
223  DistributionTableRef m_distributionTable;
224  agxCollide::GeometryRef m_geometry;
225  agxCollide::GeometryRef m_proxySphere;
226  agxCollide::SpaceRef m_space;
227  ScalarParameterRef m_timestep;
228 
229  // Random number generator for body emitter
230  UniformRealGenerator m_generator;
231 
232  Quantity m_quantity;
233  Real m_rest;
234  };
235 
236 
237  class AGXPHYSICS_EXPORT Emitter::DistributionModel : public Referenced, public virtual agxStream::Serializable
238  {
239  public:
240  DistributionModel();
241  DistributionModel(agx::Real probabilityWeight);
242 
246  void setProbabilityWeight(agx::Real weight);
247 
251  agx::Real getProbabilityWeight() const;
252 
253 
257  agx::UInt getActivationCount() const;
258 
259  agx::Real getVolume() const;
260  agx::Real getMass() const;
261 
263 
265  static agxStream::Serializable* create() { return nullptr; }
266  static agxStream::Serializable* create(agxStream::InputArchive&) { return nullptr; }
267 
269 
270  protected:
271  virtual ~DistributionModel();
272  virtual agx::Real calculateVolume() = 0;
273  virtual agx::Real calculateMass() = 0;
274  virtual void store( agxStream::OutputArchive& out ) const override;
275  virtual void restore( agxStream::InputArchive& in ) override;
276 
277  private:
278  friend class DistributionTable;
279  void setDistribution(DistributionTable *distribution);
280 
281  static bool rangeCompare(DistributionModel *model, agx::Real value);
282 
283  protected:
284  DistributionTable *m_distribution;
290  };
291 
292  class AGXPHYSICS_EXPORT Emitter::DistributionTable : public Referenced, public virtual agxStream::Serializable
293  {
294  public:
295  DistributionTable(Quantity quantity = QUANTITY_COUNT);
296 
300  void addModel(DistributionModel *model);
301 
305  const DistributionModelRefVector& getModels() const;
306 
310  DistributionModel *getRandomModel();
311 
315  void setSeed(agx::UInt32 seed);
316 
320  agx::Real calculateTotalWeight() const;
321 
325  void setProbabilityQuantity(Quantity quantity);
326 
330  Quantity getProbabilityQuantity() const;
331 
333 
335 
337 
338  protected:
339  virtual ~DistributionTable();
340 
341  private:
342  friend class DistributionModel;
343  void rebuildProbabilityTable();
344 
345  agx::Real getQuantifiedProbabilityWeight(DistributionModel *model) const;
346  agx::Real calculateTotalQuantifiedWeight() const;
347 
348 
349  private:
350  Quantity m_quantity;
351  DistributionModelRefVector m_models;
352  UniformRealGenerator m_randomGenerator;
353  agx::UInt32 m_seed;
354  bool m_needRebuild;
355  };
356 
357  /* Implementation */
359 
360  inline agx::Physics::CollisionGroupSetPtr agx::Emitter::getCollisionGroupSet() const { return m_entity.collisionGroupSet(); }
361 
363  {
364  return agxCollide::CollisionGroupManager::hasGroup(getCollisionGroupSet(), id);
365  }
366 
368  {
369  return agxCollide::CollisionGroupManager::hasGroup(getCollisionGroupSet(), id);
370  }
371 
372  AGX_FORCE_INLINE void agx::Emitter::setCollisionGroupSet(agx::Physics::CollisionGroupSetPtr set)
373  {
374  m_entity.collisionGroupSet() = set;
375  }
376 }
377 
378 #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
void AGXPHYSICS_EXPORT init()
Initialize agx.
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
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...
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:874
DistributionTable * m_distribution
Definition: Emitter.h:284
friend class DistributionTable
Definition: Emitter.h:278
agx::Real m_probabilityWeight
Definition: Emitter.h:285
#define AGXPHYSICS_EXPORT
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:62
agx::Real m_volume
Definition: Emitter.h:288
SerialTask.
Definition: Task.h:469
Spawns new bodys inside a given volume.
Definition: Emitter.h:37
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
uint32_t UInt
Definition: Integer.h:34
bool hasCollisionGroup(agx::UInt32 id) const
This is performing a linear search among the group id for this Emitter.
Definition: Emitter.h:362
agxCollide::Geometry * getGeometry()
Definition: Emitter.h:358
agx::Physics::CollisionGroupSetPtr getCollisionGroupSet() const
Definition: Emitter.h:360
typedef void(GLAD_API_PTR *GLDEBUGPROC)(GLenum source
agx::RangeReal m_range
Definition: Emitter.h:287
#define CALLABLE
Definition: Object.h:76
agx::Real m_mass
Definition: Emitter.h:289
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
static bool hasGroup(agx::Physics::CollisionGroupSetPtr set, agx::UInt32 groupId)
#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
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:56
agx::UInt m_activationCount
Definition: Emitter.h:286
agx::ref_ptr< Geometry > GeometryRef
Definition: Geometry.h:75
#define AGXSTREAM_DECLARE_ABSTRACT_SERIALIZABLE(T)
Use this in a pure abstract Serializable class to add the required methods Important: Use full namesp...
Definition: Serializable.h:220
double Real
Definition: Real.h:42
agx::ref_ptr< Space > SpaceRef
Definition: Space.h:74
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_DECLARE_VECTOR_TYPES(Clock)