Blender V5.3
editmesh_rip.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "DNA_mesh_types.h"
12#include "DNA_object_types.h"
13
14#include "BLI_array.hh"
15#include "BLI_math_geom_c.hh"
16#include "BLI_math_vector.hh"
17#include "BLI_math_vector_c.hh"
18#include "BLI_span.hh"
19#include "BLI_vector.hh"
20
21#include "BKE_context.hh"
22#include "BKE_editmesh.hh"
23#include "BKE_layer.hh"
24#include "BKE_report.hh"
25
26#include "BLT_translation.hh"
27
28#include "RNA_access.hh"
29#include "RNA_define.hh"
30
31#include "WM_types.hh"
32
33#include "ED_mesh.hh"
34#include "ED_transform.hh"
35#include "ED_view3d.hh"
36
37#include "bmesh.hh"
38#include "bmesh_tools.hh"
39
40#include "mesh_intern.hh" /* own include */
41
42namespace blender {
43
44/* -------------------------------------------------------------------- */
47
55#define INSET_DEFAULT 0.00001f
57 const float4x4 &mat,
58 const float co1[3],
59 const float co2[3],
60 const float mvalf[2],
61 const float inset)
62{
63
64 float2 vec1 = ED_view3d_project_float_v2_m4(region, co1, mat);
65 float2 vec2 = ED_view3d_project_float_v2_m4(region, co2, mat);
66
67 if (inset != 0.0f) {
68 const float dist_2d = len_v2v2(vec1, vec2);
69 if (dist_2d > FLT_EPSILON) {
70 const float dist = inset / dist_2d;
71 BLI_assert(isfinite(dist));
72 interp_v2_v2v2(vec1, vec1, vec2, dist);
73 interp_v2_v2v2(vec2, vec2, vec1, dist);
74 }
75 }
76
77 const float dist_sq = dist_squared_to_line_segment_v2(mvalf, vec1, vec2);
78 BLI_assert(isfinite(dist_sq));
79
80 return dist_sq;
81}
82
83#if 0
84static float edbm_rip_linedist(
85 ARegion *region, float mat[4][4], const float co1[3], const float co2[3], const float mvalf[2])
86{
87 float vec1[2], vec2[2];
88
89 ED_view3d_project_float_v2_m4(region, co1, vec1, mat);
90 ED_view3d_project_float_v2_m4(region, co2, vec2, mat);
91
92 return dist_to_line_v2(mvalf, vec1, vec2);
93}
94#endif
95
99static void edbm_calc_loop_co(BMLoop *l, float l_mid_co[3])
100{
101 BM_loop_calc_face_tangent(l, l_mid_co);
102
103 /* scale to average of surrounding edge size, only needs to be approx, but should
104 * be roughly equivalent to the check below which uses the middle of the edge. */
105 mul_v3_fl(l_mid_co, (BM_edge_calc_length(l->e) + BM_edge_calc_length(l->prev->e)) / 2.0f);
106
107 add_v3_v3(l_mid_co, l->v->co);
108}
109
111 BMEdge *e, BMLoop *e_l, ARegion *region, const float4x4 &projectMat, const float fmval[2])
112{
113 float vec[2];
114 float fmval_tweak[2];
115 float score;
116
117 BMVert *v1_other;
118 BMVert *v2_other;
119
121
122 /* method for calculating distance:
123 *
124 * for each edge: calculate face center, then made a vector
125 * from edge midpoint to face center. offset edge midpoint
126 * by a small amount along this vector. */
127
128 /* rather than the face center, get the middle of
129 * both edge verts connected to this one */
130 v1_other = BM_face_other_vert_loop(e_l->f, e->v2, e->v1)->v;
131 v2_other = BM_face_other_vert_loop(e_l->f, e->v1, e->v2)->v;
132
134 region, math::midpoint(float3(v1_other->co), float3(v2_other->co)), projectMat);
136 region, math::midpoint(float3(e->v1->co), float3(e->v2->co)), projectMat);
137
138 float2 e_v1_co = ED_view3d_project_float_v2_m4(region, e->v1->co, projectMat);
139 float2 e_v2_co = ED_view3d_project_float_v2_m4(region, e->v2->co, projectMat);
140
141 sub_v2_v2v2(vec, cent, mid);
142 normalize_v2_length(vec, 0.01f);
143
144 /* rather than adding to both verts, subtract from the mouse */
145 sub_v2_v2v2(fmval_tweak, fmval, vec);
146
147 score = len_v2v2(e_v1_co, e_v2_co);
148
149 if (dist_squared_to_line_segment_v2(fmval_tweak, e_v1_co, e_v2_co) >
150 dist_squared_to_line_segment_v2(fmval, e_v1_co, e_v2_co))
151 {
152 return score;
153 }
154 return -score;
155}
156
158
159/* -------------------------------------------------------------------- */
162
194
195#define IS_VISIT_POSSIBLE(e) (BM_edge_is_manifold(e) && BM_elem_flag_test(e, BM_ELEM_TAG))
196#define IS_VISIT_DONE(e) ((e)->l && (BM_elem_index_get((e)->l) != INVALID_UID))
197#define INVALID_UID INT_MIN
198
199/* mark, assign uid and step */
201{
202 BMIter iter;
203 BMEdge *e;
204 BM_ITER_ELEM (e, &iter, v, BM_EDGES_OF_VERT) {
206 BMLoop *l_a, *l_b;
207
208 BM_edge_loop_pair(e, &l_a, &l_b); /* no need to check, we know this will be true */
209
210 /* so (IS_VISIT_DONE == true) */
211 BM_elem_index_set(l_a, uid); /* set_dirty */
212 BM_elem_index_set(l_b, uid); /* set_dirty */
213
214 return e;
215 }
216 }
217 return nullptr;
218}
219
224
226{
227 BMIter fiter;
228 BMIter liter;
229
230 BMFace *f;
231 BMLoop *l;
232
233 int uid_start;
234 int uid_end;
235 int uid = bm->totedge; /* can start anywhere */
236
237 /* initialize loops with dummy invalid index values */
238 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
239 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
240 BM_elem_index_set(l, INVALID_UID); /* set_dirty */
241 }
242 }
244
245 Vector<EdgeLoopPair> eloop_pairs;
246
247 /* set contiguous loops ordered 'uid' values for walking after split */
248 while (true) {
249 int tot = 0;
250 BMIter eiter;
251 BMEdge *e_step;
252 BMVert *v_step;
253 BMEdge *e;
254 BMEdge *e_first;
255 BMEdge *e_last;
256
257 e_first = nullptr;
258 BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
260 e_first = e;
261 break;
262 }
263 }
264
265 if (e_first == nullptr) {
266 break;
267 }
268
269 /* Initialize. */
270 e_first = e;
271 v_step = e_first->v1;
272 e_step = nullptr; /* quiet warning, will never remain this value */
273
274 uid_start = uid;
275 while ((e = edbm_ripsel_edge_mark_step(v_step, uid))) {
276 v_step = BM_edge_other_vert((e_step = e), v_step);
277 uid++; /* only different line */
278 tot++;
279 }
280
281 /* this edges loops have the highest uid's, store this to walk down later */
282 e_last = e_step;
283
284 /* always store the highest 'uid' edge for the stride */
285 uid_end = uid - 1;
286 uid = uid_start - 1;
287
288 /* initialize */
289 v_step = e_first->v1;
290
291 while ((e = edbm_ripsel_edge_mark_step(v_step, uid))) {
292 v_step = BM_edge_other_vert((e_step = e), v_step);
293 uid--; /* only different line */
294 tot++;
295 }
296
297 /* stride far enough not to _ever_ overlap range */
298 uid_start = uid;
299 uid = uid_end + bm->totedge;
300
301 /* no need to check, we know this will be true */
302 EdgeLoopPair lp;
303 BM_edge_loop_pair(e_last, &lp.l_a, &lp.l_b);
304 eloop_pairs.append(lp);
305
306 BLI_assert(tot == uid_end - uid_start);
308
309#if 0
310 printf("%s: found contiguous edge loop of (%d)\n", __func__, uid_end - uid_start);
311#endif
312 }
313
314 return eloop_pairs;
315}
316
318
319/* -------------------------------------------------------------------- */
322
324{
325 BMIter eiter;
326 BMEdge *e;
327 BMVert *v = BM_edge_other_vert(e_orig, *v_prev);
328 const int uid_cmp = BM_elem_index_get(e_orig->l) - 1;
329
330 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
331 if (BM_elem_index_get(e->l) == uid_cmp) {
332 *v_prev = v;
333 return e;
334 }
335 }
336 return nullptr;
337}
338
340{
341 /* try step in a direction, if it fails we know do go the other way */
342 BMVert *v_test = e->v1;
343 return edbm_ripsel_edge_uid_step(e, &v_test) ? e->v1 : e->v2;
344}
345
347 const Span<EdgeLoopPair> eloop_pairs,
348 ARegion *region,
349 const float4x4 &projectMat,
350 const float fmval[2])
351{
352 for (const EdgeLoopPair &lp : eloop_pairs) {
353 BMEdge *e;
354 BMVert *v_prev;
355
356 float score_a = 0.0f;
357 float score_b = 0.0f;
358
359 e = lp.l_a->e;
361 for (; e; e = edbm_ripsel_edge_uid_step(e, &v_prev)) {
362 score_a += edbm_rip_edge_side_measure(e, e->l, region, projectMat, fmval);
363 }
364 e = lp.l_b->e;
366 for (; e; e = edbm_ripsel_edge_uid_step(e, &v_prev)) {
367 score_b += edbm_rip_edge_side_measure(e, e->l, region, projectMat, fmval);
368 }
369
370 e = (score_a > score_b) ? lp.l_a->e : lp.l_b->e;
372 for (; e; e = edbm_ripsel_edge_uid_step(e, &v_prev)) {
373 BM_edge_select_set(bm, e, false);
374 }
375 }
376}
377
379
380/* -------------------------------------------------------------------- */
383
393
396 char flag;
397};
398enum {
399 ULP_FLIP_0 = (1 << 0),
400 ULP_FLIP_1 = (1 << 1),
401};
402
404{
405 BMIter iter;
406 BMEdge *e;
407
408 uint total_tag = 0;
409 /* count tags, could be pre-calculated */
410 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
412 total_tag++;
413 }
414 }
415
416 Array<UnorderedLoopPair> uloop_pairs(total_tag);
417 UnorderedLoopPair *ulp = uloop_pairs.data();
418
419 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
421 BMLoop *l1, *l2;
422 if (BM_edge_loop_pair(e, &l1, &l2)) {
423 BMVert *v_cmp = l1->e->v1;
424 ulp->flag = (((l1->v != v_cmp) ? ULP_FLIP_0 : 0) | ((l2->v == v_cmp) ? ULP_FLIP_1 : 0));
425 }
426 else {
427 ulp->flag = 0;
428 }
429 ulp->l_pair[0] = l1;
430 ulp->l_pair[1] = l2;
431
432 ulp++;
433 }
434 }
435
436 return uloop_pairs;
437}
438
440 const Span<UnorderedLoopPair> uloop_pairs)
441{
442 for (const UnorderedLoopPair &ulp : uloop_pairs) {
443 if ((ulp.l_pair[0] && ulp.l_pair[1]) && (ulp.l_pair[0]->e != ulp.l_pair[1]->e)) {
444 /* time has come to make a face! */
445 BMVert *v_shared = BM_edge_share_vert(ulp.l_pair[0]->e, ulp.l_pair[1]->e);
446 BMFace *f, *f_example = ulp.l_pair[0]->f;
447 BMLoop *l_iter;
448 BMVert *f_verts[4];
449
450 if (v_shared == nullptr) {
451 /* quad */
452 f_verts[0] = ulp.l_pair[0]->e->v1;
453 f_verts[1] = ulp.l_pair[1]->e->v1;
454 f_verts[2] = ulp.l_pair[1]->e->v2;
455 f_verts[3] = ulp.l_pair[0]->e->v2;
456
457 if (ulp.flag & ULP_FLIP_0) {
458 std::swap(f_verts[0], f_verts[3]);
459 }
460 if (ulp.flag & ULP_FLIP_1) {
461 std::swap(f_verts[1], f_verts[2]);
462 }
463 }
464 else {
465 /* tri */
466 f_verts[0] = v_shared;
467 f_verts[1] = BM_edge_other_vert(ulp.l_pair[0]->e, v_shared);
468 f_verts[2] = BM_edge_other_vert(ulp.l_pair[1]->e, v_shared);
469 f_verts[3] = nullptr;
470
471 /* don't use the flip flags */
472 if (v_shared == ulp.l_pair[0]->v) {
473 std::swap(f_verts[0], f_verts[1]);
474 }
475 }
476
477 /* face should never exist */
478 BLI_assert(!BM_face_exists(f_verts, f_verts[3] ? 4 : 3));
479
480 f = BM_face_create_verts(bm, f_verts, f_verts[3] ? 4 : 3, f_example, BM_CREATE_NOP, true);
481
482 l_iter = BM_FACE_FIRST_LOOP(f);
483
484 if (f_verts[3]) {
485 BM_elem_attrs_copy(bm, BM_edge_other_loop(ulp.l_pair[0]->e, l_iter), l_iter);
486 l_iter = l_iter->next;
487 BM_elem_attrs_copy(bm, BM_edge_other_loop(ulp.l_pair[1]->e, l_iter), l_iter);
488 l_iter = l_iter->next;
489 BM_elem_attrs_copy(bm, BM_edge_other_loop(ulp.l_pair[1]->e, l_iter), l_iter);
490 l_iter = l_iter->next;
491 BM_elem_attrs_copy(bm, BM_edge_other_loop(ulp.l_pair[0]->e, l_iter), l_iter);
492 }
493 else {
494 BM_elem_attrs_copy(bm, BM_edge_other_loop(ulp.l_pair[0]->e, l_iter), l_iter);
495 l_iter = l_iter->next;
496 BM_elem_attrs_copy(bm, BM_edge_other_loop(ulp.l_pair[0]->e, l_iter), l_iter);
497 l_iter = l_iter->next;
498 BM_elem_attrs_copy(bm, BM_edge_other_loop(ulp.l_pair[1]->e, l_iter), l_iter);
499 }
500 }
501 }
502}
503
505
506/* -------------------------------------------------------------------- */
509
513static int edbm_rip_invoke__vert(bContext *C, const wmEvent *event, Object *obedit, bool do_fill)
514{
515 Array<UnorderedLoopPair> fill_uloop_pairs;
516 ARegion *region = CTX_wm_region(C);
519 BMIter iter, liter;
520 BMLoop *l;
521 BMEdge *e_best;
522 BMVert *v;
523 const int totvert_orig = bm->totvert;
524 int i;
525 float fmval[3] = {float(event->mval[0]), float(event->mval[1])};
526 float dist_sq = FLT_MAX;
527 float d;
528 bool is_wire, is_manifold_region;
529
530 BMEditSelection ese;
531 int totboundary_edge = 0;
532
533 const float4x4 projectMat = ED_view3d_ob_project_mat_get(rv3d, obedit);
534
535 /* find selected vert - same some time and check history first */
536 if (BM_select_history_active_get(bm, &ese) && ese.htype == BM_VERT) {
537 v = reinterpret_cast<BMVert *>(ese.ele);
538 }
539 else {
540 ese.ele = nullptr;
541
542 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
544 break;
545 }
546 }
547 }
548
549 /* (v == nullptr) should be impossible */
550 if ((v == nullptr) || (v->e == nullptr)) {
551 return OPERATOR_CANCELLED;
552 }
553
554 is_wire = BM_vert_is_wire(v);
555 is_manifold_region = BM_vert_is_manifold_region(v);
556
557 e_best = nullptr;
558
559 {
560 BMEdge *e;
561 /* find closest edge to mouse cursor */
562 BM_ITER_ELEM (e, &iter, v, BM_EDGES_OF_VERT) {
563 /* consider wire as boundary for this purpose,
564 * otherwise we can't a face away from a wire edge */
565 totboundary_edge += (BM_edge_is_boundary(e) || BM_edge_is_wire(e));
567 if ((is_manifold_region == false) || BM_edge_is_manifold(e)) {
569 region, projectMat, e->v1->co, e->v2->co, fmval, INSET_DEFAULT);
570 if ((e_best == nullptr) || (d < dist_sq)) {
571 dist_sq = d;
572 e_best = e;
573 }
574 }
575 }
576 }
577 }
578
579 if (e_best && e_best->l && (is_manifold_region == false)) {
580 /* Try to split off a non-manifold fan (when we have multiple disconnected fans) */
581 BMLoop *l_sep = e_best->l->v == v ? e_best->l : e_best->l->next;
582 BMVert *v_new;
583
584 BLI_assert(l_sep->v == v);
587
588 BM_vert_select_set(bm, v, false);
590
591 BM_vert_select_set(bm, v_new, true);
592 if (ese.ele) {
594 }
595
596 if (do_fill) {
597 BM_edge_create(bm, v, v_new, nullptr, BM_CREATE_NOP);
598 }
599
600 return OPERATOR_FINISHED;
601 }
602
603 /* if we are ripping a single vertex from 3 faces,
604 * then measure the distance to the face corner as well as the edge */
606 BMEdge *e_all[3];
607 BMLoop *l_all[3];
608 int i1, i2;
609
610 BM_iter_as_array(bm, BM_EDGES_OF_VERT, v, reinterpret_cast<void **>(e_all), 3);
611 BM_iter_as_array(bm, BM_LOOPS_OF_VERT, v, reinterpret_cast<void **>(l_all), 3);
612
613 /* not do a loop similar to the one above, but test against loops */
614 for (i1 = 0; i1 < 3; i1++) {
615 /* consider wire as boundary for this purpose,
616 * otherwise we can't a face away from a wire edge */
617 float l_mid_co[3];
618 l = l_all[i1];
619 edbm_calc_loop_co(l, l_mid_co);
620 d = edbm_rip_edgedist_squared(region, projectMat, l->v->co, l_mid_co, fmval, INSET_DEFAULT);
621 if ((e_best == nullptr) || (d < dist_sq)) {
622 dist_sq = d;
623
624 /* find the edge that is not in this loop */
625 e_best = nullptr;
626 for (i2 = 0; i2 < 3; i2++) {
627 if (!BM_edge_in_loop(e_all[i2], l)) {
628 e_best = e_all[i2];
629 break;
630 }
631 }
632 BLI_assert(e_best != nullptr);
633 }
634 }
635 }
636
637 /* should we go ahead with edge rip or do we need to do special case, split off vertex?:
638 * split off vertex if...
639 * - we can't find an edge - this means we are ripping a faces vert that is connected to other
640 * geometry only at the vertex.
641 * - the boundary edge total is greater than 2,
642 * in this case edge split _can_ work but we get far nicer results if we use this special case.
643 * - there are only 2 edges but we are a wire vert. */
644 if ((is_wire == false && totboundary_edge > 2) || (is_wire == true && totboundary_edge > 1)) {
645 BMVert **vout;
646 int vout_len;
647
648 BM_vert_select_set(bm, v, false);
649
650 bmesh_kernel_vert_separate(bm, v, &vout, &vout_len, true);
651
652 if (vout_len < 2) {
653 MEM_delete(vout);
654 /* set selection back to avoid active-unselected vertex */
655 BM_vert_select_set(bm, v, true);
656 /* should never happen */
657 return OPERATOR_CANCELLED;
658 }
659
660 int vi_best = 0;
661
662 if (ese.ele) {
664 }
665
666 dist_sq = FLT_MAX;
667
668 /* in the loop below we find the best vertex to drag based on its connected geometry,
669 * either by its face corner, or connected edge (when no faces are attached) */
670 for (i = 0; i < vout_len; i++) {
671
672 if (BM_vert_is_wire(vout[i]) == false) {
673 /* find the best face corner */
674 BM_ITER_ELEM (l, &iter, vout[i], BM_LOOPS_OF_VERT) {
676 float l_mid_co[3];
677
678 edbm_calc_loop_co(l, l_mid_co);
680 region, projectMat, v->co, l_mid_co, fmval, INSET_DEFAULT);
681
682 if (d < dist_sq) {
683 dist_sq = d;
684 vi_best = i;
685 }
686 }
687 }
688 }
689 else {
690 BMEdge *e;
691 /* a wire vert, find the best edge */
692 BM_ITER_ELEM (e, &iter, vout[i], BM_EDGES_OF_VERT) {
694 float e_mid_co[3];
695
696 mid_v3_v3v3(e_mid_co, e->v1->co, e->v2->co);
698 region, projectMat, v->co, e_mid_co, fmval, INSET_DEFAULT);
699
700 if (d < dist_sq) {
701 dist_sq = d;
702 vi_best = i;
703 }
704 }
705 }
706 }
707 }
708
709 /* vout[0] == best
710 * vout[1] == glue
711 * vout[2+] == splice with glue (when vout_len > 2)
712 */
713 if (vi_best != 0) {
714 std::swap(vout[0], vout[vi_best]);
715 vi_best = 0;
716 }
717
718 /* select the vert from the best region */
719 v = vout[vi_best];
720 BM_vert_select_set(bm, v, true);
721
722 if (ese.ele) {
724 }
725
726 /* splice all others back together */
727 if (vout_len > 2) {
728 for (i = 2; i < vout_len; i++) {
729 BM_vert_splice(bm, vout[1], vout[i]);
730 }
731 }
732
733 if (do_fill) {
734 /* match extrude vert-order */
735 BM_edge_create(bm, vout[1], vout[0], nullptr, BM_CREATE_NOP);
736 }
737
738 MEM_delete(vout);
739
740 return OPERATOR_FINISHED;
741 }
742
743 if (!e_best) {
744 return OPERATOR_CANCELLED;
745 }
746
747 /* *** Execute the split! *** */
748 /* unlike edge split, for single vertex split we only use the operator in one of the cases
749 * but both allocate fill */
750
751 {
752 BMVert *v_rip;
753 BMLoop *larr[2];
754 int larr_len = 0;
755
756 /* rip two adjacent edges */
758 /* Don't run the edge split operator in this case */
759
760 l = BM_edge_vert_share_loop(e_best->l, v);
761 larr[larr_len] = l;
762 larr_len++;
763
764 /* only tag for face-fill (we don't call the operator) */
765 if (BM_edge_is_boundary(e_best)) {
767 }
768 else {
771 }
772 }
773 else {
774 if (BM_edge_is_manifold(e_best)) {
775 BMLoop *l_iter, *l_first;
776 l_iter = l_first = e_best->l;
777 do {
778 larr[larr_len] = BM_edge_vert_share_loop(l_iter, v);
779
780 if (do_fill) {
781 /* Only needed when filling...
782 * Also, we never want to tag best edge,
783 * that one won't change during split. See #44618. */
784 if (larr[larr_len]->e == e_best) {
785 BM_elem_flag_enable(larr[larr_len]->prev->e, BM_ELEM_TAG);
786 }
787 else {
788 BM_elem_flag_enable(larr[larr_len]->e, BM_ELEM_TAG);
789 }
790 }
791 larr_len++;
792 } while ((l_iter = l_iter->radial_next) != l_first);
793 }
794 else {
795 /* NOTE(@ideasman42): It looks like there are no split edges,
796 * we could just return/report-error? */
797 }
798 }
799
800 /* keep directly before edgesplit */
801 if (do_fill) {
802 fill_uloop_pairs = edbm_tagged_loop_pairs_to_fill(bm);
803 }
804
805 if (larr_len) {
806 v_rip = BM_face_loop_separate_multi(bm, larr, larr_len);
807 }
808 else {
809 v_rip = nullptr;
810 }
811
812 if (v_rip) {
813 BM_vert_select_set(bm, v_rip, true);
814 }
815 else {
816 return OPERATOR_CANCELLED;
817 }
818 }
819
820 {
821 /* --- select which vert --- */
822 BMVert *v_best = nullptr;
823 float l_corner_co[3];
824
825 dist_sq = FLT_MAX;
826 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
828 /* disable by default, re-enable winner at end */
829 BM_vert_select_set(bm, v, false);
831
832 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
833
834 /* check if v_best is null in the _rare_ case there are numeric issues */
835 edbm_calc_loop_co(l, l_corner_co);
837 region, projectMat, l->v->co, l_corner_co, fmval, INSET_DEFAULT);
838 if ((v_best == nullptr) || (d < dist_sq)) {
839 v_best = v;
840 dist_sq = d;
841 }
842 }
843 }
844 }
845
846 if (v_best) {
847 BM_vert_select_set(bm, v_best, true);
848 if (ese.ele) {
850 }
851 }
852 }
853
854 if (do_fill && !fill_uloop_pairs.is_empty()) {
855 edbm_tagged_loop_pairs_do_fill_faces(bm, fill_uloop_pairs);
856 }
857
858 if (totvert_orig == bm->totvert) {
859 return OPERATOR_CANCELLED;
860 }
861
862 return OPERATOR_FINISHED;
863}
864
866
867/* -------------------------------------------------------------------- */
870
874static int edbm_rip_invoke__edge(bContext *C, const wmEvent *event, Object *obedit, bool do_fill)
875{
876 Array<UnorderedLoopPair> fill_uloop_pairs;
877 ARegion *region = CTX_wm_region(C);
880 BMIter iter, eiter;
881 BMLoop *l;
882 BMEdge *e_best;
883 BMVert *v;
884 const int totvert_orig = bm->totvert;
885 const int totedge_orig = bm->totedge;
886 float fmval[3] = {float(event->mval[0]), float(event->mval[1])};
887
888 const float4x4 projectMat = ED_view3d_ob_project_mat_get(rv3d, obedit);
889
890 /* important this runs on the original selection, before tampering with tagging */
892
893 /* expand edge selection */
894 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
895 BMEdge *e;
896 bool all_manifold;
897 int totedge_manifold; /* manifold, visible edges */
898 int i;
899
900 e_best = nullptr;
901 i = 0;
902 totedge_manifold = 0;
903 all_manifold = true;
904 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
905
907 /* important to check selection rather than tag here
908 * else we get feedback loop */
910 e_best = e;
911 i++;
912 /* Tag the edge verts so we know which verts to rip */
915 }
916 totedge_manifold++;
917 }
918
920 if ((all_manifold == true) && (BM_edge_is_manifold(e) == false)) {
921 all_manifold = false;
922 }
923 }
924
925 /* single edge, extend */
926 if (i == 1 && e_best->l) {
927 /* NOTE: if the case of 3 edges has one change in loop stepping,
928 * if this becomes more involved we may be better off splitting
929 * the 3 edge case into its own else-if branch */
930 if (ELEM(totedge_manifold, 4, 3) || (all_manifold == false)) {
931 BMLoop *l_a = e_best->l;
932 BMLoop *l_b = l_a->radial_next;
933
934 /* find the best face to follow, this way the edge won't point away from
935 * the mouse when there are more than 4 (takes the shortest face fan around) */
936 l = (edbm_rip_edge_side_measure(e_best, l_a, region, projectMat, fmval) <
937 edbm_rip_edge_side_measure(e_best, l_b, region, projectMat, fmval)) ?
938 l_a :
939 l_b;
940
942 /* Important edge is manifold else we can be attempting to split off
943 * a fan that don't budge, not crashing but adds duplicate edge. */
944 if (BM_edge_is_manifold(l->e)) {
945 l = l->radial_next;
946
947 if (totedge_manifold != 3) {
949 }
950
951 if (l) {
954 }
955 }
956 }
957 else {
958 e = BM_vert_other_disk_edge(v, e_best);
959
960 if (e) {
963 }
964 }
965 }
966 }
967
968 /* keep directly before edgesplit */
969 if (do_fill) {
970 fill_uloop_pairs = edbm_tagged_loop_pairs_to_fill(bm);
971 }
972
973 BM_mesh_edgesplit(bm, true, true, true);
974
975 /* NOTE: the output of the bmesh operator is ignored, since we built
976 * the contiguous loop pairs to split already, its possible that some
977 * edge did not split even though it was tagged which would not work
978 * as expected (but not crash), however there are checks to ensure
979 * tagged edges will split. So far its not been an issue. */
980 edbm_ripsel_deselect_helper(bm, eloop_pairs, region, projectMat, fmval);
981
982 /* deselect loose verts */
984
985 if (do_fill && !fill_uloop_pairs.is_empty()) {
986 edbm_tagged_loop_pairs_do_fill_faces(bm, fill_uloop_pairs);
987 }
988
989 if ((totvert_orig == bm->totvert) && (totedge_orig == bm->totedge)) {
990 return OPERATOR_CANCELLED;
991 }
992
994
995 return OPERATOR_FINISHED;
996}
997
999
1000/* -------------------------------------------------------------------- */
1003
1004/* based on mouse cursor position, it defines how is being ripped */
1006{
1007 const Main *bmain = CTX_data_main(C);
1008 const Scene *scene = CTX_data_scene(C);
1009 ViewLayer *view_layer = CTX_data_view_layer(C);
1011 *bmain, scene, view_layer, CTX_wm_view3d(C));
1012 const bool do_fill = RNA_boolean_get(op->ptr, "use_fill");
1013
1014 bool no_vertex_selected = true;
1015 bool error_face_selected = true;
1016 bool error_disconnected_vertices = true;
1017 bool error_rip_failed = true;
1018
1019 for (Object *obedit : objects) {
1021 BMIter iter;
1022 BMEdge *e;
1023 const bool singlesel = (bm->totvertsel == 1 && bm->totedgesel == 0 && bm->totfacesel == 0);
1024 int ret;
1025
1026 if (bm->totvertsel == 0) {
1027 continue;
1028 }
1029 no_vertex_selected = false;
1030
1031 /* running in face mode hardly makes sense, so convert to region loop and rip */
1032 if (bm->totfacesel) {
1033 /* highly nifty but hard to support since the operator can fail and we're left
1034 * with modified selection */
1035 // WM_operator_name_call(C, "MESH_OT_region_to_loop",
1036 // wm::OpCallContext::InvokeDefault, nullptr, event);
1037 continue;
1038 }
1039 error_face_selected = false;
1040
1041 /* we could support this, but not for now */
1042 if ((bm->totvertsel > 1) && (bm->totedgesel == 0)) {
1043 continue;
1044 }
1045 error_disconnected_vertices = false;
1046
1047 /* note on selection:
1048 * When calling edge split we operate on tagged edges rather than selected
1049 * this is important because the edges to operate on are extended by one,
1050 * but the selection is left alone.
1051 *
1052 * After calling edge split - the duplicated edges have the same selection state as the
1053 * original, so all we do is de-select the far side from the mouse and we have a
1054 * useful selection for grabbing.
1055 */
1056
1058
1059 /* BM_ELEM_SELECT --> BM_ELEM_TAG */
1060 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
1062 }
1063
1064 /* split 2 main parts of this operator out into vertex and edge ripping */
1065 if (singlesel) {
1066 ret = edbm_rip_invoke__vert(C, event, obedit, do_fill);
1067 }
1068 else {
1069 ret = edbm_rip_invoke__edge(C, event, obedit, do_fill);
1070 }
1071
1072 if (ret != OPERATOR_FINISHED) {
1073 continue;
1074 }
1075
1077
1078 BLI_assert(singlesel ? (bm->totvertsel > 0) : (bm->totedgesel > 0));
1079
1080 if (bm->totvertsel == 0) {
1081 continue;
1082 }
1083 error_rip_failed = false;
1084
1085 EDBMUpdate_Params params{};
1086 params.calc_looptris = true;
1087 params.calc_normals = true;
1088 params.is_destructive = true;
1089 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
1090 }
1091
1092 if (no_vertex_selected) {
1093 /* Ignore it. */
1094 return OPERATOR_CANCELLED;
1095 }
1096 if (error_face_selected) {
1097 BKE_report(op->reports, RPT_ERROR, "Cannot rip faces");
1098 return OPERATOR_CANCELLED;
1099 }
1100 if (error_disconnected_vertices) {
1101 BKE_report(op->reports, RPT_ERROR, "Cannot rip multiple disconnected vertices");
1102 return OPERATOR_CANCELLED;
1103 }
1104 if (error_rip_failed) {
1105 BKE_report(op->reports, RPT_ERROR, "Cannot rip non-manifold vertices or edges");
1106 return OPERATOR_CANCELLED;
1107 }
1108 /* No errors, everything went fine. */
1109 return OPERATOR_FINISHED;
1110}
1111
1113{
1114 PropertyRNA *prop;
1115
1116 /* identifiers */
1117 ot->name = "Rip";
1118 ot->idname = "MESH_OT_rip";
1119 ot->description = "Disconnect vertices or edges from connected geometry";
1120
1121 /* API callbacks. */
1122 ot->invoke = edbm_rip_invoke;
1123 ot->poll = EDBM_view3d_poll;
1124
1125 /* flags */
1127
1128 /* to give to transform */
1130 prop = RNA_def_boolean(ot->srna, "use_fill", false, "Fill", "Fill the ripped region");
1132}
1133
1135
1136} // namespace blender
#define BLT_I18NCONTEXT_ID_MESH
Object is a sort of wrapper for general info.
#define P_PROPORTIONAL
#define P_MIRROR_DUMMY
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BM_FACE_FIRST_LOOP(p)
#define BM_elem_index_get(ele)
#define BM_elem_flag_set(ele, hflag, val)
#define BM_elem_index_set(ele, index)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_select_history_store(bm, ele)
#define BM_select_history_remove(bm, ele)
#define BM_vert_edge_count_is_equal(v, n)
#define BM_vert_face_count_is_equal(v, n)
const T * data() const
Definition BLI_array.hh:323
bool is_empty() const
Definition BLI_array.hh:275
void append(const T &value)
#define INSET_DEFAULT
#define IS_VISIT_POSSIBLE(e)
#define INVALID_UID
#define IS_VISIT_DONE(e)
#define FLT_MAX
#define FLT_EPSILON
void properties_register(wmOperatorType *ot, int flags)
T midpoint(const T &a, const T &b)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BMFace * BM_face_create_verts(BMesh *bm, BMVert **vert_arr, const int len, const BMFace *f_example, const eBMCreateFlag create_flag, const bool create_edges)
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BMVert * BM_face_loop_separate_multi(BMesh *bm, BMLoop **larr, int larr_len)
static BMVert * edbm_ripsel_edloop_pair_start_vert(BMEdge *e)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
unsigned int uint
Definition BKE_text.h:55
ATTR_WARN_UNUSED_RESULT const BMLoop * l
bool EDBM_view3d_poll(bContext *C)
bool BM_vert_is_wire(const BMVert *v)
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
BMVert * BM_edge_share_vert(BMEdge *e1, BMEdge *e2)
void EDBM_update(Mesh *mesh, const EDBMUpdate_Params *params)
MINLINE float normalize_v2_length(float n[2], float unit_length)
BMLoop * BM_edge_vert_share_loop(BMLoop *l, BMVert *v)
Return the Loop Shared by Edge and Vert.
void BM_custom_loop_normals_from_vector_layer(BMesh *bm, bool add_sharp_edges)
float2 ED_view3d_project_float_v2_m4(const ARegion *region, const float co[3], const float4x4 &mat)
static void edbm_ripsel_deselect_helper(BMesh *bm, const Span< EdgeLoopPair > eloop_pairs, ARegion *region, const float4x4 &projectMat, const float fmval[2])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
BMLoop * BM_face_other_vert_loop(BMFace *f, BMVert *v_prev, BMVert *v)
Other Loop in Face Sharing a Vertex.
ATTR_WARN_UNUSED_RESULT const BMVert * v
MatBase< float, 4, 4 > float4x4
BLI_INLINE bool BM_edge_is_manifold(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static void edbm_calc_loop_co(BMLoop *l, float l_mid_co[3])
float BM_edge_calc_length(const BMEdge *e)
BLI_INLINE bool BM_vert_in_edge(const BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ViewLayer * CTX_data_view_layer(const bContext *C)
void BM_loop_calc_face_tangent(const BMLoop *l, float r_tangent[3])
BM_loop_calc_face_tangent.
MINLINE void mul_v3_fl(float r[3], float f)
BLI_INLINE bool BM_edge_in_loop(const BMEdge *e, const BMLoop *l) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:307
void BM_mesh_select_mode_clean_ex(BMesh *bm, const short selectmode)
Select Mode Clean.
VecBase< float, 2 > float2
@ BM_CREATE_NOP
Definition bmesh_core.hh:34
static float edbm_rip_edgedist_squared(ARegion *region, const float4x4 &mat, const float co1[3], const float co2[3], const float mvalf[2], const float inset)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
float dist_to_line_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:302
void bmesh_kernel_vert_separate(BMesh *bm, BMVert *v, BMVert ***r_vout, int *r_vout_len, const bool copy_select)
Separate Vert.
static Array< UnorderedLoopPair > edbm_tagged_loop_pairs_to_fill(BMesh *bm)
ATTR_WARN_UNUSED_RESULT const BMLoop * l_b
BMLoop * BM_vert_find_first_loop(BMVert *v)
bool BM_vert_splice(BMesh *bm, BMVert *v_dst, BMVert *v_src)
Splice Vert.
BLI_INLINE bool BM_edge_is_boundary(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static int edbm_rip_invoke__edge(bContext *C, const wmEvent *event, Object *obedit, bool do_fill)
RegionView3D * CTX_wm_region_view3d(const bContext *C)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
BMLoop * BM_edge_other_loop(BMEdge *e, BMLoop *l)
void BM_vert_select_set(BMesh *bm, BMVert *v, const bool select)
Select Vert.
void BM_select_history_validate(BMesh *bm)
BMFace * BM_face_exists(BMVert *const *varr, int len)
static void edbm_tagged_loop_pairs_do_fill_faces(BMesh *bm, const Span< UnorderedLoopPair > uloop_pairs)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
MINLINE void add_v3_v3(float r[3], const float a[3])
bool BM_edge_loop_pair(BMEdge *e, BMLoop **r_la, BMLoop **r_lb)
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
static BMEdge * edbm_ripsel_edge_mark_step(BMVert *v, const int uid)
Main * CTX_data_main(const bContext *C)
static float edbm_rip_edge_side_measure(BMEdge *e, BMLoop *e_l, ARegion *region, const float4x4 &projectMat, const float fmval[2])
void interp_v2_v2v2(float r[2], const float a[2], const float b[2], float t)
bool BM_vert_is_manifold_region(const BMVert *v)
BMEdge * BM_vert_other_disk_edge(BMVert *v, BMEdge *e_first)
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
static BMEdge * edbm_ripsel_edge_uid_step(BMEdge *e_orig, BMVert **v_prev)
void BM_mesh_edgesplit(BMesh *bm, const bool use_verts, const bool tag_only, const bool copy_select)
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)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
static int edbm_rip_invoke__vert(bContext *C, const wmEvent *event, Object *obedit, bool do_fill)
void BM_elem_attrs_copy(BMesh *bm, const BMCustomDataCopyMap &map, const BMVert *src, BMVert *dst)
VecBase< float, 3 > float3
BMVert * BM_face_loop_separate_multi_isolated(BMesh *bm, BMLoop *l_sep)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
bool BM_select_history_active_get(BMesh *bm, BMEditSelection *ese)
void MESH_OT_rip(wmOperatorType *ot)
bool BM_custom_loop_normals_to_vector_layer(BMesh *bm)
ARegion * CTX_wm_region(const bContext *C)
BMEdge * BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *e_example, const eBMCreateFlag create_flag)
Main function for creating a new edge.
int BM_iter_as_array(BMesh *bm, const char itype, void *data, void **array, const int len)
Iterator as Array.
static wmOperatorStatus edbm_rip_invoke(bContext *C, wmOperator *op, const wmEvent *event)
View3D * CTX_wm_view3d(const bContext *C)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
static Vector< EdgeLoopPair > edbm_ripsel_looptag_helper(BMesh *bm)
BMLoop * BM_loop_other_edge_loop(BMLoop *l, BMVert *v)
void BM_edge_select_set(BMesh *bm, BMEdge *e, const bool select)
Select Edge.
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * l
struct BMLoop * next
struct BMEdge * e
struct BMVert * v
struct BMLoop * radial_next
struct BMFace * f
struct BMEdge * e
struct BMLoop * radial_next
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