Blender V5.3
particle_distribute.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 by Janne Karhu. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstring>
11
12#include "MEM_guardedalloc.h"
13
14#include "BLI_jitter_2d.hh"
15#include "BLI_kdtree.hh"
16#include "BLI_math_geom_c.hh"
17#include "BLI_rand_c.hh"
18#include "BLI_sort.hh"
19#include "BLI_task_c.hh"
20#include "BLI_utildefines.hh"
21
22#include "DNA_mesh_types.h"
23#include "DNA_meshdata_types.h"
24#include "DNA_modifier_types.h"
25#include "DNA_particle_types.h"
26
27#include "BKE_customdata.hh"
28#include "BKE_global.hh"
29#include "BKE_lib_id.hh"
30#include "BKE_mesh.hh"
32#include "BKE_particle.h"
33
35
36namespace blender {
37
38static void alloc_child_particles(ParticleSystem *psys, int tot)
39{
40 if (psys->child) {
41 /* only re-allocate if we have to */
42 if (psys->part->childtype && psys->totchild == tot) {
43 std::fill_n(psys->child, tot, ChildParticle{});
44 return;
45 }
46
47 MEM_delete(psys->child);
48 psys->child = nullptr;
49 psys->totchild = 0;
50 }
51
52 if (psys->part->childtype) {
53 psys->totchild = tot;
54 if (psys->totchild) {
55 psys->child = MEM_new_array_zeroed<ChildParticle>(psys->totchild, "child_particles");
56 }
57 }
58}
59
61 Object *ob,
62 Mesh *final_mesh,
63 Mesh *deform_mesh,
64 ParticleSystem *psys,
65 const bool use_render_params)
66{
67 ChildParticle *cpa = nullptr;
68 int i, p;
69 const int child_num = psys_get_child_number(scene, psys, use_render_params);
70 const int totpart = psys_get_tot_child(scene, psys, use_render_params);
71 RNG *rng = BLI_rng_new_srandom(31415926 + psys->seed + psys->child_seed);
72
73 alloc_child_particles(psys, totpart);
74
75 cpa = psys->child;
76 for (i = 0; i < child_num; i++) {
77 for (p = 0; p < psys->totpart; p++, cpa++) {
78 float length = 2.0;
79 cpa->parent = p;
80
81 /* create even spherical distribution inside unit sphere */
82 while (length >= 1.0f) {
83 cpa->fuv[0] = 2.0f * BLI_rng_get_float(rng) - 1.0f;
84 cpa->fuv[1] = 2.0f * BLI_rng_get_float(rng) - 1.0f;
85 cpa->fuv[2] = 2.0f * BLI_rng_get_float(rng) - 1.0f;
86 length = len_v3(cpa->fuv);
87 }
88
89 cpa->num = -1;
90 }
91 }
92 /* dmcache must be updated for parent particles if children from faces is used */
93 psys_calc_dmcache(ob, final_mesh, deform_mesh, psys);
94
95 BLI_rng_free(rng);
96}
98{
99 ParticleData *pa = nullptr;
100 float min[3], max[3], delta[3], d;
101 const Span<float3> positions = mesh->vert_positions();
102 int totvert = mesh->verts_num, from = psys->part->from;
103 int i, j, k, p, res = psys->part->grid_res, size[3], axis;
104
105 /* find bounding box of dm */
106 if (totvert > 0) {
108 for (i = 1; i < totvert; i++) {
109 minmax_v3v3_v3(min, max, positions[i]);
110 }
111 }
112 else {
113 zero_v3(min);
114 zero_v3(max);
115 }
116
117 sub_v3_v3v3(delta, max, min);
118
119 /* determine major axis */
120 axis = axis_dominant_v3_single(delta);
121
122 d = delta[axis] / float(res);
123
124 size[axis] = res;
125 size[(axis + 1) % 3] = int(ceil(delta[(axis + 1) % 3] / d));
126 size[(axis + 2) % 3] = int(ceil(delta[(axis + 2) % 3] / d));
127
128 /* float errors grrr. */
129 size[(axis + 1) % 3] = std::min(size[(axis + 1) % 3], res);
130 size[(axis + 2) % 3] = std::min(size[(axis + 2) % 3], res);
131
132 size[0] = std::max(size[0], 1);
133 size[1] = std::max(size[1], 1);
134 size[2] = std::max(size[2], 1);
135
136 /* no full offset for flat/thin objects */
137 min[0] += d < delta[0] ? d / 2.0f : delta[0] / 2.0f;
138 min[1] += d < delta[1] ? d / 2.0f : delta[1] / 2.0f;
139 min[2] += d < delta[2] ? d / 2.0f : delta[2] / 2.0f;
140
141 for (i = 0, p = 0, pa = psys->particles; i < res; i++) {
142 for (j = 0; j < res; j++) {
143 for (k = 0; k < res; k++, p++, pa++) {
144 pa->fuv[0] = min[0] + float(i) * d;
145 pa->fuv[1] = min[1] + float(j) * d;
146 pa->fuv[2] = min[2] + float(k) * d;
147 pa->flag |= PARS_UNEXIST;
148 pa->hair_index = 0; /* abused in volume calculation */
149 }
150 }
151 }
152
153 /* enable particles near verts/edges/faces/inside surface */
154 if (from == PART_FROM_VERT) {
155 float vec[3];
156
157 pa = psys->particles;
158
159 min[0] -= d / 2.0f;
160 min[1] -= d / 2.0f;
161 min[2] -= d / 2.0f;
162
163 for (i = 0; i < totvert; i++) {
164 sub_v3_v3v3(vec, positions[i], min);
165 vec[0] /= delta[0];
166 vec[1] /= delta[1];
167 vec[2] /= delta[2];
168 pa[(int(vec[0] * (size[0] - 1)) * res + int(vec[1] * (size[1] - 1))) * res +
169 int(vec[2] * (size[2] - 1))]
171 }
172 }
173 else if (ELEM(from, PART_FROM_FACE, PART_FROM_VOLUME)) {
174 float co1[3], co2[3];
175
176 MFace *mface = nullptr, *mface_array;
177 float v1[3], v2[3], v3[3], v4[4], lambda;
178 int a, a1, a2, a0mul, a1mul, a2mul, totface;
179 int amax = from == PART_FROM_FACE ? 3 : 1;
180
181 totface = mesh->totface_legacy;
182 mface = mface_array = static_cast<MFace *>(
183 CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy));
184
185 for (a = 0; a < amax; a++) {
186 if (a == 0) {
187 a0mul = res * res;
188 a1mul = res;
189 a2mul = 1;
190 }
191 else if (a == 1) {
192 a0mul = res;
193 a1mul = 1;
194 a2mul = res * res;
195 }
196 else {
197 a0mul = 1;
198 a1mul = res * res;
199 a2mul = res;
200 }
201
202 for (a1 = 0; a1 < size[(a + 1) % 3]; a1++) {
203 for (a2 = 0; a2 < size[(a + 2) % 3]; a2++) {
204 mface = mface_array;
205
206 pa = psys->particles + a1 * a1mul + a2 * a2mul;
207 copy_v3_v3(co1, pa->fuv);
208 co1[a] -= d < delta[a] ? d / 2.0f : delta[a] / 2.0f;
209 copy_v3_v3(co2, co1);
210 co2[a] += delta[a] + 0.001f * d;
211 co1[a] -= 0.001f * d;
212
213 IsectRayPrecalc isect_precalc;
214 float ray_direction[3];
215 sub_v3_v3v3(ray_direction, co2, co1);
216 isect_ray_tri_watertight_v3_precalc(&isect_precalc, ray_direction);
217
218 /* lets intersect the faces */
219 for (i = 0; i < totface; i++, mface++) {
220 ParticleData *pa1 = nullptr, *pa2 = nullptr;
221
222 copy_v3_v3(v1, positions[mface->v1]);
223 copy_v3_v3(v2, positions[mface->v2]);
224 copy_v3_v3(v3, positions[mface->v3]);
225
226 bool intersects_tri = isect_ray_tri_watertight_v3(
227 co1, &isect_precalc, v1, v2, v3, &lambda, nullptr);
228 if (intersects_tri) {
229 pa1 = (pa + int(lambda * size[a]) * a0mul);
230 }
231
232 if (mface->v4 && (!intersects_tri || from == PART_FROM_VOLUME)) {
233 copy_v3_v3(v4, positions[mface->v4]);
234
235 if (isect_ray_tri_watertight_v3(co1, &isect_precalc, v1, v3, v4, &lambda, nullptr)) {
236 pa2 = (pa + int(lambda * size[a]) * a0mul);
237 }
238 }
239
240 if (pa1) {
241 if (from == PART_FROM_FACE) {
242 pa1->flag &= ~PARS_UNEXIST;
243 }
244 else { /* store number of intersections */
245 pa1->hair_index++;
246 }
247 }
248
249 if (pa2 && pa2 != pa1) {
250 if (from == PART_FROM_FACE) {
251 pa2->flag &= ~PARS_UNEXIST;
252 }
253 else { /* store number of intersections */
254 pa2->hair_index++;
255 }
256 }
257 }
258
259 if (from == PART_FROM_VOLUME) {
260 int in = pa->hair_index % 2;
261 if (in) {
262 pa->hair_index++;
263 }
264 for (i = 0; i < size[0]; i++) {
265 if (in || (pa + i * a0mul)->hair_index % 2) {
266 (pa + i * a0mul)->flag &= ~PARS_UNEXIST;
267 }
268 /* odd intersections == in->out / out->in */
269 /* even intersections -> in stays same */
270 in = (in + (pa + i * a0mul)->hair_index) % 2;
271 }
272 }
273 }
274 }
275 }
276 }
277
278 if (psys->part->flag & PART_GRID_HEXAGONAL) {
279 for (i = 0, p = 0, pa = psys->particles; i < res; i++) {
280 for (j = 0; j < res; j++) {
281 for (k = 0; k < res; k++, p++, pa++) {
282 if (j % 2) {
283 pa->fuv[0] += d / 2.0f;
284 }
285
286 if (k % 2) {
287 pa->fuv[0] += d / 2.0f;
288 pa->fuv[1] += d / 2.0f;
289 }
290 }
291 }
292 }
293 }
294
295 if (psys->part->flag & PART_GRID_INVERT) {
296 for (i = 0; i < size[0]; i++) {
297 for (j = 0; j < size[1]; j++) {
298 pa = psys->particles + res * (i * res + j);
299 for (k = 0; k < size[2]; k++, pa++) {
300 pa->flag ^= PARS_UNEXIST;
301 }
302 }
303 }
304 }
305
306 if (psys->part->grid_rand > 0.0f) {
307 float rfac = d * psys->part->grid_rand;
308 for (p = 0, pa = psys->particles; p < psys->totpart; p++, pa++) {
309 if (pa->flag & PARS_UNEXIST) {
310 continue;
311 }
312
313 pa->fuv[0] += rfac * (psys_frand(psys, p + 31) - 0.5f);
314 pa->fuv[1] += rfac * (psys_frand(psys, p + 32) - 0.5f);
315 pa->fuv[2] += rfac * (psys_frand(psys, p + 33) - 0.5f);
316 }
317 }
318}
319
320static void hammersley_create(float *out, int n, int seed, float amount)
321{
322 /* This code is originally from a modified copy from `rayshade.c`
323 * (a file that's no longer included). */
324 RNG *rng;
325
326 double ofs[2], t;
327
328 rng = BLI_rng_new(31415926 + n + seed);
329 ofs[0] = BLI_rng_get_double(rng) + double(amount);
330 ofs[1] = BLI_rng_get_double(rng) + double(amount);
331 BLI_rng_free(rng);
332
333 for (int k = 0; k < n; k++) {
334 BLI_hammersley_1d(k, &t);
335
336 out[2 * k + 0] = fmod(double(k) / double(n) + ofs[0], 1.0);
337 out[2 * k + 1] = fmod(t + ofs[1], 1.0);
338 }
339}
340
341/* almost exact copy of BLI_jitter_init */
342static void init_mv_jit(float *jit, int num, int seed2, float amount)
343{
344 RNG *rng;
345 float *jit2, x, rad1, rad2, rad3;
346 int i, num2;
347
348 if (num == 0) {
349 return;
350 }
351
352 rad1 = (1.0f / sqrtf(float(num)));
353 rad2 = (1.0f / float(num));
354 rad3 = (sqrtf(float(num)) / float(num));
355
356 rng = BLI_rng_new(31415926 + num + seed2);
357 x = 0;
358 num2 = 2 * num;
359 for (i = 0; i < num2; i += 2) {
360
361 jit[i] = x + amount * rad1 * (0.5f - BLI_rng_get_float(rng));
362 jit[i + 1] = i / (2.0f * num) + amount * rad1 * (0.5f - BLI_rng_get_float(rng));
363
364 jit[i] -= floor(jit[i]);
365 jit[i + 1] -= floor(jit[i + 1]);
366
367 x += rad3;
368 x -= floor(x);
369 }
370
371 /* FIXME: The `+ 3` number of items does not seem to be required? */
372 jit2 = MEM_new_array_uninitialized<float>(3 + 2 * size_t(num), "initjit");
373
374 for (i = 0; i < 4; i++) {
376 reinterpret_cast<float (*)[2]>(jit), reinterpret_cast<float (*)[2]>(jit2), num, rad1);
378 reinterpret_cast<float (*)[2]>(jit), reinterpret_cast<float (*)[2]>(jit2), num, rad1);
380 reinterpret_cast<float (*)[2]>(jit), reinterpret_cast<float (*)[2]>(jit2), num, rad2);
381 }
382 MEM_delete(jit2);
383 BLI_rng_free(rng);
384}
385
386static void psys_uv_to_w(float u, float v, int quad, float *w)
387{
388 float vert[4][3], co[3];
389
390 if (!quad) {
391 if (u + v > 1.0f) {
392 v = 1.0f - v;
393 }
394 else {
395 u = 1.0f - u;
396 }
397 }
398
399 vert[0][0] = 0.0f;
400 vert[0][1] = 0.0f;
401 vert[0][2] = 0.0f;
402 vert[1][0] = 1.0f;
403 vert[1][1] = 0.0f;
404 vert[1][2] = 0.0f;
405 vert[2][0] = 1.0f;
406 vert[2][1] = 1.0f;
407 vert[2][2] = 0.0f;
408
409 co[0] = u;
410 co[1] = v;
411 co[2] = 0.0f;
412
413 if (quad) {
414 vert[3][0] = 0.0f;
415 vert[3][1] = 1.0f;
416 vert[3][2] = 0.0f;
417 interp_weights_poly_v3(w, vert, 4, co);
418 }
419 else {
420 interp_weights_poly_v3(w, vert, 3, co);
421 w[3] = 0.0f;
422 }
423}
424
425/* Find the index in "sum" array before "value" is crossed. */
426static int distribute_binary_search(const float *sum, int n, float value)
427{
428 int mid, low = 0, high = n - 1;
429
430 if (high == low) {
431 return low;
432 }
433
434 if (sum[low] >= value) {
435 return low;
436 }
437
438 if (sum[high - 1] < value) {
439 return high;
440 }
441
442 while (low < high) {
443 mid = (low + high) / 2;
444
445 if ((sum[mid] >= value) && (sum[mid - 1] < value)) {
446 return mid;
447 }
448
449 if (sum[mid] > value) {
450 high = mid - 1;
451 }
452 else {
453 low = mid + 1;
454 }
455 }
456
457 return low;
458}
459
460/* the max number if calls to rng_* functions within psys_thread_distribute_particle
461 * be sure to keep up to date if this changes */
462#define PSYS_RND_DIST_SKIP 3
463
464/* NOTE: this function must be thread safe, for `from == PART_FROM_CHILD`. */
465#define ONLY_WORKING_WITH_PA_VERTS 0
467{
468 ParticleThreadContext *ctx = thread->ctx;
469 MFace *mface;
470
471 mface = static_cast<MFace *>(CustomData_get_layer_for_write(
473
474 int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls won't need skipping */
475
476 /* TODO_PARTICLE - use original index */
477 pa->num = ctx->index[p];
478
479 zero_v4(pa->fuv);
480
481 if (pa->num != DMCACHE_NOTFOUND && pa->num < ctx->mesh->verts_num) {
482
483 /* This finds the first face to contain the emitting vertex,
484 * this is not ideal, but is mostly fine as UV seams generally
485 * map to equal-colored parts of a texture */
486 for (int i = 0; i < ctx->mesh->totface_legacy; i++, mface++) {
487 if (ELEM(pa->num, mface->v1, mface->v2, mface->v3, mface->v4)) {
488 uint *vert = &mface->v1;
489
490 for (int j = 0; j < 4; j++, vert++) {
491 if (*vert == pa->num) {
492 pa->fuv[j] = 1.0f;
493 break;
494 }
495 }
496
497 break;
498 }
499 }
500 }
501
502#if ONLY_WORKING_WITH_PA_VERTS
503 if (ctx->tree) {
505 int w, maxw;
506
508 ctx->mesh, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, co1, 0, 0, 0, orco1, 0);
509 BKE_mesh_orco_verts_transform(ob->data, &orco1, 1, true);
510 maxw = kdtree_find_nearest_n<float3>(ctx->tree, orco1, ptn, 3);
511
512 for (w = 0; w < maxw; w++) {
513 pa->verts[w] = ptn->num;
514 }
515 }
516#endif
517
518 BLI_assert(rng_skip_tot >= 0); /* should never be below zero */
519 if (rng_skip_tot > 0) {
520 BLI_rng_skip(thread->rng, rng_skip_tot);
521 }
522}
523
525{
526 ParticleThreadContext *ctx = thread->ctx;
527 Mesh *mesh = ctx->mesh;
528 float randu, randv;
529 int distr = ctx->distr;
530 int i;
531 int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls won't need skipping */
532
533 MFace *mfaces = static_cast<MFace *>(
534 CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy));
535 MFace *mface;
536
537 pa->num = i = ctx->index[p];
538 mface = &mfaces[i];
539
540 switch (distr) {
541 case PART_DISTR_JIT:
542 if (ctx->jitlevel == 1) {
543 if (mface->v4) {
544 psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
545 }
546 else {
547 psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
548 }
549 }
550 else {
551 float offset = fmod(ctx->jitoff[i] + float(p), float(ctx->jitlevel));
552 if (!isnan(offset)) {
554 ctx->jit[2 * int(offset)], ctx->jit[2 * int(offset) + 1], mface->v4, pa->fuv);
555 }
556 }
557 break;
558 case PART_DISTR_RAND:
559 randu = BLI_rng_get_float(thread->rng);
560 randv = BLI_rng_get_float(thread->rng);
561 rng_skip_tot -= 2;
562
563 psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
564 break;
565 }
566 pa->foffset = 0.0f;
567
568 BLI_assert(rng_skip_tot >= 0); /* should never be below zero */
569 if (rng_skip_tot > 0) {
570 BLI_rng_skip(thread->rng, rng_skip_tot);
571 }
572}
573
575{
576 ParticleThreadContext *ctx = thread->ctx;
577 Mesh *mesh = ctx->mesh;
578 const float *v1, *v2, *v3, *v4;
579 float nor[3], co[3];
580 float cur_d, min_d, randu, randv;
581 int distr = ctx->distr;
582 int i, intersect, tot;
583 int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls won't need skipping */
584
585 MFace *mface;
586 const Span<float3> positions = mesh->vert_positions();
587
588 pa->num = i = ctx->index[p];
589 MFace *mfaces = static_cast<MFace *>(
590 CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy));
591 mface = &mfaces[i];
592
593 switch (distr) {
594 case PART_DISTR_JIT:
595 if (ctx->jitlevel == 1) {
596 if (mface->v4) {
597 psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
598 }
599 else {
600 psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
601 }
602 }
603 else {
604 float offset = fmod(ctx->jitoff[i] + float(p), float(ctx->jitlevel));
605 if (!isnan(offset)) {
607 ctx->jit[2 * int(offset)], ctx->jit[2 * int(offset) + 1], mface->v4, pa->fuv);
608 }
609 }
610 break;
611 case PART_DISTR_RAND:
612 randu = BLI_rng_get_float(thread->rng);
613 randv = BLI_rng_get_float(thread->rng);
614 rng_skip_tot -= 2;
615
616 psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
617 break;
618 }
619 pa->foffset = 0.0f;
620
621 /* experimental */
622 tot = mesh->totface_legacy;
623
625 reinterpret_cast<const float (*)[3]>(positions.data()),
626 reinterpret_cast<const float (*)[3]>(mesh->vert_normals().data()),
627 mface,
628 nullptr,
629 nullptr,
630 pa->fuv,
631 co,
632 nor,
633 nullptr,
634 nullptr,
635 nullptr);
636
637 normalize_v3(nor);
638 negate_v3(nor);
639
640 min_d = FLT_MAX;
641 intersect = 0;
642 mface = static_cast<MFace *>(
643 CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy));
644 for (i = 0; i < tot; i++, mface++) {
645 if (i == pa->num) {
646 continue;
647 }
648
649 v1 = positions[mface->v1];
650 v2 = positions[mface->v2];
651 v3 = positions[mface->v3];
652
653 if (isect_ray_tri_v3(co, nor, v2, v3, v1, &cur_d, nullptr)) {
654 if (cur_d < min_d) {
655 min_d = cur_d;
656 pa->foffset = cur_d * 0.5f; /* to the middle of volume */
657 intersect = 1;
658 }
659 }
660 if (mface->v4) {
661 v4 = positions[mface->v4];
662
663 if (isect_ray_tri_v3(co, nor, v4, v1, v3, &cur_d, nullptr)) {
664 if (cur_d < min_d) {
665 min_d = cur_d;
666 pa->foffset = cur_d * 0.5f; /* to the middle of volume */
667 intersect = 1;
668 }
669 }
670 }
671 }
672 if (intersect == 0) {
673 pa->foffset = 0.0;
674 }
675 else {
676 switch (distr) {
677 case PART_DISTR_JIT:
678 pa->foffset *= ctx->jit[p % (2 * ctx->jitlevel)];
679 break;
680 case PART_DISTR_RAND:
681 pa->foffset *= BLI_rng_get_float(thread->rng);
682 rng_skip_tot--;
683 break;
684 }
685 }
686
687 BLI_assert(rng_skip_tot >= 0); /* should never be below zero */
688 if (rng_skip_tot > 0) {
689 BLI_rng_skip(thread->rng, rng_skip_tot);
690 }
691}
692
694{
695 ParticleThreadContext *ctx = thread->ctx;
696 Object *ob = ctx->sim.ob;
697 Mesh *mesh = ctx->mesh;
698 float orco1[3], co1[3], nor1[3];
699 float randu, randv;
700 int cfrom = ctx->cfrom;
701 int i;
702 int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls won't need skipping */
703
704 MFace *mf;
705
706 if (ctx->index[p] < 0) {
707 cpa->num = 0;
708 cpa->fuv[0] = cpa->fuv[1] = cpa->fuv[2] = cpa->fuv[3] = 0.0f;
709 cpa->pa[0] = cpa->pa[1] = cpa->pa[2] = cpa->pa[3] = 0;
710 return;
711 }
712
713 MFace *mfaces = static_cast<MFace *>(
714 CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy));
715 mf = &mfaces[ctx->index[p]];
716
717 randu = BLI_rng_get_float(thread->rng);
718 randv = BLI_rng_get_float(thread->rng);
719 rng_skip_tot -= 2;
720
721 psys_uv_to_w(randu, randv, mf->v4, cpa->fuv);
722
723 cpa->num = ctx->index[p];
724
725 if (ctx->tree) {
726 KDTreeNearest<float3> ptn[10];
727 int w, maxw; //, do_seams;
728 float maxd /* , mind, dd */, totw = 0.0f;
729 int parent[10];
730 float pweight[10];
731
733 cfrom,
734 cpa->num,
736 cpa->fuv,
737 cpa->foffset,
738 co1,
739 nor1,
740 nullptr,
741 nullptr,
742 orco1);
743 BKE_mesh_orco_verts_transform(id_cast<Mesh *>(ob->data), &orco1, 1, true);
744 maxw = kdtree_find_nearest_n<float3>(ctx->tree, orco1, ptn, 3);
745
746 maxd = ptn[maxw - 1].dist;
747 // mind=ptn[0].dist; /* UNUSED */
748
749 /* the weights here could be done better */
750 for (w = 0; w < maxw; w++) {
751 parent[w] = ptn[w].index;
752 pweight[w] = float(pow(2.0, double(-6.0f * ptn[w].dist / maxd)));
753 }
754 for (; w < 10; w++) {
755 parent[w] = -1;
756 pweight[w] = 0.0f;
757 }
758
759 for (w = 0, i = 0; w < maxw && i < 4; w++) {
760 if (parent[w] >= 0) {
761 cpa->pa[i] = parent[w];
762 cpa->w[i] = pweight[w];
763 totw += pweight[w];
764 i++;
765 }
766 }
767 for (; i < 4; i++) {
768 cpa->pa[i] = -1;
769 cpa->w[i] = 0.0f;
770 }
771
772 if (totw > 0.0f) {
773 for (w = 0; w < 4; w++) {
774 cpa->w[w] /= totw;
775 }
776 }
777
778 cpa->parent = cpa->pa[0];
779 }
780
781 if (rng_skip_tot > 0) { /* should never be below zero */
782 BLI_rng_skip(thread->rng, rng_skip_tot);
783 }
784}
785
786static void exec_distribute_parent(TaskPool *__restrict /*pool*/, void *taskdata)
787{
788 ParticleTask *task = static_cast<ParticleTask *>(taskdata);
789 ParticleSystem *psys = task->ctx->sim.psys;
790 ParticleData *pa;
791 int p;
792
794
795 pa = psys->particles + task->begin;
796 switch (psys->part->from) {
797 case PART_FROM_FACE:
798 for (p = task->begin; p < task->end; p++, pa++) {
799 distribute_from_faces_exec(task, pa, p);
800 }
801 break;
802 case PART_FROM_VOLUME:
803 for (p = task->begin; p < task->end; p++, pa++) {
804 distribute_from_volume_exec(task, pa, p);
805 }
806 break;
807 case PART_FROM_VERT:
808 for (p = task->begin; p < task->end; p++, pa++) {
809 distribute_from_verts_exec(task, pa, p);
810 }
811 break;
812 case PART_FROM_CHILD:
813 break;
814 }
815}
816
817static void exec_distribute_child(TaskPool *__restrict /*pool*/, void *taskdata)
818{
819 ParticleTask *task = static_cast<ParticleTask *>(taskdata);
820 ParticleSystem *psys = task->ctx->sim.psys;
821 ChildParticle *cpa;
822 int p;
823
824 /* RNG skipping at the beginning */
825 cpa = psys->child;
826 for (p = 0; p < task->begin; p++, cpa++) {
828 }
829
830 for (; p < task->end; p++, cpa++) {
831 distribute_children_exec(task, cpa, p);
832 }
833}
834
835static int distribute_compare_orig_index(const void *p1, const void *p2, void *user_data)
836{
837 const int *orig_index = static_cast<const int *>(user_data);
838 int index1 = orig_index[*static_cast<const int *>(p1)];
839 int index2 = orig_index[*static_cast<const int *>(p2)];
840
841 if (index1 < index2) {
842 return -1;
843 }
844 if (index1 == index2) {
845 /* This pointer comparison appears to make #qsort stable for GLIBC,
846 * and apparently on SOLARIS too, makes the renders reproducible. */
847 if (p1 < p2) {
848 return -1;
849 }
850 if (p1 == p2) {
851 return 0;
852 }
853
854 return 1;
855 }
856
857 return 1;
858}
859
861{
862 Scene *scene = sim->scene;
863 ParticleSystem *psys = sim->psys;
864 const bool use_render_params = (DEG_get_mode(sim->depsgraph) == DAG_EVAL_RENDER);
865
866 if (from == PART_FROM_CHILD) {
867 ChildParticle *cpa;
868 int p, totchild = psys_get_tot_child(scene, psys, use_render_params);
869
870 if (psys->child && totchild) {
871 for (p = 0, cpa = psys->child; p < totchild; p++, cpa++) {
872 cpa->fuv[0] = cpa->fuv[1] = cpa->fuv[2] = cpa->fuv[3] = 0.0;
873 cpa->foffset = 0.0f;
874 cpa->parent = 0;
875 cpa->pa[0] = cpa->pa[1] = cpa->pa[2] = cpa->pa[3] = 0;
876 cpa->num = -1;
877 }
878 }
879 }
880 else {
883 {
884 pa->fuv[0] = pa->fuv[1] = pa->fuv[2] = pa->fuv[3] = 0.0;
885 pa->foffset = 0.0f;
886 pa->num = -1;
887 }
888 }
889}
890
891/* Creates a distribution of coordinates on a Mesh */
894 int from)
895{
896 Scene *scene = sim->scene;
897 Mesh *final_mesh = sim->psmd->mesh_final;
898 Object *ob = sim->ob;
899 ParticleSystem *psys = sim->psys;
900 ParticleData *pa = nullptr, *tpars = nullptr;
901 ParticleSettings *part;
902 ParticleSeam *seams = nullptr;
903 KDTree<float3> *tree = nullptr;
904 Mesh *mesh = nullptr;
905 float *jit = nullptr;
906 int i, p = 0;
907 int cfrom = 0;
908 int totelem = 0, totpart, *particle_element = nullptr, children = 0, totseam = 0;
909 int jitlevel = 1, distr;
910 float *element_weight = nullptr, *jitter_offset = nullptr, *vweight = nullptr;
911 float cur, maxweight = 0.0, tweight, totweight, inv_totweight, co[3], nor[3], orco[3];
912 RNG *rng = nullptr;
913
914 if (ELEM(nullptr, ob, psys, psys->part)) {
915 return 0;
916 }
917
918 part = psys->part;
919 totpart = psys->totpart;
920 if (totpart == 0) {
921 return 0;
922 }
923
924 if (!final_mesh->runtime->deformed_only &&
926 {
927 printf(
928 "Can't create particles with the current modifier stack, disable destructive modifiers\n");
929 // XXX error("Can't paint with the current modifier stack, disable destructive modifiers");
930 return 0;
931 }
932
933 /* XXX This distribution code is totally broken in case from == PART_FROM_CHILD,
934 * it's always using `final_mesh` even if use_modifier_stack is unset...
935 * But making things consistent here break all existing edited
936 * hair systems, so better wait for complete rewrite. */
937
938 psys_thread_context_init(ctx, sim);
939
940 const bool use_render_params = (DEG_get_mode(sim->depsgraph) == DAG_EVAL_RENDER);
941
942 /* First handle special cases */
943 if (from == PART_FROM_CHILD) {
944 /* Simple children */
945 if (part->childtype != PART_CHILD_FACES) {
947 scene, ob, final_mesh, sim->psmd->mesh_original, psys, use_render_params);
948 return 0;
949 }
950 }
951 else {
952 /* Grid distribution */
953 if (part->distr == PART_DISTR_GRID && from != PART_FROM_VERT) {
954 if (psys->part->use_modifier_stack) {
955 mesh = final_mesh;
956 }
957 else {
958 mesh = id_cast<Mesh *>(BKE_id_copy_ex(
959 nullptr, static_cast<const ID *>(ob->data), nullptr, LIB_ID_COPY_LOCALIZE));
960 }
962
963 distribute_grid(mesh, psys);
964
965 if (mesh != final_mesh) {
966 BKE_id_free(nullptr, mesh);
967 }
968
969 return 0;
970 }
971 }
972
973 /* After this #BKE_mesh_orco_verts_transform can be used safely from multiple threads. */
974 BKE_mesh_texspace_ensure(final_mesh);
975
976 /* Create trees and original coordinates if needed */
977 if (from == PART_FROM_CHILD) {
978 distr = PART_DISTR_RAND;
979 rng = BLI_rng_new_srandom(31415926 + psys->seed + psys->child_seed);
980 mesh = final_mesh;
981
982 /* BMESH ONLY */
984
985 children = 1;
986
987 tree = kdtree_new<float3>(totpart);
988
989 for (p = 0, pa = psys->particles; p < totpart; p++, pa++) {
991 part->from,
992 pa->num,
993 pa->num_dmcache,
994 pa->fuv,
995 pa->foffset,
996 co,
997 nor,
998 nullptr,
999 nullptr,
1000 orco);
1001 BKE_mesh_orco_verts_transform(id_cast<Mesh *>(ob->data), &orco, 1, true);
1002 kdtree_insert<float3>(tree, p, orco);
1003 }
1004
1006
1007 totpart = psys_get_tot_child(scene, psys, use_render_params);
1008 cfrom = from = PART_FROM_FACE;
1009 }
1010 else {
1011 distr = part->distr;
1012
1013 rng = BLI_rng_new_srandom(31415926 + psys->seed);
1014
1015 if (psys->part->use_modifier_stack) {
1016 mesh = final_mesh;
1017 }
1018 else {
1019 mesh = id_cast<Mesh *>(BKE_id_copy_ex(
1020 nullptr, static_cast<const ID *>(ob->data), nullptr, LIB_ID_COPY_LOCALIZE));
1021 }
1022
1024
1025 /* we need orco for consistent distributions */
1027
1028 if (from == PART_FROM_VERT) {
1029 const Span<float3> positions = mesh->vert_positions();
1030 const float (*orcodata)[3] = static_cast<const float (*)[3]>(
1031 CustomData_get_layer(&mesh->vert_data, CD_ORCO));
1032 int totvert = mesh->verts_num;
1033
1034 tree = kdtree_new<float3>(totvert);
1035
1036 for (p = 0; p < totvert; p++) {
1037 if (orcodata) {
1038 copy_v3_v3(co, orcodata[p]);
1039 BKE_mesh_orco_verts_transform(id_cast<Mesh *>(ob->data), &co, 1, true);
1040 }
1041 else {
1042 copy_v3_v3(co, positions[p]);
1043 }
1044 kdtree_insert<float3>(tree, p, co);
1045 }
1046
1048 }
1049 }
1050
1051 /* Get total number of emission elements and allocate needed arrays */
1052 totelem = (from == PART_FROM_VERT) ? mesh->verts_num : mesh->totface_legacy;
1053
1054 if (totelem == 0) {
1055 distribute_invalid(sim, children ? PART_FROM_CHILD : 0);
1056
1057 if (G.debug & G_DEBUG) {
1058 fprintf(stderr, "Particle distribution error: Nothing to emit from!\n");
1059 }
1060
1061 if (mesh != final_mesh) {
1062 BKE_id_free(nullptr, mesh);
1063 }
1064
1065 kdtree_free<float3>(tree);
1066 BLI_rng_free(rng);
1067
1068 return 0;
1069 }
1070
1071 element_weight = MEM_new_array_zeroed<float>(totelem, "particle_distribution_weights");
1072 particle_element = MEM_new_array_zeroed<int>(totpart, "particle_distribution_indexes");
1073 jitter_offset = MEM_new_array_zeroed<float>(totelem, "particle_distribution_jitoff");
1074
1075 /* Calculate weights from face areas */
1076 if ((part->flag & PART_EDISTR || children) && from != PART_FROM_VERT) {
1077 float totarea = 0.0f, co1[3], co2[3], co3[3], co4[3];
1078 const float (*orcodata)[3];
1079
1080 orcodata = static_cast<const float (*)[3]>(CustomData_get_layer(&mesh->vert_data, CD_ORCO));
1081
1082 MFace *mfaces = static_cast<MFace *>(
1083 CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy));
1084 for (i = 0; i < totelem; i++) {
1085 MFace *mf = &mfaces[i];
1086
1087 if (orcodata) {
1088 /* Transform orcos from normalized 0..1 to object space. */
1089 copy_v3_v3(co1, orcodata[mf->v1]);
1090 copy_v3_v3(co2, orcodata[mf->v2]);
1091 copy_v3_v3(co3, orcodata[mf->v3]);
1092 BKE_mesh_orco_verts_transform(id_cast<Mesh *>(ob->data), &co1, 1, true);
1093 BKE_mesh_orco_verts_transform(id_cast<Mesh *>(ob->data), &co2, 1, true);
1094 BKE_mesh_orco_verts_transform(id_cast<Mesh *>(ob->data), &co3, 1, true);
1095 if (mf->v4) {
1096 copy_v3_v3(co4, orcodata[mf->v4]);
1097 BKE_mesh_orco_verts_transform(id_cast<Mesh *>(ob->data), &co4, 1, true);
1098 }
1099 }
1100 else {
1101 MutableSpan<float3> positions = mesh->vert_positions_for_write();
1102 copy_v3_v3(co1, positions[mf->v1]);
1103 copy_v3_v3(co2, positions[mf->v2]);
1104 copy_v3_v3(co3, positions[mf->v3]);
1105 if (mf->v4) {
1106 copy_v3_v3(co4, positions[mf->v4]);
1107 }
1108 }
1109
1110 cur = mf->v4 ? area_quad_v3(co1, co2, co3, co4) : area_tri_v3(co1, co2, co3);
1111
1112 maxweight = std::max(cur, maxweight);
1113
1114 element_weight[i] = cur;
1115 totarea += cur;
1116 }
1117
1118 for (i = 0; i < totelem; i++) {
1119 element_weight[i] /= totarea;
1120 }
1121
1122 maxweight /= totarea;
1123 }
1124 else {
1125 float min = 1.0f / float(std::min(totelem, totpart));
1126 for (i = 0; i < totelem; i++) {
1127 element_weight[i] = min;
1128 }
1129 maxweight = min;
1130 }
1131
1132 /* Calculate weights from vgroup */
1133 vweight = psys_cache_vgroup(mesh, psys, PSYS_VG_DENSITY);
1134
1135 if (vweight) {
1136 if (from == PART_FROM_VERT) {
1137 for (i = 0; i < totelem; i++) {
1138 element_weight[i] *= vweight[i];
1139 }
1140 }
1141 else { /* PART_FROM_FACE / PART_FROM_VOLUME */
1142 MFace *mfaces = static_cast<MFace *>(
1143 CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy));
1144 for (i = 0; i < totelem; i++) {
1145 MFace *mf = &mfaces[i];
1146 tweight = vweight[mf->v1] + vweight[mf->v2] + vweight[mf->v3];
1147
1148 if (mf->v4) {
1149 tweight += vweight[mf->v4];
1150 tweight /= 4.0f;
1151 }
1152 else {
1153 tweight /= 3.0f;
1154 }
1155
1156 element_weight[i] *= tweight;
1157 }
1158 }
1159 MEM_delete(vweight);
1160 }
1161
1162 /* Calculate total weight of all elements */
1163 int totmapped = 0;
1164 totweight = 0.0f;
1165 for (i = 0; i < totelem; i++) {
1166 if (element_weight[i] > 0.0f) {
1167 totmapped++;
1168 totweight += element_weight[i];
1169 }
1170 }
1171
1172 if (totmapped == 0) {
1173 /* We are not allowed to distribute particles anywhere... */
1174 if (mesh != final_mesh) {
1175 BKE_id_free(nullptr, mesh);
1176 }
1177 kdtree_free<float3>(tree);
1178 BLI_rng_free(rng);
1179 MEM_delete(element_weight);
1180 MEM_delete(particle_element);
1181 MEM_delete(jitter_offset);
1182 return 0;
1183 }
1184
1185 inv_totweight = 1.0f / totweight;
1186
1187 /* Calculate cumulative weights.
1188 * We remove all null-weighted elements from element_sum, and create a new mapping
1189 * 'activ'_elem_index -> orig_elem_index.
1190 * This simplifies greatly the filtering of zero-weighted items - and can be much more efficient
1191 * especially in random case (reducing a lot the size of binary-searched array)...
1192 */
1193 float *element_sum = MEM_new_array_uninitialized<float>(size_t(totmapped), __func__);
1194 int *element_map = MEM_new_array_uninitialized<int>(size_t(totmapped), __func__);
1195 int i_mapped = 0;
1196
1197 for (i = 0; i < totelem && element_weight[i] == 0.0f; i++) {
1198 /* pass */
1199 }
1200 element_sum[i_mapped] = element_weight[i] * inv_totweight;
1201 element_map[i_mapped] = i;
1202 i_mapped++;
1203 for (i++; i < totelem; i++) {
1204 if (element_weight[i] > 0.0f) {
1205 element_sum[i_mapped] = element_sum[i_mapped - 1] + element_weight[i] * inv_totweight;
1206 /* Skip elements which weight is so small that it does not affect the sum. */
1207 if (element_sum[i_mapped] > element_sum[i_mapped - 1]) {
1208 element_map[i_mapped] = i;
1209 i_mapped++;
1210 }
1211 }
1212 }
1213 totmapped = i_mapped;
1214
1215 /* Finally assign elements to particles */
1216 if (part->flag & PART_TRAND) {
1217 for (p = 0; p < totpart; p++) {
1218 /* In theory element_sum[totmapped - 1] should be 1.0,
1219 * but due to float errors this is not necessarily always true, so scale pos accordingly. */
1220 const float pos = BLI_rng_get_float(rng) * element_sum[totmapped - 1];
1221 const int eidx = distribute_binary_search(element_sum, totmapped, pos);
1222 particle_element[p] = element_map[eidx];
1223 BLI_assert(pos <= element_sum[eidx]);
1224 BLI_assert(eidx ? (pos > element_sum[eidx - 1]) : (pos >= 0.0f));
1225 jitter_offset[particle_element[p]] = pos;
1226 }
1227 }
1228 else {
1229 double step, pos;
1230
1231 step = (totpart < 2) ? 0.5 : 1.0 / double(totpart);
1232 /* This is to address tricky issues with vertex-emitting when user tries
1233 * (and expects) exact 1-1 vert/part distribution (see #47983 and its two example files).
1234 * It allows us to consider pos as 'midpoint between v and v+1'
1235 * (or 'p and p+1', depending whether we have more vertices than particles or not),
1236 * and avoid stumbling over float impression in element_sum.
1237 * NOTE: moved face and volume distribution to this as well (instead of starting at zero),
1238 * for the same reasons, see #52682. */
1239 pos = (totpart < totmapped) ? 0.5 / double(totmapped) :
1240 step * 0.5; /* We choose the smaller step. */
1241
1242 for (i = 0, p = 0; p < totpart; p++, pos += step) {
1243 for (; (i < totmapped - 1) && (pos > double(element_sum[i])); i++) {
1244 /* pass */
1245 }
1246
1247 particle_element[p] = element_map[i];
1248
1249 jitter_offset[particle_element[p]] = pos;
1250 }
1251 }
1252
1253 MEM_delete(element_sum);
1254 MEM_delete(element_map);
1255
1256 /* For hair, sort by #CD_ORIGINDEX (allows optimization's in rendering),
1257 * however with virtual parents the children need to be in random order. */
1258 if (part->type == PART_HAIR && !(part->childtype == PART_CHILD_FACES && part->parents != 0.0f)) {
1259 const int *orig_index = nullptr;
1260
1261 if (from == PART_FROM_VERT) {
1262 if (mesh->verts_num) {
1263 orig_index = static_cast<const int *>(
1264 CustomData_get_layer(&mesh->vert_data, CD_ORIGINDEX));
1265 }
1266 }
1267 else {
1268 if (mesh->totface_legacy) {
1269 orig_index = static_cast<const int *>(
1270 CustomData_get_layer(&mesh->fdata_legacy, CD_ORIGINDEX));
1271 }
1272 }
1273
1274 if (orig_index) {
1275 BLI_qsort_r(particle_element,
1276 totpart,
1277 sizeof(int),
1279 (void *)orig_index);
1280 }
1281 }
1282
1283 /* Create jittering if needed */
1284 if (distr == PART_DISTR_JIT && ELEM(from, PART_FROM_FACE, PART_FROM_VOLUME)) {
1285 jitlevel = part->userjit;
1286
1287 if (jitlevel == 0) {
1288 jitlevel = totpart / totelem;
1289 if (part->flag & PART_EDISTR) {
1290 jitlevel *= 2; /* looks better in general, not very scientific */
1291 }
1292 jitlevel = std::max(jitlevel, 3);
1293 }
1294
1295 jit = MEM_new_array_zeroed<float>(2 + size_t(jitlevel * 2), "jit");
1296
1297 /* for small amounts of particles we use regular jitter since it looks
1298 * a bit better, for larger amounts we switch to hammersley sequence
1299 * because it is much faster */
1300 if (jitlevel < 25) {
1301 init_mv_jit(jit, jitlevel, psys->seed, part->jitfac);
1302 }
1303 else {
1304 hammersley_create(jit, jitlevel + 1, psys->seed, part->jitfac);
1305 }
1307 jit, sizeof(float[2]), jitlevel, psys->seed); /* for custom jit or even distribution */
1308 }
1309
1310 /* Setup things for threaded distribution */
1311 ctx->tree = tree;
1312 ctx->seams = seams;
1313 ctx->totseam = totseam;
1314 ctx->sim.psys = psys;
1315 ctx->index = particle_element;
1316 ctx->jit = jit;
1317 ctx->jitlevel = jitlevel;
1318 ctx->jitoff = jitter_offset;
1319 ctx->weight = element_weight;
1320 ctx->maxweight = maxweight;
1321 ctx->cfrom = cfrom;
1322 ctx->distr = distr;
1323 ctx->mesh = mesh;
1324 ctx->tpars = tpars;
1325
1326 if (children) {
1327 alloc_child_particles(psys, totpart);
1328 }
1329
1330 BLI_rng_free(rng);
1331
1332 return 1;
1333}
1334
1336{
1337 /* init random number generator */
1338 int seed = 31415926 + sim->psys->seed;
1339
1340 task->rng = BLI_rng_new(seed);
1341}
1342
1344{
1346 Mesh *final_mesh = sim->psmd->mesh_final;
1347
1348 /* create a task pool for distribution tasks */
1349 if (!psys_thread_context_init_distribute(&ctx, sim, from)) {
1350 return;
1351 }
1352
1354
1355 const int totpart = (from == PART_FROM_CHILD ? sim->psys->totchild : sim->psys->totpart);
1356 Vector<ParticleTask> tasks = psys_tasks_create(&ctx, 0, totpart);
1357 for (ParticleTask &task : tasks) {
1358 psys_task_init_distribute(&task, sim);
1359 if (from == PART_FROM_CHILD) {
1360 BLI_task_pool_push(task_pool, exec_distribute_child, &task, false, nullptr);
1361 }
1362 else {
1363 BLI_task_pool_push(task_pool, exec_distribute_parent, &task, false, nullptr);
1364 }
1365 }
1366 BLI_task_pool_work_and_wait(task_pool);
1367
1368 BLI_task_pool_free(task_pool);
1369
1370 psys_calc_dmcache(sim->ob, final_mesh, sim->psmd->mesh_original, sim->psys);
1371
1372 if (ctx.mesh != final_mesh) {
1373 BKE_id_free(nullptr, ctx.mesh);
1374 }
1375
1376 psys_tasks_free(tasks);
1377
1379}
1380
1381/* ready for future use, to emit particles without geometry */
1383{
1384 distribute_invalid(sim, 0);
1385
1386 fprintf(stderr, "Shape emission not yet possible!\n");
1387}
1388
1390{
1392 int distr_error = 0;
1393
1394 if (psmd) {
1395 if (psmd->mesh_final) {
1396 distribute_particles_on_dm(sim, from);
1397 }
1398 else {
1399 distr_error = 1;
1400 }
1401 }
1402 else {
1404 }
1405
1406 if (distr_error) {
1407 distribute_invalid(sim, from);
1408
1409 fprintf(stderr, "Particle distribution error!\n");
1410 }
1411}
1412
1413} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define DMCACHE_NOTFOUND
#define LOOP_PARTICLES
#define DMCACHE_ISCHILD
#define PARTICLE_PSMD
#define PARTICLE_P
A KD-tree for nearest neighbor search.
Random number functions.
Read Blender memory allocation and freeing.
#define INIT_MINMAX(min, max)
#define ELEM(...)
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr const T * data() const
Definition BLI_span.hh:219
bool intersect(IsectPyramid i_pyramid, Box box)
#define FLT_MAX
#define isnan
#define pow
#define floor
#define ceil
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
#define jit
ccl_device_inline float2 fmod(const float2 a, const float b)
BMesh Iterators.
float area_quad_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:89
static void init_mv_jit(float *jit, int num, int seed2, float amount)
static void exec_distribute_parent(TaskPool *__restrict, void *taskdata)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void kdtree_balance(KDTree< CoordT > *tree)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
static void distribute_from_verts_exec(ParticleTask *thread, ParticleData *pa, int p)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Vector< ParticleTask > psys_tasks_create(struct ParticleThreadContext *ctx, int startpart, int endpart)
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
double BLI_rng_get_double(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition rand.cc:85
static void distribute_simple_children(Scene *scene, Object *ob, Mesh *final_mesh, Mesh *deform_mesh, ParticleSystem *psys, const bool use_render_params)
bool isect_ray_tri_watertight_v3(const float ray_origin[3], const struct IsectRayPrecalc *isect_precalc, const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
void BLI_jitterate2(float(*jit1)[2], float(*jit2)[2], int num, float radius2)
Definition jitter_2d.cc:79
@ TASK_PRIORITY_HIGH
Definition BLI_task_c.hh:61
struct RNG * BLI_rng_new_srandom(unsigned int seed)
Definition rand.cc:48
static void psys_task_init_distribute(ParticleTask *task, ParticleSimulationData *sim)
float * psys_cache_vgroup(struct Mesh *mesh, struct ParticleSystem *psys, int vgroup)
Definition particle.cc:2621
void psys_thread_context_init(struct ParticleThreadContext *ctx, struct ParticleSimulationData *sim)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void psys_interpolate_face(struct Mesh *mesh, const float(*vert_positions)[3], const float(*vert_normals)[3], const struct MFace *mface, struct MTFace *tface, const float(*orcodata)[3], float w[4], float vec[3], float nor[3], float utan[3], float vtan[3], float orco[3])
static void distribute_children_exec(ParticleTask *thread, ChildParticle *cpa, int p)
static void alloc_child_particles(ParticleSystem *psys, int tot)
struct RNG * BLI_rng_new(unsigned int seed)
Definition rand.cc:41
static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, ParticleSimulationData *sim, int from)
static void distribute_particles_on_dm(ParticleSimulationData *sim, int from)
@ LIB_ID_COPY_LOCALIZE
void BLI_array_randomize(void *data, unsigned int elem_size, unsigned int elem_num, unsigned int seed)
Definition rand.cc:158
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:455
Global G
Definition blender.cc:57
MINLINE void zero_v4(float r[4])
KDTree< CoordT > * kdtree_new(uint nodes_len_capacity)
Definition BLI_kdtree.hh:65
static void distribute_particles_on_shape(ParticleSimulationData *sim, int)
int kdtree_find_nearest_n(const KDTree< CoordT > *tree, const CoordT &co, KDTreeNearest< CoordT > r_nearest[], uint nearest_len_capacity)
void psys_particle_on_dm(struct Mesh *mesh_final, int from, int index, int index_dmcache, const float fw[4], float foffset, float vec[3], float nor[3], float utan[3], float vtan[3], float orco[3])
Definition particle.cc:2056
MINLINE int axis_dominant_v3_single(const float vec[3])
void kdtree_insert(KDTree< CoordT > *tree, int index, const CoordT &co)
Definition BLI_kdtree.hh:96
void BKE_mesh_texspace_ensure(Mesh *mesh)
void BLI_jitterate1(float(*jit1)[2], float(*jit2)[2], int num, float radius1)
Definition jitter_2d.cc:21
MINLINE void negate_v3(float r[3])
static int distribute_binary_search(const float *sum, int n, float value)
void isect_ray_tri_watertight_v3_precalc(struct IsectRayPrecalc *isect_precalc, const float ray_direction[3])
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition rand.cc:55
static int distribute_compare_orig_index(const void *p1, const void *p2, void *user_data)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
ATTR_WARN_UNUSED_RESULT const size_t num
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
void interp_weights_poly_v3(float w[], float v[][3], int n, const float co[3])
static void distribute_from_faces_exec(ParticleTask *thread, ParticleData *pa, int p)
bool isect_ray_tri_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
MINLINE void zero_v3(float r[3])
void BKE_id_free(Main *bmain, void *idv)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
void BKE_mesh_orco_verts_transform(Mesh *mesh, MutableSpan< float3 > orco, bool invert)
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:441
void distribute_particles(struct ParticleSimulationData *sim, int from)
void BKE_mesh_orco_ensure(Object *ob, Mesh *mesh)
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition rand.cc:90
void psys_calc_dmcache(struct Object *ob, struct Mesh *mesh_final, struct Mesh *mesh_original, struct ParticleSystem *psys)
static void distribute_from_volume_exec(ParticleTask *thread, ParticleData *pa, int p)
TaskPool * BLI_task_pool_create(void *userdata, eTaskPriority priority)
Definition task_pool.cc:404
MINLINE float normalize_v3(float n[3])
void kdtree_free(KDTree< CoordT > *tree)
Definition BLI_kdtree.hh:84
static void distribute_grid(Mesh *mesh, ParticleSystem *psys)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
int psys_get_tot_child(struct Scene *scene, struct ParticleSystem *psys, bool use_render_params)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void BLI_qsort_r(void *a, size_t n, size_t es, BLI_sort_cmp_t cmp, void *thunk)
Definition sort.cc:79
float area_tri_v3(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:104
void psys_thread_context_free(struct ParticleThreadContext *ctx)
static void psys_uv_to_w(float u, float v, int quad, float *w)
void BKE_mesh_tessface_ensure(Mesh *mesh)
void void void BLI_rng_skip(struct RNG *rng, int n) ATTR_NONNULL(1)
Definition rand.cc:143
static void exec_distribute_child(TaskPool *__restrict, void *taskdata)
void psys_tasks_free(Vector< ParticleTask > &tasks)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:851
void BLI_hammersley_1d(unsigned int n, double *r)
Definition rand.cc:283
static void distribute_invalid(ParticleSimulationData *sim, int from)
BLI_INLINE float psys_frand(ParticleSystem *psys, unsigned int seed)
static void hammersley_create(float *out, int n, int seed, float amount)
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:446
int psys_get_child_number(struct Scene *scene, struct ParticleSystem *psys, bool use_render_params)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
#define PSYS_RND_DIST_SKIP
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
bke::MeshRuntime * runtime
CustomData fdata_legacy
eParticleDistribution distr
struct ParticleSystem * psys
struct ParticleSystemModifierData * psmd
ParticleThreadContext * ctx
struct ParticleData * tpars
struct ParticleSeam * seams
struct ParticleSimulationData sim