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