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