2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Campbell Barton <ideasman42@gmail.com>
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/blenkernel/intern/pointcache.c
35 #include <sys/types.h>
37 #include "MEM_guardedalloc.h"
40 #include "DNA_dynamicpaint_types.h"
41 #include "DNA_modifier_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_object_force_types.h"
44 #include "DNA_particle_types.h"
45 #include "DNA_rigidbody_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_smoke_types.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_threads.h"
52 #include "BLI_string.h"
53 #include "BLI_utildefines.h"
55 #include "BLT_translation.h"
59 #include "BKE_appdir.h"
61 #include "BKE_cloth.h"
62 #include "BKE_dynamicpaint.h"
63 #include "BKE_global.h"
65 #include "BKE_modifier.h"
66 #include "BKE_object.h"
67 #include "BKE_particle.h"
68 #include "BKE_pointcache.h"
69 #include "BKE_scene.h"
70 #include "BKE_smoke.h"
71 #include "BKE_softbody.h"
81 #include "smoke_API.h"
85 #include "openvdb_capi.h"
89 # ifdef WITH_SYSTEM_LZO
90 # include <lzo/lzo1x.h>
94 # define LZO_HEAP_ALLOC(var,size) \
95 lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
98 #define LZO_OUT_LEN(size) ((size) + (size) / 16 + 64 + 3)
104 /* needed for directory lookup */
108 # include "BLI_winstuff.h"
111 #define PTCACHE_DATA_FROM(data, type, from) \
113 memcpy(data[type], from, ptcache_data_size[type]); \
116 #define PTCACHE_DATA_TO(data, type, index, to) \
118 memcpy(to, (char *)(data)[type] + ((index) ? (index) * ptcache_data_size[type] : 0), ptcache_data_size[type]); \
121 /* could be made into a pointcache option */
122 #define DURIAN_POINTCACHE_LIB_OK 1
124 static int ptcache_data_size[] = {
125 sizeof(unsigned int), // BPHYS_DATA_INDEX
126 3 * sizeof(float), // BPHYS_DATA_LOCATION
127 3 * sizeof(float), // BPHYS_DATA_VELOCITY
128 4 * sizeof(float), // BPHYS_DATA_ROTATION
129 3 * sizeof(float), // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST
130 sizeof(float), // BPHYS_DATA_SIZE
131 3 * sizeof(float), // BPHYS_DATA_TIMES
132 sizeof(BoidData) // case BPHYS_DATA_BOIDS
135 static int ptcache_extra_datasize[] = {
137 sizeof(ParticleSpring)
140 /* forward declerations */
141 static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result, unsigned int len);
142 static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, unsigned int in_len, unsigned char *out, int mode);
143 static int ptcache_file_write(PTCacheFile *pf, const void *f, unsigned int tot, unsigned int size);
144 static int ptcache_file_read(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size);
146 /* Common functions */
147 static int ptcache_basic_header_read(PTCacheFile *pf)
151 /* Custom functions should read these basic elements too! */
152 if (!error && !fread(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
155 if (!error && !fread(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
160 static int ptcache_basic_header_write(PTCacheFile *pf)
162 /* Custom functions should write these basic elements too! */
163 if (!fwrite(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
166 if (!fwrite(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
171 /* Softbody functions */
172 static int ptcache_softbody_write(int index, void *soft_v, void **data, int UNUSED(cfra))
174 SoftBody *soft= soft_v;
175 BodyPoint *bp = soft->bpoint + index;
177 PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, bp->pos);
178 PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, bp->vec);
182 static void ptcache_softbody_read(int index, void *soft_v, void **data, float UNUSED(cfra), float *old_data)
184 SoftBody *soft= soft_v;
185 BodyPoint *bp = soft->bpoint + index;
188 memcpy(bp->pos, data, 3 * sizeof(float));
189 memcpy(bp->vec, data + 3, 3 * sizeof(float));
192 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, bp->pos);
193 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, 0, bp->vec);
196 static void ptcache_softbody_interpolate(int index, void *soft_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
198 SoftBody *soft= soft_v;
199 BodyPoint *bp = soft->bpoint + index;
206 copy_v3_v3(keys[1].co, bp->pos);
207 copy_v3_v3(keys[1].vel, bp->vec);
210 memcpy(keys[2].co, old_data, 3 * sizeof(float));
211 memcpy(keys[2].vel, old_data + 3, 3 * sizeof(float));
214 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
216 dfra = cfra2 - cfra1;
218 mul_v3_fl(keys[1].vel, dfra);
219 mul_v3_fl(keys[2].vel, dfra);
221 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
223 mul_v3_fl(keys->vel, 1.0f / dfra);
225 copy_v3_v3(bp->pos, keys->co);
226 copy_v3_v3(bp->vec, keys->vel);
228 static int ptcache_softbody_totpoint(void *soft_v, int UNUSED(cfra))
230 SoftBody *soft= soft_v;
231 return soft->totpoint;
233 static void ptcache_softbody_error(void *UNUSED(soft_v), const char *UNUSED(message))
235 /* ignored for now */
238 /* Particle functions */
239 void BKE_ptcache_make_particle_key(ParticleKey *key, int index, void **data, float time)
241 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, index, key->co);
242 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, index, key->vel);
244 /* no rotation info, so make something nice up */
245 if (data[BPHYS_DATA_ROTATION]==NULL) {
246 vec_to_quat(key->rot, key->vel, OB_NEGX, OB_POSZ);
249 PTCACHE_DATA_TO(data, BPHYS_DATA_ROTATION, index, key->rot);
252 PTCACHE_DATA_TO(data, BPHYS_DATA_AVELOCITY, index, key->ave);
255 static int ptcache_particle_write(int index, void *psys_v, void **data, int cfra)
257 ParticleSystem *psys= psys_v;
258 ParticleData *pa = psys->particles + index;
259 BoidParticle *boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
261 int step = psys->pointcache->step;
263 /* No need to store unborn or died particles outside cache step bounds */
264 if (data[BPHYS_DATA_INDEX] && (cfra < pa->time - step || cfra > pa->dietime + step))
268 times[1] = pa->dietime;
269 times[2] = pa->lifetime;
271 PTCACHE_DATA_FROM(data, BPHYS_DATA_INDEX, &index);
272 PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, pa->state.co);
273 PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, pa->state.vel);
274 PTCACHE_DATA_FROM(data, BPHYS_DATA_ROTATION, pa->state.rot);
275 PTCACHE_DATA_FROM(data, BPHYS_DATA_AVELOCITY, pa->state.ave);
276 PTCACHE_DATA_FROM(data, BPHYS_DATA_SIZE, &pa->size);
277 PTCACHE_DATA_FROM(data, BPHYS_DATA_TIMES, times);
280 PTCACHE_DATA_FROM(data, BPHYS_DATA_BOIDS, &boid->data);
283 /* return flag 1+1=2 for newly born particles to copy exact birth location to previously cached frame */
284 return 1 + (pa->state.time >= pa->time && pa->prev_state.time <= pa->time);
286 static void ptcache_particle_read(int index, void *psys_v, void **data, float cfra, float *old_data)
288 ParticleSystem *psys= psys_v;
291 float timestep = 0.04f * psys->part->timetweak;
293 if (index >= psys->totpart)
296 pa = psys->particles + index;
297 boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
299 if (cfra > pa->state.time)
300 memcpy(&pa->prev_state, &pa->state, sizeof(ParticleKey));
303 /* old format cache */
304 memcpy(&pa->state, old_data, sizeof(ParticleKey));
308 BKE_ptcache_make_particle_key(&pa->state, 0, data, cfra);
310 /* set frames cached before birth to birth time */
312 pa->state.time = pa->time;
313 else if (cfra > pa->dietime)
314 pa->state.time = pa->dietime;
316 if (data[BPHYS_DATA_SIZE]) {
317 PTCACHE_DATA_TO(data, BPHYS_DATA_SIZE, 0, &pa->size);
320 if (data[BPHYS_DATA_TIMES]) {
322 PTCACHE_DATA_TO(data, BPHYS_DATA_TIMES, 0, ×);
324 pa->dietime = times[1];
325 pa->lifetime = times[2];
329 PTCACHE_DATA_TO(data, BPHYS_DATA_BOIDS, 0, &boid->data);
332 /* determine velocity from previous location */
333 if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
334 if (cfra > pa->prev_state.time) {
335 sub_v3_v3v3(pa->state.vel, pa->state.co, pa->prev_state.co);
336 mul_v3_fl(pa->state.vel, (cfra - pa->prev_state.time) * timestep);
339 sub_v3_v3v3(pa->state.vel, pa->prev_state.co, pa->state.co);
340 mul_v3_fl(pa->state.vel, (pa->prev_state.time - cfra) * timestep);
344 /* default to no rotation */
345 if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
346 unit_qt(pa->state.rot);
349 static void ptcache_particle_interpolate(int index, void *psys_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
351 ParticleSystem *psys= psys_v;
354 float dfra, timestep = 0.04f * psys->part->timetweak;
356 if (index >= psys->totpart)
359 pa = psys->particles + index;
361 /* particle wasn't read from first cache so can't interpolate */
362 if ((int)cfra1 < pa->time - psys->pointcache->step || (int)cfra1 > pa->dietime + psys->pointcache->step)
365 cfra = MIN2(cfra, pa->dietime);
366 cfra1 = MIN2(cfra1, pa->dietime);
367 cfra2 = MIN2(cfra2, pa->dietime);
372 memcpy(keys+1, &pa->state, sizeof(ParticleKey));
374 memcpy(keys+2, old_data, sizeof(ParticleKey));
376 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
378 /* determine velocity from previous location */
379 if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
380 if (keys[1].time > keys[2].time) {
381 sub_v3_v3v3(keys[2].vel, keys[1].co, keys[2].co);
382 mul_v3_fl(keys[2].vel, (keys[1].time - keys[2].time) * timestep);
385 sub_v3_v3v3(keys[2].vel, keys[2].co, keys[1].co);
386 mul_v3_fl(keys[2].vel, (keys[2].time - keys[1].time) * timestep);
390 /* default to no rotation */
391 if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
392 unit_qt(keys[2].rot);
396 cfra1 = MAX2(cfra1, pa->time);
398 dfra = cfra2 - cfra1;
400 mul_v3_fl(keys[1].vel, dfra * timestep);
401 mul_v3_fl(keys[2].vel, dfra * timestep);
403 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, &pa->state, 1);
404 interp_qt_qtqt(pa->state.rot, keys[1].rot, keys[2].rot, (cfra - cfra1) / dfra);
406 mul_v3_fl(pa->state.vel, 1.f / (dfra * timestep));
408 pa->state.time = cfra;
411 static int ptcache_particle_totpoint(void *psys_v, int UNUSED(cfra))
413 ParticleSystem *psys = psys_v;
414 return psys->totpart;
417 static void ptcache_particle_error(void *UNUSED(psys_v), const char *UNUSED(message))
419 /* ignored for now */
422 static int ptcache_particle_totwrite(void *psys_v, int cfra)
424 ParticleSystem *psys = psys_v;
425 ParticleData *pa= psys->particles;
426 int p, step = psys->pointcache->step;
430 return psys->totpart;
432 for (p=0; p<psys->totpart; p++, pa++)
433 totwrite += (cfra >= pa->time - step && cfra <= pa->dietime + step);
438 static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int UNUSED(cfra))
440 ParticleSystem *psys = psys_v;
441 PTCacheExtra *extra = NULL;
443 if (psys->part->phystype == PART_PHYS_FLUID &&
444 psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS &&
445 psys->tot_fluidsprings && psys->fluid_springs)
447 extra = MEM_callocN(sizeof(PTCacheExtra), "Point cache: fluid extra data");
449 extra->type = BPHYS_EXTRA_FLUID_SPRINGS;
450 extra->totdata = psys->tot_fluidsprings;
452 extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Point cache: extra data");
453 memcpy(extra->data, psys->fluid_springs, extra->totdata * ptcache_extra_datasize[extra->type]);
455 BLI_addtail(&pm->extradata, extra);
459 static void ptcache_particle_extra_read(void *psys_v, PTCacheMem *pm, float UNUSED(cfra))
461 ParticleSystem *psys = psys_v;
462 PTCacheExtra *extra = pm->extradata.first;
464 for (; extra; extra=extra->next) {
465 switch (extra->type) {
466 case BPHYS_EXTRA_FLUID_SPRINGS:
468 if (psys->fluid_springs)
469 MEM_freeN(psys->fluid_springs);
471 psys->fluid_springs = MEM_dupallocN(extra->data);
472 psys->tot_fluidsprings = psys->alloc_fluidsprings = extra->totdata;
479 /* Cloth functions */
480 static int ptcache_cloth_write(int index, void *cloth_v, void **data, int UNUSED(cfra))
482 ClothModifierData *clmd= cloth_v;
483 Cloth *cloth= clmd->clothObject;
484 ClothVertex *vert = cloth->verts + index;
486 PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, vert->x);
487 PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, vert->v);
488 PTCACHE_DATA_FROM(data, BPHYS_DATA_XCONST, vert->xconst);
492 static void ptcache_cloth_read(int index, void *cloth_v, void **data, float UNUSED(cfra), float *old_data)
494 ClothModifierData *clmd= cloth_v;
495 Cloth *cloth= clmd->clothObject;
496 ClothVertex *vert = cloth->verts + index;
499 memcpy(vert->x, data, 3 * sizeof(float));
500 memcpy(vert->xconst, data + 3, 3 * sizeof(float));
501 memcpy(vert->v, data + 6, 3 * sizeof(float));
504 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, vert->x);
505 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, 0, vert->v);
506 PTCACHE_DATA_TO(data, BPHYS_DATA_XCONST, 0, vert->xconst);
509 static void ptcache_cloth_interpolate(int index, void *cloth_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
511 ClothModifierData *clmd= cloth_v;
512 Cloth *cloth= clmd->clothObject;
513 ClothVertex *vert = cloth->verts + index;
520 copy_v3_v3(keys[1].co, vert->x);
521 copy_v3_v3(keys[1].vel, vert->v);
524 memcpy(keys[2].co, old_data, 3 * sizeof(float));
525 memcpy(keys[2].vel, old_data + 6, 3 * sizeof(float));
528 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
530 dfra = cfra2 - cfra1;
532 mul_v3_fl(keys[1].vel, dfra);
533 mul_v3_fl(keys[2].vel, dfra);
535 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
537 mul_v3_fl(keys->vel, 1.0f / dfra);
539 copy_v3_v3(vert->x, keys->co);
540 copy_v3_v3(vert->v, keys->vel);
542 /* should vert->xconst be interpolated somehow too? - jahka */
545 static int ptcache_cloth_totpoint(void *cloth_v, int UNUSED(cfra))
547 ClothModifierData *clmd= cloth_v;
548 return clmd->clothObject ? clmd->clothObject->mvert_num : 0;
551 static void ptcache_cloth_error(void *cloth_v, const char *message)
553 ClothModifierData *clmd= cloth_v;
554 modifier_setError(&clmd->modifier, "%s", message);
558 /* Smoke functions */
559 static int ptcache_smoke_totpoint(void *smoke_v, int UNUSED(cfra))
561 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
562 SmokeDomainSettings *sds = smd->domain;
565 return sds->base_res[0]*sds->base_res[1]*sds->base_res[2];
571 static void ptcache_smoke_error(void *smoke_v, const char *message)
573 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
574 modifier_setError(&smd->modifier, "%s", message);
577 #define SMOKE_CACHE_VERSION "1.04"
579 static int ptcache_smoke_write(PTCacheFile *pf, void *smoke_v)
581 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
582 SmokeDomainSettings *sds = smd->domain;
584 int fluid_fields = smoke_get_data_flags(sds);
587 ptcache_file_write(pf, SMOKE_CACHE_VERSION, 4, sizeof(char));
588 ptcache_file_write(pf, &fluid_fields, 1, sizeof(int));
589 ptcache_file_write(pf, &sds->active_fields, 1, sizeof(int));
590 ptcache_file_write(pf, &sds->res, 3, sizeof(int));
591 ptcache_file_write(pf, &sds->dx, 1, sizeof(float));
594 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
595 float dt, dx, *dens, *react, *fuel, *flame, *heat, *heatold, *vx, *vy, *vz, *r, *g, *b;
596 unsigned char *obstacles;
597 unsigned int in_len = sizeof(float)*(unsigned int)res;
598 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len) * 4, "pointcache_lzo_buffer");
599 //int mode = res >= 1000000 ? 2 : 1;
601 if (sds->cache_comp == SM_CACHE_HEAVY) mode=2; // heavy
603 smoke_export(sds->fluid, &dt, &dx, &dens, &react, &flame, &fuel, &heat, &heatold, &vx, &vy, &vz, &r, &g, &b, &obstacles);
605 ptcache_file_compressed_write(pf, (unsigned char *)sds->shadow, in_len, out, mode);
606 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len, out, mode);
607 if (fluid_fields & SM_ACTIVE_HEAT) {
608 ptcache_file_compressed_write(pf, (unsigned char *)heat, in_len, out, mode);
609 ptcache_file_compressed_write(pf, (unsigned char *)heatold, in_len, out, mode);
611 if (fluid_fields & SM_ACTIVE_FIRE) {
612 ptcache_file_compressed_write(pf, (unsigned char *)flame, in_len, out, mode);
613 ptcache_file_compressed_write(pf, (unsigned char *)fuel, in_len, out, mode);
614 ptcache_file_compressed_write(pf, (unsigned char *)react, in_len, out, mode);
616 if (fluid_fields & SM_ACTIVE_COLORS) {
617 ptcache_file_compressed_write(pf, (unsigned char *)r, in_len, out, mode);
618 ptcache_file_compressed_write(pf, (unsigned char *)g, in_len, out, mode);
619 ptcache_file_compressed_write(pf, (unsigned char *)b, in_len, out, mode);
621 ptcache_file_compressed_write(pf, (unsigned char *)vx, in_len, out, mode);
622 ptcache_file_compressed_write(pf, (unsigned char *)vy, in_len, out, mode);
623 ptcache_file_compressed_write(pf, (unsigned char *)vz, in_len, out, mode);
624 ptcache_file_compressed_write(pf, (unsigned char *)obstacles, (unsigned int)res, out, mode);
625 ptcache_file_write(pf, &dt, 1, sizeof(float));
626 ptcache_file_write(pf, &dx, 1, sizeof(float));
627 ptcache_file_write(pf, &sds->p0, 3, sizeof(float));
628 ptcache_file_write(pf, &sds->p1, 3, sizeof(float));
629 ptcache_file_write(pf, &sds->dp0, 3, sizeof(float));
630 ptcache_file_write(pf, &sds->shift, 3, sizeof(int));
631 ptcache_file_write(pf, &sds->obj_shift_f, 3, sizeof(float));
632 ptcache_file_write(pf, &sds->obmat, 16, sizeof(float));
633 ptcache_file_write(pf, &sds->base_res, 3, sizeof(int));
634 ptcache_file_write(pf, &sds->res_min, 3, sizeof(int));
635 ptcache_file_write(pf, &sds->res_max, 3, sizeof(int));
636 ptcache_file_write(pf, &sds->active_color, 3, sizeof(float));
644 int res_big_array[3];
646 int res = sds->res[0]*sds->res[1]*sds->res[2];
647 float *dens, *react, *fuel, *flame, *tcu, *tcv, *tcw, *r, *g, *b;
648 unsigned int in_len = sizeof(float)*(unsigned int)res;
649 unsigned int in_len_big;
653 smoke_turbulence_get_res(sds->wt, res_big_array);
654 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
655 //mode = res_big >= 1000000 ? 2 : 1;
657 if (sds->cache_high_comp == SM_CACHE_HEAVY) mode=2; // heavy
659 in_len_big = sizeof(float) * (unsigned int)res_big;
661 smoke_turbulence_export(sds->wt, &dens, &react, &flame, &fuel, &r, &g, &b, &tcu, &tcv, &tcw);
663 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len_big), "pointcache_lzo_buffer");
664 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len_big, out, mode);
665 if (fluid_fields & SM_ACTIVE_FIRE) {
666 ptcache_file_compressed_write(pf, (unsigned char *)flame, in_len_big, out, mode);
667 ptcache_file_compressed_write(pf, (unsigned char *)fuel, in_len_big, out, mode);
668 ptcache_file_compressed_write(pf, (unsigned char *)react, in_len_big, out, mode);
670 if (fluid_fields & SM_ACTIVE_COLORS) {
671 ptcache_file_compressed_write(pf, (unsigned char *)r, in_len_big, out, mode);
672 ptcache_file_compressed_write(pf, (unsigned char *)g, in_len_big, out, mode);
673 ptcache_file_compressed_write(pf, (unsigned char *)b, in_len_big, out, mode);
677 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
678 ptcache_file_compressed_write(pf, (unsigned char *)tcu, in_len, out, mode);
679 ptcache_file_compressed_write(pf, (unsigned char *)tcv, in_len, out, mode);
680 ptcache_file_compressed_write(pf, (unsigned char *)tcw, in_len, out, mode);
689 /* read old smoke cache from 2.64 */
690 static int ptcache_smoke_read_old(PTCacheFile *pf, void *smoke_v)
692 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
693 SmokeDomainSettings *sds = smd->domain;
696 const size_t res = sds->res[0] * sds->res[1] * sds->res[2];
697 const unsigned int out_len = (unsigned int)res * sizeof(float);
698 float dt, dx, *dens, *heat, *heatold, *vx, *vy, *vz;
699 unsigned char *obstacles;
700 float *tmp_array = MEM_callocN(out_len, "Smoke old cache tmp");
702 int fluid_fields = smoke_get_data_flags(sds);
704 /* Part part of the new cache header */
705 sds->active_color[0] = 0.7f;
706 sds->active_color[1] = 0.7f;
707 sds->active_color[2] = 0.7f;
709 smoke_export(sds->fluid, &dt, &dx, &dens, NULL, NULL, NULL, &heat, &heatold, &vx, &vy, &vz, NULL, NULL, NULL, &obstacles);
711 ptcache_file_compressed_read(pf, (unsigned char *)sds->shadow, out_len);
712 ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len);
713 ptcache_file_compressed_read(pf, (unsigned char*)tmp_array, out_len);
715 if (fluid_fields & SM_ACTIVE_HEAT)
717 ptcache_file_compressed_read(pf, (unsigned char*)heat, out_len);
718 ptcache_file_compressed_read(pf, (unsigned char*)heatold, out_len);
722 ptcache_file_compressed_read(pf, (unsigned char*)tmp_array, out_len);
723 ptcache_file_compressed_read(pf, (unsigned char*)tmp_array, out_len);
725 ptcache_file_compressed_read(pf, (unsigned char*)vx, out_len);
726 ptcache_file_compressed_read(pf, (unsigned char*)vy, out_len);
727 ptcache_file_compressed_read(pf, (unsigned char*)vz, out_len);
728 ptcache_file_compressed_read(pf, (unsigned char*)tmp_array, out_len);
729 ptcache_file_compressed_read(pf, (unsigned char*)tmp_array, out_len);
730 ptcache_file_compressed_read(pf, (unsigned char*)tmp_array, out_len);
731 ptcache_file_compressed_read(pf, (unsigned char*)obstacles, (unsigned int)res);
732 ptcache_file_read(pf, &dt, 1, sizeof(float));
733 ptcache_file_read(pf, &dx, 1, sizeof(float));
735 MEM_freeN(tmp_array);
737 if (pf->data_types & (1<<BPHYS_DATA_SMOKE_HIGH) && sds->wt) {
738 int res_big, res_big_array[3];
739 float *tcu, *tcv, *tcw;
740 unsigned int out_len_big;
741 unsigned char *tmp_array_big;
742 smoke_turbulence_get_res(sds->wt, res_big_array);
743 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
744 out_len_big = sizeof(float) * (unsigned int)res_big;
746 tmp_array_big = MEM_callocN(out_len_big, "Smoke old cache tmp");
748 smoke_turbulence_export(sds->wt, &dens, NULL, NULL, NULL, NULL, NULL, NULL, &tcu, &tcv, &tcw);
750 ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len_big);
751 ptcache_file_compressed_read(pf, (unsigned char*)tmp_array_big, out_len_big);
753 ptcache_file_compressed_read(pf, (unsigned char*)tcu, out_len);
754 ptcache_file_compressed_read(pf, (unsigned char*)tcv, out_len);
755 ptcache_file_compressed_read(pf, (unsigned char*)tcw, out_len);
757 MEM_freeN(tmp_array_big);
764 static int ptcache_smoke_read(PTCacheFile *pf, void *smoke_v)
766 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
767 SmokeDomainSettings *sds = smd->domain;
771 int fluid_fields = smoke_get_data_flags(sds);
772 int cache_fields = 0;
773 int active_fields = 0;
777 ptcache_file_read(pf, version, 4, sizeof(char));
778 if (!STREQLEN(version, SMOKE_CACHE_VERSION, 4))
780 /* reset file pointer */
781 fseek(pf->fp, -4, SEEK_CUR);
782 return ptcache_smoke_read_old(pf, smoke_v);
786 ptcache_file_read(pf, &cache_fields, 1, sizeof(int));
787 ptcache_file_read(pf, &active_fields, 1, sizeof(int));
788 ptcache_file_read(pf, &ch_res, 3, sizeof(int));
789 ptcache_file_read(pf, &ch_dx, 1, sizeof(float));
791 /* check if resolution has changed */
792 if (sds->res[0] != ch_res[0] ||
793 sds->res[1] != ch_res[1] ||
794 sds->res[2] != ch_res[2])
796 if (sds->flags & MOD_SMOKE_ADAPTIVE_DOMAIN)
801 /* check if active fields have changed */
802 if (fluid_fields != cache_fields ||
803 active_fields != sds->active_fields)
806 /* reallocate fluid if needed*/
808 sds->active_fields = active_fields | cache_fields;
809 smoke_reallocate_fluid(sds, ch_dx, ch_res, 1);
811 VECCOPY(sds->res, ch_res);
812 sds->total_cells = ch_res[0]*ch_res[1]*ch_res[2];
813 if (sds->flags & MOD_SMOKE_HIGHRES) {
814 smoke_reallocate_highres_fluid(sds, ch_dx, ch_res, 1);
819 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
820 float dt, dx, *dens, *react, *fuel, *flame, *heat, *heatold, *vx, *vy, *vz, *r, *g, *b;
821 unsigned char *obstacles;
822 unsigned int out_len = (unsigned int)res * sizeof(float);
824 smoke_export(sds->fluid, &dt, &dx, &dens, &react, &flame, &fuel, &heat, &heatold, &vx, &vy, &vz, &r, &g, &b, &obstacles);
826 ptcache_file_compressed_read(pf, (unsigned char *)sds->shadow, out_len);
827 ptcache_file_compressed_read(pf, (unsigned char *)dens, out_len);
828 if (cache_fields & SM_ACTIVE_HEAT) {
829 ptcache_file_compressed_read(pf, (unsigned char *)heat, out_len);
830 ptcache_file_compressed_read(pf, (unsigned char *)heatold, out_len);
832 if (cache_fields & SM_ACTIVE_FIRE) {
833 ptcache_file_compressed_read(pf, (unsigned char *)flame, out_len);
834 ptcache_file_compressed_read(pf, (unsigned char *)fuel, out_len);
835 ptcache_file_compressed_read(pf, (unsigned char *)react, out_len);
837 if (cache_fields & SM_ACTIVE_COLORS) {
838 ptcache_file_compressed_read(pf, (unsigned char *)r, out_len);
839 ptcache_file_compressed_read(pf, (unsigned char *)g, out_len);
840 ptcache_file_compressed_read(pf, (unsigned char *)b, out_len);
842 ptcache_file_compressed_read(pf, (unsigned char *)vx, out_len);
843 ptcache_file_compressed_read(pf, (unsigned char *)vy, out_len);
844 ptcache_file_compressed_read(pf, (unsigned char *)vz, out_len);
845 ptcache_file_compressed_read(pf, (unsigned char *)obstacles, (unsigned int)res);
846 ptcache_file_read(pf, &dt, 1, sizeof(float));
847 ptcache_file_read(pf, &dx, 1, sizeof(float));
848 ptcache_file_read(pf, &sds->p0, 3, sizeof(float));
849 ptcache_file_read(pf, &sds->p1, 3, sizeof(float));
850 ptcache_file_read(pf, &sds->dp0, 3, sizeof(float));
851 ptcache_file_read(pf, &sds->shift, 3, sizeof(int));
852 ptcache_file_read(pf, &sds->obj_shift_f, 3, sizeof(float));
853 ptcache_file_read(pf, &sds->obmat, 16, sizeof(float));
854 ptcache_file_read(pf, &sds->base_res, 3, sizeof(int));
855 ptcache_file_read(pf, &sds->res_min, 3, sizeof(int));
856 ptcache_file_read(pf, &sds->res_max, 3, sizeof(int));
857 ptcache_file_read(pf, &sds->active_color, 3, sizeof(float));
860 if (pf->data_types & (1<<BPHYS_DATA_SMOKE_HIGH) && sds->wt) {
861 int res = sds->res[0]*sds->res[1]*sds->res[2];
862 int res_big, res_big_array[3];
863 float *dens, *react, *fuel, *flame, *tcu, *tcv, *tcw, *r, *g, *b;
864 unsigned int out_len = sizeof(float)*(unsigned int)res;
865 unsigned int out_len_big;
867 smoke_turbulence_get_res(sds->wt, res_big_array);
868 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
869 out_len_big = sizeof(float) * (unsigned int)res_big;
871 smoke_turbulence_export(sds->wt, &dens, &react, &flame, &fuel, &r, &g, &b, &tcu, &tcv, &tcw);
873 ptcache_file_compressed_read(pf, (unsigned char *)dens, out_len_big);
874 if (cache_fields & SM_ACTIVE_FIRE) {
875 ptcache_file_compressed_read(pf, (unsigned char *)flame, out_len_big);
876 ptcache_file_compressed_read(pf, (unsigned char *)fuel, out_len_big);
877 ptcache_file_compressed_read(pf, (unsigned char *)react, out_len_big);
879 if (cache_fields & SM_ACTIVE_COLORS) {
880 ptcache_file_compressed_read(pf, (unsigned char *)r, out_len_big);
881 ptcache_file_compressed_read(pf, (unsigned char *)g, out_len_big);
882 ptcache_file_compressed_read(pf, (unsigned char *)b, out_len_big);
885 ptcache_file_compressed_read(pf, (unsigned char *)tcu, out_len);
886 ptcache_file_compressed_read(pf, (unsigned char *)tcv, out_len);
887 ptcache_file_compressed_read(pf, (unsigned char *)tcw, out_len);
895 * Construct matrices which represent the fluid object, for low and high res:
903 * with `vs` = voxel size, and `px, py, pz`,
904 * the min position of the domain's bounding box.
906 static void compute_fluid_matrices(SmokeDomainSettings *sds)
910 copy_v3_v3(bbox_min, sds->p0);
912 if (sds->flags & MOD_SMOKE_ADAPTIVE_DOMAIN) {
913 bbox_min[0] += (sds->cell_size[0] * (float)sds->res_min[0]);
914 bbox_min[1] += (sds->cell_size[1] * (float)sds->res_min[1]);
915 bbox_min[2] += (sds->cell_size[2] * (float)sds->res_min[2]);
916 add_v3_v3(bbox_min, sds->obj_shift_f);
919 /* construct low res matrix */
920 size_to_mat4(sds->fluidmat, sds->cell_size);
921 copy_v3_v3(sds->fluidmat[3], bbox_min);
923 /* The smoke simulator stores voxels cell-centered, whilst VDB is node
924 * centered, so we offset the matrix by half a voxel to compensate. */
925 madd_v3_v3fl(sds->fluidmat[3], sds->cell_size, 0.5f);
927 mul_m4_m4m4(sds->fluidmat, sds->obmat, sds->fluidmat);
930 float voxel_size_high[3];
931 /* construct high res matrix */
932 mul_v3_v3fl(voxel_size_high, sds->cell_size, 1.0f / (float)(sds->amplify + 1));
933 size_to_mat4(sds->fluidmat_wt, voxel_size_high);
934 copy_v3_v3(sds->fluidmat_wt[3], bbox_min);
936 /* Same here, add half a voxel to adjust the position of the fluid. */
937 madd_v3_v3fl(sds->fluidmat_wt[3], voxel_size_high, 0.5f);
939 mul_m4_m4m4(sds->fluidmat_wt, sds->obmat, sds->fluidmat_wt);
943 static int ptcache_smoke_openvdb_write(struct OpenVDBWriter *writer, void *smoke_v)
945 SmokeModifierData *smd = (SmokeModifierData *)smoke_v;
946 SmokeDomainSettings *sds = smd->domain;
948 OpenVDBWriter_set_flags(writer, sds->openvdb_comp, (sds->data_depth == 16));
950 OpenVDBWriter_add_meta_int(writer, "blender/smoke/active_fields", sds->active_fields);
951 OpenVDBWriter_add_meta_v3_int(writer, "blender/smoke/resolution", sds->res);
952 OpenVDBWriter_add_meta_v3_int(writer, "blender/smoke/min_resolution", sds->res_min);
953 OpenVDBWriter_add_meta_v3_int(writer, "blender/smoke/max_resolution", sds->res_max);
954 OpenVDBWriter_add_meta_v3_int(writer, "blender/smoke/base_resolution", sds->base_res);
955 OpenVDBWriter_add_meta_v3(writer, "blender/smoke/min_bbox", sds->p0);
956 OpenVDBWriter_add_meta_v3(writer, "blender/smoke/max_bbox", sds->p1);
957 OpenVDBWriter_add_meta_v3(writer, "blender/smoke/dp0", sds->dp0);
958 OpenVDBWriter_add_meta_v3_int(writer, "blender/smoke/shift", sds->shift);
959 OpenVDBWriter_add_meta_v3(writer, "blender/smoke/obj_shift_f", sds->obj_shift_f);
960 OpenVDBWriter_add_meta_v3(writer, "blender/smoke/active_color", sds->active_color);
961 OpenVDBWriter_add_meta_mat4(writer, "blender/smoke/obmat", sds->obmat);
963 int fluid_fields = smoke_get_data_flags(sds);
965 struct OpenVDBFloatGrid *clip_grid = NULL;
967 compute_fluid_matrices(sds);
969 OpenVDBWriter_add_meta_int(writer, "blender/smoke/fluid_fields", fluid_fields);
972 struct OpenVDBFloatGrid *wt_density_grid;
973 float *dens, *react, *fuel, *flame, *tcu, *tcv, *tcw, *r, *g, *b;
975 smoke_turbulence_export(sds->wt, &dens, &react, &flame, &fuel, &r, &g, &b, &tcu, &tcv, &tcw);
977 wt_density_grid = OpenVDB_export_grid_fl(writer, "density", dens, sds->res_wt, sds->fluidmat_wt, sds->clipping, NULL);
978 clip_grid = wt_density_grid;
980 if (fluid_fields & SM_ACTIVE_FIRE) {
981 OpenVDB_export_grid_fl(writer, "flame", flame, sds->res_wt, sds->fluidmat_wt, sds->clipping, wt_density_grid);
982 OpenVDB_export_grid_fl(writer, "fuel", fuel, sds->res_wt, sds->fluidmat_wt, sds->clipping, wt_density_grid);
983 OpenVDB_export_grid_fl(writer, "react", react, sds->res_wt, sds->fluidmat_wt, sds->clipping, wt_density_grid);
986 if (fluid_fields & SM_ACTIVE_COLORS) {
987 OpenVDB_export_grid_vec(writer, "color", r, g, b, sds->res_wt, sds->fluidmat_wt, VEC_INVARIANT, true, sds->clipping, wt_density_grid);
990 OpenVDB_export_grid_vec(writer, "texture coordinates", tcu, tcv, tcw, sds->res, sds->fluidmat, VEC_INVARIANT, false, sds->clipping, wt_density_grid);
994 struct OpenVDBFloatGrid *density_grid;
995 float dt, dx, *dens, *react, *fuel, *flame, *heat, *heatold, *vx, *vy, *vz, *r, *g, *b;
996 unsigned char *obstacles;
998 smoke_export(sds->fluid, &dt, &dx, &dens, &react, &flame, &fuel, &heat,
999 &heatold, &vx, &vy, &vz, &r, &g, &b, &obstacles);
1001 OpenVDBWriter_add_meta_fl(writer, "blender/smoke/dx", dx);
1002 OpenVDBWriter_add_meta_fl(writer, "blender/smoke/dt", dt);
1004 const char *name = (!sds->wt) ? "density" : "density_low";
1005 density_grid = OpenVDB_export_grid_fl(writer, name, dens, sds->res, sds->fluidmat, sds->clipping, NULL);
1006 clip_grid = sds->wt ? clip_grid : density_grid;
1008 OpenVDB_export_grid_fl(writer, "shadow", sds->shadow, sds->res, sds->fluidmat, sds->clipping, NULL);
1010 if (fluid_fields & SM_ACTIVE_HEAT) {
1011 OpenVDB_export_grid_fl(writer, "heat", heat, sds->res, sds->fluidmat, sds->clipping, clip_grid);
1012 OpenVDB_export_grid_fl(writer, "heat_old", heatold, sds->res, sds->fluidmat, sds->clipping, clip_grid);
1015 if (fluid_fields & SM_ACTIVE_FIRE) {
1016 name = (!sds->wt) ? "flame" : "flame_low";
1017 OpenVDB_export_grid_fl(writer, name, flame, sds->res, sds->fluidmat, sds->clipping, density_grid);
1018 name = (!sds->wt) ? "fuel" : "fuel_low";
1019 OpenVDB_export_grid_fl(writer, name, fuel, sds->res, sds->fluidmat, sds->clipping, density_grid);
1020 name = (!sds->wt) ? "react" : "react_low";
1021 OpenVDB_export_grid_fl(writer, name, react, sds->res, sds->fluidmat, sds->clipping, density_grid);
1024 if (fluid_fields & SM_ACTIVE_COLORS) {
1025 name = (!sds->wt) ? "color" : "color_low";
1026 OpenVDB_export_grid_vec(writer, name, r, g, b, sds->res, sds->fluidmat, VEC_INVARIANT, true, sds->clipping, density_grid);
1029 OpenVDB_export_grid_vec(writer, "velocity", vx, vy, vz, sds->res, sds->fluidmat, VEC_CONTRAVARIANT_RELATIVE, false, sds->clipping, clip_grid);
1030 OpenVDB_export_grid_ch(writer, "obstacles", obstacles, sds->res, sds->fluidmat, sds->clipping, NULL);
1036 static int ptcache_smoke_openvdb_read(struct OpenVDBReader *reader, void *smoke_v)
1038 SmokeModifierData *smd = (SmokeModifierData *)smoke_v;
1044 SmokeDomainSettings *sds = smd->domain;
1046 int fluid_fields = smoke_get_data_flags(sds);
1047 int active_fields, cache_fields = 0;
1050 bool reallocate = false;
1052 OpenVDBReader_get_meta_v3_int(reader, "blender/smoke/min_resolution", sds->res_min);
1053 OpenVDBReader_get_meta_v3_int(reader, "blender/smoke/max_resolution", sds->res_max);
1054 OpenVDBReader_get_meta_v3_int(reader, "blender/smoke/base_resolution", sds->base_res);
1055 OpenVDBReader_get_meta_v3(reader, "blender/smoke/min_bbox", sds->p0);
1056 OpenVDBReader_get_meta_v3(reader, "blender/smoke/max_bbox", sds->p1);
1057 OpenVDBReader_get_meta_v3(reader, "blender/smoke/dp0", sds->dp0);
1058 OpenVDBReader_get_meta_v3_int(reader, "blender/smoke/shift", sds->shift);
1059 OpenVDBReader_get_meta_v3(reader, "blender/smoke/obj_shift_f", sds->obj_shift_f);
1060 OpenVDBReader_get_meta_v3(reader, "blender/smoke/active_color", sds->active_color);
1061 OpenVDBReader_get_meta_mat4(reader, "blender/smoke/obmat", sds->obmat);
1062 OpenVDBReader_get_meta_int(reader, "blender/smoke/fluid_fields", &cache_fields);
1063 OpenVDBReader_get_meta_int(reader, "blender/smoke/active_fields", &active_fields);
1064 OpenVDBReader_get_meta_fl(reader, "blender/smoke/dx", &cache_dx);
1065 OpenVDBReader_get_meta_v3_int(reader, "blender/smoke/resolution", cache_res);
1067 /* check if resolution has changed */
1068 if (sds->res[0] != cache_res[0] ||
1069 sds->res[1] != cache_res[1] ||
1070 sds->res[2] != cache_res[2])
1072 if (sds->flags & MOD_SMOKE_ADAPTIVE_DOMAIN) {
1080 /* check if active fields have changed */
1081 if ((fluid_fields != cache_fields) || (active_fields != sds->active_fields)) {
1085 /* reallocate fluid if needed*/
1087 sds->active_fields = active_fields | cache_fields;
1088 smoke_reallocate_fluid(sds, cache_dx, cache_res, 1);
1090 copy_v3_v3_int(sds->res, cache_res);
1091 sds->total_cells = cache_res[0] * cache_res[1] * cache_res[2];
1093 if (sds->flags & MOD_SMOKE_HIGHRES) {
1094 smoke_reallocate_highres_fluid(sds, cache_dx, cache_res, 1);
1099 float dt, dx, *dens, *react, *fuel, *flame, *heat, *heatold, *vx, *vy, *vz, *r, *g, *b;
1100 unsigned char *obstacles;
1102 smoke_export(sds->fluid, &dt, &dx, &dens, &react, &flame, &fuel, &heat,
1103 &heatold, &vx, &vy, &vz, &r, &g, &b, &obstacles);
1105 OpenVDBReader_get_meta_fl(reader, "blender/smoke/dt", &dt);
1107 OpenVDB_import_grid_fl(reader, "shadow", &sds->shadow, sds->res);
1109 const char *name = (!sds->wt) ? "density" : "density_low";
1110 OpenVDB_import_grid_fl(reader, name, &dens, sds->res);
1112 if (cache_fields & SM_ACTIVE_HEAT) {
1113 OpenVDB_import_grid_fl(reader, "heat", &heat, sds->res);
1114 OpenVDB_import_grid_fl(reader, "heat_old", &heatold, sds->res);
1117 if (cache_fields & SM_ACTIVE_FIRE) {
1118 name = (!sds->wt) ? "flame" : "flame_low";
1119 OpenVDB_import_grid_fl(reader, name, &flame, sds->res);
1120 name = (!sds->wt) ? "fuel" : "fuel_low";
1121 OpenVDB_import_grid_fl(reader, name, &fuel, sds->res);
1122 name = (!sds->wt) ? "react" : "react_low";
1123 OpenVDB_import_grid_fl(reader, name, &react, sds->res);
1126 if (cache_fields & SM_ACTIVE_COLORS) {
1127 name = (!sds->wt) ? "color" : "color_low";
1128 OpenVDB_import_grid_vec(reader, name, &r, &g, &b, sds->res);
1131 OpenVDB_import_grid_vec(reader, "velocity", &vx, &vy, &vz, sds->res);
1132 OpenVDB_import_grid_ch(reader, "obstacles", &obstacles, sds->res);
1136 float *dens, *react, *fuel, *flame, *tcu, *tcv, *tcw, *r, *g, *b;
1138 smoke_turbulence_export(sds->wt, &dens, &react, &flame, &fuel, &r, &g, &b, &tcu, &tcv, &tcw);
1140 OpenVDB_import_grid_fl(reader, "density", &dens, sds->res_wt);
1142 if (cache_fields & SM_ACTIVE_FIRE) {
1143 OpenVDB_import_grid_fl(reader, "flame", &flame, sds->res_wt);
1144 OpenVDB_import_grid_fl(reader, "fuel", &fuel, sds->res_wt);
1145 OpenVDB_import_grid_fl(reader, "react", &react, sds->res_wt);
1148 if (cache_fields & SM_ACTIVE_COLORS) {
1149 OpenVDB_import_grid_vec(reader, "color", &r, &g, &b, sds->res_wt);
1152 OpenVDB_import_grid_vec(reader, "texture coordinates", &tcu, &tcv, &tcw, sds->res);
1155 OpenVDBReader_free(reader);
1162 static int ptcache_smoke_totpoint(void *UNUSED(smoke_v), int UNUSED(cfra)) { return 0; }
1163 static void ptcache_smoke_error(void *UNUSED(smoke_v), const char *UNUSED(message)) { }
1164 static int ptcache_smoke_read(PTCacheFile *UNUSED(pf), void *UNUSED(smoke_v)) { return 0; }
1165 static int ptcache_smoke_write(PTCacheFile *UNUSED(pf), void *UNUSED(smoke_v)) { return 0; }
1166 #endif // WITH_SMOKE
1168 #if !defined(WITH_SMOKE) || !defined(WITH_OPENVDB)
1169 static int ptcache_smoke_openvdb_write(struct OpenVDBWriter *writer, void *smoke_v)
1171 UNUSED_VARS(writer, smoke_v);
1175 static int ptcache_smoke_openvdb_read(struct OpenVDBReader *reader, void *smoke_v)
1177 UNUSED_VARS(reader, smoke_v);
1182 static int ptcache_dynamicpaint_totpoint(void *sd, int UNUSED(cfra))
1184 DynamicPaintSurface *surface = (DynamicPaintSurface*)sd;
1186 if (!surface->data) return 0;
1187 else return surface->data->total_points;
1190 static void ptcache_dynamicpaint_error(void *UNUSED(sd), const char *UNUSED(message))
1192 /* ignored for now */
1195 #define DPAINT_CACHE_VERSION "1.01"
1197 static int ptcache_dynamicpaint_write(PTCacheFile *pf, void *dp_v)
1199 DynamicPaintSurface *surface = (DynamicPaintSurface*)dp_v;
1200 int cache_compress = 1;
1202 /* version header */
1203 ptcache_file_write(pf, DPAINT_CACHE_VERSION, 1, sizeof(char) * 4);
1205 if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
1206 int total_points=surface->data->total_points;
1207 unsigned int in_len;
1211 ptcache_file_write(pf, &surface->type, 1, sizeof(int));
1213 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
1214 in_len = sizeof(PaintPoint) * total_points;
1216 else if (surface->type == MOD_DPAINT_SURFACE_T_DISPLACE ||
1217 surface->type == MOD_DPAINT_SURFACE_T_WEIGHT)
1219 in_len = sizeof(float) * total_points;
1221 else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
1222 in_len = sizeof(PaintWavePoint) * total_points;
1228 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
1230 ptcache_file_compressed_write(pf, (unsigned char *)surface->data->type_data, in_len, out, cache_compress);
1236 static int ptcache_dynamicpaint_read(PTCacheFile *pf, void *dp_v)
1238 DynamicPaintSurface *surface = (DynamicPaintSurface*)dp_v;
1241 /* version header */
1242 ptcache_file_read(pf, version, 1, sizeof(char) * 4);
1243 if (!STREQLEN(version, DPAINT_CACHE_VERSION, 4)) {
1244 printf("Dynamic Paint: Invalid cache version: '%c%c%c%c'!\n", UNPACK4(version));
1248 if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
1249 unsigned int data_len;
1253 ptcache_file_read(pf, &surface_type, 1, sizeof(int));
1255 if (surface_type != surface->type)
1258 /* read surface data */
1259 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
1260 data_len = sizeof(PaintPoint);
1262 else if (surface->type == MOD_DPAINT_SURFACE_T_DISPLACE ||
1263 surface->type == MOD_DPAINT_SURFACE_T_WEIGHT)
1265 data_len = sizeof(float);
1267 else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
1268 data_len = sizeof(PaintWavePoint);
1274 ptcache_file_compressed_read(pf, (unsigned char *)surface->data->type_data, data_len*surface->data->total_points);
1280 /* Rigid Body functions */
1281 static int ptcache_rigidbody_write(int index, void *rb_v, void **data, int UNUSED(cfra))
1283 RigidBodyWorld *rbw = rb_v;
1287 ob = rbw->objects[index];
1289 if (ob && ob->rigidbody_object) {
1290 RigidBodyOb *rbo = ob->rigidbody_object;
1292 if (rbo->type == RBO_TYPE_ACTIVE) {
1294 RB_body_get_position(rbo->physics_object, rbo->pos);
1295 RB_body_get_orientation(rbo->physics_object, rbo->orn);
1297 PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, rbo->pos);
1298 PTCACHE_DATA_FROM(data, BPHYS_DATA_ROTATION, rbo->orn);
1304 static void ptcache_rigidbody_read(int index, void *rb_v, void **data, float UNUSED(cfra), float *old_data)
1306 RigidBodyWorld *rbw = rb_v;
1310 ob = rbw->objects[index];
1312 if (ob && ob->rigidbody_object) {
1313 RigidBodyOb *rbo = ob->rigidbody_object;
1315 if (rbo->type == RBO_TYPE_ACTIVE) {
1318 memcpy(rbo->pos, data, 3 * sizeof(float));
1319 memcpy(rbo->orn, data + 3, 4 * sizeof(float));
1322 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, rbo->pos);
1323 PTCACHE_DATA_TO(data, BPHYS_DATA_ROTATION, 0, rbo->orn);
1328 static void ptcache_rigidbody_interpolate(int index, void *rb_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
1330 RigidBodyWorld *rbw = rb_v;
1334 ob = rbw->objects[index];
1336 if (ob && ob->rigidbody_object) {
1337 RigidBodyOb *rbo = ob->rigidbody_object;
1339 if (rbo->type == RBO_TYPE_ACTIVE) {
1340 ParticleKey keys[4];
1344 memset(keys, 0, sizeof(keys));
1346 copy_v3_v3(keys[1].co, rbo->pos);
1347 copy_qt_qt(keys[1].rot, rbo->orn);
1350 memcpy(keys[2].co, data, 3 * sizeof(float));
1351 memcpy(keys[2].rot, data + 3, 4 * sizeof(float));
1354 BKE_ptcache_make_particle_key(&keys[2], 0, data, cfra2);
1357 dfra = cfra2 - cfra1;
1359 /* note: keys[0] and keys[3] unused for type < 1 (crappy) */
1360 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, &result, true);
1361 interp_qt_qtqt(result.rot, keys[1].rot, keys[2].rot, (cfra - cfra1) / dfra);
1363 copy_v3_v3(rbo->pos, result.co);
1364 copy_qt_qt(rbo->orn, result.rot);
1368 static int ptcache_rigidbody_totpoint(void *rb_v, int UNUSED(cfra))
1370 RigidBodyWorld *rbw = rb_v;
1372 return rbw->numbodies;
1375 static void ptcache_rigidbody_error(void *UNUSED(rb_v), const char *UNUSED(message))
1377 /* ignored for now */
1381 void BKE_ptcache_id_from_softbody(PTCacheID *pid, Object *ob, SoftBody *sb)
1383 memset(pid, 0, sizeof(PTCacheID));
1387 pid->type= PTCACHE_TYPE_SOFTBODY;
1388 pid->cache= sb->pointcache;
1389 pid->cache_ptr= &sb->pointcache;
1390 pid->ptcaches= &sb->ptcaches;
1391 pid->totpoint= pid->totwrite= ptcache_softbody_totpoint;
1392 pid->error = ptcache_softbody_error;
1394 pid->write_point = ptcache_softbody_write;
1395 pid->read_point = ptcache_softbody_read;
1396 pid->interpolate_point = ptcache_softbody_interpolate;
1398 pid->write_stream = NULL;
1399 pid->read_stream = NULL;
1401 pid->write_openvdb_stream = NULL;
1402 pid->read_openvdb_stream = NULL;
1404 pid->write_extra_data = NULL;
1405 pid->read_extra_data = NULL;
1406 pid->interpolate_extra_data = NULL;
1408 pid->write_header = ptcache_basic_header_write;
1409 pid->read_header = ptcache_basic_header_read;
1411 pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY);
1414 pid->stack_index = pid->cache->index;
1416 pid->default_step = 10;
1418 pid->file_type = PTCACHE_FILE_PTCACHE;
1420 void BKE_ptcache_id_from_particles(PTCacheID *pid, Object *ob, ParticleSystem *psys)
1422 memset(pid, 0, sizeof(PTCacheID));
1425 pid->calldata= psys;
1426 pid->type= PTCACHE_TYPE_PARTICLES;
1427 pid->stack_index= psys->pointcache->index;
1428 pid->cache= psys->pointcache;
1429 pid->cache_ptr= &psys->pointcache;
1430 pid->ptcaches= &psys->ptcaches;
1432 if (psys->part->type != PART_HAIR)
1433 pid->flag |= PTCACHE_VEL_PER_SEC;
1435 pid->totpoint = ptcache_particle_totpoint;
1436 pid->totwrite = ptcache_particle_totwrite;
1437 pid->error = ptcache_particle_error;
1439 pid->write_point = ptcache_particle_write;
1440 pid->read_point = ptcache_particle_read;
1441 pid->interpolate_point = ptcache_particle_interpolate;
1443 pid->write_stream = NULL;
1444 pid->read_stream = NULL;
1446 pid->write_openvdb_stream = NULL;
1447 pid->read_openvdb_stream = NULL;
1449 pid->write_extra_data = NULL;
1450 pid->read_extra_data = NULL;
1451 pid->interpolate_extra_data = NULL;
1453 pid->write_header = ptcache_basic_header_write;
1454 pid->read_header = ptcache_basic_header_read;
1456 pid->data_types = (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_INDEX);
1458 if (psys->part->phystype == PART_PHYS_BOIDS)
1459 pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION) | (1<<BPHYS_DATA_BOIDS);
1460 else if (psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS) {
1461 pid->write_extra_data = ptcache_particle_extra_write;
1462 pid->read_extra_data = ptcache_particle_extra_read;
1465 if (psys->part->flag & PART_ROTATIONS) {
1466 pid->data_types|= (1<<BPHYS_DATA_ROTATION);
1468 if (psys->part->rotmode != PART_ROT_VEL ||
1469 psys->part->avemode == PART_AVE_RAND ||
1470 psys->part->avefac != 0.0f)
1472 pid->data_types |= (1 << BPHYS_DATA_AVELOCITY);
1476 pid->info_types= (1<<BPHYS_DATA_TIMES);
1478 pid->default_step = 10;
1480 pid->file_type = PTCACHE_FILE_PTCACHE;
1482 void BKE_ptcache_id_from_cloth(PTCacheID *pid, Object *ob, ClothModifierData *clmd)
1484 memset(pid, 0, sizeof(PTCacheID));
1487 pid->calldata= clmd;
1488 pid->type= PTCACHE_TYPE_CLOTH;
1489 pid->stack_index= clmd->point_cache->index;
1490 pid->cache= clmd->point_cache;
1491 pid->cache_ptr= &clmd->point_cache;
1492 pid->ptcaches= &clmd->ptcaches;
1493 pid->totpoint= pid->totwrite= ptcache_cloth_totpoint;
1494 pid->error = ptcache_cloth_error;
1496 pid->write_point = ptcache_cloth_write;
1497 pid->read_point = ptcache_cloth_read;
1498 pid->interpolate_point = ptcache_cloth_interpolate;
1500 pid->write_openvdb_stream = NULL;
1501 pid->read_openvdb_stream = NULL;
1503 pid->write_stream = NULL;
1504 pid->read_stream = NULL;
1506 pid->write_extra_data = NULL;
1507 pid->read_extra_data = NULL;
1508 pid->interpolate_extra_data = NULL;
1510 pid->write_header = ptcache_basic_header_write;
1511 pid->read_header = ptcache_basic_header_read;
1513 pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_XCONST);
1516 pid->default_step = 1;
1518 pid->file_type = PTCACHE_FILE_PTCACHE;
1520 void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct SmokeModifierData *smd)
1522 SmokeDomainSettings *sds = smd->domain;
1524 memset(pid, 0, sizeof(PTCacheID));
1529 pid->type= PTCACHE_TYPE_SMOKE_DOMAIN;
1530 pid->stack_index= sds->point_cache[0]->index;
1532 pid->cache= sds->point_cache[0];
1533 pid->cache_ptr= &(sds->point_cache[0]);
1534 pid->ptcaches= &(sds->ptcaches[0]);
1536 pid->totpoint= pid->totwrite= ptcache_smoke_totpoint;
1537 pid->error = ptcache_smoke_error;
1539 pid->write_point = NULL;
1540 pid->read_point = NULL;
1541 pid->interpolate_point = NULL;
1543 pid->read_stream = ptcache_smoke_read;
1544 pid->write_stream = ptcache_smoke_write;
1546 pid->write_openvdb_stream = ptcache_smoke_openvdb_write;
1547 pid->read_openvdb_stream = ptcache_smoke_openvdb_read;
1549 pid->write_extra_data = NULL;
1550 pid->read_extra_data = NULL;
1551 pid->interpolate_extra_data = NULL;
1553 pid->write_header = ptcache_basic_header_write;
1554 pid->read_header = ptcache_basic_header_read;
1560 pid->data_types |= (1<<BPHYS_DATA_SMOKE_LOW);
1562 pid->data_types |= (1<<BPHYS_DATA_SMOKE_HIGH);
1564 pid->default_step = 1;
1566 pid->file_type = smd->domain->cache_file_format;
1569 void BKE_ptcache_id_from_dynamicpaint(PTCacheID *pid, Object *ob, DynamicPaintSurface *surface)
1572 memset(pid, 0, sizeof(PTCacheID));
1575 pid->calldata= surface;
1576 pid->type= PTCACHE_TYPE_DYNAMICPAINT;
1577 pid->cache= surface->pointcache;
1578 pid->cache_ptr= &surface->pointcache;
1579 pid->ptcaches= &surface->ptcaches;
1580 pid->totpoint= pid->totwrite= ptcache_dynamicpaint_totpoint;
1581 pid->error = ptcache_dynamicpaint_error;
1583 pid->write_point = NULL;
1584 pid->read_point = NULL;
1585 pid->interpolate_point = NULL;
1587 pid->write_stream = ptcache_dynamicpaint_write;
1588 pid->read_stream = ptcache_dynamicpaint_read;
1590 pid->write_openvdb_stream = NULL;
1591 pid->read_openvdb_stream = NULL;
1593 pid->write_extra_data = NULL;
1594 pid->read_extra_data = NULL;
1595 pid->interpolate_extra_data = NULL;
1597 pid->write_header = ptcache_basic_header_write;
1598 pid->read_header = ptcache_basic_header_read;
1600 pid->data_types= BPHYS_DATA_DYNAMICPAINT;
1603 pid->stack_index = pid->cache->index;
1605 pid->default_step = 1;
1607 pid->file_type = PTCACHE_FILE_PTCACHE;
1610 void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, Object *ob, RigidBodyWorld *rbw)
1613 memset(pid, 0, sizeof(PTCacheID));
1617 pid->type= PTCACHE_TYPE_RIGIDBODY;
1618 pid->cache= rbw->pointcache;
1619 pid->cache_ptr= &rbw->pointcache;
1620 pid->ptcaches= &rbw->ptcaches;
1621 pid->totpoint= pid->totwrite= ptcache_rigidbody_totpoint;
1622 pid->error = ptcache_rigidbody_error;
1624 pid->write_point = ptcache_rigidbody_write;
1625 pid->read_point = ptcache_rigidbody_read;
1626 pid->interpolate_point = ptcache_rigidbody_interpolate;
1628 pid->write_stream = NULL;
1629 pid->read_stream = NULL;
1631 pid->write_openvdb_stream = NULL;
1632 pid->read_openvdb_stream = NULL;
1634 pid->write_extra_data = NULL;
1635 pid->read_extra_data = NULL;
1636 pid->interpolate_extra_data = NULL;
1638 pid->write_header = ptcache_basic_header_write;
1639 pid->read_header = ptcache_basic_header_read;
1641 pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_ROTATION);
1644 pid->stack_index = pid->cache->index;
1646 pid->default_step = 1;
1648 pid->file_type = PTCACHE_FILE_PTCACHE;
1651 void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int duplis)
1654 ParticleSystem *psys;
1657 lb->first= lb->last= NULL;
1660 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1661 BKE_ptcache_id_from_softbody(pid, ob, ob->soft);
1662 BLI_addtail(lb, pid);
1665 for (psys=ob->particlesystem.first; psys; psys=psys->next) {
1666 if (psys->part==NULL)
1669 /* check to make sure point cache is actually used by the particles */
1670 if (ELEM(psys->part->phystype, PART_PHYS_NO, PART_PHYS_KEYED))
1673 /* hair needs to be included in id-list for cache edit mode to work */
1674 /* if (psys->part->type == PART_HAIR && (psys->flag & PSYS_HAIR_DYNAMICS)==0) */
1677 if (psys->part->type == PART_FLUID)
1680 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1681 BKE_ptcache_id_from_particles(pid, ob, psys);
1682 BLI_addtail(lb, pid);
1685 for (md=ob->modifiers.first; md; md=md->next) {
1686 if (md->type == eModifierType_Cloth) {
1687 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1688 BKE_ptcache_id_from_cloth(pid, ob, (ClothModifierData*)md);
1689 BLI_addtail(lb, pid);
1691 else if (md->type == eModifierType_Smoke) {
1692 SmokeModifierData *smd = (SmokeModifierData *)md;
1693 if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
1694 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1695 BKE_ptcache_id_from_smoke(pid, ob, (SmokeModifierData*)md);
1696 BLI_addtail(lb, pid);
1699 else if (md->type == eModifierType_DynamicPaint) {
1700 DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
1702 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
1704 for (; surface; surface=surface->next) {
1705 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1706 BKE_ptcache_id_from_dynamicpaint(pid, ob, surface);
1707 BLI_addtail(lb, pid);
1713 if (scene && ob->rigidbody_object && scene->rigidbody_world) {
1714 pid = MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1715 BKE_ptcache_id_from_rigidbody(pid, ob, scene->rigidbody_world);
1716 BLI_addtail(lb, pid);
1719 if (scene && (duplis-- > 0) && (ob->transflag & OB_DUPLI)) {
1720 ListBase *lb_dupli_ob;
1721 /* don't update the dupli groups, we only want their pid's */
1722 if ((lb_dupli_ob = object_duplilist_ex(G.main, G.main->eval_ctx, scene, ob, false))) {
1724 for (dob= lb_dupli_ob->first; dob; dob= dob->next) {
1725 if (dob->ob != ob) { /* avoids recursive loops with dupliframes: bug 22988 */
1726 ListBase lb_dupli_pid;
1727 BKE_ptcache_ids_from_object(&lb_dupli_pid, dob->ob, scene, duplis);
1728 BLI_movelisttolist(lb, &lb_dupli_pid);
1729 if (lb_dupli_pid.first)
1730 printf("Adding Dupli\n");
1734 free_object_duplilist(lb_dupli_ob); /* does restore */
1741 static const char *ptcache_file_extension(const PTCacheID *pid)
1743 switch (pid->file_type) {
1745 case PTCACHE_FILE_PTCACHE:
1747 case PTCACHE_FILE_OPENVDB:
1753 * Similar to #BLI_path_frame_get, but takes into account the stack-index which is after the frame.
1755 static int ptcache_frame_from_filename(const char *filename, const char *ext)
1757 const int frame_len = 6;
1758 const int ext_len = frame_len + strlen(ext);
1759 const int len = strlen(filename);
1761 /* could crash if trying to copy a string out of this range */
1762 if (len > ext_len) {
1763 /* using frame_len here gives compile error (vla) */
1764 char num[/* frame_len */6 + 1];
1765 BLI_strncpy(num, filename + len - ext_len, sizeof(num));
1773 /* Takes an Object ID and returns a unique name
1775 * - cfra: frame for the cache, can be negative
1776 * - stack_index: index in the modifier stack. we can have cache for more than one stack_index
1779 #define MAX_PTCACHE_PATH FILE_MAX
1780 #define MAX_PTCACHE_FILE (FILE_MAX * 2)
1782 static int ptcache_path(PTCacheID *pid, char *filename)
1784 Library *lib = (pid->ob) ? pid->ob->id.lib : NULL;
1785 const char *blendfilename= (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH)==0) ? lib->filepath: BKE_main_blendfile_path_from_global();
1788 if (pid->cache->flag & PTCACHE_EXTERNAL) {
1789 strcpy(filename, pid->cache->path);
1791 if (BLI_path_is_rel(filename)) {
1792 BLI_path_abs(filename, blendfilename);
1795 return BLI_add_slash(filename); /* new strlen() */
1797 else if (G.relbase_valid || lib) {
1798 char file[MAX_PTCACHE_PATH]; /* we don't want the dir, only the file */
1800 BLI_split_file_part(blendfilename, file, sizeof(file));
1807 BLI_snprintf(filename, MAX_PTCACHE_PATH, "//"PTCACHE_PATH"%s", file); /* add blend file name to pointcache dir */
1808 BLI_path_abs(filename, blendfilename);
1809 return BLI_add_slash(filename); /* new strlen() */
1812 /* use the temp path. this is weak but better then not using point cache at all */
1813 /* temporary directory is assumed to exist and ALWAYS has a trailing slash */
1814 BLI_snprintf(filename, MAX_PTCACHE_PATH, "%s"PTCACHE_PATH, BKE_tempdir_session());
1816 return BLI_add_slash(filename); /* new strlen() */
1819 static int ptcache_filename(PTCacheID *pid, char *filename, int cfra, short do_path, short do_ext)
1827 if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return 0; /* save blend file before using disk pointcache */
1829 /* start with temp dir */
1831 len = ptcache_path(pid, filename);
1834 if (pid->cache->name[0] == '\0' && (pid->cache->flag & PTCACHE_EXTERNAL)==0) {
1835 idname = (pid->ob->id.name + 2);
1836 /* convert chars to hex so they are always a valid filename */
1837 while ('\0' != *idname) {
1838 BLI_snprintf(newname, MAX_PTCACHE_FILE, "%02X", (unsigned int)(*idname++));
1844 int temp = (int)strlen(pid->cache->name);
1845 strcpy(newname, pid->cache->name);
1851 if (pid->cache->index < 0)
1852 pid->cache->index = pid->stack_index = BKE_object_insert_ptcache(pid->ob);
1854 const char *ext = ptcache_file_extension(pid);
1856 if (pid->cache->flag & PTCACHE_EXTERNAL) {
1857 if (pid->cache->index >= 0)
1858 BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02u%s", cfra, pid->stack_index, ext); /* always 6 chars */
1860 BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d%s", cfra, ext); /* always 6 chars */
1863 BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02u%s", cfra, pid->stack_index, ext); /* always 6 chars */
1868 return len; /* make sure the above string is always 16 chars */
1871 /* youll need to close yourself after! */
1872 static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
1876 char filename[FILE_MAX * 2];
1878 #ifndef DURIAN_POINTCACHE_LIB_OK
1879 /* don't allow writing for linked objects */
1880 if (pid->ob->id.lib && mode == PTCACHE_FILE_WRITE)
1883 if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return NULL; /* save blend file before using disk pointcache */
1885 ptcache_filename(pid, filename, cfra, 1, 1);
1887 if (mode==PTCACHE_FILE_READ) {
1888 fp = BLI_fopen(filename, "rb");
1890 else if (mode==PTCACHE_FILE_WRITE) {
1891 BLI_make_existing_file(filename); /* will create the dir if needs be, same as //textures is created */
1892 fp = BLI_fopen(filename, "wb");
1894 else if (mode==PTCACHE_FILE_UPDATE) {
1895 BLI_make_existing_file(filename);
1896 fp = BLI_fopen(filename, "rb+");
1902 pf= MEM_mallocN(sizeof(PTCacheFile), "PTCacheFile");
1909 static void ptcache_file_close(PTCacheFile *pf)
1917 static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result, unsigned int len)
1920 unsigned char compressed = 0;
1923 size_t out_len = len;
1926 unsigned char *props = MEM_callocN(16 * sizeof(char), "tmp");
1928 ptcache_file_read(pf, &compressed, 1, sizeof(unsigned char));
1931 ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
1932 in_len = (size_t)size;
1937 in = (unsigned char *)MEM_callocN(sizeof(unsigned char)*in_len, "pointcache_compressed_buffer");
1938 ptcache_file_read(pf, in, in_len, sizeof(unsigned char));
1940 if (compressed == 1)
1941 r = lzo1x_decompress_safe(in, (lzo_uint)in_len, result, (lzo_uint *)&out_len, NULL);
1944 if (compressed == 2) {
1946 size_t leni = in_len, leno = len;
1947 ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
1948 sizeOfIt = (size_t)size;
1949 ptcache_file_read(pf, props, sizeOfIt, sizeof(unsigned char));
1950 r = LzmaUncompress(result, &leno, in, &leni, props, sizeOfIt);
1957 ptcache_file_read(pf, result, len, sizeof(unsigned char));
1964 static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, unsigned int in_len, unsigned char *out, int mode)
1967 unsigned char compressed = 0;
1969 unsigned char *props = MEM_callocN(16 * sizeof(char), "tmp");
1970 size_t sizeOfIt = 5;
1972 (void)mode; /* unused when building w/o compression */
1975 out_len= LZO_OUT_LEN(in_len);
1977 LZO_HEAP_ALLOC(wrkmem, LZO1X_MEM_COMPRESS);
1979 r = lzo1x_1_compress(in, (lzo_uint)in_len, out, (lzo_uint *)&out_len, wrkmem);
1980 if (!(r == LZO_E_OK) || (out_len >= in_len))
1989 r = LzmaCompress(out, &out_len, in, in_len, //assume sizeof(char)==1....
1990 props, &sizeOfIt, 5, 1 << 24, 3, 0, 2, 32, 2);
1992 if (!(r == SZ_OK) || (out_len >= in_len))
1999 ptcache_file_write(pf, &compressed, 1, sizeof(unsigned char));
2001 unsigned int size = out_len;
2002 ptcache_file_write(pf, &size, 1, sizeof(unsigned int));
2003 ptcache_file_write(pf, out, out_len, sizeof(unsigned char));
2006 ptcache_file_write(pf, in, in_len, sizeof(unsigned char));
2008 if (compressed == 2) {
2009 unsigned int size = sizeOfIt;
2010 ptcache_file_write(pf, &sizeOfIt, 1, sizeof(unsigned int));
2011 ptcache_file_write(pf, props, size, sizeof(unsigned char));
2018 static int ptcache_file_read(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size)
2020 return (fread(f, size, tot, pf->fp) == tot);
2022 static int ptcache_file_write(PTCacheFile *pf, const void *f, unsigned int tot, unsigned int size)
2024 return (fwrite(f, size, tot, pf->fp) == tot);
2026 static int ptcache_file_data_read(PTCacheFile *pf)
2030 for (i=0; i<BPHYS_TOT_DATA; i++) {
2031 if ((pf->data_types & (1<<i)) && !ptcache_file_read(pf, pf->cur[i], 1, ptcache_data_size[i]))
2037 static int ptcache_file_data_write(PTCacheFile *pf)
2041 for (i=0; i<BPHYS_TOT_DATA; i++) {
2042 if ((pf->data_types & (1<<i)) && !ptcache_file_write(pf, pf->cur[i], 1, ptcache_data_size[i]))
2048 static int ptcache_file_header_begin_read(PTCacheFile *pf)
2050 unsigned int typeflag=0;
2056 if (fread(bphysics, sizeof(char), 8, pf->fp) != 8)
2059 if (!error && !STREQLEN(bphysics, "BPHYSICS", 8))
2062 if (!error && !fread(&typeflag, sizeof(unsigned int), 1, pf->fp))
2065 pf->type = (typeflag & PTCACHE_TYPEFLAG_TYPEMASK);
2066 pf->flag = (typeflag & PTCACHE_TYPEFLAG_FLAGMASK);
2068 /* if there was an error set file as it was */
2070 fseek(pf->fp, 0, SEEK_SET);
2074 static int ptcache_file_header_begin_write(PTCacheFile *pf)
2076 const char *bphysics = "BPHYSICS";
2077 unsigned int typeflag = pf->type + pf->flag;
2079 if (fwrite(bphysics, sizeof(char), 8, pf->fp) != 8)
2082 if (!fwrite(&typeflag, sizeof(unsigned int), 1, pf->fp))
2088 /* Data pointer handling */
2089 int BKE_ptcache_data_size(int data_type)
2091 return ptcache_data_size[data_type];
2094 static void ptcache_file_pointers_init(PTCacheFile *pf)
2096 int data_types = pf->data_types;
2098 pf->cur[BPHYS_DATA_INDEX] = (data_types & (1<<BPHYS_DATA_INDEX)) ? &pf->data.index : NULL;
2099 pf->cur[BPHYS_DATA_LOCATION] = (data_types & (1<<BPHYS_DATA_LOCATION)) ? &pf->data.loc : NULL;
2100 pf->cur[BPHYS_DATA_VELOCITY] = (data_types & (1<<BPHYS_DATA_VELOCITY)) ? &pf->data.vel : NULL;
2101 pf->cur[BPHYS_DATA_ROTATION] = (data_types & (1<<BPHYS_DATA_ROTATION)) ? &pf->data.rot : NULL;
2102 pf->cur[BPHYS_DATA_AVELOCITY] = (data_types & (1<<BPHYS_DATA_AVELOCITY))? &pf->data.ave : NULL;
2103 pf->cur[BPHYS_DATA_SIZE] = (data_types & (1<<BPHYS_DATA_SIZE)) ? &pf->data.size : NULL;
2104 pf->cur[BPHYS_DATA_TIMES] = (data_types & (1<<BPHYS_DATA_TIMES)) ? &pf->data.times : NULL;
2105 pf->cur[BPHYS_DATA_BOIDS] = (data_types & (1<<BPHYS_DATA_BOIDS)) ? &pf->data.boids : NULL;
2108 /* Check to see if point number "index" is in pm, uses binary search for index data. */
2109 int BKE_ptcache_mem_index_find(PTCacheMem *pm, unsigned int index)
2111 if (pm->totpoint > 0 && pm->data[BPHYS_DATA_INDEX]) {
2112 unsigned int *data = pm->data[BPHYS_DATA_INDEX];
2113 unsigned int mid, low = 0, high = pm->totpoint - 1;
2115 if (index < *data || index > *(data+high))
2118 /* check simple case for continuous indexes first */
2119 if (index-*data < high && data[index-*data] == index)
2122 while (low <= high) {
2123 mid= (low + high)/2;
2125 if (data[mid] > index)
2127 else if (data[mid] < index)
2136 return (index < pm->totpoint ? index : -1);
2140 void BKE_ptcache_mem_pointers_init(PTCacheMem *pm)
2142 int data_types = pm->data_types;
2145 for (i=0; i<BPHYS_TOT_DATA; i++)
2146 pm->cur[i] = ((data_types & (1<<i)) ? pm->data[i] : NULL);
2149 void BKE_ptcache_mem_pointers_incr(PTCacheMem *pm)
2153 for (i=0; i<BPHYS_TOT_DATA; i++) {
2155 pm->cur[i] = (char *)pm->cur[i] + ptcache_data_size[i];
2158 int BKE_ptcache_mem_pointers_seek(int point_index, PTCacheMem *pm)
2160 int data_types = pm->data_types;
2161 int i, index = BKE_ptcache_mem_index_find(pm, point_index);
2164 /* Can't give proper location without reallocation, so don't give any location.
2165 * Some points will be cached improperly, but this only happens with simulation
2166 * steps bigger than cache->step, so the cache has to be recalculated anyways
2172 for (i=0; i<BPHYS_TOT_DATA; i++)
2173 pm->cur[i] = data_types & (1<<i) ? (char *)pm->data[i] + index * ptcache_data_size[i] : NULL;
2177 static void ptcache_data_alloc(PTCacheMem *pm)
2179 int data_types = pm->data_types;
2180 int totpoint = pm->totpoint;
2183 for (i=0; i<BPHYS_TOT_DATA; i++) {
2184 if (data_types & (1<<i))
2185 pm->data[i] = MEM_callocN(totpoint * ptcache_data_size[i], "PTCache Data");
2188 static void ptcache_data_free(PTCacheMem *pm)
2190 void **data = pm->data;
2193 for (i=0; i<BPHYS_TOT_DATA; i++) {
2198 static void ptcache_data_copy(void *from[], void *to[])
2201 for (i=0; i<BPHYS_TOT_DATA; i++) {
2202 /* note, durian file 03.4b_comp crashes if to[i] is not tested
2203 * its NULL, not sure if this should be fixed elsewhere but for now its needed */
2204 if (from[i] && to[i])
2205 memcpy(to[i], from[i], ptcache_data_size[i]);
2209 static void ptcache_extra_free(PTCacheMem *pm)
2211 PTCacheExtra *extra = pm->extradata.first;
2214 for (; extra; extra=extra->next) {
2216 MEM_freeN(extra->data);
2219 BLI_freelistN(&pm->extradata);
2222 static int ptcache_old_elemsize(PTCacheID *pid)
2224 if (pid->type==PTCACHE_TYPE_SOFTBODY)
2225 return 6 * sizeof(float);
2226 else if (pid->type==PTCACHE_TYPE_PARTICLES)
2227 return sizeof(ParticleKey);
2228 else if (pid->type==PTCACHE_TYPE_CLOTH)
2229 return 9 * sizeof(float);
2234 static void ptcache_find_frames_around(PTCacheID *pid, unsigned int frame, int *fra1, int *fra2)
2236 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2237 int cfra1=frame, cfra2=frame+1;
2239 while (cfra1 >= pid->cache->startframe && !BKE_ptcache_id_exist(pid, cfra1))
2242 if (cfra1 < pid->cache->startframe)
2245 while (cfra2 <= pid->cache->endframe && !BKE_ptcache_id_exist(pid, cfra2))
2248 if (cfra2 > pid->cache->endframe)
2251 if (cfra1 && !cfra2) {
2260 else if (pid->cache->mem_cache.first) {
2261 PTCacheMem *pm = pid->cache->mem_cache.first;
2262 PTCacheMem *pm2 = pid->cache->mem_cache.last;
2264 while (pm->next && pm->next->frame <= frame)
2267 if (pm2->frame < frame) {
2271 while (pm2->prev && pm2->prev->frame > frame) {
2287 static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
2289 PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
2290 PTCacheMem *pm = NULL;
2291 unsigned int i, error = 0;
2296 if (!ptcache_file_header_begin_read(pf))
2299 if (!error && (pf->type != pid->type || !pid->read_header(pf)))
2303 pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
2305 pm->totpoint = pf->totpoint;
2306 pm->data_types = pf->data_types;
2307 pm->frame = pf->frame;
2309 ptcache_data_alloc(pm);
2311 if (pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
2312 for (i=0; i<BPHYS_TOT_DATA; i++) {
2313 unsigned int out_len = pm->totpoint*ptcache_data_size[i];
2314 if (pf->data_types & (1<<i))
2315 ptcache_file_compressed_read(pf, (unsigned char *)(pm->data[i]), out_len);
2319 BKE_ptcache_mem_pointers_init(pm);
2320 ptcache_file_pointers_init(pf);
2322 for (i=0; i<pm->totpoint; i++) {
2323 if (!ptcache_file_data_read(pf)) {
2327 ptcache_data_copy(pf->cur, pm->cur);
2328 BKE_ptcache_mem_pointers_incr(pm);
2333 if (!error && pf->flag & PTCACHE_TYPEFLAG_EXTRADATA) {
2334 unsigned int extratype = 0;
2336 while (ptcache_file_read(pf, &extratype, 1, sizeof(unsigned int))) {
2337 PTCacheExtra *extra = MEM_callocN(sizeof(PTCacheExtra), "Pointcache extradata");
2339 extra->type = extratype;
2341 ptcache_file_read(pf, &extra->totdata, 1, sizeof(unsigned int));
2343 extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Pointcache extradata->data");
2345 if (pf->flag & PTCACHE_TYPEFLAG_COMPRESS)
2346 ptcache_file_compressed_read(pf, (unsigned char *)(extra->data), extra->totdata*ptcache_extra_datasize[extra->type]);
2348 ptcache_file_read(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
2350 BLI_addtail(&pm->extradata, extra);
2355 ptcache_data_free(pm);
2356 ptcache_extra_free(pm);
2361 ptcache_file_close(pf);
2363 if (error && G.debug & G_DEBUG)
2364 printf("Error reading from disk cache\n");
2368 static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
2370 PTCacheFile *pf = NULL;
2371 unsigned int i, error = 0;
2373 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, pm->frame);
2375 pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, pm->frame);
2378 if (G.debug & G_DEBUG)
2379 printf("Error opening disk cache file for writing\n");
2383 pf->data_types = pm->data_types;
2384 pf->totpoint = pm->totpoint;
2385 pf->type = pid->type;
2388 if (pm->extradata.first)
2389 pf->flag |= PTCACHE_TYPEFLAG_EXTRADATA;
2391 if (pid->cache->compression)
2392 pf->flag |= PTCACHE_TYPEFLAG_COMPRESS;
2394 if (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf))
2398 if (pid->cache->compression) {
2399 for (i=0; i<BPHYS_TOT_DATA; i++) {
2401 unsigned int in_len = pm->totpoint*ptcache_data_size[i];
2402 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len) * 4, "pointcache_lzo_buffer");
2403 ptcache_file_compressed_write(pf, (unsigned char *)(pm->data[i]), in_len, out, pid->cache->compression);
2409 BKE_ptcache_mem_pointers_init(pm);
2410 ptcache_file_pointers_init(pf);
2412 for (i=0; i<pm->totpoint; i++) {
2413 ptcache_data_copy(pm->cur, pf->cur);
2414 if (!ptcache_file_data_write(pf)) {
2418 BKE_ptcache_mem_pointers_incr(pm);
2423 if (!error && pm->extradata.first) {
2424 PTCacheExtra *extra = pm->extradata.first;
2426 for (; extra; extra=extra->next) {
2427 if (extra->data == NULL || extra->totdata == 0)
2430 ptcache_file_write(pf, &extra->type, 1, sizeof(unsigned int));
2431 ptcache_file_write(pf, &extra->totdata, 1, sizeof(unsigned int));
2433 if (pid->cache->compression) {
2434 unsigned int in_len = extra->totdata * ptcache_extra_datasize[extra->type];
2435 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len) * 4, "pointcache_lzo_buffer");
2436 ptcache_file_compressed_write(pf, (unsigned char *)(extra->data), in_len, out, pid->cache->compression);
2440 ptcache_file_write(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
2445 ptcache_file_close(pf);
2447 if (error && G.debug & G_DEBUG)
2448 printf("Error writing to disk cache\n");
2453 static int ptcache_read_stream(PTCacheID *pid, int cfra)
2455 PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
2458 if (pid->read_stream == NULL)
2462 if (G.debug & G_DEBUG)
2463 printf("Error opening disk cache file for reading\n");
2467 if (!ptcache_file_header_begin_read(pf)) {
2468 pid->error(pid->calldata, "Failed to read point cache file");
2471 else if (pf->type != pid->type) {
2472 pid->error(pid->calldata, "Point cache file has wrong type");
2475 else if (!pid->read_header(pf)) {
2476 pid->error(pid->calldata, "Failed to read point cache file header");
2479 else if (pf->totpoint != pid->totpoint(pid->calldata, cfra)) {
2480 pid->error(pid->calldata, "Number of points in cache does not match mesh");
2485 ptcache_file_pointers_init(pf);
2487 // we have stream reading here
2488 if (!pid->read_stream(pf, pid->calldata)) {
2489 pid->error(pid->calldata, "Failed to read point cache file data");
2494 ptcache_file_close(pf);
2499 static int ptcache_read_openvdb_stream(PTCacheID *pid, int cfra)
2502 char filename[FILE_MAX * 2];
2504 /* save blend file before using disk pointcache */
2505 if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL) == 0)
2508 ptcache_filename(pid, filename, cfra, 1, 1);
2510 if (!BLI_exists(filename)) {
2514 struct OpenVDBReader *reader = OpenVDBReader_create();
2515 OpenVDBReader_open(reader, filename);
2517 if (!pid->read_openvdb_stream(reader, pid->calldata)) {
2523 UNUSED_VARS(pid, cfra);
2528 static int ptcache_read(PTCacheID *pid, int cfra)
2530 PTCacheMem *pm = NULL;
2534 /* get a memory cache to read from */
2535 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2536 pm = ptcache_disk_frame_to_mem(pid, cfra);
2539 pm = pid->cache->mem_cache.first;
2541 while (pm && pm->frame != cfra)
2545 /* read the cache */
2547 int totpoint = pm->totpoint;
2549 if ((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0) {
2550 int pid_totpoint = pid->totpoint(pid->calldata, cfra);
2552 if (totpoint != pid_totpoint) {
2553 pid->error(pid->calldata, "Number of points in cache does not match mesh");
2554 totpoint = MIN2(totpoint, pid_totpoint);
2558 BKE_ptcache_mem_pointers_init(pm);
2560 for (i=0; i<totpoint; i++) {
2561 if (pm->data_types & (1<<BPHYS_DATA_INDEX))
2562 index = pm->cur[BPHYS_DATA_INDEX];
2564 pid->read_point(*index, pid->calldata, pm->cur, (float)pm->frame, NULL);
2566 BKE_ptcache_mem_pointers_incr(pm);
2569 if (pid->read_extra_data && pm->extradata.first)
2570 pid->read_extra_data(pid->calldata, pm, (float)pm->frame);
2572 /* clean up temporary memory cache */
2573 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2574 ptcache_data_free(pm);
2575 ptcache_extra_free(pm);
2582 static int ptcache_interpolate(PTCacheID *pid, float cfra, int cfra1, int cfra2)
2584 PTCacheMem *pm = NULL;
2588 /* get a memory cache to read from */
2589 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2590 pm = ptcache_disk_frame_to_mem(pid, cfra2);
2593 pm = pid->cache->mem_cache.first;
2595 while (pm && pm->frame != cfra2)
2599 /* read the cache */
2601 int totpoint = pm->totpoint;
2603 if ((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0) {
2604 int pid_totpoint = pid->totpoint(pid->calldata, (int)cfra);
2606 if (totpoint != pid_totpoint) {
2607 pid->error(pid->calldata, "Number of points in cache does not match mesh");
2608 totpoint = MIN2(totpoint, pid_totpoint);
2612 BKE_ptcache_mem_pointers_init(pm);
2614 for (i=0; i<totpoint; i++) {
2615 if (pm->data_types & (1<<BPHYS_DATA_INDEX))
2616 index = pm->cur[BPHYS_DATA_INDEX];
2618 pid->interpolate_point(*index, pid->calldata, pm->cur, cfra, (float)cfra1, (float)cfra2, NULL);
2619 BKE_ptcache_mem_pointers_incr(pm);
2622 if (pid->interpolate_extra_data && pm->extradata.first)
2623 pid->interpolate_extra_data(pid->calldata, pm, cfra, (float)cfra1, (float)cfra2);
2625 /* clean up temporary memory cache */
2626 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2627 ptcache_data_free(pm);
2628 ptcache_extra_free(pm);
2635 /* reads cache from disk or memory */
2636 /* possible to get old or interpolated result */
2637 int BKE_ptcache_read(PTCacheID *pid, float cfra, bool no_extrapolate_old)
2639 int cfrai = (int)floor(cfra), cfra1=0, cfra2=0;
2642 /* nothing to read to */
2643 if (pid->totpoint(pid->calldata, cfrai) == 0)
2646 if (pid->cache->flag & PTCACHE_READ_INFO) {
2647 pid->cache->flag &= ~PTCACHE_READ_INFO;
2648 ptcache_read(pid, 0);
2651 /* first check if we have the actual frame cached */
2652 if (cfra == (float)cfrai && BKE_ptcache_id_exist(pid, cfrai))
2655 /* no exact cache frame found so try to find cached frames around cfra */
2657 ptcache_find_frames_around(pid, cfrai, &cfra1, &cfra2);
2659 if (cfra1 == 0 && cfra2 == 0)
2662 /* don't read old cache if already simulated past cached frame */
2663 if (no_extrapolate_old) {
2664 if (cfra1 == 0 && cfra2 && cfra2 <= pid->cache->simframe)
2666 if (cfra1 && cfra1 == cfra2)
2670 /* avoid calling interpolate between the same frame values */
2671 if (cfra1 && cfra1 == cfra2)
2676 if (pid->file_type == PTCACHE_FILE_OPENVDB && pid->read_openvdb_stream) {
2677 if (!ptcache_read_openvdb_stream(pid, cfra1)) {
2681 else if (pid->read_stream) {
2682 if (!ptcache_read_stream(pid, cfra1))
2685 else if (pid->read_point)
2686 ptcache_read(pid, cfra1);
2690 if (pid->file_type == PTCACHE_FILE_OPENVDB && pid->read_openvdb_stream) {
2691 if (!ptcache_read_openvdb_stream(pid, cfra2)) {
2695 else if (pid->read_stream) {
2696 if (!ptcache_read_stream(pid, cfra2))
2699 else if (pid->read_point) {
2700 if (cfra1 && cfra2 && pid->interpolate_point)
2701 ptcache_interpolate(pid, cfra, cfra1, cfra2);
2703 ptcache_read(pid, cfra2);
2708 ret = (cfra2 ? PTCACHE_READ_INTERPOLATED : PTCACHE_READ_EXACT);
2710 ret = PTCACHE_READ_OLD;
2711 pid->cache->simframe = cfra2;
2715 /* clear invalid cache frames so that better stuff can be simulated */
2716 if (pid->cache->flag & PTCACHE_OUTDATED) {
2717 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, cfrai);
2719 else if (pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
2720 if (cfra <= pid->cache->last_exact)
2721 pid->cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
2723 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, MAX2(cfrai, pid->cache->last_exact));
2728 static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
2730 PTCacheFile *pf = NULL;
2733 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, cfra);
2735 pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, cfra);
2738 if (G.debug & G_DEBUG)
2739 printf("Error opening disk cache file for writing\n");
2743 pf->data_types = pid->data_types;
2744 pf->totpoint = totpoint;
2745 pf->type = pid->type;
2748 if (!error && (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf)))
2751 if (!error && pid->write_stream)
2752 pid->write_stream(pf, pid->calldata);
2754 ptcache_file_close(pf);
2756 if (error && G.debug & G_DEBUG)
2757 printf("Error writing to disk cache\n");
2761 static int ptcache_write_openvdb_stream(PTCacheID *pid, int cfra)
2764 struct OpenVDBWriter *writer = OpenVDBWriter_create();
2765 char filename[FILE_MAX * 2];
2767 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, cfra);
2769 ptcache_filename(pid, filename, cfra, 1, 1);
2770 BLI_make_existing_file(filename);
2772 int error = pid->write_openvdb_stream(writer, pid->calldata);
2774 OpenVDBWriter_write(writer, filename);
2775 OpenVDBWriter_free(writer);
2779 UNUSED_VARS(pid, cfra);
2783 static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
2785 PointCache *cache = pid->cache;
2786 PTCacheMem *pm=NULL, *pm2=NULL;
2787 int totpoint = pid->totpoint(pid->calldata, cfra);
2790 pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
2792 pm->totpoint = pid->totwrite(pid->calldata, cfra);
2793 pm->data_types = cfra ? pid->data_types : pid->info_types;
2795 ptcache_data_alloc(pm);
2796 BKE_ptcache_mem_pointers_init(pm);
2799 if (cache->flag & PTCACHE_DISK_CACHE) {
2802 while (fra >= cache->startframe && !BKE_ptcache_id_exist(pid, fra))
2805 pm2 = ptcache_disk_frame_to_mem(pid, fra);
2808 pm2 = cache->mem_cache.last;
2811 if (pid->write_point) {
2812 for (i=0; i<totpoint; i++) {
2813 int write = pid->write_point(i, pid->calldata, pm->cur, cfra);
2815 BKE_ptcache_mem_pointers_incr(pm);
2817 /* newly born particles have to be copied to previous cached frame */
2818 if (overwrite && write == 2 && pm2 && BKE_ptcache_mem_pointers_seek(i, pm2))
2819 pid->write_point(i, pid->calldata, pm2->cur, cfra);
2824 if (pid->write_extra_data)
2825 pid->write_extra_data(pid->calldata, pm, cfra);
2829 if (cache->flag & PTCACHE_DISK_CACHE) {
2830 error += !ptcache_mem_frame_to_disk(pid, pm);
2832 // if (pm) /* pm is always set */
2834 ptcache_data_free(pm);
2835 ptcache_extra_free(pm);
2840 error += !ptcache_mem_frame_to_disk(pid, pm2);
2841 ptcache_data_free(pm2);
2842 ptcache_extra_free(pm2);
2847 BLI_addtail(&cache->mem_cache, pm);
2852 static int ptcache_write_needed(PTCacheID *pid, int cfra, int *overwrite)
2854 PointCache *cache = pid->cache;
2855 int ofra = 0, efra = cache->endframe;
2857 /* always start from scratch on the first frame */
2858 if (cfra && cfra == cache->startframe) {
2859 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, cfra);
2860 cache->flag &= ~PTCACHE_REDO_NEEDED;
2864 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2865 if (cfra==0 && cache->startframe > 0)
2868 /* find last cached frame */
2869 while (efra > cache->startframe && !BKE_ptcache_id_exist(pid, efra))
2872 /* find second last cached frame */
2874 while (ofra > cache->startframe && !BKE_ptcache_id_exist(pid, ofra))
2878 PTCacheMem *pm = cache->mem_cache.last;
2879 /* don't write info file in memory */
2887 ofra = (pm->prev ? pm->prev->frame : efra - cache->step);
2890 if (efra >= cache->startframe && cfra > efra) {
2891 if (ofra >= cache->startframe && efra - ofra < cache->step) {
2892 /* overwrite previous frame */
2893 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, efra);
2901 /* writes cache to disk or memory */
2902 int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
2904 PointCache *cache = pid->cache;
2905 int totpoint = pid->totpoint(pid->calldata, cfra);
2906 int overwrite = 0, error = 0;
2908 if (totpoint == 0 || (cfra ? pid->data_types == 0 : pid->info_types == 0))
2911 if (ptcache_write_needed(pid, cfra, &overwrite)==0)
2914 if (pid->file_type == PTCACHE_FILE_OPENVDB && pid->write_openvdb_stream) {
2915 ptcache_write_openvdb_stream(pid, cfra);
2917 else if (pid->write_stream) {
2918 ptcache_write_stream(pid, cfra, totpoint);
2920 else if (pid->write_point) {
2921 error += ptcache_write(pid, cfra, overwrite);
2924 /* Mark frames skipped if more than 1 frame forwards since last non-skipped frame. */
2925 if (cfra - cache->last_exact == 1 || cfra == cache->startframe) {
2926 cache->last_exact = cfra;
2927 cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
2929 /* Don't mark skipped when writing info file (frame 0) */
2931 cache->flag |= PTCACHE_FRAMES_SKIPPED;
2933 /* Update timeline cache display */
2934 if (cfra && cache->cached_frames)
2935 cache->cached_frames[cfra-cache->startframe] = 1;
2937 BKE_ptcache_update_info(pid);
2941 /* youll need to close yourself after!
2942 * mode - PTCACHE_CLEAR_ALL,
2945 /* Clears & resets */
2946 void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
2948 unsigned int len; /* store the length of the string */
2949 unsigned int sta, end;
2951 /* mode is same as fopen's modes */
2954 char path[MAX_PTCACHE_PATH];
2955 char filename[MAX_PTCACHE_FILE];
2956 char path_full[MAX_PTCACHE_FILE];
2957 char ext[MAX_PTCACHE_PATH];
2959 if (!pid || !pid->cache || pid->cache->flag & PTCACHE_BAKED)
2962 if (pid->cache->flag & PTCACHE_IGNORE_CLEAR)
2965 sta = pid->cache->startframe;
2966 end = pid->cache->endframe;
2968 #ifndef DURIAN_POINTCACHE_LIB_OK
2969 /* don't allow clearing for linked objects */
2970 if (pid->ob->id.lib)
2974 /*if (!G.relbase_valid) return; *//* save blend file before using pointcache */
2976 const char *fext = ptcache_file_extension(pid);
2978 /* clear all files in the temp dir with the prefix of the ID and the ".bphys" suffix */
2980 case PTCACHE_CLEAR_ALL:
2981 case PTCACHE_CLEAR_BEFORE:
2982 case PTCACHE_CLEAR_AFTER:
2983 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2984 ptcache_path(pid, path);
2986 dir = opendir(path);
2990 len = ptcache_filename(pid, filename, cfra, 0, 0); /* no path */
2991 /* append underscore terminator to ensure we don't match similar names
2992 * from objects whose names start with the same prefix
2994 if (len < sizeof(filename) - 2) {
2995 BLI_strncpy(filename + len, "_", sizeof(filename) - 2 - len);
2999 BLI_snprintf(ext, sizeof(ext), "_%02u%s", pid->stack_index, fext);
3001 while ((de = readdir(dir)) != NULL) {
3002 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
3003 if (STREQLEN(filename, de->d_name, len)) { /* do we have the right prefix */
3004 if (mode == PTCACHE_CLEAR_ALL) {
3005 pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
3006 BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
3007 BLI_delete(path_full, false, false);
3010 /* read the number of the file */
3011 const int frame = ptcache_frame_from_filename(de->d_name, ext);
3014 if ((mode == PTCACHE_CLEAR_BEFORE && frame < cfra) ||
3015 (mode == PTCACHE_CLEAR_AFTER && frame > cfra))
3018 BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
3019 BLI_delete(path_full, false, false);
3020 if (pid->cache->cached_frames && frame >=sta && frame <= end)
3021 pid->cache->cached_frames[frame-sta] = 0;
3030 if (mode == PTCACHE_CLEAR_ALL && pid->cache->cached_frames)
3031 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
3034 PTCacheMem *pm= pid->cache->mem_cache.first;
3035 PTCacheMem *link= NULL;
3037 if (mode == PTCACHE_CLEAR_ALL) {
3038 /*we want startframe if the cache starts before zero*/
3039 pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
3040 for (; pm; pm=pm->next) {
3041 ptcache_data_free(pm);
3042 ptcache_extra_free(pm);
3044 BLI_freelistN(&pid->cache->mem_cache);
3046 if (pid->cache->cached_frames)
3047 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
3051 if ((mode == PTCACHE_CLEAR_BEFORE && pm->frame < cfra) ||
3052 (mode == PTCACHE_CLEAR_AFTER && pm->frame > cfra))
3055 if (pid->cache->cached_frames && pm->frame >=sta && pm->frame <= end)
3056 pid->cache->cached_frames[pm->frame-sta] = 0;
3057 ptcache_data_free(pm);
3058 ptcache_extra_free(pm);
3060 BLI_freelinkN(&pid->cache->mem_cache, link);
3069 case PTCACHE_CLEAR_FRAME:
3070 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
3071 if (BKE_ptcache_id_exist(pid, cfra)) {
3072 ptcache_filename(pid, filename, cfra, 1, 1); /* no path */
3073 BLI_delete(filename, false, false);
3077 PTCacheMem *pm = pid->cache->mem_cache.first;
3079 for (; pm; pm=pm->next) {
3080 if (pm->frame == cfra) {
3081 ptcache_data_free(pm);
3082 ptcache_extra_free(pm);
3083 BLI_freelinkN(&pid->cache->mem_cache, pm);
3088 if (pid->cache->cached_frames && cfra >= sta && cfra <= end)
3089 pid->cache->cached_frames[cfra-sta] = 0;
3093 BKE_ptcache_update_info(pid);
3095 int BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
3100 if (cfra<pid->cache->startframe || cfra > pid->cache->endframe)
3103 if (pid->cache->cached_frames && pid->cache->cached_frames[cfra-pid->cache->startframe]==0)
3106 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
3107 char filename[MAX_PTCACHE_FILE];
3109 ptcache_filename(pid, filename, cfra, 1, 1);
3111 return BLI_exists(filename);
3114 PTCacheMem *pm = pid->cache->mem_cache.first;
3116 for (; pm; pm=pm->next) {
3117 if (pm->frame==cfra)
3123 void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
3125 /* Object *ob; */ /* UNUSED */
3127 /* float offset; unused for now */
3128 float time, nexttime;
3130 /* TODO: this has to be sorted out once bsystem_time gets redone, */
3131 /* now caches can handle interpolating etc. too - jahka */
3133 /* time handling for point cache:
3134 * - simulation time is scaled by result of bsystem_time
3135 * - for offsetting time only time offset is taken into account, since
3136 * that's always the same and can't be animated. a timeoffset which
3137 * varies over time is not simple to support.
3138 * - field and motion blur offsets are currently ignored, proper solution
3139 * is probably to interpolate results from two frames for that ..
3142 /* ob= pid->ob; */ /* UNUSED */
3146 time= BKE_scene_frame_get(scene);
3147 nexttime = BKE_scene_frame_get_from_ctime(scene, CFRA + 1.0f);
3149 *timescale= MAX2(nexttime - time, 0.0f);
3152 if (startframe && endframe) {
3153 *startframe= cache->startframe;
3154 *endframe= cache->endframe;
3156 /* TODO: time handling with object offsets and simulated vs. cached
3157 * particles isn't particularly easy, so for now what you see is what
3158 * you get. In the future point cache could handle the whole particle
3161 if ((ob->partype & PARSLOW)==0) {