AGX Dynamics  2.37.2.0
Emitter.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 #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 
69  void setGeometry(agxCollide::Geometry* geometry);
70  agxCollide::Geometry* getGeometry();
71 
72 
79  void setQuantity(Quantity quantity);
80 
84  Quantity getQuantity() const;
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 
126  void setUseFixedPosition(bool enable);
127 
131  bool getUseFixedPosition() const;
132 
139  void setUseFixedRotation( bool enable );
140 
144  bool getUseFixedRotation() const;
145 
153  void setFixedPositionOffset( const agx::Vec3& fixedPositionOffset );
154 
161  void setFixedRotationOffset( const agx::Quat& fixedRotationOffset );
162 
166  agx::Vec3 getFixedPositionOffset() const;
167 
171  agx::Quat getFixedRotationOffset() const;
172 
176  DistributionTable *getDistributionTable();
177  const DistributionTable *getDistributionTable() const;
178 
182  agx::UInt getNumEmitted() const;
183 
190  void setMaximumEmittedQuantity(const agx::Real max);
191 
195  agx::Real getEmittedQuantity() const;
196 
200  agx::Real getMaximumEmittedQuantity() const;
201 
210  void addCollisionGroup(agx::UInt32 id);
211  void addCollisionGroup(const agx::Name& name);
212 
217  void removeCollisionGroup(agx::UInt32 id);
218  void removeCollisionGroup(const agx::Name& name);
219 
226  bool hasCollisionGroup(agx::UInt32 id) const;
227  bool hasCollisionGroup(agx::Name id) const;
228 
232  agx::Physics::CollisionGroupSetPtr getCollisionGroupSet() const;
233 
239  void setEnableOverrideRenderSettings(bool enable);
240 
244  bool getEnableOverrideRenderSettings() const;
245 
254  void setEnableRenderEmittedObjects(bool enable);
255 
260  bool getEnableRenderEmittedObjects() const;
261 
265  void setName(const agx::String& name);
266 
267  agx::String getName() const;
268 
270 
271  Physics::EmitterPtr getEntity() const;
272 
273  virtual void transfer(agxSDK::Simulation *simulation);
274  void setSpace(agxCollide::Space* space);
275 
277  static agxStream::Serializable* create() { return nullptr; }
278  static agxStream::Serializable* create(agxStream::InputArchive&) { return nullptr; }
279 
281 
282  protected:
283  virtual void run() override;
284 
286  virtual ~Emitter();
287 
288  virtual void emitBody(const Vec3& position,
289  const Quat& rotation,
290  const Vec3& velocity,
291  Emitter::DistributionModel *model) = 0;
292  virtual bool preEmit() = 0;
293  virtual void postEmit() = 0;
294  virtual Real getDefaultElementQuantity(Quantity quantity);
295 
296  virtual void store( agxStream::OutputArchive& out ) const override;
297  virtual void restore( agxStream::InputArchive& in ) override;
298 
299  void setEmittedQuantity(agx::Real quantity);
300  void setEmittedCount(agx::UInt emittedCount);
301 
302  // Generates a random number from the generator in the emitter
303  agx::Real generateRandomNumber();
304 
305  // State flags for emitter
307  {
308  OVERRIDE_RENDER_SETTINGS = 1 << 0,
309  SHOULD_RENDER_EMITTED_OBJECTS = 1 << 1,
310  FIXED_POSITION = 1 << 2,
311  FIXED_ROTATION = 1 << 3
312  };
313  using Flags = agx::BitState<StateFlags, agx::UInt32>;
314 
315  private:
316  // Creates and initializes the parameters.
317  void init();
318 
319  // Few helper functions for randomly selecting points in space.
320  Vec3 randomPointInBox(const agxCollide::BoundingAABB* bound);
321  Vec3 randomPointInSphere(agx::Real maxRadius);
322  Vec3 randomPointInGeometry(agxCollide::Geometry* geometry, agxCollide::Geometry* proxySphere);
323 
324  void setCollisionGroupSet(agx::Physics::CollisionGroupSetPtr set);
325 
326  protected:
328 
334 
335  // Random number generator for body emitter
337 
340 
344  };
345 
346 
347  class AGXPHYSICS_EXPORT Emitter::DistributionModel : public Referenced, public virtual agxStream::Serializable
348  {
349  public:
350  DistributionModel();
351  DistributionModel(agx::Real probabilityWeight);
352 
356  void setProbabilityWeight(agx::Real weight);
357 
361  agx::Real getProbabilityWeight() const;
362 
363 
367  agx::UInt getActivationCount() const;
368 
369  agx::Real getVolume() const;
370  agx::Real getMass() const;
371 
373 
375  static agxStream::Serializable* create() { return nullptr; }
376  static agxStream::Serializable* create(agxStream::InputArchive&) { return nullptr; }
377 
379 
380  protected:
381  virtual ~DistributionModel();
382  virtual agx::Real calculateVolume() = 0;
383  virtual agx::Real calculateMass() = 0;
384  virtual void store( agxStream::OutputArchive& out ) const override;
385  virtual void restore( agxStream::InputArchive& in ) override;
386 
387  private:
388  friend class DistributionTable;
389  void setDistribution(DistributionTable *distribution);
390 
391  static bool rangeCompare(DistributionModel *model, agx::Real value);
392 
393  protected:
394  DistributionTable *m_distribution;
400  };
401 
402  class AGXPHYSICS_EXPORT Emitter::DistributionTable : public Referenced, public virtual agxStream::Serializable
403  {
404  public:
405  DistributionTable(Quantity quantity = QUANTITY_COUNT);
406 
410  void addModel(DistributionModel *model);
411 
415  const DistributionModelRefVector& getModels() const;
416 
420  DistributionModel *getRandomModel();
421 
425  void setSeed(agx::UInt32 seed);
426 
430  agx::Real calculateTotalWeight() const;
431 
435  void setProbabilityQuantity(Quantity quantity);
436 
440  Quantity getProbabilityQuantity() const;
441 
443 
445 
447 
448  protected:
449  virtual ~DistributionTable();
450 
451  private:
452  friend class DistributionModel;
453  void rebuildProbabilityTable();
454 
455  agx::Real getQuantifiedProbabilityWeight(DistributionModel *model) const;
456  agx::Real calculateTotalQuantifiedWeight() const;
457 
458 
459  private:
460  Quantity m_quantity;
462  UniformRealGenerator m_randomGenerator;
463  agx::UInt32 m_seed;
464  bool m_needRebuild;
465  };
466 
467  /* Implementation */
469 
470  inline agx::Physics::CollisionGroupSetPtr agx::Emitter::getCollisionGroupSet() const { return m_entity.collisionGroupSet(); }
471 
473  {
474  return agxCollide::CollisionGroupManager::hasGroup(getCollisionGroupSet(), id);
475  }
476 
478  {
479  return agxCollide::CollisionGroupManager::hasGroup(getCollisionGroupSet(), id);
480  }
481 
482  AGX_FORCE_INLINE void agx::Emitter::setCollisionGroupSet(agx::Physics::CollisionGroupSetPtr set)
483  {
484  m_entity.collisionGroupSet() = set;
485  }
486 }
487 
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 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...
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:855
This namespace consists of a set of classes for handling geometric intersection tests including boole...
DistributionTable * m_distribution
Definition: Emitter.h:394
friend class DistributionTable
Definition: Emitter.h:388
bool hasCollisionGroup(agx::UInt32 id) const
This is performing a linear search among the group id for this Emitter.
Definition: Emitter.h:472
agx::Real m_probabilityWeight
Definition: Emitter.h:395
DistributionTableRef m_distributionTable
Definition: Emitter.h:329
#define AGXPHYSICS_EXPORT
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
agx::Real m_volume
Definition: Emitter.h:398
agx::SetVector< ref_ptr< Emitter > > EmitterRefSetVector
Definition: Emitter.h:38
agxCollide::GeometryRef m_proxySphere
Definition: Emitter.h:331
SerialTask.
Definition: Task.h:469
Spawns new bodys inside a given volume.
Definition: Emitter.h:44
Pointer to a entity instance of type Physics.CollisionGroupSet.
Base class providing referencing counted objects.
Definition: Referenced.h:119
agxCollide::GeometryRef m_geometry
Definition: Emitter.h:330
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
agx::Physics::CollisionGroupSetPtr getCollisionGroupSet() const
Definition: Emitter.h:470
uint32_t UInt
Definition: Integer.h:34
agx::BitState< StateFlags, agx::UInt32 > Flags
Definition: Emitter.h:313
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
agxCollide::Geometry * getGeometry()
Definition: Emitter.h:468
Quantity m_quantity
Definition: Emitter.h:341
ScalarParameterRef m_timestep
Definition: Emitter.h:333
Pointer to a entity instance of type Physics.Emitter.
agx::RangeReal m_range
Definition: Emitter.h:397
#define CALLABLE
Definition: Object.h:73
agx::Quat m_fixedRotationOffset
Definition: Emitter.h:339
agx::Real m_mass
Definition: Emitter.h:399
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:207
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::Vec3 m_fixedPositionOffset
Definition: Emitter.h:338
agx::UInt m_activationCount
Definition: Emitter.h:396
#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
agxCollide::SpaceRef m_space
Definition: Emitter.h:332
Physics::EmitterPtr m_entity
Definition: Emitter.h:327
double Real
Definition: Real.h:42
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:37
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
UniformRealGenerator m_generator
Definition: Emitter.h:336
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
Real m_rest
Definition: Emitter.h:342
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: SetVector.h:37
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
AGXCORE_EXPORT EntityModel * getEntity(const agx::Path &path)
Get a entity with a specified path.
Flags m_state
Definition: Emitter.h:343