Blender V5.3
sculpt_uv.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2002-2009 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include "MEM_guardedalloc.h"
11
12#include "BLI_ghash.hh"
13#include "BLI_math_base_safe.hh"
14#include "BLI_math_geom_c.hh"
15#include "BLI_math_vector_c.hh"
16#include "BLI_utildefines.hh"
17
18#include "DNA_object_types.h"
19#include "DNA_scene_types.h"
20
21#include "BKE_brush.hh"
22#include "BKE_colortools.hh"
23#include "BKE_context.hh"
24#include "BKE_customdata.hh"
25#include "BKE_editmesh.hh"
26#include "BKE_image.hh"
27#include "BKE_mesh_mapping.hh"
28#include "BKE_paint.hh"
29
30#include "DEG_depsgraph.hh"
31
32#include "ED_image.hh"
33#include "ED_mesh.hh"
34#include "ED_screen.hh"
35#include "ED_uvedit.hh"
36
37#include "WM_api.hh"
38#include "WM_types.hh"
39
40#include "RNA_access.hh"
41#include "RNA_define.hh"
42
43#include "paint_intern.hh"
44#include "uvedit_intern.hh"
45
46#include "UI_view2d.hh"
47
48namespace blender {
49
50namespace {
51
52enum eBrushUVSculptTool {
53 UV_SCULPT_BRUSH_TYPE_GRAB = 0,
54 UV_SCULPT_BRUSH_TYPE_RELAX = 1,
55 UV_SCULPT_BRUSH_TYPE_PINCH = 2,
56};
57
58enum {
59 UV_SCULPT_BRUSH_TYPE_RELAX_LAPLACIAN = 0,
60 UV_SCULPT_BRUSH_TYPE_RELAX_HC = 1,
61 UV_SCULPT_BRUSH_TYPE_RELAX_COTAN = 2,
62};
63
64/* When set, the UV element is on the boundary of the graph.
65 * i.e. Instead of a 2-dimensional laplace operator, use a 1-dimensional version.
66 * Visually, UV elements on the graph boundary appear as borders of the UV Island. */
67#define MARK_BOUNDARY 1
68
69struct UvAdjacencyElement {
71 UvElement *element;
73 float *uv;
75 bool is_locked;
77 bool is_boundary;
78};
79
80struct UvEdge {
81 uint uv1;
82 uint uv2;
84 bool is_interior;
85};
86
87struct UVInitialStrokeElement {
89 int uv;
90
92 float strength;
93
95 float initial_uv[2];
96};
97
98struct UVInitialStroke {
100 UVInitialStrokeElement *initialSelection;
101
103 int totalInitialSelected;
104
106 float init_coord[2];
107};
108
110struct UvSculptData {
115 UvAdjacencyElement *uv;
116
118 int totalUniqueUvs;
119
121 UvEdge *uvedges;
122
124 int totalUvEdges;
125
127 UVInitialStroke *initial_stroke;
128
130 wmTimer *timer;
131
133 UvElementMap *elementMap;
134
136 UvSculpt *uvsculpt;
137
139 char tool;
140
142 char invert;
143
145 bool constrain_to_bounds;
146
148 float uv_base_offset[2];
149};
150
151} // namespace
152
153static void apply_sculpt_data_constraints(UvSculptData *sculptdata, float uv[2])
154{
155 if (!sculptdata->constrain_to_bounds) {
156 return;
157 }
158 float u = sculptdata->uv_base_offset[0];
159 float v = sculptdata->uv_base_offset[1];
160 uv[0] = clamp_f(uv[0], u, u + 1.0f);
161 uv[1] = clamp_f(uv[1], v, v + 1.0f);
162}
163
164static float calc_strength(const UvSculptData *sculptdata, float p, const float len)
165{
166 float strength = BKE_brush_curve_strength(
167 eBrushCurvePreset(sculptdata->uvsculpt->curve_distance_falloff_preset),
168 sculptdata->uvsculpt->curve_distance_falloff,
169 p,
170 len);
171
172 CLAMP(strength, 0.0f, 1.0f);
173
174 return strength;
175}
176
177/* -------------------------------------------------------------------- */
183
185 float sum_co[2], p[2], b[2], sum_b[2];
187};
188
189static void HC_relaxation_iteration_uv(UvSculptData *sculptdata,
190 const int cd_loop_uv_offset,
191 const float mouse_coord[2],
192 const float alpha,
193 const float radius_sq,
194 const float aspectRatio)
195{
196 float diff[2];
197 int i;
198 const float radius = sqrtf(radius_sq);
199
200 Temp_UVData *tmp_uvdata = MEM_new_array_zeroed<Temp_UVData>(sculptdata->totalUniqueUvs,
201 "Temporal data");
202
203 /* counting neighbors */
204 for (i = 0; i < sculptdata->totalUvEdges; i++) {
205 UvEdge *tmpedge = sculptdata->uvedges + i;
206 tmp_uvdata[tmpedge->uv1].ncounter++;
207 tmp_uvdata[tmpedge->uv2].ncounter++;
208
209 add_v2_v2(tmp_uvdata[tmpedge->uv2].sum_co, sculptdata->uv[tmpedge->uv1].uv);
210 add_v2_v2(tmp_uvdata[tmpedge->uv1].sum_co, sculptdata->uv[tmpedge->uv2].uv);
211 }
212
213 for (i = 0; i < sculptdata->totalUniqueUvs; i++) {
214 copy_v2_v2(diff, tmp_uvdata[i].sum_co);
215 mul_v2_fl(diff, 1.0f / tmp_uvdata[i].ncounter);
216 copy_v2_v2(tmp_uvdata[i].p, diff);
217
218 tmp_uvdata[i].b[0] = diff[0] - sculptdata->uv[i].uv[0];
219 tmp_uvdata[i].b[1] = diff[1] - sculptdata->uv[i].uv[1];
220 }
221
222 for (i = 0; i < sculptdata->totalUvEdges; i++) {
223 UvEdge *tmpedge = sculptdata->uvedges + i;
224 add_v2_v2(tmp_uvdata[tmpedge->uv1].sum_b, tmp_uvdata[tmpedge->uv2].b);
225 add_v2_v2(tmp_uvdata[tmpedge->uv2].sum_b, tmp_uvdata[tmpedge->uv1].b);
226 }
227
228 for (i = 0; i < sculptdata->totalUniqueUvs; i++) {
229 if (sculptdata->uv[i].is_locked) {
230 continue;
231 }
232
233 sub_v2_v2v2(diff, sculptdata->uv[i].uv, mouse_coord);
234 diff[1] /= aspectRatio;
235 float dist = dot_v2v2(diff, diff);
236 if (dist <= radius_sq) {
238 float strength;
239 strength = alpha * calc_strength(sculptdata, sqrtf(dist), radius);
240
241 sculptdata->uv[i].uv[0] = (1.0f - strength) * sculptdata->uv[i].uv[0] +
242 strength *
243 (tmp_uvdata[i].p[0] -
244 0.5f * (tmp_uvdata[i].b[0] +
245 tmp_uvdata[i].sum_b[0] / tmp_uvdata[i].ncounter));
246 sculptdata->uv[i].uv[1] = (1.0f - strength) * sculptdata->uv[i].uv[1] +
247 strength *
248 (tmp_uvdata[i].p[1] -
249 0.5f * (tmp_uvdata[i].b[1] +
250 tmp_uvdata[i].sum_b[1] / tmp_uvdata[i].ncounter));
251
252 apply_sculpt_data_constraints(sculptdata, sculptdata->uv[i].uv);
253
254 for (element = sculptdata->uv[i].element; element; element = element->next) {
255 if (element->separate && element != sculptdata->uv[i].element) {
256 break;
257 }
258 float (*luv)[2] = BM_ELEM_CD_GET_FLOAT2_P(element->l, cd_loop_uv_offset);
259 copy_v2_v2(*luv, sculptdata->uv[i].uv);
260 }
261 }
262 }
263
264 MEM_SAFE_DELETE(tmp_uvdata);
265}
266
267/* Legacy version which only does laplacian relaxation.
268 * Probably a little faster as it caches UvEdges.
269 * Mostly preserved for comparison with `HC_relaxation_iteration_uv`.
270 * Once the HC method has been merged into `relaxation_iteration_uv`,
271 * all the `HC_*` and `laplacian_*` specific functions can probably be removed.
272 */
273
274static void laplacian_relaxation_iteration_uv(UvSculptData *sculptdata,
275 const int cd_loop_uv_offset,
276 const float mouse_coord[2],
277 const float alpha,
278 const float radius_sq,
279 const float aspectRatio)
280{
281 float diff[2];
282 int i;
283 const float radius = sqrtf(radius_sq);
284
285 Temp_UVData *tmp_uvdata = MEM_new_array_zeroed<Temp_UVData>(sculptdata->totalUniqueUvs,
286 "Temporal data");
287
288 /* counting neighbors */
289 for (i = 0; i < sculptdata->totalUvEdges; i++) {
290 UvEdge *tmpedge = sculptdata->uvedges + i;
291 bool code1 = sculptdata->uv[sculptdata->uvedges[i].uv1].is_boundary;
292 bool code2 = sculptdata->uv[sculptdata->uvedges[i].uv2].is_boundary;
293 if (code1 || (code1 == code2)) {
294 tmp_uvdata[tmpedge->uv2].ncounter++;
295 add_v2_v2(tmp_uvdata[tmpedge->uv2].sum_co, sculptdata->uv[tmpedge->uv1].uv);
296 }
297 if (code2 || (code1 == code2)) {
298 tmp_uvdata[tmpedge->uv1].ncounter++;
299 add_v2_v2(tmp_uvdata[tmpedge->uv1].sum_co, sculptdata->uv[tmpedge->uv2].uv);
300 }
301 }
302
303 /* Original Laplacian algorithm included removal of normal component of translation.
304 * here it is not needed since we translate along the UV plane always. */
305 for (i = 0; i < sculptdata->totalUniqueUvs; i++) {
306 copy_v2_v2(tmp_uvdata[i].p, tmp_uvdata[i].sum_co);
307 mul_v2_fl(tmp_uvdata[i].p, 1.0f / tmp_uvdata[i].ncounter);
308 }
309
310 for (i = 0; i < sculptdata->totalUniqueUvs; i++) {
311 if (sculptdata->uv[i].is_locked) {
312 continue;
313 }
314
315 sub_v2_v2v2(diff, sculptdata->uv[i].uv, mouse_coord);
316 diff[1] /= aspectRatio;
317 float dist = dot_v2v2(diff, diff);
318 if (dist <= radius_sq) {
320 float strength;
321 strength = alpha * calc_strength(sculptdata, sqrtf(dist), radius);
322
323 sculptdata->uv[i].uv[0] = (1.0f - strength) * sculptdata->uv[i].uv[0] +
324 strength * tmp_uvdata[i].p[0];
325 sculptdata->uv[i].uv[1] = (1.0f - strength) * sculptdata->uv[i].uv[1] +
326 strength * tmp_uvdata[i].p[1];
327
328 apply_sculpt_data_constraints(sculptdata, sculptdata->uv[i].uv);
329
330 for (element = sculptdata->uv[i].element; element; element = element->next) {
331 if (element->separate && element != sculptdata->uv[i].element) {
332 break;
333 }
334
335 float (*luv)[2] = BM_ELEM_CD_GET_FLOAT2_P(element->l, cd_loop_uv_offset);
336 copy_v2_v2(*luv, sculptdata->uv[i].uv);
337 }
338 }
339 }
340
341 MEM_SAFE_DELETE(tmp_uvdata);
342}
343
344static void add_weighted_edge(float (*delta_buf)[3],
345 const UvElement *storage,
346 const UvElement *ele_next,
347 const UvElement *ele_prev,
348 const float luv_next[2],
349 const float luv_prev[2],
350 const float weight)
351{
352 float delta[2];
353 sub_v2_v2v2(delta, luv_next, luv_prev);
354
355 bool code1 = (ele_prev->flag & MARK_BOUNDARY);
356 bool code2 = (ele_next->flag & MARK_BOUNDARY);
357 if (code1 || (code1 == code2)) {
358 int index_next = ele_next - storage;
359 delta_buf[index_next][0] -= delta[0] * weight;
360 delta_buf[index_next][1] -= delta[1] * weight;
361 delta_buf[index_next][2] += fabsf(weight);
362 }
363 if (code2 || (code1 == code2)) {
364 int index_prev = ele_prev - storage;
365 delta_buf[index_prev][0] += delta[0] * weight;
366 delta_buf[index_prev][1] += delta[1] * weight;
367 delta_buf[index_prev][2] += fabsf(weight);
368 }
369}
370
371static float tri_weight_v3(int method, const float *v1, const float *v2, const float *v3)
372{
373 switch (method) {
374 case UV_SCULPT_BRUSH_TYPE_RELAX_LAPLACIAN:
375 case UV_SCULPT_BRUSH_TYPE_RELAX_HC:
376 return 1.0f;
377 case UV_SCULPT_BRUSH_TYPE_RELAX_COTAN:
378 return cotangent_tri_weight_v3(v1, v2, v3);
379 default:
381 }
382 return 0.0f;
383}
384
385static void relaxation_iteration_uv(UvSculptData *sculptdata,
386 const int cd_loop_uv_offset,
387 const float mouse_coord[2],
388 const float alpha,
389 const float radius_sq,
390 const float aspect_ratio,
391 const int method)
392{
393 if (method == UV_SCULPT_BRUSH_TYPE_RELAX_HC) {
395 sculptdata, cd_loop_uv_offset, mouse_coord, alpha, radius_sq, aspect_ratio);
396 return;
397 }
398 if (method == UV_SCULPT_BRUSH_TYPE_RELAX_LAPLACIAN) {
400 sculptdata, cd_loop_uv_offset, mouse_coord, alpha, radius_sq, aspect_ratio);
401 return;
402 }
403
404 UvElement **head_table = BM_uv_element_map_ensure_head_table(sculptdata->elementMap);
405
406 const int total_uvs = sculptdata->elementMap->total_uvs;
407 float (*delta_buf)[3] = MEM_new_array_zeroed<float[3]>(total_uvs, __func__);
408
409 const UvElement *storage = sculptdata->elementMap->storage;
410 for (int j = 0; j < total_uvs; j++) {
411 const UvElement *ele_curr = storage + j;
412 const UvElement *ele_next = BM_uv_element_get(sculptdata->elementMap, ele_curr->l->next);
413 const UvElement *ele_prev = BM_uv_element_get(sculptdata->elementMap, ele_curr->l->prev);
414
415 const float *v_curr_co = ele_curr->l->v->co;
416 const float *v_prev_co = ele_prev->l->v->co;
417 const float *v_next_co = ele_next->l->v->co;
418
419 const float (*luv_curr)[2] = BM_ELEM_CD_GET_FLOAT2_P(ele_curr->l, cd_loop_uv_offset);
420 const float (*luv_next)[2] = BM_ELEM_CD_GET_FLOAT2_P(ele_next->l, cd_loop_uv_offset);
421 const float (*luv_prev)[2] = BM_ELEM_CD_GET_FLOAT2_P(ele_prev->l, cd_loop_uv_offset);
422
423 const UvElement *head_curr = head_table[ele_curr - sculptdata->elementMap->storage];
424 const UvElement *head_next = head_table[ele_next - sculptdata->elementMap->storage];
425 const UvElement *head_prev = head_table[ele_prev - sculptdata->elementMap->storage];
426
427 /* If the mesh is triangulated with no boundaries, only one edge is required. */
428 const float weight_curr = tri_weight_v3(method, v_curr_co, v_prev_co, v_next_co);
429 add_weighted_edge(delta_buf, storage, head_next, head_prev, *luv_next, *luv_prev, weight_curr);
430
431 /* Triangulated with a boundary? We need the incoming edges to solve the boundary. */
432 const float weight_prev = tri_weight_v3(method, v_prev_co, v_curr_co, v_next_co);
433 add_weighted_edge(delta_buf, storage, head_next, head_curr, *luv_next, *luv_curr, weight_prev);
434
435 if (method == UV_SCULPT_BRUSH_TYPE_RELAX_LAPLACIAN) {
436 /* Laplacian method has zero weights on virtual edges. */
437 continue;
438 }
439
440 /* Meshes with quads (or other n-gons) need "virtual" edges too. */
441 const float weight_next = tri_weight_v3(method, v_next_co, v_curr_co, v_prev_co);
442 add_weighted_edge(delta_buf, storage, head_prev, head_curr, *luv_prev, *luv_curr, weight_next);
443 }
444
445 for (int i = 0; i < sculptdata->totalUniqueUvs; i++) {
446 UvAdjacencyElement *adj_el = &sculptdata->uv[i];
447 if (adj_el->is_locked) {
448 continue; /* Locked UVs can't move. */
449 }
450
451 /* Is UV within influence? */
452 float diff[2];
453 sub_v2_v2v2(diff, adj_el->uv, mouse_coord);
454 diff[1] /= aspect_ratio;
455 const float dist_sq = len_squared_v2(diff);
456 if (dist_sq > radius_sq) {
457 continue;
458 }
459 const float strength = alpha * calc_strength(sculptdata, sqrtf(dist_sq), sqrtf(radius_sq));
460
461 const float *delta_sum = delta_buf[adj_el->element - storage];
462
463 {
464 const float (*luv)[2] = BM_ELEM_CD_GET_FLOAT2_P(adj_el->element->l, cd_loop_uv_offset);
465 BLI_assert(adj_el->uv == (float *)luv); /* Only true for head. */
466 adj_el->uv[0] = (*luv)[0] + strength * safe_divide(delta_sum[0], delta_sum[2]);
467 adj_el->uv[1] = (*luv)[1] + strength * safe_divide(delta_sum[1], delta_sum[2]);
468 apply_sculpt_data_constraints(sculptdata, adj_el->uv);
469 }
470
471 /* Copy UV co-ordinates to all UvElements. */
472 UvElement *tail = adj_el->element;
473 while (tail) {
474 float (*luv)[2] = BM_ELEM_CD_GET_FLOAT2_P(tail->l, cd_loop_uv_offset);
475 copy_v2_v2(*luv, adj_el->uv);
476 tail = tail->next;
477 if (tail && tail->separate) {
478 break;
479 }
480 }
481 }
482
483 MEM_SAFE_DELETE(delta_buf);
484}
485
487
489 wmOperator *op,
490 const wmEvent *event,
491 Object *obedit)
492{
493 ARegion *region = CTX_wm_region(C);
495 UvSculptData *sculptdata = static_cast<UvSculptData *>(op->customdata);
496 eBrushUVSculptTool tool = eBrushUVSculptTool(sculptdata->tool);
497 int invert = sculptdata->invert ? -1 : 1;
498 float alpha = sculptdata->uvsculpt->strength;
499
500 float co[2];
501 ui::view2d_region_to_view(&region->v2d, event->mval[0], event->mval[1], &co[0], &co[1]);
502
504
505 int width, height;
506 ED_space_image_get_size(sima, &width, &height);
507
508 float zoomx, zoomy;
509 ED_space_image_get_zoom(sima, region, &zoomx, &zoomy);
510
511 const float radius = (sculptdata->uvsculpt->size / 2.0f) / (width * zoomx);
512 float aspectRatio = width / float(height);
513
514 /* We will compare squares to save some computation */
515 const float radius_sq = radius * radius;
516
517 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_PROP_FLOAT2);
518
519 switch (tool) {
520 case UV_SCULPT_BRUSH_TYPE_PINCH: {
521 int i;
522 alpha *= invert;
523 for (i = 0; i < sculptdata->totalUniqueUvs; i++) {
524 if (sculptdata->uv[i].is_locked) {
525 continue;
526 }
527
528 float diff[2];
529 sub_v2_v2v2(diff, sculptdata->uv[i].uv, co);
530 diff[1] /= aspectRatio;
531 float dist = dot_v2v2(diff, diff);
532 if (dist <= radius_sq) {
534 float strength;
535 strength = alpha * calc_strength(sculptdata, sqrtf(dist), radius);
537
538 sculptdata->uv[i].uv[0] -= strength * diff[0] * 0.001f;
539 sculptdata->uv[i].uv[1] -= strength * diff[1] * 0.001f;
540
541 apply_sculpt_data_constraints(sculptdata, sculptdata->uv[i].uv);
542
543 for (element = sculptdata->uv[i].element; element; element = element->next) {
544 if (element->separate && element != sculptdata->uv[i].element) {
545 break;
546 }
547 float (*luv)[2] = BM_ELEM_CD_GET_FLOAT2_P(element->l, cd_loop_uv_offset);
548 copy_v2_v2(*luv, sculptdata->uv[i].uv);
549 }
550 }
551 }
552 break;
553 }
554 case UV_SCULPT_BRUSH_TYPE_RELAX: {
555 relaxation_iteration_uv(sculptdata,
556 cd_loop_uv_offset,
557 co,
558 alpha,
559 radius_sq,
560 aspectRatio,
561 RNA_enum_get(op->ptr, "relax_method"));
562 break;
563 }
564 case UV_SCULPT_BRUSH_TYPE_GRAB: {
565 int i;
566 float diff[2];
567 sub_v2_v2v2(diff, co, sculptdata->initial_stroke->init_coord);
568
569 for (i = 0; i < sculptdata->initial_stroke->totalInitialSelected; i++) {
571 int uvindex = sculptdata->initial_stroke->initialSelection[i].uv;
572 float strength = sculptdata->initial_stroke->initialSelection[i].strength;
573 sculptdata->uv[uvindex].uv[0] =
574 sculptdata->initial_stroke->initialSelection[i].initial_uv[0] + strength * diff[0];
575 sculptdata->uv[uvindex].uv[1] =
576 sculptdata->initial_stroke->initialSelection[i].initial_uv[1] + strength * diff[1];
577
578 apply_sculpt_data_constraints(sculptdata, sculptdata->uv[uvindex].uv);
579
580 for (element = sculptdata->uv[uvindex].element; element; element = element->next) {
581 if (element->separate && element != sculptdata->uv[uvindex].element) {
582 break;
583 }
584 float (*luv)[2] = BM_ELEM_CD_GET_FLOAT2_P(element->l, cd_loop_uv_offset);
585 copy_v2_v2(*luv, sculptdata->uv[uvindex].uv);
586 }
587 }
588 if (sima->flag & SI_LIVE_UNWRAP) {
590 }
591 break;
592 }
593 }
594}
595
597{
599 if (sima->flag & SI_LIVE_UNWRAP) {
601 }
602 UvSculptData *data = static_cast<UvSculptData *>(op->customdata);
603 if (data->timer) {
605 }
606 BM_uv_element_map_free(data->elementMap);
607 data->elementMap = nullptr;
608 MEM_SAFE_DELETE(data->uv);
609 MEM_SAFE_DELETE(data->uvedges);
610 if (data->initial_stroke) {
611 MEM_SAFE_DELETE(data->initial_stroke->initialSelection);
612 MEM_SAFE_DELETE(data->initial_stroke);
613 }
614
615 MEM_SAFE_DELETE(data);
616 op->customdata = nullptr;
617}
618
620 BMLoop *l,
621 int island_index,
622 const bool do_islands)
623{
625 if (!element || (do_islands && element->island != island_index)) {
626 return -1;
627 }
628 return element - map->storage;
629}
630
631static uint uv_edge_hash(const void *key)
632{
633 const UvEdge *edge = static_cast<const UvEdge *>(key);
634 return (BLI_ghashutil_uinthash(edge->uv2) + BLI_ghashutil_uinthash(edge->uv1));
635}
636
637static bool uv_edge_compare(const void *a, const void *b)
638{
639 const UvEdge *edge1 = static_cast<const UvEdge *>(a);
640 const UvEdge *edge2 = static_cast<const UvEdge *>(b);
641
642 if ((edge1->uv1 == edge2->uv1) && (edge1->uv2 == edge2->uv2)) {
643 return false;
644 }
645 return true;
646}
647
648static void set_element_flag(UvElement *element, const int flag)
649{
650 while (element) {
651 element->flag |= flag;
652 element = element->next;
653 if (!element || element->separate) {
654 break;
655 }
656 }
657}
658
659static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wmEvent *event)
660{
661 Scene *scene = CTX_data_scene(C);
662 Object *obedit = CTX_data_edit_object(C);
663 ToolSettings *ts = scene->toolsettings;
664 UvSculptData *data = MEM_new_zeroed<UvSculptData>(__func__);
666
667 op->customdata = data;
668
670
671 if (!data) {
672 return nullptr;
673 }
674
675 ARegion *region = CTX_wm_region(C);
676 float co[2];
677 BMFace *efa;
678 float (*luv)[2];
679 BMLoop *l;
680 BMIter iter, liter;
681
682 GHashIterator gh_iter;
683
684 bool do_island_optimization = !(ts->uv_sculpt_settings & UV_SCULPT_ALL_ISLANDS);
685 int island_index = 0;
686 if (STREQ(op->type->idname, "SCULPT_OT_uv_sculpt_relax")) {
687 data->tool = UV_SCULPT_BRUSH_TYPE_RELAX;
688 }
689 else if (STREQ(op->type->idname, "SCULPT_OT_uv_sculpt_grab")) {
690 data->tool = UV_SCULPT_BRUSH_TYPE_GRAB;
691 }
692 else {
693 data->tool = UV_SCULPT_BRUSH_TYPE_PINCH;
694 }
695 data->invert = RNA_boolean_get(op->ptr, "use_invert");
696
697 data->uvsculpt = &ts->uvsculpt;
698
699 /* Winding was added to island detection in 5197aa04c6bd
700 * However the sculpt tools can flip faces, potentially creating orphaned islands.
701 * See #100132 */
702 const bool use_winding = false;
703 const bool use_seams = true;
704 data->elementMap = BM_uv_element_map_create(
705 bm, scene, false, use_winding, use_seams, do_island_optimization);
706
707 if (!data->elementMap) {
709 return nullptr;
710 }
711
712 /* Mouse coordinates, useful for some functions like grab and sculpt all islands */
713 ui::view2d_region_to_view(&region->v2d, event->mval[0], event->mval[1], &co[0], &co[1]);
714
715 /* We need to find the active island here. */
716 if (do_island_optimization) {
718 uv_find_nearest_vert(scene, obedit, co, 0.0f, &hit);
719
720 UvElement *element = BM_uv_element_get(data->elementMap, hit.l);
721 if (element) {
722 island_index = element->island;
723 }
724 }
725
726 /* Count 'unique' UVs */
727 int unique_uvs = data->elementMap->total_unique_uvs;
728 if (do_island_optimization) {
729 unique_uvs = data->elementMap->island_total_unique_uvs[island_index];
730 }
731
732 /* Allocate the unique uv buffers */
733 data->uv = MEM_new_array_zeroed<UvAdjacencyElement>(unique_uvs, __func__);
734 /* Holds, for each UvElement in elementMap, an index of its unique UV. */
735 int *uniqueUv = MEM_new_array_uninitialized<int>(data->elementMap->total_uvs, __func__);
736 GHash *edgeHash = BLI_ghash_new(uv_edge_hash, uv_edge_compare, "uv_brush_edge_hash");
737 /* we have at most totalUVs edges */
738 UvEdge *edges = MEM_new_array_zeroed<UvEdge>(data->elementMap->total_uvs, __func__);
739 if (!data->uv || !uniqueUv || !edgeHash || !edges) {
740 MEM_SAFE_DELETE(edges);
741 MEM_SAFE_DELETE(uniqueUv);
742 if (edgeHash) {
743 BLI_ghash_free(edgeHash, nullptr, nullptr);
744 }
746 return nullptr;
747 }
748
749 data->totalUniqueUvs = unique_uvs;
750 /* Index for the UvElements. */
751 int counter = -1;
752
753 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
754 /* initialize the unique UVs */
755 for (int i = 0; i < bm->totvert; i++) {
756 UvElement *element = data->elementMap->vertex[i];
757 for (; element; element = element->next) {
758 if (element->separate) {
759 if (do_island_optimization && (element->island != island_index)) {
760 /* skip this uv if not on the active island */
761 for (; element->next && !(element->next->separate); element = element->next) {
762 /* pass */
763 }
764 continue;
765 }
766
767 luv = BM_ELEM_CD_GET_FLOAT2_P(element->l, offsets.uv);
768
769 counter++;
770 data->uv[counter].element = element;
771 data->uv[counter].uv = *luv;
772 if (data->tool != UV_SCULPT_BRUSH_TYPE_GRAB) {
773 if (BM_ELEM_CD_GET_BOOL(element->l, offsets.pin)) {
774 data->uv[counter].is_locked = true;
775 }
776 }
777 }
778 /* Pointer arithmetic to the rescue, as always :). */
779 uniqueUv[element - data->elementMap->storage] = counter;
780 }
781 }
782 BLI_assert(counter + 1 == unique_uvs);
783
784 /* Now, on to generate our uv connectivity data */
785 counter = 0;
786 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
787 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
789 data->elementMap, l, island_index, do_island_optimization);
791 data->elementMap, l->next, island_index, do_island_optimization);
792
793 /* Skip edge if not found(unlikely) or not on valid island */
794 if (itmp1 == -1 || itmp2 == -1) {
795 continue;
796 }
797
798 int offset1 = uniqueUv[itmp1];
799 int offset2 = uniqueUv[itmp2];
800
801 /* Using an order policy, sort UVs according to address space.
802 * This avoids having two different UvEdges with the same UVs on different positions. */
803 if (offset1 < offset2) {
804 edges[counter].uv1 = offset1;
805 edges[counter].uv2 = offset2;
806 }
807 else {
808 edges[counter].uv1 = offset2;
809 edges[counter].uv2 = offset1;
810 }
811 UvEdge *prev_edge = static_cast<UvEdge *>(BLI_ghash_lookup(edgeHash, &edges[counter]));
812 if (prev_edge) {
813 prev_edge->is_interior = true;
814 edges[counter].is_interior = true;
815 }
816 else {
817 BLI_ghash_insert(edgeHash, &edges[counter], &edges[counter]);
818 }
819 counter++;
820 }
821 }
822
823 MEM_SAFE_DELETE(uniqueUv);
824
825 /* Allocate connectivity data, we allocate edges once */
826 data->uvedges = MEM_new_array_zeroed<UvEdge>(BLI_ghash_len(edgeHash), __func__);
827 if (!data->uvedges) {
828 BLI_ghash_free(edgeHash, nullptr, nullptr);
829 MEM_SAFE_DELETE(edges);
831 return nullptr;
832 }
833
834 /* fill the edges with data */
835 {
836 int i = 0;
837 GHASH_ITER (gh_iter, edgeHash) {
838 data->uvedges[i++] = *(static_cast<UvEdge *>(BLI_ghashIterator_getKey(&gh_iter)));
839 }
840 data->totalUvEdges = BLI_ghash_len(edgeHash);
841 }
842
843 /* cleanup temporary stuff */
844 BLI_ghash_free(edgeHash, nullptr, nullptr);
845 MEM_SAFE_DELETE(edges);
846
847 /* transfer boundary edge property to UVs */
848 for (int i = 0; i < data->totalUvEdges; i++) {
849 if (!data->uvedges[i].is_interior) {
850 data->uv[data->uvedges[i].uv1].is_boundary = true;
851 data->uv[data->uvedges[i].uv2].is_boundary = true;
853 data->uv[data->uvedges[i].uv1].is_locked = true;
854 data->uv[data->uvedges[i].uv2].is_locked = true;
855 }
856 set_element_flag(data->uv[data->uvedges[i].uv1].element, MARK_BOUNDARY);
857 set_element_flag(data->uv[data->uvedges[i].uv2].element, MARK_BOUNDARY);
858 }
859 }
860
862 data->constrain_to_bounds = (sima->flag & SI_CLIP_UV);
863 BKE_image_find_nearest_tile_with_offset(sima->image, co, data->uv_base_offset);
864
865 /* Allocate initial selection for grab tool */
866 if (data->tool == UV_SCULPT_BRUSH_TYPE_GRAB) {
867 float alpha = data->uvsculpt->strength;
868 float radius = data->uvsculpt->size / 2.0f;
869 int width, height;
870 ED_space_image_get_size(sima, &width, &height);
871 float zoomx, zoomy;
872 ED_space_image_get_zoom(sima, region, &zoomx, &zoomy);
873
874 float aspectRatio = width / float(height);
875 radius /= (width * zoomx);
876 const float radius_sq = radius * radius;
877
878 /* Allocate selection stack */
879 data->initial_stroke = MEM_new_uninitialized<UVInitialStroke>(__func__);
880 if (!data->initial_stroke) {
882 }
883 data->initial_stroke->initialSelection = MEM_new_array_uninitialized<UVInitialStrokeElement>(
884 data->totalUniqueUvs, __func__);
885 if (!data->initial_stroke->initialSelection) {
887 }
888
889 copy_v2_v2(data->initial_stroke->init_coord, co);
890
891 counter = 0;
892 for (int i = 0; i < data->totalUniqueUvs; i++) {
893 if (data->uv[i].is_locked) {
894 continue;
895 }
896
897 float diff[2];
898 sub_v2_v2v2(diff, data->uv[i].uv, co);
899 diff[1] /= aspectRatio;
900 float dist = dot_v2v2(diff, diff);
901 if (dist <= radius_sq) {
902 float strength;
903 strength = alpha * calc_strength(data, sqrtf(dist), radius);
904
905 data->initial_stroke->initialSelection[counter].uv = i;
906 data->initial_stroke->initialSelection[counter].strength = strength;
907 copy_v2_v2(data->initial_stroke->initialSelection[counter].initial_uv, data->uv[i].uv);
908 counter++;
909 }
910 }
911
912 data->initial_stroke->totalInitialSelected = counter;
913 if (sima->flag & SI_LIVE_UNWRAP) {
914 wmWindow *win_modal = CTX_wm_window(C);
915 ED_uvedit_live_unwrap_begin(scene, obedit, win_modal);
916 }
917 }
918
919 return static_cast<UvSculptData *>(op->customdata);
920}
921
923{
924 UvSculptData *data;
925 Object *obedit = CTX_data_edit_object(C);
926
927 if (!(data = uv_sculpt_stroke_init(C, op, event))) {
928 return OPERATOR_CANCELLED;
929 }
930
931 uv_sculpt_stroke_apply(C, op, event, obedit);
932
934
935 if (!data->timer) {
937 return OPERATOR_CANCELLED;
938 }
940
942}
943
945{
946 UvSculptData *data = static_cast<UvSculptData *>(op->customdata);
947 Object *obedit = CTX_data_edit_object(C);
948
949 switch (event->type) {
950 case LEFTMOUSE:
951 case MIDDLEMOUSE:
952 case RIGHTMOUSE:
954 return OPERATOR_FINISHED;
955
956 case MOUSEMOVE:
958 uv_sculpt_stroke_apply(C, op, event, obedit);
959 break;
960 case TIMER:
961 if (event->customdata == data->timer) {
962 uv_sculpt_stroke_apply(C, op, event, obedit);
963 }
964 break;
965 default:
967 }
968
973}
974
976{
977 PropertyRNA *prop;
978
979 prop = RNA_def_boolean(
980 ot->srna, "use_invert", false, "Invert", "Invert action for the duration of the stroke");
982}
983
985{
986 ot->name = "Grab UVs";
987 ot->description = "Grab UVs";
988 ot->idname = "SCULPT_OT_uv_sculpt_grab";
989
993
995
997}
998
1000{
1001 ot->name = "Relax UVs";
1002 ot->description = "Relax UVs";
1003 ot->idname = "SCULPT_OT_uv_sculpt_relax";
1004
1008
1010
1012
1013 static const EnumPropertyItem relax_method_items[] = {
1014 {UV_SCULPT_BRUSH_TYPE_RELAX_LAPLACIAN,
1015 "LAPLACIAN",
1016 0,
1017 "Laplacian",
1018 "Use Laplacian method for relaxation"},
1019 {UV_SCULPT_BRUSH_TYPE_RELAX_HC, "HC", 0, "HC", "Use HC method for relaxation"},
1020 {UV_SCULPT_BRUSH_TYPE_RELAX_COTAN,
1021 "COTAN",
1022 0,
1023 "Geometry",
1024 "Use Geometry (cotangent) relaxation, making UVs follow the underlying 3D geometry"},
1025 {0, nullptr, 0, nullptr, nullptr},
1026 };
1027
1028 RNA_def_enum(ot->srna,
1029 "relax_method",
1030 relax_method_items,
1031 UV_SCULPT_BRUSH_TYPE_RELAX_LAPLACIAN,
1032 "Relax Method",
1033 "Algorithm used for UV relaxation");
1034}
1035
1037{
1038 ot->name = "Pinch UVs";
1039 ot->description = "Pinch UVs";
1040 ot->idname = "SCULPT_OT_uv_sculpt_pinch";
1041
1045
1047
1049}
1050
1051} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define GHASH_ITER(gh_iter_, ghash_)
Definition BLI_ghash.hh:320
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define CLAMP(a, b, c)
#define BLI_assert_unreachable()
#define STREQ(a, b)
#define BM_ELEM_CD_GET_BOOL(ele, offset)
#define BM_ELEM_CD_GET_FLOAT2_P(ele, offset)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
#define storage(slot, qualifiers)
float2x2 invert(float2x2 mat)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
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.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
bool uv_find_nearest_vert(Scene *scene, Object *obedit, const float co[2], float penalty_dist, UvNearestHit *hit)
UvElement * BM_uv_element_get(const UvElementMap *element_map, const BMLoop *l)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int BLI_ghashutil_uinthash(unsigned int key)
BLI_INLINE void * BLI_ghashIterator_getKey(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.hh:297
int RNA_enum_get(PointerRNA *ptr, const char *name)
static void relaxation_iteration_uv(UvSculptData *sculptdata, const int cd_loop_uv_offset, const float mouse_coord[2], const float alpha, const float radius_sq, const float aspect_ratio, const int method)
Definition sculpt_uv.cc:385
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
UvElement ** BM_uv_element_map_ensure_head_table(UvElementMap *element_map)
unsigned int uint
Definition BKE_text.h:55
static void uv_sculpt_stroke_apply(bContext *C, wmOperator *op, const wmEvent *event, Object *obedit)
Definition sculpt_uv.cc:488
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
ATTR_WARN_UNUSED_RESULT const BMLoop * l
MINLINE void mul_v2_fl(float r[2], float f)
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:735
MINLINE void copy_v2_v2(float r[2], const float a[2])
void SCULPT_OT_uv_sculpt_relax(wmOperatorType *ot)
Definition sculpt_uv.cc:999
void ED_space_image_get_size(SpaceImage *sima, int *r_width, int *r_height)
static int uv_element_offset_from_face_get(UvElementMap *map, BMLoop *l, int island_index, const bool do_islands)
Definition sculpt_uv.cc:619
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
static void register_common_props(wmOperatorType *ot)
Definition sculpt_uv.cc:975
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
wmWindow * CTX_wm_window(const bContext *C)
MINLINE float normalize_v2(float n[2])
ATTR_WARN_UNUSED_RESULT const BMVert * v
static wmOperatorStatus uv_sculpt_stroke_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Definition sculpt_uv.cc:922
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void BM_uv_element_map_free(UvElementMap *element_map)
void ED_uvedit_live_unwrap_re_solve()
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:711
static void uv_sculpt_stroke_exit(bContext *C, wmOperator *op)
Definition sculpt_uv.cc:596
@ INBETWEEN_MOUSEMOVE
void BKE_curvemapping_init(CurveMapping *cumap)
int BKE_image_find_nearest_tile_with_offset(const Image *image, const float co[2], float r_uv_offset[2]) ATTR_NONNULL(2
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])
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:866
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
wmWindowManager * CTX_wm_manager(const bContext *C)
Object * CTX_data_edit_object(const bContext *C)
static void set_element_flag(UvElement *element, const int flag)
Definition sculpt_uv.cc:648
void SCULPT_OT_uv_sculpt_pinch(wmOperatorType *ot)
static float tri_weight_v3(int method, const float *v1, const float *v2, const float *v3)
Definition sculpt_uv.cc:371
void SCULPT_OT_uv_sculpt_grab(wmOperatorType *ot)
Definition sculpt_uv.cc:984
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1691
UvNearestHit uv_nearest_hit_init_max(const View2D *v2d)
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
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
static void laplacian_relaxation_iteration_uv(UvSculptData *sculptdata, const int cd_loop_uv_offset, const float mouse_coord[2], const float alpha, const float radius_sq, const float aspectRatio)
Definition sculpt_uv.cc:274
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool ED_operator_uvedit_space_image(bContext *C)
void ED_uvedit_live_unwrap_end(bool cancel)
SpaceImage * CTX_wm_space_image(const bContext *C)
MINLINE float len_squared_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
static float calc_strength(const UvSculptData *sculptdata, float p, const float len)
Definition sculpt_uv.cc:164
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
float cotangent_tri_weight_v3(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:202
static bool uv_edge_compare(const void *a, const void *b)
Definition sculpt_uv.cc:637
ATTR_WARN_UNUSED_RESULT const BMVert * v2
static wmOperatorStatus uv_sculpt_stroke_modal(bContext *C, wmOperator *op, const wmEvent *event)
Definition sculpt_uv.cc:944
static UvSculptData * uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wmEvent *event)
Definition sculpt_uv.cc:659
UvElementMap * BM_uv_element_map_create(BMesh *bm, const Scene *scene, bool uv_selected, bool use_winding, bool use_seams, bool do_islands)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
void ED_uvedit_live_unwrap_begin(Scene *scene, Object *obedit, struct wmWindow *win_modal)
void ED_space_image_get_zoom(SpaceImage *sima, const ARegion *region, float *r_zoomx, float *r_zoomy)
Scene * CTX_data_scene(const bContext *C)
void WM_event_timer_remove(wmWindowManager *wm, wmWindow *, wmTimer *timer)
static void apply_sculpt_data_constraints(UvSculptData *sculptdata, float uv[2])
Definition sculpt_uv.cc:153
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
static void HC_relaxation_iteration_uv(UvSculptData *sculptdata, const int cd_loop_uv_offset, const float mouse_coord[2], const float alpha, const float radius_sq, const float aspectRatio)
Definition sculpt_uv.cc:189
wmTimer * WM_event_timer_add(wmWindowManager *wm, wmWindow *win, const wmEventType event_type, const double time_step)
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)
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 uint uv_edge_hash(const void *key)
Definition sculpt_uv.cc:631
MINLINE float safe_divide(float a, float b)
ARegion * CTX_wm_region(const bContext *C)
static void add_weighted_edge(float(*delta_buf)[3], const UvElement *storage, const UvElement *ele_next, const UvElement *ele_prev, const float luv_next[2], const float luv_prev[2], const float weight)
Definition sculpt_uv.cc:344
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE float clamp_f(float value, float min, float max)
#define fabsf
#define sqrtf
#define MARK_BOUNDARY
Definition sculpt_uv.cc:67
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct ToolSettings * toolsettings
eSpaceImage_Flag flag
struct CurveMapping * curve_distance_falloff
wmEventType type
Definition WM_types.hh:817
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct wmOperatorType * type