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