f0aedae88aca632afeef2a628eea891e58cbb047
[blender.git] / source / blender / editors / sculpt_paint / sculpt_uv.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) Blender Foundation, 2002-2009
19  * All rights reserved.
20  *
21  * Contributor(s): Antony Riakiotakis
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  *
25  * UV Sculpt tools
26  *
27  */
28
29 /** \file blender/editors/sculpt_paint/sculpt_uv.c
30  *  \ingroup edsculpt
31  */
32
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_utildefines.h"
37 #include "BLI_math.h"
38 #include "BLI_ghash.h"
39
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_brush_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45
46 #include "BKE_brush.h"
47 #include "BKE_paint.h"
48 #include "BKE_context.h"
49 #include "BKE_main.h"
50 #include "BKE_depsgraph.h"
51 #include "BKE_mesh.h"
52 #include "BKE_customdata.h"
53 #include "BKE_tessmesh.h"
54
55 #include "ED_screen.h"
56 #include "ED_image.h"
57 #include "ED_mesh.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "RNA_access.h"
63 #include "RNA_define.h"
64 #include "RNA_enum_types.h"
65
66 #include "paint_intern.h"
67 #include "uvedit_intern.h"
68
69 #include "UI_view2d.h"
70
71 #define MARK_BOUNDARY   1
72
73 typedef struct UvAdjacencyElement {
74         /* pointer to original uvelement */
75         UvElement *element;
76         /* uv pointer for convenience. Caution, this points to the original UVs! */
77         float *uv;
78         /* general use flag (Used to check if Element is boundary here) */
79         char flag;
80 } UvAdjacencyElement;
81
82 typedef struct UvEdge {
83         unsigned int uv1;
84         unsigned int uv2;
85         /* general use flag (Used to check if edge is boundary here, and propagates to adjacency elements) */
86         char flag;
87 }UvEdge;
88
89 typedef struct UVInitialStrokeElement{
90         /* index to unique uv */
91         int uv;
92
93         /* strength of brush on initial position */
94         float strength;
95
96         /* initial uv position */
97         float initial_uv[2];
98 }UVInitialStrokeElement;
99
100 typedef struct UVInitialStroke{
101         /* Initial Selection,for grab brushes for instance */
102         UVInitialStrokeElement *initialSelection;
103
104         /* total initially selected UVs*/
105         int totalInitialSelected;
106
107         /* initial mouse coordinates */
108         float init_coord[2];
109 }UVInitialStroke;
110
111
112 /* custom data for uv smoothing brush */
113 typedef struct UvSculptData{
114         /* Contains the first of each set of coincident uvs.
115          * These will be used to perform smoothing on and propagate the changes
116          * to their coincident uvs */
117         UvAdjacencyElement *uv;
118
119         /* ...Is what it says */
120         int totalUniqueUvs;
121
122         /* Edges used for adjacency info, used with laplacian smoothing */
123         UvEdge *uvedges;
124
125         /* need I say more? */
126         int totalUvEdges;
127
128         /* data for initial stroke, used by tools like grab */
129         UVInitialStroke *initial_stroke;
130
131         /* timer to be used for airbrush-type brush */
132         wmTimer *timer;
133
134         /* to determine quickly adjacent uvs */
135         UvElementMap *elementMap;
136
137         /* uvsmooth Paint for fast reference */
138         Paint *uvsculpt;
139         
140         /* tool to use. duplicating here to change if modifier keys are pressed */
141         char tool;
142
143         /* store invert flag here */
144         char invert;
145 }UvSculptData;
146
147 /*********** Improved Laplacian Relaxation Operator ************************/
148 /* original code by Raul Fernandez Hernandez "farsthary"                   *
149  * adapted to uv smoothing by Antony Riakiatakis                           *
150  ***************************************************************************/
151
152 typedef struct Temp_UvData{
153         float sum_co[2], p[2], b[2], sum_b[2];
154         int ncounter;
155 }Temp_UVData;
156
157
158
159 void HC_relaxation_iteration_uv(BMEditMesh *em, UvSculptData *sculptdata, float mouse_coord[2],
160                                 float alpha, float radius, float aspectRatio)
161 {
162         Temp_UVData *tmp_uvdata;
163         float diff[2];
164         int i;
165         float radius_root = sqrt(radius);
166         Brush *brush = paint_brush(sculptdata->uvsculpt);
167
168         tmp_uvdata = (Temp_UVData *)MEM_callocN(sculptdata->totalUniqueUvs * sizeof(Temp_UVData), "Temporal data");
169
170         /* counting neighbors */
171         for (i = 0; i < sculptdata->totalUvEdges; i++) {
172                 UvEdge *tmpedge = sculptdata->uvedges+i;
173                 tmp_uvdata[tmpedge->uv1].ncounter++;
174                 tmp_uvdata[tmpedge->uv2].ncounter++;
175
176                 add_v2_v2(tmp_uvdata[tmpedge->uv2].sum_co, sculptdata->uv[tmpedge->uv1].uv);
177                 add_v2_v2(tmp_uvdata[tmpedge->uv1].sum_co, sculptdata->uv[tmpedge->uv2].uv);
178         }
179
180         for (i = 0; i < sculptdata->totalUniqueUvs; i++) {
181                 copy_v2_v2(diff,tmp_uvdata[i].sum_co);
182                 mul_v2_fl(diff,1.f/tmp_uvdata[i].ncounter);
183                 copy_v2_v2(tmp_uvdata[i].p,diff);
184
185                 tmp_uvdata[i].b[0] = diff[0] - sculptdata->uv[i].uv[0];
186                 tmp_uvdata[i].b[1] = diff[1] - sculptdata->uv[i].uv[1];
187         }
188
189         for (i = 0; i < sculptdata->totalUvEdges; i++) {
190                 UvEdge *tmpedge = sculptdata->uvedges+i;
191                 add_v2_v2(tmp_uvdata[tmpedge->uv1].sum_b, tmp_uvdata[tmpedge->uv2].b);
192                 add_v2_v2(tmp_uvdata[tmpedge->uv2].sum_b, tmp_uvdata[tmpedge->uv1].b);
193         }
194
195         for (i = 0; i < sculptdata->totalUniqueUvs; i++) {
196                 float dist;
197                 /* This is supposed to happen only if "Pin Edges" is on, since we have initialization on stroke start
198                  * If ever uv brushes get their own mode we should check for toolsettings option too */
199                 if ((sculptdata->uv[i].flag & MARK_BOUNDARY)) {
200                         continue;
201                 }
202
203                 sub_v2_v2v2(diff, sculptdata->uv[i].uv, mouse_coord);
204                 diff[1] /= aspectRatio;
205                 if ((dist = dot_v2v2(diff, diff)) <= radius) {
206                         UvElement *element;
207                         float strength;
208                         strength = alpha*brush_curve_strength(brush, sqrt(dist), radius_root);
209
210                         sculptdata->uv[i].uv[0] = (1.0-strength)*sculptdata->uv[i].uv[0] + strength*(tmp_uvdata[i].p[0] - 0.5f*(tmp_uvdata[i].b[0] + tmp_uvdata[i].sum_b[0]/tmp_uvdata[i].ncounter));
211                         sculptdata->uv[i].uv[1] = (1.0-strength)*sculptdata->uv[i].uv[1] + strength*(tmp_uvdata[i].p[1] - 0.5f*(tmp_uvdata[i].b[1] + tmp_uvdata[i].sum_b[1]/tmp_uvdata[i].ncounter));
212
213                         for (element = sculptdata->uv[i].element; element; element = element->next) {
214                                 MLoopUV *luv;
215                                 BMLoop *l;
216
217                                 if (element->separate && element != sculptdata->uv[i].element)
218                                         break;
219
220                                 l = element->l;
221                                 luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
222                                 copy_v2_v2(luv->uv, sculptdata->uv[i].uv);
223                         }
224                 }
225         }
226
227         MEM_freeN(tmp_uvdata);
228
229         return;
230 }
231
232 static void laplacian_relaxation_iteration_uv(BMEditMesh *em, UvSculptData *sculptdata, float mouse_coord[2], float alpha, float radius, float aspectRatio)
233 {
234         Temp_UVData *tmp_uvdata;
235         float diff[2];
236         int i;
237         float radius_root = sqrt(radius);
238         Brush *brush = paint_brush(sculptdata->uvsculpt);
239
240         tmp_uvdata = (Temp_UVData *)MEM_callocN(sculptdata->totalUniqueUvs * sizeof(Temp_UVData), "Temporal data");
241
242         /* counting neighbors */
243         for (i = 0; i < sculptdata->totalUvEdges; i++) {
244                 UvEdge *tmpedge = sculptdata->uvedges+i;
245                 tmp_uvdata[tmpedge->uv1].ncounter++;
246                 tmp_uvdata[tmpedge->uv2].ncounter++;
247
248                 add_v2_v2(tmp_uvdata[tmpedge->uv2].sum_co, sculptdata->uv[tmpedge->uv1].uv);
249                 add_v2_v2(tmp_uvdata[tmpedge->uv1].sum_co, sculptdata->uv[tmpedge->uv2].uv);
250         }
251
252         /* Original Lacplacian algorithm included removal of normal component of translation. here it is not
253          * needed since we translate along the UV plane always.*/
254         for (i = 0; i < sculptdata->totalUniqueUvs; i++) {
255                 copy_v2_v2(tmp_uvdata[i].p, tmp_uvdata[i].sum_co);
256                 mul_v2_fl(tmp_uvdata[i].p, 1.f/tmp_uvdata[i].ncounter);
257         }
258
259         for (i = 0; i < sculptdata->totalUniqueUvs; i++) {
260                 float dist;
261                 /* This is supposed to happen only if "Pin Edges" is on, since we have initialization on stroke start
262                  * If ever uv brushes get their own mode we should check for toolsettings option too */
263                 if ((sculptdata->uv[i].flag & MARK_BOUNDARY)) {
264                         continue;
265                 }
266
267                 sub_v2_v2v2(diff, sculptdata->uv[i].uv, mouse_coord);
268                 diff[1] /= aspectRatio;
269                 if ((dist = dot_v2v2(diff, diff)) <= radius) {
270                         UvElement *element;
271                         float strength;
272                         strength = alpha*brush_curve_strength(brush, sqrt(dist), radius_root);
273
274                         sculptdata->uv[i].uv[0] = (1.0-strength)*sculptdata->uv[i].uv[0] + strength*tmp_uvdata[i].p[0];
275                         sculptdata->uv[i].uv[1] = (1.0-strength)*sculptdata->uv[i].uv[1] + strength*tmp_uvdata[i].p[1];
276
277                         for (element = sculptdata->uv[i].element; element; element = element->next) {
278                                 MLoopUV *luv;
279                                 BMLoop *l;
280
281                                 if (element->separate && element != sculptdata->uv[i].element)
282                                         break;
283
284                                 l = element->l;
285                                 luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
286                                 copy_v2_v2(luv->uv, sculptdata->uv[i].uv);
287                         }
288                 }
289         }
290
291         MEM_freeN(tmp_uvdata);
292
293         return;
294 }
295
296
297 static void uv_sculpt_stroke_apply(bContext *C, wmOperator *op, wmEvent *event, Object *obedit)
298 {
299         float co[2], radius, radius_root;
300         Scene *scene = CTX_data_scene(C);
301         ARegion *ar = CTX_wm_region(C);
302         BMEditMesh *em = BMEdit_FromObject(obedit);
303         unsigned int tool;
304         UvSculptData *sculptdata = (UvSculptData *)op->customdata;
305         SpaceImage *sima;
306         int invert;
307         int width, height;
308         float aspectRatio;
309         float alpha, zoomx, zoomy;
310         Brush *brush = paint_brush(sculptdata->uvsculpt);
311         ToolSettings *toolsettings = CTX_data_tool_settings(C);
312         tool = sculptdata->tool;
313         invert = sculptdata->invert? -1 : 1;
314         alpha = brush_alpha(scene, brush);
315         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &co[0], &co[1]);
316
317         sima = CTX_wm_space_image(C);
318         ED_space_image_size(sima, &width, &height);
319         ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
320
321         radius = brush_size(scene, brush)/(width*zoomx);
322         aspectRatio = width/(float)height;
323
324         /* We will compare squares to save some computation */
325         radius = radius*radius;
326         radius_root = sqrt(radius);
327
328         /*
329          * Pinch Tool
330          */
331         if (tool == UV_SCULPT_TOOL_PINCH) {
332                 int i;
333                 alpha *= invert;
334                 for (i = 0; i < sculptdata->totalUniqueUvs; i++) {
335                         float dist, diff[2];
336                         /* This is supposed to happen only if "Lock Borders" is on, since we have initialization on stroke start
337                          * If ever uv brushes get their own mode we should check for toolsettings option too */
338                         if (sculptdata->uv[i].flag & MARK_BOUNDARY) {
339                                 continue;
340                         }
341
342                         sub_v2_v2v2(diff, sculptdata->uv[i].uv, co);
343                         diff[1] /= aspectRatio;
344                         if ((dist = dot_v2v2(diff, diff)) <= radius) {
345                                 UvElement *element;
346                                 float strength;
347                                 strength = alpha*brush_curve_strength(brush, sqrt(dist), radius_root);
348                                 normalize_v2(diff);
349
350                                 sculptdata->uv[i].uv[0] -= strength*diff[0]*0.001;
351                                 sculptdata->uv[i].uv[1] -= strength*diff[1]*0.001;
352
353                                 for (element = sculptdata->uv[i].element; element; element = element->next) {
354                                         MLoopUV *luv;
355                                         BMLoop *l;
356
357                                         if (element->separate && element != sculptdata->uv[i].element)
358                                                 break;
359
360                                         l = element->l;
361                                         luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
362                                         copy_v2_v2(luv->uv, sculptdata->uv[i].uv);
363                                 }
364                         }
365                 }
366         }
367
368         /*
369          * Smooth Tool
370          */
371         else if (tool == UV_SCULPT_TOOL_RELAX) {
372                 unsigned int method = toolsettings->uv_relax_method;
373                 if (method == UV_SCULPT_TOOL_RELAX_HC) {
374                         HC_relaxation_iteration_uv(em, sculptdata, co, alpha, radius, aspectRatio);
375                 }else{
376                         laplacian_relaxation_iteration_uv(em, sculptdata, co, alpha, radius, aspectRatio);
377                 }
378         }
379
380         /*
381          * Grab Tool
382          */
383         else if (tool == UV_SCULPT_TOOL_GRAB) {
384                 int i;
385                 float diff[2];
386                 sub_v2_v2v2(diff, co, sculptdata->initial_stroke->init_coord);
387
388                 for (i = 0; i < sculptdata->initial_stroke->totalInitialSelected; i++ ) {
389                         UvElement *element;
390                         int uvindex = sculptdata->initial_stroke->initialSelection[i].uv;
391                         float strength = sculptdata->initial_stroke->initialSelection[i].strength;
392                         sculptdata->uv[uvindex].uv[0] = sculptdata->initial_stroke->initialSelection[i].initial_uv[0] + strength*diff[0];
393                         sculptdata->uv[uvindex].uv[1] = sculptdata->initial_stroke->initialSelection[i].initial_uv[1] + strength*diff[1];
394
395                         for (element = sculptdata->uv[uvindex].element; element; element = element->next) {
396                                 MLoopUV *luv;
397                                 BMLoop *l;
398
399                                 if (element->separate && element != sculptdata->uv[uvindex].element)
400                                         break;
401
402                                 l = element->l;
403                                 luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
404                                 copy_v2_v2(luv->uv, sculptdata->uv[uvindex].uv);
405                         }
406                 }
407         }
408 }
409
410
411 static void uv_sculpt_stroke_exit(bContext *C, wmOperator *op)
412 {
413         UvSculptData *data = op->customdata;
414         if (data->timer) {
415                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), data->timer);
416         }
417         if (data->elementMap)
418         {
419                 EDBM_free_uv_element_map(data->elementMap);
420         }
421         if (data->uv) {
422                 MEM_freeN(data->uv);
423         }
424         if (data->uvedges) {
425                 MEM_freeN(data->uvedges);
426         }
427         if (data->initial_stroke) {
428                 if (data->initial_stroke->initialSelection) {
429                         MEM_freeN(data->initial_stroke->initialSelection);
430                 }
431                 MEM_freeN(data->initial_stroke);
432         }
433
434         MEM_freeN(data);
435         op->customdata = NULL;
436 }
437
438 static int get_uv_element_offset_from_face(UvElementMap *map, BMFace *efa, BMLoop *l, int island_index, int doIslands)
439 {
440         UvElement *element = ED_get_uv_element(map, efa, l);
441         if (!element || (doIslands && element->island != island_index)) {
442                 return -1;
443         }
444         return element - map->buf;
445 }
446
447
448 static unsigned int     uv_edge_hash(const void *key)
449 {
450         UvEdge *edge = (UvEdge *)key;
451         return 
452                 BLI_ghashutil_inthash(SET_INT_IN_POINTER(edge->uv2)) +
453                 BLI_ghashutil_inthash(SET_INT_IN_POINTER(edge->uv1));
454 }
455
456 static int uv_edge_compare(const void *a, const void *b)
457 {
458         UvEdge *edge1 = (UvEdge *)a;
459         UvEdge *edge2 = (UvEdge *)b;
460
461         if ((edge1->uv1 == edge2->uv1) && (edge1->uv2 == edge2->uv2)) {
462                 return 0;
463         }
464         return 1;
465 }
466
467
468 static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, wmEvent *event)
469 {
470         Scene *scene = CTX_data_scene(C);
471         Object *obedit = CTX_data_edit_object(C);
472         ToolSettings *ts = scene->toolsettings;
473         UvSculptData *data = MEM_callocN(sizeof(*data), "UV Smooth Brush Data");
474         BMEditMesh *em = BMEdit_FromObject(obedit);
475         BMesh *bm = em->bm;
476
477         op->customdata = data;
478
479         if (data) {
480                 int counter = 0, i;
481                 ARegion *ar= CTX_wm_region(C);
482                 float co[2];
483                 BMFace *efa;
484                 MLoopUV *luv;
485                 BMLoop *l;
486                 BMIter iter, liter;
487
488                 UvEdge *edges;
489                 GHash *edgeHash;
490                 GHashIterator* ghi;
491
492                 int do_island_optimization = !(ts->uv_sculpt_settings & UV_SCULPT_ALL_ISLANDS);
493                 int island_index = 0;
494                 /* Holds, for each UvElement in elementMap, a pointer to its unique uv.*/
495                 int *uniqueUv;
496                 data->tool = (RNA_enum_get(op->ptr, "mode") == BRUSH_STROKE_SMOOTH)? UV_SCULPT_TOOL_RELAX : ts->uv_sculpt_tool;
497                 data->invert = (RNA_enum_get(op->ptr, "mode") == BRUSH_STROKE_INVERT)? 1 : 0;
498
499                 data->uvsculpt = &ts->uvsculpt->paint;
500
501                 if (do_island_optimization) {
502                         /* We will need island information */
503                         if (ts->uv_flag & UV_SYNC_SELECTION) {
504                                 data->elementMap = EDBM_make_uv_element_map(em, 0, 1);
505                         }else{
506                                 data->elementMap = EDBM_make_uv_element_map(em, 1, 1);
507                         }
508                 }else {
509                         if (ts->uv_flag & UV_SYNC_SELECTION) {
510                                 data->elementMap = EDBM_make_uv_element_map(em, 0, 0);
511                         }else{
512                                 data->elementMap = EDBM_make_uv_element_map(em, 1, 0);
513                         }
514                 }
515
516                 if (!data->elementMap) {
517                         uv_sculpt_stroke_exit(C, op);
518                         return NULL;
519                 }
520
521                 /* Mouse coordinates, useful for some functions like grab and sculpt all islands */
522                 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &co[0], &co[1]);
523
524                 /* we need to find the active island here */
525                 if (do_island_optimization) {
526                         UvElement *element;
527                         NearestHit hit;
528                         Image *ima= CTX_data_edit_image(C);
529                         uv_find_nearest_vert(scene, ima, em, co, NULL, &hit);
530
531                         element = ED_get_uv_element(data->elementMap, hit.efa, hit.l);
532                         island_index = element->island;
533                 }
534
535
536                 /* Count 'unique' uvs */
537                 for (i = 0; i < data->elementMap->totalUVs; i++) {
538                         if (data->elementMap->buf[i].separate
539                         && (!do_island_optimization || data->elementMap->buf[i].island == island_index)) {
540                                 counter++;
541                         }
542                 }
543
544                 /* Allocate the unique uv buffers */
545                 data->uv = MEM_mallocN(sizeof(*data->uv)*counter, "uv_brush_unique_uvs");
546                 uniqueUv = MEM_mallocN(sizeof(*uniqueUv)*data->elementMap->totalUVs, "uv_brush_unique_uv_map");
547                 edgeHash = BLI_ghash_new(uv_edge_hash, uv_edge_compare, "uv_brush_edge_hash");
548                 /* we have at most totalUVs edges */
549                 edges = MEM_mallocN(sizeof(*edges)*data->elementMap->totalUVs, "uv_brush_all_edges");
550                 if (!data->uv || !uniqueUv || !edgeHash || !edges) {
551                         if (edges) {
552                                 MEM_freeN(edges);
553                         }
554                         if (uniqueUv) {
555                                 MEM_freeN(uniqueUv);
556                         }
557                         if (edgeHash) {
558                                 MEM_freeN(edgeHash);
559                         }
560                         uv_sculpt_stroke_exit(C, op);
561                         return NULL;
562                 }
563
564                 data->totalUniqueUvs = counter;
565                 /* So that we can use this as index for the UvElements */
566                 counter = -1;
567                 /* initialize the unique UVs */
568                 for (i = 0; i < bm->totvert; i++) {
569                         UvElement *element = data->elementMap->vert[i];
570                         for (; element; element = element->next) {
571                                 if (element->separate) {
572                                         if (do_island_optimization && (element->island != island_index)) {
573                                                 /* skip this uv if not on the active island */
574                                                 for (; element->next && !(element->next->separate); element = element->next)
575                                                         ;
576                                                 continue;
577                                         }
578
579                                         l = element->l;
580                                         luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
581
582                                         counter++;
583                                         data->uv[counter].element = element;
584                                         data->uv[counter].flag = 0;
585                                         data->uv[counter].uv = luv->uv;
586                                 }
587                                 /* pointer arithmetic to the rescue, as always :)*/
588                                 uniqueUv[element - data->elementMap->buf] = counter;
589                         }
590                 }
591
592
593                 /* Now, on to generate our uv connectivity data */
594                 counter = 0;
595                 BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
596                         BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, efa) {
597                                 int offset1, itmp1 = get_uv_element_offset_from_face(data->elementMap, efa, l, island_index, do_island_optimization);
598                                 int offset2, itmp2 = get_uv_element_offset_from_face(data->elementMap, efa, l->next, island_index, do_island_optimization);
599
600                                 /* Skip edge if not found(unlikely) or not on valid island */
601                                 if (itmp1 == -1 || itmp2 == -1)
602                                         continue;
603
604                                 offset1 = uniqueUv[itmp1];
605                                 offset2 = uniqueUv[itmp2];
606
607                                 edges[counter].flag = 0;
608                                 /* using an order policy, sort uvs according to address space. This avoids
609                                  * Having two different UvEdges with the same uvs on different positions  */
610                                 if (offset1 < offset2) {
611                                         edges[counter].uv1 = offset1;
612                                         edges[counter].uv2 = offset2;
613                                 }
614                                 else{
615                                         edges[counter].uv1 = offset2;
616                                         edges[counter].uv2 = offset1;
617                                 }
618                                 /* Hack! Set the value of the key to its flag. Now we can set the flag when an edge exists twice :) */
619                                 if (BLI_ghash_haskey(edgeHash, &edges[counter])) {
620                                         char *flag = BLI_ghash_lookup(edgeHash, &edges[counter]);
621                                         *flag = 1;
622                                 }
623                                 else{
624                                         /* Hack mentioned */
625                                         BLI_ghash_insert(edgeHash, &edges[counter], &edges[counter].flag);
626                                 }
627                                 counter++;
628                         }
629                 }
630
631                 MEM_freeN(uniqueUv);
632
633                 /* Allocate connectivity data, we allocate edges once */
634                 data->uvedges = MEM_mallocN(sizeof(*data->uvedges)*BLI_ghash_size(edgeHash), "uv_brush_edge_connectivity_data");
635                 if (!data->uvedges) {
636                         BLI_ghash_free(edgeHash, NULL, NULL);
637                         MEM_freeN(edges);
638                         uv_sculpt_stroke_exit(C, op);
639                         return NULL;
640                 }
641                 ghi = BLI_ghashIterator_new(edgeHash);
642                 if (!ghi) {
643                         BLI_ghash_free(edgeHash, NULL, NULL);
644                         MEM_freeN(edges);
645                         uv_sculpt_stroke_exit(C, op);
646                         return NULL;
647                 }
648                 /* fill the edges with data */
649                 for (i = 0; !BLI_ghashIterator_isDone(ghi); BLI_ghashIterator_step(ghi)) {
650                         data->uvedges[i++] = *((UvEdge *)BLI_ghashIterator_getKey(ghi));
651                 }
652                 data->totalUvEdges = BLI_ghash_size(edgeHash);
653
654                 /* cleanup temporary stuff */
655                 BLI_ghashIterator_free(ghi);
656                 BLI_ghash_free(edgeHash, NULL, NULL);
657                 MEM_freeN(edges);
658
659                 /* transfer boundary edge property to uvs */
660                 if (ts->uv_sculpt_settings & UV_SCULPT_LOCK_BORDERS) {
661                         for (i = 0; i < data->totalUvEdges; i++) {
662                                 if (!data->uvedges[i].flag) {
663                                         data->uv[data->uvedges[i].uv1].flag |= MARK_BOUNDARY;
664                                         data->uv[data->uvedges[i].uv2].flag |= MARK_BOUNDARY;
665                                 }
666                         }
667                 }
668
669                 /* Allocate initial selection for grab tool */
670                 if (data->tool == UV_SCULPT_TOOL_GRAB) {
671                         float radius, radius_root;
672                         UvSculptData *sculptdata = (UvSculptData *)op->customdata;
673                         SpaceImage *sima;
674                         int width, height;
675                         float aspectRatio;
676                         float alpha, zoomx, zoomy;
677                         Brush *brush = paint_brush(sculptdata->uvsculpt);
678
679                         alpha = brush_alpha(scene, brush);
680
681                         radius = brush_size(scene, brush);
682                         sima = CTX_wm_space_image(C);
683                         ED_space_image_size(sima, &width, &height);
684                         ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
685
686                         aspectRatio = width/(float)height;
687                         radius /= (width*zoomx);
688                         radius = radius*radius;
689                         radius_root = sqrt(radius);
690
691                         /* Allocate selection stack */
692                         data->initial_stroke = MEM_mallocN(sizeof(*data->initial_stroke), "uv_sculpt_initial_stroke");
693                         if (!data->initial_stroke) {
694                                 uv_sculpt_stroke_exit(C, op);
695                         }
696                         data->initial_stroke->initialSelection = MEM_mallocN(sizeof(*data->initial_stroke->initialSelection)*data->totalUniqueUvs, "uv_sculpt_initial_selection");
697                         if (!data->initial_stroke->initialSelection) {
698                                 uv_sculpt_stroke_exit(C, op);
699                         }
700
701                         copy_v2_v2(data->initial_stroke->init_coord, co);
702
703                         counter = 0;
704
705                         for (i = 0; i < data->totalUniqueUvs; i++) {
706                                 float dist, diff[2];
707                                 if (data->uv[i].flag & MARK_BOUNDARY) {
708                                         continue;
709                                 }
710
711                                 sub_v2_v2v2(diff, data->uv[i].uv, co);
712                                 diff[1] /= aspectRatio;
713                                 if ((dist = dot_v2v2(diff, diff)) <= radius) {
714                                         float strength;
715                                         strength = alpha*brush_curve_strength(brush, sqrt(dist), radius_root);
716
717                                         data->initial_stroke->initialSelection[counter].uv = i;
718                                         data->initial_stroke->initialSelection[counter].strength = strength;
719                                         copy_v2_v2(data->initial_stroke->initialSelection[counter].initial_uv, data->uv[i].uv);
720                                         counter++;
721                                 }
722                         }
723
724                         data->initial_stroke->totalInitialSelected = counter;
725                 }
726         }
727
728         return op->customdata;
729 }
730
731 static int uv_sculpt_stroke_invoke(bContext *C, wmOperator *op, wmEvent *event)
732 {
733         UvSculptData *data;
734         Object *obedit = CTX_data_edit_object(C);
735
736         if (!(data = uv_sculpt_stroke_init(C, op, event))) {
737                 return OPERATOR_CANCELLED;
738         }
739
740         uv_sculpt_stroke_apply(C, op, event, obedit);
741
742         data->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.001f);
743
744         if (!data->timer) {
745                 uv_sculpt_stroke_exit(C, op);
746                 return OPERATOR_CANCELLED;
747         }
748         WM_event_add_modal_handler(C, op);
749
750         return OPERATOR_RUNNING_MODAL;
751 }
752
753
754 static int uv_sculpt_stroke_modal(bContext *C, wmOperator *op, wmEvent *event)
755 {
756         UvSculptData *data = (UvSculptData *)op->customdata;
757         Object *obedit = CTX_data_edit_object(C);
758
759         switch(event->type) {
760                 case LEFTMOUSE:
761                 case MIDDLEMOUSE:
762                 case RIGHTMOUSE:
763                         uv_sculpt_stroke_exit(C, op);
764                         return OPERATOR_FINISHED;
765
766                 case MOUSEMOVE:
767                 case INBETWEEN_MOUSEMOVE:
768                         uv_sculpt_stroke_apply(C, op, event, obedit);
769                         break;
770                 case TIMER:
771                         if (event->customdata == data->timer)
772                                 uv_sculpt_stroke_apply(C, op, event, obedit);
773                         break;
774                 default:
775                         return OPERATOR_RUNNING_MODAL;
776         }
777
778         ED_region_tag_redraw(CTX_wm_region(C));
779         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
780         DAG_id_tag_update(obedit->data, 0);
781         return OPERATOR_RUNNING_MODAL;
782 }
783
784 void SCULPT_OT_uv_sculpt_stroke(wmOperatorType *ot)
785 {
786         static EnumPropertyItem stroke_mode_items[] = {
787                 {BRUSH_STROKE_NORMAL, "NORMAL", 0, "Normal", "Apply brush normally"},
788                 {BRUSH_STROKE_INVERT, "INVERT", 0, "Invert", "Invert action of brush for duration of stroke"},
789                 {BRUSH_STROKE_SMOOTH, "RELAX", 0, "Relax", "Switch brush to relax mode for duration of stroke"},
790                 {0}
791         };
792
793         /* identifiers */
794         ot->name = "Sculpt UVs";
795         ot->description = "Sculpt UVs using a brush";
796         ot->idname = "SCULPT_OT_uv_sculpt_stroke";
797
798         /* api callbacks */
799         ot->invoke = uv_sculpt_stroke_invoke;
800         ot->modal = uv_sculpt_stroke_modal;
801         ot->poll = uv_sculpt_poll;
802
803         /* flags */
804         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
805
806         /* props */
807         RNA_def_enum(ot->srna, "mode", stroke_mode_items, BRUSH_STROKE_NORMAL, "Mode", "Stroke Mode");
808 }