3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * Contributor(s): Campbell Barton <ideasman42@gmail.com>
24 * ***** END GPL LICENSE BLOCK *****
31 #include <sys/types.h>
33 #include "MEM_guardedalloc.h"
36 #include "DNA_cloth_types.h"
37 #include "DNA_modifier_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_object_force.h"
40 #include "DNA_particle_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_smoke_types.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_threads.h"
47 #include "BLI_utildefines.h"
54 #include "BKE_blender.h"
55 #include "BKE_cloth.h"
56 #include "BKE_depsgraph.h"
57 #include "BKE_global.h"
58 #include "BKE_library.h"
60 #include "BKE_object.h"
61 #include "BKE_particle.h"
62 #include "BKE_pointcache.h"
63 #include "BKE_scene.h"
64 #include "BKE_smoke.h"
65 #include "BKE_softbody.h"
66 #include "BKE_utildefines.h"
71 #include "smoke_API.h"
76 /* used for non-lzo cases */
77 #define LZO_OUT_LEN(size) ((size) + (size) / 16 + 64 + 3)
84 /* needed for directory lookup */
85 /* untitled blend's need getpid for a unique name */
91 #include "BLI_winstuff.h"
94 #define PTCACHE_DATA_FROM(data, type, from) if(data[type]) { memcpy(data[type], from, ptcache_data_size[type]); }
95 #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]); }
97 /* could be made into a pointcache option */
98 #define DURIAN_POINTCACHE_LIB_OK 1
100 static int ptcache_data_size[] = {
101 sizeof(unsigned int), // BPHYS_DATA_INDEX
102 3 * sizeof(float), // BPHYS_DATA_LOCATION
103 3 * sizeof(float), // BPHYS_DATA_VELOCITY
104 4 * sizeof(float), // BPHYS_DATA_ROTATION
105 3 * sizeof(float), // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST
106 sizeof(float), // BPHYS_DATA_SIZE
107 3 * sizeof(float), // BPHYS_DATA_TIMES
108 sizeof(BoidData) // case BPHYS_DATA_BOIDS
111 static int ptcache_extra_datasize[] = {
113 sizeof(ParticleSpring)
116 /* forward declerations */
117 static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result, unsigned int len);
118 static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, unsigned int in_len, unsigned char *out, int mode);
119 static int ptcache_file_write(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size);
120 static int ptcache_file_read(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size);
122 /* Common functions */
123 static int ptcache_basic_header_read(PTCacheFile *pf)
127 /* Custom functions should read these basic elements too! */
128 if(!error && !fread(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
131 if(!error && !fread(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
136 static int ptcache_basic_header_write(PTCacheFile *pf)
138 /* Custom functions should write these basic elements too! */
139 if(!fwrite(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
142 if(!fwrite(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
147 /* Softbody functions */
148 static int ptcache_softbody_write(int index, void *soft_v, void **data, int UNUSED(cfra))
150 SoftBody *soft= soft_v;
151 BodyPoint *bp = soft->bpoint + index;
153 PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, bp->pos);
154 PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, bp->vec);
158 static void ptcache_softbody_read(int index, void *soft_v, void **data, float UNUSED(cfra), float *old_data)
160 SoftBody *soft= soft_v;
161 BodyPoint *bp = soft->bpoint + index;
164 memcpy(bp->pos, data, 3 * sizeof(float));
165 memcpy(bp->vec, data + 3, 3 * sizeof(float));
168 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, bp->pos);
169 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, 0, bp->vec);
172 static void ptcache_softbody_interpolate(int index, void *soft_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
174 SoftBody *soft= soft_v;
175 BodyPoint *bp = soft->bpoint + index;
182 VECCOPY(keys[1].co, bp->pos);
183 VECCOPY(keys[1].vel, bp->vec);
186 memcpy(keys[2].co, old_data, 3 * sizeof(float));
187 memcpy(keys[2].vel, old_data + 3, 3 * sizeof(float));
190 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
192 dfra = cfra2 - cfra1;
194 mul_v3_fl(keys[1].vel, dfra);
195 mul_v3_fl(keys[2].vel, dfra);
197 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
199 mul_v3_fl(keys->vel, 1.0f / dfra);
201 VECCOPY(bp->pos, keys->co);
202 VECCOPY(bp->vec, keys->vel);
204 static int ptcache_softbody_totpoint(void *soft_v, int UNUSED(cfra))
206 SoftBody *soft= soft_v;
207 return soft->totpoint;
209 /* Particle functions */
210 void BKE_ptcache_make_particle_key(ParticleKey *key, int index, void **data, float time)
212 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, index, key->co);
213 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, index, key->vel);
215 /* no rotation info, so make something nice up */
216 if(data[BPHYS_DATA_ROTATION]==NULL) {
217 vec_to_quat( key->rot, key->vel, OB_NEGX, OB_POSZ);
220 PTCACHE_DATA_TO(data, BPHYS_DATA_ROTATION, index, key->rot);
223 PTCACHE_DATA_TO(data, BPHYS_DATA_AVELOCITY, index, key->ave);
226 static int ptcache_particle_write(int index, void *psys_v, void **data, int cfra)
228 ParticleSystem *psys= psys_v;
229 ParticleData *pa = psys->particles + index;
230 BoidParticle *boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
232 int step = psys->pointcache->step;
234 /* No need to store unborn or died particles outside cache step bounds */
235 if(data[BPHYS_DATA_INDEX] && (cfra < pa->time - step || cfra > pa->dietime + step))
239 times[1]= pa->dietime;
240 times[2]= pa->lifetime;
242 PTCACHE_DATA_FROM(data, BPHYS_DATA_INDEX, &index);
243 PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, pa->state.co);
244 PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, pa->state.vel);
245 PTCACHE_DATA_FROM(data, BPHYS_DATA_ROTATION, pa->state.rot);
246 PTCACHE_DATA_FROM(data, BPHYS_DATA_AVELOCITY, pa->state.ave);
247 PTCACHE_DATA_FROM(data, BPHYS_DATA_SIZE, &pa->size);
248 PTCACHE_DATA_FROM(data, BPHYS_DATA_TIMES, times);
251 PTCACHE_DATA_FROM(data, BPHYS_DATA_BOIDS, &boid->data);
253 /* return flag 1+1=2 for newly born particles to copy exact birth location to previously cached frame */
254 return 1 + (pa->state.time >= pa->time && pa->prev_state.time <= pa->time);
256 static void ptcache_particle_read(int index, void *psys_v, void **data, float cfra, float *old_data)
258 ParticleSystem *psys= psys_v;
261 float timestep = 0.04f*psys->part->timetweak;
263 if(index >= psys->totpart)
266 pa = psys->particles + index;
267 boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
269 if(cfra > pa->state.time)
270 memcpy(&pa->prev_state, &pa->state, sizeof(ParticleKey));
273 /* old format cache */
274 memcpy(&pa->state, old_data, sizeof(ParticleKey));
278 BKE_ptcache_make_particle_key(&pa->state, 0, data, cfra);
280 /* set frames cached before birth to birth time */
282 pa->state.time = pa->time;
284 if(data[BPHYS_DATA_SIZE])
285 PTCACHE_DATA_TO(data, BPHYS_DATA_SIZE, 0, &pa->size);
287 if(data[BPHYS_DATA_TIMES]) {
289 PTCACHE_DATA_TO(data, BPHYS_DATA_TIMES, 0, ×);
291 pa->dietime = times[1];
292 pa->lifetime = times[2];
296 PTCACHE_DATA_TO(data, BPHYS_DATA_BOIDS, 0, &boid->data);
298 /* determine velocity from previous location */
299 if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
300 if(cfra > pa->prev_state.time) {
301 sub_v3_v3v3(pa->state.vel, pa->state.co, pa->prev_state.co);
302 mul_v3_fl(pa->state.vel, (cfra - pa->prev_state.time) * timestep);
305 sub_v3_v3v3(pa->state.vel, pa->prev_state.co, pa->state.co);
306 mul_v3_fl(pa->state.vel, (pa->prev_state.time - cfra) * timestep);
310 /* determine rotation from velocity */
311 if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
312 vec_to_quat( pa->state.rot,pa->state.vel, OB_NEGX, OB_POSZ);
315 static void ptcache_particle_interpolate(int index, void *psys_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
317 ParticleSystem *psys= psys_v;
320 float dfra, timestep = 0.04f*psys->part->timetweak;
322 if(index >= psys->totpart)
325 pa = psys->particles + index;
327 /* particle wasn't read from first cache so can't interpolate */
328 if((int)cfra1 < pa->time - psys->pointcache->step || (int)cfra1 > pa->dietime + psys->pointcache->step)
331 cfra = MIN2(cfra, pa->dietime);
332 cfra1 = MIN2(cfra1, pa->dietime);
333 cfra2 = MIN2(cfra2, pa->dietime);
338 memcpy(keys+1, &pa->state, sizeof(ParticleKey));
340 memcpy(keys+2, old_data, sizeof(ParticleKey));
342 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
344 /* determine velocity from previous location */
345 if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
346 if(keys[1].time > keys[2].time) {
347 sub_v3_v3v3(keys[2].vel, keys[1].co, keys[2].co);
348 mul_v3_fl(keys[2].vel, (keys[1].time - keys[2].time) * timestep);
351 sub_v3_v3v3(keys[2].vel, keys[2].co, keys[1].co);
352 mul_v3_fl(keys[2].vel, (keys[2].time - keys[1].time) * timestep);
356 /* determine rotation from velocity */
357 if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
358 vec_to_quat( keys[2].rot,keys[2].vel, OB_NEGX, OB_POSZ);
362 cfra1 = MAX2(cfra1, pa->time);
364 dfra = cfra2 - cfra1;
366 mul_v3_fl(keys[1].vel, dfra * timestep);
367 mul_v3_fl(keys[2].vel, dfra * timestep);
369 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, &pa->state, 1);
370 interp_qt_qtqt(pa->state.rot, keys[1].rot, keys[2].rot, (cfra - cfra1) / dfra);
372 mul_v3_fl(pa->state.vel, 1.f / (dfra * timestep));
374 pa->state.time = cfra;
377 static int ptcache_particle_totpoint(void *psys_v, int UNUSED(cfra))
379 ParticleSystem *psys = psys_v;
380 return psys->totpart;
382 static int ptcache_particle_totwrite(void *psys_v, int cfra)
384 ParticleSystem *psys = psys_v;
385 ParticleData *pa= psys->particles;
386 int p, step = psys->pointcache->step;
390 return psys->totpart;
392 for(p=0; p<psys->totpart; p++,pa++)
393 totwrite += (cfra >= pa->time - step && cfra <= pa->dietime + step);
398 static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int UNUSED(cfra))
400 ParticleSystem *psys = psys_v;
401 PTCacheExtra *extra = NULL;
403 if(psys->part->phystype == PART_PHYS_FLUID &&
404 psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS &&
405 psys->tot_fluidsprings && psys->fluid_springs) {
407 extra = MEM_callocN(sizeof(PTCacheExtra), "Point cache: fluid extra data");
409 extra->type = BPHYS_EXTRA_FLUID_SPRINGS;
410 extra->totdata = psys->tot_fluidsprings;
412 extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Point cache: extra data");
413 memcpy(extra->data, psys->fluid_springs, extra->totdata * ptcache_extra_datasize[extra->type]);
415 BLI_addtail(&pm->extradata, extra);
419 static void ptcache_particle_extra_read(void *psys_v, PTCacheMem *pm, float UNUSED(cfra))
421 ParticleSystem *psys = psys_v;
422 PTCacheExtra *extra = pm->extradata.first;
424 for(; extra; extra=extra->next) {
425 switch(extra->type) {
426 case BPHYS_EXTRA_FLUID_SPRINGS:
428 if(psys->fluid_springs)
429 MEM_freeN(psys->fluid_springs);
431 psys->fluid_springs = MEM_dupallocN(extra->data);
432 psys->tot_fluidsprings = psys->alloc_fluidsprings = extra->totdata;
439 /* Cloth functions */
440 static int ptcache_cloth_write(int index, void *cloth_v, void **data, int UNUSED(cfra))
442 ClothModifierData *clmd= cloth_v;
443 Cloth *cloth= clmd->clothObject;
444 ClothVertex *vert = cloth->verts + index;
446 PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, vert->x);
447 PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, vert->v);
448 PTCACHE_DATA_FROM(data, BPHYS_DATA_XCONST, vert->xconst);
452 static void ptcache_cloth_read(int index, void *cloth_v, void **data, float UNUSED(cfra), float *old_data)
454 ClothModifierData *clmd= cloth_v;
455 Cloth *cloth= clmd->clothObject;
456 ClothVertex *vert = cloth->verts + index;
459 memcpy(vert->x, data, 3 * sizeof(float));
460 memcpy(vert->xconst, data + 3, 3 * sizeof(float));
461 memcpy(vert->v, data + 6, 3 * sizeof(float));
464 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, vert->x);
465 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, 0, vert->v);
466 PTCACHE_DATA_TO(data, BPHYS_DATA_XCONST, 0, vert->xconst);
469 static void ptcache_cloth_interpolate(int index, void *cloth_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
471 ClothModifierData *clmd= cloth_v;
472 Cloth *cloth= clmd->clothObject;
473 ClothVertex *vert = cloth->verts + index;
480 VECCOPY(keys[1].co, vert->x);
481 VECCOPY(keys[1].vel, vert->v);
484 memcpy(keys[2].co, old_data, 3 * sizeof(float));
485 memcpy(keys[2].vel, old_data + 6, 3 * sizeof(float));
488 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
490 dfra = cfra2 - cfra1;
492 mul_v3_fl(keys[1].vel, dfra);
493 mul_v3_fl(keys[2].vel, dfra);
495 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
497 mul_v3_fl(keys->vel, 1.0f / dfra);
499 VECCOPY(vert->x, keys->co);
500 VECCOPY(vert->v, keys->vel);
502 /* should vert->xconst be interpolated somehow too? - jahka */
505 static int ptcache_cloth_totpoint(void *cloth_v, int UNUSED(cfra))
507 ClothModifierData *clmd= cloth_v;
508 return clmd->clothObject ? clmd->clothObject->numverts : 0;
511 /* Smoke functions */
512 static int ptcache_smoke_totpoint(void *smoke_v, int UNUSED(cfra))
514 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
515 SmokeDomainSettings *sds = smd->domain;
518 return sds->res[0]*sds->res[1]*sds->res[2];
523 static int ptcache_smoke_write(PTCacheFile *pf, void *smoke_v)
525 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
526 SmokeDomainSettings *sds = smd->domain;
530 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
531 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
532 unsigned char *obstacles;
533 unsigned int in_len = sizeof(float)*(unsigned int)res;
534 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
535 //int mode = res >= 1000000 ? 2 : 1;
537 if (sds->cache_comp == SM_CACHE_HEAVY) mode=2; // heavy
539 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
541 ptcache_file_compressed_write(pf, (unsigned char *)sds->shadow, in_len, out, mode);
542 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len, out, mode);
543 ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len, out, mode);
544 ptcache_file_compressed_write(pf, (unsigned char *)heat, in_len, out, mode);
545 ptcache_file_compressed_write(pf, (unsigned char *)heatold, in_len, out, mode);
546 ptcache_file_compressed_write(pf, (unsigned char *)vx, in_len, out, mode);
547 ptcache_file_compressed_write(pf, (unsigned char *)vy, in_len, out, mode);
548 ptcache_file_compressed_write(pf, (unsigned char *)vz, in_len, out, mode);
549 ptcache_file_compressed_write(pf, (unsigned char *)vxold, in_len, out, mode);
550 ptcache_file_compressed_write(pf, (unsigned char *)vyold, in_len, out, mode);
551 ptcache_file_compressed_write(pf, (unsigned char *)vzold, in_len, out, mode);
552 ptcache_file_compressed_write(pf, (unsigned char *)obstacles, (unsigned int)res, out, mode);
553 ptcache_file_write(pf, &dt, 1, sizeof(float));
554 ptcache_file_write(pf, &dx, 1, sizeof(float));
562 int res_big_array[3];
564 int res = sds->res[0]*sds->res[1]*sds->res[2];
565 float *dens, *densold, *tcu, *tcv, *tcw;
566 unsigned int in_len = sizeof(float)*(unsigned int)res;
567 unsigned int in_len_big;
571 smoke_turbulence_get_res(sds->wt, res_big_array);
572 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
573 //mode = res_big >= 1000000 ? 2 : 1;
575 if (sds->cache_high_comp == SM_CACHE_HEAVY) mode=2; // heavy
577 in_len_big = sizeof(float) * (unsigned int)res_big;
579 smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
581 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len_big), "pointcache_lzo_buffer");
582 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len_big, out, mode);
583 ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len_big, out, mode);
586 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
587 ptcache_file_compressed_write(pf, (unsigned char *)tcu, in_len, out, mode);
588 ptcache_file_compressed_write(pf, (unsigned char *)tcv, in_len, out, mode);
589 ptcache_file_compressed_write(pf, (unsigned char *)tcw, in_len, out, mode);
597 static void ptcache_smoke_read(PTCacheFile *pf, void *smoke_v)
599 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
600 SmokeDomainSettings *sds = smd->domain;
603 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
604 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
605 unsigned char *obstacles;
606 unsigned int out_len = (unsigned int)res * sizeof(float);
608 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
610 ptcache_file_compressed_read(pf, (unsigned char *)sds->shadow, out_len);
611 ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len);
612 ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len);
613 ptcache_file_compressed_read(pf, (unsigned char*)heat, out_len);
614 ptcache_file_compressed_read(pf, (unsigned char*)heatold, out_len);
615 ptcache_file_compressed_read(pf, (unsigned char*)vx, out_len);
616 ptcache_file_compressed_read(pf, (unsigned char*)vy, out_len);
617 ptcache_file_compressed_read(pf, (unsigned char*)vz, out_len);
618 ptcache_file_compressed_read(pf, (unsigned char*)vxold, out_len);
619 ptcache_file_compressed_read(pf, (unsigned char*)vyold, out_len);
620 ptcache_file_compressed_read(pf, (unsigned char*)vzold, out_len);
621 ptcache_file_compressed_read(pf, (unsigned char*)obstacles, (unsigned int)res);
622 ptcache_file_read(pf, &dt, 1, sizeof(float));
623 ptcache_file_read(pf, &dx, 1, sizeof(float));
625 if(pf->data_types & (1<<BPHYS_DATA_SMOKE_HIGH) && sds->wt) {
626 int res = sds->res[0]*sds->res[1]*sds->res[2];
627 int res_big, res_big_array[3];
628 float *dens, *densold, *tcu, *tcv, *tcw;
629 unsigned int out_len = sizeof(float)*(unsigned int)res;
630 unsigned int out_len_big;
632 smoke_turbulence_get_res(sds->wt, res_big_array);
633 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
634 out_len_big = sizeof(float) * (unsigned int)res_big;
636 smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
638 ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len_big);
639 ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len_big);
641 ptcache_file_compressed_read(pf, (unsigned char*)tcu, out_len);
642 ptcache_file_compressed_read(pf, (unsigned char*)tcv, out_len);
643 ptcache_file_compressed_read(pf, (unsigned char*)tcw, out_len);
649 void BKE_ptcache_id_from_softbody(PTCacheID *pid, Object *ob, SoftBody *sb)
651 memset(pid, 0, sizeof(PTCacheID));
655 pid->type= PTCACHE_TYPE_SOFTBODY;
656 pid->cache= sb->pointcache;
657 pid->cache_ptr= &sb->pointcache;
658 pid->ptcaches= &sb->ptcaches;
659 pid->totpoint= pid->totwrite= ptcache_softbody_totpoint;
661 pid->write_point = ptcache_softbody_write;
662 pid->read_point = ptcache_softbody_read;
663 pid->interpolate_point = ptcache_softbody_interpolate;
665 pid->write_stream = NULL;
666 pid->read_stream = NULL;
668 pid->write_extra_data = NULL;
669 pid->read_extra_data = NULL;
670 pid->interpolate_extra_data = NULL;
672 pid->write_header = ptcache_basic_header_write;
673 pid->read_header = ptcache_basic_header_read;
675 pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY);
678 pid->stack_index = pid->cache->index;
680 void BKE_ptcache_id_from_particles(PTCacheID *pid, Object *ob, ParticleSystem *psys)
682 memset(pid, 0, sizeof(PTCacheID));
686 pid->type= PTCACHE_TYPE_PARTICLES;
687 pid->stack_index= psys->pointcache->index;
688 pid->cache= psys->pointcache;
689 pid->cache_ptr= &psys->pointcache;
690 pid->ptcaches= &psys->ptcaches;
692 if(psys->part->type != PART_HAIR)
693 pid->flag |= PTCACHE_VEL_PER_SEC;
695 pid->totpoint = ptcache_particle_totpoint;
696 pid->totwrite = ptcache_particle_totwrite;
698 pid->write_point = ptcache_particle_write;
699 pid->read_point = ptcache_particle_read;
700 pid->interpolate_point = ptcache_particle_interpolate;
702 pid->write_stream = NULL;
703 pid->read_stream = NULL;
705 pid->write_extra_data = NULL;
706 pid->read_extra_data = NULL;
707 pid->interpolate_extra_data = NULL;
709 pid->write_header = ptcache_basic_header_write;
710 pid->read_header = ptcache_basic_header_read;
712 pid->data_types = (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_INDEX);
714 if(psys->part->phystype == PART_PHYS_BOIDS)
715 pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION) | (1<<BPHYS_DATA_BOIDS);
716 else if(psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS) {
717 pid->write_extra_data = ptcache_particle_extra_write;
718 pid->read_extra_data = ptcache_particle_extra_read;
721 if(psys->part->rotmode!=PART_ROT_VEL
722 || psys->part->avemode!=PART_AVE_SPIN || psys->part->avefac!=0.0f)
723 pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION);
725 if(psys->part->flag & PART_ROT_DYN)
726 pid->data_types|= (1<<BPHYS_DATA_ROTATION);
728 pid->info_types= (1<<BPHYS_DATA_TIMES);
730 void BKE_ptcache_id_from_cloth(PTCacheID *pid, Object *ob, ClothModifierData *clmd)
732 memset(pid, 0, sizeof(PTCacheID));
736 pid->type= PTCACHE_TYPE_CLOTH;
737 pid->stack_index= clmd->point_cache->index;
738 pid->cache= clmd->point_cache;
739 pid->cache_ptr= &clmd->point_cache;
740 pid->ptcaches= &clmd->ptcaches;
741 pid->totpoint= pid->totwrite= ptcache_cloth_totpoint;
743 pid->write_point = ptcache_cloth_write;
744 pid->read_point = ptcache_cloth_read;
745 pid->interpolate_point = ptcache_cloth_interpolate;
747 pid->write_stream = NULL;
748 pid->read_stream = NULL;
750 pid->write_extra_data = NULL;
751 pid->read_extra_data = NULL;
752 pid->interpolate_extra_data = NULL;
754 pid->write_header = ptcache_basic_header_write;
755 pid->read_header = ptcache_basic_header_read;
757 pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_XCONST);
760 void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct SmokeModifierData *smd)
762 SmokeDomainSettings *sds = smd->domain;
764 memset(pid, 0, sizeof(PTCacheID));
769 pid->type= PTCACHE_TYPE_SMOKE_DOMAIN;
770 pid->stack_index= sds->point_cache[0]->index;
772 pid->cache= sds->point_cache[0];
773 pid->cache_ptr= &(sds->point_cache[0]);
774 pid->ptcaches= &(sds->ptcaches[0]);
776 pid->totpoint= pid->totwrite= ptcache_smoke_totpoint;
778 pid->write_point = NULL;
779 pid->read_point = NULL;
780 pid->interpolate_point = NULL;
782 pid->read_stream = ptcache_smoke_read;
783 pid->write_stream = ptcache_smoke_write;
785 pid->write_extra_data = NULL;
786 pid->read_extra_data = NULL;
787 pid->interpolate_extra_data = NULL;
789 pid->write_header = ptcache_basic_header_write;
790 pid->read_header = ptcache_basic_header_read;
796 pid->data_types |= (1<<BPHYS_DATA_SMOKE_LOW);
798 pid->data_types |= (1<<BPHYS_DATA_SMOKE_HIGH);
800 void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int duplis)
803 ParticleSystem *psys;
806 lb->first= lb->last= NULL;
809 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
810 BKE_ptcache_id_from_softbody(pid, ob, ob->soft);
811 BLI_addtail(lb, pid);
814 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
818 /* check to make sure point cache is actually used by the particles */
819 if(ELEM(psys->part->phystype, PART_PHYS_NO, PART_PHYS_KEYED))
822 /* hair needs to be included in id-list for cache edit mode to work */
823 /* if(psys->part->type == PART_HAIR && (psys->flag & PSYS_HAIR_DYNAMICS)==0) */
826 if(psys->part->type == PART_FLUID)
829 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
830 BKE_ptcache_id_from_particles(pid, ob, psys);
831 BLI_addtail(lb, pid);
834 for(md=ob->modifiers.first; md; md=md->next) {
835 if(md->type == eModifierType_Cloth) {
836 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
837 BKE_ptcache_id_from_cloth(pid, ob, (ClothModifierData*)md);
838 BLI_addtail(lb, pid);
840 if(md->type == eModifierType_Smoke) {
841 SmokeModifierData *smd = (SmokeModifierData *)md;
842 if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
844 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
845 BKE_ptcache_id_from_smoke(pid, ob, (SmokeModifierData*)md);
846 BLI_addtail(lb, pid);
851 if(scene && (duplis-- > 0) && (ob->transflag & OB_DUPLI)) {
852 ListBase *lb_dupli_ob;
854 if((lb_dupli_ob=object_duplilist(scene, ob))) {
856 for(dob= lb_dupli_ob->first; dob; dob= dob->next) {
857 if(dob->ob != ob) { /* avoids recursive loops with dupliframes: bug 22988 */
858 ListBase lb_dupli_pid;
859 BKE_ptcache_ids_from_object(&lb_dupli_pid, dob->ob, scene, duplis);
860 BLI_movelisttolist(lb, &lb_dupli_pid);
861 if(lb_dupli_pid.first)
862 printf("Adding Dupli\n");
866 free_object_duplilist(lb_dupli_ob); /* does restore */
873 /* Takes an Object ID and returns a unique name
875 - cfra: frame for the cache, can be negative
876 - stack_index: index in the modifier stack. we can have cache for more then one stack_index
879 #define MAX_PTCACHE_PATH FILE_MAX
880 #define MAX_PTCACHE_FILE ((FILE_MAXDIR+FILE_MAXFILE)*2)
882 static int ptcache_path(PTCacheID *pid, char *filename)
884 Library *lib= (pid->ob)? pid->ob->id.lib: NULL;
885 const char *blendfilename= (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH)==0) ? lib->filepath: G.main->name;
888 if(pid->cache->flag & PTCACHE_EXTERNAL) {
889 strcpy(filename, pid->cache->path);
891 if(strncmp(filename, "//", 2)==0)
892 BLI_path_abs(filename, blendfilename);
894 return BLI_add_slash(filename); /* new strlen() */
896 else if (G.relbase_valid || lib) {
897 char file[MAX_PTCACHE_PATH]; /* we dont want the dir, only the file */
899 BLI_split_dirfile(blendfilename, NULL, file);
906 snprintf(filename, MAX_PTCACHE_PATH, "//"PTCACHE_PATH"%s", file); /* add blend file name to pointcache dir */
907 BLI_path_abs(filename, blendfilename);
908 return BLI_add_slash(filename); /* new strlen() */
911 /* use the temp path. this is weak but better then not using point cache at all */
912 /* btempdir is assumed to exist and ALWAYS has a trailing slash */
913 snprintf(filename, MAX_PTCACHE_PATH, "%s"PTCACHE_PATH"%d", btempdir, abs(getpid()));
915 return BLI_add_slash(filename); /* new strlen() */
918 static int ptcache_filename(PTCacheID *pid, char *filename, int cfra, short do_path, short do_ext)
926 if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return 0; /* save blend file before using disk pointcache */
928 /* start with temp dir */
930 len = ptcache_path(pid, filename);
933 if(strcmp(pid->cache->name, "")==0 && (pid->cache->flag & PTCACHE_EXTERNAL)==0) {
934 idname = (pid->ob->id.name+2);
935 /* convert chars to hex so they are always a valid filename */
936 while('\0' != *idname) {
937 snprintf(newname, MAX_PTCACHE_FILE, "%02X", (char)(*idname++));
943 int temp = (int)strlen(pid->cache->name);
944 strcpy(newname, pid->cache->name);
951 if(pid->cache->index < 0)
952 pid->cache->index = pid->stack_index = object_insert_ptcache(pid->ob);
954 if(pid->cache->flag & PTCACHE_EXTERNAL) {
955 if(pid->cache->index >= 0)
956 snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02d"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
958 snprintf(newname, MAX_PTCACHE_FILE, "_%06d"PTCACHE_EXT, cfra); /* always 6 chars */
961 snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02d"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
966 return len; /* make sure the above string is always 16 chars */
969 /* youll need to close yourself after! */
970 static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
974 char filename[(FILE_MAXDIR+FILE_MAXFILE)*2];
976 #ifndef DURIAN_POINTCACHE_LIB_OK
977 /* don't allow writing for linked objects */
978 if(pid->ob->id.lib && mode == PTCACHE_FILE_WRITE)
981 if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return NULL; /* save blend file before using disk pointcache */
983 ptcache_filename(pid, filename, cfra, 1, 1);
985 if (mode==PTCACHE_FILE_READ) {
986 if (!BLI_exists(filename)) {
989 fp = fopen(filename, "rb");
990 } else if (mode==PTCACHE_FILE_WRITE) {
991 BLI_make_existing_file(filename); /* will create the dir if needs be, same as //textures is created */
992 fp = fopen(filename, "wb");
993 } else if (mode==PTCACHE_FILE_UPDATE) {
994 BLI_make_existing_file(filename);
995 fp = fopen(filename, "rb+");
1001 pf= MEM_mallocN(sizeof(PTCacheFile), "PTCacheFile");
1008 static void ptcache_file_close(PTCacheFile *pf)
1016 static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result, unsigned int len)
1019 unsigned char compressed = 0;
1022 size_t out_len = len;
1023 size_t sizeOfIt = 5;
1026 unsigned char *props = MEM_callocN(16*sizeof(char), "tmp");
1028 ptcache_file_read(pf, &compressed, 1, sizeof(unsigned char));
1031 ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
1032 in_len = (size_t)size;
1037 in = (unsigned char *)MEM_callocN(sizeof(unsigned char)*in_len, "pointcache_compressed_buffer");
1038 ptcache_file_read(pf, in, in_len, sizeof(unsigned char));
1041 r = lzo1x_decompress_safe(in, (lzo_uint)in_len, result, (lzo_uint *)&out_len, NULL);
1046 size_t leni = in_len, leno = out_len;
1047 ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
1048 sizeOfIt = (size_t)size;
1049 ptcache_file_read(pf, props, sizeOfIt, sizeof(unsigned char));
1050 r = LzmaUncompress(result, &leno, in, &leni, props, sizeOfIt);
1057 ptcache_file_read(pf, result, len, sizeof(unsigned char));
1064 static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, unsigned int in_len, unsigned char *out, int mode)
1067 unsigned char compressed = 0;
1069 unsigned char *props = MEM_callocN(16*sizeof(char), "tmp");
1070 size_t sizeOfIt = 5;
1072 (void)mode; /* unused when building w/o compression */
1075 out_len= LZO_OUT_LEN(in_len);
1077 LZO_HEAP_ALLOC(wrkmem, LZO1X_MEM_COMPRESS);
1079 r = lzo1x_1_compress(in, (lzo_uint)in_len, out, (lzo_uint *)&out_len, wrkmem);
1080 if (!(r == LZO_E_OK) || (out_len >= in_len))
1089 r = LzmaCompress(out, &out_len, in, in_len,//assume sizeof(char)==1....
1090 props, &sizeOfIt, 5, 1 << 24, 3, 0, 2, 32, 2);
1092 if(!(r == SZ_OK) || (out_len >= in_len))
1099 ptcache_file_write(pf, &compressed, 1, sizeof(unsigned char));
1101 unsigned int size = out_len;
1102 ptcache_file_write(pf, &size, 1, sizeof(unsigned int));
1103 ptcache_file_write(pf, out, out_len, sizeof(unsigned char));
1106 ptcache_file_write(pf, in, in_len, sizeof(unsigned char));
1110 unsigned int size = sizeOfIt;
1111 ptcache_file_write(pf, &sizeOfIt, 1, sizeof(unsigned int));
1112 ptcache_file_write(pf, props, size, sizeof(unsigned char));
1119 static int ptcache_file_read(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size)
1121 return (fread(f, size, tot, pf->fp) == tot);
1123 static int ptcache_file_write(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size)
1125 return (fwrite(f, size, tot, pf->fp) == tot);
1127 static int ptcache_file_data_read(PTCacheFile *pf)
1131 for(i=0; i<BPHYS_TOT_DATA; i++) {
1132 if((pf->data_types & (1<<i)) && !ptcache_file_read(pf, pf->cur[i], 1, ptcache_data_size[i]))
1138 static int ptcache_file_data_write(PTCacheFile *pf)
1142 for(i=0; i<BPHYS_TOT_DATA; i++) {
1143 if((pf->data_types & (1<<i)) && !ptcache_file_write(pf, pf->cur[i], 1, ptcache_data_size[i]))
1149 static int ptcache_file_header_begin_read(PTCacheFile *pf)
1151 unsigned int typeflag=0;
1157 if(fread(bphysics, sizeof(char), 8, pf->fp) != 8)
1160 if(!error && strncmp(bphysics, "BPHYSICS", 8))
1163 if(!error && !fread(&typeflag, sizeof(unsigned int), 1, pf->fp))
1166 pf->type = (typeflag & PTCACHE_TYPEFLAG_TYPEMASK);
1167 pf->flag = (typeflag & PTCACHE_TYPEFLAG_FLAGMASK);
1169 /* if there was an error set file as it was */
1171 fseek(pf->fp, 0, SEEK_SET);
1175 static int ptcache_file_header_begin_write(PTCacheFile *pf)
1177 const char *bphysics = "BPHYSICS";
1178 unsigned int typeflag = pf->type + pf->flag;
1180 if(fwrite(bphysics, sizeof(char), 8, pf->fp) != 8)
1183 if(!fwrite(&typeflag, sizeof(unsigned int), 1, pf->fp))
1189 /* Data pointer handling */
1190 int BKE_ptcache_data_size(int data_type)
1192 return ptcache_data_size[data_type];
1195 static void ptcache_file_pointers_init(PTCacheFile *pf)
1197 int data_types = pf->data_types;
1199 pf->cur[BPHYS_DATA_INDEX] = (data_types & (1<<BPHYS_DATA_INDEX)) ? &pf->data.index : NULL;
1200 pf->cur[BPHYS_DATA_LOCATION] = (data_types & (1<<BPHYS_DATA_LOCATION)) ? &pf->data.loc : NULL;
1201 pf->cur[BPHYS_DATA_VELOCITY] = (data_types & (1<<BPHYS_DATA_VELOCITY)) ? &pf->data.vel : NULL;
1202 pf->cur[BPHYS_DATA_ROTATION] = (data_types & (1<<BPHYS_DATA_ROTATION)) ? &pf->data.rot : NULL;
1203 pf->cur[BPHYS_DATA_AVELOCITY] = (data_types & (1<<BPHYS_DATA_AVELOCITY))? &pf->data.ave : NULL;
1204 pf->cur[BPHYS_DATA_SIZE] = (data_types & (1<<BPHYS_DATA_SIZE)) ? &pf->data.size : NULL;
1205 pf->cur[BPHYS_DATA_TIMES] = (data_types & (1<<BPHYS_DATA_TIMES)) ? &pf->data.times : NULL;
1206 pf->cur[BPHYS_DATA_BOIDS] = (data_types & (1<<BPHYS_DATA_BOIDS)) ? &pf->data.boids : NULL;
1209 /* Check to see if point number "index" is in pm, uses binary search for index data. */
1210 int BKE_ptcache_mem_index_find(PTCacheMem *pm, unsigned int index)
1212 if(pm->data[BPHYS_DATA_INDEX]) {
1213 unsigned int *data = pm->data[BPHYS_DATA_INDEX];
1214 unsigned int mid, low = 0, high = pm->totpoint - 1;
1216 if(index < *data || index > *(data+high))
1219 /* check simple case for continuous indexes first */
1220 if(index-*data < high && data[index-*data] == index)
1223 while(low <= high) {
1224 mid= (low + high)/2;
1226 if(data[mid] > index)
1228 else if(data[mid] < index)
1237 return (index < pm->totpoint ? index : -1);
1241 void BKE_ptcache_mem_pointers_init(PTCacheMem *pm)
1243 int data_types = pm->data_types;
1246 for(i=0; i<BPHYS_TOT_DATA; i++)
1247 pm->cur[i] = ((data_types & (1<<i)) ? pm->data[i] : NULL);
1250 void BKE_ptcache_mem_pointers_incr(PTCacheMem *pm)
1254 for(i=0; i<BPHYS_TOT_DATA; i++) {
1256 pm->cur[i] = (char*)pm->cur[i] + ptcache_data_size[i];
1259 int BKE_ptcache_mem_pointers_seek(int point_index, PTCacheMem *pm)
1261 int data_types = pm->data_types;
1262 int i, index = BKE_ptcache_mem_index_find(pm, point_index);
1265 /* Can't give proper location without reallocation, so don't give any location.
1266 * Some points will be cached improperly, but this only happens with simulation
1267 * steps bigger than cache->step, so the cache has to be recalculated anyways
1273 for(i=0; i<BPHYS_TOT_DATA; i++)
1274 pm->cur[i] = data_types & (1<<i) ? (char*)pm->data[i] + index * ptcache_data_size[i] : NULL;
1278 static void ptcache_data_alloc(PTCacheMem *pm)
1280 int data_types = pm->data_types;
1281 int totpoint = pm->totpoint;
1284 for(i=0; i<BPHYS_TOT_DATA; i++) {
1285 if(data_types & (1<<i))
1286 pm->data[i] = MEM_callocN(totpoint * ptcache_data_size[i], "PTCache Data");
1289 static void ptcache_data_free(PTCacheMem *pm)
1291 void **data = pm->data;
1294 for(i=0; i<BPHYS_TOT_DATA; i++) {
1299 static void ptcache_data_copy(void *from[], void *to[])
1302 for(i=0; i<BPHYS_TOT_DATA; i++) {
1303 /* note, durian file 03.4b_comp crashes if to[i] is not tested
1304 * its NULL, not sure if this should be fixed elsewhere but for now its needed */
1305 if(from[i] && to[i])
1306 memcpy(to[i], from[i], ptcache_data_size[i]);
1310 static void ptcache_extra_free(PTCacheMem *pm)
1312 PTCacheExtra *extra = pm->extradata.first;
1315 for(; extra; extra=extra->next) {
1317 MEM_freeN(extra->data);
1320 BLI_freelistN(&pm->extradata);
1323 static int ptcache_old_elemsize(PTCacheID *pid)
1325 if(pid->type==PTCACHE_TYPE_SOFTBODY)
1326 return 6 * sizeof(float);
1327 else if(pid->type==PTCACHE_TYPE_PARTICLES)
1328 return sizeof(ParticleKey);
1329 else if(pid->type==PTCACHE_TYPE_CLOTH)
1330 return 9 * sizeof(float);
1335 static void ptcache_find_frames_around(PTCacheID *pid, unsigned int frame, int *fra1, int *fra2)
1337 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1338 int cfra1=frame-1, cfra2=frame+1;
1340 while(cfra1 >= pid->cache->startframe && !BKE_ptcache_id_exist(pid, cfra1))
1343 if(cfra1 < pid->cache->startframe)
1346 while(cfra2 <= pid->cache->endframe && !BKE_ptcache_id_exist(pid, cfra2))
1349 if(cfra2 > pid->cache->endframe)
1352 if(cfra1 && !cfra2) {
1361 else if(pid->cache->mem_cache.first) {
1362 PTCacheMem *pm = pid->cache->mem_cache.first;
1363 PTCacheMem *pm2 = pid->cache->mem_cache.last;
1365 while(pm->next && pm->next->frame < frame)
1368 if(pm2 && pm2->frame < frame)
1371 while(pm2->prev && pm2->prev->frame > frame)
1386 static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
1388 PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
1389 PTCacheMem *pm = NULL;
1390 unsigned int i, error = 0;
1395 if(!ptcache_file_header_begin_read(pf))
1398 if(!error && (pf->type != pid->type || !pid->read_header(pf)))
1402 pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
1404 pm->totpoint = pf->totpoint;
1405 pm->data_types = pf->data_types;
1406 pm->frame = pf->frame;
1408 ptcache_data_alloc(pm);
1410 if(pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
1411 for(i=0; i<BPHYS_TOT_DATA; i++) {
1412 unsigned int out_len = pm->totpoint*ptcache_data_size[i];
1413 if(pf->data_types & (1<<i))
1414 ptcache_file_compressed_read(pf, (unsigned char*)(pm->data[i]), out_len);
1418 BKE_ptcache_mem_pointers_init(pm);
1419 ptcache_file_pointers_init(pf);
1421 for(i=0; i<pm->totpoint; i++) {
1422 if(!ptcache_file_data_read(pf)) {
1426 ptcache_data_copy(pf->cur, pm->cur);
1427 BKE_ptcache_mem_pointers_incr(pm);
1432 if(!error && pf->flag & PTCACHE_TYPEFLAG_EXTRADATA) {
1433 unsigned int extratype = 0;
1435 while(ptcache_file_read(pf, &extratype, 1, sizeof(unsigned int))) {
1436 PTCacheExtra *extra = MEM_callocN(sizeof(PTCacheExtra), "Pointcache extradata");
1438 extra->type = extratype;
1440 ptcache_file_read(pf, &extra->totdata, 1, sizeof(unsigned int));
1442 extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Pointcache extradata->data");
1444 if(pf->flag & PTCACHE_TYPEFLAG_COMPRESS)
1445 ptcache_file_compressed_read(pf, (unsigned char*)(extra->data), extra->totdata*ptcache_extra_datasize[extra->type]);
1447 ptcache_file_read(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
1449 BLI_addtail(&pm->extradata, extra);
1454 ptcache_data_free(pm);
1455 ptcache_extra_free(pm);
1460 ptcache_file_close(pf);
1462 if (error && G.f & G_DEBUG)
1463 printf("Error reading from disk cache\n");
1467 static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
1469 PTCacheFile *pf = NULL;
1470 unsigned int i, error = 0;
1472 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, pm->frame);
1474 pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, pm->frame);
1478 printf("Error opening disk cache file for writing\n");
1482 pf->data_types = pm->data_types;
1483 pf->totpoint = pm->totpoint;
1484 pf->type = pid->type;
1487 if(pm->extradata.first)
1488 pf->flag |= PTCACHE_TYPEFLAG_EXTRADATA;
1490 if(pid->cache->compression)
1491 pf->flag |= PTCACHE_TYPEFLAG_COMPRESS;
1493 if(!ptcache_file_header_begin_write(pf) || !pid->write_header(pf))
1497 if(pid->cache->compression) {
1498 for(i=0; i<BPHYS_TOT_DATA; i++) {
1500 unsigned int in_len = pm->totpoint*ptcache_data_size[i];
1501 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
1502 ptcache_file_compressed_write(pf, (unsigned char*)(pm->data[i]), in_len, out, pid->cache->compression);
1508 BKE_ptcache_mem_pointers_init(pm);
1509 ptcache_file_pointers_init(pf);
1511 for(i=0; i<pm->totpoint; i++) {
1512 ptcache_data_copy(pm->cur, pf->cur);
1513 if(!ptcache_file_data_write(pf)) {
1517 BKE_ptcache_mem_pointers_incr(pm);
1522 if(!error && pm->extradata.first) {
1523 PTCacheExtra *extra = pm->extradata.first;
1525 for(; extra; extra=extra->next) {
1526 if(extra->data == NULL || extra->totdata == 0)
1529 ptcache_file_write(pf, &extra->type, 1, sizeof(unsigned int));
1530 ptcache_file_write(pf, &extra->totdata, 1, sizeof(unsigned int));
1532 if(pid->cache->compression) {
1533 unsigned int in_len = extra->totdata * ptcache_extra_datasize[extra->type];
1534 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
1535 ptcache_file_compressed_write(pf, (unsigned char*)(extra->data), in_len, out, pid->cache->compression);
1539 ptcache_file_write(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
1544 ptcache_file_close(pf);
1546 if (error && G.f & G_DEBUG)
1547 printf("Error writing to disk cache\n");
1552 static int ptcache_read_stream(PTCacheID *pid, int cfra)
1554 PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
1557 if(pid->read_stream == NULL)
1562 printf("Error opening disk cache file for reading\n");
1566 if(!ptcache_file_header_begin_read(pf))
1569 if(!error && (pf->type != pid->type || !pid->read_header(pf)))
1572 if(!error && pf->totpoint != pid->totpoint(pid->calldata, cfra))
1576 ptcache_file_pointers_init(pf);
1578 // we have stream reading here
1579 pid->read_stream(pf, pid->calldata);
1582 ptcache_file_close(pf);
1586 static int ptcache_read(PTCacheID *pid, int cfra)
1588 PTCacheMem *pm = NULL;
1592 /* get a memory cache to read from */
1593 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1594 pm = ptcache_disk_frame_to_mem(pid, cfra);
1597 pm = pid->cache->mem_cache.first;
1599 while(pm && pm->frame != cfra)
1603 /* read the cache */
1605 int totpoint = pm->totpoint;
1607 if((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
1608 totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, cfra));
1610 BKE_ptcache_mem_pointers_init(pm);
1612 for(i=0; i<totpoint; i++) {
1613 if(pm->data_types & (1<<BPHYS_DATA_INDEX))
1614 index = pm->cur[BPHYS_DATA_INDEX];
1616 pid->read_point(*index, pid->calldata, pm->cur, (float)pm->frame, NULL);
1618 BKE_ptcache_mem_pointers_incr(pm);
1621 if(pid->read_extra_data && pm->extradata.first)
1622 pid->read_extra_data(pid->calldata, pm, (float)pm->frame);
1624 /* clean up temporary memory cache */
1625 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1626 ptcache_data_free(pm);
1627 ptcache_extra_free(pm);
1634 static int ptcache_interpolate(PTCacheID *pid, float cfra, int cfra1, int cfra2)
1636 PTCacheMem *pm = NULL;
1640 /* get a memory cache to read from */
1641 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1642 pm = ptcache_disk_frame_to_mem(pid, cfra2);
1645 pm = pid->cache->mem_cache.first;
1647 while(pm && pm->frame != cfra2)
1651 /* read the cache */
1653 int totpoint = pm->totpoint;
1655 if((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
1656 totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, (int)cfra));
1658 BKE_ptcache_mem_pointers_init(pm);
1660 for(i=0; i<totpoint; i++) {
1661 if(pm->data_types & (1<<BPHYS_DATA_INDEX))
1662 index = pm->cur[BPHYS_DATA_INDEX];
1664 pid->interpolate_point(*index, pid->calldata, pm->cur, cfra, (float)cfra1, (float)cfra2, NULL);
1665 BKE_ptcache_mem_pointers_incr(pm);
1668 if(pid->interpolate_extra_data && pm->extradata.first)
1669 pid->interpolate_extra_data(pid->calldata, pm, cfra, (float)cfra1, (float)cfra2);
1671 /* clean up temporary memory cache */
1672 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1673 ptcache_data_free(pm);
1674 ptcache_extra_free(pm);
1681 /* reads cache from disk or memory */
1682 /* possible to get old or interpolated result */
1683 int BKE_ptcache_read(PTCacheID *pid, float cfra)
1685 int cfrai = (int)cfra, cfra1=0, cfra2=0;
1688 /* nothing to read to */
1689 if(pid->totpoint(pid->calldata, cfrai) == 0)
1692 if(pid->cache->flag & PTCACHE_READ_INFO) {
1693 pid->cache->flag &= ~PTCACHE_READ_INFO;
1694 ptcache_read(pid, 0);
1697 /* first check if we have the actual frame cached */
1698 if(cfra == (float)cfrai && BKE_ptcache_id_exist(pid, cfrai))
1701 /* no exact cache frame found so try to find cached frames around cfra */
1703 ptcache_find_frames_around(pid, cfrai, &cfra1, &cfra2);
1705 if(cfra1 == 0 && cfra2 == 0)
1708 /* don't read old cache if already simulated past cached frame */
1709 if(cfra1 == 0 && cfra2 && cfra2 <= pid->cache->simframe)
1711 if(cfra1 && cfra1 == cfra2)
1715 if(pid->read_stream)
1716 ptcache_read_stream(pid, cfra1);
1717 else if(pid->read_point)
1718 ptcache_read(pid, cfra1);
1722 if(pid->read_stream)
1723 ptcache_read_stream(pid, cfra2);
1724 else if(pid->read_point) {
1725 if(cfra1 && cfra2 && pid->interpolate_point)
1726 ptcache_interpolate(pid, cfra, cfra1, cfra2);
1728 ptcache_read(pid, cfra2);
1733 ret = (cfra2 ? PTCACHE_READ_INTERPOLATED : PTCACHE_READ_EXACT);
1735 ret = PTCACHE_READ_OLD;
1736 pid->cache->simframe = cfra2;
1739 if((pid->cache->flag & PTCACHE_QUICK_CACHE)==0) {
1741 /* clear invalid cache frames so that better stuff can be simulated */
1742 if(pid->cache->flag & PTCACHE_OUTDATED) {
1743 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, cfrai);
1745 else if(pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
1746 if(cfra <= pid->cache->last_exact)
1747 pid->cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
1749 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, MAX2(cfrai, pid->cache->last_exact));
1755 static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
1757 PTCacheFile *pf = NULL;
1760 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, cfra);
1762 pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, cfra);
1766 printf("Error opening disk cache file for writing\n");
1770 pf->data_types = pid->data_types;
1771 pf->totpoint = totpoint;
1772 pf->type = pid->type;
1775 if(!error && (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf)))
1778 if(!error && pid->write_stream)
1779 pid->write_stream(pf, pid->calldata);
1781 ptcache_file_close(pf);
1783 if (error && G.f & G_DEBUG)
1784 printf("Error writing to disk cache\n");
1788 static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
1790 PointCache *cache = pid->cache;
1791 PTCacheMem *pm=NULL, *pm2=NULL;
1792 int totpoint = pid->totpoint(pid->calldata, cfra);
1795 pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
1797 pm->totpoint = pid->totwrite(pid->calldata, cfra);
1798 pm->data_types = cfra ? pid->data_types : pid->info_types;
1800 ptcache_data_alloc(pm);
1801 BKE_ptcache_mem_pointers_init(pm);
1804 if(cache->flag & PTCACHE_DISK_CACHE) {
1807 while(fra >= cache->startframe && !BKE_ptcache_id_exist(pid, fra))
1810 pm2 = ptcache_disk_frame_to_mem(pid, fra);
1813 pm2 = cache->mem_cache.last;
1816 if(pid->write_point) {
1817 for(i=0; i<totpoint; i++) {
1818 int write = pid->write_point(i, pid->calldata, pm->cur, cfra);
1820 BKE_ptcache_mem_pointers_incr(pm);
1822 /* newly born particles have to be copied to previous cached frame */
1823 if(overwrite && write == 2 && pm2 && BKE_ptcache_mem_pointers_seek(i, pm2))
1824 pid->write_point(i, pid->calldata, pm2->cur, cfra);
1829 if(pid->write_extra_data)
1830 pid->write_extra_data(pid->calldata, pm, cfra);
1834 if(cache->flag & PTCACHE_DISK_CACHE) {
1835 error += !ptcache_mem_frame_to_disk(pid, pm);
1838 ptcache_data_free(pm);
1839 ptcache_extra_free(pm);
1844 error += !ptcache_mem_frame_to_disk(pid, pm2);
1845 ptcache_data_free(pm2);
1846 ptcache_extra_free(pm2);
1851 BLI_addtail(&cache->mem_cache, pm);
1856 static int ptcache_write_needed(PTCacheID *pid, int cfra, int *overwrite)
1858 PointCache *cache = pid->cache;
1859 int ofra = 0, efra = cache->endframe;
1861 /* allways start from scratch on the first frame */
1862 if(cfra && cfra == cache->startframe) {
1863 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, cfra);
1864 cache->flag &= ~PTCACHE_REDO_NEEDED;
1868 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1869 if(cfra==0 && cache->startframe > 0)
1872 /* find last cached frame */
1873 while(efra > cache->startframe && !BKE_ptcache_id_exist(pid, efra))
1876 /* find second last cached frame */
1878 while(ofra > cache->startframe && !BKE_ptcache_id_exist(pid, ofra))
1882 PTCacheMem *pm = cache->mem_cache.last;
1883 /* don't write info file in memory */
1891 ofra = (pm->prev ? pm->prev->frame : efra - cache->step);
1894 if(efra >= cache->startframe && cfra > efra) {
1895 if(ofra >= cache->startframe && efra - ofra < cache->step) {
1896 /* overwrite previous frame */
1897 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, efra);
1905 /* writes cache to disk or memory */
1906 int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
1908 PointCache *cache = pid->cache;
1909 int totpoint = pid->totpoint(pid->calldata, cfra);
1910 int overwrite = 0, error = 0;
1912 if(totpoint == 0 || (cfra ? pid->data_types == 0 : pid->info_types == 0))
1915 if(ptcache_write_needed(pid, cfra, &overwrite)==0)
1918 if(pid->write_stream) {
1919 ptcache_write_stream(pid, cfra, totpoint);
1921 else if(pid->write_point) {
1922 error += ptcache_write(pid, cfra, overwrite);
1925 /* Mark frames skipped if more than 1 frame forwards since last non-skipped frame. */
1926 if(cfra - cache->last_exact == 1 || cfra == cache->startframe) {
1927 cache->last_exact = cfra;
1928 cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
1930 /* Don't mark skipped when writing info file (frame 0) */
1932 cache->flag |= PTCACHE_FRAMES_SKIPPED;
1934 /* Update timeline cache display */
1935 if(cfra && cache->cached_frames)
1936 cache->cached_frames[cfra-cache->startframe] = 1;
1938 BKE_ptcache_update_info(pid);
1942 /* youll need to close yourself after!
1943 * mode - PTCACHE_CLEAR_ALL,
1946 /* Clears & resets */
1947 void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
1949 unsigned int len; /* store the length of the string */
1950 unsigned int sta, end;
1952 /* mode is same as fopen's modes */
1955 char path[MAX_PTCACHE_PATH];
1956 char filename[MAX_PTCACHE_FILE];
1957 char path_full[MAX_PTCACHE_FILE];
1958 char ext[MAX_PTCACHE_PATH];
1960 if(!pid || !pid->cache || pid->cache->flag & PTCACHE_BAKED)
1963 sta = pid->cache->startframe;
1964 end = pid->cache->endframe;
1966 #ifndef DURIAN_POINTCACHE_LIB_OK
1967 /* don't allow clearing for linked objects */
1972 /*if (!G.relbase_valid) return; *//* save blend file before using pointcache */
1974 /* clear all files in the temp dir with the prefix of the ID and the ".bphys" suffix */
1976 case PTCACHE_CLEAR_ALL:
1977 case PTCACHE_CLEAR_BEFORE:
1978 case PTCACHE_CLEAR_AFTER:
1979 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1980 ptcache_path(pid, path);
1982 len = ptcache_filename(pid, filename, cfra, 0, 0); /* no path */
1984 dir = opendir(path);
1988 snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, pid->stack_index);
1990 while ((de = readdir(dir)) != NULL) {
1991 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
1992 if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
1993 if (mode == PTCACHE_CLEAR_ALL) {
1994 pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
1995 BLI_join_dirfile(path_full, path, de->d_name);
1996 BLI_delete(path_full, 0, 0);
1998 /* read the number of the file */
1999 unsigned int frame, len2 = (int)strlen(de->d_name);
2002 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2003 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2006 if((mode==PTCACHE_CLEAR_BEFORE && frame < cfra) ||
2007 (mode==PTCACHE_CLEAR_AFTER && frame > cfra) ) {
2009 BLI_join_dirfile(path_full, path, de->d_name);
2010 BLI_delete(path_full, 0, 0);
2011 if(pid->cache->cached_frames && frame >=sta && frame <= end)
2012 pid->cache->cached_frames[frame-sta] = 0;
2021 if(mode == PTCACHE_CLEAR_ALL && pid->cache->cached_frames)
2022 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
2025 PTCacheMem *pm= pid->cache->mem_cache.first;
2026 PTCacheMem *link= NULL;
2028 if(mode == PTCACHE_CLEAR_ALL) {
2029 /*we want startframe if the cache starts before zero*/
2030 pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
2031 for(; pm; pm=pm->next) {
2032 ptcache_data_free(pm);
2033 ptcache_extra_free(pm);
2035 BLI_freelistN(&pid->cache->mem_cache);
2037 if(pid->cache->cached_frames)
2038 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
2041 if((mode==PTCACHE_CLEAR_BEFORE && pm->frame < cfra) ||
2042 (mode==PTCACHE_CLEAR_AFTER && pm->frame > cfra) ) {
2044 if(pid->cache->cached_frames && pm->frame >=sta && pm->frame <= end)
2045 pid->cache->cached_frames[pm->frame-sta] = 0;
2046 ptcache_data_free(pm);
2047 ptcache_extra_free(pm);
2049 BLI_freelinkN(&pid->cache->mem_cache, link);
2058 case PTCACHE_CLEAR_FRAME:
2059 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
2060 if(BKE_ptcache_id_exist(pid, cfra)) {
2061 ptcache_filename(pid, filename, cfra, 1, 1); /* no path */
2062 BLI_delete(filename, 0, 0);
2066 PTCacheMem *pm = pid->cache->mem_cache.first;
2068 for(; pm; pm=pm->next) {
2069 if(pm->frame == cfra) {
2070 ptcache_data_free(pm);
2071 ptcache_extra_free(pm);
2072 BLI_freelinkN(&pid->cache->mem_cache, pm);
2077 if(pid->cache->cached_frames && cfra>=sta && cfra<=end)
2078 pid->cache->cached_frames[cfra-sta] = 0;
2082 BKE_ptcache_update_info(pid);
2084 int BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
2089 if(cfra<pid->cache->startframe || cfra > pid->cache->endframe)
2092 if(pid->cache->cached_frames && pid->cache->cached_frames[cfra-pid->cache->startframe]==0)
2095 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
2096 char filename[MAX_PTCACHE_FILE];
2098 ptcache_filename(pid, filename, cfra, 1, 1);
2100 return BLI_exists(filename);
2103 PTCacheMem *pm = pid->cache->mem_cache.first;
2105 for(; pm; pm=pm->next) {
2112 void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
2116 float offset, time, nexttime;
2118 /* TODO: this has to be sorter out once bsystem_time gets redone, */
2119 /* now caches can handle interpolating etc. too - jahka */
2121 /* time handling for point cache:
2122 * - simulation time is scaled by result of bsystem_time
2123 * - for offsetting time only time offset is taken into account, since
2124 * that's always the same and can't be animated. a timeoffset which
2125 * varies over time is not simpe to support.
2126 * - field and motion blur offsets are currently ignored, proper solution
2127 * is probably to interpolate results from two frames for that ..
2134 time= bsystem_time(scene, ob, cfra, 0.0f);
2135 nexttime= bsystem_time(scene, ob, cfra+1.0f, 0.0f);
2137 *timescale= MAX2(nexttime - time, 0.0f);
2140 if(startframe && endframe) {
2141 *startframe= cache->startframe;
2142 *endframe= cache->endframe;
2144 // XXX ipoflag is depreceated - old animation system stuff
2145 if (/*(ob->ipoflag & OB_OFFS_PARENT) &&*/ (ob->partype & PARSLOW)==0) {
2146 offset= give_timeoffset(ob);
2148 *startframe += (int)(offset+0.5f);
2149 *endframe += (int)(offset+0.5f);
2153 /* verify cached_frames array is up to date */
2154 if(cache->cached_frames) {
2155 if(MEM_allocN_len(cache->cached_frames) != sizeof(char) * (cache->endframe-cache->startframe+1)) {
2156 MEM_freeN(cache->cached_frames);
2157 cache->cached_frames = NULL;
2161 if(cache->cached_frames==NULL && cache->endframe > cache->startframe) {
2162 unsigned int sta=cache->startframe;
2163 unsigned int end=cache->endframe;
2165 cache->cached_frames = MEM_callocN(sizeof(char) * (cache->endframe-cache->startframe+1), "cached frames array");
2167 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
2168 /* mode is same as fopen's modes */
2171 char path[MAX_PTCACHE_PATH];
2172 char filename[MAX_PTCACHE_FILE];
2173 char ext[MAX_PTCACHE_PATH];
2174 unsigned int len; /* store the length of the string */
2176 ptcache_path(pid, path);
2178 len = ptcache_filename(pid, filename, (int)cfra, 0, 0); /* no path */
2180 dir = opendir(path);
2184 snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, pid->stack_index);
2186 while ((de = readdir(dir)) != NULL) {
2187 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2188 if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2189 /* read the number of the file */
2190 unsigned int frame, len2 = (int)strlen(de->d_name);
2193 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2194 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2197 if(frame >= sta && frame <= end)
2198 cache->cached_frames[frame-sta] = 1;
2206 PTCacheMem *pm= pid->cache->mem_cache.first;
2209 if(pm->frame >= sta && pm->frame <= end)
2210 cache->cached_frames[pm->frame-sta] = 1;
2216 int BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode)
2219 int reset, clear, after;
2229 if(mode == PTCACHE_RESET_DEPSGRAPH) {
2230 if(!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) {
2231 if(cache->flag & PTCACHE_QUICK_CACHE)
2237 cache->flag |= PTCACHE_OUTDATED;
2239 else if(mode == PTCACHE_RESET_BAKED) {
2240 if(!BKE_ptcache_get_continue_physics()) {
2245 cache->flag |= PTCACHE_OUTDATED;
2247 else if(mode == PTCACHE_RESET_OUTDATED) {
2250 if(cache->flag & PTCACHE_OUTDATED && !(cache->flag & PTCACHE_BAKED)) {
2252 cache->flag &= ~PTCACHE_OUTDATED;
2257 BKE_ptcache_invalidate(cache);
2258 cache->flag &= ~PTCACHE_REDO_NEEDED;
2260 if(pid->type == PTCACHE_TYPE_CLOTH)
2261 cloth_free_modifier(pid->calldata);
2262 else if(pid->type == PTCACHE_TYPE_SOFTBODY)
2263 sbFreeSimulation(pid->calldata);
2264 else if(pid->type == PTCACHE_TYPE_PARTICLES)
2265 psys_reset(pid->calldata, PSYS_RESET_DEPSGRAPH);
2266 else if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2267 smokeModifier_reset(pid->calldata);
2268 else if(pid->type == PTCACHE_TYPE_SMOKE_HIGHRES)
2269 smokeModifier_reset_turbulence(pid->calldata);
2272 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2274 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, CFRA);
2276 return (reset || clear || after);
2278 int BKE_ptcache_object_reset(Scene *scene, Object *ob, int mode)
2281 ParticleSystem *psys;
2289 BKE_ptcache_id_from_softbody(&pid, ob, ob->soft);
2290 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2293 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
2294 /* children or just redo can be calculated without reseting anything */
2295 if(psys->recalc & PSYS_RECALC_REDO || psys->recalc & PSYS_RECALC_CHILD)
2297 /* Baked cloth hair has to be checked too, because we don't want to reset */
2298 /* particles or cloth in that case -jahka */
2299 else if(psys->clmd) {
2300 BKE_ptcache_id_from_cloth(&pid, ob, psys->clmd);
2301 if(mode == PSYS_RESET_ALL || !(psys->part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED)))
2302 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2307 if(skip == 0 && psys->part) {
2308 BKE_ptcache_id_from_particles(&pid, ob, psys);
2309 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2313 for(md=ob->modifiers.first; md; md=md->next) {
2314 if(md->type == eModifierType_Cloth) {
2315 BKE_ptcache_id_from_cloth(&pid, ob, (ClothModifierData*)md);
2316 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2318 if(md->type == eModifierType_Smoke) {
2319 SmokeModifierData *smd = (SmokeModifierData *)md;
2320 if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
2322 BKE_ptcache_id_from_smoke(&pid, ob, (SmokeModifierData*)md);
2323 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2328 if (ob->type == OB_ARMATURE)
2329 BIK_clear_cache(ob->pose);
2334 /* Use this when quitting blender, with unsaved files */
2335 void BKE_ptcache_remove(void)
2337 char path[MAX_PTCACHE_PATH];
2338 char path_full[MAX_PTCACHE_PATH];
2341 ptcache_path(NULL, path);
2343 if (BLI_exist(path)) {
2344 /* The pointcache dir exists? - remove all pointcache */
2349 dir = opendir(path);
2353 while ((de = readdir(dir)) != NULL) {
2354 if( strcmp(de->d_name, ".")==0 || strcmp(de->d_name, "..")==0) {
2356 } else if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
2357 BLI_join_dirfile(path_full, path, de->d_name);
2358 BLI_delete(path_full, 0, 0);
2360 rmdir = 0; /* unknown file, dont remove the dir */
2366 rmdir = 0; /* path dosnt exist */
2370 BLI_delete(path, 1, 0);
2374 /* Continuous Interaction */
2376 static int CONTINUE_PHYSICS = 0;
2378 void BKE_ptcache_set_continue_physics(Main *bmain, Scene *scene, int enable)
2382 if(CONTINUE_PHYSICS != enable) {
2383 CONTINUE_PHYSICS = enable;
2385 if(CONTINUE_PHYSICS == 0) {
2386 for(ob=bmain->object.first; ob; ob=ob->id.next)
2387 if(BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_OUTDATED))
2388 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2393 int BKE_ptcache_get_continue_physics()
2395 return CONTINUE_PHYSICS;
2398 /* Point Cache handling */
2400 PointCache *BKE_ptcache_add(ListBase *ptcaches)
2404 cache= MEM_callocN(sizeof(PointCache), "PointCache");
2405 cache->startframe= 1;
2406 cache->endframe= 250;
2410 BLI_addtail(ptcaches, cache);
2415 void BKE_ptcache_free_mem(ListBase *mem_cache)
2417 PTCacheMem *pm = mem_cache->first;
2420 for(; pm; pm=pm->next) {
2421 ptcache_data_free(pm);
2422 ptcache_extra_free(pm);
2425 BLI_freelistN(mem_cache);
2428 void BKE_ptcache_free(PointCache *cache)
2430 BKE_ptcache_free_mem(&cache->mem_cache);
2431 if(cache->edit && cache->free_edit)
2432 cache->free_edit(cache->edit);
2433 if(cache->cached_frames)
2434 MEM_freeN(cache->cached_frames);
2437 void BKE_ptcache_free_list(ListBase *ptcaches)
2439 PointCache *cache = ptcaches->first;
2442 BLI_remlink(ptcaches, cache);
2443 BKE_ptcache_free(cache);
2444 cache = ptcaches->first;
2448 static PointCache *ptcache_copy(PointCache *cache)
2452 ncache= MEM_dupallocN(cache);
2454 /* hmm, should these be copied over instead? */
2455 ncache->mem_cache.first = NULL;
2456 ncache->mem_cache.last = NULL;
2457 ncache->cached_frames = NULL;
2458 ncache->edit = NULL;
2461 ncache->simframe= 0;
2465 /* returns first point cache */
2466 PointCache *BKE_ptcache_copy_list(ListBase *ptcaches_new, ListBase *ptcaches_old)
2468 PointCache *cache = ptcaches_old->first;
2470 ptcaches_new->first = ptcaches_new->last = NULL;
2472 for(; cache; cache=cache->next)
2473 BLI_addtail(ptcaches_new, ptcache_copy(cache));
2475 return ptcaches_new->first;
2480 void BKE_ptcache_quick_cache_all(Main *bmain, Scene *scene)
2485 baker.break_data=NULL;
2486 baker.break_test=NULL;
2488 baker.progressbar=NULL;
2489 baker.progressend=NULL;
2490 baker.progresscontext=NULL;
2492 baker.anim_init = 0;
2495 baker.quick_step=scene->physics_settings.quick_cache_step;
2497 BKE_ptcache_bake(&baker);
2500 /* Simulation thread, no need for interlocks as data written in both threads
2501 are only unitary integers (I/O assumed to be atomic for them) */
2503 int break_operation;
2510 } ptcache_bake_data;
2512 static void *ptcache_bake_thread(void *ptr) {
2513 ptcache_bake_data *data = (ptcache_bake_data*)ptr;
2515 for(; (*data->cfra_ptr <= data->endframe) && !data->break_operation; *data->cfra_ptr+=data->step) {
2516 scene_update_for_newframe(data->main, data->scene, data->scene->lay);
2518 printf("bake: frame %d :: %d\n", (int)*data->cfra_ptr, data->endframe);
2522 data->thread_ended = TRUE;
2526 /* if bake is not given run simulations to current frame */
2527 void BKE_ptcache_bake(PTCacheBaker* baker)
2529 Main *bmain = baker->main;
2530 Scene *scene = baker->scene;
2531 Scene *sce_iter; /* SETLOOPER macro only */
2534 PTCacheID *pid = baker->pid;
2535 PointCache *cache = NULL;
2536 float frameleno = scene->r.framelen;
2538 int startframe = MAXFRAME;
2539 int bake = baker->bake;
2540 int render = baker->render;
2542 ptcache_bake_data thread_data;
2543 int progress, old_progress;
2545 thread_data.endframe = baker->anim_init ? scene->r.sfra : CFRA;
2546 thread_data.step = baker->quick_step;
2547 thread_data.cfra_ptr = &CFRA;
2548 thread_data.scene = baker->scene;
2549 thread_data.main = baker->main;
2553 /* set caches to baking mode and figure out start frame */
2555 /* cache/bake a single object */
2557 if((cache->flag & PTCACHE_BAKED)==0) {
2558 if(pid->type==PTCACHE_TYPE_PARTICLES) {
2559 ParticleSystem *psys= pid->calldata;
2561 /* a bit confusing, could make this work better in the UI */
2562 if(psys->part->type == PART_EMITTER)
2563 psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
2565 else if(pid->type == PTCACHE_TYPE_SMOKE_HIGHRES) {
2566 /* get all pids from the object and search for smoke low res */
2569 BKE_ptcache_ids_from_object(&pidlist2, pid->ob, scene, MAX_DUPLI_RECUR);
2570 for(pid2=pidlist2.first; pid2; pid2=pid2->next) {
2571 if(pid2->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2573 if(pid2->cache && !(pid2->cache->flag & PTCACHE_BAKED)) {
2574 if(bake || pid2->cache->flag & PTCACHE_REDO_NEEDED)
2575 BKE_ptcache_id_clear(pid2, PTCACHE_CLEAR_ALL, 0);
2577 pid2->cache->flag |= PTCACHE_BAKING;
2578 pid2->cache->flag &= ~PTCACHE_BAKED;
2583 BLI_freelistN(&pidlist2);
2586 if(bake || cache->flag & PTCACHE_REDO_NEEDED)
2587 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2589 startframe = MAX2(cache->last_exact, cache->startframe);
2592 thread_data.endframe = cache->endframe;
2593 cache->flag |= PTCACHE_BAKING;
2596 thread_data.endframe = MIN2(thread_data.endframe, cache->endframe);
2599 cache->flag &= ~PTCACHE_BAKED;
2602 else for(SETLOOPER(scene, sce_iter, base)) {
2603 /* cache/bake everything in the scene */
2604 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
2606 for(pid=pidlist.first; pid; pid=pid->next) {
2608 if((cache->flag & PTCACHE_BAKED)==0) {
2609 if(pid->type==PTCACHE_TYPE_PARTICLES) {
2610 ParticleSystem *psys = (ParticleSystem*)pid->calldata;
2611 /* skip hair & keyed particles */
2612 if(psys->part->type == PART_HAIR || psys->part->phystype == PART_PHYS_KEYED)
2615 psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
2618 if((cache->flag & PTCACHE_REDO_NEEDED || (cache->flag & PTCACHE_SIMULATION_VALID)==0)
2619 && ((cache->flag & PTCACHE_QUICK_CACHE)==0 || render || bake))
2620 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2622 startframe = MIN2(startframe, cache->startframe);
2624 if(bake || render) {
2625 cache->flag |= PTCACHE_BAKING;
2628 thread_data.endframe = MAX2(thread_data.endframe, cache->endframe);
2631 cache->flag &= ~PTCACHE_BAKED;
2635 BLI_freelistN(&pidlist);
2639 scene->r.framelen = 1.0;
2640 thread_data.break_operation = FALSE;
2641 thread_data.thread_ended = FALSE;
2647 ptcache_bake_thread((void*)&thread_data);
2650 BLI_init_threads(&threads, ptcache_bake_thread, 1);
2651 BLI_insert_thread(&threads, (void*)&thread_data);
2653 while (thread_data.thread_ended == FALSE) {
2656 progress = (int)(100.0f * (float)(CFRA - startframe)/(float)(thread_data.endframe-startframe));
2660 /* NOTE: baking should not redraw whole ui as this slows things down */
2661 if ((baker->progressbar) && (progress != old_progress)) {
2662 baker->progressbar(baker->progresscontext, progress);
2663 old_progress = progress;
2666 /* Delay to lessen CPU load from UI thread */
2669 /* NOTE: breaking baking should leave calculated frames in cache, not clear it */
2670 if(blender_test_break() && !thread_data.break_operation) {
2671 thread_data.break_operation = TRUE;
2672 if (baker->progressend)
2673 baker->progressend(baker->progresscontext);
2678 BLI_end_threads(&threads);
2680 /* clear baking flag */
2682 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
2683 cache->flag |= PTCACHE_SIMULATION_VALID;
2685 cache->flag |= PTCACHE_BAKED;
2686 /* write info file */
2687 if(cache->flag & PTCACHE_DISK_CACHE)
2688 BKE_ptcache_write(pid, 0);
2691 else for(SETLOOPER(scene, sce_iter, base)) {
2692 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
2694 for(pid=pidlist.first; pid; pid=pid->next) {
2695 /* skip hair particles */
2696 if(pid->type==PTCACHE_TYPE_PARTICLES && ((ParticleSystem*)pid->calldata)->part->type == PART_HAIR)
2701 if(thread_data.step > 1)
2702 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_OUTDATED);
2704 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
2706 cache->flag |= PTCACHE_SIMULATION_VALID;
2709 cache->flag |= PTCACHE_BAKED;
2710 if(cache->flag & PTCACHE_DISK_CACHE)
2711 BKE_ptcache_write(pid, 0);
2714 BLI_freelistN(&pidlist);
2717 scene->r.framelen = frameleno;
2720 if(bake) /* already on cfra unless baking */
2721 scene_update_for_newframe(bmain, scene, scene->lay);
2723 if (thread_data.break_operation)
2725 else if (baker->progressend)
2726 baker->progressend(baker->progresscontext);
2730 /* TODO: call redraw all windows somehow */
2733 void BKE_ptcache_disk_to_mem(PTCacheID *pid)
2735 PointCache *cache = pid->cache;
2736 PTCacheMem *pm = NULL;
2737 int baked = cache->flag & PTCACHE_BAKED;
2738 int cfra, sfra = cache->startframe, efra = cache->endframe;
2740 /* Remove possible bake flag to allow clear */
2741 cache->flag &= ~PTCACHE_BAKED;
2743 /* PTCACHE_DISK_CACHE flag was cleared already */
2744 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2746 /* restore possible bake flag */
2747 cache->flag |= baked;
2749 for(cfra=sfra; cfra <= efra; cfra++) {
2750 pm = ptcache_disk_frame_to_mem(pid, cfra);
2753 BLI_addtail(&pid->cache->mem_cache, pm);
2756 void BKE_ptcache_mem_to_disk(PTCacheID *pid)
2758 PointCache *cache = pid->cache;
2759 PTCacheMem *pm = cache->mem_cache.first;
2760 int baked = cache->flag & PTCACHE_BAKED;
2762 /* Remove possible bake flag to allow clear */
2763 cache->flag &= ~PTCACHE_BAKED;
2765 /* PTCACHE_DISK_CACHE flag was set already */
2766 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2768 /* restore possible bake flag */
2769 cache->flag |= baked;
2771 for(; pm; pm=pm->next) {
2772 if(ptcache_mem_frame_to_disk(pid, pm)==0) {
2773 cache->flag &= ~PTCACHE_DISK_CACHE;
2778 /* write info file */
2779 if(cache->flag & PTCACHE_BAKED)
2780 BKE_ptcache_write(pid, 0);
2782 void BKE_ptcache_toggle_disk_cache(PTCacheID *pid)
2784 PointCache *cache = pid->cache;
2785 int last_exact = cache->last_exact;
2787 if (!G.relbase_valid){
2788 cache->flag &= ~PTCACHE_DISK_CACHE;
2790 printf("File must be saved before using disk cache!\n");
2794 if(cache->cached_frames) {
2795 MEM_freeN(cache->cached_frames);
2796 cache->cached_frames=NULL;
2799 if(cache->flag & PTCACHE_DISK_CACHE)
2800 BKE_ptcache_mem_to_disk(pid);
2802 BKE_ptcache_disk_to_mem(pid);
2804 cache->flag ^= PTCACHE_DISK_CACHE;
2805 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2806 cache->flag ^= PTCACHE_DISK_CACHE;
2808 cache->last_exact = last_exact;
2810 BKE_ptcache_id_time(pid, NULL, 0.0f, NULL, NULL, NULL);
2812 BKE_ptcache_update_info(pid);
2815 void BKE_ptcache_disk_cache_rename(PTCacheID *pid, char *from, char *to)
2818 int len; /* store the length of the string */
2819 /* mode is same as fopen's modes */
2822 char path[MAX_PTCACHE_PATH];
2823 char old_filename[MAX_PTCACHE_FILE];
2824 char new_path_full[MAX_PTCACHE_FILE];
2825 char old_path_full[MAX_PTCACHE_FILE];
2826 char ext[MAX_PTCACHE_PATH];
2829 strcpy(old_name, pid->cache->name);
2831 /* get "from" filename */
2832 strcpy(pid->cache->name, from);
2834 len = ptcache_filename(pid, old_filename, 0, 0, 0); /* no path */
2836 ptcache_path(pid, path);
2837 dir = opendir(path);
2839 strcpy(pid->cache->name, old_name);
2843 snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, pid->stack_index);
2845 /* put new name into cache */
2846 strcpy(pid->cache->name, to);
2848 while ((de = readdir(dir)) != NULL) {
2849 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2850 if (strncmp(old_filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2851 /* read the number of the file */
2852 int frame, len2 = (int)strlen(de->d_name);
2855 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2856 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2859 BLI_join_dirfile(old_path_full, path, de->d_name);
2860 ptcache_filename(pid, new_path_full, frame, 1, 1);
2861 BLI_rename(old_path_full, new_path_full);
2868 strcpy(pid->cache->name, old_name);
2871 void BKE_ptcache_load_external(PTCacheID *pid)
2874 PointCache *cache = pid->cache;
2875 int len; /* store the length of the string */
2877 int start = MAXFRAME;
2880 /* mode is same as fopen's modes */
2883 char path[MAX_PTCACHE_PATH];
2884 char filename[MAX_PTCACHE_FILE];
2885 char ext[MAX_PTCACHE_PATH];
2890 ptcache_path(pid, path);
2892 len = ptcache_filename(pid, filename, 1, 0, 0); /* no path */
2894 dir = opendir(path);
2898 if(cache->index >= 0)
2899 snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, cache->index);
2901 strcpy(ext, PTCACHE_EXT);
2903 while ((de = readdir(dir)) != NULL) {
2904 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2905 if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2906 /* read the number of the file */
2907 int frame, len2 = (int)strlen(de->d_name);
2910 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2911 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2915 start = MIN2(start, frame);
2916 end = MAX2(end, frame);
2926 if(start != MAXFRAME) {
2929 cache->startframe = start;
2930 cache->endframe = end;
2931 cache->totpoint = 0;
2933 if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2934 ; /*necessary info in every file*/
2935 /* read totpoint from info file (frame 0) */
2937 pf= ptcache_file_open(pid, PTCACHE_FILE_READ, 0);
2940 if(ptcache_file_header_begin_read(pf)) {
2941 if(pf->type == pid->type && pid->read_header(pf)) {
2942 cache->totpoint = pf->totpoint;
2943 cache->flag |= PTCACHE_READ_INFO;
2946 cache->totpoint = 0;
2949 ptcache_file_close(pf);
2952 /* or from any old format cache file */
2955 int elemsize = ptcache_old_elemsize(pid);
2956 pf= ptcache_file_open(pid, PTCACHE_FILE_READ, cache->startframe);
2959 while(ptcache_file_read(pf, old_data, 1, elemsize))
2962 ptcache_file_close(pf);
2965 cache->flag |= (PTCACHE_BAKED|PTCACHE_DISK_CACHE|PTCACHE_SIMULATION_VALID);
2966 cache->flag &= ~(PTCACHE_OUTDATED|PTCACHE_FRAMES_SKIPPED);
2969 BKE_ptcache_update_info(pid);
2972 void BKE_ptcache_update_info(PTCacheID *pid)
2974 PointCache *cache = pid->cache;
2975 PTCacheExtra *extra = NULL;
2979 if(cache->flag & PTCACHE_EXTERNAL) {
2980 int cfra = cache->startframe;
2982 for(; cfra<=cache->endframe; cfra++) {
2983 if(BKE_ptcache_id_exist(pid, cfra))
2987 /* smoke doesn't use frame 0 as info frame so can't check based on totpoint */
2988 if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN && totframes)
2989 sprintf(cache->info, "%i frames found!", totframes);
2990 else if(totframes && cache->totpoint)
2991 sprintf(cache->info, "%i points found!", cache->totpoint);
2993 sprintf(cache->info, "No valid data to read!");
2997 if(cache->flag & PTCACHE_DISK_CACHE) {
2998 if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
3000 int totpoint = pid->totpoint(pid->calldata, 0);
3002 if(cache->totpoint > totpoint)
3003 sprintf(mem_info, "%i cells + High Resolution cached", totpoint);
3005 sprintf(mem_info, "%i cells cached", totpoint);
3008 int cfra = cache->startframe;
3010 for(; cfra<=cache->endframe; cfra++) {
3011 if(BKE_ptcache_id_exist(pid, cfra))
3015 sprintf(mem_info, "%i frames on disk", totframes);
3019 PTCacheMem *pm = cache->mem_cache.first;
3023 for(; pm; pm=pm->next) {
3024 for(i=0; i<BPHYS_TOT_DATA; i++)
3025 bytes += MEM_allocN_len(pm->data[i]);
3027 for(extra=pm->extradata.first; extra; extra=extra->next) {
3028 bytes += MEM_allocN_len(extra->data);
3029 bytes += sizeof(PTCacheExtra);
3032 bytes += sizeof(PTCacheMem);
3037 mb = (bytes > 1024.0f * 1024.0f);
3039 sprintf(mem_info, "%i frames in memory (%.1f %s)",
3041 bytes / (mb ? 1024.0f * 1024.0f : 1024.0f),
3045 if(cache->flag & PTCACHE_OUTDATED) {
3046 sprintf(cache->info, "%s, cache is outdated!", mem_info);
3048 else if(cache->flag & PTCACHE_FRAMES_SKIPPED) {
3049 sprintf(cache->info, "%s, not exact since frame %i.", mem_info, cache->last_exact);
3052 sprintf(cache->info, "%s.", mem_info);
3055 void BKE_ptcache_validate(PointCache *cache, int framenr)
3058 cache->flag |= PTCACHE_SIMULATION_VALID;
3059 cache->simframe = framenr;
3062 void BKE_ptcache_invalidate(PointCache *cache)
3065 cache->flag &= ~PTCACHE_SIMULATION_VALID;
3066 cache->simframe = 0;
3067 cache->last_exact = MIN2(cache->startframe, 0);