Blender V4.5
pointcache.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13#include <sys/stat.h>
14#include <sys/types.h>
15
16/* needed for directory lookup */
17#ifndef WIN32
18# include <dirent.h>
19#else
20# include "BLI_winstuff.h"
21#endif
22
23#include "CLG_log.h"
24
25#include "MEM_guardedalloc.h"
26
27#include "DNA_ID.h"
29#include "DNA_fluid_types.h"
30#include "DNA_modifier_types.h"
32#include "DNA_object_types.h"
33#include "DNA_particle_types.h"
34#include "DNA_rigidbody_types.h"
35#include "DNA_scene_types.h"
36#include "DNA_space_types.h"
37
38#include "BLI_fileops.h"
39#include "BLI_listbase.h"
40#include "BLI_math_rotation.h"
41#include "BLI_math_vector.h"
42#include "BLI_path_utils.hh"
43#include "BLI_string.h"
44#include "BLI_time.h"
45#include "BLI_utildefines.h"
46
47#include "BLT_translation.hh"
48
49#include "BKE_appdir.hh"
50#include "BKE_cloth.hh"
51#include "BKE_collection.hh"
52#include "BKE_duplilist.hh"
53#include "BKE_dynamicpaint.h"
54#include "BKE_fluid.h"
55#include "BKE_global.hh"
56#include "BKE_lib_id.hh"
57#include "BKE_library.hh"
58#include "BKE_main.hh"
59#include "BKE_modifier.hh"
60#include "BKE_object.hh"
61#include "BKE_particle.h"
62#include "BKE_pointcache.h"
63#include "BKE_scene.hh"
64#include "BKE_softbody.h"
65
66#include "BLO_read_write.hh"
67
69
70#include "BIK_api.h"
71
72#ifdef WITH_BULLET
73# include "RBI_api.h"
74#endif
75
76#ifdef WITH_LZO
77# ifdef WITH_SYSTEM_LZO
78# include <lzo/lzo1x.h>
79# else
80# include "minilzo.h"
81# endif
82# define LZO_HEAP_ALLOC(var, size) \
83 lzo_align_t __LZO_MMODEL var[((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t)]
84#endif
85
86#define LZO_OUT_LEN(size) ((size) + (size) / 16 + 64 + 3)
87
88#ifdef WITH_LZMA
89# include "LzmaLib.h"
90#endif
91
92#define PTCACHE_DATA_FROM(data, type, from) \
93 if (data[type]) { \
94 memcpy(data[type], from, ptcache_data_size[type]); \
95 } \
96 (void)0
97
98#define PTCACHE_DATA_TO(data, type, index, to) \
99 if (data[type]) { \
100 memcpy(to, \
101 (char *)(data)[type] + ((index) ? (index) * ptcache_data_size[type] : 0), \
102 ptcache_data_size[type]); \
103 } \
104 (void)0
105
106/* could be made into a pointcache option */
107#define DURIAN_POINTCACHE_LIB_OK 1
108
109static CLG_LogRef LOG = {"bke.pointcache"};
110
111static int ptcache_data_size[] = {
112 sizeof(uint), /* BPHYS_DATA_INDEX */
113 sizeof(float[3]), /* BPHYS_DATA_LOCATION */
114 sizeof(float[3]), /* BPHYS_DATA_VELOCITY */
115 sizeof(float[4]), /* BPHYS_DATA_ROTATION */
116 sizeof(float[3]), /* BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
117 sizeof(float), /* BPHYS_DATA_SIZE */
118 sizeof(float[3]), /* BPHYS_DATA_TIMES */
119 sizeof(BoidData), /* case BPHYS_DATA_BOIDS */
120};
121
123 0,
124 sizeof(ParticleSpring),
125 sizeof(float[3]),
126};
127
128/* forward declarations */
129[[nodiscard]] static bool ptcache_file_compressed_read(PTCacheFile *pf,
130 uchar *result,
131 size_t items_num,
132 size_t item_size);
133[[nodiscard]] static bool ptcache_file_compressed_write(
134 PTCacheFile *pf, uchar *in, uint in_len, uchar *out, int mode);
135[[nodiscard]] static bool ptcache_file_write(PTCacheFile *pf,
136 const void *data,
137 size_t items_num,
138 size_t item_size);
139[[nodiscard]] static bool ptcache_file_read(PTCacheFile *pf,
140 void *f,
141 size_t items_num,
142 size_t item_size);
143
144/* Common functions */
146{
147 int error = 0;
148
149 /* Custom functions should read these basic elements too! */
150 if (!error && !fread(&pf->totpoint, sizeof(uint), 1, pf->fp)) {
151 error = 1;
152 }
153
154 if (!error && !fread(&pf->data_types, sizeof(uint), 1, pf->fp)) {
155 error = 1;
156 }
157
158 return !error;
159}
161{
162 /* Custom functions should write these basic elements too! */
163 if (!fwrite(&pf->totpoint, sizeof(uint), 1, pf->fp)) {
164 return 0;
165 }
166
167 if (!fwrite(&pf->data_types, sizeof(uint), 1, pf->fp)) {
168 return 0;
169 }
170
171 return 1;
172}
173static void ptcache_add_extra_data(PTCacheMem *pm, uint type, uint count, void *data)
174{
175 PTCacheExtra *extra = MEM_callocN<PTCacheExtra>("Point cache: extra data descriptor");
176
177 extra->type = type;
178 extra->totdata = count;
179
180 size_t size = extra->totdata * ptcache_extra_datasize[extra->type];
181
182 extra->data = MEM_mallocN(size, "Point cache: extra data");
183 memcpy(extra->data, data, size);
184
185 BLI_addtail(&pm->extradata, extra);
186}
187/* Softbody functions */
188static int ptcache_softbody_write(int index, void *soft_v, void **data, int /*cfra*/)
189{
190 SoftBody *soft = static_cast<SoftBody *>(soft_v);
191 BodyPoint *bp = soft->bpoint + index;
192
195
196 return 1;
197}
199 int index, void *soft_v, void **data, float /*cfra*/, const float *old_data)
200{
201 SoftBody *soft = static_cast<SoftBody *>(soft_v);
202 BodyPoint *bp = soft->bpoint + index;
203
204 if (old_data) {
205 memcpy(bp->pos, data, sizeof(float[3]));
206 memcpy(bp->vec, data + 3, sizeof(float[3]));
207 }
208 else {
211 }
212}
213static void ptcache_softbody_interpolate(int index,
214 void *soft_v,
215 void **data,
216 float cfra,
217 float cfra1,
218 float cfra2,
219 const float *old_data)
220{
221 SoftBody *soft = static_cast<SoftBody *>(soft_v);
222 BodyPoint *bp = soft->bpoint + index;
223 ParticleKey keys[4];
224 float dfra;
225
226 if (cfra1 == cfra2) {
227 return;
228 }
229
230 copy_v3_v3(keys[1].co, bp->pos);
231 copy_v3_v3(keys[1].vel, bp->vec);
232
233 if (old_data) {
234 memcpy(keys[2].co, old_data, sizeof(float[3]));
235 memcpy(keys[2].vel, old_data + 3, sizeof(float[3]));
236 }
237 else {
238 BKE_ptcache_make_particle_key(keys + 2, 0, data, cfra2);
239 }
240
241 dfra = cfra2 - cfra1;
242
243 mul_v3_fl(keys[1].vel, dfra);
244 mul_v3_fl(keys[2].vel, dfra);
245
246 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, true);
247
248 mul_v3_fl(keys->vel, 1.0f / dfra);
249
250 copy_v3_v3(bp->pos, keys->co);
251 copy_v3_v3(bp->vec, keys->vel);
252}
253static int ptcache_softbody_totpoint(void *soft_v, int /*cfra*/)
254{
255 SoftBody *soft = static_cast<SoftBody *>(soft_v);
256 return soft->totpoint;
257}
258static void ptcache_softbody_error(const ID * /*owner_id*/,
259 void * /*soft_v*/,
260 const char * /*message*/)
261{
262 /* ignored for now */
263}
264
265/* Particle functions. */
266
267void BKE_ptcache_make_particle_key(ParticleKey *key, int index, void **data, float time)
268{
271
272 /* no rotation info, so make something nice up */
273 if (data[BPHYS_DATA_ROTATION] == nullptr) {
274 vec_to_quat(key->rot, key->vel, OB_NEGX, OB_POSZ);
275 }
276 else {
278 }
279
281 key->time = time;
282}
283static int ptcache_particle_write(int index, void *psys_v, void **data, int cfra)
284{
285 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
286 ParticleData *pa = psys->particles + index;
287 BoidParticle *boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : nullptr;
288 float times[3];
289 int step = psys->pointcache->step;
290
291 /* Skip some particles that are not stored in the cache. */
292 if (data[BPHYS_DATA_INDEX]) {
293 if (psys->part->flag & PART_DIED) {
294 /* Dead particles are stored when they are displayed. */
295 if (cfra < pa->time - step) {
296 return 0;
297 }
298 }
299 else {
300 /* Inclusive ranges for particle lifetime (`dietime - 1` for an inclusive end-frame). */
301 const int pa_sfra = int(pa->time) - step;
302 const int pa_efra = (int(pa->dietime) - 1) + step;
303 if (!(cfra >= pa_sfra && cfra <= pa_efra)) {
304 return 0;
305 }
306 }
307 }
308
309 times[0] = pa->time;
310 times[1] = pa->dietime;
311 times[2] = pa->lifetime;
312
320
321 if (boid) {
323 }
324
325 /* Return flag 1+1=2 for newly born particles
326 * to copy exact birth location to previously cached frame. */
327 return 1 + (pa->state.time >= pa->time && pa->prev_state.time <= pa->time);
328}
330 int index, void *psys_v, void **data, float cfra, const float *old_data)
331{
332 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
333 ParticleData *pa;
334 BoidParticle *boid;
335 float timestep = 0.04f * psys->part->timetweak;
336
337 if (index < 0 || index >= psys->totpart) {
338 return;
339 }
340
341 pa = psys->particles + index;
342 boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : nullptr;
343
344 if (cfra > pa->state.time) {
345 memcpy(&pa->prev_state, &pa->state, sizeof(ParticleKey));
346 }
347
348 if (old_data) {
349 /* old format cache */
350 pa->state = *reinterpret_cast<const ParticleKey *>(old_data);
351 return;
352 }
353
355
356 /* set frames cached before birth to birth time */
357 if (cfra < pa->time) {
358 pa->state.time = pa->time;
359 }
360 else if (cfra > pa->dietime) {
361 pa->state.time = pa->dietime;
362 }
363
364 if (data[BPHYS_DATA_SIZE]) {
366 }
367
368 if (data[BPHYS_DATA_TIMES]) {
369 float times[3];
371 pa->time = times[0];
372 pa->dietime = times[1];
373 pa->lifetime = times[2];
374 }
375
376 if (boid) {
378 }
379
380 /* determine velocity from previous location */
382 if (cfra > pa->prev_state.time) {
383 sub_v3_v3v3(pa->state.vel, pa->state.co, pa->prev_state.co);
384 mul_v3_fl(pa->state.vel, (cfra - pa->prev_state.time) * timestep);
385 }
386 else {
387 sub_v3_v3v3(pa->state.vel, pa->prev_state.co, pa->state.co);
388 mul_v3_fl(pa->state.vel, (pa->prev_state.time - cfra) * timestep);
389 }
390 }
391
392 /* default to no rotation */
394 unit_qt(pa->state.rot);
395 }
396}
397static void ptcache_particle_interpolate(int index,
398 void *psys_v,
399 void **data,
400 float cfra,
401 float cfra1,
402 float cfra2,
403 const float *old_data)
404{
405 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
406 ParticleData *pa;
407 ParticleKey keys[4];
408 float dfra, timestep = 0.04f * psys->part->timetweak;
409
410 if (index < 0 || index >= psys->totpart) {
411 return;
412 }
413
414 pa = psys->particles + index;
415
416 /* Inclusive ranges for particle lifetime (`dietime - 1` for an inclusive end-frame). */
417 const int pa_sfra = int(pa->time) - psys->pointcache->step;
418 const int pa_efra = (int(pa->dietime) - 1) + psys->pointcache->step;
419
420 /* Particle wasn't read from first cache so can't interpolate. */
421 if (!(cfra1 >= pa_sfra && cfra1 <= pa_efra)) {
422 return;
423 }
424
425 cfra = std::min(cfra, pa->dietime);
426 cfra1 = std::min(cfra1, pa->dietime);
427 cfra2 = std::min(cfra2, pa->dietime);
428
429 if (cfra1 == cfra2) {
430 return;
431 }
432
433 keys[1] = pa->state;
434 if (old_data) {
435 keys[2] = *reinterpret_cast<const ParticleKey *>(old_data);
436 }
437 else {
438 BKE_ptcache_make_particle_key(keys + 2, 0, data, cfra2);
439 }
440
441 /* determine velocity from previous location */
443 if (keys[1].time > keys[2].time) {
444 sub_v3_v3v3(keys[2].vel, keys[1].co, keys[2].co);
445 mul_v3_fl(keys[2].vel, (keys[1].time - keys[2].time) * timestep);
446 }
447 else {
448 sub_v3_v3v3(keys[2].vel, keys[2].co, keys[1].co);
449 mul_v3_fl(keys[2].vel, (keys[2].time - keys[1].time) * timestep);
450 }
451 }
452
453 /* default to no rotation */
455 unit_qt(keys[2].rot);
456 }
457
458 if (cfra > pa->time) {
459 cfra1 = std::max(cfra1, pa->time);
460 }
461
462 dfra = cfra2 - cfra1;
463
464 mul_v3_fl(keys[1].vel, dfra * timestep);
465 mul_v3_fl(keys[2].vel, dfra * timestep);
466
467 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, &pa->state, true);
468 interp_qt_qtqt(pa->state.rot, keys[1].rot, keys[2].rot, (cfra - cfra1) / dfra);
469
470 mul_v3_fl(pa->state.vel, 1.0f / (dfra * timestep));
471
472 pa->state.time = cfra;
473}
474
475static int ptcache_particle_totpoint(void *psys_v, int /*cfra*/)
476{
477 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
478 return psys->totpart;
479}
480
481static void ptcache_particle_error(const ID * /*owner_id*/,
482 void * /*psys_v*/,
483 const char * /*message*/)
484{
485 /* ignored for now */
486}
487
488static int ptcache_particle_totwrite(void *psys_v, int cfra)
489{
490 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
491 ParticleData *pa = psys->particles;
492 int p, step = psys->pointcache->step;
493 int totwrite = 0;
494
495 if (cfra == 0) {
496 return psys->totpart;
497 }
498
499 if (psys->part->flag & PART_DIED) {
500 /* Also store dead particles when they are displayed. */
501 for (p = 0; p < psys->totpart; p++, pa++) {
502 const int pa_sfra = int(pa->time) - step;
503 totwrite += (cfra >= pa_sfra);
504 }
505 }
506 else {
507 for (p = 0; p < psys->totpart; p++, pa++) {
508 /* Inclusive ranges for particle lifetime (`dietime - 1` for an inclusive end-frame). */
509 const int pa_sfra = int(pa->time) - step;
510 const int pa_efra = (int(pa->dietime) - 1) + step;
511 totwrite += (cfra >= pa_sfra) && (cfra <= pa_efra);
512 }
513 }
514
515 return totwrite;
516}
517
518static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int /*cfra*/)
519{
520 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
521
522 if (psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid &&
524 psys->fluid_springs)
525 {
528 }
529}
530
531static void ptcache_particle_extra_read(void *psys_v, PTCacheMem *pm, float /*cfra*/)
532{
533 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
534 PTCacheExtra *extra = static_cast<PTCacheExtra *>(pm->extradata.first);
535
536 for (; extra; extra = extra->next) {
537 switch (extra->type) {
539 if (psys->fluid_springs) {
541 }
542
543 psys->fluid_springs = static_cast<ParticleSpring *>(MEM_dupallocN(extra->data));
544 psys->alloc_fluidsprings = extra->totdata;
545
546 /* Drop springs with invalid particle indices from corrupt files. */
547 const uint totpart = uint(std::max(psys->totpart, 0));
548 int springs_num = 0;
549 for (uint i = 0; i < extra->totdata; i++) {
550 const ParticleSpring &spring = psys->fluid_springs[i];
551 if (spring.particle_index[0] < totpart && spring.particle_index[1] < totpart) {
552 psys->fluid_springs[springs_num++] = spring;
553 }
554 }
555 psys->tot_fluidsprings = springs_num;
556 break;
557 }
558 }
559 }
560}
561
562/* Cloth functions */
563static int ptcache_cloth_write(int index, void *cloth_v, void **data, int /*cfra*/)
564{
565 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
566 Cloth *cloth = clmd->clothObject;
567 ClothVertex *vert = cloth->verts + index;
568
572
573 return 1;
574}
576 int index, void *cloth_v, void **data, float /*cfra*/, const float *old_data)
577{
578 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
579 Cloth *cloth = clmd->clothObject;
580 ClothVertex *vert = cloth->verts + index;
581
582 if (old_data) {
583 memcpy(vert->x, data, sizeof(float[3]));
584 memcpy(vert->xconst, data + 3, sizeof(float[3]));
585 memcpy(vert->v, data + 6, sizeof(float[3]));
586 }
587 else {
591 }
592}
593static void ptcache_cloth_interpolate(int index,
594 void *cloth_v,
595 void **data,
596 float cfra,
597 float cfra1,
598 float cfra2,
599 const float *old_data)
600{
601 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
602 Cloth *cloth = clmd->clothObject;
603 ClothVertex *vert = cloth->verts + index;
604 ParticleKey keys[4];
605 float dfra;
606
607 if (cfra1 == cfra2) {
608 return;
609 }
610
611 copy_v3_v3(keys[1].co, vert->x);
612 copy_v3_v3(keys[1].vel, vert->v);
613
614 if (old_data) {
615 memcpy(keys[2].co, old_data, sizeof(float[3]));
616 memcpy(keys[2].vel, old_data + 6, sizeof(float[3]));
617 }
618 else {
619 BKE_ptcache_make_particle_key(keys + 2, 0, data, cfra2);
620 }
621
622 dfra = cfra2 - cfra1;
623
624 mul_v3_fl(keys[1].vel, dfra);
625 mul_v3_fl(keys[2].vel, dfra);
626
627 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, true);
628
629 mul_v3_fl(keys->vel, 1.0f / dfra);
630
631 copy_v3_v3(vert->x, keys->co);
632 copy_v3_v3(vert->v, keys->vel);
633
634 /* should vert->xconst be interpolated somehow too? - jahka */
635}
636
637static void ptcache_cloth_extra_write(void *cloth_v, PTCacheMem *pm, int /*cfra*/)
638{
639 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
640 Cloth *cloth = clmd->clothObject;
641
642 if (!is_zero_v3(cloth->average_acceleration)) {
644 }
645}
646static void ptcache_cloth_extra_read(void *cloth_v, PTCacheMem *pm, float /*cfra*/)
647{
648 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
649 Cloth *cloth = clmd->clothObject;
650 PTCacheExtra *extra = static_cast<PTCacheExtra *>(pm->extradata.first);
651
653
654 for (; extra; extra = extra->next) {
655 switch (extra->type) {
657 if (extra->totdata < 1) {
658 break; /* Guard against corrupt file. */
659 }
660 copy_v3_v3(cloth->average_acceleration, static_cast<const float *>(extra->data));
661 break;
662 }
663 }
664 }
665}
666
667static int ptcache_cloth_totpoint(void *cloth_v, int /*cfra*/)
668{
669 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
670 return clmd->clothObject ? clmd->clothObject->mvert_num : 0;
671}
672
673static void ptcache_cloth_error(const ID *owner_id, void *cloth_v, const char *message)
674{
675 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
676 BLI_assert(GS(owner_id->name) == ID_OB);
677 if (clmd->hairdata == nullptr) {
678 /* If there is hair data, this modifier does not actually exist on the object. */
679 BKE_modifier_set_error((Object *)owner_id, &clmd->modifier, "%s", message);
680 }
681}
682
683static int ptcache_dynamicpaint_totpoint(void *sd, int /*cfra*/)
684{
686
687 if (!surface->data) {
688 return 0;
689 }
690
691 return surface->data->total_points;
692}
693
694static void ptcache_dynamicpaint_error(const ID * /*owner_id*/,
695 void * /*sd*/,
696 const char * /*message*/)
697{
698 /* ignored for now */
699}
700
701#define DPAINT_CACHE_VERSION "1.01"
702
704{
705 DynamicPaintSurface *surface = (DynamicPaintSurface *)dp_v;
706 int cache_compress = PTCACHE_COMPRESS_LZO;
707 bool ok = true;
708
709 /* version header */
710 ok &= ptcache_file_write(pf, DPAINT_CACHE_VERSION, 1, sizeof(char[4]));
711
712 if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
713 int total_points = surface->data->total_points;
714 uint in_len;
715 uchar *out;
716
717 /* cache type */
718 const int surface_type = int(surface->type);
719 ok &= ptcache_file_write(pf, &surface_type, 1, sizeof(int));
720
721 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
722 in_len = sizeof(PaintPoint) * total_points;
723 }
725 in_len = sizeof(float) * total_points;
726 }
727 else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
728 in_len = sizeof(PaintWavePoint) * total_points;
729 }
730 else {
731 return 0;
732 }
733
734 out = (uchar *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
735
737 pf, (uchar *)surface->data->type_data, in_len, out, cache_compress);
738 MEM_freeN(out);
739 }
740 return ok;
741}
743{
744 DynamicPaintSurface *surface = (DynamicPaintSurface *)dp_v;
745 char version[4];
746
747 /* version header */
748 if (!ptcache_file_read(pf, version, 1, sizeof(char[4]))) {
749 return 0;
750 }
751 if (!STREQLEN(version, DPAINT_CACHE_VERSION, 4)) {
752 CLOG_ERROR(&LOG, "Dynamic Paint: Invalid cache version: '%c%c%c%c'!", UNPACK4(version));
753 return 0;
754 }
755
756 if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
757 uint data_len;
758 int surface_type;
759
760 /* cache type */
761 if (!ptcache_file_read(pf, &surface_type, 1, sizeof(int))) {
762 return 0;
763 }
764
765 if (surface_type != surface->type) {
766 return 0;
767 }
768
769 /* read surface data */
770 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
771 data_len = sizeof(PaintPoint);
772 }
774 data_len = sizeof(float);
775 }
776 else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
777 data_len = sizeof(PaintWavePoint);
778 }
779 else {
780 return 0;
781 }
782
784 pf, (uchar *)surface->data->type_data, surface->data->total_points, data_len))
785 {
786 CLOG_ERROR(&LOG, "Dynamic Paint: Unable to read the compressed cache data");
787 return 0;
788 }
789 }
790 return 1;
791}
792
793/* Rigid Body functions */
794static int ptcache_rigidbody_write(int index, void *rb_v, void **data, int /*cfra*/)
795{
796 RigidBodyWorld *rbw = static_cast<RigidBodyWorld *>(rb_v);
797 Object *ob = nullptr;
798
799 if (rbw->objects) {
800 ob = rbw->objects[index];
801 }
802
803 if (ob && ob->rigidbody_object) {
804 RigidBodyOb *rbo = ob->rigidbody_object;
805
806 if (rbo->type == RBO_TYPE_ACTIVE && rbo->shared->physics_object != nullptr) {
807#ifdef WITH_BULLET
808 RB_body_get_position(static_cast<rbRigidBody *>(rbo->shared->physics_object), rbo->pos);
809 RB_body_get_orientation(static_cast<rbRigidBody *>(rbo->shared->physics_object), rbo->orn);
810#endif
813 }
814 }
815
816 return 1;
817}
819 int index, void *rb_v, void **data, float /*cfra*/, const float *old_data)
820{
821 RigidBodyWorld *rbw = static_cast<RigidBodyWorld *>(rb_v);
822 Object *ob = nullptr;
823
824 if (rbw->objects) {
825 ob = rbw->objects[index];
826 }
827
828 if (ob && ob->rigidbody_object) {
829 RigidBodyOb *rbo = ob->rigidbody_object;
830
831 if (rbo->type == RBO_TYPE_ACTIVE) {
832
833 if (old_data) {
834 memcpy(rbo->pos, data, sizeof(float[3]));
835 memcpy(rbo->orn, data + 3, sizeof(float[4]));
836 }
837 else {
840 }
841 }
842 }
843}
844static void ptcache_rigidbody_interpolate(int index,
845 void *rb_v,
846 void **data,
847 float cfra,
848 float cfra1,
849 float cfra2,
850 const float *old_data)
851{
852 RigidBodyWorld *rbw = static_cast<RigidBodyWorld *>(rb_v);
853 Object *ob = nullptr;
854
855 if (rbw->objects) {
856 ob = rbw->objects[index];
857 }
858
859 if (ob && ob->rigidbody_object) {
860 RigidBodyOb *rbo = ob->rigidbody_object;
861
862 if (rbo->type == RBO_TYPE_ACTIVE) {
863 /* It may be possible to improve results by taking into account velocity
864 * for interpolation using psys_interpolate_particle, however this is
865 * not currently cached. */
866 float pos[3], orn[4];
867
868 if (old_data) {
869 memcpy(pos, data, sizeof(float[3]));
870 memcpy(orn, data + 3, sizeof(float[4]));
871 }
872 else {
875 }
876
877 const float t = (cfra - cfra1) / (cfra2 - cfra1);
878 interp_v3_v3v3(rbo->pos, rbo->pos, pos, t);
879 interp_qt_qtqt(rbo->orn, rbo->orn, orn, t);
880 }
881 }
882}
883static int ptcache_rigidbody_totpoint(void *rb_v, int /*cfra*/)
884{
885 RigidBodyWorld *rbw = static_cast<RigidBodyWorld *>(rb_v);
886
887 return rbw->numbodies;
888}
889
890static void ptcache_rigidbody_error(const ID * /*owner_id*/,
891 void * /*rb_v*/,
892 const char * /*message*/)
893{
894 /* ignored for now */
895}
896
897/* Creating ID's */
898
900{
901 memset(pid, 0, sizeof(PTCacheID));
902
903 pid->owner_id = &ob->id;
904 pid->calldata = sb;
906 pid->cache = sb->shared->pointcache;
907 pid->cache_ptr = &sb->shared->pointcache;
908 pid->ptcaches = &sb->shared->ptcaches;
911
915
916 pid->write_stream = nullptr;
917 pid->read_stream = nullptr;
918
919 pid->write_extra_data = nullptr;
920 pid->read_extra_data = nullptr;
921 pid->interpolate_extra_data = nullptr;
922
925
927 pid->info_types = 0;
928
929 pid->stack_index = pid->cache->index;
930
931 pid->default_step = 1;
932 pid->max_step = 20;
934}
936{
937 memset(pid, 0, sizeof(PTCacheID));
938
939 pid->owner_id = &ob->id;
940 pid->calldata = psys;
942 pid->stack_index = psys->pointcache->index;
943 pid->cache = psys->pointcache;
944 pid->cache_ptr = &psys->pointcache;
945 pid->ptcaches = &psys->ptcaches;
946
947 if (psys->part->type != PART_HAIR) {
949 }
950
954
958
959 pid->write_stream = nullptr;
960 pid->read_stream = nullptr;
961
962 pid->write_extra_data = nullptr;
963 pid->read_extra_data = nullptr;
964 pid->interpolate_extra_data = nullptr;
965
968
969 pid->data_types = (1 << BPHYS_DATA_LOCATION) | (1 << BPHYS_DATA_VELOCITY) |
970 (1 << BPHYS_DATA_INDEX);
971
972 if (psys->part->phystype == PART_PHYS_BOIDS) {
973 pid->data_types |= (1 << BPHYS_DATA_AVELOCITY) | (1 << BPHYS_DATA_ROTATION) |
974 (1 << BPHYS_DATA_BOIDS);
975 }
976 else if (psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid &&
978 {
981 }
982
983 if (psys->part->flag & PART_ROTATIONS) {
984 pid->data_types |= (1 << BPHYS_DATA_ROTATION);
985
986 if (psys->part->rotmode != PART_ROT_VEL || psys->part->avemode == PART_AVE_RAND ||
987 psys->part->avefac != 0.0f)
988 {
989 pid->data_types |= (1 << BPHYS_DATA_AVELOCITY);
990 }
991 }
992
993 pid->info_types = (1 << BPHYS_DATA_TIMES);
994
995 pid->default_step = 1;
996 pid->max_step = 20;
998}
1000{
1001 memset(pid, 0, sizeof(PTCacheID));
1002
1003 pid->owner_id = &ob->id;
1004 pid->calldata = clmd;
1005 pid->type = PTCACHE_TYPE_CLOTH;
1006 pid->stack_index = clmd->point_cache->index;
1007 pid->cache = clmd->point_cache;
1008 pid->cache_ptr = &clmd->point_cache;
1009 pid->ptcaches = &clmd->ptcaches;
1012
1016
1017 pid->write_stream = nullptr;
1018 pid->read_stream = nullptr;
1019
1022 pid->interpolate_extra_data = nullptr;
1023
1026
1027 pid->data_types = (1 << BPHYS_DATA_LOCATION) | (1 << BPHYS_DATA_VELOCITY) |
1028 (1 << BPHYS_DATA_XCONST);
1029 pid->info_types = 0;
1030
1031 pid->default_step = 1;
1032 pid->max_step = 1;
1034}
1035
1037{
1038 FluidDomainSettings *fds = fmd->domain;
1039
1040 memset(pid, 0, sizeof(PTCacheID));
1041
1042 pid->owner_id = &ob->id;
1043 pid->calldata = fmd;
1044
1046 pid->stack_index = fds->point_cache[0]->index;
1047
1048 pid->cache = fds->point_cache[0];
1049 pid->cache_ptr = &(fds->point_cache[0]);
1050 pid->ptcaches = &(fds->ptcaches[0]);
1051}
1052
1054{
1055
1056 memset(pid, 0, sizeof(PTCacheID));
1057
1058 pid->owner_id = &ob->id;
1059 pid->calldata = surface;
1061 pid->cache = surface->pointcache;
1062 pid->cache_ptr = &surface->pointcache;
1063 pid->ptcaches = &surface->ptcaches;
1066
1067 pid->write_point = nullptr;
1068 pid->read_point = nullptr;
1069 pid->interpolate_point = nullptr;
1070
1073
1074 pid->write_extra_data = nullptr;
1075 pid->read_extra_data = nullptr;
1076 pid->interpolate_extra_data = nullptr;
1077
1080
1082 pid->info_types = 0;
1083
1084 pid->stack_index = pid->cache->index;
1085
1086 pid->default_step = 1;
1087 pid->max_step = 1;
1089}
1090
1092{
1093
1094 memset(pid, 0, sizeof(PTCacheID));
1095
1096 pid->owner_id = ob != nullptr ? &ob->id : nullptr;
1097 pid->calldata = rbw;
1099 pid->cache = rbw->shared->pointcache;
1100 pid->cache_ptr = &rbw->shared->pointcache;
1101 pid->ptcaches = &rbw->shared->ptcaches;
1104
1108
1109 pid->write_stream = nullptr;
1110 pid->read_stream = nullptr;
1111
1112 pid->write_extra_data = nullptr;
1113 pid->read_extra_data = nullptr;
1114 pid->interpolate_extra_data = nullptr;
1115
1118
1119 pid->data_types = (1 << BPHYS_DATA_LOCATION) | (1 << BPHYS_DATA_ROTATION);
1120 pid->info_types = 0;
1121
1122 pid->stack_index = pid->cache->index;
1123
1124 pid->default_step = 1;
1125 pid->max_step = 1;
1127}
1128
1130{
1131 PTCacheID result = {nullptr};
1132
1133 ListBase pidlist;
1134 BKE_ptcache_ids_from_object(&pidlist, ob, scene, MAX_DUPLI_RECUR);
1135
1136 LISTBASE_FOREACH (PTCacheID *, pid, &pidlist) {
1137 if (pid->cache == cache) {
1138 result = *pid;
1139 break;
1140 }
1141 }
1142
1143 BLI_freelistN(&pidlist);
1144
1145 return result;
1146}
1147
1148/* Callback which is used by point cache foreach() family of functions.
1149 *
1150 * Receives ID of the point cache.
1151 *
1152 * NOTE: This ID is owned by foreach() routines and can not be used outside of
1153 * the foreach loop. This means that if one wants to store them those are to be
1154 * malloced and copied over.
1155 *
1156 * If the function returns false, then foreach() loop aborts.
1157 */
1158
1160{
1161 PTCacheID pid;
1162 for (ParticleSystem *psys = static_cast<ParticleSystem *>(object->particlesystem.first);
1163 psys != nullptr;
1164 psys = psys->next)
1165 {
1166 if (psys->part == nullptr) {
1167 continue;
1168 }
1169 /* Check to make sure point cache is actually used by the particles. */
1170 if (ELEM(psys->part->phystype, PART_PHYS_NO, PART_PHYS_KEYED)) {
1171 continue;
1172 }
1173/* Hair needs to be included in id-list for cache edit mode to work. */
1174#if 0
1175 if ((psys->part->type == PART_HAIR) && (psys->flag & PSYS_HAIR_DYNAMICS) == 0) {
1176 continue;
1177 }
1178#endif
1179 if (psys->part->type == PART_FLUID) {
1180 continue;
1181 }
1182 BKE_ptcache_id_from_particles(&pid, object, psys);
1183 /* TODO figure out the particle modifier for this system and put it here. */
1184 if (!callback(pid, nullptr)) {
1185 return false;
1186 }
1187 }
1188 return true;
1189}
1190
1192{
1193 PTCacheID pid;
1194 for (ModifierData *md = static_cast<ModifierData *>(object->modifiers.first); md != nullptr;
1195 md = md->next)
1196 {
1197 if (md->type == eModifierType_Cloth) {
1198 BKE_ptcache_id_from_cloth(&pid, object, (ClothModifierData *)md);
1199 if (!callback(pid, md)) {
1200 return false;
1201 }
1202 }
1203 else if (md->type == eModifierType_Fluid) {
1205 if (fmd->type & MOD_FLUID_TYPE_DOMAIN) {
1206 BKE_ptcache_id_from_smoke(&pid, object, (FluidModifierData *)md);
1207 if (!callback(pid, md)) {
1208 return false;
1209 }
1210 }
1211 }
1212 else if (md->type == eModifierType_DynamicPaint) {
1214 if (pmd->canvas) {
1215 DynamicPaintSurface *surface = static_cast<DynamicPaintSurface *>(
1216 pmd->canvas->surfaces.first);
1217 for (; surface; surface = surface->next) {
1218 BKE_ptcache_id_from_dynamicpaint(&pid, object, surface);
1219 if (!callback(pid, md)) {
1220 return false;
1221 }
1222 }
1223 }
1224 }
1225 }
1226 return true;
1227}
1228
1229/* Return false if any of callbacks returned false. */
1231 Object *object,
1232 int duplis,
1233 PointCacheIdFn callback)
1234{
1235 PTCacheID pid;
1236
1237 if (object != nullptr) {
1238 /* Soft body. */
1239 if (object->soft != nullptr) {
1240 BKE_ptcache_id_from_softbody(&pid, object, object->soft);
1241 if (!callback(pid, nullptr)) {
1242 return false;
1243 }
1244 }
1245 /* Particle systems. */
1246 if (!foreach_object_particle_ptcache(object, callback)) {
1247 return false;
1248 }
1249 /* Modifiers. */
1250 if (!foreach_object_modifier_ptcache(object, callback)) {
1251 return false;
1252 }
1253 /* Consider all object in dupli-groups to be part of the same object,
1254 * for baking with linking dupli-groups. Once we have better overrides
1255 * this can be revisited so users select the local objects directly. */
1256 if (scene != nullptr && (duplis-- > 0) && (object->instance_collection != nullptr)) {
1258 if (current_object == object) {
1259 continue;
1260 }
1261 foreach_object_ptcache(scene, current_object, duplis, callback);
1262 }
1264 }
1265 }
1266
1267 /* Rigid body. */
1268 if (scene != nullptr && (object == nullptr || object->rigidbody_object != nullptr) &&
1269 scene->rigidbody_world != nullptr)
1270 {
1271 BKE_ptcache_id_from_rigidbody(&pid, object, scene->rigidbody_world);
1272 if (!callback(pid, nullptr)) {
1273 return false;
1274 }
1275 }
1276 return true;
1277}
1278
1279void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int duplis)
1280{
1281 lb->first = lb->last = nullptr;
1282 foreach_object_ptcache(scene, ob, duplis, [&](PTCacheID &pid, ModifierData * /*md*/) -> bool {
1283 PTCacheID *own_pid = MEM_mallocN<PTCacheID>("PTCacheID");
1284 *own_pid = pid;
1285 BLI_addtail(lb, own_pid);
1286 return true;
1287 });
1288}
1289
1291{
1292 foreach_object_ptcache(&scene, &ob, duplis, fn);
1293}
1294
1295bool BKE_ptcache_object_has(Scene *scene, Object *ob, int duplis)
1296{
1297 bool has_point_cache = false;
1298 foreach_object_ptcache(scene, ob, duplis, [&](PTCacheID & /*pid*/, ModifierData * /*md*/) {
1299 has_point_cache = true;
1300 return false;
1301 });
1302 return has_point_cache;
1303}
1304
1305/* File handling */
1306
1307static const char *ptcache_file_extension(const PTCacheID *pid)
1308{
1309 switch (pid->file_type) {
1310 default:
1312 return PTCACHE_EXT;
1313 }
1314}
1315
1319static int ptcache_frame_from_filename(const char *filename, const char *ext)
1320{
1321 const int frame_len = 6;
1322 const int ext_len = frame_len + strlen(ext);
1323 const int len = strlen(filename);
1324
1325 /* could crash if trying to copy a string out of this range */
1326 if (len > ext_len) {
1327 /* Using frame_len here gives compile error (VLA). */
1328 char num[/*frame_len*/ 6 + 1];
1329 STRNCPY(num, filename + len - ext_len);
1330
1331 return atoi(num);
1332 }
1333
1334 return -1;
1335}
1336
1337/* Takes an Object ID and returns a unique name
1338 * - id: object id
1339 * - cfra: frame for the cache, can be negative
1340 * - stack_index: index in the modifier stack. we can have cache for more than one stack_index
1341 */
1342
1343#define MAX_PTCACHE_PATH FILE_MAX
1344#define MAX_PTCACHE_FILE (FILE_MAX * 2)
1345
1347{
1348 const char *blendfile_path = BKE_main_blendfile_path_from_global();
1349 Library *lib = (pid->owner_id) ? pid->owner_id->lib : nullptr;
1350 const char *blendfile_path_lib = (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH) == 0) ?
1351 lib->runtime->filepath_abs :
1352 blendfile_path;
1353
1354 if (pid->cache->flag & PTCACHE_EXTERNAL) {
1356
1357 if (BLI_path_is_rel(dirname)) {
1358 BLI_path_abs(dirname, blendfile_path_lib);
1359 }
1360
1361 return BLI_path_slash_ensure(dirname, MAX_PTCACHE_PATH); /* new strlen() */
1362 }
1363 if ((blendfile_path[0] != '\0') || lib) {
1364 char file[MAX_PTCACHE_PATH]; /* we don't want the dir, only the file */
1365
1366 BLI_path_split_file_part(blendfile_path_lib, file, sizeof(file));
1367 /* Remove the `.blend` extension. */
1369
1370 /* Add blend file name to pointcache dir. */
1372
1373 BLI_path_abs(dirname, blendfile_path_lib);
1374 return BLI_path_slash_ensure(dirname, MAX_PTCACHE_PATH); /* new strlen() */
1375 }
1376
1377 /* use the temp path. this is weak but better than not using point cache at all */
1378 /* temporary directory is assumed to exist and ALWAYS has a trailing slash */
1380
1381 return BLI_path_slash_ensure(dirname, MAX_PTCACHE_PATH); /* new strlen() */
1382}
1383
1385 char filepath[MAX_PTCACHE_FILE],
1386 const size_t filepath_len,
1387 const bool use_frame_number,
1388 const int cfra)
1389{
1390 size_t len = filepath_len;
1391 char *filename_ext;
1392 filename_ext = filepath + filepath_len;
1393 *filename_ext = '\0';
1394
1395 /* PointCaches are inserted in object's list on demand, we need a valid index now. */
1396 if (pid->cache->index < 0) {
1397 BLI_assert(GS(pid->owner_id->name) == ID_OB);
1399 }
1400
1401 const char *ext = ptcache_file_extension(pid);
1402 if (use_frame_number) {
1403 if (pid->cache->flag & PTCACHE_EXTERNAL) {
1404 if (pid->cache->index >= 0) {
1406 filename_ext, MAX_PTCACHE_FILE - len, "_%06d_%02u%s", cfra, pid->stack_index, ext);
1407 }
1408 else {
1409 len += BLI_snprintf_rlen(filename_ext, MAX_PTCACHE_FILE - len, "_%06d%s", cfra, ext);
1410 }
1411 }
1412 else {
1414 filename_ext, MAX_PTCACHE_FILE - len, "_%06d_%02u%s", cfra, pid->stack_index, ext);
1415 }
1416 }
1417 else {
1418 if (pid->cache->flag & PTCACHE_EXTERNAL) {
1419 if (pid->cache->index >= 0) {
1421 filename_ext, MAX_PTCACHE_FILE - len, "_%02u%s", pid->stack_index, ext);
1422 }
1423 else {
1424 len += BLI_snprintf_rlen(filename_ext, MAX_PTCACHE_FILE - len, "%s", ext);
1425 }
1426 }
1427 else {
1429 filename_ext, MAX_PTCACHE_FILE - len, "_%02u%s", pid->stack_index, ext);
1430 }
1431 }
1432
1433 return len;
1434}
1435
1437 char filepath[MAX_PTCACHE_FILE],
1438 int cfra,
1439 const bool do_path,
1440 const bool do_ext)
1441{
1442 int len = 0;
1443 const char *idname;
1444 char *newname;
1445 filepath[0] = '\0';
1446 newname = filepath;
1447
1448 if ((pid->cache->flag & PTCACHE_EXTERNAL) == 0) {
1449 const char *blendfile_path = BKE_main_blendfile_path_from_global();
1450 if (blendfile_path[0] == '\0') {
1451 return 0; /* save blend file before using disk pointcache */
1452 }
1453 }
1454
1455 /* start with temp dir */
1456 if (do_path) {
1457 len = ptcache_path(pid, filepath);
1458 newname += len;
1459 }
1460 if (pid->cache->name[0] == '\0' && (pid->cache->flag & PTCACHE_EXTERNAL) == 0) {
1461 idname = (pid->owner_id->name + 2);
1462 /* convert chars to hex so they are always a valid filename */
1463 while ('\0' != *idname) {
1464 /* Always 2 unless there isn't enough room in the string. */
1465 const int temp = BLI_snprintf_rlen(newname, MAX_PTCACHE_FILE - len, "%02X", uint(*idname++));
1466 newname += temp;
1467 len += temp;
1468 }
1469 }
1470 else {
1471 int temp = BLI_strncpy_rlen(newname, pid->cache->name, MAX_PTCACHE_FILE - len);
1472 newname += temp;
1473 len += temp;
1474 }
1475 UNUSED_VARS(newname);
1476
1477 if (do_ext) {
1478 len += ptcache_filepath_ext_append(pid, filepath, size_t(len), true, cfra);
1479 }
1480
1481 return len; /* make sure the above string is always 16 chars */
1482}
1483
1487static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
1488{
1489 PTCacheFile *pf;
1490 FILE *fp = nullptr;
1491 char filepath[MAX_PTCACHE_FILE];
1492
1493#ifndef DURIAN_POINTCACHE_LIB_OK
1494 /* don't allow writing for linked objects */
1495 if (pid->owner_id->lib && mode == PTCACHE_FILE_WRITE) {
1496 return nullptr;
1497 }
1498#endif
1499 if ((pid->cache->flag & PTCACHE_EXTERNAL) == 0) {
1500 const char *blendfile_path = BKE_main_blendfile_path_from_global();
1501 if (blendfile_path[0] == '\0') {
1502 return nullptr; /* save blend file before using disk pointcache */
1503 }
1504 }
1505
1506 ptcache_filepath(pid, filepath, cfra, true, true);
1507
1508 if (mode == PTCACHE_FILE_READ) {
1509 fp = BLI_fopen(filepath, "rb");
1510 }
1511 else if (mode == PTCACHE_FILE_WRITE) {
1513 fp = BLI_fopen(filepath, "wb");
1514 }
1515 else if (mode == PTCACHE_FILE_UPDATE) {
1517 fp = BLI_fopen(filepath, "rb+");
1518 }
1519
1520 if (!fp) {
1521 return nullptr;
1522 }
1523
1524 pf = MEM_mallocN<PTCacheFile>("PTCacheFile");
1525 pf->fp = fp;
1526 pf->old_format = 0;
1527 pf->frame = cfra;
1528
1529 return pf;
1530}
1532{
1533 if (pf) {
1534 fclose(pf->fp);
1535 MEM_freeN(pf);
1536 }
1537}
1538
1540 uchar *result,
1541 const size_t items_num,
1542 const size_t item_size)
1543{
1544 const size_t out_len = items_num * item_size;
1545
1546 uchar compressed = 0;
1547 if (!ptcache_file_read(pf, &compressed, 1, sizeof(uchar))) {
1548 return false;
1549 }
1550
1551 if (compressed == 0) {
1552 return ptcache_file_read(pf, result, out_len, sizeof(uchar));
1553 }
1554
1555 uint in_len;
1556 if (!ptcache_file_read(pf, &in_len, 1, sizeof(uint))) {
1557 return false;
1558 }
1559 if (in_len == 0) {
1560 return out_len == 0;
1561 }
1562 uchar *in = MEM_malloc_arrayN<uchar>(in_len, "pointcache_compressed_buffer");
1563 if (in == nullptr) {
1564 return false;
1565 }
1566 if (!ptcache_file_read(pf, in, in_len, sizeof(uchar))) {
1567 MEM_freeN(in);
1568 return false;
1569 }
1570
1571 /* Require the decompressed size to match, so no part of the result is left unwritten. */
1572 bool decompressed = false;
1573#ifdef WITH_LZO
1574 if (compressed == 1) {
1575 lzo_uint lzo_out_len = out_len;
1576 decompressed = lzo1x_decompress_safe(in, lzo_uint(in_len), result, &lzo_out_len, nullptr) ==
1577 LZO_E_OK &&
1578 lzo_out_len == out_len;
1579 }
1580#endif
1581#ifdef WITH_LZMA
1582 if (compressed == 2) {
1583 uint props_size;
1584 uchar props[16];
1585 if (ptcache_file_read(pf, &props_size, 1, sizeof(uint)) && props_size <= sizeof(props) &&
1586 ptcache_file_read(pf, props, props_size, sizeof(uchar)))
1587 {
1588 size_t leni = in_len, leno = out_len;
1589 decompressed = LzmaUncompress(result, &leno, in, &leni, props, props_size) == SZ_OK &&
1590 leno == out_len;
1591 }
1592 }
1593#endif
1594 MEM_freeN(in);
1595
1596 return decompressed;
1597}
1599 PTCacheFile *pf, uchar *in, uint in_len, uchar *out, int mode)
1600{
1601 int r = 0;
1602 uchar compressed = 0;
1603 size_t out_len = 0;
1604 uchar *props = MEM_calloc_arrayN<uchar>(16, "tmp");
1605 size_t sizeOfIt = 5;
1606
1607 (void)mode; /* unused when building w/o compression */
1608
1609#ifdef WITH_LZO
1610 out_len = LZO_OUT_LEN(in_len);
1611 if (mode == PTCACHE_COMPRESS_LZO) {
1612 LZO_HEAP_ALLOC(wrkmem, LZO1X_MEM_COMPRESS);
1613
1614 r = lzo1x_1_compress(in, (lzo_uint)in_len, out, (lzo_uint *)&out_len, wrkmem);
1615 if (!(r == LZO_E_OK) || (out_len >= in_len)) {
1616 compressed = 0;
1617 }
1618 else {
1619 compressed = 1;
1620 }
1621 }
1622#endif
1623#ifdef WITH_LZMA
1624 if (mode == PTCACHE_COMPRESS_LZMA) {
1625
1626 r = LzmaCompress(out,
1627 &out_len,
1628 in,
1629 in_len, /* Assume `sizeof(char) == 1`. */
1630 props,
1631 &sizeOfIt,
1632 5,
1633 1 << 24,
1634 3,
1635 0,
1636 2,
1637 32,
1638 2);
1639
1640 if (!(r == SZ_OK) || (out_len >= in_len)) {
1641 compressed = 0;
1642 }
1643 else {
1644 compressed = 2;
1645 }
1646 }
1647#endif
1648
1649 bool ok = true;
1650 ok &= ptcache_file_write(pf, &compressed, 1, sizeof(uchar));
1651 if (compressed) {
1652 uint size = out_len;
1653 ok &= ptcache_file_write(pf, &size, 1, sizeof(uint));
1654 ok &= ptcache_file_write(pf, out, out_len, sizeof(uchar));
1655 }
1656 else {
1657 ok &= ptcache_file_write(pf, in, in_len, sizeof(uchar));
1658 }
1659
1660 if (compressed == 2) {
1661 uint size = sizeOfIt;
1662 ok &= ptcache_file_write(pf, &sizeOfIt, 1, sizeof(uint));
1663 ok &= ptcache_file_write(pf, props, size, sizeof(uchar));
1664 }
1665
1666 MEM_freeN(props);
1667
1668 return ok;
1669}
1670static bool ptcache_file_read(PTCacheFile *pf, void *f, size_t items_num, size_t item_size)
1671{
1672 return (fread(f, item_size, items_num, pf->fp) == items_num);
1673}
1675 const void *data,
1676 size_t items_num,
1677 size_t item_size)
1678{
1679 return (fwrite(data, item_size, items_num, pf->fp) == items_num);
1680}
1682{
1683 int i;
1684
1685 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1686 if ((pf->data_types & (1 << i)) &&
1688 {
1689 return 0;
1690 }
1691 }
1692
1693 return 1;
1694}
1696{
1697 uint typeflag = 0;
1698 int error = 0;
1699 char bphysics[8];
1700
1701 pf->data_types = 0;
1702
1703 if (fread(bphysics, sizeof(char), 8, pf->fp) != 8) {
1704 error = 1;
1705 }
1706
1707 if (!error && !STREQLEN(bphysics, "BPHYSICS", 8)) {
1708 error = 1;
1709 }
1710
1711 if (!error && !fread(&typeflag, sizeof(uint), 1, pf->fp)) {
1712 error = 1;
1713 }
1714
1715 pf->type = (typeflag & PTCACHE_TYPEFLAG_TYPEMASK);
1716 pf->flag = (typeflag & PTCACHE_TYPEFLAG_FLAGMASK);
1717
1718 /* if there was an error set file as it was */
1719 if (error) {
1720 BLI_fseek(pf->fp, 0, SEEK_SET);
1721 }
1722
1723 return !error;
1724}
1726{
1727 const char *bphysics = "BPHYSICS";
1728 uint typeflag = pf->type + pf->flag;
1729
1730 if (fwrite(bphysics, sizeof(char), 8, pf->fp) != 8) {
1731 return 0;
1732 }
1733
1734 if (!fwrite(&typeflag, sizeof(uint), 1, pf->fp)) {
1735 return 0;
1736 }
1737
1738 return 1;
1739}
1740
1741/* Data pointer handling. */
1742
1743int BKE_ptcache_data_size(int data_type)
1744{
1745 return ptcache_data_size[data_type];
1746}
1747
1749{
1750 int data_types = pf->data_types;
1751
1752 pf->cur[BPHYS_DATA_INDEX] = (data_types & (1 << BPHYS_DATA_INDEX)) ? &pf->data.index : nullptr;
1753 pf->cur[BPHYS_DATA_LOCATION] = (data_types & (1 << BPHYS_DATA_LOCATION)) ? &pf->data.loc :
1754 nullptr;
1755 pf->cur[BPHYS_DATA_VELOCITY] = (data_types & (1 << BPHYS_DATA_VELOCITY)) ? &pf->data.vel :
1756 nullptr;
1757 pf->cur[BPHYS_DATA_ROTATION] = (data_types & (1 << BPHYS_DATA_ROTATION)) ? &pf->data.rot :
1758 nullptr;
1759 pf->cur[BPHYS_DATA_AVELOCITY] = (data_types & (1 << BPHYS_DATA_AVELOCITY)) ? &pf->data.ave :
1760 nullptr;
1761 pf->cur[BPHYS_DATA_SIZE] = (data_types & (1 << BPHYS_DATA_SIZE)) ? &pf->data.size : nullptr;
1762 pf->cur[BPHYS_DATA_TIMES] = (data_types & (1 << BPHYS_DATA_TIMES)) ? &pf->data.times : nullptr;
1763 pf->cur[BPHYS_DATA_BOIDS] = (data_types & (1 << BPHYS_DATA_BOIDS)) ? &pf->data.boids : nullptr;
1764}
1765
1767{
1768 if (pm->totpoint > 0 && pm->data[BPHYS_DATA_INDEX]) {
1769 uint *data = static_cast<uint *>(pm->data[BPHYS_DATA_INDEX]);
1770 uint mid, low = 0, high = pm->totpoint - 1;
1771
1772 if (index < *data || index > *(data + high)) {
1773 return -1;
1774 }
1775
1776 /* check simple case for continuous indexes first */
1777 if (index - *data < high && data[index - *data] == index) {
1778 return index - *data;
1779 }
1780
1781 while (low <= high) {
1782 mid = (low + high) / 2;
1783
1784 if (data[mid] > index) {
1785 high = mid - 1;
1786 }
1787 else if (data[mid] < index) {
1788 low = mid + 1;
1789 }
1790 else {
1791 return mid;
1792 }
1793 }
1794
1795 return -1;
1796 }
1797
1798 return (index < pm->totpoint ? index : -1);
1799}
1800
1802{
1803 int data_types = pm->data_types;
1804 int i;
1805
1806 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1807 cur[i] = ((data_types & (1 << i)) ? pm->data[i] : nullptr);
1808 }
1809}
1810
1812{
1813 int i;
1814
1815 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1816 if (cur[i]) {
1817 cur[i] = (char *)cur[i] + ptcache_data_size[i];
1818 }
1819 }
1820}
1821int BKE_ptcache_mem_pointers_seek(int point_index, PTCacheMem *pm, void *cur[BPHYS_TOT_DATA])
1822{
1823 int data_types = pm->data_types;
1824 int i, index = BKE_ptcache_mem_index_find(pm, point_index);
1825
1826 if (index < 0) {
1827 /* Can't give proper location without reallocation, so don't give any location.
1828 * Some points will be cached improperly, but this only happens with simulation
1829 * steps bigger than cache->step, so the cache has to be recalculated anyways
1830 * at some point.
1831 */
1832 return 0;
1833 }
1834
1835 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1836 cur[i] = (data_types & (1 << i)) ? (char *)pm->data[i] + index * ptcache_data_size[i] :
1837 nullptr;
1838 }
1839
1840 return 1;
1841}
1842static bool ptcache_data_alloc(PTCacheMem *pm, const bool zero_init)
1843{
1844 const uint data_types = pm->data_types;
1845
1846 for (int i = 0; i < BPHYS_TOT_DATA; i++) {
1847 if (data_types & (1 << i)) {
1848 pm->data[i] = zero_init ?
1849 MEM_calloc_arrayN(pm->totpoint, ptcache_data_size[i], "PTCache Data") :
1850 MEM_malloc_arrayN(pm->totpoint, ptcache_data_size[i], "PTCache Data");
1851 if (pm->data[i] == nullptr) {
1852 return false;
1853 }
1854 }
1855 }
1856
1857 return true;
1858}
1860{
1861 void **data = pm->data;
1862 int i;
1863
1864 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1865 if (data[i]) {
1866 MEM_freeN(data[i]);
1867 }
1868 }
1869}
1870static void ptcache_data_copy(void *from[], void *to[])
1871{
1872 int i;
1873 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1874 /* NOTE: durian file 03.4b_comp crashes if to[i] is not tested
1875 * its nullptr, not sure if this should be fixed elsewhere but for now its needed */
1876 if (from[i] && to[i]) {
1877 memcpy(to[i], from[i], ptcache_data_size[i]);
1878 }
1879 }
1880}
1881
1883{
1884 PTCacheExtra *extra = static_cast<PTCacheExtra *>(pm->extradata.first);
1885
1886 if (extra) {
1887 for (; extra; extra = extra->next) {
1888 if (extra->data) {
1889 MEM_freeN(extra->data);
1890 }
1891 }
1892
1894 }
1895}
1896
1898{
1901}
1902
1904{
1905 if (pid->type == PTCACHE_TYPE_SOFTBODY) {
1906 return sizeof(float[6]);
1907 }
1908 if (pid->type == PTCACHE_TYPE_PARTICLES) {
1909 return sizeof(ParticleKey);
1910 }
1911 if (pid->type == PTCACHE_TYPE_CLOTH) {
1912 return sizeof(float[9]);
1913 }
1914
1915 return 0;
1916}
1917
1918static void ptcache_find_frames_around(PTCacheID *pid, uint frame, int *r_fra1, int *r_fra2)
1919{
1920 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
1921 int cfra1 = frame, cfra2 = frame + 1;
1922
1923 while (cfra1 >= pid->cache->startframe && !BKE_ptcache_id_exist(pid, cfra1)) {
1924 cfra1--;
1925 }
1926
1927 if (cfra1 < pid->cache->startframe) {
1928 cfra1 = 0;
1929 }
1930
1931 while (cfra2 <= pid->cache->endframe && !BKE_ptcache_id_exist(pid, cfra2)) {
1932 cfra2++;
1933 }
1934
1935 if (cfra2 > pid->cache->endframe) {
1936 cfra2 = 0;
1937 }
1938
1939 if (cfra1 && !cfra2) {
1940 *r_fra1 = 0;
1941 *r_fra2 = cfra1;
1942 }
1943 else {
1944 *r_fra1 = cfra1;
1945 *r_fra2 = cfra2;
1946 }
1947 }
1948 else if (pid->cache->mem_cache.first) {
1949 PTCacheMem *pm = static_cast<PTCacheMem *>(pid->cache->mem_cache.first);
1950 PTCacheMem *pm2 = static_cast<PTCacheMem *>(pid->cache->mem_cache.last);
1951
1952 while (pm->next && pm->next->frame <= frame) {
1953 pm = pm->next;
1954 }
1955
1956 if (pm2->frame < frame) {
1957 pm2 = nullptr;
1958 }
1959 else {
1960 while (pm2->prev && pm2->prev->frame > frame) {
1961 pm2 = pm2->prev;
1962 }
1963 }
1964
1965 if (!pm2) {
1966 *r_fra1 = 0;
1967 *r_fra2 = pm->frame;
1968 }
1969 else {
1970 *r_fra1 = pm->frame;
1971 *r_fra2 = pm2->frame;
1972 }
1973 }
1974}
1975
1976/* Read uncompressed point data, which is stored point by point. */
1978{
1979 void *cur[BPHYS_TOT_DATA];
1981
1982 for (uint i = 0; i < pm->totpoint; i++) {
1983 for (int j = 0; j < BPHYS_TOT_DATA; j++) {
1984 if ((pm->data_types & (1 << j)) && !ptcache_file_read(pf, cur[j], 1, ptcache_data_size[j])) {
1985 return false;
1986 }
1987 }
1989 }
1990
1991 return true;
1992}
1993
1995{
1997 PTCacheMem *pm = nullptr;
1998 uint i, error = 0;
1999
2000 if (pf == nullptr) {
2001 return nullptr;
2002 }
2003
2005 error = 1;
2006 }
2007
2008 if (!error && (pf->type != pid->type || !pid->read_header(pf))) {
2009 error = 1;
2010 }
2011
2012 if (!error) {
2013 pm = MEM_callocN<PTCacheMem>("Pointcache mem");
2014
2015 pm->totpoint = pf->totpoint;
2016 pm->data_types = pf->data_types;
2017 pm->frame = pf->frame;
2018
2019 if (!ptcache_data_alloc(pm, false)) {
2020 error = 1;
2021 }
2022 else if (pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
2023 for (i = 0; !error && i < BPHYS_TOT_DATA; i++) {
2024 if (pf->data_types & (1 << i)) {
2026 pf, (uchar *)(pm->data[i]), pm->totpoint, ptcache_data_size[i]);
2027 }
2028 }
2029 }
2030 else {
2031 error = !ptcache_file_points_read(pf, pm);
2032 }
2033 }
2034
2035 if (!error && pf->flag & PTCACHE_TYPEFLAG_EXTRADATA) {
2036 uint extratype = 0;
2037
2038 while (!error && ptcache_file_read(pf, &extratype, 1, sizeof(uint))) {
2040 error = 1;
2041 break;
2042 }
2043
2044 const size_t item_size = ptcache_extra_datasize[extratype];
2045
2046 uint totdata;
2047 /* Extra data is used with int counts, reject larger counts from corrupt files. */
2048 if (!ptcache_file_read(pf, &totdata, 1, sizeof(uint)) || totdata > INT_MAX) {
2049 error = 1;
2050 break;
2051 }
2052
2053 void *data = MEM_malloc_arrayN(totdata, item_size, "Pointcache extradata->data");
2054 if (data == nullptr) {
2055 error = 1;
2056 break;
2057 }
2058
2059 PTCacheExtra *extra = MEM_callocN<PTCacheExtra>("Pointcache extradata");
2060
2061 extra->type = extratype;
2062 extra->totdata = totdata;
2063 extra->data = data;
2064
2065 if (pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
2067 pf, static_cast<uchar *>(extra->data), extra->totdata, item_size);
2068 }
2069 else {
2070 error = !ptcache_file_read(pf, extra->data, extra->totdata, item_size);
2071 }
2072
2073 BLI_addtail(&pm->extradata, extra);
2074 }
2075 }
2076
2077 if (error && pm) {
2079 MEM_freeN(pm);
2080 pm = nullptr;
2081 }
2082
2084
2085 if (error && G.debug & G_DEBUG) {
2086 printf("Error reading from disk cache\n");
2087 }
2088
2089 return pm;
2090}
2092{
2093 PTCacheFile *pf = nullptr;
2094 bool ok = true;
2095 uint i;
2096
2098
2100
2101 if (pf == nullptr) {
2102 if (G.debug & G_DEBUG) {
2103 printf("Error opening disk cache file for writing\n");
2104 }
2105 return 0;
2106 }
2107
2108 pf->data_types = pm->data_types;
2109 pf->totpoint = pm->totpoint;
2110 pf->type = pid->type;
2111 pf->flag = 0;
2112
2113 if (pm->extradata.first) {
2115 }
2116
2117 if (pid->cache->compression) {
2119 }
2120
2122 ok = false;
2123 }
2124
2125 if (ok) {
2126 if (pid->cache->compression) {
2127 for (i = 0; i < BPHYS_TOT_DATA; i++) {
2128 if (pm->data[i]) {
2129 uint in_len = pm->totpoint * ptcache_data_size[i];
2130 uchar *out = (uchar *)MEM_callocN(LZO_OUT_LEN(in_len) * 4, "pointcache_lzo_buffer");
2132 pf, (uchar *)(pm->data[i]), in_len, out, pid->cache->compression);
2133 MEM_freeN(out);
2134 }
2135 }
2136 }
2137 else {
2138 void *cur[BPHYS_TOT_DATA];
2141
2142 for (i = 0; i < pm->totpoint; i++) {
2143 ptcache_data_copy(cur, pf->cur);
2145 ok = false;
2146 break;
2147 }
2149 }
2150 }
2151 }
2152
2153 if (ok && pm->extradata.first) {
2154 PTCacheExtra *extra = static_cast<PTCacheExtra *>(pm->extradata.first);
2155
2156 for (; extra; extra = extra->next) {
2157 if (extra->data == nullptr || extra->totdata == 0) {
2158 continue;
2159 }
2160
2161 ok &= ptcache_file_write(pf, &extra->type, 1, sizeof(uint));
2162 ok &= ptcache_file_write(pf, &extra->totdata, 1, sizeof(uint));
2163
2164 if (pid->cache->compression) {
2165 uint in_len = extra->totdata * ptcache_extra_datasize[extra->type];
2166 uchar *out = (uchar *)MEM_callocN(LZO_OUT_LEN(in_len) * 4, "pointcache_lzo_buffer");
2168 pf, (uchar *)(extra->data), in_len, out, pid->cache->compression);
2169 MEM_freeN(out);
2170 }
2171 else {
2172 ok &= ptcache_file_write(
2173 pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
2174 }
2175 }
2176 }
2177
2179
2180 if (!ok && G.debug & G_DEBUG) {
2181 CLOG_ERROR(&LOG, "Error writing to disk cache");
2182 }
2183
2184 return ok;
2185}
2186
2187static int ptcache_read_stream(PTCacheID *pid, int cfra)
2188{
2190 int error = 0;
2191
2192 if (pid->read_stream == nullptr) {
2193 return 0;
2194 }
2195
2196 if (pf == nullptr) {
2197 if (G.debug & G_DEBUG) {
2198 printf("Error opening disk cache file for reading\n");
2199 }
2200 return 0;
2201 }
2202
2204 pid->error(pid->owner_id, pid->calldata, "Failed to read point cache file");
2205 error = 1;
2206 }
2207 else if (pf->type != pid->type) {
2208 pid->error(pid->owner_id, pid->calldata, "Point cache file has wrong type");
2209 error = 1;
2210 }
2211 else if (!pid->read_header(pf)) {
2212 pid->error(pid->owner_id, pid->calldata, "Failed to read point cache file header");
2213 error = 1;
2214 }
2215 else if (pf->totpoint != pid->totpoint(pid->calldata, cfra)) {
2216 pid->error(pid->owner_id, pid->calldata, "Number of points in cache does not match mesh");
2217 error = 1;
2218 }
2219
2220 if (!error) {
2222
2223 /* We have stream reading here. */
2224 if (!pid->read_stream(pf, pid->calldata)) {
2225 pid->error(pid->owner_id, pid->calldata, "Failed to read point cache file data");
2226 error = 1;
2227 }
2228 }
2229
2231
2232 return error == 0;
2233}
2234
2235static int ptcache_read(PTCacheID *pid, int cfra)
2236{
2237 PTCacheMem *pm = nullptr;
2238 int i;
2239 int *index = &i;
2240
2241 /* get a memory cache to read from */
2242 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2243 pm = ptcache_disk_frame_to_mem(pid, cfra);
2244 }
2245 else {
2246 pm = static_cast<PTCacheMem *>(pid->cache->mem_cache.first);
2247
2248 while (pm && pm->frame != cfra) {
2249 pm = pm->next;
2250 }
2251 }
2252
2253 /* read the cache */
2254 if (pm) {
2255 int totpoint = pm->totpoint;
2256
2257 if ((pid->data_types & (1 << BPHYS_DATA_INDEX)) == 0) {
2258 int pid_totpoint = pid->totpoint(pid->calldata, cfra);
2259
2260 if (totpoint != pid_totpoint) {
2261 pid->error(pid->owner_id, pid->calldata, "Number of points in cache does not match mesh");
2262 totpoint = std::min(totpoint, pid_totpoint);
2263 }
2264 }
2265
2266 /* Ignore stored indices if this cache type does not use them, to
2267 * guard against unchecked use of them for corrupt files. */
2268 const bool use_index = (pm->data_types & (1 << BPHYS_DATA_INDEX)) &&
2269 (pid->data_types & (1 << BPHYS_DATA_INDEX));
2270
2271 void *cur[BPHYS_TOT_DATA];
2273
2274 for (i = 0; i < totpoint; i++) {
2275 if (use_index) {
2276 index = static_cast<int *>(cur[BPHYS_DATA_INDEX]);
2277 }
2278
2279 pid->read_point(*index, pid->calldata, cur, float(pm->frame), nullptr);
2280
2282 }
2283
2284 if (pid->read_extra_data && pm->extradata.first) {
2285 pid->read_extra_data(pid->calldata, pm, float(pm->frame));
2286 }
2287
2288 /* clean up temporary memory cache */
2289 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2291 MEM_freeN(pm);
2292 }
2293 }
2294
2295 return 1;
2296}
2297static int ptcache_interpolate(PTCacheID *pid, float cfra, int cfra1, int cfra2)
2298{
2299 PTCacheMem *pm = nullptr;
2300 int i;
2301 int *index = &i;
2302
2303 /* get a memory cache to read from */
2304 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2305 pm = ptcache_disk_frame_to_mem(pid, cfra2);
2306 }
2307 else {
2308 pm = static_cast<PTCacheMem *>(pid->cache->mem_cache.first);
2309
2310 while (pm && pm->frame != cfra2) {
2311 pm = pm->next;
2312 }
2313 }
2314
2315 /* read the cache */
2316 if (pm) {
2317 int totpoint = pm->totpoint;
2318
2319 if ((pid->data_types & (1 << BPHYS_DATA_INDEX)) == 0) {
2320 int pid_totpoint = pid->totpoint(pid->calldata, int(cfra));
2321
2322 if (totpoint != pid_totpoint) {
2323 pid->error(pid->owner_id, pid->calldata, "Number of points in cache does not match mesh");
2324 totpoint = std::min(totpoint, pid_totpoint);
2325 }
2326 }
2327
2328 /* Ignore stored indices if this cache type does not use them, to
2329 * guard against unchecked use of them for corrupt files. */
2330 const bool use_index = (pm->data_types & (1 << BPHYS_DATA_INDEX)) &&
2331 (pid->data_types & (1 << BPHYS_DATA_INDEX));
2332
2333 void *cur[BPHYS_TOT_DATA];
2335
2336 for (i = 0; i < totpoint; i++) {
2337 if (use_index) {
2338 index = static_cast<int *>(cur[BPHYS_DATA_INDEX]);
2339 }
2340
2341 pid->interpolate_point(
2342 *index, pid->calldata, cur, cfra, float(cfra1), float(cfra2), nullptr);
2344 }
2345
2346 if (pid->interpolate_extra_data && pm->extradata.first) {
2347 pid->interpolate_extra_data(pid->calldata, pm, cfra, float(cfra1), float(cfra2));
2348 }
2349
2350 /* clean up temporary memory cache */
2351 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2353 MEM_freeN(pm);
2354 }
2355 }
2356
2357 return 1;
2358}
2359
2360int BKE_ptcache_read(PTCacheID *pid, float cfra, bool no_extrapolate_old)
2361{
2362 int cfrai = int(floor(cfra)), cfra1 = 0, cfra2 = 0;
2363 int ret = 0;
2364
2365 /* nothing to read to */
2366 if (pid->totpoint(pid->calldata, cfrai) == 0) {
2367 return 0;
2368 }
2369
2370 if (pid->cache->flag & PTCACHE_READ_INFO) {
2371 pid->cache->flag &= ~PTCACHE_READ_INFO;
2372 ptcache_read(pid, 0);
2373 }
2374
2375 /* first check if we have the actual frame cached */
2376 if (cfra == float(cfrai) && BKE_ptcache_id_exist(pid, cfrai)) {
2377 cfra1 = cfrai;
2378 }
2379
2380 /* no exact cache frame found so try to find cached frames around cfra */
2381 if (cfra1 == 0) {
2382 ptcache_find_frames_around(pid, cfrai, &cfra1, &cfra2);
2383 }
2384
2385 if (cfra1 == 0 && cfra2 == 0) {
2386 return 0;
2387 }
2388
2389 /* don't read old cache if already simulated past cached frame */
2390 if (no_extrapolate_old) {
2391 if (cfra1 == 0 && cfra2 && cfra2 <= pid->cache->simframe) {
2392 return 0;
2393 }
2394 if (cfra1 && cfra1 == cfra2) {
2395 return 0;
2396 }
2397 }
2398 else {
2399 /* avoid calling interpolate between the same frame values */
2400 if (cfra1 && cfra1 == cfra2) {
2401 cfra1 = 0;
2402 }
2403 }
2404
2405 if (cfra1) {
2406 if (pid->read_stream) {
2407 if (!ptcache_read_stream(pid, cfra1)) {
2408 return 0;
2409 }
2410 }
2411 else if (pid->read_point) {
2412 ptcache_read(pid, cfra1);
2413 }
2414 }
2415
2416 if (cfra2) {
2417 if (pid->read_stream) {
2418 if (!ptcache_read_stream(pid, cfra2)) {
2419 return 0;
2420 }
2421 }
2422 else if (pid->read_point) {
2423 if (cfra1 && cfra2 && pid->interpolate_point) {
2424 ptcache_interpolate(pid, cfra, cfra1, cfra2);
2425 }
2426 else {
2427 ptcache_read(pid, cfra2);
2428 }
2429 }
2430 }
2431
2432 if (cfra1) {
2434 }
2435 else if (cfra2) {
2437 pid->cache->simframe = cfra2;
2438 }
2439
2440 cfrai = int(cfra);
2441 /* clear invalid cache frames so that better stuff can be simulated */
2442 if (pid->cache->flag & PTCACHE_OUTDATED) {
2444 }
2445 else if (pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
2446 if (cfra <= pid->cache->last_exact) {
2448 }
2449
2450 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, std::max(cfrai, pid->cache->last_exact));
2451 }
2452
2453 return ret;
2454}
2455static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
2456{
2457 PTCacheFile *pf = nullptr;
2458 int error = 0;
2459
2461
2463
2464 if (pf == nullptr) {
2465 if (G.debug & G_DEBUG) {
2466 printf("Error opening disk cache file for writing\n");
2467 }
2468 return 0;
2469 }
2470
2471 pf->data_types = pid->data_types;
2472 pf->totpoint = totpoint;
2473 pf->type = pid->type;
2474 pf->flag = 0;
2475
2476 if (!error && (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf))) {
2477 error = 1;
2478 }
2479
2480 if (!error && pid->write_stream) {
2481 pid->write_stream(pf, pid->calldata);
2482 }
2483
2485
2486 if (error && G.debug & G_DEBUG) {
2487 printf("Error writing to disk cache\n");
2488 }
2489
2490 return error == 0;
2491}
2492
2493static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
2494{
2495 PointCache *cache = pid->cache;
2496 PTCacheMem *pm = nullptr, *pm2 = nullptr;
2497 int totpoint = pid->totpoint(pid->calldata, cfra);
2498 int i, error = 0;
2499
2500 pm = MEM_callocN<PTCacheMem>("Pointcache mem");
2501
2502 pm->totpoint = pid->totwrite(pid->calldata, cfra);
2503 pm->data_types = cfra ? pid->data_types : pid->info_types;
2504
2505 if (!ptcache_data_alloc(pm, true)) {
2507 MEM_freeN(pm);
2508 return 1;
2509 }
2510 void *cur[BPHYS_TOT_DATA];
2512
2513 if (overwrite) {
2514 if (cache->flag & PTCACHE_DISK_CACHE) {
2515 int fra = cfra - 1;
2516
2517 while (fra >= cache->startframe && !BKE_ptcache_id_exist(pid, fra)) {
2518 fra--;
2519 }
2520
2521 pm2 = ptcache_disk_frame_to_mem(pid, fra);
2522 }
2523 else {
2524 pm2 = static_cast<PTCacheMem *>(cache->mem_cache.last);
2525 }
2526 }
2527
2528 if (pid->write_point) {
2529 for (i = 0; i < totpoint; i++) {
2530 int write = pid->write_point(i, pid->calldata, cur, cfra);
2531 if (write) {
2533
2534 void *cur2[BPHYS_TOT_DATA];
2535 /* newly born particles have to be copied to previous cached frame */
2536 if (overwrite && write == 2 && pm2 && BKE_ptcache_mem_pointers_seek(i, pm2, cur2)) {
2537 pid->write_point(i, pid->calldata, cur2, cfra);
2538 }
2539 }
2540 }
2541 }
2542
2543 if (pid->write_extra_data) {
2544 pid->write_extra_data(pid->calldata, pm, cfra);
2545 }
2546
2547 pm->frame = cfra;
2548
2549 if (cache->flag & PTCACHE_DISK_CACHE) {
2550 error += !ptcache_mem_frame_to_disk(pid, pm);
2551
2552 // if (pm) /* pm is always set */
2553 {
2555 MEM_freeN(pm);
2556 }
2557
2558 if (pm2) {
2559 error += !ptcache_mem_frame_to_disk(pid, pm2);
2560 ptcache_mem_clear(pm2);
2561 MEM_freeN(pm2);
2562 }
2563 }
2564 else {
2565 BLI_addtail(&cache->mem_cache, pm);
2566 }
2567
2568 return error;
2569}
2570static int ptcache_write_needed(PTCacheID *pid, int cfra, int *overwrite)
2571{
2572 PointCache *cache = pid->cache;
2573 int ofra = 0, efra = cache->endframe;
2574
2575 /* always start from scratch on the first frame */
2576 if (cfra && cfra == cache->startframe) {
2578 cache->flag &= ~PTCACHE_REDO_NEEDED;
2579 return 1;
2580 }
2581
2582 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2583 if (cfra == 0 && cache->startframe > 0) {
2584 return 1;
2585 }
2586
2587 /* find last cached frame */
2588 while (efra > cache->startframe && !BKE_ptcache_id_exist(pid, efra)) {
2589 efra--;
2590 }
2591
2592 /* find second last cached frame */
2593 ofra = efra - 1;
2594 while (ofra > cache->startframe && !BKE_ptcache_id_exist(pid, ofra)) {
2595 ofra--;
2596 }
2597 }
2598 else {
2599 PTCacheMem *pm = static_cast<PTCacheMem *>(cache->mem_cache.last);
2600 /* don't write info file in memory */
2601 if (cfra == 0) {
2602 return 0;
2603 }
2604
2605 if (pm == nullptr) {
2606 return 1;
2607 }
2608
2609 efra = pm->frame;
2610 ofra = (pm->prev ? pm->prev->frame : efra - cache->step);
2611 }
2612
2613 if (efra >= cache->startframe && cfra > efra) {
2614 if (ofra >= cache->startframe && efra - ofra < cache->step) {
2615 /* overwrite previous frame */
2617 *overwrite = 1;
2618 }
2619 return 1;
2620 }
2621
2622 return 0;
2623}
2625{
2626 PointCache *cache = pid->cache;
2627 if (!pid->totpoint) {
2628 /* This happens when `pid->type == PTCACHE_TYPE_SMOKE_DOMAIN`. The fluid system does not
2629 * actually use the pointcache anymore for caching. */
2630 return 0;
2631 }
2632
2633 int totpoint = pid->totpoint(pid->calldata, cfra);
2634 int overwrite = 0, error = 0;
2635
2636 if (totpoint == 0 || (cfra ? pid->data_types == 0 : pid->info_types == 0)) {
2637 return 0;
2638 }
2639
2640 if (ptcache_write_needed(pid, cfra, &overwrite) == 0) {
2641 return 0;
2642 }
2643
2644 if (pid->write_stream) {
2645 ptcache_write_stream(pid, cfra, totpoint);
2646 }
2647 else if (pid->write_point) {
2648 error += ptcache_write(pid, cfra, overwrite);
2649 }
2650
2651 /* Mark frames skipped if more than 1 frame forwards since last non-skipped frame. */
2652 if (cfra - cache->last_exact == 1 || cfra == cache->startframe) {
2653 cache->last_exact = cfra;
2654 cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
2655 }
2656 /* Don't mark skipped when writing info file (frame 0) */
2657 else if (cfra) {
2658 cache->flag |= PTCACHE_FRAMES_SKIPPED;
2659 }
2660
2661 /* Update timeline cache display */
2662 if (cfra && cache->cached_frames) {
2663 cache->cached_frames[cfra - cache->startframe] = 1;
2664 }
2665
2666 cache->flag |= PTCACHE_FLAG_INFO_DIRTY;
2667
2668 return !error;
2669}
2670/* you'll need to close yourself after!
2671 * mode - PTCACHE_CLEAR_ALL,
2672 */
2673
2674/* Clears & resets. */
2675
2676void BKE_ptcache_id_clear(PTCacheID *pid, int mode, uint cfra)
2677{
2678 uint len; /* store the length of the string */
2679 uint sta, end;
2680
2681 /* mode is same as fopen's modes */
2682 DIR *dir;
2683 dirent *de;
2684 char path[MAX_PTCACHE_PATH];
2685 char filepath[MAX_PTCACHE_FILE];
2686 char path_full[MAX_PTCACHE_FILE];
2687 char ext[MAX_PTCACHE_FILE];
2688
2689 if (!pid || !pid->cache || pid->cache->flag & PTCACHE_BAKED) {
2690 return;
2691 }
2692
2693 if (pid->cache->flag & PTCACHE_IGNORE_CLEAR) {
2694 return;
2695 }
2696
2697 sta = pid->cache->startframe;
2698 end = pid->cache->endframe;
2699
2700#ifndef DURIAN_POINTCACHE_LIB_OK
2701 /* don't allow clearing for linked objects */
2702 if (pid->owner_id->lib) {
2703 return;
2704 }
2705#endif
2706
2707 /* Clear all files in the temp dir with the prefix of the ID and the `.bphys` suffix. */
2708 switch (mode) {
2709 case PTCACHE_CLEAR_ALL:
2712 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2713 ptcache_path(pid, path);
2714
2715 dir = opendir(path);
2716 if (dir == nullptr) {
2717 return;
2718 }
2719
2720 len = ptcache_filepath(pid, filepath, cfra, false, false); /* no path */
2721 /* append underscore terminator to ensure we don't match similar names
2722 * from objects whose names start with the same prefix
2723 */
2724 if (len < sizeof(filepath) - 2) {
2725 BLI_strncpy(filepath + len, "_", sizeof(filepath) - 2 - len);
2726 len += 1;
2727 }
2728
2729 ptcache_filepath_ext_append(pid, ext, 0, false, 0);
2730
2731 while ((de = readdir(dir)) != nullptr) {
2732 if (strstr(de->d_name, ext)) { /* Do we have the right extension? */
2733 if (STREQLEN(filepath, de->d_name, len)) { /* Do we have the right prefix. */
2734 if (mode == PTCACHE_CLEAR_ALL) {
2735 pid->cache->last_exact = std::min(pid->cache->startframe, 0);
2736 BLI_path_join(path_full, sizeof(path_full), path, de->d_name);
2737 BLI_delete(path_full, false, false);
2738 }
2739 else {
2740 /* read the number of the file */
2741 const int frame = ptcache_frame_from_filename(de->d_name, ext);
2742
2743 if (frame != -1) {
2744 if ((mode == PTCACHE_CLEAR_BEFORE && frame < cfra) ||
2745 (mode == PTCACHE_CLEAR_AFTER && frame > cfra))
2746 {
2747 BLI_path_join(path_full, sizeof(path_full), path, de->d_name);
2748 BLI_delete(path_full, false, false);
2749 if (pid->cache->cached_frames && frame >= sta && frame <= end) {
2750 pid->cache->cached_frames[frame - sta] = 0;
2751 }
2752 }
2753 }
2754 }
2755 }
2756 }
2757 }
2758 closedir(dir);
2759
2760 if (mode == PTCACHE_CLEAR_ALL && pid->cache->cached_frames) {
2761 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
2762 }
2763 }
2764 else {
2765 PTCacheMem *pm = static_cast<PTCacheMem *>(pid->cache->mem_cache.first);
2766 PTCacheMem *link = nullptr;
2767
2768 if (mode == PTCACHE_CLEAR_ALL) {
2769 /* We want startframe if the cache starts before zero. */
2770 pid->cache->last_exact = std::min(pid->cache->startframe, 0);
2771 for (; pm; pm = pm->next) {
2773 }
2775
2776 if (pid->cache->cached_frames) {
2777 memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
2778 }
2779 }
2780 else {
2781 while (pm) {
2782 if ((mode == PTCACHE_CLEAR_BEFORE && pm->frame < cfra) ||
2783 (mode == PTCACHE_CLEAR_AFTER && pm->frame > cfra))
2784 {
2785 link = pm;
2786 if (pid->cache->cached_frames && pm->frame >= sta && pm->frame <= end) {
2787 pid->cache->cached_frames[pm->frame - sta] = 0;
2788 }
2790 pm = pm->next;
2791 BLI_freelinkN(&pid->cache->mem_cache, link);
2792 }
2793 else {
2794 pm = pm->next;
2795 }
2796 }
2797 }
2798 }
2799 break;
2800
2802 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2803 if (BKE_ptcache_id_exist(pid, cfra)) {
2804 ptcache_filepath(pid, filepath, cfra, true, true); /* no path */
2805 BLI_delete(filepath, false, false);
2806 }
2807 }
2808 else {
2809 PTCacheMem *pm = static_cast<PTCacheMem *>(pid->cache->mem_cache.first);
2810
2811 for (; pm; pm = pm->next) {
2812 if (pm->frame == cfra) {
2814 BLI_freelinkN(&pid->cache->mem_cache, pm);
2815 break;
2816 }
2817 }
2818 }
2819 if (pid->cache->cached_frames && cfra >= sta && cfra <= end) {
2820 pid->cache->cached_frames[cfra - sta] = 0;
2821 }
2822 break;
2823 }
2824
2826}
2827
2829{
2830 if (!pid->cache) {
2831 return false;
2832 }
2833
2834 if (cfra < pid->cache->startframe || cfra > pid->cache->endframe) {
2835 return false;
2836 }
2837
2838 if (pid->cache->cached_frames && pid->cache->cached_frames[cfra - pid->cache->startframe] == 0) {
2839 return false;
2840 }
2841
2842 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2843 char filepath[MAX_PTCACHE_FILE];
2844
2845 ptcache_filepath(pid, filepath, cfra, true, true);
2846
2847 return BLI_exists(filepath);
2848 }
2849
2850 PTCacheMem *pm = static_cast<PTCacheMem *>(pid->cache->mem_cache.first);
2851
2852 for (; pm; pm = pm->next) {
2853 if (pm->frame == cfra) {
2854 return true;
2855 }
2856 }
2857 return false;
2858}
2860 PTCacheID *pid, Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
2861{
2862 // Object *ob; /* UNUSED */
2863 PointCache *cache;
2864 /* float offset; unused for now */
2865 float time, nexttime;
2866
2867 /* TODO: this has to be sorted out once bsystem_time gets redone, */
2868 /* now caches can handle interpolating etc. too - jahka */
2869
2870 /* time handling for point cache:
2871 * - simulation time is scaled by result of bsystem_time
2872 * - for offsetting time only time offset is taken into account, since
2873 * that's always the same and can't be animated. a time-offset which
2874 * varies over time is not simple to support.
2875 * - field and motion blur offsets are currently ignored, proper solution
2876 * is probably to interpolate results from two frames for that ..
2877 */
2878
2879 cache = pid->cache;
2880
2881 if (timescale) {
2882 time = BKE_scene_ctime_get(scene);
2883 nexttime = BKE_scene_frame_to_ctime(scene, scene->r.cfra + 1);
2884
2885 *timescale = std::max(nexttime - time, 0.0f);
2886 }
2887
2888 if (startframe && endframe) {
2889 *startframe = cache->startframe;
2890 *endframe = cache->endframe;
2891 }
2892
2893 /* verify cached_frames array is up to date */
2894 if (cache->cached_frames) {
2895 if (cache->cached_frames_len != (cache->endframe - cache->startframe + 1)) {
2896 MEM_freeN(cache->cached_frames);
2897 cache->cached_frames = nullptr;
2898 cache->cached_frames_len = 0;
2899 }
2900 }
2901
2902 if (cache->cached_frames == nullptr && cache->endframe > cache->startframe) {
2903 uint sta = cache->startframe;
2904 uint end = cache->endframe;
2905
2906 cache->cached_frames_len = cache->endframe - cache->startframe + 1;
2908 "cached frames array");
2909
2910 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2911 /* mode is same as fopen's modes */
2912 DIR *dir;
2913 dirent *de;
2914 char path[MAX_PTCACHE_PATH];
2915 char filepath[MAX_PTCACHE_FILE];
2916 char ext[MAX_PTCACHE_FILE];
2917 uint len; /* store the length of the string */
2918
2919 ptcache_path(pid, path);
2920
2921 len = ptcache_filepath(pid, filepath, int(cfra), false, false); /* no path */
2922
2923 dir = opendir(path);
2924 if (dir == nullptr) {
2925 return;
2926 }
2927
2928 ptcache_filepath_ext_append(pid, ext, 0, false, 0);
2929
2930 while ((de = readdir(dir)) != nullptr) {
2931 if (strstr(de->d_name, ext)) { /* Do we have the right extension? */
2932 if (STREQLEN(filepath, de->d_name, len)) { /* Do we have the right prefix. */
2933 /* read the number of the file */
2934 const int frame = ptcache_frame_from_filename(de->d_name, ext);
2935
2936 if ((frame != -1) && (frame >= sta && frame <= end)) {
2937 cache->cached_frames[frame - sta] = 1;
2938 }
2939 }
2940 }
2941 }
2942 closedir(dir);
2943 }
2944 else {
2945 PTCacheMem *pm = static_cast<PTCacheMem *>(pid->cache->mem_cache.first);
2946
2947 while (pm) {
2948 if (pm->frame >= sta && pm->frame <= end) {
2949 cache->cached_frames[pm->frame - sta] = 1;
2950 }
2951 pm = pm->next;
2952 }
2953 }
2954 }
2955}
2956int BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode)
2957{
2958 PointCache *cache;
2959 int reset, clear, after;
2960
2961 if (!pid->cache) {
2962 return 0;
2963 }
2964
2965 cache = pid->cache;
2966 reset = 0;
2967 clear = 0;
2968 after = 0;
2969
2970 if (mode == PTCACHE_RESET_DEPSGRAPH) {
2971 if (!(cache->flag & PTCACHE_BAKED)) {
2972
2973 after = 1;
2974 }
2975
2976 cache->flag |= PTCACHE_OUTDATED;
2977 }
2978 else if (mode == PTCACHE_RESET_BAKED) {
2979 cache->flag |= PTCACHE_OUTDATED;
2980 }
2981 else if (mode == PTCACHE_RESET_OUTDATED) {
2982 reset = 1;
2983
2984 if (cache->flag & PTCACHE_OUTDATED && !(cache->flag & PTCACHE_BAKED)) {
2985 clear = 1;
2986 cache->flag &= ~PTCACHE_OUTDATED;
2987 }
2988 }
2989
2990 if (reset) {
2992 cache->flag &= ~PTCACHE_REDO_NEEDED;
2993
2994 if (pid->type == PTCACHE_TYPE_CLOTH) {
2995 cloth_free_modifier(static_cast<ClothModifierData *>(pid->calldata));
2996 }
2997 else if (pid->type == PTCACHE_TYPE_SOFTBODY) {
2998 sbFreeSimulation(static_cast<SoftBody *>(pid->calldata));
2999 }
3000 else if (pid->type == PTCACHE_TYPE_PARTICLES) {
3002 }
3003 else if (pid->type == PTCACHE_TYPE_DYNAMICPAINT) {
3005 }
3006 }
3007 if (clear) {
3009 }
3010 else if (after) {
3012 }
3013
3014 return (reset || clear || after);
3015}
3016int BKE_ptcache_object_reset(Scene *scene, Object *ob, int mode)
3017{
3018 PTCacheID pid;
3019 int reset, skip;
3020
3021 reset = 0;
3022 skip = 0;
3023
3024 if (ob->soft) {
3025 BKE_ptcache_id_from_softbody(&pid, ob, ob->soft);
3026 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
3027 }
3028
3030 /* children or just redo can be calculated without resetting anything */
3031 if (psys->recalc & ID_RECALC_PSYS_REDO || psys->recalc & ID_RECALC_PSYS_CHILD) {
3032 skip = 1;
3033 /* Baked cloth hair has to be checked too, because we don't want to reset */
3034 /* particles or cloth in that case -jahka */
3035 }
3036 else if (psys->clmd) {
3037 BKE_ptcache_id_from_cloth(&pid, ob, psys->clmd);
3038 if (mode == PSYS_RESET_ALL ||
3039 !(psys->part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED)))
3040 {
3041 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
3042 }
3043 else {
3044 skip = 1;
3045 }
3046 }
3047
3048 if (skip == 0 && psys->part) {
3049 BKE_ptcache_id_from_particles(&pid, ob, psys);
3050 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
3051 }
3052 }
3053
3055 if (md->type == eModifierType_Cloth) {
3057 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
3058 }
3059 if (md->type == eModifierType_DynamicPaint) {
3061 if (pmd->canvas) {
3062 DynamicPaintSurface *surface = static_cast<DynamicPaintSurface *>(
3063 pmd->canvas->surfaces.first);
3064
3065 for (; surface; surface = surface->next) {
3066 BKE_ptcache_id_from_dynamicpaint(&pid, ob, surface);
3067 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
3068 }
3069 }
3070 }
3071 if (md->type == eModifierType_Fluid) {
3073 FluidDomainSettings *fds = fmd->domain;
3074 if ((fmd->type & MOD_FLUID_TYPE_DOMAIN) && fds &&
3076 {
3077 BKE_ptcache_id_from_smoke(&pid, ob, fmd);
3078 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
3079 }
3080 }
3081 }
3082
3083 if (scene->rigidbody_world && (ob->rigidbody_object || ob->rigidbody_constraint)) {
3084 if (ob->rigidbody_object) {
3086 }
3088 /* only flag as outdated, resetting should happen on start frame */
3089 pid.cache->flag |= PTCACHE_OUTDATED;
3090 }
3091
3092 if (ob->type == OB_ARMATURE) {
3093 BIK_clear_cache(ob->pose);
3094 }
3095
3096 return reset;
3097}
3098
3099/* Point Cache handling */
3100
3102{
3103 PointCache *cache;
3104
3105 cache = MEM_callocN<PointCache>("PointCache");
3106 cache->startframe = 1;
3107 cache->endframe = 250;
3108 cache->step = 1;
3109 cache->index = -1;
3110
3111 BLI_addtail(ptcaches, cache);
3112
3113 return cache;
3114}
3115
3117{
3118 PTCacheMem *pm = static_cast<PTCacheMem *>(mem_cache->first);
3119
3120 if (pm) {
3121 for (; pm; pm = pm->next) {
3123 }
3124
3125 BLI_freelistN(mem_cache);
3126 }
3127}
3129{
3131 if (cache->edit && cache->free_edit) {
3132 cache->free_edit(cache->edit);
3133 }
3134 if (cache->cached_frames) {
3135 MEM_freeN(cache->cached_frames);
3136 }
3137 MEM_freeN(cache);
3138}
3140{
3141 while (PointCache *cache = static_cast<PointCache *>(BLI_pophead(ptcaches))) {
3142 BKE_ptcache_free(cache);
3143 }
3144}
3145
3146static PointCache *ptcache_copy(PointCache *cache, const bool copy_data)
3147{
3148 PointCache *ncache;
3149
3150 ncache = static_cast<PointCache *>(MEM_dupallocN(cache));
3151
3152 BLI_listbase_clear(&ncache->mem_cache);
3153
3154 if (copy_data == false) {
3155 ncache->cached_frames = nullptr;
3156 ncache->cached_frames_len = 0;
3157
3158 /* flag is a mix of user settings and simulator/baking state */
3160 ncache->simframe = 0;
3161 }
3162 else {
3163 LISTBASE_FOREACH (PTCacheMem *, pm, &cache->mem_cache) {
3164 PTCacheMem *pmn = static_cast<PTCacheMem *>(MEM_dupallocN(pm));
3165 int i;
3166
3167 for (i = 0; i < BPHYS_TOT_DATA; i++) {
3168 if (pmn->data[i]) {
3169 pmn->data[i] = MEM_dupallocN(pm->data[i]);
3170 }
3171 }
3172
3173 BLI_addtail(&ncache->mem_cache, pmn);
3174 }
3175
3176 if (ncache->cached_frames) {
3177 ncache->cached_frames = static_cast<char *>(MEM_dupallocN(cache->cached_frames));
3178 }
3179 }
3180
3181 /* hmm, should these be copied over instead? */
3182 ncache->edit = nullptr;
3183
3184 return ncache;
3185}
3186
3188 const ListBase *ptcaches_old,
3189 const int flag)
3190{
3191 PointCache *cache = static_cast<PointCache *>(ptcaches_old->first);
3192
3193 BLI_listbase_clear(ptcaches_new);
3194
3195 for (; cache; cache = cache->next) {
3196 BLI_addtail(ptcaches_new, ptcache_copy(cache, (flag & LIB_ID_COPY_CACHES) != 0));
3197 }
3198
3199 return static_cast<PointCache *>(ptcaches_new->first);
3200}
3201
3202/* Disabled this code; this is being called on scene_update_tagged, and that in turn gets called on
3203 * every user action changing stuff, and then it runs a complete bake??? (ton) */
3204
3205/* Baking */
3206
3207void BKE_ptcache_quick_cache_all(Main *bmain, Scene *scene, ViewLayer *view_layer)
3208{
3209 PTCacheBaker baker;
3210
3211 memset(&baker, 0, sizeof(baker));
3212 baker.bmain = bmain;
3213 baker.scene = scene;
3214 baker.view_layer = view_layer;
3215 baker.bake = false;
3216 baker.render = false;
3217 baker.anim_init = false;
3219
3220 BKE_ptcache_bake(&baker);
3221}
3222
3223static void ptcache_dt_to_str(char *str, size_t str_maxncpy, double dtime)
3224{
3225 if (dtime > 60.0) {
3226 if (dtime > 3600.0) {
3228 str_maxncpy,
3229 "%ih %im %is",
3230 int(dtime / 3600),
3231 int(dtime / 60) % 60,
3232 int(dtime) % 60);
3233 }
3234 else {
3235 BLI_snprintf(str, str_maxncpy, "%im %is", int(dtime / 60) % 60, int(dtime) % 60);
3236 }
3237 }
3238 else {
3239 BLI_snprintf(str, str_maxncpy, "%is", int(dtime) % 60);
3240 }
3241}
3242
3244{
3245 Scene *scene = baker->scene;
3246 ViewLayer *view_layer = baker->view_layer;
3247 Depsgraph *depsgraph = baker->depsgraph;
3248 Scene *sce_iter; /* SETLOOPER macro only */
3249 Base *base;
3250 ListBase pidlist;
3251 PTCacheID *pid = &baker->pid;
3252 PointCache *cache = nullptr;
3253 float frameleno = scene->r.framelen;
3254 int cfrao = scene->r.cfra;
3255 int startframe = MAXFRAME, endframe = baker->anim_init ? scene->r.sfra : scene->r.cfra;
3256 int bake = baker->bake;
3257 int render = baker->render;
3258
3259 G.is_break = false;
3260
3261 /* set caches to baking mode and figure out start frame */
3262 if (pid->owner_id) {
3263 /* cache/bake a single object */
3264 cache = pid->cache;
3265 if ((cache->flag & PTCACHE_BAKED) == 0) {
3266 if (pid->type == PTCACHE_TYPE_PARTICLES) {
3267 ParticleSystem *psys = static_cast<ParticleSystem *>(pid->calldata);
3268
3269 /* a bit confusing, could make this work better in the UI */
3270 if (psys->part->type == PART_EMITTER) {
3272 static_cast<ParticleSystem *>(pid->calldata),
3273 &cache->startframe,
3274 &cache->endframe);
3275 }
3276 }
3277 else if (pid->type == PTCACHE_TYPE_SMOKE_HIGHRES) {
3278 /* get all pids from the object and search for smoke low res */
3279 ListBase pidlist2;
3280 BLI_assert(GS(pid->owner_id->name) == ID_OB);
3281 BKE_ptcache_ids_from_object(&pidlist2, (Object *)pid->owner_id, scene, MAX_DUPLI_RECUR);
3282 LISTBASE_FOREACH (PTCacheID *, pid2, &pidlist2) {
3283 if (pid2->type == PTCACHE_TYPE_SMOKE_DOMAIN) {
3284 if (pid2->cache && !(pid2->cache->flag & PTCACHE_BAKED)) {
3285 if (bake || pid2->cache->flag & PTCACHE_REDO_NEEDED) {
3287 }
3288 if (bake) {
3289 pid2->cache->flag |= PTCACHE_BAKING;
3290 pid2->cache->flag &= ~PTCACHE_BAKED;
3291 }
3292 }
3293 }
3294 }
3295 BLI_freelistN(&pidlist2);
3296 }
3297
3298 if (bake || cache->flag & PTCACHE_REDO_NEEDED) {
3300 }
3301
3302 startframe = std::max(cache->last_exact, cache->startframe);
3303
3304 if (bake) {
3305 endframe = cache->endframe;
3306 cache->flag |= PTCACHE_BAKING;
3307 }
3308 else {
3309 endframe = std::min(endframe, cache->endframe);
3310 }
3311
3312 cache->flag &= ~PTCACHE_BAKED;
3313 }
3314 }
3315 else {
3316 for (SETLOOPER_VIEW_LAYER(scene, view_layer, sce_iter, base)) {
3317 /* cache/bake everything in the scene */
3318 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
3319
3320 for (pid = static_cast<PTCacheID *>(pidlist.first); pid; pid = pid->next) {
3321 cache = pid->cache;
3322 if ((cache->flag & PTCACHE_BAKED) == 0) {
3323 if (pid->type == PTCACHE_TYPE_PARTICLES) {
3324 ParticleSystem *psys = (ParticleSystem *)pid->calldata;
3325 /* skip hair & keyed particles */
3326 if (psys->part->type == PART_HAIR || psys->part->phystype == PART_PHYS_KEYED) {
3327 continue;
3328 }
3329
3331 static_cast<ParticleSystem *>(pid->calldata),
3332 &cache->startframe,
3333 &cache->endframe);
3334 }
3335
3336 /* XXX: workaround for regression introduced in ee3fadd, needs looking into. */
3337 if (pid->type == PTCACHE_TYPE_RIGIDBODY) {
3338 if ((cache->flag & PTCACHE_REDO_NEEDED ||
3339 (cache->flag & PTCACHE_SIMULATION_VALID) == 0) &&
3340 (render || bake))
3341 {
3343 }
3344 }
3345 else if (((cache->flag & PTCACHE_BAKED) == 0) && (render || bake)) {
3347 }
3348
3349 startframe = std::min(startframe, cache->startframe);
3350
3351 if (bake || render) {
3352 cache->flag |= PTCACHE_BAKING;
3353
3354 if (bake) {
3355 endframe = std::max(endframe, cache->endframe);
3356 }
3357 }
3358
3359 cache->flag &= ~PTCACHE_BAKED;
3360 }
3361 }
3362 BLI_freelistN(&pidlist);
3363 }
3364 }
3365
3366 scene->r.cfra = startframe;
3367 scene->r.framelen = 1.0;
3368
3369 /* bake */
3370
3371 bool use_timer = false;
3372 double stime, ptime, ctime, fetd;
3373 char run[32], cur[32], etd[32];
3374 int cancel = 0;
3375
3376 stime = ptime = BLI_time_now_seconds();
3377
3378 for (int fr = scene->r.cfra; fr <= endframe; fr += baker->quick_step, scene->r.cfra = fr) {
3380
3381 if (baker->update_progress) {
3382 float progress = (float(scene->r.cfra - startframe) / float(endframe - startframe));
3383 baker->update_progress(baker->bake_job, progress, &cancel);
3384 }
3385
3386 if (G.background) {
3387 printf("bake: frame %d :: %d\n", scene->r.cfra, endframe);
3388 }
3389 else {
3390 ctime = BLI_time_now_seconds();
3391
3392 fetd = (ctime - ptime) * (endframe - scene->r.cfra) / baker->quick_step;
3393
3394 if (use_timer || fetd > 60.0) {
3395 use_timer = true;
3396
3397 ptcache_dt_to_str(cur, sizeof(cur), ctime - ptime);
3398 ptcache_dt_to_str(run, sizeof(run), ctime - stime);
3399 ptcache_dt_to_str(etd, sizeof(etd), fetd);
3400
3401 printf("Baked for %s, current frame: %i/%i (%.3fs), ETC: %s\r",
3402 run,
3403 scene->r.cfra - startframe + 1,
3404 endframe - startframe + 1,
3405 ctime - ptime,
3406 etd);
3407 }
3408
3409 ptime = ctime;
3410 }
3411
3412 /* NOTE: breaking baking should leave calculated frames in cache, not clear it */
3413 if (cancel || G.is_break) {
3414 break;
3415 }
3416
3417 scene->r.cfra += 1;
3418 }
3419
3420 if (use_timer) {
3421 /* start with newline because of \r above */
3422 ptcache_dt_to_str(run, sizeof(run), BLI_time_now_seconds() - stime);
3423 printf("\nBake %s %s (%i frames simulated).\n",
3424 (cancel ? "canceled after" : "finished in"),
3425 run,
3426 scene->r.cfra - startframe);
3427 }
3428
3429 /* clear baking flag */
3430 if (pid && cache) {
3433 if (bake) {
3434 cache->flag |= PTCACHE_BAKED;
3435 /* write info file */
3436 if (cache->flag & PTCACHE_DISK_CACHE) {
3437 if (pid->type == PTCACHE_TYPE_PARTICLES) {
3438 /* Since writing this from outside the bake job, make sure the ParticleSystem and
3439 * PTCacheID is in a fully evaluated state. */
3440 PTCacheID pid_eval;
3441 Object *ob = reinterpret_cast<Object *>(pid->owner_id);
3442 Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
3443 ParticleSystem *psys = static_cast<ParticleSystem *>(pid->calldata);
3444 ParticleSystem *psys_eval = psys_eval_get(depsgraph, ob, psys);
3445 BKE_ptcache_id_from_particles(&pid_eval, ob_eval, psys_eval);
3446 BKE_ptcache_write(&pid_eval, 0);
3447 }
3448 else {
3449 BKE_ptcache_write(pid, 0);
3450 }
3451 }
3452 }
3453 }
3454 else {
3455 for (SETLOOPER_VIEW_LAYER(scene, view_layer, sce_iter, base)) {
3456 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
3457
3458 LISTBASE_FOREACH (PTCacheID *, pid, &pidlist) {
3459 /* skip hair particles */
3460 if (pid->type == PTCACHE_TYPE_PARTICLES &&
3461 ((ParticleSystem *)pid->calldata)->part->type == PART_HAIR)
3462 {
3463 continue;
3464 }
3465
3466 cache = pid->cache;
3467
3468 if (baker->quick_step > 1) {
3469 cache->flag &= ~(PTCACHE_BAKING | PTCACHE_OUTDATED);
3470 }
3471 else {
3473 }
3474
3476
3477 if (bake) {
3478 cache->flag |= PTCACHE_BAKED;
3479 if (cache->flag & PTCACHE_DISK_CACHE) {
3480 if (pid->type == PTCACHE_TYPE_PARTICLES) {
3481 /* Since writing this from outside the bake job, make sure the ParticleSystem and
3482 * PTCacheID is in a fully evaluated state. */
3483 PTCacheID pid_eval;
3484 Object *ob = reinterpret_cast<Object *>(pid->owner_id);
3485 Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
3486 ParticleSystem *psys = static_cast<ParticleSystem *>(pid->calldata);
3487 ParticleSystem *psys_eval = psys_eval_get(depsgraph, ob, psys);
3488 BKE_ptcache_id_from_particles(&pid_eval, ob_eval, psys_eval);
3489 BKE_ptcache_write(&pid_eval, 0);
3490 }
3491 else {
3492 BKE_ptcache_write(pid, 0);
3493 }
3494 }
3495 }
3496 }
3497 BLI_freelistN(&pidlist);
3498 }
3499 }
3500
3501 scene->r.framelen = frameleno;
3502 scene->r.cfra = cfrao;
3503
3504 if (bake) { /* already on cfra unless baking */
3506 }
3507
3508 /* TODO: call redraw all windows somehow */
3509}
3510
3511/* Helpers */
3512
3514{
3515 PointCache *cache = pid->cache;
3516 PTCacheMem *pm = nullptr;
3517 int baked = cache->flag & PTCACHE_BAKED;
3518 int cfra, sfra = cache->startframe, efra = cache->endframe;
3519
3520 /* Remove possible bake flag to allow clear */
3521 cache->flag &= ~PTCACHE_BAKED;
3522
3523 /* PTCACHE_DISK_CACHE flag was cleared already */
3525
3526 /* restore possible bake flag */
3527 cache->flag |= baked;
3528
3529 for (cfra = sfra; cfra <= efra; cfra++) {
3530 pm = ptcache_disk_frame_to_mem(pid, cfra);
3531
3532 if (pm) {
3533 BLI_addtail(&pid->cache->mem_cache, pm);
3534 }
3535 }
3536}
3538{
3539 PointCache *cache = pid->cache;
3540 PTCacheMem *pm = static_cast<PTCacheMem *>(cache->mem_cache.first);
3541 int baked = cache->flag & PTCACHE_BAKED;
3542
3543 /* Remove possible bake flag to allow clear */
3544 cache->flag &= ~PTCACHE_BAKED;
3545
3546 /* PTCACHE_DISK_CACHE flag was set already */
3548
3549 /* restore possible bake flag */
3550 cache->flag |= baked;
3551
3552 for (; pm; pm = pm->next) {
3553 if (ptcache_mem_frame_to_disk(pid, pm) == 0) {
3554 cache->flag &= ~PTCACHE_DISK_CACHE;
3555 break;
3556 }
3557 }
3558
3559 /* write info file */
3560 if (cache->flag & PTCACHE_BAKED) {
3561 BKE_ptcache_write(pid, 0);
3562 }
3563}
3565{
3566 PointCache *cache = pid->cache;
3567 int last_exact = cache->last_exact;
3568 const char *blendfile_path = BKE_main_blendfile_path_from_global();
3569
3570 if (blendfile_path[0] == '\0') {
3571 cache->flag &= ~PTCACHE_DISK_CACHE;
3572 if (G.debug & G_DEBUG) {
3573 printf("File must be saved before using disk cache!\n");
3574 }
3575 return;
3576 }
3577
3578 if (cache->cached_frames) {
3579 MEM_freeN(cache->cached_frames);
3580 cache->cached_frames = nullptr;
3581 cache->cached_frames_len = 0;
3582 }
3583
3584 if (cache->flag & PTCACHE_DISK_CACHE) {
3586 }
3587 else {
3589 }
3590
3591 cache->flag ^= PTCACHE_DISK_CACHE;
3593 cache->flag ^= PTCACHE_DISK_CACHE;
3594
3595 cache->last_exact = last_exact;
3596
3597 BKE_ptcache_id_time(pid, nullptr, 0.0f, nullptr, nullptr, nullptr);
3598
3599 cache->flag |= PTCACHE_FLAG_INFO_DIRTY;
3600
3601 if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
3602 if (cache->index) {
3604 cache->index = -1;
3605 }
3606 }
3607}
3608
3609void BKE_ptcache_disk_cache_rename(PTCacheID *pid, const char *name_src, const char *name_dst)
3610{
3611 char old_name[80];
3612 int len; /* store the length of the string */
3613 /* mode is same as fopen's modes */
3614 DIR *dir;
3615 dirent *de;
3616 char path[MAX_PTCACHE_PATH];
3617 char old_filepath[MAX_PTCACHE_FILE];
3618 char new_path_full[MAX_PTCACHE_FILE];
3619 char old_path_full[MAX_PTCACHE_FILE];
3620 char ext[MAX_PTCACHE_FILE];
3621
3622 /* If both names are the same, there is nothing to do. */
3623 if (STREQ(name_src, name_dst)) {
3624 return;
3625 }
3626
3627 /* save old name */
3628 STRNCPY(old_name, pid->cache->name);
3629
3630 /* get "from" filename */
3631 STRNCPY(pid->cache->name, name_src);
3632
3633 len = ptcache_filepath(pid, old_filepath, 0, false, false); /* no path */
3634
3635 ptcache_path(pid, path);
3636 dir = opendir(path);
3637 if (dir == nullptr) {
3638 STRNCPY(pid->cache->name, old_name);
3639 return;
3640 }
3641
3642 ptcache_filepath_ext_append(pid, ext, 0, false, 0);
3643
3644 /* put new name into cache */
3645 STRNCPY(pid->cache->name, name_dst);
3646
3647 while ((de = readdir(dir)) != nullptr) {
3648 if (strstr(de->d_name, ext)) { /* Do we have the right extension? */
3649 if (STREQLEN(old_filepath, de->d_name, len)) { /* Do we have the right prefix. */
3650 /* read the number of the file */
3651 const int frame = ptcache_frame_from_filename(de->d_name, ext);
3652
3653 if (frame != -1) {
3654 BLI_path_join(old_path_full, sizeof(old_path_full), path, de->d_name);
3655 ptcache_filepath(pid, new_path_full, frame, true, true);
3656 BLI_rename_overwrite(old_path_full, new_path_full);
3657 }
3658 }
3659 }
3660 }
3661 closedir(dir);
3662
3663 STRNCPY(pid->cache->name, old_name);
3664}
3665
3667{
3668 /* TODO: */
3669 PointCache *cache = pid->cache;
3670 int len; /* store the length of the string */
3671 int info = 0;
3672 int start = MAXFRAME;
3673 int end = -1;
3674
3675 /* mode is same as fopen's modes */
3676 DIR *dir;
3677 dirent *de;
3678 char path[MAX_PTCACHE_PATH];
3679 char filepath[MAX_PTCACHE_FILE];
3680 char ext[MAX_PTCACHE_PATH];
3681
3682 if (!cache) {
3683 return;
3684 }
3685
3686 ptcache_path(pid, path);
3687
3688 len = ptcache_filepath(pid, filepath, 1, false, false); /* no path */
3689
3690 dir = opendir(path);
3691 if (dir == nullptr) {
3692 return;
3693 }
3694
3695 const char *fext = ptcache_file_extension(pid);
3696
3697 if (cache->index >= 0) {
3698 SNPRINTF(ext, "_%02d%s", cache->index, fext);
3699 }
3700 else {
3701 STRNCPY(ext, fext);
3702 }
3703
3704 while ((de = readdir(dir)) != nullptr) {
3705 if (strstr(de->d_name, ext)) { /* Do we have the right extension? */
3706 if (STREQLEN(filepath, de->d_name, len)) { /* Do we have the right prefix. */
3707 /* read the number of the file */
3708 const int frame = ptcache_frame_from_filename(de->d_name, ext);
3709
3710 if (frame != -1) {
3711 if (frame) {
3712 start = std::min(start, frame);
3713 end = std::max(end, frame);
3714 }
3715 else {
3716 info = 1;
3717 }
3718 }
3719 }
3720 }
3721 }
3722 closedir(dir);
3723
3724 if (start != MAXFRAME) {
3725 PTCacheFile *pf;
3726
3727 cache->startframe = start;
3728 cache->endframe = end;
3729 cache->totpoint = 0;
3730
3731 if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN) {
3732 /* necessary info in every file */
3733 }
3734 /* read totpoint from info file (frame 0) */
3735 else if (info) {
3737
3738 if (pf) {
3740 if (pf->type == pid->type && pid->read_header(pf)) {
3741 cache->totpoint = pf->totpoint;
3742 cache->flag |= PTCACHE_READ_INFO;
3743 }
3744 else {
3745 cache->totpoint = 0;
3746 }
3747 }
3749 }
3750 }
3751 /* or from any old format cache file */
3752 else {
3753 float old_data[14];
3754 int elemsize = ptcache_old_elemsize(pid);
3756
3757 if (pf) {
3758 while (ptcache_file_read(pf, old_data, 1, elemsize)) {
3759 cache->totpoint++;
3760 }
3761
3763 }
3764 }
3767 }
3768
3769 /* make sure all new frames are loaded */
3770 if (cache->cached_frames) {
3771 MEM_freeN(cache->cached_frames);
3772 cache->cached_frames = nullptr;
3773 cache->cached_frames_len = 0;
3774 }
3775
3776 cache->flag |= PTCACHE_FLAG_INFO_DIRTY;
3777}
3778
3780{
3781 PointCache *cache = pid->cache;
3782 int totframes = 0;
3783 char mem_info[sizeof(PointCache::info) / sizeof(*PointCache::info)];
3784
3786
3787 if (cache->flag & PTCACHE_EXTERNAL) {
3788 int cfra = cache->startframe;
3789
3790 for (; cfra <= cache->endframe; cfra++) {
3791 if (BKE_ptcache_id_exist(pid, cfra)) {
3792 totframes++;
3793 }
3794 }
3795
3796 /* smoke doesn't use frame 0 as info frame so can't check based on totpoint */
3797 if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN && totframes) {
3798 SNPRINTF(cache->info, RPT_("%i frames found!"), totframes);
3799 }
3800 else if (totframes && cache->totpoint) {
3801 SNPRINTF(cache->info, RPT_("%i points found!"), cache->totpoint);
3802 }
3803 else {
3804 STRNCPY(cache->info, RPT_("No valid data to read!"));
3805 }
3806 return;
3807 }
3808
3809 if (cache->flag & PTCACHE_DISK_CACHE) {
3810 if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN) {
3811 int totpoint = pid->totpoint(pid->calldata, 0);
3812
3813 if (cache->totpoint > totpoint) {
3814 SNPRINTF(mem_info, RPT_("%i cells + High Resolution cached"), totpoint);
3815 }
3816 else {
3817 SNPRINTF(mem_info, RPT_("%i cells cached"), totpoint);
3818 }
3819 }
3820 else {
3821 int cfra = cache->startframe;
3822
3823 for (; cfra <= cache->endframe; cfra++) {
3824 if (BKE_ptcache_id_exist(pid, cfra)) {
3825 totframes++;
3826 }
3827 }
3828
3829 SNPRINTF(mem_info, RPT_("%i frames on disk"), totframes);
3830 }
3831 }
3832 else {
3833 PTCacheMem *pm = static_cast<PTCacheMem *>(cache->mem_cache.first);
3834 char formatted_tot[BLI_STR_FORMAT_INT32_GROUPED_SIZE];
3835 char formatted_mem[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE];
3836 long long int bytes = 0.0f;
3837 int i;
3838
3839 for (; pm; pm = pm->next) {
3840 for (i = 0; i < BPHYS_TOT_DATA; i++) {
3841 bytes += MEM_allocN_len(pm->data[i]);
3842 }
3843
3844 LISTBASE_FOREACH (PTCacheExtra *, extra, &pm->extradata) {
3845 bytes += MEM_allocN_len(extra->data);
3846 bytes += sizeof(PTCacheExtra);
3847 }
3848
3849 bytes += sizeof(PTCacheMem);
3850
3851 totframes++;
3852 }
3853
3854 BLI_str_format_int_grouped(formatted_tot, totframes);
3855 BLI_str_format_byte_unit(formatted_mem, bytes, false);
3856
3857 SNPRINTF(mem_info, RPT_("%s frames in memory (%s)"), formatted_tot, formatted_mem);
3858 }
3859
3860 if (cache->flag & PTCACHE_OUTDATED) {
3861 SNPRINTF(cache->info, RPT_("%s, cache is outdated!"), mem_info);
3862 }
3863 else if (cache->flag & PTCACHE_FRAMES_SKIPPED) {
3864 SNPRINTF(cache->info, RPT_("%s, not exact since frame %i"), mem_info, cache->last_exact);
3865 }
3866 else {
3867 SNPRINTF(cache->info, "%s.", mem_info);
3868 }
3869}
3870
3871void BKE_ptcache_validate(PointCache *cache, int framenr)
3872{
3873 if (cache) {
3875 cache->simframe = framenr;
3876 }
3877}
3879{
3880 if (cache) {
3882 cache->simframe = 0;
3883 cache->last_exact = std::min(cache->startframe, 0);
3884 }
3885}
3886
3888{
3889 LISTBASE_FOREACH (PointCache *, cache, ptcaches) {
3890 BLO_write_struct(writer, PointCache, cache);
3891
3892 if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
3893 LISTBASE_FOREACH (PTCacheMem *, pm, &cache->mem_cache) {
3894 BLO_write_struct(writer, PTCacheMem, pm);
3895
3896 for (int i = 0; i < BPHYS_TOT_DATA; i++) {
3897 if (pm->data[i] && pm->data_types & (1 << i)) {
3898 if (i == BPHYS_DATA_BOIDS) {
3899 BLO_write_struct_array(writer, BoidData, pm->totpoint, pm->data[i]);
3900 }
3901 else if (i == BPHYS_DATA_INDEX) { /* Only 'cache type' to use uint values. */
3903 writer, pm->totpoint, reinterpret_cast<uint32_t *>(pm->data[i]));
3904 }
3905 else { /* All other types of caches use (vectors of) floats. */
3906 /* data_size returns bytes. */
3907 const uint32_t items_num = pm->totpoint * (BKE_ptcache_data_size(i) / sizeof(float));
3908 BLO_write_float_array(writer, items_num, reinterpret_cast<float *>(pm->data[i]));
3909 }
3910 }
3911 }
3912
3913 LISTBASE_FOREACH (PTCacheExtra *, extra, &pm->extradata) {
3914 BLO_write_struct(writer, PTCacheExtra, extra);
3915 if (extra->type == BPHYS_EXTRA_FLUID_SPRINGS) {
3916 BLO_write_struct_array(writer, ParticleSpring, extra->totdata, extra->data);
3917 }
3918 else if (extra->type == BPHYS_EXTRA_CLOTH_ACCELERATION) {
3919 BLO_write_struct_array(writer, vec3f, extra->totdata, extra->data);
3920 }
3921 else if (extra->data) {
3923 }
3924 }
3925 }
3926 }
3927 }
3928}
3929
3931{
3932 bool ok = true;
3933 for (int i = 0; i < BPHYS_TOT_DATA; i++) {
3934 if (pm->data[i] == nullptr) {
3935 /* Corrupt file missing the array for a stored data type. */
3936 if (pm->data_types & (1 << i)) {
3937 ok = false;
3938 }
3939 continue;
3940 }
3941
3942 if (i == BPHYS_DATA_BOIDS) {
3943 BLO_read_struct_array(reader, BoidData, pm->totpoint, &pm->data[i]);
3944 }
3945 else if (i == BPHYS_DATA_INDEX) { /* Only 'cache type' to use uint values. */
3946 BLO_read_uint32_array(reader, pm->totpoint, reinterpret_cast<uint32_t **>(&pm->data[i]));
3947 }
3948 else { /* All other types of caches use (vectors of) floats. */
3949 /* data_size returns bytes. */
3950 const int64_t items_num = int64_t(pm->totpoint) * (BKE_ptcache_data_size(i) / sizeof(float));
3951 BLO_read_float_array(reader, items_num, reinterpret_cast<float **>(&pm->data[i]));
3952 }
3953 /* Array smaller than expected. */
3954 if (pm->data[i] == nullptr) {
3955 ok = false;
3956 }
3957 }
3958 if (!ok) {
3959 pm->totpoint = 0;
3960 }
3961
3963
3964 LISTBASE_FOREACH (PTCacheExtra *, extra, &pm->extradata) {
3965 if (extra->type == BPHYS_EXTRA_FLUID_SPRINGS) {
3966 BLO_read_struct_array(reader, ParticleSpring, extra->totdata, &extra->data);
3967 }
3968 else if (extra->type == BPHYS_EXTRA_CLOTH_ACCELERATION) {
3969 BLO_read_struct_array(reader, vec3f, extra->totdata, &extra->data);
3970 }
3971 else if (extra->data) {
3972 extra->data = nullptr;
3973 }
3974 }
3975}
3976
3978{
3979 if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
3980 BLO_read_struct_list(reader, PTCacheMem, &cache->mem_cache);
3981 LISTBASE_FOREACH (PTCacheMem *, pm, &cache->mem_cache) {
3982 direct_link_pointcache_mem(reader, pm);
3983 }
3984 }
3985 else {
3987 }
3988
3990 cache->simframe = 0;
3991 cache->edit = nullptr;
3992 cache->free_edit = nullptr;
3993 cache->cached_frames = nullptr;
3994 cache->cached_frames_len = 0;
3995}
3996
3998 ListBase *ptcaches,
3999 PointCache **ocache,
4000 int force_disk)
4001{
4002 if (ptcaches->first) {
4003 BLO_read_struct_list(reader, PointCache, ptcaches);
4004 LISTBASE_FOREACH (PointCache *, cache, ptcaches) {
4005 direct_link_pointcache(reader, cache);
4006 if (force_disk) {
4007 cache->flag |= PTCACHE_DISK_CACHE;
4008 cache->step = 1;
4009 }
4010 }
4011
4012 BLO_read_struct(reader, PointCache, ocache);
4013 }
4014 else if (*ocache) {
4015 /* old "single" caches need to be linked too */
4016 BLO_read_struct(reader, PointCache, ocache);
4017 direct_link_pointcache(reader, *ocache);
4018 if (force_disk) {
4019 (*ocache)->flag |= PTCACHE_DISK_CACHE;
4020 (*ocache)->step = 1;
4021 }
4022
4023 ptcaches->first = ptcaches->last = *ocache;
4024 }
4025}
void BIK_clear_cache(struct bPose *pose)
void cloth_free_modifier(ClothModifierData *clmd)
Definition cloth.cc:432
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_END
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(_collection, _object)
constexpr int MAX_DUPLI_RECUR
void dynamicPaint_clearSurface(const struct Scene *scene, struct DynamicPaintSurface *surface)
@ G_DEBUG
@ LIB_ID_COPY_CACHES
const char * BKE_main_blendfile_path_from_global()
Definition main.cc:882
void BKE_modifier_set_error(const Object *ob, ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
General operations, lookup, etc. for blender objects.
int BKE_object_insert_ptcache(Object *ob)
void BKE_object_delete_ptcache(Object *ob, int index)
void psys_get_pointcache_start_end(struct Scene *scene, ParticleSystem *psys, int *sfra, int *efra)
void psys_interpolate_particle(short type, struct ParticleKey keys[4], float dt, struct ParticleKey *result, bool velocity)
Definition particle.cc:1172
void psys_reset(struct ParticleSystem *psys, int mode)
#define PSYS_RESET_ALL
struct ParticleSystem * psys_eval_get(struct Depsgraph *depsgraph, struct Object *object, struct ParticleSystem *psys)
Definition particle.cc:700
#define PSYS_RESET_DEPSGRAPH
int BKE_ptcache_data_size(int data_type)
#define PTCACHE_TYPEFLAG_TYPEMASK
#define PTCACHE_FILE_READ
#define PTCACHE_TYPE_SMOKE_HIGHRES
void BKE_ptcache_mem_pointers_incr(void *cur[BPHYS_TOT_DATA])
void BKE_ptcache_free_mem(struct ListBase *mem_cache)
void BKE_ptcache_ids_from_object(struct ListBase *lb, struct Object *ob, struct Scene *scene, int duplis)
#define PTCACHE_VEL_PER_SEC
struct PointCache * BKE_ptcache_copy_list(struct ListBase *ptcaches_new, const struct ListBase *ptcaches_old, int flag)
bool BKE_ptcache_object_has(struct Scene *scene, struct Object *ob, int duplis)
#define PTCACHE_FILE_WRITE
void BKE_ptcache_id_time(PTCacheID *pid, struct Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, struct Object *ob, struct RigidBodyWorld *rbw)
#define PTCACHE_CLEAR_BEFORE
#define PTCACHE_CLEAR_AFTER
void BKE_ptcache_quick_cache_all(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer)
void BKE_ptcache_id_from_particles(PTCacheID *pid, struct Object *ob, struct ParticleSystem *psys)
void BKE_ptcache_update_info(PTCacheID *pid)
void BKE_ptcache_validate(struct PointCache *cache, int framenr)
#define PTCACHE_EXT
void BKE_ptcache_bake(struct PTCacheBaker *baker)
void BKE_ptcache_toggle_disk_cache(struct PTCacheID *pid)
#define PTCACHE_TYPE_CLOTH
void BKE_ptcache_id_from_dynamicpaint(PTCacheID *pid, struct Object *ob, struct DynamicPaintSurface *surface)
#define PTCACHE_TYPE_DYNAMICPAINT
int BKE_ptcache_mem_pointers_seek(int point_index, struct PTCacheMem *pm, void *cur[BPHYS_TOT_DATA])
void BKE_ptcache_id_from_softbody(PTCacheID *pid, struct Object *ob, struct SoftBody *sb)
void BKE_ptcache_disk_to_mem(struct PTCacheID *pid)
bool BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
struct PointCache * BKE_ptcache_add(struct ListBase *ptcaches)
blender::FunctionRef< bool(PTCacheID &pid, ModifierData *md)> PointCacheIdFn
#define PTCACHE_CLEAR_ALL
#define PTCACHE_READ_INTERPOLATED
#define PTCACHE_TYPE_PARTICLES
void BKE_ptcache_blend_read_data(struct BlendDataReader *reader, struct ListBase *ptcaches, struct PointCache **ocache, int force_disk)
#define PTCACHE_TYPEFLAG_FLAGMASK
#define PTCACHE_TYPE_SOFTBODY
@ PTCACHE_FILE_PTCACHE
#define PTCACHE_READ_OLD
void BKE_ptcache_load_external(struct PTCacheID *pid)
#define PTCACHE_TYPEFLAG_COMPRESS
PTCacheID BKE_ptcache_id_find(struct Object *ob, struct Scene *scene, struct PointCache *cache)
int BKE_ptcache_read(PTCacheID *pid, float cfra, bool no_extrapolate_old)
int BKE_ptcache_mem_index_find(struct PTCacheMem *pm, unsigned int index)
#define PTCACHE_FILE_UPDATE
void BKE_ptcache_foreach_object_cache(struct Object &ob, struct Scene &scene, bool duplis, PointCacheIdFn fn)
void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
void BKE_ptcache_make_particle_key(struct ParticleKey *key, int index, void **data, float time)
void BKE_ptcache_id_from_cloth(PTCacheID *pid, struct Object *ob, struct ClothModifierData *clmd)
#define PTCACHE_CLEAR_FRAME
int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
void BKE_ptcache_mem_to_disk(struct PTCacheID *pid)
void BKE_ptcache_free_list(struct ListBase *ptcaches)
#define PTCACHE_TYPE_SMOKE_DOMAIN
#define PTCACHE_RESET_OUTDATED
int BKE_ptcache_object_reset(struct Scene *scene, struct Object *ob, int mode)
int BKE_ptcache_id_reset(struct Scene *scene, PTCacheID *pid, int mode)
void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct FluidModifierData *fmd)
void BKE_ptcache_free(struct PointCache *cache)
#define PTCACHE_PATH
#define PTCACHE_TYPE_RIGIDBODY
#define PTCACHE_RESET_DEPSGRAPH
void BKE_ptcache_invalidate(struct PointCache *cache)
#define PTCACHE_RESET_BAKED
#define PTCACHE_READ_EXACT
#define PTCACHE_TYPEFLAG_EXTRADATA
void BKE_ptcache_disk_cache_rename(struct PTCacheID *pid, const char *name_src, const char *name_dst)
void BKE_ptcache_mem_pointers_init(struct PTCacheMem *pm, void *cur[BPHYS_TOT_DATA])
void BKE_ptcache_blend_write(struct BlendWriter *writer, struct ListBase *ptcaches)
float BKE_scene_ctime_get(const Scene *scene)
Definition scene.cc:2367
#define SETLOOPER_VIEW_LAYER(_sce_basis, _view_layer, _sce_iter, _base)
Definition BKE_scene.hh:44
void BKE_scene_graph_update_for_newframe(Depsgraph *depsgraph)
Definition scene.cc:2697
float BKE_scene_frame_to_ctime(const Scene *scene, int frame)
Definition scene.cc:2372
void sbFreeSimulation(struct SoftBody *sb)
Definition softbody.cc:3187
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:373
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:505
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition storage.cc:212
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:429
#define LISTBASE_FOREACH(type, var, list)
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:270
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:497
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:252
void interp_qt_qtqt(float q[4], const float a[4], const float b[4], float t)
void vec_to_quat(float q[4], const float vec[3], short axis, short upflag)
void unit_qt(float q[4])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v3(float r[3])
ATTR_WARN_UNUSED_RESULT const size_t num
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
#define BLI_path_join(...)
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
size_t BLI_str_format_int_grouped(char dst[BLI_STR_FORMAT_INT32_GROUPED_SIZE], int num) ATTR_NONNULL(1)
Definition string.cc:1181
#define BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE
Definition BLI_string.h:28
#define SNPRINTF(dst, format,...)
Definition BLI_string.h:599
#define BLI_STR_FORMAT_INT32_GROUPED_SIZE
Definition BLI_string.h:25
void BLI_str_format_byte_unit(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE], long long int bytes, bool base_10) ATTR_NONNULL(1)
Definition string.cc:1205
size_t BLI_snprintf_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char char size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
unsigned char uchar
unsigned int uint
Platform independent time functions.
double BLI_time_now_seconds(void)
Definition time.cc:65
#define UNPACK4(a)
#define UNUSED_VARS(...)
#define STREQLEN(a, b, n)
#define ELEM(...)
#define STREQ(a, b)
Compatibility-like things for windows.
struct __dirstream DIR
struct dirent * readdir(DIR *dp)
int closedir(DIR *dp)
const char * dirname(char *path)
DIR * opendir(const char *path)
void BLO_write_float_array(BlendWriter *writer, int64_t num, const float *data_ptr)
void BLO_read_uint32_array(BlendDataReader *reader, int64_t array_size, uint32_t **ptr_p)
Definition readfile.cc:5748
void BLO_read_float_array(BlendDataReader *reader, int64_t array_size, float **ptr_p)
Definition readfile.cc:5758
#define BLO_write_struct(writer, struct_name, data_ptr)
#define BLO_write_struct_array(writer, struct_name, array_size, data_ptr)
#define BLO_read_struct_list(reader, struct_name, list)
void BLO_write_uint32_array(BlendWriter *writer, int64_t num, const uint32_t *data_ptr)
#define BLO_read_struct_array(reader, struct_name, array_size, ptr_p)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define RPT_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:182
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
ID and Library types, which are fundamental for SDNA.
@ ID_RECALC_PSYS_REDO
Definition DNA_ID.h:989
@ ID_RECALC_PSYS_CHILD
Definition DNA_ID.h:993
@ ID_OB
@ MOD_DPAINT_SURFACE_F_IMAGESEQ
@ MOD_DPAINT_SURFACE_T_WEIGHT
@ MOD_DPAINT_SURFACE_T_PAINT
@ MOD_DPAINT_SURFACE_T_DISPLACE
@ MOD_DPAINT_SURFACE_T_WAVE
@ FLUID_DOMAIN_CACHE_REPLAY
@ MOD_FLUID_TYPE_DOMAIN
@ eModifierType_Cloth
@ eModifierType_Fluid
@ eModifierType_DynamicPaint
Object is a sort of wrapper for general info.
@ OB_ARMATURE
@ OB_NEGX
@ OB_POSZ
@ PART_DIED
@ PART_ROTATIONS
@ PART_ROT_VEL
@ PART_PHYS_FLUID
@ PART_PHYS_KEYED
@ PART_PHYS_BOIDS
@ PART_PHYS_NO
@ PART_AVE_RAND
@ PSYS_HAIR_DYNAMICS
@ PART_EMITTER
@ PART_FLUID
@ PART_HAIR
@ SPH_VISCOELASTIC_SPRINGS
@ PTCACHE_COMPRESS_LZMA
@ PTCACHE_COMPRESS_LZO
@ BPHYS_DATA_XCONST
@ BPHYS_DATA_SIZE
@ BPHYS_DATA_DYNAMICPAINT
@ BPHYS_DATA_VELOCITY
@ BPHYS_DATA_LOCATION
@ BPHYS_DATA_BOIDS
@ BPHYS_DATA_INDEX
@ BPHYS_DATA_AVELOCITY
@ BPHYS_DATA_ROTATION
@ BPHYS_DATA_TIMES
@ PTCACHE_READ_INFO
@ PTCACHE_IGNORE_CLEAR
@ PTCACHE_EXTERNAL
@ PTCACHE_IGNORE_LIBPATH
@ PTCACHE_BAKED
@ PTCACHE_FRAMES_SKIPPED
@ PTCACHE_FLAG_INFO_DIRTY
@ PTCACHE_BAKING
@ PTCACHE_OUTDATED
@ PTCACHE_DISK_CACHE
@ PTCACHE_SIMULATION_VALID
@ PTCACHE_REDO_NEEDED
@ BPHYS_EXTRA_FLUID_SPRINGS
@ BPHYS_EXTRA_CLOTH_ACCELERATION
#define BPHYS_TOT_DATA
Types and defines for representing Rigid Body entities.
@ RBO_TYPE_ACTIVE
@ RBO_FLAG_NEEDS_RESHAPE
#define MAXFRAME
Read Guarded memory(de)allocation.
Rigid Body API for interfacing with external Physics Engines.
float progress
Definition WM_types.hh:1019
BMesh const char void * data
long long int int64_t
static void clear(T &value)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
void reset()
#define rot(x, k)
#define str(s)
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
#define in
#define floor
#define out
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define printf(...)
#define GS(a)
#define LOG(severity)
Definition log.h:32
void * MEM_mallocN(size_t len, const char *str)
Definition mallocn.cc:128
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
size_t(*) MEM_allocN_len(const void *vmemh)
Definition mallocn.cc:36
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
ccl_device_inline float len(const float2 a)
#define G(x, y, z)
static int ptcache_rigidbody_totpoint(void *rb_v, int)
static bool ptcache_file_write(PTCacheFile *pf, const void *data, size_t items_num, size_t item_size)
#define PTCACHE_DATA_FROM(data, type, from)
Definition pointcache.cc:92
static int ptcache_basic_header_read(PTCacheFile *pf)
static void ptcache_data_copy(void *from[], void *to[])
static bool ptcache_file_read(PTCacheFile *pf, void *f, size_t items_num, size_t item_size)
static int ptcache_cloth_totpoint(void *cloth_v, int)
static int ptcache_softbody_totpoint(void *soft_v, int)
static PTCacheFile * ptcache_file_open(PTCacheID *pid, int mode, int cfra)
static int ptcache_particle_totwrite(void *psys_v, int cfra)
static void ptcache_particle_interpolate(int index, void *psys_v, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
static void ptcache_add_extra_data(PTCacheMem *pm, uint type, uint count, void *data)
static void ptcache_file_close(PTCacheFile *pf)
static int ptcache_filepath(PTCacheID *pid, char filepath[MAX_PTCACHE_FILE], int cfra, const bool do_path, const bool do_ext)
static int ptcache_read(PTCacheID *pid, int cfra)
static int ptcache_dynamicpaint_totpoint(void *sd, int)
static void ptcache_cloth_extra_write(void *cloth_v, PTCacheMem *pm, int)
#define MAX_PTCACHE_PATH
static const char * ptcache_file_extension(const PTCacheID *pid)
static void ptcache_softbody_read(int index, void *soft_v, void **data, float, const float *old_data)
static int ptcache_dynamicpaint_write(PTCacheFile *pf, void *dp_v)
static int ptcache_data_size[]
static void direct_link_pointcache_mem(BlendDataReader *reader, PTCacheMem *pm)
static void ptcache_cloth_extra_read(void *cloth_v, PTCacheMem *pm, float)
static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
static int ptcache_cloth_write(int index, void *cloth_v, void **data, int)
#define MAX_PTCACHE_FILE
static void direct_link_pointcache(BlendDataReader *reader, PointCache *cache)
static PTCacheMem * ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
static int ptcache_particle_totpoint(void *psys_v, int)
static int ptcache_file_data_write(PTCacheFile *pf)
static void ptcache_file_pointers_init(PTCacheFile *pf)
static bool ptcache_data_alloc(PTCacheMem *pm, const bool zero_init)
static bool ptcache_file_compressed_write(PTCacheFile *pf, uchar *in, uint in_len, uchar *out, int mode)
static bool ptcache_file_points_read(PTCacheFile *pf, PTCacheMem *pm)
static int ptcache_read_stream(PTCacheID *pid, int cfra)
static void ptcache_cloth_interpolate(int index, void *cloth_v, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
static int ptcache_rigidbody_write(int index, void *rb_v, void **data, int)
static int ptcache_path(PTCacheID *pid, char dirname[MAX_PTCACHE_PATH])
static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int)
static int ptcache_dynamicpaint_read(PTCacheFile *pf, void *dp_v)
static bool ptcache_file_compressed_read(PTCacheFile *pf, uchar *result, size_t items_num, size_t item_size)
static bool foreach_object_ptcache(Scene *scene, Object *object, int duplis, PointCacheIdFn callback)
static int ptcache_extra_datasize[]
static size_t ptcache_filepath_ext_append(PTCacheID *pid, char filepath[MAX_PTCACHE_FILE], const size_t filepath_len, const bool use_frame_number, const int cfra)
static int ptcache_frame_from_filename(const char *filename, const char *ext)
static void ptcache_softbody_error(const ID *, void *, const char *)
static void ptcache_cloth_read(int index, void *cloth_v, void **data, float, const float *old_data)
static void ptcache_particle_read(int index, void *psys_v, void **data, float cfra, const float *old_data)
static PointCache * ptcache_copy(PointCache *cache, const bool copy_data)
static void ptcache_cloth_error(const ID *owner_id, void *cloth_v, const char *message)
static void ptcache_rigidbody_error(const ID *, void *, const char *)
static int ptcache_old_elemsize(PTCacheID *pid)
static bool foreach_object_modifier_ptcache(Object *object, PointCacheIdFn callback)
static int ptcache_interpolate(PTCacheID *pid, float cfra, int cfra1, int cfra2)
#define PTCACHE_DATA_TO(data, type, index, to)
Definition pointcache.cc:98
static void ptcache_rigidbody_interpolate(int index, void *rb_v, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
#define LZO_OUT_LEN(size)
Definition pointcache.cc:86
static int ptcache_particle_write(int index, void *psys_v, void **data, int cfra)
static bool foreach_object_particle_ptcache(Object *object, PointCacheIdFn callback)
static void ptcache_particle_extra_read(void *psys_v, PTCacheMem *pm, float)
#define DPAINT_CACHE_VERSION
static void ptcache_dynamicpaint_error(const ID *, void *, const char *)
static int ptcache_file_header_begin_write(PTCacheFile *pf)
static int ptcache_softbody_write(int index, void *soft_v, void **data, int)
static void ptcache_softbody_interpolate(int index, void *soft_v, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
static void ptcache_find_frames_around(PTCacheID *pid, uint frame, int *r_fra1, int *r_fra2)
static void ptcache_mem_clear(PTCacheMem *pm)
static int ptcache_write_needed(PTCacheID *pid, int cfra, int *overwrite)
static void ptcache_data_free(PTCacheMem *pm)
static int ptcache_file_header_begin_read(PTCacheFile *pf)
static void ptcache_extra_free(PTCacheMem *pm)
static void ptcache_rigidbody_read(int index, void *rb_v, void **data, float, const float *old_data)
static void ptcache_dt_to_str(char *str, size_t str_maxncpy, double dtime)
static int ptcache_basic_header_write(PTCacheFile *pf)
static void ptcache_particle_error(const ID *, void *, const char *)
return ret
void RB_body_get_orientation(rbRigidBody *object, float v_out[4])
void RB_body_get_position(rbRigidBody *object, float v_out[3])
struct Object * object
float pos[3]
float vec[3]
struct BoidData data
struct ListBase ptcaches
struct ClothHairData * hairdata
struct Cloth * clothObject
struct PointCache * point_cache
float x[3]
Definition BKE_cloth.hh:96
float v[3]
Definition BKE_cloth.hh:94
float xconst[3]
Definition BKE_cloth.hh:95
ClothVertex * verts
Definition BKE_cloth.hh:69
float average_acceleration[3]
Definition BKE_cloth.hh:84
unsigned int mvert_num
Definition BKE_cloth.hh:72
struct DynamicPaintCanvasSettings * canvas
struct PaintSurfaceData * data
struct DynamicPaintSurface * next
struct PointCache * pointcache
struct ListBase ptcaches[2]
struct PointCache * point_cache[2]
struct FluidDomainSettings * domain
Definition DNA_ID.h:404
struct Library * lib
Definition DNA_ID.h:410
char name[66]
Definition DNA_ID.h:415
LibraryRuntimeHandle * runtime
Definition DNA_ID.h:516
void * last
void * first
struct ModifierData * next
ListBase particlesystem
struct Collection * instance_collection
struct bPose * pose
ListBase modifiers
struct RigidBodyOb * rigidbody_object
struct SoftBody * soft
struct RigidBodyCon * rigidbody_constraint
void(*) update_progress(void *data, float progress, int *cancel)
struct PTCacheID pid
struct Scene * scene
struct Main * bmain
struct Depsgraph * depsgraph
struct ViewLayer * view_layer
unsigned int totdata
struct PTCacheExtra * next
struct PointCache ** cache_ptr
unsigned int data_types
struct ID * owner_id
unsigned int info_types
void(*) read_extra_data(void *calldata, struct PTCacheMem *pm, float cfra)
int(*) totwrite(void *calldata, int cfra)
void * calldata
int(*) totpoint(void *calldata, int cfra)
unsigned int type
unsigned int stack_index
struct ListBase * ptcaches
unsigned int flag
int(*) read_stream(PTCacheFile *pf, void *calldata)
unsigned int default_step
unsigned int file_type
int(*) write_point(int index, void *calldata, void **data, int cfra)
void(*) read_point(int index, void *calldata, void **data, float cfra, const float *old_data)
int(*) read_header(PTCacheFile *pf)
void(*) write_extra_data(void *calldata, struct PTCacheMem *pm, int cfra)
void(*) interpolate_point(int index, void *calldata, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
unsigned int max_step
struct PTCacheID * next
struct PointCache * cache
int(*) write_stream(PTCacheFile *pf, void *calldata)
int(*) write_header(PTCacheFile *pf)
void(*) error(const struct ID *owner_id, void *calldata, const char *message)
void(*) interpolate_extra_data(void *calldata, struct PTCacheMem *pm, float cfra, float cfra1, float cfra2)
unsigned int data_types
unsigned int frame
struct ListBase extradata
struct PTCacheMem * next
struct PTCacheMem * prev
unsigned int totpoint
BoidParticle * boid
ParticleKey state
ParticleKey prev_state
struct SPHFluidSettings * fluid
unsigned int particle_index[2]
ParticleSpring * fluid_springs
struct ListBase ptcaches
ParticleData * particles
ParticleSettings * part
struct ParticleSystem * next
struct PointCache * pointcache
struct PointCache * next
struct ListBase mem_cache
struct PTCacheEdit * edit
void(*) free_edit(struct PTCacheEdit *edit)
struct RigidBodyOb_Shared * shared
struct PointCache * pointcache
struct RigidBodyWorld_Shared * shared
struct Object ** objects
struct PhysicsSettings physics_settings
struct RigidBodyWorld * rigidbody_world
struct RenderData r
struct PointCache * pointcache
struct SoftBody_Shared * shared
struct BodyPoint * bpoint
char * d_name
void * BKE_tempdir_session
Definition stubs.c:38
i
Definition text_draw.cc:230
static DynamicLibrary lib