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