-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathEmitter.cpp
More file actions
401 lines (358 loc) · 12.1 KB
/
Copy pathEmitter.cpp
File metadata and controls
401 lines (358 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "Emitter.h"
#include "SPHKernels.h"
#include <iostream>
#include "TimeManager.h"
#include "TimeStep.h"
#include "FluidModel.h"
#include "Simulation.h"
using namespace SPH;
Emitter::Emitter(FluidModel *model,
const unsigned int width, const unsigned int height,
const Vector3r &pos, const Matrix3r & rotation,
const Real velocity,
const unsigned int type)
: m_model(model)
, m_width(width)
, m_height(height)
, m_x(pos)
, m_rotation(rotation)
, m_velocity(velocity)
, m_type(type)
, m_nextEmitTime(0)
, m_emitStartTime(0)
, m_emitEndTime(std::numeric_limits<Real>::max())
, m_emitCounter(0)
{
}
Emitter::~Emitter(void)
{
}
void Emitter::reset()
{
m_nextEmitTime = m_emitStartTime;
m_emitCounter = 0;
}
Vector3r Emitter::getSize(const Real width, const Real height, const int type)
{
Simulation *sim = Simulation::getCurrent();
const Real radius = sim->getParticleRadius();
const Real diam = static_cast<Real>(2.0)*radius;
const Real supportRadius = sim->getSupportRadius();
const Real animationMarginAround = diam;
Vector3r size;
if (type == 0)
{
if (sim->getBoundaryHandlingMethod() == BoundaryHandlingMethods::Akinci2012)
{
size = {
2 * supportRadius,
height * diam + 2 * animationMarginAround,
width * diam + 2 * animationMarginAround
};
}
else
{
size = {
static_cast<Real>(2.0)* supportRadius,
height * diam + static_cast<Real>(2.5) * animationMarginAround,
width * diam + static_cast<Real>(2.5) * animationMarginAround
};
}
}
else
{
if (sim->getBoundaryHandlingMethod() == BoundaryHandlingMethods::Akinci2012)
{
// height and radius of cylinder
const Real h = 2 * supportRadius;
const Real r = 0.5f * width * diam + animationMarginAround;
size = { h, 2 * r, 2 * r };
}
else
{
// height and radius of cylinder
const Real h = static_cast<Real>(2.25) * supportRadius;
const Real r = 0.5f * width * diam + animationMarginAround;
size = { h, static_cast<Real>(2.25) * r, static_cast<Real>(2.25) * r };
}
}
return size;
}
void Emitter::emitParticles(std::vector <unsigned int> &reusedParticles, unsigned int &indexReuse, unsigned int &numEmittedParticles)
{
TimeManager *tm = TimeManager::getCurrent();
const Real t = tm->getTime();
const Real timeStepSize = tm->getTimeStepSize();
const Vector3r & emitDir = m_rotation.col(0);
Vector3r emitVel = m_velocity * emitDir;
Simulation *sim = Simulation::getCurrent();
const Real radius = sim->getParticleRadius();
const Real diam = static_cast<Real>(2.0)*radius;
// shortly before the emitter starts, cleanup the emitter from particles
if (t < m_emitStartTime || t > m_emitEndTime)
emitVel = emitDir * radius * 10 / 0.25;
if (t >= m_emitStartTime - 0.25 && t <= m_emitEndTime)
{
// animate emitted particles
const Vector3r & x0 = m_x;
const Real animationMarginAhead = sim->getSupportRadius();
const Vector3r size = getSize(static_cast<Real>(m_width), static_cast<Real>(m_height), m_type);
const Vector3r halfSize = 0.5 * size;
const Vector3r pos = x0 + static_cast<Real>(0.5) * animationMarginAhead * emitDir;
const unsigned int nModels = sim->numberOfFluidModels();
for (unsigned int m = 0; m < nModels; m++)
{
FluidModel *fm = sim->getFluidModel(m);
const unsigned int numParticles = fm->numActiveParticles();
#pragma omp parallel for schedule(static) default(shared)
for (int i = 0; i < (int)numParticles; i++)
{
Vector3r &xi = fm->getPosition(i);
if (inBox(xi, pos, m_rotation, halfSize))
{
fm->getVelocity(i) = emitVel;
fm->getPosition(i) += timeStepSize * emitVel;
fm->setParticleState(i, ParticleState::AnimatedByEmitter);
fm->setObjectId(i, m_objectId);
}
}
}
}
if (t < m_nextEmitTime || t > m_emitEndTime)
{
return;
}
const Vector3r axisHeight = m_rotation.col(1);
const Vector3r axisWidth = m_rotation.col(2);
const Real startX = -static_cast<Real>(0.5)*(m_width - 1)*diam;
const Real startZ = -static_cast<Real>(0.5)*(m_height - 1)*diam;
// t-m_nextEmitTime is the time that has passed between the time the particle has been emitted and the current time step.
// timeStepSize is added because emission happens at the end of the time step, but the particles are not animated anymore.
const Real dt = t - m_nextEmitTime + timeStepSize;
const Vector3r velocityOffset = dt * emitVel;
const Vector3r offset = m_x + velocityOffset;
if ((m_model->numActiveParticles() < m_model->numParticles()) ||
(reusedParticles.size() > 0))
{
unsigned int indexNotReuse = m_model->numActiveParticles();
for (unsigned int i = 0; i < m_width; i++)
{
for (unsigned int j = 0; j < m_height; j++)
{
unsigned int index = 0;
bool reused = false;
if (indexReuse < reusedParticles.size())
{
index = reusedParticles[indexReuse];
reused = true;
}
else
{
index = indexNotReuse;
}
if (index < m_model->numParticles())
{
m_model->getPosition(index) = (i*diam + startX)*axisWidth + (j*diam + startZ)*axisHeight + offset;
m_model->getVelocity(index) = emitVel;
m_model->setParticleState(index, ParticleState::AnimatedByEmitter);
m_model->setObjectId(index, m_objectId);
if (reused)
{
indexReuse++;
}
else
{
numEmittedParticles++;
indexNotReuse++;
}
index++;
}
}
}
if (numEmittedParticles != 0)
{
m_model->setNumActiveParticles(m_model->numActiveParticles() + numEmittedParticles);
sim->emittedParticles(m_model, m_model->numActiveParticles() - numEmittedParticles);
sim->getNeighborhoodSearch()->resize_point_set(m_model->getPointSetIndex(), &m_model->getPosition(0)[0], m_model->numActiveParticles());
}
}
else
{
if (m_model->numActiveParticles() < m_model->numParticles())
{
unsigned int index = m_model->numActiveParticles();
for (unsigned int i = 0; i < m_width; i++)
{
for (unsigned int j = 0; j < m_height; j++)
{
if (index < m_model->numParticles())
{
m_model->getPosition(index) = (i*diam + startX)*axisWidth + (j*diam + startZ)*axisHeight + offset;
m_model->getVelocity(index) = emitVel;
m_model->setParticleState(index, ParticleState::AnimatedByEmitter);
m_model->setObjectId(index, m_objectId);
numEmittedParticles++;
}
index++;
}
}
m_model->setNumActiveParticles(m_model->numActiveParticles() + numEmittedParticles);
sim->emittedParticles(m_model, m_model->numActiveParticles() - numEmittedParticles);
sim->getNeighborhoodSearch()->resize_point_set(m_model->getPointSetIndex(), &m_model->getPosition(0)[0], m_model->numActiveParticles());
}
}
m_nextEmitTime += diam / m_velocity;
m_emitCounter++;
}
void Emitter::emitParticlesCircle(std::vector <unsigned int> &reusedParticles, unsigned int &indexReuse, unsigned int &numEmittedParticles)
{
TimeManager *tm = TimeManager::getCurrent();
const Real t = tm->getTime();
const Real timeStepSize = tm->getTimeStepSize();
const Vector3r & emitDir = m_rotation.col(0);
Simulation *sim = Simulation::getCurrent();
const Real particleRadius = sim->getParticleRadius();
const Real diam = static_cast<Real>(2.0)*particleRadius;
Vector3r emitVel = m_velocity * emitDir;
// shortly before the emitter starts, cleanup the emitter from particles
if (t < m_emitStartTime || t > m_emitEndTime)
emitVel = emitDir * particleRadius * 10 / 0.25;
if (t >= m_emitStartTime - 0.25 && t <= m_emitEndTime)
{
// animate emitted particles
const Vector3r & x0 = m_x;
const Real animationMarginAhead = sim->getSupportRadius();
const Vector3r size = getSize(static_cast<Real>(m_width), static_cast<Real>(m_height), m_type);
const Real h = size[0];
const Real r = 0.5f * size[1];
const Real r2 = r * r;
const Vector3r pos = x0 + 0.5f * animationMarginAhead * emitDir;
const unsigned int nModels = sim->numberOfFluidModels();
for (unsigned int m = 0; m < nModels; m++)
{
FluidModel *fm = sim->getFluidModel(m);
const unsigned int numParticles = fm->numActiveParticles();
#pragma omp parallel for schedule(static) default(shared)
for (int i = 0; i < (int)numParticles; i++)
{
Vector3r &xi = fm->getPosition(i);
if (inCylinder(xi, pos, m_rotation, h, r2))
{
fm->getVelocity(i) = emitVel;
fm->getPosition(i) += timeStepSize * emitVel;
fm->setParticleState(i, ParticleState::AnimatedByEmitter);
fm->setObjectId(i, m_objectId);
}
}
}
}
if (t < m_nextEmitTime || t > m_emitEndTime)
{
return;
}
Vector3r axisHeight = m_rotation.col(1);
Vector3r axisWidth = m_rotation.col(2);
const Real radius = (static_cast<Real>(0.5) * (Real)m_width * diam);
const Real radius2 = radius*radius;
const Real startX = -static_cast<Real>(0.5)*(m_width - 1)*diam;
const Real startZ = -static_cast<Real>(0.5)*(m_width - 1)*diam;
// t-m_nextEmitTime is the time that has passed between the time the particle has been emitted and the current time step.
// timeStepSize is added because emission happens at the end of the time step, but the particles are not animated anymore.
const Real dt = t - m_nextEmitTime + timeStepSize;
const Vector3r velocity = emitVel;
const Vector3r velocityOffset = dt * velocity;
const Vector3r offset = m_x + velocityOffset;
if ((m_model->numActiveParticles() < m_model->numParticles()) ||
(reusedParticles.size() > 0))
{
unsigned int indexNotReuse = m_model->numActiveParticles();
for (unsigned int i = 0; i < m_width; i++)
{
for (unsigned int j = 0; j < m_width; j++)
{
const Real x = (i*diam + startX);
const Real y = (j*diam + startZ);
unsigned int index = 0;
bool reused = false;
if (indexReuse < reusedParticles.size())
{
index = reusedParticles[indexReuse];
reused = true;
}
else
{
index = indexNotReuse;
}
if ((index < m_model->numParticles()) && (x*x + y*y <= radius2))
{
m_model->getPosition(index) = x*axisWidth + y*axisHeight + offset;
m_model->getVelocity(index) = velocity;
m_model->setParticleState(index, ParticleState::AnimatedByEmitter);
m_model->setObjectId(index, m_objectId);
if (reused)
{
indexReuse++;
}
else
{
numEmittedParticles++;
indexNotReuse++;
}
index++;
}
}
}
if (numEmittedParticles != 0)
{
m_model->setNumActiveParticles(m_model->numActiveParticles() + numEmittedParticles);
sim->emittedParticles(m_model, m_model->numActiveParticles() - numEmittedParticles);
sim->getNeighborhoodSearch()->resize_point_set(m_model->getPointSetIndex(), &m_model->getPosition(0)[0], m_model->numActiveParticles());
}
}
else
{
if (m_model->numActiveParticles() < m_model->numParticles())
{
unsigned int index = m_model->numActiveParticles();
for (unsigned int i = 0; i < m_width; i++)
{
for (unsigned int j = 0; j < m_width; j++)
{
const Real x = (i*diam + startX);
const Real y = (j*diam + startZ);
if ((index < m_model->numParticles()) && (x*x + y*y <= radius2))
{
m_model->getPosition(index) = x*axisWidth + y*axisHeight + offset;
m_model->getVelocity(index) = velocity;
m_model->setParticleState(index, ParticleState::AnimatedByEmitter);
m_model->setObjectId(index, m_objectId);
numEmittedParticles++;
index++;
}
}
}
m_model->setNumActiveParticles(m_model->numActiveParticles() + numEmittedParticles);
sim->emittedParticles(m_model, m_model->numActiveParticles() - numEmittedParticles);
sim->getNeighborhoodSearch()->resize_point_set(m_model->getPointSetIndex(), &m_model->getPosition(0)[0], m_model->numActiveParticles());
}
}
m_nextEmitTime += diam / m_velocity;
m_emitCounter++;
}
void Emitter::step(std::vector <unsigned int> &reusedParticles, unsigned int &indexReuse, unsigned int &numEmittedParticles)
{
if (m_type == 1)
emitParticlesCircle(reusedParticles, indexReuse, numEmittedParticles);
else
emitParticles(reusedParticles, indexReuse, numEmittedParticles);
}
void SPH::Emitter::saveState(BinaryFileWriter &binWriter)
{
binWriter.write(m_nextEmitTime);
binWriter.write(m_emitCounter);
}
void SPH::Emitter::loadState(BinaryFileReader &binReader)
{
binReader.read(m_nextEmitTime);
binReader.read(m_emitCounter);
}