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