ParticleAPI 3.0.0 Performant particle system API in C++ for interactive graphics

pInlineActionsAPI.h
Go to the documentation of this file.
1/// PInlineActionsAPI.h
2///
3/// Copyright 1997-2007, 2022 by David K. McAllister
4///
5/// Only application code should include this.
6/// This file contains the host interface definitions to the inline particle actions.
7/// It is a header file so that the action implementations are inlined.
8/// The intended usage is as follows:
9///
10/// P.ParticleLoop(std::execution::par_unseq, [&](Particle_t& m) {
11/// P.I.Gravity(Efx.GravityVec);
12/// P.I.Bounce(0.f, 0.5f, 0.f, PDDisc(pVec(0, 0, 1.f), pVec(0, 0, 1.f), 5));
13/// P.I.Move(true, false);
14/// });
15
16// TODO: Fix PInternalState.
17//#include "PInternalState.h"
18#include "Particle/pAPI.h"
19#include "Particle/pActionImpls.h"
20
21namespace PAPI {
22
23#ifdef _DEBUG
24#define P_CHECK_ERR
25 PASSERT(!PSh.get_in_new_list(), "Can't call inline action while making action list");
26 PASSERT(PSh.get_in_particle_loop(), "Can only call inline action while inside ParticleLoop")
27#else
28#define P_CHECK_ERR
29#endif
30
31 PINLINE void PContextActions_t::Avoid(Particle_t& m, const float magnitude, const float epsilon, const float look_ahead, const pDomain& dom)
32{
33 P_CHECK_ERR;
34 switch (dom.Which) {
35 case PDDisc_e: PAAvoidDisc_Impl(m, PSh.get_dt(), *dynamic_cast<const PDDisc*>(&dom), look_ahead, magnitude, epsilon); return;
36 case PDPlane_e: PAAvoidPlane_Impl(m, PSh.get_dt(), *dynamic_cast<const PDPlane*>(&dom), look_ahead, magnitude, epsilon); return;
37 case PDRectangle_e: PAAvoidRectangle_Impl(m, PSh.get_dt(), *dynamic_cast<const PDRectangle*>(&dom), look_ahead, magnitude, epsilon); return;
38 case PDSphere_e: PAAvoidSphere_Impl(m, PSh.get_dt(), *dynamic_cast<const PDSphere*>(&dom), look_ahead, magnitude, epsilon); return;
39 case PDTriangle_e: PAAvoidTriangle_Impl(m, PSh.get_dt(), *dynamic_cast<const PDTriangle*>(&dom), look_ahead, magnitude, epsilon); return;
40 default: return;
41 }
42}
43
44 PINLINE void PContextActions_t::Bounce(Particle_t& m, const float friction, const float resilience, const float fric_min_vel, const pDomain& dom)
45{
46 P_CHECK_ERR;
47 switch (dom.Which) {
48 case PDBox_e: PABounceBox_Impl(m, PSh.get_dt(), *static_cast<const PDBox*>(&dom), friction, resilience, fric_min_vel); return;
49 case PDDisc_e: PABounceDisc_Impl(m, PSh.get_dt(), *static_cast<const PDDisc*>(&dom), friction, resilience, fric_min_vel); return;
50 case PDPlane_e: PABouncePlane_Impl(m, PSh.get_dt(), *static_cast<const PDPlane*>(&dom), friction, resilience, fric_min_vel); return;
51 case PDRectangle_e: PABounceRectangle_Impl(m, PSh.get_dt(), *static_cast<const PDRectangle*>(&dom), friction, resilience, fric_min_vel); return;
52 case PDSphere_e: PABounceSphere_Impl(m, PSh.get_dt(), *static_cast<const PDSphere*>(&dom), friction, resilience, fric_min_vel); return;
53 case PDTriangle_e: PABounceTriangle_Impl(m, PSh.get_dt(), *static_cast<const PDTriangle*>(&dom), friction, resilience, fric_min_vel); return;
54 default: return;
55 }
56}
57
58 PINLINE void PContextActions_t::CopyVertexB(Particle_t& m, const bool copy_pos, const bool copy_vel)
59{
60 P_CHECK_ERR;
61 PACopyVertexB_Impl(m, PSh.get_dt(), copy_pos, copy_vel);
62}
63
64 PINLINE void PContextActions_t::Damping(Particle_t& m, const pVec& damping, const float min_vel, const float max_vel)
65{
66 P_CHECK_ERR;
67 PADamping_Impl(m, PSh.get_dt(), damping, min_vel, max_vel);
68}
69
70 PINLINE void PContextActions_t::RotDamping(Particle_t& m, const pVec& damping, const float min_vel, const float max_vel)
71{
72 P_CHECK_ERR;
73 PARotDamping_Impl(m, PSh.get_dt(), damping, min_vel, max_vel);
74}
75
76 PINLINE void PContextActions_t::Explosion(Particle_t& m, const pVec& center, const float radius, const float magnitude, const float stdev, const float epsilon)
77{
78 P_CHECK_ERR;
79 PAExplosion_Impl(m, PSh.get_dt(), center, radius, magnitude, stdev, epsilon);
80}
81
82 PINLINE void PContextActions_t::Gravity(Particle_t& m, const pVec& dir)
83{
84 P_CHECK_ERR;
85 PAGravity_Impl(m, PSh.get_dt(), dir);
86}
87
88 PINLINE void PContextActions_t::Jet(Particle_t& m, const pDomain& dom, const pDomain& accel)
89{
90 P_CHECK_ERR;
91 PAJet_Impl(m, PSh.get_dt(), dom, accel);
92}
93
94 PINLINE void PContextActions_t::Move(Particle_t& m, const bool move_velocity, const bool move_rotational_velocity)
95{
96 P_CHECK_ERR;
97 PAMove_Impl(m, PSh.get_dt(), move_velocity, move_rotational_velocity);
98}
99
100 PINLINE void PContextActions_t::OrbitLine(Particle_t& m, const pVec& p, const pVec& axis, const float magnitude, const float epsilon, const float max_radius)
101{
102 P_CHECK_ERR;
103 PAOrbitLine_Impl(m, PSh.get_dt(), p, axis, magnitude, epsilon, max_radius);
104}
105
106 PINLINE void PContextActions_t::OrbitPoint(Particle_t& m, const pVec& center, const float magnitude, const float epsilon, const float max_radius)
107{
108 P_CHECK_ERR;
109 PAOrbitPoint_Impl(m, PSh.get_dt(), center, magnitude, epsilon, max_radius);
110}
111
112 PINLINE void PContextActions_t::RandomAccel(Particle_t& m, const pDomain& gen_acc)
113{
114 P_CHECK_ERR;
115 PARandomAccel_Impl(m, PSh.get_dt(), gen_acc);
116}
117
118 PINLINE void PContextActions_t::RandomDisplace(Particle_t& m, const pDomain& gen_disp)
119{
120 P_CHECK_ERR;
121 PARandomDisplace_Impl(m, PSh.get_dt(), gen_disp);
122}
123
124 PINLINE void PContextActions_t::RandomVelocity(Particle_t& m, const pDomain& gen_vel)
125{
126 P_CHECK_ERR;
127 PARandomVelocity_Impl(m, PSh.get_dt(), gen_vel);
128}
129
130 PINLINE void PContextActions_t::RandomRotVelocity(Particle_t& m, const pDomain& gen_vel)
131{
132 P_CHECK_ERR;
133 PARandomRotVelocity_Impl(m, PSh.get_dt(), gen_vel);
134}
135
136 PINLINE void PContextActions_t::Restore(Particle_t& m, const float time_left, const bool vel, const bool rvel)
137{
138 P_CHECK_ERR;
139 PARestore_Impl(m, PSh.get_dt(), time_left, vel, rvel);
140}
141
142 PINLINE void PContextActions_t::SpeedClamp(Particle_t& m, const float min_speed, const float max_speed)
143{
144 P_CHECK_ERR;
145 PASpeedClamp_Impl(m, PSh.get_dt(), min_speed, max_speed);
146}
147
148 PINLINE void PContextActions_t::TargetColor(Particle_t& m, const pVec& color, const float alpha, const float scale)
149{
150 P_CHECK_ERR;
151 PATargetColor_Impl(m, PSh.get_dt(), color, alpha, scale);
152}
153
154 PINLINE void PContextActions_t::TargetSize(Particle_t& m, const pVec& size, const pVec& scale)
155{
156 P_CHECK_ERR;
157 PATargetSize_Impl(m, PSh.get_dt(), size, scale);
158}
159
160 PINLINE void PContextActions_t::TargetVelocity(Particle_t& m, const pVec& vel, const float scale)
161{
162 P_CHECK_ERR;
163 PATargetVelocity_Impl(m, PSh.get_dt(), vel, scale);
164}
165
166 PINLINE void PContextActions_t::TargetRotVelocity(Particle_t& m, const pVec& rot_velocity, const float scale)
167{
168 P_CHECK_ERR;
169 PATargetRotVelocity_Impl(m, PSh.get_dt(), rot_velocity, scale);
170}
171
172 PINLINE void PContextActions_t::Vortex(Particle_t& m, const pVec& tip, const pVec& axis, const float tightnessExponent, const float max_radius,
173 const float inSpeed, const float upSpeed, const float aroundSpeed)
174{
175 P_CHECK_ERR;
176 PAVortex_Impl(m, PSh.get_dt(), tip, axis, tightnessExponent, max_radius, inSpeed, upSpeed, aroundSpeed);
177}
178
179//////////////////////////////////////////////////////////////////
180// Inter-particle actions
181
182 PINLINE void PContextActions_t::Follow(Particle_t& m, const float magnitude, const float epsilon, const float max_radius)
183{
184 P_CHECK_ERR;
185 PAFollow_Impl(m, PSh.get_dt(), magnitude, epsilon, max_radius, PSh.get_const_pgroup_begin(), PSh.get_const_pgroup_end());
186}
187
188 PINLINE void PContextActions_t::Gravitate(Particle_t& m, const float magnitude, const float epsilon, const float max_radius)
189{
190 P_CHECK_ERR;
191 PAGravitate_Impl(m, PSh.get_dt(), magnitude, epsilon, max_radius, PSh.get_const_pgroup_begin(), PSh.get_const_pgroup_end());
192}
193
194 PINLINE void PContextActions_t::MatchVelocity(Particle_t& m, const float magnitude, const float epsilon, const float max_radius)
195{
196 P_CHECK_ERR;
197 PAMatchVelocity_Impl(m, PSh.get_dt(), magnitude, epsilon, max_radius, PSh.get_const_pgroup_begin(), PSh.get_const_pgroup_end());
198}
199
200 PINLINE void PContextActions_t::MatchRotVelocity(Particle_t& m, const float magnitude, const float epsilon, const float max_radius)
201{
202 P_CHECK_ERR;
203 PAMatchRotVelocity_Impl(m, PSh.get_dt(), magnitude, epsilon, max_radius, PSh.get_const_pgroup_begin(), PSh.get_const_pgroup_end());
204}
205
206//////////////////////////////////////////////////////////////////
207// Other exceptional actions
208
209 PINLINE void PContextActions_t::Callback(Particle_t& m, P_PARTICLE_CALLBACK_ACTION callbackFunc, const pdata_t call_data)
210{
211 P_CHECK_ERR;
212 (*callbackFunc)(m, call_data, PSh.get_dt());
213}
214
215 PINLINE void PContextActions_t::KillOld(Particle_t& m, const float age_limit, const bool kill_less_than)
216{
217 P_CHECK_ERR;
218 PAKillOld_Impl(m, PSh.get_dt(), age_limit, kill_less_than);
219}
220
221 PINLINE void PContextActions_t::Sink(Particle_t& m, const bool kill_inside, const pDomain& kill_pos_dom)
222{
223 P_CHECK_ERR;
224 PASink_Impl(m, PSh.get_dt(), kill_inside, kill_pos_dom);
225}
226
227 PINLINE void PContextActions_t::SinkVelocity(Particle_t& m, const bool kill_inside, const pDomain& kill_vel_dom)
228{
229 P_CHECK_ERR;
230 PASinkVelocity_Impl(m, PSh.get_dt(), kill_inside, kill_vel_dom);
231}
232
233#undef P_CHECK_ERR
234}; // namespace PAPI
PAPI::PContextActions_t
This class contains the Action API.
Definition: pAPIContext.h:332
PAPI::PContextActions_t::RandomDisplace
void RandomDisplace(Particle_t &m, const pDomain &dom)
Immediately displace position by a random amount.
Definition: pInlineActionsAPI.h:118
PAPI::PContextActions_t::TargetRotVelocity
void TargetRotVelocity(Particle_t &m, const pVec &rvel, const float scale)
Change rotational velocity of all particles toward the specified rotational velocity.
Definition: pInlineActionsAPI.h:166
PAPI::PContextActions_t::RandomAccel
void RandomAccel(Particle_t &m, const pDomain &dom)
Accelerate particles in random directions.
Definition: pInlineActionsAPI.h:112
PAPI::PContextActions_t::SinkVelocity
void SinkVelocity(Particle_t &m, const bool kill_inside, const pDomain &kill_vel_dom)
Kill particles that have velocities on wrong side of the specified domain.
Definition: pInlineActionsAPI.h:227
PAPI::PContextActions_t::MatchVelocity
void MatchVelocity(Particle_t &m, const float magnitude=1.0f, const float epsilon=P_EPS, const float max_radius=P_MAXFLOAT)
Modify each particle's velocity to be similar to that of its neighbors.
Definition: pInlineActionsAPI.h:194
PAPI::PContextActions_t::Restore
void Restore(Particle_t &m, const float time, const bool vel=true, const bool rvel=true)
Over time, restore particles to their target positionB and upB.
Definition: pInlineActionsAPI.h:136
PAPI::PContextActions_t::Callback
void Callback(Particle_t &m, P_PARTICLE_CALLBACK_ACTION callbackFunc, const pdata_t call_data=0)
Call an arbitrary user-provided function on each particle in the group.
Definition: pInlineActionsAPI.h:209
PAPI::PContextActions_t::Move
void Move(Particle_t &m, const bool move_velocity=true, const bool move_rotational_velocity=true)
Apply the particles' velocities to their positions, and age the particles.
Definition: pInlineActionsAPI.h:94
PAPI::PContextActions_t::Jet
void Jet(Particle_t &m, const pDomain &dom, const pDomain &acc)
For particles in the domain of influence, accelerate them with a domain.
Definition: pInlineActionsAPI.h:88
PAPI::PContextActions_t::OrbitLine
void OrbitLine(Particle_t &m, const pVec &p, const pVec &axis, const float magnitude=1.0f, const float epsilon=P_EPS, const float max_radius=P_MAXFLOAT)
Accelerate particles toward the closest point on the given line.
Definition: pInlineActionsAPI.h:100
PAPI::PContextActions_t::KillOld
void KillOld(Particle_t &m, const float age_limit, const bool kill_less_than=false)
Get rid of older particles.
Definition: pInlineActionsAPI.h:215
PAPI::PContextActions_t::Bounce
void Bounce(Particle_t &m, const float friction, const float resilience, const float fric_min_vel, const pDomain &dom)
Bounce particles off an object defined by a domain.
Definition: pInlineActionsAPI.h:44
PAPI::PContextActions_t::Damping
void Damping(Particle_t &m, const pVec &damping, const float min_vel=0.0f, const float max_vel=P_MAXFLOAT)
Simulate air by dampening particle velocities.
Definition: pInlineActionsAPI.h:64
PAPI::PContextActions_t::TargetVelocity
void TargetVelocity(Particle_t &m, const pVec &vel, const float scale)
Change velocity of all particles toward the specified velocity.
Definition: pInlineActionsAPI.h:160
PAPI::PContextActions_t::Explosion
void Explosion(Particle_t &m, const pVec &center, const float radius, const float magnitude, const float sigma, const float epsilon=P_EPS)
Exert force on each particle away from explosion center.
Definition: pInlineActionsAPI.h:76
PAPI::PContextActions_t::Gravity
void Gravity(Particle_t &m, const pVec &dir)
Accelerate particles in the given direction.
Definition: pInlineActionsAPI.h:82
PAPI::PContextActions_t::Gravitate
void Gravitate(Particle_t &m, const float magnitude=1.0f, const float epsilon=P_EPS, const float max_radius=P_MAXFLOAT)
Accelerate each particle toward each other particle.
Definition: pInlineActionsAPI.h:188
PAPI::PContextActions_t::PSh
PInternalShadow_t PSh
Definition: pAPIContext.h:1097
PAPI::PContextActions_t::Follow
void Follow(Particle_t &m, const float magnitude=1.0f, const float epsilon=P_EPS, const float max_radius=P_MAXFLOAT)
Accelerate toward the next particle in the list.
Definition: pInlineActionsAPI.h:182
PAPI::PContextActions_t::Avoid
void Avoid(Particle_t &m, const float magnitude, const float epsilon, const float look_ahead, const pDomain &dom)
Steer particles away from a domain of space.
Definition: pInlineActionsAPI.h:31
PAPI::PContextActions_t::Vortex
void Vortex(Particle_t &m, const pVec &tip, const pVec &axis, const float tightnessExponent, const float max_radius, const float inSpeed, const float upSpeed, const float aroundSpeed)
Accelerate particles in a vortex-like way.
Definition: pInlineActionsAPI.h:172
PAPI::PContextActions_t::OrbitPoint
void OrbitPoint(Particle_t &m, const pVec &center, const float magnitude=1.0f, const float epsilon=P_EPS, const float max_radius=P_MAXFLOAT)
Accelerate particles toward the given center point.
Definition: pInlineActionsAPI.h:106
PAPI::PContextActions_t::Sink
void Sink(Particle_t &m, const bool kill_inside, const pDomain &kill_pos_dom)
Kill particles that have positions on wrong side of the specified domain.
Definition: pInlineActionsAPI.h:221
PAPI::PContextActions_t::SpeedClamp
void SpeedClamp(Particle_t &m, const float min_speed, const float max_speed)
Clamp particle velocities to the given range.
Definition: pInlineActionsAPI.h:142
PAPI::PContextActions_t::MatchRotVelocity
void MatchRotVelocity(Particle_t &m, const float magnitude=1.0f, const float epsilon=P_EPS, const float max_radius=P_MAXFLOAT)
Modify each particle's rotational velocity to be similar to that of its neighbors.
Definition: pInlineActionsAPI.h:200
PAPI::PContextActions_t::CopyVertexB
void CopyVertexB(Particle_t &m, const bool copy_pos=true, const bool copy_vel=false)
Set the secondary position and velocity from current.
Definition: pInlineActionsAPI.h:58
PAPI::PContextActions_t::TargetSize
void TargetSize(Particle_t &m, const pVec &size, const pVec &scale)
Change sizes of all particles toward the specified size.
Definition: pInlineActionsAPI.h:154
PAPI::PContextActions_t::RotDamping
void RotDamping(Particle_t &m, const pVec &damping, const float min_vel=0.0f, const float max_vel=P_MAXFLOAT)
Simulate air by dampening rotational velocities.
Definition: pInlineActionsAPI.h:70
PAPI::PContextActions_t::RandomRotVelocity
void RandomRotVelocity(Particle_t &m, const pDomain &dom)
Immediately assign a random rotational velocity.
Definition: pInlineActionsAPI.h:130
PAPI::PContextActions_t::RandomVelocity
void RandomVelocity(Particle_t &m, const pDomain &dom)
Replace particle velocity with a random velocity.
Definition: pInlineActionsAPI.h:124
PAPI::PContextActions_t::TargetColor
void TargetColor(Particle_t &m, const pVec &color, const float alpha, const float scale)
Change color of all particles toward the specified color.
Definition: pInlineActionsAPI.h:148
PAPI::pDomain
A representation of a region of space.
Definition: pDomain.h:55
PAPI::pDomain::Which
pDomainType_E Which
Definition: pDomain.h:58
PAPI::pVec
A single-precision floating point three-vector.
Definition: pVec.h:63
PAPI
PAPIClasses.h.
Definition: pAPIContext.h:16
PAPI::PDPlane_e
@ PDPlane_e
Definition: pDomain.h:29
PAPI::PDTriangle_e
@ PDTriangle_e
Definition: pDomain.h:26
PAPI::PDDisc_e
@ PDDisc_e
Definition: pDomain.h:28
PAPI::PDSphere_e
@ PDSphere_e
Definition: pDomain.h:33
PAPI::PDRectangle_e
@ PDRectangle_e
Definition: pDomain.h:27
PAPI::PDBox_e
@ PDBox_e
Definition: pDomain.h:30
PAPI::P_PARTICLE_CALLBACK_ACTION
void(* P_PARTICLE_CALLBACK_ACTION)(struct Particle_t &particle, const pdata_t data, const float dt)
This is the type of the callback functions that you can register for the Callback() action.
Definition: pDeclarations.h:28
PAPI::pdata_t
unsigned int pdata_t
Definition: pDeclarations.h:16
P_CHECK_ERR
#define P_CHECK_ERR
Definition: pInlineActionsAPI.h:28
PINLINE
#define PINLINE
Definition: pVec.h:20
PAPI::PDBox
Axis-aligned bounding box (AABB)
Definition: pDomain.h:523
PAPI::PDDisc
Arbitrarily-oriented disc.
Definition: pDomain.h:392
PAPI::PDPlane
Arbitrarily-oriented plane.
Definition: pDomain.h:478
PAPI::PDRectangle
Rhombus-shaped planar region.
Definition: pDomain.h:325
PAPI::PDSphere
Sphere.
Definition: pDomain.h:808
PAPI::PDTriangle
A Triangle.
Definition: pDomain.h:252
PAPI::Particle_t
Definition: pParticle.h:18