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