code cleanup: rename G.afbreek --> is_break, G.rendering --> is_rendering
[blender.git] / source / blender / blenkernel / intern / pointcache.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Campbell Barton <ideasman42@gmail.com>
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenkernel/intern/pointcache.c
27  *  \ingroup bke
28  */
29
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_ID.h"
40 #include "DNA_cloth_types.h"
41 #include "DNA_dynamicpaint_types.h"
42 #include "DNA_modifier_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_object_force.h"
45 #include "DNA_particle_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_smoke_types.h"
48
49 #include "BLI_blenlib.h"
50 #include "BLI_threads.h"
51 #include "BLI_math.h"
52 #include "BLI_utildefines.h"
53
54 #include "PIL_time.h"
55
56 #include "WM_api.h"
57
58 #include "BKE_anim.h"
59 #include "BKE_blender.h"
60 #include "BKE_cloth.h"
61 #include "BKE_depsgraph.h"
62 #include "BKE_dynamicpaint.h"
63 #include "BKE_global.h"
64 #include "BKE_library.h"
65 #include "BKE_main.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"
73
74 #include "BIK_api.h"
75
76 /* both in intern */
77 #ifdef WITH_SMOKE
78 #include "smoke_API.h"
79 #endif
80
81 #ifdef WITH_LZO
82 #include "minilzo.h"
83 #else
84 /* used for non-lzo cases */
85 #define LZO_OUT_LEN(size)     ((size) + (size) / 16 + 64 + 3)
86 #endif
87
88 #ifdef WITH_LZMA
89 #include "LzmaLib.h"
90 #endif
91
92 /* needed for directory lookup */
93 /* untitled blend's need getpid for a unique name */
94 #ifndef WIN32
95 #  include <dirent.h>
96 #  include <unistd.h>
97 #else
98 #  include <process.h>
99 #  include "BLI_winstuff.h"
100 #endif
101
102 #define PTCACHE_DATA_FROM(data, type, from)  \
103         if (data[type]) { \
104                 memcpy(data[type], from, ptcache_data_size[type]); \
105         } (void)0
106
107 #define PTCACHE_DATA_TO(data, type, index, to)  \
108         if (data[type]) { \
109                 memcpy(to, (char *)(data)[type] + ((index) ? (index) * ptcache_data_size[type] : 0), ptcache_data_size[type]); \
110         } (void)0
111
112 /* could be made into a pointcache option */
113 #define DURIAN_POINTCACHE_LIB_OK 1
114
115 static int ptcache_data_size[] = {      
116                 sizeof(unsigned int), // BPHYS_DATA_INDEX
117                 3 * sizeof(float), // BPHYS_DATA_LOCATION
118                 3 * sizeof(float), // BPHYS_DATA_VELOCITY
119                 4 * sizeof(float), // BPHYS_DATA_ROTATION
120                 3 * sizeof(float), // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST
121                 sizeof(float), // BPHYS_DATA_SIZE
122                 3 * sizeof(float), // BPHYS_DATA_TIMES
123                 sizeof(BoidData) // case BPHYS_DATA_BOIDS
124 };
125
126 static int ptcache_extra_datasize[] = {
127         0,
128         sizeof(ParticleSpring)
129 };
130
131 /* forward declerations */
132 static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result, unsigned int len);
133 static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, unsigned int in_len, unsigned char *out, int mode);
134 static int ptcache_file_write(PTCacheFile *pf, const void *f, unsigned int tot, unsigned int size);
135 static int ptcache_file_read(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size);
136
137 /* Common functions */
138 static int ptcache_basic_header_read(PTCacheFile *pf)
139 {
140         int error=0;
141
142         /* Custom functions should read these basic elements too! */
143         if (!error && !fread(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
144                 error = 1;
145         
146         if (!error && !fread(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
147                 error = 1;
148
149         return !error;
150 }
151 static int ptcache_basic_header_write(PTCacheFile *pf)
152 {
153         /* Custom functions should write these basic elements too! */
154         if (!fwrite(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
155                 return 0;
156         
157         if (!fwrite(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
158                 return 0;
159
160         return 1;
161 }
162 /* Softbody functions */
163 static int  ptcache_softbody_write(int index, void *soft_v, void **data, int UNUSED(cfra))
164 {
165         SoftBody *soft= soft_v;
166         BodyPoint *bp = soft->bpoint + index;
167
168         PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, bp->pos);
169         PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, bp->vec);
170
171         return 1;
172 }
173 static void ptcache_softbody_read(int index, void *soft_v, void **data, float UNUSED(cfra), float *old_data)
174 {
175         SoftBody *soft= soft_v;
176         BodyPoint *bp = soft->bpoint + index;
177
178         if (old_data) {
179                 memcpy(bp->pos, data, 3 * sizeof(float));
180                 memcpy(bp->vec, data + 3, 3 * sizeof(float));
181         }
182         else {
183                 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, bp->pos);
184                 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, 0, bp->vec);
185         }
186 }
187 static void ptcache_softbody_interpolate(int index, void *soft_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
188 {
189         SoftBody *soft= soft_v;
190         BodyPoint *bp = soft->bpoint + index;
191         ParticleKey keys[4];
192         float dfra;
193
194         if (cfra1 == cfra2)
195                 return;
196
197         copy_v3_v3(keys[1].co, bp->pos);
198         copy_v3_v3(keys[1].vel, bp->vec);
199
200         if (old_data) {
201                 memcpy(keys[2].co, old_data, 3 * sizeof(float));
202                 memcpy(keys[2].vel, old_data + 3, 3 * sizeof(float));
203         }
204         else
205                 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
206
207         dfra = cfra2 - cfra1;
208
209         mul_v3_fl(keys[1].vel, dfra);
210         mul_v3_fl(keys[2].vel, dfra);
211
212         psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
213
214         mul_v3_fl(keys->vel, 1.0f / dfra);
215
216         copy_v3_v3(bp->pos, keys->co);
217         copy_v3_v3(bp->vec, keys->vel);
218 }
219 static int  ptcache_softbody_totpoint(void *soft_v, int UNUSED(cfra))
220 {
221         SoftBody *soft= soft_v;
222         return soft->totpoint;
223 }
224 /* Particle functions */
225 void BKE_ptcache_make_particle_key(ParticleKey *key, int index, void **data, float time)
226 {
227         PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, index, key->co);
228         PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, index, key->vel);
229         
230         /* no rotation info, so make something nice up */
231         if (data[BPHYS_DATA_ROTATION]==NULL) {
232                 vec_to_quat(key->rot, key->vel, OB_NEGX, OB_POSZ);
233         }
234         else {
235                 PTCACHE_DATA_TO(data, BPHYS_DATA_ROTATION, index, key->rot);
236         }
237
238         PTCACHE_DATA_TO(data, BPHYS_DATA_AVELOCITY, index, key->ave);
239         key->time = time;
240 }
241 static int  ptcache_particle_write(int index, void *psys_v, void **data, int cfra)
242 {
243         ParticleSystem *psys= psys_v;
244         ParticleData *pa = psys->particles + index;
245         BoidParticle *boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
246         float times[3];
247         int step = psys->pointcache->step;
248
249         /* No need to store unborn or died particles outside cache step bounds */
250         if (data[BPHYS_DATA_INDEX] && (cfra < pa->time - step || cfra > pa->dietime + step))
251                 return 0;
252
253         times[0]= pa->time;
254         times[1]= pa->dietime;
255         times[2]= pa->lifetime;
256
257         PTCACHE_DATA_FROM(data, BPHYS_DATA_INDEX, &index);
258         PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, pa->state.co);
259         PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, pa->state.vel);
260         PTCACHE_DATA_FROM(data, BPHYS_DATA_ROTATION, pa->state.rot);
261         PTCACHE_DATA_FROM(data, BPHYS_DATA_AVELOCITY, pa->state.ave);
262         PTCACHE_DATA_FROM(data, BPHYS_DATA_SIZE, &pa->size);
263         PTCACHE_DATA_FROM(data, BPHYS_DATA_TIMES, times);
264
265         if (boid)
266                 PTCACHE_DATA_FROM(data, BPHYS_DATA_BOIDS, &boid->data);
267
268         /* return flag 1+1=2 for newly born particles to copy exact birth location to previously cached frame */
269         return 1 + (pa->state.time >= pa->time && pa->prev_state.time <= pa->time);
270 }
271 static void ptcache_particle_read(int index, void *psys_v, void **data, float cfra, float *old_data)
272 {
273         ParticleSystem *psys= psys_v;
274         ParticleData *pa;
275         BoidParticle *boid;
276         float timestep = 0.04f*psys->part->timetweak;
277
278         if (index >= psys->totpart)
279                 return;
280
281         pa = psys->particles + index;
282         boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
283
284         if (cfra > pa->state.time)
285                 memcpy(&pa->prev_state, &pa->state, sizeof(ParticleKey));
286
287         if (old_data) {
288                 /* old format cache */
289                 memcpy(&pa->state, old_data, sizeof(ParticleKey));
290                 return;
291         }
292
293         BKE_ptcache_make_particle_key(&pa->state, 0, data, cfra);
294
295         /* set frames cached before birth to birth time */
296         if (cfra < pa->time)
297                 pa->state.time = pa->time;
298         else if (cfra > pa->dietime)
299                 pa->state.time = pa->dietime;
300
301         if (data[BPHYS_DATA_SIZE])
302                 PTCACHE_DATA_TO(data, BPHYS_DATA_SIZE, 0, &pa->size);
303         
304         if (data[BPHYS_DATA_TIMES]) {
305                 float times[3];
306                 PTCACHE_DATA_TO(data, BPHYS_DATA_TIMES, 0, &times);
307                 pa->time = times[0];
308                 pa->dietime = times[1];
309                 pa->lifetime = times[2];
310         }
311
312         if (boid)
313                 PTCACHE_DATA_TO(data, BPHYS_DATA_BOIDS, 0, &boid->data);
314
315         /* determine velocity from previous location */
316         if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
317                 if (cfra > pa->prev_state.time) {
318                         sub_v3_v3v3(pa->state.vel, pa->state.co, pa->prev_state.co);
319                         mul_v3_fl(pa->state.vel, (cfra - pa->prev_state.time) * timestep);
320                 }
321                 else {
322                         sub_v3_v3v3(pa->state.vel, pa->prev_state.co, pa->state.co);
323                         mul_v3_fl(pa->state.vel, (pa->prev_state.time - cfra) * timestep);
324                 }
325         }
326
327         /* default to no rotation */
328         if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
329                 unit_qt(pa->state.rot);
330         }
331 }
332 static void ptcache_particle_interpolate(int index, void *psys_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
333 {
334         ParticleSystem *psys= psys_v;
335         ParticleData *pa;
336         ParticleKey keys[4];
337         float dfra, timestep = 0.04f*psys->part->timetweak;
338
339         if (index >= psys->totpart)
340                 return;
341
342         pa = psys->particles + index;
343
344         /* particle wasn't read from first cache so can't interpolate */
345         if ((int)cfra1 < pa->time - psys->pointcache->step || (int)cfra1 > pa->dietime + psys->pointcache->step)
346                 return;
347
348         cfra = MIN2(cfra, pa->dietime);
349         cfra1 = MIN2(cfra1, pa->dietime);
350         cfra2 = MIN2(cfra2, pa->dietime);
351
352         if (cfra1 == cfra2)
353                 return;
354
355         memcpy(keys+1, &pa->state, sizeof(ParticleKey));
356         if (old_data)
357                 memcpy(keys+2, old_data, sizeof(ParticleKey));
358         else
359                 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
360
361         /* determine velocity from previous location */
362         if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
363                 if (keys[1].time > keys[2].time) {
364                         sub_v3_v3v3(keys[2].vel, keys[1].co, keys[2].co);
365                         mul_v3_fl(keys[2].vel, (keys[1].time - keys[2].time) * timestep);
366                 }
367                 else {
368                         sub_v3_v3v3(keys[2].vel, keys[2].co, keys[1].co);
369                         mul_v3_fl(keys[2].vel, (keys[2].time - keys[1].time) * timestep);
370                 }
371         }
372
373         /* determine rotation from velocity */
374         if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
375                 vec_to_quat(keys[2].rot, keys[2].vel, OB_NEGX, OB_POSZ);
376         }
377
378         if (cfra > pa->time)
379                 cfra1 = MAX2(cfra1, pa->time);
380
381         dfra = cfra2 - cfra1;
382
383         mul_v3_fl(keys[1].vel, dfra * timestep);
384         mul_v3_fl(keys[2].vel, dfra * timestep);
385
386         psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, &pa->state, 1);
387         interp_qt_qtqt(pa->state.rot, keys[1].rot, keys[2].rot, (cfra - cfra1) / dfra);
388
389         mul_v3_fl(pa->state.vel, 1.f / (dfra * timestep));
390
391         pa->state.time = cfra;
392 }
393
394 static int  ptcache_particle_totpoint(void *psys_v, int UNUSED(cfra))
395 {
396         ParticleSystem *psys = psys_v;
397         return psys->totpart;
398 }
399 static int  ptcache_particle_totwrite(void *psys_v, int cfra)
400 {
401         ParticleSystem *psys = psys_v;
402         ParticleData *pa= psys->particles;
403         int p, step = psys->pointcache->step;
404         int totwrite = 0;
405
406         if (cfra == 0)
407                 return psys->totpart;
408
409         for (p=0; p<psys->totpart; p++, pa++)
410                 totwrite += (cfra >= pa->time - step && cfra <= pa->dietime + step);
411
412         return totwrite;
413 }
414
415 static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int UNUSED(cfra))
416 {
417         ParticleSystem *psys = psys_v;
418         PTCacheExtra *extra = NULL;
419
420         if (psys->part->phystype == PART_PHYS_FLUID &&
421                 psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS &&
422                 psys->tot_fluidsprings && psys->fluid_springs) {
423
424                 extra = MEM_callocN(sizeof(PTCacheExtra), "Point cache: fluid extra data");
425
426                 extra->type = BPHYS_EXTRA_FLUID_SPRINGS;
427                 extra->totdata = psys->tot_fluidsprings;
428
429                 extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Point cache: extra data");
430                 memcpy(extra->data, psys->fluid_springs, extra->totdata * ptcache_extra_datasize[extra->type]);
431
432                 BLI_addtail(&pm->extradata, extra);
433         }
434 }
435
436 static void ptcache_particle_extra_read(void *psys_v, PTCacheMem *pm, float UNUSED(cfra))
437 {
438         ParticleSystem *psys = psys_v;
439         PTCacheExtra *extra = pm->extradata.first;
440
441         for (; extra; extra=extra->next) {
442                 switch (extra->type) {
443                         case BPHYS_EXTRA_FLUID_SPRINGS:
444                         {
445                                 if (psys->fluid_springs)
446                                         MEM_freeN(psys->fluid_springs);
447
448                                 psys->fluid_springs = MEM_dupallocN(extra->data);
449                                 psys->tot_fluidsprings = psys->alloc_fluidsprings = extra->totdata;
450                                 break;
451                         }
452                 }
453         }
454 }
455
456 /* Cloth functions */
457 static int  ptcache_cloth_write(int index, void *cloth_v, void **data, int UNUSED(cfra))
458 {
459         ClothModifierData *clmd= cloth_v;
460         Cloth *cloth= clmd->clothObject;
461         ClothVertex *vert = cloth->verts + index;
462
463         PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, vert->x);
464         PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, vert->v);
465         PTCACHE_DATA_FROM(data, BPHYS_DATA_XCONST, vert->xconst);
466
467         return 1;
468 }
469 static void ptcache_cloth_read(int index, void *cloth_v, void **data, float UNUSED(cfra), float *old_data)
470 {
471         ClothModifierData *clmd= cloth_v;
472         Cloth *cloth= clmd->clothObject;
473         ClothVertex *vert = cloth->verts + index;
474         
475         if (old_data) {
476                 memcpy(vert->x, data, 3 * sizeof(float));
477                 memcpy(vert->xconst, data + 3, 3 * sizeof(float));
478                 memcpy(vert->v, data + 6, 3 * sizeof(float));
479         }
480         else {
481                 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, vert->x);
482                 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, 0, vert->v);
483                 PTCACHE_DATA_TO(data, BPHYS_DATA_XCONST, 0, vert->xconst);
484         }
485 }
486 static void ptcache_cloth_interpolate(int index, void *cloth_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
487 {
488         ClothModifierData *clmd= cloth_v;
489         Cloth *cloth= clmd->clothObject;
490         ClothVertex *vert = cloth->verts + index;
491         ParticleKey keys[4];
492         float dfra;
493
494         if (cfra1 == cfra2)
495                 return;
496
497         copy_v3_v3(keys[1].co, vert->x);
498         copy_v3_v3(keys[1].vel, vert->v);
499
500         if (old_data) {
501                 memcpy(keys[2].co, old_data, 3 * sizeof(float));
502                 memcpy(keys[2].vel, old_data + 6, 3 * sizeof(float));
503         }
504         else
505                 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
506
507         dfra = cfra2 - cfra1;
508
509         mul_v3_fl(keys[1].vel, dfra);
510         mul_v3_fl(keys[2].vel, dfra);
511
512         psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
513
514         mul_v3_fl(keys->vel, 1.0f / dfra);
515
516         copy_v3_v3(vert->x, keys->co);
517         copy_v3_v3(vert->v, keys->vel);
518
519         /* should vert->xconst be interpolated somehow too? - jahka */
520 }
521
522 static int  ptcache_cloth_totpoint(void *cloth_v, int UNUSED(cfra))
523 {
524         ClothModifierData *clmd= cloth_v;
525         return clmd->clothObject ? clmd->clothObject->numverts : 0;
526 }
527
528 #ifdef WITH_SMOKE
529 /* Smoke functions */
530 static int  ptcache_smoke_totpoint(void *smoke_v, int UNUSED(cfra))
531 {
532         SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
533         SmokeDomainSettings *sds = smd->domain;
534         
535         if (sds->fluid) {
536                 return sds->res[0]*sds->res[1]*sds->res[2];
537         }
538         else
539                 return 0;
540 }
541 static int  ptcache_smoke_write(PTCacheFile *pf, void *smoke_v)
542 {       
543         SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
544         SmokeDomainSettings *sds = smd->domain;
545         int ret = 0;
546         
547         if (sds->fluid) {
548                 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
549                 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
550                 unsigned char *obstacles;
551                 unsigned int in_len = sizeof(float)*(unsigned int)res;
552                 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
553                 //int mode = res >= 1000000 ? 2 : 1;
554                 int mode=1;             // light
555                 if (sds->cache_comp == SM_CACHE_HEAVY) mode=2;  // heavy
556
557                 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
558
559                 ptcache_file_compressed_write(pf, (unsigned char *)sds->shadow, in_len, out, mode);
560                 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len, out, mode);
561                 ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len, out, mode); 
562                 ptcache_file_compressed_write(pf, (unsigned char *)heat, in_len, out, mode);
563                 ptcache_file_compressed_write(pf, (unsigned char *)heatold, in_len, out, mode);
564                 ptcache_file_compressed_write(pf, (unsigned char *)vx, in_len, out, mode);
565                 ptcache_file_compressed_write(pf, (unsigned char *)vy, in_len, out, mode);
566                 ptcache_file_compressed_write(pf, (unsigned char *)vz, in_len, out, mode);
567                 ptcache_file_compressed_write(pf, (unsigned char *)vxold, in_len, out, mode);
568                 ptcache_file_compressed_write(pf, (unsigned char *)vyold, in_len, out, mode);
569                 ptcache_file_compressed_write(pf, (unsigned char *)vzold, in_len, out, mode);
570                 ptcache_file_compressed_write(pf, (unsigned char *)obstacles, (unsigned int)res, out, mode);
571                 ptcache_file_write(pf, &dt, 1, sizeof(float));
572                 ptcache_file_write(pf, &dx, 1, sizeof(float));
573
574                 MEM_freeN(out);
575                 
576                 ret = 1;
577         }
578
579         if (sds->wt) {
580                 int res_big_array[3];
581                 int res_big;
582                 int res = sds->res[0]*sds->res[1]*sds->res[2];
583                 float *dens, *densold, *tcu, *tcv, *tcw;
584                 unsigned int in_len = sizeof(float)*(unsigned int)res;
585                 unsigned int in_len_big;
586                 unsigned char *out;
587                 int mode;
588
589                 smoke_turbulence_get_res(sds->wt, res_big_array);
590                 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
591                 //mode =  res_big >= 1000000 ? 2 : 1;
592                 mode = 1;       // light
593                 if (sds->cache_high_comp == SM_CACHE_HEAVY) mode=2;     // heavy
594
595                 in_len_big = sizeof(float) * (unsigned int)res_big;
596
597                 smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
598
599                 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len_big), "pointcache_lzo_buffer");
600                 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len_big, out, mode);
601                 ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len_big, out, mode);     
602                 MEM_freeN(out);
603
604                 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
605                 ptcache_file_compressed_write(pf, (unsigned char *)tcu, in_len, out, mode);
606                 ptcache_file_compressed_write(pf, (unsigned char *)tcv, in_len, out, mode);
607                 ptcache_file_compressed_write(pf, (unsigned char *)tcw, in_len, out, mode);
608                 MEM_freeN(out);
609                 
610                 ret = 1;
611         }
612
613         return ret;
614 }
615 static int ptcache_smoke_read(PTCacheFile *pf, void *smoke_v)
616 {
617         SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
618         SmokeDomainSettings *sds = smd->domain;
619         
620         if (sds->fluid) {
621                 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
622                 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
623                 unsigned char *obstacles;
624                 unsigned int out_len = (unsigned int)res * sizeof(float);
625                 
626                 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
627
628                 ptcache_file_compressed_read(pf, (unsigned char *)sds->shadow, out_len);
629                 ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len);
630                 ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len);
631                 ptcache_file_compressed_read(pf, (unsigned char*)heat, out_len);
632                 ptcache_file_compressed_read(pf, (unsigned char*)heatold, out_len);
633                 ptcache_file_compressed_read(pf, (unsigned char*)vx, out_len);
634                 ptcache_file_compressed_read(pf, (unsigned char*)vy, out_len);
635                 ptcache_file_compressed_read(pf, (unsigned char*)vz, out_len);
636                 ptcache_file_compressed_read(pf, (unsigned char*)vxold, out_len);
637                 ptcache_file_compressed_read(pf, (unsigned char*)vyold, out_len);
638                 ptcache_file_compressed_read(pf, (unsigned char*)vzold, out_len);
639                 ptcache_file_compressed_read(pf, (unsigned char*)obstacles, (unsigned int)res);
640                 ptcache_file_read(pf, &dt, 1, sizeof(float));
641                 ptcache_file_read(pf, &dx, 1, sizeof(float));
642
643                 if (pf->data_types & (1<<BPHYS_DATA_SMOKE_HIGH) && sds->wt) {
644                         int res = sds->res[0]*sds->res[1]*sds->res[2];
645                         int res_big, res_big_array[3];
646                         float *dens, *densold, *tcu, *tcv, *tcw;
647                         unsigned int out_len = sizeof(float)*(unsigned int)res;
648                         unsigned int out_len_big;
649
650                         smoke_turbulence_get_res(sds->wt, res_big_array);
651                         res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
652                         out_len_big = sizeof(float) * (unsigned int)res_big;
653
654                         smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
655
656                         ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len_big);
657                         ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len_big);
658
659                         ptcache_file_compressed_read(pf, (unsigned char*)tcu, out_len);
660                         ptcache_file_compressed_read(pf, (unsigned char*)tcv, out_len);
661                         ptcache_file_compressed_read(pf, (unsigned char*)tcw, out_len);
662                 }
663         }
664
665         return 1;
666 }
667 #else // WITH_SMOKE
668 static int  ptcache_smoke_totpoint(void *UNUSED(smoke_v), int UNUSED(cfra)) { return 0; }
669 static int  ptcache_smoke_read(PTCacheFile *UNUSED(pf), void *UNUSED(smoke_v)) { return 0; }
670 static int  ptcache_smoke_write(PTCacheFile *UNUSED(pf), void *UNUSED(smoke_v)) { return 0; }
671 #endif // WITH_SMOKE
672
673 static int ptcache_dynamicpaint_totpoint(void *sd, int UNUSED(cfra))
674 {
675         DynamicPaintSurface *surface = (DynamicPaintSurface*)sd;
676
677         if (!surface->data) return 0;
678         else return surface->data->total_points;
679 }
680
681 #define DPAINT_CACHE_VERSION "1.01"
682
683 static int  ptcache_dynamicpaint_write(PTCacheFile *pf, void *dp_v)
684 {       
685         DynamicPaintSurface *surface = (DynamicPaintSurface*)dp_v;
686         int cache_compress = 1;
687
688         /* version header */
689         ptcache_file_write(pf, DPAINT_CACHE_VERSION, 1, sizeof(char)*4);
690
691         if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
692                 int total_points=surface->data->total_points;
693                 unsigned int in_len;
694                 unsigned char *out;
695
696                 /* cache type */
697                 ptcache_file_write(pf, &surface->type, 1, sizeof(int));
698
699                 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
700                         in_len = sizeof(PaintPoint) * total_points;
701                 }
702                 else if (surface->type == MOD_DPAINT_SURFACE_T_DISPLACE ||
703                          surface->type == MOD_DPAINT_SURFACE_T_WEIGHT)
704                 {
705                         in_len = sizeof(float) * total_points;
706                 }
707                 else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
708                         in_len = sizeof(PaintWavePoint) * total_points;
709                 }
710                 else {
711                         return 0;
712                 }
713
714                 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
715
716                 ptcache_file_compressed_write(pf, (unsigned char *)surface->data->type_data, in_len, out, cache_compress);
717                 MEM_freeN(out);
718
719         }
720         return 1;
721 }
722 static int ptcache_dynamicpaint_read(PTCacheFile *pf, void *dp_v)
723 {
724         DynamicPaintSurface *surface = (DynamicPaintSurface*)dp_v;
725         char version[4];
726         
727         /* version header */
728         ptcache_file_read(pf, version, 1, sizeof(char)*4);
729         if (strncmp(version, DPAINT_CACHE_VERSION, 4)) {printf("Dynamic Paint: Invalid cache version: %s!\n", version); return 0;}
730
731         if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
732                 unsigned int data_len;
733                 int surface_type;
734
735                 /* cache type */
736                 ptcache_file_read(pf, &surface_type, 1, sizeof(int));
737
738                 if (surface_type != surface->type)
739                         return 0;
740
741                 /* read surface data */
742                 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
743                         data_len = sizeof(PaintPoint);
744                 }
745                 else if (surface->type == MOD_DPAINT_SURFACE_T_DISPLACE ||
746                          surface->type == MOD_DPAINT_SURFACE_T_WEIGHT)
747                 {
748                         data_len = sizeof(float);
749                 }
750                 else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
751                         data_len = sizeof(PaintWavePoint);
752                 }
753                 else {
754                         return 0;
755                 }
756
757                 ptcache_file_compressed_read(pf, (unsigned char*)surface->data->type_data, data_len*surface->data->total_points);
758
759         }
760         return 1;
761 }
762
763 /* Creating ID's */
764 void BKE_ptcache_id_from_softbody(PTCacheID *pid, Object *ob, SoftBody *sb)
765 {
766         memset(pid, 0, sizeof(PTCacheID));
767
768         pid->ob= ob;
769         pid->calldata= sb;
770         pid->type= PTCACHE_TYPE_SOFTBODY;
771         pid->cache= sb->pointcache;
772         pid->cache_ptr= &sb->pointcache;
773         pid->ptcaches= &sb->ptcaches;
774         pid->totpoint= pid->totwrite= ptcache_softbody_totpoint;
775
776         pid->write_point                        = ptcache_softbody_write;
777         pid->read_point                         = ptcache_softbody_read;
778         pid->interpolate_point          = ptcache_softbody_interpolate;
779
780         pid->write_stream                       = NULL;
781         pid->read_stream                        = NULL;
782
783         pid->write_extra_data           = NULL;
784         pid->read_extra_data            = NULL;
785         pid->interpolate_extra_data     = NULL;
786
787         pid->write_header                       = ptcache_basic_header_write;
788         pid->read_header                        = ptcache_basic_header_read;
789
790         pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY);
791         pid->info_types= 0;
792
793         pid->stack_index = pid->cache->index;
794
795         pid->default_step = 10;
796         pid->max_step = 20;
797 }
798 void BKE_ptcache_id_from_particles(PTCacheID *pid, Object *ob, ParticleSystem *psys)
799 {
800         memset(pid, 0, sizeof(PTCacheID));
801
802         pid->ob= ob;
803         pid->calldata= psys;
804         pid->type= PTCACHE_TYPE_PARTICLES;
805         pid->stack_index= psys->pointcache->index;
806         pid->cache= psys->pointcache;
807         pid->cache_ptr= &psys->pointcache;
808         pid->ptcaches= &psys->ptcaches;
809
810         if (psys->part->type != PART_HAIR)
811                 pid->flag |= PTCACHE_VEL_PER_SEC;
812
813         pid->totpoint                           = ptcache_particle_totpoint;
814         pid->totwrite                           = ptcache_particle_totwrite;
815
816         pid->write_point                                = ptcache_particle_write;
817         pid->read_point                         = ptcache_particle_read;
818         pid->interpolate_point          = ptcache_particle_interpolate;
819
820         pid->write_stream                       = NULL;
821         pid->read_stream                        = NULL;
822
823         pid->write_extra_data           = NULL;
824         pid->read_extra_data            = NULL;
825         pid->interpolate_extra_data     = NULL;
826
827         pid->write_header                       = ptcache_basic_header_write;
828         pid->read_header                        = ptcache_basic_header_read;
829
830         pid->data_types = (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_INDEX);
831
832         if (psys->part->phystype == PART_PHYS_BOIDS)
833                 pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION) | (1<<BPHYS_DATA_BOIDS);
834         else if (psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS) {
835                 pid->write_extra_data = ptcache_particle_extra_write;
836                 pid->read_extra_data = ptcache_particle_extra_read;
837         }
838
839         if (psys->part->flag & PART_ROTATIONS) {
840                 pid->data_types|= (1<<BPHYS_DATA_ROTATION);
841
842                 if (psys->part->rotmode != PART_ROT_VEL  ||
843                     psys->part->avemode == PART_AVE_RAND ||
844                     psys->part->avefac != 0.0f)
845                 {
846                         pid->data_types |= (1 << BPHYS_DATA_AVELOCITY);
847                 }
848         }
849
850         pid->info_types= (1<<BPHYS_DATA_TIMES);
851
852         pid->default_step = 10;
853         pid->max_step = 20;
854 }
855 void BKE_ptcache_id_from_cloth(PTCacheID *pid, Object *ob, ClothModifierData *clmd)
856 {
857         memset(pid, 0, sizeof(PTCacheID));
858
859         pid->ob= ob;
860         pid->calldata= clmd;
861         pid->type= PTCACHE_TYPE_CLOTH;
862         pid->stack_index= clmd->point_cache->index;
863         pid->cache= clmd->point_cache;
864         pid->cache_ptr= &clmd->point_cache;
865         pid->ptcaches= &clmd->ptcaches;
866         pid->totpoint= pid->totwrite= ptcache_cloth_totpoint;
867
868         pid->write_point                        = ptcache_cloth_write;
869         pid->read_point                         = ptcache_cloth_read;
870         pid->interpolate_point          = ptcache_cloth_interpolate;
871
872         pid->write_stream                       = NULL;
873         pid->read_stream                        = NULL;
874
875         pid->write_extra_data           = NULL;
876         pid->read_extra_data            = NULL;
877         pid->interpolate_extra_data     = NULL;
878
879         pid->write_header                       = ptcache_basic_header_write;
880         pid->read_header                        = ptcache_basic_header_read;
881
882         pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_XCONST);
883         pid->info_types= 0;
884
885         pid->default_step = 1;
886         pid->max_step = 1;
887 }
888 void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct SmokeModifierData *smd)
889 {
890         SmokeDomainSettings *sds = smd->domain;
891
892         memset(pid, 0, sizeof(PTCacheID));
893
894         pid->ob= ob;
895         pid->calldata= smd;
896         
897         pid->type= PTCACHE_TYPE_SMOKE_DOMAIN;
898         pid->stack_index= sds->point_cache[0]->index;
899
900         pid->cache= sds->point_cache[0];
901         pid->cache_ptr= &(sds->point_cache[0]);
902         pid->ptcaches= &(sds->ptcaches[0]);
903
904         pid->totpoint= pid->totwrite= ptcache_smoke_totpoint;
905
906         pid->write_point                        = NULL;
907         pid->read_point                         = NULL;
908         pid->interpolate_point          = NULL;
909
910         pid->read_stream                        = ptcache_smoke_read;
911         pid->write_stream                       = ptcache_smoke_write;
912
913         pid->write_extra_data           = NULL;
914         pid->read_extra_data            = NULL;
915         pid->interpolate_extra_data     = NULL;
916
917         pid->write_header                       = ptcache_basic_header_write;
918         pid->read_header                        = ptcache_basic_header_read;
919
920         pid->data_types= 0;
921         pid->info_types= 0;
922
923         if (sds->fluid)
924                 pid->data_types |= (1<<BPHYS_DATA_SMOKE_LOW);
925         if (sds->wt)
926                 pid->data_types |= (1<<BPHYS_DATA_SMOKE_HIGH);
927
928         pid->default_step = 1;
929         pid->max_step = 1;
930 }
931
932 void BKE_ptcache_id_from_dynamicpaint(PTCacheID *pid, Object *ob, DynamicPaintSurface *surface)
933 {
934
935         memset(pid, 0, sizeof(PTCacheID));
936
937         pid->ob= ob;
938         pid->calldata= surface;
939         pid->type= PTCACHE_TYPE_DYNAMICPAINT;
940         pid->cache= surface->pointcache;
941         pid->cache_ptr= &surface->pointcache;
942         pid->ptcaches= &surface->ptcaches;
943         pid->totpoint= pid->totwrite= ptcache_dynamicpaint_totpoint;
944
945         pid->write_point                        = NULL;
946         pid->read_point                         = NULL;
947         pid->interpolate_point          = NULL;
948
949         pid->write_stream                       = ptcache_dynamicpaint_write;
950         pid->read_stream                        = ptcache_dynamicpaint_read;
951
952         pid->write_extra_data           = NULL;
953         pid->read_extra_data            = NULL;
954         pid->interpolate_extra_data     = NULL;
955
956         pid->write_header                       = ptcache_basic_header_write;
957         pid->read_header                        = ptcache_basic_header_read;
958
959         pid->data_types= BPHYS_DATA_DYNAMICPAINT;
960         pid->info_types= 0;
961
962         pid->stack_index = pid->cache->index;
963
964         pid->default_step = 1;
965         pid->max_step = 1;
966 }
967
968 void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int duplis)
969 {
970         PTCacheID *pid;
971         ParticleSystem *psys;
972         ModifierData *md;
973
974         lb->first= lb->last= NULL;
975
976         if (ob->soft) {
977                 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
978                 BKE_ptcache_id_from_softbody(pid, ob, ob->soft);
979                 BLI_addtail(lb, pid);
980         }
981
982         for (psys=ob->particlesystem.first; psys; psys=psys->next) {
983                 if (psys->part==NULL)
984                         continue;
985                 
986                 /* check to make sure point cache is actually used by the particles */
987                 if (ELEM(psys->part->phystype, PART_PHYS_NO, PART_PHYS_KEYED))
988                         continue;
989
990                 /* hair needs to be included in id-list for cache edit mode to work */
991                 /* if (psys->part->type == PART_HAIR && (psys->flag & PSYS_HAIR_DYNAMICS)==0) */
992                 /*      continue; */
993                         
994                 if (psys->part->type == PART_FLUID)
995                         continue;
996
997                 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
998                 BKE_ptcache_id_from_particles(pid, ob, psys);
999                 BLI_addtail(lb, pid);
1000         }
1001
1002         for (md=ob->modifiers.first; md; md=md->next) {
1003                 if (md->type == eModifierType_Cloth) {
1004                         pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1005                         BKE_ptcache_id_from_cloth(pid, ob, (ClothModifierData*)md);
1006                         BLI_addtail(lb, pid);
1007                 }
1008                 else if (md->type == eModifierType_Smoke) {
1009                         SmokeModifierData *smd = (SmokeModifierData *)md;
1010                         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
1011                                 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1012                                 BKE_ptcache_id_from_smoke(pid, ob, (SmokeModifierData*)md);
1013                                 BLI_addtail(lb, pid);
1014                         }
1015                 }
1016                 else if (md->type == eModifierType_DynamicPaint) {
1017                         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
1018                         if (pmd->canvas) {
1019                                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
1020
1021                                 for (; surface; surface=surface->next) {
1022                                         pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1023                                         BKE_ptcache_id_from_dynamicpaint(pid, ob, surface);
1024                                         BLI_addtail(lb, pid);
1025                                 }
1026                         }
1027                 }
1028         }
1029
1030         if (scene && (duplis-- > 0) && (ob->transflag & OB_DUPLI)) {
1031                 ListBase *lb_dupli_ob;
1032
1033                 if ((lb_dupli_ob=object_duplilist(scene, ob))) {
1034                         DupliObject *dob;
1035                         for (dob= lb_dupli_ob->first; dob; dob= dob->next) {
1036                                 if (dob->ob != ob) { /* avoids recursive loops with dupliframes: bug 22988 */
1037                                         ListBase lb_dupli_pid;
1038                                         BKE_ptcache_ids_from_object(&lb_dupli_pid, dob->ob, scene, duplis);
1039                                         BLI_movelisttolist(lb, &lb_dupli_pid);
1040                                         if (lb_dupli_pid.first)
1041                                                 printf("Adding Dupli\n");
1042                                 }
1043                         }
1044
1045                         free_object_duplilist(lb_dupli_ob);     /* does restore */
1046                 }
1047         }
1048 }
1049
1050 /* File handling */
1051
1052 /* Takes an Object ID and returns a unique name
1053  * - id: object id
1054  * - cfra: frame for the cache, can be negative
1055  * - stack_index: index in the modifier stack. we can have cache for more then one stack_index
1056  */
1057
1058 #define MAX_PTCACHE_PATH FILE_MAX
1059 #define MAX_PTCACHE_FILE ((FILE_MAX)*2)
1060
1061 static int ptcache_path(PTCacheID *pid, char *filename)
1062 {
1063         Library *lib= (pid->ob)? pid->ob->id.lib: NULL;
1064         const char *blendfilename= (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH)==0) ? lib->filepath: G.main->name;
1065         size_t i;
1066
1067         if (pid->cache->flag & PTCACHE_EXTERNAL) {
1068                 strcpy(filename, pid->cache->path);
1069
1070                 if (strncmp(filename, "//", 2)==0)
1071                         BLI_path_abs(filename, blendfilename);
1072
1073                 return BLI_add_slash(filename); /* new strlen() */
1074         }
1075         else if (G.relbase_valid || lib) {
1076                 char file[MAX_PTCACHE_PATH]; /* we don't want the dir, only the file */
1077
1078                 BLI_split_file_part(blendfilename, file, sizeof(file));
1079                 i = strlen(file);
1080                 
1081                 /* remove .blend */
1082                 if (i > 6)
1083                         file[i-6] = '\0';
1084                 
1085                 BLI_snprintf(filename, MAX_PTCACHE_PATH, "//"PTCACHE_PATH"%s", file); /* add blend file name to pointcache dir */
1086                 BLI_path_abs(filename, blendfilename);
1087                 return BLI_add_slash(filename); /* new strlen() */
1088         }
1089         
1090         /* use the temp path. this is weak but better then not using point cache at all */
1091         /* temporary directory is assumed to exist and ALWAYS has a trailing slash */
1092         BLI_snprintf(filename, MAX_PTCACHE_PATH, "%s"PTCACHE_PATH"%d", BLI_temporary_dir(), abs(getpid()));
1093         
1094         return BLI_add_slash(filename); /* new strlen() */
1095 }
1096
1097 static int ptcache_filename(PTCacheID *pid, char *filename, int cfra, short do_path, short do_ext)
1098 {
1099         int len=0;
1100         char *idname;
1101         char *newname;
1102         filename[0] = '\0';
1103         newname = filename;
1104         
1105         if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return 0; /* save blend file before using disk pointcache */
1106         
1107         /* start with temp dir */
1108         if (do_path) {
1109                 len = ptcache_path(pid, filename);
1110                 newname += len;
1111         }
1112         if (pid->cache->name[0] == '\0' && (pid->cache->flag & PTCACHE_EXTERNAL)==0) {
1113                 idname = (pid->ob->id.name+2);
1114                 /* convert chars to hex so they are always a valid filename */
1115                 while ('\0' != *idname) {
1116                         BLI_snprintf(newname, MAX_PTCACHE_FILE, "%02X", (char)(*idname++));
1117                         newname+=2;
1118                         len += 2;
1119                 }
1120         }
1121         else {
1122                 int temp = (int)strlen(pid->cache->name); 
1123                 strcpy(newname, pid->cache->name); 
1124                 newname+=temp;
1125                 len += temp;
1126         }
1127
1128         if (do_ext) {
1129
1130                 if (pid->cache->index < 0)
1131                         pid->cache->index =  pid->stack_index = BKE_object_insert_ptcache(pid->ob);
1132
1133                 if (pid->cache->flag & PTCACHE_EXTERNAL) {
1134                         if (pid->cache->index >= 0)
1135                                 BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02u"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
1136                         else
1137                                 BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d"PTCACHE_EXT, cfra); /* always 6 chars */
1138                 }
1139                 else {
1140                         BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02u"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
1141                 }
1142                 len += 16;
1143         }
1144         
1145         return len; /* make sure the above string is always 16 chars */
1146 }
1147
1148 /* youll need to close yourself after! */
1149 static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
1150 {
1151         PTCacheFile *pf;
1152         FILE *fp = NULL;
1153         char filename[(FILE_MAX)*2];
1154
1155 #ifndef DURIAN_POINTCACHE_LIB_OK
1156         /* don't allow writing for linked objects */
1157         if (pid->ob->id.lib && mode == PTCACHE_FILE_WRITE)
1158                 return NULL;
1159 #endif
1160         if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return NULL; /* save blend file before using disk pointcache */
1161         
1162         ptcache_filename(pid, filename, cfra, 1, 1);
1163
1164         if (mode==PTCACHE_FILE_READ) {
1165                 if (!BLI_exists(filename)) {
1166                         return NULL;
1167                 }
1168                 fp = BLI_fopen(filename, "rb");
1169         }
1170         else if (mode==PTCACHE_FILE_WRITE) {
1171                 BLI_make_existing_file(filename); /* will create the dir if needs be, same as //textures is created */
1172                 fp = BLI_fopen(filename, "wb");
1173         }
1174         else if (mode==PTCACHE_FILE_UPDATE) {
1175                 BLI_make_existing_file(filename);
1176                 fp = BLI_fopen(filename, "rb+");
1177         }
1178
1179         if (!fp)
1180                 return NULL;
1181
1182         pf= MEM_mallocN(sizeof(PTCacheFile), "PTCacheFile");
1183         pf->fp= fp;
1184         pf->old_format = 0;
1185         pf->frame = cfra;
1186
1187         return pf;
1188 }
1189 static void ptcache_file_close(PTCacheFile *pf)
1190 {
1191         if (pf) {
1192                 fclose(pf->fp);
1193                 MEM_freeN(pf);
1194         }
1195 }
1196
1197 static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result, unsigned int len)
1198 {
1199         int r = 0;
1200         unsigned char compressed = 0;
1201         size_t in_len;
1202 #ifdef WITH_LZO
1203         size_t out_len = len;
1204 #endif
1205         unsigned char *in;
1206         unsigned char *props = MEM_callocN(16*sizeof(char), "tmp");
1207
1208         ptcache_file_read(pf, &compressed, 1, sizeof(unsigned char));
1209         if (compressed) {
1210                 unsigned int size;
1211                 ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
1212                 in_len = (size_t)size;
1213                 if (in_len==0) {
1214                         /* do nothing */
1215                 }
1216                 else {
1217                         in = (unsigned char *)MEM_callocN(sizeof(unsigned char)*in_len, "pointcache_compressed_buffer");
1218                         ptcache_file_read(pf, in, in_len, sizeof(unsigned char));
1219 #ifdef WITH_LZO
1220                         if (compressed == 1)
1221                                 r = lzo1x_decompress_safe(in, (lzo_uint)in_len, result, (lzo_uint *)&out_len, NULL);
1222 #endif
1223 #ifdef WITH_LZMA
1224                         if (compressed == 2) {
1225                                 size_t sizeOfIt;
1226                                 size_t leni = in_len, leno = len;
1227                                 ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
1228                                 sizeOfIt = (size_t)size;
1229                                 ptcache_file_read(pf, props, sizeOfIt, sizeof(unsigned char));
1230                                 r = LzmaUncompress(result, &leno, in, &leni, props, sizeOfIt);
1231                         }
1232 #endif
1233                         MEM_freeN(in);
1234                 }
1235         }
1236         else {
1237                 ptcache_file_read(pf, result, len, sizeof(unsigned char));
1238         }
1239
1240         MEM_freeN(props);
1241
1242         return r;
1243 }
1244 static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, unsigned int in_len, unsigned char *out, int mode)
1245 {
1246         int r = 0;
1247         unsigned char compressed = 0;
1248         size_t out_len= 0;
1249         unsigned char *props = MEM_callocN(16*sizeof(char), "tmp");
1250         size_t sizeOfIt = 5;
1251
1252         (void)mode; /* unused when building w/o compression */
1253
1254 #ifdef WITH_LZO
1255         out_len= LZO_OUT_LEN(in_len);
1256         if (mode == 1) {
1257                 LZO_HEAP_ALLOC(wrkmem, LZO1X_MEM_COMPRESS);
1258                 
1259                 r = lzo1x_1_compress(in, (lzo_uint)in_len, out, (lzo_uint *)&out_len, wrkmem);  
1260                 if (!(r == LZO_E_OK) || (out_len >= in_len))
1261                         compressed = 0;
1262                 else
1263                         compressed = 1;
1264         }
1265 #endif
1266 #ifdef WITH_LZMA
1267         if (mode == 2) {
1268                 
1269                 r = LzmaCompress(out, &out_len, in, in_len, //assume sizeof(char)==1....
1270                                                 props, &sizeOfIt, 5, 1 << 24, 3, 0, 2, 32, 2);
1271
1272                 if (!(r == SZ_OK) || (out_len >= in_len))
1273                         compressed = 0;
1274                 else
1275                         compressed = 2;
1276         }
1277 #endif
1278         
1279         ptcache_file_write(pf, &compressed, 1, sizeof(unsigned char));
1280         if (compressed) {
1281                 unsigned int size = out_len;
1282                 ptcache_file_write(pf, &size, 1, sizeof(unsigned int));
1283                 ptcache_file_write(pf, out, out_len, sizeof(unsigned char));
1284         }
1285         else
1286                 ptcache_file_write(pf, in, in_len, sizeof(unsigned char));
1287
1288         if (compressed == 2) {
1289                 unsigned int size = sizeOfIt;
1290                 ptcache_file_write(pf, &sizeOfIt, 1, sizeof(unsigned int));
1291                 ptcache_file_write(pf, props, size, sizeof(unsigned char));
1292         }
1293
1294         MEM_freeN(props);
1295
1296         return r;
1297 }
1298 static int ptcache_file_read(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size)
1299 {
1300         return (fread(f, size, tot, pf->fp) == tot);
1301 }
1302 static int ptcache_file_write(PTCacheFile *pf, const void *f, unsigned int tot, unsigned int size)
1303 {
1304         return (fwrite(f, size, tot, pf->fp) == tot);
1305 }
1306 static int ptcache_file_data_read(PTCacheFile *pf)
1307 {
1308         int i;
1309
1310         for (i=0; i<BPHYS_TOT_DATA; i++) {
1311                 if ((pf->data_types & (1<<i)) && !ptcache_file_read(pf, pf->cur[i], 1, ptcache_data_size[i]))
1312                         return 0;
1313         }
1314         
1315         return 1;
1316 }
1317 static int ptcache_file_data_write(PTCacheFile *pf)
1318 {               
1319         int i;
1320
1321         for (i=0; i<BPHYS_TOT_DATA; i++) {
1322                 if ((pf->data_types & (1<<i)) && !ptcache_file_write(pf, pf->cur[i], 1, ptcache_data_size[i]))
1323                         return 0;
1324         }
1325         
1326         return 1;
1327 }
1328 static int ptcache_file_header_begin_read(PTCacheFile *pf)
1329 {
1330         unsigned int typeflag=0;
1331         int error=0;
1332         char bphysics[8];
1333         
1334         pf->data_types = 0;
1335         
1336         if (fread(bphysics, sizeof(char), 8, pf->fp) != 8)
1337                 error = 1;
1338         
1339         if (!error && strncmp(bphysics, "BPHYSICS", 8))
1340                 error = 1;
1341
1342         if (!error && !fread(&typeflag, sizeof(unsigned int), 1, pf->fp))
1343                 error = 1;
1344
1345         pf->type = (typeflag & PTCACHE_TYPEFLAG_TYPEMASK);
1346         pf->flag = (typeflag & PTCACHE_TYPEFLAG_FLAGMASK);
1347         
1348         /* if there was an error set file as it was */
1349         if (error)
1350                 fseek(pf->fp, 0, SEEK_SET);
1351
1352         return !error;
1353 }
1354 static int ptcache_file_header_begin_write(PTCacheFile *pf)
1355 {
1356         const char *bphysics = "BPHYSICS";
1357         unsigned int typeflag = pf->type + pf->flag;
1358         
1359         if (fwrite(bphysics, sizeof(char), 8, pf->fp) != 8)
1360                 return 0;
1361
1362         if (!fwrite(&typeflag, sizeof(unsigned int), 1, pf->fp))
1363                 return 0;
1364         
1365         return 1;
1366 }
1367
1368 /* Data pointer handling */
1369 int BKE_ptcache_data_size(int data_type)
1370 {
1371         return ptcache_data_size[data_type];
1372 }
1373
1374 static void ptcache_file_pointers_init(PTCacheFile *pf)
1375 {
1376         int data_types = pf->data_types;
1377
1378         pf->cur[BPHYS_DATA_INDEX] =             (data_types & (1<<BPHYS_DATA_INDEX))    ?               &pf->data.index : NULL;
1379         pf->cur[BPHYS_DATA_LOCATION] =  (data_types & (1<<BPHYS_DATA_LOCATION)) ?               &pf->data.loc   : NULL;
1380         pf->cur[BPHYS_DATA_VELOCITY] =  (data_types & (1<<BPHYS_DATA_VELOCITY)) ?               &pf->data.vel   : NULL;
1381         pf->cur[BPHYS_DATA_ROTATION] =  (data_types & (1<<BPHYS_DATA_ROTATION)) ?               &pf->data.rot   : NULL;
1382         pf->cur[BPHYS_DATA_AVELOCITY] = (data_types & (1<<BPHYS_DATA_AVELOCITY))?               &pf->data.ave   : NULL;
1383         pf->cur[BPHYS_DATA_SIZE] =              (data_types & (1<<BPHYS_DATA_SIZE))             ?               &pf->data.size  : NULL;
1384         pf->cur[BPHYS_DATA_TIMES] =             (data_types & (1<<BPHYS_DATA_TIMES))    ?               &pf->data.times : NULL;
1385         pf->cur[BPHYS_DATA_BOIDS] =             (data_types & (1<<BPHYS_DATA_BOIDS))    ?               &pf->data.boids : NULL;
1386 }
1387
1388 /* Check to see if point number "index" is in pm, uses binary search for index data. */
1389 int BKE_ptcache_mem_index_find(PTCacheMem *pm, unsigned int index)
1390 {
1391         if (pm->data[BPHYS_DATA_INDEX]) {
1392                 unsigned int *data = pm->data[BPHYS_DATA_INDEX];
1393                 unsigned int mid, low = 0, high = pm->totpoint - 1;
1394
1395                 if (index < *data || index > *(data+high))
1396                         return -1;
1397
1398                 /* check simple case for continuous indexes first */
1399                 if (index-*data < high && data[index-*data] == index)
1400                         return index-*data;
1401
1402                 while (low <= high) {
1403                         mid= (low + high)/2;
1404
1405                         if (data[mid] > index)
1406                                 high = mid - 1;
1407                         else if (data[mid] < index)
1408                                 low = mid + 1;
1409                         else
1410                                 return mid;
1411                 }
1412
1413                 return -1;
1414         }
1415         else {
1416                 return (index < pm->totpoint ? index : -1);
1417         }
1418 }
1419
1420 void BKE_ptcache_mem_pointers_init(PTCacheMem *pm)
1421 {
1422         int data_types = pm->data_types;
1423         int i;
1424
1425         for (i=0; i<BPHYS_TOT_DATA; i++)
1426                 pm->cur[i] = ((data_types & (1<<i)) ? pm->data[i] : NULL);
1427 }
1428
1429 void BKE_ptcache_mem_pointers_incr(PTCacheMem *pm)
1430 {
1431         int i;
1432
1433         for (i=0; i<BPHYS_TOT_DATA; i++) {
1434                 if (pm->cur[i])
1435                         pm->cur[i] = (char*)pm->cur[i] + ptcache_data_size[i];
1436         }
1437 }
1438 int  BKE_ptcache_mem_pointers_seek(int point_index, PTCacheMem *pm)
1439 {
1440         int data_types = pm->data_types;
1441         int i, index = BKE_ptcache_mem_index_find(pm, point_index);
1442
1443         if (index < 0) {
1444                 /* Can't give proper location without reallocation, so don't give any location.
1445                  * Some points will be cached improperly, but this only happens with simulation
1446                  * steps bigger than cache->step, so the cache has to be recalculated anyways
1447                  * at some point.
1448                  */
1449                 return 0;
1450         }
1451
1452         for (i=0; i<BPHYS_TOT_DATA; i++)
1453                 pm->cur[i] = data_types & (1<<i) ? (char*)pm->data[i] + index * ptcache_data_size[i] : NULL;
1454
1455         return 1;
1456 }
1457 static void ptcache_data_alloc(PTCacheMem *pm)
1458 {
1459         int data_types = pm->data_types;
1460         int totpoint = pm->totpoint;
1461         int i;
1462
1463         for (i=0; i<BPHYS_TOT_DATA; i++) {
1464                 if (data_types & (1<<i))
1465                         pm->data[i] = MEM_callocN(totpoint * ptcache_data_size[i], "PTCache Data");
1466         }
1467 }
1468 static void ptcache_data_free(PTCacheMem *pm)
1469 {
1470         void **data = pm->data;
1471         int i;
1472
1473         for (i=0; i<BPHYS_TOT_DATA; i++) {
1474                 if (data[i])
1475                         MEM_freeN(data[i]);
1476         }
1477 }
1478 static void ptcache_data_copy(void *from[], void *to[])
1479 {
1480         int i;
1481         for (i=0; i<BPHYS_TOT_DATA; i++) {
1482         /* note, durian file 03.4b_comp crashes if to[i] is not tested
1483          * its NULL, not sure if this should be fixed elsewhere but for now its needed */
1484                 if (from[i] && to[i])
1485                         memcpy(to[i], from[i], ptcache_data_size[i]);
1486         }
1487 }
1488
1489 static void ptcache_extra_free(PTCacheMem *pm)
1490 {
1491         PTCacheExtra *extra = pm->extradata.first;
1492
1493         if (extra) {
1494                 for (; extra; extra=extra->next) {
1495                         if (extra->data)
1496                                 MEM_freeN(extra->data);
1497                 }
1498
1499                 BLI_freelistN(&pm->extradata);
1500         }
1501 }
1502 static int ptcache_old_elemsize(PTCacheID *pid)
1503 {
1504         if (pid->type==PTCACHE_TYPE_SOFTBODY)
1505                 return 6 * sizeof(float);
1506         else if (pid->type==PTCACHE_TYPE_PARTICLES)
1507                 return sizeof(ParticleKey);
1508         else if (pid->type==PTCACHE_TYPE_CLOTH)
1509                 return 9 * sizeof(float);
1510
1511         return 0;
1512 }
1513
1514 static void ptcache_find_frames_around(PTCacheID *pid, unsigned int frame, int *fra1, int *fra2)
1515 {
1516         if (pid->cache->flag & PTCACHE_DISK_CACHE) {
1517                 int cfra1=frame, cfra2=frame+1;
1518
1519                 while (cfra1 >= pid->cache->startframe && !BKE_ptcache_id_exist(pid, cfra1))
1520                         cfra1--;
1521
1522                 if (cfra1 < pid->cache->startframe)
1523                         cfra1 = 0;
1524
1525                 while (cfra2 <= pid->cache->endframe && !BKE_ptcache_id_exist(pid, cfra2))
1526                         cfra2++;
1527
1528                 if (cfra2 > pid->cache->endframe)
1529                         cfra2 = 0;
1530
1531                 if (cfra1 && !cfra2) {
1532                         *fra1 = 0;
1533                         *fra2 = cfra1;
1534                 }
1535                 else {
1536                         *fra1 = cfra1;
1537                         *fra2 = cfra2;
1538                 }
1539         }
1540         else if (pid->cache->mem_cache.first) {
1541                 PTCacheMem *pm = pid->cache->mem_cache.first;
1542                 PTCacheMem *pm2 = pid->cache->mem_cache.last;
1543
1544                 while (pm->next && pm->next->frame <= frame)
1545                         pm= pm->next;
1546
1547                 if (pm2->frame < frame) {
1548                         pm2 = NULL;
1549                 }
1550                 else {
1551                         while (pm2->prev && pm2->prev->frame > frame) {
1552                                 pm2= pm2->prev;
1553                         }
1554                 }
1555
1556                 if (!pm2) {
1557                         *fra1 = 0;
1558                         *fra2 = pm->frame;
1559                 }
1560                 else {
1561                         *fra1 = pm->frame;
1562                         *fra2 = pm2->frame;
1563                 }
1564         }
1565 }
1566
1567 static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
1568 {
1569         PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
1570         PTCacheMem *pm = NULL;
1571         unsigned int i, error = 0;
1572
1573         if (pf == NULL)
1574                 return NULL;
1575
1576         if (!ptcache_file_header_begin_read(pf))
1577                 error = 1;
1578
1579         if (!error && (pf->type != pid->type || !pid->read_header(pf)))
1580                 error = 1;
1581
1582         if (!error) {
1583                 pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
1584
1585                 pm->totpoint = pf->totpoint;
1586                 pm->data_types = pf->data_types;
1587                 pm->frame = pf->frame;
1588
1589                 ptcache_data_alloc(pm);
1590
1591                 if (pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
1592                         for (i=0; i<BPHYS_TOT_DATA; i++) {
1593                                 unsigned int out_len = pm->totpoint*ptcache_data_size[i];
1594                                 if (pf->data_types & (1<<i))
1595                                         ptcache_file_compressed_read(pf, (unsigned char*)(pm->data[i]), out_len);
1596                         }
1597                 }
1598                 else {
1599                         BKE_ptcache_mem_pointers_init(pm);
1600                         ptcache_file_pointers_init(pf);
1601
1602                         for (i=0; i<pm->totpoint; i++) {
1603                                 if (!ptcache_file_data_read(pf)) {
1604                                         error = 1;
1605                                         break;
1606                                 }
1607                                 ptcache_data_copy(pf->cur, pm->cur);
1608                                 BKE_ptcache_mem_pointers_incr(pm);
1609                         }
1610                 }
1611         }
1612
1613         if (!error && pf->flag & PTCACHE_TYPEFLAG_EXTRADATA) {
1614                 unsigned int extratype = 0;
1615
1616                 while (ptcache_file_read(pf, &extratype, 1, sizeof(unsigned int))) {
1617                         PTCacheExtra *extra = MEM_callocN(sizeof(PTCacheExtra), "Pointcache extradata");
1618
1619                         extra->type = extratype;
1620
1621                         ptcache_file_read(pf, &extra->totdata, 1, sizeof(unsigned int));
1622
1623                         extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Pointcache extradata->data");
1624
1625                         if (pf->flag & PTCACHE_TYPEFLAG_COMPRESS)
1626                                 ptcache_file_compressed_read(pf, (unsigned char*)(extra->data), extra->totdata*ptcache_extra_datasize[extra->type]);
1627                         else
1628                                 ptcache_file_read(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
1629
1630                         BLI_addtail(&pm->extradata, extra);
1631                 }
1632         }
1633
1634         if (error && pm) {
1635                 ptcache_data_free(pm);
1636                 ptcache_extra_free(pm);
1637                 MEM_freeN(pm);
1638                 pm = NULL;
1639         }
1640
1641         ptcache_file_close(pf);
1642
1643         if (error && G.debug & G_DEBUG)
1644                 printf("Error reading from disk cache\n");
1645         
1646         return pm;
1647 }
1648 static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
1649 {
1650         PTCacheFile *pf = NULL;
1651         unsigned int i, error = 0;
1652         
1653         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, pm->frame);
1654
1655         pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, pm->frame);
1656
1657         if (pf==NULL) {
1658                 if (G.debug & G_DEBUG)
1659                         printf("Error opening disk cache file for writing\n");
1660                 return 0;
1661         }
1662
1663         pf->data_types = pm->data_types;
1664         pf->totpoint = pm->totpoint;
1665         pf->type = pid->type;
1666         pf->flag = 0;
1667         
1668         if (pm->extradata.first)
1669                 pf->flag |= PTCACHE_TYPEFLAG_EXTRADATA;
1670         
1671         if (pid->cache->compression)
1672                 pf->flag |= PTCACHE_TYPEFLAG_COMPRESS;
1673
1674         if (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf))
1675                 error = 1;
1676
1677         if (!error) {
1678                 if (pid->cache->compression) {
1679                         for (i=0; i<BPHYS_TOT_DATA; i++) {
1680                                 if (pm->data[i]) {
1681                                         unsigned int in_len = pm->totpoint*ptcache_data_size[i];
1682                                         unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
1683                                         ptcache_file_compressed_write(pf, (unsigned char*)(pm->data[i]), in_len, out, pid->cache->compression);
1684                                         MEM_freeN(out);
1685                                 }
1686                         }
1687                 }
1688                 else {
1689                         BKE_ptcache_mem_pointers_init(pm);
1690                         ptcache_file_pointers_init(pf);
1691
1692                         for (i=0; i<pm->totpoint; i++) {
1693                                 ptcache_data_copy(pm->cur, pf->cur);
1694                                 if (!ptcache_file_data_write(pf)) {
1695                                         error = 1;
1696                                         break;
1697                                 }
1698                                 BKE_ptcache_mem_pointers_incr(pm);
1699                         }
1700                 }
1701         }
1702
1703         if (!error && pm->extradata.first) {
1704                 PTCacheExtra *extra = pm->extradata.first;
1705
1706                 for (; extra; extra=extra->next) {
1707                         if (extra->data == NULL || extra->totdata == 0)
1708                                 continue;
1709
1710                         ptcache_file_write(pf, &extra->type, 1, sizeof(unsigned int));
1711                         ptcache_file_write(pf, &extra->totdata, 1, sizeof(unsigned int));
1712
1713                         if (pid->cache->compression) {
1714                                 unsigned int in_len = extra->totdata * ptcache_extra_datasize[extra->type];
1715                                 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
1716                                 ptcache_file_compressed_write(pf, (unsigned char*)(extra->data), in_len, out, pid->cache->compression);
1717                                 MEM_freeN(out);
1718                         }
1719                         else {
1720                                 ptcache_file_write(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
1721                         }
1722                 }
1723         }
1724
1725         ptcache_file_close(pf);
1726         
1727         if (error && G.debug & G_DEBUG)
1728                 printf("Error writing to disk cache\n");
1729
1730         return error==0;
1731 }
1732
1733 static int ptcache_read_stream(PTCacheID *pid, int cfra)
1734 {
1735         PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
1736         int error = 0;
1737
1738         if (pid->read_stream == NULL)
1739                 return 0;
1740
1741         if (pf == NULL) {
1742                 if (G.debug & G_DEBUG)
1743                         printf("Error opening disk cache file for reading\n");
1744                 return 0;
1745         }
1746
1747         if (!ptcache_file_header_begin_read(pf))
1748                 error = 1;
1749
1750         if (!error && (pf->type != pid->type || !pid->read_header(pf)))
1751                 error = 1;
1752
1753         if (!error && pf->totpoint != pid->totpoint(pid->calldata, cfra))
1754                 error = 1;
1755
1756         if (!error) {
1757                 ptcache_file_pointers_init(pf);
1758
1759                 // we have stream reading here
1760                 if (!pid->read_stream(pf, pid->calldata))
1761                         error = 1;
1762         }
1763
1764         ptcache_file_close(pf);
1765         
1766         return error == 0;
1767 }
1768 static int ptcache_read(PTCacheID *pid, int cfra)
1769 {
1770         PTCacheMem *pm = NULL;
1771         int i;
1772         int *index = &i;
1773
1774         /* get a memory cache to read from */
1775         if (pid->cache->flag & PTCACHE_DISK_CACHE) {
1776                 pm = ptcache_disk_frame_to_mem(pid, cfra);
1777         }
1778         else {
1779                 pm = pid->cache->mem_cache.first;
1780                 
1781                 while (pm && pm->frame != cfra)
1782                         pm = pm->next;
1783         }
1784
1785         /* read the cache */
1786         if (pm) {
1787                 int totpoint = pm->totpoint;
1788
1789                 if ((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
1790                         totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, cfra));
1791
1792                 BKE_ptcache_mem_pointers_init(pm);
1793
1794                 for (i=0; i<totpoint; i++) {
1795                         if (pm->data_types & (1<<BPHYS_DATA_INDEX))
1796                                 index = pm->cur[BPHYS_DATA_INDEX];
1797
1798                         pid->read_point(*index, pid->calldata, pm->cur, (float)pm->frame, NULL);
1799                 
1800                         BKE_ptcache_mem_pointers_incr(pm);
1801                 }
1802
1803                 if (pid->read_extra_data && pm->extradata.first)
1804                         pid->read_extra_data(pid->calldata, pm, (float)pm->frame);
1805
1806                 /* clean up temporary memory cache */
1807                 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
1808                         ptcache_data_free(pm);
1809                         ptcache_extra_free(pm);
1810                         MEM_freeN(pm);
1811                 }
1812         }
1813
1814         return 1;
1815 }
1816 static int ptcache_interpolate(PTCacheID *pid, float cfra, int cfra1, int cfra2)
1817 {
1818         PTCacheMem *pm = NULL;
1819         int i;
1820         int *index = &i;
1821
1822         /* get a memory cache to read from */
1823         if (pid->cache->flag & PTCACHE_DISK_CACHE) {
1824                 pm = ptcache_disk_frame_to_mem(pid, cfra2);
1825         }
1826         else {
1827                 pm = pid->cache->mem_cache.first;
1828                 
1829                 while (pm && pm->frame != cfra2)
1830                         pm = pm->next;
1831         }
1832
1833         /* read the cache */
1834         if (pm) {
1835                 int totpoint = pm->totpoint;
1836
1837                 if ((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
1838                         totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, (int)cfra));
1839
1840                 BKE_ptcache_mem_pointers_init(pm);
1841
1842                 for (i=0; i<totpoint; i++) {
1843                         if (pm->data_types & (1<<BPHYS_DATA_INDEX))
1844                                 index = pm->cur[BPHYS_DATA_INDEX];
1845
1846                         pid->interpolate_point(*index, pid->calldata, pm->cur, cfra, (float)cfra1, (float)cfra2, NULL);
1847                         BKE_ptcache_mem_pointers_incr(pm);
1848                 }
1849
1850                 if (pid->interpolate_extra_data && pm->extradata.first)
1851                         pid->interpolate_extra_data(pid->calldata, pm, cfra, (float)cfra1, (float)cfra2);
1852
1853                 /* clean up temporary memory cache */
1854                 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
1855                         ptcache_data_free(pm);
1856                         ptcache_extra_free(pm);
1857                         MEM_freeN(pm);
1858                 }
1859         }
1860
1861         return 1;
1862 }
1863 /* reads cache from disk or memory */
1864 /* possible to get old or interpolated result */
1865 int BKE_ptcache_read(PTCacheID *pid, float cfra)
1866 {
1867         int cfrai = (int)floor(cfra), cfra1=0, cfra2=0;
1868         int ret = 0;
1869
1870         /* nothing to read to */
1871         if (pid->totpoint(pid->calldata, cfrai) == 0)
1872                 return 0;
1873
1874         if (pid->cache->flag & PTCACHE_READ_INFO) {
1875                 pid->cache->flag &= ~PTCACHE_READ_INFO;
1876                 ptcache_read(pid, 0);
1877         }
1878
1879         /* first check if we have the actual frame cached */
1880         if (cfra == (float)cfrai && BKE_ptcache_id_exist(pid, cfrai))
1881                 cfra1 = cfrai;
1882
1883         /* no exact cache frame found so try to find cached frames around cfra */
1884         if (cfra1 == 0)
1885                 ptcache_find_frames_around(pid, cfrai, &cfra1, &cfra2);
1886
1887         if (cfra1 == 0 && cfra2 == 0)
1888                 return 0;
1889
1890         /* don't read old cache if already simulated past cached frame */
1891         if (cfra1 == 0 && cfra2 && cfra2 <= pid->cache->simframe)
1892                 return 0;
1893         if (cfra1 && cfra1 == cfra2)
1894                 return 0;
1895
1896         if (cfra1) {
1897                 
1898                 if (pid->read_stream) {
1899                         if (!ptcache_read_stream(pid, cfra1))
1900                                 return 0;
1901                 }
1902                 else if (pid->read_point)
1903                         ptcache_read(pid, cfra1);
1904         }
1905
1906         if (cfra2) {
1907                 
1908                 if (pid->read_stream) {
1909                         if (!ptcache_read_stream(pid, cfra2))
1910                                 return 0;
1911                 }
1912                 else if (pid->read_point) {
1913                         if (cfra1 && cfra2 && pid->interpolate_point)
1914                                 ptcache_interpolate(pid, cfra, cfra1, cfra2);
1915                         else
1916                                 ptcache_read(pid, cfra2);
1917                 }
1918         }
1919
1920         if (cfra1)
1921                 ret = (cfra2 ? PTCACHE_READ_INTERPOLATED : PTCACHE_READ_EXACT);
1922         else if (cfra2) {
1923                 ret = PTCACHE_READ_OLD;
1924                 pid->cache->simframe = cfra2;
1925         }
1926
1927         if ((pid->cache->flag & PTCACHE_QUICK_CACHE)==0) {
1928                 cfrai = (int)cfra;
1929                 /* clear invalid cache frames so that better stuff can be simulated */
1930                 if (pid->cache->flag & PTCACHE_OUTDATED) {
1931                         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, cfrai);
1932                 }
1933                 else if (pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
1934                         if (cfra <= pid->cache->last_exact)
1935                                 pid->cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
1936
1937                         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, MAX2(cfrai, pid->cache->last_exact));
1938                 }
1939         }
1940
1941         return ret;
1942 }
1943 static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
1944 {
1945         PTCacheFile *pf = NULL;
1946         int error = 0;
1947         
1948         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, cfra);
1949
1950         pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, cfra);
1951
1952         if (pf==NULL) {
1953                 if (G.debug & G_DEBUG)
1954                         printf("Error opening disk cache file for writing\n");
1955                 return 0;
1956         }
1957
1958         pf->data_types = pid->data_types;
1959         pf->totpoint = totpoint;
1960         pf->type = pid->type;
1961         pf->flag = 0;
1962
1963         if (!error && (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf)))
1964                 error = 1;
1965
1966         if (!error && pid->write_stream)
1967                 pid->write_stream(pf, pid->calldata);
1968
1969         ptcache_file_close(pf);
1970
1971         if (error && G.debug & G_DEBUG)
1972                 printf("Error writing to disk cache\n");
1973
1974         return error == 0;
1975 }
1976 static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
1977 {
1978         PointCache *cache = pid->cache;
1979         PTCacheMem *pm=NULL, *pm2=NULL;
1980         int totpoint = pid->totpoint(pid->calldata, cfra);
1981         int i, error = 0;
1982
1983         pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
1984
1985         pm->totpoint = pid->totwrite(pid->calldata, cfra);
1986         pm->data_types = cfra ? pid->data_types : pid->info_types;
1987
1988         ptcache_data_alloc(pm);
1989         BKE_ptcache_mem_pointers_init(pm);
1990
1991         if (overwrite) {
1992                 if (cache->flag & PTCACHE_DISK_CACHE) {
1993                         int fra = cfra-1;
1994
1995                         while (fra >= cache->startframe && !BKE_ptcache_id_exist(pid, fra))
1996                                 fra--;
1997                         
1998                         pm2 = ptcache_disk_frame_to_mem(pid, fra);
1999                 }
2000                 else
2001                         pm2 = cache->mem_cache.last;
2002         }
2003
2004         if (pid->write_point) {
2005                 for (i=0; i<totpoint; i++) {
2006                         int write = pid->write_point(i, pid->calldata, pm->cur, cfra);
2007                         if (write) {
2008                                 BKE_ptcache_mem_pointers_incr(pm);
2009
2010                                 /* newly born particles have to be copied to previous cached frame */
2011                                 if (overwrite && write == 2 && pm2 && BKE_ptcache_mem_pointers_seek(i, pm2))
2012                                         pid->write_point(i, pid->calldata, pm2->cur, cfra);
2013                         }
2014                 }
2015         }
2016
2017         if (pid->write_extra_data)
2018                 pid->write_extra_data(pid->calldata, pm, cfra);
2019
2020         pm->frame = cfra;
2021
2022         if (cache->flag & PTCACHE_DISK_CACHE) {
2023                 error += !ptcache_mem_frame_to_disk(pid, pm);
2024
2025                 // if (pm) /* pm is always set */
2026                 {
2027                         ptcache_data_free(pm);
2028                         ptcache_extra_free(pm);
2029                         MEM_freeN(pm);
2030                 }
2031
2032                 if (pm2) {
2033                         error += !ptcache_mem_frame_to_disk(pid, pm2);
2034                         ptcache_data_free(pm2);
2035                         ptcache_extra_free(pm2);
2036                         MEM_freeN(pm2);
2037                 }
2038         }
2039         else {
2040                 BLI_addtail(&cache->mem_cache, pm);
2041         }
2042
2043         return error;
2044 }
2045 static int ptcache_write_needed(PTCacheID *pid, int cfra, int *overwrite)
2046 {
2047         PointCache *cache = pid->cache;
2048         int ofra = 0, efra = cache->endframe;
2049
2050         /* allways start from scratch on the first frame */
2051         if (cfra && cfra == cache->startframe) {
2052                 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, cfra);
2053                 cache->flag &= ~PTCACHE_REDO_NEEDED;
2054                 return 1;
2055         }
2056
2057         if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2058                 if (cfra==0 && cache->startframe > 0)
2059                         return 1;
2060
2061                                 /* find last cached frame */
2062                 while (efra > cache->startframe && !BKE_ptcache_id_exist(pid, efra))
2063                         efra--;
2064
2065                 /* find second last cached frame */
2066                 ofra = efra-1;
2067                 while (ofra > cache->startframe && !BKE_ptcache_id_exist(pid, ofra))
2068                         ofra--;
2069         }
2070         else {
2071                 PTCacheMem *pm = cache->mem_cache.last;
2072                 /* don't write info file in memory */
2073                 if (cfra == 0)
2074                         return 0;
2075
2076                 if (pm == NULL)
2077                         return 1;
2078
2079                 efra = pm->frame;
2080                 ofra = (pm->prev ? pm->prev->frame : efra - cache->step);
2081         }
2082
2083         if (efra >= cache->startframe && cfra > efra) {
2084                 if (ofra >= cache->startframe && efra - ofra < cache->step) {
2085                         /* overwrite previous frame */
2086                         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, efra);
2087                         *overwrite = 1;
2088                 }
2089                 return 1;
2090         }
2091
2092         return 0;
2093 }
2094 /* writes cache to disk or memory */
2095 int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
2096 {
2097         PointCache *cache = pid->cache;
2098         int totpoint = pid->totpoint(pid->calldata, cfra);
2099         int overwrite = 0, error = 0;
2100
2101         if (totpoint == 0 || (cfra ? pid->data_types == 0 : pid->info_types == 0))
2102                 return 0;
2103
2104         if (ptcache_write_needed(pid, cfra, &overwrite)==0)
2105                 return 0;
2106
2107         if (pid->write_stream) {
2108                 ptcache_write_stream(pid, cfra, totpoint);
2109         }
2110         else if (pid->write_point) {
2111                 error += ptcache_write(pid, cfra, overwrite);
2112         }
2113
2114         /* Mark frames skipped if more than 1 frame forwards since last non-skipped frame. */
2115         if (cfra - cache->last_exact == 1 || cfra == cache->startframe) {
2116                 cache->last_exact = cfra;
2117                 cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
2118         }
2119         /* Don't mark skipped when writing info file (frame 0) */
2120         else if (cfra)
2121                 cache->flag |= PTCACHE_FRAMES_SKIPPED;
2122
2123         /* Update timeline cache display */
2124         if (cfra && cache->cached_frames)
2125                 cache->cached_frames[cfra-cache->startframe] = 1;
2126
2127         BKE_ptcache_update_info(pid);
2128
2129         return !error;
2130 }
2131 /* youll need to close yourself after!
2132  * mode - PTCACHE_CLEAR_ALL, 
2133  */
2134
2135 /* Clears & resets */
2136 void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
2137 {
2138         unsigned int len; /* store the length of the string */
2139         unsigned int sta, end;
2140
2141         /* mode is same as fopen's modes */
2142         DIR *dir; 
2143         struct dirent *de;
2144         char path[MAX_PTCACHE_PATH];
2145         char filename[MAX_PTCACHE_FILE];
2146         char path_full[MAX_PTCACHE_FILE];
2147         char ext[MAX_PTCACHE_PATH];
2148
2149         if (!pid || !pid->cache || pid->cache->flag & PTCACHE_BAKED)
2150                 return;
2151
2152         if (pid->cache->flag & PTCACHE_IGNORE_CLEAR)
2153                 return;
2154
2155         sta = pid->cache->startframe;
2156         end = pid->cache->endframe;
2157
2158 #ifndef DURIAN_POINTCACHE_LIB_OK
2159         /* don't allow clearing for linked objects */
2160         if (pid->ob->id.lib)
2161                 return;
2162 #endif
2163
2164         /*if (!G.relbase_valid) return; *//* save blend file before using pointcache */
2165         
2166         /* clear all files in the temp dir with the prefix of the ID and the ".bphys" suffix */
2167         switch (mode) {
2168         case PTCACHE_CLEAR_ALL:
2169         case PTCACHE_CLEAR_BEFORE:      
2170         case PTCACHE_CLEAR_AFTER:
2171                 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2172                         ptcache_path(pid, path);
2173                         
2174                         len = ptcache_filename(pid, filename, cfra, 0, 0); /* no path */
2175                         
2176                         dir = opendir(path);
2177                         if (dir==NULL)
2178                                 return;
2179
2180                         BLI_snprintf(ext, sizeof(ext), "_%02u"PTCACHE_EXT, pid->stack_index);
2181                         
2182                         while ((de = readdir(dir)) != NULL) {
2183                                 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2184                                         if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2185                                                 if (mode == PTCACHE_CLEAR_ALL) {
2186                                                         pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
2187                                                         BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
2188                                                         BLI_delete(path_full, 0, 0);
2189                                                 }
2190                                                 else {
2191                                                         /* read the number of the file */
2192                                                         unsigned int frame, len2 = (int)strlen(de->d_name);
2193                                                         char num[7];
2194
2195                                                         if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2196                                                                 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2197                                                                 frame = atoi(num);
2198                                                                 
2199                                                                 if ((mode == PTCACHE_CLEAR_BEFORE && frame < cfra) ||
2200                                                                     (mode == PTCACHE_CLEAR_AFTER && frame > cfra))
2201                                                                 {
2202                                                                         
2203                                                                         BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
2204                                                                         BLI_delete(path_full, 0, 0);
2205                                                                         if (pid->cache->cached_frames && frame >=sta && frame <= end)
2206                                                                                 pid->cache->cached_frames[frame-sta] = 0;
2207                                                                 }
2208                                                         }
2209                                                 }
2210                                         }
2211                                 }
2212                         }
2213                         closedir(dir);
2214
2215                         if (mode == PTCACHE_CLEAR_ALL && pid->cache->cached_frames)
2216                                 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
2217                 }
2218                 else {
2219                         PTCacheMem *pm= pid->cache->mem_cache.first;
2220                         PTCacheMem *link= NULL;
2221
2222                         if (mode == PTCACHE_CLEAR_ALL) {
2223                                 /*we want startframe if the cache starts before zero*/
2224                                 pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
2225                                 for (; pm; pm=pm->next) {
2226                                         ptcache_data_free(pm);
2227                                         ptcache_extra_free(pm);
2228                                 }
2229                                 BLI_freelistN(&pid->cache->mem_cache);
2230
2231                                 if (pid->cache->cached_frames) 
2232                                         memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
2233                         }
2234                         else {
2235                                 while (pm) {
2236                                         if ((mode == PTCACHE_CLEAR_BEFORE && pm->frame < cfra) ||
2237                                             (mode == PTCACHE_CLEAR_AFTER && pm->frame > cfra))
2238                                         {
2239                                                 link = pm;
2240                                                 if (pid->cache->cached_frames && pm->frame >=sta && pm->frame <= end)
2241                                                         pid->cache->cached_frames[pm->frame-sta] = 0;
2242                                                 ptcache_data_free(pm);
2243                                                 ptcache_extra_free(pm);
2244                                                 pm = pm->next;
2245                                                 BLI_freelinkN(&pid->cache->mem_cache, link);
2246                                         }
2247                                         else
2248                                                 pm = pm->next;
2249                                 }
2250                         }
2251                 }
2252                 break;
2253                 
2254         case PTCACHE_CLEAR_FRAME:
2255                 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2256                         if (BKE_ptcache_id_exist(pid, cfra)) {
2257                                 ptcache_filename(pid, filename, cfra, 1, 1); /* no path */
2258                                 BLI_delete(filename, 0, 0);
2259                         }
2260                 }
2261                 else {
2262                         PTCacheMem *pm = pid->cache->mem_cache.first;
2263
2264                         for (; pm; pm=pm->next) {
2265                                 if (pm->frame == cfra) {
2266                                         ptcache_data_free(pm);
2267                                         ptcache_extra_free(pm);
2268                                         BLI_freelinkN(&pid->cache->mem_cache, pm);
2269                                         break;
2270                                 }
2271                         }
2272                 }
2273                 if (pid->cache->cached_frames && cfra>=sta && cfra<=end)
2274                         pid->cache->cached_frames[cfra-sta] = 0;
2275                 break;
2276         }
2277
2278         BKE_ptcache_update_info(pid);
2279 }
2280 int  BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
2281 {
2282         if (!pid->cache)
2283                 return 0;
2284
2285         if (cfra<pid->cache->startframe || cfra > pid->cache->endframe)
2286                 return 0;
2287
2288         if (pid->cache->cached_frames &&        pid->cache->cached_frames[cfra-pid->cache->startframe]==0)
2289                 return 0;
2290         
2291         if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2292                 char filename[MAX_PTCACHE_FILE];
2293                 
2294                 ptcache_filename(pid, filename, cfra, 1, 1);
2295
2296                 return BLI_exists(filename);
2297         }
2298         else {
2299                 PTCacheMem *pm = pid->cache->mem_cache.first;
2300
2301                 for (; pm; pm=pm->next) {
2302                         if (pm->frame==cfra)
2303                                 return 1;
2304                 }
2305                 return 0;
2306         }
2307 }
2308 void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
2309 {
2310         /* Object *ob; */ /* UNUSED */
2311         PointCache *cache;
2312         /* float offset; unused for now */
2313         float time, nexttime;
2314
2315         /* TODO: this has to be sorted out once bsystem_time gets redone, */
2316         /*       now caches can handle interpolating etc. too - jahka */
2317
2318         /* time handling for point cache:
2319          * - simulation time is scaled by result of bsystem_time
2320          * - for offsetting time only time offset is taken into account, since
2321          *   that's always the same and can't be animated. a timeoffset which
2322          *   varies over time is not simple to support.
2323          * - field and motion blur offsets are currently ignored, proper solution
2324          *   is probably to interpolate results from two frames for that ..
2325          */
2326
2327         /* ob= pid->ob; */ /* UNUSED */
2328         cache= pid->cache;
2329
2330         if (timescale) {
2331                 time= BKE_scene_frame_get(scene);
2332                 nexttime = BKE_scene_frame_get_from_ctime(scene, CFRA + 1.0f);
2333                 
2334                 *timescale= MAX2(nexttime - time, 0.0f);
2335         }
2336
2337         if (startframe && endframe) {
2338                 *startframe= cache->startframe;
2339                 *endframe= cache->endframe;
2340
2341                 /* TODO: time handling with object offsets and simulated vs. cached
2342                  * particles isn't particularly easy, so for now what you see is what
2343                  * you get. In the future point cache could handle the whole particle
2344                  * system timing. */
2345 #if 0
2346                 if ((ob->partype & PARSLOW)==0) {
2347                         offset= ob->sf;
2348
2349                         *startframe += (int)(offset+0.5f);
2350                         *endframe += (int)(offset+0.5f);
2351                 }
2352 #endif
2353         }
2354
2355         /* verify cached_frames array is up to date */
2356         if (cache->cached_frames) {
2357                 if (MEM_allocN_len(cache->cached_frames) != sizeof(char) * (cache->endframe-cache->startframe+1)) {
2358                         MEM_freeN(cache->cached_frames);
2359                         cache->cached_frames = NULL;
2360                 }       
2361         }
2362
2363         if (cache->cached_frames==NULL && cache->endframe > cache->startframe) {
2364                 unsigned int sta=cache->startframe;
2365                 unsigned int end=cache->endframe;
2366
2367                 cache->cached_frames = MEM_callocN(sizeof(char) * (cache->endframe-cache->startframe+1), "cached frames array");
2368
2369                 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2370                         /* mode is same as fopen's modes */
2371                         DIR *dir; 
2372                         struct dirent *de;
2373                         char path[MAX_PTCACHE_PATH];
2374                         char filename[MAX_PTCACHE_FILE];
2375                         char ext[MAX_PTCACHE_PATH];
2376                         unsigned int len; /* store the length of the string */
2377
2378                         ptcache_path(pid, path);
2379                         
2380                         len = ptcache_filename(pid, filename, (int)cfra, 0, 0); /* no path */
2381                         
2382                         dir = opendir(path);
2383                         if (dir==NULL)
2384                                 return;
2385
2386                         BLI_snprintf(ext, sizeof(ext), "_%02u"PTCACHE_EXT, pid->stack_index);
2387                         
2388                         while ((de = readdir(dir)) != NULL) {
2389                                 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2390                                         if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2391                                                 /* read the number of the file */
2392                                                 unsigned int frame, len2 = (int)strlen(de->d_name);
2393                                                 char num[7];
2394
2395                                                 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2396                                                         BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2397                                                         frame = atoi(num);
2398                                                         
2399                                                         if (frame >= sta && frame <= end)
2400                                                                 cache->cached_frames[frame-sta] = 1;
2401                                                 }
2402                                         }
2403                                 }
2404                         }
2405                         closedir(dir);
2406                 }
2407                 else {
2408                         PTCacheMem *pm= pid->cache->mem_cache.first;
2409
2410                         while (pm) {
2411                                 if (pm->frame >= sta && pm->frame <= end)
2412                                         cache->cached_frames[pm->frame-sta] = 1;
2413                                 pm = pm->next;
2414                         }
2415                 }
2416         }
2417 }
2418 int  BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode)
2419 {
2420         PointCache *cache;
2421         int reset, clear, after;
2422
2423         if (!pid->cache)
2424                 return 0;
2425
2426         cache= pid->cache;
2427         reset= 0;
2428         clear= 0;
2429         after= 0;
2430
2431         if (mode == PTCACHE_RESET_DEPSGRAPH) {
2432                 if (!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) {
2433                         if (cache->flag & PTCACHE_QUICK_CACHE)
2434                                 clear= 1;
2435
2436                         after= 1;
2437                 }
2438
2439                 cache->flag |= PTCACHE_OUTDATED;
2440         }
2441         else if (mode == PTCACHE_RESET_BAKED) {
2442                 if (!BKE_ptcache_get_continue_physics()) {
2443                         reset= 1;
2444                         clear= 1;
2445                 }
2446                 else
2447                         cache->flag |= PTCACHE_OUTDATED;
2448         }
2449         else if (mode == PTCACHE_RESET_OUTDATED) {
2450                 reset = 1;
2451
2452                 if (cache->flag & PTCACHE_OUTDATED && !(cache->flag & PTCACHE_BAKED)) {
2453                         clear= 1;
2454                         cache->flag &= ~PTCACHE_OUTDATED;
2455                 }
2456         }
2457
2458         if (reset) {
2459                 BKE_ptcache_invalidate(cache);
2460                 cache->flag &= ~PTCACHE_REDO_NEEDED;
2461
2462                 if (pid->type == PTCACHE_TYPE_CLOTH)
2463                         cloth_free_modifier(pid->calldata);
2464                 else if (pid->type == PTCACHE_TYPE_SOFTBODY)
2465                         sbFreeSimulation(pid->calldata);
2466                 else if (pid->type == PTCACHE_TYPE_PARTICLES)
2467                         psys_reset(pid->calldata, PSYS_RESET_DEPSGRAPH);
2468                 else if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2469                         smokeModifier_reset(pid->calldata);
2470                 else if (pid->type == PTCACHE_TYPE_SMOKE_HIGHRES)
2471                         smokeModifier_reset_turbulence(pid->calldata);
2472                 else if (pid->type == PTCACHE_TYPE_DYNAMICPAINT)
2473                         dynamicPaint_clearSurface((DynamicPaintSurface*)pid->calldata);
2474         }
2475         if (clear)
2476                 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2477         else if (after)
2478                 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, CFRA);
2479
2480         return (reset || clear || after);
2481 }
2482 int  BKE_ptcache_object_reset(Scene *scene, Object *ob, int mode)
2483 {
2484         PTCacheID pid;
2485         ParticleSystem *psys;
2486         ModifierData *md;
2487         int reset, skip;
2488
2489         reset= 0;
2490         skip= 0;
2491
2492         if (ob->soft) {
2493                 BKE_ptcache_id_from_softbody(&pid, ob, ob->soft);
2494                 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2495         }
2496
2497         for (psys=ob->particlesystem.first; psys; psys=psys->next) {
2498                 /* children or just redo can be calculated without reseting anything */
2499                 if (psys->recalc & PSYS_RECALC_REDO || psys->recalc & PSYS_RECALC_CHILD)
2500                         skip = 1;
2501                 /* Baked cloth hair has to be checked too, because we don't want to reset */
2502                 /* particles or cloth in that case -jahka */
2503                 else if (psys->clmd) {
2504                         BKE_ptcache_id_from_cloth(&pid, ob, psys->clmd);
2505                         if (mode == PSYS_RESET_ALL || !(psys->part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED))) 
2506                                 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2507                         else
2508                                 skip = 1;
2509                 }
2510
2511                 if (skip == 0 && psys->part) {
2512                         BKE_ptcache_id_from_particles(&pid, ob, psys);
2513                         reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2514                 }
2515         }
2516
2517         for (md=ob->modifiers.first; md; md=md->next) {
2518                 if (md->type == eModifierType_Cloth) {
2519                         BKE_ptcache_id_from_cloth(&pid, ob, (ClothModifierData*)md);
2520                         reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2521                 }
2522                 if (md->type == eModifierType_Smoke) {
2523                         SmokeModifierData *smd = (SmokeModifierData *)md;
2524                         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
2525                                 BKE_ptcache_id_from_smoke(&pid, ob, (SmokeModifierData*)md);
2526                                 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2527                         }
2528                 }
2529                 if (md->type == eModifierType_DynamicPaint) {
2530                         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
2531                         if (pmd->canvas) {
2532                                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
2533
2534                                 for (; surface; surface=surface->next) {
2535                                         BKE_ptcache_id_from_dynamicpaint(&pid, ob, surface);
2536                                         reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2537                                 }
2538                         }
2539                 }
2540         }
2541
2542         if (ob->type == OB_ARMATURE)
2543                 BIK_clear_cache(ob->pose);
2544
2545         return reset;
2546 }
2547
2548 /* Use this when quitting blender, with unsaved files */
2549 void BKE_ptcache_remove(void)
2550 {
2551         char path[MAX_PTCACHE_PATH];
2552         char path_full[MAX_PTCACHE_PATH];
2553         int rmdir = 1;
2554         
2555         ptcache_path(NULL, path);
2556
2557         if (BLI_exists(path)) {
2558                 /* The pointcache dir exists? - remove all pointcache */
2559
2560                 DIR *dir; 
2561                 struct dirent *de;
2562
2563                 dir = opendir(path);
2564                 if (dir==NULL)
2565                         return;
2566                 
2567                 while ((de = readdir(dir)) != NULL) {
2568                         if ( strcmp(de->d_name, ".")==0 || strcmp(de->d_name, "..")==0) {
2569                                 /* do nothing */
2570                         }
2571                         else if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
2572                                 BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
2573                                 BLI_delete(path_full, 0, 0);
2574                         }
2575                         else {
2576                                 rmdir = 0; /* unknown file, don't remove the dir */
2577                         }
2578                 }
2579
2580                 closedir(dir);
2581         }
2582         else {
2583                 rmdir = 0; /* path dosnt exist  */
2584         }
2585         
2586         if (rmdir) {
2587                 BLI_delete(path, 1, 0);
2588         }
2589 }
2590
2591 /* Continuous Interaction */
2592
2593 static int CONTINUE_PHYSICS = 0;
2594
2595 void BKE_ptcache_set_continue_physics(Main *bmain, Scene *scene, int enable)
2596 {
2597         Object *ob;
2598
2599         if (CONTINUE_PHYSICS != enable) {
2600                 CONTINUE_PHYSICS = enable;
2601
2602                 if (CONTINUE_PHYSICS == 0) {
2603                         for (ob=bmain->object.first; ob; ob=ob->id.next)
2604                                 if (BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_OUTDATED))
2605                                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2606                 }
2607         }
2608 }
2609
2610 int  BKE_ptcache_get_continue_physics(void)
2611 {
2612         return CONTINUE_PHYSICS;
2613 }
2614
2615 /* Point Cache handling */
2616
2617 PointCache *BKE_ptcache_add(ListBase *ptcaches)
2618 {
2619         PointCache *cache;
2620
2621         cache= MEM_callocN(sizeof(PointCache), "PointCache");
2622         cache->startframe= 1;
2623         cache->endframe= 250;
2624         cache->step= 10;
2625         cache->index = -1;
2626
2627         BLI_addtail(ptcaches, cache);
2628
2629         return cache;
2630 }
2631
2632 void BKE_ptcache_free_mem(ListBase *mem_cache)
2633 {
2634         PTCacheMem *pm = mem_cache->first;
2635
2636         if (pm) {
2637                 for (; pm; pm=pm->next) {
2638                         ptcache_data_free(pm);
2639                         ptcache_extra_free(pm);
2640                 }
2641
2642                 BLI_freelistN(mem_cache);
2643         }
2644 }
2645 void BKE_ptcache_free(PointCache *cache)
2646 {
2647         BKE_ptcache_free_mem(&cache->mem_cache);
2648         if (cache->edit && cache->free_edit)
2649                 cache->free_edit(cache->edit);
2650         if (cache->cached_frames)
2651                 MEM_freeN(cache->cached_frames);
2652         MEM_freeN(cache);
2653 }
2654 void BKE_ptcache_free_list(ListBase *ptcaches)
2655 {
2656         PointCache *cache = ptcaches->first;
2657
2658         while (cache) {
2659                 BLI_remlink(ptcaches, cache);
2660                 BKE_ptcache_free(cache);
2661                 cache = ptcaches->first;
2662         }
2663 }
2664
2665 static PointCache *ptcache_copy(PointCache *cache)
2666 {
2667         PointCache *ncache;
2668
2669         ncache= MEM_dupallocN(cache);
2670
2671         /* hmm, should these be copied over instead? */
2672         ncache->mem_cache.first = NULL;
2673         ncache->mem_cache.last = NULL;
2674         ncache->cached_frames = NULL;
2675         ncache->edit = NULL;
2676
2677         ncache->flag= 0;
2678         ncache->simframe= 0;
2679
2680         return ncache;
2681 }
2682 /* returns first point cache */
2683 PointCache *BKE_ptcache_copy_list(ListBase *ptcaches_new, ListBase *ptcaches_old)
2684 {
2685         PointCache *cache = ptcaches_old->first;
2686
2687         ptcaches_new->first = ptcaches_new->last = NULL;
2688
2689         for (; cache; cache=cache->next)
2690                 BLI_addtail(ptcaches_new, ptcache_copy(cache));
2691
2692         return ptcaches_new->first;
2693 }
2694
2695
2696 /* Baking */
2697 void BKE_ptcache_quick_cache_all(Main *bmain, Scene *scene)
2698 {
2699         PTCacheBaker baker;
2700
2701         baker.bake=0;
2702         baker.break_data=NULL;
2703         baker.break_test=NULL;
2704         baker.pid=NULL;
2705         baker.progressbar=NULL;
2706         baker.progressend=NULL;
2707         baker.progresscontext=NULL;
2708         baker.render=0;
2709         baker.anim_init = 0;
2710         baker.main=bmain;
2711         baker.scene=scene;
2712         baker.quick_step=scene->physics_settings.quick_cache_step;
2713
2714         BKE_ptcache_bake(&baker);
2715 }
2716
2717 /* Simulation thread, no need for interlocks as data written in both threads
2718  * are only unitary integers (I/O assumed to be atomic for them) */
2719 typedef struct {
2720         int break_operation;
2721         int thread_ended;
2722         int endframe;
2723         int step;
2724         int *cfra_ptr;
2725         Main *main;
2726         Scene *scene;
2727 } ptcache_bake_data;
2728
2729 static void ptcache_dt_to_str(char *str, double dtime)
2730 {
2731         if (dtime > 60.0) {
2732                 if (dtime > 3600.0)
2733                         sprintf(str, "%ih %im %is", (int)(dtime/3600), ((int)(dtime/60))%60, ((int)dtime) % 60);
2734                 else
2735                         sprintf(str, "%im %is", ((int)(dtime/60))%60, ((int)dtime) % 60);
2736         }
2737         else
2738                 sprintf(str, "%is", ((int)dtime) % 60);
2739 }
2740
2741 static void *ptcache_bake_thread(void *ptr)
2742 {
2743         int use_timer = FALSE, sfra, efra;
2744         double stime, ptime, ctime, fetd;
2745         char run[32], cur[32], etd[32];
2746
2747         ptcache_bake_data *data = (ptcache_bake_data*)ptr;
2748
2749         stime = ptime = PIL_check_seconds_timer();
2750         sfra = *data->cfra_ptr;
2751         efra = data->endframe;
2752
2753         for (; (*data->cfra_ptr <= data->endframe) && !data->break_operation; *data->cfra_ptr+=data->step) {
2754                 BKE_scene_update_for_newframe(data->main, data->scene, data->scene->lay);
2755                 if (G.background) {
2756                         printf("bake: frame %d :: %d\n", (int)*data->cfra_ptr, data->endframe);
2757                 }
2758                 else {
2759                         ctime = PIL_check_seconds_timer();
2760
2761                         fetd = (ctime-ptime)*(efra-*data->cfra_ptr)/data->step;
2762
2763                         if (use_timer || fetd > 60.0) {
2764                                 use_timer = TRUE;
2765
2766                                 ptcache_dt_to_str(cur, ctime-ptime);
2767                                 ptcache_dt_to_str(run, ctime-stime);
2768                                 ptcache_dt_to_str(etd, fetd);
2769
2770                                 printf("Baked for %s, current frame: %i/%i (%.3fs), ETC: %s\r", run, *data->cfra_ptr-sfra+1, efra-sfra+1, ctime-ptime, etd);
2771                         }
2772                         ptime = ctime;
2773                 }
2774         }
2775
2776         if (use_timer) {
2777                 ptcache_dt_to_str(run, PIL_check_seconds_timer()-stime);
2778                 printf("Bake %s %s (%i frames simulated).\n", (data->break_operation ? "canceled after" : "finished in"), run, *data->cfra_ptr-sfra);
2779         }
2780
2781         data->thread_ended = TRUE;
2782         return NULL;
2783 }
2784
2785 /* if bake is not given run simulations to current frame */
2786 void BKE_ptcache_bake(PTCacheBaker* baker)
2787 {
2788         Main *bmain = baker->main;
2789         Scene *scene = baker->scene;
2790         Scene *sce_iter; /* SETLOOPER macro only */
2791         Base *base;
2792         ListBase pidlist;
2793         PTCacheID *pid = baker->pid;
2794         PointCache *cache = NULL;
2795         float frameleno = scene->r.framelen;
2796         int cfrao = CFRA;
2797         int startframe = MAXFRAME;
2798         int bake = baker->bake;
2799         int render = baker->render;
2800         ListBase threads;
2801         ptcache_bake_data thread_data;
2802         int progress, old_progress;
2803         
2804         thread_data.endframe = baker->anim_init ? scene->r.sfra : CFRA;
2805         thread_data.step = baker->quick_step;
2806         thread_data.cfra_ptr = &CFRA;
2807         thread_data.scene = baker->scene;
2808         thread_data.main = baker->main;
2809
2810         G.is_break = FALSE;
2811
2812         /* set caches to baking mode and figure out start frame */
2813         if (pid) {
2814                 /* cache/bake a single object */
2815                 cache = pid->cache;
2816                 if ((cache->flag & PTCACHE_BAKED)==0) {
2817                         if (pid->type==PTCACHE_TYPE_PARTICLES) {
2818                                 ParticleSystem *psys= pid->calldata;
2819
2820                                 /* a bit confusing, could make this work better in the UI */
2821                                 if (psys->part->type == PART_EMITTER)
2822                                         psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
2823                         }
2824                         else if (pid->type == PTCACHE_TYPE_SMOKE_HIGHRES) {
2825                                 /* get all pids from the object and search for smoke low res */
2826                                 ListBase pidlist2;
2827                                 PTCacheID *pid2;
2828                                 BKE_ptcache_ids_from_object(&pidlist2, pid->ob, scene, MAX_DUPLI_RECUR);
2829                                 for (pid2=pidlist2.first; pid2; pid2=pid2->next) {
2830                                         if (pid2->type == PTCACHE_TYPE_SMOKE_DOMAIN) {
2831                                                 if (pid2->cache && !(pid2->cache->flag & PTCACHE_BAKED)) {
2832                                                         if (bake || pid2->cache->flag & PTCACHE_REDO_NEEDED)
2833                                                                 BKE_ptcache_id_clear(pid2, PTCACHE_CLEAR_ALL, 0);
2834                                                         if (bake) {
2835                                                                 pid2->cache->flag |= PTCACHE_BAKING;
2836                                                                 pid2->cache->flag &= ~PTCACHE_BAKED;
2837                                                         }
2838                                                 }
2839                                         }
2840                                 }
2841                                 BLI_freelistN(&pidlist2);
2842                         }
2843
2844                         if (bake || cache->flag & PTCACHE_REDO_NEEDED)
2845                                 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2846
2847                         startframe = MAX2(cache->last_exact, cache->startframe);
2848
2849                         if (bake) {
2850                                 thread_data.endframe = cache->endframe;
2851                                 cache->flag |= PTCACHE_BAKING;
2852                         }
2853                         else {
2854                                 thread_data.endframe = MIN2(thread_data.endframe, cache->endframe);
2855                         }
2856
2857                         cache->flag &= ~PTCACHE_BAKED;
2858                 }
2859         }
2860         else {
2861                 for (SETLOOPER(scene, sce_iter, base)) {
2862                         /* cache/bake everything in the scene */
2863                         BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
2864
2865                         for (pid=pidlist.first; pid; pid=pid->next) {
2866                                 cache = pid->cache;
2867                                 if ((cache->flag & PTCACHE_BAKED)==0) {
2868                                         if (pid->type==PTCACHE_TYPE_PARTICLES) {
2869                                                 ParticleSystem *psys = (ParticleSystem*)pid->calldata;
2870                                                 /* skip hair & keyed particles */
2871                                                 if (psys->part->type == PART_HAIR || psys->part->phystype == PART_PHYS_KEYED)
2872                                                         continue;
2873
2874                                                 psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
2875                                         }
2876
2877                                         if ((cache->flag & PTCACHE_REDO_NEEDED || (cache->flag & PTCACHE_SIMULATION_VALID)==0) &&
2878                                             ((cache->flag & PTCACHE_QUICK_CACHE)==0 || render || bake))
2879                                         {
2880                                                 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2881                                         }
2882
2883                                         startframe = MIN2(startframe, cache->startframe);
2884
2885                                         if (bake || render) {
2886                                                 cache->flag |= PTCACHE_BAKING;
2887
2888                                                 if (bake)
2889                                                         thread_data.endframe = MAX2(thread_data.endframe, cache->endframe);
2890                                         }
2891
2892                                         cache->flag &= ~PTCACHE_BAKED;
2893
2894                                 }
2895                         }
2896                         BLI_freelistN(&pidlist);
2897                 }
2898         }
2899
2900         CFRA = startframe;
2901         scene->r.framelen = 1.0;
2902         thread_data.break_operation = FALSE;
2903         thread_data.thread_ended = FALSE;
2904         old_progress = -1;
2905
2906         WM_cursor_wait(1);
2907         
2908         if (G.background) {
2909                 ptcache_bake_thread((void*)&thread_data);
2910         }
2911         else {
2912                 BLI_init_threads(&threads, ptcache_bake_thread, 1);
2913                 BLI_insert_thread(&threads, (void*)&thread_data);
2914
2915                 while (thread_data.thread_ended == FALSE) {
2916
2917                         if (bake)
2918                                 progress = (int)(100.0f * (float)(CFRA - startframe)/(float)(thread_data.endframe-startframe));
2919                         else
2920                                 progress = CFRA;
2921
2922                         /* NOTE: baking should not redraw whole ui as this slows things down */
2923                         if ((baker->progressbar) && (progress != old_progress)) {
2924                                 baker->progressbar(baker->progresscontext, progress);
2925                                 old_progress = progress;
2926                         }
2927
2928                         /* Delay to lessen CPU load from UI thread */
2929                         PIL_sleep_ms(200);
2930
2931                         /* NOTE: breaking baking should leave calculated frames in cache, not clear it */
2932                         if (blender_test_break() && !thread_data.break_operation) {
2933                                 thread_data.break_operation = TRUE;
2934                                 if (baker->progressend)
2935                                         baker->progressend(baker->progresscontext);
2936                                 WM_cursor_wait(1);
2937                         }
2938                 }
2939
2940         BLI_end_threads(&threads);
2941         }
2942         /* clear baking flag */
2943         if (pid) {
2944                 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
2945                 cache->flag |= PTCACHE_SIMULATION_VALID;
2946                 if (bake) {
2947                         cache->flag |= PTCACHE_BAKED;
2948                         /* write info file */
2949                         if (cache->flag & PTCACHE_DISK_CACHE)
2950                                 BKE_ptcache_write(pid, 0);
2951                 }
2952         }
2953         else {
2954                 for (SETLOOPER(scene, sce_iter, base)) {
2955                         BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
2956
2957                         for (pid=pidlist.first; pid; pid=pid->next) {
2958                                 /* skip hair particles */
2959                                 if (pid->type==PTCACHE_TYPE_PARTICLES && ((ParticleSystem*)pid->calldata)->part->type == PART_HAIR)
2960                                         continue;
2961
2962                                 cache = pid->cache;
2963
2964                                 if (thread_data.step > 1)
2965                                         cache->flag &= ~(PTCACHE_BAKING|PTCACHE_OUTDATED);
2966                                 else
2967                                         cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
2968
2969                                 cache->flag |= PTCACHE_SIMULATION_VALID;
2970
2971                                 if (bake) {
2972                                         cache->flag |= PTCACHE_BAKED;
2973                                         if (cache->flag & PTCACHE_DISK_CACHE)
2974                                                 BKE_ptcache_write(pid, 0);
2975                                 }
2976                         }
2977                         BLI_freelistN(&pidlist);
2978                 }
2979         }
2980
2981         scene->r.framelen = frameleno;
2982         CFRA = cfrao;
2983         
2984         if (bake) /* already on cfra unless baking */
2985                 BKE_scene_update_for_newframe(bmain, scene, scene->lay);
2986
2987         if (thread_data.break_operation)
2988                 WM_cursor_wait(0);
2989         else if (baker->progressend)
2990                 baker->progressend(baker->progresscontext);
2991
2992         WM_cursor_wait(0);
2993
2994         /* TODO: call redraw all windows somehow */
2995 }
2996 /* Helpers */
2997 void BKE_ptcache_disk_to_mem(PTCacheID *pid)
2998 {
2999         PointCache *cache = pid->cache;
3000         PTCacheMem *pm = NULL;
3001         int baked = cache->flag & PTCACHE_BAKED;
3002         int cfra, sfra = cache->startframe, efra = cache->endframe;
3003
3004         /* Remove possible bake flag to allow clear */
3005         cache->flag &= ~PTCACHE_BAKED;
3006
3007         /* PTCACHE_DISK_CACHE flag was cleared already */
3008         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
3009
3010         /* restore possible bake flag */
3011         cache->flag |= baked;
3012
3013         for (cfra=sfra; cfra <= efra; cfra++) {
3014                 pm = ptcache_disk_frame_to_mem(pid, cfra);
3015
3016                 if (pm)
3017                         BLI_addtail(&pid->cache->mem_cache, pm);
3018         }
3019 }
3020 void BKE_ptcache_mem_to_disk(PTCacheID *pid)
3021 {
3022         PointCache *cache = pid->cache;
3023         PTCacheMem *pm = cache->mem_cache.first;
3024         int baked = cache->flag & PTCACHE_BAKED;
3025
3026         /* Remove possible bake flag to allow clear */
3027         cache->flag &= ~PTCACHE_BAKED;
3028
3029         /* PTCACHE_DISK_CACHE flag was set already */
3030         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
3031
3032         /* restore possible bake flag */
3033         cache->flag |= baked;
3034
3035         for (; pm; pm=pm->next) {
3036                 if (ptcache_mem_frame_to_disk(pid, pm)==0) {
3037                         cache->flag &= ~PTCACHE_DISK_CACHE;
3038                         break;
3039                 }
3040         }
3041
3042         /* write info file */
3043         if (cache->flag & PTCACHE_BAKED)
3044                 BKE_ptcache_write(pid, 0);
3045 }
3046 void BKE_ptcache_toggle_disk_cache(PTCacheID *pid)
3047 {
3048         PointCache *cache = pid->cache;
3049         int last_exact = cache->last_exact;
3050
3051         if (!G.relbase_valid) {
3052                 cache->flag &= ~PTCACHE_DISK_CACHE;
3053                 if (G.debug & G_DEBUG)
3054                         printf("File must be saved before using disk cache!\n");
3055                 return;
3056         }
3057
3058         if (cache->cached_frames) {
3059                 MEM_freeN(cache->cached_frames);
3060                 cache->cached_frames=NULL;
3061         }
3062
3063         if (cache->flag & PTCACHE_DISK_CACHE)
3064                 BKE_ptcache_mem_to_disk(pid);
3065         else
3066                 BKE_ptcache_disk_to_mem(pid);
3067
3068         cache->flag ^= PTCACHE_DISK_CACHE;
3069         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
3070         cache->flag ^= PTCACHE_DISK_CACHE;
3071         
3072         cache->last_exact = last_exact;
3073
3074         BKE_ptcache_id_time(pid, NULL, 0.0f, NULL, NULL, NULL);
3075
3076         BKE_ptcache_update_info(pid);
3077 }
3078
3079 void BKE_ptcache_disk_cache_rename(PTCacheID *pid, const char *name_src, const char *name_dst)
3080 {
3081         char old_name[80];
3082         int len; /* store the length of the string */
3083         /* mode is same as fopen's modes */
3084         DIR *dir; 
3085         struct dirent *de;
3086         char path[MAX_PTCACHE_PATH];
3087         char old_filename[MAX_PTCACHE_FILE];
3088         char new_path_full[MAX_PTCACHE_FILE];
3089         char old_path_full[MAX_PTCACHE_FILE];
3090         char ext[MAX_PTCACHE_PATH];
3091
3092         /* save old name */
3093         BLI_strncpy(old_name, pid->cache->name, sizeof(old_name));
3094
3095         /* get "from" filename */
3096         BLI_strncpy(pid->cache->name, name_src, sizeof(pid->cache->name));
3097
3098         len = ptcache_filename(pid, old_filename, 0, 0, 0); /* no path */
3099
3100         ptcache_path(pid, path);
3101         dir = opendir(path);
3102         if (dir==NULL) {
3103                 BLI_strncpy(pid->cache->name, old_name, sizeof(pid->cache->name));
3104                 return;
3105         }
3106
3107         BLI_snprintf(ext, sizeof(ext), "_%02u"PTCACHE_EXT, pid->stack_index);
3108
3109         /* put new name into cache */
3110         BLI_strncpy(pid->cache->name, name_dst, sizeof(pid->cache->name));
3111
3112         while ((de = readdir(dir)) != NULL) {
3113                 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
3114                         if (strncmp(old_filename, de->d_name, len ) == 0) { /* do we have the right prefix */
3115                                 /* read the number of the file */
3116                                 int frame, len2 = (int)strlen(de->d_name);
3117                                 char num[7];
3118
3119                                 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
3120                                         BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
3121                                         frame = atoi(num);
3122
3123                                         BLI_join_dirfile(old_path_full, sizeof(old_path_full), path, de->d_name);
3124                                         ptcache_filename(pid, new_path_full, frame, 1, 1);
3125                                         BLI_rename(old_path_full, new_path_full);
3126                                 }
3127                         }
3128                 }
3129         }
3130         closedir(dir);
3131
3132         BLI_strncpy(pid->cache->name, old_name, sizeof(pid->cache->name));
3133 }
3134
3135 void BKE_ptcache_load_external(PTCacheID *pid)
3136 {
3137         /*todo*/
3138         PointCache *cache = pid->cache;
3139         int len; /* store the length of the string */
3140         int info = 0;
3141         int start = MAXFRAME;
3142         int end = -1;
3143
3144         /* mode is same as fopen's modes */
3145         DIR *dir; 
3146         struct dirent *de;
3147         char path[MAX_PTCACHE_PATH];
3148         char filename[MAX_PTCACHE_FILE];
3149         char ext[MAX_PTCACHE_PATH];
3150
3151         if (!cache)
3152                 return;
3153
3154         ptcache_path(pid, path);
3155         
3156         len = ptcache_filename(pid, filename, 1, 0, 0); /* no path */
3157