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