Well here it is. Subdivision/Loop Tools Recode Commit #1
[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 int em_solidoffs=0, em_wireoffs=0, em_vertoffs; // 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, int min, 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(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;
566                 int a=1, index;
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, index;
651                 short mval[2], distance;
652
653                 calc_mesh_facedots_ext();       // shouldnt be needed each click
654                 getmouseco_areawin(mval);
655
656                 // sample colorcode 
657                 index= sample_backbuf(mval[0], mval[1]);
658                 
659                 if(index && index<=em_solidoffs) {
660                         for(efa= G.editMesh->faces.first; efa; efa= efa->next, a++) if(index==a) break;
661                         if(efa) {
662                                 distance= abs(mval[0]- efa->xs)+ abs(mval[1]- efa->ys);
663
664                                 if(G.scene->selectmode == SCE_SELECT_FACE || distance<*dist) {  // only faces, no dist check
665                                         *dist= distance;
666                                         return efa;
667                                 }
668                         }
669                 }
670                 
671                 return NULL;
672         }
673         else return findnearestface_f(dist);
674 }
675
676 /* for interactivity, frontbuffer draw in current window */
677 static void unified_select_draw(EditVert *eve, EditEdge *eed, EditFace *efa)
678 {
679         int dmNeedsFree;
680         DerivedMesh *dm = mesh_get_cage_derived(G.obedit, &dmNeedsFree);
681
682         glDrawBuffer(GL_FRONT);
683
684         persp(PERSP_VIEW);
685         glPushMatrix();
686         mymultmatrix(G.obedit->obmat);
687         
688         /* face selected */
689         if(efa) {
690                 if(G.scene->selectmode & SCE_SELECT_VERTEX) {
691                         glPointSize(BIF_GetThemeValuef(TH_VERTEX_SIZE));
692                         
693                         if(efa->f & SELECT) BIF_ThemeColor(TH_VERTEX_SELECT);
694                         else BIF_ThemeColor(TH_VERTEX);
695                         
696                         bglBegin(GL_POINTS);
697                         bglVertex3fv(efa->v1->co);
698                         bglVertex3fv(efa->v2->co);
699                         bglVertex3fv(efa->v3->co);
700                         if(efa->v4) bglVertex3fv(efa->v4->co);
701                         bglEnd();
702                 }
703
704                 if(G.scene->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_FACE)) {
705                         if(efa->fgonf==0) {
706                                 BIF_ThemeColor((efa->f & SELECT)?TH_EDGE_SELECT:TH_WIRE);
707         
708                                 dm->drawMappedEdgeEM(dm, efa->e1);
709                                 dm->drawMappedEdgeEM(dm, efa->e2);
710                                 dm->drawMappedEdgeEM(dm, efa->e3);
711                                 if (efa->e4) {
712                                         dm->drawMappedEdgeEM(dm, efa->e4);
713                                 }
714                         }
715                 }
716                 
717                 if(G.scene->selectmode & SCE_SELECT_FACE) {
718                         if(efa->fgonf==0) {
719                                 glPointSize(BIF_GetThemeValuef(TH_FACEDOT_SIZE));
720                                 BIF_ThemeColor((efa->f & SELECT)?TH_FACE_DOT:TH_WIRE);
721
722                                 bglBegin(GL_POINTS);
723                                 bglVertex3fv(efa->cent);
724                                 bglEnd();
725                         }
726                 }
727         }
728         /* edge selected */
729         if(eed) {
730                 if(G.scene->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_FACE)) {
731                         BIF_ThemeColor((eed->f & SELECT)?TH_EDGE_SELECT:TH_WIRE);
732
733                         dm->drawMappedEdgeEM(dm, eed);
734                 }
735                 if(G.scene->selectmode & SCE_SELECT_VERTEX) {
736                         float co[3];
737                         glPointSize(BIF_GetThemeValuef(TH_VERTEX_SIZE));
738                         
739                         BIF_ThemeColor((eed->f & SELECT)?TH_VERTEX_SELECT:TH_VERTEX);
740                         
741                         bglBegin(GL_POINTS);
742                         dm->getMappedVertCoEM(dm, eed->v1, co);
743                         bglVertex3fv(co);
744
745                         dm->getMappedVertCoEM(dm, eed->v2, co);
746                         bglVertex3fv(co);
747                         bglEnd();
748                 }
749         }
750         if(eve) {
751                 if(G.scene->selectmode & SCE_SELECT_VERTEX) {
752                         float co[3];
753                         glPointSize(BIF_GetThemeValuef(TH_VERTEX_SIZE));
754                         
755                         BIF_ThemeColor((eve->f & SELECT)?TH_VERTEX_SELECT:TH_VERTEX);
756                         
757                         bglBegin(GL_POINTS);
758                         dm->getMappedVertCoEM(dm, eve, co);
759                         bglVertex3fv(co);
760                         bglEnd();
761                 }
762         }
763
764         glPointSize(1.0);
765         glPopMatrix();
766
767         glFlush();
768         glDrawBuffer(GL_BACK);
769
770         /* signal that frontbuf differs from back */
771         curarea->win_swap= WIN_FRONT_OK;
772
773         if (dmNeedsFree) {
774                 dm->release(dm);
775         }
776 }
777
778
779 /* best distance based on screen coords. 
780    use g.scene->selectmode to define how to use 
781    selected vertices and edges get disadvantage
782    return 1 if found one
783 */
784 static int unified_findnearest(EditVert **eve, EditEdge **eed, EditFace **efa) 
785 {
786         short dist= 75;
787         
788         *eve= NULL;
789         *eed= NULL;
790         *efa= NULL;
791         
792         if(G.scene->selectmode & SCE_SELECT_VERTEX)
793                 *eve= findnearestvert(&dist, SELECT);
794         if(G.scene->selectmode & SCE_SELECT_FACE)
795                 *efa= findnearestface(&dist);
796
797         dist-= 20;      // since edges select lines, we give dots advantage of 20 pix
798         if(G.scene->selectmode & SCE_SELECT_EDGE)
799                 *eed= findnearestedge(&dist);
800
801         /* return only one of 3 pointers, for frontbuffer redraws */
802         if(*eed) {
803                 *efa= NULL; *eve= NULL;
804         }
805         else if(*efa) {
806                 *eve= NULL;
807         }
808         
809         return (*eve || *eed || *efa);
810 }
811
812 /* ****************  LOOP SELECTS *************** */
813
814 /* selects quads in loop direction of indicated edge */
815 /* only flush over edges with valence <= 2 */
816 static void faceloop_select(EditEdge *startedge, int select)
817 {
818         EditMesh *em = G.editMesh;
819         EditEdge *eed;
820         EditFace *efa;
821         int looking= 1;
822         
823         /* in eed->f1 we put the valence (amount of faces in edge) */
824         /* in eed->f2 we put tagged flag as correct loop */
825         /* in efa->f1 we put tagged flag as correct to select */
826
827         for(eed= em->edges.first; eed; eed= eed->next) {
828                 eed->f1= 0;
829                 eed->f2= 0;
830         }
831         for(efa= em->faces.first; efa; efa= efa->next) {
832                 efa->f1= 0;
833                 if(efa->h==0) {
834                         efa->e1->f1++;
835                         efa->e2->f1++;
836                         efa->e3->f1++;
837                         if(efa->e4) efa->e4->f1++;
838                 }
839         }
840         
841         // tag startedge OK
842         startedge->f2= 1;
843         
844         while(looking) {
845                 looking= 0;
846                 
847                 for(efa= em->faces.first; efa; efa= efa->next) {
848                         if(efa->e4 && efa->f1==0) {     // not done quad
849                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
850
851                                         // if edge tagged, select opposing edge and mark face ok
852                                         if(efa->e1->f2) {
853                                                 efa->e3->f2= 1;
854                                                 efa->f1= 1;
855                                                 looking= 1;
856                                         }
857                                         else if(efa->e2->f2) {
858                                                 efa->e4->f2= 1;
859                                                 efa->f1= 1;
860                                                 looking= 1;
861                                         }
862                                         if(efa->e3->f2) {
863                                                 efa->e1->f2= 1;
864                                                 efa->f1= 1;
865                                                 looking= 1;
866                                         }
867                                         if(efa->e4->f2) {
868                                                 efa->e2->f2= 1;
869                                                 efa->f1= 1;
870                                                 looking= 1;
871                                         }
872                                 }
873                         }
874                 }
875         }
876         
877         /* (de)select the faces */
878         for(efa= em->faces.first; efa; efa= efa->next) {
879                 if(efa->f1) EM_select_face(efa, select);
880         }
881 }
882
883
884 /* helper for edgeloop_select, checks for eed->f2 tag in faces */
885 static int edge_not_in_tagged_face(EditEdge *eed)
886 {
887         EditMesh *em = G.editMesh;
888         EditFace *efa;
889         
890         for(efa= em->faces.first; efa; efa= efa->next) {
891                 if(efa->h==0) {
892                         if(efa->e1==eed || efa->e2==eed || efa->e3==eed || efa->e4==eed) {      // edge is in face
893                                 if(efa->e1->f2 || efa->e2->f2 || efa->e3->f2 || (efa->e4 && efa->e4->f2)) {     // face is tagged
894                                         return 0;
895                                 }
896                         }
897                 }
898         }
899         return 1;
900 }
901
902 /* selects or deselects edges that:
903 - if edges has 2 faces:
904         - has vertices with valence of 4
905         - not shares face with previous edge
906 - if edge has 1 face:
907         - has vertices with valence 4
908         - not shares face with previous edge
909         - but also only 1 face
910 - if edge no face:
911         - has vertices with valence 2
912 */
913 static void edgeloop_select(EditEdge *starteed, int select)
914 {
915         EditMesh *em = G.editMesh;
916         EditVert *eve;
917         EditEdge *eed;
918         EditFace *efa;
919         int looking= 1;
920         
921         /* in f1 we put the valence (amount of edges in a vertex, or faces in edge) */
922         /* in eed->f2 and efa->f1 we put tagged flag as correct loop */
923         for(eve= em->verts.first; eve; eve= eve->next) {
924                 eve->f1= 0;
925                 eve->f2= 0;
926         }
927         for(eed= em->edges.first; eed; eed= eed->next) {
928                 eed->f1= 0;
929                 eed->f2= 0;
930                 if((eed->h & 1)==0) {   // fgon edges add to valence too
931                         eed->v1->f1++; eed->v2->f1++;
932                 }
933         }
934         for(efa= em->faces.first; efa; efa= efa->next) {
935                 efa->f1= 0;
936                 if(efa->h==0) {
937                         efa->e1->f1++;
938                         efa->e2->f1++;
939                         efa->e3->f1++;
940                         if(efa->e4) efa->e4->f1++;
941                 }
942         }
943         
944         /* looped edges & vertices get tagged f2 */
945         starteed->f2= 1;
946         if(starteed->v1->f1<5) starteed->v1->f2= 1;
947         if(starteed->v2->f1<5) starteed->v2->f2= 1;
948         /* sorry, first edge isnt even ok */
949         if(starteed->v1->f2==0 && starteed->v2->f2==0) looking= 0;
950         
951         while(looking) {
952                 looking= 0;
953                 
954                 /* find correct valence edges which are not tagged yet, but connect to tagged one */
955                 for(eed= em->edges.first; eed; eed= eed->next) {
956                         if(eed->h==0 && eed->f2==0) { // edge not hidden, not tagged
957                                 if( (eed->v1->f1<5 && eed->v1->f2) || (eed->v2->f1<5 && eed->v2->f2)) { // valence of vertex OK, and is tagged
958                                         /* new edge is not allowed to be in face with tagged edge */
959                                         if(edge_not_in_tagged_face(eed)) {
960                                                 if(eed->f1==starteed->f1) {     // same amount of faces
961                                                         looking= 1;
962                                                         eed->f2= 1;
963                                                         if(eed->v2->f1<5) eed->v2->f2= 1;
964                                                         if(eed->v1->f1<5) eed->v1->f2= 1;
965                                                 }
966                                         }
967                                 }
968                         }
969                 }
970         }
971         /* and we do the select */
972         for(eed= em->edges.first; eed; eed= eed->next) {
973                 if(eed->f2) EM_select_edge(eed, select);
974         }
975 }
976
977 /* 
978    Almostly exactly the same code as faceloop select
979 */
980 static void edgering_select(EditEdge *startedge, int select){
981         EditMesh *em = G.editMesh;
982         EditEdge *eed;
983         EditFace *efa;
984         int looking= 1;
985         
986         /* in eed->f1 we put the valence (amount of faces in edge) */
987         /* in eed->f2 we put tagged flag as correct loop */
988         /* in efa->f1 we put tagged flag as correct to select */
989
990         for(eed= em->edges.first; eed; eed= eed->next) {
991                 eed->f1= 0;
992                 eed->f2= 0;
993         }
994         for(efa= em->faces.first; efa; efa= efa->next) {
995                 efa->f1= 0;
996                 if(efa->h==0) {
997                         efa->e1->f1++;
998                         efa->e2->f1++;
999                         efa->e3->f1++;
1000                         if(efa->e4) efa->e4->f1++;
1001                 }
1002         }
1003         
1004         // tag startedge OK
1005         startedge->f2= 1;
1006         
1007         while(looking) {
1008                 looking= 0;
1009                 
1010                 for(efa= em->faces.first; efa; efa= efa->next) {
1011                         if(efa->e4 && efa->f1==0) {     // not done quad
1012                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
1013
1014                                         // if edge tagged, select opposing edge and mark face ok
1015                                         if(efa->e1->f2) {
1016                                                 efa->e3->f2= 1;
1017                                                 efa->f1= 1;
1018                                                 looking= 1;
1019                                         }
1020                                         else if(efa->e2->f2) {
1021                                                 efa->e4->f2= 1;
1022                                                 efa->f1= 1;
1023                                                 looking= 1;
1024                                         }
1025                                         if(efa->e3->f2) {
1026                                                 efa->e1->f2= 1;
1027                                                 efa->f1= 1;
1028                                                 looking= 1;
1029                                         }
1030                                         if(efa->e4->f2) {
1031                                                 efa->e2->f2= 1;
1032                                                 efa->f1= 1;
1033                                                 looking= 1;
1034                                         }
1035                                 }
1036                         }
1037                 }
1038         }
1039         
1040         /* (de)select the edges */
1041         for(eed= em->edges.first; eed; eed= eed->next) {
1042                 if(eed->f2) EM_select_edge(eed, select);
1043         }
1044 }
1045 /* ***************** MAIN MOUSE SELECTION ************** */
1046
1047 // just to have the functions nice together
1048 static void mouse_mesh_loop(void)
1049 {
1050         EditEdge *eed;
1051         short dist= 50;
1052         
1053         eed= findnearestedge(&dist);
1054         if(eed) {
1055                 
1056                 if((G.qual & LR_SHIFTKEY)==0) EM_clear_flag_all(SELECT);
1057                 
1058                 if((eed->f & SELECT)==0) EM_select_edge(eed, 1);
1059                 else if(G.qual & LR_SHIFTKEY) EM_select_edge(eed, 0);
1060
1061                 if(G.scene->selectmode & SCE_SELECT_FACE) {
1062                         faceloop_select(eed, eed->f & SELECT);
1063                 }
1064                 else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1065             if(G.qual == (LR_CTRLKEY | LR_ALTKEY) || G.qual == (LR_CTRLKEY | LR_ALTKEY |LR_SHIFTKEY))
1066                         edgering_select(eed, eed->f & SELECT);
1067             else if(G.qual & LR_ALTKEY)
1068                         edgeloop_select(eed, eed->f & SELECT);
1069                 }
1070         else if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1071             edgeloop_select(eed, eed->f & SELECT);
1072                 }
1073
1074                 /* frontbuffer draw of last selected only */
1075                 unified_select_draw(NULL, eed, NULL);
1076                 
1077                 EM_selectmode_flush();
1078                 countall();
1079                 
1080                 allqueue(REDRAWVIEW3D, 0);
1081         }
1082 }
1083
1084
1085 /* here actual select happens */
1086 void mouse_mesh(void)
1087 {
1088         EditVert *eve;
1089         EditEdge *eed;
1090         EditFace *efa;
1091         
1092         if(G.qual & LR_ALTKEY) mouse_mesh_loop();
1093         else if(unified_findnearest(&eve, &eed, &efa)) {
1094                 
1095                 if((G.qual & LR_SHIFTKEY)==0) EM_clear_flag_all(SELECT);
1096                 
1097                 if(efa) {
1098                         
1099                         if( (efa->f & SELECT)==0 ) {
1100                                 EM_select_face_fgon(efa, 1);
1101                         }
1102                         else if(G.qual & LR_SHIFTKEY) {
1103                                 EM_select_face_fgon(efa, 0);
1104                         }
1105                 }
1106                 else if(eed) {
1107                         if((eed->f & SELECT)==0) {
1108                                 EM_select_edge(eed, 1);
1109                         }
1110                         else if(G.qual & LR_SHIFTKEY) {
1111                                 EM_select_edge(eed, 0);
1112                         }
1113                 }
1114                 else if(eve) {
1115                         if((eve->f & SELECT)==0) eve->f |= SELECT;
1116                         else if(G.qual & LR_SHIFTKEY) eve->f &= ~SELECT;
1117                 }
1118                 
1119                 /* frontbuffer draw of last selected only */
1120                 unified_select_draw(eve, eed, efa);
1121         
1122                 EM_selectmode_flush();
1123                 countall();
1124
1125                 allqueue(REDRAWVIEW3D, 0);
1126         }
1127
1128         rightmouse_transform();
1129 }
1130
1131
1132 static void selectconnectedAll(void)
1133 {
1134         EditMesh *em = G.editMesh;
1135         EditVert *v1,*v2;
1136         EditEdge *eed;
1137         short done=1, toggle=0;
1138
1139         if(em->edges.first==0) return;
1140         
1141         while(done==1) {
1142                 done= 0;
1143                 
1144                 toggle++;
1145                 if(toggle & 1) eed= em->edges.first;
1146                 else eed= em->edges.last;
1147                 
1148                 while(eed) {
1149                         v1= eed->v1;
1150                         v2= eed->v2;
1151                         if(eed->h==0) {
1152                                 if(v1->f & SELECT) {
1153                                         if( (v2->f & SELECT)==0 ) {
1154                                                 v2->f |= SELECT;
1155                                                 done= 1;
1156                                         }
1157                                 }
1158                                 else if(v2->f & SELECT) {
1159                                         if( (v1->f & SELECT)==0 ) {
1160                                                 v1->f |= SELECT;
1161                                                 done= 1;
1162                                         }
1163                                 }
1164                         }
1165                         if(toggle & 1) eed= eed->next;
1166                         else eed= eed->prev;
1167                 }
1168         }
1169
1170         /* now use vertex select flag to select rest */
1171         EM_select_flush();
1172         
1173         countall();
1174
1175         allqueue(REDRAWVIEW3D, 0);
1176         BIF_undo_push("Select Connected (All)");
1177 }
1178
1179 void selectconnected_mesh(int qual)
1180 {
1181         EditMesh *em = G.editMesh;
1182         EditVert *eve, *v1, *v2;
1183         EditEdge *eed;
1184         EditFace *efa;
1185         short done=1, sel, toggle=0;
1186
1187         if(em->edges.first==0) return;
1188
1189         if(qual & LR_CTRLKEY) {
1190                 selectconnectedAll();
1191                 return;
1192         }
1193
1194         
1195         if( unified_findnearest(&eve, &eed, &efa)==0 ) {
1196                 error("Nothing indicated ");
1197                 return;
1198         }
1199         
1200         sel= 1;
1201         if(qual & LR_SHIFTKEY) sel=0;
1202
1203         /* clear test flags */
1204         for(v1= em->verts.first; v1; v1= v1->next) v1->f1= 0;
1205         
1206         /* start vertex/face/edge */
1207         if(eve) eve->f1= 1;
1208         else if(eed) eed->v1->f1= eed->v2->f1= 1;
1209         else efa->v1->f1= efa->v2->f1= efa->v3->f1= 1;
1210         
1211         /* set flag f1 if affected */
1212         while(done==1) {
1213                 done= 0;
1214                 toggle++;
1215                 
1216                 if(toggle & 1) eed= em->edges.first;
1217                 else eed= em->edges.last;
1218                 
1219                 while(eed) {
1220                         v1= eed->v1;
1221                         v2= eed->v2;
1222                         
1223                         if(eed->h==0) {
1224                                 if(v1->f1 && v2->f1==0) {
1225                                         v2->f1= 1;
1226                                         done= 1;
1227                                 }
1228                                 else if(v1->f1==0 && v2->f1) {
1229                                         v1->f1= 1;
1230                                         done= 1;
1231                                 }
1232                         }
1233                         
1234                         if(toggle & 1) eed= eed->next;
1235                         else eed= eed->prev;
1236                 }
1237         }
1238         
1239         /* now use vertex f1 flag to select/deselect */
1240         for(eed= em->edges.first; eed; eed= eed->next) {
1241                 if(eed->v1->f1 && eed->v2->f1) 
1242                         EM_select_edge(eed, sel);
1243         }
1244         for(efa= em->faces.first; efa; efa= efa->next) {
1245                 if(efa->v1->f1 && efa->v2->f1 && efa->v3->f1 && (efa->v4==NULL || efa->v4->f1)) 
1246                         EM_select_face(efa, sel);
1247         }
1248         /* no flush needed, connected geometry is done */
1249         
1250         countall();
1251         
1252         allqueue(REDRAWVIEW3D, 0);
1253         BIF_undo_push("Select Linked");
1254         
1255 }
1256
1257 /* swap is 0 or 1, if 1 it hides not selected */
1258 void hide_mesh(int swap)
1259 {
1260         EditMesh *em = G.editMesh;
1261         EditVert *eve;
1262         EditEdge *eed;
1263         EditFace *efa;
1264         int a;
1265         
1266         if(G.obedit==0) return;
1267
1268         /* hide happens on least dominant select mode, and flushes up, not down! (helps preventing errors in subsurf) */
1269         /*  - vertex hidden, always means edge is hidden too
1270                 - edge hidden, always means face is hidden too
1271                 - face hidden, only set face hide
1272                 - then only flush back down what's absolute hidden
1273         */
1274         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1275                 for(eve= em->verts.first; eve; eve= eve->next) {
1276                         if((eve->f & SELECT)!=swap) {
1277                                 eve->xs= 3200;
1278                                 eve->f &= ~SELECT;
1279                                 eve->h= 1;
1280                         }
1281                 }
1282         
1283                 for(eed= em->edges.first; eed; eed= eed->next) {
1284                         if(eed->v1->h || eed->v2->h) {
1285                                 eed->h |= 1;
1286                                 eed->f &= ~SELECT;
1287                         }
1288                 }
1289         
1290                 for(efa= em->faces.first; efa; efa= efa->next) {
1291                         if(efa->e1->h || efa->e2->h || efa->e3->h || (efa->e4 && efa->e4->h)) {
1292                                 efa->h= 1;
1293                                 efa->f &= ~SELECT;
1294                         }
1295                 }
1296         }
1297         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1298
1299                 for(eed= em->edges.first; eed; eed= eed->next) {
1300                         if((eed->f & SELECT)!=swap) {
1301                                 eed->h |= 1;
1302                                 EM_select_edge(eed, 0);
1303                         }
1304                 }
1305
1306                 for(efa= em->faces.first; efa; efa= efa->next) {
1307                         if(efa->e1->h || efa->e2->h || efa->e3->h || (efa->e4 && efa->e4->h)) {
1308                                 efa->h= 1;
1309                                 efa->f &= ~SELECT;
1310                         }
1311                 }
1312         }
1313         else {
1314
1315                 for(efa= em->faces.first; efa; efa= efa->next) {
1316                         if((efa->f & SELECT)!=swap) {
1317                                 efa->h= 1;
1318                                 EM_select_face(efa, 0);
1319                         }
1320                 }
1321         }
1322         
1323         /* flush down, only whats 100% hidden */
1324         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
1325         for(eed= em->edges.first; eed; eed= eed->next) eed->f1= 0;
1326         
1327         if(G.scene->selectmode & SCE_SELECT_FACE) {
1328                 for(efa= em->faces.first; efa; efa= efa->next) {
1329                         if(efa->h) a= 1; else a= 2;
1330                         efa->e1->f1 |= a;
1331                         efa->e2->f1 |= a;
1332                         efa->e3->f1 |= a;
1333                         if(efa->e4) efa->e4->f1 |= a;
1334                 }
1335         }
1336         
1337         if(G.scene->selectmode >= SCE_SELECT_EDGE) {
1338                 for(eed= em->edges.first; eed; eed= eed->next) {
1339                         if(eed->f1==1) eed->h |= 1;
1340                         if(eed->h & 1) a= 1; else a= 2;
1341                         eed->v1->f1 |= a;
1342                         eed->v2->f1 |= a;
1343                 }
1344         }
1345
1346         if(G.scene->selectmode >= SCE_SELECT_VERTEX) {
1347                 for(eve= em->verts.first; eve; eve= eve->next) {
1348                         if(eve->f1==1) eve->h= 1;
1349                 }
1350         }
1351                 
1352         allqueue(REDRAWVIEW3D, 0);
1353         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1354         BIF_undo_push("Hide");
1355 }
1356
1357
1358 void reveal_mesh(void)
1359 {
1360         EditMesh *em = G.editMesh;
1361         EditVert *eve;
1362         EditEdge *eed;
1363         EditFace *efa;
1364         
1365         if(G.obedit==0) return;
1366
1367         for(eve= em->verts.first; eve; eve= eve->next) {
1368                 if(eve->h) {
1369                         eve->h= 0;
1370                         eve->f |= SELECT;
1371                 }
1372         }
1373         for(eed= em->edges.first; eed; eed= eed->next) {
1374                 if(eed->h & 1) {
1375                         eed->h &= ~1;
1376                         if(G.scene->selectmode & SCE_SELECT_VERTEX); 
1377                         else EM_select_edge(eed, 1);
1378                 }
1379         }
1380         for(efa= em->faces.first; efa; efa= efa->next) {
1381                 if(efa->h) {
1382                         efa->h= 0;
1383                         if(G.scene->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_VERTEX)); 
1384                         else EM_select_face(efa, 1);
1385                 }
1386         }
1387
1388         EM_fgon_flags();        // redo flags and indices for fgons
1389         EM_selectmode_flush();
1390         
1391         allqueue(REDRAWVIEW3D, 0);
1392         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1393         BIF_undo_push("Reveal");
1394 }
1395
1396 void select_non_manifold(void)
1397 {
1398         EditMesh *em = G.editMesh;
1399         EditVert *eve;
1400         EditEdge *eed;
1401         EditFace *efa;
1402
1403         /* Selects isolated verts, and edges that do not have 2 neighboring
1404          * faces
1405          */
1406         
1407         if(G.scene->selectmode==SCE_SELECT_FACE) {
1408                 error("Doesn't work in face selection mode");
1409                 return;
1410         }
1411
1412         eve= em->verts.first;
1413         while(eve) {
1414                 /* this will count how many edges are connected
1415                  * to this vert */
1416                 eve->f1= 0;
1417                 eve= eve->next;
1418         }
1419
1420         eed= em->edges.first;
1421         while(eed) {
1422                 /* this will count how many faces are connected to
1423                  * this edge */
1424                 eed->f1= 0;
1425                 /* increase edge count for verts */
1426                 ++eed->v1->f1;
1427                 ++eed->v2->f1;
1428                 eed= eed->next;
1429         }
1430
1431         efa= em->faces.first;
1432         while(efa) {
1433                 /* increase face count for edges */
1434                 ++efa->e1->f1;
1435                 ++efa->e2->f1;
1436                 ++efa->e3->f1;
1437                 if (efa->e4)
1438                         ++efa->e4->f1;                  
1439                 efa= efa->next;
1440         }
1441
1442         /* select verts that are attached to an edge that does not
1443          * have 2 neighboring faces */
1444         eed= em->edges.first;
1445         while(eed) {
1446                 if (eed->h==0 && eed->f1 != 2) {
1447                         EM_select_edge(eed, 1);
1448                 }
1449                 eed= eed->next;
1450         }
1451
1452         /* select isolated verts */
1453         eve= em->verts.first;
1454         while(eve) {
1455                 if (eve->f1 == 0) {
1456                         if (!eve->h) eve->f |= SELECT;
1457                 }
1458                 eve= eve->next;
1459         }
1460
1461         countall();
1462         addqueue(curarea->win,  REDRAW, 0);
1463         BIF_undo_push("Select Non Manifold");
1464 }
1465
1466 void selectswap_mesh(void) /* UI level */
1467 {
1468         EditMesh *em = G.editMesh;
1469         EditVert *eve;
1470         EditEdge *eed;
1471         EditFace *efa;
1472         
1473         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1474
1475                 for(eve= em->verts.first; eve; eve= eve->next) {
1476                         if(eve->h==0) {
1477                                 if(eve->f & SELECT) eve->f &= ~SELECT;
1478                                 else eve->f|= SELECT;
1479                         }
1480                 }
1481         }
1482         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1483                 for(eed= em->edges.first; eed; eed= eed->next) {
1484                         if(eed->h==0) {
1485                                 EM_select_edge(eed, !(eed->f & SELECT));
1486                         }
1487                 }
1488         }
1489         else {
1490                 for(efa= em->faces.first; efa; efa= efa->next) {
1491                         if(efa->h==0) {
1492                                 EM_select_face(efa, !(efa->f & SELECT));
1493                         }
1494                 }
1495         }
1496
1497         EM_selectmode_flush();
1498         
1499         countall();
1500         allqueue(REDRAWVIEW3D, 0);
1501
1502         BIF_undo_push("Select Swap");
1503         
1504 }
1505
1506 void deselectall_mesh(void)      /* this toggles!!!, UI level */
1507 {
1508         
1509         if(G.obedit->lay & G.vd->lay) {
1510
1511                 if( EM_nvertices_selected() ) {
1512                         EM_clear_flag_all(SELECT);
1513                         BIF_undo_push("Deselect All");
1514                 }
1515                 else  {
1516                         EM_set_flag_all(SELECT);
1517                         BIF_undo_push("Select All");
1518                 }
1519                 
1520                 countall();
1521                 allqueue(REDRAWVIEW3D, 0);
1522         }
1523 }
1524
1525 void select_more(void)
1526 {
1527         EditMesh *em = G.editMesh;
1528         EditVert *eve;
1529         EditEdge *eed;
1530         EditFace *efa;
1531         
1532         for(eve= em->verts.first; eve; eve= eve->next) {
1533                 if(eve->f & SELECT) eve->f1= 1;
1534                 else eve->f1 = 0;
1535         }
1536         
1537         /* set f1 flags in vertices to select 'more' */
1538         for(eed= em->edges.first; eed; eed= eed->next) {
1539                 if(eed->h==0) {
1540                         if (eed->v1->f & SELECT)
1541                                 eed->v2->f1 = 1;
1542                         if (eed->v2->f & SELECT)
1543                                 eed->v1->f1 = 1;
1544                 }
1545         }
1546
1547         /* new selected edges, but not in facemode */
1548         if(G.scene->selectmode <= SCE_SELECT_EDGE) {
1549                 
1550                 for(eed= em->edges.first; eed; eed= eed->next) {
1551                         if(eed->h==0) {
1552                                 if(eed->v1->f1 && eed->v2->f1) EM_select_edge(eed, 1);
1553                         }
1554                 }
1555         }
1556         /* new selected faces */
1557         for(efa= em->faces.first; efa; efa= efa->next) {
1558                 if(efa->h==0) {
1559                         if(efa->v1->f1 && efa->v2->f1 && efa->v3->f1 && (efa->v4==NULL || efa->v4->f1)) 
1560                                 EM_select_face(efa, 1);
1561                 }
1562         }
1563
1564         countall();
1565         addqueue(curarea->win,  REDRAW, 0);
1566         BIF_undo_push("Select More");
1567 }
1568
1569 void select_less(void)
1570 {
1571         EditMesh *em = G.editMesh;
1572         EditEdge *eed;
1573         EditFace *efa;
1574
1575         if(G.scene->selectmode <= SCE_SELECT_EDGE) {
1576                 /* eed->f1 == 1:  edge with a selected and deselected vert */ 
1577
1578                 for(eed= em->edges.first; eed; eed= eed->next) {
1579                         eed->f1= 0;
1580                         if(eed->h==0) {
1581                                 
1582                                 if ( !(eed->v1->f & SELECT) && (eed->v2->f & SELECT) ) 
1583                                         eed->f1= 1;
1584                                 if ( (eed->v1->f & SELECT) && !(eed->v2->f & SELECT) ) 
1585                                         eed->f1= 1;
1586                         }
1587                 }
1588                 
1589                 /* deselect edges with flag set */
1590                 for(eed= em->edges.first; eed; eed= eed->next) {
1591                         if (eed->h==0 && eed->f1 == 1) {
1592                                 EM_select_edge(eed, 0);
1593                         }
1594                 }
1595                 EM_deselect_flush();
1596                 
1597         }
1598         else {
1599                 /* deselect faces with 1 or more deselect edges */
1600                 /* eed->f1 == mixed selection edge */
1601                 for(eed= em->edges.first; eed; eed= eed->next) eed->f1= 0;
1602
1603                 for(efa= em->faces.first; efa; efa= efa->next) {
1604                         if(efa->h==0) {
1605                                 if(efa->f & SELECT) {
1606                                         efa->e1->f1 |= 1;
1607                                         efa->e2->f1 |= 1;
1608                                         efa->e3->f1 |= 1;
1609                                         if(efa->e4) efa->e4->f1 |= 1;
1610                                 }
1611                                 else {
1612                                         efa->e1->f1 |= 2;
1613                                         efa->e2->f1 |= 2;
1614                                         efa->e3->f1 |= 2;
1615                                         if(efa->e4) efa->e4->f1 |= 2;
1616                                 }
1617                         }
1618                 }
1619                 for(efa= em->faces.first; efa; efa= efa->next) {
1620                         if(efa->h==0) {
1621                                 if(efa->e1->f1==3 || efa->e2->f1==3 || efa->e3->f1==3 || (efa->e4 && efa->e4->f1==3)) { 
1622                                         EM_select_face(efa, 0);
1623                                 }
1624                         }
1625                 }
1626                 EM_selectmode_flush();
1627                 
1628         }
1629         
1630         countall();
1631         allqueue(REDRAWVIEW3D, 0);
1632 }
1633
1634
1635 void selectrandom_mesh(void) /* randomly selects a user-set % of vertices/edges/faces */
1636 {
1637         EditMesh *em = G.editMesh;
1638         EditVert *eve;
1639         EditEdge *eed;
1640         EditFace *efa;
1641         short randfac = 50;
1642
1643         if(G.obedit==NULL || (G.obedit->lay & G.vd->lay)==0) return;
1644
1645         /* Get the percentage of vertices to randomly select as 'randfac' */
1646         if(button(&randfac,0, 100,"Percentage:")==0) return;
1647
1648         BLI_srand( BLI_rand() ); /* random seed */
1649         
1650         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1651                 for(eve= em->verts.first; eve; eve= eve->next) {
1652                         if(eve->h==0) {
1653                                 if ( (BLI_frand() * 100) < randfac) 
1654                                         eve->f |= SELECT;
1655                         }
1656                 }
1657         }
1658         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1659                 for(eed= em->edges.first; eed; eed= eed->next) {
1660                         if(eed->h==0) {
1661                                 if ( (BLI_frand() * 100) < randfac) 
1662                                         EM_select_edge(eed, 1);
1663                         }
1664                 }
1665         }
1666         else {
1667                 for(efa= em->faces.first; efa; efa= efa->next) {
1668                         if(efa->h==0) {
1669                                 if ( (BLI_frand() * 100) < randfac) 
1670                                         EM_select_face(efa, 1);
1671                         }
1672                 }
1673         }
1674         
1675         EM_selectmode_flush();
1676
1677         countall();
1678         allqueue(REDRAWVIEW3D, 0);
1679 }
1680
1681 void editmesh_select_by_material(int index) 
1682 {
1683         EditMesh *em = G.editMesh;
1684         EditFace *efa;
1685         
1686         for (efa=em->faces.first; efa; efa= efa->next) {
1687                 if (efa->mat_nr==index) {
1688                         EM_select_face(efa, 1);
1689                 }
1690         }
1691
1692         EM_selectmode_flush();
1693 }
1694
1695 void editmesh_deselect_by_material(int index) 
1696 {
1697         EditMesh *em = G.editMesh;
1698         EditFace *efa;
1699         
1700         for (efa=em->faces.first; efa; efa= efa->next) {
1701                 if (efa->mat_nr==index) {
1702                         EM_select_face(efa, 0);
1703                 }
1704         }
1705
1706         EM_selectmode_flush();
1707 }
1708
1709 void EM_selectmode_menu(void)
1710 {
1711         int val;
1712         
1713         if(G.scene->selectmode & SCE_SELECT_VERTEX) pupmenu_set_active(1);
1714         else if(G.scene->selectmode & SCE_SELECT_EDGE) pupmenu_set_active(2);
1715         else pupmenu_set_active(3);
1716         
1717         val= pupmenu("Select Mode%t|Vertices|Edges|Faces");
1718         if(val>0) {
1719                 if(val==1) G.scene->selectmode= SCE_SELECT_VERTEX;
1720                 else if(val==2) G.scene->selectmode= SCE_SELECT_EDGE;
1721                 else G.scene->selectmode= SCE_SELECT_FACE;
1722         
1723                 EM_selectmode_set(); // when mode changes
1724                 allqueue(REDRAWVIEW3D, 1);
1725         }
1726 }
1727
1728 /* ************************* SEAMS AND EDGES **************** */
1729
1730 void editmesh_mark_seam(int clear)
1731 {
1732         EditMesh *em= G.editMesh;
1733         EditEdge *eed;
1734         Mesh *me= G.obedit->data;
1735
1736         /* auto-enable seams drawing */
1737         if(clear==0) {
1738                 if(!(G.f & G_DRAWSEAMS)) {
1739                         G.f |= G_DRAWSEAMS;
1740                         allqueue(REDRAWBUTSEDIT, 0);
1741                 }
1742                 if(!me->medge)
1743                         me->medge= MEM_callocN(sizeof(MEdge), "fake mesh edge");
1744         }
1745
1746         if(clear) {
1747                 eed= em->edges.first;
1748                 while(eed) {
1749                         if((eed->h==0) && (eed->f & SELECT)) {
1750                                 eed->seam = 0;
1751                         }
1752                         eed= eed->next;
1753                 }
1754                 BIF_undo_push("Mark Seam");
1755         }
1756         else {
1757                 eed= em->edges.first;
1758                 while(eed) {
1759                         if((eed->h==0) && (eed->f & SELECT)) {
1760                                 eed->seam = 1;
1761                         }
1762                         eed= eed->next;
1763                 }
1764                 BIF_undo_push("Clear Seam");
1765         }
1766
1767         allqueue(REDRAWVIEW3D, 0);
1768 }
1769
1770 void Edge_Menu() {
1771         short ret;
1772
1773         ret= pupmenu("Edge Specials%t|Mark Seam %x1|Clear Seam %x2|Rotate Edge CW%x3|Rotate Edge CCW%x4|Loopcut%x6|Edge Slide%x5");
1774
1775         switch(ret)
1776         {
1777         case 1:
1778                 editmesh_mark_seam(0);
1779                 break;
1780         case 2:
1781                 editmesh_mark_seam(1);
1782                 break;
1783         case 3:
1784                 edge_rotate_selected(2);
1785                 break;
1786         case 4:
1787                 edge_rotate_selected(1);
1788                 break;
1789         case 5:
1790                 EdgeSlide(0,0.0);
1791         BIF_undo_push("EdgeSlide");
1792                 break;
1793         case 6:
1794         CutEdgeloop(1);
1795                 BIF_undo_push("Loopcut New");
1796                 break;
1797         }
1798 }
1799
1800
1801 /* **************** NORMALS ************** */
1802
1803 void righthandfaces(int select) /* makes faces righthand turning */
1804 {
1805         EditMesh *em = G.editMesh;
1806         EditEdge *eed, *ed1, *ed2, *ed3, *ed4;
1807         EditFace *efa, *startvl;
1808         float maxx, nor[3], cent[3];
1809         int totsel, found, foundone, direct, turn, tria_nr;
1810
1811    /* based at a select-connected to witness loose objects */
1812
1813         /* count per edge the amount of faces */
1814
1815         /* find the ultimate left, front, upper face (not manhattan dist!!) */
1816         /* also evaluate both triangle cases in quad, since these can be non-flat */
1817
1818         /* put normal to the outside, and set the first direction flags in edges */
1819
1820         /* then check the object, and set directions / direction-flags: but only for edges with 1 or 2 faces */
1821         /* this is in fact the 'select connected' */
1822         
1823         /* in case (selected) faces were not done: start over with 'find the ultimate ...' */
1824
1825         waitcursor(1);
1826         
1827         eed= em->edges.first;
1828         while(eed) {
1829                 eed->f2= 0;             // edge direction
1830                 eed->f1= 0;             // counter
1831                 eed= eed->next;
1832         }
1833
1834         /* count faces and edges */
1835         totsel= 0;
1836         efa= em->faces.first;
1837         while(efa) {
1838                 if(select==0 || (efa->f & SELECT) ) {
1839                         efa->f1= 1;
1840                         totsel++;
1841                         efa->e1->f1++;
1842                         efa->e2->f1++;
1843                         efa->e3->f1++;
1844                         if(efa->v4) efa->e4->f1++;
1845                 }
1846                 else efa->f1= 0;
1847
1848                 efa= efa->next;
1849         }
1850
1851         while(totsel>0) {
1852                 /* from the outside to the inside */
1853
1854                 efa= em->faces.first;
1855                 startvl= NULL;
1856                 maxx= -1.0e10;
1857                 tria_nr= 0;
1858
1859                 while(efa) {
1860                         if(efa->f1) {
1861                                 CalcCent3f(cent, efa->v1->co, efa->v2->co, efa->v3->co);
1862                                 cent[0]= cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
1863                                 
1864                                 if(cent[0]>maxx) {
1865                                         maxx= cent[0];
1866                                         startvl= efa;
1867                                         tria_nr= 0;
1868                                 }
1869                                 if(efa->v4) {
1870                                         CalcCent3f(cent, efa->v1->co, efa->v3->co, efa->v4->co);
1871                                         cent[0]= cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
1872                                         
1873                                         if(cent[0]>maxx) {
1874                                                 maxx= cent[0];
1875                                                 startvl= efa;
1876                                                 tria_nr= 1;
1877                                         }
1878                                 }
1879                         }
1880                         efa= efa->next;
1881                 }
1882                 
1883                 /* set first face correct: calc normal */
1884                 
1885                 if(tria_nr==1) {
1886                         CalcNormFloat(startvl->v1->co, startvl->v3->co, startvl->v4->co, nor);
1887                         CalcCent3f(cent, startvl->v1->co, startvl->v3->co, startvl->v4->co);
1888                 } else {
1889                         CalcNormFloat(startvl->v1->co, startvl->v2->co, startvl->v3->co, nor);
1890                         CalcCent3f(cent, startvl->v1->co, startvl->v2->co, startvl->v3->co);
1891                 }
1892                 /* first normal is oriented this way or the other */
1893                 if(select) {
1894                         if(select==2) {
1895                                 if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] > 0.0) flipface(startvl);
1896                         }
1897                         else {
1898                                 if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] < 0.0) flipface(startvl);
1899                         }
1900                 }
1901                 else if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] < 0.0) flipface(startvl);
1902
1903
1904                 eed= startvl->e1;
1905                 if(eed->v1==startvl->v1) eed->f2= 1; 
1906                 else eed->f2= 2;
1907                 
1908                 eed= startvl->e2;
1909                 if(eed->v1==startvl->v2) eed->f2= 1; 
1910                 else eed->f2= 2;
1911                 
1912                 eed= startvl->e3;
1913                 if(eed->v1==startvl->v3) eed->f2= 1; 
1914                 else eed->f2= 2;
1915                 
1916                 eed= startvl->e4;
1917                 if(eed) {
1918                         if(eed->v1==startvl->v4) eed->f2= 1; 
1919                         else eed->f2= 2;
1920                 }
1921                 
1922                 startvl->f1= 0;
1923                 totsel--;
1924
1925                 /* test normals */
1926                 found= 1;
1927                 direct= 1;
1928                 while(found) {
1929                         found= 0;
1930                         if(direct) efa= em->faces.first;
1931                         else efa= em->faces.last;
1932                         while(efa) {
1933                                 if(efa->f1) {
1934                                         turn= 0;
1935                                         foundone= 0;
1936
1937                                         ed1= efa->e1;
1938                                         ed2= efa->e2;
1939                                         ed3= efa->e3;
1940                                         ed4= efa->e4;
1941
1942                                         if(ed1->f2) {
1943                                                 if(ed1->v1==efa->v1 && ed1->f2==1) turn= 1;
1944                                                 if(ed1->v2==efa->v1 && ed1->f2==2) turn= 1;
1945                                                 foundone= 1;
1946                                         }
1947                                         else if(ed2->f2) {
1948                                                 if(ed2->v1==efa->v2 && ed2->f2==1) turn= 1;
1949                                                 if(ed2->v2==efa->v2 && ed2->f2==2) turn= 1;
1950                                                 foundone= 1;
1951                                         }
1952                                         else if(ed3->f2) {
1953                                                 if(ed3->v1==efa->v3 && ed3->f2==1) turn= 1;
1954                                                 if(ed3->v2==efa->v3 && ed3->f2==2) turn= 1;
1955                                                 foundone= 1;
1956                                         }
1957                                         else if(ed4 && ed4->f2) {
1958                                                 if(ed4->v1==efa->v4 && ed4->f2==1) turn= 1;
1959                                                 if(ed4->v2==efa->v4 && ed4->f2==2) turn= 1;
1960                                                 foundone= 1;
1961                                         }
1962
1963                                         if(foundone) {
1964                                                 found= 1;
1965                                                 totsel--;
1966                                                 efa->f1= 0;
1967
1968                                                 if(turn) {
1969                                                         if(ed1->v1==efa->v1) ed1->f2= 2; 
1970                                                         else ed1->f2= 1;
1971                                                         if(ed2->v1==efa->v2) ed2->f2= 2; 
1972                                                         else ed2->f2= 1;
1973                                                         if(ed3->v1==efa->v3) ed3->f2= 2; 
1974                                                         else ed3->f2= 1;
1975                                                         if(ed4) {
1976                                                                 if(ed4->v1==efa->v4) ed4->f2= 2; 
1977                                                                 else ed4->f2= 1;
1978                                                         }
1979
1980                                                         flipface(efa);
1981
1982                                                 }
1983                                                 else {
1984                                                         if(ed1->v1== efa->v1) ed1->f2= 1; 
1985                                                         else ed1->f2= 2;
1986                                                         if(ed2->v1==efa->v2) ed2->f2= 1; 
1987                                                         else ed2->f2= 2;
1988                                                         if(ed3->v1==efa->v3) ed3->f2= 1; 
1989                                                         else ed3->f2= 2;
1990                                                         if(ed4) {
1991                                                                 if(ed4->v1==efa->v4) ed4->f2= 1; 
1992                                                                 else ed4->f2= 2;
1993                                                         }
1994                                                 }
1995                                         }
1996                                 }
1997                                 if(direct) efa= efa->next;
1998                                 else efa= efa->prev;
1999                         }
2000                         direct= 1-direct;
2001                 }
2002         }
2003
2004         recalc_editnormals();
2005         
2006         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2007         
2008         waitcursor(0);
2009 }
2010
2011
2012 /* ********** ALIGN WITH VIEW **************** */
2013
2014
2015 static void editmesh_calc_selvert_center(float cent_r[3])
2016 {
2017         EditMesh *em = G.editMesh;
2018         EditVert *eve;
2019         int nsel= 0;
2020
2021         cent_r[0]= cent_r[1]= cent_r[0]= 0.0;
2022
2023         for (eve= em->verts.first; eve; eve= eve->next) {
2024                 if (eve->f & SELECT) {
2025                         cent_r[0]+= eve->co[0];
2026                         cent_r[1]+= eve->co[1];
2027                         cent_r[2]+= eve->co[2];
2028                         nsel++;
2029                 }
2030         }
2031
2032         if (nsel) {
2033                 cent_r[0]/= nsel;
2034                 cent_r[1]/= nsel;
2035                 cent_r[2]/= nsel;
2036         }
2037 }
2038
2039 static int tface_is_selected(TFace *tf)
2040 {
2041         return (!(tf->flag & TF_HIDE) && (tf->flag & TF_SELECT));
2042 }
2043
2044 static int faceselect_nfaces_selected(Mesh *me)
2045 {
2046         int i, count= 0;
2047
2048         for (i=0; i<me->totface; i++) {
2049                 MFace *mf= ((MFace*) me->mface) + i;
2050                 TFace *tf= ((TFace*) me->tface) + i;
2051
2052                 if (mf->v3 && tface_is_selected(tf))
2053                         count++;
2054         }
2055
2056         return count;
2057 }
2058
2059         /* XXX, code for both these functions should be abstract,
2060          * then unified, then written for other things (like objects,
2061          * which would use same as vertices method), then added
2062          * to interface! Hoera! - zr
2063          */
2064 void faceselect_align_view_to_selected(View3D *v3d, Mesh *me, int axis)
2065 {
2066         if (!faceselect_nfaces_selected(me)) {
2067                 error("No faces selected.");
2068         } else {
2069                 float norm[3];
2070                 int i;
2071
2072                 norm[0]= norm[1]= norm[2]= 0.0;
2073                 for (i=0; i<me->totface; i++) {
2074                         MFace *mf= ((MFace*) me->mface) + i;
2075                         TFace *tf= ((TFace*) me->tface) + i;
2076         
2077                         if (mf->v3 && tface_is_selected(tf)) {
2078                                 float *v1, *v2, *v3, fno[3];
2079
2080                                 v1= me->mvert[mf->v1].co;
2081                                 v2= me->mvert[mf->v2].co;
2082                                 v3= me->mvert[mf->v3].co;
2083                                 if (mf->v4) {
2084                                         float *v4= me->mvert[mf->v4].co;
2085                                         CalcNormFloat4(v1, v2, v3, v4, fno);
2086                                 } else {
2087                                         CalcNormFloat(v1, v2, v3, fno);
2088                                 }
2089
2090                                 norm[0]+= fno[0];
2091                                 norm[1]+= fno[1];
2092                                 norm[2]+= fno[2];
2093                         }
2094                 }
2095
2096                 view3d_align_axis_to_vector(v3d, axis, norm);
2097         }
2098 }
2099
2100 void editmesh_align_view_to_selected(View3D *v3d, int axis)
2101 {
2102         EditMesh *em = G.editMesh;
2103         int nselverts= EM_nvertices_selected();
2104
2105         if (nselverts<3) {
2106                 if (nselverts==0) {
2107                         error("No faces or vertices selected.");
2108                 } else {
2109                         error("At least one face or three vertices must be selected.");
2110                 }
2111         } else if (EM_nfaces_selected()) {
2112                 float norm[3];
2113                 EditFace *efa;
2114
2115                 norm[0]= norm[1]= norm[2]= 0.0;
2116                 for (efa= em->faces.first; efa; efa= efa->next) {
2117                         if (faceselectedAND(efa, SELECT)) {
2118                                 float fno[3];
2119                                 if (efa->v4) CalcNormFloat4(efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co, fno);
2120                                 else CalcNormFloat(efa->v1->co, efa->v2->co, efa->v3->co, fno);
2121                                                 /* XXX, fixme, should be flipped intp a 
2122                                                  * consistent direction. -zr
2123                                                  */
2124                                 norm[0]+= fno[0];
2125                                 norm[1]+= fno[1];
2126                                 norm[2]+= fno[2];
2127                         }
2128                 }
2129
2130                 Mat4Mul3Vecfl(G.obedit->obmat, norm);
2131                 view3d_align_axis_to_vector(v3d, axis, norm);
2132         } else {
2133                 float cent[3], norm[3];
2134                 EditVert *eve, *leve= NULL;
2135
2136                 norm[0]= norm[1]= norm[2]= 0.0;
2137                 editmesh_calc_selvert_center(cent);
2138                 for (eve= em->verts.first; eve; eve= eve->next) {
2139                         if (eve->f & SELECT) {
2140                                 if (leve) {
2141                                         float tno[3];
2142                                         CalcNormFloat(cent, leve->co, eve->co, tno);
2143                                         
2144                                                 /* XXX, fixme, should be flipped intp a 
2145                                                  * consistent direction. -zr
2146                                                  */
2147                                         norm[0]+= tno[0];
2148                                         norm[1]+= tno[1];
2149                                         norm[2]+= tno[2];
2150                                 }
2151                                 leve= eve;
2152                         }
2153                 }
2154
2155                 Mat4Mul3Vecfl(G.obedit->obmat, norm);
2156                 view3d_align_axis_to_vector(v3d, axis, norm);
2157         }
2158 }
2159
2160 /* **************** VERTEX DEFORMS *************** */
2161
2162 void vertexsmooth(void)
2163 {
2164         EditMesh *em = G.editMesh;
2165         EditVert *eve;
2166         EditEdge *eed;
2167         float *adror, *adr, fac;
2168         float fvec[3];
2169         int teller=0;
2170
2171         if(G.obedit==0) return;
2172
2173         /* count */
2174         eve= em->verts.first;
2175         while(eve) {
2176                 if(eve->f & SELECT) teller++;
2177                 eve= eve->next;
2178         }
2179         if(teller==0) return;
2180         
2181         adr=adror= (float *)MEM_callocN(3*sizeof(float *)*teller, "vertsmooth");
2182         eve= em->verts.first;
2183         while(eve) {
2184                 if(eve->f & SELECT) {
2185                         eve->vn= (EditVert *)adr;
2186                         eve->f1= 0;
2187                         adr+= 3;
2188                 }
2189                 eve= eve->next;
2190         }
2191         
2192         eed= em->edges.first;
2193         while(eed) {
2194                 if( (eed->v1->f & SELECT) || (eed->v2->f & SELECT) ) {
2195                         fvec[0]= (eed->v1->co[0]+eed->v2->co[0])/2.0;
2196                         fvec[1]= (eed->v1->co[1]+eed->v2->co[1])/2.0;
2197                         fvec[2]= (eed->v1->co[2]+eed->v2->co[2])/2.0;
2198                         
2199                         if((eed->v1->f & SELECT) && eed->v1->f1<255) {
2200                                 eed->v1->f1++;
2201                                 VecAddf((float *)eed->v1->vn, (float *)eed->v1->vn, fvec);
2202                         }
2203                         if((eed->v2->f & SELECT) && eed->v2->f1<255) {
2204                                 eed->v2->f1++;
2205                                 VecAddf((float *)eed->v2->vn, (float *)eed->v2->vn, fvec);
2206                         }
2207                 }
2208                 eed= eed->next;
2209         }
2210
2211         eve= em->verts.first;
2212         while(eve) {
2213                 if(eve->f & SELECT) {
2214                         if(eve->f1) {
2215                                 adr= (float *)eve->vn;
2216                                 fac= 0.5/(float)eve->f1;
2217                                 
2218                                 eve->co[0]= 0.5*eve->co[0]+fac*adr[0];
2219                                 eve->co[1]= 0.5*eve->co[1]+fac*adr[1];
2220                                 eve->co[2]= 0.5*eve->co[2]+fac*adr[2];
2221                         }
2222                         eve->vn= 0;
2223                 }
2224                 eve= eve->next;
2225         }
2226         MEM_freeN(adror);
2227
2228         recalc_editnormals();
2229
2230         allqueue(REDRAWVIEW3D, 0);
2231         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2232         BIF_undo_push("Vertex Smooth");
2233 }
2234
2235 void vertexnoise(void)
2236 {
2237         EditMesh *em = G.editMesh;
2238         Material *ma;
2239         Tex *tex;
2240         EditVert *eve;
2241         float b2, ofs, vec[3];
2242
2243         if(G.obedit==0) return;
2244         
2245         ma= give_current_material(G.obedit, G.obedit->actcol);
2246         if(ma==0 || ma->mtex[0]==0 || ma->mtex[0]->tex==0) {
2247                 return;
2248         }
2249         tex= ma->mtex[0]->tex;
2250         
2251         ofs= tex->turbul/200.0;
2252         
2253         eve= (struct EditVert *)em->verts.first;
2254         while(eve) {
2255                 if(eve->f & SELECT) {
2256                         
2257                         if(tex->type==TEX_STUCCI) {
2258                                 
2259                                 b2= BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1], eve->co[2]);
2260                                 if(tex->stype) ofs*=(b2*b2);
2261                                 vec[0]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0]+ofs, eve->co[1], eve->co[2]));
2262                                 vec[1]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1]+ofs, eve->co[2]));
2263                                 vec[2]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1], eve->co[2]+ofs));
2264                                 
2265                                 VecAddf(eve->co, eve->co, vec);
2266                         }
2267                         else {
2268                                 float tin, dum;
2269                                 externtex(ma->mtex[0], eve->co, &tin, &dum, &dum, &dum, &dum);
2270                                 eve->co[2]+= 0.05*tin;
2271                         }
2272                 }
2273                 eve= eve->next;
2274         }
2275
2276         recalc_editnormals();
2277         allqueue(REDRAWVIEW3D, 0);
2278         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2279         BIF_undo_push("Vertex Noise");
2280 }
2281
2282 void vertices_to_sphere(void)
2283 {
2284         EditMesh *em = G.editMesh;
2285         EditVert *eve;
2286         Object *ob= OBACT;
2287         float *curs, len, vec[3], cent[3], fac, facm, imat[3][3], bmat[3][3];
2288         int tot;
2289         short perc=100;
2290         
2291         if(ob==0) return;
2292         TEST_EDITMESH
2293         
2294         if(button(&perc, 1, 100, "Percentage:")==0) return;
2295         
2296         fac= perc/100.0;
2297         facm= 1.0-fac;
2298         
2299         Mat3CpyMat4(bmat, ob->obmat);
2300         Mat3Inv(imat, bmat);
2301
2302         /* centre */
2303         curs= give_cursor();
2304         cent[0]= curs[0]-ob->obmat[3][0];
2305         cent[1]= curs[1]-ob->obmat[3][1];
2306         cent[2]= curs[2]-ob->obmat[3][2];
2307         Mat3MulVecfl(imat, cent);
2308
2309         len= 0.0;
2310         tot= 0;
2311         eve= em->verts.first;
2312         while(eve) {
2313                 if(eve->f & SELECT) {
2314                         tot++;
2315                         len+= VecLenf(cent, eve->co);
2316                 }
2317                 eve= eve->next;
2318         }
2319         len/=tot;
2320         
2321         if(len==0.0) len= 10.0;
2322         
2323         eve= em->verts.first;
2324         while(eve) {
2325                 if(eve->f & SELECT) {
2326                         vec[0]= eve->co[0]-cent[0];
2327                         vec[1]= eve->co[1]-cent[1];
2328                         vec[2]= eve->co[2]-cent[2];
2329                         
2330                         Normalise(vec);
2331                         
2332                         eve->co[0]= fac*(cent[0]+vec[0]*len) + facm*eve->co[0];
2333                         eve->co[1]= fac*(cent[1]+vec[1]*len) + facm*eve->co[1];
2334                         eve->co[2]= fac*(cent[2]+vec[2]*len) + facm*eve->co[2];
2335                         
2336                 }
2337                 eve= eve->next;
2338         }
2339         
2340         recalc_editnormals();
2341         allqueue(REDRAWVIEW3D, 0);
2342         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2343         BIF_undo_push("To Sphere");
2344 }
2345