-
Notifications
You must be signed in to change notification settings - Fork 299
/
ParticleSource.inl
365 lines (301 loc) · 13 KB
/
ParticleSource.inl
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
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_COMPONENT_MISC_PARTICLESOURCE_INL
#define SOFA_COMPONENT_MISC_PARTICLESOURCE_INL
#include <SofaSphFluid/config.h>
#include <SofaSphFluid/ParticleSource.h>
#include <sofa/component/topology/container/dynamic/PointSetTopologyContainer.h>
#include <sofa/component/topology/container/dynamic/PointSetTopologyModifier.h>
#include <sofa/simulation/AnimateBeginEvent.h>
namespace sofa::component::misc
{
template<class DataTypes>
ParticleSource<DataTypes>::ParticleSource()
: d_translation(initData(&d_translation, Coord(), "translation", "translation applied to center(s)"))
, d_scale(initData(&d_scale, (Real)1.0, "scale", "scale applied to center(s)"))
, d_center(initData(&d_center, "center", "Source center(s)"))
, d_radius(initData(&d_radius, Coord(), "radius", "Source radius"))
, d_velocity(initData(&d_velocity, Deriv(), "velocity", "Particle initial velocity"))
, d_delay(initData(&d_delay, (Real)0.01, "delay", "Delay between particles creation"))
, d_start(initData(&d_start, (Real)0, "start", "Source starting time"))
, d_stop(initData(&d_stop, (Real)1e10, "stop", "Source stopping time"))
, d_addNoise(initData(&d_addNoise, (bool)true, "addNoise", "Will add random value to the radius of new created particles"))
, m_numberParticles(0)
, m_lastparticles(initData(&m_lastparticles, "lastparticles", "lastparticles indices"))
{
this->f_listening.setValue(true);
d_center.beginEdit()->push_back(Coord()); d_center.endEdit();
}
template<class DataTypes>
ParticleSource<DataTypes>::~ParticleSource()
{
}
template<class DataTypes>
void ParticleSource<DataTypes>::init()
{
this->core::behavior::ProjectiveConstraintSet<DataTypes>::init();
if (!this->mstate) {
//sofa::core::objectmodel::ComponentState::d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
return;
}
m_numberParticles = d_center.getValue().size();
m_lastTime = d_start.getValue() - d_delay.getValue();
if (m_lastTime < 0.0)
m_lastTime = 0.0;
m_maxdist = 0;
m_lastpos.resize(m_numberParticles);
sofa::core::topology::BaseMeshTopology* _topology = this->getContext()->getMeshTopology();
if (_topology != nullptr)
{
m_lastparticles.createTopologyHandler(_topology);
m_lastparticles.setDestructionCallback([this](Index pointIndex, Index& val)
{
SOFA_UNUSED(val);
m_lastpos[pointIndex] = m_lastpos[m_lastpos.size() - 1];
m_lastpos.pop_back();
});
}
msg_info() << " center = " << d_center.getValue() << msgendl
<< " radius = " << d_radius.getValue() << " m_numberParticles = " << m_numberParticles << msgendl
<< " start = " << d_start.getValue() << " stop = " << d_stop.getValue() << " delay = " << d_delay.getValue();
}
template<class DataTypes>
void ParticleSource<DataTypes>::reset()
{
this->mstate->resize(1);
m_lastTime = d_start.getValue() - d_delay.getValue();
if (m_lastTime < 0.0)
m_lastTime = 0.0;
m_maxdist = 0;
SetIndexArray& _lastparticles = *this->m_lastparticles.beginEdit();
_lastparticles.clear();
m_lastpos.clear();
this->m_lastparticles.endEdit();
}
template<class DataTypes>
void ParticleSource<DataTypes>::projectResponse(const sofa::core::MechanicalParams* mparams, DataVecDeriv& dxData)
{
SOFA_UNUSED(mparams);
if (!this->mstate || m_lastparticles.getValue().empty()) {
return;
}
double time = this->getContext()->getTime(); // nothing to do yet or anymore
if (time < d_start.getValue() || time > d_stop.getValue()) {
return;
}
VecDeriv& dx = *dxData.beginEdit();
const SetIndexArray& _lastparticles = this->m_lastparticles.getValue();
for (unsigned int s = 0; s<_lastparticles.size(); s++)
{
dx[_lastparticles[s]] = Deriv();
}
dxData.endEdit();
}
template<class DataTypes>
void ParticleSource<DataTypes>::projectPosition(const sofa::core::MechanicalParams* mparams, DataVecCoord& xData)
{
SOFA_UNUSED(mparams);
if (!this->mstate || m_lastparticles.getValue().empty()) {
return;
}
double time = this->getContext()->getTime(); // nothing to do yet or anymore
if (time < d_start.getValue() || time > d_stop.getValue()) {
return;
}
// constraint the most recent particles
VecCoord& x = *xData.beginEdit();
Deriv dpos = d_velocity.getValue()*(time - m_lastTime);
const SetIndexArray& _lastparticles = this->m_lastparticles.getValue();
msg_info() << "projectPosition: " << _lastparticles;
for (unsigned int s = 0; s < _lastparticles.size(); s++)
{
x[_lastparticles[s]] = m_lastpos[s];
x[_lastparticles[s]] += dpos; // account for particle initial motion
}
xData.endEdit();
}
template<class DataTypes>
void ParticleSource<DataTypes>::projectVelocity(const sofa::core::MechanicalParams* mparams, DataVecDeriv& vData)
{
SOFA_UNUSED(mparams);
if (!this->mstate || m_lastparticles.getValue().empty()) {
return;
}
double time = this->getContext()->getTime(); // nothing to do yet or anymore
if (time < d_start.getValue() || time > d_stop.getValue()) {
return;
}
// constraint the most recent particles with the initial Velocity
VecDeriv& res = *vData.beginEdit();
Deriv v0 = d_velocity.getValue();
const SetIndexArray& _lastparticles = this->m_lastparticles.getValue();
for (unsigned int s = 0; s<_lastparticles.size(); s++)
{
res[_lastparticles[s]] = v0;
}
vData.endEdit();
}
template<class DataTypes>
void ParticleSource<DataTypes>::animateBegin(double /*dt*/, double time)
{
if (!this->mstate)
return;
if (time < d_start.getValue() || time > d_stop.getValue())
{
if (time > d_stop.getValue() && time - this->getContext()->getDt() <= d_stop.getValue())
{
msg_info() << "Source stopped, current number of particles : " << this->mstate->getSize();
}
return;
}
size_t i0 = this->mstate->getSize();
if (i0 == 1) // ignore the first point if it is the only one
{
i0 = 0;
sofa::component::topology::container::dynamic::PointSetTopologyContainer* pointCon;
this->getContext()->get(pointCon);
if (pointCon != nullptr)
{
// TODO: epernod.... why why why.... still a diff between meca->getSize and topo->GetNbrPoints...
pointCon->setNbPoints(1);
}
}
size_t nbParticlesToCreate = (int)((time - m_lastTime) / d_delay.getValue());
if (nbParticlesToCreate > 0)
{
msg_info() << "ParticleSource: animate begin time= " << time << " | size: " << i0;
msg_info() << "nbParticlesToCreate: " << nbParticlesToCreate << " m_maxdist: " << m_maxdist;
SetIndexArray& _lastparticles = *m_lastparticles.beginEdit(); ///< lastparticles indices
type::vector< Coord > newX;
type::vector< Deriv > newV;
newX.reserve(nbParticlesToCreate * m_numberParticles);
newV.reserve(nbParticlesToCreate * m_numberParticles);
const Deriv& v0 = d_velocity.getValue();
const Real& delay = d_delay.getValue();
const type::vector<Coord>& center = d_center.getValue();
const Real& scale = d_scale.getValue();
const Coord& translation = d_translation.getValue();
const Coord& radius = d_radius.getValue();
bool addNoise = d_addNoise.getValue();
for (size_t i = 0; i < nbParticlesToCreate; i++)
{
m_lastTime += delay;
m_maxdist += delay * v0.norm() / scale;
//int lastparticle = i0 + i * N;
size_t lp0 = _lastparticles.empty() ? 0 : _lastparticles.size() / 2;
if (lp0 > 0)
{
size_t shift = _lastparticles.size() - lp0;
Deriv dpos = v0 * delay;
for (size_t s = 0; s < lp0; s++)
{
_lastparticles[s] = _lastparticles[s + shift];
m_lastpos[s] = m_lastpos[s + shift] + dpos;
}
}
_lastparticles.resize(lp0);
m_lastpos.resize(lp0);
for (size_t s = 0; s < m_numberParticles; s++)
{
Coord p = center[s] * scale + translation;
if (addNoise)
{
for (unsigned int c = 0; c < p.size(); c++)
p[c] += radius[c] * rrand();
}
else
{
p += radius;
}
m_lastpos.push_back(p);
_lastparticles.push_back((Index)(i0 + newX.size()));
newX.push_back(p + v0 * (time - m_lastTime)); // account for particle initial motion
newV.push_back(v0);
}
}
nbParticlesToCreate = newX.size();
msg_info() << "ParticleSource: Creating " << nbParticlesToCreate << " particles, total " << i0 + nbParticlesToCreate << " particles.";
if (nbParticlesToCreate <= 0)
return;
sofa::component::topology::container::dynamic::PointSetTopologyModifier* pointMod;
this->getContext()->get(pointMod);
// Particles creation.
if (pointMod != nullptr)
{
size_t n = i0 + nbParticlesToCreate;
if (n < this->mstate->getSize())
{
msg_error() << "Less particle to create than the number of dof in the current mstate: " << n << " vs " << this->mstate->getSize();
n = 0;
}
else
{
n -= this->mstate->getSize();
}
pointMod->addPoints(n);
}
else
{
this->mstate->resize(i0 + nbParticlesToCreate);
}
helper::WriteAccessor< Data<VecCoord> > x = *this->mstate->write(core::VecCoordId::position());
helper::WriteAccessor< Data<VecDeriv> > v = *this->mstate->write(core::VecDerivId::velocity());
for (size_t s = 0; s < nbParticlesToCreate; ++s)
{
x[i0 + s] = newX[s];
v[i0 + s] = newV[s];
}
}
m_lastparticles.endEdit();
}
template<class DataTypes>
void ParticleSource<DataTypes>::handleEvent(sofa::core::objectmodel::Event* event)
{
if (simulation::AnimateBeginEvent::checkEventType(event))
{
simulation::AnimateBeginEvent* ev = static_cast<simulation::AnimateBeginEvent*>(event);
animateBegin(ev->getDt(), this->getContext()->getTime());
}
}
template<class DataTypes>
void ParticleSource<DataTypes>::draw(const core::visual::VisualParams* vparams)
{
if (!vparams->displayFlags().getShowBehaviorModels())
return;
if (!this->mstate || m_lastparticles.getValue().empty())
return;
double time = this->getContext()->getTime();
if (time < d_start.getValue() || time > d_stop.getValue())
return;
const auto stateLifeCycle = vparams->drawTool()->makeStateLifeCycle();
Deriv dpos = d_velocity.getValue()*(time - m_lastTime);
std::vector< sofa::type::Vec3 > pointsInit;
for (unsigned int s = 0; s < m_lastpos.size(); s++)
{
sofa::type::Vec3 point;
point = DataTypes::getCPos(m_lastpos[s] + dpos);
pointsInit.push_back(point);
}
vparams->drawTool()->drawPoints(pointsInit, 10, sofa::type::RGBAColor(1., 0.5, 0.5, 1.));
}
} // namespace sofa::component::misc
#endif // SOFA_COMPONENT_MISC_PARTICLESOURCE_INL