4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Campbell Barton <ideasman42@gmail.com>
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/pointcache.c
37 #include <sys/types.h>
39 #include "MEM_guardedalloc.h"
42 #include "DNA_cloth_types.h"
43 #include "DNA_modifier_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_object_force.h"
46 #include "DNA_particle_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_smoke_types.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_threads.h"
53 #include "BLI_utildefines.h"
60 #include "BKE_blender.h"
61 #include "BKE_cloth.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_global.h"
64 #include "BKE_library.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"
72 #include "BKE_utildefines.h"
77 #include "smoke_API.h"
82 /* used for non-lzo cases */
83 #define LZO_OUT_LEN(size) ((size) + (size) / 16 + 64 + 3)
90 /* needed for directory lookup */
91 /* untitled blend's need getpid for a unique name */
97 #include "BLI_winstuff.h"
100 #define PTCACHE_DATA_FROM(data, type, from) if(data[type]) { memcpy(data[type], from, ptcache_data_size[type]); }
101 #define PTCACHE_DATA_TO(data, type, index, to) if(data[type]) { memcpy(to, (char*)data[type] + (index ? index * ptcache_data_size[type] : 0), ptcache_data_size[type]); }
103 /* could be made into a pointcache option */
104 #define DURIAN_POINTCACHE_LIB_OK 1
106 static int ptcache_data_size[] = {
107 sizeof(unsigned int), // BPHYS_DATA_INDEX
108 3 * sizeof(float), // BPHYS_DATA_LOCATION
109 3 * sizeof(float), // BPHYS_DATA_VELOCITY
110 4 * sizeof(float), // BPHYS_DATA_ROTATION
111 3 * sizeof(float), // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST
112 sizeof(float), // BPHYS_DATA_SIZE
113 3 * sizeof(float), // BPHYS_DATA_TIMES
114 sizeof(BoidData) // case BPHYS_DATA_BOIDS
117 static int ptcache_extra_datasize[] = {
119 sizeof(ParticleSpring)
122 /* forward declerations */
123 static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result, unsigned int len);
124 static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, unsigned int in_len, unsigned char *out, int mode);
125 static int ptcache_file_write(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size);
126 static int ptcache_file_read(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size);
128 /* Common functions */
129 static int ptcache_basic_header_read(PTCacheFile *pf)
133 /* Custom functions should read these basic elements too! */
134 if(!error && !fread(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
137 if(!error && !fread(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
142 static int ptcache_basic_header_write(PTCacheFile *pf)
144 /* Custom functions should write these basic elements too! */
145 if(!fwrite(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
148 if(!fwrite(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
153 /* Softbody functions */
154 static int ptcache_softbody_write(int index, void *soft_v, void **data, int UNUSED(cfra))
156 SoftBody *soft= soft_v;
157 BodyPoint *bp = soft->bpoint + index;
159 PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, bp->pos);
160 PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, bp->vec);
164 static void ptcache_softbody_read(int index, void *soft_v, void **data, float UNUSED(cfra), float *old_data)
166 SoftBody *soft= soft_v;
167 BodyPoint *bp = soft->bpoint + index;
170 memcpy(bp->pos, data, 3 * sizeof(float));
171 memcpy(bp->vec, data + 3, 3 * sizeof(float));
174 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, bp->pos);
175 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, 0, bp->vec);
178 static void ptcache_softbody_interpolate(int index, void *soft_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
180 SoftBody *soft= soft_v;
181 BodyPoint *bp = soft->bpoint + index;
188 VECCOPY(keys[1].co, bp->pos);
189 VECCOPY(keys[1].vel, bp->vec);
192 memcpy(keys[2].co, old_data, 3 * sizeof(float));
193 memcpy(keys[2].vel, old_data + 3, 3 * sizeof(float));
196 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
198 dfra = cfra2 - cfra1;
200 mul_v3_fl(keys[1].vel, dfra);
201 mul_v3_fl(keys[2].vel, dfra);
203 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
205 mul_v3_fl(keys->vel, 1.0f / dfra);
207 VECCOPY(bp->pos, keys->co);
208 VECCOPY(bp->vec, keys->vel);
210 static int ptcache_softbody_totpoint(void *soft_v, int UNUSED(cfra))
212 SoftBody *soft= soft_v;
213 return soft->totpoint;
215 /* Particle functions */
216 void BKE_ptcache_make_particle_key(ParticleKey *key, int index, void **data, float time)
218 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, index, key->co);
219 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, index, key->vel);
221 /* no rotation info, so make something nice up */
222 if(data[BPHYS_DATA_ROTATION]==NULL) {
223 vec_to_quat( key->rot, key->vel, OB_NEGX, OB_POSZ);
226 PTCACHE_DATA_TO(data, BPHYS_DATA_ROTATION, index, key->rot);
229 PTCACHE_DATA_TO(data, BPHYS_DATA_AVELOCITY, index, key->ave);
232 static int ptcache_particle_write(int index, void *psys_v, void **data, int cfra)
234 ParticleSystem *psys= psys_v;
235 ParticleData *pa = psys->particles + index;
236 BoidParticle *boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
238 int step = psys->pointcache->step;
240 /* No need to store unborn or died particles outside cache step bounds */
241 if(data[BPHYS_DATA_INDEX] && (cfra < pa->time - step || cfra > pa->dietime + step))
245 times[1]= pa->dietime;
246 times[2]= pa->lifetime;
248 PTCACHE_DATA_FROM(data, BPHYS_DATA_INDEX, &index);
249 PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, pa->state.co);
250 PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, pa->state.vel);
251 PTCACHE_DATA_FROM(data, BPHYS_DATA_ROTATION, pa->state.rot);
252 PTCACHE_DATA_FROM(data, BPHYS_DATA_AVELOCITY, pa->state.ave);
253 PTCACHE_DATA_FROM(data, BPHYS_DATA_SIZE, &pa->size);
254 PTCACHE_DATA_FROM(data, BPHYS_DATA_TIMES, times);
257 PTCACHE_DATA_FROM(data, BPHYS_DATA_BOIDS, &boid->data);
259 /* return flag 1+1=2 for newly born particles to copy exact birth location to previously cached frame */
260 return 1 + (pa->state.time >= pa->time && pa->prev_state.time <= pa->time);
262 static void ptcache_particle_read(int index, void *psys_v, void **data, float cfra, float *old_data)
264 ParticleSystem *psys= psys_v;
267 float timestep = 0.04f*psys->part->timetweak;
269 if(index >= psys->totpart)
272 pa = psys->particles + index;
273 boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
275 if(cfra > pa->state.time)
276 memcpy(&pa->prev_state, &pa->state, sizeof(ParticleKey));
279 /* old format cache */
280 memcpy(&pa->state, old_data, sizeof(ParticleKey));
284 BKE_ptcache_make_particle_key(&pa->state, 0, data, cfra);
286 /* set frames cached before birth to birth time */
288 pa->state.time = pa->time;
289 else if(cfra > pa->dietime)
290 pa->state.time = pa->dietime;
292 if(data[BPHYS_DATA_SIZE])
293 PTCACHE_DATA_TO(data, BPHYS_DATA_SIZE, 0, &pa->size);
295 if(data[BPHYS_DATA_TIMES]) {
297 PTCACHE_DATA_TO(data, BPHYS_DATA_TIMES, 0, ×);
299 pa->dietime = times[1];
300 pa->lifetime = times[2];
304 PTCACHE_DATA_TO(data, BPHYS_DATA_BOIDS, 0, &boid->data);
306 /* determine velocity from previous location */
307 if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
308 if(cfra > pa->prev_state.time) {
309 sub_v3_v3v3(pa->state.vel, pa->state.co, pa->prev_state.co);
310 mul_v3_fl(pa->state.vel, (cfra - pa->prev_state.time) * timestep);
313 sub_v3_v3v3(pa->state.vel, pa->prev_state.co, pa->state.co);
314 mul_v3_fl(pa->state.vel, (pa->prev_state.time - cfra) * timestep);
318 /* determine rotation from velocity */
319 if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
320 vec_to_quat( pa->state.rot,pa->state.vel, OB_NEGX, OB_POSZ);
323 static void ptcache_particle_interpolate(int index, void *psys_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
325 ParticleSystem *psys= psys_v;
328 float dfra, timestep = 0.04f*psys->part->timetweak;
330 if(index >= psys->totpart)
333 pa = psys->particles + index;
335 /* particle wasn't read from first cache so can't interpolate */
336 if((int)cfra1 < pa->time - psys->pointcache->step || (int)cfra1 > pa->dietime + psys->pointcache->step)
339 cfra = MIN2(cfra, pa->dietime);
340 cfra1 = MIN2(cfra1, pa->dietime);
341 cfra2 = MIN2(cfra2, pa->dietime);
346 memcpy(keys+1, &pa->state, sizeof(ParticleKey));
348 memcpy(keys+2, old_data, sizeof(ParticleKey));
350 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
352 /* determine velocity from previous location */
353 if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
354 if(keys[1].time > keys[2].time) {
355 sub_v3_v3v3(keys[2].vel, keys[1].co, keys[2].co);
356 mul_v3_fl(keys[2].vel, (keys[1].time - keys[2].time) * timestep);
359 sub_v3_v3v3(keys[2].vel, keys[2].co, keys[1].co);
360 mul_v3_fl(keys[2].vel, (keys[2].time - keys[1].time) * timestep);
364 /* determine rotation from velocity */
365 if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
366 vec_to_quat( keys[2].rot,keys[2].vel, OB_NEGX, OB_POSZ);
370 cfra1 = MAX2(cfra1, pa->time);
372 dfra = cfra2 - cfra1;
374 mul_v3_fl(keys[1].vel, dfra * timestep);
375 mul_v3_fl(keys[2].vel, dfra * timestep);
377 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, &pa->state, 1);
378 interp_qt_qtqt(pa->state.rot, keys[1].rot, keys[2].rot, (cfra - cfra1) / dfra);
380 mul_v3_fl(pa->state.vel, 1.f / (dfra * timestep));
382 pa->state.time = cfra;
385 static int ptcache_particle_totpoint(void *psys_v, int UNUSED(cfra))
387 ParticleSystem *psys = psys_v;
388 return psys->totpart;
390 static int ptcache_particle_totwrite(void *psys_v, int cfra)
392 ParticleSystem *psys = psys_v;
393 ParticleData *pa= psys->particles;
394 int p, step = psys->pointcache->step;
398 return psys->totpart;
400 for(p=0; p<psys->totpart; p++,pa++)
401 totwrite += (cfra >= pa->time - step && cfra <= pa->dietime + step);
406 static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int UNUSED(cfra))
408 ParticleSystem *psys = psys_v;
409 PTCacheExtra *extra = NULL;
411 if(psys->part->phystype == PART_PHYS_FLUID &&
412 psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS &&
413 psys->tot_fluidsprings && psys->fluid_springs) {
415 extra = MEM_callocN(sizeof(PTCacheExtra), "Point cache: fluid extra data");
417 extra->type = BPHYS_EXTRA_FLUID_SPRINGS;
418 extra->totdata = psys->tot_fluidsprings;
420 extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Point cache: extra data");
421 memcpy(extra->data, psys->fluid_springs, extra->totdata * ptcache_extra_datasize[extra->type]);
423 BLI_addtail(&pm->extradata, extra);
427 static void ptcache_particle_extra_read(void *psys_v, PTCacheMem *pm, float UNUSED(cfra))
429 ParticleSystem *psys = psys_v;
430 PTCacheExtra *extra = pm->extradata.first;
432 for(; extra; extra=extra->next) {
433 switch(extra->type) {
434 case BPHYS_EXTRA_FLUID_SPRINGS:
436 if(psys->fluid_springs)
437 MEM_freeN(psys->fluid_springs);
439 psys->fluid_springs = MEM_dupallocN(extra->data);
440 psys->tot_fluidsprings = psys->alloc_fluidsprings = extra->totdata;
447 /* Cloth functions */
448 static int ptcache_cloth_write(int index, void *cloth_v, void **data, int UNUSED(cfra))
450 ClothModifierData *clmd= cloth_v;
451 Cloth *cloth= clmd->clothObject;
452 ClothVertex *vert = cloth->verts + index;
454 PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, vert->x);
455 PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, vert->v);
456 PTCACHE_DATA_FROM(data, BPHYS_DATA_XCONST, vert->xconst);
460 static void ptcache_cloth_read(int index, void *cloth_v, void **data, float UNUSED(cfra), float *old_data)
462 ClothModifierData *clmd= cloth_v;
463 Cloth *cloth= clmd->clothObject;
464 ClothVertex *vert = cloth->verts + index;
467 memcpy(vert->x, data, 3 * sizeof(float));
468 memcpy(vert->xconst, data + 3, 3 * sizeof(float));
469 memcpy(vert->v, data + 6, 3 * sizeof(float));
472 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, vert->x);
473 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, 0, vert->v);
474 PTCACHE_DATA_TO(data, BPHYS_DATA_XCONST, 0, vert->xconst);
477 static void ptcache_cloth_interpolate(int index, void *cloth_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
479 ClothModifierData *clmd= cloth_v;
480 Cloth *cloth= clmd->clothObject;
481 ClothVertex *vert = cloth->verts + index;
488 VECCOPY(keys[1].co, vert->x);
489 VECCOPY(keys[1].vel, vert->v);
492 memcpy(keys[2].co, old_data, 3 * sizeof(float));
493 memcpy(keys[2].vel, old_data + 6, 3 * sizeof(float));
496 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
498 dfra = cfra2 - cfra1;
500 mul_v3_fl(keys[1].vel, dfra);
501 mul_v3_fl(keys[2].vel, dfra);
503 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
505 mul_v3_fl(keys->vel, 1.0f / dfra);
507 VECCOPY(vert->x, keys->co);
508 VECCOPY(vert->v, keys->vel);
510 /* should vert->xconst be interpolated somehow too? - jahka */
513 static int ptcache_cloth_totpoint(void *cloth_v, int UNUSED(cfra))
515 ClothModifierData *clmd= cloth_v;
516 return clmd->clothObject ? clmd->clothObject->numverts : 0;
520 /* Smoke functions */
521 static int ptcache_smoke_totpoint(void *smoke_v, int UNUSED(cfra))
523 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
524 SmokeDomainSettings *sds = smd->domain;
527 return sds->res[0]*sds->res[1]*sds->res[2];
532 static int ptcache_smoke_write(PTCacheFile *pf, void *smoke_v)
534 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
535 SmokeDomainSettings *sds = smd->domain;
539 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
540 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
541 unsigned char *obstacles;
542 unsigned int in_len = sizeof(float)*(unsigned int)res;
543 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
544 //int mode = res >= 1000000 ? 2 : 1;
546 if (sds->cache_comp == SM_CACHE_HEAVY) mode=2; // heavy
548 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
550 ptcache_file_compressed_write(pf, (unsigned char *)sds->shadow, in_len, out, mode);
551 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len, out, mode);
552 ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len, out, mode);
553 ptcache_file_compressed_write(pf, (unsigned char *)heat, in_len, out, mode);
554 ptcache_file_compressed_write(pf, (unsigned char *)heatold, in_len, out, mode);
555 ptcache_file_compressed_write(pf, (unsigned char *)vx, in_len, out, mode);
556 ptcache_file_compressed_write(pf, (unsigned char *)vy, in_len, out, mode);
557 ptcache_file_compressed_write(pf, (unsigned char *)vz, in_len, out, mode);
558 ptcache_file_compressed_write(pf, (unsigned char *)vxold, in_len, out, mode);
559 ptcache_file_compressed_write(pf, (unsigned char *)vyold, in_len, out, mode);
560 ptcache_file_compressed_write(pf, (unsigned char *)vzold, in_len, out, mode);
561 ptcache_file_compressed_write(pf, (unsigned char *)obstacles, (unsigned int)res, out, mode);
562 ptcache_file_write(pf, &dt, 1, sizeof(float));
563 ptcache_file_write(pf, &dx, 1, sizeof(float));
571 int res_big_array[3];
573 int res = sds->res[0]*sds->res[1]*sds->res[2];
574 float *dens, *densold, *tcu, *tcv, *tcw;
575 unsigned int in_len = sizeof(float)*(unsigned int)res;
576 unsigned int in_len_big;
580 smoke_turbulence_get_res(sds->wt, res_big_array);
581 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
582 //mode = res_big >= 1000000 ? 2 : 1;
584 if (sds->cache_high_comp == SM_CACHE_HEAVY) mode=2; // heavy
586 in_len_big = sizeof(float) * (unsigned int)res_big;
588 smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
590 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len_big), "pointcache_lzo_buffer");
591 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len_big, out, mode);
592 ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len_big, out, mode);
595 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
596 ptcache_file_compressed_write(pf, (unsigned char *)tcu, in_len, out, mode);
597 ptcache_file_compressed_write(pf, (unsigned char *)tcv, in_len, out, mode);
598 ptcache_file_compressed_write(pf, (unsigned char *)tcw, in_len, out, mode);
606 static void ptcache_smoke_read(PTCacheFile *pf, void *smoke_v)
608 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
609 SmokeDomainSettings *sds = smd->domain;
612 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
613 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
614 unsigned char *obstacles;
615 unsigned int out_len = (unsigned int)res * sizeof(float);
617 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
619 ptcache_file_compressed_read(pf, (unsigned char *)sds->shadow, out_len);
620 ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len);
621 ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len);
622 ptcache_file_compressed_read(pf, (unsigned char*)heat, out_len);
623 ptcache_file_compressed_read(pf, (unsigned char*)heatold, out_len);
624 ptcache_file_compressed_read(pf, (unsigned char*)vx, out_len);
625 ptcache_file_compressed_read(pf, (unsigned char*)vy, out_len);
626 ptcache_file_compressed_read(pf, (unsigned char*)vz, out_len);
627 ptcache_file_compressed_read(pf, (unsigned char*)vxold, out_len);
628 ptcache_file_compressed_read(pf, (unsigned char*)vyold, out_len);
629 ptcache_file_compressed_read(pf, (unsigned char*)vzold, out_len);
630 ptcache_file_compressed_read(pf, (unsigned char*)obstacles, (unsigned int)res);
631 ptcache_file_read(pf, &dt, 1, sizeof(float));
632 ptcache_file_read(pf, &dx, 1, sizeof(float));
634 if(pf->data_types & (1<<BPHYS_DATA_SMOKE_HIGH) && sds->wt) {
635 int res = sds->res[0]*sds->res[1]*sds->res[2];
636 int res_big, res_big_array[3];
637 float *dens, *densold, *tcu, *tcv, *tcw;
638 unsigned int out_len = sizeof(float)*(unsigned int)res;
639 unsigned int out_len_big;
641 smoke_turbulence_get_res(sds->wt, res_big_array);
642 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
643 out_len_big = sizeof(float) * (unsigned int)res_big;
645 smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
647 ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len_big);
648 ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len_big);
650 ptcache_file_compressed_read(pf, (unsigned char*)tcu, out_len);
651 ptcache_file_compressed_read(pf, (unsigned char*)tcv, out_len);
652 ptcache_file_compressed_read(pf, (unsigned char*)tcw, out_len);
657 static int ptcache_smoke_totpoint(void *UNUSED(smoke_v), int UNUSED(cfra)) { return 0; };
658 static void ptcache_smoke_read(PTCacheFile *UNUSED(pf), void *UNUSED(smoke_v)) {}
659 static int ptcache_smoke_write(PTCacheFile *UNUSED(pf), void *UNUSED(smoke_v)) { return 0; }
663 void BKE_ptcache_id_from_softbody(PTCacheID *pid, Object *ob, SoftBody *sb)
665 memset(pid, 0, sizeof(PTCacheID));
669 pid->type= PTCACHE_TYPE_SOFTBODY;
670 pid->cache= sb->pointcache;
671 pid->cache_ptr= &sb->pointcache;
672 pid->ptcaches= &sb->ptcaches;
673 pid->totpoint= pid->totwrite= ptcache_softbody_totpoint;
675 pid->write_point = ptcache_softbody_write;
676 pid->read_point = ptcache_softbody_read;
677 pid->interpolate_point = ptcache_softbody_interpolate;
679 pid->write_stream = NULL;
680 pid->read_stream = NULL;
682 pid->write_extra_data = NULL;
683 pid->read_extra_data = NULL;
684 pid->interpolate_extra_data = NULL;
686 pid->write_header = ptcache_basic_header_write;
687 pid->read_header = ptcache_basic_header_read;
689 pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY);
692 pid->stack_index = pid->cache->index;
694 void BKE_ptcache_id_from_particles(PTCacheID *pid, Object *ob, ParticleSystem *psys)
696 memset(pid, 0, sizeof(PTCacheID));
700 pid->type= PTCACHE_TYPE_PARTICLES;
701 pid->stack_index= psys->pointcache->index;
702 pid->cache= psys->pointcache;
703 pid->cache_ptr= &psys->pointcache;
704 pid->ptcaches= &psys->ptcaches;
706 if(psys->part->type != PART_HAIR)
707 pid->flag |= PTCACHE_VEL_PER_SEC;
709 pid->totpoint = ptcache_particle_totpoint;
710 pid->totwrite = ptcache_particle_totwrite;
712 pid->write_point = ptcache_particle_write;
713 pid->read_point = ptcache_particle_read;
714 pid->interpolate_point = ptcache_particle_interpolate;
716 pid->write_stream = NULL;
717 pid->read_stream = NULL;
719 pid->write_extra_data = NULL;
720 pid->read_extra_data = NULL;
721 pid->interpolate_extra_data = NULL;
723 pid->write_header = ptcache_basic_header_write;
724 pid->read_header = ptcache_basic_header_read;
726 pid->data_types = (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_INDEX);
728 if(psys->part->phystype == PART_PHYS_BOIDS)
729 pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION) | (1<<BPHYS_DATA_BOIDS);
730 else if(psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS) {
731 pid->write_extra_data = ptcache_particle_extra_write;
732 pid->read_extra_data = ptcache_particle_extra_read;
735 if(psys->part->rotmode!=PART_ROT_VEL
736 || psys->part->avemode!=PART_AVE_SPIN || psys->part->avefac!=0.0f)
737 pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION);
739 if(psys->part->flag & PART_ROT_DYN)
740 pid->data_types|= (1<<BPHYS_DATA_ROTATION);
742 pid->info_types= (1<<BPHYS_DATA_TIMES);
744 void BKE_ptcache_id_from_cloth(PTCacheID *pid, Object *ob, ClothModifierData *clmd)
746 memset(pid, 0, sizeof(PTCacheID));
750 pid->type= PTCACHE_TYPE_CLOTH;
751 pid->stack_index= clmd->point_cache->index;
752 pid->cache= clmd->point_cache;
753 pid->cache_ptr= &clmd->point_cache;
754 pid->ptcaches= &clmd->ptcaches;
755 pid->totpoint= pid->totwrite= ptcache_cloth_totpoint;
757 pid->write_point = ptcache_cloth_write;
758 pid->read_point = ptcache_cloth_read;
759 pid->interpolate_point = ptcache_cloth_interpolate;
761 pid->write_stream = NULL;
762 pid->read_stream = NULL;
764 pid->write_extra_data = NULL;
765 pid->read_extra_data = NULL;
766 pid->interpolate_extra_data = NULL;
768 pid->write_header = ptcache_basic_header_write;
769 pid->read_header = ptcache_basic_header_read;
771 pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_XCONST);
774 void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct SmokeModifierData *smd)
776 SmokeDomainSettings *sds = smd->domain;
778 memset(pid, 0, sizeof(PTCacheID));
783 pid->type= PTCACHE_TYPE_SMOKE_DOMAIN;
784 pid->stack_index= sds->point_cache[0]->index;
786 pid->cache= sds->point_cache[0];
787 pid->cache_ptr= &(sds->point_cache[0]);
788 pid->ptcaches= &(sds->ptcaches[0]);
790 pid->totpoint= pid->totwrite= ptcache_smoke_totpoint;
792 pid->write_point = NULL;
793 pid->read_point = NULL;
794 pid->interpolate_point = NULL;
796 pid->read_stream = ptcache_smoke_read;
797 pid->write_stream = ptcache_smoke_write;
799 pid->write_extra_data = NULL;
800 pid->read_extra_data = NULL;
801 pid->interpolate_extra_data = NULL;
803 pid->write_header = ptcache_basic_header_write;
804 pid->read_header = ptcache_basic_header_read;
810 pid->data_types |= (1<<BPHYS_DATA_SMOKE_LOW);
812 pid->data_types |= (1<<BPHYS_DATA_SMOKE_HIGH);
814 void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int duplis)
817 ParticleSystem *psys;
820 lb->first= lb->last= NULL;
823 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
824 BKE_ptcache_id_from_softbody(pid, ob, ob->soft);
825 BLI_addtail(lb, pid);
828 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
832 /* check to make sure point cache is actually used by the particles */
833 if(ELEM(psys->part->phystype, PART_PHYS_NO, PART_PHYS_KEYED))
836 /* hair needs to be included in id-list for cache edit mode to work */
837 /* if(psys->part->type == PART_HAIR && (psys->flag & PSYS_HAIR_DYNAMICS)==0) */
840 if(psys->part->type == PART_FLUID)
843 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
844 BKE_ptcache_id_from_particles(pid, ob, psys);
845 BLI_addtail(lb, pid);
848 for(md=ob->modifiers.first; md; md=md->next) {
849 if(md->type == eModifierType_Cloth) {
850 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
851 BKE_ptcache_id_from_cloth(pid, ob, (ClothModifierData*)md);
852 BLI_addtail(lb, pid);
854 if(md->type == eModifierType_Smoke) {
855 SmokeModifierData *smd = (SmokeModifierData *)md;
856 if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
858 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
859 BKE_ptcache_id_from_smoke(pid, ob, (SmokeModifierData*)md);
860 BLI_addtail(lb, pid);
865 if(scene && (duplis-- > 0) && (ob->transflag & OB_DUPLI)) {
866 ListBase *lb_dupli_ob;
868 if((lb_dupli_ob=object_duplilist(scene, ob))) {
870 for(dob= lb_dupli_ob->first; dob; dob= dob->next) {
871 if(dob->ob != ob) { /* avoids recursive loops with dupliframes: bug 22988 */
872 ListBase lb_dupli_pid;
873 BKE_ptcache_ids_from_object(&lb_dupli_pid, dob->ob, scene, duplis);
874 BLI_movelisttolist(lb, &lb_dupli_pid);
875 if(lb_dupli_pid.first)
876 printf("Adding Dupli\n");
880 free_object_duplilist(lb_dupli_ob); /* does restore */
887 /* Takes an Object ID and returns a unique name
889 - cfra: frame for the cache, can be negative
890 - stack_index: index in the modifier stack. we can have cache for more then one stack_index
893 #define MAX_PTCACHE_PATH FILE_MAX
894 #define MAX_PTCACHE_FILE ((FILE_MAXDIR+FILE_MAXFILE)*2)
896 static int ptcache_path(PTCacheID *pid, char *filename)
898 Library *lib= (pid->ob)? pid->ob->id.lib: NULL;
899 const char *blendfilename= (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH)==0) ? lib->filepath: G.main->name;
902 if(pid->cache->flag & PTCACHE_EXTERNAL) {
903 strcpy(filename, pid->cache->path);
905 if(strncmp(filename, "//", 2)==0)
906 BLI_path_abs(filename, blendfilename);
908 return BLI_add_slash(filename); /* new strlen() */
910 else if (G.relbase_valid || lib) {
911 char file[MAX_PTCACHE_PATH]; /* we dont want the dir, only the file */
913 BLI_split_file_part(blendfilename, file, sizeof(file));
920 BLI_snprintf(filename, MAX_PTCACHE_PATH, "//"PTCACHE_PATH"%s", file); /* add blend file name to pointcache dir */
921 BLI_path_abs(filename, blendfilename);
922 return BLI_add_slash(filename); /* new strlen() */
925 /* use the temp path. this is weak but better then not using point cache at all */
926 /* temporary directory is assumed to exist and ALWAYS has a trailing slash */
927 BLI_snprintf(filename, MAX_PTCACHE_PATH, "%s"PTCACHE_PATH"%d", BLI_temporary_dir(), abs(getpid()));
929 return BLI_add_slash(filename); /* new strlen() */
932 static int ptcache_filename(PTCacheID *pid, char *filename, int cfra, short do_path, short do_ext)
940 if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return 0; /* save blend file before using disk pointcache */
942 /* start with temp dir */
944 len = ptcache_path(pid, filename);
947 if(pid->cache->name[0] == '\0' && (pid->cache->flag & PTCACHE_EXTERNAL)==0) {
948 idname = (pid->ob->id.name+2);
949 /* convert chars to hex so they are always a valid filename */
950 while('\0' != *idname) {
951 BLI_snprintf(newname, MAX_PTCACHE_FILE, "%02X", (char)(*idname++));
957 int temp = (int)strlen(pid->cache->name);
958 strcpy(newname, pid->cache->name);
965 if(pid->cache->index < 0)
966 pid->cache->index = pid->stack_index = object_insert_ptcache(pid->ob);
968 if(pid->cache->flag & PTCACHE_EXTERNAL) {
969 if(pid->cache->index >= 0)
970 BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02u"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
972 BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d"PTCACHE_EXT, cfra); /* always 6 chars */
975 BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02u"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
980 return len; /* make sure the above string is always 16 chars */
983 /* youll need to close yourself after! */
984 static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
988 char filename[(FILE_MAXDIR+FILE_MAXFILE)*2];
990 #ifndef DURIAN_POINTCACHE_LIB_OK
991 /* don't allow writing for linked objects */
992 if(pid->ob->id.lib && mode == PTCACHE_FILE_WRITE)
995 if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return NULL; /* save blend file before using disk pointcache */
997 ptcache_filename(pid, filename, cfra, 1, 1);
999 if (mode==PTCACHE_FILE_READ) {
1000 if (!BLI_exists(filename)) {
1003 fp = fopen(filename, "rb");
1004 } else if (mode==PTCACHE_FILE_WRITE) {
1005 BLI_make_existing_file(filename); /* will create the dir if needs be, same as //textures is created */
1006 fp = fopen(filename, "wb");
1007 } else if (mode==PTCACHE_FILE_UPDATE) {
1008 BLI_make_existing_file(filename);
1009 fp = fopen(filename, "rb+");
1015 pf= MEM_mallocN(sizeof(PTCacheFile), "PTCacheFile");
1022 static void ptcache_file_close(PTCacheFile *pf)
1030 static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result, unsigned int len)
1033 unsigned char compressed = 0;
1036 size_t out_len = len;
1039 unsigned char *props = MEM_callocN(16*sizeof(char), "tmp");
1041 ptcache_file_read(pf, &compressed, 1, sizeof(unsigned char));
1044 ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
1045 in_len = (size_t)size;
1050 in = (unsigned char *)MEM_callocN(sizeof(unsigned char)*in_len, "pointcache_compressed_buffer");
1051 ptcache_file_read(pf, in, in_len, sizeof(unsigned char));
1054 r = lzo1x_decompress_safe(in, (lzo_uint)in_len, result, (lzo_uint *)&out_len, NULL);
1060 size_t leni = in_len, leno = out_len;
1061 ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
1062 sizeOfIt = (size_t)size;
1063 ptcache_file_read(pf, props, sizeOfIt, sizeof(unsigned char));
1064 r = LzmaUncompress(result, &leno, in, &leni, props, sizeOfIt);
1071 ptcache_file_read(pf, result, len, sizeof(unsigned char));
1078 static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, unsigned int in_len, unsigned char *out, int mode)
1081 unsigned char compressed = 0;
1083 unsigned char *props = MEM_callocN(16*sizeof(char), "tmp");
1084 size_t sizeOfIt = 5;
1086 (void)mode; /* unused when building w/o compression */
1089 out_len= LZO_OUT_LEN(in_len);
1091 LZO_HEAP_ALLOC(wrkmem, LZO1X_MEM_COMPRESS);
1093 r = lzo1x_1_compress(in, (lzo_uint)in_len, out, (lzo_uint *)&out_len, wrkmem);
1094 if (!(r == LZO_E_OK) || (out_len >= in_len))
1103 r = LzmaCompress(out, &out_len, in, in_len,//assume sizeof(char)==1....
1104 props, &sizeOfIt, 5, 1 << 24, 3, 0, 2, 32, 2);
1106 if(!(r == SZ_OK) || (out_len >= in_len))
1113 ptcache_file_write(pf, &compressed, 1, sizeof(unsigned char));
1115 unsigned int size = out_len;
1116 ptcache_file_write(pf, &size, 1, sizeof(unsigned int));
1117 ptcache_file_write(pf, out, out_len, sizeof(unsigned char));
1120 ptcache_file_write(pf, in, in_len, sizeof(unsigned char));
1124 unsigned int size = sizeOfIt;
1125 ptcache_file_write(pf, &sizeOfIt, 1, sizeof(unsigned int));
1126 ptcache_file_write(pf, props, size, sizeof(unsigned char));
1133 static int ptcache_file_read(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size)
1135 return (fread(f, size, tot, pf->fp) == tot);
1137 static int ptcache_file_write(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size)
1139 return (fwrite(f, size, tot, pf->fp) == tot);
1141 static int ptcache_file_data_read(PTCacheFile *pf)
1145 for(i=0; i<BPHYS_TOT_DATA; i++) {
1146 if((pf->data_types & (1<<i)) && !ptcache_file_read(pf, pf->cur[i], 1, ptcache_data_size[i]))
1152 static int ptcache_file_data_write(PTCacheFile *pf)
1156 for(i=0; i<BPHYS_TOT_DATA; i++) {
1157 if((pf->data_types & (1<<i)) && !ptcache_file_write(pf, pf->cur[i], 1, ptcache_data_size[i]))
1163 static int ptcache_file_header_begin_read(PTCacheFile *pf)
1165 unsigned int typeflag=0;
1171 if(fread(bphysics, sizeof(char), 8, pf->fp) != 8)
1174 if(!error && strncmp(bphysics, "BPHYSICS", 8))
1177 if(!error && !fread(&typeflag, sizeof(unsigned int), 1, pf->fp))
1180 pf->type = (typeflag & PTCACHE_TYPEFLAG_TYPEMASK);
1181 pf->flag = (typeflag & PTCACHE_TYPEFLAG_FLAGMASK);
1183 /* if there was an error set file as it was */
1185 fseek(pf->fp, 0, SEEK_SET);
1189 static int ptcache_file_header_begin_write(PTCacheFile *pf)
1191 const char *bphysics = "BPHYSICS";
1192 unsigned int typeflag = pf->type + pf->flag;
1194 if(fwrite(bphysics, sizeof(char), 8, pf->fp) != 8)
1197 if(!fwrite(&typeflag, sizeof(unsigned int), 1, pf->fp))
1203 /* Data pointer handling */
1204 int BKE_ptcache_data_size(int data_type)
1206 return ptcache_data_size[data_type];
1209 static void ptcache_file_pointers_init(PTCacheFile *pf)
1211 int data_types = pf->data_types;
1213 pf->cur[BPHYS_DATA_INDEX] = (data_types & (1<<BPHYS_DATA_INDEX)) ? &pf->data.index : NULL;
1214 pf->cur[BPHYS_DATA_LOCATION] = (data_types & (1<<BPHYS_DATA_LOCATION)) ? &pf->data.loc : NULL;
1215 pf->cur[BPHYS_DATA_VELOCITY] = (data_types & (1<<BPHYS_DATA_VELOCITY)) ? &pf->data.vel : NULL;
1216 pf->cur[BPHYS_DATA_ROTATION] = (data_types & (1<<BPHYS_DATA_ROTATION)) ? &pf->data.rot : NULL;
1217 pf->cur[BPHYS_DATA_AVELOCITY] = (data_types & (1<<BPHYS_DATA_AVELOCITY))? &pf->data.ave : NULL;
1218 pf->cur[BPHYS_DATA_SIZE] = (data_types & (1<<BPHYS_DATA_SIZE)) ? &pf->data.size : NULL;
1219 pf->cur[BPHYS_DATA_TIMES] = (data_types & (1<<BPHYS_DATA_TIMES)) ? &pf->data.times : NULL;
1220 pf->cur[BPHYS_DATA_BOIDS] = (data_types & (1<<BPHYS_DATA_BOIDS)) ? &pf->data.boids : NULL;
1223 /* Check to see if point number "index" is in pm, uses binary search for index data. */
1224 int BKE_ptcache_mem_index_find(PTCacheMem *pm, unsigned int index)
1226 if(pm->data[BPHYS_DATA_INDEX]) {
1227 unsigned int *data = pm->data[BPHYS_DATA_INDEX];
1228 unsigned int mid, low = 0, high = pm->totpoint - 1;
1230 if(index < *data || index > *(data+high))
1233 /* check simple case for continuous indexes first */
1234 if(index-*data < high && data[index-*data] == index)
1237 while(low <= high) {
1238 mid= (low + high)/2;
1240 if(data[mid] > index)
1242 else if(data[mid] < index)
1251 return (index < pm->totpoint ? index : -1);
1255 void BKE_ptcache_mem_pointers_init(PTCacheMem *pm)
1257 int data_types = pm->data_types;
1260 for(i=0; i<BPHYS_TOT_DATA; i++)
1261 pm->cur[i] = ((data_types & (1<<i)) ? pm->data[i] : NULL);
1264 void BKE_ptcache_mem_pointers_incr(PTCacheMem *pm)
1268 for(i=0; i<BPHYS_TOT_DATA; i++) {
1270 pm->cur[i] = (char*)pm->cur[i] + ptcache_data_size[i];
1273 int BKE_ptcache_mem_pointers_seek(int point_index, PTCacheMem *pm)
1275 int data_types = pm->data_types;
1276 int i, index = BKE_ptcache_mem_index_find(pm, point_index);
1279 /* Can't give proper location without reallocation, so don't give any location.
1280 * Some points will be cached improperly, but this only happens with simulation
1281 * steps bigger than cache->step, so the cache has to be recalculated anyways
1287 for(i=0; i<BPHYS_TOT_DATA; i++)
1288 pm->cur[i] = data_types & (1<<i) ? (char*)pm->data[i] + index * ptcache_data_size[i] : NULL;
1292 static void ptcache_data_alloc(PTCacheMem *pm)
1294 int data_types = pm->data_types;
1295 int totpoint = pm->totpoint;
1298 for(i=0; i<BPHYS_TOT_DATA; i++) {
1299 if(data_types & (1<<i))
1300 pm->data[i] = MEM_callocN(totpoint * ptcache_data_size[i], "PTCache Data");
1303 static void ptcache_data_free(PTCacheMem *pm)
1305 void **data = pm->data;
1308 for(i=0; i<BPHYS_TOT_DATA; i++) {
1313 static void ptcache_data_copy(void *from[], void *to[])
1316 for(i=0; i<BPHYS_TOT_DATA; i++) {
1317 /* note, durian file 03.4b_comp crashes if to[i] is not tested
1318 * its NULL, not sure if this should be fixed elsewhere but for now its needed */
1319 if(from[i] && to[i])
1320 memcpy(to[i], from[i], ptcache_data_size[i]);
1324 static void ptcache_extra_free(PTCacheMem *pm)
1326 PTCacheExtra *extra = pm->extradata.first;
1329 for(; extra; extra=extra->next) {
1331 MEM_freeN(extra->data);
1334 BLI_freelistN(&pm->extradata);
1337 static int ptcache_old_elemsize(PTCacheID *pid)
1339 if(pid->type==PTCACHE_TYPE_SOFTBODY)
1340 return 6 * sizeof(float);
1341 else if(pid->type==PTCACHE_TYPE_PARTICLES)
1342 return sizeof(ParticleKey);
1343 else if(pid->type==PTCACHE_TYPE_CLOTH)
1344 return 9 * sizeof(float);
1349 static void ptcache_find_frames_around(PTCacheID *pid, unsigned int frame, int *fra1, int *fra2)
1351 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1352 int cfra1=frame-1, cfra2=frame+1;
1354 while(cfra1 >= pid->cache->startframe && !BKE_ptcache_id_exist(pid, cfra1))
1357 if(cfra1 < pid->cache->startframe)
1360 while(cfra2 <= pid->cache->endframe && !BKE_ptcache_id_exist(pid, cfra2))
1363 if(cfra2 > pid->cache->endframe)
1366 if(cfra1 && !cfra2) {
1375 else if(pid->cache->mem_cache.first) {
1376 PTCacheMem *pm = pid->cache->mem_cache.first;
1377 PTCacheMem *pm2 = pid->cache->mem_cache.last;
1379 while(pm->next && pm->next->frame < frame)
1382 if(pm2->frame < frame) {
1386 while(pm2->prev && pm2->prev->frame > frame) {
1402 static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
1404 PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
1405 PTCacheMem *pm = NULL;
1406 unsigned int i, error = 0;
1411 if(!ptcache_file_header_begin_read(pf))
1414 if(!error && (pf->type != pid->type || !pid->read_header(pf)))
1418 pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
1420 pm->totpoint = pf->totpoint;
1421 pm->data_types = pf->data_types;
1422 pm->frame = pf->frame;
1424 ptcache_data_alloc(pm);
1426 if(pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
1427 for(i=0; i<BPHYS_TOT_DATA; i++) {
1428 unsigned int out_len = pm->totpoint*ptcache_data_size[i];
1429 if(pf->data_types & (1<<i))
1430 ptcache_file_compressed_read(pf, (unsigned char*)(pm->data[i]), out_len);
1434 BKE_ptcache_mem_pointers_init(pm);
1435 ptcache_file_pointers_init(pf);
1437 for(i=0; i<pm->totpoint; i++) {
1438 if(!ptcache_file_data_read(pf)) {
1442 ptcache_data_copy(pf->cur, pm->cur);
1443 BKE_ptcache_mem_pointers_incr(pm);
1448 if(!error && pf->flag & PTCACHE_TYPEFLAG_EXTRADATA) {
1449 unsigned int extratype = 0;
1451 while(ptcache_file_read(pf, &extratype, 1, sizeof(unsigned int))) {
1452 PTCacheExtra *extra = MEM_callocN(sizeof(PTCacheExtra), "Pointcache extradata");
1454 extra->type = extratype;
1456 ptcache_file_read(pf, &extra->totdata, 1, sizeof(unsigned int));
1458 extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Pointcache extradata->data");
1460 if(pf->flag & PTCACHE_TYPEFLAG_COMPRESS)
1461 ptcache_file_compressed_read(pf, (unsigned char*)(extra->data), extra->totdata*ptcache_extra_datasize[extra->type]);
1463 ptcache_file_read(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
1465 BLI_addtail(&pm->extradata, extra);
1470 ptcache_data_free(pm);
1471 ptcache_extra_free(pm);
1476 ptcache_file_close(pf);
1478 if (error && G.f & G_DEBUG)
1479 printf("Error reading from disk cache\n");
1483 static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
1485 PTCacheFile *pf = NULL;
1486 unsigned int i, error = 0;
1488 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, pm->frame);
1490 pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, pm->frame);
1494 printf("Error opening disk cache file for writing\n");
1498 pf->data_types = pm->data_types;
1499 pf->totpoint = pm->totpoint;
1500 pf->type = pid->type;
1503 if(pm->extradata.first)
1504 pf->flag |= PTCACHE_TYPEFLAG_EXTRADATA;
1506 if(pid->cache->compression)
1507 pf->flag |= PTCACHE_TYPEFLAG_COMPRESS;
1509 if(!ptcache_file_header_begin_write(pf) || !pid->write_header(pf))
1513 if(pid->cache->compression) {
1514 for(i=0; i<BPHYS_TOT_DATA; i++) {
1516 unsigned int in_len = pm->totpoint*ptcache_data_size[i];
1517 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
1518 ptcache_file_compressed_write(pf, (unsigned char*)(pm->data[i]), in_len, out, pid->cache->compression);
1524 BKE_ptcache_mem_pointers_init(pm);
1525 ptcache_file_pointers_init(pf);
1527 for(i=0; i<pm->totpoint; i++) {
1528 ptcache_data_copy(pm->cur, pf->cur);
1529 if(!ptcache_file_data_write(pf)) {
1533 BKE_ptcache_mem_pointers_incr(pm);
1538 if(!error && pm->extradata.first) {
1539 PTCacheExtra *extra = pm->extradata.first;
1541 for(; extra; extra=extra->next) {
1542 if(extra->data == NULL || extra->totdata == 0)
1545 ptcache_file_write(pf, &extra->type, 1, sizeof(unsigned int));
1546 ptcache_file_write(pf, &extra->totdata, 1, sizeof(unsigned int));
1548 if(pid->cache->compression) {
1549 unsigned int in_len = extra->totdata * ptcache_extra_datasize[extra->type];
1550 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
1551 ptcache_file_compressed_write(pf, (unsigned char*)(extra->data), in_len, out, pid->cache->compression);
1555 ptcache_file_write(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
1560 ptcache_file_close(pf);
1562 if (error && G.f & G_DEBUG)
1563 printf("Error writing to disk cache\n");
1568 static int ptcache_read_stream(PTCacheID *pid, int cfra)
1570 PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
1573 if(pid->read_stream == NULL)
1578 printf("Error opening disk cache file for reading\n");
1582 if(!ptcache_file_header_begin_read(pf))
1585 if(!error && (pf->type != pid->type || !pid->read_header(pf)))
1588 if(!error && pf->totpoint != pid->totpoint(pid->calldata, cfra))
1592 ptcache_file_pointers_init(pf);
1594 // we have stream reading here
1595 pid->read_stream(pf, pid->calldata);
1598 ptcache_file_close(pf);
1602 static int ptcache_read(PTCacheID *pid, int cfra)
1604 PTCacheMem *pm = NULL;
1608 /* get a memory cache to read from */
1609 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1610 pm = ptcache_disk_frame_to_mem(pid, cfra);
1613 pm = pid->cache->mem_cache.first;
1615 while(pm && pm->frame != cfra)
1619 /* read the cache */
1621 int totpoint = pm->totpoint;
1623 if((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
1624 totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, cfra));
1626 BKE_ptcache_mem_pointers_init(pm);
1628 for(i=0; i<totpoint; i++) {
1629 if(pm->data_types & (1<<BPHYS_DATA_INDEX))
1630 index = pm->cur[BPHYS_DATA_INDEX];
1632 pid->read_point(*index, pid->calldata, pm->cur, (float)pm->frame, NULL);
1634 BKE_ptcache_mem_pointers_incr(pm);
1637 if(pid->read_extra_data && pm->extradata.first)
1638 pid->read_extra_data(pid->calldata, pm, (float)pm->frame);
1640 /* clean up temporary memory cache */
1641 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1642 ptcache_data_free(pm);
1643 ptcache_extra_free(pm);
1650 static int ptcache_interpolate(PTCacheID *pid, float cfra, int cfra1, int cfra2)
1652 PTCacheMem *pm = NULL;
1656 /* get a memory cache to read from */
1657 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1658 pm = ptcache_disk_frame_to_mem(pid, cfra2);
1661 pm = pid->cache->mem_cache.first;
1663 while(pm && pm->frame != cfra2)
1667 /* read the cache */
1669 int totpoint = pm->totpoint;
1671 if((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
1672 totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, (int)cfra));
1674 BKE_ptcache_mem_pointers_init(pm);
1676 for(i=0; i<totpoint; i++) {
1677 if(pm->data_types & (1<<BPHYS_DATA_INDEX))
1678 index = pm->cur[BPHYS_DATA_INDEX];
1680 pid->interpolate_point(*index, pid->calldata, pm->cur, cfra, (float)cfra1, (float)cfra2, NULL);
1681 BKE_ptcache_mem_pointers_incr(pm);
1684 if(pid->interpolate_extra_data && pm->extradata.first)
1685 pid->interpolate_extra_data(pid->calldata, pm, cfra, (float)cfra1, (float)cfra2);
1687 /* clean up temporary memory cache */
1688 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1689 ptcache_data_free(pm);
1690 ptcache_extra_free(pm);
1697 /* reads cache from disk or memory */
1698 /* possible to get old or interpolated result */
1699 int BKE_ptcache_read(PTCacheID *pid, float cfra)
1701 int cfrai = (int)cfra, cfra1=0, cfra2=0;
1704 /* nothing to read to */
1705 if(pid->totpoint(pid->calldata, cfrai) == 0)
1708 if(pid->cache->flag & PTCACHE_READ_INFO) {
1709 pid->cache->flag &= ~PTCACHE_READ_INFO;
1710 ptcache_read(pid, 0);
1713 /* first check if we have the actual frame cached */
1714 if(cfra == (float)cfrai && BKE_ptcache_id_exist(pid, cfrai))
1717 /* no exact cache frame found so try to find cached frames around cfra */
1719 ptcache_find_frames_around(pid, cfrai, &cfra1, &cfra2);
1721 if(cfra1 == 0 && cfra2 == 0)
1724 /* don't read old cache if already simulated past cached frame */
1725 if(cfra1 == 0 && cfra2 && cfra2 <= pid->cache->simframe)
1727 if(cfra1 && cfra1 == cfra2)
1731 if(pid->read_stream)
1732 ptcache_read_stream(pid, cfra1);
1733 else if(pid->read_point)
1734 ptcache_read(pid, cfra1);
1738 if(pid->read_stream)
1739 ptcache_read_stream(pid, cfra2);
1740 else if(pid->read_point) {
1741 if(cfra1 && cfra2 && pid->interpolate_point)
1742 ptcache_interpolate(pid, cfra, cfra1, cfra2);
1744 ptcache_read(pid, cfra2);
1749 ret = (cfra2 ? PTCACHE_READ_INTERPOLATED : PTCACHE_READ_EXACT);
1751 ret = PTCACHE_READ_OLD;
1752 pid->cache->simframe = cfra2;
1755 if((pid->cache->flag & PTCACHE_QUICK_CACHE)==0) {
1757 /* clear invalid cache frames so that better stuff can be simulated */
1758 if(pid->cache->flag & PTCACHE_OUTDATED) {
1759 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, cfrai);
1761 else if(pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
1762 if(cfra <= pid->cache->last_exact)
1763 pid->cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
1765 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, MAX2(cfrai, pid->cache->last_exact));
1771 static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
1773 PTCacheFile *pf = NULL;
1776 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, cfra);
1778 pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, cfra);
1782 printf("Error opening disk cache file for writing\n");
1786 pf->data_types = pid->data_types;
1787 pf->totpoint = totpoint;
1788 pf->type = pid->type;
1791 if(!error && (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf)))
1794 if(!error && pid->write_stream)
1795 pid->write_stream(pf, pid->calldata);
1797 ptcache_file_close(pf);
1799 if (error && G.f & G_DEBUG)
1800 printf("Error writing to disk cache\n");
1804 static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
1806 PointCache *cache = pid->cache;
1807 PTCacheMem *pm=NULL, *pm2=NULL;
1808 int totpoint = pid->totpoint(pid->calldata, cfra);
1811 pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
1813 pm->totpoint = pid->totwrite(pid->calldata, cfra);
1814 pm->data_types = cfra ? pid->data_types : pid->info_types;
1816 ptcache_data_alloc(pm);
1817 BKE_ptcache_mem_pointers_init(pm);
1820 if(cache->flag & PTCACHE_DISK_CACHE) {
1823 while(fra >= cache->startframe && !BKE_ptcache_id_exist(pid, fra))
1826 pm2 = ptcache_disk_frame_to_mem(pid, fra);
1829 pm2 = cache->mem_cache.last;
1832 if(pid->write_point) {
1833 for(i=0; i<totpoint; i++) {
1834 int write = pid->write_point(i, pid->calldata, pm->cur, cfra);
1836 BKE_ptcache_mem_pointers_incr(pm);
1838 /* newly born particles have to be copied to previous cached frame */
1839 if(overwrite && write == 2 && pm2 && BKE_ptcache_mem_pointers_seek(i, pm2))
1840 pid->write_point(i, pid->calldata, pm2->cur, cfra);
1845 if(pid->write_extra_data)
1846 pid->write_extra_data(pid->calldata, pm, cfra);
1850 if(cache->flag & PTCACHE_DISK_CACHE) {
1851 error += !ptcache_mem_frame_to_disk(pid, pm);
1853 // if(pm) /* pm is always set */
1855 ptcache_data_free(pm);
1856 ptcache_extra_free(pm);
1861 error += !ptcache_mem_frame_to_disk(pid, pm2);
1862 ptcache_data_free(pm2);
1863 ptcache_extra_free(pm2);
1868 BLI_addtail(&cache->mem_cache, pm);
1873 static int ptcache_write_needed(PTCacheID *pid, int cfra, int *overwrite)
1875 PointCache *cache = pid->cache;
1876 int ofra = 0, efra = cache->endframe;
1878 /* allways start from scratch on the first frame */
1879 if(cfra && cfra == cache->startframe) {
1880 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, cfra);
1881 cache->flag &= ~PTCACHE_REDO_NEEDED;
1885 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1886 if(cfra==0 && cache->startframe > 0)
1889 /* find last cached frame */
1890 while(efra > cache->startframe && !BKE_ptcache_id_exist(pid, efra))
1893 /* find second last cached frame */
1895 while(ofra > cache->startframe && !BKE_ptcache_id_exist(pid, ofra))
1899 PTCacheMem *pm = cache->mem_cache.last;
1900 /* don't write info file in memory */
1908 ofra = (pm->prev ? pm->prev->frame : efra - cache->step);
1911 if(efra >= cache->startframe && cfra > efra) {
1912 if(ofra >= cache->startframe && efra - ofra < cache->step) {
1913 /* overwrite previous frame */
1914 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, efra);
1922 /* writes cache to disk or memory */
1923 int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
1925 PointCache *cache = pid->cache;
1926 int totpoint = pid->totpoint(pid->calldata, cfra);
1927 int overwrite = 0, error = 0;
1929 if(totpoint == 0 || (cfra ? pid->data_types == 0 : pid->info_types == 0))
1932 if(ptcache_write_needed(pid, cfra, &overwrite)==0)
1935 if(pid->write_stream) {
1936 ptcache_write_stream(pid, cfra, totpoint);
1938 else if(pid->write_point) {
1939 error += ptcache_write(pid, cfra, overwrite);
1942 /* Mark frames skipped if more than 1 frame forwards since last non-skipped frame. */
1943 if(cfra - cache->last_exact == 1 || cfra == cache->startframe) {
1944 cache->last_exact = cfra;
1945 cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
1947 /* Don't mark skipped when writing info file (frame 0) */
1949 cache->flag |= PTCACHE_FRAMES_SKIPPED;
1951 /* Update timeline cache display */
1952 if(cfra && cache->cached_frames)
1953 cache->cached_frames[cfra-cache->startframe] = 1;
1955 BKE_ptcache_update_info(pid);
1959 /* youll need to close yourself after!
1960 * mode - PTCACHE_CLEAR_ALL,
1963 /* Clears & resets */
1964 void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
1966 unsigned int len; /* store the length of the string */
1967 unsigned int sta, end;
1969 /* mode is same as fopen's modes */
1972 char path[MAX_PTCACHE_PATH];
1973 char filename[MAX_PTCACHE_FILE];
1974 char path_full[MAX_PTCACHE_FILE];
1975 char ext[MAX_PTCACHE_PATH];
1977 if(!pid || !pid->cache || pid->cache->flag & PTCACHE_BAKED)
1980 sta = pid->cache->startframe;
1981 end = pid->cache->endframe;
1983 #ifndef DURIAN_POINTCACHE_LIB_OK
1984 /* don't allow clearing for linked objects */
1989 /*if (!G.relbase_valid) return; *//* save blend file before using pointcache */
1991 /* clear all files in the temp dir with the prefix of the ID and the ".bphys" suffix */
1993 case PTCACHE_CLEAR_ALL:
1994 case PTCACHE_CLEAR_BEFORE:
1995 case PTCACHE_CLEAR_AFTER:
1996 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1997 ptcache_path(pid, path);
1999 len = ptcache_filename(pid, filename, cfra, 0, 0); /* no path */
2001 dir = opendir(path);
2005 BLI_snprintf(ext, sizeof(ext), "_%02u"PTCACHE_EXT, pid->stack_index);
2007 while ((de = readdir(dir)) != NULL) {
2008 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2009 if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2010 if (mode == PTCACHE_CLEAR_ALL) {
2011 pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
2012 BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
2013 BLI_delete(path_full, 0, 0);
2015 /* read the number of the file */
2016 unsigned int frame, len2 = (int)strlen(de->d_name);
2019 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2020 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2023 if((mode==PTCACHE_CLEAR_BEFORE && frame < cfra) ||
2024 (mode==PTCACHE_CLEAR_AFTER && frame > cfra) ) {
2026 BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
2027 BLI_delete(path_full, 0, 0);
2028 if(pid->cache->cached_frames && frame >=sta && frame <= end)
2029 pid->cache->cached_frames[frame-sta] = 0;
2038 if(mode == PTCACHE_CLEAR_ALL && pid->cache->cached_frames)
2039 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
2042 PTCacheMem *pm= pid->cache->mem_cache.first;
2043 PTCacheMem *link= NULL;
2045 if(mode == PTCACHE_CLEAR_ALL) {
2046 /*we want startframe if the cache starts before zero*/
2047 pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
2048 for(; pm; pm=pm->next) {
2049 ptcache_data_free(pm);
2050 ptcache_extra_free(pm);
2052 BLI_freelistN(&pid->cache->mem_cache);
2054 if(pid->cache->cached_frames)
2055 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
2058 if((mode==PTCACHE_CLEAR_BEFORE && pm->frame < cfra) ||
2059 (mode==PTCACHE_CLEAR_AFTER && pm->frame > cfra) ) {
2061 if(pid->cache->cached_frames && pm->frame >=sta && pm->frame <= end)
2062 pid->cache->cached_frames[pm->frame-sta] = 0;
2063 ptcache_data_free(pm);
2064 ptcache_extra_free(pm);
2066 BLI_freelinkN(&pid->cache->mem_cache, link);
2075 case PTCACHE_CLEAR_FRAME:
2076 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
2077 if(BKE_ptcache_id_exist(pid, cfra)) {
2078 ptcache_filename(pid, filename, cfra, 1, 1); /* no path */
2079 BLI_delete(filename, 0, 0);
2083 PTCacheMem *pm = pid->cache->mem_cache.first;
2085 for(; pm; pm=pm->next) {
2086 if(pm->frame == cfra) {
2087 ptcache_data_free(pm);
2088 ptcache_extra_free(pm);
2089 BLI_freelinkN(&pid->cache->mem_cache, pm);
2094 if(pid->cache->cached_frames && cfra>=sta && cfra<=end)
2095 pid->cache->cached_frames[cfra-sta] = 0;
2099 BKE_ptcache_update_info(pid);
2101 int BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
2106 if(cfra<pid->cache->startframe || cfra > pid->cache->endframe)
2109 if(pid->cache->cached_frames && pid->cache->cached_frames[cfra-pid->cache->startframe]==0)
2112 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
2113 char filename[MAX_PTCACHE_FILE];
2115 ptcache_filename(pid, filename, cfra, 1, 1);
2117 return BLI_exists(filename);
2120 PTCacheMem *pm = pid->cache->mem_cache.first;
2122 for(; pm; pm=pm->next) {
2129 void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
2133 /* float offset; unused for now */
2134 float time, nexttime;
2136 /* TODO: this has to be sorter out once bsystem_time gets redone, */
2137 /* now caches can handle interpolating etc. too - jahka */
2139 /* time handling for point cache:
2140 * - simulation time is scaled by result of bsystem_time
2141 * - for offsetting time only time offset is taken into account, since
2142 * that's always the same and can't be animated. a timeoffset which
2143 * varies over time is not simpe to support.
2144 * - field and motion blur offsets are currently ignored, proper solution
2145 * is probably to interpolate results from two frames for that ..
2152 time= bsystem_time(scene, ob, cfra, 0.0f);
2153 nexttime= bsystem_time(scene, ob, cfra+1.0f, 0.0f);
2155 *timescale= MAX2(nexttime - time, 0.0f);
2158 if(startframe && endframe) {
2159 *startframe= cache->startframe;
2160 *endframe= cache->endframe;
2162 /* TODO: time handling with object offsets and simulated vs. cached
2163 * particles isn't particularly easy, so for now what you see is what
2164 * you get. In the future point cache could handle the whole particle
2167 if ((ob->partype & PARSLOW)==0) {
2168 offset= give_timeoffset(ob);
2170 *startframe += (int)(offset+0.5f);
2171 *endframe += (int)(offset+0.5f);
2176 /* verify cached_frames array is up to date */
2177 if(cache->cached_frames) {
2178 if(MEM_allocN_len(cache->cached_frames) != sizeof(char) * (cache->endframe-cache->startframe+1)) {
2179 MEM_freeN(cache->cached_frames);
2180 cache->cached_frames = NULL;
2184 if(cache->cached_frames==NULL && cache->endframe > cache->startframe) {
2185 unsigned int sta=cache->startframe;
2186 unsigned int end=cache->endframe;
2188 cache->cached_frames = MEM_callocN(sizeof(char) * (cache->endframe-cache->startframe+1), "cached frames array");
2190 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
2191 /* mode is same as fopen's modes */
2194 char path[MAX_PTCACHE_PATH];
2195 char filename[MAX_PTCACHE_FILE];
2196 char ext[MAX_PTCACHE_PATH];
2197 unsigned int len; /* store the length of the string */
2199 ptcache_path(pid, path);
2201 len = ptcache_filename(pid, filename, (int)cfra, 0, 0); /* no path */
2203 dir = opendir(path);
2207 BLI_snprintf(ext, sizeof(ext), "_%02u"PTCACHE_EXT, pid->stack_index);
2209 while ((de = readdir(dir)) != NULL) {
2210 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2211 if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2212 /* read the number of the file */
2213 unsigned int frame, len2 = (int)strlen(de->d_name);
2216 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2217 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2220 if(frame >= sta && frame <= end)
2221 cache->cached_frames[frame-sta] = 1;
2229 PTCacheMem *pm= pid->cache->mem_cache.first;
2232 if(pm->frame >= sta && pm->frame <= end)
2233 cache->cached_frames[pm->frame-sta] = 1;
2239 int BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode)
2242 int reset, clear, after;
2252 if(mode == PTCACHE_RESET_DEPSGRAPH) {
2253 if(!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) {
2254 if(cache->flag & PTCACHE_QUICK_CACHE)
2260 cache->flag |= PTCACHE_OUTDATED;
2262 else if(mode == PTCACHE_RESET_BAKED) {
2263 if(!BKE_ptcache_get_continue_physics()) {
2268 cache->flag |= PTCACHE_OUTDATED;
2270 else if(mode == PTCACHE_RESET_OUTDATED) {
2273 if(cache->flag & PTCACHE_OUTDATED && !(cache->flag & PTCACHE_BAKED)) {
2275 cache->flag &= ~PTCACHE_OUTDATED;
2280 BKE_ptcache_invalidate(cache);
2281 cache->flag &= ~PTCACHE_REDO_NEEDED;
2283 if(pid->type == PTCACHE_TYPE_CLOTH)
2284 cloth_free_modifier(pid->calldata);
2285 else if(pid->type == PTCACHE_TYPE_SOFTBODY)
2286 sbFreeSimulation(pid->calldata);
2287 else if(pid->type == PTCACHE_TYPE_PARTICLES)
2288 psys_reset(pid->calldata, PSYS_RESET_DEPSGRAPH);
2289 else if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2290 smokeModifier_reset(pid->calldata);
2291 else if(pid->type == PTCACHE_TYPE_SMOKE_HIGHRES)
2292 smokeModifier_reset_turbulence(pid->calldata);
2295 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2297 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, CFRA);
2299 return (reset || clear || after);
2301 int BKE_ptcache_object_reset(Scene *scene, Object *ob, int mode)
2304 ParticleSystem *psys;
2312 BKE_ptcache_id_from_softbody(&pid, ob, ob->soft);
2313 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2316 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
2317 /* children or just redo can be calculated without reseting anything */
2318 if(psys->recalc & PSYS_RECALC_REDO || psys->recalc & PSYS_RECALC_CHILD)
2320 /* Baked cloth hair has to be checked too, because we don't want to reset */
2321 /* particles or cloth in that case -jahka */
2322 else if(psys->clmd) {
2323 BKE_ptcache_id_from_cloth(&pid, ob, psys->clmd);
2324 if(mode == PSYS_RESET_ALL || !(psys->part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED)))
2325 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2330 if(skip == 0 && psys->part) {
2331 BKE_ptcache_id_from_particles(&pid, ob, psys);
2332 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2336 for(md=ob->modifiers.first; md; md=md->next) {
2337 if(md->type == eModifierType_Cloth) {
2338 BKE_ptcache_id_from_cloth(&pid, ob, (ClothModifierData*)md);
2339 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2341 if(md->type == eModifierType_Smoke) {
2342 SmokeModifierData *smd = (SmokeModifierData *)md;
2343 if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
2345 BKE_ptcache_id_from_smoke(&pid, ob, (SmokeModifierData*)md);
2346 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2351 if (ob->type == OB_ARMATURE)
2352 BIK_clear_cache(ob->pose);
2357 /* Use this when quitting blender, with unsaved files */
2358 void BKE_ptcache_remove(void)
2360 char path[MAX_PTCACHE_PATH];
2361 char path_full[MAX_PTCACHE_PATH];
2364 ptcache_path(NULL, path);
2366 if (BLI_exist(path)) {
2367 /* The pointcache dir exists? - remove all pointcache */
2372 dir = opendir(path);
2376 while ((de = readdir(dir)) != NULL) {
2377 if( strcmp(de->d_name, ".")==0 || strcmp(de->d_name, "..")==0) {
2379 } else if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
2380 BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
2381 BLI_delete(path_full, 0, 0);
2383 rmdir = 0; /* unknown file, dont remove the dir */
2389 rmdir = 0; /* path dosnt exist */
2393 BLI_delete(path, 1, 0);
2397 /* Continuous Interaction */
2399 static int CONTINUE_PHYSICS = 0;
2401 void BKE_ptcache_set_continue_physics(Main *bmain, Scene *scene, int enable)
2405 if(CONTINUE_PHYSICS != enable) {
2406 CONTINUE_PHYSICS = enable;
2408 if(CONTINUE_PHYSICS == 0) {
2409 for(ob=bmain->object.first; ob; ob=ob->id.next)
2410 if(BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_OUTDATED))
2411 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2416 int BKE_ptcache_get_continue_physics(void)
2418 return CONTINUE_PHYSICS;
2421 /* Point Cache handling */
2423 PointCache *BKE_ptcache_add(ListBase *ptcaches)
2427 cache= MEM_callocN(sizeof(PointCache), "PointCache");
2428 cache->startframe= 1;
2429 cache->endframe= 250;
2433 BLI_addtail(ptcaches, cache);
2438 void BKE_ptcache_free_mem(ListBase *mem_cache)
2440 PTCacheMem *pm = mem_cache->first;
2443 for(; pm; pm=pm->next) {
2444 ptcache_data_free(pm);
2445 ptcache_extra_free(pm);
2448 BLI_freelistN(mem_cache);
2451 void BKE_ptcache_free(PointCache *cache)
2453 BKE_ptcache_free_mem(&cache->mem_cache);
2454 if(cache->edit && cache->free_edit)
2455 cache->free_edit(cache->edit);
2456 if(cache->cached_frames)
2457 MEM_freeN(cache->cached_frames);
2460 void BKE_ptcache_free_list(ListBase *ptcaches)
2462 PointCache *cache = ptcaches->first;
2465 BLI_remlink(ptcaches, cache);
2466 BKE_ptcache_free(cache);
2467 cache = ptcaches->first;
2471 static PointCache *ptcache_copy(PointCache *cache)
2475 ncache= MEM_dupallocN(cache);
2477 /* hmm, should these be copied over instead? */
2478 ncache->mem_cache.first = NULL;
2479 ncache->mem_cache.last = NULL;
2480 ncache->cached_frames = NULL;
2481 ncache->edit = NULL;
2484 ncache->simframe= 0;
2488 /* returns first point cache */
2489 PointCache *BKE_ptcache_copy_list(ListBase *ptcaches_new, ListBase *ptcaches_old)
2491 PointCache *cache = ptcaches_old->first;
2493 ptcaches_new->first = ptcaches_new->last = NULL;
2495 for(; cache; cache=cache->next)
2496 BLI_addtail(ptcaches_new, ptcache_copy(cache));
2498 return ptcaches_new->first;
2503 void BKE_ptcache_quick_cache_all(Main *bmain, Scene *scene)
2508 baker.break_data=NULL;
2509 baker.break_test=NULL;
2511 baker.progressbar=NULL;
2512 baker.progressend=NULL;
2513 baker.progresscontext=NULL;
2515 baker.anim_init = 0;
2518 baker.quick_step=scene->physics_settings.quick_cache_step;
2520 BKE_ptcache_bake(&baker);
2523 /* Simulation thread, no need for interlocks as data written in both threads
2524 are only unitary integers (I/O assumed to be atomic for them) */
2526 int break_operation;
2533 } ptcache_bake_data;
2535 static void ptcache_dt_to_str(char *str, double dtime)
2539 sprintf(str, "%ih %im %is", (int)(dtime/3600), ((int)(dtime/60))%60, ((int)dtime) % 60);
2541 sprintf(str, "%im %is", ((int)(dtime/60))%60, ((int)dtime) % 60);
2544 sprintf(str, "%is", ((int)dtime) % 60);
2547 static void *ptcache_bake_thread(void *ptr)
2549 int usetimer = 0, sfra, efra;
2550 double stime, ptime, ctime, fetd;
2551 char run[32], cur[32], etd[32];
2553 ptcache_bake_data *data = (ptcache_bake_data*)ptr;
2555 stime = ptime = PIL_check_seconds_timer();
2556 sfra = *data->cfra_ptr;
2557 efra = data->endframe;
2559 for(; (*data->cfra_ptr <= data->endframe) && !data->break_operation; *data->cfra_ptr+=data->step) {
2560 scene_update_for_newframe(data->main, data->scene, data->scene->lay);
2562 printf("bake: frame %d :: %d\n", (int)*data->cfra_ptr, data->endframe);
2565 ctime = PIL_check_seconds_timer();
2567 fetd = (ctime-ptime)*(efra-*data->cfra_ptr)/data->step;
2569 if(usetimer || fetd > 60.0) {
2572 ptcache_dt_to_str(cur, ctime-ptime);
2573 ptcache_dt_to_str(run, ctime-stime);
2574 ptcache_dt_to_str(etd, fetd);
2576 printf("Baked for %s, current frame: %i/%i (%.3fs), ETC: %s \r", run, *data->cfra_ptr-sfra+1, efra-sfra+1, ctime-ptime, etd);
2583 ptcache_dt_to_str(run, PIL_check_seconds_timer()-stime);
2584 printf("Bake %s %s (%i frames simulated). \n", (data->break_operation ? "canceled after" : "finished in"), run, *data->cfra_ptr-sfra);
2587 data->thread_ended = TRUE;
2591 /* if bake is not given run simulations to current frame */
2592 void BKE_ptcache_bake(PTCacheBaker* baker)
2594 Main *bmain = baker->main;
2595 Scene *scene = baker->scene;
2596 Scene *sce_iter; /* SETLOOPER macro only */
2599 PTCacheID *pid = baker->pid;
2600 PointCache *cache = NULL;
2601 float frameleno = scene->r.framelen;
2603 int startframe = MAXFRAME;
2604 int bake = baker->bake;
2605 int render = baker->render;
2607 ptcache_bake_data thread_data;
2608 int progress, old_progress;
2610 thread_data.endframe = baker->anim_init ? scene->r.sfra : CFRA;
2611 thread_data.step = baker->quick_step;
2612 thread_data.cfra_ptr = &CFRA;
2613 thread_data.scene = baker->scene;
2614 thread_data.main = baker->main;
2618 /* set caches to baking mode and figure out start frame */
2620 /* cache/bake a single object */
2622 if((cache->flag & PTCACHE_BAKED)==0) {
2623 if(pid->type==PTCACHE_TYPE_PARTICLES) {
2624 ParticleSystem *psys= pid->calldata;
2626 /* a bit confusing, could make this work better in the UI */
2627 if(psys->part->type == PART_EMITTER)
2628 psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
2630 else if(pid->type == PTCACHE_TYPE_SMOKE_HIGHRES) {
2631 /* get all pids from the object and search for smoke low res */
2634 BKE_ptcache_ids_from_object(&pidlist2, pid->ob, scene, MAX_DUPLI_RECUR);
2635 for(pid2=pidlist2.first; pid2; pid2=pid2->next) {
2636 if(pid2->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2638 if(pid2->cache && !(pid2->cache->flag & PTCACHE_BAKED)) {
2639 if(bake || pid2->cache->flag & PTCACHE_REDO_NEEDED)
2640 BKE_ptcache_id_clear(pid2, PTCACHE_CLEAR_ALL, 0);
2642 pid2->cache->flag |= PTCACHE_BAKING;
2643 pid2->cache->flag &= ~PTCACHE_BAKED;
2648 BLI_freelistN(&pidlist2);
2651 if(bake || cache->flag & PTCACHE_REDO_NEEDED)
2652 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2654 startframe = MAX2(cache->last_exact, cache->startframe);
2657 thread_data.endframe = cache->endframe;
2658 cache->flag |= PTCACHE_BAKING;
2661 thread_data.endframe = MIN2(thread_data.endframe, cache->endframe);
2664 cache->flag &= ~PTCACHE_BAKED;
2667 else for(SETLOOPER(scene, sce_iter, base)) {
2668 /* cache/bake everything in the scene */
2669 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
2671 for(pid=pidlist.first; pid; pid=pid->next) {
2673 if((cache->flag & PTCACHE_BAKED)==0) {
2674 if(pid->type==PTCACHE_TYPE_PARTICLES) {
2675 ParticleSystem *psys = (ParticleSystem*)pid->calldata;
2676 /* skip hair & keyed particles */
2677 if(psys->part->type == PART_HAIR || psys->part->phystype == PART_PHYS_KEYED)
2680 psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
2683 if((cache->flag & PTCACHE_REDO_NEEDED || (cache->flag & PTCACHE_SIMULATION_VALID)==0)
2684 && ((cache->flag & PTCACHE_QUICK_CACHE)==0 || render || bake))
2685 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2687 startframe = MIN2(startframe, cache->startframe);
2689 if(bake || render) {
2690 cache->flag |= PTCACHE_BAKING;
2693 thread_data.endframe = MAX2(thread_data.endframe, cache->endframe);
2696 cache->flag &= ~PTCACHE_BAKED;
2700 BLI_freelistN(&pidlist);
2704 scene->r.framelen = 1.0;
2705 thread_data.break_operation = FALSE;
2706 thread_data.thread_ended = FALSE;
2712 ptcache_bake_thread((void*)&thread_data);
2715 BLI_init_threads(&threads, ptcache_bake_thread, 1);
2716 BLI_insert_thread(&threads, (void*)&thread_data);
2718 while (thread_data.thread_ended == FALSE) {
2721 progress = (int)(100.0f * (float)(CFRA - startframe)/(float)(thread_data.endframe-startframe));
2725 /* NOTE: baking should not redraw whole ui as this slows things down */
2726 if ((baker->progressbar) && (progress != old_progress)) {
2727 baker->progressbar(baker->progresscontext, progress);
2728 old_progress = progress;
2731 /* Delay to lessen CPU load from UI thread */
2734 /* NOTE: breaking baking should leave calculated frames in cache, not clear it */
2735 if(blender_test_break() && !thread_data.break_operation) {
2736 thread_data.break_operation = TRUE;
2737 if (baker->progressend)
2738 baker->progressend(baker->progresscontext);
2743 BLI_end_threads(&threads);
2745 /* clear baking flag */
2747 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
2748 cache->flag |= PTCACHE_SIMULATION_VALID;
2750 cache->flag |= PTCACHE_BAKED;
2751 /* write info file */
2752 if(cache->flag & PTCACHE_DISK_CACHE)
2753 BKE_ptcache_write(pid, 0);
2756 else for(SETLOOPER(scene, sce_iter, base)) {
2757 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
2759 for(pid=pidlist.first; pid; pid=pid->next) {
2760 /* skip hair particles */
2761 if(pid->type==PTCACHE_TYPE_PARTICLES && ((ParticleSystem*)pid->calldata)->part->type == PART_HAIR)
2766 if(thread_data.step > 1)
2767 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_OUTDATED);
2769 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
2771 cache->flag |= PTCACHE_SIMULATION_VALID;
2774 cache->flag |= PTCACHE_BAKED;
2775 if(cache->flag & PTCACHE_DISK_CACHE)
2776 BKE_ptcache_write(pid, 0);
2779 BLI_freelistN(&pidlist);
2782 scene->r.framelen = frameleno;
2785 if(bake) /* already on cfra unless baking */
2786 scene_update_for_newframe(bmain, scene, scene->lay);
2788 if (thread_data.break_operation)
2790 else if (baker->progressend)
2791 baker->progressend(baker->progresscontext);
2795 /* TODO: call redraw all windows somehow */
2798 void BKE_ptcache_disk_to_mem(PTCacheID *pid)
2800 PointCache *cache = pid->cache;
2801 PTCacheMem *pm = NULL;
2802 int baked = cache->flag & PTCACHE_BAKED;
2803 int cfra, sfra = cache->startframe, efra = cache->endframe;
2805 /* Remove possible bake flag to allow clear */
2806 cache->flag &= ~PTCACHE_BAKED;
2808 /* PTCACHE_DISK_CACHE flag was cleared already */
2809 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2811 /* restore possible bake flag */
2812 cache->flag |= baked;
2814 for(cfra=sfra; cfra <= efra; cfra++) {
2815 pm = ptcache_disk_frame_to_mem(pid, cfra);
2818 BLI_addtail(&pid->cache->mem_cache, pm);
2821 void BKE_ptcache_mem_to_disk(PTCacheID *pid)
2823 PointCache *cache = pid->cache;
2824 PTCacheMem *pm = cache->mem_cache.first;
2825 int baked = cache->flag & PTCACHE_BAKED;
2827 /* Remove possible bake flag to allow clear */
2828 cache->flag &= ~PTCACHE_BAKED;
2830 /* PTCACHE_DISK_CACHE flag was set already */
2831 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2833 /* restore possible bake flag */
2834 cache->flag |= baked;
2836 for(; pm; pm=pm->next) {
2837 if(ptcache_mem_frame_to_disk(pid, pm)==0) {
2838 cache->flag &= ~PTCACHE_DISK_CACHE;
2843 /* write info file */
2844 if(cache->flag & PTCACHE_BAKED)
2845 BKE_ptcache_write(pid, 0);
2847 void BKE_ptcache_toggle_disk_cache(PTCacheID *pid)
2849 PointCache *cache = pid->cache;
2850 int last_exact = cache->last_exact;
2852 if (!G.relbase_valid){
2853 cache->flag &= ~PTCACHE_DISK_CACHE;
2855 printf("File must be saved before using disk cache!\n");
2859 if(cache->cached_frames) {
2860 MEM_freeN(cache->cached_frames);
2861 cache->cached_frames=NULL;
2864 if(cache->flag & PTCACHE_DISK_CACHE)
2865 BKE_ptcache_mem_to_disk(pid);
2867 BKE_ptcache_disk_to_mem(pid);
2869 cache->flag ^= PTCACHE_DISK_CACHE;
2870 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2871 cache->flag ^= PTCACHE_DISK_CACHE;
2873 cache->last_exact = last_exact;
2875 BKE_ptcache_id_time(pid, NULL, 0.0f, NULL, NULL, NULL);
2877 BKE_ptcache_update_info(pid);
2880 void BKE_ptcache_disk_cache_rename(PTCacheID *pid, char *from, char *to)
2883 int len; /* store the length of the string */
2884 /* mode is same as fopen's modes */
2887 char path[MAX_PTCACHE_PATH];
2888 char old_filename[MAX_PTCACHE_FILE];
2889 char new_path_full[MAX_PTCACHE_FILE];
2890 char old_path_full[MAX_PTCACHE_FILE];
2891 char ext[MAX_PTCACHE_PATH];
2894 BLI_strncpy(old_name, pid->cache->name, sizeof(old_name));
2896 /* get "from" filename */
2897 BLI_strncpy(pid->cache->name, from, sizeof(pid->cache->name));
2899 len = ptcache_filename(pid, old_filename, 0, 0, 0); /* no path */
2901 ptcache_path(pid, path);
2902 dir = opendir(path);
2904 BLI_strncpy(pid->cache->name, old_name, sizeof(pid->cache->name));
2908 BLI_snprintf(ext, sizeof(ext), "_%02u"PTCACHE_EXT, pid->stack_index);
2910 /* put new name into cache */
2911 BLI_strncpy(pid->cache->name, to, sizeof(pid->cache->name));
2913 while ((de = readdir(dir)) != NULL) {
2914 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2915 if (strncmp(old_filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2916 /* read the number of the file */
2917 int frame, len2 = (int)strlen(de->d_name);
2920 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2921 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2924 BLI_join_dirfile(old_path_full, sizeof(old_path_full), path, de->d_name);
2925 ptcache_filename(pid, new_path_full, frame, 1, 1);
2926 BLI_rename(old_path_full, new_path_full);
2933 strcpy(pid->cache->name, old_name);
2936 void BKE_ptcache_load_external(PTCacheID *pid)
2939 PointCache *cache = pid->cache;
2940 int len; /* store the length of the string */
2942 int start = MAXFRAME;
2945 /* mode is same as fopen's modes */
2948 char path[MAX_PTCACHE_PATH];
2949 char filename[MAX_PTCACHE_FILE];
2950 char ext[MAX_PTCACHE_PATH];
2955 ptcache_path(pid, path);
2957 len = ptcache_filename(pid, filename, 1, 0, 0); /* no path */
2959 dir = opendir(path);
2963 if(cache->index >= 0)
2964 BLI_snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, cache->index);
2966 BLI_strncpy(ext, PTCACHE_EXT, sizeof(ext));
2968 while ((de = readdir(dir)) != NULL) {
2969 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2970 if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2971 /* read the number of the file */
2972 int frame, len2 = (int)strlen(de->d_name);
2975 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2976 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2980 start = MIN2(start, frame);
2981 end = MAX2(end, frame);
2991 if(start != MAXFRAME) {
2994 cache->startframe = start;
2995 cache->endframe = end;
2996 cache->totpoint = 0;
2998 if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2999 ; /*necessary info in every file*/
3000 /* read totpoint from info file (frame 0) */
3002 pf= ptcache_file_open(pid, PTCACHE_FILE_READ, 0);
3005 if(ptcache_file_header_begin_read(pf)) {
3006 if(pf->type == pid->type && pid->read_header(pf)) {
3007 cache->totpoint = pf->totpoint;
3008 cache->flag |= PTCACHE_READ_INFO;
3011 cache->totpoint = 0;
3014 ptcache_file_close(pf);
3017 /* or from any old format cache file */
3020 int elemsize = ptcache_old_elemsize(pid);
3021 pf= ptcache_file_open(pid, PTCACHE_FILE_READ, cache->startframe);
3024 while(ptcache_file_read(pf, old_data, 1, elemsize))
3027 ptcache_file_close(pf);
3030 cache->flag |= (PTCACHE_BAKED|PTCACHE_DISK_CACHE|PTCACHE_SIMULATION_VALID);
3031 cache->flag &= ~(PTCACHE_OUTDATED|PTCACHE_FRAMES_SKIPPED);
3034 BKE_ptcache_update_info(pid);
3037 void BKE_ptcache_update_info(PTCacheID *pid)
3039 PointCache *cache = pid->cache;
3040 PTCacheExtra *extra = NULL;
3044 if(cache->flag & PTCACHE_EXTERNAL) {
3045 int cfra = cache->startframe;
3047 for(; cfra<=cache->endframe; cfra++) {
3048 if(BKE_ptcache_id_exist(pid, cfra))
3052 /* smoke doesn't use frame 0 as info frame so can't check based on totpoint */
3053 if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN && totframes)
3054 sprintf(cache->info, "%i frames found!", totframes);
3055 else if(totframes && cache->totpoint)
3056 sprintf(cache->info, "%i points found!", cache->totpoint);
3058 sprintf(cache->info, "No valid data to read!");
3062 if(cache->flag & PTCACHE_DISK_CACHE) {
3063 if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
3065 int totpoint = pid->totpoint(pid->calldata, 0);
3067 if(cache->totpoint > totpoint)
3068 sprintf(mem_info, "%i cells + High Resolution cached", totpoint);
3070 sprintf(mem_info, "%i cells cached", totpoint);
3073 int cfra = cache->startframe;
3075 for(; cfra<=cache->endframe; cfra++) {
3076 if(BKE_ptcache_id_exist(pid, cfra))