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