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