Merged changes in the trunk up to revision 52340.
[blender-staging.git] / source / blender / editors / mesh / editmesh_select.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) 2004 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/mesh/editmesh_select.c
29  *  \ingroup edmesh
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_math.h"
36 #include "BLI_rand.h"
37 #include "BLI_array.h"
38 #include "BLI_smallhash.h"
39 #include "BLI_heap.h"
40
41 #include "BKE_context.h"
42 #include "BKE_displist.h"
43 #include "BKE_depsgraph.h"
44 #include "BKE_report.h"
45 #include "BKE_paint.h"
46 #include "BKE_tessmesh.h"
47
48 #include "IMB_imbuf_types.h"
49 #include "IMB_imbuf.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "RNA_access.h"
55 #include "RNA_define.h"
56
57 #include "ED_mesh.h"
58 #include "ED_screen.h"
59 #include "ED_util.h"
60 #include "ED_uvedit.h"
61 #include "ED_object.h"
62 #include "ED_view3d.h"
63
64 #include "BIF_gl.h"
65
66 #include "DNA_scene_types.h"
67 #include "DNA_object_types.h"
68 #include "DNA_mesh_types.h"
69
70 #include "mesh_intern.h"
71
72 #include "UI_resources.h"
73
74 /* ****************************** MIRROR **************** */
75
76 void EDBM_select_mirrored(Object *UNUSED(obedit), BMEditMesh *em, int extend)
77 {
78         BMVert *v1, *v2;
79         BMIter iter;
80
81         BM_ITER_MESH (v1, &iter, em->bm, BM_VERTS_OF_MESH) {
82                 if (!BM_elem_flag_test(v1, BM_ELEM_SELECT) || BM_elem_flag_test(v1, BM_ELEM_HIDDEN)) {
83                         BM_elem_flag_disable(v1, BM_ELEM_TAG);
84                 }
85                 else {
86                         BM_elem_flag_enable(v1, BM_ELEM_TAG);
87                 }
88         }
89
90         EDBM_verts_mirror_cache_begin(em, TRUE);
91
92         if (!extend)
93                 EDBM_flag_disable_all(em, BM_ELEM_SELECT);
94
95         BM_ITER_MESH (v1, &iter, em->bm, BM_VERTS_OF_MESH) {
96                 if (!BM_elem_flag_test(v1, BM_ELEM_TAG) || BM_elem_flag_test(v1, BM_ELEM_HIDDEN))
97                         continue;
98
99                 v2 = EDBM_verts_mirror_get(em, v1);
100                 if (v2 && !BM_elem_flag_test(v2, BM_ELEM_HIDDEN)) {
101                         BM_vert_select_set(em->bm, v2, TRUE);
102                 }
103         }
104
105         EDBM_verts_mirror_cache_end(em);
106 }
107
108 void EDBM_automerge(Scene *scene, Object *obedit, int update)
109 {
110         BMEditMesh *em;
111         
112         if ((scene->toolsettings->automerge) &&
113             (obedit && obedit->type == OB_MESH))
114         {
115                 em = BMEdit_FromObject(obedit);
116                 if (!em)
117                         return;
118
119                 BMO_op_callf(em->bm, BMO_FLAG_DEFAULTS,
120                              "automerge verts=%hv dist=%f",
121                              BM_ELEM_SELECT, scene->toolsettings->doublimit);
122                 if (update) {
123                         DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
124                         BMEdit_RecalcTessellation(em);
125                 }
126         }
127 }
128
129 /* ****************************** SELECTION ROUTINES **************** */
130
131 unsigned int bm_solidoffs = 0, bm_wireoffs = 0, bm_vertoffs = 0;    /* set in drawobject.c ... for colorindices */
132
133 /* facilities for border select and circle select */
134 static char *selbuf = NULL;
135
136 /* opengl doesn't support concave... */
137 static void draw_triangulated(const int mcords[][2], const short tot)
138 {
139         ListBase lb = {NULL, NULL};
140         DispList *dl;
141         float *fp;
142         int a;
143         
144         /* make displist */
145         dl = MEM_callocN(sizeof(DispList), "poly disp");
146         dl->type = DL_POLY;
147         dl->parts = 1;
148         dl->nr = tot;
149         dl->verts = fp = MEM_callocN(tot * 3 * sizeof(float), "poly verts");
150         BLI_addtail(&lb, dl);
151         
152         for (a = 0; a < tot; a++, fp += 3) {
153                 fp[0] = (float)mcords[a][0];
154                 fp[1] = (float)mcords[a][1];
155         }
156         
157         /* do the fill */
158         BKE_displist_fill(&lb, &lb, 0);
159
160         /* do the draw */
161         dl = lb.first;  /* filldisplist adds in head of list */
162         if (dl->type == DL_INDEX3) {
163                 int *index;
164                 
165                 a = dl->parts;
166                 fp = dl->verts;
167                 index = dl->index;
168                 glBegin(GL_TRIANGLES);
169                 while (a--) {
170                         glVertex3fv(fp + 3 * index[0]);
171                         glVertex3fv(fp + 3 * index[1]);
172                         glVertex3fv(fp + 3 * index[2]);
173                         index += 3;
174                 }
175                 glEnd();
176         }
177         
178         BKE_displist_free(&lb);
179 }
180
181
182 /* reads rect, and builds selection array for quick lookup */
183 /* returns if all is OK */
184 int EDBM_backbuf_border_init(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
185 {
186         struct ImBuf *buf;
187         unsigned int *dr;
188         int a;
189         
190         if (vc->obedit == NULL || vc->v3d->drawtype < OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT) == 0) {
191                 return 0;
192         }
193         
194         buf = view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
195         if (buf == NULL) return 0;
196         if (bm_vertoffs == 0) return 0;
197
198         dr = buf->rect;
199         
200         /* build selection lookup */
201         selbuf = MEM_callocN(bm_vertoffs + 1, "selbuf");
202         
203         a = (xmax - xmin + 1) * (ymax - ymin + 1);
204         while (a--) {
205                 if (*dr > 0 && *dr <= bm_vertoffs)
206                         selbuf[*dr] = 1;
207                 dr++;
208         }
209         IMB_freeImBuf(buf);
210         return 1;
211 }
212
213 int EDBM_backbuf_check(unsigned int index)
214 {
215         if (selbuf == NULL) return 1;
216         if (index > 0 && index <= bm_vertoffs)
217                 return selbuf[index];
218         return 0;
219 }
220
221 void EDBM_backbuf_free(void)
222 {
223         if (selbuf) MEM_freeN(selbuf);
224         selbuf = NULL;
225 }
226
227 /* mcords is a polygon mask
228  * - grab backbuffer,
229  * - draw with black in backbuffer, 
230  * - grab again and compare
231  * returns 'OK' 
232  */
233 int EDBM_backbuf_border_mask_init(ViewContext *vc, const int mcords[][2], short tot, short xmin, short ymin, short xmax, short ymax)
234 {
235         unsigned int *dr, *drm;
236         struct ImBuf *buf, *bufmask;
237         int a;
238         
239         /* method in use for face selecting too */
240         if (vc->obedit == NULL) {
241                 if (!(paint_facesel_test(vc->obact) || paint_vertsel_test(vc->obact))) {
242                         return 0;
243                 }
244         }
245         else if (vc->v3d->drawtype < OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT) == 0) {
246                 return 0;
247         }
248
249         buf = view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
250         if (buf == NULL) return 0;
251         if (bm_vertoffs == 0) return 0;
252
253         dr = buf->rect;
254
255         /* draw the mask */
256         glDisable(GL_DEPTH_TEST);
257         
258         glColor3ub(0, 0, 0);
259         
260         /* yah, opengl doesn't do concave... tsk! */
261         ED_region_pixelspace(vc->ar);
262         draw_triangulated(mcords, tot);
263         
264         glBegin(GL_LINE_LOOP);  /* for zero sized masks, lines */
265         for (a = 0; a < tot; a++) {
266                 glVertex2iv(mcords[a]);
267         }
268         glEnd();
269         
270         glFinish(); /* to be sure readpixels sees mask */
271         
272         /* grab mask */
273         bufmask = view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
274
275         if (bufmask == NULL) {
276                 return 0; /* only when mem alloc fails, go crash somewhere else! */
277         }
278         else {
279                 drm = bufmask->rect;
280         }
281
282         /* build selection lookup */
283         selbuf = MEM_callocN(bm_vertoffs + 1, "selbuf");
284         
285         a = (xmax - xmin + 1) * (ymax - ymin + 1);
286         while (a--) {
287                 if (*dr > 0 && *dr <= bm_vertoffs && *drm == 0) selbuf[*dr] = 1;
288                 dr++; drm++;
289         }
290         IMB_freeImBuf(buf);
291         IMB_freeImBuf(bufmask);
292
293         return 1;
294 }
295
296 /* circle shaped sample area */
297 int EDBM_backbuf_circle_init(ViewContext *vc, short xs, short ys, short rads)
298 {
299         struct ImBuf *buf;
300         unsigned int *dr;
301         short xmin, ymin, xmax, ymax, xc, yc;
302         int radsq;
303         
304         /* method in use for face selecting too */
305         if (vc->obedit == NULL) {
306                 if (!(paint_facesel_test(vc->obact) || paint_vertsel_test(vc->obact))) {
307                         return 0;
308                 }
309         }
310         else if (vc->v3d->drawtype < OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT) == 0) return 0;
311         
312         xmin = xs - rads; xmax = xs + rads;
313         ymin = ys - rads; ymax = ys + rads;
314         buf = view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
315         if (bm_vertoffs == 0) return 0;
316         if (buf == NULL) return 0;
317
318         dr = buf->rect;
319         
320         /* build selection lookup */
321         selbuf = MEM_callocN(bm_vertoffs + 1, "selbuf");
322         radsq = rads * rads;
323         for (yc = -rads; yc <= rads; yc++) {
324                 for (xc = -rads; xc <= rads; xc++, dr++) {
325                         if (xc * xc + yc * yc < radsq) {
326                                 if (*dr > 0 && *dr <= bm_vertoffs) selbuf[*dr] = 1;
327                         }
328                 }
329         }
330
331         IMB_freeImBuf(buf);
332         return 1;
333         
334 }
335
336 static void findnearestvert__doClosest(void *userData, BMVert *eve, const float screen_co[2], int index)
337 {
338         struct { float mval_fl[2], pass, select, strict; float dist, lastIndex, closestIndex; BMVert *closest; } *data = userData;
339
340         if (data->pass == 0) {
341                 if (index <= data->lastIndex)
342                         return;
343         }
344         else {
345                 if (index > data->lastIndex)
346                         return;
347         }
348
349         if (data->dist > 3) {
350                 float dist_test = len_manhattan_v2v2(data->mval_fl, screen_co);
351                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT) == data->select) {
352                         if (data->strict == 1) {
353                                 return;
354                         }
355                         else {
356                                 dist_test += 5;
357                         }
358                 }
359
360                 if (dist_test < data->dist) {
361                         data->dist = dist_test;
362                         data->closest = eve;
363                         data->closestIndex = index;
364                 }
365         }
366 }
367
368
369
370
371 static unsigned int findnearestvert__backbufIndextest(void *handle, unsigned int index)
372 {
373         BMEditMesh *em = (BMEditMesh *)handle;
374         BMVert *eve = BM_vert_at_index(em->bm, index - 1);
375
376         if (eve && BM_elem_flag_test(eve, BM_ELEM_SELECT)) return 0;
377         return 1;
378 }
379 /**
380  * findnearestvert
381  * 
382  * dist (in/out): minimal distance to the nearest and at the end, actual distance
383  * sel: selection bias
384  *      if SELECT, selected vertice are given a 5 pixel bias to make them further than unselect verts
385  *      if 0, unselected vertice are given the bias
386  * strict: if 1, the vertice corresponding to the sel parameter are ignored and not just biased 
387  */
388 BMVert *EDBM_vert_find_nearest(ViewContext *vc, float *r_dist, const short sel, const short strict)
389 {
390         if (vc->v3d->drawtype > OB_WIRE && (vc->v3d->flag & V3D_ZBUF_SELECT)) {
391                 float distance;
392                 unsigned int index;
393                 BMVert *eve;
394                 
395                 if (strict) {
396                         index = view3d_sample_backbuf_rect(vc, vc->mval, 50, bm_wireoffs, 0xFFFFFF, &distance,
397                                                            strict, vc->em, findnearestvert__backbufIndextest);
398                 }
399                 else {
400                         index = view3d_sample_backbuf_rect(vc, vc->mval, 50, bm_wireoffs, 0xFFFFFF, &distance,
401                                                            0, NULL, NULL);
402                 }
403                 
404                 eve = BM_vert_at_index(vc->em->bm, index - 1);
405                 
406                 if (eve && distance < *r_dist) {
407                         *r_dist = distance;
408                         return eve;
409                 }
410                 else {
411                         return NULL;
412                 }
413                         
414         }
415         else {
416                 struct { float mval_fl[2], pass, select, strict; float dist, lastIndex, closestIndex; BMVert *closest; } data;
417                 static int lastSelectedIndex = 0;
418                 static BMVert *lastSelected = NULL;
419                 
420                 if (lastSelected && BM_vert_at_index(vc->em->bm, lastSelectedIndex) != lastSelected) {
421                         lastSelectedIndex = 0;
422                         lastSelected = NULL;
423                 }
424
425                 data.lastIndex = lastSelectedIndex;
426                 data.mval_fl[0] = vc->mval[0];
427                 data.mval_fl[1] = vc->mval[1];
428                 data.select = sel;
429                 data.dist = *r_dist;
430                 data.strict = strict;
431                 data.closest = NULL;
432                 data.closestIndex = 0;
433
434                 data.pass = 0;
435
436                 ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
437
438                 mesh_foreachScreenVert(vc, findnearestvert__doClosest, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
439
440                 if (data.dist > 3) {
441                         data.pass = 1;
442                         mesh_foreachScreenVert(vc, findnearestvert__doClosest, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
443                 }
444
445                 *r_dist = data.dist;
446                 lastSelected = data.closest;
447                 lastSelectedIndex = data.closestIndex;
448
449                 return data.closest;
450         }
451 }
452
453 /* returns labda for closest distance v1 to line-piece v2 - v3 */
454 float labda_PdistVL2Dfl(const float v1[2], const float v2[2], const float v3[2])
455 {
456         float rc[2], len;
457         
458         rc[0] = v3[0] - v2[0];
459         rc[1] = v3[1] - v2[1];
460         len = rc[0] * rc[0] + rc[1] * rc[1];
461         if (len == 0.0f)
462                 return 0.0f;
463         
464         return (rc[0] * (v1[0] - v2[0]) + rc[1] * (v1[1] - v2[1])) / len;
465 }
466
467 /* note; uses v3d, so needs active 3d window */
468 static void findnearestedge__doClosest(void *userData, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int UNUSED(index))
469 {
470         struct { ViewContext vc; float mval_fl[2]; float dist; BMEdge *closest; } *data = userData;
471         int distance;
472
473         distance = dist_to_line_segment_v2(data->mval_fl, screen_co_a, screen_co_b);
474                 
475         if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
476                 distance += 5;
477         }
478
479         if (distance < data->dist) {
480                 if (data->vc.rv3d->rflag & RV3D_CLIPPING) {
481                         float labda = labda_PdistVL2Dfl(data->mval_fl, screen_co_a, screen_co_b);
482                         float vec[3];
483
484                         vec[0] = eed->v1->co[0] + labda * (eed->v2->co[0] - eed->v1->co[0]);
485                         vec[1] = eed->v1->co[1] + labda * (eed->v2->co[1] - eed->v1->co[1]);
486                         vec[2] = eed->v1->co[2] + labda * (eed->v2->co[2] - eed->v1->co[2]);
487
488                         if (ED_view3d_clipping_test(data->vc.rv3d, vec, TRUE) == 0) {
489                                 data->dist = distance;
490                                 data->closest = eed;
491                         }
492                 }
493                 else {
494                         data->dist = distance;
495                         data->closest = eed;
496                 }
497         }
498 }
499 BMEdge *EDBM_edge_find_nearest(ViewContext *vc, float *r_dist)
500 {
501
502         if (vc->v3d->drawtype > OB_WIRE && (vc->v3d->flag & V3D_ZBUF_SELECT)) {
503                 float distance;
504                 unsigned int index;
505                 BMEdge *eed;
506                 
507                 view3d_validate_backbuf(vc);
508                 
509                 index = view3d_sample_backbuf_rect(vc, vc->mval, 50, bm_solidoffs, bm_wireoffs, &distance, 0, NULL, NULL);
510                 eed = BM_edge_at_index(vc->em->bm, index - 1);
511                 
512                 if (eed && distance < *r_dist) {
513                         *r_dist = distance;
514                         return eed;
515                 }
516                 else {
517                         return NULL;
518                 }
519         }
520         else {
521                 struct { ViewContext vc; float mval_fl[2]; float dist; BMEdge *closest; } data;
522
523                 data.vc = *vc;
524                 data.mval_fl[0] = vc->mval[0];
525                 data.mval_fl[1] = vc->mval[1];
526                 data.dist = *r_dist;
527                 data.closest = NULL;
528                 ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
529
530                 mesh_foreachScreenEdge(vc, findnearestedge__doClosest, &data, V3D_PROJ_TEST_CLIP_WIN);
531
532                 *r_dist = data.dist;
533                 return data.closest;
534         }
535 }
536
537 static void findnearestface__getDistance(void *userData, BMFace *efa, const float screen_co[2], int UNUSED(index))
538 {
539         struct { float mval_fl[2]; float dist; BMFace *toFace; } *data = userData;
540
541         if (efa == data->toFace) {
542                 const float dist_test = len_manhattan_v2v2(data->mval_fl, screen_co);
543
544                 if (dist_test < data->dist) {
545                         data->dist = dist_test;
546                 }
547         }
548 }
549 static void findnearestface__doClosest(void *userData, BMFace *efa, const float screen_co[2], int index)
550 {
551         struct { float mval_fl[2], pass; float dist, lastIndex, closestIndex; BMFace *closest; } *data = userData;
552
553         if (data->pass == 0) {
554                 if (index <= data->lastIndex)
555                         return;
556         }
557         else {
558                 if (index > data->lastIndex)
559                         return;
560         }
561
562         if (data->dist > 3) {
563                 const float dist_test = len_manhattan_v2v2(data->mval_fl, screen_co);
564
565                 if (dist_test < data->dist) {
566                         data->dist = dist_test;
567                         data->closest = efa;
568                         data->closestIndex = index;
569                 }
570         }
571 }
572
573 BMFace *EDBM_face_find_nearest(ViewContext *vc, float *r_dist)
574 {
575
576         if (vc->v3d->drawtype > OB_WIRE && (vc->v3d->flag & V3D_ZBUF_SELECT)) {
577                 unsigned int index;
578                 BMFace *efa;
579
580                 view3d_validate_backbuf(vc);
581
582                 index = view3d_sample_backbuf(vc, vc->mval[0], vc->mval[1]);
583                 efa = BM_face_at_index(vc->em->bm, index - 1);
584                 
585                 if (efa) {
586                         struct { float mval_fl[2]; float dist; BMFace *toFace; } data;
587
588                         data.mval_fl[0] = vc->mval[0];
589                         data.mval_fl[1] = vc->mval[1];
590                         data.dist = 0x7FFF;     /* largest short */
591                         data.toFace = efa;
592
593                         mesh_foreachScreenFace(vc, findnearestface__getDistance, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
594
595                         if ((vc->em->selectmode == SCE_SELECT_FACE) || (data.dist < *r_dist)) {  /* only faces, no dist check */
596                                 *r_dist = data.dist;
597                                 return efa;
598                         }
599                 }
600                 
601                 return NULL;
602         }
603         else {
604                 struct { float mval_fl[2], pass; float dist, lastIndex, closestIndex; BMFace *closest; } data;
605                 static int lastSelectedIndex = 0;
606                 static BMFace *lastSelected = NULL;
607
608                 if (lastSelected && BM_face_at_index(vc->em->bm, lastSelectedIndex) != lastSelected) {
609                         lastSelectedIndex = 0;
610                         lastSelected = NULL;
611                 }
612
613                 data.lastIndex = lastSelectedIndex;
614                 data.mval_fl[0] = vc->mval[0];
615                 data.mval_fl[1] = vc->mval[1];
616                 data.dist = *r_dist;
617                 data.closest = NULL;
618                 data.closestIndex = 0;
619                 ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
620
621                 data.pass = 0;
622                 mesh_foreachScreenFace(vc, findnearestface__doClosest, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
623
624                 if (data.dist > 3.0f) {
625                         data.pass = 1;
626                         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
627                         mesh_foreachScreenFace(vc, findnearestface__doClosest, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
628                 }
629
630                 *r_dist = data.dist;
631                 lastSelected = data.closest;
632                 lastSelectedIndex = data.closestIndex;
633
634                 return data.closest;
635         }
636 }
637
638 /* best distance based on screen coords. 
639  * use em->selectmode to define how to use 
640  * selected vertices and edges get disadvantage
641  * return 1 if found one
642  */
643 static int unified_findnearest(ViewContext *vc, BMVert **r_eve, BMEdge **r_eed, BMFace **r_efa)
644 {
645         BMEditMesh *em = vc->em;
646         float dist = 75.0f;
647         
648         *r_eve = NULL;
649         *r_eed = NULL;
650         *r_efa = NULL;
651         
652         /* no afterqueue (yet), so we check it now, otherwise the em_xxxofs indices are bad */
653         view3d_validate_backbuf(vc);
654         
655         if (em->selectmode & SCE_SELECT_VERTEX)
656                 *r_eve = EDBM_vert_find_nearest(vc, &dist, BM_ELEM_SELECT, 0);
657         if (em->selectmode & SCE_SELECT_FACE)
658                 *r_efa = EDBM_face_find_nearest(vc, &dist);
659
660         dist -= 20; /* since edges select lines, we give dots advantage of 20 pix */
661         if (em->selectmode & SCE_SELECT_EDGE)
662                 *r_eed = EDBM_edge_find_nearest(vc, &dist);
663
664         /* return only one of 3 pointers, for frontbuffer redraws */
665         if (*r_eed) {
666                 *r_efa = NULL; *r_eve = NULL;
667         }
668         else if (*r_efa) {
669                 *r_eve = NULL;
670         }
671         
672         return (*r_eve || *r_eed || *r_efa);
673 }
674
675 /* ****************  SIMILAR "group" SELECTS. FACE, EDGE AND VERTEX ************** */
676 static EnumPropertyItem prop_similar_compare_types[] = {
677         {SIM_CMP_EQ, "EQUAL", 0, "Equal", ""},
678         {SIM_CMP_GT, "GREATER", 0, "Greater", ""},
679         {SIM_CMP_LT, "LESS", 0, "Less", ""},
680
681         {0, NULL, 0, NULL, NULL}
682 };
683
684 static EnumPropertyItem prop_similar_types[] = {
685         {SIMVERT_NORMAL, "NORMAL", 0, "Normal", ""},
686         {SIMVERT_FACE, "FACE", 0, "Amount of Adjacent Faces", ""},
687         {SIMVERT_VGROUP, "VGROUP", 0, "Vertex Groups", ""},
688         {SIMVERT_EDGE, "EDGE", 0, "Amount of connecting edges", ""},
689
690         {SIMEDGE_LENGTH, "LENGTH", 0, "Length", ""},
691         {SIMEDGE_DIR, "DIR", 0, "Direction", ""},
692         {SIMEDGE_FACE, "FACE", 0, "Amount of Faces Around an Edge", ""},
693         {SIMEDGE_FACE_ANGLE, "FACE_ANGLE", 0, "Face Angles", ""},
694         {SIMEDGE_CREASE, "CREASE", 0, "Crease", ""},
695         {SIMEDGE_BEVEL, "BEVEL", 0, "Bevel", ""},
696         {SIMEDGE_SEAM, "SEAM", 0, "Seam", ""},
697         {SIMEDGE_SHARP, "SHARP", 0, "Sharpness", ""},
698         {SIMEDGE_FREESTYLE, "FREESTYLE_EDGE", 0, "Freestyle Edge Marks", ""},
699
700         {SIMFACE_MATERIAL, "MATERIAL", 0, "Material", ""},
701         {SIMFACE_IMAGE, "IMAGE", 0, "Image", ""},
702         {SIMFACE_AREA, "AREA", 0, "Area", ""},
703         {SIMFACE_SIDES, "SIDES", 0, "Polygon Sides", ""},
704         {SIMFACE_PERIMETER, "PERIMETER", 0, "Perimeter", ""},
705         {SIMFACE_NORMAL, "NORMAL", 0, "Normal", ""},
706         {SIMFACE_COPLANAR, "COPLANAR", 0, "Co-planar", ""},
707         {SIMFACE_FREESTYLE, "FREESTYLE_FACE", 0, "Freestyle Face Marks", ""},
708
709         {0, NULL, 0, NULL, NULL}
710 };
711
712 /* selects new faces/edges/verts based on the existing selection */
713
714 static int similar_face_select_exec(bContext *C, wmOperator *op)
715 {
716         Object *ob = CTX_data_edit_object(C);
717         BMEditMesh *em = BMEdit_FromObject(ob);
718         BMOperator bmop;
719
720         /* get the type from RNA */
721         const int type = RNA_enum_get(op->ptr, "type");
722         const float thresh = RNA_float_get(op->ptr, "threshold");
723         const int compare = RNA_enum_get(op->ptr, "compare");
724
725         /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
726         EDBM_op_init(em, &bmop, op,
727                      "similar_faces faces=%hf type=%i thresh=%f compare=%i",
728                      BM_ELEM_SELECT, type, thresh, compare);
729
730         /* execute the operator */
731         BMO_op_exec(em->bm, &bmop);
732
733         /* clear the existing selection */
734         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
735
736         /* select the output */
737         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "faceout", BM_ALL, BM_ELEM_SELECT, TRUE);
738
739         /* finish the operator */
740         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
741                 return OPERATOR_CANCELLED;
742         }
743
744         EDBM_update_generic(C, em, FALSE);
745
746         /* we succeeded */
747         return OPERATOR_FINISHED;
748 }       
749
750 /* ***************************************************** */
751
752 /* EDGE GROUP */
753
754 /* wrap the above function but do selection flushing edge to face */
755 static int similar_edge_select_exec(bContext *C, wmOperator *op)
756 {
757         Object *ob = CTX_data_edit_object(C);
758         BMEditMesh *em = BMEdit_FromObject(ob);
759         BMOperator bmop;
760
761         /* get the type from RNA */
762         const int type = RNA_enum_get(op->ptr, "type");
763         const float thresh = RNA_float_get(op->ptr, "threshold");
764         const int compare = RNA_enum_get(op->ptr, "compare");
765
766         /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
767         EDBM_op_init(em, &bmop, op,
768                      "similar_edges edges=%he type=%i thresh=%f compare=%i",
769                      BM_ELEM_SELECT, type, thresh, compare);
770
771         /* execute the operator */
772         BMO_op_exec(em->bm, &bmop);
773
774         /* clear the existing selection */
775         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
776
777         /* select the output */
778         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "edgeout", BM_ALL, BM_ELEM_SELECT, TRUE);
779         EDBM_selectmode_flush(em);
780
781         /* finish the operator */
782         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
783                 return OPERATOR_CANCELLED;
784         }
785
786         EDBM_update_generic(C, em, FALSE);
787
788         /* we succeeded */
789         return OPERATOR_FINISHED;
790 }
791
792 /* ********************************* */
793
794 /*
795  * VERT GROUP
796  * mode 1: same normal
797  * mode 2: same number of face users
798  * mode 3: same vertex groups
799  */
800 static int similar_vert_select_exec(bContext *C, wmOperator *op)
801 {
802         Object *ob = CTX_data_edit_object(C);
803         BMEditMesh *em = BMEdit_FromObject(ob);
804         BMOperator bmop;
805         /* get the type from RNA */
806         const int type = RNA_enum_get(op->ptr, "type");
807         float thresh = RNA_float_get(op->ptr, "threshold");
808         const int compare = RNA_enum_get(op->ptr, "compare");
809
810         /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
811         EDBM_op_init(em, &bmop, op,
812                      "similar_verts verts=%hv type=%i thresh=%f compare=%i",
813                      BM_ELEM_SELECT, type, thresh, compare);
814
815         /* execute the operator */
816         BMO_op_exec(em->bm, &bmop);
817
818         /* clear the existing selection */
819         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
820
821         /* select the output */
822         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "vertout", BM_ALL, BM_ELEM_SELECT, TRUE);
823
824         /* finish the operator */
825         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
826                 return OPERATOR_CANCELLED;
827         }
828
829         EDBM_selectmode_flush(em);
830
831         EDBM_update_generic(C, em, FALSE);
832
833         /* we succeeded */
834         return OPERATOR_FINISHED;
835 }
836
837 static int edbm_select_similar_exec(bContext *C, wmOperator *op)
838 {
839         ToolSettings *ts = CTX_data_tool_settings(C);
840         PropertyRNA *prop = RNA_struct_find_property(op->ptr, "threshold");
841
842         const int type = RNA_enum_get(op->ptr, "type");
843
844         if (!RNA_property_is_set(op->ptr, prop)) {
845                 RNA_property_float_set(op->ptr, prop, ts->select_thresh);
846         }
847         else {
848                 ts->select_thresh = RNA_property_float_get(op->ptr, prop);
849         }
850
851         if      (type < 100) return similar_vert_select_exec(C, op);
852         else if (type < 200) return similar_edge_select_exec(C, op);
853         else                 return similar_face_select_exec(C, op);
854 }
855
856 static EnumPropertyItem *select_similar_type_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop),
857                                                    int *free)
858 {
859         Object *obedit;
860
861         if (!C) /* needed for docs and i18n tools */
862                 return prop_similar_types;
863
864         obedit = CTX_data_edit_object(C);
865
866         if (obedit && obedit->type == OB_MESH) {
867                 EnumPropertyItem *item = NULL;
868                 int a, totitem = 0;
869                 BMEditMesh *em = BMEdit_FromObject(obedit);
870
871                 if (em->selectmode & SCE_SELECT_VERTEX) {
872                         for (a = SIMVERT_NORMAL; a < SIMEDGE_LENGTH; a++) {
873                                 RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
874                         }
875                 }
876                 else if (em->selectmode & SCE_SELECT_EDGE) {
877                         for (a = SIMEDGE_LENGTH; a < SIMFACE_MATERIAL; a++) {
878                                 RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
879                         }
880                 }
881                 else if (em->selectmode & SCE_SELECT_FACE) {
882                         for (a = SIMFACE_MATERIAL; a <= SIMFACE_FREESTYLE; a++) {
883                                 RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
884                         }
885                 }
886                 RNA_enum_item_end(&item, &totitem);
887
888                 *free = 1;
889
890                 return item;
891         }
892
893         return NULL;
894 }
895
896 void MESH_OT_select_similar(wmOperatorType *ot)
897 {
898         PropertyRNA *prop;
899
900         /* identifiers */
901         ot->name = "Select Similar";
902         ot->idname = "MESH_OT_select_similar";
903         ot->description = "Select similar vertices, edges or faces by property types";
904         
905         /* api callbacks */
906         ot->invoke = WM_menu_invoke;
907         ot->exec = edbm_select_similar_exec;
908         ot->poll = ED_operator_editmesh;
909         
910         /* flags */
911         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
912         
913         /* properties */
914         prop = ot->prop = RNA_def_enum(ot->srna, "type", prop_similar_types, SIMVERT_NORMAL, "Type", "");
915         RNA_def_enum_funcs(prop, select_similar_type_itemf);
916
917         RNA_def_enum(ot->srna, "compare", prop_similar_compare_types, SIM_CMP_EQ, "Compare", "");
918
919         RNA_def_float(ot->srna, "threshold", 0.0, 0.0, 1.0, "Threshold", "", 0.0, 1.0);
920 }
921
922
923 /* ****************  Mode Select *************** */
924
925 static int edbm_select_mode_exec(bContext *C, wmOperator *op)
926 {
927         const int type       = RNA_enum_get(op->ptr,    "type");
928         const int action     = RNA_enum_get(op->ptr,    "action");
929         const int use_extend = RNA_boolean_get(op->ptr, "use_extend");
930         const int use_expand = RNA_boolean_get(op->ptr, "use_expand");
931
932         if (EDBM_selectmode_toggle(C, type, action, use_extend, use_expand)) {
933                 return OPERATOR_FINISHED;
934         }
935         else {
936                 return OPERATOR_CANCELLED;
937         }
938 }
939
940 static int edbm_select_mode_invoke(bContext *C, wmOperator *op, wmEvent *event)
941 {
942         // RNA_enum_set(op->ptr, "type");  /* type must be set already */
943         RNA_boolean_set(op->ptr, "use_extend", event->shift);
944         RNA_boolean_set(op->ptr, "use_expand", event->ctrl);
945         return edbm_select_mode_exec(C, op);
946 }
947
948 void MESH_OT_select_mode(wmOperatorType *ot)
949 {
950         PropertyRNA *prop;
951
952         static EnumPropertyItem elem_items[] = {
953                 {SCE_SELECT_VERTEX, "VERT", ICON_VERTEXSEL, "Vertices", ""},
954                 {SCE_SELECT_EDGE,   "EDGE", ICON_EDGESEL, "Edges", ""},
955                 {SCE_SELECT_FACE,   "FACE", ICON_FACESEL, "Faces", ""},
956                 {0, NULL, 0, NULL, NULL},
957         };
958
959         static EnumPropertyItem actions_items[] = {
960                 {0, "DISABLE", 0, "Disable", "Disable selected markers"},
961                 {1, "ENABLE", 0, "Enable", "Enable selected markers"},
962                 {2, "TOGGLE", 0, "Toggle", "Toggle disabled flag for selected markers"},
963                 {0, NULL, 0, NULL, NULL}
964         };
965
966         /* identifiers */
967         ot->name = "Select Mode";
968         ot->idname = "MESH_OT_select_mode";
969         ot->description = "Change selection mode";
970
971         /* api callbacks */
972         ot->invoke = edbm_select_mode_invoke;
973         ot->exec = edbm_select_mode_exec;
974         ot->poll = ED_operator_editmesh;
975
976         /* flags */
977         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
978
979         /* properties */
980         prop = RNA_def_boolean(ot->srna, "use_extend", FALSE, "Extend", "");
981         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
982         prop = RNA_def_boolean(ot->srna, "use_expand", FALSE, "Expand", "");
983         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
984         ot->prop = prop = RNA_def_enum(ot->srna, "type", elem_items, 0, "Type", "");
985         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
986
987         RNA_def_enum(ot->srna, "action", actions_items, 2, "Action", "Selection action to execute");
988 }
989
990 /* ***************************************************** */
991
992 /* ****************  LOOP SELECTS *************** */
993
994 static void walker_select(BMEditMesh *em, int walkercode, void *start, int select)
995 {
996         BMesh *bm = em->bm;
997         BMElem *ele;
998         BMWalker walker;
999
1000         BMW_init(&walker, bm, walkercode,
1001                  BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
1002                  BMW_FLAG_TEST_HIDDEN,
1003                  BMW_NIL_LAY);
1004         ele = BMW_begin(&walker, start);
1005         for (; ele; ele = BMW_step(&walker)) {
1006                 if (!select) {
1007                         BM_select_history_remove(bm, ele);
1008                 }
1009                 BM_elem_select_set(bm, ele, select);
1010         }
1011         BMW_end(&walker);
1012 }
1013
1014 static int edbm_loop_multiselect_exec(bContext *C, wmOperator *op)
1015 {
1016         Object *obedit = CTX_data_edit_object(C);
1017         BMEditMesh *em = BMEdit_FromObject(obedit);
1018         BMEdge *eed;
1019         BMEdge **edarray;
1020         int edindex;
1021         int looptype = RNA_boolean_get(op->ptr, "ring");
1022         
1023         BMIter iter;
1024         int totedgesel = 0;
1025
1026         BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
1027                 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1028                         totedgesel++;
1029                 }
1030         }
1031         
1032         edarray = MEM_mallocN(sizeof(BMEdge *) * totedgesel, "edge array");
1033         edindex = 0;
1034         
1035         BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
1036                 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1037                         edarray[edindex] = eed;
1038                         edindex++;
1039                 }
1040         }
1041         
1042         if (looptype) {
1043                 for (edindex = 0; edindex < totedgesel; edindex += 1) {
1044                         eed = edarray[edindex];
1045                         walker_select(em, BMW_EDGERING, eed, TRUE);
1046                 }
1047                 EDBM_selectmode_flush(em);
1048         }
1049         else {
1050                 for (edindex = 0; edindex < totedgesel; edindex += 1) {
1051                         eed = edarray[edindex];
1052                         walker_select(em, BMW_LOOP, eed, TRUE);
1053                 }
1054                 EDBM_selectmode_flush(em);
1055         }
1056         MEM_freeN(edarray);
1057 //      if (EM_texFaceCheck())
1058         
1059         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
1060
1061         return OPERATOR_FINISHED;
1062 }
1063
1064 void MESH_OT_loop_multi_select(wmOperatorType *ot)
1065 {
1066         /* identifiers */
1067         ot->name = "Multi Select Loops";
1068         ot->idname = "MESH_OT_loop_multi_select";
1069         ot->description = "Select a loop of connected edges by connection type";
1070         
1071         /* api callbacks */
1072         ot->exec = edbm_loop_multiselect_exec;
1073         ot->poll = ED_operator_editmesh;
1074         
1075         /* flags */
1076         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1077         
1078         /* properties */
1079         RNA_def_boolean(ot->srna, "ring", 0, "Ring", "");
1080 }
1081
1082                 
1083 /* ***************** MAIN MOUSE SELECTION ************** */
1084
1085
1086 /* ***************** loop select (non modal) ************** */
1087
1088 static void mouse_mesh_loop(bContext *C, int mval[2], short extend, short ring)
1089 {
1090         ViewContext vc;
1091         BMEditMesh *em;
1092         BMEdge *eed;
1093         int select = TRUE;
1094         float dist = 50.0f;
1095         float mvalf[2];
1096
1097         em_setup_viewcontext(C, &vc);
1098         mvalf[0] = (float)(vc.mval[0] = mval[0]);
1099         mvalf[1] = (float)(vc.mval[1] = mval[1]);
1100         em = vc.em;
1101
1102         /* no afterqueue (yet), so we check it now, otherwise the bm_xxxofs indices are bad */
1103         view3d_validate_backbuf(&vc);
1104
1105         eed = EDBM_edge_find_nearest(&vc, &dist);
1106         if (eed) {
1107                 if (extend == 0) {
1108                         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
1109                 }
1110         
1111                 if (BM_elem_flag_test(eed, BM_ELEM_SELECT) == 0) {
1112                         select = TRUE;
1113                 }
1114                 else if (extend) {
1115                         select = FALSE;
1116                 }
1117
1118                 if (em->selectmode & SCE_SELECT_FACE) {
1119                         walker_select(em, BMW_FACELOOP, eed, select);
1120                 }
1121                 else if (em->selectmode & SCE_SELECT_EDGE) {
1122                         if (ring)
1123                                 walker_select(em, BMW_EDGERING, eed, select);
1124                         else
1125                                 walker_select(em, BMW_LOOP, eed, select);
1126                 }
1127                 else if (em->selectmode & SCE_SELECT_VERTEX) {
1128                         if (ring)
1129                                 walker_select(em, BMW_EDGERING, eed, select);
1130
1131                         else
1132                                 walker_select(em, BMW_LOOP, eed, select);
1133                 }
1134
1135                 EDBM_selectmode_flush(em);
1136
1137                 /* sets as active, useful for other tools */
1138                 if (select) {
1139                         if (em->selectmode & SCE_SELECT_VERTEX) {
1140                                 /* Find nearest vert from mouse
1141                                  * (initialize to large values incase only one vertex can be projected) */
1142                                 float v1_co[2], v2_co[2];
1143                                 float length_1 = FLT_MAX;
1144                                 float length_2 = FLT_MAX;
1145
1146                                 /* We can't be sure this has already been set... */
1147                                 ED_view3d_init_mats_rv3d(vc.obedit, vc.rv3d);
1148
1149                                 if (ED_view3d_project_float_object(vc.ar, eed->v1->co, v1_co, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
1150                                         length_1 = len_squared_v2v2(mvalf, v1_co);
1151                                 }
1152
1153                                 if (ED_view3d_project_float_object(vc.ar, eed->v2->co, v2_co, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
1154                                         length_2 = len_squared_v2v2(mvalf, v2_co);
1155                                 }
1156 #if 0
1157                                 printf("mouse to v1: %f\nmouse to v2: %f\n", len_squared_v2v2(mvalf, v1_co),
1158                                        len_squared_v2v2(mvalf, v2_co));
1159 #endif
1160                                 BM_select_history_store(em->bm, (length_1 < length_2) ? eed->v1 : eed->v2);
1161                         }
1162                         else if (em->selectmode & SCE_SELECT_EDGE) {
1163                                 BM_select_history_store(em->bm, eed);
1164                         }
1165                         else if (em->selectmode & SCE_SELECT_FACE) {
1166                                 /* Select the face of eed which is the nearest of mouse. */
1167                                 BMFace *f, *efa = NULL;
1168                                 BMIter iterf;
1169                                 float best_dist = MAXFLOAT;
1170
1171                                 /* We can't be sure this has already been set... */
1172                                 ED_view3d_init_mats_rv3d(vc.obedit, vc.rv3d);
1173
1174                                 BM_ITER_ELEM(f, &iterf, eed, BM_FACES_OF_EDGE) {
1175                                         if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
1176                                                 float cent[3];
1177                                                 float co[2], tdist;
1178
1179                                                 BM_face_calc_center_mean(f, cent);
1180                                                 if (ED_view3d_project_float_object(vc.ar, cent, co, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
1181                                                         tdist = len_squared_v2v2(mvalf, co);
1182                                                         if (tdist < best_dist) {
1183 /*                                                              printf("Best face: %p (%f)\n", f, tdist);*/
1184                                                                 best_dist = tdist;
1185                                                                 efa = f;
1186                                                         }
1187                                                 }
1188                                         }
1189                                 }
1190                                 if (efa) {
1191                                         BM_active_face_set(em->bm, efa);
1192                                         BM_select_history_store(em->bm, efa);
1193                                 }
1194                         }
1195                 }
1196
1197                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit);
1198         }
1199 }
1200
1201 static int edbm_select_loop_invoke(bContext *C, wmOperator *op, wmEvent *event)
1202 {
1203         
1204         view3d_operator_needs_opengl(C);
1205         
1206         mouse_mesh_loop(C, event->mval, RNA_boolean_get(op->ptr, "extend"),
1207                         RNA_boolean_get(op->ptr, "ring"));
1208         
1209         /* cannot do tweaks for as long this keymap is after transform map */
1210         return OPERATOR_FINISHED;
1211 }
1212
1213 void MESH_OT_loop_select(wmOperatorType *ot)
1214 {
1215         /* identifiers */
1216         ot->name = "Loop Select";
1217         ot->idname = "MESH_OT_loop_select";
1218         ot->description = "Select a loop of connected edges";
1219         
1220         /* api callbacks */
1221         ot->invoke = edbm_select_loop_invoke;
1222         ot->poll = ED_operator_editmesh_region_view3d;
1223         
1224         /* flags */
1225         ot->flag = OPTYPE_UNDO;
1226         
1227         /* properties */
1228         RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "Extend the selection");
1229         RNA_def_boolean(ot->srna, "ring", 0, "Select Ring", "Select ring");
1230 }
1231
1232 void MESH_OT_edgering_select(wmOperatorType *ot)
1233 {
1234         /* description */
1235         ot->name = "Edge Ring Select";
1236         ot->idname = "MESH_OT_edgering_select";
1237         ot->description = "Select an edge ring";
1238         
1239         /* callbacks */
1240         ot->invoke = edbm_select_loop_invoke;
1241         ot->poll = ED_operator_editmesh_region_view3d;
1242         
1243         /* flags */
1244         ot->flag = OPTYPE_UNDO;
1245
1246         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
1247         RNA_def_boolean(ot->srna, "ring", 1, "Select Ring", "Select ring");
1248 }
1249
1250 /* ******************* generic tag_shortest_path and helpers ****************** */
1251
1252 static float step_cost_3_v3(const float v1[3], const float v2[3], const float v3[3])
1253 {
1254         float cost, d1[3], d2[3];
1255
1256
1257         /* The cost is based on the simple sum of the length of the two edgees... */
1258         sub_v3_v3v3(d1, v2, v1);
1259         sub_v3_v3v3(d2, v3, v2);
1260         cost = len_v3(d1) + len_v3(d2);
1261
1262         /* but is biased to give higher values to sharp turns, so that it will take
1263          * paths with fewer "turns" when selecting between equal-weighted paths between
1264          * the two edges */
1265         cost = cost + 0.5f * cost * (2.0f - sqrtf(fabsf(dot_v3v3(d1, d2))));
1266
1267         return cost;
1268 }
1269
1270 /* ******************* edgetag_shortest_path and helpers ****************** */
1271
1272 static float edgetag_cut_cost(BMEdge *e1, BMEdge *e2, BMVert *v)
1273 {
1274         BMVert *v1 = BM_edge_other_vert(e1, v);
1275         BMVert *v2 = BM_edge_other_vert(e2, v);
1276         return step_cost_3_v3(v1->co, v->co, v2->co);
1277 }
1278
1279 static void edgetag_add_adjacent(Heap *heap, BMEdge *e1, BMEdge **edges_prev, float *cost)
1280 {
1281         BMIter viter;
1282         BMVert *v;
1283
1284         BMIter eiter;
1285         BMEdge *e2;
1286
1287         const int e1_index = BM_elem_index_get(e1);
1288
1289         BM_ITER_ELEM (v, &viter, e1, BM_VERTS_OF_EDGE) {
1290                 BM_ITER_ELEM (e2, &eiter, v, BM_EDGES_OF_VERT) {
1291                         if (!BM_elem_flag_test(e2, BM_ELEM_TAG)) {
1292                                 /* we know 'e2' is not visited, check it out! */
1293                                 const int e2_index = BM_elem_index_get(e2);
1294                                 const float cost_cut = edgetag_cut_cost(e1, e2, v);
1295                                 const float cost_new = cost[e1_index] + cost_cut;
1296
1297                                 if (cost[e2_index] > cost_new) {
1298                                         cost[e2_index] = cost_new;
1299                                         edges_prev[e2_index] = e1;
1300                                         BLI_heap_insert(heap, cost_new, e2);
1301                                 }
1302                         }
1303                 }
1304         }
1305 }
1306
1307 static void edgetag_context_set(BMesh *bm, Scene *scene, BMEdge *e, int val)
1308 {
1309         
1310         switch (scene->toolsettings->edge_mode) {
1311                 case EDGE_MODE_SELECT:
1312                         BM_edge_select_set(bm, e, val);
1313                         break;
1314                 case EDGE_MODE_TAG_SEAM:
1315                         BM_elem_flag_set(e, BM_ELEM_SEAM, val);
1316                         break;
1317                 case EDGE_MODE_TAG_SHARP:
1318                         BM_elem_flag_set(e, BM_ELEM_SMOOTH, !val);
1319                         break;
1320                 case EDGE_MODE_TAG_FREESTYLE:
1321                         BM_elem_flag_set(e, BM_ELEM_FREESTYLE, val);
1322                         break;
1323                 case EDGE_MODE_TAG_CREASE:
1324                         BM_elem_float_data_set(&bm->edata, e, CD_CREASE, (val) ? 1.0f : 0.0f);
1325                         break;
1326                 case EDGE_MODE_TAG_BEVEL:
1327                         BM_elem_float_data_set(&bm->edata, e, CD_BWEIGHT, (val) ? 1.0f : 0.0f);
1328                         break;
1329         }
1330 }
1331
1332 static int edgetag_context_check(Scene *scene, BMesh *bm, BMEdge *e)
1333 {
1334         switch (scene->toolsettings->edge_mode) {
1335                 case EDGE_MODE_SELECT:
1336                         return BM_elem_flag_test(e, BM_ELEM_SELECT) ? TRUE : FALSE;
1337                 case EDGE_MODE_TAG_SEAM:
1338                         return BM_elem_flag_test(e, BM_ELEM_SEAM);
1339                 case EDGE_MODE_TAG_SHARP:
1340                         return !BM_elem_flag_test(e, BM_ELEM_SMOOTH);
1341                 case EDGE_MODE_TAG_FREESTYLE:
1342                         return !BM_elem_flag_test(e, BM_ELEM_FREESTYLE);
1343                 case EDGE_MODE_TAG_CREASE:
1344                         return BM_elem_float_data_get(&bm->edata, e, CD_CREASE) ? TRUE : FALSE;
1345                 case EDGE_MODE_TAG_BEVEL:
1346                         return BM_elem_float_data_get(&bm->edata, e, CD_BWEIGHT) ? TRUE : FALSE;
1347         }
1348         return 0;
1349 }
1350
1351 static int edgetag_shortest_path(Scene *scene, BMesh *bm, BMEdge *e_src, BMEdge *e_dst)
1352 {
1353         /* BM_ELEM_TAG flag is used to store visited edges */
1354         BMEdge *e;
1355         BMIter eiter;
1356         Heap *heap;
1357         float *cost;
1358         BMEdge **edges_prev;
1359         int i, totedge;
1360
1361         /* note, would pass BM_EDGE except we are looping over all edges anyway */
1362         BM_mesh_elem_index_ensure(bm, BM_VERT /* | BM_EDGE */);
1363
1364         BM_ITER_MESH_INDEX (e, &eiter, bm, BM_EDGES_OF_MESH, i) {
1365                 if (BM_elem_flag_test(e, BM_ELEM_HIDDEN) == FALSE) {
1366                         BM_elem_flag_disable(e, BM_ELEM_TAG);
1367                 }
1368                 else {
1369                         BM_elem_flag_enable(e, BM_ELEM_TAG);
1370                 }
1371
1372                 BM_elem_index_set(e, i); /* set_inline */
1373         }
1374         bm->elem_index_dirty &= ~BM_EDGE;
1375
1376         /* alloc */
1377         totedge = bm->totedge;
1378         edges_prev = MEM_callocN(sizeof(*edges_prev) * totedge, "SeamPathPrevious");
1379         cost = MEM_mallocN(sizeof(*cost) * totedge, "SeamPathCost");
1380
1381         fill_vn_fl(cost, totedge, 1e20f);
1382
1383         /*
1384          * Arrays are now filled as follows:
1385          *
1386          * As the search continues, prevedge[n] will be the previous edge on the shortest
1387          * path found so far to edge n. The visitedhash will of course contain entries
1388          * for edges that have been visited, cost[n] will contain the length of the shortest
1389          * path to edge n found so far, Finally, heap is a priority heap which is built on the
1390          * the same data as the cost array, but inverted: it is a worklist of edges prioritized
1391          * by the shortest path found so far to the edge.
1392          */
1393
1394         /* regular dijkstra shortest path, but over edges instead of vertices */
1395         heap = BLI_heap_new();
1396         BLI_heap_insert(heap, 0.0f, e_src);
1397         cost[BM_elem_index_get(e_src)] = 0.0f;
1398
1399         e = NULL;
1400
1401         while (!BLI_heap_is_empty(heap)) {
1402                 e = BLI_heap_popmin(heap);
1403
1404                 if (e == e_dst)
1405                         break;
1406
1407                 if (!BM_elem_flag_test(e, BM_ELEM_TAG)) {
1408                         BM_elem_flag_enable(e, BM_ELEM_TAG);
1409                         edgetag_add_adjacent(heap, e, edges_prev, cost);
1410                 }
1411         }
1412         
1413         if (e == e_dst) {
1414                 short all_set = TRUE;
1415
1416                 /* Check whether the path is already completely tagged.
1417                  * if it is, the tags will be cleared instead of set. */
1418                 e = e_dst;
1419                 do {
1420                         if (!edgetag_context_check(scene, bm, e)) {
1421                                 all_set = FALSE;
1422                                 break;
1423                         }
1424                 } while ((e = edges_prev[BM_elem_index_get(e)]));
1425
1426                 /* Follow path back and source and add or remove tags */
1427                 e = e_dst;
1428                 do {
1429                         edgetag_context_set(bm, scene, e, !all_set);
1430                 } while ((e = edges_prev[BM_elem_index_get(e)]));
1431         }
1432
1433         MEM_freeN(edges_prev);
1434         MEM_freeN(cost);
1435         BLI_heap_free(heap, NULL);
1436
1437         return 1;
1438 }
1439
1440 /* ******************* mesh shortest path select, uses prev-selected edge ****************** */
1441
1442 /* since you want to create paths with multiple selects, it doesn't have extend option */
1443 static int mouse_mesh_shortest_path_edge(bContext *C, ViewContext *vc)
1444 {
1445         BMEditMesh *em = vc->em;
1446         BMEdge *e_dst;
1447         float dist = 75.0f;
1448         
1449         e_dst = EDBM_edge_find_nearest(vc, &dist);
1450         if (e_dst) {
1451                 Mesh *me = vc->obedit->data;
1452                 int path = 0;
1453                 
1454                 if (em->bm->selected.last) {
1455                         BMEditSelection *ese = em->bm->selected.last;
1456                         
1457                         if (ese && ese->htype == BM_EDGE) {
1458                                 BMEdge *e_act;
1459                                 e_act = (BMEdge *)ese->ele;
1460                                 if (e_act != e_dst) {
1461                                         if (edgetag_shortest_path(vc->scene, em->bm, e_act, e_dst)) {
1462                                                 BM_select_history_remove(em->bm, e_act);
1463                                                 path = 1;
1464                                         }
1465                                 }
1466                         }
1467                 }
1468                 if (path == 0) {
1469                         int act = (edgetag_context_check(vc->scene, em->bm, e_dst) == 0);
1470                         edgetag_context_set(em->bm, vc->scene, e_dst, act); /* switch the edge option */
1471                 }
1472                 
1473                 EDBM_selectmode_flush(em);
1474
1475                 /* even if this is selected it may not be in the selection list */
1476                 if (edgetag_context_check(vc->scene, em->bm, e_dst) == 0)
1477                         BM_select_history_remove(em->bm, e_dst);
1478                 else
1479                         BM_select_history_store(em->bm, e_dst);
1480         
1481                 /* force drawmode for mesh */
1482                 switch (CTX_data_tool_settings(C)->edge_mode) {
1483                         
1484                         case EDGE_MODE_TAG_SEAM:
1485                                 me->drawflag |= ME_DRAWSEAMS;
1486                                 ED_uvedit_live_unwrap(vc->scene, vc->obedit);
1487                                 break;
1488                         case EDGE_MODE_TAG_SHARP:
1489                                 me->drawflag |= ME_DRAWSHARP;
1490                                 break;
1491                         case EDGE_MODE_TAG_CREASE:
1492                                 me->drawflag |= ME_DRAWCREASES;
1493                                 break;
1494                         case EDGE_MODE_TAG_BEVEL:
1495                                 me->drawflag |= ME_DRAWBWEIGHTS;
1496                                 break;
1497                         case EDGE_MODE_TAG_FREESTYLE:
1498                                 me->drawflag |= ME_DRAW_FREESTYLE_EDGE;
1499                                 break;
1500                 }
1501                 
1502                 EDBM_update_generic(C, em, FALSE);
1503
1504                 return TRUE;
1505         }
1506         else {
1507                 return FALSE;
1508         }
1509 }
1510
1511
1512 /* ******************* facetag_shortest_path and helpers ****************** */
1513
1514
1515 static float facetag_cut_cost(BMFace *f1, BMFace *f2, BMEdge *e)
1516 {
1517         float f1_cent[3];
1518         float f2_cent[3];
1519         float e_cent[3];
1520
1521         BM_face_calc_center_mean(f1, f1_cent);
1522         BM_face_calc_center_mean(f2, f2_cent);
1523         mid_v3_v3v3(e_cent, e->v1->co, e->v2->co);
1524
1525         return step_cost_3_v3(f1_cent, e_cent, f2_cent);
1526 }
1527
1528 static void facetag_add_adjacent(Heap *heap, BMFace *f1, BMFace **faces_prev, float *cost)
1529 {
1530         BMIter liter;
1531         BMLoop *l2;
1532         BMFace *f2;
1533
1534         const int f1_index = BM_elem_index_get(f1);
1535
1536         /* loop over faces of face, but do so by first looping over loops */
1537         BM_ITER_ELEM (l2, &liter, f1, BM_LOOPS_OF_FACE) {
1538                 BMLoop *l_first;
1539                 BMLoop *l_iter;
1540
1541                 l_iter = l_first = l2;
1542                 do {
1543                         f2 = l_iter->f;
1544                         if (!BM_elem_flag_test(f2, BM_ELEM_TAG)) {
1545                                 /* we know 'f2' is not visited, check it out! */
1546                                 const int f2_index = BM_elem_index_get(f2);
1547                                 const float cost_cut = facetag_cut_cost(f1, f2, l_iter->e);
1548                                 const float cost_new = cost[f1_index] + cost_cut;
1549
1550                                 if (cost[f2_index] > cost_new) {
1551                                         cost[f2_index] = cost_new;
1552                                         faces_prev[f2_index] = f1;
1553                                         BLI_heap_insert(heap, cost_new, f2);
1554                                 }
1555                         }
1556                 } while ((l_iter = l_iter->radial_next) != l_first);
1557         }
1558 }
1559
1560 static void facetag_context_set(BMesh *bm, Scene *UNUSED(scene), BMFace *f, int val)
1561 {
1562         BM_face_select_set(bm, f, val);
1563 }
1564
1565 static int facetag_context_check(Scene *UNUSED(scene), BMesh *UNUSED(bm), BMFace *f)
1566 {
1567         return BM_elem_flag_test(f, BM_ELEM_SELECT) ? 1 : 0;
1568 }
1569
1570 static int facetag_shortest_path(Scene *scene, BMesh *bm, BMFace *f_src, BMFace *f_dst)
1571 {
1572         /* BM_ELEM_TAG flag is used to store visited edges */
1573         BMFace *f;
1574         BMIter fiter;
1575         Heap *heap;
1576         float *cost;
1577         BMFace **faces_prev;
1578         int i, totface;
1579
1580         /* note, would pass BM_EDGE except we are looping over all faces anyway */
1581         // BM_mesh_elem_index_ensure(bm, BM_VERT /* | BM_EDGE */); // NOT NEEDED FOR FACETAG
1582
1583         BM_ITER_MESH_INDEX (f, &fiter, bm, BM_FACES_OF_MESH, i) {
1584                 if (BM_elem_flag_test(f, BM_ELEM_HIDDEN) == FALSE) {
1585                         BM_elem_flag_disable(f, BM_ELEM_TAG);
1586                 }
1587                 else {
1588                         BM_elem_flag_enable(f, BM_ELEM_TAG);
1589                 }
1590
1591                 BM_elem_index_set(f, i); /* set_inline */
1592         }
1593         bm->elem_index_dirty &= ~BM_FACE;
1594
1595         /* alloc */
1596         totface = bm->totface;
1597         faces_prev = MEM_callocN(sizeof(*faces_prev) * totface, "SeamPathPrevious");
1598         cost = MEM_mallocN(sizeof(*cost) * totface, "SeamPathCost");
1599
1600         fill_vn_fl(cost, totface, 1e20f);
1601
1602         /*
1603          * Arrays are now filled as follows:
1604          *
1605          * As the search continues, faces_prev[n] will be the previous face on the shortest
1606          * path found so far to face n. The visitedhash will of course contain entries
1607          * for faces that have been visited, cost[n] will contain the length of the shortest
1608          * path to face n found so far, Finally, heap is a priority heap which is built on the
1609          * the same data as the cost array, but inverted: it is a worklist of faces prioritized
1610          * by the shortest path found so far to the face.
1611          */
1612
1613         /* regular dijkstra shortest path, but over faces instead of vertices */
1614         heap = BLI_heap_new();
1615         BLI_heap_insert(heap, 0.0f, f_src);
1616         cost[BM_elem_index_get(f_src)] = 0.0f;
1617
1618         f = NULL;
1619
1620         while (!BLI_heap_is_empty(heap)) {
1621                 f = BLI_heap_popmin(heap);
1622
1623                 if (f == f_dst)
1624                         break;
1625
1626                 if (!BM_elem_flag_test(f, BM_ELEM_TAG)) {
1627                         BM_elem_flag_enable(f, BM_ELEM_TAG);
1628                         facetag_add_adjacent(heap, f, faces_prev, cost);
1629                 }
1630         }
1631
1632         if (f == f_dst) {
1633                 short all_set = TRUE;
1634
1635                 /* Check whether the path is already completely tagged.
1636                  * if it is, the tags will be cleared instead of set. */
1637                 f = f_dst;
1638                 do {
1639                         if (!facetag_context_check(scene, bm, f)) {
1640                                 all_set = FALSE;
1641                                 break;
1642                         }
1643                 } while ((f = faces_prev[BM_elem_index_get(f)]));
1644
1645                 /* Follow path back and source and add or remove tags */
1646                 f = f_dst;
1647                 do {
1648                         facetag_context_set(bm, scene, f, !all_set);
1649                 } while ((f = faces_prev[BM_elem_index_get(f)]));
1650         }
1651
1652         MEM_freeN(faces_prev);
1653         MEM_freeN(cost);
1654         BLI_heap_free(heap, NULL);
1655
1656         return 1;
1657 }
1658
1659 static int mouse_mesh_shortest_path_face(bContext *C, ViewContext *vc)
1660 {
1661         BMEditMesh *em = vc->em;
1662         BMFace *f_dst;
1663         float dist = 75.0f;
1664
1665         f_dst = EDBM_face_find_nearest(vc, &dist);
1666         if (f_dst) {
1667                 int path = 0;
1668                 BMFace *f_act = BM_active_face_get(em->bm, FALSE, TRUE);
1669
1670                 if (f_act) {
1671                         if (f_act != f_dst) {
1672                                 if (facetag_shortest_path(vc->scene, em->bm, f_act, f_dst)) {
1673                                         BM_select_history_remove(em->bm, f_act);
1674                                         path = 1;
1675                                 }
1676                         }
1677                 }
1678                 if (path == 0) {
1679                         int act = (facetag_context_check(vc->scene, em->bm, f_dst) == 0);
1680                         facetag_context_set(em->bm, vc->scene, f_dst, act); /* switch the face option */
1681                 }
1682
1683                 EDBM_selectmode_flush(em);
1684
1685                 /* even if this is selected it may not be in the selection list */
1686                 if (facetag_context_check(vc->scene, em->bm, f_dst) == 0)
1687                         BM_select_history_remove(em->bm, f_dst);
1688                 else
1689                         BM_select_history_store(em->bm, f_dst);
1690
1691                 BM_active_face_set(em->bm, f_dst);
1692
1693                 EDBM_update_generic(C, em, FALSE);
1694
1695                 return TRUE;
1696         }
1697         else {
1698                 return FALSE;
1699         }
1700 }
1701
1702
1703 /* ******************* operator for edge and face tag ****************** */
1704
1705 static int edbm_shortest_path_select_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
1706 {
1707         ViewContext vc;
1708         BMEditMesh *em;
1709
1710         view3d_operator_needs_opengl(C);
1711
1712         em_setup_viewcontext(C, &vc);
1713         vc.mval[0] = event->mval[0];
1714         vc.mval[1] = event->mval[1];
1715         em = vc.em;
1716
1717         if (em->selectmode & SCE_SELECT_EDGE) {
1718                 if (mouse_mesh_shortest_path_edge(C, &vc)) {
1719                         return OPERATOR_FINISHED;
1720                 }
1721                 else {
1722                         return OPERATOR_PASS_THROUGH;
1723                 }
1724         }
1725         else if (em->selectmode & SCE_SELECT_FACE) {
1726                 if (mouse_mesh_shortest_path_face(C, &vc)) {
1727                         return OPERATOR_FINISHED;
1728                 }
1729                 else {
1730                         return OPERATOR_PASS_THROUGH;
1731                 }
1732         }
1733
1734         return OPERATOR_PASS_THROUGH;
1735 }
1736
1737 static int edbm_shortest_path_select_poll(bContext *C)
1738 {
1739         if (ED_operator_editmesh_region_view3d(C)) {
1740                 Object *obedit = CTX_data_edit_object(C);
1741                 BMEditMesh *em = BMEdit_FromObject(obedit);
1742                 return (em->selectmode & (SCE_SELECT_EDGE | SCE_SELECT_FACE)) != 0;
1743         }
1744         return 0;
1745 }
1746
1747 void MESH_OT_select_shortest_path(wmOperatorType *ot)
1748 {
1749         /* identifiers */
1750         ot->name = "Shortest Path Select";
1751         ot->idname = "MESH_OT_select_shortest_path";
1752         ot->description = "Select shortest path between two selections";
1753         
1754         /* api callbacks */
1755         ot->invoke = edbm_shortest_path_select_invoke;
1756         ot->poll = edbm_shortest_path_select_poll;
1757         
1758         /* flags */
1759         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1760         
1761         /* properties */
1762         RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "");
1763 }
1764
1765 /* ************************************************** */
1766 /* here actual select happens */
1767 /* gets called via generic mouse select operator */
1768 int EDBM_select_pick(bContext *C, const int mval[2], short extend, short deselect, short toggle)
1769 {
1770         ViewContext vc;
1771         BMVert *eve = NULL;
1772         BMEdge *eed = NULL;
1773         BMFace *efa = NULL;
1774
1775         /* setup view context for argument to callbacks */
1776         em_setup_viewcontext(C, &vc);
1777         vc.mval[0] = mval[0];
1778         vc.mval[1] = mval[1];
1779
1780         if (unified_findnearest(&vc, &eve, &eed, &efa)) {
1781
1782                 /* Deselect everything */
1783                 if (extend == 0 && deselect == 0 && toggle == 0)
1784                         EDBM_flag_disable_all(vc.em, BM_ELEM_SELECT);
1785
1786                 if (efa) {
1787                         if (extend) {
1788                                 /* set the last selected face */
1789                                 BM_active_face_set(vc.em->bm, efa);
1790
1791                                 /* Work-around: deselect first, so we can guarantee it will */
1792                                 /* be active even if it was already selected */
1793                                 BM_select_history_remove(vc.em->bm, efa);
1794                                 BM_face_select_set(vc.em->bm, efa, FALSE);
1795                                 BM_select_history_store(vc.em->bm, efa);
1796                                 BM_face_select_set(vc.em->bm, efa, TRUE);
1797                         }
1798                         else if (deselect) {
1799                                 BM_select_history_remove(vc.em->bm, efa);
1800                                 BM_face_select_set(vc.em->bm, efa, FALSE);
1801                         }
1802                         else {
1803                                 /* set the last selected face */
1804                                 BM_active_face_set(vc.em->bm, efa);
1805
1806                                 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
1807                                         BM_select_history_store(vc.em->bm, efa);
1808                                         BM_face_select_set(vc.em->bm, efa, TRUE);
1809                                 }
1810                                 else if (toggle) {
1811                                         BM_select_history_remove(vc.em->bm, efa);
1812                                         BM_face_select_set(vc.em->bm, efa, FALSE);
1813                                 }
1814                         }
1815                 }
1816                 else if (eed) {
1817                         if (extend) {
1818                                 /* Work-around: deselect first, so we can guarantee it will */
1819                                 /* be active even if it was already selected */
1820                                 BM_select_history_remove(vc.em->bm, eed);
1821                                 BM_edge_select_set(vc.em->bm, eed, FALSE);
1822                                 BM_select_history_store(vc.em->bm, eed);
1823                                 BM_edge_select_set(vc.em->bm, eed, TRUE);
1824                         }
1825                         else if (deselect) {
1826                                 BM_select_history_remove(vc.em->bm, eed);
1827                                 BM_edge_select_set(vc.em->bm, eed, FALSE);
1828                         }
1829                         else {
1830                                 if (!BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1831                                         BM_select_history_store(vc.em->bm, eed);
1832                                         BM_edge_select_set(vc.em->bm, eed, TRUE);
1833                                 }
1834                                 else if (toggle) {
1835                                         BM_select_history_remove(vc.em->bm, eed);
1836                                         BM_edge_select_set(vc.em->bm, eed, FALSE);
1837                                 }
1838                         }
1839                 }
1840                 else if (eve) {
1841                         if (extend) {
1842                                 /* Work-around: deselect first, so we can guarantee it will */
1843                                 /* be active even if it was already selected */
1844                                 BM_select_history_remove(vc.em->bm, eve);
1845                                 BM_vert_select_set(vc.em->bm, eve, FALSE);
1846                                 BM_select_history_store(vc.em->bm, eve);
1847                                 BM_vert_select_set(vc.em->bm, eve, TRUE);
1848                         }
1849                         else if (deselect) {
1850                                 BM_select_history_remove(vc.em->bm, eve);
1851                                 BM_vert_select_set(vc.em->bm, eve, FALSE);
1852                         }
1853                         else {
1854                                 if (!BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
1855                                         BM_select_history_store(vc.em->bm, eve);
1856                                         BM_vert_select_set(vc.em->bm, eve, TRUE);
1857                                 }
1858                                 else if (toggle) {
1859                                         BM_select_history_remove(vc.em->bm, eve);
1860                                         BM_vert_select_set(vc.em->bm, eve, FALSE);
1861                                 }
1862                         }
1863                 }
1864
1865                 EDBM_selectmode_flush(vc.em);
1866
1867                 /* change active material on object */
1868                 if (efa && efa->mat_nr != vc.obedit->actcol - 1) {
1869                         vc.obedit->actcol = efa->mat_nr + 1;
1870                         vc.em->mat_nr = efa->mat_nr;
1871
1872                         WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING, NULL);
1873
1874                 }
1875
1876                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit);
1877                 return 1;
1878         }
1879
1880         return 0;
1881 }
1882
1883 static void edbm_strip_selections(BMEditMesh *em)
1884 {
1885         BMEditSelection *ese, *nextese;
1886
1887         if (!(em->selectmode & SCE_SELECT_VERTEX)) {
1888                 ese = em->bm->selected.first;
1889                 while (ese) {
1890                         nextese = ese->next;
1891                         if (ese->htype == BM_VERT) BLI_freelinkN(&(em->bm->selected), ese);
1892                         ese = nextese;
1893                 }
1894         }
1895         if (!(em->selectmode & SCE_SELECT_EDGE)) {
1896                 ese = em->bm->selected.first;
1897                 while (ese) {
1898                         nextese = ese->next;
1899                         if (ese->htype == BM_EDGE) BLI_freelinkN(&(em->bm->selected), ese);
1900                         ese = nextese;
1901                 }
1902         }
1903         if (!(em->selectmode & SCE_SELECT_FACE)) {
1904                 ese = em->bm->selected.first;
1905                 while (ese) {
1906                         nextese = ese->next;
1907                         if (ese->htype == BM_FACE) BLI_freelinkN(&(em->bm->selected), ese);
1908                         ese = nextese;
1909                 }
1910         }
1911 }
1912
1913 /* when switching select mode, makes sure selection is consistent for editing */
1914 /* also for paranoia checks to make sure edge or face mode works */
1915 void EDBM_selectmode_set(BMEditMesh *em)
1916 {
1917         BMVert *eve;
1918         BMEdge *eed;
1919         BMFace *efa;
1920         BMIter iter;
1921         
1922         em->bm->selectmode = em->selectmode;
1923
1924         edbm_strip_selections(em); /* strip BMEditSelections from em->selected that are not relevant to new mode */
1925         
1926         if (em->selectmode & SCE_SELECT_VERTEX) {
1927                 EDBM_select_flush(em);
1928         }
1929         else if (em->selectmode & SCE_SELECT_EDGE) {
1930                 /* deselect vertices, and select again based on edge select */
1931                 eve = BM_iter_new(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
1932                 for (; eve; eve = BM_iter_step(&iter)) BM_vert_select_set(em->bm, eve, FALSE);
1933                 
1934                 eed = BM_iter_new(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
1935                 for (; eed; eed = BM_iter_step(&iter)) {
1936                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1937                                 BM_edge_select_set(em->bm, eed, TRUE);
1938                         }
1939                 }
1940                 
1941                 /* selects faces based on edge status */
1942                 EDBM_selectmode_flush(em);
1943         }
1944         else if (em->selectmode & SCE_SELECT_FACE) {
1945                 /* deselect eges, and select again based on face select */
1946                 eed = BM_iter_new(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
1947                 for (; eed; eed = BM_iter_step(&iter)) BM_edge_select_set(em->bm, eed, FALSE);
1948                 
1949                 efa = BM_iter_new(&iter, em->bm, BM_FACES_OF_MESH, NULL);
1950                 for (; efa; efa = BM_iter_step(&iter)) {
1951                         if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
1952                                 BM_face_select_set(em->bm, efa, TRUE);
1953                         }
1954                 }
1955         }
1956 }
1957
1958 void EDBM_selectmode_convert(BMEditMesh *em, const short selectmode_old, const short selectmode_new)
1959 {
1960         BMEdge *eed;
1961         BMFace *efa;
1962         BMIter iter;
1963
1964         /* first tag-to-select, then select --- this avoids a feedback loop */
1965
1966         /* have to find out what the selectionmode was previously */
1967         if (selectmode_old == SCE_SELECT_VERTEX) {
1968                 if (selectmode_new == SCE_SELECT_EDGE) {
1969                         /* select all edges associated with every selected vert */
1970                         BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
1971                                 BM_elem_flag_set(eed, BM_ELEM_TAG, BM_edge_is_any_vert_flag_test(eed, BM_ELEM_SELECT));
1972                         }
1973
1974                         BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
1975                                 if (BM_elem_flag_test(eed, BM_ELEM_TAG)) {
1976                                         BM_edge_select_set(em->bm, eed, TRUE);
1977                                 }
1978                         }
1979                 }
1980                 else if (selectmode_new == SCE_SELECT_FACE) {
1981                         /* select all faces associated with every selected vert */
1982                         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
1983                                 BM_elem_flag_set(efa, BM_ELEM_TAG, BM_face_is_any_vert_flag_test(efa, BM_ELEM_SELECT));
1984                         }
1985
1986                         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
1987                                 if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
1988                                         BM_face_select_set(em->bm, efa, TRUE);
1989                                 }
1990                         }
1991                 }
1992         }
1993         else if (selectmode_old == SCE_SELECT_EDGE) {
1994                 if (selectmode_new == SCE_SELECT_FACE) {
1995                         /* select all faces associated with every selected edge */
1996                         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
1997                                 BM_elem_flag_set(efa, BM_ELEM_TAG, BM_face_is_any_edge_flag_test(efa, BM_ELEM_SELECT));
1998                         }
1999
2000                         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2001                                 if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
2002                                         BM_face_select_set(em->bm, efa, TRUE);
2003                                 }
2004                         }
2005                 }
2006         }
2007 }
2008
2009 /* user facing function, does notification and undo push */
2010 int EDBM_selectmode_toggle(bContext *C, const short selectmode_new,
2011                            const int action, const int use_extend, const int use_expand)
2012 {
2013         ToolSettings *ts = CTX_data_tool_settings(C);
2014         Object *obedit = CTX_data_edit_object(C);
2015         BMEditMesh *em = NULL;
2016         int ret = FALSE;
2017
2018         if (obedit && obedit->type == OB_MESH) {
2019                 em = BMEdit_FromObject(obedit);
2020         }
2021
2022         if (em == NULL) {
2023                 return ret;
2024         }
2025
2026         switch (action) {
2027                 case -1:
2028                         /* already set */
2029                         break;
2030                 case 0:  /* disable */
2031                         /* check we have something to do */
2032                         if ((em->selectmode & selectmode_new) == 0) {
2033                                 return FALSE;
2034                         }
2035                         em->selectmode &= ~selectmode_new;
2036                         break;
2037                 case 1:  /* enable */
2038                         /* check we have something to do */
2039                         if ((em->selectmode & selectmode_new) != 0) {
2040                                 return FALSE;
2041                         }
2042                         em->selectmode |= selectmode_new;
2043                         break;
2044                 case 2:  /* toggle */
2045                         /* can't disable this flag if its the only one set */
2046                         if (em->selectmode == selectmode_new) {
2047                                 return FALSE;
2048                         }
2049                         em->selectmode ^= selectmode_new;
2050                         break;
2051                 default:
2052                         BLI_assert(0);
2053         }
2054
2055         switch (selectmode_new) {
2056                 case SCE_SELECT_VERTEX:
2057                         if (use_extend == 0 || em->selectmode == 0)
2058                                 em->selectmode = SCE_SELECT_VERTEX;
2059                         ts->selectmode = em->selectmode;
2060                         EDBM_selectmode_set(em);
2061                         ret = TRUE;
2062                         break;
2063                 case SCE_SELECT_EDGE:
2064                         if (use_extend == 0 || em->selectmode == 0) {
2065                                 if (use_expand) {
2066                                         const short selmode_max = highest_order_bit_s(ts->selectmode);
2067                                         if (selmode_max == SCE_SELECT_VERTEX) {
2068                                                 EDBM_selectmode_convert(em, selmode_max, SCE_SELECT_EDGE);
2069                                         }
2070                                 }
2071                                 em->selectmode = SCE_SELECT_EDGE;
2072                         }
2073                         ts->selectmode = em->selectmode;
2074                         EDBM_selectmode_set(em);
2075                         ret = TRUE;
2076                         break;
2077                 case SCE_SELECT_FACE:
2078                         if (use_extend == 0 || em->selectmode == 0) {
2079                                 if (use_expand) {
2080                                         const short selmode_max = highest_order_bit_s(ts->selectmode);
2081                                         if (ELEM(selmode_max, SCE_SELECT_VERTEX, SCE_SELECT_EDGE)) {
2082                                                 EDBM_selectmode_convert(em, selmode_max, SCE_SELECT_FACE);
2083                                         }
2084                                 }
2085
2086                                 em->selectmode = SCE_SELECT_FACE;
2087                         }
2088                         ts->selectmode = em->selectmode;
2089                         EDBM_selectmode_set(em);
2090                         ret = TRUE;
2091                         break;
2092                 default:
2093                         BLI_assert(0);
2094                         break;
2095         }
2096
2097         if (ret == TRUE) {
2098                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
2099                 WM_main_add_notifier(NC_SCENE | ND_TOOLSETTINGS, NULL);
2100         }
2101
2102         return ret;
2103 }
2104
2105 void EDBM_deselect_by_material(BMEditMesh *em, const short index, const short select)
2106 {
2107         BMIter iter;
2108         BMFace *efa;
2109
2110         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2111                 if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN))
2112                         continue;
2113                 if (efa->mat_nr == index) {
2114                         BM_face_select_set(em->bm, efa, select);
2115                 }
2116         }
2117 }
2118
2119 void EDBM_select_toggle_all(BMEditMesh *em) /* exported for UV */
2120 {
2121         if (em->bm->totvertsel || em->bm->totedgesel || em->bm->totfacesel)
2122                 EDBM_flag_disable_all(em, BM_ELEM_SELECT);
2123         else
2124                 EDBM_flag_enable_all(em, BM_ELEM_SELECT);
2125 }
2126
2127 void EDBM_select_swap(BMEditMesh *em) /* exported for UV */
2128 {
2129         BMIter iter;
2130         BMVert *eve;
2131         BMEdge *eed;
2132         BMFace *efa;
2133         
2134         if (em->bm->selectmode & SCE_SELECT_VERTEX) {
2135                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2136                         if (BM_elem_flag_test(eve, BM_ELEM_HIDDEN))
2137                                 continue;
2138                         BM_vert_select_set(em->bm, eve, !BM_elem_flag_test(eve, BM_ELEM_SELECT));
2139                 }
2140         }
2141         else if (em->selectmode & SCE_SELECT_EDGE) {
2142                 BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
2143                         if (BM_elem_flag_test(eed, BM_ELEM_HIDDEN))
2144                                 continue;
2145                         BM_edge_select_set(em->bm, eed, !BM_elem_flag_test(eed, BM_ELEM_SELECT));
2146                 }
2147         }
2148         else {
2149                 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2150                         if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN))
2151                                 continue;
2152                         BM_face_select_set(em->bm, efa, !BM_elem_flag_test(efa, BM_ELEM_SELECT));
2153                 }
2154
2155         }
2156 //      if (EM_texFaceCheck())
2157 }
2158
2159 int EDBM_select_interior_faces(BMEditMesh *em)
2160 {
2161         BMesh *bm = em->bm;
2162         BMIter iter;
2163         BMIter eiter;
2164         BMFace *efa;
2165         BMEdge *eed;
2166         int ok;
2167         int change = FALSE;
2168
2169         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2170                 if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN))
2171                         continue;
2172
2173
2174                 ok = TRUE;
2175                 BM_ITER_ELEM (eed, &eiter, efa, BM_EDGES_OF_FACE) {
2176                         if (BM_edge_face_count(eed) < 3) {
2177                                 ok = FALSE;
2178                                 break;
2179                         }
2180                 }
2181
2182                 if (ok) {
2183                         BM_face_select_set(bm, efa, TRUE);
2184                         change = TRUE;
2185                 }
2186         }
2187
2188         return change;
2189 }
2190
2191 static void linked_limit_default(bContext *C, wmOperator *op)
2192 {
2193         if (!RNA_struct_property_is_set(op->ptr, "limit")) {
2194                 Object *obedit = CTX_data_edit_object(C);
2195                 BMEditMesh *em = BMEdit_FromObject(obedit);
2196                 if (em->selectmode == SCE_SELECT_FACE)
2197                         RNA_boolean_set(op->ptr, "limit", TRUE);
2198                 else
2199                         RNA_boolean_set(op->ptr, "limit", FALSE);
2200         }
2201 }
2202
2203 static int edbm_select_linked_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
2204 {
2205         Object *obedit = CTX_data_edit_object(C);
2206         ViewContext vc;
2207         BMesh *bm;
2208         BMWalker walker;
2209         BMEditMesh *em;
2210         BMVert *eve;
2211         BMEdge *e, *eed;
2212         BMFace *efa;
2213         int sel = !RNA_boolean_get(op->ptr, "deselect");
2214
2215         int limit;
2216
2217         linked_limit_default(C, op);
2218
2219         limit = RNA_boolean_get(op->ptr, "limit");
2220
2221         /* unified_finednearest needs ogl */
2222         view3d_operator_needs_opengl(C);
2223         
2224         /* setup view context for argument to callbacks */
2225         em_setup_viewcontext(C, &vc);
2226         em = vc.em;
2227
2228         if (em->bm->totedge == 0)
2229                 return OPERATOR_CANCELLED;
2230         
2231         bm = em->bm;
2232
2233         vc.mval[0] = event->mval[0];
2234         vc.mval[1] = event->mval[1];
2235         
2236         /* return warning! */
2237         
2238         if (unified_findnearest(&vc, &eve, &eed, &efa) == 0) {
2239                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
2240         
2241                 return OPERATOR_CANCELLED;
2242         }
2243         
2244         if (em->selectmode == SCE_SELECT_FACE) {
2245                 BMIter iter;
2246
2247                 if (efa == NULL)
2248                         return OPERATOR_CANCELLED;
2249
2250                 if (limit) {
2251                         /* hflag no-seam --> bmo-tag */
2252                         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
2253                                 /* BMESH_TODO, don't use 'BM_ELEM_SELECT' here, its a HFLAG only! */
2254                                 BMO_elem_flag_set(bm, e, BM_ELEM_SELECT, !BM_elem_flag_test(e, BM_ELEM_SEAM));
2255                         }
2256                 }
2257
2258                 /* walk */
2259                 BMW_init(&walker, bm, BMW_ISLAND,
2260                          BMW_MASK_NOP, limit ? BM_ELEM_SELECT : BMW_MASK_NOP, BMW_MASK_NOP,
2261                          BMW_FLAG_TEST_HIDDEN,
2262                          BMW_NIL_LAY);
2263
2264                 e = BMW_begin(&walker, efa);
2265                 for (; efa; efa = BMW_step(&walker)) {
2266                         BM_face_select_set(bm, efa, sel);
2267                 }
2268                 BMW_end(&walker);
2269         }
2270         else {
2271                 if (efa) {
2272                         eed = BM_FACE_FIRST_LOOP(efa)->e;
2273                 }
2274                 else if (!eed) {
2275                         if (!eve || !eve->e)
2276                                 return OPERATOR_CANCELLED;
2277
2278                         eed = eve->e;
2279                 }
2280
2281                 BMW_init(&walker, bm, BMW_SHELL,
2282                          BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
2283                          BMW_FLAG_TEST_HIDDEN,
2284                          BMW_NIL_LAY);
2285
2286                 e = BMW_begin(&walker, eed->v1);
2287                 for (; e; e = BMW_step(&walker)) {
2288                         BM_edge_select_set(bm, e, sel);
2289                 }
2290                 BMW_end(&walker);
2291
2292                 EDBM_selectmode_flush(em);
2293         }
2294
2295         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
2296         return OPERATOR_FINISHED;
2297 }
2298
2299 void MESH_OT_select_linked_pick(wmOperatorType *ot)
2300 {
2301         /* identifiers */
2302         ot->name = "Select Linked";
2303         ot->idname = "MESH_OT_select_linked_pick";
2304         ot->description = "(De)select all vertices linked to the edge under the mouse cursor";
2305         
2306         /* api callbacks */
2307         ot->invoke = edbm_select_linked_pick_invoke;
2308         ot->poll = ED_operator_editmesh;
2309         
2310         /* flags */
2311         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2312         
2313         RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "");
2314         RNA_def_boolean(ot->srna, "limit", 0, "Limit by Seams", "");
2315 }
2316
2317
2318 static int edbm_select_linked_exec(bContext *C, wmOperator *op)
2319 {
2320         Object *obedit = CTX_data_edit_object(C);
2321         BMEditMesh *em = BMEdit_FromObject(obedit);
2322         BMesh *bm = em->bm;
2323         BMIter iter;
2324         BMVert *v;
2325         BMEdge *e;
2326         BMWalker walker;
2327
2328         int limit;
2329
2330         linked_limit_default(C, op);
2331
2332         limit = RNA_boolean_get(op->ptr, "limit");
2333
2334         if (em->selectmode == SCE_SELECT_FACE) {
2335                 BMFace *efa;
2336
2337                 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2338                         BM_elem_flag_set(efa, BM_ELEM_TAG, (BM_elem_flag_test(efa, BM_ELEM_SELECT) &&
2339                                                             !BM_elem_flag_test(efa, BM_ELEM_HIDDEN)));
2340                 }
2341
2342                 if (limit) {
2343                         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
2344                                 /* BMESH_TODO, don't use 'BM_ELEM_SELECT' here, its a HFLAG only! */
2345                                 BMO_elem_flag_set(bm, e, BM_ELEM_SELECT, !BM_elem_flag_test(e, BM_ELEM_SEAM));
2346                         }
2347                 }
2348
2349                 BMW_init(&walker, bm, BMW_ISLAND,
2350                          BMW_MASK_NOP, limit ? BM_ELEM_SELECT : BMW_MASK_NOP, BMW_MASK_NOP,
2351                          BMW_FLAG_TEST_HIDDEN,
2352                          BMW_NIL_LAY);
2353
2354                 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2355                         if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
2356                                 e = BMW_begin(&walker, efa);
2357                                 for (; efa; efa = BMW_step(&walker)) {
2358                                         BM_face_select_set(bm, efa, TRUE);
2359                                 }
2360                         }
2361                 }
2362                 BMW_end(&walker);
2363         }
2364         else {
2365                 BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
2366                         if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
2367                                 BM_elem_flag_enable(v, BM_ELEM_TAG);
2368                         }
2369                         else {
2370                                 BM_elem_flag_disable(v, BM_ELEM_TAG);
2371                         }
2372                 }
2373
2374                 BMW_init(&walker, em->bm, BMW_SHELL,
2375                          BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
2376                          BMW_FLAG_TEST_HIDDEN,
2377                          BMW_NIL_LAY);
2378
2379                 BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
2380                         if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
2381                                 e = BMW_begin(&walker, v);
2382                                 for (; e; e = BMW_step(&walker)) {
2383                                         BM_vert_select_set(em->bm, e->v1, TRUE);
2384                                         BM_vert_select_set(em->bm, e->v2, TRUE);
2385                                 }
2386                         }
2387                 }
2388                 BMW_end(&walker);
2389         }
2390         EDBM_selectmode_flush_ex(em, SCE_SELECT_VERTEX);
2391
2392         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
2393
2394         return OPERATOR_FINISHED;
2395 }
2396
2397 void MESH_OT_select_linked(wmOperatorType *ot)
2398 {
2399         /* identifiers */
2400         ot->name = "Select Linked All";
2401         ot->idname = "MESH_OT_select_linked";
2402         ot->description = "Select all vertices linked to the active mesh";
2403         
2404         /* api callbacks */
2405         ot->exec = edbm_select_linked_exec;
2406         ot->poll = ED_operator_editmesh;
2407         
2408         /* flags */
2409         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2410         
2411         RNA_def_boolean(ot->srna, "limit", 0, "Limit by Seams", "");
2412 }
2413
2414 /* ******************** **************** */
2415
2416 static int edbm_select_more_exec(bContext *C, wmOperator *UNUSED(op))
2417 {
2418         Object *obedit = CTX_data_edit_object(C);
2419         BMEditMesh *em = BMEdit_FromObject(obedit);
2420
2421         EDBM_select_more(em);
2422
2423         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
2424         return OPERATOR_FINISHED;
2425 }
2426
2427 void MESH_OT_select_more(wmOperatorType *ot)
2428 {
2429         /* identifiers */
2430         ot->name = "Select More";
2431         ot->idname = "MESH_OT_select_more";
2432         ot->description = "Select more vertices, edges or faces connected to initial selection";
2433
2434         /* api callbacks */
2435         ot->exec = edbm_select_more_exec;
2436         ot->poll = ED_operator_editmesh;
2437         
2438         /* flags */
2439         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2440 }
2441
2442 static int edbm_select_less_exec(bContext *C, wmOperator *UNUSED(op))
2443 {
2444         Object *obedit = CTX_data_edit_object(C);
2445         BMEditMesh *em = BMEdit_FromObject(obedit);
2446
2447         EDBM_select_less(em);
2448
2449         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
2450         return OPERATOR_FINISHED;
2451 }
2452
2453 void MESH_OT_select_less(wmOperatorType *ot)
2454 {
2455         /* identifiers */
2456         ot->name = "Select Less";
2457         ot->idname = "MESH_OT_select_less";
2458         ot->description = "Deselect vertices, edges or faces at the boundary of each selection region";
2459
2460         /* api callbacks */
2461         ot->exec = edbm_select_less_exec;
2462         ot->poll = ED_operator_editmesh;
2463         
2464         /* flags */
2465         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2466 }
2467
2468 /* Walk all reachable elements of the same type as h_act in breadth-first
2469  * order, starting from h_act. Deselects elements if the depth when they
2470  * are reached is not a multiple of "nth". */
2471 static void walker_deselect_nth(BMEditMesh *em, int nth, int offset, BMHeader *h_act)
2472 {
2473         BMElem *ele;
2474         BMesh *bm = em->bm;
2475         BMWalker walker;
2476         BMIter iter;
2477         int walktype = 0, itertype = 0, flushtype = 0;
2478         short mask_vert = 0, mask_edge = 0, mask_face = 0;
2479
2480         /* No active element from which to start - nothing to do */
2481         if (h_act == NULL) {
2482                 return;
2483         }
2484
2485         /* Determine which type of iter, walker, and select flush to use
2486          * based on type of the elements being deselected */
2487         switch (h_act->htype) {
2488                 case BM_VERT:
2489                         itertype = BM_VERTS_OF_MESH;
2490                         walktype = BMW_CONNECTED_VERTEX;
2491                         flushtype = SCE_SELECT_VERTEX;
2492                         mask_vert = BM_ELEM_SELECT;
2493                         break;
2494                 case BM_EDGE:
2495                         itertype = BM_EDGES_OF_MESH;
2496                         walktype = BMW_SHELL;
2497                         flushtype = SCE_SELECT_EDGE;
2498                         mask_edge = BM_ELEM_SELECT;
2499                         break;
2500                 case BM_FACE:
2501                         itertype = BM_FACES_OF_MESH;
2502                         walktype = BMW_ISLAND;
2503                         flushtype = SCE_SELECT_FACE;
2504                         mask_face = BM_ELEM_SELECT;
2505                         break;
2506         }
2507
2508         /* Walker restrictions uses BMO flags, not header flags,
2509          * so transfer BM_ELEM_SELECT from HFlags onto a BMO flag layer. */
2510         BMO_push(bm, NULL);
2511         BM_ITER_MESH (ele, &iter, bm, itertype) {
2512                 if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
2513                         /* BMESH_TODO, don't use 'BM_ELEM_SELECT' here, its a HFLAG only! */
2514                         BMO_elem_flag_enable(bm, (BMElemF *)ele, BM_ELEM_SELECT);
2515                 }
2516         }
2517
2518         /* Walk over selected elements starting at active */
2519         BMW_init(&walker, bm, walktype,
2520                  mask_vert, mask_edge, mask_face,
2521                  BMW_FLAG_NOP, /* don't use BMW_FLAG_TEST_HIDDEN here since we want to desel all */
2522                  BMW_NIL_LAY);
2523
2524         /* use tag to avoid touching the same verts twice */
2525         BM_ITER_MESH (ele, &iter, bm, itertype) {
2526                 BM_elem_flag_disable(ele, BM_ELEM_TAG);
2527         }
2528
2529         BLI_assert(walker.order == BMW_BREADTH_FIRST);
2530         for (ele = BMW_begin(&walker, h_act); ele != NULL; ele = BMW_step(&walker)) {
2531                 if (!BM_elem_flag_test(ele, BM_ELEM_TAG)) {
2532                         /* Deselect elements that aren't at "nth" depth from active */
2533                         if ((offset + BMW_current_depth(&walker)) % nth) {
2534                                 BM_elem_select_set(bm, ele, FALSE);
2535                         }
2536                         BM_elem_flag_enable(ele, BM_ELEM_TAG);
2537                 }
2538         }
2539         BMW_end(&walker);
2540
2541         BMO_pop(bm);
2542
2543         /* Flush selection up */
2544         EDBM_selectmode_flush_ex(em, flushtype);
2545 }
2546
2547 static void deselect_nth_active(BMEditMesh *em, BMVert **r_eve, BMEdge **r_eed, BMFace **r_efa)
2548 {
2549         BMVert *v;
2550         BMEdge *e;
2551         BMFace *f;
2552         BMIter iter;
2553         BMEditSelection *ese;
2554
2555         *r_eve = NULL;
2556         *r_eed = NULL;
2557         *r_efa = NULL;
2558
2559         EDBM_selectmode_flush(em);
2560         ese = (BMEditSelection *)em->bm->selected.last;
2561
2562         if (ese) {
2563                 switch (ese->htype) {
2564                         case BM_VERT:
2565                                 *r_eve = (BMVert *)ese->ele;
2566                                 return;
2567                         case BM_EDGE:
2568                                 *r_eed = (BMEdge *)ese->ele;
2569                                 return;
2570                         case BM_FACE:
2571                                 *r_efa = (BMFace *)ese->ele;
2572                                 return;
2573                 }
2574         }
2575
2576         if (em->selectmode & SCE_SELECT_VERTEX) {
2577                 BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
2578                         if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
2579                                 *r_eve = v;
2580                                 return;
2581                         }
2582                 }
2583         }
2584         else if (em->selectmode & SCE_SELECT_EDGE) {
2585                 BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
2586                         if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
2587                                 *r_eed = e;
2588                                 return;
2589                         }
2590                 }
2591         }
2592         else if (em->selectmode & SCE_SELECT_FACE) {
2593                 f = BM_active_face_get(em->bm, TRUE, FALSE);
2594                 if (f) {
2595                         *r_efa = f;
2596                         return;
2597                 }
2598         }
2599 }
2600
2601 static int edbm_deselect_nth(BMEditMesh *em, int nth, int offset)
2602 {
2603         BMVert *v;
2604         BMEdge *e;
2605         BMFace *f;
2606
2607         deselect_nth_active(em, &v, &e, &f);
2608
2609         if (v) {
2610                 walker_deselect_nth(em, nth, offset, &v->head);
2611                 return 1;
2612         }
2613         else if (e) {
2614                 walker_deselect_nth(em, nth, offset, &e->head);
2615                 return 1;
2616         }
2617         else if (f) {
2618                 walker_deselect_nth(em, nth, offset, &f->head);
2619                 return 1;
2620         }
2621
2622         return 0;
2623 }
2624
2625 static int edbm_select_nth_exec(bContext *C, wmOperator *op)
2626 {
2627         Object *obedit = CTX_data_edit_object(C);
2628         BMEditMesh *em = BMEdit_FromObject(obedit);
2629         int nth = RNA_int_get(op->ptr, "nth");
2630         int offset = RNA_int_get(op->ptr, "offset");
2631
2632         offset = MIN2(nth, offset);
2633
2634         if (edbm_deselect_nth(em, nth, offset) == 0) {
2635                 BKE_report(op->reports, RPT_ERROR, "Mesh has no active vert/edge/face");
2636                 return OPERATOR_CANCELLED;
2637         }
2638
2639         EDBM_update_generic(C, em, FALSE);
2640
2641         return OPERATOR_FINISHED;
2642 }
2643
2644
2645 void MESH_OT_select_nth(wmOperatorType *ot)
2646 {
2647         /* identifiers */
2648         ot->name = "Checker Deselect";
2649         ot->idname = "MESH_OT_select_nth";
2650         ot->description = "Deselect every Nth element starting from a selected vertex, edge or face";
2651
2652         /* api callbacks */
2653         ot->exec = edbm_select_nth_exec;
2654         ot->poll = ED_operator_editmesh;
2655
2656         /* flags */
2657         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2658
2659         RNA_def_int(ot->srna, "nth", 2, 2, INT_MAX, "Nth Selection", "", 2, 100);
2660         RNA_def_int(ot->srna, "offset", 0, 0, INT_MAX, "Offset", "", 0, 100);
2661 }
2662
2663 void em_setup_viewcontext(bContext *C, ViewContext *vc)
2664 {
2665         view3d_set_viewcontext(C, vc);
2666         
2667         if (vc->obedit) {
2668                 vc->em = BMEdit_FromObject(vc->obedit);
2669         }
2670 }
2671
2672 /* poll call for mesh operators requiring a view3d context */
2673 int EM_view3d_poll(bContext *C)
2674 {
2675         if (ED_operator_editmesh(C) && ED_operator_view3d_active(C))
2676                 return 1;
2677
2678         return 0;
2679 }
2680
2681
2682 static int edbm_select_sharp_edges_exec(bContext *C, wmOperator *op)
2683 {
2684         /* Find edges that have exactly two neighboring faces,
2685          * check the angle between those faces, and if angle is
2686          * small enough, select the edge
2687          */
2688         Object *obedit = CTX_data_edit_object(C);
2689         BMEditMesh *em = BMEdit_FromObject(obedit);
2690         BMIter iter;
2691         BMEdge *e;
2692         BMLoop *l1, *l2;
2693         const float sharp = RNA_float_get(op->ptr, "sharpness");
2694
2695         BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
2696                 if (BM_elem_flag_test(e, BM_ELEM_HIDDEN) == FALSE &&
2697                     BM_edge_loop_pair(e, &l1, &l2))
2698                 {
2699                         /* edge has exactly two neighboring faces, check angle */
2700                         const float angle = angle_normalized_v3v3(l1->f->no, l2->f->no);
2701
2702                         if (fabsf(angle) > sharp) {
2703                                 BM_edge_select_set(em->bm, e, TRUE);
2704                         }
2705                 }
2706         }
2707
2708         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
2709
2710         return OPERATOR_FINISHED;
2711 }
2712
2713 void MESH_OT_edges_select_sharp(wmOperatorType *ot)
2714 {
2715         PropertyRNA *prop;
2716
2717         /* identifiers */
2718         ot->name = "Select Sharp Edges";
2719         ot->description = "Select all sharp-enough edges";
2720         ot->idname = "MESH_OT_edges_select_sharp";
2721         
2722         /* api callbacks */
2723         ot->exec = edbm_select_sharp_edges_exec;
2724         ot->poll = ED_operator_editmesh;
2725         
2726         /* flags */
2727         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2728         
2729         /* props */
2730         prop = RNA_def_float_rotation(ot->srna, "sharpness", 0, NULL, DEG2RADF(0.01f), DEG2RADF(180.0f),
2731                                       "Sharpness", "", DEG2RADF(1.0f), DEG2RADF(180.0f));
2732         RNA_def_property_float_default(prop, DEG2RADF(30.0f));
2733 }
2734
2735 static int edbm_select_linked_flat_faces_exec(bContext *C, wmOperator *op)
2736 {
2737         Object *obedit = CTX_data_edit_object(C);
2738         BMEditMesh *em = BMEdit_FromObject(obedit);
2739         BMIter iter, liter, liter2;
2740         BMFace *f, **stack = NULL;
2741         BLI_array_declare(stack);
2742         BMLoop *l, *l2;
2743         float sharp = RNA_float_get(op->ptr, "sharpness");
2744         int i;
2745
2746         BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
2747                 BM_elem_flag_disable(f, BM_ELEM_TAG);
2748         }
2749
2750         BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
2751                 if (BM_elem_flag_test(f, BM_ELEM_HIDDEN) || !BM_elem_flag_test(f, BM_ELEM_SELECT) || BM_elem_flag_test(f, BM_ELEM_TAG))
2752                         continue;
2753
2754                 BLI_array_empty(stack);
2755                 i = 1;
2756
2757                 BLI_array_grow_one(stack);
2758                 stack[i - 1] = f;
2759
2760                 while (i) {
2761                         f = stack[i - 1];
2762                         i--;
2763
2764                         BM_face_select_set(em->bm, f, TRUE);
2765
2766                         BM_elem_flag_enable(f, BM_ELEM_TAG);
2767
2768                         BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
2769                                 BM_ITER_ELEM (l2, &liter2, l, BM_LOOPS_OF_LOOP) {
2770                                         float angle;
2771
2772                                         if (BM_elem_flag_test(l2->f, BM_ELEM_TAG) || BM_elem_flag_test(l2->f, BM_ELEM_HIDDEN))
2773                                                 continue;
2774
2775                                         /* edge has exactly two neighboring faces, check angle */
2776                                         angle = angle_normalized_v3v3(f->no, l2->f->no);
2777
2778                                         /* invalidate: edge too sharp */
2779                                         if (angle < sharp) {
2780                                                 BLI_array_grow_one(stack);
2781                                                 stack[i] = l2->f;
2782                                                 i++;
2783                                         }
2784                                 }
2785                         }
2786                 }
2787         }
2788
2789         BLI_array_free(stack);
2790
2791         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
2792
2793         return OPERATOR_FINISHED;
2794 }
2795
2796 void MESH_OT_faces_select_linked_flat(wmOperatorType *ot)
2797 {
2798         PropertyRNA *prop;
2799
2800         /* identifiers */
2801         ot->name = "Select Linked Flat Faces";
2802         ot->description = "Select linked faces by angle";
2803         ot->idname = "MESH_OT_faces_select_linked_flat";
2804         
2805         /* api callbacks */
2806         ot->exec = edbm_select_linked_flat_faces_exec;
2807         ot->poll = ED_operator_editmesh;
2808         
2809         /* flags */
2810         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2811         
2812         /* props */
2813         prop = RNA_def_float_rotation(ot->srna, "sharpness", 0, NULL, DEG2RADF(0.01f), DEG2RADF(180.0f),
2814                                       "Sharpness", "", DEG2RADF(1.0f), DEG2RADF(180.0f));
2815         RNA_def_property_float_default(prop, DEG2RADF(1.0f));
2816 }
2817
2818 static int edbm_select_non_manifold_exec(bContext *C, wmOperator *op)
2819 {
2820         Object *obedit = CTX_data_edit_object(C);
2821         BMEditMesh *em = BMEdit_FromObject(obedit);
2822         BMVert *v;
2823         BMEdge *e;
2824         BMIter iter;
2825
2826         /* Selects isolated verts, and edges that do not have 2 neighboring
2827          * faces
2828          */
2829         
2830         if (em->selectmode == SCE_SELECT_FACE) {
2831                 BKE_report(op->reports, RPT_ERROR, "Does not work in face selection mode");
2832                 return OPERATOR_CANCELLED;
2833         }
2834         
2835         BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
2836                 if (!BM_elem_flag_test(v, BM_ELEM_HIDDEN) && !BM_vert_is_manifold(v)) {
2837                         BM_vert_select_set(em->bm, v, TRUE);
2838                 }
2839         }
2840         
2841         BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
2842                 if (!BM_elem_flag_test(e, BM_ELEM_HIDDEN) && !BM_edge_is_manifold(e)) {
2843                         BM_edge_select_set(em->bm, e, TRUE);
2844                 }
2845         }
2846
2847         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
2848
2849         return OPERATOR_FINISHED;
2850 }
2851
2852 void MESH_OT_select_non_manifold(wmOperatorType *ot)
2853 {
2854         /* identifiers */
2855         ot->name = "Select Non Manifold";
2856         ot->description = "Select all non-manifold vertices or edges";
2857         ot->idname = "MESH_OT_select_non_manifold";
2858         
2859         /* api callbacks */
2860         ot->exec = edbm_select_non_manifold_exec;
2861         ot->poll = ED_operator_editmesh;
2862         
2863         /* flags */
2864         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2865 }
2866
2867 static int edbm_select_random_exec(bContext *C, wmOperator *op)
2868 {
2869         Object *obedit = CTX_data_edit_object(C);
2870         BMEditMesh *em = BMEdit_FromObject(obedit);
2871         BMVert *eve;
2872         BMEdge *eed;
2873         BMFace *efa;
2874         BMIter iter;
2875         float randfac =  RNA_float_get(op->ptr, "percent") / 100.0f;
2876
2877         BLI_srand(BLI_rand()); /* random seed */
2878         
2879         if (!RNA_boolean_get(op->ptr, "extend"))
2880                 EDBM_flag_disable_all(em, BM_ELEM_SELECT);
2881
2882         if (em->selectmode & SCE_SELECT_VERTEX) {
2883                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2884                         if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && BLI_frand() < randfac) {
2885                                 BM_vert_select_set(em->bm, eve, TRUE);
2886                         }
2887                 }
2888                 EDBM_selectmode_flush(em);
2889         }
2890         else if (em->selectmode & SCE_SELECT_EDGE) {
2891                 BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
2892                         if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && BLI_frand() < randfac) {
2893                                 BM_edge_select_set(em->bm, eed, TRUE);
2894                         }
2895                 }
2896                 EDBM_selectmode_flush(em);
2897         }
2898         else {
2899                 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2900                         if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN) && BLI_frand() < randfac) {
2901                                 BM_face_select_set(em->bm, efa, TRUE);
2902                         }
2903                 }
2904                 EDBM_selectmode_flush(em);
2905         }
2906         
2907         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
2908         
2909         return OPERATOR_FINISHED;
2910 }
2911
2912 void MESH_OT_select_random(wmOperatorType *ot)
2913 {
2914         /* identifiers */
2915         ot->name = "Select Random";
2916         ot->description = "Randomly select vertices";
2917         ot->idname = "MESH_OT_select_random";
2918
2919         /* api callbacks */
2920         ot->exec = edbm_select_random_exec;
2921         ot->poll = ED_operator_editmesh;
2922
2923         /* flags */
2924         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2925         
2926         /* props */
2927         RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f,
2928                                  "Percent", "Percentage of elements to select randomly", 0.f, 100.0f);
2929         RNA_def_boolean(ot->srna, "extend", 0,
2930                         "Extend Selection", "Extend selection instead of deselecting everything first");
2931 }
2932
2933 static int edbm_select_next_loop_exec(bContext *C, wmOperator *UNUSED(op))
2934 {
2935         Object *obedit = CTX_data_edit_object(C);
2936         BMEditMesh *em = BMEdit_FromObject(obedit);
2937         BMFace *f;
2938         BMVert *v;
2939         BMIter iter;
2940         
2941         BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
2942                 BM_elem_flag_disable(v, BM_ELEM_TAG);
2943         }
2944         
2945         BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
2946                 BMLoop *l;
2947                 BMIter liter;
2948                 
2949                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
2950                         if (BM_elem_flag_test(l->v, BM_ELEM_SELECT)) {
2951                                 BM_elem_flag_enable(l->next->v, BM_ELEM_TAG);
2952                                 BM_vert_select_set(em->bm, l->v, FALSE);
2953                         }
2954                 }
2955         }
2956
2957         BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
2958                 if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
2959                         BM_vert_select_set(em->bm, v, TRUE);
2960                 }
2961         }
2962
2963         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
2964         return OPERATOR_FINISHED;
2965 }
2966
2967 void MESH_OT_select_next_loop(wmOperatorType *ot)
2968 {
2969         /* identifiers */
2970         ot->name = "Select Next Loop";
2971         ot->idname = "MESH_OT_select_next_loop";
2972         ot->description = "Select next edge loop adjacent to a selected loop";
2973
2974         /* api callbacks */
2975         ot->exec = edbm_select_next_loop_exec;
2976         ot->poll = ED_operator_editmesh;
2977         
2978         /* flags */
2979         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2980 }
2981
2982
2983 static int edbm_region_to_loop_exec(bContext *C, wmOperator *UNUSED(op))
2984 {
2985         Object *obedit = CTX_data_edit_object(C);
2986         BMEditMesh *em = BMEdit_FromObject(obedit);
2987         BMFace *f;
2988         BMEdge *e;
2989         BMIter iter;
2990
2991         BM_mesh_elem_hflag_disable_all(em->bm, BM_EDGE, BM_ELEM_TAG, FALSE);
2992
2993         BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
2994                 BMLoop *l1, *l2;
2995                 BMIter liter1, liter2;
2996                 
2997                 BM_ITER_ELEM (l1, &liter1, f, BM_LOOPS_OF_FACE) {
2998                         int tot = 0, totsel = 0;
2999                         
3000                         BM_ITER_ELEM (l2, &liter2, l1->e, BM_LOOPS_OF_EDGE) {
3001                                 tot++;
3002                                 totsel += BM_elem_flag_test(l2->f, BM_ELEM_SELECT) != 0;
3003                         }
3004                         
3005                         if ((tot != totsel && totsel > 0) || (totsel == 1 && tot == 1))
3006                                 BM_elem_flag_enable(l1->e, BM_ELEM_TAG);
3007                 }
3008         }
3009
3010         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
3011         
3012         BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
3013                 if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
3014                         BM_edge_select_set(em->bm, e, TRUE);
3015                 }
3016         }
3017
3018         /* If in face-only select mode, switch to edge select mode so that
3019          * an edge-only selection is not inconsistent state */
3020         if (em->selectmode == SCE_SELECT_FACE) {
3021                 em->selectmode = SCE_SELECT_EDGE;
3022                 EDBM_selectmode_set(em);
3023                 EDBM_selectmode_to_scene(C);
3024         }
3025
3026         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
3027
3028         return OPERATOR_FINISHED;
3029 }
3030
3031 void MESH_OT_region_to_loop(wmOperatorType *ot)
3032 {
3033         /* identifiers */
3034         ot->name = "Select Boundary Loop";
3035         ot->idname = "MESH_OT_region_to_loop";
3036         ot->description = "Select boundary edges around the selected faces";
3037
3038         /* api callbacks */
3039         ot->exec = edbm_region_to_loop_exec;
3040         ot->poll = ED_operator_editmesh;
3041
3042         /* flags */
3043         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3044 }
3045
3046 static int loop_find_region(BMLoop *l, int flag,
3047                             SmallHash *fhash, BMFace ***region_out)
3048 {
3049         BLI_array_declare(region);
3050         BLI_array_declare(stack);
3051         BMFace **region = NULL;
3052         BMFace **stack = NULL;
3053         BMFace *f;
3054         
3055         BLI_array_append(stack, l->f);
3056         BLI_smallhash_insert(fhash, (uintptr_t)l->f, NULL);
3057         
3058         while (BLI_array_count(stack) > 0) {
3059                 BMIter liter1, liter2;
3060                 BMLoop *l1, *l2;
3061                 
3062                 f = BLI_array_pop(stack);
3063                 BLI_array_append(region, f);
3064                 
3065                 BM_ITER_ELEM (l1, &liter1, f, BM_LOOPS_OF_FACE) {
3066                         if (BM_elem_flag_test(l1->e, flag))
3067                                 continue;
3068                         
3069                         BM_ITER_ELEM (l2, &liter2, l1->e, BM_LOOPS_OF_EDGE) {
3070                                 if (BLI_smallhash_haskey(fhash, (uintptr_t)l2->f))
3071                                         continue;
3072                                 
3073                                 BLI_array_append(stack, l2->f);
3074                                 BLI_smallhash_insert(fhash, (uintptr_t)l2->f, NULL);
3075                         }
3076                 }
3077         }
3078         
3079         BLI_array_free(stack);
3080         
3081         *region_out = region;
3082         return BLI_array_count(region);
3083 }
3084
3085 static int verg_radial(const void *va, const void *vb)
3086 {
3087         BMEdge *e1 = *((void **)va);
3088         BMEdge *e2 = *((void **)vb);
3089         int a, b;
3090         
3091         a = BM_edge_face_count(e1);
3092         b = BM_edge_face_count(e2);
3093         
3094         if (a > b)  return -1;
3095         if (a == b) return  0;
3096         if (a < b)  return  1;
3097         
3098         return -1;
3099 }
3100
3101 static int loop_find_regions(BMEditMesh *em, int selbigger)
3102 {
3103         SmallHash visithash;
3104         BMIter iter;
3105         BMEdge *e, **edges = NULL;
3106         BLI_array_declare(edges);
3107         BMFace *f;
3108         int count = 0, i;
3109         
3110         BLI_smallhash_init(&visithash);
3111         
3112         BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
3113                 BM_elem_flag_disable(f, BM_ELEM_TAG);
3114         }
3115
3116         BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
3117                 if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
3118                         BLI_array_append(edges, e);
3119                         BM_elem_flag_enable(e, BM_ELEM_TAG);