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