c9340bd56540b512e601dcb2d5758b1a488eeabd
[blender.git] / source / blender / src / editmesh.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) 2001-2002 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 /* main mesh editing routines. please note that 'vlak' is used here to denote a 'face'. */
34 /* at that time for me a face was something at the frontside of a human head! (ton) */
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <math.h>
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #ifdef WIN32
45 #include "BLI_winstuff.h"
46 #endif
47 #include "MEM_guardedalloc.h"
48
49 #include "PIL_time.h"
50
51 #include "MTC_matrixops.h"
52
53 #include "DNA_mesh_types.h"
54 #include "DNA_meshdata_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_screen_types.h"
57 #include "DNA_key_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_view3d_types.h"
60 #include "DNA_material_types.h"
61 #include "DNA_texture_types.h"
62 #include "DNA_userdef_types.h"
63
64 #include "BLI_blenlib.h"
65 #include "BLI_arithb.h"
66 #include "BLI_editVert.h"
67 #include "BLI_rand.h"
68
69
70 #include "BKE_utildefines.h"
71 #include "BKE_key.h"
72 #include "BKE_object.h"
73 #include "BKE_texture.h"
74 #include "BKE_displist.h"
75 #include "BKE_global.h"
76 #include "BKE_library.h"
77 #include "BKE_main.h"
78 #include "BKE_material.h"
79 #include "BKE_mesh.h"
80
81 #include "BIF_gl.h"
82 #include "BIF_graphics.h"
83 #include "BIF_editkey.h"
84 #include "BIF_space.h"
85 #include "BIF_toolbox.h"
86 #include "BIF_screen.h"
87 #include "BIF_interface.h"
88 #include "BIF_editmesh.h"
89 #include "BIF_mywindow.h"
90 #include "BIF_resources.h"
91 #include "BIF_glutil.h"
92 #include "BIF_cursors.h"
93
94 #include "BSE_view.h"
95 #include "BSE_edit.h"
96 #include "BSE_trans_types.h"
97
98 #include "BDR_drawobject.h"
99 #include "BDR_editobject.h"
100 #include "BDR_editface.h"
101 #include "BDR_vpaint.h"
102
103 #include "mydevice.h"
104 #include "blendef.h"
105 #include "nla.h"                /* For __NLA : Important - Do not remove! */
106 #include "render.h"
107
108 #include "GHOST_C-api.h"
109 #include "winlay.h"
110
111 #ifdef WIN32
112         #ifndef snprintf
113                 #define snprintf  _snprintf
114         #endif
115 #endif
116
117 /****/
118
119 static float convex(float *v1, float *v2, float *v3, float *v4);
120
121 /* EditMesh Undo */
122 void make_editMesh_real(Mesh *me);
123 void load_editMesh_real(Mesh *me, int);
124 /****/
125
126
127 /* extern ListBase fillvertbase, filledgebase; */ /* scanfill.c, in
128     the lib... already in BLI_blenlib.h */
129
130
131 extern short editbutflag;
132
133 static float icovert[12][3] = {
134         {0,0,-200}, 
135         {144.72, -105.144,-89.443},
136         {-55.277, -170.128,-89.443}, 
137         {-178.885,0,-89.443},
138         {-55.277,170.128,-89.443}, 
139         {144.72,105.144,-89.443},
140         {55.277,-170.128,89.443},
141         {-144.72,-105.144,89.443},
142         {-144.72,105.144,89.443},
143         {55.277,170.128,89.443},
144         {178.885,0,89.443},
145         {0,0,200}
146 };
147 static short icovlak[20][3] = {
148         {1,0,2},
149         {1,0,5},
150         {2,0,3},
151         {3,0,4},
152         {4,0,5},
153         {1,5,10},
154         {2,1,6},
155         {3,2,7},
156         {4,3,8},
157         {5,4,9},
158         {10,1,6},
159         {6,2,7},
160         {7,3,8},
161         {8,4,9},
162         {9,5,10},
163         {6,10,11},
164         {7,6,11},
165         {8,7,11},
166         {9,8,11},
167         {10,9,11}
168 };
169
170 /* DEFINES */
171 #define UVCOPY(t, s) memcpy(t, s, 2 * sizeof(float));
172
173 #define TEST_EDITMESH   if(G.obedit==0) return; \
174                                                 if( (G.vd->lay & G.obedit->lay)==0 ) return;
175
176 #define FACE_MARKCLEAR(f) (f->f1 = 1)
177
178 /* ***************** HASH ********************* */
179
180 /* HASH struct quickly finding of edges */
181 struct HashEdge {
182         struct EditEdge *eed;
183         struct HashEdge *next;
184 };
185
186 struct HashEdge *hashedgetab=NULL;
187
188 /********* qsort routines *********/
189
190
191 struct xvertsort {
192         float x;
193         EditVert *v1;
194 };
195
196 /* Functions */
197 static int vergxco(const void *v1, const void *v2)
198 {
199         const struct xvertsort *x1=v1, *x2=v2;
200
201         if( x1->x > x2->x ) return 1;
202         else if( x1->x < x2->x) return -1;
203         return 0;
204 }
205
206 struct vlaksort {
207         long x;
208         struct EditVlak *evl;
209 };
210
211
212 static int vergvlak(const void *v1, const void *v2)
213 {
214         const struct vlaksort *x1=v1, *x2=v2;
215         
216         if( x1->x > x2->x ) return 1;
217         else if( x1->x < x2->x) return -1;
218         return 0;
219 }
220
221
222 /* ************ ADD / REMOVE / FIND ****************** */
223
224 #define EDHASH(a, b)    ( (a)*256 + (b) )
225 #define EDHASHSIZE      65536
226
227 EditVert *addvertlist(float *vec)
228 {
229         EditMesh *em = G.editMesh;
230         EditVert *eve;
231         static unsigned char hashnr= 0;
232
233         eve= calloc(sizeof(EditVert), 1);
234         BLI_addtail(&em->verts, eve);
235         
236         if(vec) VECCOPY(eve->co, vec);
237
238         eve->hash= hashnr++;
239
240         /* new verts get keyindex of -1 since they did not
241          * have a pre-editmode vertex order
242          */
243         eve->keyindex = -1;
244         return eve;
245 }
246
247 EditEdge *findedgelist(EditVert *v1, EditVert *v2)
248 {
249         EditVert *v3;
250         struct HashEdge *he;
251
252         if(hashedgetab==0) {
253                 hashedgetab= MEM_callocN(EDHASHSIZE*sizeof(struct HashEdge), "hashedgetab");
254         }
255         
256         /* swap ? */
257         if( (long)v1 > (long)v2) {
258                 v3= v2; 
259                 v2= v1; 
260                 v1= v3;
261         }
262         
263         he= hashedgetab + EDHASH(v1->hash, v2->hash);
264         
265         while(he) {
266                 
267                 if(he->eed && he->eed->v1==v1 && he->eed->v2==v2) return he->eed;
268                 
269                 he= he->next;
270         }
271         return 0;
272 }
273
274 static void insert_hashedge(EditEdge *eed)
275 {
276         /* assuming that eed is not in the list yet, and that a find has been done before */
277         
278         struct HashEdge *first, *he;
279
280         first= hashedgetab + EDHASH(eed->v1->hash, eed->v2->hash);
281
282         if( first->eed==0 ) {
283                 first->eed= eed;
284         }
285         else {
286                 he= (struct HashEdge *)malloc(sizeof(struct HashEdge)); 
287                 he->eed= eed;
288                 he->next= first->next;
289                 first->next= he;
290         }
291 }
292
293 static void remove_hashedge(EditEdge *eed)
294 {
295         /* assuming eed is in the list */
296         
297         struct HashEdge *first, *he, *prev=NULL;
298
299
300         he=first= hashedgetab + EDHASH(eed->v1->hash, eed->v2->hash);
301
302         while(he) {
303                 if(he->eed == eed) {
304                         /* remove from list */
305                         if(he==first) {
306                                 if(first->next) {
307                                         he= first->next;
308                                         first->eed= he->eed;
309                                         first->next= he->next;
310                                         free(he);
311                                 }
312                                 else he->eed= 0;
313                         }
314                         else {
315                                 prev->next= he->next;
316                                 free(he);
317                         }
318                         return;
319                 }
320                 prev= he;
321                 he= he->next;
322         }
323 }
324
325 void free_hashedgetab(void)
326 {
327         struct HashEdge *he, *first, *hen;
328         int a;
329         
330         if(hashedgetab) {
331         
332                 first= hashedgetab;
333                 for(a=0; a<EDHASHSIZE; a++, first++) {
334                         he= first->next;
335                         while(he) {
336                                 hen= he->next;
337                                 free(he);
338                                 he= hen;
339                         }
340                 }
341                 MEM_freeN(hashedgetab);
342                 hashedgetab= 0;
343         }
344 }
345
346 EditEdge *addedgelist(EditVert *v1, EditVert *v2)
347 {
348         EditMesh *em = G.editMesh;
349         EditVert *v3;
350         EditEdge *eed;
351         int swap= 0;
352         
353         /* swap ? */
354         if(v1>v2) {
355                 v3= v2; 
356                 v2= v1; 
357                 v1= v3;
358                 swap= 1;
359         }
360
361         if(v1==v2) return 0;
362         if(v1==0 || v2==0) return 0;
363         
364         /* find in hashlist */
365         eed= findedgelist(v1, v2);
366         
367         if(eed==0) {
368
369                 eed= (EditEdge *)calloc(sizeof(EditEdge), 1);
370                 eed->v1= v1;
371                 eed->v2= v2;
372                 BLI_addtail(&em->edges, eed);
373                 eed->dir= swap;
374                 insert_hashedge(eed);
375         }
376         return eed;
377 }
378
379 static void free_editvert (EditVert *eve)
380 {
381         if (eve->dw) MEM_freeN (eve->dw);
382         free (eve);
383 }
384
385 void remedge(EditEdge *eed)
386 {
387         EditMesh *em = G.editMesh;
388
389         BLI_remlink(&em->edges, eed);
390         remove_hashedge(eed);
391 }
392
393 static void free_editedge(EditEdge *eed)
394 {
395         free(eed);
396 }
397
398 static void free_editvlak(EditVlak *evl)
399 {
400         free(evl);
401 }
402
403 static void free_vertlist(ListBase *edve) 
404 {
405         EditVert *eve, *next;
406
407         if (!edve) return;
408
409         eve= edve->first;
410         while(eve) {
411                 next= eve->next;
412                 free_editvert(eve);
413                 eve= next;
414         }
415         edve->first= edve->last= NULL;
416 }
417
418 static void free_edgelist(ListBase *lb)
419 {
420         EditEdge *eed, *next;
421         
422         eed= lb->first;
423         while(eed) {
424                 next= eed->next;
425                 free_editedge(eed);
426                 eed= next;
427         }
428         lb->first= lb->last= NULL;
429 }
430
431 static void free_vlaklist(ListBase *lb)
432 {
433         EditVlak *evl, *next;
434         
435         evl= lb->first;
436         while(evl) {
437                 next= evl->next;
438                 free_editvlak(evl);
439                 evl= next;
440         }
441         lb->first= lb->last= NULL;
442 }
443
444 EditVlak *addvlaklist(EditVert *v1, EditVert *v2, EditVert *v3, EditVert *v4, EditVlak *example)
445 {
446         EditMesh *em = G.editMesh;
447         EditVlak *evl;
448         EditEdge *e1, *e2=0, *e3=0, *e4=0;
449         
450
451         /* add face to list and do the edges */
452         e1= addedgelist(v1, v2);
453         if(v3) e2= addedgelist(v2, v3);
454         if(v4) e3= addedgelist(v3, v4); else e3= addedgelist(v3, v1);
455         if(v4) e4= addedgelist(v4, v1);
456         
457         if(v1==v2 || v2==v3 || v1==v3) return 0;
458         if(e2==0) return 0;
459
460         evl= (EditVlak *)calloc(sizeof(EditVlak), 1);
461         evl->v1= v1;
462         evl->v2= v2;
463         evl->v3= v3;
464         evl->v4= v4;
465
466         evl->e1= e1;
467         evl->e2= e2;
468         evl->e3= e3;
469         evl->e4= e4;
470
471         if(example) {
472                 evl->mat_nr= example->mat_nr;
473                 evl->tf= example->tf;
474                 evl->flag= example->flag;
475         }
476         else { 
477                 if (G.obedit && G.obedit->actcol)
478                         evl->mat_nr= G.obedit->actcol-1;
479                 default_uv(evl->tf.uv, 1.0);
480
481                 /* Initialize colors */
482                 evl->tf.col[0]= evl->tf.col[1]= evl->tf.col[2]= evl->tf.col[3]= vpaint_get_current_col();
483         }
484         
485         BLI_addtail(&em->faces, evl);
486
487         if(evl->v4) CalcNormFloat4(v1->co, v2->co, v3->co, v4->co, evl->n);
488         else CalcNormFloat(v1->co, v2->co, v3->co, evl->n);
489
490         return evl;
491 }
492
493 /* ********* end add / new / find */
494
495 static int comparevlak(EditVlak *vl1, EditVlak *vl2)
496 {
497         EditVert *v1, *v2, *v3, *v4;
498         
499         if(vl1->v4 && vl2->v4) {
500                 v1= vl2->v1;
501                 v2= vl2->v2;
502                 v3= vl2->v3;
503                 v4= vl2->v4;
504                 
505                 if(vl1->v1==v1 || vl1->v2==v1 || vl1->v3==v1 || vl1->v4==v1) {
506                         if(vl1->v1==v2 || vl1->v2==v2 || vl1->v3==v2 || vl1->v4==v2) {
507                                 if(vl1->v1==v3 || vl1->v2==v3 || vl1->v3==v3 || vl1->v4==v3) {
508                                         if(vl1->v1==v4 || vl1->v2==v4 || vl1->v3==v4 || vl1->v4==v4) {
509                                                 return 1;
510                                         }
511                                 }
512                         }
513                 }
514         }
515         else if(vl1->v4==0 && vl2->v4==0) {
516                 v1= vl2->v1;
517                 v2= vl2->v2;
518                 v3= vl2->v3;
519
520                 if(vl1->v1==v1 || vl1->v2==v1 || vl1->v3==v1) {
521                         if(vl1->v1==v2 || vl1->v2==v2 || vl1->v3==v2) {
522                                 if(vl1->v1==v3 || vl1->v2==v3 || vl1->v3==v3) {
523                                         return 1;
524                                 }
525                         }
526                 }
527         }
528
529         return 0;
530 }
531
532 static int exist_vlak(EditVert *v1, EditVert *v2, EditVert *v3, EditVert *v4)
533 {
534         EditMesh *em = G.editMesh;
535         EditVlak *evl, evltest;
536         
537         evltest.v1= v1;
538         evltest.v2= v2;
539         evltest.v3= v3;
540         evltest.v4= v4;
541         
542         evl= em->faces.first;
543         while(evl) {
544                 if(comparevlak(&evltest, evl)) return 1;
545                 evl= evl->next;
546         }
547         return 0;
548 }
549
550
551 static int vlakselectedOR(EditVlak *evl, int flag)
552 {
553         
554         if(evl->v1->f & flag) return 1;
555         if(evl->v2->f & flag) return 1;
556         if(evl->v3->f & flag) return 1;
557         if(evl->v4 && (evl->v4->f & 1)) return 1;
558         return 0;
559 }
560
561 int vlakselectedAND(EditVlak *evl, int flag)
562 {
563         if(evl->v1->f & flag) {
564                 if(evl->v2->f & flag) {
565                         if(evl->v3->f & flag) {
566                                 if(evl->v4) {
567                                         if(evl->v4->f & flag) return 1;
568                                 }
569                                 else return 1;
570                         }
571                 }
572         }
573         return 0;
574 }
575
576 void recalc_editnormals(void)
577 {
578         EditMesh *em = G.editMesh;
579         EditVlak *evl;
580
581         evl= em->faces.first;
582         while(evl) {
583                 if(evl->v4) CalcNormFloat4(evl->v1->co, evl->v2->co, evl->v3->co, evl->v4->co, evl->n);
584                 else CalcNormFloat(evl->v1->co, evl->v2->co, evl->v3->co, evl->n);
585                 evl= evl->next;
586         }
587 }
588
589 static void flipvlak(EditVlak *evl)
590 {
591         if(evl->v4) {
592                 SWAP(EditVert *, evl->v2, evl->v4);
593                 SWAP(EditEdge *, evl->e1, evl->e4);
594                 SWAP(EditEdge *, evl->e2, evl->e3);
595                 SWAP(unsigned int, evl->tf.col[1], evl->tf.col[3]);
596                 SWAP(float, evl->tf.uv[1][0], evl->tf.uv[3][0]);
597                 SWAP(float, evl->tf.uv[1][1], evl->tf.uv[3][1]);
598         }
599         else {
600                 SWAP(EditVert *, evl->v2, evl->v3);
601                 SWAP(EditEdge *, evl->e1, evl->e3);
602                 SWAP(unsigned int, evl->tf.col[1], evl->tf.col[2]);
603                 evl->e2->dir= 1-evl->e2->dir;
604                 SWAP(float, evl->tf.uv[1][0], evl->tf.uv[2][0]);
605                 SWAP(float, evl->tf.uv[1][1], evl->tf.uv[2][1]);
606         }
607         if(evl->v4) CalcNormFloat4(evl->v1->co, evl->v2->co, evl->v3->co, evl->v4->co, evl->n);
608         else CalcNormFloat(evl->v1->co, evl->v2->co, evl->v3->co, evl->n);
609 }
610
611
612 void flip_editnormals(void)
613 {
614         EditMesh *em = G.editMesh;
615         EditVlak *evl;
616         
617         evl= em->faces.first;
618         while(evl) {
619                 if( vlakselectedAND(evl, 1) ) {
620                         flipvlak(evl);
621                 }
622                 evl= evl->next;
623         }
624 }
625
626 /* ************************ IN & OUT ***************************** */
627
628 static void edge_normal_compare(EditEdge *eed, EditVlak *evl1)
629 {
630         EditVlak *evl2;
631         float cent1[3], cent2[3];
632         float inp;
633         
634         evl2= (EditVlak *)eed->vn;
635         if(evl1==evl2) return;
636         
637         inp= evl1->n[0]*evl2->n[0] + evl1->n[1]*evl2->n[1] + evl1->n[2]*evl2->n[2];
638         if(inp<0.999 && inp >-0.999) eed->f= 1;
639                 
640         if(evl1->v4) CalcCent4f(cent1, evl1->v1->co, evl1->v2->co, evl1->v3->co, evl1->v4->co);
641         else CalcCent3f(cent1, evl1->v1->co, evl1->v2->co, evl1->v3->co);
642         if(evl2->v4) CalcCent4f(cent2, evl2->v1->co, evl2->v2->co, evl2->v3->co, evl2->v4->co);
643         else CalcCent3f(cent2, evl2->v1->co, evl2->v2->co, evl2->v3->co);
644         
645         VecSubf(cent1, cent2, cent1);
646         Normalise(cent1);
647         inp= cent1[0]*evl1->n[0] + cent1[1]*evl1->n[1] + cent1[2]*evl1->n[2]; 
648
649         if(inp < -0.001 ) eed->f1= 1;
650 }
651
652 static void edge_drawflags(void)
653 {
654         EditMesh *em = G.editMesh;
655         EditVert *eve;
656         EditEdge *eed, *e1, *e2, *e3, *e4;
657         EditVlak *evl;
658         
659         /* - count number of times edges are used in faces: 0 en 1 time means draw edge
660          * - edges more than 1 time used: in *vn is pointer to first face
661          * - check all faces, when normal differs to much: draw (flag becomes 1)
662          */
663
664         /* later on: added flags for 'cylinder' and 'sphere' intersection tests in old
665            game engine (2.04)
666          */
667         
668         recalc_editnormals();
669         
670         /* init */
671         eve= em->verts.first;
672         while(eve) {
673                 eve->f1= 1;             /* during test it's set at zero */
674                 eve= eve->next;
675         }
676         eed= em->edges.first;
677         while(eed) {
678                 eed->f= eed->f1= 0;
679                 eed->vn= 0;
680                 eed= eed->next;
681         }
682
683         evl= em->faces.first;
684         while(evl) {
685                 e1= evl->e1;
686                 e2= evl->e2;
687                 e3= evl->e3;
688                 e4= evl->e4;
689                 if(e1->f<3) e1->f+= 1;
690                 if(e2->f<3) e2->f+= 1;
691                 if(e3->f<3) e3->f+= 1;
692                 if(e4 && e4->f<3) e4->f+= 1;
693                 
694                 if(e1->vn==0) e1->vn= (EditVert *)evl;
695                 if(e2->vn==0) e2->vn= (EditVert *)evl;
696                 if(e3->vn==0) e3->vn= (EditVert *)evl;
697                 if(e4 && e4->vn==0) e4->vn= (EditVert *)evl;
698                 
699                 evl= evl->next;
700         }
701
702         if(G.f & G_ALLEDGES) {
703                 evl= em->faces.first;
704                 while(evl) {
705                         if(evl->e1->f>=2) evl->e1->f= 1;
706                         if(evl->e2->f>=2) evl->e2->f= 1;
707                         if(evl->e3->f>=2) evl->e3->f= 1;
708                         if(evl->e4 && evl->e4->f>=2) evl->e4->f= 1;
709                         
710                         evl= evl->next;
711                 }               
712         }       
713         else {
714                 
715                 /* handle single-edges for 'test cylinder flag' (old engine) */
716                 
717                 eed= em->edges.first;
718                 while(eed) {
719                         if(eed->f==1) eed->f1= 1;
720                         eed= eed->next;
721                 }
722
723                 /* all faces, all edges with flag==2: compare normal */
724                 evl= em->faces.first;
725                 while(evl) {
726                         if(evl->e1->f==2) edge_normal_compare(evl->e1, evl);
727                         if(evl->e2->f==2) edge_normal_compare(evl->e2, evl);
728                         if(evl->e3->f==2) edge_normal_compare(evl->e3, evl);
729                         if(evl->e4 && evl->e4->f==2) edge_normal_compare(evl->e4, evl);
730                         
731                         evl= evl->next;
732                 }
733                 
734                 /* sphere collision flag */
735                 
736                 eed= em->edges.first;
737                 while(eed) {
738                         if(eed->f1!=1) {
739                                 eed->v1->f1= eed->v2->f1= 0;
740                         }
741                         eed= eed->next;
742                 }
743                 
744         }
745 }
746
747 static int contrpuntnorm(float *n, float *puno)  /* dutch: check vertex normal */
748 {
749         float inp;
750
751         inp= n[0]*puno[0]+n[1]*puno[1]+n[2]*puno[2];
752
753         /* angles 90 degrees: dont flip */
754         if(inp> -0.000001) return 0;
755
756         return 1;
757 }
758
759 void vertexnormals(int testflip)
760 {
761         EditMesh *em = G.editMesh;
762         Mesh *me;
763         EditVert *eve;
764         EditVlak *evl;  
765         float n1[3], n2[3], n3[3], n4[3], co[4], fac1, fac2, fac3, fac4, *temp;
766         float *f1, *f2, *f3, *f4, xn, yn, zn;
767         float len;
768         
769         if(G.obedit && G.obedit->type==OB_MESH) {
770                 me= G.obedit->data;
771                 if((me->flag & ME_TWOSIDED)==0) testflip= 0;
772         }
773
774         if(G.totvert==0) return;
775
776         if(G.totface==0) {
777                 /* fake vertex normals for 'halo puno'! */
778                 eve= em->verts.first;
779                 while(eve) {
780                         VECCOPY(eve->no, eve->co);
781                         Normalise( (float *)eve->no);
782                         eve= eve->next;
783                 }
784                 return;
785         }
786
787         /* clear normals */     
788         eve= em->verts.first;
789         while(eve) {
790                 eve->no[0]= eve->no[1]= eve->no[2]= 0.0;
791                 eve= eve->next;
792         }
793         
794         /* calculate cosine angles and add to vertex normal */
795         evl= em->faces.first;
796         while(evl) {
797                 VecSubf(n1, evl->v2->co, evl->v1->co);
798                 VecSubf(n2, evl->v3->co, evl->v2->co);
799                 Normalise(n1);
800                 Normalise(n2);
801
802                 if(evl->v4==0) {
803                         VecSubf(n3, evl->v1->co, evl->v3->co);
804                         Normalise(n3);
805                         
806                         co[0]= saacos(-n3[0]*n1[0]-n3[1]*n1[1]-n3[2]*n1[2]);
807                         co[1]= saacos(-n1[0]*n2[0]-n1[1]*n2[1]-n1[2]*n2[2]);
808                         co[2]= saacos(-n2[0]*n3[0]-n2[1]*n3[1]-n2[2]*n3[2]);
809                         
810                 }
811                 else {
812                         VecSubf(n3, evl->v4->co, evl->v3->co);
813                         VecSubf(n4, evl->v1->co, evl->v4->co);
814                         Normalise(n3);
815                         Normalise(n4);
816                         
817                         co[0]= saacos(-n4[0]*n1[0]-n4[1]*n1[1]-n4[2]*n1[2]);
818                         co[1]= saacos(-n1[0]*n2[0]-n1[1]*n2[1]-n1[2]*n2[2]);
819                         co[2]= saacos(-n2[0]*n3[0]-n2[1]*n3[1]-n2[2]*n3[2]);
820                         co[3]= saacos(-n3[0]*n4[0]-n3[1]*n4[1]-n3[2]*n4[2]);
821                 }
822                 
823                 temp= evl->v1->no;
824                 if(testflip && contrpuntnorm(evl->n, temp) ) co[0]= -co[0];
825                 temp[0]+= co[0]*evl->n[0];
826                 temp[1]+= co[0]*evl->n[1];
827                 temp[2]+= co[0]*evl->n[2];
828                 
829                 temp= evl->v2->no;
830                 if(testflip && contrpuntnorm(evl->n, temp) ) co[1]= -co[1];
831                 temp[0]+= co[1]*evl->n[0];
832                 temp[1]+= co[1]*evl->n[1];
833                 temp[2]+= co[1]*evl->n[2];
834                 
835                 temp= evl->v3->no;
836                 if(testflip && contrpuntnorm(evl->n, temp) ) co[2]= -co[2];
837                 temp[0]+= co[2]*evl->n[0];
838                 temp[1]+= co[2]*evl->n[1];
839                 temp[2]+= co[2]*evl->n[2];
840                 
841                 if(evl->v4) {
842                         temp= evl->v4->no;
843                         if(testflip && contrpuntnorm(evl->n, temp) ) co[3]= -co[3];
844                         temp[0]+= co[3]*evl->n[0];
845                         temp[1]+= co[3]*evl->n[1];
846                         temp[2]+= co[3]*evl->n[2];
847                 }
848                 
849                 evl= evl->next;
850         }
851
852         /* normalise vertex normals */
853         eve= em->verts.first;
854         while(eve) {
855                 len= Normalise(eve->no);
856                 if(len==0.0) {
857                         VECCOPY(eve->no, eve->co);
858                         Normalise( eve->no);
859                 }
860                 eve= eve->next;
861         }
862         
863         /* vertex normal flip-flags for shade (render) */
864         evl= em->faces.first;
865         while(evl) {
866                 evl->f=0;                       
867
868                 if(testflip) {
869                         f1= evl->v1->no;
870                         f2= evl->v2->no;
871                         f3= evl->v3->no;
872                         
873                         fac1= evl->n[0]*f1[0] + evl->n[1]*f1[1] + evl->n[2]*f1[2];
874                         if(fac1<0.0) {
875                                 evl->f = ME_FLIPV1;
876                         }
877                         fac2= evl->n[0]*f2[0] + evl->n[1]*f2[1] + evl->n[2]*f2[2];
878                         if(fac2<0.0) {
879                                 evl->f += ME_FLIPV2;
880                         }
881                         fac3= evl->n[0]*f3[0] + evl->n[1]*f3[1] + evl->n[2]*f3[2];
882                         if(fac3<0.0) {
883                                 evl->f += ME_FLIPV3;
884                         }
885                         if(evl->v4) {
886                                 f4= evl->v4->no;
887                                 fac4= evl->n[0]*f4[0] + evl->n[1]*f4[1] + evl->n[2]*f4[2];
888                                 if(fac4<0.0) {
889                                         evl->f += ME_FLIPV4;
890                                 }
891                         }
892                 }
893                 /* projection for cubemap! */
894                 xn= fabs(evl->n[0]);
895                 yn= fabs(evl->n[1]);
896                 zn= fabs(evl->n[2]);
897                 
898                 if(zn>xn && zn>yn) evl->f += ME_PROJXY;
899                 else if(yn>xn && yn>zn) evl->f += ME_PROJXZ;
900                 else evl->f += ME_PROJYZ;
901                 
902                 evl= evl->next;
903         }
904 }
905
906 void free_editMesh(void)
907 {
908         
909         EditMesh *em = G.editMesh;
910         if(em->verts.first) free_vertlist(&em->verts);
911         if(em->edges.first) free_edgelist(&em->edges);
912         if(em->faces.first) free_vlaklist(&em->faces);
913         free_hashedgetab();
914         G.totvert= G.totface= 0;
915 }
916
917 void make_editMesh(void)
918 {
919         Mesh *me;       
920
921         me= get_mesh(G.obedit);
922         if (me != G.undo_last_data) {
923                 G.undo_edit_level= -1;
924                 G.undo_edit_highest= -1;
925                 if (G.undo_clear) G.undo_clear();
926                 G.undo_last_data= me;
927                 G.undo_clear= undo_clear_mesh;
928         }
929         make_editMesh_real(me);
930 }
931
932 void make_editMesh_real(Mesh *me)
933 {
934         EditMesh *em = G.editMesh;
935         MFace *mface;
936         TFace *tface;
937         MVert *mvert;
938         KeyBlock *actkey=0;
939         EditVert *eve, **evlist, *eve1, *eve2, *eve3, *eve4;
940         EditVlak *evl;
941         EditEdge *eed;
942         int tot, a;
943
944         if(G.obedit==0) return;
945
946         /* because of reload */
947         free_editMesh();
948         
949         G.totvert= tot= me->totvert;
950
951         if(tot==0) {
952                 countall();
953                 return;
954         }
955         
956         waitcursor(1);
957
958         /* keys? */
959         if(me->key) {
960                 actkey= me->key->block.first;
961                 while(actkey) {
962                         if(actkey->flag & SELECT) break;
963                         actkey= actkey->next;
964                 }
965         }
966
967         if(actkey) {
968                 key_to_mesh(actkey, me);
969                 tot= actkey->totelem;
970         }
971
972         /* make editverts */
973         mvert= me->mvert;
974
975         evlist= (EditVert **)MEM_mallocN(tot*sizeof(void *),"evlist");
976         for(a=0; a<tot; a++, mvert++) {
977                 eve= addvertlist(mvert->co);
978                 evlist[a]= eve;
979                 
980                 // face select sets selection in next loop
981                 if( (G.f & G_FACESELECT)==0 )
982                         eve->f |= (mvert->flag & 1);
983                 
984                 if (mvert->flag & ME_HIDE) eve->h= 1;           
985                 eve->no[0]= mvert->no[0]/32767.0;
986                 eve->no[1]= mvert->no[1]/32767.0;
987                 eve->no[2]= mvert->no[2]/32767.0;
988
989                 /* lets overwrite the keyindex of the editvert
990                  * with the order it used to be in before
991                  * editmode
992                  */
993                 eve->keyindex = a;
994
995 #ifdef __NLA
996
997                 /* OLD VERSION */
998                 /*
999                 eve->totweight = mvert->totweight;
1000                 if (mvert->dw){
1001                         eve->dw = BLI_callocN (sizeof(MDeformWeight) * mvert->totweight, "deformWeight");
1002                         memcpy (eve->dw, mvert->dw, sizeof(MDeformWeight) * mvert->totweight);
1003                 }
1004                 */
1005
1006                 /* NEW VERSION */
1007                 if (me->dvert){
1008                         eve->totweight = me->dvert[a].totweight;
1009                         if (me->dvert[a].dw){
1010                                 eve->dw = MEM_callocN (sizeof(MDeformWeight) * me->dvert[a].totweight, "deformWeight");
1011                                 memcpy (eve->dw, me->dvert[a].dw, sizeof(MDeformWeight) * me->dvert[a].totweight);
1012                         }
1013                 }
1014
1015 #endif
1016         }
1017
1018         if(actkey && actkey->totelem!=me->totvert);
1019         else {
1020                 unsigned int *mcol;
1021                 
1022                 /* make edges and faces */
1023                 mface= me->mface;
1024                 tface= me->tface;
1025                 mcol= (unsigned int *)me->mcol;
1026                 
1027                 for(a=0; a<me->totface; a++, mface++) {
1028                         eve1= evlist[mface->v1];
1029                         eve2= evlist[mface->v2];
1030                         if(mface->v3) eve3= evlist[mface->v3]; else eve3= 0;
1031                         if(mface->v4) eve4= evlist[mface->v4]; else eve4= 0;
1032                         
1033                         evl= addvlaklist(eve1, eve2, eve3, eve4, NULL);
1034                         
1035                         if(evl) {
1036                                 if(mcol) memcpy(evl->tf.col, mcol, 4*sizeof(int));
1037
1038                                 if(me->tface) {
1039                                         evl->tf= *tface;
1040
1041                                         if( tface->flag & TF_SELECT) {
1042                                                 if(G.f & G_FACESELECT) {
1043                                                         eve1->f |= 1;
1044                                                         eve2->f |= 1;
1045                                                         if(eve3) eve3->f |= 1;
1046                                                         if(eve4) eve4->f |= 1;
1047                                                 }
1048                                         }
1049                                 }
1050                         
1051                                 evl->mat_nr= mface->mat_nr;
1052                                 evl->flag= mface->flag;
1053                         }
1054
1055                         if(me->tface) tface++;
1056                         if(mcol) mcol+=4;
1057                 }
1058         }
1059         
1060         /* intrr: needed because of hidden vertices imported from Mesh */
1061         
1062         eed= em->edges.first;
1063         while(eed) {
1064                 if(eed->v1->h || eed->v2->h) eed->h= 1;
1065                 else eed->h= 0;
1066                 eed= eed->next;
1067         }       
1068         
1069         MEM_freeN(evlist);
1070         
1071         countall();
1072         
1073         if (mesh_uses_displist(me))
1074                 makeDispList(G.obedit);
1075         
1076         waitcursor(0);
1077 }
1078
1079 /** Rotates MFace and UVFace vertices in case the last
1080   * vertex index is = 0. 
1081   * This function is a hack and may only be called in the
1082   * conversion from EditMesh to Mesh data.
1083   * This function is similar to test_index_mface in
1084   * blenkernel/intern/mesh.c. 
1085   * To not clutter the blenkernel code with more bad level
1086   * calls/structures, this function resides here.
1087   */
1088
1089
1090 static void fix_faceindices(MFace *mface, EditVlak *evl, int nr)
1091 {
1092         int a;
1093         float tmpuv[2];
1094         unsigned int tmpcol;
1095
1096         /* first test if the face is legal */
1097
1098         if(mface->v3 && mface->v3==mface->v4) {
1099                 mface->v4= 0;
1100                 nr--;
1101         }
1102         if(mface->v2 && mface->v2==mface->v3) {
1103                 mface->v3= mface->v4;
1104                 mface->v4= 0;
1105                 nr--;
1106         }
1107         if(mface->v1==mface->v2) {
1108                 mface->v2= mface->v3;
1109                 mface->v3= mface->v4;
1110                 mface->v4= 0;
1111                 nr--;
1112         }
1113
1114         /* prevent a zero index value at the wrong location */
1115         if(nr==2) {
1116                 if(mface->v2==0) SWAP(int, mface->v1, mface->v2);
1117         }
1118         else if(nr==3) {
1119                 if(mface->v3==0) {
1120                         SWAP(int, mface->v1, mface->v2);
1121                         SWAP(int, mface->v2, mface->v3);
1122                         /* rotate face UV coordinates, too */
1123                         UVCOPY(tmpuv, evl->tf.uv[0]);
1124                         UVCOPY(evl->tf.uv[0], evl->tf.uv[1]);
1125                         UVCOPY(evl->tf.uv[1], evl->tf.uv[2]);
1126                         UVCOPY(evl->tf.uv[2], tmpuv);
1127                         /* same with vertex colours */
1128                         tmpcol = evl->tf.col[0];
1129                         evl->tf.col[0] = evl->tf.col[1];
1130                         evl->tf.col[1] = evl->tf.col[2];
1131                         evl->tf.col[2] = tmpcol;
1132
1133                         
1134                         a= mface->edcode;
1135                         mface->edcode= 0;
1136                         if(a & ME_V1V2) mface->edcode |= ME_V3V1;
1137                         if(a & ME_V2V3) mface->edcode |= ME_V1V2;
1138                         if(a & ME_V3V1) mface->edcode |= ME_V2V3;
1139                         
1140                         a= mface->puno;
1141                         mface->puno &= ~15;
1142                         if(a & ME_FLIPV1) mface->puno |= ME_FLIPV2;
1143                         if(a & ME_FLIPV2) mface->puno |= ME_FLIPV3;
1144                         if(a & ME_FLIPV3) mface->puno |= ME_FLIPV1;
1145                 }
1146         }
1147         else if(nr==4) {
1148                 if(mface->v3==0 || mface->v4==0) {
1149                         SWAP(int, mface->v1, mface->v3);
1150                         SWAP(int, mface->v2, mface->v4);
1151                         /* swap UV coordinates */
1152                         UVCOPY(tmpuv, evl->tf.uv[0]);
1153                         UVCOPY(evl->tf.uv[0], evl->tf.uv[2]);
1154                         UVCOPY(evl->tf.uv[2], tmpuv);
1155                         UVCOPY(tmpuv, evl->tf.uv[1]);
1156                         UVCOPY(evl->tf.uv[1], evl->tf.uv[3]);
1157                         UVCOPY(evl->tf.uv[3], tmpuv);
1158                         /* swap vertex colours */
1159                         tmpcol = evl->tf.col[0];
1160                         evl->tf.col[0] = evl->tf.col[2];
1161                         evl->tf.col[2] = tmpcol;
1162                         tmpcol = evl->tf.col[1];
1163                         evl->tf.col[1] = evl->tf.col[3];
1164                         evl->tf.col[3] = tmpcol;
1165
1166                         a= mface->edcode;
1167                         mface->edcode= 0;
1168                         if(a & ME_V1V2) mface->edcode |= ME_V3V4;
1169                         if(a & ME_V2V3) mface->edcode |= ME_V2V3;
1170                         if(a & ME_V3V4) mface->edcode |= ME_V1V2;
1171                         if(a & ME_V4V1) mface->edcode |= ME_V4V1;
1172
1173                         a= mface->puno;
1174                         mface->puno &= ~15;
1175                         if(a & ME_FLIPV1) mface->puno |= ME_FLIPV3;
1176                         if(a & ME_FLIPV2) mface->puno |= ME_FLIPV4;
1177                         if(a & ME_FLIPV3) mface->puno |= ME_FLIPV1;
1178                         if(a & ME_FLIPV4) mface->puno |= ME_FLIPV2;
1179                 }
1180         }
1181
1182 }
1183
1184
1185
1186 /* load from EditMode to Mesh */
1187
1188 void load_editMesh()
1189 {
1190         Mesh *me;
1191
1192         waitcursor(1);
1193         countall();
1194         me= get_mesh(G.obedit);
1195        
1196         load_editMesh_real(me, 0);
1197 }
1198
1199
1200 void load_editMesh_real(Mesh *me, int undo)
1201 {
1202         EditMesh *em = G.editMesh;
1203         MFace *mface;
1204         MVert *mvert, *oldverts;
1205         MSticky *ms;
1206         KeyBlock *actkey=0, *currkey;
1207         EditVert *eve;
1208         EditVlak *evl;
1209         EditEdge *eed;
1210         float *fp, *newkey, *oldkey, nor[3];
1211         int i, a, ototvert;
1212 #ifdef __NLA
1213         MDeformVert *dvert;
1214         int     usedDvert = 0;
1215 #endif
1216
1217         ototvert= me->totvert;
1218
1219         /* lets save the old verts just in case we are actually working on
1220          * a key ... we now do processing of the keys at the end*/
1221         oldverts = me->mvert;
1222
1223         /* this one also tests of edges are not in faces: */
1224         /* eed->f==0: not in face, f==1: draw it */
1225         /* eed->f1 : flag for dynaface (cylindertest, old engine) */
1226         /* eve->f1 : flag for dynaface (sphere test, old engine) */
1227         edge_drawflags();
1228         
1229         /* WATCH IT: in evl->f is punoflag (for vertex normal) */
1230         vertexnormals( (me->flag & ME_NOPUNOFLIP)==0 );
1231         
1232         eed= em->edges.first;
1233         while(eed) {
1234                 if(eed->f==0) G.totface++;
1235                 eed= eed->next;
1236         }
1237         
1238         /* new Face block */
1239         if(G.totface==0) mface= 0;
1240         else mface= MEM_callocN(G.totface*sizeof(MFace), "loadeditMesh1");
1241         /* nieuw Vertex block */
1242         if(G.totvert==0) mvert= 0;
1243         else mvert= MEM_callocN(G.totvert*sizeof(MVert), "loadeditMesh2");
1244
1245 #ifdef __NLA
1246         if (G.totvert==0) dvert=0;
1247         else dvert = MEM_callocN(G.totvert*sizeof(MDeformVert), "loadeditMesh3");
1248
1249         if (me->dvert) free_dverts(me->dvert, me->totvert);
1250         me->dvert=dvert;
1251 #endif          
1252
1253         me->mvert= mvert;
1254
1255         if(me->mface) MEM_freeN(me->mface);
1256         me->mface= mface;
1257         me->totvert= G.totvert;
1258         me->totface= G.totface;
1259                 
1260         /* the vertices, abuse ->vn as counter */
1261         eve= em->verts.first;
1262         a=0;
1263
1264         while(eve) {
1265                 VECCOPY(mvert->co, eve->co);
1266                 mvert->mat_nr= 255;  /* what was this for, halos? */
1267                 
1268                 /* vertex normal */
1269                 VECCOPY(nor, eve->no);
1270                 VecMulf(nor, 32767.0);
1271                 VECCOPY(mvert->no, nor);
1272 #ifdef __NLA
1273                 /* NEW VERSION */
1274                 if (dvert){
1275                         dvert->totweight=eve->totweight;
1276                         if (eve->dw){
1277                                 dvert->dw = MEM_callocN (sizeof(MDeformWeight)*eve->totweight,
1278                                                                                  "deformWeight");
1279                                 memcpy (dvert->dw, eve->dw, 
1280                                                 sizeof(MDeformWeight)*eve->totweight);
1281                                 usedDvert++;
1282                         }
1283                 }
1284 #endif
1285
1286                 eve->vn= (EditVert *)(long)(a++);  /* counter */
1287                         
1288                 mvert->flag= 0;
1289                         
1290                 mvert->flag= 0;
1291                 if(eve->f1==1) mvert->flag |= ME_SPHERETEST;
1292                 mvert->flag |= (eve->f & 1);
1293                 if (eve->h) mvert->flag |= ME_HIDE;                     
1294                         
1295                 eve= eve->next;
1296                 mvert++;
1297 #ifdef __NLA
1298                 dvert++;
1299 #endif
1300         }
1301         
1302 #ifdef __NLA
1303         /* If we didn't actually need the dverts, get rid of them */
1304         if (!usedDvert){
1305                 free_dverts(me->dvert, G.totvert);
1306                 me->dvert=NULL;
1307         }
1308 #endif
1309
1310         /* the faces */
1311         evl= em->faces.first;
1312         i = 0;
1313         while(evl) {
1314                 mface= &((MFace *) me->mface)[i];
1315                         
1316                 mface->v1= (unsigned int) evl->v1->vn;
1317                 mface->v2= (unsigned int) evl->v2->vn;
1318                 mface->v3= (unsigned int) evl->v3->vn;
1319                 if(evl->v4) mface->v4= (unsigned int) evl->v4->vn;
1320                         
1321                 mface->mat_nr= evl->mat_nr;
1322                 mface->puno= evl->f;
1323                 mface->flag= evl->flag;
1324                         
1325                 /* mat_nr in vertex */
1326                 if(me->totcol>1) {
1327                         mvert= me->mvert+mface->v1;
1328                         if(mvert->mat_nr == (char)255) mvert->mat_nr= mface->mat_nr;
1329                         mvert= me->mvert+mface->v2;
1330                         if(mvert->mat_nr == (char)255) mvert->mat_nr= mface->mat_nr;
1331                         mvert= me->mvert+mface->v3;
1332                         if(mvert->mat_nr == (char)255) mvert->mat_nr= mface->mat_nr;
1333                         if(mface->v4) {
1334                                 mvert= me->mvert+mface->v4;
1335                                 if(mvert->mat_nr == (char)255) mvert->mat_nr= mface->mat_nr;
1336                         }
1337                 }
1338                         
1339                 /* watch: evl->e1->f==0 means loose edge */ 
1340                         
1341                 if(evl->e1->f==1) {
1342                         mface->edcode |= ME_V1V2; 
1343                         evl->e1->f= 2;
1344                 }                       
1345                 if(evl->e2->f==1) {
1346                         mface->edcode |= ME_V2V3; 
1347                         evl->e2->f= 2;
1348                 }
1349                 if(evl->e3->f==1) {
1350                         if(evl->v4) {
1351                                 mface->edcode |= ME_V3V4;
1352                         }
1353                         else {
1354                                 mface->edcode |= ME_V3V1;
1355                         }
1356                         evl->e3->f= 2;
1357                 }
1358                 if(evl->e4 && evl->e4->f==1) {
1359                         mface->edcode |= ME_V4V1; 
1360                         evl->e4->f= 2;
1361                 }
1362                         
1363                 /* no index '0' at location 3 or 4 */
1364                 if(evl->v4) fix_faceindices(mface, evl, 4);
1365                 else fix_faceindices(mface, evl, 3);
1366                         
1367                 i++;
1368                 evl= evl->next;
1369         }
1370                 
1371         /* add loose edges as a face */
1372         eed= em->edges.first;
1373         while(eed) {
1374                 if( eed->f==0 ) {
1375                         mface= &((MFace *) me->mface)[i];
1376                         mface->v1= (unsigned int) eed->v1->vn;
1377                         mface->v2= (unsigned int) eed->v2->vn;
1378                         test_index_mface(mface, 2);
1379                         mface->edcode= ME_V1V2;
1380                         i++;
1381                 }
1382                 eed= eed->next;
1383         }
1384                 
1385         tex_space_mesh(me);
1386
1387         /* tface block, always when undo even when it wasnt used, 
1388            this because of empty me pointer */
1389         if( (me->tface || undo) && me->totface ) {
1390                 TFace *tfn, *tf;
1391                         
1392                 tf=tfn= MEM_callocN(sizeof(TFace)*me->totface, "tface");
1393                 evl= em->faces.first;
1394                 while(evl) {
1395                                 
1396                         *tf= evl->tf;
1397                                 
1398                         if(G.f & G_FACESELECT) {
1399                                 if( vlakselectedAND(evl, 1) ) tf->flag |= TF_SELECT;
1400                                 else tf->flag &= ~TF_SELECT;
1401                         }
1402                                 
1403                         tf++;
1404                         evl= evl->next;
1405                 }
1406                 /* if undo, me was empty */
1407                 if(me->tface) MEM_freeN(me->tface);
1408                 me->tface= tfn;
1409         }
1410         else if(me->tface) {
1411                 MEM_freeN(me->tface);
1412                 me->tface= NULL;
1413         }
1414                 
1415         /* mcol: same as tface... */
1416         if( (me->mcol || undo) && me->totface) {
1417                 unsigned int *mcn, *mc;
1418
1419                 mc=mcn= MEM_mallocN(4*sizeof(int)*me->totface, "mcol");
1420                 evl= em->faces.first;
1421                 while(evl) {
1422                         memcpy(mc, evl->tf.col, 4*sizeof(int));
1423                                 
1424                         mc+=4;
1425                         evl= evl->next;
1426                 }
1427                 if(me->mcol) MEM_freeN(me->mcol);
1428                         me->mcol= (MCol *)mcn;
1429         }
1430         else if(me->mcol) {
1431                 MEM_freeN(me->mcol);
1432                 me->mcol= 0;
1433         }
1434
1435
1436         /* are there keys? */
1437         if(me->key) {
1438
1439                 /* find the active key */
1440                 actkey= me->key->block.first;
1441                 while(actkey) {
1442                         if(actkey->flag & SELECT) break;
1443                         actkey= actkey->next;
1444                 }
1445
1446                 /* Lets reorder the key data so that things line up roughly
1447                  * with the way things were before editmode */
1448                 currkey = me->key->block.first;
1449                 while(currkey) {
1450                         
1451                         fp= newkey= MEM_callocN(me->key->elemsize*G.totvert,  "currkey->data");
1452                         oldkey = currkey->data;
1453
1454                         eve= em->verts.first;
1455
1456                         i = 0;
1457                         mvert = me->mvert;
1458                         while(eve) {
1459                                 if (eve->keyindex >= 0) { // old vertex
1460                                         if(currkey == actkey) {
1461                                                 if (actkey == me->key->refkey) {
1462                                                         VECCOPY(fp, mvert->co);
1463                                                 }
1464                                                 else {
1465                                                         VECCOPY(fp, mvert->co);
1466                                                         if(oldverts) {
1467                                                                 VECCOPY(mvert->co, oldverts[eve->keyindex].co);
1468                                                         }
1469                                                 }
1470                                         }
1471                                         else {
1472                                                 if(oldkey) {
1473                                                         VECCOPY(fp, oldkey + 3 * eve->keyindex);
1474                                                 }
1475                                         }
1476                                 }
1477                                 else {
1478                                         VECCOPY(fp, mvert->co);
1479                                 }
1480                                 fp+= 3;
1481                                 ++i;
1482                                 ++mvert;
1483                                 eve= eve->next;
1484                         }
1485                         currkey->totelem= G.totvert;
1486                         if(currkey->data) MEM_freeN(currkey->data);
1487                         currkey->data = newkey;
1488                         
1489                         currkey= currkey->next;
1490                 }
1491
1492         }
1493
1494         if(oldverts) MEM_freeN(oldverts);
1495
1496         if(actkey) do_spec_key(me->key);
1497         
1498         /* te be sure: clear ->vn pointers */
1499         eve= em->verts.first;
1500         while(eve) {
1501                 eve->vn= 0;
1502                 eve= eve->next;
1503         }
1504
1505         /* displists of all users, including this one */
1506         freedisplist(&me->disp);
1507         freedisplist(&G.obedit->disp);
1508         
1509         /* sticky */
1510         if(me->msticky) {
1511                 if (ototvert<me->totvert) {
1512                         ms= MEM_callocN(me->totvert*sizeof(MSticky), "msticky");
1513                         memcpy(ms, me->msticky, ototvert*sizeof(MSticky));
1514                         MEM_freeN(me->msticky);
1515                         me->msticky= ms;
1516                         error("Sticky was too small");
1517                 }
1518         }
1519         waitcursor(0);
1520 }
1521
1522 void remake_editMesh(void)
1523 {
1524         undo_push_mesh("Undo all changes");
1525         make_editMesh();
1526         allqueue(REDRAWVIEW3D, 0);
1527         makeDispList(G.obedit);
1528 }
1529
1530 /* *********************  TOOLS  ********************* */
1531
1532
1533
1534 void make_sticky(void)
1535 {
1536         Object *ob;
1537         Base *base;
1538         MVert *mvert;
1539         Mesh *me;
1540         MSticky *ms;
1541         float ho[4], mat[4][4];
1542         int a;
1543         
1544         if(G.scene->camera==0) return;
1545         
1546         if(G.obedit) {
1547                 error("Unable to make sticky in Edit Mode");
1548                 return;
1549         }
1550         base= FIRSTBASE;
1551         while(base) {
1552                 if TESTBASELIB(base) {
1553                         if(base->object->type==OB_MESH) {
1554                                 ob= base->object;
1555                                 
1556                                 me= ob->data;
1557                                 mvert= me->mvert;
1558                                 if(me->msticky) MEM_freeN(me->msticky);
1559                                 me->msticky= MEM_mallocN(me->totvert*sizeof(MSticky), "sticky");
1560                                 
1561                                 /* like convert to render data */               
1562                                 R.r= G.scene->r;
1563                                 R.r.xsch= (R.r.size*R.r.xsch)/100;
1564                                 R.r.ysch= (R.r.size*R.r.ysch)/100;
1565                                 
1566                                 R.afmx= R.r.xsch/2;
1567                                 R.afmy= R.r.ysch/2;
1568                                 
1569                                 R.ycor= ( (float)R.r.yasp)/( (float)R.r.xasp);
1570                 
1571                                 R.rectx= R.r.xsch; 
1572                                 R.recty= R.r.ysch;
1573                                 R.xstart= -R.afmx; 
1574                                 R.ystart= -R.afmy;
1575                                 R.xend= R.xstart+R.rectx-1;
1576                                 R.yend= R.ystart+R.recty-1;
1577                 
1578                                 where_is_object(G.scene->camera);
1579                                 Mat4CpyMat4(R.viewinv, G.scene->camera->obmat);
1580                                 Mat4Ortho(R.viewinv);
1581                                 Mat4Invert(R.viewmat, R.viewinv);
1582                                 
1583                                 RE_setwindowclip(1, -1);
1584                 
1585                                 where_is_object(ob);
1586                                 Mat4MulMat4(mat, ob->obmat, R.viewmat);
1587                 
1588                                 ms= me->msticky;
1589                                 for(a=0; a<me->totvert; a++, ms++, mvert++) {
1590                                         VECCOPY(ho, mvert->co);
1591                                         Mat4MulVecfl(mat, ho);
1592                                         RE_projectverto(ho, ho);
1593                                         ms->co[0]= ho[0]/ho[3];
1594                                         ms->co[1]= ho[1]/ho[3];
1595                                 }
1596                         }
1597                 }
1598                 base= base->next;
1599         }
1600         allqueue(REDRAWBUTSEDIT, 0);
1601 }
1602
1603 void fasterdraw(void)
1604 {
1605         Base *base;
1606         Mesh *me;
1607         MFace *mface;
1608         int toggle, a;
1609
1610         if(G.obedit) return;
1611
1612         /* reset flags */
1613         me= G.main->mesh.first;
1614         while(me) {
1615                 me->flag &= ~ME_ISDONE;
1616                 me= me->id.next;
1617         }
1618
1619         base= FIRSTBASE;
1620         while(base) {
1621                 if( TESTBASELIB(base) && (base->object->type==OB_MESH)) {
1622                         me= base->object->data;
1623                         if(me->id.lib==0 && (me->flag & ME_ISDONE)==0) {
1624                                 me->flag |= ME_ISDONE;
1625                                 mface= me->mface;
1626                                 toggle= 0;
1627                                 for(a=0; a<me->totface; a++) {
1628                                         if( (mface->edcode & ME_V1V2) && ( (toggle++) & 1) ) {
1629                                                 mface->edcode-= ME_V1V2;
1630                                         }
1631                                         if( (mface->edcode & ME_V2V3) && ( (toggle++) & 1)) {
1632                                                 mface->edcode-= ME_V2V3;
1633                                         }
1634                                         if( (mface->edcode & ME_V3V1) && ( (toggle++) & 1)) {
1635                                                 mface->edcode-= ME_V3V1;
1636                                         }
1637                                         if( (mface->edcode & ME_V4V1) && ( (toggle++) & 1)) {
1638                                                 mface->edcode-= ME_V4V1;
1639                                         }
1640                                         if( (mface->edcode & ME_V3V4) && ( (toggle++) & 1)) {
1641                                                 mface->edcode-= ME_V3V4;
1642                                         }
1643                                         mface++;
1644                                 }
1645                         }
1646                 }
1647                 base= base->next;
1648         }
1649
1650         /* important?: reset flags again */
1651         me= G.main->mesh.first;
1652         while(me) {
1653                 me->flag &= ~ME_ISDONE;
1654                 me= me->id.next;
1655         }
1656
1657         allqueue(REDRAWVIEW3D, 0);
1658 }
1659
1660 void slowerdraw(void)           /* reset fasterdraw */
1661 {
1662         Base *base;
1663         Mesh *me;
1664         MFace *mface;
1665         int a;
1666
1667         if(G.obedit) return;
1668
1669         base= FIRSTBASE;
1670         while(base) {
1671                 if( TESTBASELIB(base) && (base->object->type==OB_MESH)) {
1672                         me= base->object->data;
1673                         if(me->id.lib==0) {
1674                                 
1675                                 mface= me->mface;
1676                                 
1677                                 for(a=0; a<me->totface; a++) {
1678                                 
1679                                         mface->edcode |= ME_V1V2|ME_V2V3;
1680                                         mface++;
1681                                 }
1682                         }
1683                 }
1684                 base= base->next;
1685         }
1686
1687         allqueue(REDRAWVIEW3D, 0);
1688 }
1689
1690
1691 void convert_to_triface(int all)
1692 {
1693         EditMesh *em = G.editMesh;
1694         EditVlak *evl, *evln, *next;
1695         
1696         undo_push_mesh("Convert Quads to Triangles");
1697         
1698         evl= em->faces.first;
1699         while(evl) {
1700                 next= evl->next;
1701                 if(evl->v4) {
1702                         if(all || vlakselectedAND(evl, 1) ) {
1703                                 
1704                                 evln= addvlaklist(evl->v1, evl->v2, evl->v3, 0, evl);
1705                                 evln= addvlaklist(evl->v1, evl->v3, evl->v4, 0, evl);
1706
1707                                 evln->tf.uv[1][0]= evln->tf.uv[2][0];
1708                                 evln->tf.uv[1][1]= evln->tf.uv[2][1];
1709                                 evln->tf.uv[2][0]= evln->tf.uv[3][0];
1710                                 evln->tf.uv[2][1]= evln->tf.uv[3][1];
1711                                 
1712                                 evln->tf.col[1]= evln->tf.col[2];
1713                                 evln->tf.col[2]= evln->tf.col[3];
1714                                 
1715                                 BLI_remlink(&em->faces, evl);
1716                                 free_editvlak(evl);
1717                         }
1718                 }
1719                 evl= next;
1720         }
1721         
1722 }
1723
1724
1725 void deselectall_mesh(void)     /* toggle */
1726 {
1727         EditMesh *em = G.editMesh;
1728         EditVert *eve;
1729         int a;
1730         
1731         if(G.obedit->lay & G.vd->lay) {
1732                 a= 0;
1733                 eve= em->verts.first;
1734                 while(eve) {
1735                         if(eve->f & 1) {
1736                                 a= 1;
1737                                 break;
1738                         }
1739                         eve= eve->next;
1740                 }
1741                 
1742                 if (a) undo_push_mesh("Deselect All");
1743                 else undo_push_mesh("Select All");
1744                 
1745                 eve= em->verts.first;
1746                 while(eve) {
1747                         if(eve->h==0) {
1748                                 if(a) eve->f&= -2;
1749                                 else eve->f|= 1;
1750                         }
1751                         eve= eve->next;
1752                 }
1753         }
1754         countall();
1755         allqueue(REDRAWVIEW3D, 0);
1756 }
1757
1758
1759 void righthandfaces(int select) /* makes faces righthand turning */
1760 {
1761         EditMesh *em = G.editMesh;
1762         EditEdge *eed, *ed1, *ed2, *ed3, *ed4;
1763         EditVlak *evl, *startvl;
1764         float maxx, nor[3], cent[3];
1765         int totsel, found, foundone, direct, turn, tria_nr;
1766
1767    /* based at a select-connected to witness loose objects */
1768
1769         /* count per edge the amount of faces */
1770
1771         /* find the ultimate left, front, upper face (not manhattan dist!!) */
1772         /* also evaluate both triangle cases in quad, since these can be non-flat */
1773
1774         /* put normal to the outside, and set the first direction flags in edges */
1775
1776         /* then check the object, and set directions / direction-flags: but only for edges with 1 or 2 faces */
1777         /* this is in fact the 'select connected' */
1778         
1779         /* in case (selected) faces were not done: start over with 'find the ultimate ...' */
1780
1781         waitcursor(1);
1782         
1783         eed= em->edges.first;
1784         while(eed) {
1785                 eed->f= 0;
1786                 eed->f1= 0;
1787                 eed= eed->next;
1788         }
1789
1790         /* count faces and edges */
1791         totsel= 0;
1792         evl= em->faces.first;
1793         while(evl) {
1794                 if(select==0 || vlakselectedAND(evl, 1) ) {
1795                         evl->f= 1;
1796                         totsel++;
1797                         evl->e1->f1++;
1798                         evl->e2->f1++;
1799                         evl->e3->f1++;
1800                         if(evl->v4) evl->e4->f1++;
1801                 }
1802                 else evl->f= 0;
1803
1804                 evl= evl->next;
1805         }
1806
1807         while(totsel>0) {
1808                 /* from the outside to the inside */
1809
1810                 evl= em->faces.first;
1811                 startvl= NULL;
1812                 maxx= -1.0e10;
1813                 tria_nr= 0;
1814
1815                 while(evl) {
1816                         if(evl->f) {
1817                                 CalcCent3f(cent, evl->v1->co, evl->v2->co, evl->v3->co);
1818                                 cent[0]= cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
1819                                 
1820                                 if(cent[0]>maxx) {
1821                                         maxx= cent[0];
1822                                         startvl= evl;
1823                                         tria_nr= 0;
1824                                 }
1825                                 if(evl->v4) {
1826                                         CalcCent3f(cent, evl->v1->co, evl->v3->co, evl->v4->co);
1827                                         cent[0]= cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
1828                                         
1829                                         if(cent[0]>maxx) {
1830                                                 maxx= cent[0];
1831                                                 startvl= evl;
1832                                                 tria_nr= 1;
1833                                         }
1834                                 }
1835                         }
1836                         evl= evl->next;
1837                 }
1838                 
1839                 /* set first face correct: calc normal */
1840                 
1841                 if(tria_nr==1) {
1842                         CalcNormFloat(startvl->v1->co, startvl->v3->co, startvl->v4->co, nor);
1843                         CalcCent3f(cent, startvl->v1->co, startvl->v3->co, startvl->v4->co);
1844                 } else {
1845                         CalcNormFloat(startvl->v1->co, startvl->v2->co, startvl->v3->co, nor);
1846                         CalcCent3f(cent, startvl->v1->co, startvl->v2->co, startvl->v3->co);
1847                 }
1848                 /* first normal is oriented this way or the other */
1849                 if(select) {
1850                         if(select==2) {
1851                                 if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] > 0.0) flipvlak(startvl);
1852                         }
1853                         else {
1854                                 if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] < 0.0) flipvlak(startvl);
1855                         }
1856                 }
1857                 else if(cent[0]*nor[0]+cent[1]*nor[1]+cent[2]*nor[2] < 0.0) flipvlak(startvl);
1858
1859
1860                 eed= startvl->e1;
1861                 if(eed->v1==startvl->v1) eed->f= 1; 
1862                 else eed->f= 2;
1863                 
1864                 eed= startvl->e2;
1865                 if(eed->v1==startvl->v2) eed->f= 1; 
1866                 else eed->f= 2;
1867                 
1868                 eed= startvl->e3;
1869                 if(eed->v1==startvl->v3) eed->f= 1; 
1870                 else eed->f= 2;
1871                 
1872                 eed= startvl->e4;
1873                 if(eed) {
1874                         if(eed->v1==startvl->v4) eed->f= 1; 
1875                         else eed->f= 2;
1876                 }
1877                 
1878                 startvl->f= 0;
1879                 totsel--;
1880
1881                 /* test normals */
1882                 found= 1;
1883                 direct= 1;
1884                 while(found) {
1885                         found= 0;
1886                         if(direct) evl= em->faces.first;
1887                         else evl= em->faces.last;
1888                         while(evl) {
1889                                 if(evl->f) {
1890                                         turn= 0;
1891                                         foundone= 0;
1892
1893                                         ed1= evl->e1;
1894                                         ed2= evl->e2;
1895                                         ed3= evl->e3;
1896                                         ed4= evl->e4;
1897
1898                                         if(ed1->f) {
1899                                                 if(ed1->v1==evl->v1 && ed1->f==1) turn= 1;
1900                                                 if(ed1->v2==evl->v1 && ed1->f==2) turn= 1;
1901                                                 foundone= 1;
1902                                         }
1903                                         else if(ed2->f) {
1904                                                 if(ed2->v1==evl->v2 && ed2->f==1) turn= 1;
1905                                                 if(ed2->v2==evl->v2 && ed2->f==2) turn= 1;
1906                                                 foundone= 1;
1907                                         }
1908                                         else if(ed3->f) {
1909                                                 if(ed3->v1==evl->v3 && ed3->f==1) turn= 1;
1910                                                 if(ed3->v2==evl->v3 && ed3->f==2) turn= 1;
1911                                                 foundone= 1;
1912                                         }
1913                                         else if(ed4 && ed4->f) {
1914                                                 if(ed4->v1==evl->v4 && ed4->f==1) turn= 1;
1915                                                 if(ed4->v2==evl->v4 && ed4->f==2) turn= 1;
1916                                                 foundone= 1;
1917                                         }
1918
1919                                         if(foundone) {
1920                                                 found= 1;
1921                                                 totsel--;
1922                                                 evl->f= 0;
1923
1924                                                 if(turn) {
1925                                                         if(ed1->v1==evl->v1) ed1->f= 2; 
1926                                                         else ed1->f= 1;
1927                                                         if(ed2->v1==evl->v2) ed2->f= 2; 
1928                                                         else ed2->f= 1;
1929                                                         if(ed3->v1==evl->v3) ed3->f= 2; 
1930                                                         else ed3->f= 1;
1931                                                         if(ed4) {
1932                                                                 if(ed4->v1==evl->v4) ed4->f= 2; 
1933                                                                 else ed4->f= 1;
1934                                                         }
1935
1936                                                         flipvlak(evl);
1937
1938                                                 }
1939                                                 else {
1940                                                         if(ed1->v1== evl->v1) ed1->f= 1; 
1941                                                         else ed1->f= 2;
1942                                                         if(ed2->v1==evl->v2) ed2->f= 1; 
1943                                                         else ed2->f= 2;
1944                                                         if(ed3->v1==evl->v3) ed3->f= 1; 
1945                                                         else ed3->f= 2;
1946                                                         if(ed4) {
1947                                                                 if(ed4->v1==evl->v4) ed4->f= 1; 
1948                                                                 else ed4->f= 2;
1949                                                         }
1950                                                 }
1951                                         }
1952                                 }
1953                                 if(direct) evl= evl->next;
1954                                 else evl= evl->prev;
1955                         }
1956                         direct= 1-direct;
1957                 }
1958         }
1959
1960         recalc_editnormals();
1961         
1962         makeDispList(G.obedit);
1963         
1964         waitcursor(0);
1965 }
1966
1967 static EditVert *findnearestvert(short sel)
1968 {
1969         EditMesh *em = G.editMesh;
1970         /* if sel==1 the vertices with flag==1 get a disadvantage */
1971         EditVert *eve,*act=0;
1972         static EditVert *acto=0;
1973         short dist=100,temp,mval[2];
1974
1975         if(em->verts.first==0) return 0;
1976
1977         /* do projection */
1978         calc_meshverts_ext();   /* drawobject.c */
1979         
1980         /* we count from acto->next to last, and from first to acto */
1981         /* does acto exist? */
1982         eve= em->verts.first;
1983         while(eve) {
1984                 if(eve==acto) break;
1985                 eve= eve->next;
1986         }
1987         if(eve==0) acto= em->verts.first;
1988
1989         if(acto==0) return 0;
1990
1991         /* is there an indicated vertex? part 1 */
1992         getmouseco_areawin(mval);
1993         eve= acto->next;
1994         while(eve) {
1995                 if(eve->h==0) {
1996                         temp= abs(mval[0]- eve->xs)+ abs(mval[1]- eve->ys);
1997                         if( (eve->f & 1)==sel ) temp+=5;
1998                         if(temp<dist) {
1999                                 act= eve;
2000                                 dist= temp;
2001                                 if(dist<4) break;
2002                         }
2003                 }
2004                 eve= eve->next;
2005         }
2006         /* is there an indicated vertex? part 2 */
2007         if(dist>3) {
2008                 eve= em->verts.first;
2009                 while(eve) {
2010                         if(eve->h==0) {
2011                                 temp= abs(mval[0]- eve->xs)+ abs(mval[1]- eve->ys);
2012                                 if( (eve->f & 1)==sel ) temp+=5;
2013                                 if(temp<dist) {
2014                                         act= eve;
2015                                         if(temp<4) break;
2016                                         dist= temp;
2017                                 }
2018                                 if(eve== acto) break;
2019                         }
2020                         eve= eve->next;
2021                 }
2022         }
2023
2024         acto= act;
2025         return act;
2026 }
2027
2028
2029 static EditEdge *findnearestedge()
2030 {
2031         EditMesh *em = G.editMesh;
2032         EditEdge *closest, *eed;
2033         EditVert *eve;
2034         short found=0, mval[2];
2035         float distance[2], v1[2], v2[2], mval2[2];
2036         
2037         if(em->edges.first==0) return NULL;
2038         else eed=em->edges.first;       
2039         
2040         /* reset flags */       
2041         for(eve=em->verts.first; eve; eve=eve->next){
2042                 eve->f &= ~2;
2043         }       
2044                 
2045         calc_meshverts_ext_f2();        /*sets (eve->f & 2) for vertices that aren't visible*/
2046         getmouseco_areawin(mval);
2047         closest=NULL;
2048         
2049         mval2[0] = (float)mval[0];
2050         mval2[1] = (float)mval[1];
2051         
2052         eed=em->edges.first;
2053         /*compare the distance to the rest of the edges and find the closest one*/
2054         while(eed) {
2055                 /* Are both vertices of the edge ofscreen or either of them hidden? then don't select the edge*/
2056                 if( !((eed->v1->f & 2) && (eed->v2->f & 2)) && (eed->v1->h==0 && eed->v2->h==0)){
2057                         v1[0] = eed->v1->xs;
2058                         v1[1] = eed->v1->ys;
2059                         v2[0] = eed->v2->xs;
2060                         v2[1] = eed->v2->ys;
2061                         
2062                         distance[1] = PdistVL2Dfl(mval2, v1, v2);
2063                         
2064                         if(distance[1]<50){
2065                                 /*do we have to compare it to other distances? */                       
2066                                 if(found) {
2067                                         if (distance[1]<distance[0]){
2068                                                 distance[0]=distance[1];
2069                                                 /*save the current closest edge*/
2070                                                 closest=eed;    
2071                                         }
2072                                 } else {
2073                                         distance[0]=distance[1];
2074                                         closest=eed;
2075                                         found=1;
2076                                 }
2077                         }
2078                 }
2079                 eed= eed->next;
2080         }
2081         
2082         /* reset flags */       
2083         for(eve=em->verts.first; eve; eve=eve->next){
2084                 eve->f &= ~2;
2085         }
2086         
2087         if(found) return closest;
2088         else return 0;
2089 }
2090
2091 /* does the same as findnearestedge but both vertices of the edge should be on screen*/
2092 static EditEdge *findnearestvisibleedge()
2093 {
2094         EditMesh *em = G.editMesh;
2095         EditEdge *closest, *eed;
2096         EditVert *eve;
2097         short found=0, mval[2];
2098         float distance[2], v1[2], v2[2], mval2[2];
2099                 
2100         if(em->edges.first==0) return NULL;
2101         else eed=em->edges.first;       
2102         
2103         /* reset flags */       
2104         for(eve=em->verts.first; eve; eve=eve->next){
2105                 eve->f &= ~2;
2106         }       
2107         calc_meshverts_ext_f2();        /*sets (eve->f & 2) for vertices that aren't visible*/
2108         
2109         closest=NULL;
2110         getmouseco_areawin(mval);
2111         
2112         mval2[0] = (float)mval[0];      /* cast to float because of the pdist function only taking floats...*/
2113         mval2[1] = (float)mval[1];
2114         
2115         eed=em->edges.first;
2116         while(eed) {                                            /* compare the distance to the rest of the edges and find the closest one*/
2117                 if( !((eed->v1->f | eed->v2->f) & 2) && (eed->v1->h==0 && eed->v2->h==0) ){     /* only return edges with both vertices on screen */
2118                         v1[0] = eed->v1->xs;                    
2119                         v1[1] = eed->v1->ys;
2120                         v2[0] = eed->v2->xs;
2121                         v2[1] = eed->v2->ys;
2122                         
2123                         distance[1] = PdistVL2Dfl(mval2, v1, v2);
2124                         
2125                         if(distance[1]<50){                     /* TODO: make this maximum selecting distance selectable (the same with vertice select?) */
2126                                 if(found) {                     /*do we have to compare it to other distances? */
2127                                         if (distance[1]<distance[0]){
2128                                                 distance[0]=distance[1];
2129                                                 closest=eed;    /*save the current closest edge*/
2130                                         }
2131                                 } else {
2132                                         distance[0]=distance[1];
2133                                         closest=eed;
2134                                         found=1;
2135                                 }
2136                         }
2137                 }
2138                 eed= eed->next;
2139         }
2140         
2141         /* reset flags */       
2142         for(eve=em->verts.first; eve; eve=eve->next){
2143                 eve->f &= ~2;
2144         }
2145         
2146         if(found) return closest;
2147         else return 0;
2148 }
2149
2150 #if 0
2151 /* this is a template function to demonstrate a loop with drawing...
2152    it is a temporal mode, so use with wisdom! if you can avoid, always better. (ton)
2153 */
2154 void loop(int mode)
2155 {
2156         EditEdge *eed;
2157         int mousemove= 1;
2158
2159         while(mousemove) {
2160                 /* uses callback mechanism to draw it all in current area */
2161                 scrarea_do_windraw(curarea); 
2162                 
2163                 /* do your stuff */
2164                 eed= findnearestedge();
2165                 
2166                 /* set window matrix to perspective, default an area returns with buttons transform */
2167                 persp(PERSP_VIEW);
2168                 /* make a copy, for safety */
2169                 glPushMatrix();
2170                 /* multiply with the object transformation */
2171                 mymultmatrix(G.obedit->obmat);
2172                 
2173                 /* draw */
2174                 if(eed) {
2175                         glColor3ub(255, 255, 0);
2176                         glBegin(GL_LINES);
2177                         glVertex3fv(eed->v1->co);
2178                         glVertex3fv(eed->v2->co);
2179                         glEnd();
2180                 }
2181                 
2182                 /* restore matrix transform */
2183                 glPopMatrix();
2184                 
2185                 headerprint("We are now in evil edge select mode. Press any key to exit");
2186                 
2187                 /* this also verifies other area/windows for clean swap */
2188                 screen_swapbuffers();
2189                 
2190                 /* testing for user input... */
2191                 while(qtest()) {
2192                         unsigned short val;
2193                         short event= extern_qread(&val);        // extern_qread stores important events for the mainloop to handle 
2194
2195                         /* val==0 on key-release event */
2196                         if(val && event!=MOUSEY && event!=MOUSEX) {
2197                                 mousemove= 0;
2198                         }
2199                 }
2200                 /* sleep 0.01 second to prevent overload in this poor loop */
2201                 PIL_sleep_ms(10);       
2202                 
2203         }
2204         
2205         /* send event to redraw this window, does header too */
2206         addqueue(curarea->win, REDRAW, 1); 
2207 }
2208 #endif
2209
2210 /* 
2211 functionality: various loop functions
2212 parameters: mode tells the function what it should do with the loop:
2213                 LOOP_SELECT = select
2214                 LOOP_CUT = cut in half
2215 */      
2216
2217 void loopoperations(char mode)
2218 {
2219         EditMesh *em = G.editMesh;
2220         EditVert* look = NULL;
2221
2222         EditEdge *start, *eed, *opposite,*currente, *oldstart;
2223         EditEdge **tagged = NULL,**taggedsrch = NULL,*close;
2224
2225         EditVlak *evl,**percentfacesloop = NULL, *currentvl,  *formervl;        
2226
2227         short lastface=0, foundedge=0, c=0, tri=0, side=1, totface=0, searching=1, event=0, noface=1;
2228         short skip,nextpos,percentfaces;
2229
2230         int i=0,ect=0,j=0,k=0,cut,smooth,timesthrough=0,inset = 0;
2231
2232         float percentcut, outcut;
2233
2234         char mesg[100];
2235
2236         if ((G.obedit==0) || (em->faces.first==0)) return;
2237         
2238         if(mode==LOOP_CUT)undo_push_mesh("Face Loop Subdivide");
2239         else if(mode==LOOP_SELECT)undo_push_mesh("Select Face Loop");   
2240
2241         SetBlenderCursor(BC_VLOOPCURSOR);
2242
2243         start=NULL;
2244         oldstart=NULL;
2245
2246         while(searching){
2247                 
2248                 /* reset variables */
2249                 start=eed=opposite=currente=0;
2250                 evl=currentvl=formervl=0;
2251                 side=noface=1;
2252                 lastface=foundedge=c=tri=totface=0;             
2253                                 
2254                 start=findnearestvisibleedge();
2255                                 
2256                 /* If the edge doesn't belong to a face, it's not a valid starting edge */
2257                 if(start){
2258                         start->f |= 16;
2259                         evl=em->faces.first;
2260                         while(evl){
2261                                 if(evl->e1->f & 16){                                    
2262                                         noface=0;
2263                                         evl->e1->f &= ~16;
2264                                 }
2265                                 else if(evl->e2->f & 16){                                       
2266                                         noface=0;
2267                                         evl->e2->f &= ~16;
2268                                 }
2269                                 else if(evl->e3->f & 16){                                       
2270                                         noface=0;
2271                                         evl->e3->f &= ~16;
2272                                 }
2273                                 else if(evl->e4 && (evl->e4->f & 16)){                                  
2274                                         noface=0;
2275                                         evl->e4->f &= ~16;
2276                                 }
2277                                 
2278                                 evl=evl->next;
2279                         }                       
2280                 }                               
2281                                 
2282                 /* Did we find anything that is selectable? */
2283                 if(start && !noface && (oldstart==NULL || start!=oldstart)){
2284                                         
2285                         /* If we stay in the neighbourhood of this edge, we don't have to recalculate the loop everytime*/
2286                         oldstart=start; 
2287                         
2288                         /* Clear flags */
2289                         for(eed=em->edges.first; eed; eed=eed->next){                   
2290                                 eed->f &= ~(2|4|8|32|64);
2291                                 eed->v1->f &= ~(2|8|16);
2292                                 eed->v2->f &= ~(2|8|16);                                
2293                         }
2294                         
2295                         for(evl= em->faces.first; evl; evl=evl->next){                  
2296                                 evl->f &= ~(4|8);
2297                                 totface++;                              
2298                         }
2299                                         
2300                         /* Tag the starting edge */
2301                         start->f |= (2|4|8|64);                         
2302                         start->v1->f |= 2;
2303                         start->v2->f |= 2;              
2304                         
2305                         currente=start;                                         
2306                         
2307                         /*-----Limit the Search----- */
2308                         while(!lastface && c<totface+1){
2309                                 
2310                                 /*----------Get Loop------------------------*/
2311                                 tri=foundedge=lastface=0;                                                                                                       
2312                                 evl= em->faces.first;           
2313                                 while(evl && !foundedge && !tri){
2314                                                                         
2315                                         if(!(evl->v4)){ /* Exception for triangular faces */
2316                                                 
2317                                                 if((evl->e1->f | evl->e2->f | evl->e3->f) & 2){
2318                                                         if(!(evl->f & 4)){                                                              
2319                                                                 tri=1;
2320                                                                 currentvl=evl;
2321                                                                 if(side==1) evl->f |= 4;
2322                                                         }
2323                                                 }                                               
2324                                         }
2325                                         else{
2326                                                 
2327                                                 if((evl->e1->f | evl->e2->f | evl->e3->f | evl->e4->f) & 2){
2328                                                         
2329                                                         if(c==0){       /* just pick a face, doesn't matter wich side of the edge we go to */
2330                                                                 if(!(evl->f & 4)){
2331                                                                         
2332                                                                         if(!(evl->e1->v1->f & 2) && !(evl->e1->v2->f & 2)){
2333                                                                                 if(evl->e1->v1->h==0 && evl->e1->v2->h==0){
2334                                                                                         opposite=evl->e1;                                                                                                               
2335                                                                                         foundedge=1;
2336                                                                                 }
2337                                                                         }
2338                                                                         else if(!(evl->e2->v1->f & 2) && !(evl->e2->v2->f & 2)){
2339                                                                                 if(evl->e2->v1->h==0 && evl->e2->v2->h==0){
2340                                                                                         opposite=evl->e2;
2341                                                                                         foundedge=1;
2342                                                                                 }
2343                                                                         }
2344                                                                         else if(!(evl->e3->v1->f & 2) && !(evl->e3->v2->f & 2)){
2345                                                                                 if(evl->e3->v1->h==0 && evl->e3->v2->h==0){
2346                                                                                         opposite=evl->e3;
2347                                                                                         foundedge=1;
2348                                                                                 }
2349                                                                         }
2350                                                                         else if(!(evl->e4->v1->f & 2) && !(evl->e4->v2->f & 2)){
2351                                                                                 if(evl->e4->v1->h==0 && evl->e4->v2->h==0){
2352                                                                                         opposite=evl->e4;
2353                                                                                         foundedge=1;
2354                                                                                 }
2355                                                                         }
2356                                                                         
2357                                                                         if(foundedge){
2358                                                                                 currentvl=evl;
2359                                                                                 formervl=evl;
2360                                                                         
2361                                                                                 /* mark this side of the edge so we know in which direction we went */
2362                                                                                 if(side==1) evl->f |= 4;
2363                                                                         }
2364                                                                 }
2365                                                         }
2366                                                         else {  
2367                                                                 if(evl!=formervl){      /* prevent going backwards in the loop */
2368                                                                 
2369                                                                         if(!(evl->e1->v1->f & 2) && !(evl->e1->v2->f & 2)){
2370                                                                                 if(evl->e1->v1->h==0 && evl->e1->v2->h==0){
2371                                                                                         opposite=evl->e1;                                                                                                               
2372                                                                                         foundedge=1;
2373                                                                                 }
2374                                                                         }
2375                                                                         else if(!(evl->e2->v1->f & 2) && !(evl->e2->v2->f & 2)){
2376                                                                                 if(evl->e2->v1->h==0 && evl->e2->v2->h==0){
2377                                                                                         opposite=evl->e2;
2378                                                                                         foundedge=1;
2379                                                                                 }
2380                                                                         }
2381                                                                         else if(!(evl->e3->v1->f & 2) && !(evl->e3->v2->f & 2)){
2382                                                                                 if(evl->e3->v1->h==0 && evl->e3->v2->h==0){
2383                                                                                         opposite=evl->e3;
2384                                                                                         foundedge=1;
2385                                                                                 }
2386                                                                         }
2387                                                                         else if(!(evl->e4->v1->f & 2) && !(evl->e4->v2->f & 2)){
2388                                                                                 if(evl->e4->v1->h==0 && evl->e4->v2->h==0){
2389                                                                                         opposite=evl->e4;
2390                                                                                         foundedge=1;
2391                                                                                 }
2392                                                                         }
2393                                                                         
2394                                                                         currentvl=evl;
2395                                                                 }
2396                                                         }
2397                                                 }
2398                                         }
2399                                 evl=evl->next;
2400                                 }
2401                                 /*----------END Get Loop------------------------*/
2402                                 
2403                         
2404                                 /*----------Decisions-----------------------------*/
2405                                 if(foundedge){
2406                                         /* mark the edge and face as done */                                    
2407                                         currente->f |= 8;
2408                                         currentvl->f |= 8;
2409
2410                                         if(opposite->f & 4) lastface=1; /* found the starting edge! close loop */                                                               
2411                                         else{
2412                                                 /* un-set the testflags */
2413                                                 currente->f &= ~2;
2414                                                 currente->v1->f &= ~2;
2415                                                 currente->v2->f &= ~2;                                                  
2416                                                 
2417                                                 /* set the opposite edge to be the current edge */                              
2418                                                 currente=opposite;                                                      
2419                                                 
2420                                                 /* set the current face to be the FORMER face (to prevent going backwards in the loop) */
2421                                                 formervl=currentvl;
2422                                                 
2423                                                 /* set the testflags */
2424                                                 currente->f |= 2;
2425                                                 currente->v1->f |= 2;
2426                                                 currente->v2->f |= 2;                   
2427                                         }
2428                                         c++;
2429                                 }
2430                                 else{   
2431                                         /* un-set the testflags */
2432                                         currente->f &= ~2;
2433                                         currente->v1->f &= ~2;
2434                                         currente->v2->f &= ~2;
2435                                         
2436                                         /* mark the edge and face as done */
2437                                         currente->f |= 8;
2438                                         currentvl->f |= 8;
2439                                         
2440                                         
2441                                                                                                 
2442                                         /* is the the first time we've ran out of possible faces?
2443                                         *  try to start from the beginning but in the opposite direction go as far as possible
2444                                         */                              
2445                                         if(side==1){                                            
2446                                                 if(tri)tri=0;
2447                                                 currente=start;
2448                                                 currente->f |= 2;
2449                                                 currente->v1->f |= 2;
2450                                                 currente->v2->f |= 2;                                   
2451                                                 side++;
2452                                                 c=0;
2453                                         }
2454                                         else lastface=1;
2455                                 }                               
2456                                 /*----------END Decisions-----------------------------*/
2457                                 
2458                         }
2459                         /*-----END Limit the Search----- */
2460                         
2461                         
2462                         /*------------- Preview lines--------------- */
2463                         
2464                         /* uses callback mechanism to draw it all in current area */
2465                         scrarea_do_windraw(curarea);                    
2466                         
2467                         /* set window matrix to perspective, default an area returns with buttons transform */
2468                         persp(PERSP_VIEW);
2469                         /* make a copy, for safety */
2470                         glPushMatrix();
2471                         /* multiply with the object transformation */
2472                         mymultmatrix(G.obedit->obmat);
2473                         
2474                         glColor3ub(255, 255, 0);
2475                         
2476                         if(mode==LOOP_SELECT){
2477                                 evl= em->faces.first;
2478                                 while(evl){
2479                                         if(evl->f & 8){
2480                                                 
2481                                                 if(!(evl->e1->f & 8)){
2482                                                         glBegin(GL_LINES);                                                      
2483                                                         glVertex3fv(evl->e1->v1->co);
2484                                                         glVertex3fv(evl->e1->v2->co);
2485                                                         glEnd();        
2486                                                 }
2487                                                 
2488                                                 if(!(evl->e2->f & 8)){
2489                                                         glBegin(GL_LINES);                                                      
2490                                                         glVertex3fv(evl->e2->v1->co);
2491                                                         glVertex3fv(evl->e2->v2->co);
2492                                                         glEnd();        
2493                                                 }
2494                                                 
2495                                                 if(!(evl->e3->f & 8)){
2496                                                         glBegin(GL_LINES);                                                      
2497                                                         glVertex3fv(evl->e3->v1->co);
2498                                                         glVertex3fv(evl->e3->v2->co);
2499                                                         glEnd();        
2500                                                 }
2501                                                 
2502                                                 if(evl->e4){
2503                                                         if(!(evl->e4->f & 8)){
2504                                                                 glBegin(GL_LINES);                                                      
2505                                                                 glVertex3fv(evl->e4->v1->co);
2506                                                                 glVertex3fv(evl->e4->v2->co);
2507                                                                 glEnd();        
2508                                                         }
2509                                                 }
2510                                         }
2511                                         evl=evl->next;
2512                                 }
2513                         }
2514                                 
2515                         if(mode==LOOP_CUT){
2516                                 evl= em->faces.first;
2517                                 while(evl){
2518                                         if(evl->f & 8){
2519                                                 float cen[2][3];
2520                                                 int a=0;                                                
2521                                                 
2522                                                 evl->v1->f &= ~8;
2523                                                 evl->v2->f &= ~8;
2524                                                 evl->v3->f &= ~8;
2525                                                 if(evl->v4)evl->v4->f &= ~8;
2526                                         
2527                                                 if(evl->e1->f & 8){
2528                                                         cen[a][0]= (evl->e1->v1->co[0] + evl->e1->v2->co[0])/2.0;
2529                                                         cen[a][1]= (evl->e1->v1->co[1] + evl->e1->v2->co[1])/2.0;
2530                                                         cen[a][2]= (evl->e1->v1->co[2] + evl->e1->v2->co[2])/2.0;
2531                                                         
2532                                                         evl->e1->v1->f |= 8;
2533                                                         evl->e1->v2->f |= 8;
2534                                                         
2535                                                         a++;
2536                                                 }
2537                                                 if((evl->e2->f & 8) && a!=2){
2538                                                         cen[a][0]= (evl->e2->v1->co[0] + evl->e2->v2->co[0])/2.0;
2539                                                         cen[a][1]= (evl->e2->v1->co[1] + evl->e2->v2->co[1])/2.0;
2540                                                         cen[a][2]= (evl->e2->v1->co[2] + evl->e2->v2->co[2])/2.0;
2541                                                         
2542                                                         evl->e2->v1->f |= 8;
2543                                                         evl->e2->v2->f |= 8;
2544                                                         
2545                                                         a++;
2546                                                 }
2547                                                 if((evl->e3->f & 8) && a!=2){
2548                                                         cen[a][0]= (evl->e3->v1->co[0] + evl->e3->v2->co[0])/2.0;
2549                                                         cen[a][1]= (evl->e3->v1->co[1] + evl->e3->v2->co[1])/2.0;
2550                                                         cen[a][2]= (evl->e3->v1->co[2] + evl->e3->v2->co[2])/2.0;
2551                                                         
2552                                                         evl->e3->v1->f |= 8;
2553                                                         evl->e3->v2->f |= 8;
2554                                                         
2555                                                         a++;
2556                                                 }
2557                                                 
2558                                                 if(evl->e4){
2559                                                         if((evl->e4->f & 8) && a!=2){
2560                                                                 cen[a][0]= (evl->e4->v1->co[0] + evl->e4->v2->co[0])/2.0;
2561                                                                 cen[a][1]= (evl->e4->v1->co[1] + evl->e4->v2->co[1])/2.0;
2562                                                                 cen[a][2]= (evl->e4->v1->co[2] + evl->e4->v2->co[2])/2.0;
2563                                                                 
2564                                                                 evl->e4->v1->f |= 8;
2565                                                                 evl->e4->v2->f |= 8;
2566                                                         
2567                                                                 a++;
2568                                                         }
2569                                                 }
2570                                                 else{   /* if it's a triangular face, set the remaining vertex as the cutcurve coordinate */                                                                                                            
2571                                                                 if(!(evl->v1->f & 8) && evl->v1->h==0){
2572                                                                         cen[a][0]= evl->v1->co[0];
2573                                                                         cen[a][1]= evl->v1->co[1];
2574                                                                         cen[a][2]= evl->v1->co[2];
2575                                                                         a++;                                                            
2576                                                                 }
2577                                                                 else if(!(evl->v2->f & 8) && evl->v2->h==0){
2578                                                                         cen[a][0]= evl->v2->co[0];
2579                                                                         cen[a][1]= evl->v2->co[1];
2580                                                                         cen[a][2]= evl->v2->co[2];      
2581                                                                         a++;
2582                                                                 }
2583                                                                 else if(!(evl->v3->f & 8) && evl->v3->h==0){
2584                                                                         cen[a][0]= evl->v3->co[0];
2585                                                                         cen[a][1]= evl->v3->co[1];
2586                                                                         cen[a][2]= evl->v3->co[2];
2587                                                                         a++;                                                                    
2588                                                                 }                                                       
2589                                                 }
2590                                                 
2591                                                 if(a==2){
2592                                                         glBegin(GL_LINES);
2593                                                         
2594                                                         glVertex3fv(cen[0]);
2595                                                         glVertex3fv(cen[1]);    
2596                                                                                                 
2597                                                         glEnd();
2598                                                 }                                               
2599                                         }
2600                                         evl=evl->next;
2601                                 }
2602                                 
2603                                 eed=em->edges.first; 
2604                                 while(eed){
2605                                         if(eed->f & 64){
2606                                                 glBegin(GL_LINES);
2607                                                 glColor3ub(200, 255, 200);
2608                                                 glVertex3fv(eed->v1->co);
2609                                                 glVertex3fv(eed->v2->co);
2610                                                 glEnd();
2611                                                 eed=0;
2612                                         }else{
2613                                                 eed = eed->next;
2614                                         }
2615                                 }               
2616                         }
2617                         
2618                         /* restore matrix transform */
2619                         glPopMatrix();
2620                         
2621                         headerprint("LMB to confirm, RMB to cancel");
2622                         
2623                         /* this also verifies other area/windows for clean swap */
2624                         screen_swapbuffers();
2625                         
2626                         /*--------- END Preview Lines------------*/
2627                                 
2628                 }/*if(start!=NULL){ */
2629                 
2630                 while(qtest()) {
2631                         unsigned short val=0;                   
2632                         event= extern_qread(&val);      /* extern_qread stores important events for the mainloop to handle */
2633
2634                         /* val==0 on key-release event */
2635                         if(val && (event==ESCKEY || event==RIGHTMOUSE || event==LEFTMOUSE || event==RETKEY || event == MIDDLEMOUSE)){
2636                                 searching=0;
2637                         }
2638                 }       
2639                 
2640         }/*while(event!=ESCKEY && event!=RIGHTMOUSE && event!=LEFTMOUSE && event!=RETKEY){*/
2641         
2642         /*----------Select Loop------------*/
2643         if(mode==LOOP_SELECT && start!=NULL && ((event==LEFTMOUSE || event==RETKEY) || event == MIDDLEMOUSE || event == BKEY)){
2644                                 
2645                 /* If this is a unmodified select, clear the selection */
2646                 if(!(G.qual & LR_SHIFTKEY) && !(G.qual & LR_ALTKEY)){
2647                         for(evl= em->faces.first;evl;evl=evl->next){
2648                                 evl->v1->f &= !1;
2649                                 evl->v2->f &= !1;
2650                                 evl->v3->f &= !1;
2651                                 if(evl->v4)evl->v4->f &= !1;                    
2652                         }
2653                 }
2654                 /* Alt was not pressed, so add to the selection */
2655                 if(!(G.qual & LR_ALTKEY)){
2656                         for(evl= em->faces.first;evl;evl=evl->next){
2657                                 if(evl->f & 8){
2658                                         evl->v1->f |= 1;
2659                                         evl->v2->f |= 1;
2660                                         evl->v3->f |= 1;
2661                                         if(evl->v4)evl->v4->f |= 1;
2662                                 }
2663                         }
2664                 }
2665                 /* alt was pressed, so subtract from the selection */
2666                 else
2667                 {
2668                         for(evl= em->faces.first;evl;evl=evl->next){
2669                                 if(evl->f & 8){
2670                                         evl->v1->f &= !1;
2671                                         evl->v2->f &= !1;
2672                                         evl->v3->f &= !1;
2673                                         if(evl->v4)evl->v4->f &= !1;
2674                                 }
2675                         }
2676                 }
2677         
2678         }
2679         /*----------END Select Loop------------*/
2680         
2681         /*----------Cut Loop---------------*/                   
2682         if(mode==LOOP_CUT && start!=NULL && (event==LEFTMOUSE || event==RETKEY)){
2683                 
2684                 /* count the number of edges in the loop */             
2685                 for(eed=em->edges.first; eed; eed = eed->next){
2686                         if(eed->f & 8)
2687                                 ect++;
2688                 }               
2689                 
2690                 tagged = MEM_mallocN(ect*sizeof(EditEdge*), "tagged");
2691                 taggedsrch = MEM_mallocN(ect*sizeof(EditEdge*), "taggedsrch");
2692                 for(i=0;i<ect;i++)
2693                 {
2694                         tagged[i] = NULL;
2695                         taggedsrch[i] = NULL;
2696                 }
2697                 ect = 0;
2698                 for(eed=em->edges.first; eed; eed = eed->next){
2699                         if(eed->f & 8)
2700                         {
2701                                 if(eed->h==0){
2702                                         eed->v1->f |= 1;
2703                                         eed->v2->f |= 1;
2704                                         tagged[ect] = eed;
2705                                         eed->f &= ~(32);
2706                                         ect++;
2707                                 }
2708                         }                       
2709                 }
2710                 taggedsrch[0] = tagged[0];
2711
2712                 while(timesthrough < 2)
2713                 {
2714                         i=0;
2715                         while(i < ect){/*Look at the members of the search array to line up cuts*/
2716                                 if(taggedsrch[i]==NULL)break;
2717                                 for(j=0;j<ect;j++){                      /*Look through the list of tagged verts for connected edges*/
2718                                         int addededge = 0;
2719                                         if(taggedsrch[i]->f & 32)        /*If this edgee is marked as flipped, use vert 2*/
2720                                                 look = taggedsrch[i]->v2;
2721                                         else                                                     /*else use vert 1*/
2722                                                 look = taggedsrch[i]->v1;
2723
2724                                         if(taggedsrch[i] == tagged[j])
2725                                                 continue;  /*If we are looking at the same edge, skip it*/
2726         
2727                                         skip = 0;
2728                                         for(k=0;k<ect;k++)      {
2729                                                 if(taggedsrch[k] == NULL)       /*go to empty part of search list without finding*/
2730                                                         break;                                                  
2731                                                 if(tagged[j] == taggedsrch[k]){         /*We found a match already in the list*/
2732                                                         skip = 1;
2733                                                         break;
2734                                                 }
2735                                         }
2736                                         if(skip)
2737                                                 continue;
2738                                         nextpos = 0;
2739                                         if(findedgelist(look,tagged[j]->v2)){
2740                                                 while(nextpos < ect){ /*Find the first open spot in the search array*/
2741                                                         if(taggedsrch[nextpos] == NULL){
2742                                                                 taggedsrch[nextpos] = tagged[j]; /*put tagged[j] in it*/
2743                                                                 taggedsrch[nextpos]->f |= 32;
2744                                                                 addededge = 1;
2745                                                                 break;
2746                                                         }
2747                                                         else
2748                                                                 nextpos++;
2749                                                 }
2750                                         } /* End else if connected to vert 2*/
2751                                         else if(findedgelist(look,tagged[j]->v1)){   /*If our vert is connected to vert 1 */
2752                                                 while(nextpos < ect){ /*Find the first open spot in the search array */
2753                                                         if(taggedsrch[nextpos] == NULL){
2754                                                                 taggedsrch[nextpos] = tagged[j]; /*put tagged[j] in it*/
2755                                                                 addededge = 1;
2756                                                                 break;
2757                                                         }
2758                                                         else 
2759                                                                 nextpos++;
2760                                                 }
2761                                         }
2762
2763                                         if(addededge)
2764                                         {
2765                                                 break;
2766                                         }                                       
2767                                 }/* End Outer For (j)*/
2768                                 i++;
2769                         } /* End while(j<ect)*/
2770                         timesthrough++;
2771                 } /*end while timesthrough */
2772                 percentcut = 0.50;
2773                 searching = 1;
2774                 cut   = 1;
2775                 smooth = 0;
2776                 close = NULL;
2777
2778
2779                 /* Count the Number of Faces in the selected loop*/
2780                 percentfaces = 0;
2781                 for(evl= em->faces.first; evl ;evl=evl->next){
2782                         if(evl->f & 8)
2783                          {
2784                                 percentfaces++; 
2785                          }
2786                 }
2787                         
2788                 /* create a dynamic array for those face pointers */
2789                 percentfacesloop = MEM_mallocN(percentfaces*sizeof(EditVlak*), "percentage");
2790
2791                 /* put those faces in the array */
2792                 i=0;
2793                 for(evl= em->faces.first; evl ;evl=evl->next){
2794                          if(evl->f & 8)
2795                          {
2796                                 percentfacesloop[i] = evl;      
2797                                 i++;
2798                          }
2799                 }
2800
2801                 while(searching){
2802                         
2803                         /* For the % calculation */
2804                         short mval[2];                  
2805                         float labda, rc[2], len, slen=0.0;
2806                         float v1[2], v2[2], v3[2];
2807
2808                         /*------------- Percent Cut Preview Lines--------------- */
2809                         scrarea_do_windraw(curarea);                    
2810                         persp(PERSP_VIEW);
2811                         glPushMatrix();
2812                         mymultmatrix(G.obedit->obmat);
2813                         glColor3ub(0, 255, 255);
2814                                 
2815                         /*Put the preview lines where they should be for the percentage selected.*/
2816
2817                         for(i=0;i<percentfaces;i++){
2818                                 evl = percentfacesloop[i];
2819                                 for(eed = em->edges.first; eed; eed=eed->next){
2820                                         if(eed->f & 64){        /* color the starting edge */                   
2821                                                 glBegin(GL_LINES);
2822                                                                                                 
2823                                                 glColor3ub(200, 255, 200);
2824                                                 glVertex3fv(eed->v1->co);                                       
2825                                                 glVertex3fv(eed->v2->co);
2826                                                 
2827                                                 glEnd();
2828
2829                                                 glPointSize(5);
2830                                                 glBegin(GL_POINTS);
2831                                                 glColor3ub(255,0,255);
2832                                                 
2833                                                 if(eed->f & 32)
2834                                                         glVertex3fv(eed->v2->co);                       
2835                                                 else
2836                                                         glVertex3fv(eed->v1->co);
2837                                                 glEnd();
2838
2839
2840                                                 /*Get Starting Edge Length*/
2841                                                 slen = sqrt((eed->v1->co[0]-eed->v2->co[0])*(eed->v1->co[0]-eed->v2->co[0])+
2842                                                                         (eed->v1->co[1]-eed->v2->co[1])*(eed->v1->co[1]-eed->v2->co[1])+
2843                                                                         (eed->v1->co[2]-eed->v2->co[2])*(eed->v1->co[2]-eed->v2->co[2]));
2844                                         }
2845                                 }
2846                                 
2847                                 if(!inset){
2848                                         glColor3ub(0,255,255);
2849                                         if(evl->f & 8)
2850                                         {
2851                                                 float cen[2][3];
2852                                                 int a=0;                                        
2853                                                 
2854                                                 evl->v1->f &= ~8;
2855                                                 evl->v2->f &= ~8;
2856                                                 evl->v3->f &= ~8;
2857                                                 if(evl->v4)evl->v4->f &= ~8;
2858                                                 
2859                                                 if(evl->e1->f & 8){
2860                                                         float pct;
2861                                                         if(evl->e1->f & 32)
2862                                                                 pct = 1-percentcut;
2863                                                         else
2864                                                                 pct = percentcut;
2865                                                         cen[a][0]= evl->e1->v1->co[0] - ((evl->e1->v1->co[0] - evl->e1->v2->co[0]) * (pct));
2866                                                         cen[a][1]= evl->e1->v1->co[1] - ((evl->e1->v1->co[1] - evl->e1->v2->co[1]) * (pct));
2867                                                         cen[a][2]= evl->e1->v1->co[2] - ((evl->e1->v1->co[2] - evl->e1->v2->co[2]) * (pct));
2868                                                         evl->e1->v1->f |= 8;
2869                                                         evl->e1->v2->f |= 8;
2870                                                         a++;
2871                                                 }
2872                                                 if((evl->e2->f & 8) && a!=2)
2873                                                 {
2874                                                         float pct;
2875                                                         if(evl->e2->f & 32)
2876                                                                 pct = 1-percentcut;
2877                                                         else
2878                                                                 pct = percentcut;
2879                                                         cen[a][0]= evl->e2->v1->co[0] - ((evl->e2->v1->co[0] - evl->e2->v2->co[0]) * (pct));
2880                                                         cen[a][1]= evl->e2->v1->co