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 
22 /** \file
23  * \ingroup bke
24  */
25 
26 #include "DNA_ID.h"
27 #include "DNA_boid_types.h"
28 #include "DNA_dynamicpaint_types.h"
29 #include "DNA_object_force_types.h"
30 #include "DNA_pointcache_types.h"
31 #include <stdio.h> /* for FILE */
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /* Point cache clearing option, for BKE_ptcache_id_clear, before
38  * and after are non inclusive (they wont remove the cfra) */
39 #define PTCACHE_CLEAR_ALL 0
40 #define PTCACHE_CLEAR_FRAME 1
41 #define PTCACHE_CLEAR_BEFORE 2
42 #define PTCACHE_CLEAR_AFTER 3
43 
44 /* Point cache reset options */
45 #define PTCACHE_RESET_DEPSGRAPH 0
46 #define PTCACHE_RESET_BAKED 1
47 #define PTCACHE_RESET_OUTDATED 2
48 /* #define PTCACHE_RESET_FREE           3 */ /*UNUSED*/
49 
50 /* Add the blendfile name after blendcache_ */
51 #define PTCACHE_EXT ".bphys"
52 #define PTCACHE_PATH "blendcache_"
53 
54 /* File open options, for BKE_ptcache_file_open */
55 #define PTCACHE_FILE_READ 0
56 #define PTCACHE_FILE_WRITE 1
57 #define PTCACHE_FILE_UPDATE 2
58 
59 /* PTCacheID types */
60 #define PTCACHE_TYPE_SOFTBODY 0
61 #define PTCACHE_TYPE_PARTICLES 1
62 #define PTCACHE_TYPE_CLOTH 2
63 #define PTCACHE_TYPE_SMOKE_DOMAIN 3
64 #define PTCACHE_TYPE_SMOKE_HIGHRES 4
65 #define PTCACHE_TYPE_DYNAMICPAINT 5
66 #define PTCACHE_TYPE_RIGIDBODY 6
67 #define PTCACHE_TYPE_SIM_PARTICLES 7
68 
69 /* high bits reserved for flags that need to be stored in file */
70 #define PTCACHE_TYPEFLAG_COMPRESS (1 << 16)
71 #define PTCACHE_TYPEFLAG_EXTRADATA (1 << 17)
72 
73 #define PTCACHE_TYPEFLAG_TYPEMASK 0x0000FFFF
74 #define PTCACHE_TYPEFLAG_FLAGMASK 0xFFFF0000
75 
76 /* PTCache read return code */
77 #define PTCACHE_READ_EXACT 1
78 #define PTCACHE_READ_INTERPOLATED 2
79 #define PTCACHE_READ_OLD 3
80 
81 /* Structs */
82 struct ClothModifierData;
83 struct FluidModifierData;
84 struct ListBase;
85 struct Main;
86 struct Object;
87 struct ParticleKey;
88 struct ParticleSystem;
89 struct PointCache;
90 struct RigidBodyWorld;
91 struct Scene;
92 struct SoftBody;
93 struct ViewLayer;
94 
95 /* temp structure for read/write */
96 typedef struct PTCacheData {
97   unsigned int index;
98   float loc[3];
99   float vel[3];
100   float rot[4];
101   float ave[3];
102   float size;
103   float times[3];
104   struct BoidData boids;
105 } PTCacheData;
106 
107 typedef struct PTCacheFile {
108   FILE *fp;
109 
110   int frame, old_format;
111   unsigned int totpoint, type;
112   unsigned int data_types, flag;
113 
114   struct PTCacheData data;
115   void *cur[BPHYS_TOT_DATA];
116 } PTCacheFile;
117 
118 #define PTCACHE_VEL_PER_SEC 1
119 
120 enum {
121   PTCACHE_FILE_PTCACHE = 0,
122 };
123 
124 typedef struct PTCacheID {
125   struct PTCacheID *next, *prev;
126 
127   struct Scene *scene;
128   struct ID *owner_id;
129   void *calldata;
130   unsigned int type, file_type;
131   unsigned int stack_index;
132   unsigned int flag;
133 
134   unsigned int default_step;
135   unsigned int max_step;
136 
137   /* flags defined in DNA_object_force_types.h */
138   unsigned int data_types, info_types;
139 
140   /* copies point data to cache data */
141   int (*write_point)(int index, void *calldata, void **data, int cfra);
142   /* copies cache cata to point data */
143   void (*read_point)(int index, void *calldata, void **data, float cfra, const float *old_data);
144   /* interpolated between previously read point data and cache data */
145   void (*interpolate_point)(int index,
146                             void *calldata,
147                             void **data,
148                             float cfra,
149                             float cfra1,
150                             float cfra2,
151                             const float *old_data);
152 
153   /* copies point data to cache data */
154   int (*write_stream)(PTCacheFile *pf, void *calldata);
155   /* copies cache cata to point data */
156   int (*read_stream)(PTCacheFile *pf, void *calldata);
157 
158   /* copies custom extradata to cache data */
159   void (*write_extra_data)(void *calldata, struct PTCacheMem *pm, int cfra);
160   /* copies custom extradata to cache data */
161   void (*read_extra_data)(void *calldata, struct PTCacheMem *pm, float cfra);
162   /* copies custom extradata to cache data */
163   void (*interpolate_extra_data)(
164       void *calldata, struct PTCacheMem *pm, float cfra, float cfra1, float cfra2);
165 
166   /* Total number of simulated points
167    * (the cfra parameter is just for using same function pointer with totwrite). */
168   int (*totpoint)(void *calldata, int cfra);
169   /* report error if number of points does not match */
170   void (*error)(void *calldata, const char *message);
171   /* number of points written for current cache frame */
172   int (*totwrite)(void *calldata, int cfra);
173 
174   int (*write_header)(PTCacheFile *pf);
175   int (*read_header)(PTCacheFile *pf);
176 
177   struct PointCache *cache;
178   /* used for setting the current cache from ptcaches list */
179   struct PointCache **cache_ptr;
180   struct ListBase *ptcaches;
181 } PTCacheID;
182 
183 typedef struct PTCacheBaker {
184   struct Main *bmain;
185   struct Scene *scene;
186   struct ViewLayer *view_layer;
187   struct Depsgraph *depsgraph;
188   int bake;
189   int render;
190   int anim_init;
191   int quick_step;
192   struct PTCacheID pid;
193 
194   void (*update_progress)(void *data, float progress, int *cancel);
195   void *bake_job;
196 } PTCacheBaker;
197 
198 /* PTCacheEditKey->flag */
199 #define PEK_SELECT 1
200 #define PEK_TAG 2
201 #define PEK_HIDE 4
202 #define PEK_USE_WCO 8
203 
204 typedef struct PTCacheEditKey {
205   float *co;
206   float *vel;
207   float *rot;
208   float *time;
209 
210   float world_co[3];
211   float ftime;
212   float length;
213   short flag;
214 } PTCacheEditKey;
215 
216 /* PTCacheEditPoint->flag */
217 #define PEP_TAG 1
218 #define PEP_EDIT_RECALC 2
219 #define PEP_TRANSFORM 4
220 #define PEP_HIDE 8
221 
222 typedef struct PTCacheEditPoint {
223   struct PTCacheEditKey *keys;
224   int totkey;
225   short flag;
226 } PTCacheEditPoint;
227 
228 typedef struct PTCacheUndo {
229   struct PTCacheEditPoint *points;
230 
231   /* particles stuff */
232   struct ParticleData *particles;
233   struct KDTree_3d *emitter_field;
234   float *emitter_cosnos;
235   int psys_flag;
236 
237   /* cache stuff */
238   struct ListBase mem_cache;
239 
240   int totpoint;
241 
242   size_t undo_size;
243 } PTCacheUndo;
244 
245 enum {
246   /* Modifier stack got evaluated during particle edit mode, need to copy
247    * new evaluated particles to the edit struct.
248    */
249   PT_CACHE_EDIT_UPDATE_PARTICLE_FROM_EVAL = (1 << 0),
250 };
251 
252 typedef struct PTCacheEdit {
253   int flags;
254 
255   PTCacheEditPoint *points;
256 
257   struct PTCacheID pid;
258 
259   /* particles stuff */
260   struct ParticleSystem *psys;
261   struct ParticleSystem *psys_eval;
262   struct ParticleSystemModifierData *psmd;
263   struct ParticleSystemModifierData *psmd_eval;
264   struct KDTree_3d *emitter_field;
265   /* Localspace face centers and normals (average of its verts), from the derived mesh. */
266   float *emitter_cosnos;
267   int *mirror_cache;
268 
269   struct ParticleCacheKey **pathcache; /* path cache (runtime) */
270   ListBase pathcachebufs;
271 
272   int totpoint, totframes, totcached, edited;
273 } PTCacheEdit;
274 
275 /* Particle functions */
276 void BKE_ptcache_make_particle_key(struct ParticleKey *key, int index, void **data, float time);
277 
278 /**************** Creating ID's ****************************/
279 void BKE_ptcache_id_from_softbody(PTCacheID *pid, struct Object *ob, struct SoftBody *sb);
280 void BKE_ptcache_id_from_particles(PTCacheID *pid, struct Object *ob, struct ParticleSystem *psys);
281 void BKE_ptcache_id_from_cloth(PTCacheID *pid, struct Object *ob, struct ClothModifierData *clmd);
282 void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct FluidModifierData *fmd);
283 void BKE_ptcache_id_from_dynamicpaint(PTCacheID *pid,
284                                       struct Object *ob,
285                                       struct DynamicPaintSurface *surface);
286 void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, struct Object *ob, struct RigidBodyWorld *rbw);
287 
288 PTCacheID BKE_ptcache_id_find(struct Object *ob, struct Scene *scene, struct PointCache *cache);
289 void BKE_ptcache_ids_from_object(struct ListBase *lb,
290                                  struct Object *ob,
291                                  struct Scene *scene,
292                                  int duplis);
293 
294 /****************** Query funcs ****************************/
295 
296 /* Check whether object has a point cache. */
297 bool BKE_ptcache_object_has(struct Scene *scene, struct Object *ob, int duplis);
298 
299 /***************** Global funcs ****************************/
300 void BKE_ptcache_remove(void);
301 
302 /************ ID specific functions ************************/
303 void BKE_ptcache_id_clear(PTCacheID *id, int mode, unsigned int cfra);
304 int BKE_ptcache_id_exist(PTCacheID *id, int cfra);
305 int BKE_ptcache_id_reset(struct Scene *scene, PTCacheID *id, int mode);
306 void BKE_ptcache_id_time(PTCacheID *pid,
307                          struct Scene *scene,
308                          float cfra,
309                          int *startframe,
310                          int *endframe,
311                          float *timescale);
312 int BKE_ptcache_object_reset(struct Scene *scene, struct Object *ob, int mode);
313 
314 void BKE_ptcache_update_info(PTCacheID *pid);
315 
316 /*********** General cache reading/writing ******************/
317 
318 /* Size of cache data type. */
319 int BKE_ptcache_data_size(int data_type);
320 
321 /* Is point with index in memory cache */
322 int BKE_ptcache_mem_index_find(struct PTCacheMem *pm, unsigned int index);
323 
324 /* Memory cache read/write helpers. */
325 void BKE_ptcache_mem_pointers_init(struct PTCacheMem *pm, void *cur[BPHYS_TOT_DATA]);
326 void BKE_ptcache_mem_pointers_incr(void *cur[BPHYS_TOT_DATA]);
327 int BKE_ptcache_mem_pointers_seek(int point_index,
328                                   struct PTCacheMem *pm,
329                                   void *cur[BPHYS_TOT_DATA]);
330 
331 /* Main cache reading call. */
332 int BKE_ptcache_read(PTCacheID *pid, float cfra, bool no_extrapolate_old);
333 
334 /* Main cache writing call. */
335 int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra);
336 
337 /******************* Allocate & free ***************/
338 struct PointCache *BKE_ptcache_add(struct ListBase *ptcaches);
339 void BKE_ptcache_free_mem(struct ListBase *mem_cache);
340 void BKE_ptcache_free(struct PointCache *cache);
341 void BKE_ptcache_free_list(struct ListBase *ptcaches);
342 struct PointCache *BKE_ptcache_copy_list(struct ListBase *ptcaches_new,
343                                          const struct ListBase *ptcaches_old,
344                                          const int flag);
345 
346 /********************** Baking *********************/
347 
348 /* Bakes cache with cache_step sized jumps in time, not accurate but very fast. */
349 void BKE_ptcache_quick_cache_all(struct Main *bmain,
350                                  struct Scene *scene,
351                                  struct ViewLayer *view_layer);
352 
353 /* Bake cache or simulate to current frame with settings defined in the baker. */
354 void BKE_ptcache_bake(struct PTCacheBaker *baker);
355 
356 /* Convert disk cache to memory cache. */
357 void BKE_ptcache_disk_to_mem(struct PTCacheID *pid);
358 
359 /* Convert memory cache to disk cache. */
360 void BKE_ptcache_mem_to_disk(struct PTCacheID *pid);
361 
362 /* Convert disk cache to memory cache and vice versa. Clears the cache that was converted. */
363 void BKE_ptcache_toggle_disk_cache(struct PTCacheID *pid);
364 
365 /* Rename all disk cache files with a new name. Doesn't touch the actual content of the files. */
366 void BKE_ptcache_disk_cache_rename(struct PTCacheID *pid,
367                                    const char *name_src,
368                                    const char *name_dst);
369 
370 /* Loads simulation from external (disk) cache files. */
371 void BKE_ptcache_load_external(struct PTCacheID *pid);
372 
373 /* Set correct flags after successful simulation step */
374 void BKE_ptcache_validate(struct PointCache *cache, int framenr);
375 
376 /* Set correct flags after unsuccessful simulation step */
377 void BKE_ptcache_invalidate(struct PointCache *cache);
378 
379 #ifdef __cplusplus
380 }
381 #endif
382