Merged 15170:15635 from trunk (no conflicts or even merges)
[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|Bevel Weight%x2|Length%x3");
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                 case 2: /* copy bevel weight */
1476                         for(eed=em->edges.first; eed; eed=eed->next) {
1477                                 if (eed->f & SELECT && eed != eed_act && eed->bweight != eed_act->bweight) {
1478                                         eed->bweight = eed_act->bweight;
1479                                         change = 1;
1480                                 }
1481                         }
1482                         break;
1483                         
1484                 case 3: /* copy length */
1485                         
1486                         for(eed=em->edges.first; eed; eed=eed->next) {
1487                                 if (eed->f & SELECT && eed != eed_act) {
1488                                         
1489                                         eed_len = VecLenf(eed->v1->co, eed->v2->co);
1490                                         
1491                                         if (eed_len == eed_len_act) continue;
1492                                         /* if this edge is zero length we cont do anything with it*/
1493                                         if (eed_len == 0.0f) continue;
1494                                         if (eed_len_act == 0.0f) {
1495                                                 VecAddf(vec_mid, eed->v1->co, eed->v2->co);
1496                                                 VecMulf(vec_mid, 0.5);
1497                                                 VECCOPY(eed->v1->co, vec_mid);
1498                                                 VECCOPY(eed->v2->co, vec_mid);
1499                                         } else {
1500                                                 /* copy the edge length */
1501                                                 VecAddf(vec_mid, eed->v1->co, eed->v2->co);
1502                                                 VecMulf(vec_mid, 0.5);
1503                                                 
1504                                                 /* SCALE 1 */
1505                                                 VecSubf(vec, eed->v1->co, vec_mid);
1506                                                 VecMulf(vec, eed_len_act/eed_len);
1507                                                 VecAddf(eed->v1->co, vec, vec_mid);
1508                                                 
1509                                                 /* SCALE 2 */
1510                                                 VecSubf(vec, eed->v2->co, vec_mid);
1511                                                 VecMulf(vec, eed_len_act/eed_len);
1512                                                 VecAddf(eed->v2->co, vec, vec_mid);
1513                                         }
1514                                         change = 1;
1515                                 }
1516                         }
1517                         
1518                         if (change)
1519                                 recalc_editnormals();
1520                         
1521                         
1522                         break;
1523                 }
1524                 
1525         } else if(ese==NULL || ese->type == EDITFACE) {
1526                 EditFace *efa, *efa_act;
1527                 MTFace *tf, *tf_act = NULL;
1528                 MCol *mcol, *mcol_act = NULL;
1529                 
1530                 efa_act = EM_get_actFace(0);
1531                 
1532                 if (efa_act) {
1533                         ret= pupmenu(
1534                                 "Copy Face Selected%t|"
1535                                 "Active Material%x1|Active Image%x2|Active UV Coords%x3|"
1536                                 "Active Mode%x4|Active Transp%x5|Active Vertex Colors%x6|%l|"
1537                                 
1538                                 "TexFace UVs from layer%x7|"
1539                                 "TexFace Images from layer%x8|"
1540                                 "TexFace All from layer%x9|"
1541                                 "Vertex Colors from layer%x10");
1542                         if (ret<1) return;
1543                         tf_act =        CustomData_em_get(&em->fdata, efa_act->data, CD_MTFACE);
1544                         mcol_act =      CustomData_em_get(&em->fdata, efa_act->data, CD_MCOL);
1545                 } else {
1546                         ret= pupmenu(
1547                                 "Copy Face Selected%t|"
1548                                 
1549                                 /* Make sure these are always the same as above */
1550                                 "TexFace UVs from layer%x7|"
1551                                 "TexFace Images from layer%x8|"
1552                                 "TexFace All from layer%x9|"
1553                                 "Vertex Colors from layer%x10");
1554                         if (ret<1) return;
1555                 }
1556                 
1557                 switch (ret) {
1558                 case 1: /* copy material */
1559                         for(efa=em->faces.first; efa; efa=efa->next) {
1560                                 if (efa->f & SELECT && efa->mat_nr != efa_act->mat_nr) {
1561                                         efa->mat_nr = efa_act->mat_nr;
1562                                         change = 1;
1563                                 }
1564                         }
1565                         break;
1566                 case 2: /* copy image */
1567                         if (!tf_act) {
1568                                 error("mesh has no uv/image layers");
1569                                 return;
1570                         }
1571                         for(efa=em->faces.first; efa; efa=efa->next) {
1572                                 if (efa->f & SELECT && efa != efa_act) {
1573                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1574                                         if (tf_act->tpage) {
1575                                                 tf->tpage = tf_act->tpage;
1576                                                 tf->mode |= TF_TEX;
1577                                         } else {
1578                                                 tf->tpage = NULL;
1579                                                 tf->mode &= ~TF_TEX;
1580                                         }
1581                                         tf->tile= tf_act->tile;
1582                                         change = 1;
1583                                 }
1584                         }
1585                         break;
1586                         
1587                 case 3: /* copy UV's */
1588                         if (!tf_act) {
1589                                 error("mesh has no uv/image layers");
1590                                 return;
1591                         }
1592                         for(efa=em->faces.first; efa; efa=efa->next) {
1593                                 if (efa->f & SELECT && efa != efa_act) {
1594                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1595                                         memcpy(tf->uv, tf_act->uv, sizeof(tf->uv));
1596                                         change = 1;
1597                                 }
1598                         }
1599                         break;
1600                 case 4: /* mode's */
1601                         if (!tf_act) {
1602                                 error("mesh has no uv/image layers");
1603                                 return;
1604                         }
1605                         for(efa=em->faces.first; efa; efa=efa->next) {
1606                                 if (efa->f & SELECT && efa != efa_act) {
1607                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1608                                         tf->mode= tf_act->mode;
1609                                         change = 1;
1610                                 }
1611                         }
1612                         break;
1613                 case 5: /* copy transp's */
1614                         if (!tf_act) {
1615                                 error("mesh has no uv/image layers");
1616                                 return;
1617                         }
1618                         for(efa=em->faces.first; efa; efa=efa->next) {
1619                                 if (efa->f & SELECT && efa != efa_act) {
1620                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1621                                         tf->transp= tf_act->transp;
1622                                         change = 1;
1623                                 }
1624                         }
1625                         break;
1626                         
1627                 case 6: /* copy vcols's */
1628                         if (!mcol_act) {
1629                                 error("mesh has no color layers");
1630                                 return;
1631                         } else {
1632                                 /* guess the 4th color if needs be */
1633                                 float val =- 1;
1634                                 
1635                                 if (!efa_act->v4) {
1636                                         /* guess the othe vale, we may need to use it
1637                                          * 
1638                                          * Modifying the 4th value of the mcol is ok here since its not seen
1639                                          * on a triangle
1640                                          * */
1641                                         val = ((float)(mcol_act->r +  (mcol_act+1)->r + (mcol_act+2)->r)) / 3; CLAMP(val, 0, 255);
1642                                         (mcol_act+3)->r = (char)val;
1643                                         
1644                                         val = ((float)(mcol_act->g +  (mcol_act+1)->g + (mcol_act+2)->g)) / 3; CLAMP(val, 0, 255);
1645                                         (mcol_act+3)->g = (char)val;
1646                                         
1647                                         val = ((float)(mcol_act->b +  (mcol_act+1)->b + (mcol_act+2)->b)) / 3; CLAMP(val, 0, 255);
1648                                         (mcol_act+3)->b = (char)val;
1649                                 } 
1650                                 
1651                                 
1652                                 for(efa=em->faces.first; efa; efa=efa->next) {
1653                                         if (efa->f & SELECT && efa != efa_act) {
1654                                                 /* TODO - make copy from tri to quad guess the 4th vert */
1655                                                 mcol = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
1656                                                 memcpy(mcol, mcol_act, sizeof(MCol)*4); 
1657                                                 change = 1;
1658                                         }
1659                                 }
1660                         }
1661                         
1662                         break;
1663                 
1664                 /* Copy from layer - Warning! tf_act and mcol_act will be NULL here */
1665                 case 7:
1666                 case 8:
1667                 case 9:
1668                         if (CustomData_number_of_layers(&em->fdata, CD_MTFACE)<2) {
1669                                 error("mesh does not have multiple uv/image layers");
1670                                 return;
1671                         } else {
1672                                 int layer_orig_idx, layer_idx;
1673                                 
1674                                 layer_idx = mesh_layers_menu(&em->fdata, CD_MTFACE);
1675                                 if (layer_idx<0) return;
1676                                 
1677                                 /* warning, have not updated mesh pointers however this is not needed since we swicth back */
1678                                 layer_orig_idx = CustomData_get_active_layer(&em->fdata, CD_MTFACE);
1679                                 if (layer_idx==layer_orig_idx)
1680                                         return;
1681                                 
1682                                 /* get the tfaces */
1683                                 CustomData_set_layer_active(&em->fdata, CD_MTFACE, (int)layer_idx);
1684                                 /* store the tfaces in our temp */
1685                                 for(efa=em->faces.first; efa; efa=efa->next) {
1686                                         if (efa->f & SELECT) {
1687                                                 efa->tmp.p = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1688                                         }       
1689                                 }
1690                                 CustomData_set_layer_active(&em->fdata, CD_MTFACE, layer_orig_idx);
1691                         }
1692                         break;
1693                         
1694                 case 10: /* select vcol layers - make sure this stays in sync with above code */
1695                         if (CustomData_number_of_layers(&em->fdata, CD_MCOL)<2) {
1696                                 error("mesh does not have multiple color layers");
1697                                 return;
1698                         } else {
1699                                 int layer_orig_idx, layer_idx;
1700                                 
1701                                 layer_idx = mesh_layers_menu(&em->fdata, CD_MCOL);
1702                                 if (layer_idx<0) return;
1703                                 
1704                                 /* warning, have not updated mesh pointers however this is not needed since we swicth back */
1705                                 layer_orig_idx = CustomData_get_active_layer(&em->fdata, CD_MCOL);
1706                                 if (layer_idx==layer_orig_idx)
1707                                         return;
1708                                 
1709                                 /* get the tfaces */
1710                                 CustomData_set_layer_active(&em->fdata, CD_MCOL, (int)layer_idx);
1711                                 /* store the tfaces in our temp */
1712                                 for(efa=em->faces.first; efa; efa=efa->next) {
1713                                         if (efa->f & SELECT) {
1714                                                 efa->tmp.p = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
1715                                         }       
1716                                 }
1717                                 CustomData_set_layer_active(&em->fdata, CD_MCOL, layer_orig_idx);
1718                                 
1719                         }
1720                         break;
1721                 }
1722                 
1723                 /* layer copy only - sanity checks done above */
1724                 switch (ret) {
1725                 case 7: /* copy UV's only */
1726                         for(efa=em->faces.first; efa; efa=efa->next) {
1727                                 if (efa->f & SELECT) {
1728                                         tf_act = (MTFace *)efa->tmp.p; /* not active but easier to use this way */
1729                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1730                                         memcpy(tf->uv, tf_act->uv, sizeof(tf->uv));
1731                                         change = 1;
1732                                 }
1733                         }
1734                         break;
1735                 case 8: /* copy image settings only */
1736                         for(efa=em->faces.first; efa; efa=efa->next) {
1737                                 if (efa->f & SELECT) {
1738                                         tf_act = (MTFace *)efa->tmp.p; /* not active but easier to use this way */
1739                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1740                                         if (tf_act->tpage) {
1741                                                 tf->tpage = tf_act->tpage;
1742                                                 tf->mode |= TF_TEX;
1743                                         } else {
1744                                                 tf->tpage = NULL;
1745                                                 tf->mode &= ~TF_TEX;
1746                                         }
1747                                         tf->tile= tf_act->tile;
1748                                         change = 1;
1749                                 }
1750                         }
1751                         break;
1752                 case 9: /* copy all tface info */
1753                         for(efa=em->faces.first; efa; efa=efa->next) {
1754                                 if (efa->f & SELECT) {
1755                                         tf_act = (MTFace *)efa->tmp.p; /* not active but easier to use this way */
1756                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1757                                         memcpy(tf->uv, ((MTFace *)efa->tmp.p)->uv, sizeof(tf->uv));
1758                                         tf->tpage = tf_act->tpage;
1759                                         tf->mode = tf_act->mode;
1760                                         tf->transp = tf_act->transp;
1761                                         change = 1;
1762                                 }
1763                         }
1764                         break;
1765                 case 10:
1766                         for(efa=em->faces.first; efa; efa=efa->next) {
1767                                 if (efa->f & SELECT) {
1768                                         mcol_act = (MCol *)efa->tmp.p; 
1769                                         mcol = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
1770                                         memcpy(mcol, mcol_act, sizeof(MCol)*4); 
1771                                         change = 1;
1772                                 }
1773                         }
1774                         break;
1775                 }
1776                 
1777         }
1778         
1779         if (change) {
1780                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1781                 allqueue(REDRAWVIEW3D, 0);
1782                 allqueue(REDRAWBUTSEDIT, 0);
1783                 
1784                 if (ese==NULL ||        ese->type == EDITFACE)  BIF_undo_push("Copy Face Attribute");
1785                 else if (                       ese->type == EDITEDGE)  BIF_undo_push("Copy Edge Attribute");
1786                 else if (                       ese->type == EDITVERT)  BIF_undo_push("Copy Vert Attribute");
1787                 
1788         }
1789         
1790 }
1791
1792
1793 /* ****************  LOOP SELECTS *************** */
1794
1795 /* selects quads in loop direction of indicated edge */
1796 /* only flush over edges with valence <= 2 */
1797 void faceloop_select(EditEdge *startedge, int select)
1798 {
1799         EditMesh *em = G.editMesh;
1800         EditEdge *eed;
1801         EditFace *efa;
1802         int looking= 1;
1803         
1804         /* in eed->f1 we put the valence (amount of faces in edge) */
1805         /* in eed->f2 we put tagged flag as correct loop */
1806         /* in efa->f1 we put tagged flag as correct to select */
1807
1808         for(eed= em->edges.first; eed; eed= eed->next) {
1809                 eed->f1= 0;
1810                 eed->f2= 0;
1811         }
1812         for(efa= em->faces.first; efa; efa= efa->next) {
1813                 efa->f1= 0;
1814                 if(efa->h==0) {
1815                         efa->e1->f1++;
1816                         efa->e2->f1++;
1817                         efa->e3->f1++;
1818                         if(efa->e4) efa->e4->f1++;
1819                 }
1820         }
1821         
1822         /* tag startedge OK*/
1823         startedge->f2= 1;
1824         
1825         while(looking) {
1826                 looking= 0;
1827                 
1828                 for(efa= em->faces.first; efa; efa= efa->next) {
1829                         if(efa->e4 && efa->f1==0) {     /* not done quad */
1830                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { /* valence ok */
1831
1832                                         /* if edge tagged, select opposing edge and mark face ok */
1833                                         if(efa->e1->f2) {
1834                                                 efa->e3->f2= 1;
1835                                                 efa->f1= 1;
1836                                                 looking= 1;
1837                                         }
1838                                         else if(efa->e2->f2) {
1839                                                 efa->e4->f2= 1;
1840                                                 efa->f1= 1;
1841                                                 looking= 1;
1842                                         }
1843                                         if(efa->e3->f2) {
1844                                                 efa->e1->f2= 1;
1845                                                 efa->f1= 1;
1846                                                 looking= 1;
1847                                         }
1848                                         if(efa->e4->f2) {
1849                                                 efa->e2->f2= 1;
1850                                                 efa->f1= 1;
1851                                                 looking= 1;
1852                                         }
1853                                 }
1854                         }
1855                 }
1856         }
1857         
1858         /* (de)select the faces */
1859         if(select!=2) {
1860                 for(efa= em->faces.first; efa; efa= efa->next) {
1861                         if(efa->f1) EM_select_face(efa, select);
1862                 }
1863         }
1864 }
1865
1866
1867 /* helper for edgeloop_select, checks for eed->f2 tag in faces */
1868 static int edge_not_in_tagged_face(EditEdge *eed)
1869 {
1870         EditMesh *em = G.editMesh;
1871         EditFace *efa;
1872         
1873         for(efa= em->faces.first; efa; efa= efa->next) {
1874                 if(efa->h==0) {
1875                         if(efa->e1==eed || efa->e2==eed || efa->e3==eed || efa->e4==eed) {      /* edge is in face */
1876                                 if(efa->e1->f2 || efa->e2->f2 || efa->e3->f2 || (efa->e4 && efa->e4->f2)) {     /* face is tagged */
1877                                         return 0;
1878                                 }
1879                         }
1880                 }
1881         }
1882         return 1;
1883 }
1884
1885 /* selects or deselects edges that:
1886 - if edges has 2 faces:
1887         - has vertices with valence of 4
1888         - not shares face with previous edge
1889 - if edge has 1 face:
1890         - has vertices with valence 4
1891         - not shares face with previous edge
1892         - but also only 1 face
1893 - if edge no face:
1894         - has vertices with valence 2
1895 */
1896 static void edgeloop_select(EditEdge *starteed, int select)
1897 {
1898         EditMesh *em = G.editMesh;
1899         EditVert *eve;
1900         EditEdge *eed;
1901         EditFace *efa;
1902         int looking= 1;
1903         
1904         /* in f1 we put the valence (amount of edges in a vertex, or faces in edge) */
1905         /* in eed->f2 and efa->f1 we put tagged flag as correct loop */
1906         for(eve= em->verts.first; eve; eve= eve->next) {
1907                 eve->f1= 0;
1908                 eve->f2= 0;
1909         }
1910         for(eed= em->edges.first; eed; eed= eed->next) {
1911                 eed->f1= 0;
1912                 eed->f2= 0;
1913                 if((eed->h & 1)==0) {   /* fgon edges add to valence too */
1914                         eed->v1->f1++; eed->v2->f1++;
1915                 }
1916         }
1917         for(efa= em->faces.first; efa; efa= efa->next) {
1918                 efa->f1= 0;
1919                 if(efa->h==0) {
1920                         efa->e1->f1++;
1921                         efa->e2->f1++;
1922                         efa->e3->f1++;
1923                         if(efa->e4) efa->e4->f1++;
1924                 }
1925         }
1926         
1927         /* looped edges & vertices get tagged f2 */
1928         starteed->f2= 1;
1929         if(starteed->v1->f1<5) starteed->v1->f2= 1;
1930         if(starteed->v2->f1<5) starteed->v2->f2= 1;
1931         /* sorry, first edge isnt even ok */
1932         if(starteed->v1->f2==0 && starteed->v2->f2==0) looking= 0;
1933         
1934         while(looking) {
1935                 looking= 0;
1936                 
1937                 /* find correct valence edges which are not tagged yet, but connect to tagged one */
1938                 for(eed= em->edges.first; eed; eed= eed->next) {
1939                         if(eed->h==0 && eed->f2==0) { /* edge not hidden, not tagged */
1940                                 if( (eed->v1->f1<5 && eed->v1->f2) || (eed->v2->f1<5 && eed->v2->f2)) { /* valence of vertex OK, and is tagged */
1941                                         /* new edge is not allowed to be in face with tagged edge */
1942                                         if(edge_not_in_tagged_face(eed)) {
1943                                                 if(eed->f1==starteed->f1) {     /* same amount of faces */
1944                                                         looking= 1;
1945                                                         eed->f2= 1;
1946                                                         if(eed->v2->f1<5) eed->v2->f2= 1;
1947                                                         if(eed->v1->f1<5) eed->v1->f2= 1;
1948                                                 }
1949                                         }
1950                                 }
1951                         }
1952                 }
1953         }
1954         /* and we do the select */
1955         for(eed= em->edges.first; eed; eed= eed->next) {
1956                 if(eed->f2) EM_select_edge(eed, select);
1957         }
1958 }
1959
1960 /* 
1961    Almostly exactly the same code as faceloop select
1962 */
1963 static void edgering_select(EditEdge *startedge, int select){
1964         EditMesh *em = G.editMesh;
1965         EditEdge *eed;
1966         EditFace *efa;
1967         int looking= 1;
1968         
1969         /* in eed->f1 we put the valence (amount of faces in edge) */
1970         /* in eed->f2 we put tagged flag as correct loop */
1971         /* in efa->f1 we put tagged flag as correct to select */
1972
1973         for(eed= em->edges.first; eed; eed= eed->next) {
1974                 eed->f1= 0;
1975                 eed->f2= 0;
1976         }
1977         for(efa= em->faces.first; efa; efa= efa->next) {
1978                 efa->f1= 0;
1979                 if(efa->h==0) {
1980                         efa->e1->f1++;
1981                         efa->e2->f1++;
1982                         efa->e3->f1++;
1983                         if(efa->e4) efa->e4->f1++;
1984                 }
1985         }
1986         
1987         /* tag startedge OK */
1988         startedge->f2= 1;
1989         
1990         while(looking) {
1991                 looking= 0;
1992                 
1993                 for(efa= em->faces.first; efa; efa= efa->next) {
1994                         if(efa->e4 && efa->f1==0 && !efa->h) {  /* not done quad */
1995                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { /* valence ok */
1996
1997                                         /* if edge tagged, select opposing edge and mark face ok */
1998                                         if(efa->e1->f2) {
1999                                                 efa->e3->f2= 1;
2000                                                 efa->f1= 1;
2001                                                 looking= 1;
2002                                         }
2003                                         else if(efa->e2->f2) {
2004                                                 efa->e4->f2= 1;
2005                                                 efa->f1= 1;
2006                                                 looking= 1;
2007                                         }
2008                                         if(efa->e3->f2) {
2009                                                 efa->e1->f2= 1;
2010                                                 efa->f1= 1;
2011                                                 looking= 1;
2012                                         }
2013                                         if(efa->e4->f2) {
2014                                                 efa->e2->f2= 1;
2015                                                 efa->f1= 1;
2016                                                 looking= 1;
2017                                         }
2018                                 }
2019                         }
2020                 }
2021         }
2022         
2023         /* (de)select the edges */
2024         for(eed= em->edges.first; eed; eed= eed->next) {
2025                 if(eed->f2) EM_select_edge(eed, select);
2026         }
2027 }
2028
2029 void loop_multiselect(int looptype)
2030 {
2031         EditEdge *eed;
2032         EditEdge **edarray;
2033         int edindex, edfirstcount;
2034         
2035         /*edarray = MEM_mallocN(sizeof(*edarray)*G.totedgesel,"edge array");*/
2036         edarray = MEM_mallocN(sizeof(EditEdge*)*G.totedgesel,"edge array");
2037         edindex = 0;
2038         edfirstcount = G.totedgesel;
2039         
2040         for(eed=G.editMesh->edges.first; eed; eed=eed->next){
2041                 if(eed->f&SELECT){
2042                         edarray[edindex] = eed;
2043                         edindex += 1;
2044                 }
2045         }
2046         
2047         if(looptype){
2048                 for(edindex = 0; edindex < edfirstcount; edindex +=1){
2049                         eed = edarray[edindex];
2050                         edgering_select(eed,SELECT);
2051                 }
2052                 countall();
2053                 EM_selectmode_flush();
2054                 BIF_undo_push("Edge Ring Multi-Select");
2055         }
2056         else{
2057                 for(edindex = 0; edindex < edfirstcount; edindex +=1){
2058                         eed = edarray[edindex];
2059                         edgeloop_select(eed,SELECT);
2060                 }
2061                 countall();
2062                 EM_selectmode_flush();
2063                 BIF_undo_push("Edge Loop Multi-Select");
2064         }
2065         MEM_freeN(edarray);
2066         allqueue(REDRAWVIEW3D,0);
2067         if (EM_texFaceCheck())
2068                 allqueue(REDRAWIMAGE, 0);
2069 }
2070                 
2071 /* ***************** MAIN MOUSE SELECTION ************** */
2072
2073 /* just to have the functions nice together */
2074
2075 static void mouse_mesh_loop(void)
2076 {
2077         EditEdge *eed;
2078         int select= 1;
2079         int dist= 50;
2080         
2081         eed= findnearestedge(&dist);
2082         if(eed) {
2083                 if (G.scene->toolsettings->edge_mode == EDGE_MODE_SELECT) {
2084                         if((G.qual & LR_SHIFTKEY)==0) EM_clear_flag_all(SELECT);
2085                 
2086                         if((eed->f & SELECT)==0) select=1;
2087                         else if(G.qual & LR_SHIFTKEY) select=0;
2088
2089                         if(G.scene->selectmode & SCE_SELECT_FACE) {
2090                                 faceloop_select(eed, select);
2091                         }
2092                         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
2093                         if(G.qual == (LR_CTRLKEY | LR_ALTKEY) || G.qual == (LR_CTRLKEY | LR_ALTKEY |LR_SHIFTKEY))
2094                                         edgering_select(eed, select);
2095                         else if(G.qual & LR_ALTKEY)
2096                                         edgeloop_select(eed, select);
2097                         }
2098                     else if(G.scene->selectmode & SCE_SELECT_VERTEX) {
2099                         if(G.qual == (LR_CTRLKEY | LR_ALTKEY) || G.qual == (LR_CTRLKEY | LR_ALTKEY |LR_SHIFTKEY))
2100                                         edgering_select(eed, select);
2101                         else if(G.qual & LR_ALTKEY)
2102                                         edgeloop_select(eed, select);
2103                         }
2104
2105                         /* frontbuffer draw of last selected only */
2106                         unified_select_draw(NULL, eed, NULL);
2107                 
2108                         EM_selectmode_flush();
2109                         countall();
2110                         allqueue(REDRAWVIEW3D, 0);
2111                         if (EM_texFaceCheck())
2112                                 allqueue(REDRAWIMAGE, 0);
2113                 } else { /*(G.scene->toolsettings->edge_mode == EDGE_MODE_TAG_*)*/
2114                         int act = (edgetag_context_check(eed)==0);
2115                         int path = 0;
2116                         
2117                         if (G.qual == (LR_SHIFTKEY | LR_ALTKEY) && G.editMesh->selected.last) {
2118                                 EditSelection *ese = G.editMesh->selected.last;
2119         
2120                                 if(ese && ese->type == EDITEDGE) {
2121                                         EditEdge *eed_act;
2122                                         eed_act = (EditEdge*)ese->data;
2123                                         if (eed_act != eed) {
2124                                                 /* If shift is pressed we need to use the last active edge, (if it exists) */
2125                                                 if (edgetag_shortest_path(eed_act, eed)) {
2126                                                         EM_remove_selection(eed_act, EDITEDGE);
2127                                                         EM_select_edge(eed_act, 0);
2128                                                         path = 1;
2129                                                 }
2130                                         }
2131                                 }
2132                         }
2133                         if (path==0) {
2134                                 edgetag_context_set(eed, act); /* switch the edge option */
2135                         }
2136                         
2137                         if (act) {
2138                                 if ((eed->f & SELECT)==0) {
2139                                         EM_select_edge(eed, 1);
2140                                         EM_selectmode_flush();
2141                                         countall();
2142                                 }
2143                                 /* even if this is selected it may not be in the selection list */
2144                                 EM_store_selection(eed, EDITEDGE);
2145                         } else {
2146                                 if (eed->f & SELECT) {
2147                                         EM_select_edge(eed, 0);
2148                                         /* logic is differnt from above here since if this was selected we dont know if its in the selection list or not */
2149                                         EM_remove_selection(eed, EDITEDGE);
2150                                         
2151                                         EM_selectmode_flush();
2152                                         countall();
2153                                 }
2154                         }
2155                         
2156                         switch (G.scene->toolsettings->edge_mode) {
2157                         case EDGE_MODE_TAG_SEAM:
2158                                 G.f |= G_DRAWSEAMS;
2159                                 break;
2160                         case EDGE_MODE_TAG_SHARP:
2161                                 G.f |= G_DRAWSHARP;
2162                                 break;
2163                         case EDGE_MODE_TAG_CREASE:      
2164                                 G.f |= G_DRAWCREASES;
2165                                 break;
2166                         case EDGE_MODE_TAG_BEVEL:
2167                                 G.f |= G_DRAWBWEIGHTS;
2168                                 break;
2169                         }
2170                         
2171                         unified_select_draw(NULL, eed, NULL);
2172                         
2173                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2174                         allqueue(REDRAWVIEW3D, 0);
2175                 }
2176                                 
2177         }
2178 }
2179
2180
2181 /* here actual select happens */
2182 void mouse_mesh(void)
2183 {
2184         EditVert *eve;
2185         EditEdge *eed;
2186         EditFace *efa;
2187         
2188         if(G.qual & LR_ALTKEY) mouse_mesh_loop();
2189         else if(unified_findnearest(&eve, &eed, &efa)) {
2190                 
2191                 if((G.qual & LR_SHIFTKEY)==0) EM_clear_flag_all(SELECT);
2192                 
2193                 if(efa) {
2194                         /* set the last selected face */
2195                         EM_set_actFace(efa);
2196                         
2197                         if( (efa->f & SELECT)==0 ) {
2198                                 EM_store_selection(efa, EDITFACE);
2199                                 EM_select_face_fgon(efa, 1);
2200                         }
2201                         else if(G.qual & LR_SHIFTKEY) {
2202                                 EM_remove_selection(efa, EDITFACE);
2203                                 EM_select_face_fgon(efa, 0);
2204                         }
2205                 }
2206                 else if(eed) {
2207                         if((eed->f & SELECT)==0) {
2208                                 EM_store_selection(eed, EDITEDGE);
2209                                 EM_select_edge(eed, 1);
2210                         }
2211                         else if(G.qual & LR_SHIFTKEY) {
2212                                 EM_remove_selection(eed, EDITEDGE);
2213                                 EM_select_edge(eed, 0);
2214                         }
2215                 }
2216                 else if(eve) {
2217                         if((eve->f & SELECT)==0) {
2218                                 eve->f |= SELECT;
2219                                 EM_store_selection(eve, EDITVERT);
2220                         }
2221                         else if(G.qual & LR_SHIFTKEY){ 
2222                                 EM_remove_selection(eve, EDITVERT);
2223                                 eve->f &= ~SELECT;
2224                         }
2225                 }
2226                 
2227                 /* frontbuffer draw of last selected only */
2228                 unified_select_draw(eve, eed, efa);
2229         
2230                 EM_selectmode_flush();
2231                 countall();
2232                   
2233                 allqueue(REDRAWVIEW3D, 0);
2234                 if (EM_texFaceCheck()) {
2235                         allqueue(REDRAWIMAGE, 0);
2236                         allqueue(REDRAWBUTSEDIT, 0); /* for the texture face panel */
2237                 }
2238                 if (efa && efa->mat_nr != G.obedit->actcol-1) {
2239                         G.obedit->actcol= efa->mat_nr+1;
2240                         allqueue(REDRAWBUTSEDIT, 0);
2241                         allqueue(REDRAWBUTSSHADING, 0);
2242                         BIF_preview_changed(ID_MA);
2243                 }
2244         }
2245
2246         rightmouse_transform();
2247 }
2248
2249
2250 void selectconnected_mesh_all(void)
2251 {
2252         EditMesh *em = G.editMesh;
2253         EditVert *v1,*v2;
2254         EditEdge *eed;
2255         short done=1, toggle=0;
2256
2257         if(em->edges.first==0) return;
2258         
2259         while(done==1) {
2260                 done= 0;
2261                 
2262                 toggle++;
2263                 if(toggle & 1) eed= em->edges.first;
2264                 else eed= em->edges.last;
2265                 
2266                 while(eed) {
2267                         v1= eed->v1;
2268                         v2= eed->v2;
2269                         if(eed->h==0) {
2270                                 if(v1->f & SELECT) {
2271                                         if( (v2->f & SELECT)==0 ) {
2272                                                 v2->f |= SELECT;
2273                                                 done= 1;
2274                                         }
2275                                 }
2276                                 else if(v2->f & SELECT) {
2277                                         if( (v1->f & SELECT)==0 ) {
2278                                                 v1->f |= SELECT;
2279                                                 done= 1;
2280                                         }
2281                                 }
2282                         }
2283                         if(toggle & 1) eed= eed->next;
2284                         else eed= eed->prev;
2285                 }
2286         }
2287
2288         /* now use vertex select flag to select rest */
2289         EM_select_flush();
2290         
2291         countall();
2292
2293         allqueue(REDRAWVIEW3D, 0);
2294         if (EM_texFaceCheck())
2295                 allqueue(REDRAWIMAGE, 0);
2296         BIF_undo_push("Select Connected (All)");
2297 }
2298
2299 void selectconnected_mesh(void)
2300 {
2301         EditMesh *em = G.editMesh;
2302         EditVert *eve, *v1, *v2;
2303         EditEdge *eed;
2304         EditFace *efa;
2305         short done=1, sel, toggle=0;
2306
2307         if(em->edges.first==0) return;
2308         
2309         if( unified_findnearest(&eve, &eed, &efa)==0 ) {
2310                 /* error("Nothing indicated "); */ /* this is mostly annoying, eps with occluded geometry */
2311                 return;
2312         }
2313         
2314         sel= 1;
2315         if(G.qual & LR_SHIFTKEY) sel=0;
2316
2317         /* clear test flags */
2318         for(v1= em->verts.first; v1; v1= v1->next) v1->f1= 0;
2319         
2320         /* start vertex/face/edge */
2321         if(eve) eve->f1= 1;
2322         else if(eed) eed->v1->f1= eed->v2->f1= 1;
2323         else efa->v1->f1= efa->v2->f1= efa->v3->f1= 1;
2324         
2325         /* set flag f1 if affected */
2326         while(done==1) {
2327                 done= 0;
2328                 toggle++;
2329                 
2330                 if(toggle & 1) eed= em->edges.first;
2331                 else eed= em->edges.last;
2332                 
2333                 while(eed) {
2334                         v1= eed->v1;
2335                         v2= eed->v2;
2336                         
2337                         if(eed->h==0) {
2338                                 if(v1->f1 && v2->f1==0) {
2339                                         v2->f1= 1;
2340                                         done= 1;
2341                                 }
2342                                 else if(v1->f1==0 && v2->f1) {
2343                                         v1->f1= 1;
2344                                         done= 1;
2345                                 }
2346                         }
2347                         
2348                         if(toggle & 1) eed= eed->next;
2349                         else eed= eed->prev;
2350                 }
2351         }
2352         
2353         /* now use vertex f1 flag to select/deselect */
2354         for(eed= em->edges.first; eed; eed= eed->next) {
2355                 if(eed->v1->f1 && eed->v2->f1) 
2356                         EM_select_edge(eed, sel);
2357         }
2358         for(efa= em->faces.first; efa; efa= efa->next) {
2359                 if(efa->v1->f1 && efa->v2->f1 && efa->v3->f1 && (efa->v4==NULL || efa->v4->f1)) 
2360                         EM_select_face(efa, sel);
2361         }
2362         /* no flush needed, connected geometry is done */
2363         
2364         countall();
2365         
2366         allqueue(REDRAWVIEW3D, 0);
2367         if (EM_texFaceCheck())
2368                 allqueue(REDRAWIMAGE, 0);
2369         
2370         BIF_undo_push("Select Linked");
2371         
2372 }
2373
2374 /* for use with selectconnected_delimit_mesh only! */
2375 #define is_edge_delimit_ok(eed) ((eed->tmp.l == 1) && (eed->seam==0))
2376 #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))
2377
2378 #define face_tag(efa)\
2379         if(efa->v4)     efa->tmp.l=             efa->e1->tmp.l= efa->e2->tmp.l= efa->e3->tmp.l= efa->e4->tmp.l= 1;\
2380         else            efa->tmp.l=             efa->e1->tmp.l= efa->e2->tmp.l= efa->e3->tmp.l= 1;
2381
2382 /* all - 1) use all faces for extending the selection  2) only use the mouse face
2383  * sel - 1) select  0) deselect 
2384  * */
2385 static void selectconnected_delimit_mesh__internal(short all, short sel)
2386 {
2387         EditMesh *em = G.editMesh;
2388         EditFace *efa;
2389         short done=1, change=0;
2390         int dist = 75;
2391         EditEdge *eed;
2392         if(em->faces.first==0) return;
2393         
2394         /* flag all edges as off*/
2395         for(eed= em->edges.first; eed; eed= eed->next)
2396                 eed->tmp.l=0;
2397         
2398         if (all) {
2399                 for(efa= em->faces.first; efa; efa= efa->next) {
2400                         if (efa->f & SELECT) {
2401                                 face_tag(efa);
2402                         } else {
2403                                 efa->tmp.l = 0;
2404                         }
2405                 }
2406         } else {
2407                 EditFace *efa_mouse = findnearestface(&dist);
2408                 
2409                 if( !efa_mouse ) {
2410                         /* error("Nothing indicated "); */ /* this is mostly annoying, eps with occluded geometry */
2411                         return;
2412                 }
2413                 
2414                 for(efa= em->faces.first; efa; efa= efa->next) {
2415                         efa->tmp.l = 0;
2416                 }
2417                 efa_mouse->tmp.l = 1;
2418                 face_tag(efa_mouse);
2419         }
2420         
2421         while(done==1) {
2422                 done= 0;
2423                 /* simple algo - select all faces that have a selected edge
2424                  * this intern selects the edge, repeat until nothing is left to do */
2425                 for(efa= em->faces.first; efa; efa= efa->next) {
2426                         if ((efa->tmp.l == 0) && (!efa->h)) {
2427                                 if (is_face_tag(efa)) {
2428                                         face_tag(efa);
2429                                         done= 1;
2430                                 }
2431                         }
2432                 }
2433         }
2434         
2435         for(efa= em->faces.first; efa; efa= efa->next) {
2436                 if (efa->tmp.l) {
2437                         if (sel) {
2438                                 if (!(efa->f & SELECT)) {
2439                                         EM_select_face(efa, 1);
2440                                         change = 1;
2441                                 }
2442                         } else {
2443                                 if (efa->f & SELECT) {
2444                                         EM_select_face(efa, 0);
2445                                         change = 1;
2446                                 }
2447                         }
2448                 }
2449         }
2450         
2451         if (!change)
2452                 return;
2453         
2454         if (!sel) /* make sure de-selecting faces didnt de-select the verts/edges connected to selected faces, this is common with boundries */
2455                 for(efa= em->faces.first; efa; efa= efa->next)
2456                         if (efa->f & SELECT)
2457                                 EM_select_face(efa, 1);
2458         
2459         countall();
2460         
2461         allqueue(REDRAWVIEW3D, 0);
2462         if (EM_texFaceCheck())
2463                 allqueue(REDRAWIMAGE, 0);
2464         
2465         BIF_undo_push("Select Linked Delimeted");
2466         
2467 }
2468
2469 #undef is_edge_delimit_ok
2470 #undef is_face_tag
2471 #undef face_tag
2472
2473 void selectconnected_delimit_mesh(void)
2474 {
2475         selectconnected_delimit_mesh__internal(0, ((G.qual & LR_SHIFTKEY)==0));
2476 }
2477 void selectconnected_delimit_mesh_all(void)
2478 {
2479         selectconnected_delimit_mesh__internal(1, 1);
2480 }       
2481         
2482         
2483 /* swap is 0 or 1, if 1 it hides not selected */
2484 void hide_mesh(int swap)
2485 {
2486         EditMesh *em = G.editMesh;
2487         EditVert *eve;
2488         EditEdge *eed;
2489         EditFace *efa;
2490         int a;
2491         
2492         if(G.obedit==0) return;
2493
2494         /* hide happens on least dominant select mode, and flushes up, not down! (helps preventing errors in subsurf) */
2495         /*  - vertex hidden, always means edge is hidden too
2496                 - edge hidden, always means face is hidden too
2497                 - face hidden, only set face hide
2498                 - then only flush back down what's absolute hidden
2499         */
2500         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
2501                 for(eve= em->verts.first; eve; eve= eve->next) {
2502                         if((eve->f & SELECT)!=swap) {
2503                                 eve->f &= ~SELECT;
2504                                 eve->h= 1;
2505                         }
2506                 }
2507         
2508                 for(eed= em->edges.first; eed; eed= eed->next) {
2509                         if(eed->v1->h || eed->v2->h) {
2510                                 eed->h |= 1;
2511                                 eed->f &= ~SELECT;
2512                         }
2513                 }
2514         
2515                 for(efa= em->faces.first; efa; efa= efa->next) {
2516                         if(efa->e1->h & 1 || efa->e2->h & 1 || efa->e3->h & 1 || (efa->e4 && efa->e4->h & 1)) {
2517                                 efa->h= 1;
2518                                 efa->f &= ~SELECT;
2519                         }
2520                 }
2521         }
2522         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
2523
2524                 for(eed= em->edges.first; eed; eed= eed->next) {
2525                         if((eed->f & SELECT)!=swap) {
2526                                 eed->h |= 1;
2527                                 EM_select_edge(eed, 0);
2528                         }
2529                 }
2530
2531                 for(efa= em->faces.first; efa; efa= efa->next) {
2532                         if(efa->e1->h & 1 || efa->e2->h & 1 || efa->e3->h & 1 || (efa->e4 && efa->e4->h & 1)) {
2533                                 efa->h= 1;
2534                                 efa->f &= ~SELECT;
2535                         }
2536                 }
2537         }
2538         else {
2539
2540                 for(efa= em->faces.first; efa; efa= efa->next) {
2541                         if((efa->f & SELECT)!=swap) {
2542                                 efa->h= 1;
2543                                 EM_select_face(efa, 0);
2544                         }
2545                 }
2546         }
2547         
2548         /* flush down, only whats 100% hidden */
2549         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
2550         for(eed= em->edges.first; eed; eed= eed->next) eed->f1= 0;
2551         
2552         if(G.scene->selectmode & SCE_SELECT_FACE) {
2553                 for(efa= em->faces.first; efa; efa= efa->next) {
2554                         if(efa->h) a= 1; else a= 2;
2555                         efa->e1->f1 |= a;
2556                         efa->e2->f1 |= a;
2557                         efa->e3->f1 |= a;
2558                         if(efa->e4) efa->e4->f1 |= a;
2559                         /* When edges are not delt with in their own loop, we need to explicitly re-selct select edges that are joined to unselected faces */
2560                         if (swap && (G.scene->selectmode == SCE_SELECT_FACE) && (efa->f & SELECT)) {
2561                                 EM_select_face(efa, 1);
2562                         }
2563                 }
2564         }
2565         
2566         if(G.scene->selectmode >= SCE_SELECT_EDGE) {
2567                 for(eed= em->edges.first; eed; eed= eed->next) {
2568                         if(eed->f1==1) eed->h |= 1;
2569                         if(eed->h & 1) a= 1; else a= 2;
2570                         eed->v1->f1 |= a;
2571                         eed->v2->f1 |= a;
2572                 }
2573         }
2574
2575         if(G.scene->selectmode >= SCE_SELECT_VERTEX) {
2576                 for(eve= em->verts.first; eve; eve= eve->next) {
2577                         if(eve->f1==1) eve->h= 1;
2578                 }
2579         }
2580         
2581         G.totedgesel= G.totfacesel= G.totvertsel= 0;
2582         allqueue(REDRAWVIEW3D, 0);
2583         if(EM_texFaceCheck())
2584                 allqueue(REDRAWIMAGE, 0);
2585         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
2586         BIF_undo_push("Hide");
2587 }
2588
2589
2590 void reveal_mesh(void)
2591 {
2592         EditMesh *em = G.editMesh;
2593         EditVert *eve;
2594         EditEdge *eed;
2595         EditFace *efa;
2596         
2597         if(G.obedit==0) return;
2598
2599         for(eve= em->verts.first; eve; eve= eve->next) {
2600                 if(eve->h) {
2601                         eve->h= 0;
2602                         eve->f |= SELECT;
2603                 }
2604         }
2605         for(eed= em->edges.first; eed; eed= eed->next) {
2606                 if(eed->h & 1) {
2607                         eed->h &= ~1;
2608                         if(G.scene->selectmode & SCE_SELECT_VERTEX); 
2609                         else EM_select_edge(eed, 1);
2610                 }
2611         }
2612         for(efa= em->faces.first; efa; efa= efa->next) {
2613                 if(efa->h) {
2614                         efa->h= 0;
2615                         if(G.scene->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_VERTEX)); 
2616                         else EM_select_face(efa, 1);
2617                 }
2618         }
2619
2620         EM_fgon_flags();        /* redo flags and indices for fgons */
2621         EM_selectmode_flush();
2622         countall();
2623
2624         allqueue(REDRAWVIEW3D, 0);
2625         if (EM_texFaceCheck())
2626                 allqueue(REDRAWIMAGE, 0);
2627         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
2628         BIF_undo_push("Reveal");
2629 }
2630
2631 void hide_tface_uv(int swap)
2632 {
2633         EditMesh *em = G.editMesh;
2634         EditFace *efa;
2635         MTFace *tface;
2636         
2637         if( is_uv_tface_editing_allowed()==0 ) return;
2638
2639         /* call the mesh function if we are in mesh sync sel */
2640         if (G.sima->flag & SI_SYNC_UVSEL) {
2641                 hide_mesh(swap);
2642                 return;
2643         }
2644         
2645         if(swap) {
2646                 for (efa= em->faces.first; efa; efa= efa->next) {
2647                         if(efa->f & SELECT) {
2648                                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2649                                 if (G.sima->flag & SI_SELACTFACE) {
2650                                         /* Pretend face mode */
2651                                         if ((   (efa->v4==NULL && 
2652                                                         (       tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) ==                     (TF_SEL1|TF_SEL2|TF_SEL3) )                      ||
2653                                                         (       tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)) ==     (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)       ) == 0) {
2654                                                 
2655                                                 if (G.scene->selectmode == SCE_SELECT_FACE) {
2656                                                         efa->f &= ~SELECT;
2657                                                         /* must re-select after */
2658                                                         efa->e1->f &= ~SELECT;
2659                                                         efa->e2->f &= ~SELECT;
2660                                                         efa->e3->f &= ~SELECT;
2661                                                         if(efa->e4) efa->e4->f &= ~SELECT;
2662                                                 } else {
2663                                                         EM_select_face(efa, 0);
2664                                                 }
2665                                         }
2666                                         tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2667                                 } else if (G.scene->selectmode == SCE_SELECT_FACE) {
2668                                         if((tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3))==0) {
2669                                                 if(!efa->v4)
2670                                                         EM_select_face(efa, 0);
2671                                                 else if(!(tface->flag & TF_SEL4))
2672                                                         EM_select_face(efa, 0);
2673                                                 tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2674                                         }
2675                                 } else {
2676                                         /* EM_deselect_flush will deselect the face */
2677                                         if((tface->flag & TF_SEL1)==0)                          efa->v1->f &= ~SELECT;
2678                                         if((tface->flag & TF_SEL2)==0)                          efa->v2->f &= ~SELECT;
2679                                         if((tface->flag & TF_SEL3)==0)                          efa->v3->f &= ~SELECT;
2680                                         if((efa->v4) && (tface->flag & TF_SEL4)==0)     efa->v4->f &= ~SELECT;                  
2681                                         tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2682                                 }
2683                         }
2684                 }
2685         } else {
2686                 for (efa= em->faces.first; efa; efa= efa->next) {
2687                         if(efa->f & SELECT) {
2688                                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2689                                 if (G.sima->flag & SI_SELACTFACE) {
2690                                         if (    (efa->v4==NULL && 
2691                                                         (       tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) ==                     (TF_SEL1|TF_SEL2|TF_SEL3) )                      ||
2692                                                         (       tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)) ==     (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)       ) {
2693                                                 
2694                                                 if (G.scene->selectmode == SCE_SELECT_FACE) {
2695                                                         efa->f &= ~SELECT;
2696                                                         /* must re-select after */
2697                                                         efa->e1->f &= ~SELECT;
2698                                                         efa->e2->f &= ~SELECT;
2699                                                         efa->e3->f &= ~SELECT;
2700                                                         if(efa->e4) efa->e4->f &= ~SELECT;
2701                                                 } else {
2702                                                         EM_select_face(efa, 0);
2703                                                 }
2704                                         }
2705                                         tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2706                                 } else if (G.scene->selectmode == SCE_SELECT_FACE) {
2707                                         if(tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
2708                                                 EM_select_face(efa, 0);
2709                                         else if(efa->v4 && tface->flag & TF_SEL4)
2710                                                 EM_select_face(efa, 0);
2711                                         tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2712                                 } else {
2713                                         /* EM_deselect_flush will deselect the face */
2714                                         if(tface->flag & TF_SEL1)                               efa->v1->f &= ~SELECT;
2715                                         if(tface->flag & TF_SEL2)                               efa->v2->f &= ~SELECT;
2716                                         if(tface->flag & TF_SEL3)                               efa->v3->f &= ~SELECT;
2717                                         if((efa->v4) && tface->flag & TF_SEL4)  efa->v4->f &= ~SELECT;
2718                                         tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2719                                 }
2720                         }
2721                 }
2722         }
2723         
2724         
2725         /*deselects too many but ok for now*/
2726         if(G.scene->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_VERTEX)) {
2727                 EM_deselect_flush();
2728         }
2729         
2730         if (G.scene->selectmode==SCE_SELECT_FACE) {
2731                 /* de-selected all edges from faces that were de-selected.
2732                  * now make sure all faces that are selected also have selected edges */
2733                 for (efa= em->faces.first; efa; efa= efa->next) {
2734                         if (efa->f & SELECT) {
2735                                 EM_select_face(efa, 1);
2736                         }
2737                 }
2738         }
2739         
2740         EM_validate_selections();
2741         
2742         BIF_undo_push("Hide UV");
2743
2744         object_tface_flags_changed(OBACT, 0);
2745 }
2746
2747 void reveal_tface_uv(void)
2748 {
2749         EditMesh *em = G.editMesh;
2750         EditFace *efa;
2751         MTFace *tface;
2752
2753         if( is_uv_tface_editing_allowed()==0 ) return;
2754         
2755         /* call the mesh function if we are in mesh sync sel */
2756         if (G.sima->flag & SI_SYNC_UVSEL) {
2757                 reveal_mesh();
2758                 return;
2759         }
2760         
2761         if (G.sima->flag & SI_SELACTFACE) {
2762                 if (G.scene->selectmode == SCE_SELECT_FACE) {
2763                         for (efa= em->faces.first; efa; efa= efa->next) {
2764                                 if (!(efa->h) && !(efa->f & SELECT)) {
2765                                         tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2766                                         EM_select_face(efa, 1);
2767                                         tface->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
2768                                 }
2769                         }
2770                 } else {
2771                         /* enable adjacent faces to have disconnected UV selections if sticky is disabled */
2772                         if (G.sima->sticky == SI_STICKY_DISABLE) {
2773                                 for (efa= em->faces.first; efa; efa= efa->next) {
2774                                         if (!(efa->h) && !(efa->f & SELECT)) {
2775                                                 /* All verts must be unselected for the face to be selected in the UV view */
2776                                                 if ((efa->v1->f&SELECT)==0 && (efa->v2->f&SELECT)==0 && (efa->v3->f&SELECT)==0 && (efa->v4==0 || (efa->v4->f&SELECT)==0)) {
2777                                                         tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2778                                                         tface->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
2779                                                         /* Cant use EM_select_face here because it unselects the verts
2780                                                          * and we cant tell if the face was totally unselected or not */
2781                                                         /*EM_select_face(efa, 1);
2782                                                          * 
2783                                                          * See Loop with EM_select_face() below... */
2784                                                         efa->f |= SELECT;
2785                                                 }
2786                                         }
2787                                 }
2788                         } else {
2789                                 for (efa= em->faces.first; efa; efa= efa->next) {
2790                                         if (!(efa->h) && !(efa->f & SELECT)) {
2791                                                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2792                                                 if ((efa->v1->f & SELECT)==0)                           {tface->flag |= TF_SEL1;}
2793                                                 if ((efa->v2->f & SELECT)==0)                           {tface->flag |= TF_SEL2;}
2794                                                 if ((efa->v3->f & SELECT)==0)                           {tface->flag |= TF_SEL3;}
2795                                                 if ((efa->v4 && (efa->v4->f & SELECT)==0))      {tface->flag |= TF_SEL4;}
2796                                                 efa->f |= SELECT;
2797                                         }
2798                                 }
2799                         }
2800                         
2801                         /* Select all edges and verts now */
2802                         for (efa= em->faces.first; efa; efa= efa->next) {
2803                                 /* we only selected the face flags, and didnt changes edges or verts, fix this now */
2804                                 if (!(efa->h) && (efa->f & SELECT)) {
2805                                         EM_select_face(efa, 1);
2806                                 }
2807                         }
2808                         EM_select_flush();
2809                 }
2810         } else if (G.scene->selectmode == SCE_SELECT_FACE) {
2811                 for (efa= em->faces.first; efa; efa= efa->next) {
2812                         if (!(efa->h) && !(efa->f & SELECT)) {
2813                                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2814                                 efa->f |= SELECT;
2815                                 tface->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
2816                         }
2817                 }
2818                 
2819                 /* Select all edges and verts now */
2820                 for (efa= em->faces.first; efa; efa= efa->next) {
2821                         /* we only selected the face flags, and didnt changes edges or verts, fix this now */
2822                         if (!(efa->h) && (efa->f & SELECT)) {
2823                                 EM_select_face(efa, 1);
2824                         }
2825                 }
2826                 
2827         } else {
2828                 for (efa= em->faces.first; efa; efa= efa->next) {
2829                         if (!(efa->h) && !(efa->f & SELECT)) {
2830                                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2831                                 if ((efa->v1->f & SELECT)==0)                           {tface->flag |= TF_SEL1;}
2832                                 if ((efa->v2->f & SELECT)==0)                           {tface->flag |= TF_SEL2;}
2833                                 if ((efa->v3->f & SELECT)==0)                           {tface->flag |= TF_SEL3;}
2834                                 if ((efa->v4 && (efa->v4->f & SELECT)==0))      {tface->flag |= TF_SEL4;}
2835                                 efa->f |= SELECT;
2836                         }
2837                 }
2838                 
2839                 /* Select all edges and verts now */
2840                 for (efa= em->faces.first; efa; efa= efa->next) {
2841                         /* we only selected the face flags, and didnt changes edges or verts, fix this now */
2842                         if (!(efa->h) && (efa->f & SELECT)) {
2843                                 EM_select_face(efa, 1);
2844                         }
2845                 }
2846         }
2847         
2848         BIF_undo_push("Reveal UV");
2849         
2850         object_tface_flags_changed(OBACT, 0);
2851 }
2852
2853 void select_faces_by_numverts(int numverts)
2854 {
2855         EditMesh *em = G.editMesh;
2856         EditFace *efa;
2857
2858         /* Selects trias/qiads or isolated verts, and edges that do not have 2 neighboring
2859          * faces
2860          */
2861
2862         /* for loose vertices/edges, we first select all, loop below will deselect */
2863         if(numverts==5)
2864                 EM_set_flag_all(SELECT);
2865         else if(G.scene->selectmode!=SCE_SELECT_FACE) {
2866                 error("Only works in face selection mode");
2867                 return;
2868         }
2869         
2870         for(efa= em->faces.first; efa; efa= efa->next) {
2871                 if (efa->e4) {
2872                         EM_select_face(efa, (numverts==4) );
2873                 }
2874                 else {
2875                         EM_select_face(efa, (numverts==3) );
2876                 }
2877         }
2878
2879         countall();
2880         addqueue(curarea->win,  REDRAW, 0);
2881         if (EM_texFaceCheck())
2882                 allqueue(REDRAWIMAGE, 0);
2883         
2884         if (numverts==3)
2885                 BIF_undo_push("Select Triangles");
2886         else&nb