[Two Sections here; First is the log that *should* have been included
[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 /* ****************  LOOP SELECTS *************** */
748
749 /* selects quads in loop direction of indicated edge */
750 /* only flush over edges with valence <= 2 */
751 void faceloop_select(EditEdge *startedge, int select)
752 {
753         EditMesh *em = G.editMesh;
754         EditEdge *eed;
755         EditFace *efa;
756         int looking= 1;
757         
758         /* in eed->f1 we put the valence (amount of faces in edge) */
759         /* in eed->f2 we put tagged flag as correct loop */
760         /* in efa->f1 we put tagged flag as correct to select */
761
762         for(eed= em->edges.first; eed; eed= eed->next) {
763                 eed->f1= 0;
764                 eed->f2= 0;
765         }
766         for(efa= em->faces.first; efa; efa= efa->next) {
767                 efa->f1= 0;
768                 if(efa->h==0) {
769                         efa->e1->f1++;
770                         efa->e2->f1++;
771                         efa->e3->f1++;
772                         if(efa->e4) efa->e4->f1++;
773                 }
774         }
775         
776         // tag startedge OK
777         startedge->f2= 1;
778         
779         while(looking) {
780                 looking= 0;
781                 
782                 for(efa= em->faces.first; efa; efa= efa->next) {
783                         if(efa->e4 && efa->f1==0) {     // not done quad
784                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
785
786                                         // if edge tagged, select opposing edge and mark face ok
787                                         if(efa->e1->f2) {
788                                                 efa->e3->f2= 1;
789                                                 efa->f1= 1;
790                                                 looking= 1;
791                                         }
792                                         else if(efa->e2->f2) {
793                                                 efa->e4->f2= 1;
794                                                 efa->f1= 1;
795                                                 looking= 1;
796                                         }
797                                         if(efa->e3->f2) {
798                                                 efa->e1->f2= 1;
799                                                 efa->f1= 1;
800                                                 looking= 1;
801                                         }
802                                         if(efa->e4->f2) {
803                                                 efa->e2->f2= 1;
804                                                 efa->f1= 1;
805                                                 looking= 1;
806                                         }
807                                 }
808                         }
809                 }
810         }
811         
812         /* (de)select the faces */
813         if(select!=2) {
814                 for(efa= em->faces.first; efa; efa= efa->next) {
815                         if(efa->f1) EM_select_face(efa, select);
816                 }
817         }
818 }
819
820
821 /* helper for edgeloop_select, checks for eed->f2 tag in faces */
822 static int edge_not_in_tagged_face(EditEdge *eed)
823 {
824         EditMesh *em = G.editMesh;
825         EditFace *efa;
826         
827         for(efa= em->faces.first; efa; efa= efa->next) {
828                 if(efa->h==0) {
829                         if(efa->e1==eed || efa->e2==eed || efa->e3==eed || efa->e4==eed) {      // edge is in face
830                                 if(efa->e1->f2 || efa->e2->f2 || efa->e3->f2 || (efa->e4 && efa->e4->f2)) {     // face is tagged
831                                         return 0;
832                                 }
833                         }
834                 }
835         }
836         return 1;
837 }
838
839 /* selects or deselects edges that:
840 - if edges has 2 faces:
841         - has vertices with valence of 4
842         - not shares face with previous edge
843 - if edge has 1 face:
844         - has vertices with valence 4
845         - not shares face with previous edge
846         - but also only 1 face
847 - if edge no face:
848         - has vertices with valence 2
849 */
850 static void edgeloop_select(EditEdge *starteed, int select)
851 {
852         EditMesh *em = G.editMesh;
853         EditVert *eve;
854         EditEdge *eed;
855         EditFace *efa;
856         int looking= 1;
857         
858         /* in f1 we put the valence (amount of edges in a vertex, or faces in edge) */
859         /* in eed->f2 and efa->f1 we put tagged flag as correct loop */
860         for(eve= em->verts.first; eve; eve= eve->next) {
861                 eve->f1= 0;
862                 eve->f2= 0;
863         }
864         for(eed= em->edges.first; eed; eed= eed->next) {
865                 eed->f1= 0;
866                 eed->f2= 0;
867                 if((eed->h & 1)==0) {   // fgon edges add to valence too
868                         eed->v1->f1++; eed->v2->f1++;
869                 }
870         }
871         for(efa= em->faces.first; efa; efa= efa->next) {
872                 efa->f1= 0;
873                 if(efa->h==0) {
874                         efa->e1->f1++;
875                         efa->e2->f1++;
876                         efa->e3->f1++;
877                         if(efa->e4) efa->e4->f1++;
878                 }
879         }
880         
881         /* looped edges & vertices get tagged f2 */
882         starteed->f2= 1;
883         if(starteed->v1->f1<5) starteed->v1->f2= 1;
884         if(starteed->v2->f1<5) starteed->v2->f2= 1;
885         /* sorry, first edge isnt even ok */
886         if(starteed->v1->f2==0 && starteed->v2->f2==0) looking= 0;
887         
888         while(looking) {
889                 looking= 0;
890                 
891                 /* find correct valence edges which are not tagged yet, but connect to tagged one */
892                 for(eed= em->edges.first; eed; eed= eed->next) {
893                         if(eed->h==0 && eed->f2==0) { // edge not hidden, not tagged
894                                 if( (eed->v1->f1<5 && eed->v1->f2) || (eed->v2->f1<5 && eed->v2->f2)) { // valence of vertex OK, and is tagged
895                                         /* new edge is not allowed to be in face with tagged edge */
896                                         if(edge_not_in_tagged_face(eed)) {
897                                                 if(eed->f1==starteed->f1) {     // same amount of faces
898                                                         looking= 1;
899                                                         eed->f2= 1;
900                                                         if(eed->v2->f1<5) eed->v2->f2= 1;
901                                                         if(eed->v1->f1<5) eed->v1->f2= 1;
902                                                 }
903                                         }
904                                 }
905                         }
906                 }
907         }
908         /* and we do the select */
909         for(eed= em->edges.first; eed; eed= eed->next) {
910                 if(eed->f2) EM_select_edge(eed, select);
911         }
912 }
913
914 /* 
915    Almostly exactly the same code as faceloop select
916 */
917 static void edgering_select(EditEdge *startedge, int select){
918         EditMesh *em = G.editMesh;
919         EditEdge *eed;
920         EditFace *efa;
921         int looking= 1;
922         
923         /* in eed->f1 we put the valence (amount of faces in edge) */
924         /* in eed->f2 we put tagged flag as correct loop */
925         /* in efa->f1 we put tagged flag as correct to select */
926
927         for(eed= em->edges.first; eed; eed= eed->next) {
928                 eed->f1= 0;
929                 eed->f2= 0;
930         }
931         for(efa= em->faces.first; efa; efa= efa->next) {
932                 efa->f1= 0;
933                 if(efa->h==0) {
934                         efa->e1->f1++;
935                         efa->e2->f1++;
936                         efa->e3->f1++;
937                         if(efa->e4) efa->e4->f1++;
938                 }
939         }
940         
941         // tag startedge OK
942         startedge->f2= 1;
943         
944         while(looking) {
945                 looking= 0;
946                 
947                 for(efa= em->faces.first; efa; efa= efa->next) {
948                         if(efa->e4 && efa->f1==0) {     // not done quad
949                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
950
951                                         // if edge tagged, select opposing edge and mark face ok
952                                         if(efa->e1->f2) {
953                                                 efa->e3->f2= 1;
954                                                 efa->f1= 1;
955                                                 looking= 1;
956                                         }
957                                         else if(efa->e2->f2) {
958                                                 efa->e4->f2= 1;
959                                                 efa->f1= 1;
960                                                 looking= 1;
961                                         }
962                                         if(efa->e3->f2) {
963                                                 efa->e1->f2= 1;
964                                                 efa->f1= 1;
965                                                 looking= 1;
966                                         }
967                                         if(efa->e4->f2) {
968                                                 efa->e2->f2= 1;
969                                                 efa->f1= 1;
970                                                 looking= 1;
971                                         }
972                                 }
973                         }
974                 }
975         }
976         
977         /* (de)select the edges */
978         for(eed= em->edges.first; eed; eed= eed->next) {
979                 if(eed->f2) EM_select_edge(eed, select);
980         }
981 }
982 /* ***************** MAIN MOUSE SELECTION ************** */
983
984 // just to have the functions nice together
985 static void mouse_mesh_loop(void)
986 {
987         EditEdge *eed;
988         short dist= 50;
989         
990         eed= findnearestedge(&dist);
991         if(eed) {
992                 
993                 if((G.qual & LR_SHIFTKEY)==0) EM_clear_flag_all(SELECT);
994                 
995                 if((eed->f & SELECT)==0) EM_select_edge(eed, 1);
996                 else if(G.qual & LR_SHIFTKEY) EM_select_edge(eed, 0);
997
998                 if(G.scene->selectmode & SCE_SELECT_FACE) {
999                         faceloop_select(eed, eed->f & SELECT);
1000                 }
1001                 else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1002             if(G.qual == (LR_CTRLKEY | LR_ALTKEY) || G.qual == (LR_CTRLKEY | LR_ALTKEY |LR_SHIFTKEY))
1003                         edgering_select(eed, eed->f & SELECT);
1004             else if(G.qual & LR_ALTKEY)
1005                         edgeloop_select(eed, eed->f & SELECT);
1006                 }
1007         else if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1008             if(G.qual == (LR_CTRLKEY | LR_ALTKEY) || G.qual == (LR_CTRLKEY | LR_ALTKEY |LR_SHIFTKEY))
1009                         edgering_select(eed, eed->f & SELECT);
1010             else if(G.qual & LR_ALTKEY)
1011                         edgeloop_select(eed, eed->f & SELECT);
1012                 }
1013
1014                 /* frontbuffer draw of last selected only */
1015                 unified_select_draw(NULL, eed, NULL);
1016                 
1017                 EM_selectmode_flush();
1018                 countall();
1019                 
1020                 allqueue(REDRAWVIEW3D, 0);
1021         }
1022 }
1023
1024
1025 /* here actual select happens */
1026 void mouse_mesh(void)
1027 {
1028         EditVert *eve;
1029         EditEdge *eed;
1030         EditFace *efa;
1031         
1032         if(G.qual & LR_ALTKEY) mouse_mesh_loop();
1033         else if(unified_findnearest(&eve, &eed, &efa)) {
1034                 
1035                 if((G.qual & LR_SHIFTKEY)==0) EM_clear_flag_all(SELECT);
1036                 
1037                 if(efa) {
1038                         
1039                         if( (efa->f & SELECT)==0 ) {
1040                                 EM_select_face_fgon(efa, 1);
1041                         }
1042                         else if(G.qual & LR_SHIFTKEY) {
1043                                 EM_select_face_fgon(efa, 0);
1044                         }
1045                 }
1046                 else if(eed) {
1047                         if((eed->f & SELECT)==0) {
1048                                 EM_select_edge(eed, 1);
1049                         }
1050                         else if(G.qual & LR_SHIFTKEY) {
1051                                 EM_select_edge(eed, 0);
1052                         }
1053                 }
1054                 else if(eve) {
1055                         if((eve->f & SELECT)==0) {
1056                                 eve->f |= SELECT;
1057                                 if((G.qual & LR_SHIFTKEY)==0) G.editMesh->firstvert = eve;
1058                                 else G.editMesh->lastvert = eve;
1059                         }
1060                         else if(G.qual & LR_SHIFTKEY) eve->f &= ~SELECT;
1061                 }
1062                 
1063                 /* frontbuffer draw of last selected only */
1064                 unified_select_draw(eve, eed, efa);
1065         
1066                 EM_selectmode_flush();
1067                 countall();
1068
1069                 allqueue(REDRAWVIEW3D, 0);
1070         }
1071
1072         rightmouse_transform();
1073 }
1074
1075
1076 static void selectconnectedAll(void)
1077 {
1078         EditMesh *em = G.editMesh;
1079         EditVert *v1,*v2;
1080         EditEdge *eed;
1081         short done=1, toggle=0;
1082
1083         if(em->edges.first==0) return;
1084         
1085         while(done==1) {
1086                 done= 0;
1087                 
1088                 toggle++;
1089                 if(toggle & 1) eed= em->edges.first;
1090                 else eed= em->edges.last;
1091                 
1092                 while(eed) {
1093                         v1= eed->v1;
1094                         v2= eed->v2;
1095                         if(eed->h==0) {
1096                                 if(v1->f & SELECT) {
1097                                         if( (v2->f & SELECT)==0 ) {
1098                                                 v2->f |= SELECT;
1099                                                 done= 1;
1100                                         }
1101                                 }
1102                                 else if(v2->f & SELECT) {
1103                                         if( (v1->f & SELECT)==0 ) {
1104                                                 v1->f |= SELECT;
1105                                                 done= 1;
1106                                         }
1107                                 }
1108                         }
1109                         if(toggle & 1) eed= eed->next;
1110                         else eed= eed->prev;
1111                 }
1112         }
1113
1114         /* now use vertex select flag to select rest */
1115         EM_select_flush();
1116         
1117         countall();
1118
1119         allqueue(REDRAWVIEW3D, 0);
1120         BIF_undo_push("Select Connected (All)");
1121 }
1122
1123 void selectconnected_mesh(int qual)
1124 {
1125         EditMesh *em = G.editMesh;
1126         EditVert *eve, *v1, *v2;
1127         EditEdge *eed;
1128         EditFace *efa;
1129         short done=1, sel, toggle=0;
1130
1131         if(em->edges.first==0) return;
1132
1133         if(qual & LR_CTRLKEY) {
1134                 selectconnectedAll();
1135                 return;
1136         }
1137
1138         
1139         if( unified_findnearest(&eve, &eed, &efa)==0 ) {
1140                 error("Nothing indicated ");
1141                 return;
1142         }
1143         
1144         sel= 1;
1145         if(qual & LR_SHIFTKEY) sel=0;
1146
1147         /* clear test flags */
1148         for(v1= em->verts.first; v1; v1= v1->next) v1->f1= 0;
1149         
1150         /* start vertex/face/edge */
1151         if(eve) eve->f1= 1;
1152         else if(eed) eed->v1->f1= eed->v2->f1= 1;
1153         else efa->v1->f1= efa->v2->f1= efa->v3->f1= 1;
1154         
1155         /* set flag f1 if affected */
1156         while(done==1) {
1157                 done= 0;
1158                 toggle++;
1159                 
1160                 if(toggle & 1) eed= em->edges.first;
1161                 else eed= em->edges.last;
1162                 
1163                 while(eed) {
1164                         v1= eed->v1;
1165                         v2= eed->v2;
1166                         
1167                         if(eed->h==0) {
1168                                 if(v1->f1 && v2->f1==0) {
1169                                         v2->f1= 1;
1170                                         done= 1;
1171                                 }
1172                                 else if(v1->f1==0 && v2->f1) {
1173                                         v1->f1= 1;
1174                                         done= 1;
1175                                 }
1176                         }
1177                         
1178                         if(toggle & 1) eed= eed->next;
1179                         else eed= eed->prev;
1180                 }
1181         }
1182         
1183         /* now use vertex f1 flag to select/deselect */
1184         for(eed= em->edges.first; eed; eed= eed->next) {
1185                 if(eed->v1->f1 && eed->v2->f1) 
1186                         EM_select_edge(eed, sel);
1187         }
1188         for(efa= em->faces.first; efa; efa= efa->next) {
1189                 if(efa->v1->f1 && efa->v2->f1 && efa->v3->f1 && (efa->v4==NULL || efa->v4->f1)) 
1190                         EM_select_face(efa, sel);
1191         }
1192         /* no flush needed, connected geometry is done */
1193         
1194         countall();
1195         
1196         allqueue(REDRAWVIEW3D, 0);
1197         BIF_undo_push("Select Linked");
1198         
1199 }
1200
1201 /* swap is 0 or 1, if 1 it hides not selected */
1202 void hide_mesh(int swap)
1203 {
1204         EditMesh *em = G.editMesh;
1205         EditVert *eve;
1206         EditEdge *eed;
1207         EditFace *efa;
1208         int a;
1209         
1210         if(G.obedit==0) return;
1211
1212         /* hide happens on least dominant select mode, and flushes up, not down! (helps preventing errors in subsurf) */
1213         /*  - vertex hidden, always means edge is hidden too
1214                 - edge hidden, always means face is hidden too
1215                 - face hidden, only set face hide
1216                 - then only flush back down what's absolute hidden
1217         */
1218         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1219                 for(eve= em->verts.first; eve; eve= eve->next) {
1220                         if((eve->f & SELECT)!=swap) {
1221                                 eve->f &= ~SELECT;
1222                                 eve->h= 1;
1223                         }
1224                 }
1225         
1226                 for(eed= em->edges.first; eed; eed= eed->next) {
1227                         if(eed->v1->h || eed->v2->h) {
1228                                 eed->h |= 1;
1229                                 eed->f &= ~SELECT;
1230                         }
1231                 }
1232         
1233                 for(efa= em->faces.first; efa; efa= efa->next) {
1234                         if(efa->e1->h || efa->e2->h || efa->e3->h || (efa->e4 && efa->e4->h)) {
1235                                 efa->h= 1;
1236                                 efa->f &= ~SELECT;
1237                         }
1238                 }
1239         }
1240         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1241
1242                 for(eed= em->edges.first; eed; eed= eed->next) {
1243                         if((eed->f & SELECT)!=swap) {
1244                                 eed->h |= 1;
1245                                 EM_select_edge(eed, 0);
1246                         }
1247                 }
1248
1249                 for(efa= em->faces.first; efa; efa= efa->next) {
1250                         if(efa->e1->h || efa->e2->h || efa->e3->h || (efa->e4 && efa->e4->h)) {
1251                                 efa->h= 1;
1252                                 efa->f &= ~SELECT;
1253                         }
1254                 }
1255         }
1256         else {
1257
1258                 for(efa= em->faces.first; efa; efa= efa->next) {
1259                         if((efa->f & SELECT)!=swap) {
1260                                 efa->h= 1;
1261                                 EM_select_face(efa, 0);
1262                         }
1263                 }
1264         }
1265         
1266         /* flush down, only whats 100% hidden */
1267         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
1268         for(eed= em->edges.first; eed; eed= eed->next) eed->f1= 0;
1269         
1270         if(G.scene->selectmode & SCE_SELECT_FACE) {
1271                 for(efa= em->faces.first; efa; efa= efa->next) {
1272                         if(efa->h) a= 1; else a= 2;
1273                         efa->e1->f1 |= a;
1274                         efa->e2->f1 |= a;
1275                         efa->e3->f1 |= a;
1276                         if(efa->e4) efa->e4->f1 |= a;
1277                 }
1278         }
1279         
1280         if(G.scene->selectmode >= SCE_SELECT_EDGE) {
1281                 for(eed= em->edges.first; eed; eed= eed->next) {
1282                         if(eed->f1==1) eed->h |= 1;
1283                         if(eed->h & 1) a= 1; else a= 2;
1284                         eed->v1->f1 |= a;
1285                         eed->v2->f1 |= a;
1286                 }
1287         }
1288
1289         if(G.scene->selectmode >= SCE_SELECT_VERTEX) {
1290                 for(eve= em->verts.first; eve; eve= eve->next) {
1291                         if(eve->f1==1) eve->h= 1;
1292                 }
1293         }
1294                 
1295         allqueue(REDRAWVIEW3D, 0);
1296         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1297         BIF_undo_push("Hide");
1298 }
1299
1300
1301 void reveal_mesh(void)
1302 {
1303         EditMesh *em = G.editMesh;
1304         EditVert *eve;
1305         EditEdge *eed;
1306         EditFace *efa;
1307         
1308         if(G.obedit==0) return;
1309
1310         for(eve= em->verts.first; eve; eve= eve->next) {
1311                 if(eve->h) {
1312                         eve->h= 0;
1313                         eve->f |= SELECT;
1314                 }
1315         }
1316         for(eed= em->edges.first; eed; eed= eed->next) {
1317                 if(eed->h & 1) {
1318                         eed->h &= ~1;
1319                         if(G.scene->selectmode & SCE_SELECT_VERTEX); 
1320                         else EM_select_edge(eed, 1);
1321                 }
1322         }
1323         for(efa= em->faces.first; efa; efa= efa->next) {
1324                 if(efa->h) {
1325                         efa->h= 0;
1326                         if(G.scene->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_VERTEX)); 
1327                         else EM_select_face(efa, 1);
1328                 }
1329         }
1330
1331         EM_fgon_flags();        // redo flags and indices for fgons
1332         EM_selectmode_flush();
1333         countall();
1334
1335         allqueue(REDRAWVIEW3D, 0);
1336         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1337         BIF_undo_push("Reveal");
1338 }
1339
1340 void select_faces_by_numverts(int numverts)
1341 {
1342         EditMesh *em = G.editMesh;
1343         EditFace *efa;
1344
1345         /* Selects isolated verts, and edges that do not have 2 neighboring
1346          * faces
1347          */
1348         
1349         if(G.scene->selectmode!=SCE_SELECT_FACE) {
1350                 error("Only works in face selection mode");
1351                 return;
1352         }
1353
1354         efa= em->faces.first;
1355         while(efa) {
1356                 if (efa->e4) {
1357                         EM_select_face(efa, (numverts==4) );
1358                 }
1359                 else if (efa->e3) {
1360                         EM_select_face(efa, (numverts==3) );
1361                 }
1362                 else 
1363                         EM_select_face(efa, (numverts!=3) && (numverts!=4) );
1364                 efa= efa->next;
1365         }
1366
1367         countall();
1368         addqueue(curarea->win,  REDRAW, 0);
1369
1370         if (numverts==3)
1371                 BIF_undo_push("Select Triangles");
1372         else if (numverts==4)
1373                 BIF_undo_push("Select Quads");
1374         else
1375                 BIF_undo_push("Select non-Triangles/Quads");
1376 }
1377
1378 void select_sharp_edges(void)
1379 {
1380         /* Find edges that have exactly two neighboring faces,
1381          * check the angle between those faces, and if angle is
1382          * small enough, select the edge
1383          */
1384         EditMesh *em = G.editMesh;
1385         EditEdge *eed;
1386         EditFace *efa;
1387         EditFace **efa1;
1388         EditFace **efa2;
1389         long edgecount = 0, i;
1390         static short sharpness = 135;
1391         float fsharpness;
1392
1393         if(G.scene->selectmode==SCE_SELECT_FACE) {
1394                 error("Doesn't work in face selection mode");
1395                 return;
1396         }
1397
1398         if(button(&sharpness,0, 180,"Max Angle:")==0) return;
1399         /* if faces are at angle 'sharpness', then the face normals
1400          * are at angle 180.0 - 'sharpness' (convert to radians too)
1401          */
1402         fsharpness = ((180.0 - sharpness) * M_PI) / 180.0;
1403
1404         i=0;
1405         /* count edges, use tmp.l  */
1406         eed= em->edges.first;
1407         while(eed) {
1408                 edgecount++;
1409                 eed->tmp.l = i;
1410                 eed= eed->next;
1411                 ++i;
1412         }
1413
1414         /* for each edge, we want a pointer to two adjacent faces */
1415         efa1 = MEM_callocN(edgecount*sizeof(EditFace *), 
1416                                            "pairs of edit face pointers");
1417         efa2 = MEM_callocN(edgecount*sizeof(EditFace *), 
1418                                            "pairs of edit face pointers");
1419
1420 #define face_table_edge(eed) { \
1421                 i = eed->tmp.l; \
1422                 if (i != -1) { \
1423                         if (efa1[i]) { \
1424                                 if (efa2[i]) { \
1425                                         /* invalidate, edge has more than two neighbors */ \
1426                                         eed->tmp.l = -1; \
1427                                 } \
1428                                 else { \
1429                                         efa2[i] = efa; \
1430                                 } \
1431                         } \
1432                         else { \
1433                                 efa1[i] = efa; \
1434                         } \
1435                 } \
1436         }
1437
1438         /* find the adjacent faces of each edge, we want only two */
1439         efa= em->faces.first;
1440         while(efa) {
1441                 face_table_edge(efa->e1);
1442                 face_table_edge(efa->e2);
1443                 face_table_edge(efa->e3);
1444                 if (efa->e4) {
1445                         face_table_edge(efa->e4);
1446                 }
1447                 efa= efa->next;
1448         }
1449
1450 #undef face_table_edge
1451
1452         eed = em->edges.first;
1453         while(eed) {
1454                 i = eed->tmp.l;
1455                 if (i != -1) { 
1456                         /* edge has two or less neighboring faces */
1457                         if ( (efa1[i]) && (efa2[i]) ) { 
1458                                 /* edge has exactly two neighboring faces, check angle */
1459                                 float angle;
1460                                 angle = saacos(efa1[i]->n[0]*efa2[i]->n[0] +
1461                                                            efa1[i]->n[1]*efa2[i]->n[1] +
1462                                                            efa1[i]->n[2]*efa2[i]->n[2]);
1463                                 if (fabs(angle) >= fsharpness)
1464                                         EM_select_edge(eed, 1);
1465                         }
1466                 }
1467
1468                 eed= eed->next;
1469         }
1470
1471         MEM_freeN(efa1);
1472         MEM_freeN(efa2);
1473
1474         countall();
1475         addqueue(curarea->win,  REDRAW, 0);
1476         BIF_undo_push("Select Sharp Edges");
1477 }
1478
1479 void select_linked_flat_faces(void)
1480 {
1481         /* Find faces that are linked to selected faces that are 
1482          * relatively flat (angle between faces is higher than
1483          * specified angle)
1484          */
1485         EditMesh *em = G.editMesh;
1486         EditEdge *eed;
1487         EditFace *efa;
1488         EditFace **efa1;
1489         EditFace **efa2;
1490         long edgecount = 0, i, faceselcount=0, faceselcountold=0;
1491         static short sharpness = 135;
1492         float fsharpness;
1493
1494         if(G.scene->selectmode!=SCE_SELECT_FACE) {
1495                 error("Only works in face selection mode");
1496                 return;
1497         }
1498
1499         if(button(&sharpness,0, 180,"Min Angle:")==0) return;
1500         /* if faces are at angle 'sharpness', then the face normals
1501          * are at angle 180.0 - 'sharpness' (convert to radians too)
1502          */
1503         fsharpness = ((180.0 - sharpness) * M_PI) / 180.0;
1504
1505         i=0;
1506         /* count edges, use tmp.l */
1507         eed= em->edges.first;
1508         while(eed) {
1509                 edgecount++;
1510                 eed->tmp.l = i;
1511                 eed= eed->next;
1512                 ++i;
1513         }
1514
1515         /* for each edge, we want a pointer to two adjacent faces */
1516         efa1 = MEM_callocN(edgecount*sizeof(EditFace *), 
1517                                            "pairs of edit face pointers");
1518         efa2 = MEM_callocN(edgecount*sizeof(EditFace *), 
1519                                            "pairs of edit face pointers");
1520
1521 #define face_table_edge(eed) { \
1522                 i = eed->tmp.l; \
1523                 if (i != -1) { \
1524                         if (efa1[i]) { \
1525                                 if (efa2[i]) { \
1526                                         /* invalidate, edge has more than two neighbors */ \
1527                                         eed->tmp.l = -1; \
1528                                 } \
1529                                 else { \
1530                                         efa2[i] = efa; \
1531                                 } \
1532                         } \
1533                         else { \
1534                                 efa1[i] = efa; \
1535                         } \
1536                 } \
1537         }
1538
1539         /* find the adjacent faces of each edge, we want only two */
1540         efa= em->faces.first;
1541         while(efa) {
1542                 face_table_edge(efa->e1);
1543                 face_table_edge(efa->e2);
1544                 face_table_edge(efa->e3);
1545                 if (efa->e4) {
1546                         face_table_edge(efa->e4);
1547                 }
1548
1549                 /* while were at it, count the selected faces */
1550                 if (efa->f & SELECT) ++faceselcount;
1551
1552                 efa= efa->next;
1553         }
1554
1555 #undef face_table_edge
1556
1557         eed= em->edges.first;
1558         while(eed) {
1559                 i = eed->tmp.l;
1560                 if (i != -1) { 
1561                         /* edge has two or less neighboring faces */
1562                         if ( (efa1[i]) && (efa2[i]) ) { 
1563                                 /* edge has exactly two neighboring faces, check angle */
1564                                 float angle;
1565                                 angle = saacos(efa1[i]->n[0]*efa2[i]->n[0] +
1566                                                            efa1[i]->n[1]*efa2[i]->n[1] +
1567                                                            efa1[i]->n[2]*efa2[i]->n[2]);
1568                                 /* invalidate: edge too sharp */
1569                                 if (fabs(angle) >= fsharpness)
1570                                         eed->tmp.l = -1;
1571                         }
1572                         else {
1573                                 /* invalidate: less than two neighbors */
1574                                 eed->tmp.l = -1;
1575                         }
1576                 }
1577
1578                 eed= eed->next;
1579         }
1580
1581 #define select_flat_neighbor(eed) { \
1582                                 i = eed->tmp.l; \
1583                                 if (i!=-1) { \
1584                                         if (! (efa1[i]->f & SELECT) ) { \
1585                                                 EM_select_face(efa1[i], 1); \
1586                                                 ++faceselcount; \
1587                                         } \
1588                                         if (! (efa2[i]->f & SELECT) ) { \
1589                                                 EM_select_face(efa2[i], 1); \
1590                                                 ++faceselcount; \
1591                                         } \
1592                                 } \
1593         }
1594
1595         while (faceselcount != faceselcountold) {
1596                 faceselcountold = faceselcount;
1597
1598                 efa= em->faces.first;
1599                 while(efa) {
1600                         if (efa->f & SELECT) {
1601                                 select_flat_neighbor(efa->e1);
1602                                 select_flat_neighbor(efa->e2);
1603                                 select_flat_neighbor(efa->e3);
1604                                 if (efa->e4) {
1605                                         select_flat_neighbor(efa->e4);
1606                                 }
1607                         }
1608                         efa= efa->next;
1609                 }
1610         }
1611
1612 #undef select_flat_neighbor
1613
1614         MEM_freeN(efa1);
1615         MEM_freeN(efa2);
1616
1617         countall();
1618         addqueue(curarea->win,  REDRAW, 0);
1619         BIF_undo_push("Select Linked Flat Faces");
1620 }
1621
1622 void select_non_manifold(void)
1623 {
1624         EditMesh *em = G.editMesh;
1625         EditVert *eve;
1626         EditEdge *eed;
1627         EditFace *efa;
1628
1629         /* Selects isolated verts, and edges that do not have 2 neighboring
1630          * faces
1631          */
1632         
1633         if(G.scene->selectmode==SCE_SELECT_FACE) {
1634                 error("Doesn't work in face selection mode");
1635                 return;
1636         }
1637
1638         eve= em->verts.first;
1639         while(eve) {
1640                 /* this will count how many edges are connected
1641                  * to this vert */
1642                 eve->f1= 0;
1643                 eve= eve->next;
1644         }
1645
1646         eed= em->edges.first;
1647         while(eed) {
1648                 /* this will count how many faces are connected to
1649                  * this edge */
1650                 eed->f1= 0;
1651                 /* increase edge count for verts */
1652                 ++eed->v1->f1;
1653                 ++eed->v2->f1;
1654                 eed= eed->next;
1655         }
1656
1657         efa= em->faces.first;
1658         while(efa) {
1659                 /* increase face count for edges */
1660                 ++efa->e1->f1;
1661                 ++efa->e2->f1;
1662                 ++efa->e3->f1;
1663                 if (efa->e4)
1664                         ++efa->e4->f1;                  
1665                 efa= efa->next;
1666         }
1667
1668         /* select verts that are attached to an edge that does not
1669          * have 2 neighboring faces */
1670         eed= em->edges.first;
1671         while(eed) {
1672                 if (eed->h==0 && eed->f1 != 2) {
1673                         EM_select_edge(eed, 1);
1674                 }
1675                 eed= eed->next;
1676         }
1677
1678         /* select isolated verts */
1679         eve= em->verts.first;
1680         while(eve) {
1681                 if (eve->f1 == 0) {
1682                         if (!eve->h) eve->f |= SELECT;
1683                 }
1684                 eve= eve->next;
1685         }
1686
1687         countall();
1688         addqueue(curarea->win,  REDRAW, 0);
1689         BIF_undo_push("Select Non Manifold");
1690 }
1691
1692 void selectswap_mesh(void) /* UI level */
1693 {
1694         EditMesh *em = G.editMesh;
1695         EditVert *eve;
1696         EditEdge *eed;
1697         EditFace *efa;
1698         
1699         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1700
1701                 for(eve= em->verts.first; eve; eve= eve->next) {
1702                         if(eve->h==0) {
1703                                 if(eve->f & SELECT) eve->f &= ~SELECT;
1704                                 else eve->f|= SELECT;
1705                         }
1706                 }
1707         }
1708         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1709                 for(eed= em->edges.first; eed; eed= eed->next) {
1710                         if(eed->h==0) {
1711                                 EM_select_edge(eed, !(eed->f & SELECT));
1712                         }
1713                 }
1714         }
1715         else {
1716                 for(efa= em->faces.first; efa; efa= efa->next) {
1717                         if(efa->h==0) {
1718                                 EM_select_face(efa, !(efa->f & SELECT));
1719                         }
1720                 }
1721         }
1722
1723         EM_selectmode_flush();
1724         
1725         countall();
1726         allqueue(REDRAWVIEW3D, 0);
1727
1728         BIF_undo_push("Select Swap");
1729         
1730 }
1731
1732 void deselectall_mesh(void)      /* this toggles!!!, UI level */
1733 {
1734         
1735         if(G.obedit->lay & G.vd->lay) {
1736
1737                 if( EM_nvertices_selected() ) {
1738                         EM_clear_flag_all(SELECT);
1739                         BIF_undo_push("Deselect All");
1740                 }
1741                 else  {
1742                         EM_set_flag_all(SELECT);
1743                         BIF_undo_push("Select All");
1744                 }
1745                 
1746                 countall();
1747                 allqueue(REDRAWVIEW3D, 0);
1748         }
1749 }
1750
1751 void select_more(void)
1752 {
1753         EditMesh *em = G.editMesh;
1754         EditVert *eve;
1755         EditEdge *eed;
1756         EditFace *efa;
1757         
1758         for(eve= em->verts.first; eve; eve= eve->next) {
1759                 if(eve->f & SELECT) eve->f1= 1;
1760                 else eve->f1 = 0;
1761         }
1762         
1763         /* set f1 flags in vertices to select 'more' */
1764         for(eed= em->edges.first; eed; eed= eed->next) {
1765                 if(eed->h==0) {
1766                         if (eed->v1->f & SELECT)
1767                                 eed->v2->f1 = 1;
1768                         if (eed->v2->f & SELECT)
1769                                 eed->v1->f1 = 1;
1770                 }
1771         }
1772
1773         /* new selected edges, but not in facemode */
1774         if(G.scene->selectmode <= SCE_SELECT_EDGE) {
1775                 
1776                 for(eed= em->edges.first; eed; eed= eed->next) {
1777                         if(eed->h==0) {
1778                                 if(eed->v1->f1 && eed->v2->f1) EM_select_edge(eed, 1);
1779                         }
1780                 }
1781         }
1782         /* new selected faces */
1783         for(efa= em->faces.first; efa; efa= efa->next) {
1784                 if(efa->h==0) {
1785                         if(efa->v1->f1 && efa->v2->f1 && efa->v3->f1 && (efa->v4==NULL || efa->v4->f1)) 
1786                                 EM_select_face(efa, 1);
1787                 }
1788         }
1789
1790         countall();
1791         addqueue(curarea->win,  REDRAW, 0);
1792         BIF_undo_push("Select More");
1793 }
1794
1795 void select_less(void)
1796 {
1797         EditMesh *em = G.editMesh;
1798         EditEdge *eed;
1799         EditFace *efa;
1800
1801         if(G.scene->selectmode <= SCE_SELECT_EDGE) {
1802                 /* eed->f1 == 1:  edge with a selected and deselected vert */ 
1803
1804                 for(eed= em->edges.first; eed; eed= eed->next) {
1805                         eed->f1= 0;
1806                         if(eed->h==0) {
1807                                 
1808                                 if ( !(eed->v1->f & SELECT) && (eed->v2->f & SELECT) ) 
1809                                         eed->f1= 1;
1810                                 if ( (eed->v1->f & SELECT) && !(eed->v2->f & SELECT) ) 
1811                                         eed->f1= 1;
1812                         }
1813                 }
1814                 
1815                 /* deselect edges with flag set */
1816                 for(eed= em->edges.first; eed; eed= eed->next) {
1817                         if (eed->h==0 && eed->f1 == 1) {
1818                                 EM_select_edge(eed, 0);
1819                         }
1820                 }
1821                 EM_deselect_flush();
1822                 
1823         }
1824         else {
1825                 /* deselect faces with 1 or more deselect edges */
1826                 /* eed->f1 == mixed selection edge */
1827                 for(eed= em->edges.first; eed; eed= eed->next) eed->f1= 0;
1828
1829                 for(efa= em->faces.first; efa; efa= efa->next) {
1830                         if(efa->h==0) {
1831                                 if(efa->f & SELECT) {
1832                                         efa->e1->f1 |= 1;
1833                                         efa->e2->f1 |= 1;
1834                                         efa->e3->f1 |= 1;
1835                                         if(efa->e4) efa->e4->f1 |= 1;
1836                                 }
1837                                 else {
1838                                         efa->e1->f1 |= 2;
1839                                         efa->e2->f1 |= 2;
1840                                         efa->e3->f1 |= 2;
1841                                         if(efa->e4) efa->e4->f1 |= 2;
1842                                 }
1843                         }
1844                 }
1845                 for(efa= em->faces.first; efa; efa= efa->next) {
1846                         if(efa->h==0) {
1847                                 if(efa->e1->f1==3 || efa->e2->f1==3 || efa->e3->f1==3 || (efa->e4 && efa->e4->f1==3)) { 
1848                                         EM_select_face(efa, 0);
1849                                 }
1850                         }
1851                 }
1852                 EM_selectmode_flush();
1853                 
1854         }
1855         
1856         countall();
1857         allqueue(REDRAWVIEW3D, 0);
1858 }
1859
1860
1861 void selectrandom_mesh(void) /* randomly selects a user-set % of vertices/edges/faces */
1862 {
1863         EditMesh *em = G.editMesh;
1864         EditVert *eve;
1865         EditEdge *eed;
1866         EditFace *efa;
1867         short randfac = 50;
1868
1869         if(G.obedit==NULL || (G.obedit->lay & G.vd->lay)==0) return;
1870
1871         /* Get the percentage of vertices to randomly select as 'randfac' */
1872         if(button(&randfac,0, 100,"Percentage:")==0) return;
1873
1874         BLI_srand( BLI_rand() ); /* random seed */
1875         
1876         if(G.scene->selectmode & SCE_SELECT_VERTEX) {
1877                 for(eve= em->verts.first; eve; eve= eve->next) {
1878                         if(eve->h==0) {
1879                                 if ( (BLI_frand() * 100) < randfac) 
1880                                         eve->f |= SELECT;
1881                         }
1882                 }
1883         }
1884         else if(G.scene->selectmode & SCE_SELECT_EDGE) {
1885                 for(eed= em->edges.first; eed; eed= eed->next) {
1886                         if(eed->h==0) {
1887                                 if ( (BLI_frand() * 100) < randfac) 
1888                                         EM_select_edge(eed, 1);
1889                         }
1890                 }
1891         }
1892         else {
1893                 for(efa= em->faces.first; efa; efa= efa->next) {
1894                         if(efa->h==0) {
1895                                 if ( (BLI_frand() * 100) < randfac) 
1896                                         EM_select_face(efa, 1);
1897                         }
1898                 }
1899         }
1900         
1901         EM_selectmode_flush();
1902
1903         countall();
1904         allqueue(REDRAWVIEW3D, 0);
1905 }
1906
1907 void editmesh_select_by_material(int index) 
1908 {
1909         EditMesh *em = G.editMesh;
1910         EditFace *efa;
1911         
1912         for (efa=em->faces.first; efa; efa= efa->next) {
1913                 if (efa->mat_nr==index) {
1914                         EM_select_face(efa, 1);
1915                 }
1916         }
1917
1918         EM_selectmode_flush();
1919 }
1920
1921 void editmesh_deselect_by_material(int index) 
1922 {
1923         EditMesh *em = G.editMesh;
1924         EditFace *efa;
1925         
1926         for (efa=em->faces.first; efa; efa= efa->next) {
1927                 if (efa->mat_nr==index) {
1928                         EM_select_face(efa, 0);
1929                 }
1930         }
1931
1932         EM_selectmode_flush();
1933 }
1934
1935 void EM_selectmode_menu(void)
1936 {
1937         int val;
1938         
1939         if(G.scene->selectmode & SCE_SELECT_VERTEX) pupmenu_set_active(1);
1940         else if(G.scene->selectmode & SCE_SELECT_EDGE) pupmenu_set_active(2);
1941         else pupmenu_set_active(3);
1942         
1943         val= pupmenu("Select Mode%t|Vertices|Edges|Faces");
1944         
1945         
1946         if(val>0) {
1947                 if(val==1){ 
1948                         G.scene->selectmode= SCE_SELECT_VERTEX; 
1949                         BIF_undo_push("Selectmode Set: Vertex");
1950                         }
1951                 else if(val==2){
1952                         if((G.qual==LR_CTRLKEY)) EM_convertsel(G.scene->selectmode, SCE_SELECT_EDGE);
1953                         G.scene->selectmode= SCE_SELECT_EDGE;
1954                         BIF_undo_push("Selectmode Set: Edge");
1955                 }
1956                 
1957                 else{
1958                         if((G.qual==LR_CTRLKEY)) EM_convertsel(G.scene->selectmode, SCE_SELECT_FACE);
1959                         G.scene->selectmode= SCE_SELECT_FACE;
1960                         BIF_undo_push("Selectmode Set: Vertex");
1961                 }
1962         
1963                 EM_selectmode_set(); // when mode changes
1964                 allqueue(REDRAWVIEW3D, 1);
1965         }
1966 }
1967
1968 /* ************************* SEAMS AND EDGES **************** */
1969
1970 void editmesh_mark_seam(int clear)
1971 {
1972         EditMesh *em= G.editMesh;
1973         EditEdge *eed;
1974
1975         /* auto-enable seams drawing */
1976         if(clear==0) {
1977                 if(!(G.f & G_DRAWSEAMS)) {
1978                         G.f |= G_DRAWSEAMS;
1979                         allqueue(REDRAWBUTSEDIT, 0);
1980                 }
1981         }
1982
1983         if(clear) {
1984                 eed= em->edges.first;
1985                 while(eed) {
1986                         if((eed->h==0) && (eed->f & SELECT)) {
1987                                 eed->seam = 0;
1988                         }
1989                         eed= eed->next;
1990                 }
1991                 BIF_undo_push("Mark Seam");
1992         }
1993         else {
1994                 eed= em->edges.first;
1995                 while(eed) {
1996                         if((eed->h==0) && (eed->f & SELECT)) {
1997                                 eed->seam = 1;
1998                         }
1999                         eed= eed->next;
2000                 }
2001                 BIF_undo_push("Clear Seam");
2002         }
2003
2004         allqueue(REDRAWVIEW3D, 0);
2005 }
2006
2007 void Edge_Menu() {
2008         short ret;
2009
2010         ret= pupmenu("Edge Specials%t|Mark Seam %x1|Clear Seam %x2|Rotate Edge CW%x3|Rotate Edge CCW%x4|Loopcut%x6|Edge Slide%x5");
2011
2012         switch(ret)
2013         {
2014         case 1:
2015                 editmesh_mark_seam(0);
2016                 break;
2017         case 2:
2018                 editmesh_mark_seam(1);
2019                 break;
2020         case 3:
2021                 edge_rotate_selected(2);
2022                 break;
2023         case 4:
2024                 edge_rotate_selected(1);
2025                 break;
2026         case 5:
2027                 EdgeSlide(0,0.0);
2028                 BIF_undo_push("EdgeSlide");
2029                 break;
2030         case 6:
2031                 CutEdgeloop(1);
2032                 BIF_undo_push("Loopcut New");
2033                 break;
2034         }
2035 }
2036
2037
2038 /* **************** NORMALS ************** */
2039
2040 void righthandfaces(int select) /* makes faces righthand turning */
2041 {
2042         EditMesh *em = G.editMesh;
2043         EditEdge *eed, *ed1, *ed2, *ed3, *ed4;
2044         EditFace *efa, *startvl;
2045         float maxx, nor[3], cent[3];
2046         int totsel, found, foundone, direct, turn, tria_nr;
2047
2048    /* based at a select-connected to witness loose objects */
2049
2050         /* count per edge the amount of faces */
2051
2052         /* find the ultimate left, front, upper face (not manhattan dist!!) */
2053         /* also evaluate both triangle cases in quad, since these can be non-flat */
2054
2055         /* put normal to the outside, and set the first direction flags in edges */
2056
2057         /* then check the object, and set directions / direction-flags: but only for edges with 1 or 2 faces */
2058         /* this is in fact the 'select connected' */
2059         
2060         /* in case (selected) faces were not done: start over with 'find the ultimate ...' */
2061
2062         waitcursor(1);
2063         
2064         eed= em->edges.first;
2065         while(eed) {
2066                 eed->f2= 0;             // edge direction
2067                 eed->f1= 0;             // counter
2068                 eed= eed->next;
2069         }
2070
2071         /* count faces and edges */
2072         totsel= 0;
2073         efa= em->faces.first;
2074         while(efa) {
2075                 if(select==0 || (efa->f & SELECT) ) {
2076                         efa->f1= 1;
2077                         totsel++;
2078                         efa->e1->f1++;
2079                         efa->e2->f1++;
2080                         efa->e3->f1++;
2081                         if(efa->v4) efa->e4->f1++;
2082                 }
2083                 else efa->f1= 0;
2084
2085                 efa= efa->next;
2086         }
2087
2088         while(totsel>0) {
2089                 /* from the outside to the inside */
2090
2091                 efa= em->faces.first;
2092                 startvl= NULL;
2093                 maxx= -1.0e10;
2094                 tria_nr= 0;
2095
2096                 while(efa) {
2097                         if(efa->f1) {
2098                                 CalcCent3f(cent, efa->v1->co, efa->v2->co, efa->v3->co);
2099                                 cent[0]= cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
2100                                 
2101                                 if(cent[0]>maxx) {
2102                                         maxx= cent[0];
2103                                         startvl= efa;
2104                                         tria_nr= 0;
2105                                 }
2106                                 if(efa->v4) {
2107                                         CalcCent3f(cent, efa->v1->co, efa->v3->co, efa->v4->co);
2108                                         cent[0]= cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
2109                                         
2110                                         if(cent[0]>maxx) {
2111                                                 maxx= cent[0];
2112                                                 startvl= efa;
2113                                                 tria_nr= 1;
2114                                         }
2115                                 }
2116                         }
2117                         efa= efa->next;
2118                 }
2119                 
2120                 /* set first face correct: calc normal */
2121                 
2122                 if(tria_nr==1) {
2123                         CalcNormFloat(startvl->v1->co, startvl->v3->co, startvl->v4->co, nor);
2124                         CalcCent3f(cent, startvl->v1->co, startvl->v3->co, startvl->v4->co);
2125                 } else {
2126                         CalcNormFloat(startvl->v1->co, startvl->v2->co, startvl->v3->co, nor);
2127                         CalcCent3f(cent, startvl->v1->co, startvl->v2->co, startvl->v3->co);
2128                 }
2129                 /* first normal is oriented this way or the other */
2130                 if(select) {
2131                         if(select==2) {
2132                                 if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] > 0.0) flipface(startvl);
2133                         }
2134                         else {
2135                                 if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] < 0.0) flipface(startvl);
2136                         }
2137                 }
2138                 else if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] < 0.0) flipface(startvl);
2139
2140
2141                 eed= startvl->e1;
2142                 if(eed->v1==startvl->v1) eed->f2= 1; 
2143                 else eed->f2= 2;
2144                 
2145                 eed= startvl->e2;
2146                 if(eed->v1==startvl->v2) eed->f2= 1; 
2147                 else eed->f2= 2;
2148                 
2149                 eed= startvl->e3;
2150                 if(eed->v1==startvl->v3) eed->f2= 1; 
2151                 else eed->f2= 2;
2152                 
2153                 eed= startvl->e4;
2154                 if(eed) {
2155                         if(eed->v1==startvl->v4) eed->f2= 1; 
2156                         else eed->f2= 2;
2157                 }
2158                 
2159                 startvl->f1= 0;
2160                 totsel--;
2161
2162                 /* test normals */
2163                 found= 1;
2164                 direct= 1;
2165                 while(found) {
2166                         found= 0;
2167                         if(direct) efa= em->faces.first;
2168                         else efa= em->faces.last;
2169                         while(efa) {
2170                                 if(efa->f1) {
2171                                         turn= 0;
2172                                         foundone= 0;
2173
2174                                         ed1= efa->e1;
2175                                         ed2= efa->e2;
2176                                         ed3= efa->e3;
2177                                         ed4= efa->e4;
2178
2179                                         if(ed1->f2) {
2180                                                 if(ed1->v1==efa->v1 && ed1->f2==1) turn= 1;
2181                                                 if(ed1->v2==efa->v1 && ed1->f2==2) turn= 1;
2182                                                 foundone= 1;
2183                                         }
2184                                         else if(ed2->f2) {
2185                                                 if(ed2->v1==efa->v2 && ed2->f2==1) turn= 1;
2186                                                 if(ed2->v2==efa->v2 && ed2->f2==2) turn= 1;
2187                                                 foundone= 1;
2188                                         }
2189                                         else if(ed3->f2) {
2190                                                 if(ed3->v1==efa->v3 && ed3->f2==1) turn= 1;
2191                                                 if(ed3->v2==efa->v3 && ed3->f2==2) turn= 1;
2192                                                 foundone= 1;
2193                                         }
2194                                         else if(ed4 && ed4->f2) {
2195                                                 if(ed4->v1==efa->v4 && ed4->f2==1) turn= 1;
2196                                                 if(ed4->v2==efa->v4 && ed4->f2==2) turn= 1;
2197                                                 foundone= 1;
2198                                         }
2199
2200                                         if(foundone) {
2201                                                 found= 1;
2202                                                 totsel--;
2203                                                 efa->f1= 0;
2204
2205                                                 if(turn) {
2206                                                         if(ed1->v1==efa->v1) ed1->f2= 2; 
2207                                                         else ed1->f2= 1;
2208                                                         if(ed2->v1==efa->v2) ed2->f2= 2; 
2209                                                         else ed2->f2= 1;
2210                                                         if(ed3->v1==efa->v3) ed3->f2= 2; 
2211                                                         else ed3->f2= 1;
2212                                                         if(ed4) {
2213                                                                 if(ed4->v1==efa->v4) ed4->f2= 2; 
2214                                                                 else ed4->f2= 1;
2215                                                         }
2216
2217                                                         flipface(efa);
2218
2219                                                 }
2220                                                 else {
2221                                                         if(ed1->v1== efa->v1) ed1->f2= 1; 
2222                                                         else ed1->f2= 2;
2223                                                         if(ed2->v1==efa->v2) ed2->f2= 1; 
2224                                                         else ed2->f2= 2;
2225                                                         if(ed3->v1==efa->v3) ed3->f2= 1; 
2226                                                         else ed3->f2= 2;
2227                                                         if(ed4) {
2228                                                                 if(ed4->v1==efa->v4) ed4->f2= 1; 
2229                                                                 else ed4->f2= 2;
2230                                                         }
2231                                                 }
2232                                         }
2233                                 }
2234                                 if(direct) efa= efa->next;
2235                                 else efa= efa->prev;
2236                         }
2237                         direct= 1-direct;
2238                 }
2239         }
2240
2241         recalc_editnormals();
2242         
2243         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2244         
2245         waitcursor(0);
2246 }
2247
2248
2249 /* ********** ALIGN WITH VIEW **************** */
2250
2251
2252 static void editmesh_calc_selvert_center(float cent_r[3])
2253 {
2254         EditMesh *em = G.editMesh;
2255         EditVert *eve;
2256         int nsel= 0;
2257
2258         cent_r[0]= cent_r[1]= cent_r[0]= 0.0;
2259
2260         for (eve= em->verts.first; eve; eve= eve->next) {
2261                 if (eve->f & SELECT) {
2262                         cent_r[0]+= eve->co[0];
2263                         cent_r[1]+= eve->co[1];
2264                         cent_r[2]+= eve->co[2];
2265                         nsel++;
2266                 }
2267         }
2268
2269         if (nsel) {
2270                 cent_r[0]/= nsel;
2271                 cent_r[1]/= nsel;
2272                 cent_r[2]/= nsel;
2273         }
2274 }
2275
2276 static int tface_is_selected(TFace *tf)
2277 {
2278         return (!(tf->flag & TF_HIDE) && (tf->flag & TF_SELECT));
2279 }
2280
2281 static int faceselect_nfaces_selected(Mesh *me)
2282 {
2283         int i, count= 0;
2284
2285         for (i=0; i<me->totface; i++)
2286                 if (tface_is_selected(&me->tface[i]))
2287                         count++;
2288
2289         return count;
2290 }
2291
2292         /* XXX, code for both these functions should be abstract,
2293          * then unified, then written for other things (like objects,
2294          * which would use same as vertices method), then added
2295          * to interface! Hoera! - zr
2296          */
2297 void faceselect_align_view_to_selected(View3D *v3d, Mesh *me, int axis)
2298 {
2299         if (!faceselect_nfaces_selected(me)) {
2300                 error("No faces selected.");
2301         } else {
2302                 float norm[3];
2303                 int i;
2304
2305                 norm[0]= norm[1]= norm[2]= 0.0;
2306                 for (i=0; i<me->totface; i++) {
2307                         MFace *mf= ((MFace*) me->mface) + i;
2308                         TFace *tf= ((TFace*) me->tface) + i;
2309         
2310                         if (tface_is_selected(tf)) {
2311                                 float *v1, *v2, *v3, fno[3];
2312
2313                                 v1= me->mvert[mf->v1].co;
2314                                 v2= me->mvert[mf->v2].co;
2315                                 v3= me->mvert[mf->v3].co;
2316                                 if (mf->v4) {
2317                                         float *v4= me->mvert[mf->v4].co;
2318                                         CalcNormFloat4(v1, v2, v3, v4, fno);
2319                                 } else {
2320                                         CalcNormFloat(v1, v2, v3, fno);
2321                                 }
2322
2323                                 norm[0]+= fno[0];
2324                                 norm[1]+= fno[1];
2325                                 norm[2]+= fno[2];
2326                         }
2327                 }
2328
2329                 view3d_align_axis_to_vector(v3d, axis, norm);
2330         }
2331 }
2332
2333 void editmesh_align_view_to_selected(View3D *v3d, int axis)
2334 {
2335         EditMesh *em = G.editMesh;
2336         int nselverts= EM_nvertices_selected();
2337         float norm[3]={0.0, 0.0, 0.0}; /* used for storing the mesh normal */
2338         
2339         if (nselverts==0) {
2340                 error("No faces or vertices selected.");
2341         } else if (EM_nfaces_selected()) {
2342                 EditFace *efa;
2343                 for (efa= em->faces.first; efa; efa= efa->next) {
2344                         if (faceselectedAND(efa, SELECT)) {
2345                                 float fno[3];
2346                                 if (efa->v4) CalcNormFloat4(efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co, fno);
2347                                 else CalcNormFloat(efa->v1->co, efa->v2->co, efa->v3->co, fno);
2348                                                 /* XXX, fixme, should be flipped intp a 
2349                                                  * consistent direction. -zr
2350                                                  */
2351                                 norm[0]+= fno[0];
2352                                 norm[1]+= fno[1];
2353                                 norm[2]+= fno[2];
2354                         }
2355                 }
2356
2357                 Mat4Mul3Vecfl(G.obedit->obmat, norm);
2358                 view3d_align_axis_to_vector(v3d, axis, norm);
2359         } else if (nselverts>2) {
2360                 float cent[3];
2361                 EditVert *eve, *leve= NULL;
2362
2363                 editmesh_calc_selvert_center(cent);
2364                 for (eve= em->verts.first; eve; eve= eve->next) {
2365                         if (eve->f & SELECT) {
2366                                 if (leve) {
2367                                         float tno[3];
2368                                         CalcNormFloat(cent, leve->co, eve->co, tno);
2369                                         
2370                                                 /* XXX, fixme, should be flipped intp a 
2371                                                  * consistent direction. -zr
2372                                                  */
2373                                         norm[0]+= tno[0];
2374                                         norm[1]+= tno[1];
2375                                         norm[2]+= tno[2];
2376                                 }
2377                                 leve= eve;
2378                         }
2379                 }
2380
2381                 Mat4Mul3Vecfl(G.obedit->obmat, norm);
2382                 view3d_align_axis_to_vector(v3d, axis, norm);
2383         } else if (nselverts==2) { /* Align view to edge (or 2 verts) */ 
2384                 EditVert *eve, *leve= NULL;
2385
2386                 for (eve= em->verts.first; eve; eve= eve->next) {
2387                         if (eve->f & SELECT) {
2388                                 if (leve) {
2389                                         norm[0]= leve->co[0] - eve->co[0];
2390                                         norm[1]= leve->co[1] - eve->co[1];
2391                                         norm[2]= leve->co[2] - eve->co[2];
2392                                         break; /* we know there are only 2 verts so no need to keep looking */
2393                                 }
2394                                 leve= eve;
2395                         }
2396                 }
2397                 Mat4Mul3Vecfl(G.obedit->obmat, norm);
2398                 view3d_align_axis_to_vector(v3d, axis, norm);
2399         } else if (nselverts==1) { /* Align view to vert normal */ 
2400                 EditVert *eve;
2401
2402                 for (eve= em->verts.first; eve; eve= eve->next) {
2403                         if (eve->f & SELECT) {
2404                                 norm[0]= eve->no[0];
2405                                 norm[1]= eve->no[1];
2406                                 norm[2]= eve->no[2];
2407                                 break; /* we know this is the only selected vert, so no need to keep looking */
2408                         }
2409                 }
2410                 Mat4Mul3Vecfl(G.obedit->obmat, norm);
2411                 view3d_align_axis_to_vector(v3d, axis, norm);
2412         }
2413
2414
2415 /* **************** VERTEX DEFORMS *************** */
2416
2417 void vertexsmooth(void)
2418 {
2419         EditMesh *em = G.editMesh;
2420         EditVert *eve;
2421         EditEdge *eed;
2422         float *adror, *adr, fac;
2423         float fvec[3];
2424         int teller=0;
2425         ModifierData *md= G.obedit->modifiers.first;
2426
2427         if(G.obedit==0) return;
2428
2429         /* count */
2430         eve= em->verts.first;
2431         while(eve) {
2432                 if(eve->f & SELECT) teller++;
2433                 eve= eve->next;
2434         }
2435         if(teller==0) return;
2436         
2437         adr=adror= (float *)MEM_callocN(3*sizeof(float *)*teller, "vertsmooth");
2438         eve= em->verts.first;
2439         while(eve) {
2440                 if(eve->f & SELECT) {
2441                         eve->tmp.fp = adr;
2442                         eve->f1= 0;
2443                         eve->f2= 0;
2444                         adr+= 3;
2445                 }
2446                 eve= eve->next;
2447         }
2448
2449         /* if there is a mirror modifier with clipping, flag the verts that
2450          * are within tolerance of the plane(s) of reflection 
2451          */
2452         for (; md; md=md->next) {
2453                 if (md->type==eModifierType_Mirror) {
2454                         MirrorModifierData *mmd = (MirrorModifierData*) md;     
2455                 
2456                         if(mmd->flag & MOD_MIR_CLIPPING) {
2457                                 for (eve= em->verts.first; eve; eve= eve->next) {
2458                                         if(eve->f & SELECT) {
2459
2460                                                 switch(mmd->axis){
2461                                                         case 0:
2462                                                                 if (fabs(eve->co[0]) < mmd->tolerance)
2463                                                                         eve->f2 |= 1;
2464                                                                 break;
2465                                                         case 1:
2466                                                                 if (fabs(eve->co[1]) < mmd->tolerance)
2467                                                                         eve->f2 |= 2;
2468                                                                 break;
2469                                                         case 2:
2470                                                                 if (fabs(eve->co[2]) < mmd->tolerance)
2471                                                                         eve->f2 |= 4;
2472                                                                 break;
2473                                                 }
2474                                         }
2475                                 }
2476                         }
2477                 }
2478         }
2479         
2480         eed= em->edges.first;
2481         while(eed) {
2482                 if( (eed->v1->f & SELECT) || (eed->v2->f & SELECT) ) {
2483                         fvec[0]= (eed->v1->co[0]+eed->v2->co[0])/2.0;
2484                         fvec[1]= (eed->v1->co[1]+eed->v2->co[1])/2.0;
2485                         fvec[2]= (eed->v1->co[2]+eed->v2->co[2])/2.0;
2486                         
2487                         if((eed->v1->f & SELECT) && eed->v1->f1<255) {
2488                                 eed->v1->f1++;
2489                                 VecAddf(eed->v1->tmp.fp, eed->v1->tmp.fp, fvec);
2490                         }
2491                         if((eed->v2->f & SELECT) && eed->v2->f1<255) {
2492                                 eed->v2->f1++;
2493                                 VecAddf(eed->v2->tmp.fp, eed->v2->tmp.fp, fvec);
2494                         }
2495                 }
2496                 eed= eed->next;
2497         }
2498
2499         eve= em->verts.first;
2500         while(eve) {
2501                 if(eve->f & SELECT) {
2502                         if(eve->f1) {
2503                                 adr = eve->tmp.fp;
2504                                 fac= 0.5/(float)eve->f1;
2505                                 
2506                                 eve->co[0]= 0.5*eve->co[0]+fac*adr[0];
2507                                 eve->co[1]= 0.5*eve->co[1]+fac*adr[1];
2508                                 eve->co[2]= 0.5*eve->co[2]+fac*adr[2];
2509
2510                                 /* clip if needed by mirror modifier */
2511                                 if (eve->f2) {
2512                                         if (eve->f2 & 1) {
2513                                                 eve->co[0]= 0.0f;
2514                                         }
2515                                         if (eve->f2 & 2) {
2516                                                 eve->co[1]= 0.0f;
2517                                         }
2518                                         if (eve->f2 & 4) {
2519                                                 eve->co[2]= 0.0f;
2520                                         }
2521                                 }
2522                         }
2523                         eve->tmp.fp= 0;
2524                 }
2525                 eve= eve->next;
2526         }
2527         MEM_freeN(adror);
2528
2529         recalc_editnormals();
2530
2531         allqueue(REDRAWVIEW3D, 0);
2532         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2533         BIF_undo_push("Vertex Smooth");
2534 }
2535
2536 void vertexnoise(void)
2537 {
2538         EditMesh *em = G.editMesh;
2539         Material *ma;
2540         Tex *tex;
2541         EditVert *eve;
2542         float b2, ofs, vec[3];
2543
2544         if(G.obedit==0) return;
2545         
2546         ma= give_current_material(G.obedit, G.obedit->actcol);
2547         if(ma==0 || ma->mtex[0]==0 || ma->mtex[0]->tex==0) {
2548                 return;
2549         }
2550         tex= ma->mtex[0]->tex;
2551         
2552         ofs= tex->turbul/200.0;
2553         
2554         eve= (struct EditVert *)em->verts.first;
2555         while(eve) {
2556                 if(eve->f & SELECT) {
2557                         
2558                         if(tex->type==TEX_STUCCI) {
2559                                 
2560                                 b2= BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1], eve->co[2]);
2561                                 if(tex->stype) ofs*=(b2*b2);
2562                                 vec[0]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0]+ofs, eve->co[1], eve->co[2]));
2563                                 vec[1]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1]+ofs, eve->co[2]));
2564                                 vec[2]= 0.2*(b2-BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1], eve->co[2]+ofs));
2565                                 
2566                                 VecAddf(eve->co, eve->co, vec);
2567                         }
2568                         else {
2569                                 float tin, dum;
2570                                 externtex(ma->mtex[0], eve->co, &tin, &dum, &dum, &dum, &dum);
2571                                 eve->co[2]+= 0.05*tin;
2572                         }
2573                 }
2574                 eve= eve->next;
2575         }
2576
2577         recalc_editnormals();
2578         allqueue(REDRAWVIEW3D, 0);
2579         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2580         BIF_undo_push("Vertex Noise");
2581 }
2582
2583 void vertices_to_sphere(void)
2584 {
2585         EditMesh *em = G.editMesh;
2586         EditVert *eve;
2587         Object *ob= OBACT;
2588         float *curs, len, vec[3], cent[3], fac, facm, imat[3][3], bmat[3][3];
2589         int tot;
2590         short perc=100;
2591         
2592         if(ob==0) return;
2593         TEST_EDITMESH
2594         
2595         if(button(&perc, 1, 100, "Percentage:")==0) return;
2596         
2597         fac= perc/100.0;
2598         facm= 1.0-fac;
2599         
2600         Mat3CpyMat4(bmat, ob->obmat);
2601         Mat3Inv(imat, bmat);
2602
2603         /* centre */
2604         curs= give_cursor();
2605         cent[0]= curs[0]-ob->obmat[3][0];
2606         cent[1]= curs[1]-ob->obmat[3][1];
2607         cent[2]= curs[2]-ob->obmat[3][2];
2608         Mat3MulVecfl(imat, cent);
2609
2610         len= 0.0;
2611         tot= 0;
2612         eve= em->verts.first;
2613         while(eve) {
2614                 if(eve->f & SELECT) {
2615                         tot++;
2616                         len+= VecLenf(cent, eve->co);
2617                 }
2618                 eve= eve->next;
2619         }
2620         len/=tot;
2621         
2622         if(len==0.0) len= 10.0;
2623         
2624         eve= em->verts.first;
2625         while(eve) {
2626                 if(eve->f & SELECT) {
2627                         vec[0]= eve->co[0]-cent[0];
2628                         vec[1]= eve->co[1]-cent[1];
2629                         vec[2]= eve->co[2]-cent[2];
2630                         
2631                         Normalise(vec);
2632                         
2633                         eve->co[0]= fac*(cent[0]+vec[0]*len) + facm*eve->co[0];
2634                         eve->co[1]= fac*(cent[1]+vec[1]*len) + facm*eve->co[1];
2635                         eve->co[2]= fac*(cent[2]+vec[2]*len) + facm*eve->co[2];
2636                         
2637                 }
2638                 eve= eve->next;
2639         }
2640         
2641         recalc_editnormals();
2642         allqueue(REDRAWVIEW3D, 0);
2643         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2644         BIF_undo_push("To Sphere");
2645 }
2646