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;
519 /* Smoke functions */
520 static int ptcache_smoke_totpoint(void *smoke_v, int UNUSED(cfra))
522 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
523 SmokeDomainSettings *sds = smd->domain;
526 return sds->res[0]*sds->res[1]*sds->res[2];
531 static int ptcache_smoke_write(PTCacheFile *pf, void *smoke_v)
533 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
534 SmokeDomainSettings *sds = smd->domain;
538 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
539 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
540 unsigned char *obstacles;
541 unsigned int in_len = sizeof(float)*(unsigned int)res;
542 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
543 //int mode = res >= 1000000 ? 2 : 1;
545 if (sds->cache_comp == SM_CACHE_HEAVY) mode=2; // heavy
547 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
549 ptcache_file_compressed_write(pf, (unsigned char *)sds->shadow, in_len, out, mode);
550 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len, out, mode);
551 ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len, out, mode);
552 ptcache_file_compressed_write(pf, (unsigned char *)heat, in_len, out, mode);
553 ptcache_file_compressed_write(pf, (unsigned char *)heatold, in_len, out, mode);
554 ptcache_file_compressed_write(pf, (unsigned char *)vx, in_len, out, mode);
555 ptcache_file_compressed_write(pf, (unsigned char *)vy, in_len, out, mode);
556 ptcache_file_compressed_write(pf, (unsigned char *)vz, in_len, out, mode);
557 ptcache_file_compressed_write(pf, (unsigned char *)vxold, in_len, out, mode);
558 ptcache_file_compressed_write(pf, (unsigned char *)vyold, in_len, out, mode);
559 ptcache_file_compressed_write(pf, (unsigned char *)vzold, in_len, out, mode);
560 ptcache_file_compressed_write(pf, (unsigned char *)obstacles, (unsigned int)res, out, mode);
561 ptcache_file_write(pf, &dt, 1, sizeof(float));
562 ptcache_file_write(pf, &dx, 1, sizeof(float));
570 int res_big_array[3];
572 int res = sds->res[0]*sds->res[1]*sds->res[2];
573 float *dens, *densold, *tcu, *tcv, *tcw;
574 unsigned int in_len = sizeof(float)*(unsigned int)res;
575 unsigned int in_len_big;
579 smoke_turbulence_get_res(sds->wt, res_big_array);
580 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
581 //mode = res_big >= 1000000 ? 2 : 1;
583 if (sds->cache_high_comp == SM_CACHE_HEAVY) mode=2; // heavy
585 in_len_big = sizeof(float) * (unsigned int)res_big;
587 smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
589 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len_big), "pointcache_lzo_buffer");
590 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len_big, out, mode);
591 ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len_big, out, mode);
594 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
595 ptcache_file_compressed_write(pf, (unsigned char *)tcu, in_len, out, mode);
596 ptcache_file_compressed_write(pf, (unsigned char *)tcv, in_len, out, mode);
597 ptcache_file_compressed_write(pf, (unsigned char *)tcw, in_len, out, mode);
605 static void ptcache_smoke_read(PTCacheFile *pf, void *smoke_v)
607 SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
608 SmokeDomainSettings *sds = smd->domain;
611 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
612 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
613 unsigned char *obstacles;
614 unsigned int out_len = (unsigned int)res * sizeof(float);
616 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
618 ptcache_file_compressed_read(pf, (unsigned char *)sds->shadow, out_len);
619 ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len);
620 ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len);
621 ptcache_file_compressed_read(pf, (unsigned char*)heat, out_len);
622 ptcache_file_compressed_read(pf, (unsigned char*)heatold, out_len);
623 ptcache_file_compressed_read(pf, (unsigned char*)vx, out_len);
624 ptcache_file_compressed_read(pf, (unsigned char*)vy, out_len);
625 ptcache_file_compressed_read(pf, (unsigned char*)vz, out_len);
626 ptcache_file_compressed_read(pf, (unsigned char*)vxold, out_len);
627 ptcache_file_compressed_read(pf, (unsigned char*)vyold, out_len);
628 ptcache_file_compressed_read(pf, (unsigned char*)vzold, out_len);
629 ptcache_file_compressed_read(pf, (unsigned char*)obstacles, (unsigned int)res);
630 ptcache_file_read(pf, &dt, 1, sizeof(float));
631 ptcache_file_read(pf, &dx, 1, sizeof(float));
633 if(pf->data_types & (1<<BPHYS_DATA_SMOKE_HIGH) && sds->wt) {
634 int res = sds->res[0]*sds->res[1]*sds->res[2];
635 int res_big, res_big_array[3];
636 float *dens, *densold, *tcu, *tcv, *tcw;
637 unsigned int out_len = sizeof(float)*(unsigned int)res;
638 unsigned int out_len_big;
640 smoke_turbulence_get_res(sds->wt, res_big_array);
641 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
642 out_len_big = sizeof(float) * (unsigned int)res_big;
644 smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
646 ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len_big);
647 ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len_big);
649 ptcache_file_compressed_read(pf, (unsigned char*)tcu, out_len);
650 ptcache_file_compressed_read(pf, (unsigned char*)tcv, out_len);
651 ptcache_file_compressed_read(pf, (unsigned char*)tcw, out_len);
657 void BKE_ptcache_id_from_softbody(PTCacheID *pid, Object *ob, SoftBody *sb)
659 memset(pid, 0, sizeof(PTCacheID));
663 pid->type= PTCACHE_TYPE_SOFTBODY;
664 pid->cache= sb->pointcache;
665 pid->cache_ptr= &sb->pointcache;
666 pid->ptcaches= &sb->ptcaches;
667 pid->totpoint= pid->totwrite= ptcache_softbody_totpoint;
669 pid->write_point = ptcache_softbody_write;
670 pid->read_point = ptcache_softbody_read;
671 pid->interpolate_point = ptcache_softbody_interpolate;
673 pid->write_stream = NULL;
674 pid->read_stream = NULL;
676 pid->write_extra_data = NULL;
677 pid->read_extra_data = NULL;
678 pid->interpolate_extra_data = NULL;
680 pid->write_header = ptcache_basic_header_write;
681 pid->read_header = ptcache_basic_header_read;
683 pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY);
686 pid->stack_index = pid->cache->index;
688 void BKE_ptcache_id_from_particles(PTCacheID *pid, Object *ob, ParticleSystem *psys)
690 memset(pid, 0, sizeof(PTCacheID));
694 pid->type= PTCACHE_TYPE_PARTICLES;
695 pid->stack_index= psys->pointcache->index;
696 pid->cache= psys->pointcache;
697 pid->cache_ptr= &psys->pointcache;
698 pid->ptcaches= &psys->ptcaches;
700 if(psys->part->type != PART_HAIR)
701 pid->flag |= PTCACHE_VEL_PER_SEC;
703 pid->totpoint = ptcache_particle_totpoint;
704 pid->totwrite = ptcache_particle_totwrite;
706 pid->write_point = ptcache_particle_write;
707 pid->read_point = ptcache_particle_read;
708 pid->interpolate_point = ptcache_particle_interpolate;
710 pid->write_stream = NULL;
711 pid->read_stream = NULL;
713 pid->write_extra_data = NULL;
714 pid->read_extra_data = NULL;
715 pid->interpolate_extra_data = NULL;
717 pid->write_header = ptcache_basic_header_write;
718 pid->read_header = ptcache_basic_header_read;
720 pid->data_types = (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_INDEX);
722 if(psys->part->phystype == PART_PHYS_BOIDS)
723 pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION) | (1<<BPHYS_DATA_BOIDS);
724 else if(psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS) {
725 pid->write_extra_data = ptcache_particle_extra_write;
726 pid->read_extra_data = ptcache_particle_extra_read;
729 if(psys->part->rotmode!=PART_ROT_VEL
730 || psys->part->avemode!=PART_AVE_SPIN || psys->part->avefac!=0.0f)
731 pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION);
733 if(psys->part->flag & PART_ROT_DYN)
734 pid->data_types|= (1<<BPHYS_DATA_ROTATION);
736 pid->info_types= (1<<BPHYS_DATA_TIMES);
738 void BKE_ptcache_id_from_cloth(PTCacheID *pid, Object *ob, ClothModifierData *clmd)
740 memset(pid, 0, sizeof(PTCacheID));
744 pid->type= PTCACHE_TYPE_CLOTH;
745 pid->stack_index= clmd->point_cache->index;
746 pid->cache= clmd->point_cache;
747 pid->cache_ptr= &clmd->point_cache;
748 pid->ptcaches= &clmd->ptcaches;
749 pid->totpoint= pid->totwrite= ptcache_cloth_totpoint;
751 pid->write_point = ptcache_cloth_write;
752 pid->read_point = ptcache_cloth_read;
753 pid->interpolate_point = ptcache_cloth_interpolate;
755 pid->write_stream = NULL;
756 pid->read_stream = NULL;
758 pid->write_extra_data = NULL;
759 pid->read_extra_data = NULL;
760 pid->interpolate_extra_data = NULL;
762 pid->write_header = ptcache_basic_header_write;
763 pid->read_header = ptcache_basic_header_read;
765 pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_XCONST);
768 void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct SmokeModifierData *smd)
770 SmokeDomainSettings *sds = smd->domain;
772 memset(pid, 0, sizeof(PTCacheID));
777 pid->type= PTCACHE_TYPE_SMOKE_DOMAIN;
778 pid->stack_index= sds->point_cache[0]->index;
780 pid->cache= sds->point_cache[0];
781 pid->cache_ptr= &(sds->point_cache[0]);
782 pid->ptcaches= &(sds->ptcaches[0]);
784 pid->totpoint= pid->totwrite= ptcache_smoke_totpoint;
786 pid->write_point = NULL;
787 pid->read_point = NULL;
788 pid->interpolate_point = NULL;
790 pid->read_stream = ptcache_smoke_read;
791 pid->write_stream = ptcache_smoke_write;
793 pid->write_extra_data = NULL;
794 pid->read_extra_data = NULL;
795 pid->interpolate_extra_data = NULL;
797 pid->write_header = ptcache_basic_header_write;
798 pid->read_header = ptcache_basic_header_read;
804 pid->data_types |= (1<<BPHYS_DATA_SMOKE_LOW);
806 pid->data_types |= (1<<BPHYS_DATA_SMOKE_HIGH);
808 void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int duplis)
811 ParticleSystem *psys;
814 lb->first= lb->last= NULL;
817 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
818 BKE_ptcache_id_from_softbody(pid, ob, ob->soft);
819 BLI_addtail(lb, pid);
822 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
826 /* check to make sure point cache is actually used by the particles */
827 if(ELEM(psys->part->phystype, PART_PHYS_NO, PART_PHYS_KEYED))
830 /* hair needs to be included in id-list for cache edit mode to work */
831 /* if(psys->part->type == PART_HAIR && (psys->flag & PSYS_HAIR_DYNAMICS)==0) */
834 if(psys->part->type == PART_FLUID)
837 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
838 BKE_ptcache_id_from_particles(pid, ob, psys);
839 BLI_addtail(lb, pid);
842 for(md=ob->modifiers.first; md; md=md->next) {
843 if(md->type == eModifierType_Cloth) {
844 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
845 BKE_ptcache_id_from_cloth(pid, ob, (ClothModifierData*)md);
846 BLI_addtail(lb, pid);
848 if(md->type == eModifierType_Smoke) {
849 SmokeModifierData *smd = (SmokeModifierData *)md;
850 if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
852 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
853 BKE_ptcache_id_from_smoke(pid, ob, (SmokeModifierData*)md);
854 BLI_addtail(lb, pid);
859 if(scene && (duplis-- > 0) && (ob->transflag & OB_DUPLI)) {
860 ListBase *lb_dupli_ob;
862 if((lb_dupli_ob=object_duplilist(scene, ob))) {
864 for(dob= lb_dupli_ob->first; dob; dob= dob->next) {
865 if(dob->ob != ob) { /* avoids recursive loops with dupliframes: bug 22988 */
866 ListBase lb_dupli_pid;
867 BKE_ptcache_ids_from_object(&lb_dupli_pid, dob->ob, scene, duplis);
868 BLI_movelisttolist(lb, &lb_dupli_pid);
869 if(lb_dupli_pid.first)
870 printf("Adding Dupli\n");
874 free_object_duplilist(lb_dupli_ob); /* does restore */
881 /* Takes an Object ID and returns a unique name
883 - cfra: frame for the cache, can be negative
884 - stack_index: index in the modifier stack. we can have cache for more then one stack_index
887 #define MAX_PTCACHE_PATH FILE_MAX
888 #define MAX_PTCACHE_FILE ((FILE_MAXDIR+FILE_MAXFILE)*2)
890 static int ptcache_path(PTCacheID *pid, char *filename)
892 Library *lib= (pid->ob)? pid->ob->id.lib: NULL;
893 const char *blendfilename= (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH)==0) ? lib->filepath: G.main->name;
896 if(pid->cache->flag & PTCACHE_EXTERNAL) {
897 strcpy(filename, pid->cache->path);
899 if(strncmp(filename, "//", 2)==0)
900 BLI_path_abs(filename, blendfilename);
902 return BLI_add_slash(filename); /* new strlen() */
904 else if (G.relbase_valid || lib) {
905 char file[MAX_PTCACHE_PATH]; /* we dont want the dir, only the file */
907 BLI_split_dirfile(blendfilename, NULL, file);
914 snprintf(filename, MAX_PTCACHE_PATH, "//"PTCACHE_PATH"%s", file); /* add blend file name to pointcache dir */
915 BLI_path_abs(filename, blendfilename);
916 return BLI_add_slash(filename); /* new strlen() */
919 /* use the temp path. this is weak but better then not using point cache at all */
920 /* btempdir is assumed to exist and ALWAYS has a trailing slash */
921 snprintf(filename, MAX_PTCACHE_PATH, "%s"PTCACHE_PATH"%d", btempdir, abs(getpid()));
923 return BLI_add_slash(filename); /* new strlen() */
926 static int ptcache_filename(PTCacheID *pid, char *filename, int cfra, short do_path, short do_ext)
934 if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return 0; /* save blend file before using disk pointcache */
936 /* start with temp dir */
938 len = ptcache_path(pid, filename);
941 if(pid->cache->name[0] == '\0' && (pid->cache->flag & PTCACHE_EXTERNAL)==0) {
942 idname = (pid->ob->id.name+2);
943 /* convert chars to hex so they are always a valid filename */
944 while('\0' != *idname) {
945 snprintf(newname, MAX_PTCACHE_FILE, "%02X", (char)(*idname++));
951 int temp = (int)strlen(pid->cache->name);
952 strcpy(newname, pid->cache->name);
959 if(pid->cache->index < 0)
960 pid->cache->index = pid->stack_index = object_insert_ptcache(pid->ob);
962 if(pid->cache->flag & PTCACHE_EXTERNAL) {
963 if(pid->cache->index >= 0)
964 snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02d"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
966 snprintf(newname, MAX_PTCACHE_FILE, "_%06d"PTCACHE_EXT, cfra); /* always 6 chars */
969 snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02d"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
974 return len; /* make sure the above string is always 16 chars */
977 /* youll need to close yourself after! */
978 static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
982 char filename[(FILE_MAXDIR+FILE_MAXFILE)*2];
984 #ifndef DURIAN_POINTCACHE_LIB_OK
985 /* don't allow writing for linked objects */
986 if(pid->ob->id.lib && mode == PTCACHE_FILE_WRITE)
989 if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return NULL; /* save blend file before using disk pointcache */
991 ptcache_filename(pid, filename, cfra, 1, 1);
993 if (mode==PTCACHE_FILE_READ) {
994 if (!BLI_exists(filename)) {
997 fp = fopen(filename, "rb");
998 } else if (mode==PTCACHE_FILE_WRITE) {
999 BLI_make_existing_file(filename); /* will create the dir if needs be, same as //textures is created */
1000 fp = fopen(filename, "wb");
1001 } else if (mode==PTCACHE_FILE_UPDATE) {
1002 BLI_make_existing_file(filename);
1003 fp = fopen(filename, "rb+");
1009 pf= MEM_mallocN(sizeof(PTCacheFile), "PTCacheFile");
1016 static void ptcache_file_close(PTCacheFile *pf)
1024 static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result, unsigned int len)
1027 unsigned char compressed = 0;
1030 size_t out_len = len;
1033 unsigned char *props = MEM_callocN(16*sizeof(char), "tmp");
1035 ptcache_file_read(pf, &compressed, 1, sizeof(unsigned char));
1038 ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
1039 in_len = (size_t)size;
1044 in = (unsigned char *)MEM_callocN(sizeof(unsigned char)*in_len, "pointcache_compressed_buffer");
1045 ptcache_file_read(pf, in, in_len, sizeof(unsigned char));
1048 r = lzo1x_decompress_safe(in, (lzo_uint)in_len, result, (lzo_uint *)&out_len, NULL);
1054 size_t leni = in_len, leno = out_len;
1055 ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
1056 sizeOfIt = (size_t)size;
1057 ptcache_file_read(pf, props, sizeOfIt, sizeof(unsigned char));
1058 r = LzmaUncompress(result, &leno, in, &leni, props, sizeOfIt);
1065 ptcache_file_read(pf, result, len, sizeof(unsigned char));
1072 static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, unsigned int in_len, unsigned char *out, int mode)
1075 unsigned char compressed = 0;
1077 unsigned char *props = MEM_callocN(16*sizeof(char), "tmp");
1078 size_t sizeOfIt = 5;
1080 (void)mode; /* unused when building w/o compression */
1083 out_len= LZO_OUT_LEN(in_len);
1085 LZO_HEAP_ALLOC(wrkmem, LZO1X_MEM_COMPRESS);
1087 r = lzo1x_1_compress(in, (lzo_uint)in_len, out, (lzo_uint *)&out_len, wrkmem);
1088 if (!(r == LZO_E_OK) || (out_len >= in_len))
1097 r = LzmaCompress(out, &out_len, in, in_len,//assume sizeof(char)==1....
1098 props, &sizeOfIt, 5, 1 << 24, 3, 0, 2, 32, 2);
1100 if(!(r == SZ_OK) || (out_len >= in_len))
1107 ptcache_file_write(pf, &compressed, 1, sizeof(unsigned char));
1109 unsigned int size = out_len;
1110 ptcache_file_write(pf, &size, 1, sizeof(unsigned int));
1111 ptcache_file_write(pf, out, out_len, sizeof(unsigned char));
1114 ptcache_file_write(pf, in, in_len, sizeof(unsigned char));
1118 unsigned int size = sizeOfIt;
1119 ptcache_file_write(pf, &sizeOfIt, 1, sizeof(unsigned int));
1120 ptcache_file_write(pf, props, size, sizeof(unsigned char));
1127 static int ptcache_file_read(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size)
1129 return (fread(f, size, tot, pf->fp) == tot);
1131 static int ptcache_file_write(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size)
1133 return (fwrite(f, size, tot, pf->fp) == tot);
1135 static int ptcache_file_data_read(PTCacheFile *pf)
1139 for(i=0; i<BPHYS_TOT_DATA; i++) {
1140 if((pf->data_types & (1<<i)) && !ptcache_file_read(pf, pf->cur[i], 1, ptcache_data_size[i]))
1146 static int ptcache_file_data_write(PTCacheFile *pf)
1150 for(i=0; i<BPHYS_TOT_DATA; i++) {
1151 if((pf->data_types & (1<<i)) && !ptcache_file_write(pf, pf->cur[i], 1, ptcache_data_size[i]))
1157 static int ptcache_file_header_begin_read(PTCacheFile *pf)
1159 unsigned int typeflag=0;
1165 if(fread(bphysics, sizeof(char), 8, pf->fp) != 8)
1168 if(!error && strncmp(bphysics, "BPHYSICS", 8))
1171 if(!error && !fread(&typeflag, sizeof(unsigned int), 1, pf->fp))
1174 pf->type = (typeflag & PTCACHE_TYPEFLAG_TYPEMASK);
1175 pf->flag = (typeflag & PTCACHE_TYPEFLAG_FLAGMASK);
1177 /* if there was an error set file as it was */
1179 fseek(pf->fp, 0, SEEK_SET);
1183 static int ptcache_file_header_begin_write(PTCacheFile *pf)
1185 const char *bphysics = "BPHYSICS";
1186 unsigned int typeflag = pf->type + pf->flag;
1188 if(fwrite(bphysics, sizeof(char), 8, pf->fp) != 8)
1191 if(!fwrite(&typeflag, sizeof(unsigned int), 1, pf->fp))
1197 /* Data pointer handling */
1198 int BKE_ptcache_data_size(int data_type)
1200 return ptcache_data_size[data_type];
1203 static void ptcache_file_pointers_init(PTCacheFile *pf)
1205 int data_types = pf->data_types;
1207 pf->cur[BPHYS_DATA_INDEX] = (data_types & (1<<BPHYS_DATA_INDEX)) ? &pf->data.index : NULL;
1208 pf->cur[BPHYS_DATA_LOCATION] = (data_types & (1<<BPHYS_DATA_LOCATION)) ? &pf->data.loc : NULL;
1209 pf->cur[BPHYS_DATA_VELOCITY] = (data_types & (1<<BPHYS_DATA_VELOCITY)) ? &pf->data.vel : NULL;
1210 pf->cur[BPHYS_DATA_ROTATION] = (data_types & (1<<BPHYS_DATA_ROTATION)) ? &pf->data.rot : NULL;
1211 pf->cur[BPHYS_DATA_AVELOCITY] = (data_types & (1<<BPHYS_DATA_AVELOCITY))? &pf->data.ave : NULL;
1212 pf->cur[BPHYS_DATA_SIZE] = (data_types & (1<<BPHYS_DATA_SIZE)) ? &pf->data.size : NULL;
1213 pf->cur[BPHYS_DATA_TIMES] = (data_types & (1<<BPHYS_DATA_TIMES)) ? &pf->data.times : NULL;
1214 pf->cur[BPHYS_DATA_BOIDS] = (data_types & (1<<BPHYS_DATA_BOIDS)) ? &pf->data.boids : NULL;
1217 /* Check to see if point number "index" is in pm, uses binary search for index data. */
1218 int BKE_ptcache_mem_index_find(PTCacheMem *pm, unsigned int index)
1220 if(pm->data[BPHYS_DATA_INDEX]) {
1221 unsigned int *data = pm->data[BPHYS_DATA_INDEX];
1222 unsigned int mid, low = 0, high = pm->totpoint - 1;
1224 if(index < *data || index > *(data+high))
1227 /* check simple case for continuous indexes first */
1228 if(index-*data < high && data[index-*data] == index)
1231 while(low <= high) {
1232 mid= (low + high)/2;
1234 if(data[mid] > index)
1236 else if(data[mid] < index)
1245 return (index < pm->totpoint ? index : -1);
1249 void BKE_ptcache_mem_pointers_init(PTCacheMem *pm)
1251 int data_types = pm->data_types;
1254 for(i=0; i<BPHYS_TOT_DATA; i++)
1255 pm->cur[i] = ((data_types & (1<<i)) ? pm->data[i] : NULL);
1258 void BKE_ptcache_mem_pointers_incr(PTCacheMem *pm)
1262 for(i=0; i<BPHYS_TOT_DATA; i++) {
1264 pm->cur[i] = (char*)pm->cur[i] + ptcache_data_size[i];
1267 int BKE_ptcache_mem_pointers_seek(int point_index, PTCacheMem *pm)
1269 int data_types = pm->data_types;
1270 int i, index = BKE_ptcache_mem_index_find(pm, point_index);
1273 /* Can't give proper location without reallocation, so don't give any location.
1274 * Some points will be cached improperly, but this only happens with simulation
1275 * steps bigger than cache->step, so the cache has to be recalculated anyways
1281 for(i=0; i<BPHYS_TOT_DATA; i++)
1282 pm->cur[i] = data_types & (1<<i) ? (char*)pm->data[i] + index * ptcache_data_size[i] : NULL;
1286 static void ptcache_data_alloc(PTCacheMem *pm)
1288 int data_types = pm->data_types;
1289 int totpoint = pm->totpoint;
1292 for(i=0; i<BPHYS_TOT_DATA; i++) {
1293 if(data_types & (1<<i))
1294 pm->data[i] = MEM_callocN(totpoint * ptcache_data_size[i], "PTCache Data");
1297 static void ptcache_data_free(PTCacheMem *pm)
1299 void **data = pm->data;
1302 for(i=0; i<BPHYS_TOT_DATA; i++) {
1307 static void ptcache_data_copy(void *from[], void *to[])
1310 for(i=0; i<BPHYS_TOT_DATA; i++) {
1311 /* note, durian file 03.4b_comp crashes if to[i] is not tested
1312 * its NULL, not sure if this should be fixed elsewhere but for now its needed */
1313 if(from[i] && to[i])
1314 memcpy(to[i], from[i], ptcache_data_size[i]);
1318 static void ptcache_extra_free(PTCacheMem *pm)
1320 PTCacheExtra *extra = pm->extradata.first;
1323 for(; extra; extra=extra->next) {
1325 MEM_freeN(extra->data);
1328 BLI_freelistN(&pm->extradata);
1331 static int ptcache_old_elemsize(PTCacheID *pid)
1333 if(pid->type==PTCACHE_TYPE_SOFTBODY)
1334 return 6 * sizeof(float);
1335 else if(pid->type==PTCACHE_TYPE_PARTICLES)
1336 return sizeof(ParticleKey);
1337 else if(pid->type==PTCACHE_TYPE_CLOTH)
1338 return 9 * sizeof(float);
1343 static void ptcache_find_frames_around(PTCacheID *pid, unsigned int frame, int *fra1, int *fra2)
1345 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1346 int cfra1=frame-1, cfra2=frame+1;
1348 while(cfra1 >= pid->cache->startframe && !BKE_ptcache_id_exist(pid, cfra1))
1351 if(cfra1 < pid->cache->startframe)
1354 while(cfra2 <= pid->cache->endframe && !BKE_ptcache_id_exist(pid, cfra2))
1357 if(cfra2 > pid->cache->endframe)
1360 if(cfra1 && !cfra2) {
1369 else if(pid->cache->mem_cache.first) {
1370 PTCacheMem *pm = pid->cache->mem_cache.first;
1371 PTCacheMem *pm2 = pid->cache->mem_cache.last;
1373 while(pm->next && pm->next->frame < frame)
1376 if(pm2->frame < frame) {
1380 while(pm2->prev && pm2->prev->frame > frame) {
1396 static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
1398 PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
1399 PTCacheMem *pm = NULL;
1400 unsigned int i, error = 0;
1405 if(!ptcache_file_header_begin_read(pf))
1408 if(!error && (pf->type != pid->type || !pid->read_header(pf)))
1412 pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
1414 pm->totpoint = pf->totpoint;
1415 pm->data_types = pf->data_types;
1416 pm->frame = pf->frame;
1418 ptcache_data_alloc(pm);
1420 if(pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
1421 for(i=0; i<BPHYS_TOT_DATA; i++) {
1422 unsigned int out_len = pm->totpoint*ptcache_data_size[i];
1423 if(pf->data_types & (1<<i))
1424 ptcache_file_compressed_read(pf, (unsigned char*)(pm->data[i]), out_len);
1428 BKE_ptcache_mem_pointers_init(pm);
1429 ptcache_file_pointers_init(pf);
1431 for(i=0; i<pm->totpoint; i++) {
1432 if(!ptcache_file_data_read(pf)) {
1436 ptcache_data_copy(pf->cur, pm->cur);
1437 BKE_ptcache_mem_pointers_incr(pm);
1442 if(!error && pf->flag & PTCACHE_TYPEFLAG_EXTRADATA) {
1443 unsigned int extratype = 0;
1445 while(ptcache_file_read(pf, &extratype, 1, sizeof(unsigned int))) {
1446 PTCacheExtra *extra = MEM_callocN(sizeof(PTCacheExtra), "Pointcache extradata");
1448 extra->type = extratype;
1450 ptcache_file_read(pf, &extra->totdata, 1, sizeof(unsigned int));
1452 extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Pointcache extradata->data");
1454 if(pf->flag & PTCACHE_TYPEFLAG_COMPRESS)
1455 ptcache_file_compressed_read(pf, (unsigned char*)(extra->data), extra->totdata*ptcache_extra_datasize[extra->type]);
1457 ptcache_file_read(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
1459 BLI_addtail(&pm->extradata, extra);
1464 ptcache_data_free(pm);
1465 ptcache_extra_free(pm);
1470 ptcache_file_close(pf);
1472 if (error && G.f & G_DEBUG)
1473 printf("Error reading from disk cache\n");
1477 static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
1479 PTCacheFile *pf = NULL;
1480 unsigned int i, error = 0;
1482 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, pm->frame);
1484 pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, pm->frame);
1488 printf("Error opening disk cache file for writing\n");
1492 pf->data_types = pm->data_types;
1493 pf->totpoint = pm->totpoint;
1494 pf->type = pid->type;
1497 if(pm->extradata.first)
1498 pf->flag |= PTCACHE_TYPEFLAG_EXTRADATA;
1500 if(pid->cache->compression)
1501 pf->flag |= PTCACHE_TYPEFLAG_COMPRESS;
1503 if(!ptcache_file_header_begin_write(pf) || !pid->write_header(pf))
1507 if(pid->cache->compression) {
1508 for(i=0; i<BPHYS_TOT_DATA; i++) {
1510 unsigned int in_len = pm->totpoint*ptcache_data_size[i];
1511 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
1512 ptcache_file_compressed_write(pf, (unsigned char*)(pm->data[i]), in_len, out, pid->cache->compression);
1518 BKE_ptcache_mem_pointers_init(pm);
1519 ptcache_file_pointers_init(pf);
1521 for(i=0; i<pm->totpoint; i++) {
1522 ptcache_data_copy(pm->cur, pf->cur);
1523 if(!ptcache_file_data_write(pf)) {
1527 BKE_ptcache_mem_pointers_incr(pm);
1532 if(!error && pm->extradata.first) {
1533 PTCacheExtra *extra = pm->extradata.first;
1535 for(; extra; extra=extra->next) {
1536 if(extra->data == NULL || extra->totdata == 0)
1539 ptcache_file_write(pf, &extra->type, 1, sizeof(unsigned int));
1540 ptcache_file_write(pf, &extra->totdata, 1, sizeof(unsigned int));
1542 if(pid->cache->compression) {
1543 unsigned int in_len = extra->totdata * ptcache_extra_datasize[extra->type];
1544 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
1545 ptcache_file_compressed_write(pf, (unsigned char*)(extra->data), in_len, out, pid->cache->compression);
1549 ptcache_file_write(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
1554 ptcache_file_close(pf);
1556 if (error && G.f & G_DEBUG)
1557 printf("Error writing to disk cache\n");
1562 static int ptcache_read_stream(PTCacheID *pid, int cfra)
1564 PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
1567 if(pid->read_stream == NULL)
1572 printf("Error opening disk cache file for reading\n");
1576 if(!ptcache_file_header_begin_read(pf))
1579 if(!error && (pf->type != pid->type || !pid->read_header(pf)))
1582 if(!error && pf->totpoint != pid->totpoint(pid->calldata, cfra))
1586 ptcache_file_pointers_init(pf);
1588 // we have stream reading here
1589 pid->read_stream(pf, pid->calldata);
1592 ptcache_file_close(pf);
1596 static int ptcache_read(PTCacheID *pid, int cfra)
1598 PTCacheMem *pm = NULL;
1602 /* get a memory cache to read from */
1603 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1604 pm = ptcache_disk_frame_to_mem(pid, cfra);
1607 pm = pid->cache->mem_cache.first;
1609 while(pm && pm->frame != cfra)
1613 /* read the cache */
1615 int totpoint = pm->totpoint;
1617 if((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
1618 totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, cfra));
1620 BKE_ptcache_mem_pointers_init(pm);
1622 for(i=0; i<totpoint; i++) {
1623 if(pm->data_types & (1<<BPHYS_DATA_INDEX))
1624 index = pm->cur[BPHYS_DATA_INDEX];
1626 pid->read_point(*index, pid->calldata, pm->cur, (float)pm->frame, NULL);
1628 BKE_ptcache_mem_pointers_incr(pm);
1631 if(pid->read_extra_data && pm->extradata.first)
1632 pid->read_extra_data(pid->calldata, pm, (float)pm->frame);
1634 /* clean up temporary memory cache */
1635 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1636 ptcache_data_free(pm);
1637 ptcache_extra_free(pm);
1644 static int ptcache_interpolate(PTCacheID *pid, float cfra, int cfra1, int cfra2)
1646 PTCacheMem *pm = NULL;
1650 /* get a memory cache to read from */
1651 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1652 pm = ptcache_disk_frame_to_mem(pid, cfra2);
1655 pm = pid->cache->mem_cache.first;
1657 while(pm && pm->frame != cfra2)
1661 /* read the cache */
1663 int totpoint = pm->totpoint;
1665 if((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
1666 totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, (int)cfra));
1668 BKE_ptcache_mem_pointers_init(pm);
1670 for(i=0; i<totpoint; i++) {
1671 if(pm->data_types & (1<<BPHYS_DATA_INDEX))
1672 index = pm->cur[BPHYS_DATA_INDEX];
1674 pid->interpolate_point(*index, pid->calldata, pm->cur, cfra, (float)cfra1, (float)cfra2, NULL);
1675 BKE_ptcache_mem_pointers_incr(pm);
1678 if(pid->interpolate_extra_data && pm->extradata.first)
1679 pid->interpolate_extra_data(pid->calldata, pm, cfra, (float)cfra1, (float)cfra2);
1681 /* clean up temporary memory cache */
1682 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1683 ptcache_data_free(pm);
1684 ptcache_extra_free(pm);
1691 /* reads cache from disk or memory */
1692 /* possible to get old or interpolated result */
1693 int BKE_ptcache_read(PTCacheID *pid, float cfra)
1695 int cfrai = (int)cfra, cfra1=0, cfra2=0;
1698 /* nothing to read to */
1699 if(pid->totpoint(pid->calldata, cfrai) == 0)
1702 if(pid->cache->flag & PTCACHE_READ_INFO) {
1703 pid->cache->flag &= ~PTCACHE_READ_INFO;
1704 ptcache_read(pid, 0);
1707 /* first check if we have the actual frame cached */
1708 if(cfra == (float)cfrai && BKE_ptcache_id_exist(pid, cfrai))
1711 /* no exact cache frame found so try to find cached frames around cfra */
1713 ptcache_find_frames_around(pid, cfrai, &cfra1, &cfra2);
1715 if(cfra1 == 0 && cfra2 == 0)
1718 /* don't read old cache if already simulated past cached frame */
1719 if(cfra1 == 0 && cfra2 && cfra2 <= pid->cache->simframe)
1721 if(cfra1 && cfra1 == cfra2)
1725 if(pid->read_stream)
1726 ptcache_read_stream(pid, cfra1);
1727 else if(pid->read_point)
1728 ptcache_read(pid, cfra1);
1732 if(pid->read_stream)
1733 ptcache_read_stream(pid, cfra2);
1734 else if(pid->read_point) {
1735 if(cfra1 && cfra2 && pid->interpolate_point)
1736 ptcache_interpolate(pid, cfra, cfra1, cfra2);
1738 ptcache_read(pid, cfra2);
1743 ret = (cfra2 ? PTCACHE_READ_INTERPOLATED : PTCACHE_READ_EXACT);
1745 ret = PTCACHE_READ_OLD;
1746 pid->cache->simframe = cfra2;
1749 if((pid->cache->flag & PTCACHE_QUICK_CACHE)==0) {
1751 /* clear invalid cache frames so that better stuff can be simulated */
1752 if(pid->cache->flag & PTCACHE_OUTDATED) {
1753 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, cfrai);
1755 else if(pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
1756 if(cfra <= pid->cache->last_exact)
1757 pid->cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
1759 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, MAX2(cfrai, pid->cache->last_exact));
1765 static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
1767 PTCacheFile *pf = NULL;
1770 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, cfra);
1772 pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, cfra);
1776 printf("Error opening disk cache file for writing\n");
1780 pf->data_types = pid->data_types;
1781 pf->totpoint = totpoint;
1782 pf->type = pid->type;
1785 if(!error && (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf)))
1788 if(!error && pid->write_stream)
1789 pid->write_stream(pf, pid->calldata);
1791 ptcache_file_close(pf);
1793 if (error && G.f & G_DEBUG)
1794 printf("Error writing to disk cache\n");
1798 static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
1800 PointCache *cache = pid->cache;
1801 PTCacheMem *pm=NULL, *pm2=NULL;
1802 int totpoint = pid->totpoint(pid->calldata, cfra);
1805 pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
1807 pm->totpoint = pid->totwrite(pid->calldata, cfra);
1808 pm->data_types = cfra ? pid->data_types : pid->info_types;
1810 ptcache_data_alloc(pm);
1811 BKE_ptcache_mem_pointers_init(pm);
1814 if(cache->flag & PTCACHE_DISK_CACHE) {
1817 while(fra >= cache->startframe && !BKE_ptcache_id_exist(pid, fra))
1820 pm2 = ptcache_disk_frame_to_mem(pid, fra);
1823 pm2 = cache->mem_cache.last;
1826 if(pid->write_point) {
1827 for(i=0; i<totpoint; i++) {
1828 int write = pid->write_point(i, pid->calldata, pm->cur, cfra);
1830 BKE_ptcache_mem_pointers_incr(pm);
1832 /* newly born particles have to be copied to previous cached frame */
1833 if(overwrite && write == 2 && pm2 && BKE_ptcache_mem_pointers_seek(i, pm2))
1834 pid->write_point(i, pid->calldata, pm2->cur, cfra);
1839 if(pid->write_extra_data)
1840 pid->write_extra_data(pid->calldata, pm, cfra);
1844 if(cache->flag & PTCACHE_DISK_CACHE) {
1845 error += !ptcache_mem_frame_to_disk(pid, pm);
1847 // if(pm) /* pm is always set */
1849 ptcache_data_free(pm);
1850 ptcache_extra_free(pm);
1855 error += !ptcache_mem_frame_to_disk(pid, pm2);
1856 ptcache_data_free(pm2);
1857 ptcache_extra_free(pm2);
1862 BLI_addtail(&cache->mem_cache, pm);
1867 static int ptcache_write_needed(PTCacheID *pid, int cfra, int *overwrite)
1869 PointCache *cache = pid->cache;
1870 int ofra = 0, efra = cache->endframe;
1872 /* allways start from scratch on the first frame */
1873 if(cfra && cfra == cache->startframe) {
1874 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, cfra);
1875 cache->flag &= ~PTCACHE_REDO_NEEDED;
1879 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1880 if(cfra==0 && cache->startframe > 0)
1883 /* find last cached frame */
1884 while(efra > cache->startframe && !BKE_ptcache_id_exist(pid, efra))
1887 /* find second last cached frame */
1889 while(ofra > cache->startframe && !BKE_ptcache_id_exist(pid, ofra))
1893 PTCacheMem *pm = cache->mem_cache.last;
1894 /* don't write info file in memory */
1902 ofra = (pm->prev ? pm->prev->frame : efra - cache->step);
1905 if(efra >= cache->startframe && cfra > efra) {
1906 if(ofra >= cache->startframe && efra - ofra < cache->step) {
1907 /* overwrite previous frame */
1908 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, efra);
1916 /* writes cache to disk or memory */
1917 int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
1919 PointCache *cache = pid->cache;
1920 int totpoint = pid->totpoint(pid->calldata, cfra);
1921 int overwrite = 0, error = 0;
1923 if(totpoint == 0 || (cfra ? pid->data_types == 0 : pid->info_types == 0))
1926 if(ptcache_write_needed(pid, cfra, &overwrite)==0)
1929 if(pid->write_stream) {
1930 ptcache_write_stream(pid, cfra, totpoint);
1932 else if(pid->write_point) {
1933 error += ptcache_write(pid, cfra, overwrite);
1936 /* Mark frames skipped if more than 1 frame forwards since last non-skipped frame. */
1937 if(cfra - cache->last_exact == 1 || cfra == cache->startframe) {
1938 cache->last_exact = cfra;
1939 cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
1941 /* Don't mark skipped when writing info file (frame 0) */
1943 cache->flag |= PTCACHE_FRAMES_SKIPPED;
1945 /* Update timeline cache display */
1946 if(cfra && cache->cached_frames)
1947 cache->cached_frames[cfra-cache->startframe] = 1;
1949 BKE_ptcache_update_info(pid);
1953 /* youll need to close yourself after!
1954 * mode - PTCACHE_CLEAR_ALL,
1957 /* Clears & resets */
1958 void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
1960 unsigned int len; /* store the length of the string */
1961 unsigned int sta, end;
1963 /* mode is same as fopen's modes */
1966 char path[MAX_PTCACHE_PATH];
1967 char filename[MAX_PTCACHE_FILE];
1968 char path_full[MAX_PTCACHE_FILE];
1969 char ext[MAX_PTCACHE_PATH];
1971 if(!pid || !pid->cache || pid->cache->flag & PTCACHE_BAKED)
1974 sta = pid->cache->startframe;
1975 end = pid->cache->endframe;
1977 #ifndef DURIAN_POINTCACHE_LIB_OK
1978 /* don't allow clearing for linked objects */
1983 /*if (!G.relbase_valid) return; *//* save blend file before using pointcache */
1985 /* clear all files in the temp dir with the prefix of the ID and the ".bphys" suffix */
1987 case PTCACHE_CLEAR_ALL:
1988 case PTCACHE_CLEAR_BEFORE:
1989 case PTCACHE_CLEAR_AFTER:
1990 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1991 ptcache_path(pid, path);
1993 len = ptcache_filename(pid, filename, cfra, 0, 0); /* no path */
1995 dir = opendir(path);
1999 snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, pid->stack_index);
2001 while ((de = readdir(dir)) != NULL) {
2002 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2003 if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2004 if (mode == PTCACHE_CLEAR_ALL) {
2005 pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
2006 BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
2007 BLI_delete(path_full, 0, 0);
2009 /* read the number of the file */
2010 unsigned int frame, len2 = (int)strlen(de->d_name);
2013 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2014 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2017 if((mode==PTCACHE_CLEAR_BEFORE && frame < cfra) ||
2018 (mode==PTCACHE_CLEAR_AFTER && frame > cfra) ) {
2020 BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
2021 BLI_delete(path_full, 0, 0);
2022 if(pid->cache->cached_frames && frame >=sta && frame <= end)
2023 pid->cache->cached_frames[frame-sta] = 0;
2032 if(mode == PTCACHE_CLEAR_ALL && pid->cache->cached_frames)
2033 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
2036 PTCacheMem *pm= pid->cache->mem_cache.first;
2037 PTCacheMem *link= NULL;
2039 if(mode == PTCACHE_CLEAR_ALL) {
2040 /*we want startframe if the cache starts before zero*/
2041 pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
2042 for(; pm; pm=pm->next) {
2043 ptcache_data_free(pm);
2044 ptcache_extra_free(pm);
2046 BLI_freelistN(&pid->cache->mem_cache);
2048 if(pid->cache->cached_frames)
2049 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
2052 if((mode==PTCACHE_CLEAR_BEFORE && pm->frame < cfra) ||
2053 (mode==PTCACHE_CLEAR_AFTER && pm->frame > cfra) ) {
2055 if(pid->cache->cached_frames && pm->frame >=sta && pm->frame <= end)
2056 pid->cache->cached_frames[pm->frame-sta] = 0;
2057 ptcache_data_free(pm);
2058 ptcache_extra_free(pm);
2060 BLI_freelinkN(&pid->cache->mem_cache, link);
2069 case PTCACHE_CLEAR_FRAME:
2070 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
2071 if(BKE_ptcache_id_exist(pid, cfra)) {
2072 ptcache_filename(pid, filename, cfra, 1, 1); /* no path */
2073 BLI_delete(filename, 0, 0);
2077 PTCacheMem *pm = pid->cache->mem_cache.first;
2079 for(; pm; pm=pm->next) {
2080 if(pm->frame == cfra) {
2081 ptcache_data_free(pm);
2082 ptcache_extra_free(pm);
2083 BLI_freelinkN(&pid->cache->mem_cache, pm);
2088 if(pid->cache->cached_frames && cfra>=sta && cfra<=end)
2089 pid->cache->cached_frames[cfra-sta] = 0;
2093 BKE_ptcache_update_info(pid);
2095 int BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
2100 if(cfra<pid->cache->startframe || cfra > pid->cache->endframe)
2103 if(pid->cache->cached_frames && pid->cache->cached_frames[cfra-pid->cache->startframe]==0)
2106 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
2107 char filename[MAX_PTCACHE_FILE];
2109 ptcache_filename(pid, filename, cfra, 1, 1);
2111 return BLI_exists(filename);
2114 PTCacheMem *pm = pid->cache->mem_cache.first;
2116 for(; pm; pm=pm->next) {
2123 void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
2127 /* float offset; unused for now */
2128 float time, nexttime;
2130 /* TODO: this has to be sorter out once bsystem_time gets redone, */
2131 /* now caches can handle interpolating etc. too - jahka */
2133 /* time handling for point cache:
2134 * - simulation time is scaled by result of bsystem_time
2135 * - for offsetting time only time offset is taken into account, since
2136 * that's always the same and can't be animated. a timeoffset which
2137 * varies over time is not simpe to support.
2138 * - field and motion blur offsets are currently ignored, proper solution
2139 * is probably to interpolate results from two frames for that ..
2146 time= bsystem_time(scene, ob, cfra, 0.0f);
2147 nexttime= bsystem_time(scene, ob, cfra+1.0f, 0.0f);
2149 *timescale= MAX2(nexttime - time, 0.0f);
2152 if(startframe && endframe) {
2153 *startframe= cache->startframe;
2154 *endframe= cache->endframe;
2156 /* TODO: time handling with object offsets and simulated vs. cached
2157 * particles isn't particularly easy, so for now what you see is what
2158 * you get. In the future point cache could handle the whole particle
2161 if ((ob->partype & PARSLOW)==0) {
2162 offset= give_timeoffset(ob);
2164 *startframe += (int)(offset+0.5f);
2165 *endframe += (int)(offset+0.5f);
2170 /* verify cached_frames array is up to date */
2171 if(cache->cached_frames) {
2172 if(MEM_allocN_len(cache->cached_frames) != sizeof(char) * (cache->endframe-cache->startframe+1)) {
2173 MEM_freeN(cache->cached_frames);
2174 cache->cached_frames = NULL;
2178 if(cache->cached_frames==NULL && cache->endframe > cache->startframe) {
2179 unsigned int sta=cache->startframe;
2180 unsigned int end=cache->endframe;
2182 cache->cached_frames = MEM_callocN(sizeof(char) * (cache->endframe-cache->startframe+1), "cached frames array");
2184 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
2185 /* mode is same as fopen's modes */
2188 char path[MAX_PTCACHE_PATH];
2189 char filename[MAX_PTCACHE_FILE];
2190 char ext[MAX_PTCACHE_PATH];
2191 unsigned int len; /* store the length of the string */
2193 ptcache_path(pid, path);
2195 len = ptcache_filename(pid, filename, (int)cfra, 0, 0); /* no path */
2197 dir = opendir(path);
2201 snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, pid->stack_index);
2203 while ((de = readdir(dir)) != NULL) {
2204 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2205 if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2206 /* read the number of the file */
2207 unsigned int frame, len2 = (int)strlen(de->d_name);
2210 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2211 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2214 if(frame >= sta && frame <= end)
2215 cache->cached_frames[frame-sta] = 1;
2223 PTCacheMem *pm= pid->cache->mem_cache.first;
2226 if(pm->frame >= sta && pm->frame <= end)
2227 cache->cached_frames[pm->frame-sta] = 1;
2233 int BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode)
2236 int reset, clear, after;
2246 if(mode == PTCACHE_RESET_DEPSGRAPH) {
2247 if(!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) {
2248 if(cache->flag & PTCACHE_QUICK_CACHE)
2254 cache->flag |= PTCACHE_OUTDATED;
2256 else if(mode == PTCACHE_RESET_BAKED) {
2257 if(!BKE_ptcache_get_continue_physics()) {
2262 cache->flag |= PTCACHE_OUTDATED;
2264 else if(mode == PTCACHE_RESET_OUTDATED) {
2267 if(cache->flag & PTCACHE_OUTDATED && !(cache->flag & PTCACHE_BAKED)) {
2269 cache->flag &= ~PTCACHE_OUTDATED;
2274 BKE_ptcache_invalidate(cache);
2275 cache->flag &= ~PTCACHE_REDO_NEEDED;
2277 if(pid->type == PTCACHE_TYPE_CLOTH)
2278 cloth_free_modifier(pid->calldata);
2279 else if(pid->type == PTCACHE_TYPE_SOFTBODY)
2280 sbFreeSimulation(pid->calldata);
2281 else if(pid->type == PTCACHE_TYPE_PARTICLES)
2282 psys_reset(pid->calldata, PSYS_RESET_DEPSGRAPH);
2283 else if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2284 smokeModifier_reset(pid->calldata);
2285 else if(pid->type == PTCACHE_TYPE_SMOKE_HIGHRES)
2286 smokeModifier_reset_turbulence(pid->calldata);
2289 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2291 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, CFRA);
2293 return (reset || clear || after);
2295 int BKE_ptcache_object_reset(Scene *scene, Object *ob, int mode)
2298 ParticleSystem *psys;
2306 BKE_ptcache_id_from_softbody(&pid, ob, ob->soft);
2307 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2310 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
2311 /* children or just redo can be calculated without reseting anything */
2312 if(psys->recalc & PSYS_RECALC_REDO || psys->recalc & PSYS_RECALC_CHILD)
2314 /* Baked cloth hair has to be checked too, because we don't want to reset */
2315 /* particles or cloth in that case -jahka */
2316 else if(psys->clmd) {
2317 BKE_ptcache_id_from_cloth(&pid, ob, psys->clmd);
2318 if(mode == PSYS_RESET_ALL || !(psys->part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED)))
2319 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2324 if(skip == 0 && psys->part) {
2325 BKE_ptcache_id_from_particles(&pid, ob, psys);
2326 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2330 for(md=ob->modifiers.first; md; md=md->next) {
2331 if(md->type == eModifierType_Cloth) {
2332 BKE_ptcache_id_from_cloth(&pid, ob, (ClothModifierData*)md);
2333 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2335 if(md->type == eModifierType_Smoke) {
2336 SmokeModifierData *smd = (SmokeModifierData *)md;
2337 if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
2339 BKE_ptcache_id_from_smoke(&pid, ob, (SmokeModifierData*)md);
2340 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2345 if (ob->type == OB_ARMATURE)
2346 BIK_clear_cache(ob->pose);
2351 /* Use this when quitting blender, with unsaved files */
2352 void BKE_ptcache_remove(void)
2354 char path[MAX_PTCACHE_PATH];
2355 char path_full[MAX_PTCACHE_PATH];
2358 ptcache_path(NULL, path);
2360 if (BLI_exist(path)) {
2361 /* The pointcache dir exists? - remove all pointcache */
2366 dir = opendir(path);
2370 while ((de = readdir(dir)) != NULL) {
2371 if( strcmp(de->d_name, ".")==0 || strcmp(de->d_name, "..")==0) {
2373 } else if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
2374 BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
2375 BLI_delete(path_full, 0, 0);
2377 rmdir = 0; /* unknown file, dont remove the dir */
2383 rmdir = 0; /* path dosnt exist */
2387 BLI_delete(path, 1, 0);
2391 /* Continuous Interaction */
2393 static int CONTINUE_PHYSICS = 0;
2395 void BKE_ptcache_set_continue_physics(Main *bmain, Scene *scene, int enable)
2399 if(CONTINUE_PHYSICS != enable) {
2400 CONTINUE_PHYSICS = enable;
2402 if(CONTINUE_PHYSICS == 0) {
2403 for(ob=bmain->object.first; ob; ob=ob->id.next)
2404 if(BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_OUTDATED))
2405 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2410 int BKE_ptcache_get_continue_physics(void)
2412 return CONTINUE_PHYSICS;
2415 /* Point Cache handling */
2417 PointCache *BKE_ptcache_add(ListBase *ptcaches)
2421 cache= MEM_callocN(sizeof(PointCache), "PointCache");
2422 cache->startframe= 1;
2423 cache->endframe= 250;
2427 BLI_addtail(ptcaches, cache);
2432 void BKE_ptcache_free_mem(ListBase *mem_cache)
2434 PTCacheMem *pm = mem_cache->first;
2437 for(; pm; pm=pm->next) {
2438 ptcache_data_free(pm);
2439 ptcache_extra_free(pm);
2442 BLI_freelistN(mem_cache);
2445 void BKE_ptcache_free(PointCache *cache)
2447 BKE_ptcache_free_mem(&cache->mem_cache);
2448 if(cache->edit && cache->free_edit)
2449 cache->free_edit(cache->edit);
2450 if(cache->cached_frames)
2451 MEM_freeN(cache->cached_frames);
2454 void BKE_ptcache_free_list(ListBase *ptcaches)
2456 PointCache *cache = ptcaches->first;
2459 BLI_remlink(ptcaches, cache);
2460 BKE_ptcache_free(cache);
2461 cache = ptcaches->first;
2465 static PointCache *ptcache_copy(PointCache *cache)
2469 ncache= MEM_dupallocN(cache);
2471 /* hmm, should these be copied over instead? */
2472 ncache->mem_cache.first = NULL;
2473 ncache->mem_cache.last = NULL;
2474 ncache->cached_frames = NULL;
2475 ncache->edit = NULL;
2478 ncache->simframe= 0;
2482 /* returns first point cache */
2483 PointCache *BKE_ptcache_copy_list(ListBase *ptcaches_new, ListBase *ptcaches_old)
2485 PointCache *cache = ptcaches_old->first;
2487 ptcaches_new->first = ptcaches_new->last = NULL;
2489 for(; cache; cache=cache->next)
2490 BLI_addtail(ptcaches_new, ptcache_copy(cache));
2492 return ptcaches_new->first;
2497 void BKE_ptcache_quick_cache_all(Main *bmain, Scene *scene)
2502 baker.break_data=NULL;
2503 baker.break_test=NULL;
2505 baker.progressbar=NULL;
2506 baker.progressend=NULL;
2507 baker.progresscontext=NULL;
2509 baker.anim_init = 0;
2512 baker.quick_step=scene->physics_settings.quick_cache_step;
2514 BKE_ptcache_bake(&baker);
2517 /* Simulation thread, no need for interlocks as data written in both threads
2518 are only unitary integers (I/O assumed to be atomic for them) */
2520 int break_operation;
2527 } ptcache_bake_data;
2529 static void ptcache_dt_to_str(char *str, double dtime)
2533 sprintf(str, "%ih %im %is", (int)(dtime/3600), ((int)(dtime/60))%60, ((int)dtime) % 60);
2535 sprintf(str, "%im %is", ((int)(dtime/60))%60, ((int)dtime) % 60);
2538 sprintf(str, "%is", ((int)dtime) % 60);
2541 static void *ptcache_bake_thread(void *ptr) {
2542 int usetimer = 0, sfra, efra;
2543 double stime, ptime, ctime, fetd;
2544 char run[32], cur[32], etd[32];
2546 ptcache_bake_data *data = (ptcache_bake_data*)ptr;
2548 stime = ptime = PIL_check_seconds_timer();
2549 sfra = *data->cfra_ptr;
2550 efra = data->endframe;
2552 for(; (*data->cfra_ptr <= data->endframe) && !data->break_operation; *data->cfra_ptr+=data->step) {
2553 scene_update_for_newframe(data->main, data->scene, data->scene->lay);
2555 printf("bake: frame %d :: %d\n", (int)*data->cfra_ptr, data->endframe);
2558 ctime = PIL_check_seconds_timer();
2560 fetd = (ctime-ptime)*(efra-*data->cfra_ptr)/data->step;
2562 if(usetimer || fetd > 60.0) {
2565 ptcache_dt_to_str(cur, ctime-ptime);
2566 ptcache_dt_to_str(run, ctime-stime);
2567 ptcache_dt_to_str(etd, fetd);
2569 printf("Baked for %s, current frame: %i/%i (%.3fs), ETC: %s \r", run, *data->cfra_ptr-sfra+1, efra-sfra+1, ctime-ptime, etd);
2576 ptcache_dt_to_str(run, PIL_check_seconds_timer()-stime);
2577 printf("Bake %s %s (%i frames simulated). \n", (data->break_operation ? "canceled after" : "finished in"), run, *data->cfra_ptr-sfra);
2580 data->thread_ended = TRUE;
2584 /* if bake is not given run simulations to current frame */
2585 void BKE_ptcache_bake(PTCacheBaker* baker)
2587 Main *bmain = baker->main;
2588 Scene *scene = baker->scene;
2589 Scene *sce_iter; /* SETLOOPER macro only */
2592 PTCacheID *pid = baker->pid;
2593 PointCache *cache = NULL;
2594 float frameleno = scene->r.framelen;
2596 int startframe = MAXFRAME;
2597 int bake = baker->bake;
2598 int render = baker->render;
2600 ptcache_bake_data thread_data;
2601 int progress, old_progress;
2603 thread_data.endframe = baker->anim_init ? scene->r.sfra : CFRA;
2604 thread_data.step = baker->quick_step;
2605 thread_data.cfra_ptr = &CFRA;
2606 thread_data.scene = baker->scene;
2607 thread_data.main = baker->main;
2611 /* set caches to baking mode and figure out start frame */
2613 /* cache/bake a single object */
2615 if((cache->flag & PTCACHE_BAKED)==0) {
2616 if(pid->type==PTCACHE_TYPE_PARTICLES) {
2617 ParticleSystem *psys= pid->calldata;
2619 /* a bit confusing, could make this work better in the UI */
2620 if(psys->part->type == PART_EMITTER)
2621 psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
2623 else if(pid->type == PTCACHE_TYPE_SMOKE_HIGHRES) {
2624 /* get all pids from the object and search for smoke low res */
2627 BKE_ptcache_ids_from_object(&pidlist2, pid->ob, scene, MAX_DUPLI_RECUR);
2628 for(pid2=pidlist2.first; pid2; pid2=pid2->next) {
2629 if(pid2->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2631 if(pid2->cache && !(pid2->cache->flag & PTCACHE_BAKED)) {
2632 if(bake || pid2->cache->flag & PTCACHE_REDO_NEEDED)
2633 BKE_ptcache_id_clear(pid2, PTCACHE_CLEAR_ALL, 0);
2635 pid2->cache->flag |= PTCACHE_BAKING;
2636 pid2->cache->flag &= ~PTCACHE_BAKED;
2641 BLI_freelistN(&pidlist2);
2644 if(bake || cache->flag & PTCACHE_REDO_NEEDED)
2645 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2647 startframe = MAX2(cache->last_exact, cache->startframe);
2650 thread_data.endframe = cache->endframe;
2651 cache->flag |= PTCACHE_BAKING;
2654 thread_data.endframe = MIN2(thread_data.endframe, cache->endframe);
2657 cache->flag &= ~PTCACHE_BAKED;
2660 else for(SETLOOPER(scene, sce_iter, base)) {
2661 /* cache/bake everything in the scene */
2662 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
2664 for(pid=pidlist.first; pid; pid=pid->next) {
2666 if((cache->flag & PTCACHE_BAKED)==0) {
2667 if(pid->type==PTCACHE_TYPE_PARTICLES) {
2668 ParticleSystem *psys = (ParticleSystem*)pid->calldata;
2669 /* skip hair & keyed particles */
2670 if(psys->part->type == PART_HAIR || psys->part->phystype == PART_PHYS_KEYED)
2673 psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
2676 if((cache->flag & PTCACHE_REDO_NEEDED || (cache->flag & PTCACHE_SIMULATION_VALID)==0)
2677 && ((cache->flag & PTCACHE_QUICK_CACHE)==0 || render || bake))
2678 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2680 startframe = MIN2(startframe, cache->startframe);
2682 if(bake || render) {
2683 cache->flag |= PTCACHE_BAKING;
2686 thread_data.endframe = MAX2(thread_data.endframe, cache->endframe);
2689 cache->flag &= ~PTCACHE_BAKED;
2693 BLI_freelistN(&pidlist);
2697 scene->r.framelen = 1.0;
2698 thread_data.break_operation = FALSE;
2699 thread_data.thread_ended = FALSE;
2705 ptcache_bake_thread((void*)&thread_data);
2708 BLI_init_threads(&threads, ptcache_bake_thread, 1);
2709 BLI_insert_thread(&threads, (void*)&thread_data);
2711 while (thread_data.thread_ended == FALSE) {
2714 progress = (int)(100.0f * (float)(CFRA - startframe)/(float)(thread_data.endframe-startframe));
2718 /* NOTE: baking should not redraw whole ui as this slows things down */
2719 if ((baker->progressbar) && (progress != old_progress)) {
2720 baker->progressbar(baker->progresscontext, progress);
2721 old_progress = progress;
2724 /* Delay to lessen CPU load from UI thread */
2727 /* NOTE: breaking baking should leave calculated frames in cache, not clear it */
2728 if(blender_test_break() && !thread_data.break_operation) {
2729 thread_data.break_operation = TRUE;
2730 if (baker->progressend)
2731 baker->progressend(baker->progresscontext);
2736 BLI_end_threads(&threads);
2738 /* clear baking flag */
2740 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
2741 cache->flag |= PTCACHE_SIMULATION_VALID;
2743 cache->flag |= PTCACHE_BAKED;
2744 /* write info file */
2745 if(cache->flag & PTCACHE_DISK_CACHE)
2746 BKE_ptcache_write(pid, 0);
2749 else for(SETLOOPER(scene, sce_iter, base)) {
2750 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
2752 for(pid=pidlist.first; pid; pid=pid->next) {
2753 /* skip hair particles */
2754 if(pid->type==PTCACHE_TYPE_PARTICLES && ((ParticleSystem*)pid->calldata)->part->type == PART_HAIR)
2759 if(thread_data.step > 1)
2760 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_OUTDATED);
2762 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
2764 cache->flag |= PTCACHE_SIMULATION_VALID;
2767 cache->flag |= PTCACHE_BAKED;
2768 if(cache->flag & PTCACHE_DISK_CACHE)
2769 BKE_ptcache_write(pid, 0);
2772 BLI_freelistN(&pidlist);
2775 scene->r.framelen = frameleno;
2778 if(bake) /* already on cfra unless baking */
2779 scene_update_for_newframe(bmain, scene, scene->lay);
2781 if (thread_data.break_operation)
2783 else if (baker->progressend)
2784 baker->progressend(baker->progresscontext);
2788 /* TODO: call redraw all windows somehow */
2791 void BKE_ptcache_disk_to_mem(PTCacheID *pid)
2793 PointCache *cache = pid->cache;
2794 PTCacheMem *pm = NULL;
2795 int baked = cache->flag & PTCACHE_BAKED;
2796 int cfra, sfra = cache->startframe, efra = cache->endframe;
2798 /* Remove possible bake flag to allow clear */
2799 cache->flag &= ~PTCACHE_BAKED;
2801 /* PTCACHE_DISK_CACHE flag was cleared already */
2802 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2804 /* restore possible bake flag */
2805 cache->flag |= baked;
2807 for(cfra=sfra; cfra <= efra; cfra++) {
2808 pm = ptcache_disk_frame_to_mem(pid, cfra);
2811 BLI_addtail(&pid->cache->mem_cache, pm);
2814 void BKE_ptcache_mem_to_disk(PTCacheID *pid)
2816 PointCache *cache = pid->cache;
2817 PTCacheMem *pm = cache->mem_cache.first;
2818 int baked = cache->flag & PTCACHE_BAKED;
2820 /* Remove possible bake flag to allow clear */
2821 cache->flag &= ~PTCACHE_BAKED;
2823 /* PTCACHE_DISK_CACHE flag was set already */
2824 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2826 /* restore possible bake flag */
2827 cache->flag |= baked;
2829 for(; pm; pm=pm->next) {
2830 if(ptcache_mem_frame_to_disk(pid, pm)==0) {
2831 cache->flag &= ~PTCACHE_DISK_CACHE;
2836 /* write info file */
2837 if(cache->flag & PTCACHE_BAKED)
2838 BKE_ptcache_write(pid, 0);
2840 void BKE_ptcache_toggle_disk_cache(PTCacheID *pid)
2842 PointCache *cache = pid->cache;
2843 int last_exact = cache->last_exact;
2845 if (!G.relbase_valid){
2846 cache->flag &= ~PTCACHE_DISK_CACHE;
2848 printf("File must be saved before using disk cache!\n");
2852 if(cache->cached_frames) {
2853 MEM_freeN(cache->cached_frames);
2854 cache->cached_frames=NULL;
2857 if(cache->flag & PTCACHE_DISK_CACHE)
2858 BKE_ptcache_mem_to_disk(pid);
2860 BKE_ptcache_disk_to_mem(pid);
2862 cache->flag ^= PTCACHE_DISK_CACHE;
2863 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2864 cache->flag ^= PTCACHE_DISK_CACHE;
2866 cache->last_exact = last_exact;
2868 BKE_ptcache_id_time(pid, NULL, 0.0f, NULL, NULL, NULL);
2870 BKE_ptcache_update_info(pid);
2873 void BKE_ptcache_disk_cache_rename(PTCacheID *pid, char *from, char *to)
2876 int len; /* store the length of the string */
2877 /* mode is same as fopen's modes */
2880 char path[MAX_PTCACHE_PATH];
2881 char old_filename[MAX_PTCACHE_FILE];
2882 char new_path_full[MAX_PTCACHE_FILE];
2883 char old_path_full[MAX_PTCACHE_FILE];
2884 char ext[MAX_PTCACHE_PATH];
2887 strcpy(old_name, pid->cache->name);
2889 /* get "from" filename */
2890 strcpy(pid->cache->name, from);
2892 len = ptcache_filename(pid, old_filename, 0, 0, 0); /* no path */
2894 ptcache_path(pid, path);
2895 dir = opendir(path);
2897 strcpy(pid->cache->name, old_name);
2901 snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, pid->stack_index);
2903 /* put new name into cache */
2904 strcpy(pid->cache->name, to);
2906 while ((de = readdir(dir)) != NULL) {
2907 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2908 if (strncmp(old_filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2909 /* read the number of the file */
2910 int frame, len2 = (int)strlen(de->d_name);
2913 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2914 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2917 BLI_join_dirfile(old_path_full, sizeof(old_path_full), path, de->d_name);
2918 ptcache_filename(pid, new_path_full, frame, 1, 1);
2919 BLI_rename(old_path_full, new_path_full);
2926 strcpy(pid->cache->name, old_name);
2929 void BKE_ptcache_load_external(PTCacheID *pid)
2932 PointCache *cache = pid->cache;
2933 int len; /* store the length of the string */
2935 int start = MAXFRAME;
2938 /* mode is same as fopen's modes */
2941 char path[MAX_PTCACHE_PATH];
2942 char filename[MAX_PTCACHE_FILE];
2943 char ext[MAX_PTCACHE_PATH];
2948 ptcache_path(pid, path);
2950 len = ptcache_filename(pid, filename, 1, 0, 0); /* no path */
2952 dir = opendir(path);
2956 if(cache->index >= 0)
2957 snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, cache->index);
2959 strcpy(ext, PTCACHE_EXT);
2961 while ((de = readdir(dir)) != NULL) {
2962 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2963 if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2964 /* read the number of the file */
2965 int frame, len2 = (int)strlen(de->d_name);
2968 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2969 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2973 start = MIN2(start, frame);
2974 end = MAX2(end, frame);
2984 if(start != MAXFRAME) {
2987 cache->startframe = start;
2988 cache->endframe = end;
2989 cache->totpoint = 0;
2991 if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2992 ; /*necessary info in every file*/
2993 /* read totpoint from info file (frame 0) */
2995 pf= ptcache_file_open(pid, PTCACHE_FILE_READ, 0);
2998 if(ptcache_file_header_begin_read(pf)) {
2999 if(pf->type == pid->type && pid->read_header(pf)) {
3000 cache->totpoint = pf->totpoint;
3001 cache->flag |= PTCACHE_READ_INFO;
3004 cache->totpoint = 0;
3007 ptcache_file_close(pf);
3010 /* or from any old format cache file */
3013 int elemsize = ptcache_old_elemsize(pid);
3014 pf= ptcache_file_open(pid, PTCACHE_FILE_READ, cache->startframe);
3017 while(ptcache_file_read(pf, old_data, 1, elemsize))
3020 ptcache_file_close(pf);
3023 cache->flag |= (PTCACHE_BAKED|PTCACHE_DISK_CACHE|PTCACHE_SIMULATION_VALID);
3024 cache->flag &= ~(PTCACHE_OUTDATED|PTCACHE_FRAMES_SKIPPED);
3027 BKE_ptcache_update_info(pid);
3030 void BKE_ptcache_update_info(PTCacheID *pid)
3032 PointCache *cache = pid->cache;
3033 PTCacheExtra *extra = NULL;
3037 if(cache->flag & PTCACHE_EXTERNAL) {
3038 int cfra = cache->startframe;
3040 for(; cfra<=cache->endframe; cfra++) {
3041 if(BKE_ptcache_id_exist(pid, cfra))
3045 /* smoke doesn't use frame 0 as info frame so can't check based on totpoint */
3046 if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN && totframes)
3047 sprintf(cache->info, "%i frames found!", totframes);
3048 else if(totframes && cache->totpoint)
3049 sprintf(cache->info, "%i points found!", cache->totpoint);
3051 sprintf(cache->info, "No valid data to read!");
3055 if(cache->flag & PTCACHE_DISK_CACHE) {
3056 if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
3058 int totpoint = pid->totpoint(pid->calldata, 0);
3060 if(cache->totpoint > totpoint)
3061 sprintf(mem_info, "%i cells + High Resolution cached", totpoint);
3063 sprintf(mem_info, "%i cells cached", totpoint);
3066 int cfra = cache->startframe;
3068 for(; cfra<=cache->endframe; cfra++) {
3069 if(BKE_ptcache_id_exist(pid, cfra))
3073 sprintf(mem_info, "%i frames on disk", totframes);
3077 PTCacheMem *pm = cache->mem_cache.first;
3081 for(; pm; pm=pm->next) {
3082 for(i=0; i<BPHYS_TOT_DATA; i++)
3083 bytes += MEM_allocN_len(pm->data[i]);
3085 for(extra=pm->extradata.first; extra; extra=extra->next) {
3086 bytes += MEM_allocN_len(extra->data);
3087 bytes += sizeof(PTCacheExtra);
3090 bytes += sizeof(PTCacheMem);
3095 mb = (bytes > 1024.0f * 1024.0f);
3097 sprintf(mem_info, "%i frames in memory (%.1f %s)",
3099 bytes / (mb ? 1024.0f * 1024.0f : 1024.0f),
3103 if(cache->flag & PTCACHE_OUTDATED) {
3104 sprintf(cache->info, "%s, cache is outdated!", mem_info);
3106 else if(cache->flag & PTCACHE_FRAMES_SKIPPED) {
3107 sprintf(cache->info, "%s, not exact since frame %i.", mem_info, cache->last_exact);
3110 sprintf(cache->info, "%s.", mem_info);
3113 void BKE_ptcache_validate(PointCache *cache, int framenr)
3116 cache->flag |= PTCACHE_SIMULATION_VALID;
3117 cache->simframe = framenr;
3120 void BKE_ptcache_invalidate(PointCache *cache)
3123 cache->flag &= ~PTCACHE_SIMULATION_VALID;
3124 cache->simframe = 0;
3125 cache->last_exact = MIN2(cache->startframe, 0);