5e12b15a658feee1e4c922394708dad329d5b551
[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
73 #include "BIK_api.h"
74
75 /* both in intern */
76 #ifdef WITH_SMOKE
77 #include "smoke_API.h"
78 #endif
79
80 #ifdef WITH_LZO
81 #include "minilzo.h"
82 #else
83 /* used for non-lzo cases */
84 #define LZO_OUT_LEN(size)     ((size) + (size) / 16 + 64 + 3)
85 #endif
86
87 #ifdef WITH_LZMA
88 #include "LzmaLib.h"
89 #endif
90
91 /* needed for directory lookup */
92 /* untitled blend's need getpid for a unique name */
93 #ifndef WIN32
94 #  include <dirent.h>
95 #  include <unistd.h>
96 #else
97 #  include <process.h>
98 #  include "BLI_winstuff.h"
99 #endif
100
101 #define PTCACHE_DATA_FROM(data, type, from)  \
102         if (data[type]) { \
103                 memcpy(data[type], from, ptcache_data_size[type]); \
104         } (void)0
105
106 #define PTCACHE_DATA_TO(data, type, index, to)  \
107         if (data[type]) { \
108                 memcpy(to, (char *)(data)[type] + ((index) ? (index) * ptcache_data_size[type] : 0), ptcache_data_size[type]); \
109         } (void)0
110
111 /* could be made into a pointcache option */
112 #define DURIAN_POINTCACHE_LIB_OK 1
113
114 static int ptcache_data_size[] = {      
115                 sizeof(unsigned int), // BPHYS_DATA_INDEX
116                 3 * sizeof(float), // BPHYS_DATA_LOCATION
117                 3 * sizeof(float), // BPHYS_DATA_VELOCITY
118                 4 * sizeof(float), // BPHYS_DATA_ROTATION
119                 3 * sizeof(float), // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST
120                 sizeof(float), // BPHYS_DATA_SIZE
121                 3 * sizeof(float), // BPHYS_DATA_TIMES
122                 sizeof(BoidData) // case BPHYS_DATA_BOIDS
123 };
124
125 static int ptcache_extra_datasize[] = {
126         0,
127         sizeof(ParticleSpring)
128 };
129
130 /* forward declerations */
131 static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result, unsigned int len);
132 static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, unsigned int in_len, unsigned char *out, int mode);
133 static int ptcache_file_write(PTCacheFile *pf, const void *f, unsigned int tot, unsigned int size);
134 static int ptcache_file_read(PTCacheFile *pf, void *f, unsigned int tot, unsigned int size);
135
136 /* Common functions */
137 static int ptcache_basic_header_read(PTCacheFile *pf)
138 {
139         int error=0;
140
141         /* Custom functions should read these basic elements too! */
142         if (!error && !fread(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
143                 error = 1;
144         
145         if (!error && !fread(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
146                 error = 1;
147
148         return !error;
149 }
150 static int ptcache_basic_header_write(PTCacheFile *pf)
151 {
152         /* Custom functions should write these basic elements too! */
153         if (!fwrite(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
154                 return 0;
155         
156         if (!fwrite(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
157                 return 0;
158
159         return 1;
160 }
161 /* Softbody functions */
162 static int  ptcache_softbody_write(int index, void *soft_v, void **data, int UNUSED(cfra))
163 {
164         SoftBody *soft= soft_v;
165         BodyPoint *bp = soft->bpoint + index;
166
167         PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, bp->pos);
168         PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, bp->vec);
169
170         return 1;
171 }
172 static void ptcache_softbody_read(int index, void *soft_v, void **data, float UNUSED(cfra), float *old_data)
173 {
174         SoftBody *soft= soft_v;
175         BodyPoint *bp = soft->bpoint + index;
176
177         if (old_data) {
178                 memcpy(bp->pos, data, 3 * sizeof(float));
179                 memcpy(bp->vec, data + 3, 3 * sizeof(float));
180         }
181         else {
182                 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, bp->pos);
183                 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, 0, bp->vec);
184         }
185 }
186 static void ptcache_softbody_interpolate(int index, void *soft_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
187 {
188         SoftBody *soft= soft_v;
189         BodyPoint *bp = soft->bpoint + index;
190         ParticleKey keys[4];
191         float dfra;
192
193         if (cfra1 == cfra2)
194                 return;
195
196         copy_v3_v3(keys[1].co, bp->pos);
197         copy_v3_v3(keys[1].vel, bp->vec);
198
199         if (old_data) {
200                 memcpy(keys[2].co, old_data, 3 * sizeof(float));
201                 memcpy(keys[2].vel, old_data + 3, 3 * sizeof(float));
202         }
203         else
204                 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
205
206         dfra = cfra2 - cfra1;
207
208         mul_v3_fl(keys[1].vel, dfra);
209         mul_v3_fl(keys[2].vel, dfra);
210
211         psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
212
213         mul_v3_fl(keys->vel, 1.0f / dfra);
214
215         copy_v3_v3(bp->pos, keys->co);
216         copy_v3_v3(bp->vec, keys->vel);
217 }
218 static int  ptcache_softbody_totpoint(void *soft_v, int UNUSED(cfra))
219 {
220         SoftBody *soft= soft_v;
221         return soft->totpoint;
222 }
223 /* Particle functions */
224 void BKE_ptcache_make_particle_key(ParticleKey *key, int index, void **data, float time)
225 {
226         PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, index, key->co);
227         PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, index, key->vel);
228         
229         /* no rotation info, so make something nice up */
230         if (data[BPHYS_DATA_ROTATION]==NULL) {
231                 vec_to_quat(key->rot, key->vel, OB_NEGX, OB_POSZ);
232         }
233         else {
234                 PTCACHE_DATA_TO(data, BPHYS_DATA_ROTATION, index, key->rot);
235         }
236
237         PTCACHE_DATA_TO(data, BPHYS_DATA_AVELOCITY, index, key->ave);
238         key->time = time;
239 }
240 static int  ptcache_particle_write(int index, void *psys_v, void **data, int cfra)
241 {
242         ParticleSystem *psys= psys_v;
243         ParticleData *pa = psys->particles + index;
244         BoidParticle *boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
245         float times[3];
246         int step = psys->pointcache->step;
247
248         /* No need to store unborn or died particles outside cache step bounds */
249         if (data[BPHYS_DATA_INDEX] && (cfra < pa->time - step || cfra > pa->dietime + step))
250                 return 0;
251
252         times[0]= pa->time;
253         times[1]= pa->dietime;
254         times[2]= pa->lifetime;
255
256         PTCACHE_DATA_FROM(data, BPHYS_DATA_INDEX, &index);
257         PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, pa->state.co);
258         PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, pa->state.vel);
259         PTCACHE_DATA_FROM(data, BPHYS_DATA_ROTATION, pa->state.rot);
260         PTCACHE_DATA_FROM(data, BPHYS_DATA_AVELOCITY, pa->state.ave);
261         PTCACHE_DATA_FROM(data, BPHYS_DATA_SIZE, &pa->size);
262         PTCACHE_DATA_FROM(data, BPHYS_DATA_TIMES, times);
263
264         if (boid)
265                 PTCACHE_DATA_FROM(data, BPHYS_DATA_BOIDS, &boid->data);
266
267         /* return flag 1+1=2 for newly born particles to copy exact birth location to previously cached frame */
268         return 1 + (pa->state.time >= pa->time && pa->prev_state.time <= pa->time);
269 }
270 static void ptcache_particle_read(int index, void *psys_v, void **data, float cfra, float *old_data)
271 {
272         ParticleSystem *psys= psys_v;
273         ParticleData *pa;
274         BoidParticle *boid;
275         float timestep = 0.04f*psys->part->timetweak;
276
277         if (index >= psys->totpart)
278                 return;
279
280         pa = psys->particles + index;
281         boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
282
283         if (cfra > pa->state.time)
284                 memcpy(&pa->prev_state, &pa->state, sizeof(ParticleKey));
285
286         if (old_data) {
287                 /* old format cache */
288                 memcpy(&pa->state, old_data, sizeof(ParticleKey));
289                 return;
290         }
291
292         BKE_ptcache_make_particle_key(&pa->state, 0, data, cfra);
293
294         /* set frames cached before birth to birth time */
295         if (cfra < pa->time)
296                 pa->state.time = pa->time;
297         else if (cfra > pa->dietime)
298                 pa->state.time = pa->dietime;
299
300         if (data[BPHYS_DATA_SIZE])
301                 PTCACHE_DATA_TO(data, BPHYS_DATA_SIZE, 0, &pa->size);
302         
303         if (data[BPHYS_DATA_TIMES]) {
304                 float times[3];
305                 PTCACHE_DATA_TO(data, BPHYS_DATA_TIMES, 0, &times);
306                 pa->time = times[0];
307                 pa->dietime = times[1];
308                 pa->lifetime = times[2];
309         }
310
311         if (boid)
312                 PTCACHE_DATA_TO(data, BPHYS_DATA_BOIDS, 0, &boid->data);
313
314         /* determine velocity from previous location */
315         if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
316                 if (cfra > pa->prev_state.time) {
317                         sub_v3_v3v3(pa->state.vel, pa->state.co, pa->prev_state.co);
318                         mul_v3_fl(pa->state.vel, (cfra - pa->prev_state.time) * timestep);
319                 }
320                 else {
321                         sub_v3_v3v3(pa->state.vel, pa->prev_state.co, pa->state.co);
322                         mul_v3_fl(pa->state.vel, (pa->prev_state.time - cfra) * timestep);
323                 }
324         }
325
326         /* default to no rotation */
327         if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
328                 unit_qt(pa->state.rot);
329         }
330 }
331 static void ptcache_particle_interpolate(int index, void *psys_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
332 {
333         ParticleSystem *psys= psys_v;
334         ParticleData *pa;
335         ParticleKey keys[4];
336         float dfra, timestep = 0.04f*psys->part->timetweak;
337
338         if (index >= psys->totpart)
339                 return;
340
341         pa = psys->particles + index;
342
343         /* particle wasn't read from first cache so can't interpolate */
344         if ((int)cfra1 < pa->time - psys->pointcache->step || (int)cfra1 > pa->dietime + psys->pointcache->step)
345                 return;
346
347         cfra = MIN2(cfra, pa->dietime);
348         cfra1 = MIN2(cfra1, pa->dietime);
349         cfra2 = MIN2(cfra2, pa->dietime);
350
351         if (cfra1 == cfra2)
352                 return;
353
354         memcpy(keys+1, &pa->state, sizeof(ParticleKey));
355         if (old_data)
356                 memcpy(keys+2, old_data, sizeof(ParticleKey));
357         else
358                 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
359
360         /* determine velocity from previous location */
361         if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
362                 if (keys[1].time > keys[2].time) {
363                         sub_v3_v3v3(keys[2].vel, keys[1].co, keys[2].co);
364                         mul_v3_fl(keys[2].vel, (keys[1].time - keys[2].time) * timestep);
365                 }
366                 else {
367                         sub_v3_v3v3(keys[2].vel, keys[2].co, keys[1].co);
368                         mul_v3_fl(keys[2].vel, (keys[2].time - keys[1].time) * timestep);
369                 }
370         }
371
372         /* determine rotation from velocity */
373         if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
374                 vec_to_quat(keys[2].rot, keys[2].vel, OB_NEGX, OB_POSZ);
375         }
376
377         if (cfra > pa->time)
378                 cfra1 = MAX2(cfra1, pa->time);
379
380         dfra = cfra2 - cfra1;
381
382         mul_v3_fl(keys[1].vel, dfra * timestep);
383         mul_v3_fl(keys[2].vel, dfra * timestep);
384
385         psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, &pa->state, 1);
386         interp_qt_qtqt(pa->state.rot, keys[1].rot, keys[2].rot, (cfra - cfra1) / dfra);
387
388         mul_v3_fl(pa->state.vel, 1.f / (dfra * timestep));
389
390         pa->state.time = cfra;
391 }
392
393 static int  ptcache_particle_totpoint(void *psys_v, int UNUSED(cfra))
394 {
395         ParticleSystem *psys = psys_v;
396         return psys->totpart;
397 }
398 static int  ptcache_particle_totwrite(void *psys_v, int cfra)
399 {
400         ParticleSystem *psys = psys_v;
401         ParticleData *pa= psys->particles;
402         int p, step = psys->pointcache->step;
403         int totwrite = 0;
404
405         if (cfra == 0)
406                 return psys->totpart;
407
408         for (p=0; p<psys->totpart; p++, pa++)
409                 totwrite += (cfra >= pa->time - step && cfra <= pa->dietime + step);
410
411         return totwrite;
412 }
413
414 static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int UNUSED(cfra))
415 {
416         ParticleSystem *psys = psys_v;
417         PTCacheExtra *extra = NULL;
418
419         if (psys->part->phystype == PART_PHYS_FLUID &&
420                 psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS &&
421                 psys->tot_fluidsprings && psys->fluid_springs) {
422
423                 extra = MEM_callocN(sizeof(PTCacheExtra), "Point cache: fluid extra data");
424
425                 extra->type = BPHYS_EXTRA_FLUID_SPRINGS;
426                 extra->totdata = psys->tot_fluidsprings;
427
428                 extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Point cache: extra data");
429                 memcpy(extra->data, psys->fluid_springs, extra->totdata * ptcache_extra_datasize[extra->type]);
430
431                 BLI_addtail(&pm->extradata, extra);
432         }
433 }
434
435 static void ptcache_particle_extra_read(void *psys_v, PTCacheMem *pm, float UNUSED(cfra))
436 {
437         ParticleSystem *psys = psys_v;
438         PTCacheExtra *extra = pm->extradata.first;
439
440         for (; extra; extra=extra->next) {
441                 switch (extra->type) {
442                         case BPHYS_EXTRA_FLUID_SPRINGS:
443                         {
444                                 if (psys->fluid_springs)
445                                         MEM_freeN(psys->fluid_springs);
446
447                                 psys->fluid_springs = MEM_dupallocN(extra->data);
448                                 psys->tot_fluidsprings = psys->alloc_fluidsprings = extra->totdata;
449                                 break;
450                         }
451                 }
452         }
453 }
454
455 /* Cloth functions */
456 static int  ptcache_cloth_write(int index, void *cloth_v, void **data, int UNUSED(cfra))
457 {
458         ClothModifierData *clmd= cloth_v;
459         Cloth *cloth= clmd->clothObject;
460         ClothVertex *vert = cloth->verts + index;
461
462         PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, vert->x);
463         PTCACHE_DATA_FROM(data, BPHYS_DATA_VELOCITY, vert->v);
464         PTCACHE_DATA_FROM(data, BPHYS_DATA_XCONST, vert->xconst);
465
466         return 1;
467 }
468 static void ptcache_cloth_read(int index, void *cloth_v, void **data, float UNUSED(cfra), float *old_data)
469 {
470         ClothModifierData *clmd= cloth_v;
471         Cloth *cloth= clmd->clothObject;
472         ClothVertex *vert = cloth->verts + index;
473         
474         if (old_data) {
475                 memcpy(vert->x, data, 3 * sizeof(float));
476                 memcpy(vert->xconst, data + 3, 3 * sizeof(float));
477                 memcpy(vert->v, data + 6, 3 * sizeof(float));
478         }
479         else {
480                 PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, vert->x);
481                 PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, 0, vert->v);
482                 PTCACHE_DATA_TO(data, BPHYS_DATA_XCONST, 0, vert->xconst);
483         }
484 }
485 static void ptcache_cloth_interpolate(int index, void *cloth_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
486 {
487         ClothModifierData *clmd= cloth_v;
488         Cloth *cloth= clmd->clothObject;
489         ClothVertex *vert = cloth->verts + index;
490         ParticleKey keys[4];
491         float dfra;
492
493         if (cfra1 == cfra2)
494                 return;
495
496         copy_v3_v3(keys[1].co, vert->x);
497         copy_v3_v3(keys[1].vel, vert->v);
498
499         if (old_data) {
500                 memcpy(keys[2].co, old_data, 3 * sizeof(float));
501                 memcpy(keys[2].vel, old_data + 6, 3 * sizeof(float));
502         }
503         else
504                 BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
505
506         dfra = cfra2 - cfra1;
507
508         mul_v3_fl(keys[1].vel, dfra);
509         mul_v3_fl(keys[2].vel, dfra);
510
511         psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, 1);
512
513         mul_v3_fl(keys->vel, 1.0f / dfra);
514
515         copy_v3_v3(vert->x, keys->co);
516         copy_v3_v3(vert->v, keys->vel);
517
518         /* should vert->xconst be interpolated somehow too? - jahka */
519 }
520
521 static int  ptcache_cloth_totpoint(void *cloth_v, int UNUSED(cfra))
522 {
523         ClothModifierData *clmd= cloth_v;
524         return clmd->clothObject ? clmd->clothObject->numverts : 0;
525 }
526
527 #ifdef WITH_SMOKE
528 /* Smoke functions */
529 static int  ptcache_smoke_totpoint(void *smoke_v, int UNUSED(cfra))
530 {
531         SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
532         SmokeDomainSettings *sds = smd->domain;
533         
534         if (sds->fluid) {
535                 return sds->res[0]*sds->res[1]*sds->res[2];
536         }
537         else
538                 return 0;
539 }
540 static int  ptcache_smoke_write(PTCacheFile *pf, void *smoke_v)
541 {       
542         SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
543         SmokeDomainSettings *sds = smd->domain;
544         int ret = 0;
545         
546         if (sds->fluid) {
547                 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
548                 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
549                 unsigned char *obstacles;
550                 unsigned int in_len = sizeof(float)*(unsigned int)res;
551                 unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
552                 //int mode = res >= 1000000 ? 2 : 1;
553                 int mode=1;             // light
554                 if (sds->cache_comp == SM_CACHE_HEAVY) mode=2;  // heavy
555
556                 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
557
558                 ptcache_file_compressed_write(pf, (unsigned char *)sds->shadow, in_len, out, mode);
559                 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len, out, mode);
560                 ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len, out, mode); 
561                 ptcache_file_compressed_write(pf, (unsigned char *)heat, in_len, out, mode);
562                 ptcache_file_compressed_write(pf, (unsigned char *)heatold, in_len, out, mode);
563                 ptcache_file_compressed_write(pf, (unsigned char *)vx, in_len, out, mode);
564                 ptcache_file_compressed_write(pf, (unsigned char *)vy, in_len, out, mode);
565                 ptcache_file_compressed_write(pf, (unsigned char *)vz, in_len, out, mode);
566                 ptcache_file_compressed_write(pf, (unsigned char *)vxold, in_len, out, mode);
567                 ptcache_file_compressed_write(pf, (unsigned char *)vyold, in_len, out, mode);
568                 ptcache_file_compressed_write(pf, (unsigned char *)vzold, in_len, out, mode);
569                 ptcache_file_compressed_write(pf, (unsigned char *)obstacles, (unsigned int)res, out, mode);
570                 ptcache_file_write(pf, &dt, 1, sizeof(float));
571                 ptcache_file_write(pf, &dx, 1, sizeof(float));
572
573                 MEM_freeN(out);
574                 
575                 ret = 1;
576         }
577
578         if (sds->wt) {
579                 int res_big_array[3];
580                 int res_big;
581                 int res = sds->res[0]*sds->res[1]*sds->res[2];
582                 float *dens, *densold, *tcu, *tcv, *tcw;
583                 unsigned int in_len = sizeof(float)*(unsigned int)res;
584                 unsigned int in_len_big;
585                 unsigned char *out;
586                 int mode;
587
588                 smoke_turbulence_get_res(sds->wt, res_big_array);
589                 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
590                 //mode =  res_big >= 1000000 ? 2 : 1;
591                 mode = 1;       // light
592                 if (sds->cache_high_comp == SM_CACHE_HEAVY) mode=2;     // heavy
593
594                 in_len_big = sizeof(float) * (unsigned int)res_big;
595
596                 smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
597
598                 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len_big), "pointcache_lzo_buffer");
599                 ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len_big, out, mode);
600                 ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len_big, out, mode);     
601                 MEM_freeN(out);
602
603                 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
604                 ptcache_file_compressed_write(pf, (unsigned char *)tcu, in_len, out, mode);
605                 ptcache_file_compressed_write(pf, (unsigned char *)tcv, in_len, out, mode);
606                 ptcache_file_compressed_write(pf, (unsigned char *)tcw, in_len, out, mode);
607                 MEM_freeN(out);
608                 
609                 ret = 1;
610         }
611
612         return ret;
613 }
614 static int ptcache_smoke_read(PTCacheFile *pf, void *smoke_v)
615 {
616         SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
617         SmokeDomainSettings *sds = smd->domain;
618         
619         if (sds->fluid) {
620                 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
621                 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
622                 unsigned char *obstacles;
623                 unsigned int out_len = (unsigned int)res * sizeof(float);
624                 
625                 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
626
627                 ptcache_file_compressed_read(pf, (unsigned char *)sds->shadow, out_len);
628                 ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len);
629                 ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len);
630                 ptcache_file_compressed_read(pf, (unsigned char*)heat, out_len);
631                 ptcache_file_compressed_read(pf, (unsigned char*)heatold, out_len);
632                 ptcache_file_compressed_read(pf, (unsigned char*)vx, out_len);
633                 ptcache_file_compressed_read(pf, (unsigned char*)vy, out_len);
634                 ptcache_file_compressed_read(pf, (unsigned char*)vz, out_len);
635                 ptcache_file_compressed_read(pf, (unsigned char*)vxold, out_len);
636                 ptcache_file_compressed_read(pf, (unsigned char*)vyold, out_len);
637                 ptcache_file_compressed_read(pf, (unsigned char*)vzold, out_len);
638                 ptcache_file_compressed_read(pf, (unsigned char*)obstacles, (unsigned int)res);
639                 ptcache_file_read(pf, &dt, 1, sizeof(float));
640                 ptcache_file_read(pf, &dx, 1, sizeof(float));
641
642                 if (pf->data_types & (1<<BPHYS_DATA_SMOKE_HIGH) && sds->wt) {
643                         int res = sds->res[0]*sds->res[1]*sds->res[2];
644                         int res_big, res_big_array[3];
645                         float *dens, *densold, *tcu, *tcv, *tcw;
646                         unsigned int out_len = sizeof(float)*(unsigned int)res;
647                         unsigned int out_len_big;
648
649                         smoke_turbulence_get_res(sds->wt, res_big_array);
650                         res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
651                         out_len_big = sizeof(float) * (unsigned int)res_big;
652
653                         smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
654
655                         ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len_big);
656                         ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len_big);
657
658                         ptcache_file_compressed_read(pf, (unsigned char*)tcu, out_len);
659                         ptcache_file_compressed_read(pf, (unsigned char*)tcv, out_len);
660                         ptcache_file_compressed_read(pf, (unsigned char*)tcw, out_len);
661                 }
662         }
663
664         return 1;
665 }
666 #else // WITH_SMOKE
667 static int  ptcache_smoke_totpoint(void *UNUSED(smoke_v), int UNUSED(cfra)) { return 0; }
668 static int  ptcache_smoke_read(PTCacheFile *UNUSED(pf), void *UNUSED(smoke_v)) { return 0; }
669 static int  ptcache_smoke_write(PTCacheFile *UNUSED(pf), void *UNUSED(smoke_v)) { return 0; }
670 #endif // WITH_SMOKE
671
672 static int ptcache_dynamicpaint_totpoint(void *sd, int UNUSED(cfra))
673 {
674         DynamicPaintSurface *surface = (DynamicPaintSurface*)sd;
675
676         if (!surface->data) return 0;
677         else return surface->data->total_points;
678 }
679
680 #define DPAINT_CACHE_VERSION "1.01"
681
682 static int  ptcache_dynamicpaint_write(PTCacheFile *pf, void *dp_v)
683 {       
684         DynamicPaintSurface *surface = (DynamicPaintSurface*)dp_v;
685         int cache_compress = 1;
686
687         /* version header */
688         ptcache_file_write(pf, DPAINT_CACHE_VERSION, 1, sizeof(char)*4);
689
690         if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
691                 int total_points=surface->data->total_points;
692                 unsigned int in_len;
693                 unsigned char *out;
694
695                 /* cache type */
696                 ptcache_file_write(pf, &surface->type, 1, sizeof(int));
697
698                 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
699                         in_len = sizeof(PaintPoint) * total_points;
700                 }
701                 else if (surface->type == MOD_DPAINT_SURFACE_T_DISPLACE ||
702                          surface->type == MOD_DPAINT_SURFACE_T_WEIGHT)
703                 {
704                         in_len = sizeof(float) * total_points;
705                 }
706                 else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
707                         in_len = sizeof(PaintWavePoint) * total_points;
708                 }
709                 else {
710                         return 0;
711                 }
712
713                 out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
714
715                 ptcache_file_compressed_write(pf, (unsigned char *)surface->data->type_data, in_len, out, cache_compress);
716                 MEM_freeN(out);
717
718         }
719         return 1;
720 }
721 static int ptcache_dynamicpaint_read(PTCacheFile *pf, void *dp_v)
722 {
723         DynamicPaintSurface *surface = (DynamicPaintSurface*)dp_v;
724         char version[4];
725         
726         /* version header */
727         ptcache_file_read(pf, version, 1, sizeof(char)*4);
728         if (strncmp(version, DPAINT_CACHE_VERSION, 4)) {printf("Dynamic Paint: Invalid cache version: %s!\n", version); return 0;}
729
730         if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
731                 unsigned int data_len;
732                 int surface_type;
733
734                 /* cache type */
735                 ptcache_file_read(pf, &surface_type, 1, sizeof(int));
736
737                 if (surface_type != surface->type)
738                         return 0;
739
740                 /* read surface data */
741                 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
742                         data_len = sizeof(PaintPoint);
743                 }
744                 else if (surface->type == MOD_DPAINT_SURFACE_T_DISPLACE ||
745                          surface->type == MOD_DPAINT_SURFACE_T_WEIGHT)
746                 {
747                         data_len = sizeof(float);
748                 }
749                 else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
750                         data_len = sizeof(PaintWavePoint);
751                 }
752                 else {
753                         return 0;
754                 }
755
756                 ptcache_file_compressed_read(pf, (unsigned char*)surface->data->type_data, data_len*surface->data->total_points);
757
758         }
759         return 1;
760 }
761
762 /* Creating ID's */
763 void BKE_ptcache_id_from_softbody(PTCacheID *pid, Object *ob, SoftBody *sb)
764 {
765         memset(pid, 0, sizeof(PTCacheID));
766
767         pid->ob= ob;
768         pid->calldata= sb;
769         pid->type= PTCACHE_TYPE_SOFTBODY;
770         pid->cache= sb->pointcache;
771         pid->cache_ptr= &sb->pointcache;
772         pid->ptcaches= &sb->ptcaches;
773         pid->totpoint= pid->totwrite= ptcache_softbody_totpoint;
774
775         pid->write_point                        = ptcache_softbody_write;
776         pid->read_point                         = ptcache_softbody_read;
777         pid->interpolate_point          = ptcache_softbody_interpolate;
778
779         pid->write_stream                       = NULL;
780         pid->read_stream                        = NULL;
781
782         pid->write_extra_data           = NULL;
783         pid->read_extra_data            = NULL;
784         pid->interpolate_extra_data     = NULL;
785
786         pid->write_header                       = ptcache_basic_header_write;
787         pid->read_header                        = ptcache_basic_header_read;
788
789         pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY);
790         pid->info_types= 0;
791
792         pid->stack_index = pid->cache->index;
793
794         pid->default_step = 10;
795         pid->max_step = 20;
796 }
797 void BKE_ptcache_id_from_particles(PTCacheID *pid, Object *ob, ParticleSystem *psys)
798 {
799         memset(pid, 0, sizeof(PTCacheID));
800
801         pid->ob= ob;
802         pid->calldata= psys;
803         pid->type= PTCACHE_TYPE_PARTICLES;
804         pid->stack_index= psys->pointcache->index;
805         pid->cache= psys->pointcache;
806         pid->cache_ptr= &psys->pointcache;
807         pid->ptcaches= &psys->ptcaches;
808
809         if (psys->part->type != PART_HAIR)
810                 pid->flag |= PTCACHE_VEL_PER_SEC;
811
812         pid->totpoint                           = ptcache_particle_totpoint;
813         pid->totwrite                           = ptcache_particle_totwrite;
814
815         pid->write_point                                = ptcache_particle_write;
816         pid->read_point                         = ptcache_particle_read;
817         pid->interpolate_point          = ptcache_particle_interpolate;
818
819         pid->write_stream                       = NULL;
820         pid->read_stream                        = NULL;
821
822         pid->write_extra_data           = NULL;
823         pid->read_extra_data            = NULL;
824         pid->interpolate_extra_data     = NULL;
825
826         pid->write_header                       = ptcache_basic_header_write;
827         pid->read_header                        = ptcache_basic_header_read;
828
829         pid->data_types = (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_INDEX);
830
831         if (psys->part->phystype == PART_PHYS_BOIDS)
832                 pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION) | (1<<BPHYS_DATA_BOIDS);
833         else if (psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS) {
834                 pid->write_extra_data = ptcache_particle_extra_write;
835                 pid->read_extra_data = ptcache_particle_extra_read;
836         }
837
838         if (psys->part->flag & PART_ROTATIONS) {
839                 pid->data_types|= (1<<BPHYS_DATA_ROTATION);
840
841                 if (psys->part->rotmode != PART_ROT_VEL  ||
842                     psys->part->avemode == PART_AVE_RAND ||
843                     psys->part->avefac != 0.0f)
844                 {
845                         pid->data_types |= (1 << BPHYS_DATA_AVELOCITY);
846                 }
847         }
848
849         pid->info_types= (1<<BPHYS_DATA_TIMES);
850
851         pid->default_step = 10;
852         pid->max_step = 20;
853 }
854 void BKE_ptcache_id_from_cloth(PTCacheID *pid, Object *ob, ClothModifierData *clmd)
855 {
856         memset(pid, 0, sizeof(PTCacheID));
857
858         pid->ob= ob;
859         pid->calldata= clmd;
860         pid->type= PTCACHE_TYPE_CLOTH;
861         pid->stack_index= clmd->point_cache->index;
862         pid->cache= clmd->point_cache;
863         pid->cache_ptr= &clmd->point_cache;
864         pid->ptcaches= &clmd->ptcaches;
865         pid->totpoint= pid->totwrite= ptcache_cloth_totpoint;
866
867         pid->write_point                        = ptcache_cloth_write;
868         pid->read_point                         = ptcache_cloth_read;
869         pid->interpolate_point          = ptcache_cloth_interpolate;
870
871         pid->write_stream                       = NULL;
872         pid->read_stream                        = NULL;
873
874         pid->write_extra_data           = NULL;
875         pid->read_extra_data            = NULL;
876         pid->interpolate_extra_data     = NULL;
877
878         pid->write_header                       = ptcache_basic_header_write;
879         pid->read_header                        = ptcache_basic_header_read;
880
881         pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_XCONST);
882         pid->info_types= 0;
883
884         pid->default_step = 1;
885         pid->max_step = 1;
886 }
887 void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct SmokeModifierData *smd)
888 {
889         SmokeDomainSettings *sds = smd->domain;
890
891         memset(pid, 0, sizeof(PTCacheID));
892
893         pid->ob= ob;
894         pid->calldata= smd;
895         
896         pid->type= PTCACHE_TYPE_SMOKE_DOMAIN;
897         pid->stack_index= sds->point_cache[0]->index;
898
899         pid->cache= sds->point_cache[0];
900         pid->cache_ptr= &(sds->point_cache[0]);
901         pid->ptcaches= &(sds->ptcaches[0]);
902
903         pid->totpoint= pid->totwrite= ptcache_smoke_totpoint;
904
905         pid->write_point                        = NULL;
906         pid->read_point                         = NULL;
907         pid->interpolate_point          = NULL;
908
909         pid->read_stream                        = ptcache_smoke_read;
910         pid->write_stream                       = ptcache_smoke_write;
911
912         pid->write_extra_data           = NULL;
913         pid->read_extra_data            = NULL;
914         pid->interpolate_extra_data     = NULL;
915
916         pid->write_header                       = ptcache_basic_header_write;
917         pid->read_header                        = ptcache_basic_header_read;
918
919         pid->data_types= 0;
920         pid->info_types= 0;
921
922         if (sds->fluid)
923                 pid->data_types |= (1<<BPHYS_DATA_SMOKE_LOW);
924         if (sds->wt)
925                 pid->data_types |= (1<<BPHYS_DATA_SMOKE_HIGH);
926
927         pid->default_step = 1;
928         pid->max_step = 1;
929 }
930
931 void BKE_ptcache_id_from_dynamicpaint(PTCacheID *pid, Object *ob, DynamicPaintSurface *surface)
932 {
933
934         memset(pid, 0, sizeof(PTCacheID));
935
936         pid->ob= ob;
937         pid->calldata= surface;
938         pid->type= PTCACHE_TYPE_DYNAMICPAINT;
939         pid->cache= surface->pointcache;
940         pid->cache_ptr= &surface->pointcache;
941         pid->ptcaches= &surface->ptcaches;
942         pid->totpoint= pid->totwrite= ptcache_dynamicpaint_totpoint;
943
944         pid->write_point                        = NULL;
945         pid->read_point                         = NULL;
946         pid->interpolate_point          = NULL;
947
948         pid->write_stream                       = ptcache_dynamicpaint_write;
949         pid->read_stream                        = ptcache_dynamicpaint_read;
950
951         pid->write_extra_data           = NULL;
952         pid->read_extra_data            = NULL;
953         pid->interpolate_extra_data     = NULL;
954
955         pid->write_header                       = ptcache_basic_header_write;
956         pid->read_header                        = ptcache_basic_header_read;
957
958         pid->data_types= BPHYS_DATA_DYNAMICPAINT;
959         pid->info_types= 0;
960
961         pid->stack_index = pid->cache->index;
962
963         pid->default_step = 1;
964         pid->max_step = 1;
965 }
966
967 void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int duplis)
968 {
969         PTCacheID *pid;
970         ParticleSystem *psys;
971         ModifierData *md;
972
973         lb->first= lb->last= NULL;
974
975         if (ob->soft) {
976                 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
977                 BKE_ptcache_id_from_softbody(pid, ob, ob->soft);
978                 BLI_addtail(lb, pid);
979         }
980
981         for (psys=ob->particlesystem.first; psys; psys=psys->next) {
982                 if (psys->part==NULL)
983                         continue;
984                 
985                 /* check to make sure point cache is actually used by the particles */
986                 if (ELEM(psys->part->phystype, PART_PHYS_NO, PART_PHYS_KEYED))
987                         continue;
988
989                 /* hair needs to be included in id-list for cache edit mode to work */
990                 /* if (psys->part->type == PART_HAIR && (psys->flag & PSYS_HAIR_DYNAMICS)==0) */
991                 /*      continue; */
992                         
993                 if (psys->part->type == PART_FLUID)
994                         continue;
995
996                 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
997                 BKE_ptcache_id_from_particles(pid, ob, psys);
998                 BLI_addtail(lb, pid);
999         }
1000
1001         for (md=ob->modifiers.first; md; md=md->next) {
1002                 if (md->type == eModifierType_Cloth) {
1003                         pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1004                         BKE_ptcache_id_from_cloth(pid, ob, (ClothModifierData*)md);
1005                         BLI_addtail(lb, pid);
1006                 }
1007                 else if (md->type == eModifierType_Smoke) {
1008                         SmokeModifierData *smd = (SmokeModifierData *)md;
1009                         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
1010                                 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1011                                 BKE_ptcache_id_from_smoke(pid, ob, (SmokeModifierData*)md);
1012                                 BLI_addtail(lb, pid);
1013                         }
1014                 }
1015                 else if (md->type == eModifierType_DynamicPaint) {
1016                         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
1017                         if (pmd->canvas) {
1018                                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
1019
1020                                 for (; surface; surface=surface->next) {
1021                                         pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1022                                         BKE_ptcache_id_from_dynamicpaint(pid, ob, surface);
1023                                         BLI_addtail(lb, pid);
1024                                 }
1025                         }
1026                 }
1027         }
1028
1029         if (scene && (duplis-- > 0) && (ob->transflag & OB_DUPLI)) {
1030                 ListBase *lb_dupli_ob;
1031
1032                 if ((lb_dupli_ob=object_duplilist(scene, ob))) {
1033                         DupliObject *dob;
1034                         for (dob= lb_dupli_ob->first; dob; dob= dob->next) {
1035                                 if (dob->ob != ob) { /* avoids recursive loops with dupliframes: bug 22988 */
1036                                         ListBase lb_dupli_pid;
1037                                         BKE_ptcache_ids_from_object(&lb_dupli_pid, dob->ob, scene, duplis);
1038                                         BLI_movelisttolist(lb, &lb_dupli_pid);
1039                                         if (lb_dupli_pid.first)
1040                                                 printf("Adding Dupli\n");
1041                                 }
1042                         }
1043
1044                         free_object_duplilist(lb_dupli_ob);     /* does restore */
1045                 }
1046         }
1047 }
1048
1049 /* File handling */
1050
1051 /* Takes an Object ID and returns a unique name
1052  * - id: object id
1053  * - cfra: frame for the cache, can be negative
1054  * - stack_index: index in the modifier stack. we can have cache for more then one stack_index
1055  */
1056
1057 #define MAX_PTCACHE_PATH FILE_MAX
1058 #define MAX_PTCACHE_FILE ((FILE_MAX)*2)
1059
1060 static int ptcache_path(PTCacheID *pid, char *filename)
1061 {
1062         Library *lib= (pid->ob)? pid->ob->id.lib: NULL;
1063         const char *blendfilename= (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH)==0) ? lib->filepath: G.main->name;
1064         size_t i;
1065
1066         if (pid->cache->flag & PTCACHE_EXTERNAL) {
1067                 strcpy(filename, pid->cache->path);
1068
1069                 if (BLI_path_is_rel(filename)) {
1070                         BLI_path_abs(filename, blendfilename);
1071                 }
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         &