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