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