svn merge -r40000:40179 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / editors / mesh / editmesh_mods.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2004 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/editors/mesh/editmesh_mods.c
31  *  \ingroup edmesh
32  */
33
34
35 /*
36
37 editmesh_mods.c, UI level access, no geometry changes 
38
39 */
40
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include "MEM_guardedalloc.h"
45
46 #include "DNA_material_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_modifier_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
51
52 #include "BLI_blenlib.h"
53 #include "BLI_math.h"
54 #include "BLI_editVert.h"
55 #include "BLI_rand.h"
56 #include "BLI_utildefines.h"
57
58 #include "BKE_context.h"
59 #include "BKE_displist.h"
60 #include "BKE_depsgraph.h"
61 #include "BKE_mesh.h"
62 #include "BKE_material.h"
63 #include "BKE_paint.h"
64 #include "BKE_report.h"
65 #include "BKE_texture.h"
66
67 #include "IMB_imbuf_types.h"
68 #include "IMB_imbuf.h"
69
70 #include "RE_render_ext.h"  /* externtex */
71
72 #include "WM_api.h"
73 #include "WM_types.h"
74
75
76 #include "RNA_access.h"
77 #include "RNA_define.h"
78
79 #include "ED_mesh.h"
80 #include "ED_screen.h"
81 #include "ED_view3d.h"
82 #include "ED_uvedit.h"
83
84 #include "BIF_gl.h"
85
86 #include "mesh_intern.h"
87
88 #include "BLO_sys_types.h" // for intptr_t support
89
90 /* XXX */
91 static void waitcursor(int UNUSED(val)) {}
92 static int pupmenu(const char *UNUSED(arg)) {return 0;}
93
94 /* ****************************** MIRROR **************** */
95
96 void EM_cache_x_mirror_vert(struct Object *ob, struct EditMesh *em)
97 {
98         EditVert *eve, *eve_mirror;
99         int index= 0;
100
101         for(eve= em->verts.first; eve; eve= eve->next) {
102                 eve->tmp.v= NULL;
103         }
104
105         for(eve= em->verts.first; eve; eve= eve->next, index++) {
106                 if(eve->tmp.v==NULL) {
107                         eve_mirror = editmesh_get_x_mirror_vert(ob, em, eve, eve->co, index);
108                         if(eve_mirror) {
109                                 eve->tmp.v= eve_mirror;
110                                 eve_mirror->tmp.v = eve;
111                         }
112                 }
113         }
114 }
115
116 static void EM_select_mirrored(Object *obedit, EditMesh *em, int extend)
117 {
118
119         EditVert *eve;
120
121         EM_cache_x_mirror_vert(obedit, em);
122
123         for(eve= em->verts.first; eve; eve= eve->next) {
124                 if(eve->f & SELECT && eve->tmp.v && (eve->tmp.v != eve->tmp.v->tmp.v)) {
125                         eve->tmp.v->f |= SELECT;
126
127                         if(extend==FALSE)
128                                 eve->f &= ~SELECT;
129
130                         /* remove the interference */
131                         eve->tmp.v->tmp.v= NULL;
132                         eve->tmp.v= NULL;
133                 }
134         }
135 }
136
137 void EM_automerge(Scene *scene, Object *obedit, int update)
138 {
139         Mesh *me= obedit ? obedit->data : NULL; /* can be NULL */
140         int len;
141
142         if ((scene->toolsettings->automerge) &&
143                 (obedit && obedit->type==OB_MESH && (obedit->mode & OB_MODE_EDIT))
144           ) {
145                 EditMesh *em= me->edit_mesh;
146                 int totvert= em->totvert, totedge= em->totedge, totface= em->totface;
147
148                 len = removedoublesflag(em, 1, 1, scene->toolsettings->doublimit);
149                 if (totvert != em->totvert || totedge != em->totedge || totface != em->totface) {
150                         if (update) {
151                                 DAG_id_tag_update(&me->id, 0);
152                         }
153                 }
154         }
155 }
156
157 /* ****************************** SELECTION ROUTINES **************** */
158
159 unsigned int em_solidoffs=0, em_wireoffs=0, em_vertoffs=0;      /* set in drawobject.c ... for colorindices */
160
161 /* facilities for border select and circle select */
162 static char *selbuf= NULL;
163
164 /* opengl doesn't support concave... */
165 static void draw_triangulated(int mcords[][2], short tot)
166 {
167         ListBase lb={NULL, NULL};
168         DispList *dl;
169         float *fp;
170         int a;
171         
172         /* make displist */
173         dl= MEM_callocN(sizeof(DispList), "poly disp");
174         dl->type= DL_POLY;
175         dl->parts= 1;
176         dl->nr= tot;
177         dl->verts= fp=  MEM_callocN(tot*3*sizeof(float), "poly verts");
178         BLI_addtail(&lb, dl);
179         
180         for(a=0; a<tot; a++, fp+=3) {
181                 fp[0]= (float)mcords[a][0];
182                 fp[1]= (float)mcords[a][1];
183         }
184         
185         /* do the fill */
186         filldisplist(&lb, &lb, 0);
187
188         /* do the draw */
189         dl= lb.first;   /* filldisplist adds in head of list */
190         if(dl->type==DL_INDEX3) {
191                 int *index;
192                 
193                 a= dl->parts;
194                 fp= dl->verts;
195                 index= dl->index;
196                 glBegin(GL_TRIANGLES);
197                 while(a--) {
198                         glVertex3fv(fp+3*index[0]);
199                         glVertex3fv(fp+3*index[1]);
200                         glVertex3fv(fp+3*index[2]);
201                         index+= 3;
202                 }
203                 glEnd();
204         }
205         
206         freedisplist(&lb);
207 }
208
209
210 /* reads rect, and builds selection array for quick lookup */
211 /* returns if all is OK */
212 int EM_init_backbuf_border(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
213 {
214         struct ImBuf *buf;
215         unsigned int *dr;
216         int a;
217         
218         if(vc->obedit==NULL || vc->v3d->drawtype<OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT)==0) return 0;
219         
220         buf= view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
221         if(buf==NULL) return 0;
222         if(em_vertoffs==0) return 0;
223
224         dr = buf->rect;
225         
226         /* build selection lookup */
227         selbuf= MEM_callocN(em_vertoffs+1, "selbuf");
228         
229         a= (xmax-xmin+1)*(ymax-ymin+1);
230         while(a--) {
231                 if(*dr>0 && *dr<=em_vertoffs) 
232                         selbuf[*dr]= 1;
233                 dr++;
234         }
235         IMB_freeImBuf(buf);
236         return 1;
237 }
238
239 int EM_check_backbuf(unsigned int index)
240 {
241         if(selbuf==NULL) return 1;
242         if(index>0 && index<=em_vertoffs)
243                 return selbuf[index];
244         return 0;
245 }
246
247 void EM_free_backbuf(void)
248 {
249         if(selbuf) MEM_freeN(selbuf);
250         selbuf= NULL;
251 }
252
253 /* mcords is a polygon mask
254    - grab backbuffer,
255    - draw with black in backbuffer, 
256    - grab again and compare
257    returns 'OK' 
258 */
259 int EM_mask_init_backbuf_border(ViewContext *vc, int mcords[][2], short tot, short xmin, short ymin, short xmax, short ymax)
260 {
261         unsigned int *dr, *drm;
262         struct ImBuf *buf, *bufmask;
263         int a;
264         GLboolean is_cull;
265         
266         /* method in use for face selecting too */
267         if(vc->obedit==NULL) {
268                 if(paint_facesel_test(vc->obact));
269                 else if(paint_vertsel_test(vc->obact));//Jason
270                 else return 0;
271         }
272         else if(vc->v3d->drawtype<OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT)==0) return 0;
273
274         buf= view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
275         if(buf==NULL) return 0;
276         if(em_vertoffs==0) return 0;
277
278         dr = buf->rect;
279
280         /* draw the mask */
281         glDisable(GL_DEPTH_TEST);
282         
283         glColor3ub(0, 0, 0);
284
285         /* some opengl drivers have problems with draw direction */
286         glGetBooleanv(GL_CULL_FACE, &is_cull);
287         if(is_cull) glDisable(GL_CULL_FACE);
288         
289         /* yah, opengl doesn't do concave... tsk! */
290         ED_region_pixelspace(vc->ar);
291         draw_triangulated(mcords, tot);
292         
293         glBegin(GL_LINE_LOOP);  /* for zero sized masks, lines */
294         for(a=0; a<tot; a++) glVertex2iv(mcords[a]);
295         glEnd();
296         
297         glFinish();     /* to be sure readpixels sees mask */
298         
299         /* grab mask */
300         bufmask= view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
301         drm = bufmask->rect;
302         if(bufmask==NULL) return 0; /* only when mem alloc fails, go crash somewhere else! */
303         
304         /* build selection lookup */
305         selbuf= MEM_callocN(em_vertoffs+1, "selbuf");
306         
307         a= (xmax-xmin+1)*(ymax-ymin+1);
308         while(a--) {
309                 if(*dr>0 && *dr<=em_vertoffs && *drm==0) selbuf[*dr]= 1;
310                 dr++; drm++;
311         }
312         IMB_freeImBuf(buf);
313         IMB_freeImBuf(bufmask);
314         
315         if(is_cull) glEnable(GL_CULL_FACE);
316
317         return 1;
318         
319 }
320
321 /* circle shaped sample area */
322 int EM_init_backbuf_circle(ViewContext *vc, short xs, short ys, short rads)
323 {
324         struct ImBuf *buf;
325         unsigned int *dr;
326         short xmin, ymin, xmax, ymax, xc, yc;
327         int radsq;
328         
329         /* method in use for face selecting too */
330         if(vc->obedit==NULL) {
331                 if(paint_facesel_test(vc->obact));
332                 else if (paint_vertsel_test(vc->obact));//Jason
333                 else return 0;
334         }
335         else if(vc->v3d->drawtype<OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT)==0) return 0;
336         
337         xmin= xs-rads; xmax= xs+rads;
338         ymin= ys-rads; ymax= ys+rads;
339         buf= view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
340         if(em_vertoffs==0) return 0;
341         if(buf==NULL) return 0;
342
343         dr = buf->rect;
344         
345         /* build selection lookup */
346         selbuf= MEM_callocN(em_vertoffs+1, "selbuf");
347         radsq= rads*rads;
348         for(yc= -rads; yc<=rads; yc++) {
349                 for(xc= -rads; xc<=rads; xc++, dr++) {
350                         if(xc*xc + yc*yc < radsq) {
351                                 if(*dr>0 && *dr<=em_vertoffs) selbuf[*dr]= 1;
352                         }
353                 }
354         }
355
356         IMB_freeImBuf(buf);
357         return 1;
358         
359 }
360
361 static void findnearestvert__doClosest(void *userData, EditVert *eve, int x, int y, int index)
362 {
363         struct { int mval[2]; short pass, select, strict; int dist, lastIndex, closestIndex; EditVert *closest; } *data = userData;
364
365         if (data->pass==0) {
366                 if (index<=data->lastIndex)
367                         return;
368         } else {
369                 if (index>data->lastIndex)
370                         return;
371         }
372
373         if (data->dist>3) {
374                 int temp = abs(data->mval[0] - x) + abs(data->mval[1]- y);
375                 if ((eve->f&1) == data->select) {
376                         if (data->strict == 1)
377                                 return;
378                         else
379                                 temp += 5;
380                 }
381
382                 if (temp<data->dist) {
383                         data->dist = temp;
384                         data->closest = eve;
385                         data->closestIndex = index;
386                 }
387         }
388 }
389
390
391
392
393 static unsigned int findnearestvert__backbufIndextest(void *handle, unsigned int index)
394 {
395         EditMesh *em= (EditMesh *)handle;
396         EditVert *eve = BLI_findlink(&em->verts, index-1);
397
398         if(eve && (eve->f & SELECT)) return 0;
399         return 1; 
400 }
401 /**
402  * findnearestvert
403  * 
404  * dist (in/out): minimal distance to the nearest and at the end, actual distance
405  * sel: selection bias
406  *              if SELECT, selected vertice are given a 5 pixel bias to make them farter than unselect verts
407  *              if 0, unselected vertice are given the bias
408  * strict: if 1, the vertice corresponding to the sel parameter are ignored and not just biased 
409  */
410 EditVert *findnearestvert(ViewContext *vc, int *dist, short sel, short strict)
411 {
412         if(vc->v3d->drawtype>OB_WIRE && (vc->v3d->flag & V3D_ZBUF_SELECT)){
413                 int distance;
414                 unsigned int index;
415                 EditVert *eve;
416                 
417                 if(strict) index = view3d_sample_backbuf_rect(vc, vc->mval, 50, em_wireoffs, 0xFFFFFF, &distance, strict, vc->em, findnearestvert__backbufIndextest); 
418                 else index = view3d_sample_backbuf_rect(vc, vc->mval, 50, em_wireoffs, 0xFFFFFF, &distance, 0, NULL, NULL); 
419                 
420                 eve = BLI_findlink(&vc->em->verts, index-1);
421                 
422                 if(eve && distance < *dist) {
423                         *dist = distance;
424                         return eve;
425                 } else {
426                         return NULL;
427                 }
428                         
429         }
430         else {
431                 struct { int mval[2]; short pass, select, strict; int dist, lastIndex, closestIndex; EditVert *closest; } data;
432                 static int lastSelectedIndex=0;
433                 static EditVert *lastSelected=NULL;
434
435                 if (lastSelected && BLI_findlink(&vc->em->verts, lastSelectedIndex)!=lastSelected) {
436                         lastSelectedIndex = 0;
437                         lastSelected = NULL;
438                 }
439
440                 data.lastIndex = lastSelectedIndex;
441                 data.mval[0] = vc->mval[0];
442                 data.mval[1] = vc->mval[1];
443                 data.select = sel;
444                 data.dist = *dist;
445                 data.strict = strict;
446                 data.closest = NULL;
447                 data.closestIndex = 0;
448
449                 data.pass = 0;
450
451                 ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
452
453                 mesh_foreachScreenVert(vc, findnearestvert__doClosest, &data, 1);
454
455                 if (data.dist>3) {
456                         data.pass = 1;
457                         mesh_foreachScreenVert(vc, findnearestvert__doClosest, &data, 1);
458                 }
459
460                 *dist = data.dist;
461                 lastSelected = data.closest;
462                 lastSelectedIndex = data.closestIndex;
463
464                 return data.closest;
465         }
466 }
467
468 /* returns labda for closest distance v1 to line-piece v2-v3 */
469 static float labda_PdistVL2Dfl( float *v1, float *v2, float *v3) 
470 {
471         float rc[2], len;
472         
473         rc[0]= v3[0]-v2[0];
474         rc[1]= v3[1]-v2[1];
475         len= rc[0]*rc[0]+ rc[1]*rc[1];
476         if(len==0.0f)
477                 return 0.0f;
478         
479         return ( rc[0]*(v1[0]-v2[0]) + rc[1]*(v1[1]-v2[1]) )/len;
480 }
481
482 /* note; uses v3d, so needs active 3d window */
483 static void findnearestedge__doClosest(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int UNUSED(index))
484 {
485         struct { ViewContext vc; float mval[2]; int dist; EditEdge *closest; } *data = userData;
486         float v1[2], v2[2];
487         int distance;
488
489         ED_view3d_local_clipping(data->vc.rv3d, data->vc.obedit->obmat); /* for local clipping lookups */
490
491         v1[0] = x0;
492         v1[1] = y0;
493         v2[0] = x1;
494         v2[1] = y1;
495
496         distance= dist_to_line_segment_v2(data->mval, v1, v2);
497
498
499         if(eed->f & SELECT) distance+=5;
500         if(distance < data->dist) {
501                 if(data->vc.rv3d->rflag & RV3D_CLIPPING) {
502                         float labda= labda_PdistVL2Dfl(data->mval, v1, v2);
503                         float vec[3];
504
505                         vec[0]= eed->v1->co[0] + labda*(eed->v2->co[0] - eed->v1->co[0]);
506                         vec[1]= eed->v1->co[1] + labda*(eed->v2->co[1] - eed->v1->co[1]);
507                         vec[2]= eed->v1->co[2] + labda*(eed->v2->co[2] - eed->v1->co[2]);
508
509                         if(ED_view3d_test_clipping(data->vc.rv3d, vec, 1)==0) {
510                                 data->dist = distance;
511                                 data->closest = eed;
512                         }
513                 }
514                 else {
515                         data->dist = distance;
516                         data->closest = eed;
517                 }
518         }
519 }
520 EditEdge *findnearestedge(ViewContext *vc, int *dist)
521 {
522
523         if(vc->v3d->drawtype>OB_WIRE && (vc->v3d->flag & V3D_ZBUF_SELECT)) {
524                 int distance;
525                 unsigned int index = view3d_sample_backbuf_rect(vc, vc->mval, 50, em_solidoffs, em_wireoffs, &distance,0, NULL, NULL);
526                 EditEdge *eed = BLI_findlink(&vc->em->edges, index-1);
527
528                 if (eed && distance<*dist) {
529                         *dist = distance;
530                         return eed;
531                 } else {
532                         return NULL;
533                 }
534         }
535         else {
536                 struct { ViewContext vc; float mval[2]; int dist; EditEdge *closest; } data;
537
538                 data.vc= *vc;
539                 data.mval[0] = vc->mval[0];
540                 data.mval[1] = vc->mval[1];
541                 data.dist = *dist;
542                 data.closest = NULL;
543
544                 ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
545                 mesh_foreachScreenEdge(vc, findnearestedge__doClosest, &data, 2);
546
547                 *dist = data.dist;
548                 return data.closest;
549         }
550 }
551
552 static void findnearestface__getDistance(void *userData, EditFace *efa, int x, int y, int UNUSED(index))
553 {
554         struct { int mval[2]; int dist; EditFace *toFace; } *data = userData;
555
556         if (efa==data->toFace) {
557                 int temp = abs(data->mval[0]-x) + abs(data->mval[1]-y);
558
559                 if (temp<data->dist)
560                         data->dist = temp;
561         }
562 }
563 static void findnearestface__doClosest(void *userData, EditFace *efa, int x, int y, int index)
564 {
565         struct { int mval[2]; short pass; int dist, lastIndex, closestIndex; EditFace *closest; } *data = userData;
566
567         if (data->pass==0) {
568                 if (index<=data->lastIndex)
569                         return;
570         } else {
571                 if (index>data->lastIndex)
572                         return;
573         }
574
575         if (data->dist>3) {
576                 int temp = abs(data->mval[0]-x) + abs(data->mval[1]-y);
577
578                 if (temp<data->dist) {
579                         data->dist = temp;
580                         data->closest = efa;
581                         data->closestIndex = index;
582                 }
583         }
584 }
585 static EditFace *findnearestface(ViewContext *vc, int *dist)
586 {
587
588         if(vc->v3d->drawtype>OB_WIRE && (vc->v3d->flag & V3D_ZBUF_SELECT)) {
589                 unsigned int index = view3d_sample_backbuf(vc, vc->mval[0], vc->mval[1]);
590                 EditFace *efa = BLI_findlink(&vc->em->faces, index-1);
591
592                 if (efa) {
593                         struct { int mval[2]; int dist; EditFace *toFace; } data;
594
595                         data.mval[0] = vc->mval[0];
596                         data.mval[1] = vc->mval[1];
597                         data.dist = 0x7FFF;             /* largest short */
598                         data.toFace = efa;
599
600                         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
601                         mesh_foreachScreenFace(vc, findnearestface__getDistance, &data);
602
603                         if(vc->em->selectmode == SCE_SELECT_FACE || data.dist<*dist) {  /* only faces, no dist check */
604                                 *dist= data.dist;
605                                 return efa;
606                         }
607                 }
608                 
609                 return NULL;
610         }
611         else {
612                 struct { int mval[2]; short pass; int dist, lastIndex, closestIndex; EditFace *closest; } data;
613                 static int lastSelectedIndex=0;
614                 static EditFace *lastSelected=NULL;
615
616                 if (lastSelected && BLI_findlink(&vc->em->faces, lastSelectedIndex)!=lastSelected) {
617                         lastSelectedIndex = 0;
618                         lastSelected = NULL;
619                 }
620
621                 data.lastIndex = lastSelectedIndex;
622                 data.mval[0] = vc->mval[0];
623                 data.mval[1] = vc->mval[1];
624                 data.dist = *dist;
625                 data.closest = NULL;
626                 data.closestIndex = 0;
627
628                 data.pass = 0;
629
630                 ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
631                 mesh_foreachScreenFace(vc, findnearestface__doClosest, &data);
632
633                 if (data.dist>3) {
634                         data.pass = 1;
635                         mesh_foreachScreenFace(vc, findnearestface__doClosest, &data);
636                 }
637
638                 *dist = data.dist;
639                 lastSelected = data.closest;
640                 lastSelectedIndex = data.closestIndex;
641
642                 return data.closest;
643         }
644 }
645
646 /* best distance based on screen coords. 
647    use em->selectmode to define how to use 
648    selected vertices and edges get disadvantage
649    return 1 if found one
650 */
651 static int unified_findnearest(ViewContext *vc, EditVert **eve, EditEdge **eed, EditFace **efa) 
652 {
653         EditMesh *em= vc->em;
654         int dist= 75;
655         
656         *eve= NULL;
657         *eed= NULL;
658         *efa= NULL;
659         
660         /* no afterqueue (yet), so we check it now, otherwise the em_xxxofs indices are bad */
661         view3d_validate_backbuf(vc);
662         
663         if(em->selectmode & SCE_SELECT_VERTEX)
664                 *eve= findnearestvert(vc, &dist, SELECT, 0);
665         if(em->selectmode & SCE_SELECT_FACE)
666                 *efa= findnearestface(vc, &dist);
667
668         dist-= 20;      /* since edges select lines, we give dots advantage of 20 pix */
669         if(em->selectmode & SCE_SELECT_EDGE)
670                 *eed= findnearestedge(vc, &dist);
671
672         /* return only one of 3 pointers, for frontbuffer redraws */
673         if(*eed) {
674                 *efa= NULL; *eve= NULL;
675         }
676         else if(*efa) {
677                 *eve= NULL;
678         }
679         
680         return (*eve || *eed || *efa);
681 }
682
683
684 /* ****************  SIMILAR "group" SELECTS. FACE, EDGE AND VERTEX ************** */
685
686 /* selects new faces/edges/verts based on the existing selection */
687
688 /* VERT GROUP */
689
690 #define SIMVERT_NORMAL  0
691 #define SIMVERT_FACE    1
692 #define SIMVERT_VGROUP  2
693 #define SIMVERT_TOT             3
694
695 /* EDGE GROUP */
696
697 #define SIMEDGE_LENGTH          101
698 #define SIMEDGE_DIR                     102
699 #define SIMEDGE_FACE            103
700 #define SIMEDGE_FACE_ANGLE      104
701 #define SIMEDGE_CREASE          105
702 #define SIMEDGE_SEAM            106
703 #define SIMEDGE_SHARP           107
704 #define SIMEDGE_TOT                     108
705
706 /* FACE GROUP */
707
708 #define SIMFACE_MATERIAL        201
709 #define SIMFACE_IMAGE           202
710 #define SIMFACE_AREA            203
711 #define SIMFACE_PERIMETER       204
712 #define SIMFACE_NORMAL          205
713 #define SIMFACE_COPLANAR        206
714 #define SIMFACE_TOT                     207
715
716 static EnumPropertyItem prop_similar_types[] = {
717         {SIMVERT_NORMAL, "NORMAL", 0, "Normal", ""},
718         {SIMVERT_FACE, "FACE", 0, "Amount of Vertices in Face", ""},
719         {SIMVERT_VGROUP, "VGROUP", 0, "Vertex Groups", ""},
720         {SIMEDGE_LENGTH, "LENGTH", 0, "Length", ""},
721         {SIMEDGE_DIR, "DIR", 0, "Direction", ""},
722         {SIMEDGE_FACE, "FACE", 0, "Amount of Vertices in Face", ""},
723         {SIMEDGE_FACE_ANGLE, "FACE_ANGLE", 0, "Face Angles", ""},
724         {SIMEDGE_CREASE, "CREASE", 0, "Crease", ""},
725         {SIMEDGE_SEAM, "SEAM", 0, "Seam", ""},
726         {SIMEDGE_SHARP, "SHARP", 0, "Sharpness", ""},
727         {SIMFACE_MATERIAL, "MATERIAL", 0, "Material", ""},
728         {SIMFACE_IMAGE, "IMAGE", 0, "Image", ""},
729         {SIMFACE_AREA, "AREA", 0, "Area", ""},
730         {SIMFACE_PERIMETER, "PERIMETER", 0, "Perimeter", ""},
731         {SIMFACE_NORMAL, "NORMAL", 0, "Normal", ""},
732         {SIMFACE_COPLANAR, "COPLANAR", 0, "Co-planar", ""},
733         {0, NULL, 0, NULL, NULL}
734 };
735
736
737 /* this as a way to compare the ares, perim  of 2 faces thay will scale to different sizes
738 *0.5 so smaller faces arnt ALWAYS selected with a thresh of 1.0 */
739 #define SCALE_CMP(a,b) ((a+a*thresh >= b) && (a-(a*thresh*0.5f) <= b))
740
741 static int similar_face_select__internal(EditMesh *em, int mode, float thresh)
742 {
743         EditFace *efa, *base_efa=NULL;
744         unsigned int selcount=0; /*count how many new faces we select*/
745         
746         /*deselcount, count how many deselected faces are left, so we can bail out early
747         also means that if there are no deselected faces, we can avoid a lot of looping */
748         unsigned int deselcount=0; 
749         short ok=0;
750         
751         for(efa= em->faces.first; efa; efa= efa->next) {
752                 if (!efa->h) {
753                         if (efa->f & SELECT) {
754                                 efa->f1=1;
755                                 ok=1;
756                         } else {
757                                 efa->f1=0;
758                                 deselcount++; /* a deselected face we may select later */
759                         }
760                 }
761         }
762         
763         if (!ok || !deselcount) /* no data selected OR no more data to select */
764                 return 0;
765         
766         if (mode==SIMFACE_AREA) {
767                 for(efa= em->faces.first; efa; efa= efa->next) {
768                         efa->tmp.fp= EM_face_area(efa);
769                 }
770         } else if (mode==SIMFACE_PERIMETER) {
771                 for(efa= em->faces.first; efa; efa= efa->next) {
772                         efa->tmp.fp= EM_face_perimeter(efa);
773                 }
774         }
775         
776         for(base_efa= em->faces.first; base_efa; base_efa= base_efa->next) {
777                 if (base_efa->f1) { /* This was one of the faces originaly selected */
778                         if (mode==SIMFACE_MATERIAL) { /* same material */
779                                 for(efa= em->faces.first; efa; efa= efa->next) {
780                                         if (
781                                                 !(efa->f & SELECT) &&
782                                                 !efa->h &&
783                                                 base_efa->mat_nr == efa->mat_nr
784                                         ) {
785                                                 EM_select_face(efa, 1);
786                                                 selcount++;
787                                                 deselcount--;
788                                                 if (!deselcount) /*have we selected all posible faces?, if so return*/
789                                                         return selcount;
790                                         }
791                                 }
792                         } else if (mode==SIMFACE_IMAGE) { /* same image */
793                                 MTFace *tf, *base_tf;
794
795                                 base_tf = (MTFace*)CustomData_em_get(&em->fdata, base_efa->data,
796                                                                                                          CD_MTFACE);
797
798                                 if(!base_tf)
799                                         return selcount;
800
801                                 for(efa= em->faces.first; efa; efa= efa->next) {
802                                         if (!(efa->f & SELECT) && !efa->h) {
803                                                 tf = (MTFace*)CustomData_em_get(&em->fdata, efa->data,
804                                                                                                                 CD_MTFACE);
805
806                                                 if(base_tf->tpage == tf->tpage) {
807                                                         EM_select_face(efa, 1);
808                                                         selcount++;
809                                                         deselcount--;
810                                                         if (!deselcount) /*have we selected all posible faces?, if so return*/
811                                                                 return selcount;
812                                                 }
813                                         }
814                                 }
815                         } else if (mode==SIMFACE_AREA || mode==SIMFACE_PERIMETER) { /* same area OR same perimeter, both use the same temp var */
816                                 for(efa= em->faces.first; efa; efa= efa->next) {
817                                         if (
818                                                 (!(efa->f & SELECT) && !efa->h) &&
819                                                 SCALE_CMP(base_efa->tmp.fp, efa->tmp.fp)
820                                         ) {
821                                                 EM_select_face(efa, 1);
822                                                 selcount++;
823                                                 deselcount--;
824                                                 if (!deselcount) /*have we selected all posible faces?, if so return*/
825                                                         return selcount;
826                                         }
827                                 }
828                         } else if (mode==SIMFACE_NORMAL) {
829                                 float angle;
830                                 for(efa= em->faces.first; efa; efa= efa->next) {
831                                         if (!(efa->f & SELECT) && !efa->h) {
832                                                 angle= RAD2DEGF(angle_v3v3(base_efa->n, efa->n));
833                                                 if (angle/180.0f<=thresh) {
834                                                         EM_select_face(efa, 1);
835                                                         selcount++;
836                                                         deselcount--;
837                                                         if (!deselcount) /*have we selected all posible faces?, if so return*/
838                                                                 return selcount;
839                                                 }
840                                         }
841                                 }
842                         } else if (mode==SIMFACE_COPLANAR) { /* same planer */
843                                 float angle, base_dot, dot;
844                                 base_dot= dot_v3v3(base_efa->cent, base_efa->n);
845                                 for(efa= em->faces.first; efa; efa= efa->next) {
846                                         if (!(efa->f & SELECT) && !efa->h) {
847                                                 angle= RAD2DEGF(angle_v3v3(base_efa->n, efa->n));
848                                                 if (angle/180.0f<=thresh) {
849                                                         dot=dot_v3v3(efa->cent, base_efa->n);
850                                                         if (fabsf(base_dot-dot) <= thresh) {
851                                                                 EM_select_face(efa, 1);
852                                                                 selcount++;
853                                                                 deselcount--;
854                                                                 if (!deselcount) /*have we selected all posible faces?, if so return*/
855                                                                         return selcount;
856                                                         }
857                                                 }
858                                         }
859                                 }
860                         }
861                 }
862         } /* end base_efa loop */
863         return selcount;
864 }
865
866 static int similar_face_select_exec(bContext *C, wmOperator *op)
867 {
868         Object *obedit= CTX_data_edit_object(C);
869         Mesh *me= obedit->data;
870         EditMesh *em= BKE_mesh_get_editmesh(me); 
871
872         int selcount = similar_face_select__internal(em, RNA_enum_get(op->ptr, "type"), RNA_float_get(op->ptr, "threshold"));
873         
874         if (selcount) {
875                 /* here was an edge-mode only select flush case, has to be generalized */
876                 EM_selectmode_flush(em);
877                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
878                 BKE_mesh_end_editmesh(me, em);
879                 return OPERATOR_FINISHED;
880         }
881         
882         BKE_mesh_end_editmesh(me, em);
883         return OPERATOR_CANCELLED;
884 }       
885
886 /* ***************************************************** */
887
888 static int similar_edge_select__internal(EditMesh *em, int mode, float thresh)
889 {
890         EditEdge *eed, *base_eed=NULL;
891         unsigned int selcount=0; /* count how many new edges we select*/
892         
893         /*count how many visible selected edges there are,
894         so we can return when there are none left */
895         unsigned int deselcount=0;
896         
897         short ok=0;
898         
899         for(eed= em->edges.first; eed; eed= eed->next) {
900                 if (!eed->h) {
901                         if (eed->f & SELECT) {
902                                 eed->f1=1;
903                                 ok=1;
904                         } else {
905                                 eed->f1=0;
906                                 deselcount++;
907                         }
908                         /* set all eed->tmp.l to 0 we use it later.
909                         for counting face users*/
910                         eed->tmp.l=0;
911                         eed->f2=0; /* only for mode SIMEDGE_FACE_ANGLE, edge animations */
912                 }
913         }
914         
915         if (!ok || !deselcount) /* no data selected OR no more data to select*/
916                 return 0;
917         
918         if (mode==SIMEDGE_LENGTH) { /*store length*/
919                 for(eed= em->edges.first; eed; eed= eed->next) {
920                         if (!eed->h) /* dont calc data for hidden edges*/
921                                 eed->tmp.fp= len_v3v3(eed->v1->co, eed->v2->co);
922                 }
923         } else if (mode==SIMEDGE_FACE) { /*store face users*/
924                 EditFace *efa;
925                 /* cound how many faces each edge uses use tmp->l */
926                 for(efa= em->faces.first; efa; efa= efa->next) {
927                         efa->e1->tmp.l++;
928                         efa->e2->tmp.l++;
929                         efa->e3->tmp.l++;
930                         if (efa->e4) efa->e4->tmp.l++;
931                 }
932         } else if (mode==SIMEDGE_FACE_ANGLE) { /*store edge angles */
933                 EditFace *efa;
934                 int j;
935                 /* cound how many faces each edge uses use tmp.l */
936                 for(efa= em->faces.first; efa; efa= efa->next) {
937                         /* here we use the edges temp data to assign a face
938                         if a face has already been assigned (eed->f2==1)
939                         we calculate the angle between the current face and
940                         the edges previously found face.
941                         store the angle in eed->tmp.fp (loosing the face eed->tmp.f)
942                         but tagging eed->f2==2, so we know not to look at it again.
943                         This only works for edges that connect to 2 faces. but its good enough
944                         */
945                         
946                         /* se we can loop through face edges*/
947                         j=0;
948                         eed= efa->e1;
949                         while (j<4) {
950                                 if (j==1) eed= efa->e2;
951                                 else if (j==2) eed= efa->e3;
952                                 else if (j==3) {
953                                         eed= efa->e4;
954                                         if (!eed)
955                                                 break;
956                                 } /* done looping */
957                                 
958                                 if (!eed->h) { /* dont calc data for hidden edges*/
959                                         if (eed->f2==2)
960                                                 break;
961                                         else if (eed->f2==0) /* first access, assign the face */
962                                                 eed->tmp.f= efa;
963                                         else if (eed->f2==1) /* second, we assign the angle*/
964                                                 eed->tmp.fp= RAD2DEGF(angle_v3v3(eed->tmp.f->n, efa->n))/180;
965                                         eed->f2++; /* f2==0 no face assigned. f2==1 one face found. f2==2 angle calculated.*/
966                                 }
967                                 j++;
968                         }
969                 }
970         }
971         
972         for(base_eed= em->edges.first; base_eed; base_eed= base_eed->next) {
973                 if (base_eed->f1) {
974                         if (mode==SIMEDGE_LENGTH) { /* same length */
975                                 for(eed= em->edges.first; eed; eed= eed->next) {
976                                         if (
977                                                 !(eed->f & SELECT) &&
978                                                 !eed->h &&
979                                                 SCALE_CMP(base_eed->tmp.fp, eed->tmp.fp)
980                                         ) {
981                                                 EM_select_edge(eed, 1);
982                                                 selcount++;
983                                                 deselcount--;
984                                                 if (!deselcount) /*have we selected all posible faces?, if so return*/
985                                                         return selcount;
986                                         }
987                                 }
988                         } else if (mode==SIMEDGE_DIR) { /* same direction */
989                                 float base_dir[3], dir[3], angle;
990                                 sub_v3_v3v3(base_dir, base_eed->v1->co, base_eed->v2->co);
991                                 for(eed= em->edges.first; eed; eed= eed->next) {
992                                         if (!(eed->f & SELECT) && !eed->h) {
993                                                 sub_v3_v3v3(dir, eed->v1->co, eed->v2->co);
994                                                 angle= RAD2DEGF(angle_v3v3(base_dir, dir));
995                                                 
996                                                 if (angle>90.0f) /* use the smallest angle between the edges */
997                                                         angle= fabsf(angle-180.0f);
998                                                 
999                                                 if (angle / 90.0f<=thresh) {
1000                                                         EM_select_edge(eed, 1);
1001                                                         selcount++;
1002                                                         deselcount--;
1003                                                         if (!deselcount) /*have we selected all posible faces?, if so return*/
1004                                                                 return selcount;
1005                                                 }
1006                                         }
1007                                 }
1008                         } else if (mode==SIMEDGE_FACE) { /* face users */                               
1009                                 for(eed= em->edges.first; eed; eed= eed->next) {
1010                                         if (
1011                                                 !(eed->f & SELECT) &&
1012                                                 !eed->h &&
1013                                                 base_eed->tmp.l==eed->tmp.l
1014                                         ) {
1015                                                 EM_select_edge(eed, 1);
1016                                                 selcount++;
1017                                                 deselcount--;
1018                                                 if (!deselcount) /*have we selected all posible faces?, if so return*/
1019                                                         return selcount;
1020                                         }
1021                                 }
1022                         } else if (mode==SIMEDGE_FACE_ANGLE && base_eed->f2==2) { /* edge angles, f2==2 means the edge has an angle. */                         
1023                                 for(eed= em->edges.first; eed; eed= eed->next) {
1024                                         if (
1025                                                 !(eed->f & SELECT) &&
1026                                                 !eed->h &&
1027                                                 eed->f2==2 &&
1028                                                 (fabsf(base_eed->tmp.fp-eed->tmp.fp)<=thresh)
1029                                         ) {
1030                                                 EM_select_edge(eed, 1);
1031                                                 selcount++;
1032                                                 deselcount--;
1033                                                 if (!deselcount) /*have we selected all posible faces?, if so return*/
1034                                                         return selcount;
1035                                         }
1036                                 }
1037                         } else if (mode==SIMEDGE_CREASE) { /* edge crease */
1038                                 for(eed= em->edges.first; eed; eed= eed->next) {
1039                                         if (
1040                                                 !(eed->f & SELECT) &&
1041                                                 !eed->h &&
1042                                                 (fabsf(base_eed->crease-eed->crease) <= thresh)
1043                                         ) {
1044                                                 EM_select_edge(eed, 1);
1045                                                 selcount++;
1046                                                 deselcount--;
1047                                                 if (!deselcount) /*have we selected all posible faces?, if so return*/
1048                                                         return selcount;
1049                                         }
1050                                 }
1051                         } else if (mode==SIMEDGE_SEAM) { /* edge seam */
1052                                 for(eed= em->edges.first; eed; eed= eed->next) {
1053                                         if (
1054                                                 !(eed->f & SELECT) &&
1055                                                 !eed->h &&
1056                                                 (eed->seam == base_eed->seam)
1057                                         ) {
1058                                                 EM_select_edge(eed, 1);
1059                                                 selcount++;
1060                                                 deselcount--;
1061                                                 if (!deselcount) /*have we selected all posible faces?, if so return*/
1062                                                         return selcount;
1063                                         }
1064                                 }
1065                         } else if (mode==SIMEDGE_SHARP) { /* edge sharp */
1066                                 for(eed= em->edges.first; eed; eed= eed->next) {
1067                                         if (
1068                                                 !(eed->f & SELECT) &&
1069                                                 !eed->h &&
1070                                                 (eed->sharp == base_eed->sharp)
1071                                         ) {
1072                                                 EM_select_edge(eed, 1);
1073                                                 selcount++;
1074                                                 deselcount--;
1075                                                 if (!deselcount) /*have we selected all posible faces?, if so return*/
1076                                                         return selcount;
1077                                         }
1078                                 }
1079                         }
1080                 }
1081         }       
1082         return selcount;
1083 }
1084 /* wrap the above function but do selection flushing edge to face */
1085 static int similar_edge_select_exec(bContext *C, wmOperator *op)
1086 {
1087         Object *obedit= CTX_data_edit_object(C);
1088         Mesh *me= obedit->data;
1089         EditMesh *em= BKE_mesh_get_editmesh(me); 
1090
1091         int selcount = similar_edge_select__internal(em, RNA_enum_get(op->ptr, "type"), RNA_float_get(op->ptr, "threshold"));
1092         
1093         if (selcount) {
1094                 /* here was an edge-mode only select flush case, has to be generalized */
1095                 EM_selectmode_flush(em);
1096                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
1097                 BKE_mesh_end_editmesh(me, em);
1098                 return OPERATOR_FINISHED;
1099         }
1100         
1101         BKE_mesh_end_editmesh(me, em);
1102         return OPERATOR_CANCELLED;
1103 }
1104
1105 /* ********************************* */
1106
1107 static int similar_vert_select_exec(bContext *C, wmOperator *op)
1108 {
1109         Object *obedit= CTX_data_edit_object(C);
1110         Mesh *me= obedit->data;
1111         EditMesh *em= BKE_mesh_get_editmesh(me); 
1112         EditVert *eve, *base_eve=NULL;
1113         unsigned int selcount=0; /* count how many new edges we select*/
1114         
1115         /*count how many visible selected edges there are,
1116         so we can return when there are none left */
1117         unsigned int deselcount=0;
1118         int mode= RNA_enum_get(op->ptr, "type");
1119         float thresh = RNA_float_get(op->ptr, "threshold");
1120         
1121         short ok=0;
1122         
1123         for(eve= em->verts.first; eve; eve= eve->next) {
1124                 if (!eve->h) {
1125                         if (eve->f & SELECT) {
1126                                 eve->f1=1;
1127                                 ok=1;
1128                         } else {
1129                                 eve->f1=0;
1130                                 deselcount++;
1131                         }
1132                         /* set all eve->tmp.l to 0 we use them later.*/
1133                         eve->tmp.l=0;
1134                 }
1135                 
1136         }
1137         
1138         if (!ok || !deselcount) { /* no data selected OR no more data to select*/
1139                 BKE_mesh_end_editmesh(me, em);
1140                 return 0;
1141         }
1142         
1143         if(mode == SIMVERT_FACE) {
1144                 /* store face users */
1145                 EditFace *efa;
1146                 
1147                 /* count how many faces each edge uses use tmp->l */
1148                 for(efa= em->faces.first; efa; efa= efa->next) {
1149                         efa->v1->tmp.l++;
1150                         efa->v2->tmp.l++;
1151                         efa->v3->tmp.l++;
1152                         if (efa->v4) efa->v4->tmp.l++;
1153                 }
1154         }
1155         
1156         
1157         for(base_eve= em->verts.first; base_eve; base_eve= base_eve->next) {
1158                 if (base_eve->f1) {
1159                                 
1160                         if(mode == SIMVERT_NORMAL) {
1161                                 float angle;
1162                                 for(eve= em->verts.first; eve; eve= eve->next) {
1163                                         if (!(eve->f & SELECT) && !eve->h) {
1164                                                 angle= RAD2DEGF(angle_v3v3(base_eve->no, eve->no));
1165                                                 if (angle/180.0f<=thresh) {
1166                                                         eve->f |= SELECT;
1167                                                         selcount++;
1168                                                         deselcount--;
1169                                                         if (!deselcount) {/*have we selected all posible faces?, if so return*/
1170                                                                 BKE_mesh_end_editmesh(me, em);
1171                                                                 return selcount;
1172                                                         }
1173                                                 }
1174                                         }
1175                                 }
1176                         }
1177                         else if(mode == SIMVERT_FACE) {
1178                                 for(eve= em->verts.first; eve; eve= eve->next) {
1179                                         if (
1180                                                 !(eve->f & SELECT) &&
1181                                                 !eve->h &&
1182                                                 base_eve->tmp.l==eve->tmp.l
1183                                         ) {
1184                                                 eve->f |= SELECT;
1185                                                 selcount++;
1186                                                 deselcount--;
1187                                                 if (!deselcount) {/*have we selected all posible faces?, if so return*/
1188                                                         BKE_mesh_end_editmesh(me, em);
1189                                                         return selcount;
1190                                                 }
1191                                         }
1192                                 }
1193                         } 
1194                         else if(mode == SIMVERT_VGROUP) {
1195                                 MDeformVert *dvert, *base_dvert;
1196                                 short i, j; /* weight index */
1197
1198                                 base_dvert= CustomData_em_get(&em->vdata, base_eve->data,
1199                                         CD_MDEFORMVERT);
1200
1201                                 if (!base_dvert || base_dvert->totweight == 0) {
1202                                         BKE_mesh_end_editmesh(me, em);
1203                                         return selcount;
1204                                 }
1205                                 
1206                                 for(eve= em->verts.first; eve; eve= eve->next) {
1207                                         dvert= CustomData_em_get(&em->vdata, eve->data,
1208                                                 CD_MDEFORMVERT);
1209
1210                                         if (dvert && !(eve->f & SELECT) && !eve->h && dvert->totweight) {
1211                                                 /* do the extra check for selection in the following if, so were not
1212                                                 checking verts that may be already selected */
1213                                                 for (i=0; base_dvert->totweight >i && !(eve->f & SELECT); i++) { 
1214                                                         for (j=0; dvert->totweight >j; j++) {
1215                                                                 if (base_dvert->dw[i].def_nr==dvert->dw[j].def_nr) {
1216                                                                         eve->f |= SELECT;
1217                                                                         selcount++;
1218                                                                         deselcount--;
1219                                                                         if (!deselcount) { /*have we selected all posible faces?, if so return*/
1220                                                                                 BKE_mesh_end_editmesh(me, em);
1221                                                                                 return selcount;
1222                                                                         }
1223                                                                         break;
1224                                                                 }
1225                                                         }
1226                                                 }
1227                                         }
1228                                 }
1229                         }
1230                 }
1231         } /* end basevert loop */
1232
1233         if(selcount) {
1234                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
1235                 BKE_mesh_end_editmesh(me, em);
1236                 return OPERATOR_FINISHED;
1237         }
1238
1239         BKE_mesh_end_editmesh(me, em);
1240         return OPERATOR_CANCELLED;
1241 }
1242
1243 static int select_similar_exec(bContext *C, wmOperator *op)
1244 {
1245         int type= RNA_enum_get(op->ptr, "type");
1246
1247         if(type < 100)
1248                 return similar_vert_select_exec(C, op);
1249         else if(type < 200)
1250                 return similar_edge_select_exec(C, op);
1251         else
1252                 return similar_face_select_exec(C, op);
1253 }
1254
1255 static EnumPropertyItem *select_similar_type_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
1256 {
1257         Object *obedit= CTX_data_edit_object(C);
1258         EnumPropertyItem *item= NULL;
1259         int a, totitem= 0;
1260
1261         if (C == NULL) {
1262                 return prop_similar_types;
1263         }
1264                 
1265         if(obedit && obedit->type == OB_MESH) {
1266                 EditMesh *em= BKE_mesh_get_editmesh(obedit->data); 
1267
1268                 if(em->selectmode & SCE_SELECT_VERTEX) {
1269                         for(a=SIMVERT_NORMAL; a<=SIMVERT_TOT; a++)
1270                                 RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
1271                 }
1272                 else if(em->selectmode & SCE_SELECT_EDGE) {
1273                         for(a=SIMEDGE_LENGTH; a<=SIMEDGE_TOT; a++)
1274                                 RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
1275                 }
1276                 else if(em->selectmode & SCE_SELECT_FACE) {
1277                         for(a=SIMFACE_MATERIAL; a<=SIMFACE_TOT; a++)
1278                                 RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
1279                 }
1280         }
1281
1282         RNA_enum_item_end(&item, &totitem);
1283         *free= 1;
1284
1285         return item;
1286 }
1287
1288 void MESH_OT_select_similar(wmOperatorType *ot)
1289 {
1290         PropertyRNA *prop;
1291
1292         /* identifiers */
1293         ot->name= "Select Similar";
1294         ot->description= "Select similar vertices, edges or faces by property types";
1295         ot->idname= "MESH_OT_select_similar";
1296         
1297         /* api callbacks */
1298         ot->invoke= WM_menu_invoke;
1299         ot->exec= select_similar_exec;
1300         ot->poll= ED_operator_editmesh;
1301         
1302         /* flags */
1303         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1304         
1305         /* properties */
1306         prop= RNA_def_enum(ot->srna, "type", prop_similar_types, SIMVERT_NORMAL, "Type", "");
1307         RNA_def_enum_funcs(prop, select_similar_type_itemf);
1308         ot->prop= prop;
1309         RNA_def_float(ot->srna, "threshold", 0.01f, 0.0f, FLT_MAX, "Threshold", "", 0.0f, 100.f);
1310 }
1311
1312 /* ******************************************* */
1313
1314
1315 int mesh_layers_menu_charlen(CustomData *data, int type)
1316 {
1317          int i, len = 0;
1318         /* see if there is a duplicate */
1319         for(i=0; i<data->totlayer; i++) {
1320                 if((&data->layers[i])->type == type) {
1321                         /* we could count the chars here but we'll just assumeme each
1322                          * is 32 chars with some room for the menu text - 40 should be fine */
1323                         len+=40; 
1324                 }
1325         }
1326         return len;
1327 }
1328
1329 /* this function adds menu text into an existing string.
1330  * this string's size should be allocated with mesh_layers_menu_charlen */
1331 void mesh_layers_menu_concat(CustomData *data, int type, char *str) 
1332 {
1333         int i, count = 0;
1334         char *str_pt = str;
1335         CustomDataLayer *layer;
1336         
1337         /* see if there is a duplicate */
1338         for(i=0; i<data->totlayer; i++) {
1339                 layer = &data->layers[i];
1340                 if(layer->type == type) {
1341                         str_pt += sprintf(str_pt, "%s%%x%d|", layer->name, count);
1342                         count++;
1343                 }
1344         }
1345 }
1346
1347 int mesh_layers_menu(CustomData *data, int type) {
1348         int ret;
1349         char *str_pt, *str;
1350         
1351         str_pt = str = MEM_mallocN(mesh_layers_menu_charlen(data, type) + 18, "layer menu");
1352         str[0] = '\0';
1353         
1354         str_pt += sprintf(str_pt, "Layers%%t|");
1355         
1356         mesh_layers_menu_concat(data, type, str_pt);
1357         
1358         ret = pupmenu(str);
1359         MEM_freeN(str);
1360         return ret;
1361 }
1362
1363 static void EM_mesh_copy_edge(EditMesh *em, short type) 
1364 {
1365         EditSelection *ese;
1366         short change=0;
1367         
1368         EditEdge *eed, *eed_act;
1369         float vec[3], vec_mid[3], eed_len, eed_len_act;
1370         
1371         if (!em) return;
1372         
1373         ese = em->selected.last;
1374         if (!ese) return;
1375         
1376         eed_act = (EditEdge*)ese->data;
1377         
1378         switch (type) {
1379         case 1: /* copy crease */
1380                 for(eed=em->edges.first; eed; eed=eed->next) {
1381                         if (eed->f & SELECT && eed != eed_act && eed->crease != eed_act->crease) {
1382                                 eed->crease = eed_act->crease;
1383                                 change = 1;
1384                         }
1385                 }
1386                 break;
1387         case 2: /* copy bevel weight */
1388                 for(eed=em->edges.first; eed; eed=eed->next) {
1389                         if (eed->f & SELECT && eed != eed_act && eed->bweight != eed_act->bweight) {
1390                                 eed->bweight = eed_act->bweight;
1391                                 change = 1;
1392                         }
1393                 }
1394                 break;
1395
1396         case 3: /* copy length */
1397                 eed_len_act = len_v3v3(eed_act->v1->co, eed_act->v2->co);
1398                 for(eed=em->edges.first; eed; eed=eed->next) {
1399                         if (eed->f & SELECT && eed != eed_act) {
1400
1401                                 eed_len = len_v3v3(eed->v1->co, eed->v2->co);
1402
1403                                 if (eed_len == eed_len_act) continue;
1404                                 /* if this edge is zero length we cont do anything with it*/
1405                                 if (eed_len == 0.0f) continue;
1406                                 if (eed_len_act == 0.0f) {
1407                                         add_v3_v3v3(vec_mid, eed->v1->co, eed->v2->co);
1408                                         mul_v3_fl(vec_mid, 0.5);
1409                                         VECCOPY(eed->v1->co, vec_mid);
1410                                         VECCOPY(eed->v2->co, vec_mid);
1411                                 } else {
1412                                         /* copy the edge length */
1413                                         add_v3_v3v3(vec_mid, eed->v1->co, eed->v2->co);
1414                                         mul_v3_fl(vec_mid, 0.5);
1415
1416                                         /* SCALE 1 */
1417                                         sub_v3_v3v3(vec, eed->v1->co, vec_mid);
1418                                         mul_v3_fl(vec, eed_len_act/eed_len);
1419                                         add_v3_v3v3(eed->v1->co, vec, vec_mid);
1420
1421                                         /* SCALE 2 */
1422                                         sub_v3_v3v3(vec, eed->v2->co, vec_mid);
1423                                         mul_v3_fl(vec, eed_len_act/eed_len);
1424                                         add_v3_v3v3(eed->v2->co, vec, vec_mid);
1425                                 }
1426                                 change = 1;
1427                         }
1428                 }
1429
1430                 if (change)
1431                         recalc_editnormals(em);
1432
1433                 break;
1434         }
1435         
1436         if (change) {
1437 //              DAG_id_tag_update(obedit->data, 0);
1438                 
1439         }
1440 }
1441
1442 static void EM_mesh_copy_face(EditMesh *em, wmOperator *op, short type)
1443 {
1444         short change=0;
1445         
1446         EditFace *efa, *efa_act;
1447         MTFace *tf, *tf_act = NULL;
1448         MCol *mcol, *mcol_act = NULL;
1449         if (!em) return;
1450         efa_act = EM_get_actFace(em, 0);
1451         
1452         if (!efa_act) return;
1453         
1454         tf_act =        CustomData_em_get(&em->fdata, efa_act->data, CD_MTFACE);
1455         mcol_act =      CustomData_em_get(&em->fdata, efa_act->data, CD_MCOL);
1456         
1457         switch (type) {
1458         case 1: /* copy material */
1459                 for(efa=em->faces.first; efa; efa=efa->next) {
1460                         if (efa->f & SELECT && efa->mat_nr != efa_act->mat_nr) {
1461                                 efa->mat_nr = efa_act->mat_nr;
1462                                 change = 1;
1463                         }
1464                 }
1465                 break;
1466         case 2: /* copy image */
1467                 if (!tf_act) {
1468                         BKE_report(op->reports, RPT_WARNING, "Mesh has no uv/image layers.");
1469                         return;
1470                 }
1471                 for(efa=em->faces.first; efa; efa=efa->next) {
1472                         if (efa->f & SELECT && efa != efa_act) {
1473                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1474                                 if (tf_act->tpage) {
1475                                         tf->tpage = tf_act->tpage;
1476                                         tf->mode |= TF_TEX;
1477                                 } else {
1478                                         tf->tpage = NULL;
1479                                         tf->mode &= ~TF_TEX;
1480                                 }
1481                                 tf->tile= tf_act->tile;
1482                                 change = 1;
1483                         }
1484                 }
1485                 break;
1486
1487         case 3: /* copy UV's */
1488                 if (!tf_act) {
1489                         BKE_report(op->reports, RPT_WARNING, "Mesh has no uv/image layers.");
1490                         return;
1491                 }
1492                 for(efa=em->faces.first; efa; efa=efa->next) {
1493                         if (efa->f & SELECT && efa != efa_act) {
1494                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1495                                 memcpy(tf->uv, tf_act->uv, sizeof(tf->uv));
1496                                 change = 1;
1497                         }
1498                 }
1499                 break;
1500         case 4: /* mode's */
1501                 if (!tf_act) {
1502                         BKE_report(op->reports, RPT_WARNING, "Mesh has no uv/image layers.");
1503                         return;
1504                 }
1505                 for(efa=em->faces.first; efa; efa=efa->next) {
1506                         if (efa->f & SELECT && efa != efa_act) {
1507                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1508                                 tf->mode= tf_act->mode;
1509                                 change = 1;
1510                         }
1511                 }
1512                 break;
1513         case 5: /* copy transp's */
1514                 if (!tf_act) {
1515                         BKE_report(op->reports, RPT_WARNING, "Mesh has no uv/image layers.");
1516                         return;
1517                 }
1518                 for(efa=em->faces.first; efa; efa=efa->next) {
1519                         if (efa->f & SELECT && efa != efa_act) {
1520                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1521                                 tf->transp= tf_act->transp;
1522                                 change = 1;
1523                         }
1524                 }
1525                 break;
1526
1527         case 6: /* copy vcols's */
1528                 if (!mcol_act) {
1529                         BKE_report(op->reports, RPT_WARNING, "Mesh has no color layers.");
1530                         return;
1531                 } else {
1532                         /* guess the 4th color if needs be */
1533                         float val =- 1;
1534
1535                         if (!efa_act->v4) {
1536                                 /* guess the othe vale, we may need to use it
1537                                  * 
1538                                  * Modifying the 4th value of the mcol is ok here since its not seen
1539                                  * on a triangle
1540                                  * */
1541                                 val = ((float)(mcol_act->r +  (mcol_act+1)->r + (mcol_act+2)->r)) / 3; CLAMP(val, 0, 255);
1542                                 (mcol_act+3)->r = (char)val;
1543
1544                                 val = ((float)(mcol_act->g +  (mcol_act+1)->g + (mcol_act+2)->g)) / 3; CLAMP(val, 0, 255);
1545                                 (mcol_act+3)->g = (char)val;
1546
1547                                 val = ((float)(mcol_act->b +  (mcol_act+1)->b + (mcol_act+2)->b)) / 3; CLAMP(val, 0, 255);
1548                                 (mcol_act+3)->b = (char)val;
1549                         } 
1550
1551
1552                         for(efa=em->faces.first; efa; efa=efa->next) {
1553                                 if (efa->f & SELECT && efa != efa_act) {
1554                                         /* TODO - make copy from tri to quad guess the 4th vert */
1555                                         mcol = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
1556                                         memcpy(mcol, mcol_act, sizeof(MCol)*4); 
1557                                         change = 1;
1558                                 }
1559                         }
1560                 }
1561                 break;
1562         }
1563         
1564         if (change) {
1565 //              DAG_id_tag_update(obedit->data, 0);
1566                 
1567         }
1568 }
1569
1570
1571 void EM_mesh_copy_face_layer(EditMesh *em, wmOperator *op, short type) 
1572 {
1573         short change=0;
1574         
1575         EditFace *efa;
1576         MTFace *tf, *tf_from;
1577         MCol *mcol, *mcol_from;
1578         
1579         if (!em) return;
1580         
1581         switch(type) {
1582         case 7:
1583         case 8:
1584         case 9:
1585                 if (CustomData_number_of_layers(&em->fdata, CD_MTFACE)<2) {
1586                         BKE_report(op->reports, RPT_WARNING, "mesh does not have multiple uv/image layers");
1587                         return;
1588                 } else {
1589                         int layer_orig_idx, layer_idx;
1590
1591                         layer_idx = mesh_layers_menu(&em->fdata, CD_MTFACE);
1592                         if (layer_idx<0) return;
1593
1594                         /* warning, have not updated mesh pointers however this is not needed since we swicth back */
1595                         layer_orig_idx = CustomData_get_active_layer(&em->fdata, CD_MTFACE);
1596                         if (layer_idx==layer_orig_idx)
1597                                 return;
1598
1599                         /* get the tfaces */
1600                         CustomData_set_layer_active(&em->fdata, CD_MTFACE, (int)layer_idx);
1601                         /* store the tfaces in our temp */
1602                         for(efa=em->faces.first; efa; efa=efa->next) {
1603                                 if (efa->f & SELECT) {
1604                                         efa->tmp.p = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1605                                 }       
1606                         }
1607                         CustomData_set_layer_active(&em->fdata, CD_MTFACE, layer_orig_idx);
1608                 }
1609                 break;
1610
1611         case 10: /* select vcol layers - make sure this stays in sync with above code */
1612                 if (CustomData_number_of_layers(&em->fdata, CD_MCOL)<2) {
1613                         BKE_report(op->reports, RPT_WARNING, "mesh does not have multiple color layers");
1614                         return;
1615                 } else {
1616                         int layer_orig_idx, layer_idx;
1617
1618                         layer_idx = mesh_layers_menu(&em->fdata, CD_MCOL);
1619                         if (layer_idx<0) return;
1620
1621                         /* warning, have not updated mesh pointers however this is not needed since we swicth back */
1622                         layer_orig_idx = CustomData_get_active_layer(&em->fdata, CD_MCOL);
1623                         if (layer_idx==layer_orig_idx)
1624                                 return;
1625
1626                         /* get the tfaces */
1627                         CustomData_set_layer_active(&em->fdata, CD_MCOL, (int)layer_idx);
1628                         /* store the tfaces in our temp */
1629                         for(efa=em->faces.first; efa; efa=efa->next) {
1630                                 if (efa->f & SELECT) {
1631                                         efa->tmp.p = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
1632                                 }       
1633                         }
1634                         CustomData_set_layer_active(&em->fdata, CD_MCOL, layer_orig_idx);
1635
1636                 }
1637                 break;
1638         }
1639
1640         /* layer copy only - sanity checks done above */
1641         switch (type) {
1642         case 7: /* copy UV's only */
1643                 for(efa=em->faces.first; efa; efa=efa->next) {
1644                         if (efa->f & SELECT) {
1645                                 tf_from = (MTFace *)efa->tmp.p; /* not active but easier to use this way */
1646                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1647                                 memcpy(tf->uv, tf_from->uv, sizeof(tf->uv));
1648                                 change = 1;
1649                         }
1650                 }
1651                 break;
1652         case 8: /* copy image settings only */
1653                 for(efa=em->faces.first; efa; efa=efa->next) {
1654                         if (efa->f & SELECT) {
1655                                 tf_from = (MTFace *)efa->tmp.p; /* not active but easier to use this way */
1656                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1657                                 if (tf_from->tpage) {
1658                                         tf->tpage = tf_from->tpage;
1659                                         tf->mode |= TF_TEX;
1660                                 } else {
1661                                         tf->tpage = NULL;
1662                                         tf->mode &= ~TF_TEX;
1663                                 }
1664                                 tf->tile= tf_from->tile;
1665                                 change = 1;
1666                         }
1667                 }
1668                 break;
1669         case 9: /* copy all tface info */
1670                 for(efa=em->faces.first; efa; efa=efa->next) {
1671                         if (efa->f & SELECT) {
1672                                 tf_from = (MTFace *)efa->tmp.p; /* not active but easier to use this way */
1673                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1674                                 memcpy(tf->uv, ((MTFace *)efa->tmp.p)->uv, sizeof(tf->uv));
1675                                 tf->tpage = tf_from->tpage;
1676                                 tf->mode = tf_from->mode;
1677                                 tf->transp = tf_from->transp;
1678                                 change = 1;
1679                         }
1680                 }
1681                 break;
1682         case 10:
1683                 for(efa=em->faces.first; efa; efa=efa->next) {
1684                         if (efa->f & SELECT) {
1685                                 mcol_from = (MCol *)efa->tmp.p; 
1686                                 mcol = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
1687                                 memcpy(mcol, mcol_from, sizeof(MCol)*4);        
1688                                 change = 1;
1689                         }
1690                 }
1691                 break;
1692         }
1693
1694         if (change) {
1695 //              DAG_id_tag_update(obedit->data, 0);
1696                 
1697         }
1698 }
1699
1700
1701 /* ctrl+c in mesh editmode */
1702 static void UNUSED_FUNCTION(mesh_copy_menu)(EditMesh *em, wmOperator *op)
1703 {
1704         EditSelection *ese;
1705         int ret;
1706         if (!em) return;
1707         
1708         ese = em->selected.last;
1709         
1710         /* Faces can have a NULL ese, so dont return on a NULL ese here */
1711         
1712         if(ese && ese->type == EDITVERT) {
1713                 /* EditVert *ev, *ev_act = (EditVert*)ese->data;
1714                 ret= pupmenu(""); */
1715         } else if(ese && ese->type == EDITEDGE) {
1716                 ret= pupmenu("Copy Active Edge to Selected%t|Crease%x1|Bevel Weight%x2|Length%x3");
1717                 if (ret<1) return;
1718                 
1719                 EM_mesh_copy_edge(em, ret);
1720                 
1721         } else if(ese==NULL || ese->type == EDITFACE) {
1722                 ret= pupmenu(
1723                         "Copy Face Selected%t|"
1724                         "Active Material%x1|Active Image%x2|Active UV Coords%x3|"
1725                         "Active Mode%x4|Active Transp%x5|Active Vertex Colors%x6|%l|"
1726
1727                         "TexFace UVs from layer%x7|"
1728                         "TexFace Images from layer%x8|"
1729                         "TexFace All from layer%x9|"
1730                         "Vertex Colors from layer%x10");
1731                 if (ret<1) return;
1732                 
1733                 if (ret<=6) {
1734                         EM_mesh_copy_face(em, op, ret);
1735                 } else {
1736                         EM_mesh_copy_face_layer(em, op, ret);
1737                 }
1738         }
1739 }
1740
1741 /* ****************  LOOP SELECTS *************** */
1742
1743 /* selects quads in loop direction of indicated edge */
1744 /* only flush over edges with valence <= 2 */
1745 void faceloop_select(EditMesh *em, EditEdge *startedge, int select)
1746 {
1747         EditEdge *eed;
1748         EditFace *efa;
1749         int looking= 1;
1750         
1751         /* in eed->f1 we put the valence (amount of faces in edge) */
1752         /* in eed->f2 we put tagged flag as correct loop */
1753         /* in efa->f1 we put tagged flag as correct to select */
1754
1755         for(eed= em->edges.first; eed; eed= eed->next) {
1756                 eed->f1= 0;
1757                 eed->f2= 0;
1758         }
1759         for(efa= em->faces.first; efa; efa= efa->next) {
1760                 efa->f1= 0;
1761                 if(efa->h==0) {
1762                         efa->e1->f1++;
1763                         efa->e2->f1++;
1764                         efa->e3->f1++;
1765                         if(efa->e4) efa->e4->f1++;
1766                 }
1767         }
1768         
1769         /* tag startedge OK*/
1770         startedge->f2= 1;
1771         
1772         while(looking) {
1773                 looking= 0;
1774                 
1775                 for(efa= em->faces.first; efa; efa= efa->next) {
1776                         if(efa->h==0 && efa->e4 && efa->f1==0) {        /* not done quad */
1777                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { /* valence ok */
1778
1779                                         /* if edge tagged, select opposing edge and mark face ok */
1780                                         if(efa->e1->f2) {
1781                                                 efa->e3->f2= 1;
1782                                                 efa->f1= 1;
1783                                                 looking= 1;
1784                                         }
1785                                         else if(efa->e2->f2) {
1786                                                 efa->e4->f2= 1;
1787                                                 efa->f1= 1;
1788                                                 looking= 1;
1789                                         }
1790                                         if(efa->e3->f2) {
1791                                                 efa->e1->f2= 1;
1792                                                 efa->f1= 1;
1793                                                 looking= 1;
1794                                         }
1795                                         if(efa->e4->f2) {
1796                                                 efa->e2->f2= 1;
1797                                                 efa->f1= 1;
1798                                                 looking= 1;
1799                                         }
1800                                 }
1801                         }
1802                 }
1803         }
1804         
1805         /* (de)select the faces */
1806         if(select!=2) {
1807                 for(efa= em->faces.first; efa; efa= efa->next) {
1808                         if(efa->f1) EM_select_face(efa, select);
1809                 }
1810         }
1811 }
1812
1813
1814 /* helper for edgeloop_select, checks for eed->f2 tag in faces */
1815 static int edge_not_in_tagged_face(EditMesh *em, EditEdge *eed)
1816 {
1817         EditFace *efa;
1818         
1819         for(efa= em->faces.first; efa; efa= efa->next) {
1820                 if(efa->h==0) {
1821                         if(efa->e1==eed || efa->e2==eed || efa->e3==eed || efa->e4==eed) {      /* edge is in face */
1822                                 if(efa->e1->f2 || efa->e2->f2 || efa->e3->f2 || (efa->e4 && efa->e4->f2)) {     /* face is tagged */
1823                                         return 0;
1824                                 }
1825                         }
1826                 }
1827         }
1828         return 1;
1829 }
1830
1831 static void ensure_ed_vert_sel(EditMesh *em)
1832 {
1833         EditEdge *eed;
1834
1835         /* EM_selectmode_flush() doesnt take into account that deselected edges
1836          * may be still connected to selected edges [#26885] */
1837         for(eed= em->edges.first; eed; eed= eed->next) {
1838                 if(eed->f & SELECT) {
1839                         eed->v1->f |= SELECT;
1840                         eed->v2->f |= SELECT;
1841                 }
1842         }
1843 }
1844
1845 /* selects or deselects edges that:
1846 - if edges has 2 faces:
1847         - has vertices with valence of 4
1848         - not shares face with previous edge
1849 - if edge has 1 face:
1850         - has vertices with valence 4
1851         - not shares face with previous edge
1852         - but also only 1 face
1853 - if edge no face:
1854         - has vertices with valence 2
1855 */
1856 static void edgeloop_select(EditMesh *em, EditEdge *starteed, int select)
1857 {
1858         EditVert *eve;
1859         EditEdge *eed;
1860         EditFace *efa;
1861         int looking= 1;
1862         
1863         /* in f1 we put the valence (amount of edges in a vertex, or faces in edge) */
1864         /* in eed->f2 and efa->f1 we put tagged flag as correct loop */
1865         for(eve= em->verts.first; eve; eve= eve->next) {
1866                 eve->f1= 0;
1867                 eve->f2= 0;
1868         }
1869         for(eed= em->edges.first; eed; eed= eed->next) {
1870                 eed->f1= 0;
1871                 eed->f2= 0;
1872                 if((eed->h & 1)==0) {   /* fgon edges add to valence too */
1873                         eed->v1->f1++; eed->v2->f1++;
1874                 }
1875         }
1876         for(efa= em->faces.first; efa; efa= efa->next) {
1877                 efa->f1= 0;
1878                 if(efa->h==0) {
1879                         efa->e1->f1++;
1880                         efa->e2->f1++;
1881                         efa->e3->f1++;
1882                         if(efa->e4) efa->e4->f1++;
1883                 }
1884         }
1885         
1886         /* looped edges & vertices get tagged f2 */
1887         starteed->f2= 1;
1888         if(starteed->v1->f1<5) starteed->v1->f2= 1;
1889         if(starteed->v2->f1<5) starteed->v2->f2= 1;
1890         /* sorry, first edge isnt even ok */
1891         if(starteed->v1->f2==0 && starteed->v2->f2==0) looking= 0;
1892         
1893         while(looking) {
1894                 looking= 0;
1895                 
1896                 /* find correct valence edges which are not tagged yet, but connect to tagged one */
1897                 for(eed= em->edges.first; eed; eed= eed->next) {
1898                         if(eed->h==0 && eed->f2==0) { /* edge not hidden, not tagged */
1899                                 if( (eed->v1->f1<5 && eed->v1->f2) || (eed->v2->f1<5 && eed->v2->f2)) { /* valence of vertex OK, and is tagged */
1900                                         /* new edge is not allowed to be in face with tagged edge */
1901                                         if(edge_not_in_tagged_face(em, eed)) {
1902                                                 if(eed->f1==starteed->f1) {     /* same amount of faces */
1903                                                         looking= 1;
1904                                                         eed->f2= 1;
1905                                                         if(eed->v2->f1<5) eed->v2->f2= 1;
1906                                                         if(eed->v1->f1<5) eed->v1->f2= 1;
1907                                                 }
1908                                         }
1909                                 }
1910                         }
1911                 }
1912         }
1913         /* and we do the select */
1914         for(eed= em->edges.first; eed; eed= eed->next) {
1915                 if(eed->f2) EM_select_edge(eed, select);
1916         }
1917
1918         if(select == FALSE && !(em->selectmode & SCE_SELECT_VERTEX)) { /* only when not in vert sel [#26931] */
1919                 ensure_ed_vert_sel(em);
1920         }
1921 }
1922
1923 /* 
1924    Almostly exactly the same code as faceloop select
1925 */
1926 static void edgering_select(EditMesh *em, EditEdge *startedge, int select)
1927 {
1928         EditEdge *eed;
1929         EditFace *efa;
1930         int looking= 1;
1931         
1932         /* in eed->f1 we put the valence (amount of faces in edge) */
1933         /* in eed->f2 we put tagged flag as correct loop */
1934         /* in efa->f1 we put tagged flag as correct to select */
1935
1936         for(eed= em->edges.first; eed; eed= eed->next) {
1937                 eed->f1= 0;
1938                 eed->f2= 0;
1939         }
1940         for(efa= em->faces.first; efa; efa= efa->next) {
1941                 efa->f1= 0;
1942                 if(efa->h==0) {
1943                         efa->e1->f1++;
1944                         efa->e2->f1++;
1945                         efa->e3->f1++;
1946                         if(efa->e4) efa->e4->f1++;
1947                 }
1948         }
1949         
1950         /* tag startedge OK */
1951         startedge->f2= 1;
1952         
1953         while(looking) {
1954                 looking= 0;
1955                 
1956                 for(efa= em->faces.first; efa; efa= efa->next) {
1957                         if(efa->e4 && efa->f1==0 && !efa->h) {  /* not done quad */
1958                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { /* valence ok */
1959
1960                                         /* if edge tagged, select opposing edge and mark face ok */
1961                                         if(efa->e1->f2) {
1962                                                 efa->e3->f2= 1;
1963                                                 efa->f1= 1;
1964                                                 looking= 1;
1965                                         }
1966                                         else if(efa->e2->f2) {
1967                                                 efa->e4->f2= 1;
1968                                                 efa->f1= 1;
1969                                                 looking= 1;
1970                                         }
1971                                         if(efa->e3->f2) {
1972                                                 efa->e1->f2= 1;
1973                                                 efa->f1= 1;
1974                                                 looking= 1;
1975                                         }
1976                                         if(efa->e4->f2) {
1977                                                 efa->e2->f2= 1;
1978                                                 efa->f1= 1;
1979                                                 looking= 1;
1980                                         }
1981                                 }
1982                         }
1983                 }
1984         }
1985         
1986         /* (de)select the edges */
1987         for(eed= em->edges.first; eed; eed= eed->next) {
1988                         if(eed->f2) EM_select_edge(eed, select);
1989         }
1990
1991         if(select == FALSE && !(em->selectmode & SCE_SELECT_VERTEX)) { /* only when not in vert sel [#26931] */
1992                 ensure_ed_vert_sel(em);
1993         }
1994 }
1995
1996 static int loop_multiselect(bContext *C, wmOperator *op)
1997 {
1998         Object *obedit= CTX_data_edit_object(C);
1999         EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
2000         EditEdge *eed;
2001         EditEdge **edarray;
2002         int edindex, edfirstcount;
2003         int looptype= RNA_boolean_get(op->ptr, "ring");
2004         
2005         /* sets em->totedgesel */
2006         EM_nedges_selected(em);
2007         
2008         edarray = MEM_mallocN(sizeof(EditEdge*)*em->totedgesel,"edge array");
2009         edindex = 0;
2010         edfirstcount = em->totedgesel;
2011         
2012         for(eed=em->edges.first; eed; eed=eed->next){
2013                 if(eed->f&SELECT){
2014                         edarray[edindex] = eed;
2015                         edindex += 1;
2016                 }
2017         }
2018         
2019         if(looptype){
2020                 for(edindex = 0; edindex < edfirstcount; edindex +=1){
2021                         eed = edarray[edindex];
2022                         edgering_select(em, eed,SELECT);
2023                 }
2024                 EM_selectmode_flush(em);
2025         }
2026         else{
2027                 for(edindex = 0; edindex < edfirstcount; edindex +=1){
2028                         eed = edarray[edindex];
2029                         edgeloop_select(em, eed,SELECT);
2030                 }
2031                 EM_selectmode_flush(em);
2032         }
2033         MEM_freeN(edarray);
2034 //      if (EM_texFaceCheck())
2035         
2036         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
2037
2038         BKE_mesh_end_editmesh(obedit->data, em);
2039         return OPERATOR_FINISHED;       
2040 }
2041
2042 void MESH_OT_loop_multi_select(wmOperatorType *ot)
2043 {
2044         /* identifiers */
2045         ot->name= "Multi Select Loops";
2046         ot->description= "Select a loop of connected edges by connection type";
2047         ot->idname= "MESH_OT_loop_multi_select";
2048         
2049         /* api callbacks */
2050         ot->exec= loop_multiselect;
2051         ot->poll= ED_operator_editmesh;
2052         
2053         /* flags */
2054         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2055         
2056         /* properties */
2057         RNA_def_boolean(ot->srna, "ring", 0, "Ring", "");
2058 }
2059
2060                 
2061 /* ***************** MAIN MOUSE SELECTION ************** */
2062
2063
2064 /* ***************** loop select (non modal) ************** */
2065
2066 static void mouse_mesh_loop(bContext *C, const int mval[2], short extend, short ring)
2067 {
2068         ViewContext vc;
2069         EditMesh *em;
2070         EditEdge *eed;
2071         int select= 1;
2072         int dist= 50;
2073         
2074         em_setup_viewcontext(C, &vc);
2075         vc.mval[0]= mval[0];
2076         vc.mval[1]= mval[1];
2077         em= vc.em;
2078
2079         /* no afterqueue (yet), so we check it now, otherwise the em_xxxofs indices are bad */
2080         view3d_validate_backbuf(&vc);
2081         
2082         eed= findnearestedge(&vc, &dist);
2083         if(eed) {
2084                 if(extend==0) EM_clear_flag_all(em, SELECT);
2085         
2086                 if((eed->f & SELECT)==0) select=1;
2087                 else if(extend) select=0;
2088
2089                 if(em->selectmode & SCE_SELECT_FACE) {
2090                         faceloop_select(em, eed, select);
2091                 }
2092                 else if(em->selectmode & SCE_SELECT_EDGE) {
2093                         if(ring)
2094                                 edgering_select(em, eed, select);
2095                         else
2096                                 edgeloop_select(em, eed, select);
2097                 }
2098                 else if(em->selectmode & SCE_SELECT_VERTEX) {
2099                         if(ring)
2100                                 edgering_select(em, eed, select);
2101                         else 
2102                                 edgeloop_select(em, eed, select);
2103                 }
2104
2105                 EM_selectmode_flush(em);
2106 //                      if (EM_texFaceCheck())
2107                 
2108                 /* sets as active, useful for other tools */
2109                 if(select) {
2110                         if(em->selectmode & SCE_SELECT_VERTEX)
2111                                 EM_store_selection(em, eed->v1, EDITVERT);
2112                         if(em->selectmode & SCE_SELECT_EDGE)
2113                                 EM_store_selection(em, eed, EDITEDGE);
2114                 }
2115
2116                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, vc.obedit->data);
2117         }
2118 }
2119
2120 static int mesh_select_loop_invoke(bContext *C, wmOperator *op, wmEvent *event)
2121 {
2122         
2123         view3d_operator_needs_opengl(C);
2124         
2125         mouse_mesh_loop(C, event->mval, RNA_boolean_get(op->ptr, "extend"),
2126                                         RNA_boolean_get(op->ptr, "ring"));
2127         
2128         /* cannot do tweaks for as long this keymap is after transform map */
2129         return OPERATOR_FINISHED;
2130 }
2131
2132 void MESH_OT_loop_select(wmOperatorType *ot)
2133 {
2134         /* identifiers */
2135         ot->name= "Loop Select";
2136         ot->description= "Select a loop of connected edges";
2137         ot->idname= "MESH_OT_loop_select";
2138         
2139         /* api callbacks */
2140         ot->invoke= mesh_select_loop_invoke;
2141         ot->poll= ED_operator_editmesh_region_view3d;
2142         
2143         /* flags */
2144         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2145         
2146         /* properties */
2147         RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "");
2148         RNA_def_boolean(ot->srna, "ring", 0, "Select Ring", "");
2149 }
2150
2151 /* ******************* mesh shortest path select, uses prev-selected edge ****************** */
2152
2153 /* since you want to create paths with multiple selects, it doesn't have extend option */
2154 static void mouse_mesh_shortest_path(bContext *C, const int mval[2])
2155 {
2156         ViewContext vc;
2157         EditMesh *em;
2158         EditEdge *eed, *eed_act= NULL;
2159         int dist= 50;
2160         
2161         em_setup_viewcontext(C, &vc);
2162         vc.mval[0]= mval[0];
2163         vc.mval[1]= mval[1];
2164         em= vc.em;
2165         
2166         /* no afterqueue (yet), so we check it now, otherwise the em_xxxofs indices are bad */
2167         view3d_validate_backbuf(&vc);
2168         
2169         eed= findnearestedge(&vc, &dist);
2170         if(eed) {
2171                 Mesh *me= vc.obedit->data;
2172                 int path = 0;
2173                 
2174                 if (em->selected.last) {
2175                         EditSelection *ese = em->selected.last;
2176                         
2177                         if(ese && ese->type == EDITEDGE) {
2178                                 eed_act = (EditEdge*)ese->data;
2179                                 if (eed_act != eed) {
2180                                         if (edgetag_shortest_path(vc.scene, em, eed_act, eed)) { /* <- this is where the magic happens */
2181                                                 EM_remove_selection(em, eed_act, EDITEDGE);
2182                                                 path = 1;
2183                                         }
2184                                 }
2185                         }
2186                 }
2187                 if (path==0) {
2188                         int act = (edgetag_context_check(vc.scene, eed)==0);
2189                         edgetag_context_set(vc.scene, eed, act); /* switch the edge option */
2190                 }
2191
2192                 /* even if this is selected it may not be in the selection list */
2193                 if(edgetag_context_check(vc.scene, eed)==0) {
2194                         EM_remove_selection(em, eed, EDITEDGE);
2195                 }
2196                 else {
2197                         /* other modes need to keep the last edge tagged */
2198                         if(eed_act) {
2199                                 if(vc.scene->toolsettings->edge_mode!=EDGE_MODE_SELECT) {
2200                                         /* for non-select modes, always de-select the previous active edge */
2201                                         EM_select_edge(eed_act, 0);
2202                                 }
2203                         }
2204
2205                         /* set the new edge active */
2206                         EM_select_edge(eed, 1);
2207                         EM_store_selection(em, eed, EDITEDGE);
2208                 }
2209
2210                 EM_selectmode_flush(em);
2211         
2212                 /* force drawmode for mesh */
2213                 switch (vc.scene->toolsettings->edge_mode) {
2214                         
2215                         case EDGE_MODE_TAG_SEAM:
2216                                 me->drawflag |= ME_DRAWSEAMS;
2217                                 break;
2218                         case EDGE_MODE_TAG_SHARP:
2219                                 me->drawflag |= ME_DRAWSHARP;
2220                                 break;
2221                         case EDGE_MODE_TAG_CREASE:      
2222                                 me->drawflag |= ME_DRAWCREASES;
2223                                 break;
2224                         case EDGE_MODE_TAG_BEVEL:
2225                                 me->drawflag |= ME_DRAWBWEIGHTS;
2226                                 break;
2227                 }
2228
2229                 /* live unwrap while tagging */
2230                 if(     (vc.scene->toolsettings->edge_mode_live_unwrap) &&
2231                         (vc.scene->toolsettings->edge_mode == EDGE_MODE_TAG_SEAM) &&
2232                         (CustomData_has_layer(&em->fdata, CD_MTFACE))
2233                 ) {
2234                         ED_unwrap_lscm(vc.scene, vc.obedit, FALSE); /* unwrap all not just sel */
2235                 }
2236
2237                 DAG_id_tag_update(vc.obedit->data, 0);
2238                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, vc.obedit->data);
2239         }
2240 }
2241
2242
2243 static int mesh_shortest_path_select_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
2244 {
2245         
2246         view3d_operator_needs_opengl(C);
2247
2248         mouse_mesh_shortest_path(C, event->mval);
2249         
2250         return OPERATOR_FINISHED;
2251 }
2252
2253 static int mesh_shortest_path_select_poll(bContext *C)
2254 {
2255         if(ED_operator_editmesh_region_view3d(C)) {
2256                 Object *obedit= CTX_data_edit_object(C);
2257                 EditMesh *em= BKE_mesh_get_editmesh(obedit->data);
2258                 return (em->selectmode & SCE_SELECT_EDGE);
2259         }
2260         return 0;
2261 }
2262         
2263 void MESH_OT_select_shortest_path(wmOperatorType *ot)
2264 {
2265         /* identifiers */
2266         ot->name= "Shortest Path Select";
2267         ot->description= "Select shortest path between two selections";
2268         ot->idname= "MESH_OT_select_shortest_path";
2269         
2270         /* api callbacks */
2271         ot->invoke= mesh_shortest_path_select_invoke;
2272         ot->poll= mesh_shortest_path_select_poll;
2273         
2274         /* flags */
2275         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2276         
2277         /* properties */
2278         RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "");
2279 }
2280
2281
2282 /* ************************************************** */
2283
2284
2285 /* here actual select happens */
2286 /* gets called via generic mouse select operator */
2287 int mouse_mesh(bContext *C, const int mval[2], short extend)
2288 {
2289         ViewContext vc;
2290         EditVert *eve;
2291         EditEdge *eed;
2292         EditFace *efa;
2293         
2294         /* setup view context for argument to callbacks */
2295         em_setup_viewcontext(C, &vc);
2296         vc.mval[0]= mval[0];
2297         vc.mval[1]= mval[1];
2298         
2299         if(unified_findnearest(&vc, &eve, &eed, &efa)) {
2300                 
2301                 if(extend==0) EM_clear_flag_all(vc.em, SELECT);
2302                 
2303                 if(efa) {
2304                         /* set the last selected face */
2305                         EM_set_actFace(vc.em, efa);
2306                         
2307                         if( (efa->f & SELECT)==0 ) {
2308                                 EM_store_selection(vc.em, efa, EDITFACE);
2309                                 EM_select_face_fgon(vc.em, efa, 1);
2310                         }
2311                         else if(extend) {
2312                                 EM_remove_selection(vc.em, efa, EDITFACE);
2313                                 EM_select_face_fgon(vc.em, efa, 0);
2314                         }
2315                 }
2316                 else if(eed) {
2317                         if((eed->f & SELECT)==0) {
2318                                 EM_store_selection(vc.em, eed, EDITEDGE);
2319                                 EM_select_edge(eed, 1);
2320                         }
2321                         else if(extend) {
2322                                 EM_remove_selection(vc.em, eed, EDITEDGE);
2323                                 EM_select_edge(eed, 0);
2324                         }
2325                 }
2326                 else if(eve) {
2327                         if((eve->f & SELECT)==0) {
2328                                 eve->f |= SELECT;
2329                                 EM_store_selection(vc.em, eve, EDITVERT);
2330                         }
2331                         else if(extend){ 
2332                                 EM_remove_selection(vc.em, eve, EDITVERT);
2333                                 eve->f &= ~SELECT;
2334                         }
2335                 }
2336                 
2337                 EM_selectmode_flush(vc.em);
2338                   
2339 //              if (EM_texFaceCheck()) {
2340
2341                 if (efa && efa->mat_nr != vc.obedit->actcol-1) {
2342                         vc.obedit->actcol= efa->mat_nr+1;
2343                         vc.em->mat_nr= efa->mat_nr;
2344                         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING, NULL);
2345                 }
2346
2347                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, vc.obedit->data);
2348
2349                 return 1;
2350         }
2351         
2352         return 0;
2353 }
2354
2355 /* *********** select linked ************* */
2356
2357 /* for use with selectconnected_delimit_mesh only! */
2358 #define is_edge_delimit_ok(eed) ((eed->tmp.l == 1) && (eed->seam==0))
2359 #define is_face_tag(efa) is_edge_delimit_ok(efa->e1) || is_edge_delimit_ok(efa->e2) || is_edge_delimit_ok(efa->e3) || (efa->v4 && is_edge_delimit_ok(efa->e4))
2360
2361 #define face_tag(efa)\
2362 if(efa->v4)     efa->tmp.l=             efa->e1->tmp.l= efa->e2->tmp.l= efa->e3->tmp.l= efa->e4->tmp.l= 1;\
2363 else            efa->tmp.l=             efa->e1->tmp.l= efa->e2->tmp.l= efa->e3->tmp.l= 1;
2364
2365 /* all - 1) use all faces for extending the selection  2) only use the mouse face
2366 * sel - 1) select  0) deselect 
2367 * */
2368
2369 /* legacy warning, this function combines too much :) */
2370 static int select_linked_limited_invoke(ViewContext *vc, short all, short sel)
2371 {
2372         EditMesh *em= vc->em;
2373         EditFace *efa;
2374         EditEdge *eed;
2375         EditVert *eve;
2376         short done=1, change=0;
2377         
2378         if(em->faces.first==0) return OPERATOR_CANCELLED;
2379         
2380         /* flag all edges+faces as off*/
2381         for(eed= em->edges.first; eed; eed= eed->next)
2382                 eed->tmp.l=0;
2383         
2384         for(efa= em->faces.first; efa; efa= efa->next) {
2385                 efa->tmp.l = 0;
2386         }
2387         
2388         if (all) {
2389                 // XXX verts?
2390                 for(eed= em->edges.first; eed; eed= eed->next) {
2391                         if(eed->f & SELECT)
2392                                 eed->tmp.l= 1;
2393                 }
2394                 for(efa= em->faces.first; efa; efa= efa->next) {
2395                         
2396                         if (efa->f & SELECT) {
2397                                 face_tag(efa);
2398                         } else {
2399                                 efa->tmp.l = 0;
2400                         }
2401                 }
2402         } 
2403         else {
2404                 if( unified_findnearest(vc, &eve, &eed, &efa) ) {
2405                         
2406                         if(efa) {
2407                                 efa->tmp.l = 1;
2408                                 face_tag(efa);
2409                         }
2410                         else if(eed)
2411                                 eed->tmp.l= 1;
2412                         else {
2413                                 for(eed= em->edges.first; eed; eed= eed->next)
2414                                         if(eed->v1==eve || eed->v2==eve)
2415                                                 break;
2416                                 eed->tmp.l= 1;
2417                         }
2418                 }
2419                 else
2420                         return OPERATOR_FINISHED;
2421         }
2422         
2423         while(done==1) {
2424                 done= 0;
2425                 /* simple algo - select all faces that have a selected edge
2426                 * this intern selects the edge, repeat until nothing is left to do */
2427                 for(efa= em->faces.first; efa; efa= efa->next) {
2428                         if ((efa->tmp.l == 0) && (!efa->h)) {
2429                                 if (is_face_tag(efa)) {
2430                                         face_tag(efa);
2431                                         done= 1;
2432                                 }
2433                         }
2434                 }
2435         }
2436         
2437         for(efa= em->faces.first; efa; efa= efa->next) {
2438                 if (efa->tmp.l) {
2439                         if (sel) {
2440                                 if (!(efa->f & SELECT)) {
2441                                         EM_select_face(efa, 1);
2442                                         change = 1;
2443                                 }
2444                         } else {
2445                                 if (efa->f & SELECT) {
2446                                         EM_select_face(efa, 0);
2447                                         change = 1;
2448                                 }
2449                         }
2450                 }
2451         }
2452         
2453         if (!change)
2454                 return OPERATOR_CANCELLED;
2455         
2456         if (!sel) /* make sure de-selecting faces didnt de-select the verts/edges connected to selected faces, this is common with boundaries */
2457                 for(efa= em->faces.first; efa; efa= efa->next)
2458                         if (efa->f & SELECT)
2459                                 EM_select_face(efa, 1);
2460         
2461         //      if (EM_texFaceCheck())
2462         
2463         return OPERATOR_FINISHED;
2464 }
2465
2466 #undef is_edge_delimit_ok
2467 #undef is_face_tag
2468 #undef face_tag
2469
2470 static void linked_limit_default(bContext *C, wmOperator *op) {
2471         if(!RNA_property_is_set(op->ptr, "limit")) {
2472                 Object *obedit= CTX_data_edit_object(C);
2473                 EditMesh *em= BKE_mesh_get_editmesh(obedit->data);
2474                 if(em->selectmode == SCE_SELECT_FACE)
2475                         RNA_boolean_set(op->ptr, "limit", TRUE);
2476         }
2477 }
2478
2479 static int select_linked_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
2480 {
2481         Object *obedit= CTX_data_edit_object(C);
2482         ViewContext vc;
2483         EditVert *eve, *v1, *v2;
2484         EditEdge *eed;
2485         EditFace *efa;
2486         short done=1, toggle=0;
2487         int sel= !RNA_boolean_get(op->ptr, "deselect");
2488         int limit;
2489         
2490         linked_limit_default(C, op);
2491
2492         limit = RNA_boolean_get(op->ptr, "limit");
2493
2494         /* unified_finednearest needs ogl */
2495         view3d_operator_needs_opengl(C);
2496         
2497         /* setup view context for argument to callbacks */
2498         em_setup_viewcontext(C, &vc);
2499         
2500         if(vc.em->edges.first==0) return OPERATOR_CANCELLED;
2501         
2502         vc.mval[0]= event->mval[0];
2503         vc.mval[1]= event->mval[1];
2504         
2505         /* return warning! */
2506         if(limit) {
2507                 int retval= select_linked_limited_invoke(&vc, 0, sel);
2508                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
2509                 return retval;
2510         }
2511         
2512         if( unified_findnearest(&vc, &eve, &eed, &efa)==0 ) {
2513                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
2514         
2515                 return OPERATOR_CANCELLED;
2516         }
2517
2518         /* clear test flags */
2519         for(v1= vc.em->verts.first; v1; v1= v1->next) v1->f1= 0;
2520         
2521         /* start vertex/face/edge */
2522         if(eve) eve->f1= 1;
2523         else if(eed) eed->v1->f1= eed->v2->f1= 1;
2524         else efa->v1->f1= efa->v2->f1= efa->v3->f1= 1;
2525         
2526         /* set flag f1 if affected */
2527         while(done==1) {
2528                 done= 0;
2529                 toggle++;
2530                 
2531                 if(toggle & 1) eed= vc.em->edges.first;
2532                 else eed= vc.em->edges.last;
2533                 
2534                 while(eed) {
2535                         v1= eed->v1;
2536                         v2= eed->v2;
2537                         
2538                         if(eed->h==0) {
2539                                 if(v1->f1 && v2->f1==0) {
2540                                         v2->f1= 1;
2541                                         done= 1;
2542                                 }
2543                                 else if(v1->f1==0 && v2->f1) {
2544                                         v1->f1= 1;
2545                                         done= 1;
2546                                 }
2547                         }
2548                         
2549                         if(toggle & 1) eed= eed->next;
2550                         else eed= eed->prev;
2551                 }
2552         }
2553         
2554         /* now use vertex f1 flag to select/deselect */
2555         for(eed= vc.em->edges.first; eed; eed= eed->next) {
2556                 if(eed->v1->f1 && eed->v2->f1) 
2557                         EM_select_edge(eed, sel);
2558         }
2559         for(efa= vc.em->faces.first; efa; efa= efa->next) {
2560                 if(efa->v1->f1 && efa->v2->f1 && efa->v3->f1 && (efa->v4==NULL || efa->v4->f1)) 
2561                         EM_select_face(efa, sel);
2562         }
2563         /* no flush needed, connected geometry is done */
2564         
2565 //      if (EM_texFaceCheck())
2566         
2567         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
2568         return OPERATOR_FINISHED;       
2569 }
2570
2571 void MESH_OT_select_linked_pick(wmOperatorType *ot)
2572 {
2573         /* identifiers */
2574         ot->name= "Select Linked";
2575         ot->description= "(un)select all vertices linked to the active mesh";
2576         ot->idname= "MESH_OT_select_linked_pick";
2577         
2578         /* api callbacks */
2579         ot->invoke= select_linked_pick_invoke;
2580         ot->poll= ED_operator_editmesh_region_view3d;
2581         
2582         /* flags */
2583         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2584         
2585         RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "");
2586         RNA_def_boolean(ot->srna, "limit", 0, "Limit by Seams", "Limit selection by seam boundaries (faces only)");
2587 }
2588
2589
2590 /* ************************* */
2591
2592 void selectconnected_mesh_all(EditMesh *em)
2593 {
2594         EditVert *v1,*v2;
2595         EditEdge *eed;
2596         short done=1, toggle=0;
2597         
2598         if(em->edges.first==0) return;
2599         
2600         while(done==1) {
2601                 done= 0;
2602                 
2603                 toggle++;
2604                 if(toggle & 1) eed= em->edges.first;
2605                 else eed= em->edges.last;
2606                 
2607                 while(eed) {
2608                         v1= eed->v1;
2609                         v2= eed->v2;
2610                         if(eed->h==0) {
2611                                 if(v1->f & SELECT) {
2612                                         if( (v2->f & SELECT)==0 ) {
2613                                                 v2->f |= SELECT;
2614                                                 done= 1;
2615                                         }
2616                                 }
2617                                 else if(v2->f & SELECT) {
2618                                         if( (v1->f & SELECT)==0 ) {
2619                                                 v1->f |= SELECT;
2620                                                 done= 1;
2621                                         }
2622                                 }
2623                         }
2624                         if(toggle & 1) eed= eed->next;
2625                         else eed= eed->prev;
2626                 }
2627         }
2628         
2629         /* now use vertex select flag to select rest */
2630         EM_select_flush(em);
2631         
2632         //      if (EM_texFaceCheck())
2633 }
2634
2635 static int select_linked_exec(bContext *C, wmOperator *op)
2636 {
2637         Object *obedit= CTX_data_edit_object(C);
2638         EditMesh *em= BKE_mesh_get_editmesh(obedit->data);
2639         
2640         if( RNA_boolean_get(op->ptr, "limit") ) {
2641                 ViewContext vc;
2642                 em_setup_viewcontext(C, &vc);
2643                 select_linked_limited_invoke(&vc, 1, 1);
2644         }
2645         else
2646                 selectconnected_mesh_all(em);
2647         
2648         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
2649
2650         BKE_mesh_end_editmesh(obedit->data, em);
2651         return OPERATOR_FINISHED;       
2652 }
2653
2654 static int select_linked_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2655 {
2656         linked_limit_default(C, op);
2657         return select_linked_exec(C, op);
2658 }
2659
2660 void MESH_OT_select_linked(wmOperatorType *ot)
2661 {
2662         /* identifiers */
2663         ot->name= "Select Linked All";
2664         ot->description= "Select all vertices linked to the active mesh";
2665         ot->idname= "MESH_OT_select_linked";
2666         
2667         /* api callbacks */
2668         ot->exec= select_linked_exec;
2669         ot->invoke= select_linked_invoke;
2670         ot->poll= ED_operator_editmesh;
2671         
2672         /* flags */
2673         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2674         
2675         RNA_def_boolean(ot->srna, "limit", 0, "Limit by Seams", "Limit selection by seam boundaries (faces only)");
2676 }
2677
2678
2679 /* ************************* */
2680
2681 /* swap is 0 or 1, if 1 it hides not selected */
2682 void EM_hide_mesh(EditMesh *em, int swap)
2683 {
2684         EditVert *eve;
2685         EditEdge *eed;
2686         EditFace *efa;
2687         int a;
2688         
2689         if(em==NULL) return;
2690
2691         /* hide happens on least dominant select mode, and flushes up, not down! (helps preventing errors in subsurf) */
2692         /*  - vertex hidden, always means edge is hidden too
2693                 - edge hidden, always means face is hidden too
2694                 - face hidden, only set face hide
2695                 - then only flush back down what's absolute hidden
2696         */
2697         if(em->selectmode & SCE_SELECT_VERTEX) {
2698                 for(eve= em->verts.first; eve; eve= eve->next) {
2699                         if((eve->f & SELECT)!=swap) {
2700                                 eve->f &= ~SELECT;
2701                                 eve->h= 1;
2702                         }
2703                 }
2704         
2705                 for(eed= em->edges.first; eed; eed= eed->next) {
2706                         if(eed->v1->h || eed->v2->h) {
2707                                 eed->h |= 1;
2708                                 eed->f &= ~SELECT;
2709                         }
2710                 }
2711         
2712                 for(efa= em->faces.first; efa; efa= efa->next) {
2713                         if(efa->e1->h & 1 || efa->e2->h & 1 || efa->e3->h & 1 || (efa->e4 && efa->e4->h & 1)) {
2714                                 efa->h= 1;
2715                                 efa->f &= ~SELECT;
2716                         }
2717                 }
2718         }
2719         else if(em->selectmode & SCE_SELECT_EDGE) {
2720
2721                 for(eed= em->edges.first; eed; eed= eed->next) {
2722                         if((eed->f & SELECT)!=swap) {
2723                                 eed->h |= 1;
2724                                 EM_select_edge(eed, 0);
2725                         }
2726                 }
2727
2728                 for(efa= em->faces.first; efa; efa= efa->next) {
2729                         if(efa->e1->h & 1 || efa->e2->h & 1 || efa->e3->h & 1 || (efa->e4 && efa->e4->h & 1)) {
2730                                 efa->h= 1;
2731                                 efa->f &= ~SELECT;
2732                         }
2733                 }
2734         }
2735         else {
2736
2737                 for(efa= em->faces.first; efa; efa= efa->next) {
2738                         if((efa->f & SELECT)!=swap) {
2739                                 efa->h= 1;
2740                                 EM_select_face(efa, 0);
2741                         }
2742                 }
2743         }
2744         
2745         /* flush down, only whats 100% hidden */
2746         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
2747         for(eed= em->edges.first; eed; eed= eed->next) eed->f1= 0;
2748         
2749         if(em->selectmode & SCE_SELECT_FACE) {
2750                 for(efa= em->faces.first; efa; efa= efa->next) {
2751                         if(efa->h) a= 1; else a= 2;
2752                         efa->e1->f1 |= a;
2753                         efa->e2->f1 |= a;
2754                         efa->e3->f1 |= a;
2755                         if(efa->e4) efa->e4->f1 |= a;
2756                         /* When edges are not delt with in their own loop, we need to explicitly re-selct select edges that are joined to unselected faces */
2757                         if (swap && (em->selectmode == SCE_SELECT_FACE) && (efa->f & SELECT)) {
2758                                 EM_select_face(efa, 1);
2759                         }
2760                 }
2761         }
2762         
2763         if(em->selectmode >= SCE_SELECT_EDGE) {
2764                 for(eed= em->edges.first; eed; eed= eed->next) {
2765                         if(eed->f1==1) eed->h |= 1;
2766                         if(eed->h & 1) a= 1; else a= 2;
2767                         eed->v1->f1 |= a;
2768                         eed->v2->f1 |= a;
2769                 }
2770         }
2771
2772         if(em->selectmode >= SCE_SELECT_VERTEX) {
2773                 for(eve= em->verts.first; eve; eve= eve->next) {
2774                         if(eve->f1==1) eve->h= 1;
2775                 }
2776         }
2777         
2778         em->totedgesel= em->totfacesel= em->totvertsel= 0;
2779 //      if(EM_texFaceCheck())
2780
2781         //      DAG_id_tag_update(obedit->data, 0);
2782 }
2783
2784 static int hide_mesh_exec(bContext *C, wmOperator *op)
2785 {
2786         Object *obedit= CTX_data_edit_object(C);
2787         EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
2788         
2789         EM_hide_mesh(em, RNA_boolean_get(op->ptr, "unselected"));
2790                 
2791         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
2792
2793         BKE_mesh_end_editmesh(obedit->data, em);
2794         return OPERATOR_FINISHED;       
2795 }
2796
2797 void MESH_OT_hide(wmOperatorType *ot)
2798 {
2799         /* identifiers */
2800         ot->name= "Hide Selection";
2801         ot->description= "Hide (un)selected vertices, edges or faces";
2802         ot->idname= "MESH_OT_hide";
2803         
2804         /* api callbacks */
2805         ot->exec= hide_mesh_exec;
2806         ot->poll= ED_operator_editmesh;
2807         
2808         /* flags */
2809         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2810         
2811         /* props */
2812         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected.");
2813 }
2814
2815 void EM_reveal_mesh(EditMesh *em)
2816 {
2817         EditVert *eve;
2818         EditEdge *eed;
2819         EditFace *efa;
2820         
2821         if(em==NULL) return;
2822
2823         for(eve= em->verts.first; eve; eve= eve->next) {
2824                 if(eve->h) {
2825                         eve->h= 0;
2826                         eve->f |= SELECT;
2827                 }
2828         }
2829         for(eed= em->edges.first; eed; eed= eed->next) {
2830                 if(eed->h & 1) {
2831                         eed->h &= ~1;
2832                         if(em->selectmode & SCE_SELECT_VERTEX); 
2833                         else EM_select_edge(eed, 1);
2834                 }
2835         }
2836         for(efa= em->faces.first; efa; efa= efa->next) {
2837                 if(efa->h) {
2838                         efa->h= 0;
2839                         if(em->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_VERTEX)); 
2840                         else EM_select_face(efa, 1);
2841                 }
2842         }
2843
2844         EM_fgon_flags(em);      /* redo flags and indices for fgons */
2845         EM_selectmode_flush(em);
2846
2847 //      if (EM_texFaceCheck())
2848 //      DAG_id_tag_update(obedit->data, 0);
2849 }
2850
2851 static int reveal_mesh_exec(bContext *C, wmOperator *UNUSED(op))
2852 {
2853         Object *obedit= CTX_data_edit_object(C);
2854         EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
2855         
2856         EM_reveal_mesh(em);
2857
2858         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
2859
2860         BKE_mesh_end_editmesh(obedit->data, em);
2861         return OPERATOR_FINISHED;       
2862 }
2863
2864 void MESH_OT_reveal(wmOperatorType *ot)
2865 {
2866         /* identifiers */
2867         ot->name= "Reveal Hidden";
2868         ot->description= "Reveal all hidden vertices, edges and faces";
2869         ot->idname= "MESH_OT_reveal";
2870         
2871         /* api callbacks */
2872         ot->exec= reveal_mesh_exec;
2873         ot->poll= ED_operator_editmesh;
2874         
2875         /* flags */
2876         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2877 }
2878
2879 static int select_by_number_vertices_exec(bContext *C, wmOperator *op)
2880 {
2881         Object *obedit= CTX_data_edit_object(C);
2882         EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
2883         EditFace *efa;
2884         int numverts= RNA_enum_get(op->ptr, "type");
2885
2886         /* Selects trias/qiads or isolated verts, and edges that do not have 2 neighboring
2887          * faces
2888          */
2889
2890         /* for loose vertices/edges, we first select all, loop below will deselect */
2891         if(numverts==5) {
2892                 EM_set_flag_all(em, SELECT);
2893         }
2894         else if(em->selectmode!=SCE_SELECT_FACE) {
2895                 BKE_report(op->reports, RPT_WARNING, "Only works in face selection mode");
2896                 return OPERATOR_CANCELLED;
2897         }
2898         
2899         for(efa= em->faces.first; efa; efa= efa->next) {
2900                 if (efa->e4) {
2901                         EM_select_face(efa, (numverts==4) );
2902                 }
2903                 else {
2904                         EM_select_face(efa, (numverts==3) );
2905                 }
2906         }
2907         
2908         EM_selectmode_flush(em);
2909
2910         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
2911         
2912         return OPERATOR_FINISHED;
2913 }
2914
2915 void MESH_OT_select_by_number_vertices(wmOperatorType *ot)
2916 {
2917         static const EnumPropertyItem type_items[]= {
2918                 {3, "TRIANGLES", 0, "Triangles", NULL},
2919                 {4, "QUADS", 0, "Quads", NULL},
2920                 {5, "OTHER", 0, "Other", NULL},
2921                 {0, NULL, 0, NULL, NULL}};
2922
2923         /* identifiers */
2924         ot->name= "Select by Number of Vertices";
2925         ot->description= "Select vertices or faces by vertex count";
2926         ot->idname= "MESH_OT_select_by_number_vertices";
2927         
2928         /* api callbacks */
2929         ot->exec= select_by_number_vertices_exec;
2930         ot->invoke= WM_menu_invoke;
2931         ot->poll= ED_operator_editmesh;
2932         
2933         /* flags */
2934         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2935         
2936         /* props */
2937         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 3, "Type", "Type of elements to select.");
2938 }
2939
2940
2941 static int select_mirror_exec(bContext *C, wmOperator *op)
2942 {
2943         Object *obedit= CTX_data_edit_object(C);
2944         EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
2945
2946         int extend= RNA_boolean_get(op->ptr, "extend");
2947
2948         EM_select_mirrored(obedit, em, extend);
2949         EM_selectmode_flush(em);
2950         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
2951
2952         return OPERATOR_FINISHED;
2953 }
2954
2955 void MESH_OT_select_mirror(wmOperatorType *ot)
2956 {
2957         /* identifiers */
2958         ot->name= "Select Mirror";
2959         ot->description= "Select mesh items at mirrored locations";
2960         ot->idname= "MESH_OT_select_mirror";
2961
2962         /* api callbacks */
2963         ot->exec= select_mirror_exec;
2964         ot->poll= ED_operator_editmesh;
2965
2966         /* flags */
2967         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2968
2969         /* props */
2970         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the existing selection");
2971 }
2972
2973 static int select_sharp_edges_exec(bContext *C, wmOperator *op)
2974 {
2975         /* Find edges that have exactly two neighboring faces,
2976         * check the angle between those faces, and if angle is
2977         * small enough, select the edge
2978         */
2979         Object *obedit= CTX_data_edit_object(C);
2980         EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
2981         EditEdge *eed;
2982         EditFace *efa;
2983         EditFace **efa1;
2984         EditFace **efa2;
2985         intptr_t edgecount = 0, i = 0;
2986         float sharpness, fsharpness;
2987         
2988         /* 'standard' behaviour - check if selected, then apply relevant selection */
2989         
2990         if(em->selectmode==SCE_SELECT_FACE) {
2991                 BKE_report(op->reports, RPT_WARNING, "Doesn't work in face selection mode");
2992                 BKE_mesh_end_editmesh(obedit->data, em);
2993                 return OPERATOR_CANCELLED;
2994         }
2995
2996         sharpness= RNA_float_get(op->ptr, "sharpness");
2997         fsharpness = ((180.0f - sharpness) * (float)M_PI) / 180.0f;
2998