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