Blender V4.5
uvedit_rip.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10#include <cstdlib>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_ghash.h"
16#include "BLI_linklist_stack.h"
17#include "BLI_math_vector.h"
18#include "BLI_utildefines.h"
19
20#include "DNA_mesh_types.h"
21#include "DNA_object_types.h"
22#include "DNA_scene_types.h"
23#include "DNA_space_types.h"
24
25#include "BKE_context.hh"
26#include "BKE_customdata.hh"
27#include "BKE_editmesh.hh"
28#include "BKE_layer.hh"
29#include "BKE_mesh_types.hh"
30#include "BKE_report.hh"
31
32#include "DEG_depsgraph.hh"
33
34#include "ED_screen.hh"
35#include "ED_transform.hh"
36#include "ED_uvedit.hh"
37
38#include "RNA_access.hh"
39#include "RNA_define.hh"
40
41#include "WM_api.hh"
42#include "WM_types.hh"
43
44#include "UI_view2d.hh"
45
46#include "uvedit_intern.hh"
47
48using blender::Vector;
49
50/* -------------------------------------------------------------------- */
53
86
88BLI_STATIC_ASSERT(sizeof(ULData) <= sizeof(int), "");
89
91{
92 return (ULData *)&l->head.index;
93}
94
96
97/* -------------------------------------------------------------------- */
100
102 const int cd_loop_uv_offset)
103{
104 BMLoop *l_other = nullptr;
105 BMLoop *l_iter = l_src->radial_next;
106 if (l_iter != l_src) {
107 do {
108 if (BM_elem_flag_test(l_iter->f, BM_ELEM_TAG) && UL(l_iter)->is_select_edge &&
109 BM_loop_uv_share_edge_check(l_src, l_iter, cd_loop_uv_offset))
110 {
111 /* Check UVs are contiguous. */
112 if (l_other == nullptr) {
113 l_other = l_iter;
114 }
115 else {
116 /* Only use when there is a single alternative. */
117 l_other = nullptr;
118 break;
119 }
120 }
121 } while ((l_iter = l_iter->radial_next) != l_src);
122 }
123 return l_other;
124}
125
127 BMVert *v_src,
128 const int cd_loop_uv_offset)
129{
130 BLI_assert(BM_vert_in_edge(l_src->e, v_src));
131 BMLoop *l_other = nullptr;
132 BMLoop *l_iter = l_src->radial_next;
133 if (l_iter != l_src) {
134 do {
135 if (BM_elem_flag_test(l_iter->f, BM_ELEM_TAG) &&
136 BM_loop_uv_share_edge_check(l_src, l_iter, cd_loop_uv_offset))
137 {
138 /* Check UVs are contiguous. */
139 if (l_other == nullptr) {
140 l_other = l_iter;
141 }
142 else {
143 /* Only use when there is a single alternative. */
144 l_other = nullptr;
145 break;
146 }
147 }
148 } while ((l_iter = l_iter->radial_next) != l_src);
149 }
150 if (l_other != nullptr) {
151 if (l_other->v == v_src) {
152 /* do nothing. */
153 }
154 else if (l_other->next->v == v_src) {
155 l_other = l_other->next;
156 }
157 else if (l_other->prev->v == v_src) {
158 l_other = l_other->prev;
159 }
160 else {
162 }
163 }
164 return l_other;
165}
166
170static BMLoop *bm_vert_step_fan_loop_uv(BMLoop *l, BMEdge **e_step, const int cd_loop_uv_offset)
171{
172 BMEdge *e_prev = *e_step;
173 BMLoop *l_next;
174 if (l->e == e_prev) {
175 l_next = l->prev;
176 }
177 else if (l->prev->e == e_prev) {
178 l_next = l;
179 }
180 else {
182 return nullptr;
183 }
184
185 *e_step = l_next->e;
186
187 return bm_loop_find_other_fan_loop_with_visible_face(l_next, l->v, cd_loop_uv_offset);
188}
189
190static void bm_loop_uv_select_single_vert_validate(BMLoop *l_init, const int cd_loop_uv_offset)
191{
192 const float *luv_init = BM_ELEM_CD_GET_FLOAT_P(l_init, cd_loop_uv_offset);
193 BMIter liter;
194 BMLoop *l;
195 bool is_single_vert = true;
196 BM_ITER_ELEM (l, &liter, l_init->v, BM_LOOPS_OF_VERT) {
197 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
198 if (equals_v2v2(luv_init, luv)) {
199 if (UL(l->prev)->is_select_edge || UL(l)->is_select_edge) {
200 is_single_vert = false;
201 break;
202 }
203 }
204 }
205 if (is_single_vert == false) {
206 BM_ITER_ELEM (l, &liter, l_init->v, BM_LOOPS_OF_VERT) {
207 if (UL(l)->is_select_vert_single) {
208 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
209 if (equals_v2v2(luv_init, luv)) {
210 UL(l)->is_select_vert_single = false;
211 }
212 }
213 }
214 }
215}
216
224 const float dir[2],
225 const float aspect_y,
226 const int cd_loop_uv_offset,
227 float *r_corner_angle,
228 float *r_edge_angle,
229 int *r_edge_index)
230{
231 /* Calculate 3 directions, return the shortest angle. */
232 blender::float2 dir_test[3];
233 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
234 const float *luv_prev = BM_ELEM_CD_GET_FLOAT_P(l->prev, cd_loop_uv_offset);
235 const float *luv_next = BM_ELEM_CD_GET_FLOAT_P(l->next, cd_loop_uv_offset);
236
237 sub_v2_v2v2(dir_test[0], luv, luv_prev);
238 sub_v2_v2v2(dir_test[2], luv, luv_next);
239 dir_test[0][1] /= aspect_y;
240 dir_test[2][1] /= aspect_y;
241
242 normalize_v2(dir_test[0]);
243 normalize_v2(dir_test[2]);
244
245 /* Calculate the orthogonal line (same as negating one, then adding). */
246 sub_v2_v2v2(dir_test[1], dir_test[0], dir_test[2]);
247 normalize_v2(dir_test[1]);
248
249 /* Rotate 90 degrees. */
250 std::swap(dir_test[1][0], dir_test[1][1]);
251 dir_test[1][1] *= -1.0f;
252
253 if (BM_face_uv_calc_cross(l->f, cd_loop_uv_offset) > 0.0f) {
254 dir_test[1] *= -1.0f;
255 }
256
257 const float angles[3] = {
258 angle_v2v2(dir, dir_test[0]),
259 angle_v2v2(dir, dir_test[1]),
260 angle_v2v2(dir, dir_test[2]),
261 };
262
263 /* Set the corner values. */
264 *r_corner_angle = angles[1];
265
266 /* Set the edge values. */
267 if (angles[0] < angles[2]) {
268 *r_edge_angle = angles[0];
269 *r_edge_index = -1;
270 }
271 else {
272 *r_edge_angle = angles[2];
273 *r_edge_index = 1;
274 }
275}
276
278
279/* -------------------------------------------------------------------- */
282
286};
287
303 const float co[2],
304 const float aspect_y,
305 const int cd_loop_uv_offset)
306{
307 UVRipSingle *rip = MEM_callocN<UVRipSingle>(__func__);
308 const float *co_center = BM_ELEM_CD_GET_FLOAT_P(l_init_orig, cd_loop_uv_offset);
309 rip->loops = BLI_gset_ptr_new(__func__);
310
311 /* Track the closest loop, start walking from this so in the event we have multiple
312 * disconnected fans, we can rip away loops connected to this one. */
313 BMLoop *l_init = nullptr;
314 BMLoop *l_init_edge = nullptr;
315 float corner_angle_best = FLT_MAX;
316 float edge_angle_best = FLT_MAX;
317 int edge_index_best = 0; /* -1 or +1 (never center). */
318
319 /* Calculate the direction from the cursor with aspect correction. */
320 float dir_co[2];
321 sub_v2_v2v2(dir_co, co_center, co);
322 dir_co[1] /= aspect_y;
323 if (UNLIKELY(normalize_v2(dir_co) == 0.0)) {
324 dir_co[1] = 1.0f;
325 }
326
327 int uv_fan_count_all = 0;
328 {
329 BMIter liter;
330 BMLoop *l;
331 BM_ITER_ELEM (l, &liter, l_init_orig->v, BM_LOOPS_OF_VERT) {
333 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
334 if (equals_v2v2(co_center, luv)) {
335 uv_fan_count_all += 1;
336 /* Clear at the same time. */
337 UL(l)->is_select_vert_single = true;
338 UL(l)->side = 0;
339 BLI_gset_add(rip->loops, l);
340
341 /* Update `l_init_close` */
342 float corner_angle_test;
343 float edge_angle_test;
344 int edge_index_test;
346 dir_co,
347 aspect_y,
348 cd_loop_uv_offset,
349 &corner_angle_test,
350 &edge_angle_test,
351 &edge_index_test);
352 if ((corner_angle_best == FLT_MAX) || (corner_angle_test < corner_angle_best)) {
353 corner_angle_best = corner_angle_test;
354 l_init = l;
355 }
356
357 /* Trick so we don't consider concave corners further away than they should be. */
358 edge_angle_test = min_ff(corner_angle_test, edge_angle_test);
359
360 if ((edge_angle_best == FLT_MAX) || (edge_angle_test < edge_angle_best)) {
361 edge_angle_best = edge_angle_test;
362 edge_index_best = edge_index_test;
363 l_init_edge = l;
364 }
365 }
366 }
367 }
368 }
369
370 /* Walk around the `l_init` in both directions of the UV fan. */
371 int uv_fan_count_contiguous = 1;
372 UL(l_init)->side = 1;
373 for (int i = 0; i < 2; i += 1) {
374 BMEdge *e_prev = i ? l_init->e : l_init->prev->e;
375 BMLoop *l_iter = l_init;
376 while (((l_iter = bm_vert_step_fan_loop_uv(l_iter, &e_prev, cd_loop_uv_offset)) != l_init) &&
377 (l_iter != nullptr) && (UL(l_iter)->side == 0))
378 {
379 uv_fan_count_contiguous += 1;
380 /* Keep. */
381 UL(l_iter)->side = 1;
382 }
383 /* May be useful to know if the fan is closed, currently it's not needed. */
384#if 0
385 if (l_iter == l_init) {
386 is_closed = true;
387 }
388#endif
389 }
390
391 if (uv_fan_count_contiguous != uv_fan_count_all) {
392 /* Simply rip off the current fan, all tagging is done. */
393 }
394 else {
395 GSetIterator gs_iter;
396 GSET_ITER (gs_iter, rip->loops) {
397 BMLoop *l = static_cast<BMLoop *>(BLI_gsetIterator_getKey(&gs_iter));
398 UL(l)->side = 0;
399 }
400
401 if (uv_fan_count_contiguous <= 2) {
402 /* Simple case, rip away the closest loop. */
403 UL(l_init)->side = 1;
404 }
405 else {
406 /* Rip away from the closest edge. */
407 BMLoop *l_radial_init = (edge_index_best == -1) ? l_init_edge->prev : l_init_edge;
408 BMLoop *l_radial_iter = l_radial_init;
409 do {
410 if (BM_loop_uv_share_edge_check(l_radial_init, l_radial_iter, cd_loop_uv_offset)) {
411 BMLoop *l = (l_radial_iter->v == l_init->v) ? l_radial_iter : l_radial_iter->next;
412 BLI_assert(l->v == l_init->v);
413 /* Keep. */
414 UL(l)->side = 1;
415 }
416 } while ((l_radial_iter = l_radial_iter->radial_next) != l_radial_init);
417 }
418 }
419
420 return rip;
421}
422
424{
425 BLI_gset_free(rip->loops, nullptr);
426 MEM_freeN(rip);
427}
428
430
431/* -------------------------------------------------------------------- */
434
438};
439
441{
442 ULData *ul = UL(l);
444 BLI_assert(ul->in_rip_pairs == false);
445 ul->in_rip_pairs = true;
446 BLI_gset_add(rip->loops, l);
447}
448
450{
451 ULData *ul = UL(l);
453 BLI_assert(ul->in_rip_pairs == true);
454 ul->in_rip_pairs = false;
455 BLI_gset_remove(rip->loops, l, nullptr);
456}
457
463 uint side,
464 const float aspect_y,
465 const int cd_loop_uv_offset)
466{
467 BMIter liter;
468 const float *luv_init = BM_ELEM_CD_GET_FLOAT_P(l_init, cd_loop_uv_offset);
469 float angle_of_side = 0.0f;
470 BMLoop *l;
471 BM_ITER_ELEM (l, &liter, l_init->v, BM_LOOPS_OF_VERT) {
472 if (UL(l)->in_rip_pairs) {
473 if (UL(l)->side == side) {
474 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
475 if (equals_v2v2(luv_init, luv)) {
476 const float *luv_prev = BM_ELEM_CD_GET_FLOAT_P(l->prev, cd_loop_uv_offset);
477 const float *luv_next = BM_ELEM_CD_GET_FLOAT_P(l->next, cd_loop_uv_offset);
478 float dir_prev[2], dir_next[2];
479 sub_v2_v2v2(dir_prev, luv_prev, luv);
480 sub_v2_v2v2(dir_next, luv_next, luv);
481 dir_prev[1] /= aspect_y;
482 dir_next[1] /= aspect_y;
483 const float luv_angle = angle_v2v2(dir_prev, dir_next);
484 if (LIKELY(isfinite(luv_angle))) {
485 angle_of_side += luv_angle;
486 }
487 }
488 }
489 }
490 }
491 return angle_of_side;
492}
493
494static int uv_rip_pairs_loop_count_on_side(BMLoop *l_init, uint side, const int cd_loop_uv_offset)
495{
496 const float *luv_init = BM_ELEM_CD_GET_FLOAT_P(l_init, cd_loop_uv_offset);
497 int count = 0;
498 BMIter liter;
499 BMLoop *l;
500 BM_ITER_ELEM (l, &liter, l_init->v, BM_LOOPS_OF_VERT) {
501 if (UL(l)->in_rip_pairs) {
502 if (UL(l)->side == side) {
503 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
504 if (equals_v2v2(luv_init, luv)) {
505 count += 1;
506 }
507 }
508 }
509 }
510 return count;
511}
512
514 BMLoop *l_target,
515 const float aspect_y,
516 const int cd_loop_uv_offset)
517{
518 const int side_a = UL(l_switch)->side;
519 const int side_b = UL(l_target)->side;
520
521 BLI_assert(UL(l_switch)->side != UL(l_target)->side);
522
523 /* First, check if this is a simple grid topology,
524 * in that case always choose the adjacent edge. */
525 const int count_a = uv_rip_pairs_loop_count_on_side(l_switch, side_a, cd_loop_uv_offset);
526 const int count_b = uv_rip_pairs_loop_count_on_side(l_target, side_b, cd_loop_uv_offset);
527 if (count_a + count_b == 4) {
528 return count_a > count_b;
529 }
530
531 const float angle_a_before = uv_rip_pairs_calc_uv_angle(
532 l_switch, side_a, aspect_y, cd_loop_uv_offset);
533 const float angle_b_before = uv_rip_pairs_calc_uv_angle(
534 l_target, side_b, aspect_y, cd_loop_uv_offset);
535
536 UL(l_switch)->side = side_b;
537
538 const float angle_a_after = uv_rip_pairs_calc_uv_angle(
539 l_switch, side_a, aspect_y, cd_loop_uv_offset);
540 const float angle_b_after = uv_rip_pairs_calc_uv_angle(
541 l_target, side_b, aspect_y, cd_loop_uv_offset);
542
543 UL(l_switch)->side = side_a;
544
545 return fabsf(angle_a_before - angle_b_before) > fabsf(angle_a_after - angle_b_after);
546}
547
557 const float aspect_y,
558 const int cd_loop_uv_offset)
559{
560 UVRipPairs *rip = MEM_callocN<UVRipPairs>(__func__);
561 rip->loops = BLI_gset_ptr_new(__func__);
562
563 /* We can rely on this stack being small, as we're walking down two sides of an edge loop,
564 * so the stack won't be much larger than the total number of fans at any one vertex. */
566
567 /* Needed for cases when we walk onto loops which already have a side assigned,
568 * in this case we need to pick a better side (see #uv_rip_pairs_loop_change_sides_test)
569 * and put the loop back in the stack,
570 * which is needed in the case adjacent loops should also switch sides. */
571#define UV_SET_SIDE_AND_REMOVE_FROM_RAIL(loop, side_value) \
572 { \
573 BLI_assert(UL(loop)->side_was_swapped == false); \
574 BLI_assert(UL(loop)->side != side_value); \
575 if (!UL(loop)->in_stack) { \
576 BLI_SMALLSTACK_PUSH(stack, loop); \
577 UL(loop)->in_stack = true; \
578 } \
579 if (UL(loop)->in_rip_pairs) { \
580 uv_rip_pairs_remove(rip, loop); \
581 } \
582 UL(loop)->side = side_value; \
583 UL(loop)->side_was_swapped = true; \
584 }
585
586 /* Initialize the stack. */
587 BLI_SMALLSTACK_PUSH(stack, l_init);
588 UL(l_init)->in_stack = true;
589
590 BMLoop *l_step;
591 while ((l_step = static_cast<BMLoop *>(BLI_SMALLSTACK_POP(stack)))) {
592 int side = UL(l_step)->side;
593 UL(l_step)->in_stack = false;
594
595 /* Note that we could add all loops into the rip-pairs when adding into the stack,
596 * however this complicates removal, so add into the rip-pairs when popping from the stack. */
597 uv_rip_pairs_add(rip, l_step);
598
599 /* Add to the other side if it exists. */
600 if (UL(l_step)->is_select_edge) {
602 cd_loop_uv_offset);
603 if (l_other != nullptr) {
604 if (!UL(l_other)->in_rip_pairs && !UL(l_other)->in_stack) {
605 BLI_SMALLSTACK_PUSH(stack, l_other);
606 UL(l_other)->in_stack = true;
607 UL(l_other)->side = !side;
608 }
609 else {
610 if (UL(l_other)->side == side) {
611 if (UL(l_other)->side_was_swapped == false) {
612 UV_SET_SIDE_AND_REMOVE_FROM_RAIL(l_other, !side);
613 }
614 }
615 }
616 }
617
618 /* Add the next loop along the edge on the same side. */
619 l_other = l_step->next;
620 if (!UL(l_other)->in_rip_pairs && !UL(l_other)->in_stack) {
621 BLI_SMALLSTACK_PUSH(stack, l_other);
622 UL(l_other)->in_stack = true;
623 UL(l_other)->side = side;
624 }
625 else {
626 if (UL(l_other)->side != side) {
627 if ((UL(l_other)->side_was_swapped == false) &&
628 uv_rip_pairs_loop_change_sides_test(l_other, l_step, aspect_y, cd_loop_uv_offset))
629 {
631 }
632 }
633 }
634 }
635
636 /* Walk over the fan of loops, starting from `l_step` in both directions. */
637 for (int i = 0; i < 2; i++) {
638 BMLoop *l_radial_first = i ? l_step : l_step->prev;
639 if (l_radial_first != l_radial_first->radial_next) {
640 BMEdge *e_radial = l_radial_first->e;
641 BMLoop *l_radial_iter = l_radial_first->radial_next;
642 do {
643 /* Not a boundary and visible. */
644 if (!UL(l_radial_iter)->is_select_edge &&
645 BM_elem_flag_test(l_radial_iter->f, BM_ELEM_TAG))
646 {
647 BMLoop *l_other = (l_radial_iter->v == l_step->v) ? l_radial_iter :
648 l_radial_iter->next;
649 BLI_assert(l_other->v == l_step->v);
650 if (BM_edge_uv_share_vert_check(e_radial, l_other, l_step, cd_loop_uv_offset)) {
651 if (!UL(l_other)->in_rip_pairs && !UL(l_other)->in_stack) {
652 BLI_SMALLSTACK_PUSH(stack, l_other);
653 UL(l_other)->in_stack = true;
654 UL(l_other)->side = side;
655 }
656 else {
657 if (UL(l_other)->side != side) {
658 if ((UL(l_other)->side_was_swapped == false) &&
660 l_other, l_step, aspect_y, cd_loop_uv_offset))
661 {
663 }
664 }
665 }
666 }
667 }
668 } while ((l_radial_iter = l_radial_iter->radial_next) != l_radial_first);
669 }
670 }
671 }
672
673#undef UV_SET_SIDE_AND_REMOVE_FROM_RAIL
674
675 return rip;
676}
677
679{
680 BLI_gset_free(rip->loops, nullptr);
681 MEM_freeN(rip);
682}
683
688 const int cd_loop_uv_offset,
689 float r_center[2],
690 float r_dir_side[2][2])
691{
692 zero_v2(r_center);
693 int center_total = 0;
694 int side_total[2] = {0, 0};
695
696 for (int i = 0; i < 2; i++) {
697 zero_v2(r_dir_side[i]);
698 }
699 GSetIterator gs_iter;
700 GSET_ITER (gs_iter, rip->loops) {
701 BMLoop *l = static_cast<BMLoop *>(BLI_gsetIterator_getKey(&gs_iter));
702 int side = UL(l)->side;
703 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
704 add_v2_v2(r_center, luv);
705
706 float dir[2];
707 if (!UL(l)->is_select_edge) {
708 const float *luv_next = BM_ELEM_CD_GET_FLOAT_P(l->next, cd_loop_uv_offset);
709 sub_v2_v2v2(dir, luv_next, luv);
710 add_v2_v2(r_dir_side[side], dir);
711 }
712 if (!UL(l->prev)->is_select_edge) {
713 const float *luv_prev = BM_ELEM_CD_GET_FLOAT_P(l->prev, cd_loop_uv_offset);
714 sub_v2_v2v2(dir, luv_prev, luv);
715 add_v2_v2(r_dir_side[side], dir);
716 }
717 side_total[side] += 1;
718 }
719 center_total += BLI_gset_len(rip->loops);
720
721 for (int i = 0; i < 2; i++) {
722 normalize_v2(r_dir_side[i]);
723 }
724 mul_v2_fl(r_center, 1.0f / center_total);
725
726 /* If only a single side is selected, don't handle this rip-pairs. */
727 return side_total[0] && side_total[1];
728}
729
731
732/* -------------------------------------------------------------------- */
735
739static bool uv_rip_object(Scene *scene, Object *obedit, const float co[2], const float aspect_y)
740{
741 Mesh *mesh = (Mesh *)obedit->data;
742 BMEditMesh *em = mesh->runtime->edit_mesh.get();
743 BMesh *bm = em->bm;
744 const char *active_uv_name = CustomData_get_active_layer_name(&bm->ldata, CD_PROP_FLOAT2);
745 BM_uv_map_attr_vert_select_ensure(bm, active_uv_name);
746 BM_uv_map_attr_edge_select_ensure(bm, active_uv_name);
747 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
748
749 BMFace *efa;
750 BMIter iter, liter;
751 BMLoop *l;
752
753 const ULData ul_clear = {0};
754
755 bool changed = false;
756
757 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
759 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
760 ULData *ul = UL(l);
761 *ul = ul_clear;
762 }
763 }
764 bm->elem_index_dirty |= BM_LOOP;
765
766 bool is_select_all_any = false;
767 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
768 if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
769 bool is_all = true;
770 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
771 if (BM_ELEM_CD_GET_BOOL(l, offsets.select_vert)) {
772 if (BM_ELEM_CD_GET_BOOL(l, offsets.select_edge)) {
773 UL(l)->is_select_edge = true;
774 }
775 else if (!BM_ELEM_CD_GET_BOOL(l->prev, offsets.select_edge)) {
776 /* #bm_loop_uv_select_single_vert_validate validates below. */
777 UL(l)->is_select_vert_single = true;
778 is_all = false;
779 }
780 else {
781 /* Cases where all vertices of a face are selected but not all edges are selected. */
782 is_all = false;
783 }
784 }
785 else {
786 is_all = false;
787 }
788 }
789 if (is_all) {
790 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
791 UL(l)->is_select_all = true;
792 }
793 is_select_all_any = true;
794 }
795 }
796 }
797
798 /* Remove #ULData.is_select_vert_single when connected to selected edges. */
799 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
800 if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
801 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
802 if (UL(l)->is_select_vert_single) {
804 }
805 }
806 }
807 }
808
809 /* Special case: if we have selected faces, isolate them.
810 * This isn't a rip, however it's useful for users as a quick way
811 * to detach the selection.
812 *
813 * We could also extract an edge loop from the boundary
814 * however in practice it's not that useful, see #78751. */
815 if (is_select_all_any) {
816 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
817 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
818 if (!UL(l)->is_select_all) {
819 if (BM_ELEM_CD_GET_BOOL(l, offsets.select_vert)) {
820 BM_ELEM_CD_SET_BOOL(l, offsets.select_vert, false);
821 changed = true;
822 }
823 if (BM_ELEM_CD_GET_BOOL(l, offsets.select_edge)) {
824 BM_ELEM_CD_SET_BOOL(l, offsets.select_edge, false);
825 changed = true;
826 }
827 }
828 }
829 }
830 return changed;
831 }
832
833 /* Extract loop pairs or single loops. */
834 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
835 if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
836 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
837 if (UL(l)->is_select_edge) {
838 if (!UL(l)->in_rip_pairs) {
839 UVRipPairs *rip = uv_rip_pairs_from_loop(l, aspect_y, offsets.uv);
840 float center[2];
841 float dir_cursor[2];
842 float dir_side[2][2];
843 int side_from_cursor = -1;
844 if (uv_rip_pairs_calc_center_and_direction(rip, offsets.uv, center, dir_side)) {
845 for (int i = 0; i < 2; i++) {
846 sub_v2_v2v2(dir_cursor, center, co);
847 normalize_v2(dir_cursor);
848 }
849 side_from_cursor = (dot_v2v2(dir_side[0], dir_cursor) -
850 dot_v2v2(dir_side[1], dir_cursor)) < 0.0f;
851 }
852 GSetIterator gs_iter;
853 GSET_ITER (gs_iter, rip->loops) {
854 BMLoop *l_iter = static_cast<BMLoop *>(BLI_gsetIterator_getKey(&gs_iter));
855 ULData *ul = UL(l_iter);
856 if (ul->side == side_from_cursor) {
857 uvedit_uv_select_disable(scene, bm, l_iter, offsets);
858 changed = true;
859 }
860 /* Ensure we don't operate on these again. */
861 *ul = ul_clear;
862 }
864 }
865 }
866 else if (UL(l)->is_select_vert_single) {
867 UVRipSingle *rip = uv_rip_single_from_loop(l, co, aspect_y, offsets.uv);
868 /* We only ever use one side. */
869 const int side_from_cursor = 0;
870 GSetIterator gs_iter;
871 GSET_ITER (gs_iter, rip->loops) {
872 BMLoop *l_iter = static_cast<BMLoop *>(BLI_gsetIterator_getKey(&gs_iter));
873 ULData *ul = UL(l_iter);
874 if (ul->side == side_from_cursor) {
875 uvedit_uv_select_disable(scene, bm, l_iter, offsets);
876 changed = true;
877 }
878 /* Ensure we don't operate on these again. */
879 *ul = ul_clear;
880 }
882 }
883 }
884 }
885 }
886 if (changed) {
888 }
889 return changed;
890}
891
893
894/* -------------------------------------------------------------------- */
897
899{
901 Scene *scene = CTX_data_scene(C);
902 ViewLayer *view_layer = CTX_data_view_layer(C);
903
904 if (scene->toolsettings->uv_flag & UV_SYNC_SELECTION) {
905 /* "Rip" is logically incompatible with sync-select.
906 * Report an error instead of "poll" so this is reported when the tool is used,
907 * with #131642 implemented, this can be made to work. */
908 BKE_report(op->reports, RPT_ERROR, "Rip is not compatible with sync selection");
909 return OPERATOR_CANCELLED;
910 }
911
912 bool changed_multi = false;
913
914 float co[2];
915 RNA_float_get_array(op->ptr, "location", co);
916
917 float aspx, aspy;
918 {
919 /* Note that we only want to run this on the. */
920 Object *obedit = CTX_data_edit_object(C);
921 ED_uvedit_get_aspect(obedit, &aspx, &aspy);
922 }
923 const float aspect_y = aspx / aspy;
924
926 scene, view_layer, nullptr);
927
928 for (Object *obedit : objects) {
929 if (uv_rip_object(scene, obedit, co, aspect_y)) {
930 changed_multi = true;
931 uvedit_live_unwrap_update(sima, scene, obedit);
932 DEG_id_tag_update(static_cast<ID *>(obedit->data), 0);
933 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
934 }
935 }
936
937 if (!changed_multi) {
938 BKE_report(op->reports, RPT_ERROR, "Rip failed");
939 return OPERATOR_CANCELLED;
940 }
941 return OPERATOR_FINISHED;
942}
943
945{
946 ARegion *region = CTX_wm_region(C);
947 float co[2];
948
949 UI_view2d_region_to_view(&region->v2d, event->mval[0], event->mval[1], &co[0], &co[1]);
950 RNA_float_set_array(op->ptr, "location", co);
951
952 return uv_rip_exec(C, op);
953}
954
956{
957 /* identifiers */
958 ot->name = "UV Rip";
959 ot->description = "Rip selected vertices or a selected region";
960 ot->idname = "UV_OT_rip";
962
963 /* API callbacks. */
964 ot->exec = uv_rip_exec;
965 ot->invoke = uv_rip_invoke;
966 ot->poll = ED_operator_uvedit;
967
968 /* translation data */
970
971 /* properties */
973 ot->srna,
974 "location",
975 2,
976 nullptr,
977 -FLT_MAX,
978 FLT_MAX,
979 "Location",
980 "Mouse location in normalized coordinates, 0.0 to 1.0 is within the image bounds",
981 -100.0f,
982 100.0f);
983}
984
SpaceImage * CTX_wm_space_image(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
Object * CTX_data_edit_object(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
CustomData interface, see also DNA_customdata_types.h.
const char * CustomData_get_active_layer_name(const CustomData *data, eCustomDataType type)
blender::Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs(const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:126
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_STATIC_ASSERT(a, msg)
Definition BLI_assert.h:83
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_INLINE
struct GSet GSet
Definition BLI_ghash.h:337
GSet * BLI_gset_ptr_new(const char *info)
bool BLI_gset_haskey(const GSet *gs, const void *key) ATTR_WARN_UNUSED_RESULT
unsigned int BLI_gset_len(const GSet *gs) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:954
BLI_INLINE void * BLI_gsetIterator_getKey(GSetIterator *gsi)
Definition BLI_ghash.h:455
#define GSET_ITER(gs_iter_, gset_)
Definition BLI_ghash.h:468
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
bool BLI_gset_add(GSet *gs, void *key)
Definition BLI_ghash.cc:966
bool BLI_gset_remove(GSet *gs, const void *key, GSetKeyFreeFP keyfreefp)
Definition BLI_ghash.cc:999
MINLINE float min_ff(float a, float b)
float angle_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void mul_v2_fl(float r[2], float f)
MINLINE void add_v2_v2(float r[2], const float a[2])
MINLINE bool equals_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE void zero_v2(float r[2])
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v2(float n[2])
unsigned int uint
#define UNLIKELY(x)
#define LIKELY(x)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ CD_PROP_FLOAT2
Object is a sort of wrapper for general info.
@ UV_SYNC_SELECTION
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
bool ED_operator_uvedit(bContext *C)
#define P_MIRROR_DUMMY
void uvedit_deselect_flush(const Scene *scene, BMesh *bm)
void uvedit_uv_select_disable(const Scene *scene, BMesh *bm, BMLoop *l, const BMUVOffsets &offsets)
void ED_uvedit_get_aspect(Object *obedit, float *r_aspx, float *r_aspy)
bool uvedit_face_visible_test(const Scene *scene, const BMFace *efa)
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
void UI_view2d_region_to_view(const View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
Definition view2d.cc:1667
#define NC_GEOM
Definition WM_types.hh:390
#define ND_DATA
Definition WM_types.hh:506
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:218
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
#define BM_ELEM_CD_GET_BOOL(ele, offset)
@ BM_ELEM_TAG
#define BM_ELEM_CD_GET_FLOAT_P(ele, offset)
@ BM_LOOP
#define BM_ELEM_CD_SET_BOOL(ele, offset, f)
#define BM_elem_flag_set(ele, hflag, val)
#define BM_elem_flag_test(ele, hflag)
void BM_uv_map_attr_edge_select_ensure(BMesh *bm, const StringRef uv_map_name)
void BM_uv_map_attr_vert_select_ensure(BMesh *bm, const StringRef uv_map_name)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
@ BM_FACES_OF_MESH
@ BM_LOOPS_OF_VERT
@ BM_LOOPS_OF_FACE
BMesh * bm
BLI_INLINE bool BM_vert_in_edge(const BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMLoop * l
BMUVOffsets BM_uv_map_offsets_get(const BMesh *bm)
bool BM_loop_uv_share_edge_check(const BMLoop *l_a, const BMLoop *l_b, const int cd_loop_uv_offset)
bool BM_edge_uv_share_vert_check(const BMEdge *e, const BMLoop *l_a, const BMLoop *l_b, const int cd_loop_uv_offset)
float BM_face_uv_calc_cross(const BMFace *f, const int cd_loop_uv_offset)
#define fabsf(x)
int count
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void properties_register(wmOperatorType *ot, int flags)
VecBase< float, 2 > float2
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
PropertyRNA * RNA_def_float_vector(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
#define FLT_MAX
Definition stdcycles.h:14
struct BMVert * v
struct BMEdge * e
struct BMLoop * radial_next
struct BMLoop * prev
struct BMFace * f
struct BMLoop * next
Definition DNA_ID.h:404
MeshRuntimeHandle * runtime
struct ToolSettings * toolsettings
uint side_was_swapped
Definition uvedit_rip.cc:84
uint in_rip_pairs
Definition uvedit_rip.cc:76
uint is_select_vert_single
Definition uvedit_rip.cc:70
uint in_stack
Definition uvedit_rip.cc:74
uint is_select_edge
Definition uvedit_rip.cc:57
uint side
Definition uvedit_rip.cc:78
uint is_select_all
Definition uvedit_rip.cc:72
GSet * loops
int mval[2]
Definition WM_types.hh:760
struct ReportList * reports
struct PointerRNA * ptr
i
Definition text_draw.cc:230
void uvedit_live_unwrap_update(SpaceImage *sima, Scene *scene, Object *obedit)
static wmOperatorStatus uv_rip_exec(bContext *C, wmOperator *op)
static wmOperatorStatus uv_rip_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static BMLoop * bm_loop_find_other_fan_loop_with_visible_face(BMLoop *l_src, BMVert *v_src, const int cd_loop_uv_offset)
#define UV_SET_SIDE_AND_REMOVE_FROM_RAIL(loop, side_value)
static void bm_loop_uv_select_single_vert_validate(BMLoop *l_init, const int cd_loop_uv_offset)
static void uv_rip_pairs_free(UVRipPairs *rip)
BLI_INLINE ULData * UL(BMLoop *l)
Definition uvedit_rip.cc:90
static void uv_rip_single_free(UVRipSingle *rip)
static void uv_rip_pairs_remove(UVRipPairs *rip, BMLoop *l)
static void uv_rip_pairs_add(UVRipPairs *rip, BMLoop *l)
static int uv_rip_pairs_loop_count_on_side(BMLoop *l_init, uint side, const int cd_loop_uv_offset)
static UVRipSingle * uv_rip_single_from_loop(BMLoop *l_init_orig, const float co[2], const float aspect_y, const int cd_loop_uv_offset)
static bool uv_rip_object(Scene *scene, Object *obedit, const float co[2], const float aspect_y)
static BMLoop * bm_loop_find_other_radial_loop_with_visible_face(BMLoop *l_src, const int cd_loop_uv_offset)
void UV_OT_rip(wmOperatorType *ot)
static UVRipPairs * uv_rip_pairs_from_loop(BMLoop *l_init, const float aspect_y, const int cd_loop_uv_offset)
static float uv_rip_pairs_calc_uv_angle(BMLoop *l_init, uint side, const float aspect_y, const int cd_loop_uv_offset)
static bool uv_rip_pairs_calc_center_and_direction(UVRipPairs *rip, const int cd_loop_uv_offset, float r_center[2], float r_dir_side[2][2])
static bool uv_rip_pairs_loop_change_sides_test(BMLoop *l_switch, BMLoop *l_target, const float aspect_y, const int cd_loop_uv_offset)
static void bm_loop_calc_uv_angle_from_dir(BMLoop *l, const float dir[2], const float aspect_y, const int cd_loop_uv_offset, float *r_corner_angle, float *r_edge_angle, int *r_edge_index)
static BMLoop * bm_vert_step_fan_loop_uv(BMLoop *l, BMEdge **e_step, const int cd_loop_uv_offset)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition wm_files.cc:4226