Audaspace Py API documentation fixes.
[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 ? clmd->clothObject->numverts : 0;
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 #ifdef WITH_LZO
806         unsigned int out_len = len;
807         size_t sizeOfIt = 5;
808 #endif
809         unsigned char *in;
810         unsigned char *props = MEM_callocN(16*sizeof(char), "tmp");
811
812         ptcache_file_read(pf, &compressed, 1, sizeof(unsigned char));
813         if(compressed) {
814                 ptcache_file_read(pf, &in_len, 1, sizeof(unsigned int));
815                 if(in_len==0) {
816                         /* do nothing */
817                 }
818                 else {
819                         in = (unsigned char *)MEM_callocN(sizeof(unsigned char)*in_len, "pointcache_compressed_buffer");
820                         ptcache_file_read(pf, in, in_len, sizeof(unsigned char));
821 #ifdef WITH_LZO
822                         if(compressed == 1)
823                                 r = lzo1x_decompress_safe(in, (lzo_uint)in_len, result, (lzo_uint *)&out_len, NULL);
824 #endif
825 #ifdef WITH_LZMA
826                         if(compressed == 2)
827                         {
828                                 size_t leni = in_len, leno = out_len;
829                                 ptcache_file_read(pf, &sizeOfIt, 1, sizeof(unsigned int));
830                                 ptcache_file_read(pf, props, sizeOfIt, sizeof(unsigned char));
831                                 r = LzmaUncompress(result, &leno, in, &leni, props, sizeOfIt);
832                         }
833 #endif
834                         MEM_freeN(in);
835                 }
836         }
837         else {
838                 ptcache_file_read(pf, result, len, sizeof(unsigned char));
839         }
840
841         MEM_freeN(props);
842
843         return r;
844 }
845
846 static void ptcache_read_smoke(PTCacheFile *pf, void *smoke_v)
847 {
848         SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
849         SmokeDomainSettings *sds = smd->domain;
850         
851         if(sds->fluid) {
852                 size_t res = sds->res[0]*sds->res[1]*sds->res[2];
853                 float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
854                 unsigned char *obstacles;
855                 unsigned int out_len = (unsigned int)res * sizeof(float);
856                 
857                 smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
858
859                 ptcache_compress_read(pf, (unsigned char *)sds->shadow, out_len);
860                 ptcache_compress_read(pf, (unsigned char*)dens, out_len);
861                 ptcache_compress_read(pf, (unsigned char*)densold, out_len);
862                 ptcache_compress_read(pf, (unsigned char*)heat, out_len);
863                 ptcache_compress_read(pf, (unsigned char*)heatold, out_len);
864                 ptcache_compress_read(pf, (unsigned char*)vx, out_len);
865                 ptcache_compress_read(pf, (unsigned char*)vy, out_len);
866                 ptcache_compress_read(pf, (unsigned char*)vz, out_len);
867                 ptcache_compress_read(pf, (unsigned char*)vxold, out_len);
868                 ptcache_compress_read(pf, (unsigned char*)vyold, out_len);
869                 ptcache_compress_read(pf, (unsigned char*)vzold, out_len);
870                 ptcache_compress_read(pf, (unsigned char*)obstacles, (unsigned int)res);
871                 ptcache_file_read(pf, &dt, 1, sizeof(float));
872                 ptcache_file_read(pf, &dx, 1, sizeof(float));
873         }
874 }
875
876 static void ptcache_read_smoke_turbulence(PTCacheFile *pf, void *smoke_v)
877 {
878         SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
879         SmokeDomainSettings *sds = smd->domain;
880         
881         if(sds->fluid) {
882                 int res = sds->res[0]*sds->res[1]*sds->res[2];
883                 int res_big, res_big_array[3];
884                 float *dens, *densold, *tcu, *tcv, *tcw;
885                 unsigned int out_len = sizeof(float)*(unsigned int)res;
886                 unsigned int out_len_big;
887
888                 smoke_turbulence_get_res(sds->wt, res_big_array);
889                 res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
890                 out_len_big = sizeof(float) * (unsigned int)res_big;
891
892                 smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
893
894                 ptcache_compress_read(pf, (unsigned char*)dens, out_len_big);
895                 ptcache_compress_read(pf, (unsigned char*)densold, out_len_big);
896
897                 ptcache_compress_read(pf, (unsigned char*)tcu, out_len);
898                 ptcache_compress_read(pf, (unsigned char*)tcv, out_len);
899                 ptcache_compress_read(pf, (unsigned char*)tcw, out_len);                
900         }
901 }
902
903 void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct SmokeModifierData *smd)
904 {
905         SmokeDomainSettings *sds = smd->domain;
906
907         memset(pid, 0, sizeof(PTCacheID));
908
909         pid->ob= ob;
910         pid->calldata= smd;
911         
912         pid->type= PTCACHE_TYPE_SMOKE_DOMAIN;
913         pid->stack_index= sds->point_cache[0]->index;
914
915         pid->cache= sds->point_cache[0];
916         pid->cache_ptr= &(sds->point_cache[0]);
917         pid->ptcaches= &(sds->ptcaches[0]);
918
919         pid->totpoint= pid->totwrite= ptcache_totpoint_smoke;
920
921         pid->write_elem= NULL;
922         pid->read_elem= NULL;
923
924         pid->read_stream = ptcache_read_smoke;
925         pid->write_stream = ptcache_write_smoke;
926         
927         pid->interpolate_elem= NULL;
928
929         pid->write_header= ptcache_write_basic_header;
930         pid->read_header= ptcache_read_basic_header;
931
932         pid->data_types= (1<<BPHYS_DATA_LOCATION); // bogus values to make pointcache happy
933         pid->info_types= 0;
934 }
935
936 void BKE_ptcache_id_from_smoke_turbulence(PTCacheID *pid, struct Object *ob, struct SmokeModifierData *smd)
937 {
938         SmokeDomainSettings *sds = smd->domain;
939
940         memset(pid, 0, sizeof(PTCacheID));
941
942         pid->ob= ob;
943         pid->calldata= smd;
944         
945         pid->type= PTCACHE_TYPE_SMOKE_HIGHRES;
946         pid->stack_index= sds->point_cache[1]->index;
947
948         pid->cache= sds->point_cache[1];
949         pid->cache_ptr= &sds->point_cache[1];
950         pid->ptcaches= &sds->ptcaches[1];
951
952         pid->totpoint= pid->totwrite= ptcache_totpoint_smoke_turbulence;
953
954         pid->write_elem= NULL;
955         pid->read_elem= NULL;
956
957         pid->read_stream = ptcache_read_smoke_turbulence;
958         pid->write_stream = ptcache_write_smoke_turbulence;
959         
960         pid->interpolate_elem= NULL;
961
962         pid->write_header= ptcache_write_basic_header;
963         pid->read_header= ptcache_read_basic_header;
964
965         pid->data_types= (1<<BPHYS_DATA_LOCATION); // bogus values tot make pointcache happy
966         pid->info_types= 0;
967 }
968
969 void BKE_ptcache_id_from_cloth(PTCacheID *pid, Object *ob, ClothModifierData *clmd)
970 {
971         memset(pid, 0, sizeof(PTCacheID));
972
973         pid->ob= ob;
974         pid->calldata= clmd;
975         pid->type= PTCACHE_TYPE_CLOTH;
976         pid->stack_index= clmd->point_cache->index;
977         pid->cache= clmd->point_cache;
978         pid->cache_ptr= &clmd->point_cache;
979         pid->ptcaches= &clmd->ptcaches;
980         pid->totpoint= pid->totwrite= ptcache_totpoint_cloth;
981
982         pid->write_elem= ptcache_write_cloth;
983         pid->write_stream = NULL;
984         pid->read_stream = NULL;
985         pid->read_elem= ptcache_read_cloth;
986         pid->interpolate_elem= ptcache_interpolate_cloth;
987
988         pid->write_header= ptcache_write_basic_header;
989         pid->read_header= ptcache_read_basic_header;
990
991         pid->data_types= (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_XCONST);
992         pid->info_types= 0;
993 }
994
995 void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int duplis)
996 {
997         PTCacheID *pid;
998         ParticleSystem *psys;
999         ModifierData *md;
1000
1001         lb->first= lb->last= NULL;
1002
1003         if(ob->soft) {
1004                 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1005                 BKE_ptcache_id_from_softbody(pid, ob, ob->soft);
1006                 BLI_addtail(lb, pid);
1007         }
1008
1009         for(psys=ob->particlesystem.first; psys; psys=psys->next) {
1010                 if(psys->part) {
1011                         pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1012                         BKE_ptcache_id_from_particles(pid, ob, psys);
1013                         BLI_addtail(lb, pid);
1014                 }
1015         }
1016
1017         for(md=ob->modifiers.first; md; md=md->next) {
1018                 if(md->type == eModifierType_Cloth) {
1019                         pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1020                         BKE_ptcache_id_from_cloth(pid, ob, (ClothModifierData*)md);
1021                         BLI_addtail(lb, pid);
1022                 }
1023                 if(md->type == eModifierType_Smoke) {
1024                         SmokeModifierData *smd = (SmokeModifierData *)md;
1025                         if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
1026                         {
1027                                 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1028                                 BKE_ptcache_id_from_smoke(pid, ob, (SmokeModifierData*)md);
1029                                 BLI_addtail(lb, pid);
1030
1031                                 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
1032                                 BKE_ptcache_id_from_smoke_turbulence(pid, ob, (SmokeModifierData*)md);
1033                                 BLI_addtail(lb, pid);
1034                         }
1035                 }
1036         }
1037
1038         if(scene && (duplis-- > 0) && (ob->transflag & OB_DUPLI)) {
1039                 ListBase *lb_dupli_ob;
1040
1041                 if((lb_dupli_ob=object_duplilist(scene, ob))) {
1042                         DupliObject *dob;
1043                         for(dob= lb_dupli_ob->first; dob; dob= dob->next) {
1044                                 ListBase lb_dupli_pid;
1045                                 BKE_ptcache_ids_from_object(&lb_dupli_pid, dob->ob, scene, duplis);
1046                                 addlisttolist(lb, &lb_dupli_pid);
1047                                 if(lb_dupli_pid.first)
1048                                         printf("Adding Dupli\n");
1049                         }
1050
1051                         free_object_duplilist(lb_dupli_ob);     /* does restore */
1052                 }
1053         }
1054 }
1055
1056
1057 /* File handling */
1058
1059 /*      Takes an Object ID and returns a unique name
1060         - id: object id
1061         - cfra: frame for the cache, can be negative
1062         - stack_index: index in the modifier stack. we can have cache for more then one stack_index
1063 */
1064
1065 #define MAX_PTCACHE_PATH FILE_MAX
1066 #define MAX_PTCACHE_FILE ((FILE_MAXDIR+FILE_MAXFILE)*2)
1067
1068 static int ptcache_path(PTCacheID *pid, char *filename)
1069 {
1070         Library *lib= (pid)? pid->ob->id.lib: NULL;
1071         const char *blendfilename= (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH)==0) ? lib->filepath: G.sce;
1072         size_t i;
1073
1074         if(pid->cache->flag & PTCACHE_EXTERNAL) {
1075                 strcpy(filename, pid->cache->path);
1076
1077                 if(strncmp(filename, "//", 2)==0)
1078                         BLI_path_abs(filename, blendfilename);
1079
1080                 return BLI_add_slash(filename); /* new strlen() */
1081         }
1082         else if (G.relbase_valid || lib) {
1083                 char file[MAX_PTCACHE_PATH]; /* we dont want the dir, only the file */
1084
1085                 BLI_split_dirfile(blendfilename, NULL, file);
1086                 i = strlen(file);
1087                 
1088                 /* remove .blend */
1089                 if (i > 6)
1090                         file[i-6] = '\0';
1091                 
1092                 snprintf(filename, MAX_PTCACHE_PATH, "//"PTCACHE_PATH"%s", file); /* add blend file name to pointcache dir */
1093                 BLI_path_abs(filename, blendfilename);
1094                 return BLI_add_slash(filename); /* new strlen() */
1095         }
1096         
1097         /* use the temp path. this is weak but better then not using point cache at all */
1098         /* btempdir is assumed to exist and ALWAYS has a trailing slash */
1099         snprintf(filename, MAX_PTCACHE_PATH, "%s"PTCACHE_PATH"%d", btempdir, abs(getpid()));
1100         
1101         return BLI_add_slash(filename); /* new strlen() */
1102 }
1103
1104 static int BKE_ptcache_id_filename(PTCacheID *pid, char *filename, int cfra, short do_path, short do_ext)
1105 {
1106         int len=0;
1107         char *idname;
1108         char *newname;
1109         filename[0] = '\0';
1110         newname = filename;
1111         
1112         if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return 0; /* save blend file before using disk pointcache */
1113         
1114         /* start with temp dir */
1115         if (do_path) {
1116                 len = ptcache_path(pid, filename);
1117                 newname += len;
1118         }
1119         if(strcmp(pid->cache->name, "")==0 && (pid->cache->flag & PTCACHE_EXTERNAL)==0) {
1120                 idname = (pid->ob->id.name+2);
1121                 /* convert chars to hex so they are always a valid filename */
1122                 while('\0' != *idname) {
1123                         snprintf(newname, MAX_PTCACHE_FILE, "%02X", (char)(*idname++));
1124                         newname+=2;
1125                         len += 2;
1126                 }
1127         }
1128         else {
1129                 int temp = (int)strlen(pid->cache->name); 
1130                 strcpy(newname, pid->cache->name); 
1131                 newname+=temp;
1132                 len += temp;
1133         }
1134
1135         if (do_ext) {
1136
1137                 if(pid->cache->index < 0)
1138                         pid->cache->index =  pid->stack_index = object_insert_ptcache(pid->ob);
1139
1140                 if(pid->cache->flag & PTCACHE_EXTERNAL) {
1141                         if(pid->cache->index >= 0)
1142                                 snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02d"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
1143                         else
1144                                 snprintf(newname, MAX_PTCACHE_FILE, "_%06d"PTCACHE_EXT, cfra); /* always 6 chars */
1145                 }
1146                 else {
1147                         snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02d"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
1148                 }
1149                 len += 16;
1150         }
1151         
1152         return len; /* make sure the above string is always 16 chars */
1153 }
1154
1155 /* youll need to close yourself after! */
1156 static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
1157 {
1158         PTCacheFile *pf;
1159         FILE *fp = NULL;
1160         char filename[(FILE_MAXDIR+FILE_MAXFILE)*2];
1161
1162 #ifndef DURIAN_POINTCACHE_LIB_OK
1163         /* don't allow writing for linked objects */
1164         if(pid->ob->id.lib && mode == PTCACHE_FILE_WRITE)
1165                 return NULL;
1166 #endif
1167         if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return NULL; /* save blend file before using disk pointcache */
1168         
1169         BKE_ptcache_id_filename(pid, filename, cfra, 1, 1);
1170
1171         if (mode==PTCACHE_FILE_READ) {
1172                 if (!BLI_exists(filename)) {
1173                         return NULL;
1174                 }
1175                  fp = fopen(filename, "rb");
1176         } else if (mode==PTCACHE_FILE_WRITE) {
1177                 BLI_make_existing_file(filename); /* will create the dir if needs be, same as //textures is created */
1178                 fp = fopen(filename, "wb");
1179         } else if (mode==PTCACHE_FILE_UPDATE) {
1180                 BLI_make_existing_file(filename);
1181                 fp = fopen(filename, "rb+");
1182         }
1183
1184          if (!fp)
1185                  return NULL;
1186         
1187         pf= MEM_mallocN(sizeof(PTCacheFile), "PTCacheFile");
1188         pf->fp= fp;
1189         
1190          return pf;
1191 }
1192
1193 static void ptcache_file_close(PTCacheFile *pf)
1194 {
1195         fclose(pf->fp);
1196         MEM_freeN(pf);
1197 }
1198
1199 static int ptcache_file_read(PTCacheFile *pf, void *f, size_t tot, int size)
1200 {
1201         return (fread(f, size, tot, pf->fp) == tot);
1202 }
1203 static int ptcache_file_write(PTCacheFile *pf, void *f, size_t tot, int size)
1204 {
1205         return (fwrite(f, size, tot, pf->fp) == tot);
1206 }
1207 static int ptcache_file_read_data(PTCacheFile *pf)
1208 {
1209         int i;
1210
1211         for(i=0; i<BPHYS_TOT_DATA; i++) {
1212                 if(pf->data_types & (1<<i) && !ptcache_file_read(pf, pf->cur[i], 1, ptcache_data_size[i]))
1213                         return 0;
1214         }
1215         
1216         return 1;
1217 }
1218 static int ptcache_file_write_data(PTCacheFile *pf)
1219 {               
1220         int i;
1221
1222         for(i=0; i<BPHYS_TOT_DATA; i++) {
1223                 if(pf->data_types & (1<<i) && !ptcache_file_write(pf, pf->cur[i], 1, ptcache_data_size[i]))
1224                         return 0;
1225         }
1226         
1227         return 1;
1228 }
1229 static int ptcache_file_read_header_begin(PTCacheFile *pf)
1230 {
1231         int error=0;
1232         char bphysics[8];
1233         
1234         pf->data_types = 0;
1235         
1236         if(fread(bphysics, sizeof(char), 8, pf->fp) != 8)
1237                 error = 1;
1238         
1239         if(!error && strncmp(bphysics, "BPHYSICS", 8))
1240                 error = 1;
1241
1242         if(!error && !fread(&pf->type, sizeof(int), 1, pf->fp))
1243                 error = 1;
1244         
1245         /* if there was an error set file as it was */
1246         if(error)
1247                 fseek(pf->fp, 0, SEEK_SET);
1248
1249         return !error;
1250 }
1251
1252
1253 static int ptcache_file_write_header_begin(PTCacheFile *pf)
1254 {
1255         char *bphysics = "BPHYSICS";
1256         
1257         if(fwrite(bphysics, sizeof(char), 8, pf->fp) != 8)
1258                 return 0;
1259
1260         if(!fwrite(&pf->type, sizeof(int), 1, pf->fp))
1261                 return 0;
1262         
1263         return 1;
1264 }
1265
1266
1267 /* Data pointer handling */
1268 int BKE_ptcache_data_size(int data_type)
1269 {
1270         return ptcache_data_size[data_type];
1271 }
1272
1273 static void ptcache_file_init_pointers(PTCacheFile *pf)
1274 {
1275         int data_types = pf->data_types;
1276
1277         pf->cur[BPHYS_DATA_INDEX] =             data_types & (1<<BPHYS_DATA_INDEX) ?            &pf->data.index : NULL;
1278         pf->cur[BPHYS_DATA_LOCATION] =  data_types & (1<<BPHYS_DATA_LOCATION) ?         &pf->data.loc   : NULL;
1279         pf->cur[BPHYS_DATA_VELOCITY] =  data_types & (1<<BPHYS_DATA_VELOCITY) ?         &pf->data.vel   : NULL;
1280         pf->cur[BPHYS_DATA_ROTATION] =  data_types & (1<<BPHYS_DATA_ROTATION) ?         &pf->data.rot   : NULL;
1281         pf->cur[BPHYS_DATA_AVELOCITY] = data_types & (1<<BPHYS_DATA_AVELOCITY) ?        &pf->data.ave   : NULL;
1282         pf->cur[BPHYS_DATA_SIZE] =              data_types & (1<<BPHYS_DATA_SIZE)       ?               &pf->data.size  : NULL;
1283         pf->cur[BPHYS_DATA_TIMES] =             data_types & (1<<BPHYS_DATA_TIMES) ?            &pf->data.times : NULL;
1284         pf->cur[BPHYS_DATA_BOIDS] =             data_types & (1<<BPHYS_DATA_BOIDS) ?            &pf->data.boids : NULL;
1285 }
1286
1287 static void ptcache_file_seek_pointers(int index, PTCacheFile *pf)
1288 {
1289         int i, size=0;
1290         int data_types = pf->data_types;
1291
1292         if(data_types & (1<<BPHYS_DATA_INDEX)) {
1293                 int totpoint;
1294                 /* The simplest solution is to just write to the very end. This may cause
1295                  * some data duplication, but since it's on disk it's not so bad. The correct
1296                  * thing would be to search through the file for the correct index and only
1297                  * write to the end if it's not found, but this could be quite slow.
1298                  */
1299                 fseek(pf->fp, 8 + sizeof(int), SEEK_SET);
1300                 fread(&totpoint, sizeof(int), 1, pf->fp);
1301                 
1302                 totpoint++;
1303
1304                 fseek(pf->fp, 8 + sizeof(int), SEEK_SET);
1305                 fwrite(&totpoint, sizeof(int), 1, pf->fp);
1306
1307                 fseek(pf->fp, 0, SEEK_END);
1308         }
1309         else {
1310                 for(i=0; i<BPHYS_TOT_DATA; i++)
1311                         size += pf->data_types & (1<<i) ? ptcache_data_size[i] : 0;
1312
1313                 /* size of default header + data up to index */
1314                 fseek(pf->fp, 8 + 3*sizeof(int) + index * size, SEEK_SET);
1315         }
1316
1317         ptcache_file_init_pointers(pf);
1318 }
1319 void BKE_ptcache_mem_init_pointers(PTCacheMem *pm)
1320 {
1321         int data_types = pm->data_types;
1322         int i;
1323
1324         for(i=0; i<BPHYS_TOT_DATA; i++)
1325                 pm->cur[i] = data_types & (1<<i) ? pm->data[i] : NULL;
1326 }
1327
1328 void BKE_ptcache_mem_incr_pointers(PTCacheMem *pm)
1329 {
1330         int i;
1331
1332         for(i=0; i<BPHYS_TOT_DATA; i++) {
1333                 if(pm->cur[i])
1334                         pm->cur[i] = (char*)pm->cur[i] + ptcache_data_size[i];
1335         }
1336 }
1337 int BKE_ptcache_mem_seek_pointers(int point_index, PTCacheMem *pm)
1338 {
1339         int data_types = pm->data_types;
1340         int i, index = pm->index_array ? pm->index_array[point_index] - 1 : point_index;
1341
1342         if(index < 0) {
1343                 /* Can't give proper location without reallocation, so don't give any location.
1344                  * Some points will be cached improperly, but this only happens with simulation
1345                  * steps bigger than cache->step, so the cache has to be recalculated anyways
1346                  * at some point.
1347                  */
1348                 return 0;
1349         }
1350
1351         for(i=0; i<BPHYS_TOT_DATA; i++)
1352                 pm->cur[i] = data_types & (1<<i) ? (char*)pm->data[i] + index * ptcache_data_size[i] : NULL;
1353
1354         return 1;
1355 }
1356 static void ptcache_alloc_data(PTCacheMem *pm)
1357 {
1358         int data_types = pm->data_types;
1359         int totpoint = pm->totpoint;
1360         int i;
1361
1362         for(i=0; i<BPHYS_TOT_DATA; i++) {
1363                 if(data_types & (1<<i))
1364                         pm->data[i] = MEM_callocN(totpoint * ptcache_data_size[i], "PTCache Data");
1365         }
1366 }
1367 static void ptcache_free_data(PTCacheMem *pm)
1368 {
1369         void **data = pm->data;
1370         int i;
1371
1372         for(i=0; i<BPHYS_TOT_DATA; i++) {
1373                 if(data[i])
1374                         MEM_freeN(data[i]);
1375         }
1376
1377         if(pm->index_array) {
1378                 MEM_freeN(pm->index_array);
1379                 pm->index_array = NULL;
1380         }
1381 }
1382 static void ptcache_copy_data(void *from[], void *to[])
1383 {
1384         int i;
1385         for(i=0; i<BPHYS_TOT_DATA; i++) {
1386         /* note, durian file 03.4b_comp crashes if to[i] is not tested
1387          * its NULL, not sure if this should be fixed elsewhere but for now its needed */
1388                 if(from[i] && to[i])
1389                         memcpy(to[i], from[i], ptcache_data_size[i]);
1390         }
1391 }
1392
1393
1394
1395 static int ptcache_pid_old_elemsize(PTCacheID *pid)
1396 {
1397         if(pid->type==PTCACHE_TYPE_SOFTBODY)
1398                 return 6 * sizeof(float);
1399         else if(pid->type==PTCACHE_TYPE_PARTICLES)
1400                 return sizeof(ParticleKey);
1401         else if(pid->type==PTCACHE_TYPE_CLOTH)
1402                 return 9 * sizeof(float);
1403
1404         return 0;
1405 }
1406
1407 /* reads cache from disk or memory */
1408 /* possible to get old or interpolated result */
1409 int BKE_ptcache_read_cache(PTCacheID *pid, float cfra, float frs_sec)
1410 {
1411         PTCacheFile *pf=NULL, *pf2=NULL;
1412         PTCacheMem *pm=NULL, *pm2=NULL;
1413         float old_data1[14], old_data2[14];
1414         int cfrai = (int)cfra;
1415         int old_elemsize = ptcache_pid_old_elemsize(pid);
1416         int i;
1417
1418         int cfra1 = 0, cfra2 = 0;
1419         int totpoint = 0, totpoint2 = 0;
1420         int *index = &i, *index2 = &i;
1421         int use_old = 0, old_frame = 0;
1422
1423         int ret = 0, error = 0;
1424
1425         /* nothing to read to */
1426         if(pid->totpoint(pid->calldata, (int)cfra) == 0)
1427                 return 0;
1428
1429         if(pid->cache->flag & PTCACHE_READ_INFO) {
1430                 pid->cache->flag &= ~PTCACHE_READ_INFO;
1431                 BKE_ptcache_read_cache(pid, 0, frs_sec);
1432         }
1433
1434
1435         /* first check if we have the actual frame cached */
1436         if(cfra == (float)cfrai) {
1437                 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1438                         pf= ptcache_file_open(pid, PTCACHE_FILE_READ, cfrai);
1439                 }
1440                 else {
1441                         pm = pid->cache->mem_cache.first;
1442
1443                         for(; pm; pm=pm->next) {
1444                                 if(pm->frame == cfrai)
1445                                         break;
1446                         }
1447                 }
1448         }
1449
1450         /* no exact cache frame found so try to find cached frames around cfra */
1451         if(!pm && !pf) {
1452                 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1453                         pf=NULL;
1454                         while(cfrai > pid->cache->startframe && !pf) {
1455                                 cfrai--;
1456                                 pf= ptcache_file_open(pid, PTCACHE_FILE_READ, cfrai);
1457                                 cfra1 = cfrai;
1458                         }
1459
1460                         old_frame = cfrai;
1461
1462                         cfrai = (int)cfra;
1463                         while(cfrai < pid->cache->endframe && !pf2) {
1464                                 cfrai++;
1465                                 pf2= ptcache_file_open(pid, PTCACHE_FILE_READ, cfrai);
1466                                 cfra2 = cfrai;
1467                         }
1468
1469                         if(pf && !pf2) {
1470                                 pf2 = pf;
1471                                 pf = NULL;
1472                         }
1473                 }
1474                 else if(pid->cache->mem_cache.first){
1475                         pm = pid->cache->mem_cache.first;
1476
1477                         while(pm->next && pm->next->frame < cfra)
1478                                 pm= pm->next;
1479
1480                         if(pm) {
1481                                 old_frame = pm->frame;
1482                                 cfra1 = pm->frame;
1483                         }
1484
1485                         pm2 = pid->cache->mem_cache.last;
1486
1487                         if(pm2 && pm2->frame < cfra)
1488                                 pm2 = NULL;
1489                         else {
1490                                 while(pm2->prev && pm2->prev->frame > cfra)
1491                                         pm2= pm2->prev;
1492
1493                                 if(pm2)
1494                                         cfra2 = pm2->frame;
1495                         }
1496
1497                         if(pm && !pm2) {
1498                                 pm2 = pm;
1499                                 pm = NULL;
1500                         }
1501                 }
1502         }
1503
1504         if(!pm && !pm2 && !pf && !pf2)
1505                 return 0;
1506
1507         if(pm) {
1508                 BKE_ptcache_mem_init_pointers(pm);
1509                 totpoint = pm->totpoint;
1510                 index = pm->data_types & (1<<BPHYS_DATA_INDEX) ? pm->cur[BPHYS_DATA_INDEX] : &i;
1511         }
1512         if(pm2) {
1513                 BKE_ptcache_mem_init_pointers(pm2);
1514                 totpoint2 = pm2->totpoint;
1515                 index2 = pm2->data_types & (1<<BPHYS_DATA_INDEX) ? pm2->cur[BPHYS_DATA_INDEX] : &i;
1516         }
1517         if(pf) {
1518                 if(ptcache_file_read_header_begin(pf)) {
1519                         if(pf->type != pid->type) {
1520                                 /* todo report error */
1521                                 ptcache_file_close(pf);
1522                                 pf = NULL;
1523                         }
1524                         else if(pid->read_header(pf)) {
1525                                 ptcache_file_init_pointers(pf);
1526                                 totpoint = pf->totpoint;
1527                                 index = pf->data_types & (1<<BPHYS_DATA_INDEX) ? &pf->data.index : &i;
1528                         }
1529                 }
1530                 else {
1531                         /* fall back to old cache file format */
1532                         use_old = 1;
1533                         totpoint = pid->totpoint(pid->calldata, (int) cfra);
1534                 }
1535         }
1536         if(pf2) {
1537                 if(ptcache_file_read_header_begin(pf2)) {
1538                         if(pf2->type != pid->type) {
1539                                 /* todo report error */
1540                                 ptcache_file_close(pf2);
1541                                 pf2 = NULL;
1542                         }
1543                         else if(pid->read_header(pf2)) {
1544                                 ptcache_file_init_pointers(pf2);
1545                                 totpoint2 = pf2->totpoint;
1546                                 index2 = pf2->data_types & (1<<BPHYS_DATA_INDEX) ? &pf2->data.index : &i;
1547                         }
1548                 }
1549                 else {
1550                         /* fall back to old cache file format */
1551                         use_old = 1;
1552                         totpoint2 = pid->totpoint(pid->calldata, (int) cfra);
1553                 }
1554         }
1555
1556         /* don't read old cache if already simulated past cached frame */
1557         if(!pm && !pf && cfra1 && cfra1 <= pid->cache->simframe)
1558                 error = 1;
1559         if(cfra1 && cfra1==cfra2)
1560                 error = 1;
1561
1562         if(!error) 
1563         {
1564                 if(pf && pid->read_stream) {
1565                         if(totpoint != pid->totpoint(pid->calldata, (int) cfra))
1566                                 error = 1;
1567                         else
1568                         {
1569                                 // we have stream writing here
1570                                 pid->read_stream(pf, pid->calldata);
1571                         }
1572                 }
1573         }
1574
1575         if((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
1576                 totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, (int) cfra));
1577
1578         if(!error) 
1579         {       
1580                 for(i=0; i<totpoint; i++) {
1581                         /* read old cache file format */
1582                         if(use_old) {
1583                                 if(pid->read_elem && ptcache_file_read(pf, (void*)old_data1, 1, old_elemsize))
1584                                         pid->read_elem(i, pid->calldata, NULL, frs_sec, cfra, old_data1);
1585                                 else if(pid->read_elem)
1586                                         { error = 1; break; }
1587                         }
1588                         else {
1589                                 if(pid->read_elem && (pm || ptcache_file_read_data(pf)))
1590                                         pid->read_elem(*index, pid->calldata, pm ? pm->cur : pf->cur, frs_sec, cfra1 ? (float)cfra1 : (float)cfrai, NULL);
1591                                 else if(pid->read_elem)
1592                                         { error = 1; break; }
1593                         }
1594
1595                         if(pm) {
1596                                 BKE_ptcache_mem_incr_pointers(pm);
1597                                 index = pm->data_types & (1<<BPHYS_DATA_INDEX) ? pm->cur[BPHYS_DATA_INDEX] : &i;
1598                         }
1599                 }
1600         }
1601
1602         if(!error) 
1603         {
1604                 if(pf2 && pid->read_stream) {
1605                         if(totpoint2 != pid->totpoint(pid->calldata, (int) cfra))
1606                                 error = 1;
1607                         else
1608                         {
1609                                 // we have stream writing here
1610                                 pid->read_stream(pf2, pid->calldata);
1611                         }
1612                 }
1613         }
1614
1615         if((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
1616                 totpoint2 = MIN2(totpoint2, pid->totpoint(pid->calldata, (int) cfra));
1617
1618         if(!error) 
1619         {
1620                 for(i=0; i<totpoint2; i++) {
1621                         /* read old cache file format */
1622                         if(use_old) {
1623                                 if(pid->read_elem && ptcache_file_read(pf2, (void*)old_data2, 1, old_elemsize)) {
1624                                         if(!pf && pf2)
1625                                                 pid->read_elem(i, pid->calldata, NULL, frs_sec, (float)cfra2, old_data2);
1626                                         else if(pid->interpolate_elem)
1627                                                 pid->interpolate_elem(i, pid->calldata, NULL, frs_sec, cfra, (float)cfra1, (float)cfra2, old_data2);
1628                                         else
1629                                         { error = 1; break; }
1630                                 }
1631                                 else if(pid->read_elem)
1632                                         { error = 1; break; }
1633                         }
1634                         else {
1635                                 if(pid->read_elem && (pm2 || ptcache_file_read_data(pf2))) {
1636                                         if((!pf && pf2) || (!pm && pm2))
1637                                                 pid->read_elem(*index2, pid->calldata, pm2 ? pm2->cur : pf2->cur, frs_sec, (float)cfra2, NULL);
1638                                         else if(pid->interpolate_elem)
1639                                                 pid->interpolate_elem(*index2, pid->calldata, pm2 ? pm2->cur : pf2->cur, frs_sec, cfra, (float)cfra1, (float)cfra2, NULL);
1640                                         else
1641                                         { error = 1; break; }
1642                                 }
1643                                 else if(pid->read_elem)
1644                                         { error = 1; break; }
1645                         }
1646
1647                         if(pm2) {
1648                                 BKE_ptcache_mem_incr_pointers(pm2);
1649                                 index2 = pm2->data_types & (1<<BPHYS_DATA_INDEX) ? pm2->cur[BPHYS_DATA_INDEX] : &i;
1650                         }
1651                 }
1652         }
1653
1654         if(pm || pf)
1655                 ret = (pm2 || pf2) ? PTCACHE_READ_INTERPOLATED : PTCACHE_READ_EXACT;
1656         else if(pm2 || pf2) {
1657                 ret = PTCACHE_READ_OLD;
1658                 pid->cache->simframe = old_frame;
1659         }
1660
1661         if(pf) {
1662                 ptcache_file_close(pf);
1663                 pf = NULL;
1664         }
1665
1666         if(pf2) {
1667                 ptcache_file_close(pf2);
1668                 pf = NULL;
1669         }
1670
1671         if((pid->cache->flag & PTCACHE_QUICK_CACHE)==0) {
1672                 cfrai = (int)cfra;
1673                 /* clear invalid cache frames so that better stuff can be simulated */
1674                 if(pid->cache->flag & PTCACHE_OUTDATED) {
1675                         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, cfrai);
1676                 }
1677                 else if(pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
1678                         if(cfra <= pid->cache->last_exact)
1679                                 pid->cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
1680
1681                         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, MAX2(cfrai,pid->cache->last_exact));
1682                 }
1683         }
1684
1685         return (error ? 0 : ret);
1686 }
1687 /* TODO for later */
1688 static void ptcache_make_index_array(PTCacheMem *pm, int totpoint)
1689 {
1690         int i, *index;
1691
1692         if(pm->index_array) {
1693                 MEM_freeN(pm->index_array);
1694                 pm->index_array = NULL;
1695         }
1696
1697         if(!pm->data[BPHYS_DATA_INDEX])
1698                 return;
1699
1700         pm->index_array = MEM_callocN(totpoint * sizeof(int), "PTCacheMem index_array");
1701         index = pm->data[BPHYS_DATA_INDEX];
1702
1703         for(i=0; i<pm->totpoint; i++, index++)
1704                 pm->index_array[*index] = i + 1;
1705 }
1706 /* writes cache to disk or memory */
1707 int BKE_ptcache_write_cache(PTCacheID *pid, int cfra)
1708 {
1709         PointCache *cache = pid->cache;
1710         PTCacheFile *pf= NULL, *pf2= NULL;
1711         int i;
1712         int totpoint = pid->totpoint(pid->calldata, cfra);
1713         int add = 0, overwrite = 0;
1714
1715         if(totpoint == 0 || (cfra ? pid->data_types == 0 : pid->info_types == 0))
1716                 return 0;
1717
1718         if(cache->flag & PTCACHE_DISK_CACHE) {
1719                 int ofra=0, efra = cache->endframe;
1720
1721                 if(cfra==0 && cache->startframe > 0)
1722                         add = 1;
1723                 /* allways start from scratch on the first frame */
1724                 else if(cfra == cache->startframe) {
1725                         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, cfra);
1726                         cache->flag &= ~PTCACHE_REDO_NEEDED;
1727                         add = 1;
1728                 }
1729                 else {
1730                         /* find last cached frame */
1731                         while(efra > cache->startframe && !BKE_ptcache_id_exist(pid, efra))
1732                                 efra--;
1733
1734                         /* find second last cached frame */
1735                         ofra = efra-1;
1736                         while(ofra > cache->startframe && !BKE_ptcache_id_exist(pid, ofra))
1737                                 ofra--;
1738
1739                         if(efra >= cache->startframe && cfra > efra) {
1740                                 if(ofra >= cache->startframe && efra - ofra < cache->step)
1741                                         overwrite = 1;
1742                                 else
1743                                         add = 1;
1744                         }
1745                 }
1746
1747                 if(add || overwrite) {
1748                         if(overwrite)
1749                                 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, efra);
1750
1751                         pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, cfra);
1752                         if(!pf)
1753                                 return 0;
1754
1755                         pf->type = pid->type;
1756                         pf->totpoint = cfra ? pid->totwrite(pid->calldata, cfra) : totpoint;
1757                         pf->data_types = cfra ? pid->data_types : pid->info_types;
1758
1759                         if(!ptcache_file_write_header_begin(pf) || !pid->write_header(pf)) {
1760                                 ptcache_file_close(pf);
1761                                 return 0;
1762                         }
1763
1764                         ptcache_file_init_pointers(pf);
1765
1766                         if(pf && pid->write_stream) {
1767                                 // we have stream writing here
1768                                 pid->write_stream(pf, pid->calldata);
1769                         }
1770                         else
1771                                 for(i=0; i<totpoint; i++) {
1772                                         if(pid->write_elem) {
1773                                                 int write = pid->write_elem(i, pid->calldata, pf->cur, cfra);
1774                                                 if(write) {
1775                                                         if(!ptcache_file_write_data(pf)) {
1776                                                                 ptcache_file_close(pf);
1777                                                                 if(pf2) ptcache_file_close(pf2);
1778                                                                 return 0;
1779                                                         }
1780                                                         /* newly born particles have to be copied to previous cached frame */
1781                                                         else if(overwrite && write == 2) {
1782                                                                 if(!pf2) {
1783                                                                         pf2 = ptcache_file_open(pid, PTCACHE_FILE_UPDATE, ofra);
1784                                                                         if(!pf2) {
1785                                                                                 ptcache_file_close(pf);
1786                                                                                 return 0;
1787                                                                         }
1788                                                                         pf2->type = pid->type;
1789                                                                         pf2->totpoint = totpoint;
1790                                                                         pf2->data_types = pid->data_types;
1791                                                                 }
1792                                                                 ptcache_file_seek_pointers(i, pf2);
1793                                                                 pid->write_elem(i, pid->calldata, pf2->cur, cfra);
1794                                                                 if(!ptcache_file_write_data(pf2)) {
1795                                                                         ptcache_file_close(pf);
1796                                                                         ptcache_file_close(pf2);
1797                                                                         return 0;
1798                                                                 }
1799                                                         }
1800                                                 }
1801                                         }
1802                                 }
1803                 }
1804         }
1805         else {
1806                 PTCacheMem *pm;
1807                 PTCacheMem *pm2;
1808
1809                 pm2 = cache->mem_cache.first;
1810                 
1811                 /* don't write info file in memory */
1812                 if(cfra==0)
1813                         return 1;
1814                 /* allways start from scratch on the first frame */
1815                 if(cfra == cache->startframe) {
1816                         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, cfra);
1817                         cache->flag &= ~PTCACHE_REDO_NEEDED;
1818                         add = 1;
1819                 }
1820                 else if (cache->mem_cache.last) {
1821                         pm2 = cache->mem_cache.last;
1822
1823                         if(pm2 && cfra > pm2->frame) {
1824                                 if(pm2->prev && pm2->frame - pm2->prev->frame < cache->step)
1825                                         overwrite = 1;
1826                                 else
1827                                         add = 1;
1828                         }
1829                 }
1830                 else
1831                         add = 1;
1832
1833                 if(add || overwrite) {
1834                         if(overwrite)
1835                                 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, pm2->frame);
1836
1837                         pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
1838
1839                         pm->totpoint = pid->totwrite(pid->calldata, cfra);
1840                         pm->data_types = cfra ? pid->data_types : pid->info_types;
1841
1842                         ptcache_alloc_data(pm);
1843                         BKE_ptcache_mem_init_pointers(pm);
1844
1845                         for(i=0; i<totpoint; i++) {
1846                                 if(pid->write_elem) {
1847                                         int write = pid->write_elem(i, pid->calldata, pm->cur, cfra);
1848                                         if(write) {
1849                                                 BKE_ptcache_mem_incr_pointers(pm);
1850
1851                                                 /* newly born particles have to be copied to previous cached frame */
1852                                                 if(overwrite && write == 2) {
1853                                                         pm2 = cache->mem_cache.last;
1854                                                         if(BKE_ptcache_mem_seek_pointers(i, pm2))
1855                                                                 pid->write_elem(i, pid->calldata, pm2->cur, cfra);
1856                                                 }
1857                                         }
1858                                 }
1859                         }
1860                         ptcache_make_index_array(pm, pid->totpoint(pid->calldata, cfra));
1861
1862                         pm->frame = cfra;
1863                         BLI_addtail(&cache->mem_cache, pm);
1864                 }
1865         }
1866
1867         if(add || overwrite) {
1868                 if(cfra - cache->last_exact == 1
1869                         || cfra == cache->startframe) {
1870                         cache->last_exact = cfra;
1871                         cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
1872                 }
1873                 else
1874                         cache->flag |= PTCACHE_FRAMES_SKIPPED;
1875         }
1876         
1877         if(pf) ptcache_file_close(pf);
1878
1879         if(pf2) ptcache_file_close(pf2);
1880
1881         BKE_ptcache_update_info(pid);
1882
1883         return 1;
1884 }
1885 /* youll need to close yourself after!
1886  * mode - PTCACHE_CLEAR_ALL, 
1887
1888 */
1889 /* Clears & resets */
1890 void BKE_ptcache_id_clear(PTCacheID *pid, int mode, int cfra)
1891 {
1892         int len; /* store the length of the string */
1893
1894         /* mode is same as fopen's modes */
1895         DIR *dir; 
1896         struct dirent *de;
1897         char path[MAX_PTCACHE_PATH];
1898         char filename[MAX_PTCACHE_FILE];
1899         char path_full[MAX_PTCACHE_FILE];
1900         char ext[MAX_PTCACHE_PATH];
1901
1902         if(!pid->cache || pid->cache->flag & PTCACHE_BAKED)
1903                 return;
1904
1905 #ifndef DURIAN_POINTCACHE_LIB_OK
1906         /* don't allow clearing for linked objects */
1907         if(pid->ob->id.lib)
1908                 return;
1909 #endif
1910
1911         /*if (!G.relbase_valid) return; *//* save blend file before using pointcache */
1912         
1913         /* clear all files in the temp dir with the prefix of the ID and the ".bphys" suffix */
1914         switch (mode) {
1915         case PTCACHE_CLEAR_ALL:
1916         case PTCACHE_CLEAR_BEFORE:      
1917         case PTCACHE_CLEAR_AFTER:
1918                 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1919                         ptcache_path(pid, path);
1920                         
1921                         len = BKE_ptcache_id_filename(pid, filename, cfra, 0, 0); /* no path */
1922                         
1923                         dir = opendir(path);
1924                         if (dir==NULL)
1925                                 return;
1926
1927                         snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, pid->stack_index);
1928                         
1929                         while ((de = readdir(dir)) != NULL) {
1930                                 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
1931                                         if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
1932                                                 if (mode == PTCACHE_CLEAR_ALL) {
1933                                                         pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
1934                                                         BLI_join_dirfile(path_full, path, de->d_name);
1935                                                         BLI_delete(path_full, 0, 0);
1936                                                 } else {
1937                                                         /* read the number of the file */
1938                                                         int frame, len2 = (int)strlen(de->d_name);
1939                                                         char num[7];
1940
1941                                                         if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
1942                                                                 BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
1943                                                                 frame = atoi(num);
1944                                                                 
1945                                                                 if((mode==PTCACHE_CLEAR_BEFORE && frame < cfra) || 
1946                                                                 (mode==PTCACHE_CLEAR_AFTER && frame > cfra)     ) {
1947                                                                         
1948                                                                         BLI_join_dirfile(path_full, path, de->d_name);
1949                                                                         BLI_delete(path_full, 0, 0);
1950                                                                 }
1951                                                         }
1952                                                 }
1953                                         }
1954                                 }
1955                         }
1956                         closedir(dir);
1957                 }
1958                 else {
1959                         PTCacheMem *pm= pid->cache->mem_cache.first;
1960                         PTCacheMem *link= NULL;
1961
1962                         pm= pid->cache->mem_cache.first;
1963
1964                         if(mode == PTCACHE_CLEAR_ALL) {
1965                                 /*we want startframe if the cache starts before zero*/
1966                                 pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
1967                                 for(; pm; pm=pm->next)
1968                                         ptcache_free_data(pm);
1969                                 BLI_freelistN(&pid->cache->mem_cache);
1970                         } else {
1971                                 while(pm) {
1972                                         if((mode==PTCACHE_CLEAR_BEFORE && pm->frame < cfra)     || 
1973                                         (mode==PTCACHE_CLEAR_AFTER && pm->frame > cfra) ) {
1974                                                 link = pm;
1975                                                 ptcache_free_data(pm);
1976                                                 pm = pm->next;
1977                                                 BLI_freelinkN(&pid->cache->mem_cache, link);
1978                                         }
1979                                         else
1980                                                 pm = pm->next;
1981                                 }
1982                         }
1983                 }
1984                 break;
1985                 
1986         case PTCACHE_CLEAR_FRAME:
1987                 if(pid->cache->flag & PTCACHE_DISK_CACHE) {
1988                         if(BKE_ptcache_id_exist(pid, cfra)) {
1989                                 BKE_ptcache_id_filename(pid, filename, cfra, 1, 1); /* no path */
1990                                 BLI_delete(filename, 0, 0);
1991                         }
1992                 }
1993                 else {
1994                         PTCacheMem *pm = pid->cache->mem_cache.first;
1995
1996                         for(; pm; pm=pm->next) {
1997                                 if(pm->frame == cfra) {
1998                                         ptcache_free_data(pm);
1999                                         BLI_freelinkN(&pid->cache->mem_cache, pm);
2000                                         break;
2001                                 }
2002                         }
2003                 }
2004                 break;
2005         }
2006
2007         BKE_ptcache_update_info(pid);
2008 }
2009
2010 int BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
2011 {
2012         if(!pid->cache)
2013                 return 0;
2014         
2015         if(pid->cache->flag & PTCACHE_DISK_CACHE) {
2016                 char filename[MAX_PTCACHE_FILE];
2017                 
2018                 BKE_ptcache_id_filename(pid, filename, cfra, 1, 1);
2019
2020                 return BLI_exists(filename);
2021         }
2022         else {
2023                 PTCacheMem *pm = pid->cache->mem_cache.first;
2024
2025                 for(; pm; pm=pm->next) {
2026                         if(pm->frame==cfra)
2027                                 return 1;
2028                 }
2029                 return 0;
2030         }
2031 }
2032
2033 void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
2034 {
2035         Object *ob;
2036         PointCache *cache;
2037         float offset, time, nexttime;
2038
2039         /* TODO: this has to be sorter out once bsystem_time gets redone, */
2040         /*       now caches can handle interpolating etc. too - jahka */
2041
2042         /* time handling for point cache:
2043          * - simulation time is scaled by result of bsystem_time
2044          * - for offsetting time only time offset is taken into account, since
2045          *   that's always the same and can't be animated. a timeoffset which
2046          *   varies over time is not simpe to support.
2047          * - field and motion blur offsets are currently ignored, proper solution
2048          *   is probably to interpolate results from two frames for that ..
2049          */
2050
2051         ob= pid->ob;
2052         cache= pid->cache;
2053
2054         if(timescale) {
2055                 time= bsystem_time(scene, ob, cfra, 0.0f);
2056                 nexttime= bsystem_time(scene, ob, cfra+1.0f, 0.0f);
2057
2058                 *timescale= MAX2(nexttime - time, 0.0f);
2059         }
2060
2061         if(startframe && endframe) {
2062                 *startframe= cache->startframe;
2063                 *endframe= cache->endframe;
2064
2065                 // XXX ipoflag is depreceated - old animation system stuff
2066                 if (/*(ob->ipoflag & OB_OFFS_PARENT) &&*/ (ob->partype & PARSLOW)==0) {
2067                         offset= give_timeoffset(ob);
2068
2069                         *startframe += (int)(offset+0.5f);
2070                         *endframe += (int)(offset+0.5f);
2071                 }
2072         }
2073 }
2074
2075 int BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode)
2076 {
2077         PointCache *cache;
2078         int reset, clear, after;
2079
2080         if(!pid->cache)
2081                 return 0;
2082
2083         cache= pid->cache;
2084         reset= 0;
2085         clear= 0;
2086         after= 0;
2087
2088         if(mode == PTCACHE_RESET_DEPSGRAPH) {
2089                 if(!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) {
2090                         if(cache->flag & PTCACHE_QUICK_CACHE)
2091                                 clear= 1;
2092
2093                         after= 1;
2094                 }
2095
2096                 cache->flag |= PTCACHE_OUTDATED;
2097         }
2098         else if(mode == PTCACHE_RESET_BAKED) {
2099                 if(!BKE_ptcache_get_continue_physics()) {
2100                         reset= 1;
2101                         clear= 1;
2102                 }
2103                 else
2104                         cache->flag |= PTCACHE_OUTDATED;
2105         }
2106         else if(mode == PTCACHE_RESET_OUTDATED) {
2107                 reset = 1;
2108
2109                 if(cache->flag & PTCACHE_OUTDATED && !(cache->flag & PTCACHE_BAKED)) {
2110                         clear= 1;
2111                         cache->flag &= ~PTCACHE_OUTDATED;
2112                 }
2113         }
2114
2115         if(reset) {
2116                 BKE_ptcache_invalidate(cache);
2117                 cache->flag &= ~PTCACHE_REDO_NEEDED;
2118
2119                 if(pid->type == PTCACHE_TYPE_CLOTH)
2120                         cloth_free_modifier(pid->ob, pid->calldata);
2121                 else if(pid->type == PTCACHE_TYPE_SOFTBODY)
2122                         sbFreeSimulation(pid->calldata);
2123                 else if(pid->type == PTCACHE_TYPE_PARTICLES)
2124                         psys_reset(pid->calldata, PSYS_RESET_DEPSGRAPH);
2125                 else if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
2126                         smokeModifier_reset(pid->calldata);
2127                 else if(pid->type == PTCACHE_TYPE_SMOKE_HIGHRES)
2128                         smokeModifier_reset_turbulence(pid->calldata);
2129         }
2130         if(clear)
2131                 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2132         else if(after)
2133                 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, CFRA);
2134
2135         return (reset || clear || after);
2136 }
2137
2138 int BKE_ptcache_object_reset(Scene *scene, Object *ob, int mode)
2139 {
2140         PTCacheID pid;
2141         ParticleSystem *psys;
2142         ModifierData *md;
2143         int reset, skip;
2144
2145         reset= 0;
2146         skip= 0;
2147
2148         if(ob->soft) {
2149                 BKE_ptcache_id_from_softbody(&pid, ob, ob->soft);
2150                 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2151         }
2152
2153         for(psys=ob->particlesystem.first; psys; psys=psys->next) {
2154                 /* Baked cloth hair has to be checked first, because we don't want to reset */
2155                 /* particles or cloth in that case -jahka */
2156                 if(psys->clmd) {
2157                         BKE_ptcache_id_from_cloth(&pid, ob, psys->clmd);
2158                         if(mode == PSYS_RESET_ALL || !(psys->part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED))) 
2159                                 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2160                         else
2161                                 skip = 1;
2162                 }
2163                 else if(psys->recalc & PSYS_RECALC_REDO || psys->recalc & PSYS_RECALC_CHILD)
2164                         skip = 1;
2165
2166                 if(skip == 0 && psys->part) {
2167                         BKE_ptcache_id_from_particles(&pid, ob, psys);
2168                         reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2169                 }
2170         }
2171
2172         for(md=ob->modifiers.first; md; md=md->next) {
2173                 if(md->type == eModifierType_Cloth) {
2174                         BKE_ptcache_id_from_cloth(&pid, ob, (ClothModifierData*)md);
2175                         reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2176                 }
2177                 if(md->type == eModifierType_Smoke) {
2178                         SmokeModifierData *smd = (SmokeModifierData *)md;
2179                         if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
2180                         {
2181                                 BKE_ptcache_id_from_smoke(&pid, ob, (SmokeModifierData*)md);
2182                                 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2183
2184                                 BKE_ptcache_id_from_smoke_turbulence(&pid, ob, (SmokeModifierData*)md);
2185                                 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2186                         }
2187                 }
2188         }
2189
2190         if (ob->type == OB_ARMATURE)
2191                 BIK_clear_cache(ob->pose);
2192
2193         return reset;
2194 }
2195
2196 /* Use this when quitting blender, with unsaved files */
2197 void BKE_ptcache_remove(void)
2198 {
2199         char path[MAX_PTCACHE_PATH];
2200         char path_full[MAX_PTCACHE_PATH];
2201         int rmdir = 1;
2202         
2203         ptcache_path(NULL, path);
2204
2205         if (BLI_exist(path)) {
2206                 /* The pointcache dir exists? - remove all pointcache */
2207
2208                 DIR *dir; 
2209                 struct dirent *de;
2210
2211                 dir = opendir(path);
2212                 if (dir==NULL)
2213                         return;
2214                 
2215                 while ((de = readdir(dir)) != NULL) {
2216                         if( strcmp(de->d_name, ".")==0 || strcmp(de->d_name, "..")==0) {
2217                                 /* do nothing */
2218                         } else if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
2219                                 BLI_join_dirfile(path_full, path, de->d_name);
2220                                 BLI_delete(path_full, 0, 0);
2221                         } else {
2222                                 rmdir = 0; /* unknown file, dont remove the dir */
2223                         }
2224                 }
2225
2226                 closedir(dir);
2227         } else { 
2228                 rmdir = 0; /* path dosnt exist  */
2229         }
2230         
2231         if (rmdir) {
2232                 BLI_delete(path, 1, 0);
2233         }
2234 }
2235
2236 /* Continuous Interaction */
2237
2238 static int CONTINUE_PHYSICS = 0;
2239
2240 void BKE_ptcache_set_continue_physics(Scene *scene, int enable)
2241 {
2242         Object *ob;
2243
2244         if(CONTINUE_PHYSICS != enable) {
2245                 CONTINUE_PHYSICS = enable;
2246
2247                 if(CONTINUE_PHYSICS == 0) {
2248                         for(ob=G.main->object.first; ob; ob=ob->id.next)
2249                                 if(BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_OUTDATED))
2250                                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
2251                 }
2252         }
2253 }
2254
2255 int BKE_ptcache_get_continue_physics()
2256 {
2257         return CONTINUE_PHYSICS;
2258 }
2259
2260 /* Point Cache handling */
2261
2262 PointCache *BKE_ptcache_add(ListBase *ptcaches)
2263 {
2264         PointCache *cache;
2265
2266         cache= MEM_callocN(sizeof(PointCache), "PointCache");
2267         cache->startframe= 1;
2268         cache->endframe= 250;
2269         cache->step= 10;
2270         cache->index = -1;
2271
2272         BLI_addtail(ptcaches, cache);
2273
2274         return cache;
2275 }
2276
2277 void BKE_ptcache_free_mem(ListBase *mem_cache)
2278 {
2279         PTCacheMem *pm = mem_cache->first;
2280
2281         if(pm) {
2282                 for(; pm; pm=pm->next)
2283                         ptcache_free_data(pm);
2284
2285                 BLI_freelistN(mem_cache);
2286         }
2287 }
2288 void BKE_ptcache_free(PointCache *cache)
2289 {
2290         BKE_ptcache_free_mem(&cache->mem_cache);
2291         if(cache->edit && cache->free_edit)
2292                 cache->free_edit(cache->edit);
2293         MEM_freeN(cache);
2294 }
2295 void BKE_ptcache_free_list(ListBase *ptcaches)
2296 {
2297         PointCache *cache = ptcaches->first;
2298
2299         while(cache) {
2300                 BLI_remlink(ptcaches, cache);
2301                 BKE_ptcache_free(cache);
2302                 cache = ptcaches->first;
2303         }
2304 }
2305
2306 static PointCache *ptcache_copy(PointCache *cache)
2307 {
2308         PointCache *ncache;
2309
2310         ncache= MEM_dupallocN(cache);
2311
2312         /* hmm, should these be copied over instead? */
2313         ncache->mem_cache.first = NULL;
2314         ncache->mem_cache.last = NULL;
2315
2316         ncache->flag= 0;
2317         ncache->simframe= 0;
2318
2319         return ncache;
2320 }
2321 /* returns first point cache */
2322 PointCache *BKE_ptcache_copy_list(ListBase *ptcaches_new, ListBase *ptcaches_old)
2323 {
2324         PointCache *cache = ptcaches_old->first;
2325
2326         ptcaches_new->first = ptcaches_new->last = NULL;
2327
2328         for(; cache; cache=cache->next)
2329                 BLI_addtail(ptcaches_new, ptcache_copy(cache));
2330
2331         return ptcaches_new->first;
2332 }
2333
2334
2335 /* Baking */
2336 void BKE_ptcache_quick_cache_all(Scene *scene)
2337 {
2338         PTCacheBaker baker;
2339
2340         baker.bake=0;
2341         baker.break_data=NULL;
2342         baker.break_test=NULL;
2343         baker.pid=NULL;
2344         baker.progressbar=NULL;
2345         baker.progressend=NULL;
2346         baker.progresscontext=NULL;
2347         baker.render=0;
2348         baker.anim_init = 0;
2349         baker.scene=scene;
2350         baker.quick_step=scene->physics_settings.quick_cache_step;
2351
2352         BKE_ptcache_make_cache(&baker);
2353 }
2354
2355 /* Simulation thread, no need for interlocks as data written in both threads
2356  are only unitary integers (I/O assumed to be atomic for them) */
2357 typedef struct {
2358         int break_operation;
2359         int thread_ended;
2360         int endframe;
2361         int step;
2362         int *cfra_ptr;
2363         Scene *scene;
2364 } ptcache_make_cache_data;
2365
2366 static void *ptcache_make_cache_thread(void *ptr) {
2367         ptcache_make_cache_data *data = (ptcache_make_cache_data*)ptr;
2368
2369         for(; (*data->cfra_ptr <= data->endframe) && !data->break_operation; *data->cfra_ptr+=data->step) {
2370                 scene_update_for_newframe(data->scene, data->scene->lay);
2371                 if(G.background) {
2372                         printf("bake: frame %d :: %d\n", (int)*data->cfra_ptr, data->endframe);
2373                 }
2374         }
2375
2376         data->thread_ended = TRUE;
2377         return NULL;
2378 }
2379
2380 /* if bake is not given run simulations to current frame */
2381 void BKE_ptcache_make_cache(PTCacheBaker* baker)
2382 {
2383         Scene *scene = baker->scene;
2384         Scene *sce; /* SETLOOPER macro only */
2385         Base *base;
2386         ListBase pidlist;
2387         PTCacheID *pid = baker->pid;
2388         PointCache *cache = NULL;
2389         float frameleno = scene->r.framelen;
2390         int cfrao = CFRA;
2391         int startframe = MAXFRAME;
2392         int bake = baker->bake;
2393         int render = baker->render;
2394         ListBase threads;
2395         ptcache_make_cache_data thread_data;
2396         int progress, old_progress;
2397         
2398         thread_data.endframe = baker->anim_init ? scene->r.sfra : CFRA;
2399         thread_data.step = baker->quick_step;
2400         thread_data.cfra_ptr = &CFRA;
2401         thread_data.scene = baker->scene;
2402
2403         G.afbreek = 0;
2404
2405         /* set caches to baking mode and figure out start frame */
2406         if(pid) {
2407                 /* cache/bake a single object */
2408                 cache = pid->cache;
2409                 if((cache->flag & PTCACHE_BAKED)==0) {
2410                         if(pid->type==PTCACHE_TYPE_PARTICLES) {
2411                                 ParticleSystem *psys= pid->calldata;
2412
2413                                 /* a bit confusing, could make this work better in the UI */
2414                                 if(psys->part->type == PART_EMITTER)
2415                                         psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
2416                         }
2417                         else if(pid->type == PTCACHE_TYPE_SMOKE_HIGHRES) {
2418                                 /* get all pids from the object and search for smoke low res */
2419                                 ListBase pidlist2;
2420                                 PTCacheID *pid2;
2421                                 BKE_ptcache_ids_from_object(&pidlist2, pid->ob, scene, MAX_DUPLI_RECUR);
2422                                 for(pid2=pidlist2.first; pid2; pid2=pid2->next) {
2423                                         if(pid2->type == PTCACHE_TYPE_SMOKE_DOMAIN) 
2424                                         {
2425                                                 if(pid2->cache && !(pid2->cache->flag & PTCACHE_BAKED)) {
2426                                                         if(bake || pid2->cache->flag & PTCACHE_REDO_NEEDED)
2427                                                                 BKE_ptcache_id_clear(pid2, PTCACHE_CLEAR_ALL, 0);
2428                                                         if(bake) {
2429                                                                 pid2->cache->flag |= PTCACHE_BAKING;
2430                                                                 pid2->cache->flag &= ~PTCACHE_BAKED;
2431                                                         }
2432                                                 }
2433                                         }
2434                                 }
2435                                 BLI_freelistN(&pidlist2);
2436                         }
2437
2438                         if(bake || cache->flag & PTCACHE_REDO_NEEDED)
2439                                 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2440
2441                         startframe = MAX2(cache->last_exact, cache->startframe);
2442
2443                         if(bake) {
2444                                 thread_data.endframe = cache->endframe;
2445                                 cache->flag |= PTCACHE_BAKING;
2446                         }
2447                         else {
2448                                 thread_data.endframe = MIN2(thread_data.endframe, cache->endframe);
2449                         }
2450
2451                         cache->flag &= ~PTCACHE_BAKED;
2452                 }
2453         }
2454         else for(SETLOOPER(scene, base)) {
2455                 /* cache/bake everything in the scene */
2456                 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
2457
2458                 for(pid=pidlist.first; pid; pid=pid->next) {
2459                         cache = pid->cache;
2460                         if((cache->flag & PTCACHE_BAKED)==0) {
2461                                 if(pid->type==PTCACHE_TYPE_PARTICLES) {
2462                                         ParticleSystem *psys = (ParticleSystem*)pid->calldata;
2463                                         /* skip hair & keyed particles */
2464                                         if(psys->part->type == PART_HAIR || psys->part->phystype == PART_PHYS_KEYED)
2465                                                 continue;
2466
2467                                         psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
2468                                 }
2469
2470                                 if((cache->flag & PTCACHE_REDO_NEEDED || (cache->flag & PTCACHE_SIMULATION_VALID)==0)
2471                                         && ((cache->flag & PTCACHE_QUICK_CACHE)==0 || render || bake))
2472                                         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2473
2474                                 startframe = MIN2(startframe, cache->startframe);
2475
2476                                 if(bake || render) {
2477                                         cache->flag |= PTCACHE_BAKING;
2478
2479                                         if(bake)
2480                                                 thread_data.endframe = MAX2(thread_data.endframe, cache->endframe);
2481                                 }
2482
2483                                 cache->flag &= ~PTCACHE_BAKED;
2484
2485                         }
2486                 }
2487                 BLI_freelistN(&pidlist);
2488         }
2489
2490         CFRA = startframe;
2491         scene->r.framelen = 1.0;
2492         thread_data.break_operation = FALSE;
2493         thread_data.thread_ended = FALSE;
2494         old_progress = -1;
2495         
2496         if(G.background) {
2497                 ptcache_make_cache_thread((void*)&thread_data);
2498         }
2499         else {
2500                 BLI_init_threads(&threads, ptcache_make_cache_thread, 1);
2501                 BLI_insert_thread(&threads, (void*)&thread_data);
2502
2503                 while (thread_data.thread_ended == FALSE) {
2504
2505                         if(bake)
2506                                 progress = (int)(100.0f * (float)(CFRA - startframe)/(float)(thread_data.endframe-startframe));
2507                         else
2508                                 progress = CFRA;
2509
2510                         /* NOTE: baking should not redraw whole ui as this slows things down */
2511                         if ((baker->progressbar) && (progress != old_progress)) {
2512                                 baker->progressbar(baker->progresscontext, progress);
2513                                 old_progress = progress;
2514                         }
2515
2516                         /* Delay to lessen CPU load from UI thread */
2517                         PIL_sleep_ms(200);
2518
2519                         /* NOTE: breaking baking should leave calculated frames in cache, not clear it */
2520                         if(blender_test_break() && !thread_data.break_operation) {
2521                                 thread_data.break_operation = TRUE;
2522                                 if (baker->progressend)
2523                                         baker->progressend(baker->progresscontext);
2524                                 WM_cursor_wait(1);
2525                         }
2526                 }
2527
2528         BLI_end_threads(&threads);
2529         }
2530         /* clear baking flag */
2531         if(pid) {
2532                 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
2533                 cache->flag |= PTCACHE_SIMULATION_VALID;
2534                 if(bake) {
2535                         cache->flag |= PTCACHE_BAKED;
2536                         /* write info file */
2537                         if(cache->flag & PTCACHE_DISK_CACHE)
2538                                 BKE_ptcache_write_cache(pid, 0);
2539                 }
2540         }
2541         else for(SETLOOPER(scene, base)) {
2542                 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
2543
2544                 for(pid=pidlist.first; pid; pid=pid->next) {
2545                         /* skip hair particles */
2546                         if(pid->type==PTCACHE_TYPE_PARTICLES && ((ParticleSystem*)pid->calldata)->part->type == PART_HAIR)
2547                                 continue;
2548                 
2549                         cache = pid->cache;
2550
2551                         if(thread_data.step > 1)
2552                                 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_OUTDATED);
2553                         else
2554                                 cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
2555
2556                         cache->flag |= PTCACHE_SIMULATION_VALID;
2557
2558                         if(bake) {
2559                                 cache->flag |= PTCACHE_BAKED;
2560                                 if(cache->flag & PTCACHE_DISK_CACHE)
2561                                         BKE_ptcache_write_cache(pid, 0);
2562                         }
2563                 }
2564                 BLI_freelistN(&pidlist);
2565         }
2566
2567         scene->r.framelen = frameleno;
2568         CFRA = cfrao;
2569         
2570         if(bake) /* already on cfra unless baking */
2571                 scene_update_for_newframe(scene, scene->lay);
2572
2573         if (thread_data.break_operation)
2574                 WM_cursor_wait(0);
2575         else if (baker->progressend)
2576                 baker->progressend(baker->progresscontext);
2577
2578         /* TODO: call redraw all windows somehow */
2579 }
2580 /* Helpers */
2581 void BKE_ptcache_disk_to_mem(PTCacheID *pid)
2582 {
2583         PointCache *cache = pid->cache;
2584         PTCacheFile *pf;
2585         PTCacheMem *pm;
2586
2587         int cfra, sfra = cache->startframe, efra = cache->endframe;
2588         int i;
2589
2590         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2591
2592         for(cfra=sfra; cfra <= efra; cfra++) {
2593                 pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
2594
2595                 if(pf) {
2596                         if(!ptcache_file_read_header_begin(pf)) {
2597                                 printf("Can't yet convert old cache format\n");
2598                                 cache->flag |= PTCACHE_DISK_CACHE;
2599                                 ptcache_file_close(pf);
2600                                 return;
2601                         }
2602
2603                         if(pf->type != pid->type || !pid->read_header(pf)) {
2604                                 cache->flag |= PTCACHE_DISK_CACHE;
2605                                 ptcache_file_close(pf);
2606                                 return;
2607                         }
2608                         
2609                         pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
2610
2611                         pm->totpoint = pf->totpoint;
2612                         pm->data_types = pf->data_types;
2613                         pm->frame = cfra;
2614
2615                         ptcache_alloc_data(pm);
2616                         BKE_ptcache_mem_init_pointers(pm);
2617                         ptcache_file_init_pointers(pf);
2618
2619                         for(i=0; i<pm->totpoint; i++) {
2620                                 if(!ptcache_file_read_data(pf)) {
2621                                         printf("Error reading from disk cache\n");
2622                                         
2623                                         cache->flag |= PTCACHE_DISK_CACHE;
2624                                         
2625                                         ptcache_free_data(pm);
2626                                         MEM_freeN(pm);
2627                                         ptcache_file_close(pf);
2628
2629                                         return;
2630                                 }
2631                                 ptcache_copy_data(pf->cur, pm->cur);
2632                                 BKE_ptcache_mem_incr_pointers(pm);
2633                         }
2634
2635                         ptcache_make_index_array(pm, pid->totpoint(pid->calldata, cfra));
2636
2637                         BLI_addtail(&pid->cache->mem_cache, pm);
2638
2639                         ptcache_file_close(pf);
2640                 }
2641         }
2642
2643 }
2644 void BKE_ptcache_mem_to_disk(PTCacheID *pid)
2645 {
2646         PointCache *cache = pid->cache;
2647         PTCacheFile *pf;
2648         PTCacheMem *pm;
2649         int i;
2650
2651         pm = cache->mem_cache.first;
2652
2653         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2654
2655         for(; pm; pm=pm->next) {
2656                 pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, pm->frame);
2657
2658                 if(pf) {
2659                         pf->data_types = pm->data_types;
2660                         pf->totpoint = pm->totpoint;
2661                         pf->type = pid->type;
2662
2663                         BKE_ptcache_mem_init_pointers(pm);
2664                         ptcache_file_init_pointers(pf);
2665
2666                         if(!ptcache_file_write_header_begin(pf) || !pid->write_header(pf)) {
2667                                 if (G.f & G_DEBUG) 
2668                                         printf("Error writing to disk cache\n");
2669                                 cache->flag &= ~PTCACHE_DISK_CACHE;
2670
2671                                 ptcache_file_close(pf);
2672                                 return;
2673                         }
2674
2675                         for(i=0; i<pm->totpoint; i++) {
2676                                 ptcache_copy_data(pm->cur, pf->cur);
2677                                 if(!ptcache_file_write_data(pf)) {
2678                                         if (G.f & G_DEBUG) 
2679                                                 printf("Error writing to disk cache\n");
2680                                         cache->flag &= ~PTCACHE_DISK_CACHE;
2681
2682                                         ptcache_file_close(pf);
2683                                         return;
2684                                 }
2685                                 BKE_ptcache_mem_incr_pointers(pm);
2686                         }
2687
2688                         ptcache_file_close(pf);
2689
2690                         /* write info file */
2691                         if(cache->flag & PTCACHE_BAKED)
2692                                 BKE_ptcache_write_cache(pid, 0);
2693                 }
2694                 else
2695                         if (G.f & G_DEBUG) 
2696                                 printf("Error creating disk cache file\n");
2697         }
2698 }
2699 void BKE_ptcache_toggle_disk_cache(PTCacheID *pid)
2700 {
2701         PointCache *cache = pid->cache;
2702         int last_exact = cache->last_exact;
2703
2704         if (!G.relbase_valid){
2705                 cache->flag &= ~PTCACHE_DISK_CACHE;
2706                 if (G.f & G_DEBUG) 
2707                         printf("File must be saved before using disk cache!\n");
2708                 return;
2709         }
2710
2711         if(cache->flag & PTCACHE_DISK_CACHE)
2712                 BKE_ptcache_mem_to_disk(pid);
2713         else
2714                 BKE_ptcache_disk_to_mem(pid);
2715
2716         cache->flag ^= PTCACHE_DISK_CACHE;
2717         BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
2718         cache->flag ^= PTCACHE_DISK_CACHE;
2719         
2720         cache->last_exact = last_exact;
2721
2722         BKE_ptcache_update_info(pid);
2723 }
2724
2725 void BKE_ptcache_load_external(PTCacheID *pid)
2726 {
2727         /*todo*/
2728         PointCache *cache = pid->cache;
2729         int len; /* store the length of the string */
2730         int info = 0;
2731
2732         /* mode is same as fopen's modes */
2733         DIR *dir; 
2734         struct dirent *de;
2735         char path[MAX_PTCACHE_PATH];
2736         char filename[MAX_PTCACHE_FILE];
2737         char ext[MAX_PTCACHE_PATH];
2738
2739         if(!cache)
2740                 return;
2741
2742         cache->startframe = MAXFRAME;
2743         cache->endframe = -1;
2744         cache->totpoint = 0;
2745
2746         ptcache_path(pid, path);
2747         
2748         len = BKE_ptcache_id_filename(pid, filename, 1, 0, 0); /* no path */
2749         
2750         dir = opendir(path);
2751         if (dir==NULL)
2752                 return;
2753
2754         if(cache->index >= 0)
2755                 snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, cache->index);
2756         else
2757                 strcpy(ext, PTCACHE_EXT);
2758         
2759         while ((de = readdir(dir)) != NULL) {
2760                 if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
2761                         if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
2762                                 /* read the number of the file */
2763                                 int frame, len2 = (int)strlen(de->d_name);
2764                                 char num[7];
2765
2766                                 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
2767                                         BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
2768                                         frame = atoi(num);
2769
2770                                         if(frame) {
2771                                                 cache->startframe = MIN2(cache->startframe, frame);
2772                                                 cache->endframe = MAX2(cache->endframe, frame);
2773                                         }
2774                                         else
2775                                                 info = 1;
2776                                 }
2777                         }
2778                 }
2779         }
2780         closedir(dir);
2781
2782         if(cache->startframe != MAXFRAME) {
2783                 PTCacheFile *pf;
2784
2785                 /* read totpoint from info file (frame 0) */
2786                 if(info) {
2787                         pf= ptcache_file_open(pid, PTCACHE_FILE_READ, 0);
2788
2789                         if(pf) {
2790                                 if(ptcache_file_read_header_begin(pf)) {
2791                                         if(pf->type == pid->type && pid->read_header(pf)) {
2792                                                 cache->totpoint = pf->totpoint;
2793                                                 cache->flag |= PTCACHE_READ_INFO;
2794                                         }
2795                                         else {
2796                                                 cache->totpoint = 0;
2797                                         }
2798                                 }
2799                                 ptcache_file_close(pf);
2800                         }
2801                 }
2802                 /* or from any old format cache file */
2803                 else {
2804                         float old_data[14];
2805                         int elemsize = ptcache_pid_old_elemsize(pid);
2806                         pf= ptcache_file_open(pid, PTCACHE_FILE_READ, cache->startframe);
2807
2808                         if(pf) {
2809                                 while(ptcache_file_read(pf, old_data, 1, elemsize))
2810                                         cache->totpoint++;
2811                                 
2812                                 ptcache_file_close(pf);
2813                         }
2814                 }
2815         }
2816
2817         cache->flag &= ~(PTCACHE_OUTDATED|PTCACHE_FRAMES_SKIPPED);
2818
2819         BKE_ptcache_update_info(pid);
2820 }
2821
2822 void BKE_ptcache_update_info(PTCacheID *pid)
2823 {
2824         PointCache *cache = pid->cache;
2825         int totframes = 0;
2826         char mem_info[64];
2827
2828         if(cache->flag & PTCACHE_EXTERNAL) {
2829                 int cfra = cache->startframe;
2830
2831                 for(; cfra<=cache->endframe; cfra++) {
2832                         if(BKE_ptcache_id_exist(pid, cfra))
2833                                 totframes++;
2834                 }
2835
2836                 if(totframes && cache->totpoint)
2837                         sprintf(cache->info, "%i points found!", cache->totpoint);
2838                 else
2839                         sprintf(cache->info, "No valid data to read!");
2840                 return;
2841         }
2842
2843         if(cache->flag & PTCACHE_DISK_CACHE) {
2844                 int cfra = cache->startframe;
2845
2846                 for(; cfra<=cache->endframe; cfra++) {
2847                         if(BKE_ptcache_id_exist(pid, cfra))
2848                                 totframes++;
2849                 }
2850
2851                 sprintf(mem_info, "%i frames on disk", totframes);
2852         }
2853         else {
2854                 PTCacheMem *pm = cache->mem_cache.first;                
2855                 float bytes = 0.0f;
2856                 int i, mb;
2857                 
2858                 for(; pm; pm=pm->next) {
2859                         for(i=0; i<BPHYS_TOT_DATA; i++)
2860                                 bytes += pm->data[i] ? MEM_allocN_len(pm->data[i]) : 0.0f;
2861                         totframes++;
2862                 }
2863
2864                 mb = (bytes > 1024.0f * 1024.0f);
2865
2866                 sprintf(mem_info, "%i frames in memory (%.1f %s)",
2867                         totframes,
2868                         bytes / (mb ? 1024.0f * 1024.0f : 1024.0f),
2869                         mb ? "Mb" : "kb");
2870         }
2871
2872         if(cache->flag & PTCACHE_OUTDATED) {
2873                 sprintf(cache->info, "%s, cache is outdated!", mem_info);
2874         }
2875         else if(cache->flag & PTCACHE_FRAMES_SKIPPED) {
2876                 sprintf(cache->info, "%s, not exact since frame %i.", mem_info, cache->last_exact);
2877         }
2878         else
2879                 sprintf(cache->info, "%s.", mem_info);
2880 }
2881
2882 void BKE_ptcache_validate(PointCache *cache, int framenr)
2883 {
2884         cache->flag |= PTCACHE_SIMULATION_VALID;
2885         cache->simframe = framenr;
2886 }
2887 void BKE_ptcache_invalidate(PointCache *cache)
2888 {
2889         cache->flag &= ~PTCACHE_SIMULATION_VALID;
2890         cache->simframe = 0;
2891         cache->last_exact = MIN2(cache->startframe, 0);
2892 }
2893