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