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