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