In Vertex Select Mode
[blender.git] / source / blender / src / editmesh_mods.c
1 /**
2  * $Id: 
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2004 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 /*
34
35 editmesh_mods.c, UI level access, no geometry changes 
36
37 */
38
39 #include <stdlib.h>
40 #include <string.h>
41 #include <math.h>
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 #include "MEM_guardedalloc.h"
48
49 #include "MTC_matrixops.h"
50
51 #include "DNA_mesh_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_meshdata_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_texture_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_view3d_types.h"
59
60 #include "BLI_blenlib.h"
61 #include "BLI_arithb.h"
62 #include "BLI_editVert.h"
63 #include "BLI_rand.h"
64
65 #include "BKE_displist.h"
66 #include "BKE_depsgraph.h"
67 #include "BKE_DerivedMesh.h"
68 #include "BKE_global.h"
69 #include "BKE_mesh.h"
70 #include "BKE_material.h"
71 #include "BKE_texture.h"
72 #include "BKE_utildefines.h"
73
74 #include "BIF_editmesh.h"
75 #include "BIF_resources.h"
76 #include "BIF_gl.h"
77 #include "BIF_glutil.h"
78 #include "BIF_graphics.h"
79 #include "BIF_interface.h"
80 #include "BIF_mywindow.h"
81 #include "BIF_resources.h"
82 #include "BIF_screen.h"
83 #include "BIF_space.h"
84 #include "BIF_toolbox.h"
85
86 #include "BDR_drawobject.h"
87 #include "BDR_editobject.h"
88
89 #include "BSE_drawview.h"
90 #include "BSE_edit.h"
91 #include "BSE_view.h"
92
93 #include "IMB_imbuf.h"
94
95 #include "mydevice.h"
96 #include "blendef.h"
97 #include "render.h"  // externtex
98
99 #include "editmesh.h"
100
101
102 /* ****************************** SELECTION ROUTINES **************** */
103
104 unsigned int em_solidoffs=0, em_wireoffs=0, em_vertoffs=0;      // set in drawobject.c ... for colorindices
105
106 static void check_backbuf(void)
107 {
108         if(G.vd->flag & V3D_NEEDBACKBUFDRAW) {
109                 backdrawview3d(0);
110         }
111 }
112
113 /* samples a single pixel (copied from vpaint) */
114 static unsigned int sample_backbuf(int x, int y)
115 {
116         unsigned int col;
117         
118         if(x>=curarea->winx || y>=curarea->winy) return 0;
119         x+= curarea->winrct.xmin;
120         y+= curarea->winrct.ymin;
121         
122         check_backbuf(); // actually not needed for apple
123
124 #ifdef __APPLE__
125         glReadBuffer(GL_AUX0);
126 #endif
127         glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
128         glReadBuffer(GL_BACK);  
129         
130         if(G.order==B_ENDIAN) SWITCH_INT(col);
131         
132         return framebuffer_to_index(col);
133 }
134
135 /* reads full rect, converts indices */
136 static unsigned int *read_backbuf(short xmin, short ymin, short xmax, short ymax)
137 {
138         unsigned int *dr, *buf;
139         int a;
140         short xminc, yminc, xmaxc, ymaxc;
141         
142         /* clip */
143         if(xmin<0) xminc= 0; else xminc= xmin;
144         if(xmax>=curarea->winx) xmaxc= curarea->winx-1; else xmaxc= xmax;
145         if(xminc > xmaxc) return NULL;
146
147         if(ymin<0) yminc= 0; else yminc= ymin;
148         if(ymax>=curarea->winy) ymaxc= curarea->winy-1; else ymaxc= ymax;
149         if(yminc > ymaxc) return NULL;
150         
151         buf= MEM_mallocN( (xmaxc-xminc+1)*(ymaxc-yminc+1)*sizeof(int), "sample rect");
152
153         check_backbuf(); // actually not needed for apple
154         
155 #ifdef __APPLE__
156         glReadBuffer(GL_AUX0);
157 #endif
158         glReadPixels(curarea->winrct.xmin+xminc, curarea->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, buf);
159         glReadBuffer(GL_BACK);  
160
161         if(G.order==B_ENDIAN) IMB_convert_rgba_to_abgr((xmaxc-xminc+1)*(ymaxc-yminc+1), buf);
162
163         a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
164         dr= buf;
165         while(a--) {
166                 if(*dr) *dr= framebuffer_to_index(*dr);
167                 dr++;
168         }
169         
170         /* put clipped result back, if needed */
171         if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax) return buf;
172         else {
173                 unsigned int *buf1= MEM_callocN( (xmax-xmin+1)*(ymax-ymin+1)*sizeof(int), "sample rect2");
174                 unsigned int *rd;
175                 short xs, ys;
176
177                 rd= buf;
178                 dr= buf1;
179                 
180                 for(ys= ymin; ys<=ymax; ys++) {
181                         for(xs= xmin; xs<=xmax; xs++, dr++) {
182                                 if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
183                                         *dr= *rd;
184                                         rd++;
185                                 }
186                         }
187                 }
188                 MEM_freeN(buf);
189                 return buf1;
190         }
191         
192         return buf;
193 }
194
195
196 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
197 static unsigned int sample_backbuf_rect(unsigned int *buf, int size, unsigned int min, unsigned int max, short *dist)
198 {
199         unsigned int *bufmin, *bufmax;
200         int a, b, rc, nr, amount, dirvec[4][2];
201         short distance=0;
202         
203         amount= (size-1)/2;
204         rc= 0;
205         
206         dirvec[0][0]= 1; dirvec[0][1]= 0;
207         dirvec[1][0]= 0; dirvec[1][1]= -size;
208         dirvec[2][0]= -1; dirvec[2][1]= 0;
209         dirvec[3][0]= 0; dirvec[3][1]= size;
210         
211         bufmin= buf;
212         bufmax= buf+ size*size;
213         buf+= amount*size+ amount;
214         
215         for(nr=1; nr<=size; nr++) {
216                 
217                 for(a=0; a<2; a++) {
218                         for(b=0; b<nr; b++, distance++) {
219                                 
220                                 if(*buf && *buf>=min && *buf<max ) {
221                                         *dist= (short) sqrt( (float)distance );
222                                         return *buf - min+1;    // messy yah, but indices start at 1
223                                 }
224                                 
225                                 buf+= (dirvec[rc][0]+dirvec[rc][1]);
226                                 
227                                 if(buf<bufmin || buf>=bufmax) {
228                                         return 0;
229                                 }
230                         }
231                         rc++;
232                         rc &= 3;
233                 }
234         }
235         return 0;
236 }
237
238 /* facilities for border select and circle select */
239 static char *selbuf= NULL;
240
241 /* opengl doesn't support concave... */
242 static void draw_triangulated(short mcords[][2], short tot)
243 {
244         ListBase lb={NULL, NULL};
245         DispList *dl;
246         float *fp;
247         int a;
248         
249         /* make displist */
250         dl= MEM_callocN(sizeof(DispList), "poly disp");
251         dl->type= DL_POLY;
252         dl->parts= 1;
253         dl->nr= tot;
254         dl->verts= fp=  MEM_callocN(tot*3*sizeof(float), "poly verts");
255         BLI_addtail(&lb, dl);
256         
257         for(a=0; a<tot; a++, fp+=3) {
258                 fp[0]= (float)mcords[a][0];
259                 fp[1]= (float)mcords[a][1];
260         }
261         
262         /* do the fill */
263         filldisplist(&lb, &lb);
264
265         /* do the draw */
266         dl= lb.first;   // filldisplist adds in head of list
267         if(dl->type==DL_INDEX3) {
268                 int *index;
269                 
270                 a= dl->parts;
271                 fp= dl->verts;
272                 index= dl->index;
273                 glBegin(GL_TRIANGLES);
274                 while(a--) {
275                         glVertex3fv(fp+3*index[0]);
276                         glVertex3fv(fp+3*index[1]);
277                         glVertex3fv(fp+3*index[2]);
278                         index+= 3;
279                 }
280                 glEnd();
281         }
282         
283         freedisplist(&lb);
284 }
285
286
287 /* reads rect, and builds selection array for quick lookup */
288 /* returns if all is OK */
289 int EM_init_backbuf_border(short xmin, short ymin, short xmax, short ymax)
290 {
291         unsigned int *buf, *dr;
292         int a;
293         
294         if(G.obedit==NULL || G.vd->drawtype<OB_SOLID || (G.vd->flag & V3D_ZBUF_SELECT)==0) return 0;
295         if(em_vertoffs==0) return 0;
296         
297         dr= buf= read_backbuf(xmin, ymin, xmax, ymax);
298         if(buf==NULL) return 0;
299         
300         /* build selection lookup */
301         selbuf= MEM_callocN(em_vertoffs+1, "selbuf");
302         
303         a= (xmax-xmin+1)*(ymax-ymin+1);
304         while(a--) {
305                 if(*dr>0 && *dr<=em_vertoffs) selbuf[*dr]= 1;
306                 dr++;
307         }
308         MEM_freeN(buf);
309         return 1;
310 }
311
312 int EM_check_backbuf_border(unsigned int index)
313 {
314         if(selbuf==NULL) return 1;
315         if(index>0 && index<=em_vertoffs)
316                 return selbuf[index];
317         return 0;
318 }
319
320 void EM_free_backbuf_border(void)
321 {
322         if(selbuf) MEM_freeN(selbuf);
323         selbuf= NULL;
324 }
325
326 /* mcords is a polygon mask
327    - grab backbuffer,
328    - draw with black in backbuffer, 
329    - grab again and compare
330    returns 'OK' 
331 */
332 int EM_mask_init_backbuf_border(short mcords[][2], short tot, short xmin, short ymin, short xmax, short ymax)
333 {
334         unsigned int *buf, *bufmask, *dr, *drm;
335         int a;
336         
337         /* method in use for face selecting too */
338         if(G.obedit==NULL) {
339                 if(G.f & G_FACESELECT);
340                 else return 0;
341         }
342         else if(G.vd->drawtype<OB_SOLID || (G.vd->flag & V3D_ZBUF_SELECT)==0) return 0;
343
344         if(em_vertoffs==0) return 0;
345         
346         dr= buf= read_backbuf(xmin, ymin, xmax, ymax);
347         if(buf==NULL) return 0;
348
349         /* draw the mask */
350 #ifdef __APPLE__
351         glDrawBuffer(GL_AUX0);
352 #endif
353         glDisable(GL_DEPTH_TEST);
354         
355         persp(PERSP_WIN);
356         glColor3ub(0, 0, 0);
357         
358         /* yah, opengl doesn't do concave... tsk! */
359         draw_triangulated(mcords, tot); 
360         
361         glBegin(GL_LINE_LOOP);  // for zero sized masks, lines
362         for(a=0; a<tot; a++) glVertex2s(mcords[a][0], mcords[a][1]);
363         glEnd();
364         
365         persp(PERSP_VIEW);
366         glFinish();     // to be sure readpixels sees mask
367         
368         glDrawBuffer(GL_BACK);
369         
370         /* grab mask */
371         drm= bufmask= read_backbuf(xmin, ymin, xmax, ymax);
372         if(bufmask==NULL) return 0; // only when mem alloc fails, go crash somewhere else!
373         
374         /* build selection lookup */
375         selbuf= MEM_callocN(em_vertoffs+1, "selbuf");
376         
377         a= (xmax-xmin+1)*(ymax-ymin+1);
378         while(a--) {
379                 if(*dr>0 && *dr<=em_vertoffs && *drm==0) selbuf[*dr]= 1;
380                 dr++; drm++;
381         }
382         MEM_freeN(buf);
383         MEM_freeN(bufmask);
384         return 1;
385         
386 }
387
388 /* circle shaped sample area */
389 int EM_init_backbuf_circle(short xs, short ys, short rads)
390 {
391         unsigned int *buf, *dr;
392         short xmin, ymin, xmax, ymax, xc, yc;
393         int radsq;
394         
395         if(G.obedit==NULL || G.vd->drawtype<OB_SOLID || (G.vd->flag & V3D_ZBUF_SELECT)==0) return 0;
396         if(em_vertoffs==0) return 0;
397         
398         xmin= xs-rads; xmax= xs+rads;
399         ymin= ys-rads; ymax= ys+rads;
400         dr= buf= read_backbuf(xmin, ymin, xmax, ymax);
401         if(buf==NULL) return 0;
402         
403         /* build selection lookup */
404         selbuf= MEM_callocN(em_vertoffs+1, "selbuf");
405         radsq= rads*rads;
406         for(yc= -rads; yc<=rads; yc++) {
407                 for(xc= -rads; xc<=rads; xc++, dr++) {
408                         if(xc*xc + yc*yc < radsq) {
409                                 if(*dr>0 && *dr<=em_vertoffs) selbuf[*dr]= 1;
410                         }
411                 }
412         }
413
414         MEM_freeN(buf);
415         return 1;
416         
417 }
418
419
420 static EditVert *findnearestvert_f(short *dist, short sel)
421 {
422         static EditVert *acto= NULL;
423         EditMesh *em = G.editMesh;
424         /* if sel==1 the vertices with flag==1 get a disadvantage */
425         EditVert *eve,*act=NULL;
426         short temp, mval[2];
427
428         if(em->verts.first==NULL) return NULL;
429
430         /* do projection */
431         calc_meshverts_ext();   /* drawobject.c */
432         
433         /* we count from acto->next to last, and from first to acto */
434         /* does acto exist? */
435         eve= em->verts.first;
436         while(eve) {
437                 if(eve==acto) break;
438                 eve= eve->next;
439         }
440         if(eve==NULL) acto= em->verts.first;
441
442         /* is there an indicated vertex? part 1 */
443         getmouseco_areawin(mval);
444         eve= acto->next;
445         while(eve) {
446                 if(eve->h==0 && eve->xs!=3200) {
447                         temp= abs(mval[0]- eve->xs)+ abs(mval[1]- eve->ys);
448                         if( (eve->f & 1)==sel ) temp+=5;
449                         if(temp< *dist) {
450                                 act= eve;
451                                 *dist= temp;
452                                 if(*dist<4) break;
453                         }
454                 }
455                 eve= eve->next;
456         }
457         /* is there an indicated vertex? part 2 */
458         if(*dist>3) {
459                 eve= em->verts.first;
460                 while(eve) {
461                         if(eve->h==0 && eve->xs!=3200) {
462                                 temp= abs(mval[0]- eve->xs)+ abs(mval[1]- eve->ys);
463                                 if( (eve->f & 1)==sel ) temp+=5;
464                                 if(temp< *dist) {
465                                         act= eve;
466                                         if(temp<4) break;
467                                         *dist= temp;
468                                 }
469                                 if(eve== acto) break;
470                         }
471                         eve= eve->next;
472                 }
473         }
474
475         acto= act;
476         return act;
477 }
478
479 /* backbuffer version */
480 static EditVert *findnearestvert(short *dist, short sel)
481 {
482         if(G.vd->drawtype>OB_WIRE && (G.vd->flag & V3D_ZBUF_SELECT)) {
483                 EditVert *eve=NULL;
484                 unsigned int *buf;
485                 int a=1, index;
486                 short mval[2], distance=255;
487                 
488                 getmouseco_areawin(mval);
489                 
490                 // sample colorcode 
491                 buf= read_backbuf(mval[0]-25, mval[1]-25, mval[0]+24, mval[1]+24);
492                 if(buf) {
493                         index= sample_backbuf_rect(buf, 50, em_wireoffs, 0xFFFFFF, &distance); // globals, set in drawobject.c
494                         MEM_freeN(buf);
495                         if(distance < *dist) {
496                                 if(index>0) for(eve= G.editMesh->verts.first; eve; eve= eve->next, a++) if(index==a) break;
497                                 if(eve) *dist= distance;
498                         }
499                 }
500                 return eve;
501         }
502         else return findnearestvert_f(dist, sel);
503 }
504
505 /* helper for findnearest edge */
506 static float dist_mval_edge(short *mval, EditEdge *eed)
507 {
508         float v1[2], v2[2], mval2[2];
509
510         mval2[0] = (float)mval[0];
511         mval2[1] = (float)mval[1];
512         
513         v1[0] = eed->v1->xs;
514         v1[1] = eed->v1->ys;
515         v2[0] = eed->v2->xs;
516         v2[1] = eed->v2->ys;
517         
518         return PdistVL2Dfl(mval2, v1, v2);
519 }
520
521 static EditEdge *findnearestedge_f(short *dist)
522 {
523         EditMesh *em = G.editMesh;
524         EditEdge *closest, *eed;
525         EditVert *eve;
526         short mval[2], distance;
527         
528         if(em->edges.first==NULL) return NULL;
529         else eed= em->edges.first;      
530         
531         calc_meshverts_ext_f2();        /* sets/clears (eve->f & 2) for vertices that aren't visible */
532
533         getmouseco_areawin(mval);
534         closest=NULL;
535         
536         /*compare the distance to the rest of the edges and find the closest one*/
537         eed=em->edges.first;
538         while(eed) {
539                 /* Are both vertices of the edge ofscreen or either of them hidden? then don't select the edge*/
540                 if( !((eed->v1->f & 2) && (eed->v2->f & 2)) && eed->h==0){
541                         
542                         distance= dist_mval_edge(mval, eed);
543                         if(eed->f & SELECT) distance+=5;
544                         if(distance < *dist) {
545                                 *dist= distance;
546                                 closest= eed;
547                         }
548                 }
549                 eed= eed->next;
550         }
551         
552         /* reset flags */       
553         for(eve=em->verts.first; eve; eve=eve->next){
554                 eve->f &= ~2;
555         }
556         
557         return closest;
558 }
559
560 /* backbuffer version */
561 EditEdge *findnearestedge(short *dist)
562 {
563         if(G.vd->drawtype>OB_WIRE && (G.vd->flag & V3D_ZBUF_SELECT)) {
564                 EditEdge *eed=NULL;
565                 unsigned int *buf, index;
566                 int a=1;
567                 short mval[2], distance=255;
568                 
569                 getmouseco_areawin(mval);
570                 // sample colorcode 
571
572                 buf= read_backbuf(mval[0]-25, mval[1]-25, mval[0]+24, mval[1]+24);
573                 if(buf) {
574                         index= sample_backbuf_rect(buf, 50, em_solidoffs, em_wireoffs, &distance); // global, set in drawobject.c
575                         MEM_freeN(buf);
576                         if(distance < *dist) {
577                                 if(index>0 && index<=em_wireoffs-em_solidoffs) {
578                                         for(eed= G.editMesh->edges.first; eed; eed= eed->next, a++) 
579                                                 if(index==a) break;
580                                 }
581                                 if(eed) *dist= distance;
582                         }
583                 }
584                 
585                 return eed;
586         }
587         else return findnearestedge_f(dist);
588 }
589
590
591 static EditFace *findnearestface_f(short *dist)
592 {
593         static EditFace *acto= NULL;
594         EditMesh *em = G.editMesh;
595         /* if selected the faces with flag==1 get a disadvantage */
596         EditFace *efa, *act=NULL;
597         short temp, mval[2];
598
599         if(em->faces.first==NULL) return NULL;
600
601         /* do projection */
602         calc_mesh_facedots_ext();
603         
604         /* we count from acto->next to last, and from first to acto */
605         /* does acto exist? */
606         efa= em->faces.first;
607         while(efa) {
608                 if(efa==acto) break;
609                 efa= efa->next;
610         }
611         if(efa==NULL) acto= em->faces.first;
612
613         /* is there an indicated face? part 1 */
614         getmouseco_areawin(mval);
615         efa= acto->next;
616         while(efa) {
617                 if(efa->h==0 && efa->fgonf!=EM_FGON) {
618                         temp= abs(mval[0]- efa->xs)+ abs(mval[1]- efa->ys);
619                         if(temp< *dist) {
620                                 act= efa;
621                                 *dist= temp;
622                         }
623                 }
624                 efa= efa->next;
625         }
626         /* is there an indicated face? part 2 */
627         if(*dist>3) {
628                 efa= em->faces.first;
629                 while(efa) {
630                         if(efa->h==0 && efa->fgonf!=EM_FGON) {
631                                 temp= abs(mval[0]- efa->xs)+ abs(mval[1]- efa->ys);
632                                 if(temp< *dist) {
633                                         act= efa;
634                                         *dist= temp;
635                                 }
636                                 if(efa== acto) break;
637                         }
638                         efa= efa->next;
639                 }
640         }
641
642         acto= act;
643         return act;
644 }
645
646 static EditFace *findnearestface(short *dist)
647 {
648         if(G.vd->drawtype>OB_WIRE && (G.vd->flag & V3D_ZBUF_SELECT)) {
649                 EditFace *efa=NULL;
650                 int a=1;
651                 unsigned int index;
652                 short mval[2], distance;
653
654                 calc_mesh_facedots_ext();       // shouldnt be needed each click
655                 getmouseco_areawin(mval);
656
657                 // sample colorcode 
658                 index= sample_backbuf(mval[0], mval[1]);
659                 
660                 if(index && index<=em_solidoffs) {
661                         for(efa= G.editMesh->faces.first; efa; efa= efa->next, a++) if(index==a) break;
662                         if(efa) {
663                                 distance= abs(mval[0]- efa->xs)+ abs(mval[1]- efa->ys);
664
665                                 if(G.scene->selectmode == SCE_SELECT_FACE || distance<*dist) {  // only faces, no dist check
666                                         *dist= distance;
667                                         return efa;
668                                 }
669                         }
670                 }
671                 
672                 return NULL;
673         }
674         else return findnearestface_f(dist);
675 }
676
677 /* for interactivity, frontbuffer draw in current window */
678 static void unified_select_draw(EditVert *eve, EditEdge *eed, EditFace *efa)
679 {
680         int dmNeedsFree;
681         DerivedMesh *dm = editmesh_get_derived_cage(&dmNeedsFree);
682
683         glDrawBuffer(GL_FRONT);
684
685         persp(PERSP_VIEW);
686         glPushMatrix();
687         mymultmatrix(G.obedit->obmat);
688         
689         /* face selected */
690         if(efa) {
691                 if(G.scene->selectmode & SCE_SELECT_VERTEX) {
692                         glPointSize(BIF_GetThemeValuef(TH_VERTEX_SIZE));
693                         
694                         if(efa->f & SELECT) BIF_ThemeColor(TH_VERTEX_SELECT);
695                         else BIF_ThemeColor(TH_VERTEX);
696                         
697                         bglBegin(GL_POINTS);
698                         bglVertex3fv(efa->v1->co);
699                         bglVertex3fv(efa->v2->co);
700                         bglVertex3fv(efa->v3->co);
701                         if(efa->v4) bglVertex3fv(efa->v4->co);
702                         bglEnd();
703                 }
704
705                 if(G.scene->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_FACE)) {
706                         if(efa->fgonf==0) {
707                                 BIF_ThemeColor((efa->f & SELECT)?TH_EDGE_SELECT:TH_WIRE);
708         
709                                 dm->drawMappedEdgeEM(dm, efa->e1);
710                                 dm->drawMappedEdgeEM(dm, efa->e2);
711                                 dm->drawMappedEdgeEM(dm, efa->e3);
712                                 if (efa->e4) {
713                                         dm->drawMappedEdgeEM(dm, efa->e4);
714                                 }
715                         }
716                 }
717                 
718                 if(G.scene->selectmode & SCE_SELECT_FACE) {
719                         if(efa->fgonf==0) {
720                                 glPointSize(BIF_GetThemeValuef(TH_FACEDOT_SIZE));
721                                 BIF_ThemeColor((efa->f & SELECT)?TH_FACE_DOT:TH_WIRE);
722
723                                 bglBegin(GL_POINTS);
724                                 bglVertex3fv(efa->cent);
725                                 bglEnd();
726                         }
727                 }
728         }
729         /* edge selected */
730         if(eed) {
731                 if(G.scene->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_FACE)) {
732                         BIF_ThemeColor((eed->f & SELECT)?TH_EDGE_SELECT:TH_WIRE);
733
734                         dm->drawMappedEdgeEM(dm, eed);
735                 }
736                 if(G.scene->selectmode & SCE_SELECT_VERTEX) {
737                         float co[3];
738                         glPointSize(BIF_GetThemeValuef(TH_VERTEX_SIZE));
739                         
740                         BIF_ThemeColor((eed->f & SELECT)?TH_VERTEX_SELECT:TH_VERTEX);
741                         
742                         bglBegin(GL_POINTS);
743                         dm->getMappedVertCoEM(dm, eed->v1, co);
744                         bglVertex3fv(co);
745
746                         dm->getMappedVertCoEM(dm, eed->v2, co);
747                         bglVertex3fv(co);
748                         bglEnd();
749                 }
750         }
751         if(eve) {
752                 if(G.scene->selectmode & SCE_SELECT_VERTEX) {
753                         float co[3];
754                         glPointSize(BIF_GetThemeValuef(TH_VERTEX_SIZE));
755                         
756                         BIF_ThemeColor((eve->f & SELECT)?TH_VERTEX_SELECT:TH_VERTEX);
757                         
758                         bglBegin(GL_POINTS);
759                         dm->getMappedVertCoEM(dm, eve, co);
760                         bglVertex3fv(co);
761                         bglEnd();
762                 }
763         }
764
765         glPointSize(1.0);
766         glPopMatrix();
767
768         glFlush();
769         glDrawBuffer(GL_BACK);
770
771         /* signal that frontbuf differs from back */
772         curarea->win_swap= WIN_FRONT_OK;
773
774         if (dmNeedsFree) {
775                 dm->release(dm);
776         }
777 }
778
779
780 /* best distance based on screen coords. 
781    use g.scene->selectmode to define how to use 
782    selected vertices and edges get disadvantage
783    return 1 if found one
784 */
785 static int unified_findnearest(EditVert **eve, EditEdge **eed, EditFace **efa) 
786 {
787         short dist= 75;
788         
789         *eve= NULL;
790         *eed= NULL;
791         *efa= NULL;
792         
793         if(G.scene->selectmode & SCE_SELECT_VERTEX)
794                 *eve= findnearestvert(&dist, SELECT);
795         if(G.scene->selectmode & SCE_SELECT_FACE)
796                 *efa= findnearestface(&dist);
797
798         dist-= 20;      // since edges select lines, we give dots advantage of 20 pix
799         if(G.scene->selectmode & SCE_SELECT_EDGE)
800                 *eed= findnearestedge(&dist);
801
802         /* return only one of 3 pointers, for frontbuffer redraws */
803         if(*eed) {
804                 *efa= NULL; *eve= NULL;
805         }
806         else if(*efa) {
807                 *eve= NULL;
808         }
809         
810         return (*eve || *eed || *efa);
811 }
812
813 /* ****************  LOOP SELECTS *************** */
814
815 /* selects quads in loop direction of indicated edge */
816 /* only flush over edges with valence <= 2 */
817 static void faceloop_select(EditEdge *startedge, int select)
818 {
819         EditMesh *em = G.editMesh;
820         EditEdge *eed;
821         EditFace *efa;
822         int looking= 1;
823         
824         /* in eed->f1 we put the valence (amount of faces in edge) */
825         /* in eed->f2 we put tagged flag as correct loop */
826         /* in efa->f1 we put tagged flag as correct to select */
827
828         for(eed= em->edges.first; eed; eed= eed->next) {
829                 eed->f1= 0;
830                 eed->f2= 0;
831         }
832         for(efa= em->faces.first; efa; efa= efa->next) {
833                 efa->f1= 0;
834                 if(efa->h==0) {
835                         efa->e1->f1++;
836                         efa->e2->f1++;
837                         efa->e3->f1++;
838                         if(efa->e4) efa->e4->f1++;
839                 }
840         }
841         
842         // tag startedge OK
843         startedge->f2= 1;
844         
845         while(looking) {
846                 looking= 0;
847                 
848                 for(efa= em->faces.first; efa; efa= efa->next) {
849                         if(efa->e4 && efa->f1==0) {     // not done quad
850                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
851
852                                         // if edge tagged, select opposing edge and mark face ok
853                                         if(efa->e1->f2) {
854                                                 efa->e3->f2= 1;
855                                                 efa->f1= 1;
856                                                 looking= 1;
857                                         }
858                                         else if(efa->e2->f2) {
859                                                 efa->e4->f2= 1;
860                                                 efa->f1= 1;
861                                                 looking= 1;
862                                         }
863                                         if(efa->e3->f2) {
864                                                 efa->e1->f2= 1;
865                                                 efa->f1= 1;
866                                                 looking= 1;
867                                         }
868                                         if(efa->e4->f2) {
869                                                 efa->e2->f2= 1;
870                                                 efa->f1= 1;
871                                                 looking= 1;
872                                         }
873                                 }
874                         }
875                 }
876         }
877         
878         /* (de)select the faces */
879         for(efa= em->faces.first; efa; efa= efa->next) {
880                 if(efa->f1) EM_select_face(efa, select);
881         }
882 }
883
884
885 /* helper for edgeloop_select, checks for eed->f2 tag in faces */
886 static int edge_not_in_tagged_face(EditEdge *eed)
887 {
888         EditMesh *em = G.editMesh;
889         EditFace *efa;
890         
891         for(efa= em->faces.first; efa; efa= efa->next) {
892                 if(efa->h==0) {
893                         if(efa->e1==eed || efa->e2==eed || efa->e3==eed || efa->e4==eed) {      // edge is in face
894                                 if(efa->e1->f2 || efa->e2->f2 || efa->e3->f2 || (efa->e4 && efa->e4->f2)) {     // face is tagged
895                                         return 0;
896                                 }
897                         }
898                 }
899         }
900         return 1;
901 }
902
903 /* selects or deselects edges that:
904 - if edges has 2 faces:
905         - has vertices with valence of 4
906         - not shares face with previous edge
907 - if edge has 1 face:
908         - has vertices with valence 4
909         - not shares face with previous edge
910         - but also only 1 face
911 - if edge no face:
912         - has vertices with valence 2
913 */
914 static void edgeloop_select(EditEdge *starteed, int select)
915 {
916         EditMesh *em = G.editMesh;
917         EditVert *eve;
918         EditEdge *eed;
919         EditFace *efa;
920         int looking= 1;
921         
922         /* in f1 we put the valence (amount of edges in a vertex, or faces in edge) */
923         /* in eed->f2 and efa->f1 we put tagged flag as correct loop */
924         for(eve= em->verts.first; eve; eve= eve->next) {
925                 eve->f1= 0;
926                 eve->f2= 0;
927         }
928         for(eed= em->edges.first; eed; eed= eed->next) {
929                 eed->f1= 0;
930                 eed->f2= 0;
931                 if((eed->h & 1)==0) {   // fgon edges add to valence too
932                         eed->v1->f1++; eed->v2->f1++;
933                 }
934         }
935         for(efa= em->faces.first; efa; efa= efa->next) {
936                 efa->f1= 0;
937                 if(efa->h==0) {
938                         efa->e1->f1++;
939                         efa->e2->f1++;
940                         efa->e3->f1++;
941                         if(efa->e4) efa->e4->f1++;
942                 }
943         }
944         
945         /* looped edges & vertices get tagged f2 */
946         starteed->f2= 1;
947         if(starteed->v1->f1<5) starteed->v1->f2= 1;
948         if(starteed->v2->f1<5) starteed->v2->f2= 1;
949         /* sorry, first edge isnt even ok */
950         if(starteed->v1->f2==0 && starteed->v2->f2==0) looking= 0;
951         
952         while(looking) {
953                 looking= 0;
954                 
955                 /* find correct valence edges which are not tagged yet, but connect to tagged one */
956                 for(eed= em->edges.first; eed; eed= eed->next) {
957                         if(eed->h==0 && eed->f2==0) { // edge not hidden, not tagged
958                                 if( (eed->v1->f1<5 && eed->v1->f2) || (eed->v2->f1<5 && eed->v2->f2)) { // valence of vertex OK, and is tagged
959                                         /* new edge is not allowed to be in face with tagged edge */
960                                         if(edge_not_in_tagged_face(eed)) {
961                                                 if(eed->f1==starteed->f1) {     // same amount of faces
962                                                         looking= 1;
963                                                         eed->f2= 1;
964                                                         if(eed->v2->f1<5) eed->v2->f2= 1;
965                                                         if(eed->v1->f1<5) eed->v1->f2= 1;
966                                                 }
967                                         }
968                                 }
969                         }
970                 }
971         }
972         /* and we do the select */
973         for(eed= em->edges.first; eed; eed= eed->next) {
974                 if(eed->f2) EM_select_edge(eed, select);
975         }
976 }
977
978 /* 
979    Almostly exactly the same code as faceloop select
980 */
981 static void edgering_select(EditEdge *startedge, int select){
982         EditMesh *em = G.editMesh;
983         EditEdge *eed;
984         EditFace *efa;
985         int looking= 1;
986         
987         /* in eed->f1 we put the valence (amount of faces in edge) */
988         /* in eed->f2 we put tagged flag as correct loop */
989         /* in efa->f1 we put tagged flag as correct to select */
990
991         for(eed= em->edges.first; eed; eed= eed->next) {
992                 eed->f1= 0;
993                 eed->f2= 0;
994         }
995         for(efa= em->faces.first; efa; efa= efa->next) {
996                 efa->f1= 0;
997                 if(efa->h==0) {
998                         efa->e1->f1++;
999                         efa->e2->f1++;
1000                         efa->e3->f1++;
1001                         if(efa->e4) efa->e4->f1++;
1002                 }
1003         }
1004         
1005         // tag startedge OK
1006         startedge->f2= 1;
1007         
1008         while(looking) {
1009                 looking= 0;
1010                 
1011                 for(efa= em->faces.first; efa; efa= efa->next) {
1012                         if(efa->e4 && efa->f1==0) {     // not done quad
1013                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
1014
1015                                         // if edge tagged, select opposing edge and mark face ok
1016                                         if(efa->e1->f2) {
1017                                                 efa->e3->f2= 1;
1018                                                 efa->f1= 1;
1019                                                 looking= 1;
1020                                         }
1021                                         else if(efa->e2->f2) {
1022                                                 efa->e4->f2= 1;
1023                                                 efa->f1= 1;
1024                                                 looking= 1;
1025                                         }
1026                                         if(efa->e3->f2) {
1027                                                 efa->e1->f2= 1;
1028                                                 efa->f1= 1;
1029                                                 looking= 1;
1030                                         }
1031                                         if(efa->e4->f2) {
1032                                                 efa->e2->f2= 1;
1033                                                 efa->f1= 1;
1034                                                 looking= 1;
1035                                         }
1036                                 }
1037                         }
1038                 }
1039         }
1040         
1041         /* (de)select the edges */
1042         for(eed= em->edges.first; eed; eed= eed->next) {
1043                 if(eed->f2) EM_select_edge(eed, select);
1044         }
1045 }
1046 /* ***************** MAIN MOUSE SELECTION ************** */
1047
1048 // just to have the functions nice together
1049 static void mouse_mesh_loop(void)
1050 {
1051         EditEdge *eed;
1052         short dist= 50;
1053         
1054         eed= findnearestedge(&dist);
1055         if(eed) {
1056                 
1057                 if((G.qual & LR_SHIFTKEY)==0) EM_clear_flag_all(SELECT);
1058                 
1059                 if((eed->f & SELECT)==0) EM_select_edge(eed, 1);
1060                 else if(G.qual & LR_SHIFTKEY) EM_select_edge(eed, 0);
1061
1062                 if(G.scene->selectmode & SCE_SELECT_FACE) {
1063                         faceloop_select(eed, eed->f & SELECT);
1064                 }
1065                 else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1066             if(G.qual == (LR_CTRLKEY | LR_ALTKEY) || G.qual == (LR_CTRLKEY | LR_ALTKEY |LR_SHIFTKEY))
1067                         edgering_select(eed, eed->f & SELECT);
1068             else if(G.qual & LR_ALTKEY)
1069                         edgeloop_select(eed, eed->f & SELECT);
1070                 }
1071         else if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1072             if(G.qual == (LR_CTRLKEY | LR_ALTKEY) || G.qual == (LR_CTRLKEY | LR_ALTKEY |LR_SHIFTKEY))
1073                         edgering_select(eed, eed->f & SELECT);
1074             else if(G.qual & LR_ALTKEY)
1075                         edgeloop_select(eed, eed->f & SELECT);
1076                 }
1077
1078                 /* frontbuffer draw of last selected only */
1079                 unified_select_draw(NULL, eed, NULL);
1080                 
1081                 EM_selectmode_flush();
1082                 countall();
1083                 
1084                 allqueue(REDRAWVIEW3D, 0);
1085         }
1086 }
1087
1088
1089 /* here actual select happens */
1090 void mouse_mesh(void)
1091 {
1092         EditVert *eve;
1093         EditEdge *eed;
1094         EditFace *efa;
1095         
1096         if(G.qual & LR_ALTKEY) mouse_mesh_loop();
1097         else if(unified_findnearest(&eve, &eed, &efa)) {
1098                 
1099                 if((G.qual & LR_SHIFTKEY)==0) EM_clear_flag_all(SELECT);
1100                 
1101                 if(efa) {
1102                         
1103                         if( (efa->f & SELECT)==0 ) {
1104                                 EM_select_face_fgon(efa, 1);
1105                         }
1106                         else if(G.qual & LR_SHIFTKEY) {
1107                                 EM_select_face_fgon(efa, 0);
1108                         }
1109                 }
1110                 else if(eed) {
1111                         if((eed->f & SELECT)==0) {
1112                                 EM_select_edge(eed, 1);
1113                         }
1114                         else if(G.qual & LR_SHIFTKEY) {
1115                                 EM_select_edge(eed, 0);
1116                         }
1117                 }
1118                 else if(eve) {
1119                         if((eve->f & SELECT)==0) eve->f |= SELECT;
1120                         else if(G.qual & LR_SHIFTKEY) eve->f &= ~SELECT;
1121                 }
1122                 
1123                 /* frontbuffer draw of last selected only */
1124                 unified_select_draw(eve, eed, efa);
1125         
1126                 EM_selectmode_flush();
1127                 countall();
1128
1129                 allqueue(REDRAWVIEW3D, 0);
1130         }
1131
1132         rightmouse_transform();
1133 }
1134
1135
1136 static void selectconnectedAll(void)
1137 {
1138         EditMesh *em = G.editMesh;
1139         EditVert *v1,*v2;
1140         EditEdge *eed;
1141         short done=1, toggle=0;
1142
1143         if(em->edges.first==0) return;
1144         
1145         while(done==1) {
1146                 done= 0;
1147                 
1148                 toggle++;
1149                 if(toggle & 1) eed= em->edges.first;
1150                 else eed= em->edges.last;
1151                 
1152                 while(eed) {
1153                         v1= eed->v1;
1154                         v2= eed->v2;
1155                         if(eed->h==0) {
1156                                 if(v1->f & SELECT) {
1157                                         if( (v2->f & SELECT)==0 ) {
1158                                                 v2->f |= SELECT;
1159                                                 done= 1;
1160                                         }
1161                                 }
1162                                 else if(v2->f & SELECT) {
1163                                         if( (v1->f & SELECT)==0 ) {
1164                                                 v1->f |= SELECT;
1165                                                 done= 1;
1166                                         }
1167                                 }
1168                         }
1169                         if(toggle & 1) eed= eed->next;
1170                         else eed= eed->prev;
1171                 }
1172         }
1173
1174         /* now use vertex select flag to select rest */
1175         EM_select_flush();
1176         
1177         countall();
1178
1179         allqueue(REDRAWVIEW3D, 0);
1180         BIF_undo_push("Select Connected (All)");
1181 }
1182
1183 void selectconnected_mesh(int qual)
1184 {
1185         EditMesh *em = G.editMesh;
1186         EditVert *eve, *v1, *v2;
1187         EditEdge *eed;
1188         EditFace *efa;
1189         short done=1, sel, toggle=0;
1190
1191         if(em->edges.first==0) return;
1192
1193         if(qual & LR_CTRLKEY) {
1194                 selectconnectedAll();
1195                 return;
1196         }
1197
1198         
1199         if( unified_findnearest(&eve, &eed, &efa)==0 ) {
1200                 error("Nothing indicated ");
1201                 return;
1202         }
1203         
1204         sel= 1;
1205         if(qual & LR_SHIFTKEY) sel=0;
1206
1207         /* clear test flags */
1208         for(v1= em->verts.first; v1; v1= v1->next) v1->f1= 0;
1209         
1210         /* start vertex/face/edge */
1211         if(eve) eve->f1= 1;
1212         else if(eed) eed->v1->f1= eed->v2->f1= 1;
1213         else efa->v1->f1= efa->v2->f1= efa->v3->f1= 1;
1214         
1215         /* set flag f1 if affected */
1216         while(done==1) {
1217                 done= 0;
1218                 toggle++;
1219                 
1220                 if(toggle & 1) eed= em->edges.first;
1221                 else eed= em->edges.last;
1222                 
1223                 while(eed) {
1224                         v1= eed->v1;
1225                         v2= eed->v2;
1226                         
1227                         if(eed->h==0) {
1228                                 if(v1->f1 && v2->f1==0) {
1229                                         v2->f1= 1;
1230                                         done= 1;
1231                                 }
1232                                 else if(v1->f1==0 && v2->f1) {
1233                                         v1->f1= 1;
1234                                         done= 1;
1235                                 }
1236                         }
1237                         
1238                         if(toggle & 1) eed= eed->next;
1239                         else eed= eed->prev;
1240                 }
1241         }
1242         
1243         /* now use vertex f1 flag to select/deselect */
1244         for(eed= em->edges.first; eed; eed= eed->next) {
1245                 if(eed->v1->f1 && eed->v2->f1) 
1246                         EM_select_edge(eed, sel);
1247         }
1248         for(efa= em->faces.first; efa; efa= efa->next) {
1249                 if(efa->v1->f1 && efa->v2->f1 && efa->v3->f1 && (efa->v4==NULL || efa->v4->f1)) 
1250                         EM_select_face(efa, sel);
1251         }
1252         /* no flush needed, connected geometry is done */
1253         
1254         countall();
1255         
1256         allqueue(REDRAWVIEW3D, 0);
1257         BIF_undo_push("Select Linked");
1258         
1259 }
1260
1261 /* swap is 0 or 1, if 1 it hides not selected */
1262 void hide_mesh(int swap)
1263 {
1264         EditMesh *em = G.editMesh;
1265         EditVert *eve;
1266         EditEdge *eed;
1267         EditFace *efa;
1268         int a;
1269         
1270         if(G.obedit==0) return;
1271
1272         /* hide happens on least dominant select mode, and flushes up, not down! (helps preventing errors in subsurf) */
1273         /*  - vertex hidden, always means edge is hidden too
1274                 - edge hidden, always means face is hidden too
1275                 - face hidden, only set face hide
1276                 - then only flush back down what's absolute hidden
1277         */
1278         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1279                 for(eve= em->verts.first; eve; eve= eve->next) {
1280                         if((eve->f & SELECT)!=swap) {
1281                                 eve->xs= 3200;
1282                                 eve->f &= ~SELECT;
1283                                 eve->h= 1;
1284                         }
1285                 }
1286         
1287                 for(eed= em->edges.first; eed; eed= eed->next) {
1288                         if(eed->v1->h || eed->v2->h) {
1289                                 eed->h |= 1;
1290                                 eed->f &= ~SELECT;
1291                         }
1292                 }
1293         
1294                 for(efa= em->faces.first; efa; efa= efa->next) {
1295                         if(efa->e1->h || efa->e2->h || efa->e3->h || (efa->e4 && efa->e4->h)) {
1296                                 efa->h= 1;
1297                                 efa->f &= ~SELECT;
1298                         }
1299                 }
1300         }
1301         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1302
1303                 for(eed= em->edges.first; eed; eed= eed->next) {
1304                         if((eed->f & SELECT)!=swap) {
1305                                 eed->h |= 1;
1306                                 EM_select_edge(eed, 0);
1307                         }
1308                 }
1309
1310                 for(efa= em->faces.first; efa; efa= efa->next) {
1311                         if(efa->e1->h || efa->e2->h || efa->e3->h || (efa->e4 && efa->e4->h)) {
1312                                 efa->h= 1;
1313                                 efa->f &= ~SELECT;
1314                         }
1315                 }
1316         }
1317         else {
1318
1319                 for(efa= em->faces.first; efa; efa= efa->next) {
1320                         if((efa->f & SELECT)!=swap) {
1321                                 efa->h= 1;
1322                                 EM_select_face(efa, 0);
1323                         }
1324                 }
1325         }
1326         
1327         /* flush down, only whats 100% hidden */
1328         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
1329         for(eed= em->edges.first; eed; eed= eed->next) eed->f1= 0;
1330         
1331         if(G.scene->selectmode & SCE_SELECT_FACE) {
1332                 for(efa= em->faces.first; efa; efa= efa->next) {
1333                         if(efa->h) a= 1; else a= 2;
1334                         efa->e1->f1 |= a;
1335                         efa->e2->f1 |= a;
1336                         efa->e3->f1 |= a;
1337                         if(efa->e4) efa->e4->f1 |= a;
1338                 }
1339         }
1340         
1341         if(G.scene->selectmode >= SCE_SELECT_EDGE) {
1342                 for(eed= em->edges.first; eed; eed= eed->next) {
1343                         if(eed->f1==1) eed->h |= 1;
1344                         if(eed->h & 1) a= 1; else a= 2;
1345                         eed->v1->f1 |= a;
1346                         eed->v2->f1 |= a;
1347                 }
1348         }
1349
1350         if(G.scene->selectmode >= SCE_SELECT_VERTEX) {
1351                 for(eve= em->verts.first; eve; eve= eve->next) {
1352                         if(eve->f1==1) eve->h= 1;
1353                 }
1354         }
1355                 
1356         allqueue(REDRAWVIEW3D, 0);
1357         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1358         BIF_undo_push("Hide");
1359 }
1360
1361
1362 void reveal_mesh(void)
1363 {
1364         EditMesh *em = G.editMesh;
1365         EditVert *eve;
1366         EditEdge *eed;
1367         EditFace *efa;
1368         
1369         if(G.obedit==0) return;
1370
1371         for(eve= em->verts.first; eve; eve= eve->next) {
1372                 if(eve->h) {
1373                         eve->h= 0;
1374                         eve->f |= SELECT;
1375                 }
1376         }
1377         for(eed= em->edges.first; eed; eed= eed->next) {
1378                 if(eed->h & 1) {
1379                         eed->h &= ~1;
1380                         if(G.scene->selectmode & SCE_SELECT_VERTEX); 
1381                         else EM_select_edge(eed, 1);
1382                 }
1383         }
1384         for(efa= em->faces.first; efa; efa= efa->next) {
1385                 if(efa->h) {
1386                         efa->h= 0;
1387                         if(G.scene->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_VERTEX)); 
1388                         else EM_select_face(efa, 1);
1389                 }
1390         }
1391
1392         EM_fgon_flags();        // redo flags and indices for fgons
1393         EM_selectmode_flush();
1394         
1395         allqueue(REDRAWVIEW3D, 0);
1396         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1397         BIF_undo_push("Reveal");
1398 }
1399
1400 void select_non_manifold(void)
1401 {
1402         EditMesh *em = G.editMesh;
1403         EditVert *eve;
1404         EditEdge *eed;
1405         EditFace *efa;
1406
1407         /* Selects isolated verts, and edges that do not have 2 neighboring
1408          * faces
1409          */
1410         
1411         if(G.scene->selectmode==SCE_SELECT_FACE) {
1412                 error("Doesn't work in face selection mode");
1413                 return;
1414         }
1415
1416         eve= em->verts.first;
1417         while(eve) {
1418                 /* this will count how many edges are connected
1419                  * to this vert */
1420                 eve->f1= 0;
1421                 eve= eve->next;
1422         }
1423
1424         eed= em->edges.first;
1425         while(eed) {
1426                 /* this will count how many faces are connected to
1427                  * this edge */
1428                 eed->f1= 0;
1429                 /* increase edge count for verts */
1430                 ++eed->v1->f1;
1431                 ++eed->v2->f1;
1432                 eed= eed->next;
1433         }
1434
1435         efa= em->faces.first;
1436         while(efa) {
1437                 /* increase face count for edges */
1438                 ++efa->e1->f1;
1439                 ++efa->e2->f1;
1440                 ++efa->e3->f1;
1441                 if (efa->e4)
1442                         ++efa->e4->f1;                  
1443                 efa= efa->next;
1444         }
1445
1446         /* select verts that are attached to an edge that does not
1447          * have 2 neighboring faces */
1448         eed= em->edges.first;
1449         while(eed) {
1450                 if (eed->h==0 && eed->f1 != 2) {
1451                         EM_select_edge(eed, 1);
1452                 }
1453                 eed= eed->next;
1454         }
1455
1456         /* select isolated verts */
1457         eve= em->verts.first;
1458         while(eve) {
1459                 if (eve->f1 == 0) {
1460                         if (!eve->h) eve->f |= SELECT;
1461                 }
1462                 eve= eve->next;
1463         }
1464
1465         countall();
1466         addqueue(curarea->win,  REDRAW, 0);
1467         BIF_undo_push("Select Non Manifold");
1468 }
1469
1470 void selectswap_mesh(void) /* UI level */
1471 {
1472         EditMesh *em = G.editMesh;
1473         EditVert *eve;
1474         EditEdge *eed;
1475         EditFace *efa;
1476         
1477         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1478
1479                 for(eve= em->verts.first; eve; eve= eve->next) {
1480                         if(eve->h==0) {
1481                                 if(eve->f & SELECT) eve->f &= ~SELECT;
1482                                 else eve->f|= SELECT;
1483                         }
1484                 }
1485         }
1486         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1487                 for(eed= em->edges.first; eed; eed= eed->next) {
1488                         if(eed->h==0) {
1489                                 EM_select_edge(eed, !(eed->f & SELECT));
1490                         }
1491                 }
1492         }
1493         else {
1494                 for(efa= em->faces.first; efa; efa= efa->next) {
1495                         if(efa->h==0) {
1496                                 EM_select_face(efa, !(efa->f & SELECT));
1497                         }
1498                 }
1499         }
1500
1501         EM_selectmode_flush();
1502         
1503         countall();
1504         allqueue(REDRAWVIEW3D, 0);
1505
1506         BIF_undo_push("Select Swap");
1507         
1508 }
1509
1510 void deselectall_mesh(void)      /* this toggles!!!, UI level */
1511 {
1512         
1513         if(G.obedit->lay & G.vd->lay) {
1514
1515                 if( EM_nvertices_selected() ) {
1516                         EM_clear_flag_all(SELECT);
1517                         BIF_undo_push("Deselect All");
1518                 }
1519                 else  {
1520                         EM_set_flag_all(SELECT);
1521                         BIF_undo_push("Select All");
1522                 }
1523                 
1524                 countall();
1525                 allqueue(REDRAWVIEW3D, 0);
1526         }
1527 }
1528
1529 void select_more(void)
1530 {
1531         EditMesh *em = G.editMesh;
1532         EditVert *eve;
1533         EditEdge *eed;
1534         EditFace *efa;
1535         
1536         for(eve= em->verts.first; eve; eve= eve->next) {
1537                 if(eve->f & SELECT) eve->f1= 1;
1538                 else eve->f1 = 0;
1539         }
1540         
1541         /* set f1 flags in vertices to select 'more' */
1542         for(eed= em->edges.first; eed; eed= eed->next) {
1543                 if(eed->h==0) {
1544                         if (eed->v1->f & SELECT)
1545                                 eed->v2->f1 = 1;
1546                         if (eed->v2->f & SELECT)
1547                                 eed->v1->f1 = 1;
1548                 }
1549         }
1550
1551         /* new selected edges, but not in facemode */
1552         if(G.scene->selectmode <= SCE_SELECT_EDGE) {
1553                 
1554                 for(eed= em->edges.first; eed; eed= eed->next) {
1555                         if(eed->h==0) {
1556                                 if(eed->v1->f1 && eed->v2->f1) EM_select_edge(eed, 1);
1557                         }
1558                 }
1559         }
1560         /* new selected faces */
1561         for(efa= em->faces.first; efa; efa= efa->next) {
1562                 if(efa->h==0) {
1563                         if(efa->v1->f1 && efa->v2->f1 && efa->v3->f1 && (efa->v4==NULL || efa->v4->f1)) 
1564                                 EM_select_face(efa, 1);
1565                 }
1566         }
1567
1568         countall();
1569         addqueue(curarea->win,  REDRAW, 0);
1570         BIF_undo_push("Select More");
1571 }
1572
1573 void select_less(void)
1574 {
1575         EditMesh *em = G.editMesh;
1576         EditEdge *eed;
1577         EditFace *efa;
1578
1579         if(G.scene->selectmode <= SCE_SELECT_EDGE) {
1580                 /* eed->f1 == 1:  edge with a selected and deselected vert */ 
1581
1582                 for(eed= em->edges.first; eed; eed= eed->next) {
1583                         eed->f1= 0;
1584                         if(eed->h==0) {
1585                                 
1586                                 if ( !(eed->v1->f & SELECT) && (eed->v2->f & SELECT) ) 
1587                                         eed->f1= 1;
1588                                 if ( (eed->v1->f & SELECT) && !(eed->v2->f & SELECT) ) 
1589                                         eed->f1= 1;
1590                         }
1591                 }
1592                 
1593                 /* deselect edges with flag set */
1594                 for(eed= em->edges.first; eed; eed= eed->next) {
1595                         if (eed->h==0 && eed->f1 == 1) {
1596                                 EM_select_edge(eed, 0);
1597                         }
1598                 }
1599                 EM_deselect_flush();
1600                 
1601         }
1602         else {
1603                 /* deselect faces with 1 or more deselect edges */
1604                 /* eed->f1 == mixed selection edge */
1605                 for(eed= em->edges.first; eed; eed= eed->next) eed->f1= 0;
1606
1607                 for(efa= em->faces.first; efa; efa= efa->next) {
1608                         if(efa->h==0) {
1609                                 if(efa->f & SELECT) {
1610                                         efa->e1->f1 |= 1;
1611                                         efa->e2->f1 |= 1;
1612                                         efa->e3->f1 |= 1;
1613                                         if(efa->e4) efa->e4->f1 |= 1;
1614                                 }
1615                                 else {
1616                                         efa->e1->f1 |= 2;
1617                                         efa->e2->f1 |= 2;
1618                                         efa->e3->f1 |= 2;
1619                                         if(efa->e4) efa->e4->f1 |= 2;
1620                                 }
1621                         }
1622                 }
1623                 for(efa= em->faces.first; efa; efa= efa->next) {
1624                         if(efa->h==0) {
1625                                 if(efa->e1->f1==3 || efa->e2->f1==3 || efa->e3->f1==3 || (efa->e4 && efa->e4->f1==3)) { 
1626                                         EM_select_face(efa, 0);
1627                                 }
1628                         }
1629                 }
1630                 EM_selectmode_flush();
1631                 
1632         }
1633         
1634         countall();
1635         allqueue(REDRAWVIEW3D, 0);
1636 }
1637
1638
1639 void selectrandom_mesh(void) /* randomly selects a user-set % of vertices/edges/faces */
1640 {
1641         EditMesh *em = G.editMesh;
1642         EditVert *eve;
1643         EditEdge *eed;
1644         EditFace *efa;
1645         short randfac = 50;
1646
1647         if(G.obedit==NULL || (G.obedit->lay & G.vd->lay)==0) return;
1648
1649         /* Get the percentage of vertices to randomly select as 'randfac' */
1650         if(button(&randfac,0, 100,"Percentage:")==0) return;
1651
1652         BLI_srand( BLI_rand() ); /* random seed */
1653         
1654         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1655                 for(eve= em->verts.first; eve; eve= eve->next) {
1656                         if(eve->h==0) {
1657                                 if ( (BLI_frand() * 100) < randfac) 
1658                                         eve->f |= SELECT;
1659                         }
1660                 }
1661         }
1662         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1663                 for(eed= em->edges.first; eed; eed= eed->next) {
1664                         if(eed->h==0) {
1665                                 if ( (BLI_frand() * 100) < randfac) 
1666                                         EM_select_edge(eed, 1);
1667                         }
1668                 }
1669         }
1670         else {
1671                 for(efa= em->faces.first; efa; efa= efa->next) {
1672                         if(efa->h==0) {
1673                                 if ( (BLI_frand() * 100) < randfac) 
1674                                         EM_select_face(efa, 1);
1675                         }
1676                 }
1677         }
1678         
1679         EM_selectmode_flush();
1680
1681         countall();
1682         allqueue(REDRAWVIEW3D, 0);
1683 }
1684
1685 void editmesh_select_by_material(int index) 
1686 {
1687         EditMesh *em = G.editMesh;
1688         EditFace *efa;
1689         
1690         for (efa=em->faces.first; efa; efa= efa->next) {
1691                 if (efa->mat_nr==index) {
1692                         EM_select_face(efa, 1);
1693                 }
1694         }
1695
1696         EM_selectmode_flush();
1697 }
1698
1699 void editmesh_deselect_by_material(int index) 
1700 {
1701         EditMesh *em = G.editMesh;
1702         EditFace *efa;
1703         
1704         for (efa=em->faces.first; efa; efa= efa->next) {
1705                 if (efa->mat_nr==index) {
1706                         EM_select_face(efa, 0);
1707                 }
1708         }
1709
1710         EM_selectmode_flush();
1711 }
1712
1713 void EM_selectmode_menu(void)
1714 {
1715         int val;
1716         
1717         if(G.scene->selectmode & SCE_SELECT_VERTEX) pupmenu_set_active(1);
1718         else if(G.scene->selectmode & SCE_SELECT_EDGE) pupmenu_set_active(2);
1719         else pupmenu_set_active(3);
1720         
1721         val= pupmenu("Select Mode%t|Vertices|Edges|Faces");
1722         if(val>0) {
1723                 if(val==1) G.scene->selectmode= SCE_SELECT_VERTEX;
1724                 else if(val==2) G.scene->selectmode= SCE_SELECT_EDGE;
1725                 else G.scene->selectmode= SCE_SELECT_FACE;
1726         
1727                 EM_selectmode_set(); // when mode changes
1728                 allqueue(REDRAWVIEW3D, 1);
1729         }
1730 }
1731
1732 /* ************************* SEAMS AND EDGES **************** */
1733
1734 void editmesh_mark_seam(int clear)
1735 {
1736         EditMesh *em= G.editMesh;
1737         EditEdge *eed;
1738         Mesh *me= G.obedit->data;
1739
1740         /* auto-enable seams drawing */
1741         if(clear==0) {
1742                 if(!(G.f & G_DRAWSEAMS)) {
1743                         G.f |= G_DRAWSEAMS;
1744                         allqueue(REDRAWBUTSEDIT, 0);
1745                 }
1746                 if(!me->medge)
1747                         me->medge= MEM_callocN(sizeof(MEdge), "fake mesh edge");
1748         }
1749
1750         if(clear) {
1751                 eed= em->edges.first;
1752                 while(eed) {
1753                         if((eed->h==0) && (eed->f & SELECT)) {
1754                                 eed->seam = 0;
1755                         }
1756                         eed= eed->next;
1757                 }
1758                 BIF_undo_push("Mark Seam");
1759         }
1760         else {
1761                 eed= em->edges.first;
1762                 while(eed) {
1763                         if((eed->h==0) && (eed->f & SELECT)) {
1764                                 eed->seam = 1;
1765                         }
1766                         eed= eed->next;
1767                 }
1768                 BIF_undo_push("Clear Seam");
1769         }
1770
1771         allqueue(REDRAWVIEW3D, 0);
1772 }
1773
1774 void Edge_Menu() {
1775         short ret;
1776
1777         ret= pupmenu("Edge Specials%t|Mark Seam %x1|Clear Seam %x2|Rotate Edge CW%x3|Rotate Edge CCW%x4|Loopcut%x6|Edge Slide%x5");
1778
1779         switch(ret)
1780         {
1781         case 1:
1782                 editmesh_mark_seam(0);
1783                 break;
1784         case 2:
1785                 editmesh_mark_seam(1);
1786                 break;
1787         case 3:
1788                 edge_rotate_selected(2);
1789                 break;
1790         case 4:
1791                 edge_rotate_selected(1);
1792                 break;
1793         case 5:
1794                 EdgeSlide(0,0.0);
1795         BIF_undo_push("EdgeSlide");
1796                 break;
1797         case 6:
1798         CutEdgeloop(1);
1799                 BIF_undo_push("Loopcut New");
1800                 break;
1801         }
1802 }
1803
1804
1805 /* **************** NORMALS ************** */
1806
1807 void righthandfaces(int select) /* makes faces righthand turning */
1808 {
1809         EditMesh *em = G.editMesh;
1810         EditEdge *eed, *ed1, *ed2, *ed3, *ed4;
1811         EditFace *efa, *startvl;
1812         float maxx, nor[3], cent[3];
1813         int totsel, found, foundone, direct, turn, tria_nr;
1814
1815    /* based at a select-connected to witness loose objects */
1816
1817         /* count per edge the amount of faces */
1818
1819         /* find the ultimate left, front, upper face (not manhattan dist!!) */
1820         /* also evaluate both triangle cases in quad, since these can be non-flat */
1821
1822         /* put normal to the outside, and set the first direction flags in edges */
1823
1824         /* then check the object, and set directions / direction-flags: but only for edges with 1 or 2 faces */
1825         /* this is in fact the 'select connected' */
1826         
1827         /* in case (selected) faces were not done: start over with 'find the ultimate ...' */
1828
1829         waitcursor(1);
1830         
1831         eed= em->edges.first;
1832         while(eed) {
1833                 eed->f2= 0;             // edge direction
1834                 eed->f1= 0;             // counter
1835                 eed= eed->next;
1836         }
1837
1838         /* count faces and edges */
1839         totsel= 0;
1840         efa= em->faces.first;
1841         while(efa) {
1842                 if(select==0 || (efa->f & SELECT) ) {
1843                         efa->f1= 1;
1844                         totsel++;
1845                         efa->e1->f1++;
1846                         efa->e2->f1++;
1847                         efa->e3->f1++;
1848                         if(efa->v4) efa->e4->f1++;
1849                 }
1850                 else efa->f1= 0;
1851
1852                 efa= efa->next;
1853         }
1854
1855         while(totsel>0) {
1856                 /* from the outside to the inside */
1857
1858                 efa= em->faces.first;
1859                 startvl= NULL;
1860                 maxx= -1.0e10;
1861                 tria_nr= 0;
1862
1863                 while(efa) {
1864                         if(efa->f1) {
1865                                 CalcCent3f(cent, efa->v1->co, efa->v2->co, efa->v3->co);
1866                                 cent[0]= cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
1867                                 
1868                                 if(cent[0]>maxx) {
1869                                         maxx= cent[0];
1870                                         startvl= efa;
1871                                         tria_nr= 0;
1872                                 }
1873                                 if(efa->v4) {
1874                                         CalcCent3f(cent, efa->v1->co, efa->v3->co, efa->v4->co);
1875                                         cent[0]= cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
1876                                         
1877                                         if(cent[0]>maxx) {
1878                                                 maxx= cent[0];
1879                                                 startvl= efa;
1880                                                 tria_nr= 1;
1881                                         }
1882                                 }
1883                         }
1884                         efa= efa->next;
1885                 }
1886                 
1887                 /* set first face correct: calc normal */
1888                 
1889                 if(tria_nr==1) {
1890                         CalcNormFloat(startvl->v1->co, startvl->v3->co, startvl->v4->co, nor);
1891                         CalcCent3f(cent, startvl->v1->co, startvl->v3->co, startvl->v4->co);
1892                 } else {
1893                         CalcNormFloat(startvl->v1->co, startvl->v2->co, startvl->v3->co, nor);
1894                         CalcCent3f(cent, startvl->v1->co, startvl->v2->co, startvl->v3->co);
1895                 }
1896                 /* first normal is oriented this way or the other */
1897                 if(select) {
1898                         if(select==2) {
1899                                 if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] > 0.0) flipface(startvl);
1900                         }
1901                         else {
1902                                 if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] < 0.0) flipface(startvl);
1903                         }
1904                 }
1905                 else if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] < 0.0) flipface(startvl);
1906
1907
1908                 eed= startvl->e1;
1909                 if(eed->v1==startvl->v1) eed->f2= 1; 
1910                 else eed->f2= 2;
1911                 
1912                 eed= startvl->e2;
1913                 if(eed->v1==startvl->v2) eed->f2= 1; 
1914                 else eed->f2= 2;
1915                 
1916                 eed= startvl->e3;
1917                 if(eed->v1==startvl->v3) eed->f2= 1; 
1918                 else eed->f2= 2;
1919                 
1920                 eed= startvl->e4;
1921                 if(eed) {
1922                         if(eed->v1==startvl->v4) eed->f2= 1; 
1923                         else eed->f2= 2;
1924                 }
1925                 
1926                 startvl->f1= 0;
1927                 totsel--;
1928
1929                 /* test normals */
1930                 found= 1;
1931                 direct= 1;
1932                 while(found) {
1933                         found= 0;
1934                         if(direct) efa= em->faces.first;
1935                         else efa= em->faces.last;
1936                         while(efa) {
1937                                 if(efa->f1) {
1938                                         turn= 0;
1939                                         foundone= 0;
1940
1941                                         ed1= efa->e1;
1942                                         ed2= efa->e2;
1943                                         ed3= efa->e3;
1944                                         ed4= efa->e4;
1945
1946                                         if(ed1->f2) {
1947                                                 if(ed1->v1==efa->v1 && ed1->f2==1) turn= 1;
1948                                                 if(ed1->v2==efa->v1 && ed1->f2==2) turn= 1;
1949                                                 foundone= 1;
1950                                         }
1951                                         else if(ed2->f2) {
1952                                                 if(ed2->v1==efa->v2 && ed2->f2==1) turn= 1;
1953                                                 if(ed2->v2==efa->v2 && ed2->f2==2) turn= 1;
1954                                                 foundone= 1;
1955                                         }
1956                                         else if(ed3->f2) {
1957                                                 if(ed3->v1==efa->v3 && ed3->f2==1) turn= 1;
1958                                                 if(ed3->v2==efa->v3 && ed3->f2==2) turn= 1;
1959                                                 foundone= 1;
1960                                         }
1961                                         else if(ed4 && ed4->f2) {
1962                                                 if(ed4->v1==efa->v4 && ed4->f2==1) turn= 1;
1963                                                 if(ed4->v2==efa->v4 && ed4->f2==2) turn= 1;
1964                                                 foundone= 1;
1965                                         }
1966
1967                                         if(foundone) {
1968                                                 found= 1;
1969                                                 totsel--;
1970                                                 efa->f1= 0;
1971
1972                                                 if(turn) {
1973                                                         if(ed1->v1==efa->v1) ed1->f2= 2; 
1974                                                         else ed1->f2= 1;
1975                                                         if(ed2->v1==efa->v2) ed2->f2= 2; 
1976                                                         else ed2->f2= 1;
1977                                                         if(ed3->v1==efa->v3) ed3->f2= 2; 
1978                                                         else ed3->f2= 1;
1979                                                         if(ed4) {
1980                                                                 if(ed4->v1==efa->v4) ed4->f2= 2; 
1981                                                                 else ed4->f2= 1;
1982                                                         }
1983
1984                                                         flipface(efa);
1985
1986                                                 }
1987                                                 else {
1988                                                         if(ed1->v1== efa->v1) ed1->f2= 1; 
1989                                                         else ed1->f2= 2;
1990                                                         if(ed2->v1==efa->v2) ed2->f2= 1; 
1991                                                         else ed2->f2= 2;
1992                                                         if(ed3->v1==efa->v3) ed3->f2= 1; 
1993                                                         else ed3->f2= 2;
1994                                                         if(ed4) {
1995                                                                 if(ed4->v1==efa->v4) ed4->f2= 1; 
1996                                                                 else ed4->f2= 2;
1997                                                         }
1998                                                 }
1999                                         }
2000                                 }
2001                                 if(direct) efa= efa->next;
2002                                 else efa= efa->prev;
2003                         }
2004                         direct= 1-direct;
2005                 }
2006         }
2007
2008         recalc_editnormals();
2009         
2010         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2011         
2012         waitcursor(0);
2013 }
2014
2015
2016 /* ********** ALIGN WITH VIEW **************** */
2017
2018
2019 static void editmesh_calc_selvert_center(float cent_r[3])
2020 {
2021         EditMesh *em = G.editMesh;
2022         EditVert *eve;
2023         int nsel= 0;
2024
2025         cent_r[0]= cent_r[1]= cent_r[0]= 0.0;
2026
2027         for (eve= em->verts.first; eve; eve= eve->next) {
2028                 if (eve->f & SELECT) {
2029                         cent_r[0]+= eve->co[0];
2030                         cent_r[1]+= eve->co[1];
2031                         cent_r[2]+= eve->co[2];
2032                         nsel++;
2033                 }
2034         }
2035
2036         if (nsel) {
2037                 cent_r[0]/= nsel;
2038                 cent_r[1]/= nsel;
2039                 cent_r[2]/= nsel;
2040         }
2041 }
2042
2043 static int tface_is_selected(TFace *tf)
2044 {
2045         return (!(tf->flag & TF_HIDE) && (tf->flag & TF_SELECT));
2046 }
2047
2048 static int faceselect_nfaces_selected(Mesh *me)
2049 {
2050         int i, count= 0;
2051
2052         for (i=0; i<me->totface; i++) {
2053                 MFace *mf= ((MFace*) me->mface) + i;
2054                 TFace *tf= ((TFace*) me->tface) + i;
2055
2056                 if (mf->v3 && tface_is_selected(tf))
2057                         count++;
2058         }
2059
2060         return count;
2061 }
2062
2063         /* XXX, code for both these functions should be abstract,
2064          * then unified, then written for other things (like objects,
2065          * which would use same as vertices method), then added
2066          * to interface! Hoera! - zr
2067          */
2068 void faceselect_align_view_to_selected(View3D *v3d, Mesh *me, int axis)
2069 {
2070         if (!faceselect_nfaces_selected(me)) {
2071                 error("No faces selected.");
2072         } else {
2073                 float norm[3];
2074                 int i;
2075
2076                 norm[0]= norm[1]= norm[2]= 0.0;
2077                 for (i=0; i<me->totface; i++) {
2078                         MFace *mf= ((MFace*) me->mface) + i;
2079                         TFace *tf= ((TFace*) me->tface) + i;
2080         
2081                         if (mf->v3 && tface_is_selected(tf)) {
2082                                 float *v1, *v2, *v3, fno[3];
2083
2084                                 v1= me->mvert[mf->v1].co;
2085                                 v2= me->mvert[mf->v2].co;
2086                                 v3= me->mvert[mf->v3].co;
2087                                 if (mf->v4) {
2088                                         float *v4= me->mvert[mf->v4].co;
2089                                         CalcNormFloat4(v1, v2, v3, v4, fno);
2090                                 } else {
2091                                         CalcNormFloat(v1, v2, v3, fno);
2092                                 }
2093
2094                                 norm[0]+= fno[0];
2095                                 norm[1]+= fno[1];
2096                                 norm[2]+= fno[2];
2097                         }
2098                 }
2099
2100                 view3d_align_axis_to_vector(v3d, axis, norm);
2101         }
2102 }
2103
2104 void editmesh_align_view_to_selected(View3D *v3d, int axis)
2105 {
2106         EditMesh *em = G.editMesh;
2107         int nselverts= EM_nvertices_selected();
2108
2109         if (nselverts<3) {
2110                 if (nselverts==0) {
2111                         error("No faces or vertices selected.");
2112                 } else {
2113                         error("At least one face or three vertices must be selected.");
2114                 }
2115         } else if (EM_nfaces_selected()) {
2116                 float norm[3];
2117                 EditFace *efa;
2118
2119                 norm[0]= norm[1]= norm[2]= 0.0;
2120                 for (efa= em->faces.first; efa; efa= efa->next) {
2121                         if (faceselectedAND(efa, SELECT)) {
2122                                 float fno[3];
2123                                 if (efa->v4) CalcNormFloat4(efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co, fno);
2124                                 else CalcNormFloat(efa->v1->co, efa->v2->co, efa->v3->co, fno);
2125                                                 /* XXX, fixme, should be flipped intp a 
2126                                                  * consistent direction. -zr
2127                                                  */
2128                                 norm[0]+= fno[0];
2129                                 norm[1]+= fno[1];
2130                                 norm[2]+= fno[2];
2131                         }
2132                 }
2133
2134                 Mat4Mul3Vecfl(G.obedit->obmat, norm);
2135                 view3d_align_axis_to_vector(v3d, axis, norm);
2136         } else {
2137                 float cent[3], norm[3];
2138                 EditVert *eve, *leve= NULL;
2139
2140                 norm[0]= norm[1]= norm[2]= 0.0;
2141                 editmesh_calc_selvert_center(cent);
2142                 for (eve= em->verts.first; eve; eve= eve->next) {
2143                         if (eve->f & SELECT) {
2144                                 if (leve) {
2145                                         float tno[3];
2146                                         CalcNormFloat(cent, leve->co, eve->co, tno);
2147                                         
2148                                                 /* XXX, fixme, should be flipped intp a 
2149                                                  * consistent direction. -zr
2150                                                  */
2151                                         norm[0]+= tno[0];
2152                                         norm[1]+= tno[1];
2153                                         norm[2]+= tno[2];
2154                                 }
2155                                 leve= eve;
2156                         }
2157                 }
2158
2159                 Mat4Mul3Vecfl(G.obedit->obmat, norm);
2160                 view3d_align_axis_to_vector(v3d, axis, norm);
2161         }
2162 }
2163
2164 /* **************** VERTEX DEFORMS *************** */
2165
2166 void vertexsmooth(void)
2167 {
2168         EditMesh *em = G.editMesh;
2169         EditVert *eve;
2170         EditEdge *eed;
2171         float *adror, *adr, fac;
2172         float fvec[3];
2173         int teller=0;
2174
2175         if(G.obedit==0) return;
2176
2177         /* count */
2178         eve= em->verts.first;
2179         while(eve) {
2180                 if(eve->f & SELECT) teller++;
2181                 eve= eve->next;
2182         }
2183         if(teller==0) return;
2184         
2185         adr=adror= (float *)MEM_callocN(3*sizeof(float *)*teller, "vertsmooth");
2186         eve= em->verts.first;
2187         while(eve) {
2188                 if(eve->f & SELECT) {
2189                         eve->vn= (EditVert *)adr;
2190                         eve->f1= 0;
2191                         adr+= 3;
2192                 }
2193                 eve= eve->next;
2194         }
2195         
2196         eed= em->edges.first;
2197         while(eed) {
2198                 if( (eed->v1->f & SELECT) || (eed->v2->f & SELECT) ) {
2199                         fvec[0]= (eed->v1->co[0]+eed->v2->co[0])/2.0;
2200                         fvec[1]= (eed->v1->co[1]+eed->v2->co[1])/2.0;
2201                         fvec[2]= (eed->v1->co[2]+eed->v2->co[2])/2.0;
2202                         
2203                         if((eed->v1->f & SELECT) && eed->v1->f1<255) {
2204                                 eed->v1->f1++;
2205                                 VecAddf((float *)eed->v1->vn, (float *)eed->v1->vn, fvec);
2206                         }
2207                         if((eed->v2->f & SELECT) && eed->v2->f1<255) {
2208                                 eed->v2->f1++;
2209                                 VecAddf((float *)eed->v2->vn, (float *)eed->v2->vn, fvec);
2210                         }
2211                 }
2212                 eed= eed->next;
2213         }
2214
2215         eve= em->verts.first;
2216         while(eve) {
2217                 if(eve->f & SELECT) {
2218                         if(eve->f1) {
2219                                 adr= (float *)eve->vn;
2220                                 fac= 0.5/(float)eve->f1;
2221                                 
2222                                 eve->co[0]= 0.5*eve->co[0]+fac*adr[0];
2223                                 eve->co[1]= 0.5*eve->co[1]+fac*adr[1];
2224                                 eve->co[2]= 0.5*eve->co[2]+fac*adr[2];
2225                         }
2226                         eve->vn= 0;
2227                 }
2228                 eve= eve->next;
2229         }
2230         MEM_freeN(adror);
2231
2232         recalc_editnormals();
2233
2234         allqueue(REDRAWVIEW3D, 0);
2235         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2236         BIF_undo_push("Vertex Smooth");
2237 }
2238
2239 void vertexnoise(void)
2240 {
2241         EditMesh *em = G.editMesh;
2242         Material *ma;
2243         Tex *tex;
2244         EditVert *eve;
2245         float b2, ofs, vec[3];
2246
2247         if(G.obedit==0) return;
2248         
2249         ma= give_current_material(G.obedit, G.obedit->actcol);
2250         if(ma==0 || ma->mtex[0]==0 || ma->mtex[0]->tex==0) {
2251                 return;
2252         }
2253         tex= ma->mtex[0]->tex;
2254         
2255         ofs= tex->turbul/200.0;
2256         
2257         eve= (struct EditVert *)em->verts.first;
2258         while(eve) {
2259                 if(eve->f & SELECT) {
2260                         
2261                         if(tex->type==TEX_STUCCI) {
2262                                 
2263                                 b2= BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1], eve->co[2]);
2264                                 if(tex->stype) ofs*=(b2*b2);
2265                                 vec[0]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0]+ofs, eve->co[1], eve->co[2]));
2266                                 vec[1]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1]+ofs, eve->co[2]));
2267                                 vec[2]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1], eve->co[2]+ofs));
2268                                 
2269                                 VecAddf(eve->co, eve->co, vec);
2270                         }
2271                         else {
2272                                 float tin, dum;
2273                                 externtex(ma->mtex[0], eve->co, &tin, &dum, &dum, &dum, &dum);
2274                                 eve->co[2]+= 0.05*tin;
2275                         }
2276                 }
2277                 eve= eve->next;
2278         }
2279
2280         recalc_editnormals();
2281         allqueue(REDRAWVIEW3D, 0);
2282         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2283         BIF_undo_push("Vertex Noise");
2284 }
2285
2286 void vertices_to_sphere(void)
2287 {
2288         EditMesh *em = G.editMesh;
2289         EditVert *eve;
2290         Object *ob= OBACT;
2291         float *curs, len, vec[3], cent[3], fac, facm, imat[3][3], bmat[3][3];
2292         int tot;
2293         short perc=100;
2294         
2295         if(ob==0) return;
2296         TEST_EDITMESH
2297         
2298         if(button(&perc, 1, 100, "Percentage:")==0) return;
2299         
2300         fac= perc/100.0;
2301         facm= 1.0-fac;
2302         
2303         Mat3CpyMat4(bmat, ob->obmat);
2304         Mat3Inv(imat, bmat);
2305
2306         /* centre */
2307         curs= give_cursor();
2308         cent[0]= curs[0]-ob->obmat[3][0];
2309         cent[1]= curs[1]-ob->obmat[3][1];
2310         cent[2]= curs[2]-ob->obmat[3][2];
2311         Mat3MulVecfl(imat, cent);
2312
2313         len= 0.0;
2314         tot= 0;
2315         eve= em->verts.first;
2316         while(eve) {
2317                 if(eve->f & SELECT) {
2318                         tot++;
2319                         len+= VecLenf(cent, eve->co);
2320                 }
2321                 eve= eve->next;
2322         }
2323         len/=tot;
2324         
2325         if(len==0.0) len= 10.0;
2326         
2327         eve= em->verts.first;
2328         while(eve) {
2329                 if(eve->f & SELECT) {
2330                         vec[0]= eve->co[0]-cent[0];
2331                         vec[1]= eve->co[1]-cent[1];
2332                         vec[2]= eve->co[2]-cent[2];
2333                         
2334                         Normalise(vec);
2335                         
2336                         eve->co[0]= fac*(cent[0]+vec[0]*len) + facm*eve->co[0];
2337                         eve->co[1]= fac*(cent[1]+vec[1]*len) + facm*eve->co[1];
2338                         eve->co[2]= fac*(cent[2]+vec[2]*len) + facm*eve->co[2];
2339                         
2340                 }
2341                 eve= eve->next;
2342         }
2343         
2344         recalc_editnormals();
2345         allqueue(REDRAWVIEW3D, 0);
2346         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2347         BIF_undo_push("To Sphere");
2348 }
2349