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) 2007 by Janne Karhu.
17 * All rights reserved.
18 * Adaptive time step
19 * Classical SPH
20 * Copyright 2011-2012 AutoCRC
21 */
22
23 #pragma once
24
25 /** \file
26 * \ingroup bke
27 */
28
29 #include "BLI_buffer.h"
30 #include "BLI_utildefines.h"
31
32 #include "DNA_object_types.h"
33 #include "DNA_particle_types.h"
34
35 #include "BKE_customdata.h"
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 struct ParticleKey;
42 struct ParticleSettings;
43 struct ParticleSystem;
44 struct ParticleSystemModifierData;
45
46 struct BVHTreeRay;
47 struct BVHTreeRayHit;
48 struct CustomData_MeshMasks;
49 struct Depsgraph;
50 struct EdgeHash;
51 struct KDTree_3d;
52 struct LatticeDeformData;
53 struct LinkNode;
54 struct MCol;
55 struct MFace;
56 struct MTFace;
57 struct MVert;
58 struct Main;
59 struct ModifierData;
60 struct Object;
61 struct RNG;
62 struct Scene;
63
64 #define PARTICLE_COLLISION_MAX_COLLISIONS 10
65
66 #define PARTICLE_P \
67 ParticleData *pa; \
68 int p
69 #define LOOP_PARTICLES for (p = 0, pa = psys->particles; p < psys->totpart; p++, pa++)
70 #define LOOP_EXISTING_PARTICLES \
71 for (p = 0, pa = psys->particles; p < psys->totpart; p++, pa++) \
72 if (!(pa->flag & PARS_UNEXIST))
73 #define LOOP_SHOWN_PARTICLES \
74 for (p = 0, pa = psys->particles; p < psys->totpart; p++, pa++) \
75 if (!(pa->flag & (PARS_UNEXIST | PARS_NO_DISP)))
76 /* OpenMP: Can only advance one variable within loop definition. */
77 #define LOOP_DYNAMIC_PARTICLES \
78 for (p = 0; p < psys->totpart; p++) \
79 if ((pa = psys->particles + p)->state.time > 0.0f)
80
81 /* fast but sure way to get the modifier*/
82 #define PARTICLE_PSMD \
83 ParticleSystemModifierData *psmd = sim->psmd ? sim->psmd : psys_get_modifier(sim->ob, sim->psys)
84
85 /* common stuff that many particle functions need */
86 typedef struct ParticleSimulationData {
87 struct Depsgraph *depsgraph;
88 struct Scene *scene;
89 struct Object *ob;
90 struct ParticleSystem *psys;
91 struct ParticleSystemModifierData *psmd;
92 struct ListBase *colliders;
93 /* Courant number. This is used to implement an adaptive time step. Only the
94 * maximum value per time step is important. Only sph_integrate makes use of
95 * this at the moment. Other solvers could, too. */
96 float courant_num;
97 /* Only valid during dynamics_step(). */
98 struct RNG *rng;
99 } ParticleSimulationData;
100
101 typedef struct SPHData {
102 ParticleSystem *psys[10];
103 ParticleData *pa;
104 float mass;
105 struct EdgeHash *eh;
106 float *gravity;
107 float hfac;
108 /* Average distance to neighbors (other particles in the support domain),
109 * for calculating the Courant number (adaptive time step). */
110 int pass;
111 float element_size;
112 float flow[3];
113
114 /* Temporary thread-local buffer for springs created during this step. */
115 BLI_Buffer new_springs;
116
117 /* Integrator callbacks. This allows different SPH implementations. */
118 void (*force_cb)(void *sphdata_v, ParticleKey *state, float *force, float *impulse);
119 void (*density_cb)(void *rangedata_v, int index, const float co[3], float squared_dist);
120 } SPHData;
121
122 typedef struct ParticleTexture {
123 float ivel; /* used in reset */
124 float time, life, exist, size; /* used in init */
125 float damp, gravity, field; /* used in physics */
126 float length, clump, kink_freq, kink_amp, effector; /* used in path caching */
127 float rough1, rough2, roughe; /* used in path caching */
128 float twist; /* used in path caching */
129 } ParticleTexture;
130
131 typedef struct ParticleSeam {
132 float v0[3], v1[3];
133 float nor[3], dir[3], tan[3];
134 float length2;
135 } ParticleSeam;
136
137 typedef struct ParticleCacheKey {
138 float co[3];
139 float vel[3];
140 float rot[4];
141 float col[3];
142 float time;
143 int segments;
144 } ParticleCacheKey;
145
146 typedef struct ParticleThreadContext {
147 /* shared */
148 struct ParticleSimulationData sim;
149 struct Mesh *mesh;
150 struct Material *ma;
151
152 /* distribution */
153 struct KDTree_3d *tree;
154
155 struct ParticleSeam *seams;
156 int totseam;
157
158 float *jit, *jitoff, *weight;
159 float maxweight;
160 int *index, jitlevel;
161
162 int cfrom, distr;
163
164 struct ParticleData *tpars;
165
166 /* path caching */
167 bool editupdate;
168 int between, segments, extra_segments;
169 int totchild, totparent, parent_pass;
170
171 float cfra;
172
173 float *vg_length, *vg_clump, *vg_kink;
174 float *vg_rough1, *vg_rough2, *vg_roughe;
175 float *vg_effector;
176 float *vg_twist;
177
178 struct CurveMapping *clumpcurve;
179 struct CurveMapping *roughcurve;
180 struct CurveMapping *twistcurve;
181 } ParticleThreadContext;
182
183 typedef struct ParticleTask {
184 ParticleThreadContext *ctx;
185 struct RNG *rng, *rng_path;
186 int begin, end;
187 } ParticleTask;
188
189 typedef struct ParticleCollisionElement {
190 /* pointers to original data */
191 float *x[3], *v[3];
192
193 /* values interpolated from original data*/
194 float x0[3], x1[3], x2[3], p[3];
195
196 /* results for found intersection point */
197 float nor[3], vel[3], uv[2];
198
199 /* count of original data (1-4) */
200 int tot;
201
202 /* index of the collision face */
203 int index;
204
205 /* flags for inversed normal / particle already inside element at start */
206 short inv_nor, inside;
207 } ParticleCollisionElement;
208
209 /** Container for moving data between deflet_particle and particle_intersect_face. */
210 typedef struct ParticleCollision {
211 struct Object *current;
212 struct Object *hit;
213 struct Object *skip[PARTICLE_COLLISION_MAX_COLLISIONS + 1];
214 struct Object *emitter;
215
216 /** Collision modifier for current object. */
217 struct CollisionModifierData *md;
218
219 /** Time factor of previous collision, needed for subtracting face velocity. */
220 float f;
221 float fac1, fac2;
222
223 float cfra, old_cfra;
224
225 /** Original length of co2-co1, needed for collision time evaluation. */
226 float original_ray_length;
227
228 int skip_count;
229
230 ParticleCollisionElement pce;
231
232 /* total_time is the amount of time in this subframe
233 * inv_total_time is the opposite
234 * inv_timestep is the inverse of the amount of time in this frame */
235 float total_time, inv_total_time, inv_timestep;
236
237 float radius;
238 float co1[3], co2[3];
239 float ve1[3], ve2[3];
240
241 float acc[3], boid_z;
242
243 int boid;
244 } ParticleCollision;
245
246 typedef struct ParticleDrawData {
247 float *vdata, *vd; /* vertice data */
248 float *ndata, *nd; /* normal data */
249 float *cdata, *cd; /* color data */
250 float *vedata, *ved; /* velocity data */
251 float *ma_col;
252 int totpart, partsize;
253 int flag;
254 int totpoint, totve;
255 } ParticleDrawData;
256
257 #define PARTICLE_DRAW_DATA_UPDATED 1
258
259 #define PSYS_FRAND_COUNT 1024
260 extern unsigned int PSYS_FRAND_SEED_OFFSET[PSYS_FRAND_COUNT];
261 extern unsigned int PSYS_FRAND_SEED_MULTIPLIER[PSYS_FRAND_COUNT];
262 extern float PSYS_FRAND_BASE[PSYS_FRAND_COUNT];
263
264 void psys_init_rng(void);
265
psys_frand(ParticleSystem * psys,unsigned int seed)266 BLI_INLINE float psys_frand(ParticleSystem *psys, unsigned int seed)
267 {
268 /* XXX far from ideal, this simply scrambles particle random numbers a bit
269 * to avoid obvious correlations.
270 * Can't use previous psys->frand arrays because these require initialization
271 * inside psys_check_enabled, which wreaks havoc in multi-threaded depsgraph updates.
272 */
273 unsigned int offset = PSYS_FRAND_SEED_OFFSET[psys->seed % PSYS_FRAND_COUNT];
274 unsigned int multiplier = PSYS_FRAND_SEED_MULTIPLIER[psys->seed % PSYS_FRAND_COUNT];
275 return PSYS_FRAND_BASE[(offset + seed * multiplier) % PSYS_FRAND_COUNT];
276 }
277
psys_frand_vec(ParticleSystem * psys,unsigned int seed,float vec[3])278 BLI_INLINE void psys_frand_vec(ParticleSystem *psys, unsigned int seed, float vec[3])
279 {
280 unsigned int offset = PSYS_FRAND_SEED_OFFSET[psys->seed % PSYS_FRAND_COUNT];
281 unsigned int multiplier = PSYS_FRAND_SEED_MULTIPLIER[psys->seed % PSYS_FRAND_COUNT];
282 vec[0] = PSYS_FRAND_BASE[(offset + (seed + 0) * multiplier) % PSYS_FRAND_COUNT];
283 vec[1] = PSYS_FRAND_BASE[(offset + (seed + 1) * multiplier) % PSYS_FRAND_COUNT];
284 vec[2] = PSYS_FRAND_BASE[(offset + (seed + 2) * multiplier) % PSYS_FRAND_COUNT];
285 }
286
287 /* ----------- functions needed outside particlesystem ---------------- */
288 /* particle.c */
289 int count_particles(struct ParticleSystem *psys);
290 int count_particles_mod(struct ParticleSystem *psys, int totgr, int cur);
291
292 int psys_get_child_number(struct Scene *scene,
293 struct ParticleSystem *psys,
294 const bool use_render_params);
295 int psys_get_tot_child(struct Scene *scene,
296 struct ParticleSystem *psys,
297 const bool use_render_params);
298
299 struct ParticleSystem *psys_get_current(struct Object *ob);
300 /* for rna */
301 short psys_get_current_num(struct Object *ob);
302 void psys_set_current_num(Object *ob, int index);
303 /* UNUSED */
304 // struct Object *psys_find_object(struct Scene *scene, struct ParticleSystem *psys);
305
306 struct LatticeDeformData *psys_create_lattice_deform_data(struct ParticleSimulationData *sim);
307
308 /* For a given evaluated particle system get its original.
309 *
310 * If this input is an original particle system already, the return value is the
311 * same as the input. */
312 struct ParticleSystem *psys_orig_get(struct ParticleSystem *psys);
313
314 /* For a given original object and its particle system, get evaluated particle
315 * system within a given dependency graph. */
316 struct ParticleSystem *psys_eval_get(struct Depsgraph *depsgraph,
317 struct Object *object,
318 struct ParticleSystem *psys);
319
320 bool psys_in_edit_mode(struct Depsgraph *depsgraph, const struct ParticleSystem *psys);
321 bool psys_check_enabled(struct Object *ob,
322 struct ParticleSystem *psys,
323 const bool use_render_params);
324 bool psys_check_edited(struct ParticleSystem *psys);
325
326 void psys_find_group_weights(struct ParticleSettings *part);
327 void psys_check_group_weights(struct ParticleSettings *part);
328 int psys_uses_gravity(struct ParticleSimulationData *sim);
329 void BKE_particlesettings_fluid_default_settings(struct ParticleSettings *part);
330
331 /* free */
332 void psys_free_path_cache(struct ParticleSystem *psys, struct PTCacheEdit *edit);
333 void psys_free(struct Object *ob, struct ParticleSystem *psys);
334
335 /* Copy. */
336 void psys_copy_particles(struct ParticleSystem *psys_dst, struct ParticleSystem *psys_src);
337
338 bool psys_render_simplify_params(struct ParticleSystem *psys,
339 struct ChildParticle *cpa,
340 float *params);
341
342 void psys_interpolate_uvs(const struct MTFace *tface, int quad, const float w[4], float uvco[2]);
343 void psys_interpolate_mcol(const struct MCol *mcol, int quad, const float w[4], struct MCol *mc);
344
345 void copy_particle_key(struct ParticleKey *to, struct ParticleKey *from, int time);
346
347 void psys_emitter_customdata_mask(struct ParticleSystem *psys,
348 struct CustomData_MeshMasks *r_cddata_masks);
349 void psys_particle_on_emitter(struct ParticleSystemModifierData *psmd,
350 int from,
351 int index,
352 int index_dmcache,
353 float fuv[4],
354 float foffset,
355 float vec[3],
356 float nor[3],
357 float utan[3],
358 float vtan[3],
359 float orco[3]);
360 struct ParticleSystemModifierData *psys_get_modifier(struct Object *ob,
361 struct ParticleSystem *psys);
362
363 struct ModifierData *object_add_particle_system(struct Main *bmain,
364 struct Scene *scene,
365 struct Object *ob,
366 const char *name);
367 struct ModifierData *object_copy_particle_system(struct Main *bmain,
368 struct Scene *scene,
369 struct Object *ob,
370 const struct ParticleSystem *psys_orig);
371 void object_remove_particle_system(struct Main *bmain, struct Scene *scene, struct Object *ob);
372 struct ParticleSettings *BKE_particlesettings_add(struct Main *bmain, const char *name);
373 void psys_reset(struct ParticleSystem *psys, int mode);
374
375 void psys_find_parents(struct ParticleSimulationData *sim, const bool use_render_params);
376
377 void psys_unique_name(struct Object *object, struct ParticleSystem *psys, const char *defname);
378
379 void psys_cache_paths(struct ParticleSimulationData *sim,
380 float cfra,
381 const bool use_render_params);
382 void psys_cache_edit_paths(struct Depsgraph *depsgraph,
383 struct Scene *scene,
384 struct Object *ob,
385 struct PTCacheEdit *edit,
386 float cfra,
387 const bool use_render_params);
388 void psys_cache_child_paths(struct ParticleSimulationData *sim,
389 float cfra,
390 const bool editupdate,
391 const bool use_render_params);
392 int do_guides(struct Depsgraph *depsgraph,
393 struct ParticleSettings *part,
394 struct ListBase *effectors,
395 ParticleKey *state,
396 int index,
397 float time);
398 void precalc_guides(struct ParticleSimulationData *sim, struct ListBase *effectors);
399 float psys_get_timestep(struct ParticleSimulationData *sim);
400 float psys_get_child_time(struct ParticleSystem *psys,
401 struct ChildParticle *cpa,
402 float cfra,
403 float *birthtime,
404 float *dietime);
405 float psys_get_child_size(struct ParticleSystem *psys,
406 struct ChildParticle *cpa,
407 float cfra,
408 float *pa_time);
409 void psys_get_particle_on_path(struct ParticleSimulationData *sim,
410 int pa_num,
411 struct ParticleKey *state,
412 const bool vel);
413 int psys_get_particle_state(struct ParticleSimulationData *sim,
414 int p,
415 struct ParticleKey *state,
416 int always);
417
418 /* child paths */
419 void BKE_particlesettings_clump_curve_init(struct ParticleSettings *part);
420 void BKE_particlesettings_rough_curve_init(struct ParticleSettings *part);
421 void BKE_particlesettings_twist_curve_init(struct ParticleSettings *part);
422 void psys_apply_child_modifiers(struct ParticleThreadContext *ctx,
423 struct ListBase *modifiers,
424 struct ChildParticle *cpa,
425 struct ParticleTexture *ptex,
426 const float orco[3],
427 float hairmat[4][4],
428 struct ParticleCacheKey *keys,
429 struct ParticleCacheKey *parent_keys,
430 const float parent_orco[3]);
431
432 void psys_sph_init(struct ParticleSimulationData *sim, struct SPHData *sphdata);
433 void psys_sph_finalize(struct SPHData *sphdata);
434 void psys_sph_density(struct BVHTree *tree, struct SPHData *data, float co[3], float vars[2]);
435
436 /* for anim.c */
437 void psys_get_dupli_texture(struct ParticleSystem *psys,
438 struct ParticleSettings *part,
439 struct ParticleSystemModifierData *psmd,
440 struct ParticleData *pa,
441 struct ChildParticle *cpa,
442 float uv[2],
443 float orco[3]);
444 void psys_get_dupli_path_transform(struct ParticleSimulationData *sim,
445 struct ParticleData *pa,
446 struct ChildParticle *cpa,
447 struct ParticleCacheKey *cache,
448 float mat[4][4],
449 float *scale);
450
451 void psys_thread_context_init(struct ParticleThreadContext *ctx,
452 struct ParticleSimulationData *sim);
453 void psys_thread_context_free(struct ParticleThreadContext *ctx);
454 void psys_tasks_create(struct ParticleThreadContext *ctx,
455 int startpart,
456 int endpart,
457 struct ParticleTask **r_tasks,
458 int *r_numtasks);
459 void psys_tasks_free(struct ParticleTask *tasks, int numtasks);
460
461 void psys_apply_hair_lattice(struct Depsgraph *depsgraph,
462 struct Scene *scene,
463 struct Object *ob,
464 struct ParticleSystem *psys);
465
466 /* particle_system.c */
467 struct ParticleSystem *psys_get_target_system(struct Object *ob, struct ParticleTarget *pt);
468 void psys_count_keyed_targets(struct ParticleSimulationData *sim);
469 void psys_update_particle_tree(struct ParticleSystem *psys, float cfra);
470 void psys_changed_type(struct Object *ob, struct ParticleSystem *psys);
471
472 void psys_make_temp_pointcache(struct Object *ob, struct ParticleSystem *psys);
473 void psys_get_pointcache_start_end(struct Scene *scene,
474 ParticleSystem *psys,
475 int *sfra,
476 int *efra);
477
478 void psys_check_boid_data(struct ParticleSystem *psys);
479
480 void psys_get_birth_coords(struct ParticleSimulationData *sim,
481 struct ParticleData *pa,
482 struct ParticleKey *state,
483 float dtime,
484 float cfra);
485
486 void particle_system_update(struct Depsgraph *depsgraph,
487 struct Scene *scene,
488 struct Object *ob,
489 struct ParticleSystem *psys,
490 const bool use_render_params);
491
492 /* Callback format for performing operations on ID-pointers for particle systems */
493 typedef void (*ParticleSystemIDFunc)(struct ParticleSystem *psys,
494 struct ID **idpoin,
495 void *userdata,
496 int cb_flag);
497
498 void BKE_particlesystem_id_loop(struct ParticleSystem *psys,
499 ParticleSystemIDFunc func,
500 void *userdata);
501
502 /* Reset all particle systems in the given object. */
503 void BKE_particlesystem_reset_all(struct Object *object);
504
505 /* ----------- functions needed only inside particlesystem ------------ */
506 /* particle.c */
507 void psys_disable_all(struct Object *ob);
508 void psys_enable_all(struct Object *ob);
509
510 void free_hair(struct Object *ob, struct ParticleSystem *psys, int dynamics);
511 void free_keyed_keys(struct ParticleSystem *psys);
512 void psys_free_particles(struct ParticleSystem *psys);
513 void psys_free_children(struct ParticleSystem *psys);
514
515 void psys_interpolate_particle(
516 short type, struct ParticleKey keys[4], float dt, struct ParticleKey *result, bool velocity);
517 void psys_vec_rot_to_face(struct Mesh *mesh, struct ParticleData *pa, float vec[3]);
518 void psys_mat_hair_to_object(struct Object *ob,
519 struct Mesh *mesh,
520 short from,
521 struct ParticleData *pa,
522 float hairmat[4][4]);
523 void psys_mat_hair_to_global(struct Object *ob,
524 struct Mesh *mesh,
525 short from,
526 struct ParticleData *pa,
527 float hairmat[4][4]);
528 void psys_mat_hair_to_orco(struct Object *ob,
529 struct Mesh *mesh,
530 short from,
531 struct ParticleData *pa,
532 float hairmat[4][4]);
533
534 float psys_get_dietime_from_cache(struct PointCache *cache, int index);
535
536 void psys_free_pdd(struct ParticleSystem *psys);
537
538 float *psys_cache_vgroup(struct Mesh *mesh, struct ParticleSystem *psys, int vgroup);
539 void psys_get_texture(struct ParticleSimulationData *sim,
540 struct ParticleData *pa,
541 struct ParticleTexture *ptex,
542 int event,
543 float cfra);
544 void psys_interpolate_face(struct MVert *mvert,
545 struct MFace *mface,
546 struct MTFace *tface,
547 float (*orcodata)[3],
548 float w[4],
549 float vec[3],
550 float nor[3],
551 float utan[3],
552 float vtan[3],
553 float orco[3]);
554 float psys_particle_value_from_verts(struct Mesh *mesh,
555 short from,
556 struct ParticleData *pa,
557 float *values);
558 void psys_get_from_key(
559 struct ParticleKey *key, float loc[3], float vel[3], float rot[4], float *time);
560
561 /* BLI_bvhtree_ray_cast callback */
562 void BKE_psys_collision_neartest_cb(void *userdata,
563 int index,
564 const struct BVHTreeRay *ray,
565 struct BVHTreeRayHit *hit);
566 void psys_particle_on_dm(struct Mesh *mesh_final,
567 int from,
568 int index,
569 int index_dmcache,
570 const float fw[4],
571 float foffset,
572 float vec[3],
573 float nor[3],
574 float utan[3],
575 float vtan[3],
576 float orco[3]);
577
578 /* particle_system.c */
579 void distribute_particles(struct ParticleSimulationData *sim, int from);
580 void init_particle(struct ParticleSimulationData *sim, struct ParticleData *pa);
581 void psys_calc_dmcache(struct Object *ob,
582 struct Mesh *mesh_final,
583 struct Mesh *mesh_original,
584 struct ParticleSystem *psys);
585 int psys_particle_dm_face_lookup(struct Mesh *mesh_final,
586 struct Mesh *mesh_original,
587 int findex,
588 const float fw[4],
589 struct LinkNode **poly_nodes);
590
591 void reset_particle(struct ParticleSimulationData *sim,
592 struct ParticleData *pa,
593 float dtime,
594 float cfra);
595
596 float psys_get_current_display_percentage(struct ParticleSystem *psys,
597 const bool use_render_params);
598
599 /* psys_reset */
600 #define PSYS_RESET_ALL 1
601 #define PSYS_RESET_DEPSGRAPH 2
602 /* #define PSYS_RESET_CHILDREN 3 */ /*UNUSED*/
603 #define PSYS_RESET_CACHE_MISS 4
604
605 /* index_dmcache */
606 #define DMCACHE_NOTFOUND -1
607 #define DMCACHE_ISCHILD -2
608
609 /* **** Depsgraph evaluation **** */
610
611 struct Depsgraph;
612
613 void BKE_particle_settings_eval_reset(struct Depsgraph *depsgraph,
614 struct ParticleSettings *particle_settings);
615
616 void BKE_particle_system_eval_init(struct Depsgraph *depsgraph, struct Object *object);
617
618 /* Draw Cache */
619 enum {
620 BKE_PARTICLE_BATCH_DIRTY_ALL = 0,
621 };
622 void BKE_particle_batch_cache_dirty_tag(struct ParticleSystem *psys, int mode);
623 void BKE_particle_batch_cache_free(struct ParticleSystem *psys);
624
625 extern void (*BKE_particle_batch_cache_dirty_tag_cb)(struct ParticleSystem *psys, int mode);
626 extern void (*BKE_particle_batch_cache_free_cb)(struct ParticleSystem *psys);
627
628 #ifdef __cplusplus
629 }
630 #endif
631