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