Blender V5.3
editcurve_select.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_object_types.h"
10#include "DNA_scene_types.h"
11
12#include "MEM_guardedalloc.h"
13
14#include "BLI_bitmap.hh"
15#include "BLI_ghash.hh"
16#include "BLI_heap_simple.hh"
17#include "BLI_kdtree.hh"
18#include "BLI_listbase.hh"
19#include "BLI_math_matrix_c.hh"
20#include "BLI_math_vector_c.hh"
21#include "BLI_rand_c.hh"
22
23#include "BKE_context.hh"
24#include "BKE_curve.hh"
25#include "BKE_fcurve.hh"
26#include "BKE_layer.hh"
27#include "BKE_report.hh"
28
29#include "WM_api.hh"
30#include "WM_types.hh"
31
32#include "ED_curve.hh"
33#include "ED_object.hh"
34#include "ED_screen.hh"
35#include "ED_select_utils.hh"
36#include "ED_view3d.hh"
37
38#include "curve_intern.hh"
39
40#include "RNA_access.hh"
41#include "RNA_define.hh"
42
43#include "DEG_depsgraph.hh"
44
45namespace blender {
46
47/* -------------------------------------------------------------------- */
50
51bool select_beztriple(BezTriple *bezt, bool selstatus, eBezTriple_Flag flag, eVisible_Types hidden)
52{
53 if ((bezt->hide == 0) || (hidden == HIDDEN)) {
54 if (selstatus) { /* selects */
55 bezt->f1 |= flag;
56 bezt->f2 |= flag;
57 bezt->f3 |= flag;
58 return true;
59 }
60 /* deselects */
61 bezt->f1 &= ~flag;
62 bezt->f2 &= ~flag;
63 bezt->f3 &= ~flag;
64 return true;
65 }
66
67 return false;
68}
69
70bool select_bpoint(BPoint *bp, bool selstatus, eBezTriple_Flag flag, bool hidden)
71{
72 if ((bp->hide == 0) || (hidden == 1)) {
73 if (selstatus) {
74 bp->f1 |= flag;
75 return true;
76 }
77 bp->f1 &= ~flag;
78 return true;
79 }
80
81 return false;
82}
83
85{
86 if (bezt->f2 & SELECT) {
87 return select_beztriple(bezt, false, BEZT_FLAG_SELECT, VISIBLE);
88 }
89 return select_beztriple(bezt, true, BEZT_FLAG_SELECT, VISIBLE);
90}
91
93{
94 if (bp->f1 & SELECT) {
95 return select_bpoint(bp, false, BEZT_FLAG_SELECT, VISIBLE);
96 }
97 return select_bpoint(bp, true, BEZT_FLAG_SELECT, VISIBLE);
98}
99
100bool ED_curve_nurb_select_check(const View3D *v3d, const Nurb *nu)
101{
102 if (nu->type == CU_BEZIER) {
103 const BezTriple *bezt;
104 int i;
105
106 for (i = nu->pntsu, bezt = nu->bezt; i--; bezt++) {
107 if (BEZT_ISSEL_ANY_HIDDENHANDLES(v3d, bezt)) {
108 return true;
109 }
110 }
111 }
112 else {
113 const BPoint *bp;
114 int i;
115
116 for (i = nu->pntsu * nu->pntsv, bp = nu->bp; i--; bp++) {
117 if (bp->f1 & SELECT) {
118 return true;
119 }
120 }
121 }
122 return false;
123}
124
125int ED_curve_nurb_select_count(const View3D *v3d, const Nurb *nu)
126{
127 int sel = 0;
128
129 if (nu->type == CU_BEZIER) {
130 const BezTriple *bezt;
131 int i;
132
133 for (i = nu->pntsu, bezt = nu->bezt; i--; bezt++) {
134 if (BEZT_ISSEL_ANY_HIDDENHANDLES(v3d, bezt)) {
135 sel++;
136 }
137 }
138 }
139 else {
140 const BPoint *bp;
141 int i;
142
143 for (i = nu->pntsu * nu->pntsv, bp = nu->bp; i--; bp++) {
144 if (bp->f1 & SELECT) {
145 sel++;
146 }
147 }
148 }
149
150 return sel;
151}
152
154{
155 bool changed = false;
156 int i;
157 if (nu->bezt) {
158 BezTriple *bezt;
159 for (i = nu->pntsu, bezt = nu->bezt; i--; bezt++) {
160 if (bezt->hide == 0) {
161 if (BEZT_ISSEL_ALL(bezt) == false) {
162 BEZT_SEL_ALL(bezt);
163 changed = true;
164 }
165 }
166 }
167 }
168 else if (nu->bp) {
169 BPoint *bp;
170 for (i = nu->pntsu * nu->pntsv, bp = nu->bp; i--; bp++) {
171 if (bp->hide == 0) {
172 if ((bp->f1 & SELECT) == 0) {
173 bp->f1 |= SELECT;
174 changed = true;
175 }
176 }
177 }
178 }
179 return changed;
180}
181
183{
184 bool changed = false;
185 for (Nurb &nu : editnurb->nurbs) {
186 changed |= ED_curve_nurb_select_all(&nu);
187 }
188 return changed;
189}
190
192{
193 bool changed = false;
194 int i;
195 if (nu->bezt) {
196 BezTriple *bezt;
197 for (i = nu->pntsu, bezt = nu->bezt; i--; bezt++) {
198 if (BEZT_ISSEL_ANY(bezt)) {
199 BEZT_DESEL_ALL(bezt);
200 changed = true;
201 }
202 }
203 }
204 else if (nu->bp) {
205 BPoint *bp;
206 for (i = nu->pntsu * nu->pntsv, bp = nu->bp; i--; bp++) {
207 if (bp->f1 & SELECT) {
208 bp->f1 &= ~SELECT;
209 changed = true;
210 }
211 }
212 }
213 return changed;
214}
215
216int ED_curve_select_count(const View3D *v3d, const EditNurb *editnurb)
217{
218 int sel = 0;
219
220 for (Nurb &nu : editnurb->nurbs) {
221 sel += ED_curve_nurb_select_count(v3d, &nu);
222 }
223
224 return sel;
225}
226
227bool ED_curve_select_check(const View3D *v3d, const EditNurb *editnurb)
228{
229 for (const Nurb &nu : editnurb->nurbs) {
230 if (ED_curve_nurb_select_check(v3d, &nu)) {
231 return true;
232 }
233 }
234
235 return false;
236}
237
239{
240 bool changed = false;
241 for (Nurb &nu : editnurb->nurbs) {
242 changed |= ED_curve_nurb_deselect_all(&nu);
243 }
244 return changed;
245}
246
248{
249 bool changed_multi = false;
250 for (Base *base : bases) {
251 Object *obedit = base->object;
252 Curve *cu = id_cast<Curve *>(obedit->data);
253 changed_multi |= ED_curve_deselect_all(cu->editnurb);
255 }
256 return changed_multi;
257}
258
267
268bool ED_curve_select_swap(EditNurb *editnurb, bool hide_handles)
269{
270 BPoint *bp;
271 BezTriple *bezt;
272 int a;
273 bool changed = false;
274
275 /* This could be an argument to swap individual handle selection.
276 * At the moment this is always used though. */
277 bool swap_handles = false;
278
279 /* When hiding handles, ignore handle selection. */
280 if (hide_handles) {
281 swap_handles = true;
282 }
283
284 for (Nurb &nu : editnurb->nurbs) {
285 if (nu.type == CU_BEZIER) {
286 bezt = nu.bezt;
287 a = nu.pntsu;
288 while (a--) {
289 if (bezt->hide == 0) {
290 if (swap_handles) {
291 bezt->f2 ^= BEZT_FLAG_SELECT; /* always do the center point */
292 if (!hide_handles) {
293 bezt->f1 ^= BEZT_FLAG_SELECT;
294 bezt->f3 ^= BEZT_FLAG_SELECT;
295 }
296 }
297 else {
298 BLI_assert(!hide_handles);
299 if (BEZT_ISSEL_ANY(bezt)) {
300 BEZT_DESEL_ALL(bezt);
301 }
302 else {
303 BEZT_SEL_ALL(bezt);
304 }
305 }
306 changed = true;
307 }
308 bezt++;
309 }
310 }
311 else {
312 bp = nu.bp;
313 a = nu.pntsu * nu.pntsv;
314 while (a--) {
315 if (bp->hide == 0) {
317 changed = true;
318 }
319 bp++;
320 }
321 }
322 }
323 return changed;
324}
325
331static void select_adjacent_cp(ListBaseT<Nurb> *editnurb,
332 short next,
333 const bool cont,
334 const bool selstatus)
335{
336 BezTriple *bezt;
337 BPoint *bp;
338 int a;
339 bool lastsel = false;
340
341 if (next == 0) {
342 return;
343 }
344
345 for (Nurb &nu : *editnurb) {
346 lastsel = false;
347 if (nu.type == CU_BEZIER) {
348 a = nu.pntsu;
349 bezt = nu.bezt;
350 if (next < 0) {
351 bezt = &nu.bezt[a - 1];
352 }
353 while (a--) {
354 if (a - abs(next) < 0) {
355 break;
356 }
357 if ((lastsel == false) && (bezt->hide == 0) &&
358 ((bezt->f2 & SELECT) || (selstatus == false)))
359 {
360 bezt += next;
361 if (!(bezt->f2 & SELECT) || (selstatus == false)) {
362 bool sel = select_beztriple(bezt, selstatus, BEZT_FLAG_SELECT, VISIBLE);
363 if (sel && !cont) {
364 lastsel = true;
365 }
366 }
367 }
368 else {
369 bezt += next;
370 lastsel = false;
371 }
372 /* move around in zigzag way so that we go through each */
373 bezt -= (next - next / abs(next));
374 }
375 }
376 else {
377 a = nu.pntsu * nu.pntsv;
378 bp = nu.bp;
379 if (next < 0) {
380 bp = &nu.bp[a - 1];
381 }
382 while (a--) {
383 if (a - abs(next) < 0) {
384 break;
385 }
386 if ((lastsel == false) && (bp->hide == 0) && ((bp->f1 & SELECT) || (selstatus == false))) {
387 bp += next;
388 if (!(bp->f1 & SELECT) || (selstatus == false)) {
389 bool sel = select_bpoint(bp, selstatus, BEZT_FLAG_SELECT, VISIBLE);
390 if (sel && !cont) {
391 lastsel = true;
392 }
393 }
394 }
395 else {
396 bp += next;
397 lastsel = false;
398 }
399 /* move around in zigzag way so that we go through each */
400 bp -= (next - next / abs(next));
401 }
402 }
403 }
404}
405
407
408/* -------------------------------------------------------------------- */
411
419static void selectend_nurb(Object *obedit, eEndPoint_Types selfirst, bool doswap, bool selstatus)
420{
421 ListBaseT<Nurb> *editnurb = object_editcurve_get(obedit);
422 BPoint *bp;
423 BezTriple *bezt;
424 Curve *cu;
425 int a;
426
427 if (obedit == nullptr) {
428 return;
429 }
430
431 cu = id_cast<Curve *>(obedit->data);
432 cu->actvert = CU_ACT_NONE;
433
434 for (Nurb &nu : *editnurb) {
435 if (nu.type == CU_BEZIER) {
436 a = nu.pntsu;
437
438 /* which point? */
439 if (selfirst == LAST) { /* select last */
440 bezt = &nu.bezt[a - 1];
441 }
442 else { /* select first */
443 bezt = nu.bezt;
444 }
445
446 while (a--) {
447 bool sel;
448 if (doswap) {
449 sel = swap_selection_beztriple(bezt);
450 }
451 else {
452 sel = select_beztriple(bezt, selstatus, BEZT_FLAG_SELECT, VISIBLE);
453 }
454
455 if (sel == true) {
456 break;
457 }
458 }
459 }
460 else {
461 a = nu.pntsu * nu.pntsv;
462
463 /* which point? */
464 if (selfirst == LAST) { /* select last */
465 bp = &nu.bp[a - 1];
466 }
467 else { /* select first */
468 bp = nu.bp;
469 }
470
471 while (a--) {
472 if (bp->hide == 0) {
473 bool sel;
474 if (doswap) {
475 sel = swap_selection_bpoint(bp);
476 }
477 else {
478 sel = select_bpoint(bp, selstatus, BEZT_FLAG_SELECT, VISIBLE);
479 }
480
481 if (sel == true) {
482 break;
483 }
484 }
485 }
486 }
487 }
488}
489
491{
492 const Main *bmain = CTX_data_main(C);
493 const Scene *scene = CTX_data_scene(C);
494 ViewLayer *view_layer = CTX_data_view_layer(C);
496 *bmain, scene, view_layer, CTX_wm_view3d(C));
497
498 for (Object *obedit : objects) {
499 selectend_nurb(obedit, FIRST, true, false);
500 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
501 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
502 BKE_curve_nurb_vert_active_validate(id_cast<Curve *>(obedit->data));
503 }
504 return OPERATOR_FINISHED;
505}
506
508{
509 /* identifiers */
510 ot->name = "(De)select First";
511 ot->idname = "CURVE_OT_de_select_first";
512 ot->description = "(De)select first of visible part of each NURBS";
513
514 /* API callbacks. */
517
518 /* flags */
520}
521
523{
524 const Main *bmain = CTX_data_main(C);
525 Scene *scene = CTX_data_scene(C);
526 ViewLayer *view_layer = CTX_data_view_layer(C);
528 *bmain, scene, view_layer, CTX_wm_view3d(C));
529
530 for (Object *obedit : objects) {
531 selectend_nurb(obedit, LAST, true, false);
532 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
533 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
534 BKE_curve_nurb_vert_active_validate(id_cast<Curve *>(obedit->data));
535 }
536
537 return OPERATOR_FINISHED;
538}
539
541{
542 /* identifiers */
543 ot->name = "(De)select Last";
544 ot->idname = "CURVE_OT_de_select_last";
545 ot->description = "(De)select last of visible part of each NURBS";
546
547 /* API callbacks. */
550
551 /* flags */
553}
554
556
557/* -------------------------------------------------------------------- */
560
562{
563 int action = RNA_enum_get(op->ptr, "action");
564
565 const Main *bmain = CTX_data_main(C);
566 Scene *scene = CTX_data_scene(C);
567 ViewLayer *view_layer = CTX_data_view_layer(C);
568 View3D *v3d = CTX_wm_view3d(C);
570 *bmain, scene, view_layer, CTX_wm_view3d(C));
571 if (action == SEL_TOGGLE) {
572 action = SEL_SELECT;
573 for (Object *obedit : objects) {
574 Curve *cu = id_cast<Curve *>(obedit->data);
575
576 if (ED_curve_select_check(v3d, cu->editnurb)) {
577 action = SEL_DESELECT;
578 break;
579 }
580 }
581 }
582
583 for (Object *obedit : objects) {
584 Curve *cu = id_cast<Curve *>(obedit->data);
585 bool changed = false;
586
587 switch (action) {
588 case SEL_SELECT:
589 changed = ED_curve_select_all(cu->editnurb);
590 break;
591 case SEL_DESELECT:
592 changed = ED_curve_deselect_all(cu->editnurb);
593 break;
594 case SEL_INVERT:
595 changed = ED_curve_select_swap(
596 cu->editnurb, (v3d && (v3d->overlay.handle_display == CURVE_HANDLE_NONE)));
597 break;
598 }
599
600 if (changed) {
601 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
602 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
604 }
605 }
606
607 return OPERATOR_FINISHED;
608}
609
611{
612 /* identifiers */
613 ot->name = "(De)select All";
614 ot->idname = "CURVE_OT_select_all";
615 ot->description = "(De)select all control points";
616
617 /* API callbacks. */
620
621 /* flags */
623
624 /* properties */
626}
627
629
630/* -------------------------------------------------------------------- */
633
635{
636 const Main *bmain = CTX_data_main(C);
637 Scene *scene = CTX_data_scene(C);
638 ViewLayer *view_layer = CTX_data_view_layer(C);
639 View3D *v3d = CTX_wm_view3d(C);
640
642 *bmain, scene, view_layer, CTX_wm_view3d(C));
643 for (Object *obedit : objects) {
644 Curve *cu = id_cast<Curve *>(obedit->data);
645 EditNurb *editnurb = cu->editnurb;
646 ListBaseT<Nurb> *nurbs = &editnurb->nurbs;
647 bool changed = false;
648
649 for (Nurb &nu : *nurbs) {
650 if (ED_curve_nurb_select_check(v3d, &nu)) {
651 changed |= ED_curve_nurb_select_all(&nu);
652 }
653 }
654
655 if (changed) {
656 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
657 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
658 }
659 }
660
661 return OPERATOR_FINISHED;
662}
663
665 wmOperator *op,
666 const wmEvent * /*event*/)
667{
668 return select_linked_exec(C, op);
669}
670
672{
673 /* identifiers */
674 ot->name = "Select Linked All";
675 ot->idname = "CURVE_OT_select_linked";
676 ot->description = "Select all control points linked to the current selection";
677
678 /* API callbacks. */
682
683 /* flags */
685
686 /* properties */
687}
688
690
691/* -------------------------------------------------------------------- */
694
696 wmOperator *op,
697 const wmEvent *event)
698{
699 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
700 Nurb *nu;
701 BezTriple *bezt;
702 BPoint *bp;
703 int a;
704 const bool select = !RNA_boolean_get(op->ptr, "deselect");
705 Base *basact = nullptr;
706
709 copy_v2_v2_int(vc.mval, event->mval);
710
711 if (!ED_curve_pick_vert(&vc, 1, &nu, &bezt, &bp, nullptr, &basact)) {
712 return OPERATOR_CANCELLED;
713 }
714
715 if (bezt) {
716 a = nu->pntsu;
717 bezt = nu->bezt;
718 while (a--) {
720 bezt++;
721 }
722 }
723 else if (bp) {
724 a = nu->pntsu * nu->pntsv;
725 bp = nu->bp;
726 while (a--) {
728 bp++;
729 }
730 }
731
732 Object *obedit = basact->object;
733
736
737 if (!select) {
738 BKE_curve_nurb_vert_active_validate(id_cast<Curve *>(obedit->data));
739 }
740
741 return OPERATOR_FINISHED;
742}
743
745{
746 /* identifiers */
747 ot->name = "Select Linked";
748 ot->idname = "CURVE_OT_select_linked_pick";
749 ot->description = "Select all control points linked to already selected ones";
750
751 /* API callbacks. */
754
755 /* flags */
757
758 /* properties */
760 "deselect",
761 false,
762 "Deselect",
763 "Deselect linked control points rather than selecting them");
764}
765
767
768/* -------------------------------------------------------------------- */
771
773{
774 Object *obedit = CTX_data_edit_object(C);
775 Curve *cu = id_cast<Curve *>(obedit->data);
776 ListBaseT<Nurb> *editnurb = object_editcurve_get(obedit);
777 static BPoint *last = nullptr;
778 static int direction = 0;
779 Nurb *nu = nullptr;
780 BPoint *bp = nullptr;
781 int u = 0, v = 0, a, b;
782
783 if (!BKE_curve_nurb_vert_active_get(cu, &nu, static_cast<void **>(static_cast<void *>(&bp)))) {
784 return OPERATOR_CANCELLED;
785 }
786
787 if (last == bp) {
788 direction = 1 - direction;
789 BKE_nurbList_flag_set(editnurb, BEZT_FLAG_SELECT, false);
790 }
791 last = bp;
792
793 u = cu->actvert % nu->pntsu;
794 v = cu->actvert / nu->pntsu;
795 bp = nu->bp;
796 for (a = 0; a < nu->pntsv; a++) {
797 for (b = 0; b < nu->pntsu; b++, bp++) {
798 if (direction) {
799 if (a == v) {
801 }
802 }
803 else {
804 if (b == u) {
806 }
807 }
808 }
809 }
810
813
814 return OPERATOR_FINISHED;
815}
816
818{
819 /* identifiers */
820 ot->name = "Select Control Point Row";
821 ot->idname = "CURVE_OT_select_row";
822 ot->description =
823 "Select a row of control points including active one. "
824 "Successive use on the same point switches between U/V directions";
825
826 /* API callbacks. */
827 ot->exec = select_row_exec;
829
830 /* flags */
832}
833
835
836/* -------------------------------------------------------------------- */
839
841{
842 const Main *bmain = CTX_data_main(C);
843 Scene *scene = CTX_data_scene(C);
844 ViewLayer *view_layer = CTX_data_view_layer(C);
846 *bmain, scene, view_layer, CTX_wm_view3d(C));
847
848 for (Object *obedit : objects) {
849
850 ListBaseT<Nurb> *editnurb = object_editcurve_get(obedit);
851 select_adjacent_cp(editnurb, 1, false, SELECT);
852
853 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
854 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
855 }
856 return OPERATOR_FINISHED;
857}
858
860{
861 /* identifiers */
862 ot->name = "Select Next";
863 ot->idname = "CURVE_OT_select_next";
864 ot->description = "Select control points following already selected ones along the curves";
865
866 /* API callbacks. */
869
870 /* flags */
872}
873
875
876/* -------------------------------------------------------------------- */
879
881{
882 const Main *bmain = CTX_data_main(C);
883 Scene *scene = CTX_data_scene(C);
884 ViewLayer *view_layer = CTX_data_view_layer(C);
886 *bmain, scene, view_layer, CTX_wm_view3d(C));
887
888 for (Object *obedit : objects) {
889
890 ListBaseT<Nurb> *editnurb = object_editcurve_get(obedit);
891 select_adjacent_cp(editnurb, -1, false, SELECT);
892
893 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
894 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
895 }
896 return OPERATOR_FINISHED;
897}
898
900{
901 /* identifiers */
902 ot->name = "Select Previous";
903 ot->idname = "CURVE_OT_select_previous";
904 ot->description = "Select control points preceding already selected ones along the curves";
905
906 /* API callbacks. */
909
910 /* flags */
912}
913
915
916/* -------------------------------------------------------------------- */
919
920static void curve_select_more(Object *obedit)
921{
922 ListBaseT<Nurb> *editnurb = object_editcurve_get(obedit);
923 BPoint *bp, *tempbp;
924 int a;
925 short sel = 0;
926
927 /* NOTE: NURBS surface is a special case because we mimic
928 * the behavior of "select more" of mesh tools.
929 * The algorithm is designed to work in planar cases so it
930 * may not be optimal always (example: end of NURBS sphere). */
931 if (obedit->type == OB_SURF) {
932 for (Nurb &nu : *editnurb) {
933 BLI_bitmap *selbpoints;
934 a = nu.pntsu * nu.pntsv;
935 bp = nu.bp;
936 selbpoints = BLI_BITMAP_NEW(a + 1, "selectlist");
937 while (a > 0) {
938 if (!BLI_BITMAP_TEST(selbpoints, a) && (bp->hide == 0) && (bp->f1 & SELECT)) {
939 /* upper control point */
940 if (a % nu.pntsu != 0) {
941 tempbp = bp - 1;
942 if (!(tempbp->f1 & SELECT)) {
943 select_bpoint(tempbp, true, BEZT_FLAG_SELECT, VISIBLE);
944 }
945 }
946
947 /* left control point. select only if it is not selected already */
948 if (a - nu.pntsu > 0) {
949 sel = 0;
950 tempbp = bp + nu.pntsu;
951 if (!(tempbp->f1 & SELECT)) {
952 sel = select_bpoint(tempbp, true, BEZT_FLAG_SELECT, VISIBLE);
953 }
954 /* make sure selected bpoint is discarded */
955 if (sel == 1) {
956 BLI_BITMAP_ENABLE(selbpoints, a - nu.pntsu);
957 }
958 }
959
960 /* right control point */
961 if (a + nu.pntsu <= nu.pntsu * nu.pntsv) {
962 tempbp = bp - nu.pntsu;
963 if (!(tempbp->f1 & SELECT)) {
964 select_bpoint(tempbp, true, BEZT_FLAG_SELECT, VISIBLE);
965 }
966 }
967
968 /* lower control point. skip next bp in case selection was made */
969 if (a % nu.pntsu != 1) {
970 sel = 0;
971 tempbp = bp + 1;
972 if (!(tempbp->f1 & SELECT)) {
973 sel = select_bpoint(tempbp, true, BEZT_FLAG_SELECT, VISIBLE);
974 }
975 if (sel) {
976 bp++;
977 a--;
978 }
979 }
980 }
981
982 bp++;
983 a--;
984 }
985
986 MEM_delete(selbpoints);
987 }
988 }
989 else {
990 select_adjacent_cp(editnurb, 1, false, SELECT);
991 select_adjacent_cp(editnurb, -1, false, SELECT);
992 }
993}
994
996{
997 const Main *bmain = CTX_data_main(C);
998 Scene *scene = CTX_data_scene(C);
999 ViewLayer *view_layer = CTX_data_view_layer(C);
1001 *bmain, scene, view_layer, CTX_wm_view3d(C));
1002 for (Object *obedit : objects) {
1003 curve_select_more(obedit);
1004 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
1005 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
1006 }
1007 return OPERATOR_FINISHED;
1008}
1009
1011{
1012 /* identifiers */
1013 ot->name = "Select More";
1014 ot->idname = "CURVE_OT_select_more";
1015 ot->description = "Select control points at the boundary of each selection region";
1016
1017 /* API callbacks. */
1020
1021 /* flags */
1023}
1024
1026
1027/* -------------------------------------------------------------------- */
1030
1031/* basic method: deselect if control point doesn't have all neighbors selected */
1032static void curve_select_less(Object *obedit)
1033{
1034 ListBaseT<Nurb> *editnurb = object_editcurve_get(obedit);
1035 BPoint *bp;
1036 BezTriple *bezt;
1037 int a;
1038 int sel = 0;
1039 bool lastsel = false;
1040
1041 if (obedit->type == OB_SURF) {
1042 for (Nurb &nu : *editnurb) {
1043 BLI_bitmap *selbpoints;
1044 a = nu.pntsu * nu.pntsv;
1045 bp = nu.bp;
1046 selbpoints = BLI_BITMAP_NEW(a, "selectlist");
1047 while (a--) {
1048 if ((bp->hide == 0) && (bp->f1 & SELECT)) {
1049 sel = 0;
1050
1051 /* check if neighbors have been selected */
1052 /* edges of surface are an exception */
1053 if ((a + 1) % nu.pntsu == 0) {
1054 sel++;
1055 }
1056 else {
1057 bp--;
1058 if (BLI_BITMAP_TEST(selbpoints, a + 1) || ((bp->hide == 0) && (bp->f1 & SELECT))) {
1059 sel++;
1060 }
1061 bp++;
1062 }
1063
1064 if ((a + 1) % nu.pntsu == 1) {
1065 sel++;
1066 }
1067 else {
1068 bp++;
1069 if ((bp->hide == 0) && (bp->f1 & SELECT)) {
1070 sel++;
1071 }
1072 bp--;
1073 }
1074
1075 if (a + 1 > nu.pntsu * nu.pntsv - nu.pntsu) {
1076 sel++;
1077 }
1078 else {
1079 bp -= nu.pntsu;
1080 if (BLI_BITMAP_TEST(selbpoints, a + nu.pntsu) ||
1081 ((bp->hide == 0) && (bp->f1 & SELECT)))
1082 {
1083 sel++;
1084 }
1085 bp += nu.pntsu;
1086 }
1087
1088 if (a < nu.pntsu) {
1089 sel++;
1090 }
1091 else {
1092 bp += nu.pntsu;
1093 if ((bp->hide == 0) && (bp->f1 & SELECT)) {
1094 sel++;
1095 }
1096 bp -= nu.pntsu;
1097 }
1098
1099 if (sel != 4) {
1101 BLI_BITMAP_ENABLE(selbpoints, a);
1102 }
1103 }
1104 else {
1105 lastsel = false;
1106 }
1107
1108 bp++;
1109 }
1110
1111 MEM_delete(selbpoints);
1112 }
1113 }
1114 else {
1115 for (Nurb &nu : *editnurb) {
1116 lastsel = false;
1117 /* check what type of curve/nurb it is */
1118 if (nu.type == CU_BEZIER) {
1119 a = nu.pntsu;
1120 bezt = nu.bezt;
1121 while (a--) {
1122 if ((bezt->hide == 0) && (bezt->f2 & SELECT)) {
1123 sel = (lastsel == 1);
1124
1125 /* check if neighbors have been selected */
1126 /* first and last are exceptions */
1127 if (a == nu.pntsu - 1) {
1128 sel++;
1129 }
1130 else {
1131 bezt--;
1132 if ((bezt->hide == 0) && (bezt->f2 & SELECT)) {
1133 sel++;
1134 }
1135 bezt++;
1136 }
1137
1138 if (a == 0) {
1139 sel++;
1140 }
1141 else {
1142 bezt++;
1143 if ((bezt->hide == 0) && (bezt->f2 & SELECT)) {
1144 sel++;
1145 }
1146 bezt--;
1147 }
1148
1149 if (sel != 2) {
1151 lastsel = true;
1152 }
1153 else {
1154 lastsel = false;
1155 }
1156 }
1157 else {
1158 lastsel = false;
1159 }
1160
1161 bezt++;
1162 }
1163 }
1164 else {
1165 a = nu.pntsu * nu.pntsv;
1166 bp = nu.bp;
1167 while (a--) {
1168 if ((lastsel == false) && (bp->hide == 0) && (bp->f1 & SELECT)) {
1169 sel = 0;
1170
1171 /* first and last are exceptions */
1172 if (a == nu.pntsu * nu.pntsv - 1) {
1173 sel++;
1174 }
1175 else {
1176 bp--;
1177 if ((bp->hide == 0) && (bp->f1 & SELECT)) {
1178 sel++;
1179 }
1180 bp++;
1181 }
1182
1183 if (a == 0) {
1184 sel++;
1185 }
1186 else {
1187 bp++;
1188 if ((bp->hide == 0) && (bp->f1 & SELECT)) {
1189 sel++;
1190 }
1191 bp--;
1192 }
1193
1194 if (sel != 2) {
1196 lastsel = true;
1197 }
1198 else {
1199 lastsel = false;
1200 }
1201 }
1202 else {
1203 lastsel = false;
1204 }
1205
1206 bp++;
1207 }
1208 }
1209 }
1210 }
1211}
1212
1214{
1215 const Main *bmain = CTX_data_main(C);
1216 const Scene *scene = CTX_data_scene(C);
1217 ViewLayer *view_layer = CTX_data_view_layer(C);
1219 *bmain, scene, view_layer, CTX_wm_view3d(C));
1220 for (Object *obedit : objects) {
1221 curve_select_less(obedit);
1222 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
1223 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
1224 }
1225 return OPERATOR_FINISHED;
1226}
1227
1229{
1230 /* identifiers */
1231 ot->name = "Select Less";
1232 ot->idname = "CURVE_OT_select_less";
1233 ot->description = "Deselect control points at the boundary of each selection region";
1234
1235 /* API callbacks. */
1238
1239 /* flags */
1241}
1242
1244
1245/* -------------------------------------------------------------------- */
1248
1250{
1251 const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
1252 const float randfac = RNA_float_get(op->ptr, "ratio");
1254
1255 const Main *bmain = CTX_data_main(C);
1256 const Scene *scene = CTX_data_scene(C);
1257 ViewLayer *view_layer = CTX_data_view_layer(C);
1259 *bmain, scene, view_layer, CTX_wm_view3d(C));
1260
1261 for (const int ob_index : objects.index_range()) {
1262 Object *obedit = objects[ob_index];
1263 ListBaseT<Nurb> *editnurb = object_editcurve_get(obedit);
1264 int seed_iter = seed;
1265
1266 /* This gives a consistent result regardless of object order. */
1267 if (ob_index) {
1268 seed_iter += BLI_ghashutil_strhash_p(obedit->id.name);
1269 }
1270
1271 int totvert = 0;
1272 for (Nurb &nu : *editnurb) {
1273 if (nu.type == CU_BEZIER) {
1274 int a = nu.pntsu;
1275 BezTriple *bezt = nu.bezt;
1276 while (a--) {
1277 if (!bezt->hide) {
1278 totvert++;
1279 }
1280 bezt++;
1281 }
1282 }
1283 else {
1284 int a = nu.pntsu * nu.pntsv;
1285 BPoint *bp = nu.bp;
1286 while (a--) {
1287 if (!bp->hide) {
1288 totvert++;
1289 }
1290 bp++;
1291 }
1292 }
1293 }
1294
1295 BLI_bitmap *verts_selection_mask = BLI_BITMAP_NEW(totvert, __func__);
1296 const int count_select = totvert * randfac;
1297 for (int i = 0; i < count_select; i++) {
1298 BLI_BITMAP_SET(verts_selection_mask, i, true);
1299 }
1300 BLI_bitmap_randomize(verts_selection_mask, totvert, seed_iter);
1301
1302 int bit_index = 0;
1303 for (Nurb &nu : *editnurb) {
1304 if (nu.type == CU_BEZIER) {
1305 int a = nu.pntsu;
1306 BezTriple *bezt = nu.bezt;
1307
1308 while (a--) {
1309 if (!bezt->hide) {
1310 if (BLI_BITMAP_TEST(verts_selection_mask, bit_index)) {
1312 }
1313 bit_index++;
1314 }
1315 bezt++;
1316 }
1317 }
1318 else {
1319 int a = nu.pntsu * nu.pntsv;
1320 BPoint *bp = nu.bp;
1321
1322 while (a--) {
1323 if (!bp->hide) {
1324 if (BLI_BITMAP_TEST(verts_selection_mask, bit_index)) {
1326 }
1327 bit_index++;
1328 }
1329 bp++;
1330 }
1331 }
1332 }
1333
1334 MEM_delete(verts_selection_mask);
1335 BKE_curve_nurb_vert_active_validate(id_cast<Curve *>(obedit->data));
1338 }
1339
1340 return OPERATOR_FINISHED;
1341}
1342
1344{
1345 /* identifiers */
1346 ot->name = "Select Random";
1347 ot->idname = "CURVE_OT_select_random";
1348 ot->description = "Randomly select some control points";
1349
1350 /* API callbacks. */
1353
1354 /* flags */
1356
1357 /* properties */
1359}
1360
1362
1363/* -------------------------------------------------------------------- */
1366
1367static void select_nth_bezt(Nurb *nu, BezTriple *bezt, const CheckerIntervalParams *params)
1368{
1369 int a, start;
1370
1371 start = bezt - nu->bezt;
1372 a = nu->pntsu;
1373 bezt = &nu->bezt[a - 1];
1374
1375 while (a--) {
1376 const int depth = abs(start - a);
1379 }
1380
1381 bezt--;
1382 }
1383}
1384
1385static void select_nth_bp(Nurb *nu, BPoint *bp, const CheckerIntervalParams *params)
1386{
1387 int a, startrow, startpnt;
1388 int row, pnt;
1389
1390 startrow = (bp - nu->bp) / nu->pntsu;
1391 startpnt = (bp - nu->bp) % nu->pntsu;
1392
1393 a = nu->pntsu * nu->pntsv;
1394 bp = &nu->bp[a - 1];
1395 row = nu->pntsv - 1;
1396 pnt = nu->pntsu - 1;
1397
1398 while (a--) {
1399 const int depth = abs(pnt - startpnt) + abs(row - startrow);
1402 }
1403
1404 pnt--;
1405 if (pnt < 0) {
1406 pnt = nu->pntsu - 1;
1407 row--;
1408 }
1409
1410 bp--;
1411 }
1412}
1413
1414static bool ed_curve_select_nth(Curve *cu, const CheckerIntervalParams *params)
1415{
1416 Nurb *nu = nullptr;
1417 void *vert = nullptr;
1418
1419 if (!BKE_curve_nurb_vert_active_get(cu, &nu, &vert)) {
1420 return false;
1421 }
1422
1423 if (nu->bezt) {
1424 select_nth_bezt(nu, static_cast<BezTriple *>(vert), params);
1425 }
1426 else {
1427 select_nth_bp(nu, static_cast<BPoint *>(vert), params);
1428 }
1429
1430 return true;
1431}
1432
1434{
1435 const Main *bmain = CTX_data_main(C);
1436 const Scene *scene = CTX_data_scene(C);
1437 ViewLayer *view_layer = CTX_data_view_layer(C);
1438 Object *obact = CTX_data_edit_object(C);
1439 View3D *v3d = CTX_wm_view3d(C);
1440 bool changed = false;
1441
1442 CheckerIntervalParams op_params;
1444
1446 *bmain, scene, view_layer, CTX_wm_view3d(C));
1447 for (Object *obedit : objects) {
1448 Curve *cu = id_cast<Curve *>(obedit->data);
1449
1450 if (!ED_curve_select_check(v3d, cu->editnurb)) {
1451 continue;
1452 }
1453
1454 if (ed_curve_select_nth(id_cast<Curve *>(obedit->data), &op_params) == true) {
1455 changed = true;
1456
1457 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
1458 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
1459 }
1460 }
1461
1462 if (!changed) {
1463 if (obact->type == OB_SURF) {
1464 BKE_report(op->reports, RPT_ERROR, "Surface(s) have no active point");
1465 }
1466 else {
1467 BKE_report(op->reports, RPT_ERROR, "Curve(s) have no active point");
1468 }
1469 return OPERATOR_CANCELLED;
1470 }
1471 return OPERATOR_FINISHED;
1472}
1473
1475{
1476 /* identifiers */
1477 ot->name = "Checker Deselect";
1478 ot->description = "Deselect every Nth point starting from the active one";
1479 ot->idname = "CURVE_OT_select_nth";
1480
1481 /* API callbacks. */
1482 ot->exec = select_nth_exec;
1484
1485 /* flags */
1487
1489}
1490
1492
1493/* -------------------------------------------------------------------- */
1496
1498 {SIM_CMP_EQ, "EQUAL", 0, "Equal", ""},
1499 {SIM_CMP_GT, "GREATER", 0, "Greater", ""},
1500 {SIM_CMP_LT, "LESS", 0, "Less", ""},
1501
1502 {0, nullptr, 0, nullptr, nullptr},
1503};
1504
1505enum {
1510};
1511
1513 {SIMCURHAND_TYPE, "TYPE", 0, "Type", ""},
1514 {SIMCURHAND_RADIUS, "RADIUS", 0, "Radius", ""},
1515 {SIMCURHAND_WEIGHT, "WEIGHT", 0, "Weight", ""},
1516 {SIMCURHAND_DIRECTION, "DIRECTION", 0, "Direction", ""},
1517 {0, nullptr, 0, nullptr, nullptr},
1518};
1519
1521 Nurb *nu,
1522 BezTriple *bezt,
1523 float r_dir[3])
1524{
1525 float rsmat[3][3];
1526 BKE_nurb_bezt_calc_normal(nu, bezt, r_dir);
1527 copy_m3_m4(rsmat, ob->object_to_world().ptr());
1528 mul_m3_v3(rsmat, r_dir);
1529 normalize_v3(r_dir);
1530}
1531
1532static void nurb_bpoint_direction_worldspace_get(Object *ob, Nurb *nu, BPoint *bp, float r_dir[3])
1533{
1534 float rsmat[3][3];
1535 BKE_nurb_bpoint_calc_normal(nu, bp, r_dir);
1536 copy_m3_m4(rsmat, ob->object_to_world().ptr());
1537 mul_m3_v3(rsmat, r_dir);
1538 normalize_v3(r_dir);
1539}
1540
1542 Object *ob, Nurb *nu, const int type, Map<float, int> &points_1d, Map<float3, int> &points_3d)
1543{
1544 float tree_entry[3] = {0.0f, 0.0f, 0.0f};
1545
1546 if (nu->type == CU_BEZIER) {
1547 BezTriple *bezt;
1548 int i;
1549 int tree_index = 0;
1550
1551 for (i = nu->pntsu, bezt = nu->bezt; i--; bezt++) {
1552 if ((!bezt->hide) && (bezt->f1 & SELECT)) {
1553
1554 switch (type) {
1555 case SIMCURHAND_RADIUS: {
1556 float radius_ref = bezt->radius;
1557 tree_entry[0] = radius_ref;
1558 break;
1559 }
1560 case SIMCURHAND_WEIGHT: {
1561 float weight_ref = bezt->weight;
1562 tree_entry[0] = weight_ref;
1563 break;
1564 }
1565 case SIMCURHAND_DIRECTION: {
1566 nurb_bezt_direction_worldspace_get(ob, nu, bezt, tree_entry);
1567 break;
1568 }
1569 }
1570
1571 switch (type) {
1572 case SIMCURHAND_RADIUS:
1573 case SIMCURHAND_WEIGHT:
1574 points_1d.add(tree_entry[0], tree_index++);
1575 break;
1577 points_3d.add(tree_entry, tree_index++);
1578 break;
1579 }
1580 }
1581 }
1582 }
1583 else {
1584 BPoint *bp;
1585 int i;
1586 int tree_index = 0;
1587
1588 for (i = nu->pntsu * nu->pntsv, bp = nu->bp; i--; bp++) {
1589 if (!bp->hide && bp->f1 & SELECT) {
1590 switch (type) {
1591 case SIMCURHAND_RADIUS: {
1592 float radius_ref = bp->radius;
1593 tree_entry[0] = radius_ref;
1594 break;
1595 }
1596 case SIMCURHAND_WEIGHT: {
1597 float weight_ref = bp->weight;
1598 tree_entry[0] = weight_ref;
1599 break;
1600 }
1601 case SIMCURHAND_DIRECTION: {
1602 nurb_bpoint_direction_worldspace_get(ob, nu, bp, tree_entry);
1603 break;
1604 }
1605 }
1606
1607 switch (type) {
1608 case SIMCURHAND_RADIUS:
1609 case SIMCURHAND_WEIGHT:
1610 points_1d.add(tree_entry[0], tree_index++);
1611 break;
1613 points_3d.add(tree_entry, tree_index++);
1614 break;
1615 }
1616 }
1617 }
1618 }
1619}
1620
1622 Nurb *nu,
1623 const int type,
1624 const KDTree<float> *tree_1d,
1625 const KDTree<float3> *tree_3d,
1626 const float thresh,
1627 const int compare)
1628{
1629 const float thresh_cos = cosf(thresh * float(M_PI_2));
1630 bool changed = false;
1631
1632 if (nu->type == CU_BEZIER) {
1633 BezTriple *bezt;
1634 int i;
1635
1636 for (i = nu->pntsu, bezt = nu->bezt; i--; bezt++) {
1637 if (!bezt->hide) {
1638 bool select = false;
1639
1640 switch (type) {
1641 case SIMCURHAND_RADIUS: {
1642 float radius_ref = bezt->radius;
1644 tree_1d, radius_ref, thresh, eSimilarCmp(compare)))
1645 {
1646 select = true;
1647 }
1648 break;
1649 }
1650 case SIMCURHAND_WEIGHT: {
1651 float weight_ref = bezt->weight;
1653 tree_1d, weight_ref, thresh, eSimilarCmp(compare)))
1654 {
1655 select = true;
1656 }
1657 break;
1658 }
1659 case SIMCURHAND_DIRECTION: {
1660 float dir[3];
1661 nurb_bezt_direction_worldspace_get(ob, nu, bezt, dir);
1662 KDTreeNearest<float3> nearest;
1663 if (kdtree_find_nearest<float3>(tree_3d, dir, &nearest) != -1) {
1664 float orient = angle_normalized_v3v3(dir, nearest.co);
1665 float delta = thresh_cos - fabsf(cosf(orient));
1666 if (ED_select_similar_compare_float(delta, thresh, eSimilarCmp(compare))) {
1667 select = true;
1668 }
1669 }
1670 break;
1671 }
1672 }
1673
1674 if (select) {
1676 changed = true;
1677 }
1678 }
1679 }
1680 }
1681 else {
1682 BPoint *bp;
1683 int i;
1684
1685 for (i = nu->pntsu * nu->pntsv, bp = nu->bp; i--; bp++) {
1686 if (!bp->hide) {
1687 bool select = false;
1688
1689 switch (type) {
1690 case SIMCURHAND_RADIUS: {
1691 float radius_ref = bp->radius;
1693 tree_1d, radius_ref, thresh, eSimilarCmp(compare)))
1694 {
1695 select = true;
1696 }
1697 break;
1698 }
1699 case SIMCURHAND_WEIGHT: {
1700 float weight_ref = bp->weight;
1702 tree_1d, weight_ref, thresh, eSimilarCmp(compare)))
1703 {
1704 select = true;
1705 }
1706 break;
1707 }
1708 case SIMCURHAND_DIRECTION: {
1709 float dir[3];
1710 nurb_bpoint_direction_worldspace_get(ob, nu, bp, dir);
1711 KDTreeNearest<float3> nearest;
1712 if (kdtree_find_nearest<float3>(tree_3d, dir, &nearest) != -1) {
1713 float orient = angle_normalized_v3v3(dir, nearest.co);
1714 float delta = fabsf(cosf(orient)) - thresh_cos;
1715 if (ED_select_similar_compare_float(delta, thresh, eSimilarCmp(compare))) {
1716 select = true;
1717 }
1718 }
1719 break;
1720 }
1721 }
1722
1723 if (select) {
1725 changed = true;
1726 }
1727 }
1728 }
1729 }
1730 return changed;
1731}
1732
1734{
1735 /* Get props. */
1736 const int optype = RNA_enum_get(op->ptr, "type");
1737 const float thresh = RNA_float_get(op->ptr, "threshold");
1738 const int compare = RNA_enum_get(op->ptr, "compare");
1739
1740 const Main *bmain = CTX_data_main(C);
1741 const Scene *scene = CTX_data_scene(C);
1742 ViewLayer *view_layer = CTX_data_view_layer(C);
1743 View3D *v3d = CTX_wm_view3d(C);
1745 *bmain, scene, view_layer, CTX_wm_view3d(C));
1746
1747 bool anything_selected = false;
1748 for (Object *obedit : objects) {
1749 Curve *cu = id_cast<Curve *>(obedit->data);
1750 if (!ED_curve_select_count(v3d, cu->editnurb)) {
1751 continue;
1752 }
1753 anything_selected = true;
1754 break;
1755 }
1756
1757 if (!anything_selected) {
1758 BKE_report(op->reports, RPT_ERROR, "No control point selected");
1759 return OPERATOR_CANCELLED;
1760 }
1761
1762 Map<float, int> points_1d;
1763 Map<float3, int> points_3d;
1764
1765 short type_ref = 0;
1766
1767 /* Get type of selected control points. */
1768 for (Object *obedit : objects) {
1769 Curve *cu = id_cast<Curve *>(obedit->data);
1770 EditNurb *editnurb = cu->editnurb;
1771
1772 for (Nurb &nu : editnurb->nurbs) {
1773 if (!ED_curve_nurb_select_check(v3d, &nu)) {
1774 continue;
1775 }
1776 switch (optype) {
1777 case SIMCURHAND_TYPE: {
1778 type_ref |= nu.type;
1779 break;
1780 }
1781 case SIMCURHAND_RADIUS:
1782 case SIMCURHAND_WEIGHT:
1784 curve_nurb_selected_type_get(obedit, &nu, optype, points_1d, points_3d);
1785 break;
1786 }
1787 }
1788 }
1789
1790 KDTree<float> *tree_1d = nullptr;
1791 KDTree<float3> *tree_3d = nullptr;
1792
1793 switch (optype) {
1794 case SIMCURHAND_RADIUS:
1795 case SIMCURHAND_WEIGHT: {
1796 tree_1d = kdtree_new<float>(points_1d.size());
1797 for (const auto &[pos, index] : points_1d.items()) {
1798 kdtree_insert(tree_1d, index, pos);
1799 }
1800 kdtree_balance<float>(tree_1d);
1801 break;
1802 }
1803 case SIMCURHAND_DIRECTION: {
1804 tree_3d = kdtree_new<float3>(points_3d.size());
1805 for (const auto &[pos, index] : points_3d.items()) {
1806 kdtree_insert(tree_3d, index, pos);
1807 }
1808 kdtree_balance<float3>(tree_3d);
1809 break;
1810 }
1811 }
1812
1813 /* Select control points with desired type. */
1814 for (Object *obedit : objects) {
1815 Curve *cu = id_cast<Curve *>(obedit->data);
1816 EditNurb *editnurb = cu->editnurb;
1817 bool changed = false;
1818
1819 for (Nurb &nu : editnurb->nurbs) {
1820 switch (optype) {
1821 case SIMCURHAND_TYPE: {
1822 if (nu.type & type_ref) {
1823 changed |= ED_curve_nurb_select_all(&nu);
1824 }
1825 break;
1826 }
1827 case SIMCURHAND_RADIUS:
1828 case SIMCURHAND_WEIGHT:
1831 obedit, &nu, optype, tree_1d, tree_3d, thresh, compare);
1832 break;
1833 }
1834 }
1835
1836 if (changed) {
1837 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
1838 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
1839 }
1840 }
1841
1842 if (tree_1d != nullptr) {
1843 kdtree_free<float>(tree_1d);
1844 }
1845 if (tree_3d != nullptr) {
1846 kdtree_free<float3>(tree_3d);
1847 }
1848 return OPERATOR_FINISHED;
1849}
1850
1852{
1853 /* identifiers */
1854 ot->name = "Select Similar";
1855 ot->idname = "CURVE_OT_select_similar";
1856 ot->description = "Select similar curve points by property type";
1857
1858 /* API callbacks. */
1859 ot->invoke = WM_menu_invoke;
1862
1863 /* flags */
1865
1866 /* properties */
1867 ot->prop = RNA_def_enum(
1868 ot->srna, "type", curve_prop_similar_types, SIMCURHAND_WEIGHT, "Type", "");
1869 RNA_def_enum(ot->srna, "compare", curve_prop_similar_compare_types, SIM_CMP_EQ, "Compare", "");
1870 RNA_def_float(ot->srna, "threshold", 0.1, 0.0, FLT_MAX, "Threshold", "", 0.0, 100.0);
1871}
1872
1874
1875/* -------------------------------------------------------------------- */
1878
1879static float curve_calc_dist_pair(const Nurb *nu, int a, int b)
1880{
1881 const float *a_fl, *b_fl;
1882
1883 if (nu->type == CU_BEZIER) {
1884 a_fl = nu->bezt[a].vec[1];
1885 b_fl = nu->bezt[b].vec[1];
1886 }
1887 else {
1888 a_fl = nu->bp[a].vec;
1889 b_fl = nu->bp[b].vec;
1890 }
1891
1892 return len_v3v3(a_fl, b_fl);
1893}
1894
1895static float curve_calc_dist_span(Nurb *nu, int vert_src, int vert_dst)
1896{
1897 const int u = nu->pntsu;
1898 int i_prev, i;
1899 float dist = 0.0f;
1900
1901 BLI_assert(nu->pntsv <= 1);
1902
1903 i_prev = vert_src;
1904 i = (i_prev + 1) % u;
1905
1906 while (true) {
1907 dist += curve_calc_dist_pair(nu, i_prev, i);
1908
1909 if (i == vert_dst) {
1910 break;
1911 }
1912 i = (i + 1) % u;
1913 }
1914 return dist;
1915}
1916
1917static void curve_select_shortest_path_curve(Nurb *nu, int vert_src, int vert_dst)
1918{
1919 const int u = nu->pntsu;
1920 int i;
1921
1922 if (vert_src > vert_dst) {
1923 std::swap(vert_src, vert_dst);
1924 }
1925
1926 if (nu->flagu & CU_NURB_CYCLIC) {
1927 if (curve_calc_dist_span(nu, vert_src, vert_dst) >
1928 curve_calc_dist_span(nu, vert_dst, vert_src))
1929 {
1930 std::swap(vert_src, vert_dst);
1931 }
1932 }
1933
1934 i = vert_src;
1935 while (true) {
1936 if (nu->type & CU_BEZIER) {
1938 }
1939 else {
1940 select_bpoint(&nu->bp[i], true, BEZT_FLAG_SELECT, HIDDEN);
1941 }
1942
1943 if (i == vert_dst) {
1944 break;
1945 }
1946 i = (i + 1) % u;
1947 }
1948}
1949
1950static void curve_select_shortest_path_surf(Nurb *nu, int vert_src, int vert_dst)
1951{
1952 int totu = nu->pntsu;
1953 int totv = nu->pntsv;
1954 int vert_num = totu * totv;
1955
1956 /* custom data */
1957 struct PointAdj {
1958 int vert, vert_prev;
1959 float cost;
1960 } *data;
1961
1962 /* init connectivity data */
1963 data = MEM_new_array_uninitialized<PointAdj>(vert_num, __func__);
1964 for (int i = 0; i < vert_num; i++) {
1965 data[i].vert = i;
1966 data[i].vert_prev = -1;
1967 data[i].cost = FLT_MAX;
1968 }
1969
1970 /* init heap */
1972
1973 int vert_curr = data[vert_src].vert;
1974 BLI_heapsimple_insert(heap, 0.0f, &data[vert_src].vert);
1975 data[vert_src].cost = 0.0f;
1976 data[vert_src].vert_prev = vert_src; /* nop */
1977
1978 while (!BLI_heapsimple_is_empty(heap)) {
1979 vert_curr = *(static_cast<int *>(BLI_heapsimple_pop_min(heap)));
1980 if (vert_curr == vert_dst) {
1981 break;
1982 }
1983
1984 int u, v;
1985 BKE_nurb_index_to_uv(nu, vert_curr, &u, &v);
1986
1987 /* loop over 4 adjacent verts */
1988 for (int sign = -1; sign != 3; sign += 2) {
1989 for (int axis = 0; axis != 2; axis += 1) {
1990 int uv_other[2] = {u, v};
1991 int vert_other;
1992
1993 uv_other[axis] += sign;
1994
1995 vert_other = BKE_nurb_index_from_uv(nu, uv_other[0], uv_other[1]);
1996 if (vert_other != -1) {
1997 const float dist = data[vert_curr].cost +
1998 curve_calc_dist_pair(nu, vert_curr, vert_other);
1999
2000 if (data[vert_other].cost > dist) {
2001 data[vert_other].cost = dist;
2002 if (data[vert_other].vert_prev == -1) {
2003 BLI_heapsimple_insert(heap, data[vert_other].cost, &data[vert_other].vert);
2004 }
2005 data[vert_other].vert_prev = vert_curr;
2006 }
2007 }
2008 }
2009 }
2010 }
2011
2012 BLI_heapsimple_free(heap, nullptr);
2013
2014 if (vert_curr == vert_dst) {
2015 int i = 0;
2016 while (vert_curr != vert_src && i++ < vert_num) {
2017 if (nu->type == CU_BEZIER) {
2018 select_beztriple(&nu->bezt[vert_curr], true, BEZT_FLAG_SELECT, HIDDEN);
2019 }
2020 else {
2021 select_bpoint(&nu->bp[vert_curr], true, BEZT_FLAG_SELECT, HIDDEN);
2022 }
2023 vert_curr = data[vert_curr].vert_prev;
2024 }
2025 }
2026
2027 MEM_delete(data);
2028}
2029
2031 wmOperator *op,
2032 const wmEvent *event)
2033{
2034 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2035 Nurb *nu_dst;
2036 BezTriple *bezt_dst;
2037 BPoint *bp_dst;
2038 int vert_dst;
2039 void *vert_dst_p;
2040 Base *basact = nullptr;
2041
2043 ViewContext vc = ED_view3d_viewcontext_init(C, depsgraph);
2044 copy_v2_v2_int(vc.mval, event->mval);
2045
2046 if (!ED_curve_pick_vert(&vc, 1, &nu_dst, &bezt_dst, &bp_dst, nullptr, &basact)) {
2047 return OPERATOR_PASS_THROUGH;
2048 }
2049
2051 Object *obedit = basact->object;
2052 Curve *cu = id_cast<Curve *>(obedit->data);
2053 Nurb *nu_src = BKE_curve_nurb_active_get(cu);
2054 int vert_src = cu->actvert;
2055
2056 if (vert_src == CU_ACT_NONE) {
2057 return OPERATOR_PASS_THROUGH;
2058 }
2059
2060 if (nu_src != nu_dst) {
2061 BKE_report(op->reports, RPT_ERROR, "Control point belongs to another spline");
2062 return OPERATOR_CANCELLED;
2063 }
2064
2065 vert_dst_p = bezt_dst ? static_cast<void *>(bezt_dst) : static_cast<void *>(bp_dst);
2066 vert_dst = BKE_curve_nurb_vert_index_get(nu_dst, vert_dst_p);
2067 if (vert_src == vert_dst) {
2068 return OPERATOR_CANCELLED;
2069 }
2070
2071 if ((obedit->type == OB_SURF) && (nu_src->pntsv > 1)) {
2072 curve_select_shortest_path_surf(nu_src, vert_src, vert_dst);
2073 }
2074 else {
2075 curve_select_shortest_path_curve(nu_src, vert_src, vert_dst);
2076 }
2077
2078 BKE_curve_nurb_vert_active_set(cu, nu_dst, vert_dst_p);
2079
2081 if (BKE_view_layer_active_base_get(vc.view_layer) != basact) {
2083 }
2084
2087 return OPERATOR_FINISHED;
2088}
2089
2091{
2092 /* identifiers */
2093 ot->name = "Pick Shortest Path";
2094 ot->idname = "CURVE_OT_shortest_path_pick";
2095 ot->description = "Select shortest path between two selections";
2096
2097 /* API callbacks. */
2100
2101 /* flags */
2103}
2104
2106
2107} // namespace blender
#define SELECT
Definition BKE_mask.hh:19
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.hh:39
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.hh:63
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.hh:80
#define BLI_BITMAP_SET(_bitmap, _index, _set)
A min-heap / priority queue ADT.
A KD-tree for nearest neighbor search.
#define M_PI_2
Random number functions.
#define CU_ACT_NONE
#define BEZT_ISSEL_ANY_HIDDENHANDLES(v3d, bezt)
#define BEZT_SEL_ALL(bezt)
#define BEZT_ISSEL_ANY(bezt)
#define BEZT_DESEL_ALL(bezt)
#define BEZT_ISSEL_ALL(bezt)
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_SELECT
Definition WM_types.hh:551
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
int64_t size() const
Definition BLI_map.hh:976
ItemIterator items() const &
Definition BLI_map.hh:902
IndexRange index_range() const
#define FLT_MAX
constexpr T sign(T) RET
#define abs
void base_activate(bContext *C, Base *base)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_curve_nurb_vert_active_set(Curve *cu, const Nurb *nu, const void *vert)
Definition curve.cc:5048
void mul_m3_v3(const float M[3][3], float r[3])
void kdtree_balance(KDTree< CoordT > *tree)
void CURVE_OT_select_random(wmOperatorType *ot)
static void nurb_bezt_direction_worldspace_get(Object *ob, Nurb *nu, BezTriple *bezt, float r_dir[3])
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
static void curve_select_shortest_path_surf(Nurb *nu, int vert_src, int vert_dst)
int BKE_curve_nurb_vert_index_get(const Nurb *nu, const void *vert)
Definition curve.cc:5037
Nurb * BKE_curve_nurb_active_get(Curve *cu)
Definition curve.cc:5022
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
void CURVE_OT_select_next(wmOperatorType *ot)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void WM_operator_properties_select_random(wmOperatorType *ot)
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
void CURVE_OT_select_all(wmOperatorType *ot)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void copy_m3_m4(float m1[3][3], const float m2[4][4])
static void curve_select_less(Object *obedit)
static bool ed_curve_select_nth(Curve *cu, const CheckerIntervalParams *params)
static wmOperatorStatus de_select_all_exec(bContext *C, wmOperator *op)
static wmOperatorStatus de_select_first_exec(bContext *C, wmOperator *)
static wmOperatorStatus curve_select_similar_exec(bContext *C, wmOperator *op)
int WM_operator_properties_select_random_seed_increment_get(wmOperator *op)
static wmOperatorStatus select_linked_exec(bContext *C, wmOperator *)
static void nurb_bpoint_direction_worldspace_get(Object *ob, Nurb *nu, BPoint *bp, float r_dir[3])
static bool swap_selection_bpoint(BPoint *bp)
bool ED_operator_editcurve(bContext *C)
static const EnumPropertyItem curve_prop_similar_types[]
static wmOperatorStatus select_next_exec(bContext *C, wmOperator *)
static void select_nth_bp(Nurb *nu, BPoint *bp, const CheckerIntervalParams *params)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
bool ED_curve_deselect_all_multi_ex(Span< Base * > bases)
void BKE_nurb_bezt_calc_normal(Nurb *nu, BezTriple *bezt, float r_normal[3])
Definition curve.cc:1032
void BLI_bitmap_randomize(unsigned int *bitmap, unsigned int bits_num, unsigned int seed) ATTR_NONNULL(1)
Definition rand.cc:166
void view3d_operator_needs_gpu(const bContext *C)
Base * BKE_view_layer_active_base_get(ViewLayer *view_layer)
ATTR_WARN_UNUSED_RESULT const BMVert * v
bool ED_curve_deselect_all_multi(bContext *C)
bool ED_operator_editsurfcurve_region_view3d(bContext *C)
void BKE_nurbList_flag_set(ListBaseT< Nurb > *editnurb, eBezTriple_Flag flag, bool set)
Definition curve.cc:4376
void WM_operator_properties_checker_interval(wmOperatorType *ot, bool nth_can_disable)
static wmOperatorStatus curve_select_more_exec(bContext *C, wmOperator *)
void BKE_nurb_index_to_uv(Nurb *nu, int index, int *r_u, int *r_v)
Definition curve.cc:937
ViewLayer * CTX_data_view_layer(const bContext *C)
void WM_operator_properties_select_all(wmOperatorType *ot)
void BKE_curve_nurb_vert_active_validate(Curve *cu)
Definition curve.cc:5092
bool ED_curve_nurb_deselect_all(const Nurb *nu)
KDTree< CoordT > * kdtree_new(uint nodes_len_capacity)
Definition BLI_kdtree.hh:65
void ED_view3d_viewcontext_init_object(ViewContext *vc, Object *obact)
static wmOperatorStatus select_row_exec(bContext *C, wmOperator *)
bool ED_curve_select_swap(EditNurb *editnurb, bool hide_handles)
bool ED_curve_nurb_select_check(const View3D *v3d, const Nurb *nu)
void * BLI_heapsimple_pop_min(HeapSimple *heap) ATTR_NONNULL(1)
bool ED_select_similar_compare_float_tree(const KDTree< float > *tree, float length, float thresh, eSimilarCmp compare)
static bool curve_nurb_select_similar_type(Object *ob, Nurb *nu, const int type, const KDTree< float > *tree_1d, const KDTree< float3 > *tree_3d, const float thresh, const int compare)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
static float curve_calc_dist_span(Nurb *nu, int vert_src, int vert_dst)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
static const EnumPropertyItem curve_prop_similar_compare_types[]
bool WM_operator_properties_checker_interval_test(const CheckerIntervalParams *op_params, int depth)
static void curve_select_shortest_path_curve(Nurb *nu, int vert_src, int vert_dst)
Object * CTX_data_edit_object(const bContext *C)
bool ED_curve_select_all(EditNurb *editnurb)
static bool swap_selection_beztriple(BezTriple *bezt)
void kdtree_insert(KDTree< CoordT > *tree, int index, const CoordT &co)
Definition BLI_kdtree.hh:96
static void select_adjacent_cp(ListBaseT< Nurb > *editnurb, short next, const bool cont, const bool selstatus)
int ED_curve_select_count(const View3D *v3d, const EditNurb *editnurb)
bool ED_select_similar_compare_float(float delta, float thresh, eSimilarCmp compare)
void CURVE_OT_de_select_last(wmOperatorType *ot)
static wmOperatorStatus curve_select_less_exec(bContext *C, wmOperator *)
static float curve_calc_dist_pair(const Nurb *nu, int a, int b)
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)
bool ED_operator_editsurfcurve(bContext *C)
static void curve_nurb_selected_type_get(Object *ob, Nurb *nu, const int type, Map< float, int > &points_1d, Map< float3, int > &points_3d)
void BLI_heapsimple_free(HeapSimple *heap, HeapSimpleFreeFP ptrfreefp) ATTR_NONNULL(1)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
ListBaseT< Nurb > * object_editcurve_get(Object *ob)
Definition editcurve.cc:95
static wmOperatorStatus de_select_last_exec(bContext *C, wmOperator *)
static void select_nth_bezt(Nurb *nu, BezTriple *bezt, const CheckerIntervalParams *params)
void CURVE_OT_shortest_path_pick(wmOperatorType *ot)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void CURVE_OT_de_select_first(wmOperatorType *ot)
HeapSimple * BLI_heapsimple_new() ATTR_WARN_UNUSED_RESULT
static wmOperatorStatus curve_select_random_exec(bContext *C, wmOperator *op)
static void curve_select_more(Object *obedit)
void CURVE_OT_select_more(wmOperatorType *ot)
int BKE_nurb_index_from_uv(Nurb *nu, int u, int v)
Definition curve.cc:915
MINLINE float normalize_v3(float n[3])
void kdtree_free(KDTree< CoordT > *tree)
Definition BLI_kdtree.hh:84
void BLI_heapsimple_insert(HeapSimple *heap, float value, void *ptr) ATTR_NONNULL(1)
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
Main * CTX_data_main(const bContext *C)
bool BKE_curve_nurb_vert_active_get(Curve *cu, Nurb **r_nu, void **r_vert)
Definition curve.cc:5065
static wmOperatorStatus edcu_shortest_path_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
bool select_beztriple(BezTriple *bezt, bool selstatus, eBezTriple_Flag flag, eVisible_Types hidden)
void CURVE_OT_select_similar(wmOperatorType *ot)
static wmOperatorStatus select_previous_exec(bContext *C, wmOperator *)
bool BLI_heapsimple_is_empty(const HeapSimple *heap) ATTR_NONNULL(1)
static wmOperatorStatus select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
bool ED_curve_select_check(const View3D *v3d, const EditNurb *editnurb)
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
void BKE_nurb_bpoint_calc_normal(Nurb *nu, BPoint *bp, float r_normal[3])
Definition curve.cc:1084
void CURVE_OT_select_linked_pick(wmOperatorType *ot)
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
Vector< Base * > BKE_view_layer_array_from_bases_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
bool select_bpoint(BPoint *bp, bool selstatus, eBezTriple_Flag flag, bool hidden)
Scene * CTX_data_scene(const bContext *C)
static wmOperatorStatus select_linked_invoke(bContext *C, wmOperator *op, const wmEvent *)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
void CURVE_OT_select_previous(wmOperatorType *ot)
bool ED_curve_pick_vert(ViewContext *vc, short sel, Nurb **r_nurb, BezTriple **r_bezt, BPoint **r_bp, short *r_handle, Base **r_base)
int ED_curve_nurb_select_count(const View3D *v3d, const Nurb *nu)
bool ED_curve_deselect_all(EditNurb *editnurb)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
bool ED_operator_editsurf(bContext *C)
void CURVE_OT_select_linked(wmOperatorType *ot)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
bool ED_curve_nurb_select_all(const Nurb *nu)
void CURVE_OT_select_nth(wmOperatorType *ot)
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)
void WM_operator_properties_checker_interval_from_op(wmOperator *op, CheckerIntervalParams *op_params)
void CURVE_OT_select_row(wmOperatorType *ot)
float RNA_float_get(PointerRNA *ptr, const char *name)
void CURVE_OT_select_less(wmOperatorType *ot)
View3D * CTX_wm_view3d(const bContext *C)
static wmOperatorStatus select_nth_exec(bContext *C, wmOperator *op)
void selectend_nurb(Object *obedit, enum eEndPoint_Types selfirst, bool doswap, bool selstatus)
#define fabsf
#define cosf
struct Object * object
eBezTriple_Flag f2
eBezTriple_Flag f3
eBezTriple_Flag f1
EditNurb * editnurb
ListBaseT< Nurb > nurbs
char name[258]
Definition DNA_ID.h:433
BezTriple * bezt
eNurbKnotFlag flagu
eHandleDisplay handle_display
View3DOverlay overlay
ViewLayer * view_layer
Definition ED_view3d.hh:80
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
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
struct ReportList * reports