Result of 2 weeks of quiet coding work in Greece :)
[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 /* ***************** MAIN MOUSE SELECTION ************** */
978
979 // just to have the functions nice together
980 static void mouse_mesh_loop(void)
981 {
982         EditEdge *eed;
983         short dist= 50;
984         
985         eed= findnearestedge(&dist);
986         if(eed) {
987                 
988                 if((G.qual & LR_SHIFTKEY)==0) EM_clear_flag_all(SELECT);
989                 
990                 if((eed->f & SELECT)==0) EM_select_edge(eed, 1);
991                 else if(G.qual & LR_SHIFTKEY) EM_select_edge(eed, 0);
992
993                 if(G.scene->selectmode & SCE_SELECT_FACE) {
994                         faceloop_select(eed, eed->f & SELECT);
995                 }
996                 else {
997                         edgeloop_select(eed, eed->f & SELECT);
998                 }
999
1000                 /* frontbuffer draw of last selected only */
1001                 unified_select_draw(NULL, eed, NULL);
1002                 
1003                 EM_selectmode_flush();
1004                 countall();
1005                 
1006                 allqueue(REDRAWVIEW3D, 0);
1007         }
1008 }
1009
1010
1011 /* here actual select happens */
1012 void mouse_mesh(void)
1013 {
1014         EditVert *eve;
1015         EditEdge *eed;
1016         EditFace *efa;
1017         
1018         if(G.qual & LR_ALTKEY) mouse_mesh_loop();
1019         else if(unified_findnearest(&eve, &eed, &efa)) {
1020                 
1021                 if((G.qual & LR_SHIFTKEY)==0) EM_clear_flag_all(SELECT);
1022                 
1023                 if(efa) {
1024                         
1025                         if( (efa->f & SELECT)==0 ) {
1026                                 EM_select_face_fgon(efa, 1);
1027                         }
1028                         else if(G.qual & LR_SHIFTKEY) {
1029                                 EM_select_face_fgon(efa, 0);
1030                         }
1031                 }
1032                 else if(eed) {
1033                         if((eed->f & SELECT)==0) {
1034                                 EM_select_edge(eed, 1);
1035                         }
1036                         else if(G.qual & LR_SHIFTKEY) {
1037                                 EM_select_edge(eed, 0);
1038                         }
1039                 }
1040                 else if(eve) {
1041                         if((eve->f & SELECT)==0) eve->f |= SELECT;
1042                         else if(G.qual & LR_SHIFTKEY) eve->f &= ~SELECT;
1043                 }
1044                 
1045                 /* frontbuffer draw of last selected only */
1046                 unified_select_draw(eve, eed, efa);
1047         
1048                 EM_selectmode_flush();
1049                 countall();
1050
1051                 allqueue(REDRAWVIEW3D, 0);
1052         }
1053
1054         rightmouse_transform();
1055 }
1056
1057
1058 static void selectconnectedAll(void)
1059 {
1060         EditMesh *em = G.editMesh;
1061         EditVert *v1,*v2;
1062         EditEdge *eed;
1063         short done=1, toggle=0;
1064
1065         if(em->edges.first==0) return;
1066         
1067         while(done==1) {
1068                 done= 0;
1069                 
1070                 toggle++;
1071                 if(toggle & 1) eed= em->edges.first;
1072                 else eed= em->edges.last;
1073                 
1074                 while(eed) {
1075                         v1= eed->v1;
1076                         v2= eed->v2;
1077                         if(eed->h==0) {
1078                                 if(v1->f & SELECT) {
1079                                         if( (v2->f & SELECT)==0 ) {
1080                                                 v2->f |= SELECT;
1081                                                 done= 1;
1082                                         }
1083                                 }
1084                                 else if(v2->f & SELECT) {
1085                                         if( (v1->f & SELECT)==0 ) {
1086                                                 v1->f |= SELECT;
1087                                                 done= 1;
1088                                         }
1089                                 }
1090                         }
1091                         if(toggle & 1) eed= eed->next;
1092                         else eed= eed->prev;
1093                 }
1094         }
1095
1096         /* now use vertex select flag to select rest */
1097         EM_select_flush();
1098         
1099         countall();
1100
1101         allqueue(REDRAWVIEW3D, 0);
1102         BIF_undo_push("Select Connected (All)");
1103 }
1104
1105 void selectconnected_mesh(int qual)
1106 {
1107         EditMesh *em = G.editMesh;
1108         EditVert *eve, *v1, *v2;
1109         EditEdge *eed;
1110         EditFace *efa;
1111         short done=1, sel, toggle=0;
1112
1113         if(em->edges.first==0) return;
1114
1115         if(qual & LR_CTRLKEY) {
1116                 selectconnectedAll();
1117                 return;
1118         }
1119
1120         
1121         if( unified_findnearest(&eve, &eed, &efa)==0 ) {
1122                 error("Nothing indicated ");
1123                 return;
1124         }
1125         
1126         sel= 1;
1127         if(qual & LR_SHIFTKEY) sel=0;
1128
1129         /* clear test flags */
1130         for(v1= em->verts.first; v1; v1= v1->next) v1->f1= 0;
1131         
1132         /* start vertex/face/edge */
1133         if(eve) eve->f1= 1;
1134         else if(eed) eed->v1->f1= eed->v2->f1= 1;
1135         else efa->v1->f1= efa->v2->f1= efa->v3->f1= 1;
1136         
1137         /* set flag f1 if affected */
1138         while(done==1) {
1139                 done= 0;
1140                 toggle++;
1141                 
1142                 if(toggle & 1) eed= em->edges.first;
1143                 else eed= em->edges.last;
1144                 
1145                 while(eed) {
1146                         v1= eed->v1;
1147                         v2= eed->v2;
1148                         
1149                         if(eed->h==0) {
1150                                 if(v1->f1 && v2->f1==0) {
1151                                         v2->f1= 1;
1152                                         done= 1;
1153                                 }
1154                                 else if(v1->f1==0 && v2->f1) {
1155                                         v1->f1= 1;
1156                                         done= 1;
1157                                 }
1158                         }
1159                         
1160                         if(toggle & 1) eed= eed->next;
1161                         else eed= eed->prev;
1162                 }
1163         }
1164         
1165         /* now use vertex f1 flag to select/deselect */
1166         for(eed= em->edges.first; eed; eed= eed->next) {
1167                 if(eed->v1->f1 && eed->v2->f1) 
1168                         EM_select_edge(eed, sel);
1169         }
1170         for(efa= em->faces.first; efa; efa= efa->next) {
1171                 if(efa->v1->f1 && efa->v2->f1 && efa->v3->f1 && (efa->v4==NULL || efa->v4->f1)) 
1172                         EM_select_face(efa, sel);
1173         }
1174         /* no flush needed, connected geometry is done */
1175         
1176         countall();
1177         
1178         allqueue(REDRAWVIEW3D, 0);
1179         BIF_undo_push("Select Linked");
1180         
1181 }
1182
1183 /* swap is 0 or 1, if 1 it hides not selected */
1184 void hide_mesh(int swap)
1185 {
1186         EditMesh *em = G.editMesh;
1187         EditVert *eve;
1188         EditEdge *eed;
1189         EditFace *efa;
1190         int a;
1191         
1192         if(G.obedit==0) return;
1193
1194         /* hide happens on least dominant select mode, and flushes up, not down! (helps preventing errors in subsurf) */
1195         /*  - vertex hidden, always means edge is hidden too
1196                 - edge hidden, always means face is hidden too
1197                 - face hidden, only set face hide
1198                 - then only flush back down what's absolute hidden
1199         */
1200         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1201                 for(eve= em->verts.first; eve; eve= eve->next) {
1202                         if((eve->f & SELECT)!=swap) {
1203                                 eve->xs= 3200;
1204                                 eve->f &= ~SELECT;
1205                                 eve->h= 1;
1206                         }
1207                 }
1208         
1209                 for(eed= em->edges.first; eed; eed= eed->next) {
1210                         if(eed->v1->h || eed->v2->h) {
1211                                 eed->h |= 1;
1212                                 eed->f &= ~SELECT;
1213                         }
1214                 }
1215         
1216                 for(efa= em->faces.first; efa; efa= efa->next) {
1217                         if(efa->e1->h || efa->e2->h || efa->e3->h || (efa->e4 && efa->e4->h)) {
1218                                 efa->h= 1;
1219                                 efa->f &= ~SELECT;
1220                         }
1221                 }
1222         }
1223         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1224
1225                 for(eed= em->edges.first; eed; eed= eed->next) {
1226                         if((eed->f & SELECT)!=swap) {
1227                                 eed->h |= 1;
1228                                 EM_select_edge(eed, 0);
1229                         }
1230                 }
1231
1232                 for(efa= em->faces.first; efa; efa= efa->next) {
1233                         if(efa->e1->h || efa->e2->h || efa->e3->h || (efa->e4 && efa->e4->h)) {
1234                                 efa->h= 1;
1235                                 efa->f &= ~SELECT;
1236                         }
1237                 }
1238         }
1239         else {
1240
1241                 for(efa= em->faces.first; efa; efa= efa->next) {
1242                         if((efa->f & SELECT)!=swap) {
1243                                 efa->h= 1;
1244                                 EM_select_face(efa, 0);
1245                         }
1246                 }
1247         }
1248         
1249         /* flush down, only whats 100% hidden */
1250         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
1251         for(eed= em->edges.first; eed; eed= eed->next) eed->f1= 0;
1252         
1253         if(G.scene->selectmode & SCE_SELECT_FACE) {
1254                 for(efa= em->faces.first; efa; efa= efa->next) {
1255                         if(efa->h) a= 1; else a= 2;
1256                         efa->e1->f1 |= a;
1257                         efa->e2->f1 |= a;
1258                         efa->e3->f1 |= a;
1259                         if(efa->e4) efa->e4->f1 |= a;
1260                 }
1261         }
1262         
1263         if(G.scene->selectmode >= SCE_SELECT_EDGE) {
1264                 for(eed= em->edges.first; eed; eed= eed->next) {
1265                         if(eed->f1==1) eed->h |= 1;
1266                         if(eed->h & 1) a= 1; else a= 2;
1267                         eed->v1->f1 |= a;
1268                         eed->v2->f1 |= a;
1269                 }
1270         }
1271
1272         if(G.scene->selectmode >= SCE_SELECT_VERTEX) {
1273                 for(eve= em->verts.first; eve; eve= eve->next) {
1274                         if(eve->f1==1) eve->h= 1;
1275                 }
1276         }
1277                 
1278         allqueue(REDRAWVIEW3D, 0);
1279         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1280         BIF_undo_push("Hide");
1281 }
1282
1283
1284 void reveal_mesh(void)
1285 {
1286         EditMesh *em = G.editMesh;
1287         EditVert *eve;
1288         EditEdge *eed;
1289         EditFace *efa;
1290         
1291         if(G.obedit==0) return;
1292
1293         for(eve= em->verts.first; eve; eve= eve->next) {
1294                 if(eve->h) {
1295                         eve->h= 0;
1296                         eve->f |= SELECT;
1297                 }
1298         }
1299         for(eed= em->edges.first; eed; eed= eed->next) {
1300                 if(eed->h & 1) {
1301                         eed->h &= ~1;
1302                         if(G.scene->selectmode & SCE_SELECT_VERTEX); 
1303                         else EM_select_edge(eed, 1);
1304                 }
1305         }
1306         for(efa= em->faces.first; efa; efa= efa->next) {
1307                 if(efa->h) {
1308                         efa->h= 0;
1309                         if(G.scene->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_VERTEX)); 
1310                         else EM_select_face(efa, 1);
1311                 }
1312         }
1313
1314         EM_fgon_flags();        // redo flags and indices for fgons
1315         EM_selectmode_flush();
1316         
1317         allqueue(REDRAWVIEW3D, 0);
1318         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1319         BIF_undo_push("Reveal");
1320 }
1321
1322 void select_non_manifold(void)
1323 {
1324         EditMesh *em = G.editMesh;
1325         EditVert *eve;
1326         EditEdge *eed;
1327         EditFace *efa;
1328
1329         /* Selects isolated verts, and edges that do not have 2 neighboring
1330          * faces
1331          */
1332         
1333         if(G.scene->selectmode==SCE_SELECT_FACE) {
1334                 error("Doesn't work in face selection mode");
1335                 return;
1336         }
1337
1338         eve= em->verts.first;
1339         while(eve) {
1340                 /* this will count how many edges are connected
1341                  * to this vert */
1342                 eve->f1= 0;
1343                 eve= eve->next;
1344         }
1345
1346         eed= em->edges.first;
1347         while(eed) {
1348                 /* this will count how many faces are connected to
1349                  * this edge */
1350                 eed->f1= 0;
1351                 /* increase edge count for verts */
1352                 ++eed->v1->f1;
1353                 ++eed->v2->f1;
1354                 eed= eed->next;
1355         }
1356
1357         efa= em->faces.first;
1358         while(efa) {
1359                 /* increase face count for edges */
1360                 ++efa->e1->f1;
1361                 ++efa->e2->f1;
1362                 ++efa->e3->f1;
1363                 if (efa->e4)
1364                         ++efa->e4->f1;                  
1365                 efa= efa->next;
1366         }
1367
1368         /* select verts that are attached to an edge that does not
1369          * have 2 neighboring faces */
1370         eed= em->edges.first;
1371         while(eed) {
1372                 if (eed->h==0 && eed->f1 != 2) {
1373                         EM_select_edge(eed, 1);
1374                 }
1375                 eed= eed->next;
1376         }
1377
1378         /* select isolated verts */
1379         eve= em->verts.first;
1380         while(eve) {
1381                 if (eve->f1 == 0) {
1382                         if (!eve->h) eve->f |= SELECT;
1383                 }
1384                 eve= eve->next;
1385         }
1386
1387         countall();
1388         addqueue(curarea->win,  REDRAW, 0);
1389         BIF_undo_push("Select Non Manifold");
1390 }
1391
1392 void selectswap_mesh(void) /* UI level */
1393 {
1394         EditMesh *em = G.editMesh;
1395         EditVert *eve;
1396         EditEdge *eed;
1397         EditFace *efa;
1398         
1399         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1400
1401                 for(eve= em->verts.first; eve; eve= eve->next) {
1402                         if(eve->h==0) {
1403                                 if(eve->f & SELECT) eve->f &= ~SELECT;
1404                                 else eve->f|= SELECT;
1405                         }
1406                 }
1407         }
1408         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1409                 for(eed= em->edges.first; eed; eed= eed->next) {
1410                         if(eed->h==0) {
1411                                 EM_select_edge(eed, !(eed->f & SELECT));
1412                         }
1413                 }
1414         }
1415         else {
1416                 for(efa= em->faces.first; efa; efa= efa->next) {
1417                         if(efa->h==0) {
1418                                 EM_select_face(efa, !(efa->f & SELECT));
1419                         }
1420                 }
1421         }
1422
1423         EM_selectmode_flush();
1424         
1425         countall();
1426         allqueue(REDRAWVIEW3D, 0);
1427
1428         BIF_undo_push("Select Swap");
1429         
1430 }
1431
1432 void deselectall_mesh(void)      /* this toggles!!!, UI level */
1433 {
1434         
1435         if(G.obedit->lay & G.vd->lay) {
1436
1437                 if( EM_nvertices_selected() ) {
1438                         EM_clear_flag_all(SELECT);
1439                         BIF_undo_push("Deselect All");
1440                 }
1441                 else  {
1442                         EM_set_flag_all(SELECT);
1443                         BIF_undo_push("Select All");
1444                 }
1445                 
1446                 countall();
1447                 allqueue(REDRAWVIEW3D, 0);
1448         }
1449 }
1450
1451 void select_more(void)
1452 {
1453         EditMesh *em = G.editMesh;
1454         EditVert *eve;
1455         EditEdge *eed;
1456         EditFace *efa;
1457         
1458         for(eve= em->verts.first; eve; eve= eve->next) {
1459                 if(eve->f & SELECT) eve->f1= 1;
1460                 else eve->f1 = 0;
1461         }
1462         
1463         /* set f1 flags in vertices to select 'more' */
1464         for(eed= em->edges.first; eed; eed= eed->next) {
1465                 if(eed->h==0) {
1466                         if (eed->v1->f & SELECT)
1467                                 eed->v2->f1 = 1;
1468                         if (eed->v2->f & SELECT)
1469                                 eed->v1->f1 = 1;
1470                 }
1471         }
1472
1473         /* new selected edges, but not in facemode */
1474         if(G.scene->selectmode <= SCE_SELECT_EDGE) {
1475                 
1476                 for(eed= em->edges.first; eed; eed= eed->next) {
1477                         if(eed->h==0) {
1478                                 if(eed->v1->f1 && eed->v2->f1) EM_select_edge(eed, 1);
1479                         }
1480                 }
1481         }
1482         /* new selected faces */
1483         for(efa= em->faces.first; efa; efa= efa->next) {
1484                 if(efa->h==0) {
1485                         if(efa->v1->f1 && efa->v2->f1 && efa->v3->f1 && (efa->v4==NULL || efa->v4->f1)) 
1486                                 EM_select_face(efa, 1);
1487                 }
1488         }
1489
1490         countall();
1491         addqueue(curarea->win,  REDRAW, 0);
1492         BIF_undo_push("Select More");
1493 }
1494
1495 void select_less(void)
1496 {
1497         EditMesh *em = G.editMesh;
1498         EditEdge *eed;
1499         EditFace *efa;
1500
1501         if(G.scene->selectmode <= SCE_SELECT_EDGE) {
1502                 /* eed->f1 == 1:  edge with a selected and deselected vert */ 
1503
1504                 for(eed= em->edges.first; eed; eed= eed->next) {
1505                         eed->f1= 0;
1506                         if(eed->h==0) {
1507                                 
1508                                 if ( !(eed->v1->f & SELECT) && (eed->v2->f & SELECT) ) 
1509                                         eed->f1= 1;
1510                                 if ( (eed->v1->f & SELECT) && !(eed->v2->f & SELECT) ) 
1511                                         eed->f1= 1;
1512                         }
1513                 }
1514                 
1515                 /* deselect edges with flag set */
1516                 for(eed= em->edges.first; eed; eed= eed->next) {
1517                         if (eed->h==0 && eed->f1 == 1) {
1518                                 EM_select_edge(eed, 0);
1519                         }
1520                 }
1521                 EM_deselect_flush();
1522                 
1523         }
1524         else {
1525                 /* deselect faces with 1 or more deselect edges */
1526                 /* eed->f1 == mixed selection edge */
1527                 for(eed= em->edges.first; eed; eed= eed->next) eed->f1= 0;
1528
1529                 for(efa= em->faces.first; efa; efa= efa->next) {
1530                         if(efa->h==0) {
1531                                 if(efa->f & SELECT) {
1532                                         efa->e1->f1 |= 1;
1533                                         efa->e2->f1 |= 1;
1534                                         efa->e3->f1 |= 1;
1535                                         if(efa->e4) efa->e4->f1 |= 1;
1536                                 }
1537                                 else {
1538                                         efa->e1->f1 |= 2;
1539                                         efa->e2->f1 |= 2;
1540                                         efa->e3->f1 |= 2;
1541                                         if(efa->e4) efa->e4->f1 |= 2;
1542                                 }
1543                         }
1544                 }
1545                 for(efa= em->faces.first; efa; efa= efa->next) {
1546                         if(efa->h==0) {
1547                                 if(efa->e1->f1==3 || efa->e2->f1==3 || efa->e3->f1==3 || (efa->e4 && efa->e4->f1==3)) { 
1548                                         EM_select_face(efa, 0);
1549                                 }
1550                         }
1551                 }
1552                 EM_selectmode_flush();
1553                 
1554         }
1555         
1556         countall();
1557         allqueue(REDRAWVIEW3D, 0);
1558 }
1559
1560
1561 void selectrandom_mesh(void) /* randomly selects a user-set % of vertices/edges/faces */
1562 {
1563         EditMesh *em = G.editMesh;
1564         EditVert *eve;
1565         EditEdge *eed;
1566         EditFace *efa;
1567         short randfac = 50;
1568
1569         if(G.obedit==NULL || (G.obedit->lay & G.vd->lay)==0) return;
1570
1571         /* Get the percentage of vertices to randomly select as 'randfac' */
1572         if(button(&randfac,0, 100,"Percentage:")==0) return;
1573
1574         BLI_srand( BLI_rand() ); /* random seed */
1575         
1576         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1577                 for(eve= em->verts.first; eve; eve= eve->next) {
1578                         if(eve->h==0) {
1579                                 if ( (BLI_frand() * 100) < randfac) 
1580                                         eve->f |= SELECT;
1581                         }
1582                 }
1583         }
1584         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1585                 for(eed= em->edges.first; eed; eed= eed->next) {
1586                         if(eed->h==0) {
1587                                 if ( (BLI_frand() * 100) < randfac) 
1588                                         EM_select_edge(eed, 1);
1589                         }
1590                 }
1591         }
1592         else {
1593                 for(efa= em->faces.first; efa; efa= efa->next) {
1594                         if(efa->h==0) {
1595                                 if ( (BLI_frand() * 100) < randfac) 
1596                                         EM_select_face(efa, 1);
1597                         }
1598                 }
1599         }
1600         
1601         EM_selectmode_flush();
1602
1603         countall();
1604         allqueue(REDRAWVIEW3D, 0);
1605 }
1606
1607 void editmesh_select_by_material(int index) 
1608 {
1609         EditMesh *em = G.editMesh;
1610         EditFace *efa;
1611         
1612         for (efa=em->faces.first; efa; efa= efa->next) {
1613                 if (efa->mat_nr==index) {
1614                         EM_select_face(efa, 1);
1615                 }
1616         }
1617
1618         EM_selectmode_flush();
1619 }
1620
1621 void editmesh_deselect_by_material(int index) 
1622 {
1623         EditMesh *em = G.editMesh;
1624         EditFace *efa;
1625         
1626         for (efa=em->faces.first; efa; efa= efa->next) {
1627                 if (efa->mat_nr==index) {
1628                         EM_select_face(efa, 0);
1629                 }
1630         }
1631
1632         EM_selectmode_flush();
1633 }
1634
1635 void EM_selectmode_menu(void)
1636 {
1637         int val;
1638         
1639         if(G.scene->selectmode & SCE_SELECT_VERTEX) pupmenu_set_active(1);
1640         else if(G.scene->selectmode & SCE_SELECT_EDGE) pupmenu_set_active(2);
1641         else pupmenu_set_active(3);
1642         
1643         val= pupmenu("Select Mode%t|Vertices|Edges|Faces");
1644         if(val>0) {
1645                 if(val==1) G.scene->selectmode= SCE_SELECT_VERTEX;
1646                 else if(val==2) G.scene->selectmode= SCE_SELECT_EDGE;
1647                 else G.scene->selectmode= SCE_SELECT_FACE;
1648         
1649                 EM_selectmode_set(); // when mode changes
1650                 allqueue(REDRAWVIEW3D, 1);
1651         }
1652 }
1653
1654 /* ************************* SEAMS AND EDGES **************** */
1655
1656 void editmesh_mark_seam(int clear)
1657 {
1658         EditMesh *em= G.editMesh;
1659         EditEdge *eed;
1660         Mesh *me= G.obedit->data;
1661
1662         /* auto-enable seams drawing */
1663         if(clear==0) {
1664                 if(!(G.f & G_DRAWSEAMS)) {
1665                         G.f |= G_DRAWSEAMS;
1666                         allqueue(REDRAWBUTSEDIT, 0);
1667                 }
1668                 if(!me->medge)
1669                         me->medge= MEM_callocN(sizeof(MEdge), "fake mesh edge");
1670         }
1671
1672         if(clear) {
1673                 eed= em->edges.first;
1674                 while(eed) {
1675                         if((eed->h==0) && (eed->f & SELECT)) {
1676                                 eed->seam = 0;
1677                         }
1678                         eed= eed->next;
1679                 }
1680                 BIF_undo_push("Mark Seam");
1681         }
1682         else {
1683                 eed= em->edges.first;
1684                 while(eed) {
1685                         if((eed->h==0) && (eed->f & SELECT)) {
1686                                 eed->seam = 1;
1687                         }
1688                         eed= eed->next;
1689                 }
1690                 BIF_undo_push("Clear Seam");
1691         }
1692
1693         allqueue(REDRAWVIEW3D, 0);
1694 }
1695
1696 void Edge_Menu() {
1697         short ret;
1698
1699         ret= pupmenu("Edge Specials%t|Mark Seam %x1|Clear Seam %x2|Rotate Edge CW%x3|Rotate Edge CCW%x4");
1700
1701         switch(ret)
1702         {
1703         case 1:
1704                 editmesh_mark_seam(0);
1705                 break;
1706         case 2:
1707                 editmesh_mark_seam(1);
1708                 break;
1709         case 3:
1710                 edge_rotate_selected(2);
1711                 break;
1712         case 4:
1713                 edge_rotate_selected(1);
1714                 break;
1715         }
1716 }
1717
1718
1719 /* **************** NORMALS ************** */
1720
1721 void righthandfaces(int select) /* makes faces righthand turning */
1722 {
1723         EditMesh *em = G.editMesh;
1724         EditEdge *eed, *ed1, *ed2, *ed3, *ed4;
1725         EditFace *efa, *startvl;
1726         float maxx, nor[3], cent[3];
1727         int totsel, found, foundone, direct, turn, tria_nr;
1728
1729    /* based at a select-connected to witness loose objects */
1730
1731         /* count per edge the amount of faces */
1732
1733         /* find the ultimate left, front, upper face (not manhattan dist!!) */
1734         /* also evaluate both triangle cases in quad, since these can be non-flat */
1735
1736         /* put normal to the outside, and set the first direction flags in edges */
1737
1738         /* then check the object, and set directions / direction-flags: but only for edges with 1 or 2 faces */
1739         /* this is in fact the 'select connected' */
1740         
1741         /* in case (selected) faces were not done: start over with 'find the ultimate ...' */
1742
1743         waitcursor(1);
1744         
1745         eed= em->edges.first;
1746         while(eed) {
1747                 eed->f2= 0;             // edge direction
1748                 eed->f1= 0;             // counter
1749                 eed= eed->next;
1750         }
1751
1752         /* count faces and edges */
1753         totsel= 0;
1754         efa= em->faces.first;
1755         while(efa) {
1756                 if(select==0 || (efa->f & SELECT) ) {
1757                         efa->f1= 1;
1758                         totsel++;
1759                         efa->e1->f1++;
1760                         efa->e2->f1++;
1761                         efa->e3->f1++;
1762                         if(efa->v4) efa->e4->f1++;
1763                 }
1764                 else efa->f1= 0;
1765
1766                 efa= efa->next;
1767         }
1768
1769         while(totsel>0) {
1770                 /* from the outside to the inside */
1771
1772                 efa= em->faces.first;
1773                 startvl= NULL;
1774                 maxx= -1.0e10;
1775                 tria_nr= 0;
1776
1777                 while(efa) {
1778                         if(efa->f1) {
1779                                 CalcCent3f(cent, efa->v1->co, efa->v2->co, efa->v3->co);
1780                                 cent[0]= cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
1781                                 
1782                                 if(cent[0]>maxx) {
1783                                         maxx= cent[0];
1784                                         startvl= efa;
1785                                         tria_nr= 0;
1786                                 }
1787                                 if(efa->v4) {
1788                                         CalcCent3f(cent, efa->v1->co, efa->v3->co, efa->v4->co);
1789                                         cent[0]= cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
1790                                         
1791                                         if(cent[0]>maxx) {
1792                                                 maxx= cent[0];
1793                                                 startvl= efa;
1794                                                 tria_nr= 1;
1795                                         }
1796                                 }
1797                         }
1798                         efa= efa->next;
1799                 }
1800                 
1801                 /* set first face correct: calc normal */
1802                 
1803                 if(tria_nr==1) {
1804                         CalcNormFloat(startvl->v1->co, startvl->v3->co, startvl->v4->co, nor);
1805                         CalcCent3f(cent, startvl->v1->co, startvl->v3->co, startvl->v4->co);
1806                 } else {
1807                         CalcNormFloat(startvl->v1->co, startvl->v2->co, startvl->v3->co, nor);
1808                         CalcCent3f(cent, startvl->v1->co, startvl->v2->co, startvl->v3->co);
1809                 }
1810                 /* first normal is oriented this way or the other */
1811                 if(select) {
1812                         if(select==2) {
1813                                 if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] > 0.0) flipface(startvl);
1814                         }
1815                         else {
1816                                 if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] < 0.0) flipface(startvl);
1817                         }
1818                 }
1819                 else if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] < 0.0) flipface(startvl);
1820
1821
1822                 eed= startvl->e1;
1823                 if(eed->v1==startvl->v1) eed->f2= 1; 
1824                 else eed->f2= 2;
1825                 
1826                 eed= startvl->e2;
1827                 if(eed->v1==startvl->v2) eed->f2= 1; 
1828                 else eed->f2= 2;
1829                 
1830                 eed= startvl->e3;
1831                 if(eed->v1==startvl->v3) eed->f2= 1; 
1832                 else eed->f2= 2;
1833                 
1834                 eed= startvl->e4;
1835                 if(eed) {
1836                         if(eed->v1==startvl->v4) eed->f2= 1; 
1837                         else eed->f2= 2;
1838                 }
1839                 
1840                 startvl->f1= 0;
1841                 totsel--;
1842
1843                 /* test normals */
1844                 found= 1;
1845                 direct= 1;
1846                 while(found) {
1847                         found= 0;
1848                         if(direct) efa= em->faces.first;
1849                         else efa= em->faces.last;
1850                         while(efa) {
1851                                 if(efa->f1) {
1852                                         turn= 0;
1853                                         foundone= 0;
1854
1855                                         ed1= efa->e1;
1856                                         ed2= efa->e2;
1857                                         ed3= efa->e3;
1858                                         ed4= efa->e4;
1859
1860                                         if(ed1->f2) {
1861                                                 if(ed1->v1==efa->v1 && ed1->f2==1) turn= 1;
1862                                                 if(ed1->v2==efa->v1 && ed1->f2==2) turn= 1;
1863                                                 foundone= 1;
1864                                         }
1865                                         else if(ed2->f2) {
1866                                                 if(ed2->v1==efa->v2 && ed2->f2==1) turn= 1;
1867                                                 if(ed2->v2==efa->v2 && ed2->f2==2) turn= 1;
1868                                                 foundone= 1;
1869                                         }
1870                                         else if(ed3->f2) {
1871                                                 if(ed3->v1==efa->v3 && ed3->f2==1) turn= 1;
1872                                                 if(ed3->v2==efa->v3 && ed3->f2==2) turn= 1;
1873                                                 foundone= 1;
1874                                         }
1875                                         else if(ed4 && ed4->f2) {
1876                                                 if(ed4->v1==efa->v4 && ed4->f2==1) turn= 1;
1877                                                 if(ed4->v2==efa->v4 && ed4->f2==2) turn= 1;
1878                                                 foundone= 1;
1879                                         }
1880
1881                                         if(foundone) {
1882                                                 found= 1;
1883                                                 totsel--;
1884                                                 efa->f1= 0;
1885
1886                                                 if(turn) {
1887                                                         if(ed1->v1==efa->v1) ed1->f2= 2; 
1888                                                         else ed1->f2= 1;
1889                                                         if(ed2->v1==efa->v2) ed2->f2= 2; 
1890                                                         else ed2->f2= 1;
1891                                                         if(ed3->v1==efa->v3) ed3->f2= 2; 
1892                                                         else ed3->f2= 1;
1893                                                         if(ed4) {
1894                                                                 if(ed4->v1==efa->v4) ed4->f2= 2; 
1895                                                                 else ed4->f2= 1;
1896                                                         }
1897
1898                                                         flipface(efa);
1899
1900                                                 }
1901                                                 else {
1902                                                         if(ed1->v1== efa->v1) ed1->f2= 1; 
1903                                                         else ed1->f2= 2;
1904                                                         if(ed2->v1==efa->v2) ed2->f2= 1; 
1905                                                         else ed2->f2= 2;
1906                                                         if(ed3->v1==efa->v3) ed3->f2= 1; 
1907                                                         else ed3->f2= 2;
1908                                                         if(ed4) {
1909                                                                 if(ed4->v1==efa->v4) ed4->f2= 1; 
1910                                                                 else ed4->f2= 2;
1911                                                         }
1912                                                 }
1913                                         }
1914                                 }
1915                                 if(direct) efa= efa->next;
1916                                 else efa= efa->prev;
1917                         }
1918                         direct= 1-direct;
1919                 }
1920         }
1921
1922         recalc_editnormals();
1923         
1924         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1925         
1926         waitcursor(0);
1927 }
1928
1929
1930 /* ********** ALIGN WITH VIEW **************** */
1931
1932
1933 static void editmesh_calc_selvert_center(float cent_r[3])
1934 {
1935         EditMesh *em = G.editMesh;
1936         EditVert *eve;
1937         int nsel= 0;
1938
1939         cent_r[0]= cent_r[1]= cent_r[0]= 0.0;
1940
1941         for (eve= em->verts.first; eve; eve= eve->next) {
1942                 if (eve->f & SELECT) {
1943                         cent_r[0]+= eve->co[0];
1944                         cent_r[1]+= eve->co[1];
1945                         cent_r[2]+= eve->co[2];
1946                         nsel++;
1947                 }
1948         }
1949
1950         if (nsel) {
1951                 cent_r[0]/= nsel;
1952                 cent_r[1]/= nsel;
1953                 cent_r[2]/= nsel;
1954         }
1955 }
1956
1957 static int tface_is_selected(TFace *tf)
1958 {
1959         return (!(tf->flag & TF_HIDE) && (tf->flag & TF_SELECT));
1960 }
1961
1962 static int faceselect_nfaces_selected(Mesh *me)
1963 {
1964         int i, count= 0;
1965
1966         for (i=0; i<me->totface; i++) {
1967                 MFace *mf= ((MFace*) me->mface) + i;
1968                 TFace *tf= ((TFace*) me->tface) + i;
1969
1970                 if (mf->v3 && tface_is_selected(tf))
1971                         count++;
1972         }
1973
1974         return count;
1975 }
1976
1977         /* XXX, code for both these functions should be abstract,
1978          * then unified, then written for other things (like objects,
1979          * which would use same as vertices method), then added
1980          * to interface! Hoera! - zr
1981          */
1982 void faceselect_align_view_to_selected(View3D *v3d, Mesh *me, int axis)
1983 {
1984         if (!faceselect_nfaces_selected(me)) {
1985                 error("No faces selected.");
1986         } else {
1987                 float norm[3];
1988                 int i;
1989
1990                 norm[0]= norm[1]= norm[2]= 0.0;
1991                 for (i=0; i<me->totface; i++) {
1992                         MFace *mf= ((MFace*) me->mface) + i;
1993                         TFace *tf= ((TFace*) me->tface) + i;
1994         
1995                         if (mf->v3 && tface_is_selected(tf)) {
1996                                 float *v1, *v2, *v3, fno[3];
1997
1998                                 v1= me->mvert[mf->v1].co;
1999                                 v2= me->mvert[mf->v2].co;
2000                                 v3= me->mvert[mf->v3].co;
2001                                 if (mf->v4) {
2002                                         float *v4= me->mvert[mf->v4].co;
2003                                         CalcNormFloat4(v1, v2, v3, v4, fno);
2004                                 } else {
2005                                         CalcNormFloat(v1, v2, v3, fno);
2006                                 }
2007
2008                                 norm[0]+= fno[0];
2009                                 norm[1]+= fno[1];
2010                                 norm[2]+= fno[2];
2011                         }
2012                 }
2013
2014                 view3d_align_axis_to_vector(v3d, axis, norm);
2015         }
2016 }
2017
2018 void editmesh_align_view_to_selected(View3D *v3d, int axis)
2019 {
2020         EditMesh *em = G.editMesh;
2021         int nselverts= EM_nvertices_selected();
2022
2023         if (nselverts<3) {
2024                 if (nselverts==0) {
2025                         error("No faces or vertices selected.");
2026                 } else {
2027                         error("At least one face or three vertices must be selected.");
2028                 }
2029         } else if (EM_nfaces_selected()) {
2030                 float norm[3];
2031                 EditFace *efa;
2032
2033                 norm[0]= norm[1]= norm[2]= 0.0;
2034                 for (efa= em->faces.first; efa; efa= efa->next) {
2035                         if (faceselectedAND(efa, SELECT)) {
2036                                 float fno[3];
2037                                 if (efa->v4) CalcNormFloat4(efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co, fno);
2038                                 else CalcNormFloat(efa->v1->co, efa->v2->co, efa->v3->co, fno);
2039                                                 /* XXX, fixme, should be flipped intp a 
2040                                                  * consistent direction. -zr
2041                                                  */
2042                                 norm[0]+= fno[0];
2043                                 norm[1]+= fno[1];
2044                                 norm[2]+= fno[2];
2045                         }
2046                 }
2047
2048                 Mat4Mul3Vecfl(G.obedit->obmat, norm);
2049                 view3d_align_axis_to_vector(v3d, axis, norm);
2050         } else {
2051                 float cent[3], norm[3];
2052                 EditVert *eve, *leve= NULL;
2053
2054                 norm[0]= norm[1]= norm[2]= 0.0;
2055                 editmesh_calc_selvert_center(cent);
2056                 for (eve= em->verts.first; eve; eve= eve->next) {
2057                         if (eve->f & SELECT) {
2058                                 if (leve) {
2059                                         float tno[3];
2060                                         CalcNormFloat(cent, leve->co, eve->co, tno);
2061                                         
2062                                                 /* XXX, fixme, should be flipped intp a 
2063                                                  * consistent direction. -zr
2064                                                  */
2065                                         norm[0]+= tno[0];
2066                                         norm[1]+= tno[1];
2067                                         norm[2]+= tno[2];
2068                                 }
2069                                 leve= eve;
2070                         }
2071                 }
2072
2073                 Mat4Mul3Vecfl(G.obedit->obmat, norm);
2074                 view3d_align_axis_to_vector(v3d, axis, norm);
2075         }
2076 }
2077
2078 /* **************** VERTEX DEFORMS *************** */
2079
2080 void vertexsmooth(void)
2081 {
2082         EditMesh *em = G.editMesh;
2083         EditVert *eve;
2084         EditEdge *eed;
2085         float *adror, *adr, fac;
2086         float fvec[3];
2087         int teller=0;
2088
2089         if(G.obedit==0) return;
2090
2091         /* count */
2092         eve= em->verts.first;
2093         while(eve) {
2094                 if(eve->f & SELECT) teller++;
2095                 eve= eve->next;
2096         }
2097         if(teller==0) return;
2098         
2099         adr=adror= (float *)MEM_callocN(3*sizeof(float *)*teller, "vertsmooth");
2100         eve= em->verts.first;
2101         while(eve) {
2102                 if(eve->f & SELECT) {
2103                         eve->vn= (EditVert *)adr;
2104                         eve->f1= 0;
2105                         adr+= 3;
2106                 }
2107                 eve= eve->next;
2108         }
2109         
2110         eed= em->edges.first;
2111         while(eed) {
2112                 if( (eed->v1->f & SELECT) || (eed->v2->f & SELECT) ) {
2113                         fvec[0]= (eed->v1->co[0]+eed->v2->co[0])/2.0;
2114                         fvec[1]= (eed->v1->co[1]+eed->v2->co[1])/2.0;
2115                         fvec[2]= (eed->v1->co[2]+eed->v2->co[2])/2.0;
2116                         
2117                         if((eed->v1->f & SELECT) && eed->v1->f1<255) {
2118                                 eed->v1->f1++;
2119                                 VecAddf((float *)eed->v1->vn, (float *)eed->v1->vn, fvec);
2120                         }
2121                         if((eed->v2->f & SELECT) && eed->v2->f1<255) {
2122                                 eed->v2->f1++;
2123                                 VecAddf((float *)eed->v2->vn, (float *)eed->v2->vn, fvec);
2124                         }
2125                 }
2126                 eed= eed->next;
2127         }
2128
2129         eve= em->verts.first;
2130         while(eve) {
2131                 if(eve->f & SELECT) {
2132                         if(eve->f1) {
2133                                 adr= (float *)eve->vn;
2134                                 fac= 0.5/(float)eve->f1;
2135                                 
2136                                 eve->co[0]= 0.5*eve->co[0]+fac*adr[0];
2137                                 eve->co[1]= 0.5*eve->co[1]+fac*adr[1];
2138                                 eve->co[2]= 0.5*eve->co[2]+fac*adr[2];
2139                         }
2140                         eve->vn= 0;
2141                 }
2142                 eve= eve->next;
2143         }
2144         MEM_freeN(adror);
2145
2146         recalc_editnormals();
2147
2148         allqueue(REDRAWVIEW3D, 0);
2149         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2150         BIF_undo_push("Vertex Smooth");
2151 }
2152
2153 void vertexnoise(void)
2154 {
2155         EditMesh *em = G.editMesh;
2156         Material *ma;
2157         Tex *tex;
2158         EditVert *eve;
2159         float b2, ofs, vec[3];
2160
2161         if(G.obedit==0) return;
2162         
2163         ma= give_current_material(G.obedit, G.obedit->actcol);
2164         if(ma==0 || ma->mtex[0]==0 || ma->mtex[0]->tex==0) {
2165                 return;
2166         }
2167         tex= ma->mtex[0]->tex;
2168         
2169         ofs= tex->turbul/200.0;
2170         
2171         eve= (struct EditVert *)em->verts.first;
2172         while(eve) {
2173                 if(eve->f & SELECT) {
2174                         
2175                         if(tex->type==TEX_STUCCI) {
2176                                 
2177                                 b2= BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1], eve->co[2]);
2178                                 if(tex->stype) ofs*=(b2*b2);
2179                                 vec[0]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0]+ofs, eve->co[1], eve->co[2]));
2180                                 vec[1]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1]+ofs, eve->co[2]));
2181                                 vec[2]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1], eve->co[2]+ofs));
2182                                 
2183                                 VecAddf(eve->co, eve->co, vec);
2184                         }
2185                         else {
2186                                 float tin, dum;
2187                                 externtex(ma->mtex[0], eve->co, &tin, &dum, &dum, &dum, &dum);
2188                                 eve->co[2]+= 0.05*tin;
2189                         }
2190                 }
2191                 eve= eve->next;
2192         }
2193
2194         recalc_editnormals();
2195         allqueue(REDRAWVIEW3D, 0);
2196         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2197         BIF_undo_push("Vertex Noise");
2198 }
2199
2200 void vertices_to_sphere(void)
2201 {
2202         EditMesh *em = G.editMesh;
2203         EditVert *eve;
2204         Object *ob= OBACT;
2205         float *curs, len, vec[3], cent[3], fac, facm, imat[3][3], bmat[3][3];
2206         int tot;
2207         short perc=100;
2208         
2209         if(ob==0) return;
2210         TEST_EDITMESH
2211         
2212         if(button(&perc, 1, 100, "Percentage:")==0) return;
2213         
2214         fac= perc/100.0;
2215         facm= 1.0-fac;
2216         
2217         Mat3CpyMat4(bmat, ob->obmat);
2218         Mat3Inv(imat, bmat);
2219
2220         /* centre */
2221         curs= give_cursor();
2222         cent[0]= curs[0]-ob->obmat[3][0];
2223         cent[1]= curs[1]-ob->obmat[3][1];
2224         cent[2]= curs[2]-ob->obmat[3][2];
2225         Mat3MulVecfl(imat, cent);
2226
2227         len= 0.0;
2228         tot= 0;
2229         eve= em->verts.first;
2230         while(eve) {
2231                 if(eve->f & SELECT) {
2232                         tot++;
2233                         len+= VecLenf(cent, eve->co);
2234                 }
2235                 eve= eve->next;
2236         }
2237         len/=tot;
2238         
2239         if(len==0.0) len= 10.0;
2240         
2241         eve= em->verts.first;
2242         while(eve) {
2243                 if(eve->f & SELECT) {
2244                         vec[0]= eve->co[0]-cent[0];
2245                         vec[1]= eve->co[1]-cent[1];
2246                         vec[2]= eve->co[2]-cent[2];
2247                         
2248                         Normalise(vec);
2249                         
2250                         eve->co[0]= fac*(cent[0]+vec[0]*len) + facm*eve->co[0];
2251                         eve->co[1]= fac*(cent[1]+vec[1]*len) + facm*eve->co[1];
2252                         eve->co[2]= fac*(cent[2]+vec[2]*len) + facm*eve->co[2];
2253                         
2254                 }
2255                 eve= eve->next;
2256         }
2257         
2258         recalc_editnormals();
2259         allqueue(REDRAWVIEW3D, 0);
2260         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2261         BIF_undo_push("To Sphere");
2262 }
2263