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
|
/*
* Copyright 2013, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "particles.h"
extern "C" {
#include "DNA_object_types.h"
#include "DNA_particle_types.h"
}
#include "PTC_api.h"
namespace PTC {
using namespace Abc;
using namespace AbcGeom;
#if 0
void IParticlesSchema::init(const Abc::Argument &iArg0,
const Abc::Argument &iArg1)
{
ALEMBIC_ABC_SAFE_CALL_BEGIN("IParticlesSchema::init()");
Abc::Arguments args;
iArg0.setInto(args);
iArg1.setInto(args);
AbcA::CompoundPropertyReaderPtr _this = this->getPtr();
// no matching so we pick up old assets written as V3f
m_positionsProperty = Abc::IP3fArrayProperty(_this, "P", kNoMatching,
args.getErrorHandlerPolicy());
m_idsProperty = Abc::IUInt64ArrayProperty(_this, ".pointIds",
iArg0, iArg1);
if (_this->getPropertyHeader(".velocities") != NULL) {
m_velocitiesProperty = Abc::IV3fArrayProperty(_this, ".velocities",
iArg0, iArg1);
}
if (_this->getPropertyHeader(".widths" ) != NULL) {
m_widthsParam = IFloatGeomParam(_this, ".widths", iArg0, iArg1);
}
ALEMBIC_ABC_SAFE_CALL_END_RESET();
}
void OParticlesSchema::set( const Sample &iSamp )
{
ALEMBIC_ABC_SAFE_CALL_BEGIN("OParticlesSchema::set()");
// do we need to create velocities prop?
if (iSamp.getVelocities() && !m_velocitiesProperty) {
m_velocitiesProperty = Abc::OV3fArrayProperty(this->getPtr(), ".velocities",
m_positionsProperty.getTimeSampling());
std::vector<V3f> emptyVec;
const V3fArraySample empty(emptyVec);
const size_t numSamps = m_positionsProperty.getNumSamples();
for (size_t i = 0 ; i < numSamps ; ++i) {
m_velocitiesProperty.set(empty);
}
}
// do we need to create widths prop?
if (iSamp.getWidths() && !m_widthsParam) {
std::vector<float> emptyVals;
std::vector<Util::uint32_t> emptyIndices;
OFloatGeomParam::Sample empty;
if (iSamp.getWidths().getIndices()) {
empty = OFloatGeomParam::Sample(Abc::FloatArraySample(emptyVals),
Abc::UInt32ArraySample(emptyIndices),
iSamp.getWidths().getScope());
// widths are indexed which is wasteful, but technically ok
m_widthsParam = OFloatGeomParam(this->getPtr(), ".widths", true,
iSamp.getWidths().getScope(),
1, this->getTimeSampling());
}
else {
empty = OFloatGeomParam::Sample(Abc::FloatArraySample(emptyVals),
iSamp.getWidths().getScope());
// widths are not indexed
m_widthsParam = OFloatGeomParam(this->getPtr(), ".widths", false,
iSamp.getWidths().getScope(), 1,
this->getTimeSampling());
}
size_t numSamples = m_positionsProperty.getNumSamples();
// set all the missing samples
for (size_t i = 0; i < numSamples; ++i) {
m_widthsParam.set( empty );
}
}
// We could add sample integrity checking here.
if (m_positionsProperty.getNumSamples() == 0) {
// First sample must be valid on all points.
ABCA_ASSERT(iSamp.getPositions() &&
iSamp.getIds(),
"Sample 0 must have valid data for points and ids");
m_positionsProperty.set(iSamp.getPositions());
m_idsProperty.set(iSamp.getIds());
if (m_velocitiesProperty) {
m_velocitiesProperty.set(iSamp.getVelocities());
}
if (m_widthsParam) {
m_widthsParam.set(iSamp.getWidths());
}
if (iSamp.getSelfBounds().isEmpty()) {
// OTypedScalarProperty::set() is not referentially transparent,
// so we need a a placeholder variable.
Abc::Box3d bnds(ComputeBoundsFromPositions(iSamp.getPositions()));
m_selfBoundsProperty.set( bnds );
}
else {
m_selfBoundsProperty.set(iSamp.getSelfBounds());
}
}
else {
SetPropUsePrevIfNull(m_positionsProperty, iSamp.getPositions());
SetPropUsePrevIfNull(m_idsProperty, iSamp.getIds());
SetPropUsePrevIfNull(m_velocitiesProperty, iSamp.getVelocities());
if (iSamp.getSelfBounds().hasVolume()) {
m_selfBoundsProperty.set(iSamp.getSelfBounds());
}
else if (iSamp.getPositions()) {
Abc::Box3d bnds(ComputeBoundsFromPositions(iSamp.getPositions()));
m_selfBoundsProperty.set(bnds);
}
else {
m_selfBoundsProperty.setFromPrevious();
}
if (m_widthsParam) {
m_widthsParam.set(iSamp.getWidths());
}
}
ALEMBIC_ABC_SAFE_CALL_END();
}
//-*****************************************************************************
void OParticlesSchema::setFromPrevious()
{
ALEMBIC_ABC_SAFE_CALL_BEGIN("OParticlesSchema::setFromPrevious");
m_positionsProperty.setFromPrevious();
m_idsProperty.setFromPrevious();
m_selfBoundsProperty.setFromPrevious();
if (m_widthsParam) {
m_widthsParam.setFromPrevious();
}
ALEMBIC_ABC_SAFE_CALL_END();
}
//-*****************************************************************************
void OParticlesSchema::setTimeSampling(uint32_t iIndex)
{
ALEMBIC_ABC_SAFE_CALL_BEGIN("OParticlesSchema::setTimeSampling( uint32_t )");
m_positionsProperty.setTimeSampling(iIndex);
m_idsProperty.setTimeSampling(iIndex);
m_selfBoundsProperty.setTimeSampling(iIndex);
if (m_widthsParam)
{
m_widthsParam.setTimeSampling(iIndex);
}
ALEMBIC_ABC_SAFE_CALL_END();
}
//-*****************************************************************************
void OParticlesSchema::setTimeSampling( AbcA::TimeSamplingPtr iTime )
{
ALEMBIC_ABC_SAFE_CALL_BEGIN("OParticlesSchema::setTimeSampling( TimeSamplingPtr )");
if (iTime)
{
uint32_t tsIndex = getObject().getArchive().addTimeSampling(*iTime);
setTimeSampling(tsIndex);
}
ALEMBIC_ABC_SAFE_CALL_END();
}
//-*****************************************************************************
void OParticlesSchema::init(uint32_t iTsIdx)
{
ALEMBIC_ABC_SAFE_CALL_BEGIN("OParticlesSchema::init()");
AbcA::MetaData mdata;
SetGeometryScope(mdata, kVaryingScope);
AbcA::CompoundPropertyWriterPtr _this = this->getPtr();
m_positionsProperty = Abc::OP3fArrayProperty(_this, "P", mdata, iTsIdx);
m_idsProperty = Abc::OUInt64ArrayProperty(_this, ".pointIds", mdata,
iTsIdx);
ALEMBIC_ABC_SAFE_CALL_END_RESET();
}
#endif
ParticlesWriter::ParticlesWriter(Scene *scene, Object *ob, ParticleSystem *psys) :
Writer(scene, &ob->id, psys->pointcache),
m_ob(ob),
m_psys(psys)
{
uint32_t fs = add_frame_sampling();
OObject root = m_archive.getTop();
m_points = OPoints(root, m_psys->name, fs);
}
ParticlesWriter::~ParticlesWriter()
{
}
void ParticlesWriter::write_sample()
{
OPointsSchema &schema = m_points.getSchema();
int totpart = m_psys->totpart;
ParticleData *pa;
int i;
/* XXX TODO only needed for the first frame/sample */
std::vector<Util::uint64_t> ids;
ids.reserve(totpart);
for (i = 0, pa = m_psys->particles; i < totpart; ++i, ++pa)
ids.push_back(i);
std::vector<V3f> positions;
positions.reserve(totpart);
for (i = 0, pa = m_psys->particles; i < totpart; ++i, ++pa) {
float *co = pa->state.co;
positions.push_back(V3f(co[0], co[1], co[2]));
}
OPointsSchema::Sample sample = OPointsSchema::Sample(V3fArraySample(positions), UInt64ArraySample(ids));
schema.set(sample);
}
ParticlesReader::ParticlesReader(Scene *scene, Object *ob, ParticleSystem *psys) :
Reader(scene, &ob->id, psys->pointcache),
m_ob(ob),
m_psys(psys),
m_totpoint(0)
{
if (m_archive.valid()) {
IObject root = m_archive.getTop();
m_points = IPoints(root, m_psys->name);
}
/* XXX TODO read first sample for info on particle count and times */
m_totpoint = 0;
}
ParticlesReader::~ParticlesReader()
{
}
PTCReadSampleResult ParticlesReader::read_sample(float frame)
{
if (!m_points.valid())
return PTC_READ_SAMPLE_INVALID;
IPointsSchema &schema = m_points.getSchema();
TimeSamplingPtr ts = schema.getTimeSampling();
ISampleSelector ss = get_frame_sample_selector(frame);
chrono_t time = ss.getRequestedTime();
std::pair<index_t, chrono_t> sres = ts->getFloorIndex(time, schema.getNumSamples());
chrono_t stime = sres.second;
float sframe = time_to_frame(stime);
IPointsSchema::Sample sample;
schema.get(sample, ss);
const V3f *positions = sample.getPositions()->get();
int totpart = m_psys->totpart, i;
ParticleData *pa;
for (i = 0, pa = m_psys->particles; i < sample.getPositions()->size(); ++i, ++pa) {
pa->state.co[0] = positions[i].x;
pa->state.co[1] = positions[i].y;
pa->state.co[2] = positions[i].z;
}
return PTC_READ_SAMPLE_EXACT;
}
} /* namespace PTC */
/* ==== C API ==== */
PTCWriter *PTC_writer_particles(Scene *scene, Object *ob, ParticleSystem *psys)
{
return (PTCWriter *)(new PTC::ParticlesWriter(scene, ob, psys));
}
PTCReader *PTC_reader_particles(Scene *scene, Object *ob, ParticleSystem *psys)
{
return (PTCReader *)(new PTC::ParticlesReader(scene, ob, psys));
}
int PTC_reader_particles_totpoint(PTCReader *_reader)
{
PTC::ParticlesReader *reader = (PTC::ParticlesReader *)_reader;
return reader->totpoint();
}
|