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