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