AGX Dynamics 2.42.3.0
Loading...
Searching...
No Matches
Emitter.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17
18#pragma once
19
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
30namespace agxSDK {
31 class Simulation;
32}
33
34namespace agx
35{
39
40
45 {
46 public:
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
129 void setUseFixedPosition( bool enable, agx::Int axis = -1 );
130
138 bool getUseFixedPosition( agx::Int axis = -1 ) const;
139
150 void setUseFixedRotation( bool enable, agx::Int axis = -1 );
151
159 bool getUseFixedRotation( agx::Int axis = -1 ) const;
160
168 void setFixedPositionOffset( const agx::Vec3& fixedPositionOffset );
169
176 void setFixedRotationOffset( const agx::Quat& fixedRotationOffset );
177
182
187
191 DistributionTable *getDistributionTable();
192 const DistributionTable *getDistributionTable() const;
193
198
206
211
216
226 void addCollisionGroup(const agx::Name& name);
227
234
241 bool hasCollisionGroup(agx::UInt32 id) const;
242 bool hasCollisionGroup(agx::Name id) const;
243
247 agx::Physics::CollisionGroupSetPtr getCollisionGroupSet() const;
248
255
260
270
276
280 void setName(const agx::String& name);
281
283
285
286 Physics::EmitterPtr getEntity() const;
287
288 virtual void transfer(agxSDK::Simulation *simulation);
289 void setSpace(agxCollide::Space* space);
290
292 static agxStream::Serializable* create() { return nullptr; }
293 static agxStream::Serializable* create(agxStream::InputArchive&) { return nullptr; }
294
296
297 protected:
298 virtual void run() override;
299
301 virtual ~Emitter();
302
303 virtual void emitBody(const Vec3& position,
304 const Quat& rotation,
305 const Vec3& velocity,
306 Emitter::DistributionModel *model) = 0;
307 virtual bool preEmit() = 0;
308 virtual void postEmit() = 0;
309 virtual Real getDefaultElementQuantity(Quantity quantity);
310
311 virtual void store( agxStream::OutputArchive& out ) const override;
312 virtual void restore( agxStream::InputArchive& in ) override;
313
314 void setEmittedQuantity(agx::Real quantity);
315 void setEmittedCount(agx::UInt emittedCount);
316
317 // Generates a random number from the generator in the emitter
318 agx::Real generateRandomNumber();
319
320 // State flags for emitter
322 {
323 OVERRIDE_RENDER_SETTINGS = 1 << 0,
324 SHOULD_RENDER_EMITTED_OBJECTS = 1 << 1,
325 FIXED_POSITION_DEPRECATED = 1 << 2,
326 FIXED_ROTATION_DEPRECATED = 1 << 3,
327 FIXED_ROTATION_X = 1 << 4,
328 FIXED_ROTATION_Y = 1 << 5,
329 FIXED_ROTATION_Z = 1 << 6,
330 FIXED_POSITION_X = 1 << 7,
331 FIXED_POSITION_Y = 1 << 8,
332 FIXED_POSITION_Z = 1 << 9,
333 };
334 using Flags = agx::BitState<StateFlags, agx::UInt32>;
335
336 private:
337 // Creates and initializes the parameters.
338 void init();
339
340 // Few helper functions for randomly selecting points in space.
341 Vec3 randomPointInBox(const agxCollide::BoundingAABB* bound);
342 Vec3 randomPointInSphere(agx::Real maxRadius);
343 Vec3 randomPointInGeometry(agxCollide::Geometry* geometry, agxCollide::Geometry* proxySphere);
344
345 void setCollisionGroupSet(agx::Physics::CollisionGroupSetPtr set);
346
347 protected:
349
355
356 // Random number generator for body emitter
358
361
365 };
366
367
368 class AGXPHYSICS_EXPORT Emitter::DistributionModel : public Referenced, public virtual agxStream::Serializable
369 {
370 public:
372 DistributionModel(agx::Real probabilityWeight);
373
378
383
384
389
392
394
396 static agxStream::Serializable* create() { return nullptr; }
397 static agxStream::Serializable* create(agxStream::InputArchive&) { return nullptr; }
398
400
401 protected:
402 virtual ~DistributionModel();
403 virtual agx::Real calculateVolume() = 0;
404 virtual agx::Real calculateMass() = 0;
405 virtual void store( agxStream::OutputArchive& out ) const override;
406 virtual void restore( agxStream::InputArchive& in ) override;
407
408 private:
409 friend class DistributionTable;
410 void setDistribution(DistributionTable *distribution);
411
412 static bool rangeCompare(DistributionModel *model, agx::Real value);
413
414 protected:
415 DistributionTable *m_distribution;
416 agx::Real m_probabilityWeight;
417 agx::UInt m_activationCount;
418 agx::RangeReal m_range;
419 agx::Real m_volume;
420 agx::Real m_mass;
421 };
422
423 class AGXPHYSICS_EXPORT Emitter::DistributionTable : public Referenced, public virtual agxStream::Serializable
424 {
425 public:
426 DistributionTable(Quantity quantity = QUANTITY_COUNT);
427
431 void addModel(DistributionModel *model);
432
437
441 DistributionModel *getRandomModel();
442
447
452
457
462
464
465 AGXSTREAM_DECLARE_SERIALIZABLE(agx::Emitter::DistributionTable);
466
468
469 protected:
470 virtual ~DistributionTable();
471
472 private:
473 friend class DistributionModel;
474 void rebuildProbabilityTable();
475
476 agx::Real getQuantifiedProbabilityWeight(DistributionModel *model) const;
477 agx::Real calculateTotalQuantifiedWeight() const;
478
479
480 private:
481 Quantity m_quantity;
483 UniformRealGenerator m_randomGenerator;
484 agx::UInt32 m_seed;
485 bool m_needRebuild;
486 };
487
488 /* Implementation */
489 AGX_FORCE_INLINE agxCollide::Geometry* agx::Emitter::getGeometry() { return m_geometry; }
490
491 inline agx::Physics::CollisionGroupSetPtr agx::Emitter::getCollisionGroupSet() const { return m_entity.collisionGroupSet(); }
492
494 {
495 return agxCollide::CollisionGroupManager::hasGroup(getCollisionGroupSet(), id);
496 }
497
499 {
500 return agxCollide::CollisionGroupManager::hasGroup(getCollisionGroupSet(), id);
501 }
502
503 AGX_FORCE_INLINE void agx::Emitter::setCollisionGroupSet(agx::Physics::CollisionGroupSetPtr set)
504 {
505 m_entity.collisionGroupSet() = set;
506 }
507}
508
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#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:92
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
Spawns new bodys inside a given volume.
Definition: Emitter.h:45
void setFixedPositionOffset(const agx::Vec3 &fixedPositionOffset)
Set the fixed position offset of the objects created from the emitter.
void addModel(DistributionModel *model)
Add a particle model.
void setUseFixedRotation(bool enable, agx::Int axis=-1)
Set if a fixed rotation should be used on objects created from this emitter.
agx::UInt getActivationCount() 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 ...
DistributionTable * getDistributionTable()
agx::Physics::CollisionGroupSetPtr getCollisionGroupSet() const
Definition: Emitter.h:491
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.
Quantity getQuantity() const
ScalarParameterRef m_timestep
Definition: Emitter.h:354
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:363
@ QUANTITY_COUNT
Definition: Emitter.h:49
@ QUANTITY_VOLUME
Definition: Emitter.h:50
agxCollide::GeometryRef m_proxySphere
Definition: Emitter.h:352
agxCollide::GeometryRef m_geometry
Definition: Emitter.h:351
void setVelocity(const Vec3 &velocity)
Set the initial velocity for the bodys when spawned, in local geometry coordinates.
const DistributionModelRefVector & getModels() const
void removeCollisionGroup(const agx::Name &name)
Quantity m_quantity
Definition: Emitter.h:362
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.
agx::Quat m_fixedRotationOffset
Definition: Emitter.h:360
bool getUseFixedPosition(agx::Int axis=-1) const
const DistributionTable * getDistributionTable() const
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:348
agx::Quat getFixedRotationOffset() const
agx::Real getMass() const
Emitter(Quantity quantity=QUANTITY_COUNT)
Create an emitter.
bool getUseFixedRotation(agx::Int axis=-1) const
void setUseFixedPosition(bool enable, agx::Int axis=-1)
Set if fixed position should be used on objects created from this emitter.
void setEnableOverrideRenderSettings(bool enable)
Sets whenever the render settings of emitted objects should be overriden.
void setProbabilityQuantity(Quantity quantity)
Set The probability quantity.
const Vec3 & getVelocity() const
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:493
UniformRealGenerator m_generator
Definition: Emitter.h:357
DistributionModel * getRandomModel()
bool getEnableRenderEmittedObjects() const
DistributionTable(Quantity quantity=QUANTITY_COUNT)
agx::Vec3 m_fixedPositionOffset
Definition: Emitter.h:359
agx::Real getProbabilityWeight() const
void setFixedRotationOffset(const agx::Quat &fixedRotationOffset)
Set the fixed rotation offset of the objects created from the emitter.
void setEnableRenderEmittedObjects(bool enable)
Sets whenever the emitted objects should be rendered or not, if possible.
agxCollide::SpaceRef m_space
Definition: Emitter.h:353
Real getRate() const
void setName(const agx::String &name)
Set emitter name.
DistributionModel(agx::Real probabilityWeight)
agx::Vec3 getFixedPositionOffset() const
agx::BitState< StateFlags, agx::UInt32 > Flags
Definition: Emitter.h:334
DistributionTableRef m_distributionTable
Definition: Emitter.h:350
Quantity getProbabilityQuantity() const
agx::String getName() const
Flags m_state
Definition: Emitter.h:364
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:120
SerialTask.
Definition: Task.h:469
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:89
#define CALLABLE
Definition: macros.h:72
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:88
#define AGX_FORCE_INLINE
Definition: macros.h:58
This namespace consists of a set of classes for handling geometric intersection tests including boole...
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...
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:32
uint64_t UInt
Definition: Integer.h:27
double Real
Definition: Real.h:41
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API.
agx::SetVector< ref_ptr< Emitter > > EmitterRefSetVector
Definition: Emitter.h:38
int64_t Int
Definition: Integer.h:28