Blender V5.3
particle_edit.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 <cmath>
11#include <cstdlib>
12#include <cstring>
13
14#include "MEM_guardedalloc.h"
15
16#include "DNA_mesh_types.h"
17#include "DNA_meshdata_types.h"
18#include "DNA_scene_types.h"
19#include "DNA_screen_types.h"
20#include "DNA_space_types.h"
21#include "DNA_view3d_types.h"
22
23#include "BLI_kdtree.hh"
24#include "BLI_lasso_2d.hh"
25#include "BLI_listbase.hh"
26#include "BLI_math_geom_c.hh"
27#include "BLI_math_matrix_c.hh"
28#include "BLI_math_vector.hh"
29#include "BLI_rand_c.hh"
30#include "BLI_rect.hh"
31#include "BLI_task_c.hh"
32#include "BLI_time.hh"
33#include "BLI_utildefines.hh"
34
35#include "BLT_translation.hh"
36
37#include "BKE_bvhutils.hh"
38#include "BKE_context.hh"
39#include "BKE_customdata.hh"
40#include "BKE_global.hh"
41#include "BKE_layer.hh"
42#include "BKE_library.hh"
43#include "BKE_main.hh"
44#include "BKE_mesh.hh"
46#include "BKE_mesh_runtime.hh"
47#include "BKE_modifier.hh"
48#include "BKE_object.hh"
49#include "BKE_particle.h"
50#include "BKE_pointcache.h"
51#include "BKE_report.hh"
52#include "BKE_scene.hh"
53
54#include "DEG_depsgraph.hh"
55
56#include "ED_mesh.hh"
57#include "ED_object.hh"
58#include "ED_particle.hh"
59#include "ED_physics.hh"
60#include "ED_screen.hh"
61#include "ED_select_utils.hh"
62#include "ED_view3d.hh"
63
64#include "GPU_immediate.hh"
65#include "GPU_immediate_util.hh"
66#include "GPU_state.hh"
67
68#include "WM_api.hh"
69#include "WM_message.hh"
70#include "WM_toolsystem.hh"
71#include "WM_types.hh"
72
73#include "RNA_access.hh"
74#include "RNA_define.hh"
75
77
78#include "physics_intern.hh"
79
81
82namespace blender {
83
84/* -------------------------------------------------------------------- */
87
89{
90 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
91 Scene *scene = CTX_data_scene(C);
93
94 if (!scene || !ob || !(ob->mode & OB_MODE_PARTICLE_EDIT)) {
95 return false;
96 }
97
98 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
99 if (edit == nullptr) {
100 return false;
101 }
102 if (edit->psmd_eval == nullptr || edit->psmd_eval->mesh_final == nullptr) {
103 return false;
104 }
105
106 return true;
107}
108
110{
111 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
112 Scene *scene = CTX_data_scene(C);
114
115 if (!scene || !ob || !(ob->mode & OB_MODE_PARTICLE_EDIT)) {
116 return false;
117 }
118
119 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
120 if (edit == nullptr || edit->psys == nullptr) {
121 return false;
122 }
123 if (edit->psmd_eval == nullptr || edit->psmd_eval->mesh_final == nullptr) {
124 return false;
125 }
126
127 return true;
128}
129
131{
132 ScrArea *area = CTX_wm_area(C);
133 ARegion *region = CTX_wm_region(C);
134
135 return (PE_poll(C) && (area && area->spacetype == SPACE_VIEW3D) &&
136 (region && region->regiontype == RGN_TYPE_WINDOW));
137}
138
140{
141 POINT_P;
142
143 if (edit == nullptr) {
144 return;
145 }
146
147 if (edit->points) {
149 if (point->keys) {
150 MEM_delete(point->keys);
151 }
152 }
153
154 MEM_delete(edit->points);
155 }
156
157 if (edit->mirror_cache) {
158 MEM_delete(edit->mirror_cache);
159 }
160
161 if (edit->emitter_cosnos) {
162 MEM_delete(edit->emitter_cosnos);
163 edit->emitter_cosnos = nullptr;
164 }
165
166 if (edit->emitter_field) {
168 edit->emitter_field = nullptr;
169 }
170
171 psys_free_path_cache(edit->psys, edit);
172
173 MEM_delete(edit);
174}
175
176int PE_minmax(Depsgraph *depsgraph, Scene *scene, ViewLayer *view_layer, float3 &min, float3 &max)
177{
178 BKE_view_layer_synced_ensure(*DEG_get_bmain(depsgraph), scene, view_layer);
180 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
181 ParticleSystem *psys;
182 ParticleSystemModifierData *psmd_eval = nullptr;
183 POINT_P;
184 KEY_K;
185 float co[3], mat[4][4];
186 int ok = 0;
187
188 if (!edit) {
189 return ok;
190 }
191
192 if ((psys = edit->psys)) {
193 psmd_eval = edit->psmd_eval;
194 }
195 else {
196 unit_m4(mat);
197 }
198
200 if (psys) {
202 ob, psmd_eval->mesh_final, psys->part->from, psys->particles + p, mat);
203 }
204
206 copy_v3_v3(co, key->co);
207 mul_m4_v3(mat, co);
209 ok = 1;
210 }
211 }
212
213 if (!ok) {
215 ok = 1;
216 }
217
218 return ok;
219}
220
222
223/* -------------------------------------------------------------------- */
226
228{
229 if (edit) {
230 edit->edited = 1;
231 if (edit->psys) {
232 edit->psys->flag |= PSYS_EDITED;
233 }
234 return 1;
235 }
236
237 return 0;
238}
239
241{
242 return scene->toolsettings ? &scene->toolsettings->particle : nullptr;
243}
244
245static float pe_brush_size_get(const Scene * /*scene*/, ParticleBrushData *brush)
246{
247#if 0 /* TODO: Here we can enable unified brush size, needs more work. */
248 UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
249 float size = (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
250#endif
251
252 return brush->size;
253}
254
256{
257 if (psys->part && psys->part->type == PART_HAIR) {
258 if ((psys->flag & PSYS_HAIR_DYNAMICS) != 0 && (psys->pointcache->flag & PTCACHE_BAKED) != 0) {
259 return psys->pointcache->edit;
260 }
261 return psys->edit;
262 }
263 if (psys->pointcache->flag & PTCACHE_BAKED) {
264 return psys->pointcache->edit;
265 }
266 return nullptr;
267}
268
269/* NOTE: Similar to creation of edit, but only updates pointers in the
270 * existing struct.
271 */
273{
274 ParticleSystem *psys = edit->psys;
275 ParticleData *pa = psys->particles;
276 for (int p = 0; p < edit->totpoint; p++) {
277 PTCacheEditPoint *point = &edit->points[p];
278 HairKey *hair_key = pa->hair;
279 for (int k = 0; k < point->totkey; k++) {
280 PTCacheEditKey *key = &point->keys[k];
281 key->co = hair_key->co;
282 key->time = &hair_key->time;
283 key->flag = hair_key->editflag;
284 if (!(psys->flag & PSYS_GLOBAL_HAIR)) {
285 key->flag |= PEK_USE_WCO;
286 hair_key->editflag |= PEK_USE_WCO;
287 }
288 hair_key++;
289 }
290 pa++;
291 }
292}
293
294/* always gets at least the first particlesystem even if PSYS_CURRENT flag is not set
295 *
296 * NOTE: this function runs on poll, therefore it can runs many times a second
297 * keep it fast! */
298static PTCacheEdit *pe_get_current(Depsgraph *depsgraph, Scene *scene, Object *ob, bool create)
299{
300 ParticleEditSettings *pset = PE_settings(scene);
301 PTCacheEdit *edit = nullptr;
302 ListBaseT<PTCacheID> pidlist;
303 PTCacheID *pid;
304
305 if (pset == nullptr || ob == nullptr) {
306 return nullptr;
307 }
308
309 pset->scene = scene;
310 pset->object = ob;
311
312 BKE_ptcache_ids_from_object(&pidlist, ob, nullptr, 0);
313
314 /* in the case of only one editable thing, set pset->edittype accordingly */
315 if (pidlist.is_single()) {
316 pid = pidlist.first();
317 switch (pid->type) {
320 break;
323 break;
325 pset->edittype = PE_TYPE_CLOTH;
326 break;
327 }
328 }
329
330 for (pid = pidlist.first(); pid; pid = pid->next) {
331 if (pset->edittype == PE_TYPE_PARTICLES && pid->type == PTCACHE_TYPE_PARTICLES) {
332 ParticleSystem *psys = static_cast<ParticleSystem *>(pid->calldata);
333
334 if (psys->flag & PSYS_CURRENT) {
335 if (psys->part && psys->part->type == PART_HAIR) {
336 if (psys->flag & PSYS_HAIR_DYNAMICS && psys->pointcache->flag & PTCACHE_BAKED) {
337 if (create && !psys->pointcache->edit) {
338 PE_create_particle_edit(depsgraph, scene, ob, pid->cache, nullptr);
339 }
340 edit = pid->cache->edit;
341 }
342 else {
343 if (create && !psys->edit) {
344 if (psys->flag & PSYS_HAIR_DONE) {
345 PE_create_particle_edit(depsgraph, scene, ob, nullptr, psys);
346 }
347 }
348 edit = psys->edit;
349 }
350 }
351 else {
352 if (create && pid->cache->flag & PTCACHE_BAKED && !pid->cache->edit) {
353 PE_create_particle_edit(depsgraph, scene, ob, pid->cache, psys);
354 }
355 edit = pid->cache->edit;
356 }
357
358 break;
359 }
360 }
361 else if (pset->edittype == PE_TYPE_SOFTBODY && pid->type == PTCACHE_TYPE_SOFTBODY) {
362 if (create && pid->cache->flag & PTCACHE_BAKED && !pid->cache->edit) {
363 pset->flag |= PE_FADE_TIME;
364 /* Nice to have but doesn't work: `pset->brushtype = PE_BRUSH_COMB;`. */
365 PE_create_particle_edit(depsgraph, scene, ob, pid->cache, nullptr);
366 }
367 edit = pid->cache->edit;
368 break;
369 }
370 else if (pset->edittype == PE_TYPE_CLOTH && pid->type == PTCACHE_TYPE_CLOTH) {
371 if (create && pid->cache->flag & PTCACHE_BAKED && !pid->cache->edit) {
372 pset->flag |= PE_FADE_TIME;
373 /* Nice to have but doesn't work: `pset->brushtype = PE_BRUSH_COMB;`. */
374 PE_create_particle_edit(depsgraph, scene, ob, pid->cache, nullptr);
375 }
376 edit = pid->cache->edit;
377 break;
378 }
379 }
380
381 /* Don't consider inactive or render dependency graphs, since they might be evaluated for a
382 * different number of children. or have different pointer to evaluated particle system or
383 * modifier which will also cause troubles. */
384 if (edit && DEG_is_active(depsgraph)) {
385 edit->pid = *pid;
387 if (edit->psys != nullptr && edit->psys_eval != nullptr) {
388 psys_copy_particles(edit->psys, edit->psys_eval);
390 }
392 }
393 }
394
395 pidlist.free_no_destruct();
396
397 return edit;
398}
399
400PTCacheEdit *PE_get_current(Depsgraph *depsgraph, Scene *scene, Object *ob)
401{
402 return pe_get_current(depsgraph, scene, ob, false);
403}
404
405PTCacheEdit *PE_create_current(Depsgraph *depsgraph, Scene *scene, Object *ob)
406{
407 return pe_get_current(depsgraph, scene, ob, true);
408}
409
410void PE_current_changed(Depsgraph *depsgraph, Scene *scene, Object *ob)
411{
412 if (ob->mode == OB_MODE_PARTICLE_EDIT) {
413 PE_create_current(depsgraph, scene, ob);
414 }
415}
416
417void PE_hide_keys_time(Scene *scene, PTCacheEdit *edit, float cfra)
418{
419 ParticleEditSettings *pset = PE_settings(scene);
420 POINT_P;
421 KEY_K;
422
423 if (pset->flag & PE_FADE_TIME && pset->selectmode == SCE_SELECT_POINT) {
425 LOOP_KEYS {
426 if (fabsf(cfra - *key->time) < pset->fade_frames) {
427 key->flag &= ~PEK_HIDE;
428 }
429 else {
430 key->flag |= PEK_HIDE;
431 // key->flag &= ~PEK_SELECT;
432 }
433 }
434 }
435 }
436 else {
438 LOOP_KEYS {
439 key->flag &= ~PEK_HIDE;
440 }
441 }
442 }
443}
444
445static int pe_x_mirror(Object *ob)
446{
447 if (ob->type == OB_MESH) {
448 return ((id_cast<Mesh *>(ob->data))->symmetry & ME_SYMMETRY_X);
449 }
450
451 return 0;
452}
453
455
456/* -------------------------------------------------------------------- */
459
502
504{
505 *data = {};
506
507 data->context = C;
508 data->bmain = CTX_data_main(C);
509 data->scene = CTX_data_scene(C);
510 data->view_layer = CTX_data_view_layer(C);
513 data->edit = PE_get_current(data->depsgraph, data->scene, data->ob);
514}
515
517{
519
520 data->vc = ED_view3d_viewcontext_init(C, data->depsgraph);
521
522 if (!XRAY_ENABLED(data->vc.v3d)) {
524 data->vc.region,
525 data->vc.v3d,
526 data->vc.obact,
528 false,
529 &data->depths);
530 }
531}
532
533static bool PE_create_shape_tree(PEData *data, Object *shapeob)
534{
535 Object *shapeob_eval = DEG_get_evaluated(data->depsgraph, shapeob);
536 const Mesh *mesh = BKE_object_get_evaluated_mesh(shapeob_eval);
537
538 if (!mesh) {
539 return false;
540 }
541
542 data->shape_bvh = MEM_new<bke::BVHTreeFromMesh>(__func__, mesh->bvh_corner_tris());
543 return data->shape_bvh->tree != nullptr;
544}
545
547{
548 MEM_delete(data->shape_bvh);
549}
550
552{
554 rng_seed ^= POINTER_AS_UINT(data->ob);
555 rng_seed ^= POINTER_AS_UINT(data->edit);
556 data->rng = BLI_rng_new(rng_seed);
557}
558
560{
561 if (data->rng != nullptr) {
562 BLI_rng_free(data->rng);
563 data->rng = nullptr;
564 }
565}
566
568{
571 if (data->depths) {
573 data->depths = nullptr;
574 }
575}
576
578
579/* -------------------------------------------------------------------- */
582
583static bool key_test_depth(const PEData *data, const float co[3], const int screen_co[2])
584{
585 View3D *v3d = data->vc.v3d;
586 ViewDepths *vd = data->depths;
587 float depth;
588
589 /* nothing to do */
590 if (XRAY_ENABLED(v3d)) {
591 return true;
592 }
593
594/* used to calculate here but all callers have the screen_co already, so pass as arg */
595#if 0
596 if (ED_view3d_project_int_global(data->vc.region,
597 co,
598 screen_co,
601 {
602 return 0;
603 }
604#endif
605
606 /* check if screen_co is within bounds because brush_cut uses out of screen coords */
607 if (screen_co[0] >= 0 && screen_co[0] < vd->w && screen_co[1] >= 0 && screen_co[1] < vd->h) {
608 BLI_assert(vd && vd->depths);
609 /* we know its not clipped */
610 depth = vd->depths[screen_co[1] * vd->w + screen_co[0]];
611 }
612 else {
613 return false;
614 }
615
616 float win[3];
617 ED_view3d_project_v3(data->vc.region, co, win);
618
619 if (win[2] - 0.00001f > depth) {
620 return false;
621 }
622 return true;
623}
624
625static bool key_inside_circle(const PEData *data, float rad, const float co[3], float *distance)
626{
627 float dx, dy, dist;
628 int screen_co[2];
629
630 /* TODO: should this check V3D_PROJ_TEST_CLIP_BB too? */
631 if (ED_view3d_project_int_global(data->vc.region, co, screen_co, V3D_PROJ_TEST_CLIP_WIN) !=
633 {
634 return false;
635 }
636
637 dx = data->mval[0] - screen_co[0];
638 dy = data->mval[1] - screen_co[1];
639 dist = sqrtf(dx * dx + dy * dy);
640
641 if (dist > rad) {
642 return false;
643 }
644
645 if (key_test_depth(data, co, screen_co)) {
646 if (distance) {
647 *distance = dist;
648 }
649
650 return true;
651 }
652
653 return false;
654}
655
656static bool key_inside_rect(PEData *data, const float co[3])
657{
658 int screen_co[2];
659
660 if (ED_view3d_project_int_global(data->vc.region, co, screen_co, V3D_PROJ_TEST_CLIP_WIN) !=
662 {
663 return false;
664 }
665
666 if (screen_co[0] > data->rect->xmin && screen_co[0] < data->rect->xmax &&
667 screen_co[1] > data->rect->ymin && screen_co[1] < data->rect->ymax)
668 {
669 return key_test_depth(data, co, screen_co);
670 }
671
672 return false;
673}
674
675static bool key_inside_test(PEData *data, const float co[3])
676{
677 if (data->mval) {
678 return key_inside_circle(data, data->rad, co, nullptr);
679 }
680 return key_inside_rect(data, co);
681}
682
684{
685 KEY_K;
686
687 if (point->flag & PEP_HIDE) {
688 return false;
689 }
690
692 return true;
693 }
694
695 return false;
696}
697
699
700/* -------------------------------------------------------------------- */
703
704using ForPointFunc = void (*)(PEData *data, int point_index);
705using ForHitPointFunc = void (*)(PEData *data, int point_index, float mouse_distance);
706
707using ForKeyFunc = void (*)(PEData *data, int point_index, int key_index, bool is_inside);
708
709using ForKeyMatFunc = void (*)(PEData *data,
710 const float mat[4][4],
711 const float imat[4][4],
712 int point_index,
713 int key_index,
714 PTCacheEditKey *key);
715using ForHitKeyMatFunc = void (*)(PEData *data,
716 float mat[4][4],
717 float imat[4][4],
718 int point_index,
719 int key_index,
720 PTCacheEditKey *key,
721 float mouse_distance);
722
724 PSEL_NEAREST = (1 << 0),
725 PSEL_ALL_KEYS = (1 << 1),
726};
727
728static void for_mouse_hit_keys(PEData *data, ForKeyFunc func, const enum eParticleSelectFlag flag)
729{
730 ParticleEditSettings *pset = PE_settings(data->scene);
731 PTCacheEdit *edit = data->edit;
732 POINT_P;
733 KEY_K;
734 int nearest_point, nearest_key;
735 float dist = data->rad;
736
737 /* in path select mode we have no keys */
738 if (pset->selectmode == SCE_SELECT_PATH) {
739 return;
740 }
741
742 nearest_point = -1;
743 nearest_key = -1;
744
746 if (pset->selectmode == SCE_SELECT_END) {
747 if (point->totkey) {
748 /* only do end keys */
749 key = point->keys + point->totkey - 1;
750
751 if (flag & PSEL_NEAREST) {
752 if (key_inside_circle(data, dist, KEY_WCO, &dist)) {
753 nearest_point = p;
754 nearest_key = point->totkey - 1;
755 }
756 }
757 else {
758 const bool is_inside = key_inside_test(data, KEY_WCO);
759 if (is_inside || (flag & PSEL_ALL_KEYS)) {
760 func(data, p, point->totkey - 1, is_inside);
761 }
762 }
763 }
764 }
765 else {
766 /* do all keys */
768 if (flag & PSEL_NEAREST) {
769 if (key_inside_circle(data, dist, KEY_WCO, &dist)) {
770 nearest_point = p;
771 nearest_key = k;
772 }
773 }
774 else {
775 const bool is_inside = key_inside_test(data, KEY_WCO);
776 if (is_inside || (flag & PSEL_ALL_KEYS)) {
777 func(data, p, k, is_inside);
778 }
779 }
780 }
781 }
782 }
783
784 /* do nearest only */
785 if (flag & PSEL_NEAREST) {
786 if (nearest_point != -1) {
787 func(data, nearest_point, nearest_key, true);
788 }
789 }
790}
791
792static void foreach_mouse_hit_point(PEData *data, ForHitPointFunc func, int selected)
793{
794 ParticleEditSettings *pset = PE_settings(data->scene);
795 PTCacheEdit *edit = data->edit;
796 POINT_P;
797 KEY_K;
798
799 /* all is selected in path mode */
800 if (pset->selectmode == SCE_SELECT_PATH) {
801 selected = 0;
802 }
803
805 if (pset->selectmode == SCE_SELECT_END) {
806 if (point->totkey) {
807 /* only do end keys */
808 key = point->keys + point->totkey - 1;
809
810 if (selected == 0 || key->flag & PEK_SELECT) {
811 float mouse_distance;
812 if (key_inside_circle(data, data->rad, KEY_WCO, &mouse_distance)) {
813 func(data, p, mouse_distance);
814 }
815 }
816 }
817 }
818 else {
819 /* do all keys */
821 if (selected == 0 || key->flag & PEK_SELECT) {
822 float mouse_distance;
823 if (key_inside_circle(data, data->rad, KEY_WCO, &mouse_distance)) {
824 func(data, p, mouse_distance);
825 break;
826 }
827 }
828 }
829 }
830 }
831}
832
839
840static void foreach_mouse_hit_key_iter(void *__restrict iter_data_v,
841 const int iter,
842 const TaskParallelTLS *__restrict /*tls*/)
843{
844 KeyIterData *iter_data = static_cast<KeyIterData *>(iter_data_v);
845 PEData *data = iter_data->data;
846 PTCacheEdit *edit = data->edit;
847 PTCacheEditPoint *point = &edit->points[iter];
848 if (point->flag & PEP_HIDE) {
849 return;
850 }
851 ParticleSystem *psys = edit->psys;
852 ParticleSystemModifierData *psmd_eval = iter_data->edit->psmd_eval;
853 ParticleEditSettings *pset = PE_settings(data->scene);
854 const int selected = iter_data->selected;
855 float mat[4][4], imat[4][4];
856 unit_m4(mat);
857 unit_m4(imat);
858 if (pset->selectmode == SCE_SELECT_END) {
859 if (point->totkey) {
860 /* only do end keys */
861 PTCacheEditKey *key = point->keys + point->totkey - 1;
862
863 if (selected == 0 || key->flag & PEK_SELECT) {
864 float mouse_distance;
865 if (key_inside_circle(data, data->rad, KEY_WCO, &mouse_distance)) {
866 if (edit->psys && !(edit->psys->flag & PSYS_GLOBAL_HAIR)) {
868 data->ob, psmd_eval->mesh_final, psys->part->from, psys->particles + iter, mat);
869 invert_m4_m4(imat, mat);
870 }
871 iter_data->func(data, mat, imat, iter, point->totkey - 1, key, mouse_distance);
872 }
873 }
874 }
875 }
876 else {
877 /* do all keys */
878 PTCacheEditKey *key;
879 int k;
881 if (selected == 0 || key->flag & PEK_SELECT) {
882 float mouse_distance;
883 if (key_inside_circle(data, data->rad, KEY_WCO, &mouse_distance)) {
884 if (edit->psys && !(edit->psys->flag & PSYS_GLOBAL_HAIR)) {
886 data->ob, psmd_eval->mesh_final, psys->part->from, psys->particles + iter, mat);
887 invert_m4_m4(imat, mat);
888 }
889 iter_data->func(data, mat, imat, iter, k, key, mouse_distance);
890 }
891 }
892 }
893 }
894}
895
896static void foreach_mouse_hit_key(PEData *data, ForHitKeyMatFunc func, int selected)
897{
898 PTCacheEdit *edit = data->edit;
899 ParticleEditSettings *pset = PE_settings(data->scene);
900 /* all is selected in path mode */
901 if (pset->selectmode == SCE_SELECT_PATH) {
902 selected = 0;
903 }
904
905 KeyIterData iter_data;
906 iter_data.data = data;
907 iter_data.edit = edit;
908 iter_data.selected = selected;
909 iter_data.func = func;
910
911 TaskParallelSettings settings;
913 BLI_task_parallel_range(0, edit->totpoint, &iter_data, foreach_mouse_hit_key_iter, &settings);
914}
915
917{
918 PTCacheEdit *edit = data->edit;
919 POINT_P;
920
922 func(data, p);
923 }
924}
925
927{
928 PTCacheEdit *edit = data->edit;
929 POINT_P;
930 KEY_K;
931
934 func(data, p, k, true);
935 }
936 }
937}
938
940{
941 PTCacheEdit *edit = data->edit;
942 POINT_P;
943
945 func(data, p);
946 }
947}
948
949static int count_selected_keys(Scene *scene, PTCacheEdit *edit)
950{
951 ParticleEditSettings *pset = PE_settings(scene);
952 POINT_P;
953 KEY_K;
954 int sel = 0;
955
957 if (pset->selectmode == SCE_SELECT_POINT) {
959 sel++;
960 }
961 }
962 else if (pset->selectmode == SCE_SELECT_END) {
963 if (point->totkey) {
964 key = point->keys + point->totkey - 1;
965 if (key->flag & PEK_SELECT) {
966 sel++;
967 }
968 }
969 }
970 }
971
972 return sel;
973}
974
976
977/* -------------------------------------------------------------------- */
980
982{
983 PTCacheEdit *edit;
985 KDTree<float3> *tree;
986 KDTreeNearest<float3> nearest;
987 HairKey *key;
989 float mat[4][4], co[3];
990 int index, totpart;
991
992 edit = psys->edit;
993 psmd_eval = edit->psmd_eval;
994 totpart = psys->totpart;
995
996 if (!psmd_eval->mesh_final) {
997 return;
998 }
999
1000 tree = kdtree_new<float3>(totpart);
1001
1002 /* Insert particles into KD-tree. */
1004 {
1005 key = pa->hair;
1006 psys_mat_hair_to_orco(ob, psmd_eval->mesh_final, psys->part->from, pa, mat);
1007 copy_v3_v3(co, key->co);
1008 mul_m4_v3(mat, co);
1009 kdtree_insert<float3>(tree, p, co);
1010 }
1011
1013
1014 /* lookup particles and set in mirror cache */
1015 if (!edit->mirror_cache) {
1016 edit->mirror_cache = MEM_new_array_zeroed<int>(totpart, "PE mirror cache");
1017 }
1018
1020 {
1021 key = pa->hair;
1022 psys_mat_hair_to_orco(ob, psmd_eval->mesh_final, psys->part->from, pa, mat);
1023 copy_v3_v3(co, key->co);
1024 mul_m4_v3(mat, co);
1025 co[0] = -co[0];
1026
1027 index = kdtree_find_nearest<float3>(tree, co, &nearest);
1028
1029 /* this needs a custom threshold still, duplicated for editmode mirror */
1030 if (index != -1 && index != p && (nearest.dist <= 0.0002f)) {
1031 edit->mirror_cache[p] = index;
1032 }
1033 else {
1034 edit->mirror_cache[p] = -1;
1035 }
1036 }
1037
1038 /* make sure mirrors are in two directions */
1040 {
1041 if (edit->mirror_cache[p]) {
1042 index = edit->mirror_cache[p];
1043 if (edit->mirror_cache[index] != p) {
1044 edit->mirror_cache[p] = -1;
1045 }
1046 }
1047 }
1048
1049 kdtree_free<float3>(tree);
1050}
1051
1053 Object *ob, Mesh *mesh, ParticleSystem *psys, ParticleData *pa, ParticleData *mpa)
1054{
1055 HairKey *hkey, *mhkey;
1056 PTCacheEditPoint *point, *mpoint;
1057 PTCacheEditKey *key, *mkey;
1058 PTCacheEdit *edit;
1059 float mat[4][4], mmat[4][4], immat[4][4];
1060 int i, mi, k;
1061
1062 edit = psys->edit;
1063 i = pa - psys->particles;
1064
1065 /* find mirrored particle if needed */
1066 if (!mpa) {
1067 if (!edit->mirror_cache) {
1068 PE_update_mirror_cache(ob, psys);
1069 }
1070
1071 if (!edit->mirror_cache) {
1072 return; /* something went wrong! */
1073 }
1074
1075 mi = edit->mirror_cache[i];
1076 if (mi == -1) {
1077 return;
1078 }
1079 mpa = psys->particles + mi;
1080 }
1081 else {
1082 mi = mpa - psys->particles;
1083 }
1084
1085 point = edit->points + i;
1086 mpoint = edit->points + mi;
1087
1088 /* make sure they have the same amount of keys */
1089 if (pa->totkey != mpa->totkey) {
1090 if (mpa->hair) {
1091 MEM_delete(mpa->hair);
1092 }
1093 if (mpoint->keys) {
1094 MEM_delete(mpoint->keys);
1095 }
1096
1097 mpa->hair = MEM_dupalloc(pa->hair);
1098 mpa->totkey = pa->totkey;
1099 mpoint->keys = MEM_dupalloc(point->keys);
1100 mpoint->totkey = point->totkey;
1101
1102 mhkey = mpa->hair;
1103 mkey = mpoint->keys;
1104 for (k = 0; k < mpa->totkey; k++, mkey++, mhkey++) {
1105 mkey->co = mhkey->co;
1106 mkey->time = &mhkey->time;
1107 mkey->flag &= ~PEK_SELECT;
1108 }
1109 }
1110
1111 /* mirror positions and tags */
1112 psys_mat_hair_to_orco(ob, mesh, psys->part->from, pa, mat);
1113 psys_mat_hair_to_orco(ob, mesh, psys->part->from, mpa, mmat);
1114 invert_m4_m4(immat, mmat);
1115
1116 hkey = pa->hair;
1117 mhkey = mpa->hair;
1118 key = point->keys;
1119 mkey = mpoint->keys;
1120 for (k = 0; k < pa->totkey; k++, hkey++, mhkey++, key++, mkey++) {
1121 copy_v3_v3(mhkey->co, hkey->co);
1122 mul_m4_v3(mat, mhkey->co);
1123 mhkey->co[0] = -mhkey->co[0];
1124 mul_m4_v3(immat, mhkey->co);
1125
1126 if (key->flag & PEK_TAG) {
1127 mkey->flag |= PEK_TAG;
1128 }
1129
1130 mkey->length = key->length;
1131 }
1132
1133 if (point->flag & PEP_TAG) {
1134 mpoint->flag |= PEP_TAG;
1135 }
1136 if (point->flag & PEP_EDIT_RECALC) {
1137 mpoint->flag |= PEP_EDIT_RECALC;
1138 }
1139}
1140
1142{
1143 PTCacheEdit *edit;
1144 ParticleSystemModifierData *psmd_eval;
1145 POINT_P;
1146
1147 if (!psys) {
1148 return;
1149 }
1150
1151 edit = psys->edit;
1152 psmd_eval = edit->psmd_eval;
1153
1154 if (psmd_eval == nullptr || psmd_eval->mesh_final == nullptr) {
1155 return;
1156 }
1157
1158 if (!edit->mirror_cache) {
1159 PE_update_mirror_cache(ob, psys);
1160 }
1161
1162 if (!edit->mirror_cache) {
1163 return; /* something went wrong */
1164 }
1165
1166 /* we delay settings the PARS_EDIT_RECALC for mirrored particles
1167 * to avoid doing mirror twice */
1168 LOOP_POINTS {
1169 if (point->flag & PEP_EDIT_RECALC) {
1170 PE_mirror_particle(ob, psmd_eval->mesh_final, psys, psys->particles + p, nullptr);
1171
1172 if (edit->mirror_cache[p] != -1) {
1173 edit->points[edit->mirror_cache[p]].flag &= ~PEP_EDIT_RECALC;
1174 }
1175 }
1176 }
1177
1178 LOOP_POINTS {
1179 if (point->flag & PEP_EDIT_RECALC) {
1180 if (edit->mirror_cache[p] != -1) {
1181 edit->points[edit->mirror_cache[p]].flag |= PEP_EDIT_RECALC;
1182 }
1183 }
1184 }
1185}
1186
1188
1189/* -------------------------------------------------------------------- */
1192
1200
1201static void deflect_emitter_iter(void *__restrict iter_data_v,
1202 const int iter,
1203 const TaskParallelTLS *__restrict /*tls*/)
1204{
1205 DeflectEmitterIter *iter_data = static_cast<DeflectEmitterIter *>(iter_data_v);
1206 PTCacheEdit *edit = iter_data->edit;
1207 PTCacheEditPoint *point = &edit->points[iter];
1208 if ((point->flag & PEP_EDIT_RECALC) == 0) {
1209 return;
1210 }
1211 Object *object = iter_data->object;
1212 ParticleSystem *psys = iter_data->psys;
1213 ParticleSystemModifierData *psmd_eval = iter_data->edit->psmd_eval;
1214 PTCacheEditKey *key;
1215 int k;
1216 float hairimat[4][4], hairmat[4][4];
1217 int index;
1218 float *vec, *nor, dvec[3], dot, dist_1st = 0.0f;
1219 const float dist = iter_data->dist;
1220 const float emitterdist = iter_data->emitterdist;
1222 object, psmd_eval->mesh_final, psys->part->from, psys->particles + iter, hairmat);
1223
1224 LOOP_KEYS {
1225 mul_m4_v3(hairmat, key->co);
1226 }
1227
1228 LOOP_KEYS {
1229 if (k == 0) {
1230 dist_1st = len_v3v3((key + 1)->co, key->co);
1231 dist_1st *= dist * emitterdist;
1232 }
1233 else {
1234 index = kdtree_find_nearest<float3>(edit->emitter_field, key->co, nullptr);
1235
1236 vec = edit->emitter_cosnos + index * 6;
1237 nor = vec + 3;
1238
1239 sub_v3_v3v3(dvec, key->co, vec);
1240
1241 dot = dot_v3v3(dvec, nor);
1242 copy_v3_v3(dvec, nor);
1243
1244 if (dot > 0.0f) {
1245 if (dot < dist_1st) {
1246 normalize_v3(dvec);
1247 mul_v3_fl(dvec, dist_1st - dot);
1248 add_v3_v3(key->co, dvec);
1249 }
1250 }
1251 else {
1252 normalize_v3(dvec);
1253 mul_v3_fl(dvec, dist_1st - dot);
1254 add_v3_v3(key->co, dvec);
1255 }
1256 if (k == 1) {
1257 dist_1st *= 1.3333f;
1258 }
1259 }
1260 }
1261
1262 invert_m4_m4(hairimat, hairmat);
1263
1264 LOOP_KEYS {
1265 mul_m4_v3(hairimat, key->co);
1266 }
1267}
1268
1269/* tries to stop edited particles from going through the emitter's surface */
1270static void pe_deflect_emitter(Scene *scene, Object *ob, PTCacheEdit *edit)
1271{
1272 ParticleEditSettings *pset = PE_settings(scene);
1273 ParticleSystem *psys;
1274 const float dist = ED_view3d_select_dist_px() * 0.01f;
1275
1276 if (edit == nullptr || edit->psys == nullptr || (pset->flag & PE_DEFLECT_EMITTER) == 0 ||
1277 (edit->psys->flag & PSYS_GLOBAL_HAIR))
1278 {
1279 return;
1280 }
1281
1282 psys = edit->psys;
1283
1284 if (edit->psmd_eval == nullptr || edit->psmd_eval->mesh_final == nullptr) {
1285 return;
1286 }
1287
1288 DeflectEmitterIter iter_data;
1289 iter_data.object = ob;
1290 iter_data.psys = psys;
1291 iter_data.edit = edit;
1292 iter_data.dist = dist;
1293 iter_data.emitterdist = pset->emitterdist;
1294
1295 TaskParallelSettings settings;
1297 BLI_task_parallel_range(0, edit->totpoint, &iter_data, deflect_emitter_iter, &settings);
1298}
1299
1303
1304static void apply_lengths_iter(void *__restrict iter_data_v,
1305 const int iter,
1306 const TaskParallelTLS *__restrict /*tls*/)
1307{
1308 ApplyLengthsIterData *iter_data = static_cast<ApplyLengthsIterData *>(iter_data_v);
1309 PTCacheEdit *edit = iter_data->edit;
1310 PTCacheEditPoint *point = &edit->points[iter];
1311 if ((point->flag & PEP_EDIT_RECALC) == 0) {
1312 return;
1313 }
1314 PTCacheEditKey *key;
1315 int k;
1316 LOOP_KEYS {
1317 if (k) {
1318 float dv1[3];
1319 sub_v3_v3v3(dv1, key->co, (key - 1)->co);
1320 normalize_v3(dv1);
1321 mul_v3_fl(dv1, (key - 1)->length);
1322 add_v3_v3v3(key->co, (key - 1)->co, dv1);
1323 }
1324 }
1325}
1326
1327/* force set distances between neighboring keys */
1328static void PE_apply_lengths(Scene *scene, PTCacheEdit *edit)
1329{
1330 ParticleEditSettings *pset = PE_settings(scene);
1331
1332 if (edit == nullptr || (pset->flag & PE_KEEP_LENGTHS) == 0) {
1333 return;
1334 }
1335
1336 if (edit->psys && edit->psys->flag & PSYS_GLOBAL_HAIR) {
1337 return;
1338 }
1339
1340 ApplyLengthsIterData iter_data;
1341 iter_data.edit = edit;
1342
1343 TaskParallelSettings settings;
1345 BLI_task_parallel_range(0, edit->totpoint, &iter_data, apply_lengths_iter, &settings);
1346}
1347
1352
1353static void iterate_lengths_iter(void *__restrict iter_data_v,
1354 const int iter,
1355 const TaskParallelTLS *__restrict /*tls*/)
1356{
1357 IterateLengthsIterData *iter_data = static_cast<IterateLengthsIterData *>(iter_data_v);
1358 PTCacheEdit *edit = iter_data->edit;
1359 PTCacheEditPoint *point = &edit->points[iter];
1360 if ((point->flag & PEP_EDIT_RECALC) == 0) {
1361 return;
1362 }
1363 ParticleEditSettings *pset = iter_data->pset;
1364 float tlen;
1365 float dv0[3] = {0.0f, 0.0f, 0.0f};
1366 float dv1[3] = {0.0f, 0.0f, 0.0f};
1367 float dv2[3] = {0.0f, 0.0f, 0.0f};
1368 for (int j = 1; j < point->totkey; j++) {
1369 PTCacheEditKey *key;
1370 int k;
1371 float mul = 1.0f / float(point->totkey);
1372 if (pset->flag & PE_LOCK_FIRST) {
1373 key = point->keys + 1;
1374 k = 1;
1375 dv1[0] = dv1[1] = dv1[2] = 0.0;
1376 }
1377 else {
1378 key = point->keys;
1379 k = 0;
1380 dv0[0] = dv0[1] = dv0[2] = 0.0;
1381 }
1382
1383 for (; k < point->totkey; k++, key++) {
1384 if (k) {
1385 sub_v3_v3v3(dv0, (key - 1)->co, key->co);
1386 tlen = normalize_v3(dv0);
1387 mul_v3_fl(dv0, (mul * (tlen - (key - 1)->length)));
1388 }
1389 if (k < point->totkey - 1) {
1390 sub_v3_v3v3(dv2, (key + 1)->co, key->co);
1391 tlen = normalize_v3(dv2);
1392 mul_v3_fl(dv2, mul * (tlen - key->length));
1393 }
1394 if (k) {
1395 add_v3_v3((key - 1)->co, dv1);
1396 }
1397 add_v3_v3v3(dv1, dv0, dv2);
1398 }
1399 }
1400}
1401
1402/* try to find a nice solution to keep distances between neighboring keys */
1403static void pe_iterate_lengths(Scene *scene, PTCacheEdit *edit)
1404{
1405 ParticleEditSettings *pset = PE_settings(scene);
1406 if (edit == nullptr || (pset->flag & PE_KEEP_LENGTHS) == 0) {
1407 return;
1408 }
1409 if (edit->psys && edit->psys->flag & PSYS_GLOBAL_HAIR) {
1410 return;
1411 }
1412
1413 IterateLengthsIterData iter_data;
1414 iter_data.edit = edit;
1415 iter_data.pset = pset;
1416
1417 TaskParallelSettings settings;
1419 BLI_task_parallel_range(0, edit->totpoint, &iter_data, iterate_lengths_iter, &settings);
1420}
1421
1423{
1424 POINT_P;
1425 KEY_K;
1426
1427 if (edit == nullptr) {
1428 return;
1429 }
1430
1432 key = point->keys;
1433 for (k = 0; k < point->totkey - 1; k++, key++) {
1434 key->length = len_v3v3(key->co, (key + 1)->co);
1435 }
1436 }
1437}
1438
1439void recalc_emitter_field(Depsgraph * /*depsgraph*/, Object * /*ob*/, ParticleSystem *psys)
1440{
1441 PTCacheEdit *edit = psys->edit;
1442 Mesh *mesh = edit->psmd_eval->mesh_final;
1443 float *vec, *nor;
1444 int i, totface;
1445
1446 if (!mesh) {
1447 return;
1448 }
1449
1450 if (edit->emitter_cosnos) {
1451 MEM_delete(edit->emitter_cosnos);
1452 }
1453
1455
1456 totface = mesh->totface_legacy;
1457 // int totvert = dm->getNumVerts(dm); /* UNUSED */
1458
1459 edit->emitter_cosnos = MEM_new_array_zeroed<float>(6 * totface, "emitter cosnos");
1460
1461 edit->emitter_field = kdtree_new<float3>(totface);
1462
1463 vec = edit->emitter_cosnos;
1464 nor = vec + 3;
1465
1466 const Span<float3> positions = mesh->vert_positions();
1467 const Span<float3> vert_normals = mesh->vert_normals();
1468 const MFace *mfaces = static_cast<const MFace *>(
1469 CustomData_get_layer(&mesh->fdata_legacy, CD_MFACE));
1470 for (i = 0; i < totface; i++, vec += 6, nor += 6) {
1471 const MFace *mface = &mfaces[i];
1472
1473 copy_v3_v3(vec, positions[mface->v1]);
1474 copy_v3_v3(nor, vert_normals[mface->v1]);
1475
1476 add_v3_v3v3(vec, vec, positions[mface->v2]);
1477 add_v3_v3(nor, vert_normals[mface->v2]);
1478
1479 add_v3_v3v3(vec, vec, positions[mface->v3]);
1480 add_v3_v3(nor, vert_normals[mface->v3]);
1481
1482 if (mface->v4) {
1483 add_v3_v3v3(vec, vec, positions[mface->v4]);
1484 add_v3_v3(nor, vert_normals[mface->v4]);
1485
1486 mul_v3_fl(vec, 0.25);
1487 }
1488 else {
1489 mul_v3_fl(vec, 1.0f / 3.0f);
1490 }
1491
1492 normalize_v3(nor);
1493
1494 kdtree_insert<float3>(edit->emitter_field, i, vec);
1495 }
1496
1498}
1499
1500static void PE_update_selection(Depsgraph *depsgraph, Scene *scene, Object *ob, int useflag)
1501{
1502 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
1503 HairKey *hkey;
1504 POINT_P;
1505 KEY_K;
1506
1507 /* flag all particles to be updated if not using flag */
1508 if (!useflag) {
1509 LOOP_POINTS {
1510 point->flag |= PEP_EDIT_RECALC;
1511 }
1512 }
1513
1514 /* flush edit key flag to hair key flag to preserve selection
1515 * on save */
1516 if (edit->psys) {
1517 LOOP_POINTS {
1518 hkey = edit->psys->particles[p].hair;
1519 LOOP_KEYS {
1520 hkey->editflag = key->flag;
1521 hkey++;
1522 }
1523 }
1524 }
1525
1526 psys_cache_edit_paths(depsgraph, scene, ob, edit, scene->r.cfra, G.is_rendering);
1527
1528 /* disable update flag */
1529 LOOP_POINTS {
1530 point->flag &= ~PEP_EDIT_RECALC;
1531 }
1532
1534}
1535
1537{
1538 ParticleSystem *psys = edit->psys;
1539 ParticleSystemModifierData *psmd_eval = edit->psmd_eval;
1540 POINT_P;
1541 KEY_K;
1542 float hairmat[4][4];
1543
1544 if (psys == nullptr || psys->edit == nullptr || psmd_eval == nullptr ||
1545 psmd_eval->mesh_final == nullptr)
1546 {
1547 return;
1548 }
1549
1550 LOOP_POINTS {
1551 if (!(psys->flag & PSYS_GLOBAL_HAIR)) {
1553 ob, psmd_eval->mesh_final, psys->part->from, psys->particles + p, hairmat);
1554 }
1555
1556 LOOP_KEYS {
1557 copy_v3_v3(key->world_co, key->co);
1558 if (!(psys->flag & PSYS_GLOBAL_HAIR)) {
1559 mul_m4_v3(hairmat, key->world_co);
1560 }
1561 }
1562 }
1563}
1565{
1566 /* TODO: get frs_sec properly. */
1567 float vec1[3], vec2[3], frs_sec, dfra;
1568 POINT_P;
1569 KEY_K;
1570
1571 /* hair doesn't use velocities */
1572 if (edit->psys || !edit->points || !edit->points->keys->vel) {
1573 return;
1574 }
1575
1576 frs_sec = edit->pid.flag & PTCACHE_VEL_PER_SEC ? 25.0f : 1.0f;
1577
1579 LOOP_KEYS {
1580 if (k == 0) {
1581 dfra = *(key + 1)->time - *key->time;
1582
1583 if (dfra <= 0.0f) {
1584 continue;
1585 }
1586
1587 sub_v3_v3v3(key->vel, (key + 1)->co, key->co);
1588
1589 if (point->totkey > 2) {
1590 sub_v3_v3v3(vec1, (key + 1)->co, (key + 2)->co);
1591 project_v3_v3v3(vec2, vec1, key->vel);
1592 sub_v3_v3v3(vec2, vec1, vec2);
1593 madd_v3_v3fl(key->vel, vec2, 0.5f);
1594 }
1595 }
1596 else if (k == point->totkey - 1) {
1597 dfra = *key->time - *(key - 1)->time;
1598
1599 if (dfra <= 0.0f) {
1600 continue;
1601 }
1602
1603 sub_v3_v3v3(key->vel, key->co, (key - 1)->co);
1604
1605 if (point->totkey > 2) {
1606 sub_v3_v3v3(vec1, (key - 2)->co, (key - 1)->co);
1607 project_v3_v3v3(vec2, vec1, key->vel);
1608 sub_v3_v3v3(vec2, vec1, vec2);
1609 madd_v3_v3fl(key->vel, vec2, 0.5f);
1610 }
1611 }
1612 else {
1613 dfra = *(key + 1)->time - *(key - 1)->time;
1614
1615 if (dfra <= 0.0f) {
1616 continue;
1617 }
1618
1619 sub_v3_v3v3(key->vel, (key + 1)->co, (key - 1)->co);
1620 }
1621 mul_v3_fl(key->vel, frs_sec / dfra);
1622 }
1623 }
1624}
1625
1626void PE_update_object(Depsgraph *depsgraph, Scene *scene, Object *ob, int useflag)
1627{
1628 /* use this to do partial particle updates, not usable when adding or
1629 * removing, then a full redo is necessary and calling this may crash */
1630 ParticleEditSettings *pset = PE_settings(scene);
1631 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
1632 POINT_P;
1633
1634 if (!edit) {
1635 return;
1636 }
1637
1638 /* flag all particles to be updated if not using flag */
1639 if (!useflag) {
1640 LOOP_POINTS {
1641 point->flag |= PEP_EDIT_RECALC;
1642 }
1643 }
1644
1645 /* do post process on particle edit keys */
1646 pe_iterate_lengths(scene, edit);
1647 pe_deflect_emitter(scene, ob, edit);
1648 PE_apply_lengths(scene, edit);
1649 if (pe_x_mirror(ob)) {
1650 PE_apply_mirror(ob, edit->psys);
1651 }
1652 if (edit->psys) {
1653 update_world_cos(ob, edit);
1654 }
1655 if (pset->flag & PE_AUTO_VELOCITY) {
1656 update_velocities(edit);
1657 }
1658
1659 /* Only do this for emitter particles because drawing PE_FADE_TIME is not respected in 2.8 yet
1660 * and flagging with PEK_HIDE will prevent selection. This might get restored once this is
1661 * supported in drawing (but doesn't make much sense for hair anyways). */
1662 if (edit->psys && edit->psys->part->type == PART_EMITTER) {
1663 PE_hide_keys_time(scene, edit, scene->r.cfra);
1664 }
1665
1666 /* regenerate path caches */
1667 psys_cache_edit_paths(depsgraph, scene, ob, edit, scene->r.cfra, G.is_rendering);
1668
1669 /* disable update flag */
1670 LOOP_POINTS {
1671 point->flag &= ~PEP_EDIT_RECALC;
1672 }
1673
1674 if (edit->psys) {
1675 edit->psys->flag &= ~PSYS_HAIR_UPDATED;
1676 }
1677}
1678
1680
1681/* -------------------------------------------------------------------- */
1684
1685static void select_key(PEData *data, int point_index, int key_index, bool /*is_inside*/)
1686{
1687 PTCacheEdit *edit = data->edit;
1688 PTCacheEditPoint *point = edit->points + point_index;
1689 PTCacheEditKey *key = point->keys + key_index;
1690
1691 if (data->select) {
1692 key->flag |= PEK_SELECT;
1693 }
1694 else {
1695 key->flag &= ~PEK_SELECT;
1696 }
1697
1698 point->flag |= PEP_EDIT_RECALC;
1699 data->is_changed = true;
1700}
1701
1702static void select_key_op(PEData *data, int point_index, int key_index, bool is_inside)
1703{
1704 PTCacheEdit *edit = data->edit;
1705 PTCacheEditPoint *point = edit->points + point_index;
1706 PTCacheEditKey *key = point->keys + key_index;
1707 const bool is_select = key->flag & PEK_SELECT;
1708 const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
1709 if (sel_op_result != -1) {
1710 SET_FLAG_FROM_TEST(key->flag, sel_op_result, PEK_SELECT);
1711 point->flag |= PEP_EDIT_RECALC;
1712 data->is_changed = true;
1713 }
1714}
1715
1716static void select_keys(PEData *data, int point_index, int /*key_index*/, bool /*is_inside*/)
1717{
1718 PTCacheEdit *edit = data->edit;
1719 PTCacheEditPoint *point = edit->points + point_index;
1720 KEY_K;
1721
1722 LOOP_KEYS {
1723 if (data->select) {
1724 key->flag |= PEK_SELECT;
1725 }
1726 else {
1727 key->flag &= ~PEK_SELECT;
1728 }
1729 }
1730
1731 point->flag |= PEP_EDIT_RECALC;
1732}
1733
1735
1736/* -------------------------------------------------------------------- */
1739
1740static bool select_action_apply(PTCacheEditPoint *point, PTCacheEditKey *key, int action)
1741{
1742 bool changed = false;
1743 switch (action) {
1744 case SEL_SELECT:
1745 if ((key->flag & PEK_SELECT) == 0) {
1746 key->flag |= PEK_SELECT;
1747 point->flag |= PEP_EDIT_RECALC;
1748 changed = true;
1749 }
1750 break;
1751 case SEL_DESELECT:
1752 if (key->flag & PEK_SELECT) {
1753 key->flag &= ~PEK_SELECT;
1754 point->flag |= PEP_EDIT_RECALC;
1755 changed = true;
1756 }
1757 break;
1758 case SEL_INVERT:
1759 if ((key->flag & PEK_SELECT) == 0) {
1760 key->flag |= PEK_SELECT;
1761 point->flag |= PEP_EDIT_RECALC;
1762 changed = true;
1763 }
1764 else {
1765 key->flag &= ~PEK_SELECT;
1766 point->flag |= PEP_EDIT_RECALC;
1767 changed = true;
1768 }
1769 break;
1770 }
1771 return changed;
1772}
1773
1775{
1776 Scene *scene = CTX_data_scene(C);
1777 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
1779 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
1780 POINT_P;
1781 KEY_K;
1782 int action = RNA_enum_get(op->ptr, "action");
1783
1784 if (action == SEL_TOGGLE) {
1785 action = SEL_SELECT;
1788 action = SEL_DESELECT;
1789 break;
1790 }
1791
1792 if (action == SEL_DESELECT) {
1793 break;
1794 }
1795 }
1796 }
1797
1798 bool changed = false;
1801 changed |= select_action_apply(point, key, action);
1802 }
1803 }
1804
1805 if (changed) {
1806 PE_update_selection(depsgraph, scene, ob, 1);
1808 }
1809 return OPERATOR_FINISHED;
1810}
1811
1813{
1814 /* identifiers */
1815 ot->name = "(De)select All";
1816 ot->idname = "PARTICLE_OT_select_all";
1817 ot->description = "(De)select all particles' keys";
1818
1819 /* API callbacks. */
1821 ot->poll = PE_poll;
1822
1823 /* flags */
1825
1827}
1828
1830
1831/* -------------------------------------------------------------------- */
1834
1839
1840static void nearest_key_fn(PEData *data, int point_index, int key_index, bool /*is_inside*/)
1841{
1842 PTCacheEdit *edit = data->edit;
1843 PTCacheEditPoint *point = edit->points + point_index;
1844 PTCacheEditKey *key = point->keys + key_index;
1845
1846 NearestParticleData *user_data = static_cast<NearestParticleData *>(data->user_data);
1847 user_data->point = point;
1848 user_data->key = key;
1849 data->is_changed = true;
1850}
1851
1853 const int mval[2],
1854 PTCacheEditPoint **r_point,
1855 PTCacheEditKey **r_key)
1856{
1857 NearestParticleData user_data = {nullptr};
1858
1859 PEData data;
1861 data.mval = mval;
1863
1864 data.user_data = &user_data;
1866 bool found = data.is_changed;
1868
1869 *r_point = user_data.point;
1870 *r_key = user_data.key;
1871 return found;
1872}
1873
1874bool PE_mouse_particles(bContext *C, const int mval[2], const SelectPick_Params &params)
1875{
1876 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
1877 Scene *scene = CTX_data_scene(C);
1879
1880 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
1881
1882 if (!PE_start_edit(edit)) {
1883 return false;
1884 }
1885
1886 PTCacheEditPoint *point;
1887 PTCacheEditKey *key;
1888
1889 bool changed = false;
1890 bool found = pe_nearest_point_and_key(C, mval, &point, &key);
1891
1892 if (params.sel_op == SEL_OP_SET) {
1893 if ((found && params.select_passthrough) && (key->flag & PEK_SELECT)) {
1894 found = false;
1895 }
1896 else if (found || params.deselect_all) {
1897 /* Deselect everything. */
1898 changed |= PE_deselect_all_visible_ex(edit);
1899 }
1900 }
1901
1902 if (found) {
1903 switch (params.sel_op) {
1904 case SEL_OP_ADD: {
1905 if ((key->flag & PEK_SELECT) == 0) {
1906 key->flag |= PEK_SELECT;
1907 point->flag |= PEP_EDIT_RECALC;
1908 changed = true;
1909 }
1910 break;
1911 }
1912 case SEL_OP_SUB: {
1913 if ((key->flag & PEK_SELECT) != 0) {
1914 key->flag &= ~PEK_SELECT;
1915 point->flag |= PEP_EDIT_RECALC;
1916 changed = true;
1917 }
1918 break;
1919 }
1920 case SEL_OP_XOR: {
1921 key->flag ^= PEK_SELECT;
1922 point->flag |= PEP_EDIT_RECALC;
1923 changed = true;
1924 break;
1925 }
1926 case SEL_OP_SET: {
1927 if ((key->flag & PEK_SELECT) == 0) {
1928 key->flag |= PEK_SELECT;
1929 point->flag |= PEP_EDIT_RECALC;
1930 changed = true;
1931 }
1932 break;
1933 }
1934 case SEL_OP_AND: {
1935 BLI_assert_unreachable(); /* Doesn't make sense for picking. */
1936 break;
1937 }
1938 }
1939 }
1940
1941 if (changed) {
1942 PE_update_selection(depsgraph, scene, ob, 1);
1944 }
1945
1946 return changed || found;
1947}
1948
1950
1951/* -------------------------------------------------------------------- */
1954
1955static void select_root(PEData *data, int point_index)
1956{
1957 PTCacheEditPoint *point = data->edit->points + point_index;
1958 PTCacheEditKey *key = point->keys;
1959
1960 if (point->flag & PEP_HIDE) {
1961 return;
1962 }
1963
1964 if (data->select_action != SEL_TOGGLE) {
1965 data->is_changed = select_action_apply(point, key, data->select_action);
1966 }
1967 else if (key->flag & PEK_SELECT) {
1968 data->select_toggle_action = SEL_DESELECT;
1969 }
1970}
1971
1973{
1974 PEData data;
1975 int action = RNA_enum_get(op->ptr, "action");
1976
1977 PE_set_data(C, &data);
1978
1979 if (action == SEL_TOGGLE) {
1980 data.select_action = SEL_TOGGLE;
1981 data.select_toggle_action = SEL_SELECT;
1982
1984
1985 action = data.select_toggle_action;
1986 }
1987
1988 data.select_action = action;
1990
1991 if (data.is_changed) {
1992 PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
1994 }
1995 return OPERATOR_FINISHED;
1996}
1997
1999{
2000 /* identifiers */
2001 ot->name = "Select Roots";
2002 ot->idname = "PARTICLE_OT_select_roots";
2003 ot->description = "Select roots of all visible particles";
2004
2005 /* API callbacks. */
2006 ot->exec = select_roots_exec;
2007 ot->poll = PE_poll;
2008
2009 /* flags */
2011
2012 /* properties */
2014}
2015
2017
2018/* -------------------------------------------------------------------- */
2021
2022static void select_tip(PEData *data, int point_index)
2023{
2024 PTCacheEditPoint *point = data->edit->points + point_index;
2025 PTCacheEditKey *key;
2026
2027 if (point->totkey == 0) {
2028 return;
2029 }
2030
2031 key = &point->keys[point->totkey - 1];
2032
2033 if (point->flag & PEP_HIDE) {
2034 return;
2035 }
2036
2037 if (data->select_action != SEL_TOGGLE) {
2038 data->is_changed = select_action_apply(point, key, data->select_action);
2039 }
2040 else if (key->flag & PEK_SELECT) {
2041 data->select_toggle_action = SEL_DESELECT;
2042 }
2043}
2044
2046{
2047 PEData data;
2048 int action = RNA_enum_get(op->ptr, "action");
2049
2050 PE_set_data(C, &data);
2051
2052 if (action == SEL_TOGGLE) {
2053 data.select_action = SEL_TOGGLE;
2054 data.select_toggle_action = SEL_SELECT;
2055
2057
2058 action = data.select_toggle_action;
2059 }
2060
2061 data.select_action = action;
2063
2064 if (data.is_changed) {
2065 PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
2067
2068 return OPERATOR_FINISHED;
2069 }
2070 return OPERATOR_CANCELLED;
2071}
2072
2074{
2075 /* identifiers */
2076 ot->name = "Select Tips";
2077 ot->idname = "PARTICLE_OT_select_tips";
2078 ot->description = "Select tips of all visible particles";
2079
2080 /* API callbacks. */
2081 ot->exec = select_tips_exec;
2082 ot->poll = PE_poll;
2083
2084 /* flags */
2086
2087 /* properties */
2089}
2090
2092
2093/* -------------------------------------------------------------------- */
2096
2098
2100 {RAN_HAIR, "HAIR", 0, "Hair", ""},
2101 {RAN_POINTS, "POINTS", 0, "Points", ""},
2102 {0, nullptr, 0, nullptr, nullptr},
2103};
2104
2106{
2107 PEData data;
2108 int type;
2109
2110 /* used by LOOP_VISIBLE_POINTS, LOOP_VISIBLE_KEYS and LOOP_KEYS */
2111 PTCacheEdit *edit;
2112 PTCacheEditPoint *point;
2113 PTCacheEditKey *key;
2114 int p;
2115 int k;
2116
2117 const float randfac = RNA_float_get(op->ptr, "ratio");
2119 const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
2120 RNG *rng;
2121
2122 type = RNA_enum_get(op->ptr, "type");
2123
2124 PE_set_data(C, &data);
2125 data.select_action = SEL_SELECT;
2126 edit = PE_get_current(data.depsgraph, data.scene, data.ob);
2127
2128 rng = BLI_rng_new_srandom(seed);
2129
2130 switch (type) {
2131 case RAN_HAIR:
2133 int flag = ((BLI_rng_get_float(rng) < randfac) == select) ? SEL_SELECT : SEL_DESELECT;
2134 LOOP_KEYS {
2135 data.is_changed |= select_action_apply(point, key, flag);
2136 }
2137 }
2138 break;
2139 case RAN_POINTS:
2142 int flag = ((BLI_rng_get_float(rng) < randfac) == select) ? SEL_SELECT : SEL_DESELECT;
2143 data.is_changed |= select_action_apply(point, key, flag);
2144 }
2145 }
2146 break;
2147 }
2148
2149 BLI_rng_free(rng);
2150
2151 if (data.is_changed) {
2152 PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
2154 }
2155 return OPERATOR_FINISHED;
2156}
2157
2159{
2160 /* identifiers */
2161 ot->name = "Select Random";
2162 ot->idname = "PARTICLE_OT_select_random";
2163 ot->description = "Select a randomly distributed set of hair or points";
2164
2165 /* API callbacks. */
2167 ot->poll = PE_poll;
2168
2169 /* flags */
2171
2172 /* properties */
2174 ot->prop = RNA_def_enum(ot->srna,
2175 "type",
2177 RAN_HAIR,
2178 "Type",
2179 "Select either hair or points");
2180}
2181
2183
2184/* -------------------------------------------------------------------- */
2187
2189{
2190 PEData data;
2191 PE_set_data(C, &data);
2192 data.select = true;
2193
2195
2196 PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
2198
2199 return OPERATOR_FINISHED;
2200}
2201
2203{
2204 /* identifiers */
2205 ot->name = "Select Linked All";
2206 ot->idname = "PARTICLE_OT_select_linked";
2207 ot->description = "Select all keys linked to already selected ones";
2208
2209 /* API callbacks. */
2211 ot->poll = PE_poll;
2212
2213 /* flags */
2215
2216 /* properties */
2217}
2218
2220{
2221 PEData data;
2222 int mval[2];
2223 int location[2];
2224
2225 RNA_int_get_array(op->ptr, "location", location);
2226 mval[0] = location[0];
2227 mval[1] = location[1];
2228
2230 data.mval = mval;
2231 data.rad = 75.0f;
2232 data.select = !RNA_boolean_get(op->ptr, "deselect");
2233
2235 PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
2238
2239 return OPERATOR_FINISHED;
2240}
2241
2243 wmOperator *op,
2244 const wmEvent *event)
2245{
2246 RNA_int_set_array(op->ptr, "location", event->mval);
2247 return select_linked_pick_exec(C, op);
2248}
2249
2251{
2252 /* identifiers */
2253 ot->name = "Select Linked";
2254 ot->idname = "PARTICLE_OT_select_linked_pick";
2255 ot->description = "Select nearest particle from mouse pointer";
2256
2257 /* API callbacks. */
2260 ot->poll = PE_poll_view3d;
2261
2262 /* flags */
2264
2265 /* properties */
2267 ot->srna, "deselect", false, "Deselect", "Deselect linked keys rather than selecting them");
2268 RNA_def_int_vector(ot->srna, "location", 2, nullptr, 0, INT_MAX, "Location", "", 0, 16384);
2269}
2270
2272
2273/* -------------------------------------------------------------------- */
2276
2278{
2279 bool changed = false;
2280 POINT_P;
2281 KEY_K;
2282
2285 if ((key->flag & PEK_SELECT) != 0) {
2286 key->flag &= ~PEK_SELECT;
2287 point->flag |= PEP_EDIT_RECALC;
2288 changed = true;
2289 }
2290 }
2291 }
2292 return changed;
2293}
2294
2296{
2297 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
2298 Scene *scene = CTX_data_scene(C);
2300 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
2301 if (!PE_start_edit(edit)) {
2302 return false;
2303 }
2304 return PE_deselect_all_visible_ex(edit);
2305}
2306
2307bool PE_box_select(bContext *C, const rcti *rect, const int sel_op)
2308{
2309 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
2310 Scene *scene = CTX_data_scene(C);
2312 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
2313 PEData data;
2314
2315 if (!PE_start_edit(edit)) {
2316 return false;
2317 }
2318
2320 data.rect = rect;
2321 data.sel_op = eSelectOp(sel_op);
2322
2323 if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
2324 data.is_changed = PE_deselect_all_visible_ex(edit);
2325 }
2326
2327 if (BLI_rcti_is_empty(rect)) {
2328 /* pass */
2329 }
2330 else {
2332 }
2333
2334 bool is_changed = data.is_changed;
2336
2337 if (is_changed) {
2338 PE_update_selection(depsgraph, scene, ob, 1);
2340 }
2341 return is_changed;
2342}
2343
2345
2346/* -------------------------------------------------------------------- */
2349
2351{
2352 PE_data_free(static_cast<PEData *>(data));
2353 MEM_delete(static_cast<PEData *>(data));
2354}
2355
2357{
2358 PEData *data = MEM_new_zeroed<PEData>(__func__);
2359 wm_userdata->data = data;
2361 wm_userdata->use_free = true;
2363}
2364
2366 bContext *C, wmGenericUserData *wm_userdata, const int sel_op, const int mval[2], float rad)
2367{
2369 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
2370 Scene *scene = CTX_data_scene(C);
2372 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
2373
2374 if (!PE_start_edit(edit)) {
2375 return false;
2376 }
2377
2378 if (wm_userdata->data == nullptr) {
2380 }
2381
2382 PEData *data = static_cast<PEData *>(wm_userdata->data);
2383 data->mval = mval;
2384 data->rad = rad;
2385 data->select = (sel_op != SEL_OP_SUB);
2386
2387 if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
2388 data->is_changed = PE_deselect_all_visible_ex(edit);
2389 }
2391
2392 if (data->is_changed) {
2393 PE_update_selection(depsgraph, scene, ob, 1);
2395 }
2396 return data->is_changed;
2397}
2398
2400
2401/* -------------------------------------------------------------------- */
2404
2405int PE_lasso_select(bContext *C, const int mcoords[][2], const int mcoords_len, const int sel_op)
2406{
2407 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
2408 Scene *scene = CTX_data_scene(C);
2410 ARegion *region = CTX_wm_region(C);
2411 ParticleEditSettings *pset = PE_settings(scene);
2412 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
2413 POINT_P;
2414 KEY_K;
2415 float co[3], mat[4][4];
2416 int screen_co[2];
2417
2418 PEData data;
2419
2420 unit_m4(mat);
2421
2422 if (!PE_start_edit(edit)) {
2423 return OPERATOR_CANCELLED;
2424 }
2425
2426 /* only for depths */
2428
2429 if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
2430 data.is_changed |= PE_deselect_all_visible_ex(edit);
2431 }
2432
2433 ParticleSystem *psys = edit->psys;
2434 ParticleSystemModifierData *psmd_eval = edit->psmd_eval;
2436 if (edit->psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
2438 ob, psmd_eval->mesh_final, psys->part->from, psys->particles + p, mat);
2439 }
2440
2441 if (pset->selectmode == SCE_SELECT_POINT) {
2443 copy_v3_v3(co, key->co);
2444 mul_m4_v3(mat, co);
2445 const bool is_select = key->flag & PEK_SELECT;
2446 const bool is_inside =
2447 ((ED_view3d_project_int_global(region, co, screen_co, V3D_PROJ_TEST_CLIP_WIN) ==
2448 V3D_PROJ_RET_OK) &&
2449 BLI_lasso_is_point_inside({reinterpret_cast<const int2 *>(mcoords), mcoords_len},
2450 screen_co[0],
2451 screen_co[1],
2452 IS_CLIPPED) &&
2453 key_test_depth(&data, co, screen_co));
2454 const int sel_op_result = ED_select_op_action_deselected(
2455 eSelectOp(sel_op), is_select, is_inside);
2456 if (sel_op_result != -1) {
2457 SET_FLAG_FROM_TEST(key->flag, sel_op_result, PEK_SELECT);
2458 point->flag |= PEP_EDIT_RECALC;
2459 data.is_changed = true;
2460 }
2461 }
2462 }
2463 else if (pset->selectmode == SCE_SELECT_END) {
2464 if (point->totkey) {
2465 key = point->keys + point->totkey - 1;
2466 copy_v3_v3(co, key->co);
2467 mul_m4_v3(mat, co);
2468 const bool is_select = key->flag & PEK_SELECT;
2469 const bool is_inside =
2470 ((ED_view3d_project_int_global(region, co, screen_co, V3D_PROJ_TEST_CLIP_WIN) ==
2471 V3D_PROJ_RET_OK) &&
2472 BLI_lasso_is_point_inside({reinterpret_cast<const int2 *>(mcoords), mcoords_len},
2473 screen_co[0],
2474 screen_co[1],
2475 IS_CLIPPED) &&
2476 key_test_depth(&data, co, screen_co));
2477 const int sel_op_result = ED_select_op_action_deselected(
2478 eSelectOp(sel_op), is_select, is_inside);
2479 if (sel_op_result != -1) {
2480 SET_FLAG_FROM_TEST(key->flag, sel_op_result, PEK_SELECT);
2481 point->flag |= PEP_EDIT_RECALC;
2482 data.is_changed = true;
2483 }
2484 }
2485 }
2486 }
2487
2488 bool is_changed = data.is_changed;
2490
2491 if (is_changed) {
2492 PE_update_selection(depsgraph, scene, ob, 1);
2494 return OPERATOR_FINISHED;
2495 }
2496 return OPERATOR_CANCELLED;
2497}
2498
2500
2501/* -------------------------------------------------------------------- */
2504
2506{
2508 Scene *scene = CTX_data_scene(C);
2509 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2510
2511 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
2512 POINT_P;
2513 KEY_K;
2514
2515 if (RNA_boolean_get(op->ptr, "unselected")) {
2517 point->flag |= PEP_HIDE;
2518 point->flag |= PEP_EDIT_RECALC;
2519
2520 LOOP_KEYS {
2521 key->flag &= ~PEK_SELECT;
2522 }
2523 }
2524 }
2525 else {
2527 point->flag |= PEP_HIDE;
2528 point->flag |= PEP_EDIT_RECALC;
2529
2530 LOOP_KEYS {
2531 key->flag &= ~PEK_SELECT;
2532 }
2533 }
2534 }
2535
2536 PE_update_selection(depsgraph, scene, ob, 1);
2538
2539 return OPERATOR_FINISHED;
2540}
2541
2543{
2544 /* identifiers */
2545 ot->name = "Hide Selected";
2546 ot->idname = "PARTICLE_OT_hide";
2547 ot->description = "Hide selected particles";
2548
2549 /* API callbacks. */
2550 ot->exec = hide_exec;
2551 ot->poll = PE_poll;
2552
2553 /* flags */
2555
2556 /* props */
2558 ot->srna, "unselected", false, "Unselected", "Hide unselected rather than selected");
2559}
2560
2562
2563/* -------------------------------------------------------------------- */
2566
2568{
2570 Scene *scene = CTX_data_scene(C);
2571 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2572 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
2573 const bool select = RNA_boolean_get(op->ptr, "select");
2574 POINT_P;
2575 KEY_K;
2576
2577 LOOP_POINTS {
2578 if (point->flag & PEP_HIDE) {
2579 point->flag &= ~PEP_HIDE;
2580 point->flag |= PEP_EDIT_RECALC;
2581
2582 LOOP_KEYS {
2584 }
2585 }
2586 }
2587
2588 PE_update_selection(depsgraph, scene, ob, 1);
2590
2591 return OPERATOR_FINISHED;
2592}
2593
2595{
2596 /* identifiers */
2597 ot->name = "Reveal";
2598 ot->idname = "PARTICLE_OT_reveal";
2599 ot->description = "Show hidden particles";
2600
2601 /* API callbacks. */
2602 ot->exec = reveal_exec;
2603 ot->poll = PE_poll;
2604
2605 /* flags */
2607
2608 /* props */
2609 RNA_def_boolean(ot->srna, "select", true, "Select", "");
2610}
2611
2613
2614/* -------------------------------------------------------------------- */
2617
2618static void select_less_keys(PEData *data, int point_index)
2619{
2620 PTCacheEdit *edit = data->edit;
2621 PTCacheEditPoint *point = edit->points + point_index;
2622 KEY_K;
2623
2625 if (k == 0) {
2626 if (((key + 1)->flag & PEK_SELECT) == 0) {
2627 key->flag |= PEK_TAG;
2628 }
2629 }
2630 else if (k == point->totkey - 1) {
2631 if (((key - 1)->flag & PEK_SELECT) == 0) {
2632 key->flag |= PEK_TAG;
2633 }
2634 }
2635 else {
2636 if ((((key - 1)->flag & (key + 1)->flag) & PEK_SELECT) == 0) {
2637 key->flag |= PEK_TAG;
2638 }
2639 }
2640 }
2641
2642 LOOP_KEYS {
2643 if ((key->flag & PEK_TAG) && (key->flag & PEK_SELECT)) {
2644 key->flag &= ~(PEK_TAG | PEK_SELECT);
2645 point->flag |= PEP_EDIT_RECALC; /* redraw selection only */
2646 data->is_changed = true;
2647 }
2648 }
2649}
2650
2652{
2653 PEData data;
2654
2655 PE_set_data(C, &data);
2657
2658 PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
2660
2661 return OPERATOR_FINISHED;
2662}
2663
2665{
2666 /* identifiers */
2667 ot->name = "Select Less";
2668 ot->idname = "PARTICLE_OT_select_less";
2669 ot->description = "Deselect boundary selected keys of each particle";
2670
2671 /* API callbacks. */
2672 ot->exec = select_less_exec;
2673 ot->poll = PE_poll;
2674
2675 /* flags */
2677}
2678
2680
2681/* -------------------------------------------------------------------- */
2684
2685static void select_more_keys(PEData *data, int point_index)
2686{
2687 PTCacheEdit *edit = data->edit;
2688 PTCacheEditPoint *point = edit->points + point_index;
2689 KEY_K;
2690
2691 LOOP_KEYS {
2692 if (key->flag & PEK_SELECT) {
2693 continue;
2694 }
2695
2696 if (k == 0) {
2697 if ((key + 1)->flag & PEK_SELECT) {
2698 key->flag |= PEK_TAG;
2699 }
2700 }
2701 else if (k == point->totkey - 1) {
2702 if ((key - 1)->flag & PEK_SELECT) {
2703 key->flag |= PEK_TAG;
2704 }
2705 }
2706 else {
2707 if (((key - 1)->flag | (key + 1)->flag) & PEK_SELECT) {
2708 key->flag |= PEK_TAG;
2709 }
2710 }
2711 }
2712
2713 LOOP_KEYS {
2714 if ((key->flag & PEK_TAG) && (key->flag & PEK_SELECT) == 0) {
2715 key->flag &= ~PEK_TAG;
2716 key->flag |= PEK_SELECT;
2717 point->flag |= PEP_EDIT_RECALC; /* redraw selection only */
2718 data->is_changed = true;
2719 }
2720 }
2721}
2722
2724{
2725 PEData data;
2726
2727 PE_set_data(C, &data);
2729
2730 PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
2732
2733 return OPERATOR_FINISHED;
2734}
2735
2737{
2738 /* identifiers */
2739 ot->name = "Select More";
2740 ot->idname = "PARTICLE_OT_select_more";
2741 ot->description = "Select keys linked to boundary selected keys of each particle";
2742
2743 /* API callbacks. */
2744 ot->exec = select_more_exec;
2745 ot->poll = PE_poll;
2746
2747 /* flags */
2749}
2750
2752
2753/* -------------------------------------------------------------------- */
2756
2757static void rekey_particle(PEData *data, int pa_index)
2758{
2759 PTCacheEdit *edit = data->edit;
2760 ParticleSystem *psys = edit->psys;
2761 ParticleSimulationData sim = {nullptr};
2762 ParticleData *pa = psys->particles + pa_index;
2763 PTCacheEditPoint *point = edit->points + pa_index;
2765 HairKey *key, *new_keys, *okey;
2766 PTCacheEditKey *ekey;
2767 float dval, sta, end;
2768 int k;
2769
2770 sim.depsgraph = data->depsgraph;
2771 sim.scene = data->scene;
2772 sim.ob = data->ob;
2773 sim.psys = edit->psys;
2774
2775 pa->flag |= PARS_REKEY;
2776
2777 key = new_keys = MEM_new_array_zeroed<HairKey>(data->totrekey, "Hair re-key keys");
2778
2779 okey = pa->hair;
2780 /* root and tip stay the same */
2781 copy_v3_v3(key->co, okey->co);
2782 copy_v3_v3((key + data->totrekey - 1)->co, (okey + pa->totkey - 1)->co);
2783
2784 sta = key->time = okey->time;
2785 end = (key + data->totrekey - 1)->time = (okey + pa->totkey - 1)->time;
2786 dval = (end - sta) / float(data->totrekey - 1);
2787
2788 /* interpolate new keys from old ones */
2789 for (k = 1, key++; k < data->totrekey - 1; k++, key++) {
2790 state.time = float(k) / float(data->totrekey - 1);
2791 psys_get_particle_on_path(&sim, pa_index, &state, false);
2792 copy_v3_v3(key->co, state.co);
2793 key->time = sta + k * dval;
2794 }
2795
2796 /* replace keys */
2797 MEM_delete(pa->hair);
2798 pa->hair = new_keys;
2799
2800 point->totkey = pa->totkey = data->totrekey;
2801
2802 if (point->keys) {
2803 MEM_delete(point->keys);
2804 }
2805 ekey = point->keys = MEM_new_array_zeroed<PTCacheEditKey>(pa->totkey, "Hair re-key edit keys");
2806
2807 for (k = 0, key = pa->hair; k < pa->totkey; k++, key++, ekey++) {
2808 ekey->co = key->co;
2809 ekey->time = &key->time;
2810 ekey->flag |= PEK_SELECT;
2811 if (!(psys->flag & PSYS_GLOBAL_HAIR)) {
2812 ekey->flag |= PEK_USE_WCO;
2813 }
2814 }
2815
2816 pa->flag &= ~PARS_REKEY;
2817 point->flag |= PEP_EDIT_RECALC;
2818}
2819
2821{
2822 PEData data;
2823
2824 PE_set_data(C, &data);
2825
2826 data.dval = 1.0f / float(data.totrekey - 1);
2827 data.totrekey = RNA_int_get(op->ptr, "keys_number");
2828
2830
2831 recalc_lengths(data.edit);
2832 PE_update_object(data.depsgraph, data.scene, data.ob, 1);
2834
2835 return OPERATOR_FINISHED;
2836}
2837
2839{
2840 /* identifiers */
2841 ot->name = "Rekey";
2842 ot->idname = "PARTICLE_OT_rekey";
2843 ot->description = "Change the number of keys of selected particles (root and tip keys included)";
2844
2845 /* API callbacks. */
2846 ot->exec = rekey_exec;
2848 ot->poll = PE_hair_poll;
2849
2850 /* flags */
2852
2853 /* properties */
2854 RNA_def_int(ot->srna, "keys_number", 2, 2, INT_MAX, "Number of Keys", "", 2, 100);
2855}
2856
2858 const bContext *C, Scene *scene, Object *ob, int pa_index, float path_time)
2859{
2860 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
2861 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
2862 ParticleSystem *psys;
2863 ParticleSimulationData sim = {nullptr};
2864 ParticleData *pa;
2866 HairKey *new_keys, *key;
2867 PTCacheEditKey *ekey;
2868 int k;
2869
2870 if (!edit || !edit->psys) {
2871 return;
2872 }
2873
2874 psys = edit->psys;
2875
2876 sim.depsgraph = depsgraph;
2877 sim.scene = scene;
2878 sim.ob = ob;
2879 sim.psys = psys;
2880
2881 pa = psys->particles + pa_index;
2882
2883 pa->flag |= PARS_REKEY;
2884
2885 key = new_keys = MEM_dupalloc(pa->hair);
2886
2887 /* interpolate new keys from old ones (roots stay the same) */
2888 for (k = 1, key++; k < pa->totkey; k++, key++) {
2889 state.time = path_time * float(k) / float(pa->totkey - 1);
2890 psys_get_particle_on_path(&sim, pa_index, &state, false);
2891 copy_v3_v3(key->co, state.co);
2892 }
2893
2894 /* replace hair keys */
2895 if (pa->hair) {
2896 MEM_delete(pa->hair);
2897 }
2898 pa->hair = new_keys;
2899
2900 /* update edit pointers */
2901 for (k = 0, key = pa->hair, ekey = edit->points[pa_index].keys; k < pa->totkey;
2902 k++, key++, ekey++)
2903 {
2904 ekey->co = key->co;
2905 ekey->time = &key->time;
2906 }
2907
2908 pa->flag &= ~PARS_REKEY;
2909}
2910
2912
2913/* -------------------------------------------------------------------- */
2916
2917static int remove_tagged_particles(Object *ob, ParticleSystem *psys, int mirror)
2918{
2919 PTCacheEdit *edit = psys->edit;
2920 ParticleData *pa, *npa = nullptr, *new_pars = nullptr;
2921 POINT_P;
2922 PTCacheEditPoint *npoint = nullptr, *new_points = nullptr;
2923 ParticleSystemModifierData *psmd_eval;
2924 int i, new_totpart = psys->totpart, removed = 0;
2925
2926 if (mirror) {
2927 /* mirror tags */
2928 psmd_eval = edit->psmd_eval;
2929
2931 PE_mirror_particle(ob, psmd_eval->mesh_final, psys, psys->particles + p, nullptr);
2932 }
2933 }
2934
2936 new_totpart--;
2937 removed++;
2938 }
2939
2940 if (new_totpart != psys->totpart) {
2941 if (new_totpart) {
2942 npa = new_pars = MEM_new_array<ParticleData>(new_totpart, "ParticleData array");
2943 npoint = new_points = MEM_new_array_zeroed<PTCacheEditPoint>(new_totpart,
2944 "PTCacheEditKey array");
2945
2946 if (ELEM(nullptr, new_pars, new_points)) {
2947 /* allocation error! */
2948 if (new_pars) {
2949 MEM_delete(new_pars);
2950 }
2951 if (new_points) {
2952 MEM_delete(new_points);
2953 }
2954 return 0;
2955 }
2956 }
2957
2958 pa = psys->particles;
2959 point = edit->points;
2960 for (i = 0; i < psys->totpart; i++, pa++, point++) {
2961 if (point->flag & PEP_TAG) {
2962 if (point->keys) {
2963 MEM_delete(point->keys);
2964 }
2965 if (pa->hair) {
2966 MEM_delete(pa->hair);
2967 }
2968 }
2969 else {
2970 memcpy(npa, pa, sizeof(ParticleData));
2971 memcpy(npoint, point, sizeof(PTCacheEditPoint));
2972 npa++;
2973 npoint++;
2974 }
2975 }
2976
2977 if (psys->particles) {
2978 MEM_delete(psys->particles);
2979 }
2980 psys->particles = new_pars;
2981
2982 if (edit->points) {
2983 MEM_delete(edit->points);
2984 }
2985 edit->points = new_points;
2986
2987 MEM_SAFE_DELETE(edit->mirror_cache);
2988
2989 if (psys->child) {
2990 MEM_delete(psys->child);
2991 psys->child = nullptr;
2992 psys->totchild = 0;
2993 }
2994
2995 edit->totpoint = psys->totpart = new_totpart;
2996 }
2997
2998 return removed;
2999}
3000
3001static void remove_tagged_keys(Depsgraph *depsgraph, Object *ob, ParticleSystem *psys)
3002{
3003 PTCacheEdit *edit = psys->edit;
3004 ParticleData *pa;
3005 HairKey *hkey, *nhkey, *new_hkeys = nullptr;
3006 POINT_P;
3007 KEY_K;
3008 PTCacheEditKey *nkey, *new_keys;
3009 short new_totkey;
3010
3011 if (pe_x_mirror(ob)) {
3012 /* mirror key tags */
3014 ParticleSystemModifierData *psmd_eval = reinterpret_cast<ParticleSystemModifierData *>(
3015 BKE_modifier_get_evaluated(depsgraph, ob, &psmd->modifier));
3016
3017 LOOP_POINTS {
3019 PE_mirror_particle(ob, psmd_eval->mesh_final, psys, psys->particles + p, nullptr);
3020 break;
3021 }
3022 }
3023 }
3024
3025 LOOP_POINTS {
3026 new_totkey = point->totkey;
3028 new_totkey--;
3029 }
3030 /* We can't have elements with less than two keys. */
3031 if (new_totkey < 2) {
3032 point->flag |= PEP_TAG;
3033 }
3034 }
3035 remove_tagged_particles(ob, psys, pe_x_mirror(ob));
3036
3037 LOOP_POINTS {
3038 pa = psys->particles + p;
3039 new_totkey = pa->totkey;
3040
3042 new_totkey--;
3043 }
3044
3045 if (new_totkey != pa->totkey) {
3046 nhkey = new_hkeys = MEM_new_array_zeroed<HairKey>(new_totkey, "HairKeys");
3047 nkey = new_keys = MEM_new_array_zeroed<PTCacheEditKey>(new_totkey, "particle edit keys");
3048
3049 hkey = pa->hair;
3050 LOOP_KEYS {
3051 while (key->flag & PEK_TAG && hkey < pa->hair + pa->totkey) {
3052 key++;
3053 hkey++;
3054 }
3055
3056 if (hkey < pa->hair + pa->totkey) {
3057 copy_v3_v3(nhkey->co, hkey->co);
3058 nhkey->editflag = hkey->editflag;
3059 nhkey->time = hkey->time;
3060 nhkey->weight = hkey->weight;
3061
3062 nkey->co = nhkey->co;
3063 nkey->time = &nhkey->time;
3064 /* these can be copied from old edit keys */
3065 nkey->flag = key->flag;
3066 nkey->ftime = key->ftime;
3067 nkey->length = key->length;
3068 copy_v3_v3(nkey->world_co, key->world_co);
3069 }
3070 nkey++;
3071 nhkey++;
3072 hkey++;
3073 }
3074
3075 if (pa->hair) {
3076 MEM_delete(pa->hair);
3077 }
3078
3079 if (point->keys) {
3080 MEM_delete(point->keys);
3081 }
3082
3083 pa->hair = new_hkeys;
3084 point->keys = new_keys;
3085
3086 point->totkey = pa->totkey = new_totkey;
3087
3088 /* flag for recalculating length */
3089 point->flag |= PEP_EDIT_RECALC;
3090 }
3091 }
3092}
3093
3095
3096/* -------------------------------------------------------------------- */
3099
3100/* works like normal edit mode subdivide, inserts keys between neighboring selected keys */
3101static void subdivide_particle(PEData *data, int pa_index)
3102{
3103 PTCacheEdit *edit = data->edit;
3104 ParticleSystem *psys = edit->psys;
3105 ParticleSimulationData sim = {nullptr};
3106 ParticleData *pa = psys->particles + pa_index;
3107 PTCacheEditPoint *point = edit->points + pa_index;
3109 HairKey *key, *nkey, *new_keys;
3110 PTCacheEditKey *ekey, *nekey, *new_ekeys;
3111
3112 int k;
3113 short totnewkey = 0;
3114 float endtime;
3115
3116 sim.depsgraph = data->depsgraph;
3117 sim.scene = data->scene;
3118 sim.ob = data->ob;
3119 sim.psys = edit->psys;
3120
3121 for (k = 0, ekey = point->keys; k < pa->totkey - 1; k++, ekey++) {
3122 if (ekey->flag & PEK_SELECT && (ekey + 1)->flag & PEK_SELECT) {
3123 totnewkey++;
3124 }
3125 }
3126
3127 if (totnewkey == 0) {
3128 return;
3129 }
3130
3131 pa->flag |= PARS_REKEY;
3132
3133 nkey = new_keys = MEM_new_array_zeroed<HairKey>((pa->totkey + totnewkey), "Hair subdivide keys");
3134 nekey = new_ekeys = MEM_new_array_zeroed<PTCacheEditKey>((pa->totkey + totnewkey),
3135 "Hair subdivide edit keys");
3136
3137 key = pa->hair;
3138 endtime = key[pa->totkey - 1].time;
3139
3140 for (k = 0, ekey = point->keys; k < pa->totkey - 1; k++, key++, ekey++) {
3141
3142 memcpy(nkey, key, sizeof(HairKey));
3143 memcpy(nekey, ekey, sizeof(PTCacheEditKey));
3144
3145 nekey->co = nkey->co;
3146 nekey->time = &nkey->time;
3147
3148 nkey++;
3149 nekey++;
3150
3151 if (ekey->flag & PEK_SELECT && (ekey + 1)->flag & PEK_SELECT) {
3152 nkey->time = (key->time + (key + 1)->time) * 0.5f;
3153 state.time = (endtime != 0.0f) ? nkey->time / endtime : 0.0f;
3154 psys_get_particle_on_path(&sim, pa_index, &state, false);
3155 copy_v3_v3(nkey->co, state.co);
3156
3157 nekey->co = nkey->co;
3158 nekey->time = &nkey->time;
3159 nekey->flag |= PEK_SELECT;
3160 if (!(psys->flag & PSYS_GLOBAL_HAIR)) {
3161 nekey->flag |= PEK_USE_WCO;
3162 }
3163
3164 nekey++;
3165 nkey++;
3166 }
3167 }
3168 /* Tip still not copied. */
3169 memcpy(nkey, key, sizeof(HairKey));
3170 memcpy(nekey, ekey, sizeof(PTCacheEditKey));
3171
3172 nekey->co = nkey->co;
3173 nekey->time = &nkey->time;
3174
3175 MEM_delete(pa->hair);
3176 pa->hair = new_keys;
3177
3178 if (point->keys) {
3179 MEM_delete(point->keys);
3180 }
3181 point->keys = new_ekeys;
3182
3183 point->totkey = pa->totkey = pa->totkey + totnewkey;
3184 point->flag |= PEP_EDIT_RECALC;
3185 pa->flag &= ~PARS_REKEY;
3186}
3187
3189{
3190 PEData data;
3191
3192 PE_set_data(C, &data);
3194
3195 recalc_lengths(data.edit);
3196 PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
3197 PE_update_object(data.depsgraph, data.scene, data.ob, 1);
3200
3201 return OPERATOR_FINISHED;
3202}
3203
3205{
3206 /* identifiers */
3207 ot->name = "Subdivide";
3208 ot->idname = "PARTICLE_OT_subdivide";
3209 ot->description = "Subdivide selected particles segments (adds keys)";
3210
3211 /* API callbacks. */
3212 ot->exec = subdivide_exec;
3213 ot->poll = PE_hair_poll;
3214
3215 /* flags */
3217}
3218
3220
3221/* -------------------------------------------------------------------- */
3224
3226{
3227 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
3228 Scene *scene = CTX_data_scene(C);
3230 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
3231 ParticleSystem *psys = edit->psys;
3232 ParticleSystemModifierData *psmd_eval;
3233 KDTree<float3> *tree;
3234 KDTreeNearest<float3> nearest[10];
3235 POINT_P;
3236 float mat[4][4], co[3], threshold = RNA_float_get(op->ptr, "threshold");
3237 int n, totn, removed, totremoved;
3238
3239 if (psys->flag & PSYS_GLOBAL_HAIR) {
3240 return OPERATOR_CANCELLED;
3241 }
3242
3243 edit = psys->edit;
3244 psmd_eval = edit->psmd_eval;
3245 totremoved = 0;
3246
3247 do {
3248 removed = 0;
3249
3250 tree = kdtree_new<float3>(psys->totpart);
3251
3252 /* Insert particles into KD-tree. */
3255 ob, psmd_eval->mesh_final, psys->part->from, psys->particles + p, mat);
3256 copy_v3_v3(co, point->keys->co);
3257 mul_m4_v3(mat, co);
3258 kdtree_insert<float3>(tree, p, co);
3259 }
3260
3262
3263 /* tag particles to be removed */
3266 ob, psmd_eval->mesh_final, psys->part->from, psys->particles + p, mat);
3267 copy_v3_v3(co, point->keys->co);
3268 mul_m4_v3(mat, co);
3269
3270 totn = kdtree_find_nearest_n<float3>(tree, co, nearest, 10);
3271
3272 for (n = 0; n < totn; n++) {
3273 /* this needs a custom threshold still */
3274 if (nearest[n].index > p && nearest[n].dist < threshold) {
3275 if (!(point->flag & PEP_TAG)) {
3276 point->flag |= PEP_TAG;
3277 removed++;
3278 }
3279 }
3280 }
3281 }
3282
3283 kdtree_free<float3>(tree);
3284
3285 /* remove tagged particles - don't do mirror here! */
3286 remove_tagged_particles(ob, psys, 0);
3287 totremoved += removed;
3288 } while (removed);
3289
3290 if (totremoved == 0) {
3291 return OPERATOR_CANCELLED;
3292 }
3293
3294 BKE_reportf(op->reports, RPT_INFO, "Removed %d double particle(s)", totremoved);
3295
3298
3299 return OPERATOR_FINISHED;
3300}
3301
3303{
3304 /* identifiers */
3305 ot->name = "Remove Doubles";
3306 ot->idname = "PARTICLE_OT_remove_doubles";
3307 ot->description = "Remove selected particles close enough to others";
3308
3309 /* API callbacks. */
3311 ot->poll = PE_hair_poll;
3312
3313 /* flags */
3315
3316 /* properties */
3317 RNA_def_float(ot->srna,
3318 "threshold",
3319 0.0002f,
3320 0.0f,
3321 FLT_MAX,
3322 "Merge Distance",
3323 "Threshold distance within which particles are removed",
3324 0.00001f,
3325 0.1f);
3326}
3327
3329{
3330 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
3331 Scene *scene = CTX_data_scene(C);
3332 ParticleEditSettings *pset = PE_settings(scene);
3334 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
3335 ParticleSystem *psys = edit->psys;
3336 POINT_P;
3337 KEY_K;
3338 HairKey *hkey;
3339 float weight;
3340 ParticleBrushData *brush = &pset->brush[pset->brushtype];
3341 float factor = RNA_float_get(op->ptr, "factor");
3342
3343 weight = brush->strength;
3344 edit = psys->edit;
3345
3347 ParticleData *pa = psys->particles + p;
3348
3350 hkey = pa->hair + k;
3351 hkey->weight = interpf(weight, hkey->weight, factor);
3352 }
3353 }
3354
3357
3358 return OPERATOR_FINISHED;
3359}
3360
3362{
3363 /* identifiers */
3364 ot->name = "Weight Set";
3365 ot->idname = "PARTICLE_OT_weight_set";
3366 ot->description = "Set the weight of selected keys";
3367
3368 /* API callbacks. */
3369 ot->exec = weight_set_exec;
3370 ot->poll = PE_hair_poll;
3371
3372 /* flags */
3374
3375 RNA_def_float(ot->srna,
3376 "factor",
3377 1,
3378 0,
3379 1,
3380 "Factor",
3381 "Interpolation factor between current brush weight, and keys' weights",
3382 0,
3383 1);
3384}
3385
3387
3388/* -------------------------------------------------------------------- */
3391
3393 const int2 &xy,
3394 const float2 & /*tilt*/,
3395 void * /*customdata*/)
3396{
3397 Scene *scene = CTX_data_scene(C);
3398 ParticleEditSettings *pset = PE_settings(scene);
3400
3402 return;
3403 }
3404
3405 brush = &pset->brush[pset->brushtype];
3406
3407 if (brush) {
3408 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
3410
3411 immUniformColor4ub(255, 255, 255, 128);
3412
3413 GPU_line_smooth(true);
3415
3416 imm_draw_circle_wire_2d(pos, float(xy.x), float(xy.y), pe_brush_size_get(scene, brush), 40);
3417
3419 GPU_line_smooth(false);
3420
3422 }
3423}
3424
3425static void toggle_particle_cursor(Scene *scene, bool enable)
3426{
3427 ParticleEditSettings *pset = PE_settings(scene);
3428
3429 if (pset->paintcursor && !enable) {
3430 WM_paint_cursor_end(static_cast<wmPaintCursor *>(pset->paintcursor));
3431 pset->paintcursor = nullptr;
3432 }
3433 else if (enable) {
3436 }
3437}
3438
3440
3441/* -------------------------------------------------------------------- */
3444
3446
3448 {DEL_PARTICLE, "PARTICLE", 0, "Particle", ""},
3449 {DEL_KEY, "KEY", 0, "Key", ""},
3450 {0, nullptr, 0, nullptr, nullptr},
3451};
3452
3453static void set_delete_particle(PEData *data, int pa_index)
3454{
3455 PTCacheEdit *edit = data->edit;
3456
3457 edit->points[pa_index].flag |= PEP_TAG;
3458}
3459
3460static void set_delete_particle_key(PEData *data, int pa_index, int key_index, bool /*is_inside*/)
3461{
3462 PTCacheEdit *edit = data->edit;
3463
3464 edit->points[pa_index].keys[key_index].flag |= PEK_TAG;
3465}
3466
3468{
3469 PEData data;
3470 int type = RNA_enum_get(op->ptr, "type");
3471
3472 PE_set_data(C, &data);
3473
3474 if (type == DEL_KEY) {
3476 remove_tagged_keys(data.depsgraph, data.ob, data.edit->psys);
3477 recalc_lengths(data.edit);
3478 }
3479 else if (type == DEL_PARTICLE) {
3481 remove_tagged_particles(data.ob, data.edit->psys, pe_x_mirror(data.ob));
3482 recalc_lengths(data.edit);
3483 }
3484
3488
3489 return OPERATOR_FINISHED;
3490}
3491
3493{
3494 /* identifiers */
3495 ot->name = "Delete";
3496 ot->idname = "PARTICLE_OT_delete";
3497 ot->description = "Delete selected particles or keys";
3498
3499 /* API callbacks. */
3500 ot->exec = delete_exec;
3501 ot->invoke = WM_menu_invoke;
3502 ot->poll = PE_hair_poll;
3503
3504 /* flags */
3506
3507 /* properties */
3508 ot->prop = RNA_def_enum(ot->srna,
3509 "type",
3512 "Type",
3513 "Delete a full particle or only keys");
3514}
3515
3517
3518/* -------------------------------------------------------------------- */
3521
3522static void PE_mirror_x(Depsgraph *depsgraph, Scene *scene, Object *ob, int tagged)
3523{
3524 Mesh *mesh = id_cast<Mesh *>(ob->data);
3525 ParticleSystemModifierData *psmd_eval;
3526 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
3527 ParticleSystem *psys = edit->psys;
3528 ParticleData *pa, *newpa, *new_pars;
3529 PTCacheEditPoint *newpoint, *new_points;
3530 POINT_P;
3531 KEY_K;
3532 HairKey *hkey;
3533 int *mirrorfaces = nullptr;
3534 int rotation, totpart, newtotpart;
3535
3536 if (psys->flag & PSYS_GLOBAL_HAIR) {
3537 return;
3538 }
3539
3540 psmd_eval = edit->psmd_eval;
3541 if (!psmd_eval->mesh_final) {
3542 return;
3543 }
3544
3545 const bool use_dm_final_indices = (psys->part->use_modifier_stack &&
3546 !psmd_eval->mesh_final->runtime->deformed_only);
3547
3548 /* NOTE: this is not nice to use tessfaces but hard to avoid since pa->num uses tessfaces */
3550
3551 /* NOTE: In case psys uses Mesh tessface indices, we mirror final Mesh itself, not orig mesh.
3552 * Avoids an (impossible) mesh -> orig -> mesh tessface indices conversion. */
3553 mirrorfaces = mesh_get_x_mirror_faces(ob,
3554 use_dm_final_indices ? psmd_eval->mesh_final : nullptr);
3555
3556 if (!edit->mirror_cache) {
3557 PE_update_mirror_cache(ob, psys);
3558 }
3559
3560 totpart = psys->totpart;
3561 newtotpart = psys->totpart;
3563 pa = psys->particles + p;
3564
3565 if (!tagged) {
3566 if (point_is_selected(point)) {
3567 if (edit->mirror_cache[p] != -1) {
3568 /* already has a mirror, don't need to duplicate */
3569 PE_mirror_particle(ob, psmd_eval->mesh_final, psys, pa, nullptr);
3570 continue;
3571 }
3572 point->flag |= PEP_TAG;
3573 }
3574 }
3575
3576 if ((point->flag & PEP_TAG) && mirrorfaces[pa->num * 2] != -1) {
3577 newtotpart++;
3578 }
3579 }
3580
3581 if (newtotpart != psys->totpart) {
3582 const MFace *mtessface = use_dm_final_indices ?
3583 static_cast<const MFace *>(CustomData_get_layer(
3584 &psmd_eval->mesh_final->fdata_legacy, CD_MFACE)) :
3585 static_cast<const MFace *>(
3586 CustomData_get_layer(&mesh->fdata_legacy, CD_MFACE));
3587
3588 /* allocate new arrays and copy existing */
3589 new_pars = MEM_new_array<ParticleData>(newtotpart, "ParticleData new");
3590 new_points = MEM_new_array_zeroed<PTCacheEditPoint>(newtotpart, "PTCacheEditPoint new");
3591
3592 if (psys->particles) {
3593 memcpy(new_pars, psys->particles, totpart * sizeof(ParticleData));
3594 MEM_delete(psys->particles);
3595 }
3596 psys->particles = new_pars;
3597
3598 if (edit->points) {
3599 memcpy(new_points, edit->points, totpart * sizeof(PTCacheEditPoint));
3600 MEM_delete(edit->points);
3601 }
3602 edit->points = new_points;
3603
3604 MEM_SAFE_DELETE(edit->mirror_cache);
3605
3606 edit->totpoint = psys->totpart = newtotpart;
3607
3608 /* create new elements */
3609 newpa = psys->particles + totpart;
3610 newpoint = edit->points + totpart;
3611
3612 for (p = 0, point = edit->points; p < totpart; p++, point++) {
3613 pa = psys->particles + p;
3614 const int pa_num = pa->num;
3615
3616 if (point->flag & PEP_HIDE) {
3617 continue;
3618 }
3619
3620 if (!(point->flag & PEP_TAG) || mirrorfaces[pa_num * 2] == -1) {
3621 continue;
3622 }
3623
3624 /* duplicate */
3625 *newpa = *pa;
3626 *newpoint = *point;
3627 if (pa->hair) {
3628 newpa->hair = MEM_dupalloc(pa->hair);
3629 }
3630 if (point->keys) {
3631 newpoint->keys = MEM_dupalloc(point->keys);
3632 }
3633
3634 /* rotate weights according to vertex index rotation */
3635 rotation = mirrorfaces[pa_num * 2 + 1];
3636 newpa->fuv[0] = pa->fuv[2];
3637 newpa->fuv[1] = pa->fuv[1];
3638 newpa->fuv[2] = pa->fuv[0];
3639 newpa->fuv[3] = pa->fuv[3];
3640 while (rotation--) {
3641 if (mtessface[pa_num].v4) {
3642 SHIFT4(float, newpa->fuv[0], newpa->fuv[1], newpa->fuv[2], newpa->fuv[3]);
3643 }
3644 else {
3645 SHIFT3(float, newpa->fuv[0], newpa->fuv[1], newpa->fuv[2]);
3646 }
3647 }
3648
3649 /* assign face index */
3650 /* NOTE: mesh_get_x_mirror_faces generates -1 for non-found mirror,
3651 * same as DMCACHE_NOTFOUND. */
3652 newpa->num = mirrorfaces[pa_num * 2];
3653
3654 if (use_dm_final_indices) {
3656 }
3657 else {
3659 psmd_eval->mesh_final, psmd_eval->mesh_original, newpa->num, newpa->fuv, nullptr);
3660 }
3661
3662 /* update edit key pointers */
3663 key = newpoint->keys;
3664 for (k = 0, hkey = newpa->hair; k < newpa->totkey; k++, hkey++, key++) {
3665 key->co = hkey->co;
3666 key->time = &hkey->time;
3667 }
3668
3669 /* map key positions as mirror over x axis */
3670 PE_mirror_particle(ob, psmd_eval->mesh_final, psys, pa, newpa);
3671
3672 newpa++;
3673 newpoint++;
3674 }
3675 }
3676
3677 LOOP_POINTS {
3678 point->flag &= ~PEP_TAG;
3679 }
3680
3681 MEM_delete(mirrorfaces);
3682}
3683
3685{
3686 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
3687 Scene *scene = CTX_data_scene(C);
3689 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
3690
3691 PE_mirror_x(depsgraph, scene, ob, 0);
3692
3693 update_world_cos(ob, edit);
3694 psys_free_path_cache(nullptr, edit);
3695
3699
3700 return OPERATOR_FINISHED;
3701}
3702
3704{
3705 if (!PE_hair_poll(C)) {
3706 return false;
3707 }
3708
3709 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
3710 Scene *scene = CTX_data_scene(C);
3712 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
3713
3714 /* The operator only works for hairs emitted from faces. */
3715 return edit->psys->part->from == PART_FROM_FACE;
3716}
3717
3719{
3720 /* identifiers */
3721 ot->name = "Mirror";
3722 /* Using default context for 'flipping along axis', to differentiate from 'symmetrizing' (i.e.
3723 * 'mirrored copy').
3724 * See https://projects.blender.org/blender/blender/issues/43295#issuecomment-1400465 */
3726 ot->idname = "PARTICLE_OT_mirror";
3727 ot->description = "Duplicate and mirror the selected particles along the local X axis";
3728
3729 /* API callbacks. */
3730 ot->exec = mirror_exec;
3731 ot->poll = mirror_poll;
3732
3733 /* flags */
3735}
3736
3738
3739/* -------------------------------------------------------------------- */
3742
3744 float /*mat*/[4][4],
3745 float imat[4][4],
3746 int point_index,
3747 int key_index,
3748 PTCacheEditKey *key,
3749 float mouse_distance)
3750{
3751 ParticleEditSettings *pset = PE_settings(data->scene);
3752 float cvec[3], fac;
3753
3754 if (pset->flag & PE_LOCK_FIRST && key_index == 0) {
3755 return;
3756 }
3757
3758 fac = float(pow(double(1.0f - mouse_distance / data->rad), double(data->combfac)));
3759
3760 copy_v3_v3(cvec, data->dvec);
3761 mul_mat3_m4_v3(imat, cvec);
3762 mul_v3_fl(cvec, fac);
3763 add_v3_v3(key->co, cvec);
3764
3765 (data->edit->points + point_index)->flag |= PEP_EDIT_RECALC;
3766}
3767
3768static void brush_cut(PEData *data, int pa_index)
3769{
3770 PTCacheEdit *edit = data->edit;
3771 ARegion *region = data->vc.region;
3772 Object *ob = data->ob;
3773 ParticleEditSettings *pset = PE_settings(data->scene);
3774 ParticleCacheKey *key = edit->pathcache[pa_index];
3775 float rad2, cut_time = 1.0;
3776 float x0, x1, v0, v1, o0, o1, xo0, xo1, d, dv;
3777 int k, cut, keys = int(pow(2.0, double(pset->draw_step)));
3778 int screen_co[2];
3779
3780 BLI_assert(data->rng != nullptr);
3781 /* blunt scissors */
3782 if (BLI_rng_get_float(data->rng) > data->cutfac) {
3783 return;
3784 }
3785
3786 /* don't cut hidden */
3787 if (edit->points[pa_index].flag & PEP_HIDE) {
3788 return;
3789 }
3790
3791 if (ED_view3d_project_int_global(region, key->co, screen_co, V3D_PROJ_TEST_CLIP_NEAR) !=
3793 {
3794 return;
3795 }
3796
3797 rad2 = data->rad * data->rad;
3798
3799 cut = 0;
3800
3801 x0 = float(screen_co[0]);
3802 x1 = float(screen_co[1]);
3803
3804 o0 = float(data->mval[0]);
3805 o1 = float(data->mval[1]);
3806
3807 xo0 = x0 - o0;
3808 xo1 = x1 - o1;
3809
3810 /* check if root is inside circle */
3811 if (xo0 * xo0 + xo1 * xo1 < rad2 && key_test_depth(data, key->co, screen_co)) {
3812 cut_time = -1.0f;
3813 cut = 1;
3814 }
3815 else {
3816 /* calculate path time closest to root that was inside the circle */
3817 for (k = 1, key++; k <= keys; k++, key++) {
3818
3819 if ((ED_view3d_project_int_global(region, key->co, screen_co, V3D_PROJ_TEST_CLIP_NEAR) !=
3820 V3D_PROJ_RET_OK) ||
3821 key_test_depth(data, key->co, screen_co) == 0)
3822 {
3823 x0 = float(screen_co[0]);
3824 x1 = float(screen_co[1]);
3825
3826 xo0 = x0 - o0;
3827 xo1 = x1 - o1;
3828 continue;
3829 }
3830
3831 v0 = float(screen_co[0]) - x0;
3832 v1 = float(screen_co[1]) - x1;
3833
3834 dv = v0 * v0 + v1 * v1;
3835
3836 d = (v0 * xo1 - v1 * xo0);
3837
3838 d = dv * rad2 - d * d;
3839
3840 if (d > 0.0f) {
3841 d = sqrtf(d);
3842
3843 cut_time = -(v0 * xo0 + v1 * xo1 + d);
3844
3845 if (cut_time > 0.0f) {
3846 cut_time /= dv;
3847
3848 if (cut_time < 1.0f) {
3849 cut_time += float(k - 1);
3850 cut_time /= float(keys);
3851 cut = 1;
3852 break;
3853 }
3854 }
3855 }
3856
3857 x0 = float(screen_co[0]);
3858 x1 = float(screen_co[1]);
3859
3860 xo0 = x0 - o0;
3861 xo1 = x1 - o1;
3862 }
3863 }
3864
3865 if (cut) {
3866 if (cut_time < 0.0f) {
3867 edit->points[pa_index].flag |= PEP_TAG;
3868 }
3869 else {
3870 rekey_particle_to_time(data->context, data->scene, ob, pa_index, cut_time);
3871 edit->points[pa_index].flag |= PEP_EDIT_RECALC;
3872 }
3873 }
3874}
3875
3876static void brush_length(PEData *data, int point_index, float /*mouse_distance*/)
3877{
3878 PTCacheEdit *edit = data->edit;
3879 PTCacheEditPoint *point = edit->points + point_index;
3880 KEY_K;
3881 float dvec[3], pvec[3] = {0.0f, 0.0f, 0.0f};
3882
3883 LOOP_KEYS {
3884 if (k == 0) {
3885 copy_v3_v3(pvec, key->co);
3886 }
3887 else {
3888 sub_v3_v3v3(dvec, key->co, pvec);
3889 copy_v3_v3(pvec, key->co);
3890 mul_v3_fl(dvec, data->growfac);
3891 add_v3_v3v3(key->co, (key - 1)->co, dvec);
3892 }
3893 }
3894
3895 point->flag |= PEP_EDIT_RECALC;
3896}
3897
3898static void brush_puff(PEData *data, int point_index, float mouse_distance)
3899{
3900 PTCacheEdit *edit = data->edit;
3901 ParticleSystem *psys = edit->psys;
3902 PTCacheEditPoint *point = edit->points + point_index;
3903 KEY_K;
3904 float mat[4][4], imat[4][4];
3905
3906 float onor_prev[3]; /* previous normal (particle-space) */
3907 float ofs_prev[3]; /* accumulate offset for puff_volume (particle-space) */
3908 float co_root[3], no_root[3]; /* root location and normal (global-space) */
3909 float co_prev[3], co[3]; /* track key coords as we loop (global-space) */
3910 float fac = 0.0f, length_accum = 0.0f;
3911 bool puff_volume = false;
3912 bool changed = false;
3913
3914 zero_v3(ofs_prev);
3915
3916 {
3917 ParticleEditSettings *pset = PE_settings(data->scene);
3918 ParticleBrushData *brush = &pset->brush[pset->brushtype];
3919 puff_volume = (brush->flag & PE_BRUSH_DATA_PUFF_VOLUME) != 0;
3920 }
3921
3922 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
3924 data->ob, data->mesh, psys->part->from, psys->particles + point_index, mat);
3925 invert_m4_m4(imat, mat);
3926 }
3927 else {
3928 unit_m4(mat);
3929 unit_m4(imat);
3930 }
3931
3932 LOOP_KEYS {
3933 float kco[3];
3934
3935 if (k == 0) {
3936 /* find root coordinate and normal on emitter */
3937 copy_v3_v3(co, key->co);
3938 mul_m4_v3(mat, co);
3939
3940 /* Use `kco` as the object space version of world-space `co`,
3941 * `ob->world_to_object` is set before calling. */
3942 mul_v3_m4v3(kco, data->ob->world_to_object().ptr(), co);
3943
3944 point_index = kdtree_find_nearest<float3>(edit->emitter_field, kco, nullptr);
3945 if (point_index == -1) {
3946 return;
3947 }
3948
3949 copy_v3_v3(co_root, co);
3950 copy_v3_v3(no_root, &edit->emitter_cosnos[point_index * 6 + 3]);
3951 mul_mat3_m4_v3(data->ob->object_to_world().ptr(), no_root); /* normal into global-space */
3952 normalize_v3(no_root);
3953
3954 if (puff_volume) {
3955 copy_v3_v3(onor_prev, no_root);
3956 mul_mat3_m4_v3(imat, onor_prev); /* global-space into particle space */
3957 normalize_v3(onor_prev);
3958 }
3959
3960 fac = float(pow(double(1.0f - mouse_distance / data->rad), double(data->pufffac)));
3961 fac *= 0.025f;
3962 if (data->invert) {
3963 fac = -fac;
3964 }
3965 }
3966 else {
3967 /* Compute position as if hair was standing up straight. */
3968 float length;
3969 copy_v3_v3(co_prev, co);
3970 copy_v3_v3(co, key->co);
3971 mul_m4_v3(mat, co);
3972 length = len_v3v3(co_prev, co);
3973 length_accum += length;
3974
3975 if ((data->select == 0 || (key->flag & PEK_SELECT)) && !(key->flag & PEK_HIDE)) {
3976 float dco[3]; /* delta temp var */
3977
3978 madd_v3_v3v3fl(kco, co_root, no_root, length_accum);
3979
3980 /* blend between the current and straight position */
3981 sub_v3_v3v3(dco, kco, co);
3982 madd_v3_v3fl(co, dco, fac);
3983 /* keep the same distance from the root or we get glitches #35406. */
3984 dist_ensure_v3_v3fl(co, co_root, length_accum);
3985
3986 /* Re-use dco to compare before and after translation and add to the offset. */
3987 copy_v3_v3(dco, key->co);
3988
3989 mul_v3_m4v3(key->co, imat, co);
3990
3991 if (puff_volume) {
3992 /* accumulate the total distance moved to apply to unselected
3993 * keys that come after */
3994 sub_v3_v3v3(ofs_prev, key->co, dco);
3995 }
3996 changed = true;
3997 }
3998 else {
3999
4000 if (puff_volume) {
4001#if 0
4002 /* this is simple but looks bad, adds annoying kinks */
4003 add_v3_v3(key->co, ofs);
4004#else
4005 /* Translate (not rotate) the rest of the hair if its not selected. */
4006 {
4007/* NOLINTNEXTLINE: readability-redundant-preprocessor */
4008# if 0 /* Kind of works but looks worse than what's below. */
4009
4010 /* Move the unselected point on a vector based on the
4011 * hair direction and the offset */
4012 float c1[3], c2[3];
4013 sub_v3_v3v3(dco, lastco, co);
4014 mul_mat3_m4_v3(imat, dco); /* into particle space */
4015
4016 /* move the point along a vector perpendicular to the
4017 * hairs direction, reduces odd kinks, */
4018 cross_v3_v3v3(c1, ofs, dco);
4019 cross_v3_v3v3(c2, c1, dco);
4020 normalize_v3(c2);
4021 mul_v3_fl(c2, len_v3(ofs));
4022 add_v3_v3(key->co, c2);
4023# else
4024 /* Move the unselected point on a vector based on the
4025 * the normal of the closest geometry */
4026 float oco[3], onor[3];
4027 copy_v3_v3(oco, key->co);
4028 mul_m4_v3(mat, oco);
4029
4030 /* Use `kco` as the object space version of world-space `co`,
4031 * `ob->world_to_object` is set before calling. */
4032 mul_v3_m4v3(kco, data->ob->world_to_object().ptr(), oco);
4033
4034 point_index = kdtree_find_nearest<float3>(edit->emitter_field, kco, nullptr);
4035 if (point_index != -1) {
4036 copy_v3_v3(onor, &edit->emitter_cosnos[point_index * 6 + 3]);
4037 mul_mat3_m4_v3(data->ob->object_to_world().ptr(),
4038 onor); /* Normal into world-space. */
4039 mul_mat3_m4_v3(imat, onor); /* World-space into particle-space. */
4040 normalize_v3(onor);
4041 }
4042 else {
4043 copy_v3_v3(onor, onor_prev);
4044 }
4045
4046 if (!is_zero_v3(ofs_prev)) {
4047 mul_v3_fl(onor, len_v3(ofs_prev));
4048
4049 add_v3_v3(key->co, onor);
4050 }
4051
4052 copy_v3_v3(onor_prev, onor);
4053# endif
4054 }
4055#endif
4056 }
4057 }
4058 }
4059 }
4060
4061 if (changed) {
4062 point->flag |= PEP_EDIT_RECALC;
4063 }
4064}
4065
4067 float /*mat*/[4][4],
4068 float /*imat*/[4][4],
4069 int point_index,
4070 int key_index,
4071 PTCacheEditKey * /*key*/,
4072 float /*mouse_distance*/)
4073{
4074 /* roots have full weight always */
4075 if (key_index) {
4076 PTCacheEdit *edit = data->edit;
4077 ParticleSystem *psys = edit->psys;
4078
4079 ParticleData *pa = psys->particles + point_index;
4080 pa->hair[key_index].weight = data->weightfac;
4081
4082 (data->edit->points + point_index)->flag |= PEP_EDIT_RECALC;
4083 }
4084}
4085
4087 float mat[4][4],
4088 float /*imat*/[4][4],
4089 int /*point_index*/,
4090 int key_index,
4091 PTCacheEditKey *key,
4092 float /*mouse_distance*/)
4093{
4094 if (key_index) {
4095 float dvec[3];
4096
4097 sub_v3_v3v3(dvec, key->co, (key - 1)->co);
4098 mul_mat3_m4_v3(mat, dvec);
4099 add_v3_v3(data->vec, dvec);
4100 data->tot++;
4101 }
4102}
4103
4105 float /*mat*/[4][4],
4106 float imat[4][4],
4107 int point_index,
4108 int key_index,
4109 PTCacheEditKey *key,
4110 float /*mouse_distance*/)
4111{
4112 float vec[3], dvec[3];
4113
4114 if (key_index) {
4115 copy_v3_v3(vec, data->vec);
4116 mul_mat3_m4_v3(imat, vec);
4117
4118 sub_v3_v3v3(dvec, key->co, (key - 1)->co);
4119
4120 sub_v3_v3v3(dvec, vec, dvec);
4121 mul_v3_fl(dvec, data->smoothfac);
4122
4123 add_v3_v3(key->co, dvec);
4124 }
4125
4126 (data->edit->points + point_index)->flag |= PEP_EDIT_RECALC;
4127}
4128
4129/* convert from triangle barycentric weights to quad mean value weights */
4131 const float v1[3], const float v2[3], const float v3[3], const float v4[3], float w[4])
4132{
4133 float co[3], vert[4][3];
4134
4135 copy_v3_v3(vert[0], v1);
4136 copy_v3_v3(vert[1], v2);
4137 copy_v3_v3(vert[2], v3);
4138 copy_v3_v3(vert[3], v4);
4139
4140 co[0] = v1[0] * w[0] + v2[0] * w[1] + v3[0] * w[2] + v4[0] * w[3];
4141 co[1] = v1[1] * w[0] + v2[1] * w[1] + v3[1] * w[2] + v4[1] * w[3];
4142 co[2] = v1[2] * w[0] + v2[2] * w[1] + v3[2] * w[2] + v4[2] * w[3];
4143
4144 interp_weights_poly_v3(w, vert, 4, co);
4145}
4146
4148static int particle_intersect_mesh(Depsgraph *depsgraph,
4149 Scene * /*scene*/,
4150 Object *ob,
4151 Mesh *mesh,
4152 float *vert_cos,
4153 const float co1[3],
4154 const float co2[3],
4155 float *min_d,
4156 int *min_face,
4157 float *min_w,
4158 float *face_minmax,
4159 float *pa_minmax,
4160 float radius,
4161 float *ipoint)
4162{
4163 const MFace *mface = nullptr;
4164 int i, totface, intersect = 0;
4165 float cur_d;
4166 float2 cur_uv;
4167 float3 v1, v2, v3, v4, min, max, p_min, p_max;
4168 float cur_ipoint[3];
4169
4170 if (mesh == nullptr) {
4171 psys_disable_all(ob);
4172
4173 Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
4175 if (mesh == nullptr) {
4176 return 0;
4177 }
4178
4179 psys_enable_all(ob);
4180
4181 if (mesh == nullptr) {
4182 return 0;
4183 }
4184 }
4185
4186 /* BMESH_ONLY, deform dm may not have tessface */
4188
4189 if (pa_minmax == nullptr) {
4190 INIT_MINMAX(p_min, p_max);
4191 minmax_v3v3_v3(p_min, p_max, co1);
4192 minmax_v3v3_v3(p_min, p_max, co2);
4193 }
4194 else {
4195 copy_v3_v3(p_min, pa_minmax);
4196 copy_v3_v3(p_max, pa_minmax + 3);
4197 }
4198
4199 totface = mesh->totface_legacy;
4200 mface = static_cast<const MFace *>(CustomData_get_layer(&mesh->fdata_legacy, CD_MFACE));
4201 Span<float3> positions = mesh->vert_positions();
4202
4203 /* lets intersect the faces */
4204 for (i = 0; i < totface; i++, mface++) {
4205 if (vert_cos) {
4206 copy_v3_v3(v1, vert_cos + 3 * mface->v1);
4207 copy_v3_v3(v2, vert_cos + 3 * mface->v2);
4208 copy_v3_v3(v3, vert_cos + 3 * mface->v3);
4209 if (mface->v4) {
4210 copy_v3_v3(v4, vert_cos + 3 * mface->v4);
4211 }
4212 }
4213 else {
4214 copy_v3_v3(v1, positions[mface->v1]);
4215 copy_v3_v3(v2, positions[mface->v2]);
4216 copy_v3_v3(v3, positions[mface->v3]);
4217 if (mface->v4) {
4218 copy_v3_v3(v4, positions[mface->v4]);
4219 }
4220 }
4221
4222 if (face_minmax == nullptr) {
4224 math::min_max(float3(v1), min, max);
4226 math::min_max(float3(v3), min, max);
4227 if (mface->v4) {
4228 math::min_max(float3(v4), min, max);
4229 }
4230 if (isect_aabb_aabb_v3(min, max, p_min, p_max) == 0) {
4231 continue;
4232 }
4233 }
4234 else {
4235 copy_v3_v3(min, face_minmax + 6 * i);
4236 copy_v3_v3(max, face_minmax + 6 * i + 3);
4237 if (isect_aabb_aabb_v3(min, max, p_min, p_max) == 0) {
4238 continue;
4239 }
4240 }
4241
4242 if (radius > 0.0f) {
4243 if (isect_sweeping_sphere_tri_v3(co1, co2, radius, v2, v3, v1, &cur_d, cur_ipoint)) {
4244 if (cur_d < *min_d) {
4245 *min_d = cur_d;
4246 copy_v3_v3(ipoint, cur_ipoint);
4247 *min_face = i;
4248 intersect = 1;
4249 }
4250 }
4251 if (mface->v4) {
4252 if (isect_sweeping_sphere_tri_v3(co1, co2, radius, v4, v1, v3, &cur_d, cur_ipoint)) {
4253 if (cur_d < *min_d) {
4254 *min_d = cur_d;
4255 copy_v3_v3(ipoint, cur_ipoint);
4256 *min_face = i;
4257 intersect = 1;
4258 }
4259 }
4260 }
4261 }
4262 else {
4263 if (isect_line_segment_tri_v3(co1, co2, v1, v2, v3, &cur_d, cur_uv)) {
4264 if (cur_d < *min_d) {
4265 *min_d = cur_d;
4266 min_w[0] = 1.0f - cur_uv[0] - cur_uv[1];
4267 min_w[1] = cur_uv[0];
4268 min_w[2] = cur_uv[1];
4269 min_w[3] = 0.0f;
4270 if (mface->v4) {
4271 intersect_dm_quad_weights(v1, v2, v3, v4, min_w);
4272 }
4273 *min_face = i;
4274 intersect = 1;
4275 }
4276 }
4277 if (mface->v4) {
4278 if (isect_line_segment_tri_v3(co1, co2, v1, v3, v4, &cur_d, cur_uv)) {
4279 if (cur_d < *min_d) {
4280 *min_d = cur_d;
4281 min_w[0] = 1.0f - cur_uv[0] - cur_uv[1];
4282 min_w[1] = 0.0f;
4283 min_w[2] = cur_uv[0];
4284 min_w[3] = cur_uv[1];
4285 intersect_dm_quad_weights(v1, v2, v3, v4, min_w);
4286 *min_face = i;
4287 intersect = 1;
4288 }
4289 }
4290 }
4291 }
4292 }
4293 return intersect;
4294}
4295
4307
4312
4313static void brush_add_count_iter(void *__restrict iter_data_v,
4314 const int iter,
4315 const TaskParallelTLS *__restrict tls_v)
4316{
4317 BrushAddCountIterData *iter_data = static_cast<BrushAddCountIterData *>(iter_data_v);
4318 Depsgraph *depsgraph = iter_data->depsgraph;
4319 PEData *data = iter_data->data;
4320 PTCacheEdit *edit = data->edit;
4321 ParticleSystem *psys = edit->psys;
4322 ParticleSystemModifierData *psmd_eval = edit->psmd_eval;
4323 ParticleData *add_pars = iter_data->add_pars;
4324 BrushAddCountIterTLSData *tls = static_cast<BrushAddCountIterTLSData *>(tls_v->userdata_chunk);
4325 const int number = iter_data->number;
4326 const short size = iter_data->size;
4327 const int size2 = size * size;
4328 float dmx, dmy;
4329 if (number > 1) {
4330 dmx = size;
4331 dmy = size;
4332 if (tls->rng == nullptr) {
4333 tls->rng = BLI_rng_new_srandom(psys->seed + data->mval[0] + data->mval[1] +
4335 }
4336 /* rejection sampling to get points in circle */
4337 while (dmx * dmx + dmy * dmy > size2) {
4338 dmx = (2.0f * BLI_rng_get_float(tls->rng) - 1.0f) * size;
4339 dmy = (2.0f * BLI_rng_get_float(tls->rng) - 1.0f) * size;
4340 }
4341 }
4342 else {
4343 dmx = 0.0f;
4344 dmy = 0.0f;
4345 }
4346
4347 float mco[2];
4348 mco[0] = data->mval[0] + dmx;
4349 mco[1] = data->mval[1] + dmy;
4350
4351 float co1[3], co2[3];
4352 ED_view3d_win_to_segment_clipped(depsgraph, data->vc.region, data->vc.v3d, mco, co1, co2, true);
4353
4354 mul_m4_v3(iter_data->imat, co1);
4355 mul_m4_v3(iter_data->imat, co2);
4356 float min_d = 2.0;
4357
4358 /* warning, returns the derived mesh face */
4359 BLI_assert(iter_data->mesh != nullptr);
4360 if (particle_intersect_mesh(depsgraph,
4361 iter_data->scene,
4362 iter_data->object,
4363 iter_data->mesh,
4364 nullptr,
4365 co1,
4366 co2,
4367 &min_d,
4368 &add_pars[iter].num_dmcache,
4369 add_pars[iter].fuv,
4370 nullptr,
4371 nullptr,
4372 0,
4373 nullptr))
4374 {
4375 if (psys->part->use_modifier_stack && !psmd_eval->mesh_final->runtime->deformed_only) {
4376 add_pars[iter].num = add_pars[iter].num_dmcache;
4377 add_pars[iter].num_dmcache = DMCACHE_ISCHILD;
4378 }
4379 else if (iter_data->mesh == psmd_eval->mesh_original) {
4380 /* Final DM is not same topology as orig mesh,
4381 * we have to map num_dmcache to real final dm. */
4382 add_pars[iter].num = add_pars[iter].num_dmcache;
4383 add_pars[iter].num_dmcache = psys_particle_dm_face_lookup(psmd_eval->mesh_final,
4384 psmd_eval->mesh_original,
4385 add_pars[iter].num,
4386 add_pars[iter].fuv,
4387 nullptr);
4388 }
4389 else {
4390 add_pars[iter].num = add_pars[iter].num_dmcache;
4391 }
4392 if (add_pars[iter].num != DMCACHE_NOTFOUND) {
4393 tls->num_added++;
4394 }
4395 }
4396}
4397
4398static void brush_add_count_iter_reduce(const void *__restrict /*userdata*/,
4399 void *__restrict join_v,
4400 void *__restrict chunk_v)
4401{
4402 BrushAddCountIterTLSData *join = static_cast<BrushAddCountIterTLSData *>(join_v);
4403 BrushAddCountIterTLSData *tls = static_cast<BrushAddCountIterTLSData *>(chunk_v);
4404 join->num_added += tls->num_added;
4405}
4406
4407static void brush_add_count_iter_free(const void *__restrict /*userdata_v*/,
4408 void *__restrict chunk_v)
4409{
4410 BrushAddCountIterTLSData *tls = static_cast<BrushAddCountIterTLSData *>(chunk_v);
4411 if (tls->rng != nullptr) {
4412 BLI_rng_free(tls->rng);
4413 }
4414}
4415
4416static int brush_add(const bContext *C, PEData *data, short number)
4417{
4418 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
4419 Scene *scene = data->scene;
4420 Object *ob = data->ob;
4421 Mesh *mesh;
4422 PTCacheEdit *edit = data->edit;
4423 ParticleSystem *psys = edit->psys;
4424 ParticleData *add_pars;
4425 ParticleSystemModifierData *psmd_eval = edit->psmd_eval;
4426 ParticleSimulationData sim = {nullptr};
4427 ParticleEditSettings *pset = PE_settings(scene);
4428 int i, k, n = 0, totpart = psys->totpart;
4429 float co1[3], imat[4][4];
4430 float framestep, timestep;
4431 short size = pset->brush[PE_BRUSH_ADD].size;
4432 RNG *rng;
4433
4434 invert_m4_m4(imat, ob->object_to_world().ptr());
4435
4436 if (psys->flag & PSYS_GLOBAL_HAIR) {
4437 return 0;
4438 }
4439
4440 add_pars = MEM_new_array<ParticleData>(number, "ParticleData add");
4441
4442 rng = BLI_rng_new_srandom(psys->seed + data->mval[0] + data->mval[1]);
4443
4444 sim.depsgraph = depsgraph;
4445 sim.scene = scene;
4446 sim.ob = ob;
4447 sim.psys = psys;
4448 sim.psmd = psmd_eval;
4449
4450 timestep = psys_get_timestep(&sim);
4451
4452 if (psys->part->use_modifier_stack || psmd_eval->mesh_final->runtime->deformed_only) {
4453 mesh = psmd_eval->mesh_final;
4454 }
4455 else {
4456 mesh = psmd_eval->mesh_original;
4457 }
4459
4460 /* Calculate positions of new particles to add, based on brush intersection
4461 * with object. New particle data is assigned to a corresponding to check
4462 * index element of add_pars array. This means, that add_pars is a sparse
4463 * array.
4464 */
4465 BrushAddCountIterData iter_data;
4466 iter_data.depsgraph = depsgraph;
4467 iter_data.scene = scene;
4468 iter_data.object = ob;
4469 iter_data.mesh = mesh;
4470 iter_data.data = data;
4471 iter_data.number = number;
4472 iter_data.size = size;
4473 iter_data.add_pars = add_pars;
4474 copy_m4_m4(iter_data.imat, imat);
4475
4476 BrushAddCountIterTLSData tls = {nullptr};
4477
4478 TaskParallelSettings settings;
4480 settings.userdata_chunk = &tls;
4484 BLI_task_parallel_range(0, number, &iter_data, brush_add_count_iter, &settings);
4485
4486 /* Convert add_parse to a dense array, where all new particles are in the
4487 * beginning of the array.
4488 */
4489 n = tls.num_added;
4490 for (int current_iter = 0, new_index = 0; current_iter < number; current_iter++) {
4491 if (add_pars[current_iter].num == DMCACHE_NOTFOUND) {
4492 continue;
4493 }
4494 if (new_index != current_iter) {
4495 new_index++;
4496 continue;
4497 }
4498 memcpy(add_pars + new_index, add_pars + current_iter, sizeof(ParticleData));
4499 new_index++;
4500 }
4501
4502 /* TODO(sergey): Consider multi-threading this part as well. */
4503 if (n) {
4504 int newtotpart = totpart + n;
4505 float hairmat[4][4], cur_co[3];
4506 KDTree<float3> *tree = nullptr;
4507 ParticleData *pa, *new_pars = MEM_new_array<ParticleData>(newtotpart, "ParticleData new");
4509 newtotpart, "PTCacheEditPoint array new");
4510 PTCacheEditKey *key;
4511 HairKey *hkey;
4512
4513 /* save existing elements */
4514 memcpy(new_pars, psys->particles, totpart * sizeof(ParticleData));
4515 memcpy(new_points, edit->points, totpart * sizeof(PTCacheEditPoint));
4516
4517 /* change old arrays to new ones */
4518 if (psys->particles) {
4519 MEM_delete(psys->particles);
4520 }
4521 psys->particles = new_pars;
4522
4523 if (edit->points) {
4524 MEM_delete(edit->points);
4525 }
4526 edit->points = new_points;
4527
4528 MEM_SAFE_DELETE(edit->mirror_cache);
4529
4530 /* create tree for interpolation */
4531 if (pset->flag & PE_INTERPOLATE_ADDED && psys->totpart) {
4532 tree = kdtree_new<float3>(psys->totpart);
4533
4534 for (i = 0, pa = psys->particles; i < totpart; i++, pa++) {
4536 psys->part->from,
4537 pa->num,
4538 pa->num_dmcache,
4539 pa->fuv,
4540 pa->foffset,
4541 cur_co,
4542 nullptr,
4543 nullptr,
4544 nullptr,
4545 nullptr);
4546 kdtree_insert<float3>(tree, i, cur_co);
4547 }
4548
4550 }
4551
4552 edit->totpoint = psys->totpart = newtotpart;
4553
4554 /* create new elements */
4555 pa = psys->particles + totpart;
4556 point = edit->points + totpart;
4557
4558 for (i = totpart; i < newtotpart; i++, pa++, point++) {
4559 memcpy(pa, add_pars + i - totpart, sizeof(ParticleData));
4560 pa->hair = MEM_new_array_zeroed<HairKey>(pset->totaddkey, "BakeKey key add");
4562 "PTCacheEditKey add");
4563 point->totkey = pa->totkey = pset->totaddkey;
4564
4565 for (k = 0, hkey = pa->hair; k < pa->totkey; k++, hkey++, key++) {
4566 key->co = hkey->co;
4567 key->time = &hkey->time;
4568
4569 if (!(psys->flag & PSYS_GLOBAL_HAIR)) {
4570 key->flag |= PEK_USE_WCO;
4571 }
4572 }
4573
4574 pa->size = 1.0f;
4575 init_particle(&sim, pa);
4576 reset_particle(&sim, pa, 0.0, 1.0);
4577 point->flag |= PEP_EDIT_RECALC;
4578 if (pe_x_mirror(ob)) {
4579 point->flag |= PEP_TAG; /* signal for duplicate */
4580 }
4581
4582 framestep = pa->lifetime / float(pset->totaddkey - 1);
4583
4584 if (tree) {
4585 ParticleData *ppa;
4586 HairKey *thkey;
4587 ParticleKey key3[3];
4588 KDTreeNearest<float3> ptn[3];
4589 int w, maxw;
4590 float maxd, totw = 0.0, weight[3];
4591
4593 psys->part->from,
4594 pa->num,
4595 pa->num_dmcache,
4596 pa->fuv,
4597 pa->foffset,
4598 co1,
4599 nullptr,
4600 nullptr,
4601 nullptr,
4602 nullptr);
4603 maxw = kdtree_find_nearest_n<float3>(tree, co1, ptn, 3);
4604
4605 maxd = ptn[maxw - 1].dist;
4606
4607 for (w = 0; w < maxw; w++) {
4608 weight[w] = float(pow(2.0, double(-6.0f * ptn[w].dist / maxd)));
4609 totw += weight[w];
4610 }
4611 for (; w < 3; w++) {
4612 weight[w] = 0.0f;
4613 }
4614
4615 if (totw > 0.0f) {
4616 for (w = 0; w < maxw; w++) {
4617 weight[w] /= totw;
4618 }
4619 }
4620 else {
4621 for (w = 0; w < maxw; w++) {
4622 weight[w] = 1.0f / maxw;
4623 }
4624 }
4625
4626 ppa = psys->particles + ptn[0].index;
4627
4628 for (k = 0; k < pset->totaddkey; k++) {
4629 thkey = pa->hair + k;
4630 thkey->time = pa->time + k * framestep;
4631
4632 key3[0].time = thkey->time / 100.0f;
4633 psys_get_particle_on_path(&sim, ptn[0].index, key3, false);
4634 mul_v3_fl(key3[0].co, weight[0]);
4635
4636 /* TODO: interpolating the weight would be nicer */
4637 thkey->weight = (ppa->hair + std::min(k, ppa->totkey - 1))->weight;
4638
4639 if (maxw > 1) {
4640 key3[1].time = key3[0].time;
4641 psys_get_particle_on_path(&sim, ptn[1].index, &key3[1], false);
4642 mul_v3_fl(key3[1].co, weight[1]);
4643 add_v3_v3(key3[0].co, key3[1].co);
4644
4645 if (maxw > 2) {
4646 key3[2].time = key3[0].time;
4647 psys_get_particle_on_path(&sim, ptn[2].index, &key3[2], false);
4648 mul_v3_fl(key3[2].co, weight[2]);
4649 add_v3_v3(key3[0].co, key3[2].co);
4650 }
4651 }
4652
4653 if (k == 0) {
4654 sub_v3_v3v3(co1, pa->state.co, key3[0].co);
4655 }
4656
4657 add_v3_v3v3(thkey->co, key3[0].co, co1);
4658
4659 thkey->time = key3[0].time;
4660 }
4661 }
4662 else {
4663 for (k = 0, hkey = pa->hair; k < pset->totaddkey; k++, hkey++) {
4664 madd_v3_v3v3fl(hkey->co, pa->state.co, pa->state.vel, k * framestep * timestep);
4665 hkey->time += k * framestep;
4666 hkey->weight = 1.0f - float(k) / float(pset->totaddkey - 1);
4667 }
4668 }
4669 for (k = 0, hkey = pa->hair; k < pset->totaddkey; k++, hkey++) {
4670 psys_mat_hair_to_global(ob, psmd_eval->mesh_final, psys->part->from, pa, hairmat);
4671 invert_m4_m4(imat, hairmat);
4672 mul_m4_v3(imat, hkey->co);
4673 }
4674 }
4675
4676 if (tree) {
4677 kdtree_free<float3>(tree);
4678 }
4679 }
4680
4681 MEM_delete(add_pars);
4682
4683 BLI_rng_free(rng);
4684
4685 return n;
4686}
4687
4689
4690/* -------------------------------------------------------------------- */
4693
4707
4709{
4710 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
4711 Scene *scene = CTX_data_scene(C);
4712 ViewLayer *view_layer = CTX_data_view_layer(C);
4714 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
4715 ARegion *region = CTX_wm_region(C);
4716 BrushEdit *bedit;
4717 float3 min, max;
4718
4719 /* set the 'distance factor' for grabbing (used in comb etc) */
4721 PE_minmax(depsgraph, scene, view_layer, min, max);
4723
4724 bedit = MEM_new_zeroed<BrushEdit>("BrushEdit");
4725 bedit->first = 1;
4726 op->customdata = bedit;
4727
4728 bedit->scene = scene;
4729 bedit->view_layer = view_layer;
4730 bedit->ob = ob;
4731 bedit->edit = edit;
4732
4733 bedit->zfac = ED_view3d_calc_zfac(static_cast<const RegionView3D *>(region->regiondata), min);
4734
4735 /* cache view depths and settings for re-use */
4736 PE_set_view3d_data(C, &bedit->data);
4738
4739 return 1;
4740}
4741
4743{
4744 BrushEdit *bedit = static_cast<BrushEdit *>(op->customdata);
4745 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
4746 Scene *scene = bedit->scene;
4747 Object *ob = bedit->ob;
4748 PTCacheEdit *edit = bedit->edit;
4749 ParticleEditSettings *pset = PE_settings(scene);
4750 ParticleSystemModifierData *psmd_eval = edit->psmd_eval;
4751 ParticleBrushData *brush = &pset->brush[pset->brushtype];
4752 ARegion *region = CTX_wm_region(C);
4753 float vec[3], mousef[2];
4754 int mval[2];
4755 int flip, mouse[2], removed = 0, added = 0, selected = 0, tot_steps = 1, step = 1;
4756 float dx, dy, dmax;
4757 int lock_root = pset->flag & PE_LOCK_FIRST;
4758
4759 if (!PE_start_edit(edit)) {
4760 return;
4761 }
4762
4763 RNA_float_get_array(itemptr, "mouse", mousef);
4764 mouse[0] = mousef[0];
4765 mouse[1] = mousef[1];
4766 flip = RNA_boolean_get(op->ptr, "pen_flip");
4767
4768 if (bedit->first) {
4769 bedit->lastmouse[0] = mouse[0];
4770 bedit->lastmouse[1] = mouse[1];
4771 }
4772
4773 dx = mouse[0] - bedit->lastmouse[0];
4774 dy = mouse[1] - bedit->lastmouse[1];
4775
4776 mval[0] = mouse[0];
4777 mval[1] = mouse[1];
4778
4779 /* Disable locking temporarily for disconnected hair. */
4780 if (edit->psys && edit->psys->flag & PSYS_GLOBAL_HAIR) {
4781 pset->flag &= ~PE_LOCK_FIRST;
4782 }
4783
4784 if (((pset->brushtype == PE_BRUSH_ADD) ?
4785 (sqrtf(dx * dx + dy * dy) > pset->brush[PE_BRUSH_ADD].step) :
4786 (dx != 0 || dy != 0)) ||
4787 bedit->first)
4788 {
4789 PEData data = bedit->data;
4790 data.context = C; /* TODO(mai): why isn't this set in bedit->data? */
4791
4793 selected = short(count_selected_keys(scene, edit));
4794
4795 dmax = max_ff(fabsf(dx), fabsf(dy));
4796 tot_steps = dmax / (0.2f * pe_brush_size_get(scene, brush)) + 1;
4797
4798 dx /= float(tot_steps);
4799 dy /= float(tot_steps);
4800
4801 for (step = 1; step <= tot_steps; step++) {
4802 mval[0] = bedit->lastmouse[0] + step * dx;
4803 mval[1] = bedit->lastmouse[1] + step * dy;
4804
4805 switch (pset->brushtype) {
4806 case PE_BRUSH_COMB: {
4807 const float xy_delta[2] = {dx, dy};
4808 data.mval = mval;
4809 data.rad = pe_brush_size_get(scene, brush);
4810
4811 data.combfac = (brush->strength - 0.5f) * 2.0f;
4812 if (data.combfac < 0.0f) {
4813 data.combfac = 1.0f - 9.0f * data.combfac;
4814 }
4815 else {
4816 data.combfac = 1.0f - data.combfac;
4817 }
4818
4819 invert_m4_m4(ob->runtime->world_to_object.ptr(), ob->object_to_world().ptr());
4820
4821 ED_view3d_win_to_delta(region, xy_delta, bedit->zfac, vec);
4822 data.dvec = vec;
4823
4825 break;
4826 }
4827 case PE_BRUSH_CUT: {
4828 if (edit->psys && edit->pathcache) {
4829 data.mval = mval;
4830 data.rad = pe_brush_size_get(scene, brush);
4831 data.cutfac = brush->strength;
4832
4833 if (selected) {
4835 }
4836 else {
4838 }
4839
4840 removed = remove_tagged_particles(ob, edit->psys, pe_x_mirror(ob));
4841 if (pset->flag & PE_KEEP_LENGTHS) {
4842 recalc_lengths(edit);
4843 }
4844 }
4845 else {
4846 removed = 0;
4847 }
4848
4849 break;
4850 }
4851 case PE_BRUSH_LENGTH: {
4852 data.mval = mval;
4853
4854 data.rad = pe_brush_size_get(scene, brush);
4855 data.growfac = brush->strength / 50.0f;
4856
4857 if (brush->invert ^ flip) {
4858 data.growfac = 1.0f - data.growfac;
4859 }
4860 else {
4861 data.growfac = 1.0f + data.growfac;
4862 }
4863
4865
4866 if (pset->flag & PE_KEEP_LENGTHS) {
4867 recalc_lengths(edit);
4868 }
4869 break;
4870 }
4871 case PE_BRUSH_PUFF: {
4872 if (edit->psys) {
4873 data.mesh = psmd_eval->mesh_final;
4874 data.mval = mval;
4875 data.rad = pe_brush_size_get(scene, brush);
4876 data.select = selected;
4877
4878 data.pufffac = (brush->strength - 0.5f) * 2.0f;
4879 if (data.pufffac < 0.0f) {
4880 data.pufffac = 1.0f - 9.0f * data.pufffac;
4881 }
4882 else {
4883 data.pufffac = 1.0f - data.pufffac;
4884 }
4885
4886 data.invert = (brush->invert ^ flip);
4887 invert_m4_m4(ob->runtime->world_to_object.ptr(), ob->object_to_world().ptr());
4888
4890 }
4891 break;
4892 }
4893 case PE_BRUSH_ADD: {
4894 if (edit->psys && edit->psys->part->from == PART_FROM_FACE) {
4895 data.mval = mval;
4896
4897 added = brush_add(C, &data, brush->count);
4898
4899 if (pset->flag & PE_KEEP_LENGTHS) {
4900 recalc_lengths(edit);
4901 }
4902 }
4903 else {
4904 added = 0;
4905 }
4906 break;
4907 }
4908 case PE_BRUSH_SMOOTH: {
4909 data.mval = mval;
4910 data.rad = pe_brush_size_get(scene, brush);
4911
4912 data.vec[0] = data.vec[1] = data.vec[2] = 0.0f;
4913 data.tot = 0;
4914
4915 data.smoothfac = brush->strength;
4916
4917 invert_m4_m4(ob->runtime->world_to_object.ptr(), ob->object_to_world().ptr());
4918
4920
4921 if (data.tot) {
4922 mul_v3_fl(data.vec, 1.0f / float(data.tot));
4924 }
4925
4926 break;
4927 }
4928 case PE_BRUSH_WEIGHT: {
4929 if (edit->psys) {
4930 data.mesh = psmd_eval->mesh_final;
4931 data.mval = mval;
4932 data.rad = pe_brush_size_get(scene, brush);
4933
4934 data.weightfac = brush->strength; /* note that this will never be zero */
4935
4937 }
4938
4939 break;
4940 }
4941 }
4942 if ((pset->flag & PE_KEEP_LENGTHS) == 0) {
4943 recalc_lengths(edit);
4944 }
4945
4946 if (ELEM(pset->brushtype, PE_BRUSH_ADD, PE_BRUSH_CUT) && (added || removed)) {
4947 if (pset->brushtype == PE_BRUSH_ADD && pe_x_mirror(ob)) {
4948 PE_mirror_x(depsgraph, scene, ob, 1);
4949 }
4950
4951 update_world_cos(ob, edit);
4952 psys_free_path_cache(nullptr, edit);
4954 }
4955 else {
4956 PE_update_object(depsgraph, scene, ob, 1);
4957 }
4958 }
4959
4960 if (edit->psys) {
4964 }
4965 else {
4968 }
4969
4970 bedit->lastmouse[0] = mouse[0];
4971 bedit->lastmouse[1] = mouse[1];
4972 bedit->first = 0;
4973 }
4974
4975 pset->flag |= lock_root;
4976}
4977
4979{
4980 BrushEdit *bedit = static_cast<BrushEdit *>(op->customdata);
4981
4982 PE_data_free(&bedit->data);
4983 MEM_delete(bedit);
4984}
4985
4987{
4988 if (!brush_edit_init(C, op)) {
4989 return OPERATOR_CANCELLED;
4990 }
4991
4992 RNA_BEGIN (op->ptr, itemptr, "stroke") {
4993 brush_edit_apply(C, op, &itemptr);
4994 }
4995 RNA_END;
4996
4997 brush_edit_exit(op);
4998
4999 return OPERATOR_FINISHED;
5000}
5001
5002static void brush_edit_apply_event(bContext *C, wmOperator *op, const wmEvent *event)
5003{
5004 PointerRNA itemptr;
5005 float mouse[2];
5006
5007 copy_v2fl_v2i(mouse, event->mval);
5008
5009 /* fill in stroke */
5010 RNA_collection_add(op->ptr, "stroke", &itemptr);
5011
5012 RNA_float_set_array(&itemptr, "mouse", mouse);
5013
5014 /* apply */
5015 brush_edit_apply(C, op, &itemptr);
5016}
5017
5019{
5020 if (!brush_edit_init(C, op)) {
5021 return OPERATOR_CANCELLED;
5022 }
5023
5024 RNA_boolean_set(op->ptr, "pen_flip", event->modifier & KM_SHIFT); /* XXX hardcoded */
5025
5026 brush_edit_apply_event(C, op, event);
5027
5029
5031}
5032
5034{
5035 switch (event->type) {
5036 case LEFTMOUSE:
5037 case MIDDLEMOUSE:
5038 case RIGHTMOUSE: /* XXX hardcoded */
5039 if (event->val == KM_RELEASE) {
5040 brush_edit_exit(op);
5041 return OPERATOR_FINISHED;
5042 }
5043 break;
5044 case MOUSEMOVE:
5045 brush_edit_apply_event(C, op, event);
5046 break;
5047 default: {
5048 break;
5049 }
5050 }
5051
5053}
5054
5055static void brush_edit_cancel(bContext * /*C*/, wmOperator *op)
5056{
5057 brush_edit_exit(op);
5058}
5059
5064
5066{
5067 /* identifiers */
5068 ot->name = "Brush Edit";
5069 ot->idname = "PARTICLE_OT_brush_edit";
5070 ot->description = "Apply a stroke of brush to the particles";
5071
5072 /* API callbacks. */
5073 ot->exec = brush_edit_exec;
5075 ot->modal = brush_edit_modal;
5077 ot->poll = brush_edit_poll;
5078
5079 /* flags */
5081
5082 /* properties */
5083 PropertyRNA *prop;
5084 prop = RNA_def_collection_runtime(ot->srna, "stroke", RNA_OperatorStrokeElement, "Stroke", "");
5086 prop = RNA_def_boolean(
5087 ot->srna, "pen_flip", false, "Pen Flip", "Whether a tablet's eraser mode is being used");
5089}
5090
5092
5093/* -------------------------------------------------------------------- */
5096
5098{
5099 if (PE_hair_poll(C)) {
5100 Scene *scene = CTX_data_scene(C);
5101 ParticleEditSettings *pset = PE_settings(scene);
5102
5103 if (pset->shape_object && (pset->shape_object->type == OB_MESH)) {
5104 return true;
5105 }
5106 }
5107
5108 return false;
5109}
5110
5115
5116static void point_inside_bvh_cb(void *userdata,
5117 int index,
5118 const BVHTreeRay *ray,
5119 BVHTreeRayHit *hit)
5120{
5121 PointInsideBVH *data = static_cast<PointInsideBVH *>(userdata);
5122
5123 data->bvhdata->raycast_callback(data->bvhdata, index, ray, hit);
5124
5125 if (hit->index != -1) {
5126 ++data->num_hits;
5127 }
5128}
5129
5130/* true if the point is inside the shape mesh */
5132{
5133 bke::BVHTreeFromMesh *shape_bvh = data->shape_bvh;
5134 const float dir[3] = {1.0f, 0.0f, 0.0f};
5135 PointInsideBVH userdata;
5136
5137 userdata.bvhdata = data->shape_bvh;
5138 userdata.num_hits = 0;
5139
5140 float co_shape[3];
5141 mul_v3_m4v3(co_shape, pset->shape_object->world_to_object().ptr(), key->co);
5142
5144 shape_bvh->tree, co_shape, dir, 0.0f, BVH_RAYCAST_DIST_MAX, point_inside_bvh_cb, &userdata);
5145
5146 /* for any point inside a watertight mesh the number of hits is uneven */
5147 return (userdata.num_hits % 2) == 1;
5148}
5149
5150static void shape_cut(PEData *data, int pa_index)
5151{
5152 PTCacheEdit *edit = data->edit;
5153 Object *ob = data->ob;
5154 ParticleEditSettings *pset = PE_settings(data->scene);
5155 ParticleCacheKey *key;
5156
5157 bool cut;
5158 float cut_time = 1.0;
5159 int k, totkeys = 1 << pset->draw_step;
5160
5161 /* don't cut hidden */
5162 if (edit->points[pa_index].flag & PEP_HIDE) {
5163 return;
5164 }
5165
5166 cut = false;
5167
5168 /* check if root is inside the cut shape */
5169 key = edit->pathcache[pa_index];
5170 if (!shape_cut_test_point(data, pset, key)) {
5171 cut_time = -1.0f;
5172 cut = true;
5173 }
5174 else {
5175 for (k = 0; k < totkeys; k++, key++) {
5176 BVHTreeRayHit hit;
5177
5178 float co_curr_shape[3], co_next_shape[3];
5179 float dir_shape[3];
5180 float len_shape;
5181
5182 mul_v3_m4v3(co_curr_shape, pset->shape_object->world_to_object().ptr(), key->co);
5183 mul_v3_m4v3(co_next_shape, pset->shape_object->world_to_object().ptr(), (key + 1)->co);
5184
5185 sub_v3_v3v3(dir_shape, co_next_shape, co_curr_shape);
5186 len_shape = normalize_v3(dir_shape);
5187
5188 memset(&hit, 0, sizeof(hit));
5189 hit.index = -1;
5190 hit.dist = len_shape;
5191 BLI_bvhtree_ray_cast(data->shape_bvh->tree,
5192 co_curr_shape,
5193 dir_shape,
5194 0.0f,
5195 &hit,
5196 data->shape_bvh->raycast_callback,
5197 data->shape_bvh);
5198 if (hit.index >= 0) {
5199 if (hit.dist < len_shape) {
5200 cut_time = ((hit.dist / len_shape) + float(k)) / float(totkeys);
5201 cut = true;
5202 break;
5203 }
5204 }
5205 }
5206 }
5207
5208 if (cut) {
5209 if (cut_time < 0.0f) {
5210 edit->points[pa_index].flag |= PEP_TAG;
5211 }
5212 else {
5213 rekey_particle_to_time(data->context, data->scene, ob, pa_index, cut_time);
5214 edit->points[pa_index].flag |= PEP_EDIT_RECALC;
5215 }
5216 }
5217}
5218
5220{
5221 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
5222 Scene *scene = CTX_data_scene(C);
5224 ParticleEditSettings *pset = PE_settings(scene);
5225 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
5226 Object *shapeob = pset->shape_object;
5227 int selected = count_selected_keys(scene, edit);
5228 int lock_root = pset->flag & PE_LOCK_FIRST;
5229
5230 if (!PE_start_edit(edit)) {
5231 return OPERATOR_CANCELLED;
5232 }
5233
5234 /* Disable locking temporarily for disconnected hair. */
5235 if (edit->psys && edit->psys->flag & PSYS_GLOBAL_HAIR) {
5236 pset->flag &= ~PE_LOCK_FIRST;
5237 }
5238
5239 if (edit->psys && edit->pathcache) {
5240 PEData data;
5241 int removed;
5242
5243 PE_set_data(C, &data);
5244 if (!PE_create_shape_tree(&data, shapeob)) {
5245 /* shapeob may not have faces... */
5246 return OPERATOR_CANCELLED;
5247 }
5248
5249 if (selected) {
5251 }
5252 else {
5254 }
5255
5256 removed = remove_tagged_particles(ob, edit->psys, pe_x_mirror(ob));
5257 recalc_lengths(edit);
5258
5259 if (removed) {
5260 update_world_cos(ob, edit);
5261 psys_free_path_cache(nullptr, edit);
5263 }
5264 else {
5265 PE_update_object(data.depsgraph, scene, ob, 1);
5266 }
5267
5268 if (edit->psys) {
5272 }
5273 else {
5276 }
5277
5279 }
5280
5281 pset->flag |= lock_root;
5282
5283 return OPERATOR_FINISHED;
5284}
5285
5287{
5288 /* identifiers */
5289 ot->name = "Shape Cut";
5290 ot->idname = "PARTICLE_OT_shape_cut";
5291 ot->description = "Cut hair to conform to the set shape object";
5292
5293 /* API callbacks. */
5294 ot->exec = shape_cut_exec;
5295 ot->poll = shape_cut_poll;
5296
5297 /* flags */
5299}
5300
5302
5303/* -------------------------------------------------------------------- */
5306
5308 Depsgraph *depsgraph, Scene *scene, Object *ob, PointCache *cache, ParticleSystem *psys)
5309{
5310 Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
5311 PTCacheEdit *edit;
5312 ParticleSystemModifierData *psmd = (psys) ? psys_get_modifier(ob, psys) : nullptr;
5313 ParticleSystemModifierData *psmd_eval = nullptr;
5314 POINT_P;
5315 KEY_K;
5316 ParticleData *pa = nullptr;
5317 HairKey *hkey;
5318 int totpoint;
5319
5320 if (psmd != nullptr) {
5321 psmd_eval = reinterpret_cast<ParticleSystemModifierData *>(
5322 BKE_modifiers_findby_name(ob_eval, psmd->modifier.name));
5323 }
5324
5325 /* no psmd->dm happens in case particle system modifier is not enabled */
5326 if (!(psys && psmd && psmd_eval->mesh_final) && !cache) {
5327 return;
5328 }
5329
5330 if (cache && cache->flag & PTCACHE_DISK_CACHE) {
5331 return;
5332 }
5333
5334 if (psys == nullptr && (cache && cache->mem_cache.is_empty())) {
5335 return;
5336 }
5337
5338 edit = (psys) ? psys->edit : cache->edit;
5339
5340 if (!edit) {
5341 ParticleSystem *psys_eval = nullptr;
5342 if (psys) {
5343 psys_eval = psys_eval_get(depsgraph, ob, psys);
5344 psys_copy_particles(psys, psys_eval);
5345 }
5346
5347 totpoint = psys ? psys->totpart : int((cache->mem_cache.first())->totpoint);
5348
5349 edit = MEM_new_zeroed<PTCacheEdit>("PE_create_particle_edit");
5350 edit->points = MEM_new_array_zeroed<PTCacheEditPoint>(totpoint, "PTCacheEditPoints");
5351 edit->totpoint = totpoint;
5352
5353 if (psys && !cache) {
5354 edit->psmd = psmd;
5355 edit->psmd_eval = psmd_eval;
5356 psys->edit = edit;
5357 edit->psys = psys;
5358 edit->psys_eval = psys_eval;
5359
5361
5362 edit->pathcache = nullptr;
5363 edit->pathcachebufs.clear_no_delete();
5364
5365 pa = psys->particles;
5366 LOOP_POINTS {
5367 point->totkey = pa->totkey;
5368 point->keys = MEM_new_array_zeroed<PTCacheEditKey>(point->totkey, "ParticleEditKeys");
5369 point->flag |= PEP_EDIT_RECALC;
5370
5371 hkey = pa->hair;
5372 LOOP_KEYS {
5373 key->co = hkey->co;
5374 key->time = &hkey->time;
5375 key->flag = hkey->editflag;
5376 if (!(psys->flag & PSYS_GLOBAL_HAIR)) {
5377 key->flag |= PEK_USE_WCO;
5378 hkey->editflag |= PEK_USE_WCO;
5379 }
5380
5381 hkey++;
5382 }
5383 pa++;
5384 }
5385 update_world_cos(ob, edit);
5386 }
5387 else {
5388 int totframe = 0;
5389
5390 cache->edit = edit;
5392 edit->psys = nullptr;
5393
5394 totframe += cache->mem_cache.count();
5395
5396 for (PTCacheMem &pm : cache->mem_cache) {
5397 LOOP_POINTS {
5398 void *cur[BPHYS_TOT_DATA];
5399 if (BKE_ptcache_mem_pointers_seek(p, &pm, cur) == 0) {
5400 continue;
5401 }
5402
5403 if (!point->totkey) {
5404 key = point->keys = MEM_new_array_zeroed<PTCacheEditKey>(totframe, "ParticleEditKeys");
5405 point->flag |= PEP_EDIT_RECALC;
5406 }
5407 else {
5408 key = point->keys + point->totkey;
5409 }
5410
5411 key->co = static_cast<float *>(cur[BPHYS_DATA_LOCATION]);
5412 key->vel = static_cast<float *>(cur[BPHYS_DATA_VELOCITY]);
5413 key->rot = static_cast<float *>(cur[BPHYS_DATA_ROTATION]);
5414 key->ftime = float(pm.frame);
5415 key->time = &key->ftime;
5417
5418 point->totkey++;
5419 }
5420 }
5421 psys = nullptr;
5422 }
5423
5424 recalc_lengths(edit);
5425 if (psys && !cache) {
5426 recalc_emitter_field(depsgraph, ob, psys);
5427 }
5428
5429 PE_update_object(depsgraph, scene, ob, 1);
5430 }
5431}
5432
5434{
5436
5437 if (ob == nullptr || ob->type != OB_MESH) {
5438 return false;
5439 }
5440 if (!ob->data || !ID_IS_EDITABLE(ob->data) || ID_IS_OVERRIDE_LIBRARY(ob->data)) {
5441 return false;
5442 }
5443
5445}
5446
5447static void free_all_psys_edit(Object *object)
5448{
5449 for (ParticleSystem *psys = object->particlesystem.first(); psys != nullptr; psys = psys->next) {
5450 if (psys->edit != nullptr) {
5451 BLI_assert(psys->free_edit != nullptr);
5452 psys->free_edit(psys->edit);
5453 psys->free_edit = nullptr;
5454 psys->edit = nullptr;
5455 }
5456 }
5457}
5458
5464
5465void ED_object_particle_edit_mode_enter_ex(Depsgraph *depsgraph, Scene *scene, Object *ob)
5466{
5467 /* Needed so #ParticleSystemModifierData.mesh_final is set. */
5469
5470 PTCacheEdit *edit;
5471
5473
5474 edit = PE_create_current(depsgraph, scene, ob);
5475
5476 /* Mesh may have changed since last entering editmode.
5477 * NOTE: this may have run before if the edit data was just created,
5478 * so could avoid this and speed up a little. */
5479 if (edit && edit->psys) {
5480 /* Make sure pointer to the evaluated modifier data is up to date,
5481 * with possible changes applied when object was outside of the
5482 * edit mode. */
5483 Object *object_eval = DEG_get_evaluated(depsgraph, ob);
5484 edit->psmd_eval = reinterpret_cast<ParticleSystemModifierData *>(
5485 BKE_modifiers_findby_name(object_eval, edit->psmd->modifier.name));
5486 recalc_emitter_field(depsgraph, ob, edit->psys);
5487 }
5488
5489 toggle_particle_cursor(scene, true);
5492}
5493
5495{
5496 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
5497 Scene *scene = CTX_data_scene(C);
5499 ED_object_particle_edit_mode_enter_ex(depsgraph, scene, ob);
5500}
5501
5511
5518
5520{
5521 wmMsgBus *mbus = CTX_wm_message_bus(C);
5522 Scene *scene = CTX_data_scene(C);
5524 const int mode_flag = OB_MODE_PARTICLE_EDIT;
5525 const bool is_mode_set = (ob->mode & mode_flag) != 0;
5526
5527 if (!is_mode_set) {
5528 if (!ed::object::mode_compat_set(C, ob, eObjectMode(mode_flag), op->reports)) {
5529 return OPERATOR_CANCELLED;
5530 }
5531 }
5532
5533 if (!is_mode_set) {
5534 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
5535 ED_object_particle_edit_mode_enter_ex(depsgraph, scene, ob);
5536 }
5537 else {
5539 }
5540
5541 WM_msg_publish_rna_prop(mbus, &ob->id, ob, Object, mode);
5542
5544
5545 return OPERATOR_FINISHED;
5546}
5547
5549{
5550 /* identifiers */
5551 ot->name = "Particle Edit Toggle";
5552 ot->idname = "PARTICLE_OT_particle_edit_toggle";
5553 ot->description = "Toggle particle edit mode";
5554
5555 /* API callbacks. */
5558
5559 /* flags */
5561}
5562
5564
5565/* -------------------------------------------------------------------- */
5568
5570{
5572 ParticleSystem *psys = psys_get_current(ob);
5573 /* Additional check as poll doesn't do this exact lookup. */
5574 if (psys == nullptr) {
5575 return OPERATOR_CANCELLED;
5576 }
5577
5578 if (psys->edit) {
5579 if (/*psys->edit->edited ||*/ true) {
5581
5582 psys->edit = nullptr;
5583 psys->free_edit = nullptr;
5584
5586 psys->flag &= ~PSYS_GLOBAL_HAIR;
5587 psys->flag &= ~PSYS_EDITED;
5588
5593 }
5594 }
5595 else { /* some operation might have protected hair from editing so let's clear the flag */
5597 psys->flag &= ~PSYS_GLOBAL_HAIR;
5598 psys->flag &= ~PSYS_EDITED;
5601 }
5602
5603 return OPERATOR_FINISHED;
5604}
5605
5607{
5608 /* identifiers */
5609 ot->name = "Clear Edited";
5610 ot->idname = "PARTICLE_OT_edited_clear";
5611 ot->description = "Undo all edition performed on the particle system";
5612
5613 /* API callbacks. */
5614 ot->exec = clear_edited_exec;
5616
5617 /* flags */
5619}
5620
5622
5623/* -------------------------------------------------------------------- */
5626
5628{
5629 float length = 0.0f;
5630 KEY_K;
5631 LOOP_KEYS {
5632 if (k > 0) {
5633 length += len_v3v3((key - 1)->co, key->co);
5634 }
5635 }
5636 return length;
5637}
5638
5640{
5641 int num_selected = 0;
5642 float total_length = 0;
5643 POINT_P;
5645 total_length += calculate_point_length(point);
5646 num_selected++;
5647 }
5648 if (num_selected == 0) {
5649 return 0.0f;
5650 }
5651 return total_length / num_selected;
5652}
5653
5654static void scale_point_factor(PTCacheEditPoint *point, float factor)
5655{
5656 float orig_prev_co[3], prev_co[3];
5657 KEY_K;
5658 LOOP_KEYS {
5659 if (k == 0) {
5660 copy_v3_v3(orig_prev_co, key->co);
5661 copy_v3_v3(prev_co, key->co);
5662 }
5663 else {
5664 float new_co[3];
5665 float delta[3];
5666
5667 sub_v3_v3v3(delta, key->co, orig_prev_co);
5668 mul_v3_fl(delta, factor);
5669 add_v3_v3v3(new_co, prev_co, delta);
5670
5671 copy_v3_v3(orig_prev_co, key->co);
5672 copy_v3_v3(key->co, new_co);
5673 copy_v3_v3(prev_co, key->co);
5674 }
5675 }
5676 point->flag |= PEP_EDIT_RECALC;
5677}
5678
5680{
5681 const float point_length = calculate_point_length(point);
5682 if (point_length != 0.0f) {
5683 const float factor = length / point_length;
5684 scale_point_factor(point, factor);
5685 }
5686}
5687
5689{
5690 POINT_P;
5693 }
5694 recalc_lengths(edit);
5695}
5696
5698{
5700 Scene *scene = CTX_data_scene(C);
5701 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
5702
5703 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
5704 float average_length = calculate_average_length(edit);
5705
5706 if (average_length == 0.0f) {
5707 return OPERATOR_CANCELLED;
5708 }
5709 scale_points_to_length(edit, average_length);
5710
5711 PE_update_object(depsgraph, scene, ob, 1);
5712 if (edit->psys) {
5714 }
5715 else {
5718 }
5719
5720 return OPERATOR_FINISHED;
5721}
5722
5724{
5725 /* identifiers */
5726 ot->name = "Unify Length";
5727 ot->idname = "PARTICLE_OT_unify_length";
5728 ot->description = "Make selected hair the same length";
5729
5730 /* API callbacks. */
5731 ot->exec = unify_length_exec;
5732 ot->poll = PE_poll_view3d;
5733
5734 /* flags */
5736}
5737
5739
5740} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define G_MAIN
General operations, lookup, etc. for blender objects.
#define DMCACHE_NOTFOUND
#define LOOP_PARTICLES
#define DMCACHE_ISCHILD
#define PSYS_RESET_DEPSGRAPH
#define PARTICLE_P
#define PTCACHE_VEL_PER_SEC
#define PEK_HIDE
#define PEP_EDIT_RECALC
#define PTCACHE_TYPE_CLOTH
#define PEP_HIDE
#define PTCACHE_TYPE_PARTICLES
#define PTCACHE_TYPE_SOFTBODY
#define PEP_TAG
#define PEK_SELECT
#define PEK_USE_WCO
#define PEK_TAG
#define BVH_RAYCAST_DIST_MAX
A KD-tree for nearest neighbor search.
Random number functions.
Platform independent time functions.
#define BLT_I18NCONTEXT_DEFAULT
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define SEL_OP_USE_PRE_DESELECT(sel_op)
#define XRAY_ENABLED(v3d)
#define IS_CLIPPED
Definition ED_view3d.hh:269
Read Blender memory allocation and freeing.
#define RNA_BEGIN(sptr, itemptr, propname)
#define RNA_END
#define C
Definition RandGen.cpp:29
#define NS_MODE_PARTICLE
Definition WM_types.hh:620
#define ND_MODE
Definition WM_types.hh:464
#define NC_SCENE
Definition WM_types.hh:391
#define ND_MODIFIER
Definition WM_types.hh:503
#define NA_EDITED
Definition WM_types.hh:634
#define ND_PARTICLE
Definition WM_types.hh:506
#define NS_MODE_OBJECT
Definition WM_types.hh:610
#define NC_OBJECT
Definition WM_types.hh:392
#define NA_SELECTED
Definition WM_types.hh:639
#define SHIFT4(type, a, b, c, d)
#define INIT_MINMAX(min, max)
#define BLI_assert_unreachable()
#define POINTER_AS_UINT(i)
#define SHIFT3(type, a, b, c)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
bool intersect(IsectPyramid i_pyramid, Box box)
#define FLT_MAX
#define INT_MAX
#define pow
float dot(VecOp< float, D >, VecOp< float, D >) RET
float distance(float, float) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
#define UINT_MAX
Definition hash_md5.cc:46
const int state
bool mode_compat_set(bContext *C, Object *ob, eObjectMode mode, ReportList *reports)
void min_max(const T &value, T &min, T &max)
BMesh Iterators.
void psys_free_path_cache(struct ParticleSystem *psys, struct PTCacheEdit *edit)
Definition particle.cc:938
void WM_main_add_notifier(uint type, void *reference)
eV3DProjStatus ED_view3d_project_int_global(const ARegion *region, const float co[3], int r_co[2], eV3DProjTest flag)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
static wmOperatorStatus hide_exec(bContext *C, wmOperator *op)
static void free_all_psys_edit(Object *object)
static void brush_edit_apply_event(bContext *C, wmOperator *op, const wmEvent *event)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void brush_smooth_do(PEData *data, float[4][4], float imat[4][4], int point_index, int key_index, PTCacheEditKey *key, float)
static bool point_is_selected(PTCacheEditPoint *point)
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
static bool particle_edit_toggle_poll(bContext *C)
static int remove_tagged_particles(Object *ob, ParticleSystem *psys, int mirror)
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
void kdtree_balance(KDTree< CoordT > *tree)
static void foreach_mouse_hit_point(PEData *data, ForHitPointFunc func, int selected)
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
static void PE_free_shape_tree(PEData *data)
ModifierData * BKE_modifiers_findby_name(const Object *ob, const char *name)
ScrArea * CTX_wm_area(const bContext *C)
void PARTICLE_OT_rekey(wmOperatorType *ot)
void PARTICLE_OT_particle_edit_toggle(wmOperatorType *ot)
static wmOperatorStatus select_linked_pick_exec(bContext *C, wmOperator *op)
bool PE_hair_poll(bContext *C)
int RNA_enum_get(PointerRNA *ptr, const char *name)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], float radius, const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float ipoint[3])
void PARTICLE_OT_delete(wmOperatorType *ot)
static void select_more_keys(PEData *data, int point_index)
static void brush_length(PEData *data, int point_index, float)
void BKE_particle_batch_cache_dirty_tag(struct ParticleSystem *psys, int mode)
Definition particle.cc:5580
static bool pe_nearest_point_and_key(bContext *C, const int mval[2], PTCacheEditPoint **r_point, PTCacheEditKey **r_key)
void WM_operator_properties_select_random(wmOperatorType *ot)
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
int64_t BLI_time_now_seconds_i()
Definition time.cc:36
static int brush_edit_init(bContext *C, wmOperator *op)
struct ParticleSystem * psys_get_current(struct Object *ob)
Definition particle.cc:571
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
static int particle_intersect_mesh(Depsgraph *depsgraph, Scene *, Object *ob, Mesh *mesh, float *vert_cos, const float co1[3], const float co2[3], float *min_d, int *min_face, float *min_w, float *face_minmax, float *pa_minmax, float radius, float *ipoint)
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:324
void ED_view3d_depths_free(ViewDepths *depths)
static void PE_set_data(bContext *C, PEData *data)
static void point_inside_bvh_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
PTCacheEdit * PE_get_current_from_psys(ParticleSystem *psys)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void immUniformColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
void PARTICLE_OT_select_roots(wmOperatorType *ot)
bool PE_box_select(bContext *C, const rcti *rect, int sel_op)
static void foreach_mouse_hit_key(PEData *data, ForHitKeyMatFunc func, int selected)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
struct RNG * BLI_rng_new_srandom(unsigned int seed)
Definition rand.cc:48
GPUVertFormat * immVertexFormat()
int WM_operator_properties_select_random_seed_increment_get(wmOperator *op)
static wmOperatorStatus select_linked_exec(bContext *C, wmOperator *)
void PARTICLE_OT_select_tips(wmOperatorType *ot)
static void foreach_mouse_hit_key_iter(void *__restrict iter_data_v, const int iter, const TaskParallelTLS *__restrict)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
static void apply_lengths_iter(void *__restrict iter_data_v, const int iter, const TaskParallelTLS *__restrict)
static void brush_add_count_iter_reduce(const void *__restrict, void *__restrict join_v, void *__restrict chunk_v)
static wmOperatorStatus brush_edit_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void select_key_op(PEData *data, int point_index, int key_index, bool is_inside)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void PE_hide_keys_time(Scene *scene, PTCacheEdit *edit, float cfra)
PropertyRNA * RNA_def_int_vector(StructOrFunctionRNA *cont_, const char *identifier, const int len, const int *default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void PE_current_changed(Depsgraph *depsgraph, Scene *scene, Object *ob)
void reset_particle(struct ParticleSimulationData *sim, struct ParticleData *pa, float dtime, float cfra)
void GPU_line_smooth(bool enable)
Definition gpu_state.cc:80
static void PE_data_free(PEData *data)
int RNA_int_get(PointerRNA *ptr, const char *name)
static wmOperatorStatus select_less_exec(bContext *C, wmOperator *)
static void PE_apply_mirror(Object *ob, ParticleSystem *psys)
void(*)(PEData *data, int point_index, float mouse_distance) ForHitPointFunc
ModifierData * BKE_modifier_get_evaluated(Depsgraph *depsgraph, Object *object, ModifierData *md)
static wmOperatorStatus remove_doubles_exec(bContext *C, wmOperator *op)
void view3d_operator_needs_gpu(const bContext *C)
static void PE_create_random_generator(PEData *data)
static wmOperatorStatus delete_exec(bContext *C, wmOperator *op)
Definition editfont.cc:1731
bool PE_deselect_all_visible(bContext *C)
PropertyRNA * RNA_def_collection_runtime(StructOrFunctionRNA *cont, const char *identifier, StructRNA *type, const char *ui_name, const char *ui_description)
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
static void foreach_selected_key(PEData *data, ForKeyFunc func)
void ED_object_particle_edit_mode_exit(bContext *C)
static void update_velocities(PTCacheEdit *edit)
void PARTICLE_OT_select_less(wmOperatorType *ot)
static void shape_cut(PEData *data, int pa_index)
static void pe_select_cache_init_with_generic_userdata(bContext *C, wmGenericUserData *wm_userdata)
struct ParticleSystemModifierData * psys_get_modifier(struct Object *ob, struct ParticleSystem *psys)
Definition particle.cc:2190
static void brush_edit_apply(bContext *C, wmOperator *op, PointerRNA *itemptr)
void PARTICLE_OT_mirror(wmOperatorType *ot)
bool BLI_lasso_is_point_inside(Span< int2 > mcoords, int sx, int sy, int error_value)
Definition lasso_2d.cc:42
static bool shape_cut_test_point(PEData *data, ParticleEditSettings *pset, ParticleCacheKey *key)
int BLI_bvhtree_ray_cast(const BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
float psys_get_timestep(struct ParticleSimulationData *sim)
Definition particle.cc:4518
static void deflect_emitter_iter(void *__restrict iter_data_v, const int iter, const TaskParallelTLS *__restrict)
struct RNG * BLI_rng_new(unsigned int seed)
Definition rand.cc:41
static wmOperatorStatus pe_select_all_exec(bContext *C, wmOperator *op)
static void pe_iterate_lengths(Scene *scene, PTCacheEdit *edit)
int PE_minmax(Depsgraph *depsgraph, Scene *scene, ViewLayer *view_layer, float3 &min, float3 &max)
void init_particle(struct ParticleSimulationData *sim, struct ParticleData *pa)
void immUnbindProgram()
static wmOperatorStatus brush_edit_modal(bContext *C, wmOperator *op, const wmEvent *event)
bool isect_line_segment_tri_v3(const float p1[3], const float p2[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
void PARTICLE_OT_select_random(wmOperatorType *ot)
ViewLayer * CTX_data_view_layer(const bContext *C)
void WM_operator_properties_select_all(wmOperatorType *ot)
void PE_update_object(Depsgraph *depsgraph, Scene *scene, Object *ob, int useflag)
void BKE_scene_graph_evaluated_ensure(Depsgraph *depsgraph, Main *bmain)
Definition scene.cc:2872
MINLINE void mul_v3_fl(float r[3], float f)
@ PE_BRUSH_DATA_PUFF_VOLUME
static void brush_edit_exit(wmOperator *op)
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
void PARTICLE_OT_select_all(wmOperatorType *ot)
int * mesh_get_x_mirror_faces(Object *ob, Mesh *mesh_eval)
Definition meshtools.cc:496
void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
Global G
Definition blender.cc:57
void psys_get_particle_on_path(struct ParticleSimulationData *sim, int pa_num, struct ParticleKey *state, bool vel)
Definition particle.cc:4643
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
static int count_selected_keys(Scene *scene, PTCacheEdit *edit)
static void brush_drawcursor(bContext *C, const int2 &xy, const float2 &, void *)
KDTree< CoordT > * kdtree_new(uint nodes_len_capacity)
Definition BLI_kdtree.hh:65
void imm_draw_circle_wire_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
void mul_m4_v3(const float M[4][4], float r[3])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
void PARTICLE_OT_hide(wmOperatorType *ot)
static void intersect_dm_quad_weights(const float v1[3], const float v2[3], const float v3[3], const float v4[3], float w[4])
int PE_lasso_select(bContext *C, const int mcoords[][2], int mcoords_len, int sel_op)
void ED_object_particle_edit_mode_exit_ex(Scene *scene, Object *ob)
void BKE_object_minmax(Object *ob, float3 &r_min, float3 &r_max)
Definition object.cc:3842
static void remove_tagged_keys(Depsgraph *depsgraph, Object *ob, ParticleSystem *psys)
void RNA_int_set_array(PointerRNA *ptr, const char *name, const int *values)
static wmOperatorStatus subdivide_exec(bContext *C, wmOperator *op)
void BKE_ptcache_mem_pointers_incr(void *cur[BPHYS_TOT_DATA])
void recalc_lengths(PTCacheEdit *edit)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void recalc_emitter_field(Depsgraph *, Object *, ParticleSystem *psys)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void WM_operator_properties_select_action(wmOperatorType *ot, int default_action, bool hide_gui)
PTCacheEdit * PE_get_current(Depsgraph *depsgraph, Scene *scene, Object *ob)
void dist_ensure_v3_v3fl(float v1[3], const float v2[3], float dist)
static float calculate_average_length(PTCacheEdit *edit)
static bool key_inside_test(PEData *data, const float co[3])
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
static void PE_mirror_particle(Object *ob, Mesh *mesh, ParticleSystem *psys, ParticleData *pa, ParticleData *mpa)
int ED_select_op_action_deselected(eSelectOp sel_op, bool is_select, bool is_inside)
static int pe_x_mirror(Object *ob)
Main * DEG_get_bmain(const Depsgraph *graph)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ OB_MODE_PARTICLE_EDIT
BMesh const char void * data
void RNA_int_get_array(PointerRNA *ptr, const char *name, int *values)
static bool PE_create_shape_tree(PEData *data, Object *shapeob)
static void for_mouse_hit_keys(PEData *data, ForKeyFunc func, const enum eParticleSelectFlag flag)
static void select_tip(PEData *data, int point_index)
static wmOperatorStatus select_more_exec(bContext *C, wmOperator *)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
static float calculate_point_length(PTCacheEditPoint *point)
static bool select_action_apply(PTCacheEditPoint *point, PTCacheEditKey *key, int action)
int kdtree_find_nearest_n(const KDTree< CoordT > *tree, const CoordT &co, KDTreeNearest< CoordT > r_nearest[], uint nearest_len_capacity)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
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
void PARTICLE_OT_select_linked_pick(wmOperatorType *ot)
void PARTICLE_OT_select_linked(wmOperatorType *ot)
bool PE_poll_view3d(bContext *C)
static const EnumPropertyItem select_random_type_items[]
void kdtree_insert(KDTree< CoordT > *tree, int index, const CoordT &co)
Definition BLI_kdtree.hh:96
@ PT_CACHE_EDIT_UPDATE_PARTICLE_FROM_EVAL
static void set_delete_particle_key(PEData *data, int pa_index, int key_index, bool)
static void select_keys(PEData *data, int point_index, int, bool)
static void brush_cut(PEData *data, int pa_index)
static wmOperatorStatus rekey_exec(bContext *C, wmOperator *op)
static float pe_brush_size_get(const Scene *, ParticleBrushData *brush)
void PARTICLE_OT_subdivide(wmOperatorType *ot)
void PARTICLE_OT_remove_doubles(wmOperatorType *ot)
void WM_toolsystem_update_from_context_view3d(bContext *C)
static wmOperatorStatus shape_cut_exec(bContext *C, wmOperator *)
wmOperatorStatus WM_operator_props_popup(bContext *C, wmOperator *op, const wmEvent *)
static bool brush_edit_poll(bContext *C)
int kdtree_find_nearest(const KDTree< CoordT > *tree, const CoordT &co, KDTreeNearest< CoordT > *r_nearest)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
static void PE_update_mirror_cache(Object *ob, ParticleSystem *psys)
static void PE_apply_lengths(Scene *scene, PTCacheEdit *edit)
static wmOperatorStatus select_random_exec(bContext *C, wmOperator *op)
static void brush_add_count_iter(void *__restrict iter_data_v, const int iter, const TaskParallelTLS *__restrict tls_v)
void unit_m4(float m[4][4])
static void rekey_particle_to_time(const bContext *C, Scene *scene, Object *ob, int pa_index, float path_time)
static void brush_comb(PEData *data, float[4][4], float imat[4][4], int point_index, int key_index, PTCacheEditKey *key, float mouse_distance)
bool ED_object_particle_edit_mode_supported(const Object *ob)
float ED_view3d_calc_zfac(const RegionView3D *rv3d, const float co[3])
bool PE_circle_select(bContext *C, wmGenericUserData *wm_userdata, int sel_op, const int mval[2], float rad)
void psys_mat_hair_to_object(struct Object *ob, struct Mesh *mesh, short from, struct ParticleData *pa, float hairmat[4][4])
Definition particle.cc:3888
static void nearest_key_fn(PEData *data, int point_index, int key_index, bool)
void BKE_ptcache_ids_from_object(ListBaseT< PTCacheID > *lb, struct Object *ob, struct Scene *scene, int duplis)
static bool key_inside_circle(const PEData *data, float rad, const float co[3], float *distance)
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition rand.cc:55
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
int BLI_task_parallel_thread_id(const TaskParallelTLS *tls)
bool BLI_rcti_is_empty(const struct rcti *rect)
void interp_weights_poly_v3(float w[], float v[][3], int n, const float co[3])
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void ED_view3d_project_v3(const ARegion *region, const float world[3], float r_region_co[3])
void BLI_bvhtree_ray_cast_all(const BVHTree *tree, const float co[3], const float dir[3], float radius, float hit_dist, BVHTree_RayCastCallback callback, void *userdata)
static void select_less_keys(PEData *data, int point_index)
void ED_object_particle_edit_mode_enter(bContext *C)
MINLINE void zero_v3(float r[3])
static void set_delete_particle(PEData *data, int pa_index)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
bool PE_mouse_particles(bContext *C, const int mval[2], const SelectPick_Params &params)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static wmOperatorStatus weight_set_exec(bContext *C, wmOperator *op)
wmMsgBus * CTX_wm_message_bus(const bContext *C)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
static void PE_update_selection(Depsgraph *depsgraph, Scene *scene, Object *ob, int useflag)
static wmOperatorStatus reveal_exec(bContext *C, wmOperator *op)
wmPaintCursor * WM_paint_cursor_activate(short space_type, short region_type, wmPaintCursorPoll poll, wmPaintCursorDraw draw, void *customdata)
static void pe_deflect_emitter(Scene *scene, Object *ob, PTCacheEdit *edit)
static void PE_set_view3d_data(bContext *C, PEData *data)
static void select_root(PEData *data, int point_index)
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition rand.cc:90
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
void(*)(PEData *data, int point_index) ForPointFunc
void PARTICLE_OT_brush_edit(wmOperatorType *ot)
MINLINE float normalize_v3(float n[3])
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
static void scale_points_to_length(PTCacheEdit *edit, float length)
static void foreach_point(PEData *data, ForPointFunc func)
MINLINE void add_v3_v3(float r[3], const float a[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void kdtree_free(KDTree< CoordT > *tree)
Definition BLI_kdtree.hh:84
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
void psys_disable_all(struct Object *ob)
Definition particle.cc:672
static wmOperatorStatus particle_edit_toggle_exec(bContext *C, wmOperator *op)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
static void rekey_particle(PEData *data, int pa_index)
void psys_mat_hair_to_global(struct Object *ob, struct Mesh *mesh, short from, struct ParticleData *pa, float hairmat[4][4])
Definition particle.cc:3944
void PARTICLE_OT_select_more(wmOperatorType *ot)
void(*)(PEData *data, int point_index, int key_index, bool is_inside) ForKeyFunc
void PARTICLE_OT_reveal(wmOperatorType *ot)
Main * CTX_data_main(const bContext *C)
int psys_particle_dm_face_lookup(struct Mesh *mesh_final, struct Mesh *mesh_original, int findex_orig, const float fw[4], struct LinkNode **poly_nodes)
Definition particle.cc:1869
ATTR_WARN_UNUSED_RESULT const BMVert * v2
bool WM_toolsystem_active_tool_is_brush(const bContext *C)
void PE_free_ptcache_edit(PTCacheEdit *edit)
static void pe_update_hair_particle_edit_pointers(PTCacheEdit *edit)
bool ED_view3d_win_to_segment_clipped(const Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const float mval[2], float r_ray_start[3], float r_ray_end[3], bool do_clip_planes)
MINLINE float max_ff(float a, float b)
void ED_view3d_depth_override(Depsgraph *depsgraph, ARegion *region, View3D *v3d, Object *obact, eV3DDepthOverrideMode mode, bool use_overlay, ViewDepths **r_depths)
void PARTICLE_OT_unify_length(wmOperatorType *ot)
static void scale_point_factor(PTCacheEditPoint *point, float factor)
static wmOperatorStatus select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_PSYS_RESET
Definition DNA_ID.h:1083
@ ID_RECALC_PSYS_REDO
Definition DNA_ID.h:1081
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void update_world_cos(Object *ob, PTCacheEdit *edit)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
@ V3D_PROJ_TEST_CLIP_NEAR
Definition ED_view3d.hh:298
@ V3D_PROJ_TEST_CLIP_WIN
Definition ED_view3d.hh:297
@ V3D_PROJ_TEST_CLIP_BB
Definition ED_view3d.hh:296
static void brush_add_count_iter_free(const void *__restrict, void *__restrict chunk_v)
void BKE_mesh_tessface_ensure(Mesh *mesh)
static void pe_select_cache_free_generic_userdata(void *data)
@ V3D_DEPTH_OBJECT_ONLY
Definition ED_view3d.hh:213
void psys_cache_edit_paths(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct PTCacheEdit *edit, float cfra, bool use_render_params)
Definition particle.cc:3697
static void scale_point_to_length(PTCacheEditPoint *point, float length)
void PE_create_particle_edit(Depsgraph *depsgraph, Scene *scene, Object *ob, PointCache *cache, ParticleSystem *psys)
static PTCacheEdit * pe_get_current(Depsgraph *depsgraph, Scene *scene, Object *ob, bool create)
void psys_copy_particles(struct ParticleSystem *psys_dst, struct ParticleSystem *psys_src)
Definition particle.cc:1087
static bool mirror_poll(bContext *C)
@ KM_RELEASE
Definition WM_types.hh:325
MINLINE float interpf(float target, float origin, float t)
static void PE_mirror_x(Depsgraph *depsgraph, Scene *scene, Object *ob, int tagged)
static void PE_free_random_generator(PEData *data)
Scene * CTX_data_scene(const bContext *C)
void psys_reset(struct ParticleSystem *psys, int mode)
static void iterate_lengths_iter(void *__restrict iter_data_v, const int iter, const TaskParallelTLS *__restrict)
void PARTICLE_OT_weight_set(wmOperatorType *ot)
void PARTICLE_OT_edited_clear(wmOperatorType *ot)
bool PE_poll(bContext *C)
static bool key_test_depth(const PEData *data, const float co[3], const int screen_co[2])
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
void psys_enable_all(struct Object *ob)
Definition particle.cc:680
static wmOperatorStatus brush_edit_exec(bContext *C, wmOperator *op)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
int PE_start_edit(PTCacheEdit *edit)
float BKE_brush_weight_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1464
static void brush_puff(PEData *data, int point_index, float mouse_distance)
VecBase< float, 3 > float3
static void brush_smooth_get(PEData *data, float mat[4][4], float[4][4], int, int key_index, PTCacheEditKey *key, float)
PTCacheEdit * PE_create_current(Depsgraph *depsgraph, Scene *scene, Object *ob)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
float ED_view3d_select_dist_px()
void psys_mat_hair_to_orco(struct Object *ob, struct Mesh *mesh, short from, struct ParticleData *pa, float hairmat[4][4])
Definition particle.cc:3914
static wmOperatorStatus select_roots_exec(bContext *C, wmOperator *op)
static bool shape_cut_poll(bContext *C)
MINLINE void copy_v2fl_v2i(float r[2], const int a[2])
void ED_object_particle_edit_mode_enter_ex(Depsgraph *depsgraph, Scene *scene, Object *ob)
static void select_key(PEData *data, int point_index, int key_index, bool)
void RNA_collection_add(PointerRNA *ptr, const char *name, PointerRNA *r_value)
int BKE_ptcache_mem_pointers_seek(int point_index, struct PTCacheMem *pm, void *cur[BPHYS_TOT_DATA])
static bool key_inside_rect(PEData *data, const float co[3])
bool isect_aabb_aabb_v3(const float min1[3], const float max1[3], const float min2[3], const float max2[3])
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
Object * CTX_data_active_object(const bContext *C)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
ARegion * CTX_wm_region(const bContext *C)
struct ParticleSystem * psys_eval_get(struct Depsgraph *depsgraph, struct Object *object, struct ParticleSystem *psys)
Definition particle.cc:697
void PARTICLE_OT_shape_cut(wmOperatorType *ot)
static wmOperatorStatus unify_length_exec(bContext *C, wmOperator *)
float RNA_float_get(PointerRNA *ptr, const char *name)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
void(*)(PEData *data, float mat[4][4], float imat[4][4], int point_index, int key_index, PTCacheEditKey *key, float mouse_distance) ForHitKeyMatFunc
static const EnumPropertyItem delete_type_items[]
Definition editfont.cc:1720
void ED_view3d_win_to_delta(const ARegion *region, const float xy_delta[2], float zfac, float r_out[3], bool precise=false)
static wmOperatorStatus clear_edited_exec(bContext *C, wmOperator *)
static wmOperatorStatus mirror_exec(bContext *C, wmOperator *)
static void toggle_particle_cursor(Scene *scene, bool enable)
static void subdivide_particle(PEData *data, int pa_index)
void(*)(PEData *data, const float mat[4][4], const float imat[4][4], int point_index, int key_index, PTCacheEditKey *key) ForKeyMatFunc
bool PE_deselect_all_visible_ex(PTCacheEdit *edit)
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
@ BKE_PARTICLE_BATCH_DIRTY_ALL
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436
static void foreach_selected_point(PEData *data, ForPointFunc func)
static int brush_add(const bContext *C, PEData *data, short number)
ParticleEditSettings * PE_settings(Scene *scene)
bool WM_paint_cursor_end(wmPaintCursor *handle)
static wmOperatorStatus select_tips_exec(bContext *C, wmOperator *op)
static void brush_edit_cancel(bContext *, wmOperator *op)
#define KEY_WCO
#define LOOP_TAGGED_KEYS
#define LOOP_UNSELECTED_POINTS
#define LOOP_SELECTED_POINTS
#define POINT_P
#define LOOP_KEYS
#define LOOP_SELECTED_KEYS
#define KEY_K
#define LOOP_TAGGED_POINTS
#define LOOP_EDITED_POINTS
#define LOOP_VISIBLE_KEYS
#define LOOP_POINTS
#define LOOP_VISIBLE_POINTS
#define fabsf
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
eRegion_Type regiontype
ForHitKeyMatFunc func
const c_style_mat & ptr() const
bke::MeshRuntime * runtime
CustomData fdata_legacy
bke::ObjectRuntime * runtime
ListBaseT< struct ParticleSystem > particlesystem
ViewDepths * depths
ViewLayer * view_layer
Depsgraph * depsgraph
PTCacheEdit * edit
const bContext * context
const rcti * rect
bke::BVHTreeFromMesh * shape_bvh
struct PTCacheEditKey * keys
struct PTCacheID pid
ListBaseT< LinkData > pathcachebufs
PTCacheEditPoint * points
struct ParticleSystemModifierData * psmd
struct ParticleSystem * psys
struct ParticleSystem * psys_eval
struct ParticleSystemModifierData * psmd_eval
struct ParticleCacheKey ** pathcache
struct PointCache * cache
struct PTCacheID * next
struct ParticleSystem * psys
struct ParticleSystemModifierData * psmd
struct PTCacheEdit * edit
void(*) free_edit(struct PTCacheEdit *edit)
eParticleSystem_Flag flag
struct PointCache * pointcache
ListBaseT< PTCacheMem > mem_cache
void(*) free_edit(struct PTCacheEdit *edit)
struct PTCacheEdit * edit
bke::BVHTreeFromMesh * bvhdata
struct RenderData r
struct ToolSettings * toolsettings
TaskParallelReduceFunc func_reduce
TaskParallelFreeFunc func_free
struct ParticleEditSettings particle
eUnifiedPaintSettingsFlags flag
unsigned short w
Definition ED_view3d.hh:92
unsigned short h
Definition ED_view3d.hh:92
wmEventType type
Definition WM_types.hh:817
wmEventModifierFlag modifier
Definition WM_types.hh:834
wmGenericUserDataFreeFn free_fn
Definition WM_types.hh:151
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
const char * translation_context
Definition WM_types.hh:1113
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
#define WM_msg_publish_rna_prop(mbus, id_, data_, type_, prop_)