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