AGX Dynamics  2.35.0.1
Emitter.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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 #pragma once
19 
20 #include <agx/agxPhysics_export.h>
22 #include <agxStream/Serializable.h>
23 #include <agx/Task.h>
24 #include <agxCollide/Geometry.h>
25 #include <agxCollide/Space.h>
26 #include <agx/Random.h>
27 #include <agx/SetVector.h>
28 #include <agx/BitState.h>
29 
30 namespace agxSDK {
31  class Simulation;
32 }
33 
34 namespace agx
35 {
39 
40 
45  {
46  public:
47  enum Quantity
48  {
51  QUANTITY_MASS
52  };
53 
54  AGX_DECLARE_POINTER_TYPES(DistributionModel);
55  AGX_DECLARE_VECTOR_TYPES(DistributionModel);
56  AGX_DECLARE_POINTER_TYPES(DistributionTable);
58 
59  public:
60 
64  Emitter(Quantity quantity = QUANTITY_COUNT);
65 
70  agxCollide::Geometry* getGeometry();
71 
72 
79  void setQuantity(Quantity quantity);
80 
85 
92  void setRate(Real rate);
93 
97  Real getRate() const;
98 
103  void setVelocity(const Vec3& velocity);
104 
108  const Vec3& getVelocity() const;
109 
113  void setSeed(const agx::UInt32 seed);
114 
118  void setDistributionTable(DistributionTable *table);
119 
123  DistributionTable *getDistributionTable();
124  const DistributionTable *getDistributionTable() const;
125 
130 
138 
143 
148 
158  void addCollisionGroup(const agx::Name& name);
159 
165  void removeCollisionGroup(const agx::Name& name);
166 
173  bool hasCollisionGroup(agx::UInt32 id) const;
174  bool hasCollisionGroup(agx::Name id) const;
175 
179  agx::Physics::CollisionGroupSetPtr getCollisionGroupSet() const;
180 
187 
192 
201  void setEnableRenderEmittedObjects(bool enable);
202 
208 
209  void setName(const agx::String& name);
210 
212 
214 
215  Physics::EmitterPtr getEntity() const;
216 
217  virtual void transfer(agxSDK::Simulation *simulation);
218  void setSpace(agxCollide::Space* space);
219 
221  static agxStream::Serializable* create() { return nullptr; }
222  static agxStream::Serializable* create(agxStream::InputArchive&) { return nullptr; }
223 
225 
226  protected:
227  virtual void run() override;
228 
230  virtual ~Emitter();
231 
232  virtual void emitBody(const Vec3& position, const Vec3& velocity, Emitter::DistributionModel *model) = 0;
233  virtual bool preEmit() = 0;
234  virtual void postEmit() = 0;
235  virtual Real getDefaultElementQuantity(Quantity quantity);
236 
237  virtual void store( agxStream::OutputArchive& out ) const override;
238  virtual void restore( agxStream::InputArchive& in ) override;
239 
240  void setEmittedQuantity(agx::Real quantity);
241  void setEmittedCount(agx::UInt emittedCount);
242 
243  // Generates a random number from the generator in the emitter
244  agx::Real generateRandomNumber();
245 
246  // State flags for emitter
248  {
249  OVERRIDE_RENDER_SETTINGS = 1 << 0,
250  SHOULD_RENDER_EMITTED_OBJECTS = 1 << 1
251  };
252  using Flags = agx::BitState<StateFlags, agx::UInt32>;
253 
254  private:
255  // Creates and initializes the parameters.
256  void init();
257 
258  // Few helper functions for randomly selecting points in space.
259  Vec3 randomPointInBox(const agxCollide::BoundingAABB* bound);
260  Vec3 randomPointInSphere(agx::Real maxRadius);
261  Vec3 randomPointInGeometry(agxCollide::Geometry* geometry, agxCollide::Geometry* proxySphere);
262 
263  void setCollisionGroupSet(agx::Physics::CollisionGroupSetPtr set);
264 
265  protected:
267 
273 
274  // Random number generator for body emitter
276 
280  };
281 
282 
283  class AGXPHYSICS_EXPORT Emitter::DistributionModel : public Referenced, public virtual agxStream::Serializable
284  {
285  public:
287  DistributionModel(agx::Real probabilityWeight);
288 
293 
298 
299 
304 
307 
309 
311  static agxStream::Serializable* create() { return nullptr; }
312  static agxStream::Serializable* create(agxStream::InputArchive&) { return nullptr; }
313 
315 
316  protected:
317  virtual ~DistributionModel();
318  virtual agx::Real calculateVolume() = 0;
319  virtual agx::Real calculateMass() = 0;
320  virtual void store( agxStream::OutputArchive& out ) const override;
321  virtual void restore( agxStream::InputArchive& in ) override;
322 
323  private:
324  friend class DistributionTable;
325  void setDistribution(DistributionTable *distribution);
326 
327  static bool rangeCompare(DistributionModel *model, agx::Real value);
328 
329  protected:
330  DistributionTable *m_distribution;
331  agx::Real m_probabilityWeight;
332  agx::UInt m_activationCount;
333  agx::RangeReal m_range;
334  agx::Real m_volume;
335  agx::Real m_mass;
336  };
337 
338  class AGXPHYSICS_EXPORT Emitter::DistributionTable : public Referenced, public virtual agxStream::Serializable
339  {
340  public:
341  DistributionTable(Quantity quantity = QUANTITY_COUNT);
342 
346  void addModel(DistributionModel *model);
347 
352 
356  DistributionModel *getRandomModel();
357 
361  void setSeed(agx::UInt32 seed);
362 
367 
372 
377 
379 
380  AGXSTREAM_DECLARE_SERIALIZABLE(agx::Emitter::DistributionTable);
381 
383 
384  protected:
385  virtual ~DistributionTable();
386 
387  private:
388  friend class DistributionModel;
389  void rebuildProbabilityTable();
390 
391  agx::Real getQuantifiedProbabilityWeight(DistributionModel *model) const;
392  agx::Real calculateTotalQuantifiedWeight() const;
393 
394 
395  private:
396  Quantity m_quantity;
398  UniformRealGenerator m_randomGenerator;
399  agx::UInt32 m_seed;
400  bool m_needRebuild;
401  };
402 
403  /* Implementation */
404  AGX_FORCE_INLINE agxCollide::Geometry* agx::Emitter::getGeometry() { return m_geometry; }
405 
406  inline agx::Physics::CollisionGroupSetPtr agx::Emitter::getCollisionGroupSet() const { return m_entity.collisionGroupSet(); }
407 
409  {
410  return agxCollide::CollisionGroupManager::hasGroup(getCollisionGroupSet(), id);
411  }
412 
414  {
415  return agxCollide::CollisionGroupManager::hasGroup(getCollisionGroupSet(), id);
416  }
417 
418  AGX_FORCE_INLINE void agx::Emitter::setCollisionGroupSet(agx::Physics::CollisionGroupSetPtr set)
419  {
420  m_entity.collisionGroupSet() = set;
421  }
422 }
423 
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#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:221
#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
#define AGXPHYSICS_EXPORT
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:38
static bool hasGroup(agx::Physics::CollisionGroupSetPtr set, agx::UInt32 groupId)
The geometry representation used by the collision detection engine.
Definition: Geometry.h:93
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:52
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:45
Spawns new bodys inside a given volume.
Definition: Emitter.h:45
void addModel(DistributionModel *model)
Add a particle model.
agx::UInt getActivationCount() const
const DistributionModelRefVector & getModels() const
void setSeed(const agx::UInt32 seed)
Set the seed for the random number generator of the emitter.
bool getEnableOverrideRenderSettings() const
void setGeometry(agxCollide::Geometry *geometry)
Set the volume that bodies will be spawned inside.
void addCollisionGroup(agx::UInt32 id)
Add the specified id to a vector of group id, this will make a bodies emitted from this emitter part ...
agx::Physics::CollisionGroupSetPtr getCollisionGroupSet() const
Definition: Emitter.h:406
agx::Real getVolume() const
agx::Real calculateTotalWeight() const
agx::Real getMaximumEmittedQuantity() const
Get the maximum amount of quantity that this emitter is allowed to emit.
const Vec3 & getVelocity() const
Quantity getQuantity() const
ScalarParameterRef m_timestep
Definition: Emitter.h:272
void setMaximumEmittedQuantity(const agx::Real max)
Sets the maximum amount of objects that this emitter is allowed to emit.
void setRate(Real rate)
Set the rate in which the emitter emits objects, measured in the specified quantity QUANTITY_COUNT - ...
Real m_rest
Definition: Emitter.h:278
@ QUANTITY_COUNT
Definition: Emitter.h:49
@ QUANTITY_VOLUME
Definition: Emitter.h:50
agxCollide::GeometryRef m_proxySphere
Definition: Emitter.h:270
agxCollide::GeometryRef m_geometry
Definition: Emitter.h:269
void setVelocity(const Vec3 &velocity)
Set the initial velocity for the bodys when spawned, in local geometry coordinates.
void removeCollisionGroup(const agx::Name &name)
Quantity m_quantity
Definition: Emitter.h:277
DistributionModel * getRandomModel()
agx::Real getEmittedQuantity() const
Returns the quantity amount that this emitter has emitted.
agx::UInt getNumEmitted() const
void setProbabilityWeight(agx::Real weight)
Set the random probability weight.
DistributionTable * getDistributionTable()
void removeCollisionGroup(agx::UInt32 id)
Remove a group id from the vector of group ids.
void setSeed(agx::UInt32 seed)
Set the seed of the random number generator in the distribution table.
Physics::EmitterPtr m_entity
Definition: Emitter.h:266
agx::Real getMass() const
Emitter(Quantity quantity=QUANTITY_COUNT)
Create an emitter.
const DistributionTable * getDistributionTable() const
void setEnableOverrideRenderSettings(bool enable)
Sets whenever the render settings of emitted objects should be overriden.
void setProbabilityQuantity(Quantity quantity)
Set The probability quantity.
void addCollisionGroup(const agx::Name &name)
void setQuantity(Quantity quantity)
Set the emitting quantity QUANTITY_COUNT - number of emitted objects QUANTITY_VOLUME - volume of emit...
void setDistributionTable(DistributionTable *table)
Set a distribution table to be used for body attributes.
bool hasCollisionGroup(agx::UInt32 id) const
This is performing a linear search among the group id for this Emitter.
Definition: Emitter.h:408
UniformRealGenerator m_generator
Definition: Emitter.h:275
bool getEnableRenderEmittedObjects() const
DistributionTable(Quantity quantity=QUANTITY_COUNT)
agx::Real getProbabilityWeight() const
void setEnableRenderEmittedObjects(bool enable)
Sets whenever the emitted objects should be rendered or not, if possible.
agxCollide::SpaceRef m_space
Definition: Emitter.h:271
Real getRate() const
void setName(const agx::String &name)
DistributionModel(agx::Real probabilityWeight)
agx::BitState< StateFlags, agx::UInt32 > Flags
Definition: Emitter.h:252
DistributionTableRef m_distributionTable
Definition: Emitter.h:268
Quantity getProbabilityQuantity() const
agx::String getName() const
Flags m_state
Definition: Emitter.h:279
Representation of a name string.
Definition: Name.h:33
Pointer to a entity instance of type Physics.CollisionGroupSet.
Pointer to a entity instance of type Physics.Emitter.
Base class providing referencing counted objects.
Definition: Referenced.h:114
SerialTask.
Definition: Task.h:470
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: SetVector.h:38
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define CALLABLE
Definition: macros.h:72
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
#define AGX_FORCE_INLINE
Definition: macros.h:58
This namespace consists of a set of classes for handling geometric intersection tests including boole...
AGXCORE_EXPORT EntityModel * getEntity(const agx::Path &path)
Get a entity with a specified path.
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
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
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.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:40
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:874
void AGXPHYSICS_EXPORT init()
Initialize agx.
agx::SetVector< ref_ptr< Emitter > > EmitterRefSetVector
Definition: Emitter.h:38