Blender V5.3
uvedit_smart_stitch.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 <cmath>
10#include <cstdlib>
11#include <cstring>
12#include <fmt/format.h>
13
14#include "MEM_guardedalloc.h"
15
16#include "DNA_object_types.h"
17#include "DNA_scene_types.h"
19
20#include "BLI_ghash.hh"
21#include "BLI_math_matrix_c.hh"
23#include "BLI_math_vector_c.hh"
24#include "BLI_span.hh"
25
26#include "BLT_translation.hh"
27
28#include "BKE_context.hh"
29#include "BKE_customdata.hh"
30#include "BKE_editmesh.hh"
31#include "BKE_layer.hh"
32#include "BKE_mesh_mapping.hh"
33#include "BKE_report.hh"
34#include "BKE_screen.hh"
35
36#include "DEG_depsgraph.hh"
37
38#include "ED_mesh.hh"
39#include "ED_screen.hh"
40#include "ED_space_api.hh"
41#include "ED_uvedit.hh"
42
43#include "GPU_batch.hh"
44#include "GPU_state.hh"
45
46#include "RNA_access.hh"
47#include "RNA_define.hh"
48#include "RNA_prototypes.hh"
49
50#include "WM_api.hh"
51#include "WM_types.hh"
52
53#include "UI_resources.hh"
54#include "UI_view2d.hh"
55
56#include "uvedit_intern.hh"
57
58namespace blender {
59
60/* ********************** smart stitch operator *********************** */
61
62namespace {
63
64/* object that stores display data for previewing before confirming stitching */
65struct StitchPreviewer {
66 /* here we'll store the preview triangle indices of the mesh */
67 float *preview_polys;
68 /* uvs per face. */
69 uint *uvs_per_polygon;
70 /* Number of preview polygons. */
71 uint num_polys;
72 /* preview data. These will be either the previewed vertices or edges
73 * depending on stitch mode settings */
74 float *preview_stitchable;
75 float *preview_unstitchable;
76 /* here we'll store the number of elements to be drawn */
77 uint num_stitchable;
78 uint num_unstitchable;
79 uint preview_uvs;
80 /* ...and here we'll store the static island triangles */
81 float *static_tris;
82 uint num_static_tris;
83};
84
90struct IslandStitchData {
91 /* rotation can be used only for edges, for vertices there is no such notion */
92 float rotation;
93 float rotation_neg;
94 float translation[2];
95 /* Used for rotation, the island will rotate around this point */
96 float medianPoint[2];
97 int numOfElements;
98 int num_rot_elements;
99 int num_rot_elements_neg;
100 /* flag to remember if island has been added for preview */
101 char addedForPreview;
102 /* flag an island to be considered for determining static island */
103 char stitchableCandidate;
104 /* if edge rotation is used, flag so that vertex rotation is not used */
105 bool use_edge_rotation;
106 /* boundary seam checking */
107 bool all_boundaries_are_seams;
108};
109
110/* just for averaging UVs */
111struct UVVertAverage {
112 float uv[2];
113 ushort count;
114};
115
116struct UvEdge {
118 uint uv1;
119 uint uv2;
122 uchar flag;
127 UvElement *element;
130 UvEdge *next;
132 UvEdge *first;
133};
134
139struct StitchStateOrigBounds {
141 blender::Vector<bool> island_has_selected;
142};
143
144/* stitch state object */
145struct StitchState {
147 float aspect;
148 /* object for editmesh */
149 Object *obedit;
150
151 /* element map for getting info about uv connectivity */
152 UvElementMap *element_map;
153 /* edge container */
154 UvEdge *uvedges;
155 /* container of first of a group of coincident uvs, these will be operated upon */
156 UvElement **uvs;
157 /* maps uvelements to their first coincident uv */
158 int *map;
159 /* 2D normals per uv to calculate rotation for snapping */
160 float *normals;
161 /* edge storage */
162 UvEdge *edges;
163 /* hash for quick lookup of edges */
164 GHash *edge_hash;
165 /* which islands to stop at (to make active) when pressing 'I' */
166 bool *island_is_stitchable;
168 StitchStateOrigBounds *orig_bounds;
169
170 /* count of separate uvs and edges */
171 int total_separate_edges;
172 int total_separate_uvs;
173 /* hold selection related information */
174 void **selection_stack;
175 int selection_size;
176
177 /* store number of primitives per face so that we can allocate the active island buffer later */
178 uint *tris_per_island;
179 /* preview data */
180 StitchPreviewer *stitch_preview;
181};
182
183/* Stitch state container. */
184struct StitchStateContainer {
185 /* clear seams of stitched edges after stitch */
186 bool clear_seams;
187 /* use limit flag */
188 bool use_limit;
189 /* limit to operator, same as original operator */
190 float limit_dist;
191 /* snap uv islands together during stitching */
192 bool snap_islands;
193 /* stitch at midpoints or at islands */
194 bool midpoints;
195 /* vert or edge mode used for stitching */
196 char mode;
197 /* handle for drawing */
198 void *draw_handle;
199 /* island that stays in place */
200 int static_island;
201
202 /* Objects and states are aligned. */
203 int objects_len;
204 Object **objects;
205 StitchState **states;
206
207 int active_object_index;
208
209 bool ignore_seam_boundary;
210 bool only_selected_uvs;
211};
212
213struct PreviewPosition {
214 int data_position;
215 int polycount_position;
216};
217/*
218 * defines for UvElement/UcEdge flags
219 */
220enum {
221 STITCH_SELECTED = 1,
222 STITCH_STITCHABLE = 2,
223 STITCH_PROCESSED = 4,
224 STITCH_BOUNDARY = 8,
225 STITCH_STITCHABLE_CANDIDATE = 16,
226};
227
228#define STITCH_NO_PREVIEW -1
229
230enum StitchModes {
231 STITCH_VERT,
232 STITCH_EDGE,
233};
234
236struct UvElementID {
237 int faceIndex;
238 int elementIndex;
239};
240
246struct StitchStateInit {
248 int *objs_selection_count;
250 UvElementID *selected_uvs;
251};
252
253} // namespace
254
255static void stitch_state_init_free(StitchStateInit *state_init)
256{
257 MEM_SAFE_DELETE(state_init->objs_selection_count);
258 MEM_SAFE_DELETE(state_init->selected_uvs);
259}
260
261/* constructor */
262static StitchPreviewer *stitch_preview_init()
263{
264 StitchPreviewer *stitch_preview;
265
266 stitch_preview = MEM_new_uninitialized<StitchPreviewer>("stitch_previewer");
267 stitch_preview->preview_polys = nullptr;
268 stitch_preview->preview_stitchable = nullptr;
269 stitch_preview->preview_unstitchable = nullptr;
270 stitch_preview->uvs_per_polygon = nullptr;
271
272 stitch_preview->preview_uvs = 0;
273 stitch_preview->num_polys = 0;
274 stitch_preview->num_stitchable = 0;
275 stitch_preview->num_unstitchable = 0;
276
277 stitch_preview->static_tris = nullptr;
278
279 stitch_preview->num_static_tris = 0;
280
281 return stitch_preview;
282}
283
284/* destructor...yeah this should be C++ :) */
285static void stitch_preview_delete(StitchPreviewer *stitch_preview)
286{
287 if (stitch_preview) {
288 MEM_SAFE_DELETE(stitch_preview->preview_polys);
289 MEM_SAFE_DELETE(stitch_preview->uvs_per_polygon);
290 MEM_SAFE_DELETE(stitch_preview->preview_stitchable);
291 MEM_SAFE_DELETE(stitch_preview->preview_unstitchable);
292 MEM_SAFE_DELETE(stitch_preview->static_tris);
293 MEM_delete(stitch_preview);
294 }
295}
296
297/* This function updates the header of the UV editor when the stitch tool updates its settings */
298static void stitch_update_header(StitchStateContainer *ssc, bContext *C)
299{
300 WorkspaceStatus status(C);
301 status.item(IFACE_("Confirm"), ICON_MOUSE_LMB);
302 status.item(IFACE_("Cancel"), ICON_EVENT_ESC);
303 status.item(fmt::format("{} {}",
304 IFACE_("Select"),
305 (ssc->mode == STITCH_VERT ? IFACE_("Vertices") : IFACE_("Edges"))),
306 ICON_EVENT_SHIFT,
307 ICON_MOUSE_RMB);
308 status.item(fmt::format("{} : {}",
309 IFACE_("Mode"),
310 (ssc->mode == STITCH_VERT ? IFACE_("Vertex") : IFACE_("Edge"))),
311 ICON_EVENT_TAB);
312 status.item(IFACE_("Switch Island"), ICON_EVENT_I);
313 status.item_bool(IFACE_("Snap"), ssc->snap_islands, ICON_EVENT_S);
314 status.item_bool(IFACE_("Midpoints"), ssc->midpoints, ICON_EVENT_M);
315 status.item_bool(IFACE_("Limit"), ssc->use_limit, ICON_EVENT_L);
316 if (ssc->use_limit) {
317 status.item(fmt::format("{} ({:.2f})", IFACE_("Limit Distance"), ssc->limit_dist),
318 ICON_EVENT_ALT,
319 ICON_MOUSE_MMB_SCROLL);
320 }
321}
322
323static void stitch_uv_rotate(const float mat[2][2],
324 const float medianPoint[2],
325 float uv[2],
326 float aspect)
327{
328 float uv_rotation_result[2];
329
330 uv[1] /= aspect;
331
332 sub_v2_v2(uv, medianPoint);
333 mul_v2_m2v2(uv_rotation_result, mat, uv);
334 add_v2_v2v2(uv, uv_rotation_result, medianPoint);
335
336 uv[1] *= aspect;
337}
338
339/* check if two uvelements are stitchable.
340 * This should only operate on -different- separate UvElements */
341static bool stitch_check_uvs_stitchable(const int cd_loop_uv_offset,
343 UvElement *element_iter,
344 StitchStateContainer *ssc)
345{
346 float limit;
347
348 if (element_iter == element) {
349 return false;
350 }
351
352 limit = ssc->limit_dist;
353
354 if (ssc->use_limit) {
355 BMLoop *l;
356
357 l = element->l;
358 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
359 l = element_iter->l;
360 float *luv_iter = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
361
362 if (fabsf(luv[0] - luv_iter[0]) < limit && fabsf(luv[1] - luv_iter[1]) < limit) {
363 return true;
364 }
365 return false;
366 }
367 return true;
368}
369
370static bool stitch_check_edges_stitchable(const int cd_loop_uv_offset,
371 UvEdge *edge,
372 UvEdge *edge_iter,
373 StitchStateContainer *ssc,
374 StitchState *state)
375{
376 float limit;
377
378 if (edge_iter == edge) {
379 return false;
380 }
381
382 limit = ssc->limit_dist;
383
384 if (ssc->use_limit) {
385 float *luv_orig1 = BM_ELEM_CD_GET_FLOAT_P(state->uvs[edge->uv1]->l, cd_loop_uv_offset);
386 float *luv_iter1 = BM_ELEM_CD_GET_FLOAT_P(state->uvs[edge_iter->uv1]->l, cd_loop_uv_offset);
387
388 float *luv_orig2 = BM_ELEM_CD_GET_FLOAT_P(state->uvs[edge->uv2]->l, cd_loop_uv_offset);
389 float *luv_iter2 = BM_ELEM_CD_GET_FLOAT_P(state->uvs[edge_iter->uv2]->l, cd_loop_uv_offset);
390
391 if (fabsf(luv_orig1[0] - luv_iter1[0]) < limit && fabsf(luv_orig1[1] - luv_iter1[1]) < limit &&
392 fabsf(luv_orig2[0] - luv_iter2[0]) < limit && fabsf(luv_orig2[1] - luv_iter2[1]) < limit)
393 {
394 return true;
395 }
396 return false;
397 }
398 return true;
399}
400
401static bool stitch_check_uvs_state_stitchable(const int cd_loop_uv_offset,
403 UvElement *element_iter,
404 StitchStateContainer *ssc)
405{
406 if ((ssc->snap_islands && element->island == element_iter->island) ||
407 (!ssc->midpoints && element->island == element_iter->island))
408 {
409 return false;
410 }
411
412 return stitch_check_uvs_stitchable(cd_loop_uv_offset, element, element_iter, ssc);
413}
414
415static bool stitch_check_edges_state_stitchable(const int cd_loop_uv_offset,
416 UvEdge *edge,
417 UvEdge *edge_iter,
418 StitchStateContainer *ssc,
419 StitchState *state)
420{
421 if ((ssc->snap_islands && edge->element->island == edge_iter->element->island) ||
422 (!ssc->midpoints && edge->element->island == edge_iter->element->island))
423 {
424 return false;
425 }
426
427 return stitch_check_edges_stitchable(cd_loop_uv_offset, edge, edge_iter, ssc, state);
428}
429
430/* calculate snapping for islands */
431static void stitch_calculate_island_snapping(const int cd_loop_uv_offset,
432 StitchState *state,
433 PreviewPosition *preview_position,
434 StitchPreviewer *preview,
435 IslandStitchData *island_stitch_data,
436 int final)
437{
439
440 for (int i = 0; i < state->element_map->total_islands; i++) {
441 if (island_stitch_data[i].addedForPreview) {
442 int numOfIslandUVs = 0, j;
443 int totelem = island_stitch_data[i].num_rot_elements_neg +
444 island_stitch_data[i].num_rot_elements;
445 float rotation;
446 float rotation_mat[2][2];
447
448 /* check to avoid divide by 0 */
449 if (island_stitch_data[i].num_rot_elements > 1) {
450 island_stitch_data[i].rotation /= island_stitch_data[i].num_rot_elements;
451 }
452
453 if (island_stitch_data[i].num_rot_elements_neg > 1) {
454 island_stitch_data[i].rotation_neg /= island_stitch_data[i].num_rot_elements_neg;
455 }
456
457 if (island_stitch_data[i].numOfElements > 1) {
458 island_stitch_data[i].medianPoint[0] /= island_stitch_data[i].numOfElements;
459 island_stitch_data[i].medianPoint[1] /= island_stitch_data[i].numOfElements;
460
461 island_stitch_data[i].translation[0] /= island_stitch_data[i].numOfElements;
462 island_stitch_data[i].translation[1] /= island_stitch_data[i].numOfElements;
463 }
464
465 island_stitch_data[i].medianPoint[1] /= state->aspect;
466 if ((island_stitch_data[i].rotation + island_stitch_data[i].rotation_neg < float(M_PI_2)) ||
467 island_stitch_data[i].num_rot_elements == 0 ||
468 island_stitch_data[i].num_rot_elements_neg == 0)
469 {
470 rotation = (island_stitch_data[i].rotation * island_stitch_data[i].num_rot_elements -
471 island_stitch_data[i].rotation_neg *
472 island_stitch_data[i].num_rot_elements_neg) /
473 totelem;
474 }
475 else {
476 rotation = (island_stitch_data[i].rotation * island_stitch_data[i].num_rot_elements +
477 (2.0f * float(M_PI) - island_stitch_data[i].rotation_neg) *
478 island_stitch_data[i].num_rot_elements_neg) /
479 totelem;
480 }
481
482 angle_to_mat2(rotation_mat, rotation);
483 numOfIslandUVs = state->element_map->island_total_uvs[i];
484 element = &state->element_map->storage[state->element_map->island_indices[i]];
485 for (j = 0; j < numOfIslandUVs; j++, element++) {
486 /* stitchable uvs have already been processed, don't process */
487 if (!(element->flag & STITCH_PROCESSED)) {
488 BMLoop *l;
489
490 l = element->l;
491 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
492
493 if (final) {
494
495 stitch_uv_rotate(rotation_mat, island_stitch_data[i].medianPoint, luv, state->aspect);
496
497 add_v2_v2(luv, island_stitch_data[i].translation);
498 }
499
500 else {
501
502 int face_preview_pos =
503 preview_position[BM_elem_index_get(element->l->f)].data_position;
504
505 stitch_uv_rotate(rotation_mat,
506 island_stitch_data[i].medianPoint,
507 preview->preview_polys + face_preview_pos +
508 2 * element->loop_of_face_index,
509 state->aspect);
510
511 add_v2_v2(preview->preview_polys + face_preview_pos + 2 * element->loop_of_face_index,
512 island_stitch_data[i].translation);
513 }
514 }
515 /* cleanup */
516 element->flag &= STITCH_SELECTED;
517 }
518 }
519 }
520}
521
522static void stitch_island_calculate_edge_rotation(const int cd_loop_uv_offset,
523 UvEdge *edge,
524 StitchStateContainer *ssc,
525 StitchState *state,
526 UVVertAverage *uv_average,
527 const uint *uvfinal_map,
528 IslandStitchData *island_stitch_data)
529{
530 UvElement *element1, *element2;
531 float uv1[2], uv2[2];
532 float edgecos, edgesin;
533 int index1, index2;
534 float rotation;
535
536 element1 = state->uvs[edge->uv1];
537 element2 = state->uvs[edge->uv2];
538
539 float *luv1 = BM_ELEM_CD_GET_FLOAT_P(element1->l, cd_loop_uv_offset);
540 float *luv2 = BM_ELEM_CD_GET_FLOAT_P(element2->l, cd_loop_uv_offset);
541
542 if (ssc->mode == STITCH_VERT) {
543 index1 = uvfinal_map[element1 - state->element_map->storage];
544 index2 = uvfinal_map[element2 - state->element_map->storage];
545 }
546 else {
547 index1 = edge->uv1;
548 index2 = edge->uv2;
549 }
550 /* the idea here is to take the directions of the edges and find the rotation between
551 * final and initial direction. This, using inner and outer vector products,
552 * gives the angle. Directions are differences so... */
553 uv1[0] = luv2[0] - luv1[0];
554 uv1[1] = luv2[1] - luv1[1];
555
556 uv1[1] /= state->aspect;
557
558 uv2[0] = uv_average[index2].uv[0] - uv_average[index1].uv[0];
559 uv2[1] = uv_average[index2].uv[1] - uv_average[index1].uv[1];
560
561 uv2[1] /= state->aspect;
562
563 normalize_v2(uv1);
564 normalize_v2(uv2);
565
566 edgecos = dot_v2v2(uv1, uv2);
567 edgesin = cross_v2v2(uv1, uv2);
568 rotation = acosf(max_ff(-1.0f, min_ff(1.0f, edgecos)));
569
570 if (edgesin > 0.0f) {
571 island_stitch_data[element1->island].num_rot_elements++;
572 island_stitch_data[element1->island].rotation += rotation;
573 }
574 else {
575 island_stitch_data[element1->island].num_rot_elements_neg++;
576 island_stitch_data[element1->island].rotation_neg += rotation;
577 }
578}
579
580static void stitch_island_calculate_vert_rotation(const int cd_loop_uv_offset,
582 StitchStateContainer *ssc,
583 StitchState *state,
584 IslandStitchData *island_stitch_data)
585{
586 float rotation = 0, rotation_neg = 0;
587 int rot_elem = 0, rot_elem_neg = 0;
588
589 if (element->island == ssc->static_island && !ssc->midpoints) {
590 return;
591 }
592
593 UvElement *element_iter = BM_uv_element_get_head(state->element_map, element);
594 for (; element_iter; element_iter = element_iter->next) {
595 if (element_iter->separate &&
596 stitch_check_uvs_state_stitchable(cd_loop_uv_offset, element, element_iter, ssc))
597 {
598 float normal[2];
599
600 /* only calculate rotation against static island uv verts */
601 if (!ssc->midpoints && element_iter->island != ssc->static_island) {
602 continue;
603 }
604
605 int index_tmp1 = element_iter - state->element_map->storage;
606 index_tmp1 = state->map[index_tmp1];
607 int index_tmp2 = element - state->element_map->storage;
608 index_tmp2 = state->map[index_tmp2];
609
610 negate_v2_v2(normal, state->normals + index_tmp2 * 2);
611 float edgecos = dot_v2v2(normal, state->normals + index_tmp1 * 2);
612 float edgesin = cross_v2v2(normal, state->normals + index_tmp1 * 2);
613 if (edgesin > 0.0f) {
614 rotation += acosf(max_ff(-1.0f, min_ff(1.0f, edgecos)));
615 rot_elem++;
616 }
617 else {
618 rotation_neg += acosf(max_ff(-1.0f, min_ff(1.0f, edgecos)));
619 rot_elem_neg++;
620 }
621 }
622 }
623
624 if (ssc->midpoints) {
625 rotation /= 2.0f;
626 rotation_neg /= 2.0f;
627 }
628 island_stitch_data[element->island].num_rot_elements += rot_elem;
629 island_stitch_data[element->island].rotation += rotation;
630 island_stitch_data[element->island].num_rot_elements_neg += rot_elem_neg;
631 island_stitch_data[element->island].rotation_neg += rotation_neg;
632}
633
634static void state_delete(StitchState *state)
635{
636 if (state) {
637 if (state->island_is_stitchable) {
638 MEM_delete(state->island_is_stitchable);
639 }
640 if (state->orig_bounds) {
641 MEM_delete(state->orig_bounds);
642 }
643 if (state->element_map) {
644 BM_uv_element_map_free(state->element_map);
645 }
646 if (state->uvs) {
647 MEM_delete(state->uvs);
648 }
649 if (state->selection_stack) {
650 MEM_delete(state->selection_stack);
651 }
652 if (state->tris_per_island) {
653 MEM_delete(state->tris_per_island);
654 }
655 if (state->map) {
656 MEM_delete(state->map);
657 }
658 if (state->normals) {
659 MEM_delete(state->normals);
660 }
661 if (state->edges) {
662 MEM_delete(state->edges);
663 }
664 stitch_preview_delete(state->stitch_preview);
665 state->stitch_preview = nullptr;
666 if (state->edge_hash) {
667 BLI_ghash_free(state->edge_hash, nullptr, nullptr);
668 }
669 MEM_delete(state);
670 }
671}
672
673static void state_delete_all(StitchStateContainer *ssc)
674{
675 if (ssc) {
676 for (uint ob_index = 0; ob_index < ssc->objects_len; ob_index++) {
677 state_delete(ssc->states[ob_index]);
678 }
679 MEM_delete(ssc->states);
680 MEM_delete(ssc->objects);
681 MEM_delete(ssc);
682 }
683}
684
685static void stitch_uv_edge_generate_linked_edges(GHash *edge_hash, StitchState *state)
686{
687 UvEdge *edges = state->edges;
688 const int *map = state->map;
689 UvElementMap *element_map = state->element_map;
690 for (int i = 0; i < state->total_separate_edges; i++) {
691 UvEdge *edge = edges + i;
692
693 if (edge->first) {
694 continue;
695 }
696
697 /* only boundary edges can be stitched. Yes. Sorry about that :p */
698 if (edge->flag & STITCH_BOUNDARY) {
699 UvElement *element1 = state->uvs[edge->uv1];
700 UvElement *element2 = state->uvs[edge->uv2];
701
702 /* Now iterate through all faces and try to find edges sharing the same vertices */
703 UvElement *iter1 = BM_uv_element_get_head(state->element_map, element1);
704 UvEdge *last_set = edge;
705 int elemindex2 = BM_elem_index_get(element2->l->v);
706
707 edge->first = edge;
708
709 for (; iter1; iter1 = iter1->next) {
710 UvElement *iter2 = nullptr;
711
712 /* check to see if other vertex of edge belongs to same vertex as */
713 if (BM_elem_index_get(iter1->l->next->v) == elemindex2) {
714 iter2 = BM_uv_element_get(element_map, iter1->l->next);
715 }
716 else if (BM_elem_index_get(iter1->l->prev->v) == elemindex2) {
717 iter2 = BM_uv_element_get(element_map, iter1->l->prev);
718 }
719
720 if (iter2) {
721 int index1 = map[iter1 - element_map->storage];
722 int index2 = map[iter2 - element_map->storage];
723 UvEdge edgetmp;
724 UvEdge *edge2, *eiter;
725 bool valid = true;
726
727 /* make sure the indices are well behaved */
728 if (index1 > index2) {
729 std::swap(index1, index2);
730 }
731
732 edgetmp.uv1 = index1;
733 edgetmp.uv2 = index2;
734
735 /* get the edge from the hash */
736 edge2 = static_cast<UvEdge *>(BLI_ghash_lookup(edge_hash, &edgetmp));
737
738 /* more iteration to make sure non-manifold case is handled nicely */
739 for (eiter = edge; eiter; eiter = eiter->next) {
740 if (edge2 == eiter) {
741 valid = false;
742 break;
743 }
744 }
745
746 if (valid) {
747 /* here I am taking care of non manifold case, assuming more than two matching edges.
748 * I am not too sure we want this though */
749 last_set->next = edge2;
750 last_set = edge2;
751 /* set first, similarly to uv elements.
752 * Now we can iterate among common edges easily */
753 edge2->first = edge;
754 }
755 }
756 }
757 }
758 else {
759 /* so stitchability code works */
760 edge->first = edge;
761 }
762 }
763}
764
768static bool stitch_island_pair_excluded(const StitchStateContainer *ssc,
769 const StitchState *state,
770 const IslandStitchData *island_stitch_data,
771 const int island_a,
772 const int island_b)
773{
774 if (ssc->ignore_seam_boundary) {
775 if (island_stitch_data[island_a].all_boundaries_are_seams ||
776 island_stitch_data[island_b].all_boundaries_are_seams)
777 {
778 return true;
779 }
780 }
781 if (ssc->only_selected_uvs) {
782 BLI_assert(state->orig_bounds != nullptr);
783 if (!state->orig_bounds->island_has_selected[island_a] ||
784 !state->orig_bounds->island_has_selected[island_b])
785 {
786 return true;
787 }
788 }
789 return false;
790}
791
792/* checks for remote uvs that may be stitched with a certain uv, flags them if stitchable. */
793static void determine_uv_stitchability(const int cd_loop_uv_offset,
795 StitchStateContainer *ssc,
796 StitchState *state,
797 IslandStitchData *island_stitch_data)
798{
799 UvElement *element_iter = BM_uv_element_get_head(state->element_map, element);
800 for (; element_iter; element_iter = element_iter->next) {
801 if (element_iter->separate) {
803 ssc, state, island_stitch_data, element_iter->island, element->island))
804 {
805 continue;
806 }
807
808 if (stitch_check_uvs_stitchable(cd_loop_uv_offset, element, element_iter, ssc)) {
809 island_stitch_data[element_iter->island].stitchableCandidate = 1;
810 island_stitch_data[element->island].stitchableCandidate = 1;
811 element->flag |= STITCH_STITCHABLE_CANDIDATE;
812 }
813 }
814 }
815}
816
817static void determine_uv_edge_stitchability(const int cd_loop_uv_offset,
818 UvEdge *edge,
819 StitchStateContainer *ssc,
820 StitchState *state,
821 IslandStitchData *island_stitch_data)
822{
823 UvEdge *edge_iter = edge->first;
824
825 for (; edge_iter; edge_iter = edge_iter->next) {
827 ssc, state, island_stitch_data, edge->element->island, edge_iter->element->island))
828 {
829 continue;
830 }
831 if (stitch_check_edges_stitchable(cd_loop_uv_offset, edge, edge_iter, ssc, state)) {
832 island_stitch_data[edge_iter->element->island].stitchableCandidate = 1;
833 island_stitch_data[edge->element->island].stitchableCandidate = 1;
834 edge->flag |= STITCH_STITCHABLE_CANDIDATE;
835 }
836 }
837}
838
839/* set preview buffer position of UV face in editface->tmp.l */
841 StitchPreviewer *preview,
842 PreviewPosition *preview_position)
843{
844 int index = BM_elem_index_get(efa);
845
846 if (preview_position[index].data_position == STITCH_NO_PREVIEW) {
847 preview_position[index].data_position = preview->preview_uvs * 2;
848 preview_position[index].polycount_position = preview->num_polys++;
849 preview->preview_uvs += efa->len;
850 }
851}
852
853/* setup face preview for all coincident uvs and their faces */
855 StitchStateContainer *ssc,
856 StitchState *state,
857 IslandStitchData *island_stitch_data,
858 PreviewPosition *preview_position)
859{
860 StitchPreviewer *preview = state->stitch_preview;
861
862 /* static island does not change so returning immediately */
863 if (ssc->snap_islands && !ssc->midpoints && ssc->static_island == element->island) {
864 return;
865 }
866
867 if (ssc->snap_islands) {
868 island_stitch_data[element->island].addedForPreview = 1;
869 }
870
871 do {
872 stitch_set_face_preview_buffer_position(element->l->f, preview, preview_position);
873 element = element->next;
874 } while (element && !element->separate);
875}
876
877/* checks if uvs are indeed stitchable and registers so that they can be shown in preview */
878static void stitch_validate_uv_stitchability(const int cd_loop_uv_offset,
880 StitchStateContainer *ssc,
881 StitchState *state,
882 IslandStitchData *island_stitch_data,
883 PreviewPosition *preview_position)
884{
885 StitchPreviewer *preview = state->stitch_preview;
886
887 /* If not the active object, then it's unstitchable */
888 if (ssc->states[ssc->active_object_index] != state) {
889 preview->num_unstitchable++;
890 return;
891 }
892
893 UvElement *element_iter = BM_uv_element_get_head(state->element_map, element);
894 for (; element_iter; element_iter = element_iter->next) {
895 if (element_iter->separate) {
896 if (element_iter == element) {
897 continue;
898 }
899 if (stitch_check_uvs_state_stitchable(cd_loop_uv_offset, element, element_iter, ssc)) {
900 if ((element_iter->island == ssc->static_island) ||
901 (element->island == ssc->static_island))
902 {
903 element->flag |= STITCH_STITCHABLE;
904 preview->num_stitchable++;
906 element, ssc, state, island_stitch_data, preview_position);
907 return;
908 }
909 }
910 }
911 }
912
913 /* this can happen if the uvs to be stitched are not on a stitchable island */
914 if (!(element->flag & STITCH_STITCHABLE)) {
915 preview->num_unstitchable++;
916 }
917}
918
919static void stitch_validate_edge_stitchability(const int cd_loop_uv_offset,
920 UvEdge *edge,
921 StitchStateContainer *ssc,
922 StitchState *state,
923 IslandStitchData *island_stitch_data,
924 PreviewPosition *preview_position)
925{
926 StitchPreviewer *preview = state->stitch_preview;
927
928 /* If not the active object, then it's unstitchable */
929 if (ssc->states[ssc->active_object_index] != state) {
930 preview->num_unstitchable++;
931 return;
932 }
933
934 UvEdge *edge_iter = edge->first;
935
936 for (; edge_iter; edge_iter = edge_iter->next) {
937 if (edge_iter == edge) {
938 continue;
939 }
940 if (stitch_check_edges_state_stitchable(cd_loop_uv_offset, edge, edge_iter, ssc, state)) {
941 if ((edge_iter->element->island == ssc->static_island) ||
942 (edge->element->island == ssc->static_island))
943 {
944 edge->flag |= STITCH_STITCHABLE;
945 preview->num_stitchable++;
947 state->uvs[edge->uv1], ssc, state, island_stitch_data, preview_position);
949 state->uvs[edge->uv2], ssc, state, island_stitch_data, preview_position);
950 return;
951 }
952 }
953 }
954
955 /* this can happen if the uvs to be stitched are not on a stitchable island */
956 if (!(edge->flag & STITCH_STITCHABLE)) {
957 preview->num_unstitchable++;
958 }
959}
960
963 int index,
964 PreviewPosition *preview_position,
965 UVVertAverage *final_position,
966 StitchStateContainer *ssc,
967 StitchState *state,
968 const bool final)
969{
971 StitchPreviewer *preview = state->stitch_preview;
972
973 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
974
975 if (element->flag & STITCH_STITCHABLE) {
976 UvElement *element_iter = element;
977 /* propagate to coincident uvs */
978 do {
979 BMLoop *l;
980
981 l = element_iter->l;
982 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
983
984 element_iter->flag |= STITCH_PROCESSED;
985 /* either flush to preview or to the MTFace, if final */
986 if (final) {
987 copy_v2_v2(luv, final_position[index].uv);
988
990 }
991 else {
992 int face_preview_pos =
993 preview_position[BM_elem_index_get(element_iter->l->f)].data_position;
994 if (face_preview_pos != STITCH_NO_PREVIEW) {
995 copy_v2_v2(preview->preview_polys + face_preview_pos +
996 2 * element_iter->loop_of_face_index,
997 final_position[index].uv);
998 }
999 }
1000
1001 /* end of calculations, keep only the selection flag */
1002 if ((!ssc->snap_islands) ||
1003 ((!ssc->midpoints) && (element_iter->island == ssc->static_island)))
1004 {
1005 element_iter->flag &= STITCH_SELECTED;
1006 }
1007
1008 element_iter = element_iter->next;
1009 } while (element_iter && !element_iter->separate);
1010 }
1011}
1012
1018 IslandStitchData *island_stitch_data)
1019{
1020 for (int i = 0; i < state->element_map->total_islands; i++) {
1021 island_stitch_data[i].all_boundaries_are_seams = true;
1022 }
1023
1024 for (int i = 0; i < state->total_separate_edges; i++) {
1025 UvEdge *edge = &state->edges[i];
1026
1027 if (edge->flag & STITCH_BOUNDARY) {
1028 int island1 = state->uvs[edge->uv1]->island;
1029 int island2 = state->uvs[edge->uv2]->island;
1030
1031 if (!BM_elem_flag_test(edge->element->l->e, BM_ELEM_SEAM)) {
1032 island_stitch_data[island1].all_boundaries_are_seams = false;
1033 if (island1 != island2) {
1034 island_stitch_data[island2].all_boundaries_are_seams = false;
1035 }
1036 }
1037 }
1038 }
1039}
1040
1041/* main processing function. It calculates preview and final positions. */
1042static int stitch_process_data(StitchStateContainer *ssc,
1043 StitchState *state,
1044 const Scene *scene,
1045 int final)
1046{
1047 int i;
1048 StitchPreviewer *preview;
1049 IslandStitchData *island_stitch_data = nullptr;
1050 int previous_island = ssc->static_island;
1052 BMFace *efa;
1053 BMIter iter;
1054 UVVertAverage *final_position = nullptr;
1055 bool is_active_state = (state == ssc->states[ssc->active_object_index]);
1056
1057 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_PROP_FLOAT2);
1058
1059 char stitch_midpoints = ssc->midpoints;
1060 /* Used to map UV indices to UV-average indices for selection. */
1061 uint *uvfinal_map = nullptr;
1062 /* per face preview position in preview buffer */
1063 PreviewPosition *preview_position = nullptr;
1064
1065 /* cleanup previous preview */
1066 stitch_preview_delete(state->stitch_preview);
1067 preview = state->stitch_preview = stitch_preview_init();
1068 if (preview == nullptr) {
1069 return 0;
1070 }
1071
1073 "stitch_face_preview_position");
1074 /* each face holds its position in the preview buffer in tmp. -1 is uninitialized */
1075 for (i = 0; i < bm->totface; i++) {
1076 preview_position[i].data_position = STITCH_NO_PREVIEW;
1077 }
1078
1079 island_stitch_data = MEM_new_array_zeroed<IslandStitchData>(state->element_map->total_islands,
1080 "stitch_island_data");
1081 if (!island_stitch_data) {
1082 return 0;
1083 }
1084
1085 /* store indices to editVerts and Faces. May be unneeded but ensuring anyway */
1087
1088 /****************************************
1089 * First determine stitchability of uvs *
1090 ****************************************/
1091 if (ssc->ignore_seam_boundary) {
1092 stitch_determine_seam_bounded_islands(state, island_stitch_data);
1093 }
1094
1095 for (i = 0; i < state->selection_size; i++) {
1096 if (ssc->mode == STITCH_VERT) {
1097 UvElement *element = static_cast<UvElement *>(state->selection_stack[i]);
1098 determine_uv_stitchability(cd_loop_uv_offset, element, ssc, state, island_stitch_data);
1099 }
1100 else {
1101 UvEdge *edge = static_cast<UvEdge *>(state->selection_stack[i]);
1102 determine_uv_edge_stitchability(cd_loop_uv_offset, edge, ssc, state, island_stitch_data);
1103 }
1104 }
1105
1106 /* Remember stitchable candidates as places the 'I' button will stop at. */
1107 for (int island_idx = 0; island_idx < state->element_map->total_islands; island_idx++) {
1108 state->island_is_stitchable[island_idx] = island_stitch_data[island_idx].stitchableCandidate ?
1109 true :
1110 false;
1111 }
1112
1113 if (is_active_state) {
1114 /* set static island to one that is added for preview */
1115 ssc->static_island %= state->element_map->total_islands;
1116 while (!(island_stitch_data[ssc->static_island].stitchableCandidate)) {
1117 ssc->static_island++;
1118 ssc->static_island %= state->element_map->total_islands;
1119 /* this is entirely possible if for example limit stitching
1120 * with no stitchable verts or no selection */
1121 if (ssc->static_island == previous_island) {
1122 break;
1123 }
1124 }
1125 }
1126
1127 for (i = 0; i < state->selection_size; i++) {
1128 if (ssc->mode == STITCH_VERT) {
1129 UvElement *element = static_cast<UvElement *>(state->selection_stack[i]);
1130 if (element->flag & STITCH_STITCHABLE_CANDIDATE) {
1131 element->flag &= ~STITCH_STITCHABLE_CANDIDATE;
1133 cd_loop_uv_offset, element, ssc, state, island_stitch_data, preview_position);
1134 }
1135 else {
1136 /* add to preview for unstitchable */
1137 preview->num_unstitchable++;
1138 }
1139 }
1140 else {
1141 UvEdge *edge = static_cast<UvEdge *>(state->selection_stack[i]);
1142 if (edge->flag & STITCH_STITCHABLE_CANDIDATE) {
1143 edge->flag &= ~STITCH_STITCHABLE_CANDIDATE;
1145 cd_loop_uv_offset, edge, ssc, state, island_stitch_data, preview_position);
1146 }
1147 else {
1148 preview->num_unstitchable++;
1149 }
1150 }
1151 }
1152
1153 /*********************************************************************
1154 * Setup the stitchable & unstitchable preview buffers and fill *
1155 * them with the appropriate data *
1156 *********************************************************************/
1157 if (!final) {
1158 float *luv;
1159 int stitchBufferIndex = 0, unstitchBufferIndex = 0;
1160 int preview_size = (ssc->mode == STITCH_VERT) ? 2 : 4;
1161 /* initialize the preview buffers */
1162 preview->preview_stitchable = MEM_new_array_uninitialized<float>(
1163 preview->num_stitchable * preview_size, "stitch_preview_stitchable_data");
1164 preview->preview_unstitchable = MEM_new_array_uninitialized<float>(
1165 preview->num_unstitchable * preview_size, "stitch_preview_unstitchable_data");
1166
1167 /* will cause cancel and freeing of all data structures so OK */
1168 if (!preview->preview_stitchable || !preview->preview_unstitchable) {
1169 return 0;
1170 }
1171
1172 /* fill the appropriate preview buffers */
1173 if (ssc->mode == STITCH_VERT) {
1174 for (i = 0; i < state->total_separate_uvs; i++) {
1175 UvElement *element = state->uvs[i];
1176 if (element->flag & STITCH_STITCHABLE) {
1177 luv = BM_ELEM_CD_GET_FLOAT_P(element->l, cd_loop_uv_offset);
1178 copy_v2_v2(&preview->preview_stitchable[stitchBufferIndex * 2], luv);
1179 stitchBufferIndex++;
1180 }
1181 else if (element->flag & STITCH_SELECTED) {
1182 luv = BM_ELEM_CD_GET_FLOAT_P(element->l, cd_loop_uv_offset);
1183 copy_v2_v2(&preview->preview_unstitchable[unstitchBufferIndex * 2], luv);
1184 unstitchBufferIndex++;
1185 }
1186 }
1187 }
1188 else {
1189 for (i = 0; i < state->total_separate_edges; i++) {
1190 UvEdge *edge = state->edges + i;
1191 UvElement *element1 = state->uvs[edge->uv1];
1192 UvElement *element2 = state->uvs[edge->uv2];
1193
1194 if (edge->flag & STITCH_STITCHABLE) {
1195 luv = BM_ELEM_CD_GET_FLOAT_P(element1->l, cd_loop_uv_offset);
1196 copy_v2_v2(&preview->preview_stitchable[stitchBufferIndex * 4], luv);
1197
1198 luv = BM_ELEM_CD_GET_FLOAT_P(element2->l, cd_loop_uv_offset);
1199 copy_v2_v2(&preview->preview_stitchable[stitchBufferIndex * 4 + 2], luv);
1200
1201 stitchBufferIndex++;
1202 BLI_assert(stitchBufferIndex <= preview->num_stitchable);
1203 }
1204 else if (edge->flag & STITCH_SELECTED) {
1205 luv = BM_ELEM_CD_GET_FLOAT_P(element1->l, cd_loop_uv_offset);
1206 copy_v2_v2(&preview->preview_unstitchable[unstitchBufferIndex * 4], luv);
1207
1208 luv = BM_ELEM_CD_GET_FLOAT_P(element2->l, cd_loop_uv_offset);
1209 copy_v2_v2(&preview->preview_unstitchable[unstitchBufferIndex * 4 + 2], luv);
1210
1211 unstitchBufferIndex++;
1212 BLI_assert(unstitchBufferIndex <= preview->num_unstitchable);
1213 }
1214 }
1215 }
1216 }
1217
1218 if (ssc->states[ssc->active_object_index] != state) {
1219 /* This is not the active object/state, exit here */
1220 MEM_delete(island_stitch_data);
1221 MEM_delete(preview_position);
1222 return 1;
1223 }
1224
1225 /****************************************
1226 * Setup preview for stitchable islands *
1227 ****************************************/
1228 if (ssc->snap_islands) {
1229 for (i = 0; i < state->element_map->total_islands; i++) {
1230 if (island_stitch_data[i].addedForPreview) {
1231 int numOfIslandUVs = state->element_map->island_total_uvs[i];
1232 UvElement *element = &state->element_map->storage[state->element_map->island_indices[i]];
1233 for (int j = 0; j < numOfIslandUVs; j++, element++) {
1234 stitch_set_face_preview_buffer_position(element->l->f, preview, preview_position);
1235 }
1236 }
1237 }
1238 }
1239
1240 /*********************************************************************
1241 * Setup the remaining preview buffers and fill them with the *
1242 * appropriate data *
1243 *********************************************************************/
1244 if (!final) {
1245 BMIter liter;
1246 BMLoop *l;
1247 float *luv;
1248 uint buffer_index = 0;
1249
1250 /* initialize the preview buffers */
1251 preview->preview_polys = MEM_new_array_uninitialized<float>(2 * preview->preview_uvs,
1252 "tri_uv_stitch_prev");
1253 preview->uvs_per_polygon = MEM_new_array_uninitialized<uint>(preview->num_polys,
1254 "tri_uv_stitch_prev");
1255
1256 preview->static_tris = MEM_new_array_uninitialized<float>(
1257 6 * state->tris_per_island[ssc->static_island], "static_island_preview_tris");
1258
1259 preview->num_static_tris = state->tris_per_island[ssc->static_island];
1260 /* will cause cancel and freeing of all data structures so OK */
1261 if (!preview->preview_polys) {
1262 return 0;
1263 }
1264
1265 /* copy data from UVs to the preview display buffers */
1266 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
1267 /* just to test if face was added for processing.
1268 * uvs of unselected vertices will return null */
1270
1271 if (element) {
1272 int numoftris = efa->len - 2;
1273 int index = BM_elem_index_get(efa);
1274 int face_preview_pos = preview_position[index].data_position;
1275 if (face_preview_pos != STITCH_NO_PREVIEW) {
1276 preview->uvs_per_polygon[preview_position[index].polycount_position] = efa->len;
1277 BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
1278 luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
1279 copy_v2_v2(preview->preview_polys + face_preview_pos + i * 2, luv);
1280 }
1281 }
1282
1283 /* if this is the static_island on the active object */
1284 if (element->island == ssc->static_island) {
1285 BMLoop *fl = BM_FACE_FIRST_LOOP(efa);
1286 float *fuv = BM_ELEM_CD_GET_FLOAT_P(fl, cd_loop_uv_offset);
1287
1288 BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
1289 if (i < numoftris) {
1290 /* using next since the first uv is already accounted for */
1291 BMLoop *lnext = l->next;
1292 float *luvnext = BM_ELEM_CD_GET_FLOAT_P(lnext->next, cd_loop_uv_offset);
1293 luv = BM_ELEM_CD_GET_FLOAT_P(lnext, cd_loop_uv_offset);
1294
1295 memcpy(preview->static_tris + buffer_index, fuv, sizeof(float[2]));
1296 memcpy(preview->static_tris + buffer_index + 2, luv, sizeof(float[2]));
1297 memcpy(preview->static_tris + buffer_index + 4, luvnext, sizeof(float[2]));
1298 buffer_index += 6;
1299 }
1300 else {
1301 break;
1302 }
1303 }
1304 }
1305 }
1306 }
1307 }
1308
1309 /******************************************************
1310 * Here we calculate the final coordinates of the uvs *
1311 ******************************************************/
1312
1313 if (ssc->mode == STITCH_VERT) {
1314 final_position = MEM_new_array_zeroed<UVVertAverage>(state->selection_size,
1315 "stitch_uv_average");
1316 uvfinal_map = MEM_new_array_zeroed<uint>(state->element_map->total_uvs, "stitch_uv_final_map");
1317 }
1318 else {
1319 final_position = MEM_new_array_zeroed<UVVertAverage>(state->total_separate_uvs,
1320 "stitch_uv_average");
1321 }
1322
1323 /* first pass, calculate final position for stitchable uvs of the static island */
1324 for (i = 0; i < state->selection_size; i++) {
1325 if (ssc->mode == STITCH_VERT) {
1326 UvElement *element = static_cast<UvElement *>(state->selection_stack[i]);
1327
1328 if (element->flag & STITCH_STITCHABLE) {
1329 BMLoop *l = element->l;
1330 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
1331
1332 uvfinal_map[element - state->element_map->storage] = i;
1333
1334 copy_v2_v2(final_position[i].uv, luv);
1335 final_position[i].count = 1;
1336
1337 if (ssc->snap_islands && element->island == ssc->static_island && !stitch_midpoints) {
1338 continue;
1339 }
1340
1341 UvElement *element_iter = state->element_map->vertex[BM_elem_index_get(l->v)];
1342 for (; element_iter; element_iter = element_iter->next) {
1343 if (element_iter->separate) {
1344 if (stitch_check_uvs_state_stitchable(cd_loop_uv_offset, element, element_iter, ssc)) {
1345 l = element_iter->l;
1346 luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
1347 if (stitch_midpoints) {
1348 add_v2_v2(final_position[i].uv, luv);
1349 final_position[i].count++;
1350 }
1351 else if (element_iter->island == ssc->static_island) {
1352 /* if multiple uvs on the static island exist,
1353 * last checked remains. to disambiguate we need to limit or use
1354 * edge stitch */
1355 copy_v2_v2(final_position[i].uv, luv);
1356 }
1357 }
1358 }
1359 }
1360 }
1361 if (stitch_midpoints) {
1362 final_position[i].uv[0] /= final_position[i].count;
1363 final_position[i].uv[1] /= final_position[i].count;
1364 }
1365 }
1366 else {
1367 UvEdge *edge = static_cast<UvEdge *>(state->selection_stack[i]);
1368
1369 if (edge->flag & STITCH_STITCHABLE) {
1370 float *luv2, *luv1;
1371 BMLoop *l;
1372 UvEdge *edge_iter;
1373
1374 l = state->uvs[edge->uv1]->l;
1375 luv1 = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
1376 l = state->uvs[edge->uv2]->l;
1377 luv2 = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
1378
1379 copy_v2_v2(final_position[edge->uv1].uv, luv1);
1380 copy_v2_v2(final_position[edge->uv2].uv, luv2);
1381 final_position[edge->uv1].count = 1;
1382 final_position[edge->uv2].count = 1;
1383
1384 state->uvs[edge->uv1]->flag |= STITCH_STITCHABLE;
1385 state->uvs[edge->uv2]->flag |= STITCH_STITCHABLE;
1386
1387 if (ssc->snap_islands && edge->element->island == ssc->static_island && !stitch_midpoints)
1388 {
1389 continue;
1390 }
1391
1392 for (edge_iter = edge->first; edge_iter; edge_iter = edge_iter->next) {
1393 if (stitch_check_edges_state_stitchable(cd_loop_uv_offset, edge, edge_iter, ssc, state))
1394 {
1395 l = state->uvs[edge_iter->uv1]->l;
1396 luv1 = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
1397 l = state->uvs[edge_iter->uv2]->l;
1398 luv2 = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
1399
1400 if (stitch_midpoints) {
1401 add_v2_v2(final_position[edge->uv1].uv, luv1);
1402 final_position[edge->uv1].count++;
1403 add_v2_v2(final_position[edge->uv2].uv, luv2);
1404 final_position[edge->uv2].count++;
1405 }
1406 else if (edge_iter->element->island == ssc->static_island) {
1407 copy_v2_v2(final_position[edge->uv1].uv, luv1);
1408 copy_v2_v2(final_position[edge->uv2].uv, luv2);
1409 }
1410 }
1411 }
1412 }
1413 }
1414 }
1415
1416 /* Take mean position here.
1417 * For edge case, this can't be done inside the loop for shared UV-verts. */
1418 if (ssc->mode == STITCH_EDGE && stitch_midpoints) {
1419 for (i = 0; i < state->total_separate_uvs; i++) {
1420 final_position[i].uv[0] /= final_position[i].count;
1421 final_position[i].uv[1] /= final_position[i].count;
1422 }
1423 }
1424
1425 /* second pass, calculate island rotation and translation before modifying any uvs */
1426 if (ssc->snap_islands) {
1427 if (ssc->mode == STITCH_VERT) {
1428 for (i = 0; i < state->selection_size; i++) {
1429 UvElement *element = static_cast<UvElement *>(state->selection_stack[i]);
1430
1431 if (element->flag & STITCH_STITCHABLE) {
1432 BMLoop *l;
1433 float *luv;
1434
1435 l = element->l;
1436 luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
1437
1438 /* accumulate each islands' translation from stitchable elements.
1439 * It is important to do here because in final pass MTFaces
1440 * get modified and result is zero. */
1441 island_stitch_data[element->island].translation[0] += final_position[i].uv[0] - luv[0];
1442 island_stitch_data[element->island].translation[1] += final_position[i].uv[1] - luv[1];
1443 island_stitch_data[element->island].medianPoint[0] += luv[0];
1444 island_stitch_data[element->island].medianPoint[1] += luv[1];
1445 island_stitch_data[element->island].numOfElements++;
1446 }
1447 }
1448
1449 /* only calculate rotation when an edge has been fully selected */
1450 for (i = 0; i < state->total_separate_edges; i++) {
1451 UvEdge *edge = state->edges + i;
1452 if ((edge->flag & STITCH_BOUNDARY) && (state->uvs[edge->uv1]->flag & STITCH_STITCHABLE) &&
1453 (state->uvs[edge->uv2]->flag & STITCH_STITCHABLE))
1454 {
1455 stitch_island_calculate_edge_rotation(cd_loop_uv_offset,
1456 edge,
1457 ssc,
1458 state,
1459 final_position,
1460 uvfinal_map,
1461 island_stitch_data);
1462 island_stitch_data[state->uvs[edge->uv1]->island].use_edge_rotation = true;
1463 }
1464 }
1465
1466 /* clear seams of stitched edges */
1467 if (final && ssc->clear_seams) {
1468 for (i = 0; i < state->total_separate_edges; i++) {
1469 UvEdge *edge = state->edges + i;
1470 if ((state->uvs[edge->uv1]->flag & STITCH_STITCHABLE) &&
1471 (state->uvs[edge->uv2]->flag & STITCH_STITCHABLE))
1472 {
1473 BM_elem_flag_disable(edge->element->l->e, BM_ELEM_SEAM);
1474 }
1475 }
1476 }
1477
1478 for (i = 0; i < state->selection_size; i++) {
1479 UvElement *element = static_cast<UvElement *>(state->selection_stack[i]);
1480 if (!island_stitch_data[element->island].use_edge_rotation) {
1481 if (element->flag & STITCH_STITCHABLE) {
1483 cd_loop_uv_offset, element, ssc, state, island_stitch_data);
1484 }
1485 }
1486 }
1487 }
1488 else {
1489 for (i = 0; i < state->total_separate_uvs; i++) {
1490 UvElement *element = state->uvs[i];
1491
1492 if (element->flag & STITCH_STITCHABLE) {
1493 BMLoop *l;
1494 float *luv;
1495
1496 l = element->l;
1497 luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
1498
1499 /* accumulate each islands' translation from stitchable elements.
1500 * it is important to do here because in final pass MTFaces
1501 * get modified and result is zero. */
1502 island_stitch_data[element->island].translation[0] += final_position[i].uv[0] - luv[0];
1503 island_stitch_data[element->island].translation[1] += final_position[i].uv[1] - luv[1];
1504 island_stitch_data[element->island].medianPoint[0] += luv[0];
1505 island_stitch_data[element->island].medianPoint[1] += luv[1];
1506 island_stitch_data[element->island].numOfElements++;
1507 }
1508 }
1509
1510 for (i = 0; i < state->selection_size; i++) {
1511 UvEdge *edge = static_cast<UvEdge *>(state->selection_stack[i]);
1512
1513 if (edge->flag & STITCH_STITCHABLE) {
1515 cd_loop_uv_offset, edge, ssc, state, final_position, nullptr, island_stitch_data);
1516 island_stitch_data[state->uvs[edge->uv1]->island].use_edge_rotation = true;
1517 }
1518 }
1519
1520 /* clear seams of stitched edges */
1521 if (final && ssc->clear_seams) {
1522 for (i = 0; i < state->selection_size; i++) {
1523 UvEdge *edge = static_cast<UvEdge *>(state->selection_stack[i]);
1524 if (edge->flag & STITCH_STITCHABLE) {
1525 BM_elem_flag_disable(edge->element->l->e, BM_ELEM_SEAM);
1526 }
1527 }
1528 }
1529 }
1530 }
1531
1532 /* third pass, propagate changes to coincident uvs */
1533 for (i = 0; i < state->selection_size; i++) {
1534 if (ssc->mode == STITCH_VERT) {
1535 UvElement *element = static_cast<UvElement *>(state->selection_stack[i]);
1536
1538 scene, element, i, preview_position, final_position, ssc, state, final);
1539 }
1540 else {
1541 UvEdge *edge = static_cast<UvEdge *>(state->selection_stack[i]);
1542
1544 state->uvs[edge->uv1],
1545 edge->uv1,
1546 preview_position,
1547 final_position,
1548 ssc,
1549 state,
1550 final);
1552 state->uvs[edge->uv2],
1553 edge->uv2,
1554 preview_position,
1555 final_position,
1556 ssc,
1557 state,
1558 final);
1559
1560 edge->flag &= (STITCH_SELECTED | STITCH_BOUNDARY);
1561 }
1562 }
1563
1564 /* final pass, calculate Island translation/rotation if needed */
1565 if (ssc->snap_islands) {
1567 cd_loop_uv_offset, state, preview_position, preview, island_stitch_data, final);
1568 }
1569
1570 MEM_delete(final_position);
1571 if (ssc->mode == STITCH_VERT) {
1572 MEM_delete(uvfinal_map);
1573 }
1574 MEM_delete(island_stitch_data);
1575 MEM_delete(preview_position);
1576
1577 return 1;
1578}
1579
1580static int stitch_process_data_all(StitchStateContainer *ssc, const Scene *scene, int final)
1581{
1582 for (uint ob_index = 0; ob_index < ssc->objects_len; ob_index++) {
1583 if (!stitch_process_data(ssc, ssc->states[ob_index], scene, final)) {
1584 return 0;
1585 }
1586 }
1587
1588 return 1;
1589}
1590
1591/* Stitch hash initialization functions */
1592static uint uv_edge_hash(const void *key)
1593{
1594 const UvEdge *edge = static_cast<const UvEdge *>(key);
1595 BLI_assert(edge->uv1 < edge->uv2);
1596 return (BLI_ghashutil_uinthash(edge->uv2) + BLI_ghashutil_uinthash(edge->uv1));
1597}
1598
1599static bool uv_edge_compare(const void *a, const void *b)
1600{
1601 const UvEdge *edge1 = static_cast<const UvEdge *>(a);
1602 const UvEdge *edge2 = static_cast<const UvEdge *>(b);
1603 BLI_assert(edge1->uv1 < edge1->uv2);
1604 BLI_assert(edge2->uv1 < edge2->uv2);
1605
1606 if ((edge1->uv1 == edge2->uv1) && (edge1->uv2 == edge2->uv2)) {
1607 return false;
1608 }
1609 return true;
1610}
1611
1612/* select all common edges */
1613static void stitch_select_edge(UvEdge *edge, StitchState *state, int always_select)
1614{
1615 UvEdge *eiter;
1616 UvEdge **selection_stack = reinterpret_cast<UvEdge **>(state->selection_stack);
1617
1618 for (eiter = edge->first; eiter; eiter = eiter->next) {
1619 if (eiter->flag & STITCH_SELECTED) {
1620 int i;
1621 if (always_select) {
1622 continue;
1623 }
1624
1625 eiter->flag &= ~STITCH_SELECTED;
1626 for (i = 0; i < state->selection_size; i++) {
1627 if (selection_stack[i] == eiter) {
1628 (state->selection_size)--;
1629 selection_stack[i] = selection_stack[state->selection_size];
1630 break;
1631 }
1632 }
1633 }
1634 else {
1635 eiter->flag |= STITCH_SELECTED;
1636 selection_stack[state->selection_size++] = eiter;
1637 }
1638 }
1639}
1640
1641/* Select all common uvs */
1642static void stitch_select_uv(UvElement *element, StitchState *state, int always_select)
1643{
1644 UvElement **selection_stack = reinterpret_cast<UvElement **>(state->selection_stack);
1645 UvElement *element_iter = BM_uv_element_get_head(state->element_map, element);
1646 /* first deselect all common uvs */
1647 for (; element_iter; element_iter = element_iter->next) {
1648 if (element_iter->separate) {
1649 /* only separators go to selection */
1650 if (element_iter->flag & STITCH_SELECTED) {
1651 int i;
1652 if (always_select) {
1653 continue;
1654 }
1655
1656 element_iter->flag &= ~STITCH_SELECTED;
1657 for (i = 0; i < state->selection_size; i++) {
1658 if (selection_stack[i] == element_iter) {
1659 (state->selection_size)--;
1660 selection_stack[i] = selection_stack[state->selection_size];
1661 break;
1662 }
1663 }
1664 }
1665 else {
1666 element_iter->flag |= STITCH_SELECTED;
1667 selection_stack[state->selection_size++] = element_iter;
1668 }
1669 }
1670 }
1671}
1672
1673static void stitch_set_selection_mode(StitchState *state, const char from_stitch_mode)
1674{
1675 void **old_selection_stack = state->selection_stack;
1676 int old_selection_size = state->selection_size;
1677 state->selection_size = 0;
1678
1679 if (from_stitch_mode == STITCH_VERT) {
1680 int i;
1681 state->selection_stack = MEM_new_array_uninitialized<void *>(
1682 state->total_separate_edges, "stitch_new_edge_selection_stack");
1683
1684 /* check if both elements of an edge are selected */
1685 for (i = 0; i < state->total_separate_edges; i++) {
1686 UvEdge *edge = state->edges + i;
1687 UvElement *element1 = state->uvs[edge->uv1];
1688 UvElement *element2 = state->uvs[edge->uv2];
1689
1690 if ((element1->flag & STITCH_SELECTED) && (element2->flag & STITCH_SELECTED)) {
1691 stitch_select_edge(edge, state, true);
1692 }
1693 }
1694
1695 /* unselect selected uvelements */
1696 for (i = 0; i < old_selection_size; i++) {
1697 UvElement *element = static_cast<UvElement *>(old_selection_stack[i]);
1698
1699 element->flag &= ~STITCH_SELECTED;
1700 }
1701 }
1702 else {
1703 int i;
1704 state->selection_stack = MEM_new_array_uninitialized<void *>(
1705 state->total_separate_uvs, "stitch_new_vert_selection_stack");
1706
1707 for (i = 0; i < old_selection_size; i++) {
1708 UvEdge *edge = static_cast<UvEdge *>(old_selection_stack[i]);
1709 UvElement *element1 = state->uvs[edge->uv1];
1710 UvElement *element2 = state->uvs[edge->uv2];
1711
1712 stitch_select_uv(element1, state, true);
1713 stitch_select_uv(element2, state, true);
1714
1715 edge->flag &= ~STITCH_SELECTED;
1716 }
1717 }
1718 MEM_delete(old_selection_stack);
1719}
1720
1721static void stitch_switch_selection_mode_all(StitchStateContainer *ssc)
1722{
1723 for (uint ob_index = 0; ob_index < ssc->objects_len; ob_index++) {
1724 stitch_set_selection_mode(ssc->states[ob_index], ssc->mode);
1725 }
1726
1727 if (ssc->mode == STITCH_VERT) {
1728 ssc->mode = STITCH_EDGE;
1729 }
1730 else {
1731 ssc->mode = STITCH_VERT;
1732 }
1733}
1734
1735static void stitch_calculate_edge_normal(const int cd_loop_uv_offset,
1736 UvEdge *edge,
1737 float *normal,
1738 float aspect)
1739{
1740 BMLoop *l1 = edge->element->l;
1741 float tangent[2];
1742
1743 float *luv1 = BM_ELEM_CD_GET_FLOAT_P(l1, cd_loop_uv_offset);
1744 float *luv2 = BM_ELEM_CD_GET_FLOAT_P(l1->next, cd_loop_uv_offset);
1745
1746 sub_v2_v2v2(tangent, luv2, luv1);
1747
1748 tangent[1] /= aspect;
1749
1750 normal[0] = tangent[1];
1751 normal[1] = -tangent[0];
1752
1753 normalize_v2(normal);
1754}
1755
1758static void stitch_draw_vbo(gpu::VertBuf *vbo, GPUPrimType prim_type, const float col[4])
1759{
1760 gpu::Batch *batch = GPU_batch_create_ex(prim_type, vbo, nullptr, GPU_BATCH_OWNS_VBO);
1762 GPU_batch_uniform_4fv(batch, "color", col);
1763 GPU_batch_draw(batch);
1764 GPU_batch_discard(batch);
1765}
1766
1767/* TODO: make things prettier : store batches inside StitchPreviewer instead of the bare verts pos
1768 */
1769static void stitch_draw(const bContext * /*C*/, ARegion * /*region*/, void *arg)
1770{
1771
1772 StitchStateContainer *ssc = static_cast<StitchStateContainer *>(arg);
1773
1774 for (uint ob_index = 0; ob_index < ssc->objects_len; ob_index++) {
1775 int j, index = 0;
1776 uint num_line = 0, num_tri, tri_idx = 0, line_idx = 0;
1777 StitchState *state = ssc->states[ob_index];
1778 StitchPreviewer *stitch_preview = state->stitch_preview;
1779 gpu::VertBuf *vbo, *vbo_line;
1780 float col[4];
1781
1782 static GPUVertFormat format = {0};
1783 static uint pos_id;
1784 if (format.attr_len == 0) {
1785 pos_id = GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32);
1786 }
1787
1789
1790 /* Static Triangles. */
1791 if (stitch_preview->static_tris) {
1793 vbo = GPU_vertbuf_create_with_format(format);
1794 GPU_vertbuf_data_alloc(*vbo, stitch_preview->num_static_tris * 3);
1795 for (int i = 0; i < stitch_preview->num_static_tris * 3; i++) {
1796 GPU_vertbuf_attr_set(vbo, pos_id, i, &stitch_preview->static_tris[i * 2]);
1797 }
1798 stitch_draw_vbo(vbo, GPU_PRIM_TRIS, col);
1799 }
1800
1801 /* Preview Polys */
1802 if (stitch_preview->preview_polys) {
1803 for (int i = 0; i < stitch_preview->num_polys; i++) {
1804 num_line += stitch_preview->uvs_per_polygon[i];
1805 }
1806
1807 num_tri = num_line - 2 * stitch_preview->num_polys;
1808
1809 /* we need to convert the polys into triangles / lines */
1810 vbo = GPU_vertbuf_create_with_format(format);
1811 vbo_line = GPU_vertbuf_create_with_format(format);
1812
1813 GPU_vertbuf_data_alloc(*vbo, num_tri * 3);
1814 GPU_vertbuf_data_alloc(*vbo_line, num_line * 2);
1815
1816 for (int i = 0; i < stitch_preview->num_polys; i++) {
1817 BLI_assert(stitch_preview->uvs_per_polygon[i] >= 3);
1818
1819 /* Start line */
1820 GPU_vertbuf_attr_set(vbo_line, pos_id, line_idx++, &stitch_preview->preview_polys[index]);
1822 vbo_line, pos_id, line_idx++, &stitch_preview->preview_polys[index + 2]);
1823
1824 for (j = 1; j < stitch_preview->uvs_per_polygon[i] - 1; j++) {
1825 GPU_vertbuf_attr_set(vbo, pos_id, tri_idx++, &stitch_preview->preview_polys[index]);
1827 vbo, pos_id, tri_idx++, &stitch_preview->preview_polys[index + (j + 0) * 2]);
1829 vbo, pos_id, tri_idx++, &stitch_preview->preview_polys[index + (j + 1) * 2]);
1830
1832 vbo_line, pos_id, line_idx++, &stitch_preview->preview_polys[index + (j + 0) * 2]);
1834 vbo_line, pos_id, line_idx++, &stitch_preview->preview_polys[index + (j + 1) * 2]);
1835 }
1836
1837 /* Closing line */
1838 GPU_vertbuf_attr_set(vbo_line, pos_id, line_idx++, &stitch_preview->preview_polys[index]);
1839 /* `j = uvs_per_polygon[i] - 1` */
1841 vbo_line, pos_id, line_idx++, &stitch_preview->preview_polys[index + j * 2]);
1842
1843 index += stitch_preview->uvs_per_polygon[i] * 2;
1844 }
1845
1847 stitch_draw_vbo(vbo, GPU_PRIM_TRIS, col);
1849 stitch_draw_vbo(vbo_line, GPU_PRIM_LINES, col);
1850 }
1851
1853
1854 /* draw stitch vert/lines preview */
1855 if (ssc->mode == STITCH_VERT) {
1857
1859 vbo = GPU_vertbuf_create_with_format(format);
1860 GPU_vertbuf_data_alloc(*vbo, stitch_preview->num_stitchable);
1861 for (int i = 0; i < stitch_preview->num_stitchable; i++) {
1862 GPU_vertbuf_attr_set(vbo, pos_id, i, &stitch_preview->preview_stitchable[i * 2]);
1863 }
1865
1867 vbo = GPU_vertbuf_create_with_format(format);
1868 GPU_vertbuf_data_alloc(*vbo, stitch_preview->num_unstitchable);
1869 for (int i = 0; i < stitch_preview->num_unstitchable; i++) {
1870 GPU_vertbuf_attr_set(vbo, pos_id, i, &stitch_preview->preview_unstitchable[i * 2]);
1871 }
1873 }
1874 else {
1876 vbo = GPU_vertbuf_create_with_format(format);
1877 GPU_vertbuf_data_alloc(*vbo, stitch_preview->num_stitchable * 2);
1878 for (int i = 0; i < stitch_preview->num_stitchable * 2; i++) {
1879 GPU_vertbuf_attr_set(vbo, pos_id, i, &stitch_preview->preview_stitchable[i * 2]);
1880 }
1882
1884 vbo = GPU_vertbuf_create_with_format(format);
1885 GPU_vertbuf_data_alloc(*vbo, stitch_preview->num_unstitchable * 2);
1886 for (int i = 0; i < stitch_preview->num_unstitchable * 2; i++) {
1887 GPU_vertbuf_attr_set(vbo, pos_id, i, &stitch_preview->preview_unstitchable[i * 2]);
1888 }
1890 }
1891 }
1892}
1893
1894static UvEdge *uv_edge_get(BMLoop *l, StitchState *state)
1895{
1896 UvEdge tmp_edge;
1897
1898 UvElement *element1 = BM_uv_element_get(state->element_map, l);
1899 UvElement *element2 = BM_uv_element_get(state->element_map, l->next);
1900
1901 if (!element1 || !element2) {
1902 return nullptr;
1903 }
1904
1905 int uv1 = state->map[element1 - state->element_map->storage];
1906 int uv2 = state->map[element2 - state->element_map->storage];
1907
1908 if (uv1 < uv2) {
1909 tmp_edge.uv1 = uv1;
1910 tmp_edge.uv2 = uv2;
1911 }
1912 else {
1913 tmp_edge.uv1 = uv2;
1914 tmp_edge.uv2 = uv1;
1915 }
1916
1917 return static_cast<UvEdge *>(BLI_ghash_lookup(state->edge_hash, &tmp_edge));
1918}
1919
1920static StitchState *stitch_init(const Scene *scene,
1921 StitchStateContainer *ssc,
1922 Object *obedit,
1923 const StitchModes stored_mode,
1924 const Span<UvElementID> to_select)
1925{
1926 /* for fast edge lookup... */
1927 GHash *edge_hash;
1928 /* ...and actual edge storage */
1929 UvEdge *edges;
1930 int total_edges;
1931 /* maps uvelements to their first coincident uv */
1932 int *map;
1933 BMFace *efa;
1934 BMLoop *l;
1935 BMIter iter, liter;
1936 GHashIterator gh_iter;
1937 UvEdge *all_edges;
1938 StitchState *state;
1939 ToolSettings *ts = scene->toolsettings;
1940
1942 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
1943
1944 state = MEM_new_zeroed<StitchState>("stitch state obj");
1945
1946 /* initialize state */
1947 state->obedit = obedit;
1948
1949 uvedit_select_prepare(scene, bm);
1950
1951 /* Workaround for sync-select & face-select mode which implies all selected faces are detached,
1952 * for stitch this isn't useful behavior, see #86924. */
1953 const int selectmode_orig = scene->toolsettings->selectmode;
1955 state->element_map = BM_uv_element_map_create(bm, scene, false, true, true, true);
1956 scene->toolsettings->selectmode = selectmode_orig;
1957
1958 if (!state->element_map) {
1960 return nullptr;
1961 }
1962
1963 state->aspect = ED_uvedit_get_aspect_y(obedit);
1964
1965 /* Mark islands that have at least one selected UV edge as selected.
1966 * Indexed by this object's island index, so it must be allocated per-object. */
1967 if (ssc->only_selected_uvs) {
1968 state->orig_bounds = MEM_new<StitchStateOrigBounds>("stitch state orig bounds");
1969 state->orig_bounds->island_has_selected.resize(state->element_map->total_islands, false);
1970 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
1971 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
1972 if (uvedit_uv_select_test(scene, bm, l, offsets) &&
1973 uvedit_uv_select_test(scene, bm, l->next, offsets))
1974 {
1975 UvElement *element = BM_uv_element_get(state->element_map, l);
1976 if (element) {
1977 state->orig_bounds->island_has_selected[element->island] = true;
1978 }
1979 }
1980 }
1981 }
1982 }
1983 int unique_uvs = state->element_map->total_unique_uvs;
1984 state->total_separate_uvs = unique_uvs;
1985
1986 /* Allocate the unique uv buffers */
1987 state->uvs = MEM_new_array_uninitialized<UvElement *>(unique_uvs, "uv_stitch_unique_uvs");
1988 /* internal uvs need no normals but it is hard and slow to keep a map of
1989 * normals only for boundary uvs, so allocating for all uvs.
1990 * Times 2 because each `float[2]` is stored as `{n[2 * i], n[2*i + 1]}`. */
1991 state->normals = MEM_new_array_zeroed<float>(2 * unique_uvs, "uv_stitch_normals");
1992 state->map = map = MEM_new_array_uninitialized<int>(state->element_map->total_uvs,
1993 "uv_stitch_unique_map");
1994 /* Allocate the edge stack */
1995 edge_hash = BLI_ghash_new(uv_edge_hash, uv_edge_compare, "stitch_edge_hash");
1996 all_edges = MEM_new_array_uninitialized<UvEdge>(state->element_map->total_uvs, "ssc_edges");
1997
1998 BLI_assert(!state->stitch_preview); /* Paranoia. */
1999 if (!state->uvs || !map || !edge_hash || !all_edges) {
2001 return nullptr;
2002 }
2003
2004 /* Index for the UvElements. */
2005 int counter = -1;
2006 /* initialize the unique UVs and map */
2007 for (int i = 0; i < bm->totvert; i++) {
2008 UvElement *element = state->element_map->vertex[i];
2009 for (; element; element = element->next) {
2010 if (element->separate) {
2011 counter++;
2012 state->uvs[counter] = element;
2013 }
2014 /* Pointer arithmetic to the rescue, as always :). */
2015 map[element - state->element_map->storage] = counter;
2016 }
2017 }
2018
2019 counter = 0;
2020 /* Now, on to generate our uv connectivity data */
2021 const bool face_selected = !(ts->uv_flag & UV_FLAG_SELECT_SYNC);
2022 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2024 continue;
2025 }
2026 if (face_selected && !BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2027 continue;
2028 }
2029
2030 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2031 UvElement *element = BM_uv_element_get(state->element_map, l);
2032 int itmp1 = element - state->element_map->storage;
2033 int itmp2 = BM_uv_element_get(state->element_map, l->next) - state->element_map->storage;
2034 UvEdge *edge;
2035
2036 int offset1 = map[itmp1];
2037 int offset2 = map[itmp2];
2038
2039 all_edges[counter].next = nullptr;
2040 all_edges[counter].first = nullptr;
2041 all_edges[counter].flag = 0;
2042 all_edges[counter].element = element;
2043 /* Using an order policy, sort UVs according to address space.
2044 * This avoids having two different UvEdges with the same UVs on different positions. */
2045 if (offset1 < offset2) {
2046 all_edges[counter].uv1 = offset1;
2047 all_edges[counter].uv2 = offset2;
2048 }
2049 else {
2050 all_edges[counter].uv1 = offset2;
2051 all_edges[counter].uv2 = offset1;
2052 }
2053
2054 edge = static_cast<UvEdge *>(BLI_ghash_lookup(edge_hash, &all_edges[counter]));
2055 if (edge) {
2056 edge->flag = 0;
2057 }
2058 else {
2059 BLI_ghash_insert(edge_hash, &all_edges[counter], &all_edges[counter]);
2060 all_edges[counter].flag = STITCH_BOUNDARY;
2061 }
2062 counter++;
2063 }
2064 }
2065
2066 total_edges = BLI_ghash_len(edge_hash);
2067 state->edges = edges = MEM_new_array_uninitialized<UvEdge>(total_edges, "stitch_edges");
2068
2069 /* I assume any system will be able to at least allocate an iterator :p */
2070 if (!edges) {
2072 return nullptr;
2073 }
2074
2075 state->total_separate_edges = total_edges;
2076
2077 /* fill the edges with data */
2078 int i = 0;
2079 GHASH_ITER (gh_iter, edge_hash) {
2080 edges[i++] = *(static_cast<UvEdge *>(BLI_ghashIterator_getKey(&gh_iter)));
2081 }
2082
2083 /* cleanup temporary stuff */
2084 MEM_delete(all_edges);
2085
2086 BLI_ghash_free(edge_hash, nullptr, nullptr);
2087
2088 /* Refill an edge hash to create edge connectivity data. */
2089 state->edge_hash = edge_hash = BLI_ghash_new(uv_edge_hash, uv_edge_compare, "stitch_edge_hash");
2090 for (i = 0; i < total_edges; i++) {
2091 BLI_ghash_insert(edge_hash, edges + i, edges + i);
2092 }
2094
2095 /***** calculate 2D normals for boundary uvs *****/
2096
2097 /* we use boundary edges to calculate 2D normals.
2098 * to disambiguate the direction of the normal, we also need
2099 * a point "inside" the island, that can be provided by
2100 * the winding of the face (assuming counter-clockwise flow). */
2101
2102 for (i = 0; i < total_edges; i++) {
2103 UvEdge *edge = edges + i;
2104 float normal[2];
2105 if (edge->flag & STITCH_BOUNDARY) {
2106 stitch_calculate_edge_normal(offsets.uv, edge, normal, state->aspect);
2107
2108 add_v2_v2(state->normals + edge->uv1 * 2, normal);
2109 add_v2_v2(state->normals + edge->uv2 * 2, normal);
2110
2111 normalize_v2(state->normals + edge->uv1 * 2);
2112 normalize_v2(state->normals + edge->uv2 * 2);
2113 }
2114 }
2115
2116 /***** fill selection stack *******/
2117
2118 state->selection_size = 0;
2119
2120 /* Restore the operator's own (modal-edited) stitch selection on redo; it is
2121 * not the mesh UV selection, so it can't be re-derived from the mesh. */
2122 if (!to_select.is_empty()) {
2123 int faceIndex, elementIndex;
2125
2127
2128 int selected_count = to_select.size();
2129
2130 if (stored_mode == STITCH_VERT) {
2131 state->selection_stack = MEM_new_array_uninitialized<void *>(state->total_separate_uvs,
2132 "uv_stitch_selection_stack");
2133
2134 while (selected_count--) {
2135 faceIndex = to_select[selected_count].faceIndex;
2136 elementIndex = to_select[selected_count].elementIndex;
2137 efa = BM_face_at_index(bm, faceIndex);
2139 state->element_map,
2140 static_cast<BMLoop *>(BM_iter_at_index(nullptr, BM_LOOPS_OF_FACE, efa, elementIndex)));
2142 }
2143 }
2144 else {
2145 state->selection_stack = MEM_new_array_uninitialized<void *>(state->total_separate_edges,
2146 "uv_stitch_selection_stack");
2147
2148 while (selected_count--) {
2149 UvEdge tmp_edge, *edge;
2150 int uv1, uv2;
2151 faceIndex = to_select[selected_count].faceIndex;
2152 elementIndex = to_select[selected_count].elementIndex;
2153 efa = BM_face_at_index(bm, faceIndex);
2155 state->element_map,
2156 static_cast<BMLoop *>(BM_iter_at_index(nullptr, BM_LOOPS_OF_FACE, efa, elementIndex)));
2157 uv1 = map[element - state->element_map->storage];
2158
2160 state->element_map,
2161 static_cast<BMLoop *>(
2162 BM_iter_at_index(nullptr, BM_LOOPS_OF_FACE, efa, (elementIndex + 1) % efa->len)));
2163 uv2 = map[element - state->element_map->storage];
2164
2165 if (uv1 < uv2) {
2166 tmp_edge.uv1 = uv1;
2167 tmp_edge.uv2 = uv2;
2168 }
2169 else {
2170 tmp_edge.uv1 = uv2;
2171 tmp_edge.uv2 = uv1;
2172 }
2173
2174 edge = static_cast<UvEdge *>(BLI_ghash_lookup(edge_hash, &tmp_edge));
2175
2176 stitch_select_edge(edge, state, true);
2177 }
2178 }
2179 /* if user has switched the operator mode after operation, we need to convert
2180 * the stored format */
2181 if (ssc->mode != stored_mode) {
2182 stitch_set_selection_mode(state, stored_mode);
2183 }
2184 }
2185 else {
2186 if (ssc->mode == STITCH_VERT) {
2187 state->selection_stack = MEM_new_array_uninitialized<void *>(state->total_separate_uvs,
2188 "uv_stitch_selection_stack");
2189
2190 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2191 BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
2192 if (uvedit_uv_select_test(scene, bm, l, offsets)) {
2193 UvElement *element = BM_uv_element_get(state->element_map, l);
2194 if (element) {
2196 }
2197 }
2198 }
2199 }
2200 }
2201 else {
2202 state->selection_stack = MEM_new_array_uninitialized<void *>(state->total_separate_edges,
2203 "uv_stitch_selection_stack");
2204
2205 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2206 if (!(ts->uv_flag & UV_FLAG_SELECT_SYNC) &&
2208 {
2209 continue;
2210 }
2211
2212 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2213 if (uvedit_edge_select_test(scene, bm, l, offsets)) {
2214 UvEdge *edge = uv_edge_get(l, state);
2215 if (edge) {
2216 stitch_select_edge(edge, state, true);
2217 }
2218 }
2219 }
2220 }
2221 }
2222 }
2223
2224 /***** initialize static island preview data *****/
2225
2226 state->tris_per_island = MEM_new_array_uninitialized<uint>(state->element_map->total_islands,
2227 "stitch island tris");
2228 for (i = 0; i < state->element_map->total_islands; i++) {
2229 state->tris_per_island[i] = 0;
2230 }
2231
2232 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2234
2235 if (element) {
2236 state->tris_per_island[element->island] += (efa->len > 2) ? efa->len - 2 : 0;
2237 }
2238 }
2239
2240 state->island_is_stitchable = MEM_new_array_zeroed<bool>(state->element_map->total_islands,
2241 "stitch I stops");
2242 if (!state->island_is_stitchable) {
2244 return nullptr;
2245 }
2246
2247 if (!stitch_process_data(ssc, state, scene, false)) {
2249 return nullptr;
2250 }
2251
2252 return state;
2253}
2254
2255static bool goto_next_island(StitchStateContainer *ssc)
2256{
2257 StitchState *active_state = ssc->states[ssc->active_object_index];
2258 StitchState *original_active_state = active_state;
2259
2260 int original_island = ssc->static_island;
2261
2262 do {
2263 ssc->static_island++;
2264 if (ssc->static_island >= active_state->element_map->total_islands) {
2265 /* go to next object */
2266 ssc->active_object_index++;
2267 ssc->active_object_index %= ssc->objects_len;
2268
2269 active_state = ssc->states[ssc->active_object_index];
2270 ssc->static_island = 0;
2271 }
2272
2273 if (active_state->island_is_stitchable[ssc->static_island]) {
2274 /* We're at an island to make active */
2275 return true;
2276 }
2277 } while (!(active_state == original_active_state && ssc->static_island == original_island));
2278
2279 return false;
2280}
2281
2282static StitchStateInit stitch_extract_rna_selection(wmOperator *op, const Span<Object *> objects)
2283{
2284 StitchStateInit state_init = {};
2285
2286 /* Retrieve list of selected UVs, one list contains all selected UVs
2287 * for all objects. */
2288 state_init.objs_selection_count = MEM_new_array_zeroed<int>(objects.size(),
2289 "objects_selection_count");
2290 RNA_int_get_array(op->ptr, "objects_selection_count", state_init.objs_selection_count);
2291
2292 int total_selected = 0;
2293 for (uint ob_index = 0; ob_index < objects.size(); ob_index++) {
2294 total_selected += state_init.objs_selection_count[ob_index];
2295 }
2296
2297 state_init.selected_uvs = MEM_new_array_zeroed<UvElementID>(total_selected, "selected_uvs_arr");
2298 int sel_idx = 0;
2299 RNA_BEGIN (op->ptr, itemptr, "selection") {
2300 BLI_assert(sel_idx < total_selected);
2301 state_init.selected_uvs[sel_idx].faceIndex = RNA_int_get(&itemptr, "face_index");
2302 state_init.selected_uvs[sel_idx].elementIndex = RNA_int_get(&itemptr, "element_index");
2303 sel_idx++;
2304 }
2305 RNA_END;
2306
2307 RNA_collection_clear(op->ptr, "selection");
2308
2309 return state_init;
2310}
2311
2312static StitchStateContainer *stitch_settings_init_for_operator(const Span<Object *> objects,
2313 Scene *scene,
2314 wmOperator *op)
2315{
2316 if (objects.is_empty()) {
2317 BKE_report(op->reports, RPT_ERROR, "No objects selected");
2318 return nullptr;
2319 }
2320
2321 if (objects.size() > RNA_MAX_ARRAY_LENGTH) {
2322 BKE_reportf(op->reports,
2323 RPT_ERROR,
2324 "Stitching only works with less than %i objects selected (%i selected)",
2326 int(objects.size()));
2327 return nullptr;
2328 }
2329
2330 StitchStateContainer *ssc = MEM_new<StitchStateContainer>("stitch collection");
2331 ToolSettings *ts = scene->toolsettings;
2332
2333 ssc->use_limit = RNA_boolean_get(op->ptr, "use_limit");
2334 ssc->limit_dist = RNA_float_get(op->ptr, "limit");
2335 ssc->snap_islands = RNA_boolean_get(op->ptr, "snap_islands");
2336 ssc->midpoints = RNA_boolean_get(op->ptr, "midpoint_snap");
2337 ssc->clear_seams = RNA_boolean_get(op->ptr, "clear_seams");
2338 ssc->active_object_index = RNA_int_get(op->ptr, "active_object_index");
2339 ssc->static_island = RNA_int_get(op->ptr, "static_island");
2340 ssc->ignore_seam_boundary = false;
2341 ssc->only_selected_uvs = false;
2342 if (RNA_struct_property_is_set(op->ptr, "mode")) {
2343 ssc->mode = RNA_enum_get(op->ptr, "mode");
2344 }
2345 else {
2346 if (ts->uv_flag & UV_FLAG_SELECT_SYNC) {
2347 if (ts->selectmode & SCE_SELECT_VERTEX) {
2348 ssc->mode = STITCH_VERT;
2349 }
2350 else {
2351 ssc->mode = STITCH_EDGE;
2352 }
2353 }
2354 else {
2355 if (ts->uv_selectmode & UV_SELECT_VERT) {
2356 ssc->mode = STITCH_VERT;
2357 }
2358 else {
2359 ssc->mode = STITCH_EDGE;
2360 }
2361 }
2362 }
2363
2364 return ssc;
2365}
2366
2372static StitchStateContainer *stitch_settings_init_for_original_bounds()
2373{
2374 StitchStateContainer *ssc = MEM_new<StitchStateContainer>("stitch collection");
2375 ssc->use_limit = false;
2376 ssc->snap_islands = true;
2377 ssc->midpoints = false;
2378 ssc->clear_seams = false;
2379 ssc->mode = STITCH_VERT;
2380 ssc->only_selected_uvs = true;
2381 ssc->ignore_seam_boundary = true;
2382 return ssc;
2383}
2384
2385static int stitch_init_all(const Scene *scene,
2386 Span<Object *> objects,
2387 ARegion *region,
2388 StitchStateContainer *ssc,
2389 const StitchModes stored_mode,
2390 wmOperator *op)
2391{
2392 if (objects.is_empty() || objects.size() > RNA_MAX_ARRAY_LENGTH) {
2393 return 0;
2394 }
2395
2396 /* Selection to restore when redoing the operator. Extracted here, where the
2397 * object list and the loop that consumes it live; null `op` (e.g. unwrap's
2398 * internal stitch) means nothing to restore. */
2399 StitchStateInit state_init = {};
2400 if (op && RNA_struct_property_is_set(op->ptr, "selection") &&
2401 RNA_struct_property_is_set(op->ptr, "objects_selection_count"))
2402 {
2403 state_init = stitch_extract_rna_selection(op, objects);
2404 }
2405
2406 ssc->objects = MEM_new_array_zeroed<Object *>(objects.size(), "Object *ssc->objects");
2407 ssc->states = MEM_new_array_zeroed<StitchState *>(objects.size(), "StitchState");
2408 ssc->objects_len = 0;
2409
2410 /* Cursor into the flat stored-selection list, advanced one object at a time. */
2411 int selection_offset = 0;
2412 for (uint ob_index = 0; ob_index < objects.size(); ob_index++) {
2413 Object *obedit = objects[ob_index];
2414
2415 Span<UvElementID> to_select;
2416 if (state_init.objs_selection_count) {
2417 const int selected_count = state_init.objs_selection_count[ob_index];
2418 to_select = Span<UvElementID>(state_init.selected_uvs + selection_offset, selected_count);
2419 selection_offset += selected_count;
2420 }
2421 StitchState *stitch_state_ob = stitch_init(scene, ssc, obedit, stored_mode, to_select);
2422
2423 if (stitch_state_ob) {
2424 ssc->objects[ssc->objects_len] = obedit;
2425 ssc->states[ssc->objects_len] = stitch_state_ob;
2426 ssc->objects_len++;
2427 }
2428 }
2429
2430 /* The per-object loop has consumed it; nothing below needs the selection. */
2431 stitch_state_init_free(&state_init);
2432
2433 if (ssc->objects_len == 0) {
2434 /* No object could be initialized for stitching (e.g. all faces hidden). */
2435 MEM_SAFE_DELETE(ssc->states);
2436 MEM_SAFE_DELETE(ssc->objects);
2437 return 0;
2438 }
2439
2440 ssc->active_object_index %= ssc->objects_len;
2441
2442 StitchState *state = ssc->states[ssc->active_object_index];
2443 ssc->static_island %= state->element_map->total_islands;
2444
2445 /* If the initial active object doesn't have any stitchable islands
2446 * then no active island will be seen in the UI.
2447 * Make sure we're on a stitchable object and island. */
2448 if (!state->island_is_stitchable[ssc->static_island]) {
2449 goto_next_island(ssc);
2450 state = ssc->states[ssc->active_object_index];
2451 }
2452
2453 /* process active stitchobj again now that it can detect it's the active stitchobj */
2454 stitch_process_data(ssc, state, scene, false);
2455
2456 /* A region is only needed to draw the interactive preview overlay. The stitch
2457 * itself works without one; a null region (e.g. unwrap's internal stitch)
2458 * just skips the overlay, it is not an error. */
2459 if (region) {
2460 ssc->draw_handle = ED_region_draw_cb_activate(
2462 }
2463
2464 return 1;
2465}
2466
2468{
2469 StitchStateContainer *ssc = stitch_settings_init_for_original_bounds();
2470 /* Non-interactive: no region (no preview overlay) and no operator
2471 * (no stored selection to restore). */
2472 if (!stitch_init_all(scene, objects, nullptr, ssc, STITCH_VERT, nullptr)) {
2473 MEM_delete(ssc);
2474 return false;
2475 }
2476 stitch_process_data_all(ssc, scene, true);
2477 state_delete_all(ssc);
2478 return true;
2479}
2480
2487static StitchStateContainer *stitch_init_from_operator(bContext *C, wmOperator *op)
2488{
2489 Scene *scene = CTX_data_scene(C);
2490 const Vector<Object *> objects =
2493
2494 StitchStateContainer *ssc = stitch_settings_init_for_operator(objects, scene, op);
2495 op->customdata = ssc;
2496
2497 if (!ssc || !stitch_init_all(scene,
2498 objects,
2500 ssc,
2501 StitchModes(RNA_enum_get(op->ptr, "stored_mode")),
2502 op))
2503 {
2504 MEM_SAFE_DELETE(ssc);
2505 op->customdata = nullptr;
2506 return nullptr;
2507 }
2508 stitch_update_header(ssc, C);
2509 return ssc;
2510}
2511
2513{
2514 Scene *scene = CTX_data_scene(C);
2515 StitchStateContainer *ssc = stitch_init_from_operator(C, op);
2516 if (!ssc) {
2517 return OPERATOR_CANCELLED;
2518 }
2519
2521
2522 ToolSettings *ts = scene->toolsettings;
2523 const bool synced_selection = (ts->uv_flag & UV_FLAG_SELECT_SYNC) != 0;
2524
2525 for (uint ob_index = 0; ob_index < ssc->objects_len; ob_index++) {
2526 StitchState *state = ssc->states[ob_index];
2527 Object *obedit = state->obedit;
2529 if (synced_selection && (bm->totvertsel == 0)) {
2530 continue;
2531 }
2532
2534 }
2535
2537}
2538
2539static void stitch_exit(bContext *C, wmOperator *op, int finished)
2540{
2541 Scene *scene = CTX_data_scene(C);
2543 ScrArea *area = CTX_wm_area(C);
2544
2545 StitchStateContainer *ssc = static_cast<StitchStateContainer *>(op->customdata);
2546
2547 if (finished) {
2548 RNA_float_set(op->ptr, "limit", ssc->limit_dist);
2549 RNA_boolean_set(op->ptr, "use_limit", ssc->use_limit);
2550 RNA_boolean_set(op->ptr, "snap_islands", ssc->snap_islands);
2551 RNA_boolean_set(op->ptr, "midpoint_snap", ssc->midpoints);
2552 RNA_boolean_set(op->ptr, "clear_seams", ssc->clear_seams);
2553 RNA_enum_set(op->ptr, "mode", ssc->mode);
2554 RNA_enum_set(op->ptr, "stored_mode", ssc->mode);
2555 RNA_int_set(op->ptr, "active_object_index", ssc->active_object_index);
2556
2557 RNA_int_set(op->ptr, "static_island", ssc->static_island);
2558
2559 int *objs_selection_count = MEM_new_array_uninitialized<int>(ssc->objects_len,
2560 "objects_selection_count");
2561
2562 /* Store selection for re-execution of stitch
2563 * - Store all selected UVs in "selection"
2564 * - Store how many each object has in "objects_selection_count". */
2565 RNA_collection_clear(op->ptr, "selection");
2566 for (uint ob_index = 0; ob_index < ssc->objects_len; ob_index++) {
2567 StitchState *state = ssc->states[ob_index];
2568 Object *obedit = state->obedit;
2569
2570 PointerRNA itemptr;
2571 for (int i = 0; i < state->selection_size; i++) {
2573
2574 if (ssc->mode == STITCH_VERT) {
2575 element = static_cast<UvElement *>(state->selection_stack[i]);
2576 }
2577 else {
2578 element = (static_cast<UvEdge *>(state->selection_stack[i]))->element;
2579 }
2580 RNA_collection_add(op->ptr, "selection", &itemptr);
2581
2582 RNA_int_set(&itemptr, "face_index", BM_elem_index_get(element->l->f));
2583 RNA_int_set(&itemptr, "element_index", element->loop_of_face_index);
2584 }
2585 uvedit_live_unwrap_update(sima, scene, obedit);
2586
2587 objs_selection_count[ob_index] = state->selection_size;
2588 }
2589
2590 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "objects_selection_count");
2591 RNA_def_property_array(prop, ssc->objects_len);
2592 RNA_int_set_array(op->ptr, "objects_selection_count", objs_selection_count);
2593 MEM_delete(objs_selection_count);
2594 }
2595
2596 if (area) {
2597 ED_workspace_status_text(C, nullptr);
2598 }
2599
2600 ED_region_draw_cb_exit(CTX_wm_region(C)->runtime->type, ssc->draw_handle);
2601
2602 ToolSettings *ts = scene->toolsettings;
2603 const bool synced_selection = (ts->uv_flag & UV_FLAG_SELECT_SYNC) != 0;
2604
2605 for (uint ob_index = 0; ob_index < ssc->objects_len; ob_index++) {
2606 StitchState *state = ssc->states[ob_index];
2607 Object *obedit = state->obedit;
2609 if (synced_selection && (bm->totvertsel == 0)) {
2610 continue;
2611 }
2612
2613 DEG_id_tag_update(obedit->data, 0);
2615 }
2616
2617 state_delete_all(ssc);
2618
2619 op->customdata = nullptr;
2620}
2621
2623{
2624 stitch_exit(C, op, 0);
2625}
2626
2628{
2629 Scene *scene = CTX_data_scene(C);
2630
2631 StitchStateContainer *ssc = stitch_init_from_operator(C, op);
2632 if (!ssc) {
2633 return OPERATOR_CANCELLED;
2634 }
2635 if (stitch_process_data_all(ssc, scene, 1)) {
2636 stitch_exit(C, op, 1);
2637 return OPERATOR_FINISHED;
2638 }
2639 stitch_cancel(C, op);
2640 return OPERATOR_CANCELLED;
2641}
2642
2643static StitchState *stitch_select(bContext *C,
2644 Scene *scene,
2645 const wmEvent *event,
2646 StitchStateContainer *ssc)
2647{
2648 /* add uv under mouse to processed uv's */
2649 float co[2];
2650 ARegion *region = CTX_wm_region(C);
2652
2653 ui::view2d_region_to_view(&region->v2d, event->mval[0], event->mval[1], &co[0], &co[1]);
2654
2655 if (ssc->mode == STITCH_VERT) {
2656 if (uv_find_nearest_vert_multi(scene, {ssc->objects, ssc->objects_len}, co, 0.0f, &hit)) {
2657 /* Add vertex to selection, deselect all common uv's of vert other than selected and
2658 * update the preview. This behavior was decided so that you can do stuff like deselect
2659 * the opposite stitchable vertex and the initial still gets deselected */
2660
2661 /* find StitchState from hit->ob */
2662 StitchState *state = nullptr;
2663 for (uint ob_index = 0; ob_index < ssc->objects_len; ob_index++) {
2664 if (hit.ob == ssc->objects[ob_index]) {
2665 state = ssc->states[ob_index];
2666 break;
2667 }
2668 }
2669
2670 /* This works due to setting of tmp in find nearest uv vert */
2671 UvElement *element = BM_uv_element_get(state->element_map, hit.l);
2672 if (element) {
2674 }
2675
2676 return state;
2677 }
2678 }
2679 else if (uv_find_nearest_edge_multi(scene, {ssc->objects, ssc->objects_len}, co, 0.0f, &hit)) {
2680 /* find StitchState from hit->ob */
2681 StitchState *state = nullptr;
2682 for (uint ob_index = 0; ob_index < ssc->objects_len; ob_index++) {
2683 if (hit.ob == ssc->objects[ob_index]) {
2684 state = ssc->states[ob_index];
2685 break;
2686 }
2687 }
2688
2689 UvEdge *edge = uv_edge_get(hit.l, state);
2690 stitch_select_edge(edge, state, false);
2691
2692 return state;
2693 }
2694
2695 return nullptr;
2696}
2697
2699{
2700 StitchStateContainer *ssc;
2701 Scene *scene = CTX_data_scene(C);
2702
2703 ssc = static_cast<StitchStateContainer *>(op->customdata);
2704 StitchState *active_state = ssc->states[ssc->active_object_index];
2705
2706 switch (event->type) {
2707 case MIDDLEMOUSE:
2708 return OPERATOR_PASS_THROUGH;
2709
2710 /* Cancel */
2711 case EVT_ESCKEY:
2712 stitch_cancel(C, op);
2713 return OPERATOR_CANCELLED;
2714
2715 case LEFTMOUSE:
2716 case EVT_PADENTER:
2717 case EVT_RETKEY:
2718 if (event->val == KM_PRESS) {
2719 if (stitch_process_data(ssc, active_state, scene, true)) {
2720 stitch_exit(C, op, 1);
2721 return OPERATOR_FINISHED;
2722 }
2723
2724 stitch_cancel(C, op);
2725 return OPERATOR_CANCELLED;
2726 }
2727 return OPERATOR_PASS_THROUGH;
2728
2729 /* Increase limit */
2730 case EVT_PADPLUSKEY:
2731 case WHEELUPMOUSE:
2732 if ((event->val == KM_PRESS) && (event->modifier & KM_ALT)) {
2733 ssc->limit_dist += 0.01f;
2734 if (!stitch_process_data(ssc, active_state, scene, false)) {
2735 stitch_cancel(C, op);
2736 return OPERATOR_CANCELLED;
2737 }
2738 break;
2739 }
2740 else {
2741 return OPERATOR_PASS_THROUGH;
2742 }
2743 /* Decrease limit */
2744 case EVT_PADMINUS:
2745 case WHEELDOWNMOUSE:
2746 if ((event->val == KM_PRESS) && (event->modifier & KM_ALT)) {
2747 ssc->limit_dist -= 0.01f;
2748 ssc->limit_dist = std::max(0.01f, ssc->limit_dist);
2749 if (!stitch_process_data(ssc, active_state, scene, false)) {
2750 stitch_cancel(C, op);
2751 return OPERATOR_CANCELLED;
2752 }
2753 break;
2754 }
2755 else {
2756 return OPERATOR_PASS_THROUGH;
2757 }
2758
2759 /* Use Limit (Default off) */
2760 case EVT_LKEY:
2761 if (event->val == KM_PRESS) {
2762 ssc->use_limit = !ssc->use_limit;
2763 if (!stitch_process_data(ssc, active_state, scene, false)) {
2764 stitch_cancel(C, op);
2765 return OPERATOR_CANCELLED;
2766 }
2767 break;
2768 }
2770
2771 case EVT_IKEY:
2772 if (event->val == KM_PRESS) {
2773 /* Move to next island and maybe next object */
2774
2775 if (goto_next_island(ssc)) {
2776 StitchState *new_active_state = ssc->states[ssc->active_object_index];
2777
2778 /* active_state is the original active state */
2779 if (active_state != new_active_state) {
2780 if (!stitch_process_data(ssc, active_state, scene, false)) {
2781 stitch_cancel(C, op);
2782 return OPERATOR_CANCELLED;
2783 }
2784 }
2785
2786 if (!stitch_process_data(ssc, new_active_state, scene, false)) {
2787 stitch_cancel(C, op);
2788 return OPERATOR_CANCELLED;
2789 }
2790 }
2791 break;
2792 }
2794
2795 case EVT_MKEY:
2796 if (event->val == KM_PRESS) {
2797 ssc->midpoints = !ssc->midpoints;
2798 if (!stitch_process_data(ssc, active_state, scene, false)) {
2799 stitch_cancel(C, op);
2800 return OPERATOR_CANCELLED;
2801 }
2802 }
2803 break;
2804
2805 /* Select geometry */
2806 case RIGHTMOUSE:
2807 if ((event->modifier & KM_SHIFT) == 0) {
2808 stitch_cancel(C, op);
2809 return OPERATOR_CANCELLED;
2810 }
2811 if (event->val == KM_PRESS) {
2812 StitchState *selected_state = stitch_select(C, scene, event, ssc);
2813
2814 if (selected_state && !stitch_process_data(ssc, selected_state, scene, false)) {
2815 stitch_cancel(C, op);
2816 return OPERATOR_CANCELLED;
2817 }
2818 break;
2819 }
2821
2822 /* snap islands on/off */
2823 case EVT_SKEY:
2824 if (event->val == KM_PRESS) {
2825 ssc->snap_islands = !ssc->snap_islands;
2826 if (!stitch_process_data(ssc, active_state, scene, false)) {
2827 stitch_cancel(C, op);
2828 return OPERATOR_CANCELLED;
2829 }
2830 break;
2831 }
2832 else {
2834 }
2835
2836 /* switch between edge/vertex mode */
2837 case EVT_TABKEY:
2838 if (event->val == KM_PRESS) {
2840
2841 if (!stitch_process_data_all(ssc, scene, false)) {
2842 stitch_cancel(C, op);
2843 return OPERATOR_CANCELLED;
2844 }
2845 }
2846 break;
2847
2848 default:
2850 }
2851
2852 /* if updated settings, renew feedback message */
2853 stitch_update_header(ssc, C);
2855
2857}
2858
2860{
2861 PropertyRNA *prop;
2862
2863 static const EnumPropertyItem stitch_modes[] = {
2864 {STITCH_VERT, "VERTEX", 0, "Vertex", ""},
2865 {STITCH_EDGE, "EDGE", 0, "Edge", ""},
2866 {0, nullptr, 0, nullptr, nullptr},
2867 };
2868
2869 /* identifiers */
2870 ot->name = "Stitch";
2871 ot->description = "Stitch selected UV vertices by proximity";
2872 ot->idname = "UV_OT_stitch";
2874
2875 /* API callbacks. */
2876 ot->invoke = stitch_invoke;
2877 ot->modal = stitch_modal;
2878 ot->exec = stitch_exec;
2879 ot->cancel = stitch_cancel;
2881
2882 /* properties */
2884 ot->srna, "use_limit", false, "Use Limit", "Stitch UVs within a specified limit distance");
2886 "snap_islands",
2887 true,
2888 "Snap Islands",
2889 "Snap islands together (on edge stitch mode, rotates the islands too)");
2890
2891 RNA_def_float(ot->srna,
2892 "limit",
2893 0.01f,
2894 0.0f,
2895 FLT_MAX,
2896 "Limit",
2897 "Limit distance in normalized coordinates",
2898 0.0,
2899 FLT_MAX);
2900 RNA_def_int(ot->srna,
2901 "static_island",
2902 0,
2903 0,
2904 INT_MAX,
2905 "Static Island",
2906 "Island that stays in place when stitching islands",
2907 0,
2908 INT_MAX);
2909 RNA_def_int(ot->srna,
2910 "active_object_index",
2911 0,
2912 0,
2913 INT_MAX,
2914 "Active Object",
2915 "Index of the active object",
2916 0,
2917 INT_MAX);
2919 "midpoint_snap",
2920 false,
2921 "Snap at Midpoint",
2922 "UVs are stitched at midpoint instead of at static island");
2923 RNA_def_boolean(ot->srna, "clear_seams", true, "Clear Seams", "Clear seams of stitched edges");
2924 RNA_def_enum(ot->srna,
2925 "mode",
2926 stitch_modes,
2927 STITCH_VERT,
2928 "Operation Mode",
2929 "Use vertex or edge stitching");
2930 prop = RNA_def_enum(ot->srna,
2931 "stored_mode",
2932 stitch_modes,
2933 STITCH_VERT,
2934 "Stored Operation Mode",
2935 "Use vertex or edge stitching");
2937 prop = RNA_def_collection_runtime(ot->srna, "selection", RNA_SelectedUvElement, "Selection", "");
2938 /* Selection should not be editable or viewed in toolbar */
2940
2941 /* test should not be editable or viewed in toolbar */
2942 prop = RNA_def_int_array(ot->srna,
2943 "objects_selection_count",
2944 1,
2945 nullptr,
2946 0,
2947 INT_MAX,
2948 "Objects Selection Count",
2949 "",
2950 0,
2951 INT_MAX);
2952 RNA_def_property_array(prop, 6);
2954}
2955
2956} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define GHASH_ITER(gh_iter_, ghash_)
Definition BLI_ghash.hh:320
#define M_PI_2
#define M_PI
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define REGION_DRAW_POST_VIEW
#define GPU_batch_uniform_4fv(batch, name, val)
Definition GPU_batch.hh:275
Read Blender memory allocation and freeing.
#define RNA_BEGIN(sptr, itemptr, propname)
#define RNA_END
#define RNA_MAX_ARRAY_LENGTH
Definition RNA_define.hh:26
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define BM_FACE_FIRST_LOOP(p)
#define BM_ELEM_CD_GET_FLOAT_P(ele, offset)
#define BM_elem_index_get(ele)
#define BM_elem_flag_disable(ele, hflag)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_ELEM_INDEX(ele, iter, data, itype, indexvar)
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr bool is_empty() const
Definition BLI_span.hh:264
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
void item_bool(std::string text, bool inverted, int icon1, int icon2=0)
Definition area.cc:1097
#define acosf(x)
#define FLT_MAX
#define INT_MAX
const int state
void get_color_4fv(int colorid, float col[4])
float get_value_f(int colorid)
void view2d_region_to_view(const View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
Definition view2d.cc:1651
BMesh Iterators.
MINLINE float min_ff(float a, float b)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
static void stitch_setup_face_preview_for_uv_group(UvElement *element, StitchStateContainer *ssc, StitchState *state, IslandStitchData *island_stitch_data, PreviewPosition *preview_position)
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
UvElement * BM_uv_element_get(const UvElementMap *element_map, const BMLoop *l)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void uvedit_live_unwrap_update(SpaceImage *sima, Scene *scene, Object *obedit)
unsigned int BLI_ghashutil_uinthash(unsigned int key)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
bool ED_operator_uvedit(bContext *C)
static void stitch_switch_selection_mode_all(StitchStateContainer *ssc)
ScrArea * CTX_wm_area(const bContext *C)
BLI_INLINE void * BLI_ghashIterator_getKey(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.hh:297
static UvEdge * uv_edge_get(BMLoop *l, StitchState *state)
int RNA_enum_get(PointerRNA *ptr, const char *name)
gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, gpu::VertBuf *vertex_buf, gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:53
bool uvedit_edge_select_test(const Scene *scene, const BMesh *bm, const BMLoop *l, const BMUVOffsets &offsets)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool uv_stitch_selected_islands_for_original_bounds(const Scene *scene, Span< Object * > objects)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
float ED_uvedit_get_aspect_y(Object *obedit)
static bool stitch_island_pair_excluded(const StitchStateContainer *ssc, const StitchState *state, const IslandStitchData *island_stitch_data, const int island_a, const int island_b)
static void stitch_preview_delete(StitchPreviewer *stitch_preview)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
static int stitch_process_data_all(StitchStateContainer *ssc, const Scene *scene, int final)
MINLINE float cross_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:735
MINLINE void negate_v2_v2(float r[2], const float a[2])
MINLINE void copy_v2_v2(float r[2], const float a[2])
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
static void stitch_set_face_preview_buffer_position(BMFace *efa, StitchPreviewer *preview, PreviewPosition *preview_position)
void GPU_point_size(float size)
Definition gpu_state.cc:157
unsigned short ushort
Definition BKE_text.h:56
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
int RNA_int_get(PointerRNA *ptr, const char *name)
static void stitch_island_calculate_edge_rotation(const int cd_loop_uv_offset, UvEdge *edge, StitchStateContainer *ssc, StitchState *state, UVVertAverage *uv_average, const uint *uvfinal_map, IslandStitchData *island_stitch_data)
static void stitch_validate_edge_stitchability(const int cd_loop_uv_offset, UvEdge *edge, StitchStateContainer *ssc, StitchState *state, IslandStitchData *island_stitch_data, PreviewPosition *preview_position)
static void stitch_determine_seam_bounded_islands(StitchState *state, IslandStitchData *island_stitch_data)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
void GPU_batch_discard(gpu::Batch *batch)
static void stitch_state_init_free(StitchStateInit *state_init)
MINLINE float normalize_v2(float n[2])
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
PropertyRNA * RNA_def_collection_runtime(StructOrFunctionRNA *cont, const char *identifier, StructRNA *type, const char *ui_name, const char *ui_description)
@ TH_STITCH_PREVIEW_ACTIVE
@ TH_STITCH_PREVIEW_FACE
@ TH_STITCH_PREVIEW_EDGE
@ TH_STITCH_PREVIEW_STITCHABLE
@ TH_STITCH_PREVIEW_UNSTITCHABLE
UvElement * BM_uv_element_get_head(UvElementMap *element_map, UvElement *child)
static void stitch_uv_edge_generate_linked_edges(GHash *edge_hash, StitchState *state)
bool uv_find_nearest_edge_multi(Scene *scene, Span< Object * > objects, const float co[2], float penalty, UvNearestHit *hit)
static bool stitch_check_uvs_stitchable(const int cd_loop_uv_offset, UvElement *element, UvElement *element_iter, StitchStateContainer *ssc)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void BM_uv_element_map_free(UvElementMap *element_map)
void uvedit_select_prepare(const Scene *scene, BMesh *bm)
static void stitch_propagate_uv_final_position(const Scene *scene, UvElement *element, int index, PreviewPosition *preview_position, UVVertAverage *final_position, StitchStateContainer *ssc, StitchState *state, const bool final)
static bool stitch_check_edges_state_stitchable(const int cd_loop_uv_offset, UvEdge *edge, UvEdge *edge_iter, StitchStateContainer *ssc, StitchState *state)
ViewLayer * CTX_data_view_layer(const bContext *C)
static int stitch_process_data(StitchStateContainer *ssc, StitchState *state, const Scene *scene, int final)
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:711
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
void * BM_iter_at_index(BMesh *bm, const char itype, void *data, int index)
bool uvedit_uv_select_test(const Scene *scene, const BMesh *bm, const BMLoop *l, const BMUVOffsets &offsets)
bool uv_find_nearest_vert_multi(Scene *scene, Span< Object * > objects, const float co[2], float penalty_dist, UvNearestHit *hit)
static void determine_uv_stitchability(const int cd_loop_uv_offset, UvElement *element, StitchStateContainer *ssc, StitchState *state, IslandStitchData *island_stitch_data)
void * ED_region_draw_cb_activate(ARegionType *art, void(*draw)(const bContext *, ARegion *, void *), void *customdata, int type)
void RNA_int_set_array(PointerRNA *ptr, const char *name, const int *values)
void uvedit_uv_select_enable(const Scene *scene, BMesh *bm, BMLoop *l)
static void stitch_set_selection_mode(StitchState *state, const char from_stitch_mode)
void RNA_collection_clear(PointerRNA *ptr, const char *name)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
MINLINE void add_v2_v2(float r[2], const float a[2])
unsigned char uchar
Definition BKE_text.h:58
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:866
bool ED_region_draw_cb_exit(ARegionType *art, void *handle)
static bool goto_next_island(StitchStateContainer *ssc)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
static bool stitch_check_edges_stitchable(const int cd_loop_uv_offset, UvEdge *edge, UvEdge *edge_iter, StitchStateContainer *ssc, StitchState *state)
static void stitch_draw_vbo(gpu::VertBuf *vbo, GPUPrimType prim_type, const float col[4])
void RNA_int_get_array(PointerRNA *ptr, const char *name, int *values)
void GPU_vertbuf_attr_set(gpu::VertBuf *, uint a_idx, uint v_idx, const void *data)
static wmOperatorStatus stitch_modal(bContext *C, wmOperator *op, const wmEvent *event)
static void stitch_island_calculate_vert_rotation(const int cd_loop_uv_offset, UvElement *element, StitchStateContainer *ssc, StitchState *state, IslandStitchData *island_stitch_data)
static void stitch_calculate_island_snapping(const int cd_loop_uv_offset, StitchState *state, PreviewPosition *preview_position, StitchPreviewer *preview, IslandStitchData *island_stitch_data, int final)
UvNearestHit uv_nearest_hit_init_max(const View2D *v2d)
static int stitch_init_all(const Scene *scene, Span< Object * > objects, ARegion *region, StitchStateContainer *ssc, const StitchModes stored_mode, wmOperator *op)
void mul_v2_m2v2(float r[2], const float mat[2][2], const float vec[2])
void GPU_batch_draw(gpu::Batch *batch)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:44
static StitchPreviewer * stitch_preview_init()
static void determine_uv_edge_stitchability(const int cd_loop_uv_offset, UvEdge *edge, StitchStateContainer *ssc, StitchState *state, IslandStitchData *island_stitch_data)
static StitchStateContainer * stitch_init_from_operator(bContext *C, wmOperator *op)
static void stitch_uv_rotate(const float mat[2][2], const float medianPoint[2], float uv[2], float aspect)
static wmOperatorStatus stitch_invoke(bContext *C, wmOperator *op, const wmEvent *)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
unsigned int BLI_ghash_len(const GHash *gh) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:706
static wmOperatorStatus stitch_exec(bContext *C, wmOperator *op)
void angle_to_mat2(float R[2][2], float angle)
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
void GPU_batch_program_set_builtin(gpu::Batch *batch, GPUBuiltinShader shader_id)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
SpaceImage * CTX_wm_space_image(const bContext *C)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
static void stitch_select_edge(UvEdge *edge, StitchState *state, int always_select)
static bool uv_edge_compare(const void *a, const void *b)
Definition sculpt_uv.cc:637
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
Main * CTX_data_main(const bContext *C)
static StitchState * stitch_init(const Scene *scene, StitchStateContainer *ssc, Object *obedit, const StitchModes stored_mode, const Span< UvElementID > to_select)
static StitchStateInit stitch_extract_rna_selection(wmOperator *op, const Span< Object * > objects)
static void stitch_validate_uv_stitchability(const int cd_loop_uv_offset, UvElement *element, StitchStateContainer *ssc, StitchState *state, IslandStitchData *island_stitch_data, PreviewPosition *preview_position)
static void state_delete_all(StitchStateContainer *ssc)
static bool stitch_check_uvs_state_stitchable(const int cd_loop_uv_offset, UvElement *element, UvElement *element_iter, StitchStateContainer *ssc)
MINLINE float max_ff(float a, float b)
UvElementMap * BM_uv_element_map_create(BMesh *bm, const Scene *scene, bool uv_selected, bool use_winding, bool use_seams, bool do_islands)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
@ PROP_HIDDEN
Definition RNA_types.hh:408
static void stitch_exit(bContext *C, wmOperator *op, int finished)
PropertyRNA * RNA_def_int_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const int *default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
Scene * CTX_data_scene(const bContext *C)
MINLINE void sub_v2_v2(float r[2], const float a[2])
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
static void stitch_select_uv(UvElement *element, StitchState *state, int always_select)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
BLI_INLINE BMFace * BM_face_at_index(BMesh *bm, const int index)
GHash * BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:690
BMUVOffsets BM_uv_map_offsets_get(const BMesh *bm)
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
ATTR_WARN_UNUSED_RESULT const void * element
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
static StitchStateContainer * stitch_settings_init_for_original_bounds()
static StitchStateContainer * stitch_settings_init_for_operator(const Span< Object * > objects, Scene *scene, wmOperator *op)
static void stitch_update_header(StitchStateContainer *ssc, bContext *C)
static uint uv_edge_hash(const void *key)
Definition sculpt_uv.cc:631
void RNA_collection_add(PointerRNA *ptr, const char *name, PointerRNA *r_value)
static void state_delete(StitchState *state)
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 RNA_def_property_array(PropertyRNA *prop, int length)
ARegion * CTX_wm_region(const bContext *C)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
float RNA_float_get(PointerRNA *ptr, const char *name)
static StitchState * stitch_select(bContext *C, Scene *scene, const wmEvent *event, StitchStateContainer *ssc)
View3D * CTX_wm_view3d(const bContext *C)
static void stitch_calculate_edge_normal(const int cd_loop_uv_offset, UvEdge *edge, float *normal, float aspect)
static void stitch_cancel(bContext *C, wmOperator *op)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void stitch_draw(const bContext *, ARegion *, void *arg)
void UV_OT_stitch(wmOperatorType *ot)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bke::ARegionRuntime * runtime
struct BMLoop * next
struct ToolSettings * toolsettings
eTool_UvSelectMode uv_selectmode
unsigned short loop_of_face_index
struct ARegionType * type
wmEventType type
Definition WM_types.hh:817
wmEventModifierFlag modifier
Definition WM_types.hh:834
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
#define STITCH_NO_PREVIEW