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