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