Blender  V2.93
BKE_pointcache.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2006 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
26 #include "DNA_boid_types.h" /* for #BoidData */
27 #include "DNA_pointcache_types.h" /* for #BPHYS_TOT_DATA */
28 
29 #include <stdio.h> /* for #FILE */
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /* Point cache clearing option, for BKE_ptcache_id_clear, before
36  * and after are non inclusive (they wont remove the cfra) */
37 #define PTCACHE_CLEAR_ALL 0
38 #define PTCACHE_CLEAR_FRAME 1
39 #define PTCACHE_CLEAR_BEFORE 2
40 #define PTCACHE_CLEAR_AFTER 3
41 
42 /* Point cache reset options */
43 #define PTCACHE_RESET_DEPSGRAPH 0
44 #define PTCACHE_RESET_BAKED 1
45 #define PTCACHE_RESET_OUTDATED 2
46 /* #define PTCACHE_RESET_FREE 3 */ /*UNUSED*/
47 
48 /* Add the blendfile name after blendcache_ */
49 #define PTCACHE_EXT ".bphys"
50 #define PTCACHE_PATH "blendcache_"
51 
52 /* File open options, for BKE_ptcache_file_open */
53 #define PTCACHE_FILE_READ 0
54 #define PTCACHE_FILE_WRITE 1
55 #define PTCACHE_FILE_UPDATE 2
56 
57 /* PTCacheID types */
58 #define PTCACHE_TYPE_SOFTBODY 0
59 #define PTCACHE_TYPE_PARTICLES 1
60 #define PTCACHE_TYPE_CLOTH 2
61 #define PTCACHE_TYPE_SMOKE_DOMAIN 3
62 #define PTCACHE_TYPE_SMOKE_HIGHRES 4
63 #define PTCACHE_TYPE_DYNAMICPAINT 5
64 #define PTCACHE_TYPE_RIGIDBODY 6
65 #define PTCACHE_TYPE_SIM_PARTICLES 7
66 
67 /* high bits reserved for flags that need to be stored in file */
68 #define PTCACHE_TYPEFLAG_COMPRESS (1 << 16)
69 #define PTCACHE_TYPEFLAG_EXTRADATA (1 << 17)
70 
71 #define PTCACHE_TYPEFLAG_TYPEMASK 0x0000FFFF
72 #define PTCACHE_TYPEFLAG_FLAGMASK 0xFFFF0000
73 
74 /* PTCache read return code */
75 #define PTCACHE_READ_EXACT 1
76 #define PTCACHE_READ_INTERPOLATED 2
77 #define PTCACHE_READ_OLD 3
78 
79 /* Structs */
80 struct BlendDataReader;
81 struct BlendWriter;
82 struct ClothModifierData;
83 struct DynamicPaintSurface;
84 struct FluidModifierData;
85 struct ListBase;
86 struct Main;
87 struct Object;
88 struct ParticleKey;
89 struct ParticleSystem;
90 struct PointCache;
91 struct RigidBodyWorld;
92 struct Scene;
93 struct SoftBody;
94 struct ViewLayer;
95 
96 /* temp structure for read/write */
97 typedef struct PTCacheData {
98  unsigned int index;
99  float loc[3];
100  float vel[3];
101  float rot[4];
102  float ave[3];
103  float size;
104  float times[3];
105  struct BoidData boids;
107 
108 typedef struct PTCacheFile {
109  FILE *fp;
110 
112  unsigned int totpoint, type;
113  unsigned int data_types, flag;
114 
115  struct PTCacheData data;
118 
119 #define PTCACHE_VEL_PER_SEC 1
120 
121 enum {
123 };
124 
125 typedef struct PTCacheID {
126  struct PTCacheID *next, *prev;
127 
128  struct Scene *scene;
129  struct ID *owner_id;
130  void *calldata;
131  unsigned int type, file_type;
132  unsigned int stack_index;
133  unsigned int flag;
134 
135  unsigned int default_step;
136  unsigned int max_step;
137 
138  /* flags defined in DNA_object_force_types.h */
139  unsigned int data_types, info_types;
140 
141  /* copies point data to cache data */
142  int (*write_point)(int index, void *calldata, void **data, int cfra);
143  /* copies cache cata to point data */
144  void (*read_point)(int index, void *calldata, void **data, float cfra, const float *old_data);
145  /* interpolated between previously read point data and cache data */
146  void (*interpolate_point)(int index,
147  void *calldata,
148  void **data,
149  float cfra,
150  float cfra1,
151  float cfra2,
152  const float *old_data);
153 
154  /* copies point data to cache data */
156  /* copies cache cata to point data */
158 
159  /* copies custom extradata to cache data */
160  void (*write_extra_data)(void *calldata, struct PTCacheMem *pm, int cfra);
161  /* copies custom extradata to cache data */
162  void (*read_extra_data)(void *calldata, struct PTCacheMem *pm, float cfra);
163  /* copies custom extradata to cache data */
165  void *calldata, struct PTCacheMem *pm, float cfra, float cfra1, float cfra2);
166 
167  /* Total number of simulated points
168  * (the cfra parameter is just for using same function pointer with totwrite). */
169  int (*totpoint)(void *calldata, int cfra);
170  /* report error if number of points does not match */
171  void (*error)(const struct ID *owner_id, void *calldata, const char *message);
172  /* number of points written for current cache frame */
173  int (*totwrite)(void *calldata, int cfra);
174 
177 
178  struct PointCache *cache;
179  /* used for setting the current cache from ptcaches list */
183 
184 typedef struct PTCacheBaker {
185  struct Main *bmain;
186  struct Scene *scene;
189  int bake;
190  int render;
193  struct PTCacheID pid;
194 
195  void (*update_progress)(void *data, float progress, int *cancel);
196  void *bake_job;
198 
199 /* PTCacheEditKey->flag */
200 #define PEK_SELECT 1
201 #define PEK_TAG 2
202 #define PEK_HIDE 4
203 #define PEK_USE_WCO 8
204 
205 typedef struct PTCacheEditKey {
206  float *co;
207  float *vel;
208  float *rot;
209  float *time;
210 
211  float world_co[3];
212  float ftime;
213  float length;
214  short flag;
216 
217 /* PTCacheEditPoint->flag */
218 #define PEP_TAG 1
219 #define PEP_EDIT_RECALC 2
220 #define PEP_TRANSFORM 4
221 #define PEP_HIDE 8
222 
223 typedef struct PTCacheEditPoint {
225  int totkey;
226  short flag;
228 
229 typedef struct PTCacheUndo {
231 
232  /* particles stuff */
234  struct KDTree_3d *emitter_field;
237 
238  /* cache stuff */
239  struct ListBase mem_cache;
240 
241  int totpoint;
242 
243  size_t undo_size;
245 
246 enum {
247  /* Modifier stack got evaluated during particle edit mode, need to copy
248  * new evaluated particles to the edit struct.
249  */
251 };
252 
253 typedef struct PTCacheEdit {
254  int flags;
255 
257 
258  struct PTCacheID pid;
259 
260  /* particles stuff */
265  struct KDTree_3d *emitter_field;
266  /* Localspace face centers and normals (average of its verts), from the derived mesh. */
269 
270  struct ParticleCacheKey **pathcache; /* path cache (runtime) */
272 
275 
276 /* Particle functions */
277 void BKE_ptcache_make_particle_key(struct ParticleKey *key, int index, void **data, float time);
278 
279 /**************** Creating ID's ****************************/
280 void BKE_ptcache_id_from_softbody(PTCacheID *pid, struct Object *ob, struct SoftBody *sb);
281 void BKE_ptcache_id_from_particles(PTCacheID *pid, struct Object *ob, struct ParticleSystem *psys);
282 void BKE_ptcache_id_from_cloth(PTCacheID *pid, struct Object *ob, struct ClothModifierData *clmd);
283 void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct FluidModifierData *fmd);
285  struct Object *ob,
286  struct DynamicPaintSurface *surface);
287 void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, struct Object *ob, struct RigidBodyWorld *rbw);
288 
289 PTCacheID BKE_ptcache_id_find(struct Object *ob, struct Scene *scene, struct PointCache *cache);
290 void BKE_ptcache_ids_from_object(struct ListBase *lb,
291  struct Object *ob,
292  struct Scene *scene,
293  int duplis);
294 
295 /****************** Query funcs ****************************/
296 
297 /* Check whether object has a point cache. */
298 bool BKE_ptcache_object_has(struct Scene *scene, struct Object *ob, int duplis);
299 
300 /***************** Global funcs ****************************/
301 void BKE_ptcache_remove(void);
302 
303 /************ ID specific functions ************************/
304 void BKE_ptcache_id_clear(PTCacheID *id, int mode, unsigned int cfra);
305 int BKE_ptcache_id_exist(PTCacheID *id, int cfra);
306 int BKE_ptcache_id_reset(struct Scene *scene, PTCacheID *id, int mode);
308  struct Scene *scene,
309  float cfra,
310  int *startframe,
311  int *endframe,
312  float *timescale);
313 int BKE_ptcache_object_reset(struct Scene *scene, struct Object *ob, int mode);
314 
316 
317 /*********** General cache reading/writing ******************/
318 
319 /* Size of cache data type. */
320 int BKE_ptcache_data_size(int data_type);
321 
322 /* Is point with index in memory cache */
323 int BKE_ptcache_mem_index_find(struct PTCacheMem *pm, unsigned int index);
324 
325 /* Memory cache read/write helpers. */
326 void BKE_ptcache_mem_pointers_init(struct PTCacheMem *pm, void *cur[BPHYS_TOT_DATA]);
328 int BKE_ptcache_mem_pointers_seek(int point_index,
329  struct PTCacheMem *pm,
330  void *cur[BPHYS_TOT_DATA]);
331 
332 /* Main cache reading call. */
333 int BKE_ptcache_read(PTCacheID *pid, float cfra, bool no_extrapolate_old);
334 
335 /* Main cache writing call. */
336 int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra);
337 
338 /******************* Allocate & free ***************/
339 struct PointCache *BKE_ptcache_add(struct ListBase *ptcaches);
341 void BKE_ptcache_free(struct PointCache *cache);
342 void BKE_ptcache_free_list(struct ListBase *ptcaches);
343 struct PointCache *BKE_ptcache_copy_list(struct ListBase *ptcaches_new,
344  const struct ListBase *ptcaches_old,
345  const int flag);
346 
347 /********************** Baking *********************/
348 
349 /* Bakes cache with cache_step sized jumps in time, not accurate but very fast. */
350 void BKE_ptcache_quick_cache_all(struct Main *bmain,
351  struct Scene *scene,
352  struct ViewLayer *view_layer);
353 
354 /* Bake cache or simulate to current frame with settings defined in the baker. */
355 void BKE_ptcache_bake(struct PTCacheBaker *baker);
356 
357 /* Convert disk cache to memory cache. */
358 void BKE_ptcache_disk_to_mem(struct PTCacheID *pid);
359 
360 /* Convert memory cache to disk cache. */
361 void BKE_ptcache_mem_to_disk(struct PTCacheID *pid);
362 
363 /* Convert disk cache to memory cache and vice versa. Clears the cache that was converted. */
364 void BKE_ptcache_toggle_disk_cache(struct PTCacheID *pid);
365 
366 /* Rename all disk cache files with a new name. Doesn't touch the actual content of the files. */
368  const char *name_src,
369  const char *name_dst);
370 
371 /* Loads simulation from external (disk) cache files. */
372 void BKE_ptcache_load_external(struct PTCacheID *pid);
373 
374 /* Set correct flags after successful simulation step */
375 void BKE_ptcache_validate(struct PointCache *cache, int framenr);
376 
377 /* Set correct flags after unsuccessful simulation step */
378 void BKE_ptcache_invalidate(struct PointCache *cache);
379 
380 /********************** .blend File I/O *********************/
381 
382 void BKE_ptcache_blend_write(struct BlendWriter *writer, struct ListBase *ptcaches);
383 void BKE_ptcache_blend_read_data(struct BlendDataReader *reader,
384  struct ListBase *ptcaches,
385  struct PointCache **ocache,
386  int force_disk);
387 
388 #ifdef __cplusplus
389 }
390 #endif
int BKE_ptcache_data_size(int data_type)
Definition: pointcache.c:1726
void BKE_ptcache_mem_pointers_incr(void *cur[BPHYS_TOT_DATA])
Definition: pointcache.c:1792
void BKE_ptcache_free_mem(struct ListBase *mem_cache)
Definition: pointcache.c:3087
void BKE_ptcache_ids_from_object(struct ListBase *lb, struct Object *ob, struct Scene *scene, int duplis)
Definition: pointcache.c:1278
int BKE_ptcache_id_exist(PTCacheID *id, int cfra)
Definition: pointcache.c:2765
struct PTCacheFile PTCacheFile
bool BKE_ptcache_object_has(struct Scene *scene, struct Object *ob, int duplis)
Definition: pointcache.c:1291
void BKE_ptcache_id_time(PTCacheID *pid, struct Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
Definition: pointcache.c:2796
void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, struct Object *ob, struct RigidBodyWorld *rbw)
Definition: pointcache.c:1078
struct PTCacheBaker PTCacheBaker
void BKE_ptcache_quick_cache_all(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer)
Definition: pointcache.c:3182
void BKE_ptcache_id_from_particles(PTCacheID *pid, struct Object *ob, struct ParticleSystem *psys)
Definition: pointcache.c:921
void BKE_ptcache_update_info(PTCacheID *pid)
Definition: pointcache.c:3712
void BKE_ptcache_validate(struct PointCache *cache, int framenr)
Definition: pointcache.c:3814
@ PT_CACHE_EDIT_UPDATE_PARTICLE_FROM_EVAL
void BKE_ptcache_bake(struct PTCacheBaker *baker)
Definition: pointcache.c:3215
void BKE_ptcache_toggle_disk_cache(struct PTCacheID *pid)
Definition: pointcache.c:3503
void BKE_ptcache_id_from_dynamicpaint(PTCacheID *pid, struct Object *ob, struct DynamicPaintSurface *surface)
Definition: pointcache.c:1040
struct PTCacheID PTCacheID
int BKE_ptcache_mem_pointers_seek(int point_index, struct PTCacheMem *pm, void *cur[BPHYS_TOT_DATA])
Definition: pointcache.c:1802
void BKE_ptcache_id_clear(PTCacheID *id, int mode, unsigned int cfra)
Definition: pointcache.c:2613
void BKE_ptcache_id_from_softbody(PTCacheID *pid, struct Object *ob, struct SoftBody *sb)
Definition: pointcache.c:885
struct PTCacheEditKey PTCacheEditKey
void BKE_ptcache_disk_to_mem(struct PTCacheID *pid)
Definition: pointcache.c:3452
struct PTCacheEdit PTCacheEdit
struct PointCache * BKE_ptcache_copy_list(struct ListBase *ptcaches_new, const struct ListBase *ptcaches_old, const int flag)
void BKE_ptcache_blend_read_data(struct BlendDataReader *reader, struct ListBase *ptcaches, struct PointCache **ocache, int force_disk)
Definition: pointcache.c:3921
void BKE_ptcache_remove(void)
Definition: pointcache.c:3027
int BKE_ptcache_id_reset(struct Scene *scene, PTCacheID *id, int mode)
Definition: pointcache.c:2893
struct PointCache * BKE_ptcache_add(struct ListBase *ptcaches)
Definition: pointcache.c:3072
void BKE_ptcache_load_external(struct PTCacheID *pid)
Definition: pointcache.c:3599
PTCacheID BKE_ptcache_id_find(struct Object *ob, struct Scene *scene, struct PointCache *cache)
Definition: pointcache.c:1120
int BKE_ptcache_read(PTCacheID *pid, float cfra, bool no_extrapolate_old)
Definition: pointcache.c:2301
int BKE_ptcache_mem_index_find(struct PTCacheMem *pm, unsigned int index)
Definition: pointcache.c:1747
void BKE_ptcache_make_particle_key(struct ParticleKey *key, int index, void **data, float time)
Definition: pointcache.c:270
void BKE_ptcache_id_from_cloth(PTCacheID *pid, struct Object *ob, struct ClothModifierData *clmd)
Definition: pointcache.c:983
int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
Definition: pointcache.c:2562
void BKE_ptcache_mem_to_disk(struct PTCacheID *pid)
Definition: pointcache.c:3476
void BKE_ptcache_free_list(struct ListBase *ptcaches)
Definition: pointcache.c:3110
int BKE_ptcache_object_reset(struct Scene *scene, struct Object *ob, int mode)
Definition: pointcache.c:2953
struct PTCacheEditPoint PTCacheEditPoint
void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct FluidModifierData *fmd)
Definition: pointcache.c:1023
void BKE_ptcache_free(struct PointCache *cache)
Definition: pointcache.c:3099
@ PTCACHE_FILE_PTCACHE
struct PTCacheData PTCacheData
void BKE_ptcache_invalidate(struct PointCache *cache)
Definition: pointcache.c:3821
struct PTCacheUndo PTCacheUndo
void BKE_ptcache_disk_cache_rename(struct PTCacheID *pid, const char *name_src, const char *name_dst)
Definition: pointcache.c:3547
void BKE_ptcache_mem_pointers_init(struct PTCacheMem *pm, void *cur[BPHYS_TOT_DATA])
Definition: pointcache.c:1782
void BKE_ptcache_blend_write(struct BlendWriter *writer, struct ListBase *ptcaches)
Definition: pointcache.c:3845
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
#define BPHYS_TOT_DATA
double time
Scene scene
struct @203::@204 surface
#define pf(_x, _i)
Prefetch 64.
Definition: gim_memory.h:48
Definition: DNA_ID.h:273
Definition: BKE_main.h:116
struct PTCacheID pid
struct Scene * scene
struct Main * bmain
void(* update_progress)(void *data, float progress, int *cancel)
struct Depsgraph * depsgraph
struct ViewLayer * view_layer
struct BoidData boids
float ave[3]
float vel[3]
float times[3]
float loc[3]
float rot[4]
unsigned int index
struct PTCacheEditKey * keys
ListBase pathcachebufs
struct ParticleSystemModifierData * psmd
struct ParticleCacheKey ** pathcache
float * emitter_cosnos
int * mirror_cache
PTCacheEditPoint * points
struct ParticleSystem * psys
struct ParticleSystemModifierData * psmd_eval
struct KDTree_3d * emitter_field
struct PTCacheID pid
struct ParticleSystem * psys_eval
void * cur[BPHYS_TOT_DATA]
struct PTCacheData data
unsigned int totpoint
unsigned int flag
unsigned int type
unsigned int data_types
struct PointCache ** cache_ptr
int(* write_stream)(PTCacheFile *pf, void *calldata)
int(* write_point)(int index, void *calldata, void **data, int cfra)
unsigned int data_types
struct ID * owner_id
unsigned int info_types
int(* write_header)(PTCacheFile *pf)
void(* read_point)(int index, void *calldata, void **data, float cfra, const float *old_data)
void * calldata
unsigned int type
struct Scene * scene
void(* interpolate_extra_data)(void *calldata, struct PTCacheMem *pm, float cfra, float cfra1, float cfra2)
unsigned int stack_index
struct ListBase * ptcaches
struct PTCacheID * prev
unsigned int flag
void(* read_extra_data)(void *calldata, struct PTCacheMem *pm, float cfra)
void(* interpolate_point)(int index, void *calldata, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
unsigned int default_step
int(* totpoint)(void *calldata, int cfra)
unsigned int file_type
int(* totwrite)(void *calldata, int cfra)
void(* write_extra_data)(void *calldata, struct PTCacheMem *pm, int cfra)
int(* read_stream)(PTCacheFile *pf, void *calldata)
unsigned int max_step
void(* error)(const struct ID *owner_id, void *calldata, const char *message)
struct PTCacheID * next
int(* read_header)(PTCacheFile *pf)
struct PointCache * cache
size_t undo_size
float * emitter_cosnos
struct ListBase mem_cache
struct KDTree_3d * emitter_field
struct PTCacheEditPoint * points
struct ParticleData * particles
struct ListBase mem_cache