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