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