4dc64b0f9334bc19953801e1976107d30447a601
[blender-staging.git] / source / blender / src / editmesh_add.c
1 /**
2  * $Id: 
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2004 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "MEM_guardedalloc.h"
40
41
42 #include "DNA_mesh_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_userdef_types.h"
49 #include "DNA_view3d_types.h"
50
51 #include "BLI_blenlib.h"
52 #include "BLI_arithb.h"
53 #include "BLI_editVert.h"
54
55 #include "BKE_depsgraph.h"
56 #include "BKE_global.h"
57 #include "BKE_library.h"
58 #include "BKE_mesh.h"
59 #include "BKE_object.h"
60 #include "BKE_utildefines.h"
61
62 #ifdef WITH_VERSE
63 #include "BKE_verse.h"
64 #endif
65
66
67 #include "BIF_editmesh.h"
68 #include "BIF_graphics.h"
69 #include "BIF_interface.h"
70 #include "BIF_mywindow.h"
71 #include "BIF_retopo.h"
72 #include "BIF_screen.h"
73 #include "BIF_space.h"
74 #include "BIF_toolbox.h"
75 #include "BIF_transform.h"
76
77 #ifdef WITH_VERSE
78 #include "BIF_verse.h"
79 #endif
80
81 #include "BDR_editobject.h" 
82
83 #include "BSE_view.h"
84 #include "BSE_edit.h"
85
86 #include "blendef.h"
87 #include "multires.h"
88 #include "mydevice.h"
89
90 #include "editmesh.h"
91
92 /* bpymenu */
93 #include "BPY_extern.h"
94 #include "BPY_menus.h"
95
96 static float icovert[12][3] = {
97         {0,0,-200}, 
98         {144.72, -105.144,-89.443},
99         {-55.277, -170.128,-89.443}, 
100         {-178.885,0,-89.443},
101         {-55.277,170.128,-89.443}, 
102         {144.72,105.144,-89.443},
103         {55.277,-170.128,89.443},
104         {-144.72,-105.144,89.443},
105         {-144.72,105.144,89.443},
106         {55.277,170.128,89.443},
107         {178.885,0,89.443},
108         {0,0,200}
109 };
110 static short icoface[20][3] = {
111         {1,0,2},
112         {1,0,5},
113         {2,0,3},
114         {3,0,4},
115         {4,0,5},
116         {1,5,10},
117         {2,1,6},
118         {3,2,7},
119         {4,3,8},
120         {5,4,9},
121         {10,1,6},
122         {6,2,7},
123         {7,3,8},
124         {8,4,9},
125         {9,5,10},
126         {6,10,11},
127         {7,6,11},
128         {8,7,11},
129         {9,8,11},
130         {10,9,11}
131 };
132
133 static void get_view_aligned_coordinate(float *fp)
134 {
135         float dvec[3];
136         short mx, my, mval[2];
137         
138         getmouseco_areawin(mval);
139         mx= mval[0];
140         my= mval[1];
141         
142         project_short_noclip(fp, mval);
143         
144         initgrabz(fp[0], fp[1], fp[2]);
145         
146         if(mval[0]!=IS_CLIPPED) {
147                 window_to_3d(dvec, mval[0]-mx, mval[1]-my);
148                 VecSubf(fp, fp, dvec);
149         }
150 }
151
152 void add_click_mesh(void)
153 {
154         EditMesh *em = G.editMesh;
155         EditVert *eve, *v1;
156         float min[3], max[3];
157         int done= 0;
158         
159         TEST_EDITMESH
160         if(multires_test()) return;
161         
162         INIT_MINMAX(min, max);
163         
164         for(v1= em->verts.first;v1; v1=v1->next) {
165                 if(v1->f & SELECT) {
166                         DO_MINMAX(v1->co, min, max);
167                         done= 1;
168                 }
169         }
170
171         /* call extrude? */
172         if(done) {
173                 EditEdge *eed;
174                 float vec[3], cent[3], mat[3][3];
175                 float nor[3]= {0.0, 0.0, 0.0};
176                 
177                 /* check for edges that are half selected, use for rotation */
178                 done= 0;
179                 for(eed= em->edges.first; eed; eed= eed->next) {
180                         if( (eed->v1->f & SELECT)+(eed->v2->f & SELECT) == SELECT ) {
181                                 if(eed->v1->f & SELECT) VecSubf(vec, eed->v1->co, eed->v2->co);
182                                 else VecSubf(vec, eed->v2->co, eed->v1->co);
183                                 VecAddf(nor, nor, vec);
184                                 done= 1;
185                         }
186                 }
187                 if(done) Normalize(nor);
188                 
189                 /* center */
190                 VecAddf(cent, min, max);
191                 VecMulf(cent, 0.5f);
192                 VECCOPY(min, cent);
193                 
194                 Mat4MulVecfl(G.obedit->obmat, min);     // view space
195                 get_view_aligned_coordinate(min);
196                 Mat4Invert(G.obedit->imat, G.obedit->obmat); 
197                 Mat4MulVecfl(G.obedit->imat, min); // back in object space
198                 
199                 VecSubf(min, min, cent);
200                 
201                 /* calculate rotation */
202                 Mat3One(mat);
203                 if(done) {
204                         float dot;
205                         
206                         VECCOPY(vec, min);
207                         Normalize(vec);
208                         dot= INPR(vec, nor);
209
210                         if( fabs(dot)<0.999) {
211                                 float cross[3], si, q1[4];
212                                 
213                                 Crossf(cross, nor, vec);
214                                 Normalize(cross);
215                                 dot= 0.5f*saacos(dot);
216                                 si= (float)sin(dot);
217                                 q1[0]= (float)cos(dot);
218                                 q1[1]= cross[0]*si;
219                                 q1[2]= cross[1]*si;
220                                 q1[3]= cross[2]*si;
221                                 
222                                 QuatToMat3(q1, mat);
223                         }
224                 }
225                 
226                 extrudeflag(SELECT, nor);
227                 rotateflag(SELECT, cent, mat);
228                 translateflag(SELECT, min);
229                 
230                 recalc_editnormals();
231         }
232         else {
233                 float mat[3][3],imat[3][3];
234                 float *curs= give_cursor();
235                 
236                 eve= addvertlist(0, NULL);
237
238                 Mat3CpyMat4(mat, G.obedit->obmat);
239                 Mat3Inv(imat, mat);
240                 
241                 VECCOPY(eve->co, curs);
242                 VecSubf(eve->co, eve->co, G.obedit->obmat[3]);
243
244                 Mat3MulVecfl(imat, eve->co);
245                 
246                 eve->f= SELECT;
247         }
248         
249         retopo_do_all();
250         
251         countall();
252
253 #ifdef WITH_VERSE
254         if(G.editMesh->vnode) {
255                 sync_all_verseverts_with_editverts((VNode*)G.editMesh->vnode);
256         }
257 #endif
258
259         BIF_undo_push("Add vertex/edge/face");
260         allqueue(REDRAWVIEW3D, 0);
261         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
262         
263         while(get_mbut()&R_MOUSE);
264
265 }
266
267 /* selected faces get hidden edges */
268 static void make_fgon(int make)
269 {
270         EditMesh *em = G.editMesh;
271         EditFace *efa;
272         EditEdge *eed;
273         EditVert *eve;
274         float *nor=NULL;        // reference
275         int done=0;
276         
277         if(!make) {
278                 for(efa= em->faces.first; efa; efa= efa->next) {
279                         if(efa->f & SELECT) {
280                                 efa->fgonf= 0;
281                                 efa->e1->h &= ~EM_FGON;
282                                 efa->e2->h &= ~EM_FGON;
283                                 efa->e3->h &= ~EM_FGON;
284                                 if(efa->e4) efa->e4->h &= ~EM_FGON;
285                         }
286                 }
287                 allqueue(REDRAWVIEW3D, 0);
288                 EM_fgon_flags();        // redo flags and indices for fgons
289                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
290                 BIF_undo_push("Clear FGon");
291                 return;
292         }
293
294         /* tagging edges. rule is:
295            - edge used by exactly 2 selected faces
296            - no vertices allowed with only tagged edges (return)
297            - face normals are allowed to difffer
298          
299         */
300         for(eed= em->edges.first; eed; eed= eed->next) {
301                 eed->f1= 0;     // amount of selected
302                 eed->f2= 0; // amount of unselected
303         }
304         
305         for(efa= em->faces.first; efa; efa= efa->next) {
306                 if(efa->f & SELECT) {
307                         if(nor==NULL) nor= efa->n;
308                         if(efa->e1->f1 < 3) efa->e1->f1++;
309                         if(efa->e2->f1 < 3) efa->e2->f1++;
310                         if(efa->e3->f1 < 3) efa->e3->f1++;
311                         if(efa->e4 && efa->e4->f1 < 3) efa->e4->f1++;
312                 }
313                 else {
314                         if(efa->e1->f2 < 3) efa->e1->f2++;
315                         if(efa->e2->f2 < 3) efa->e2->f2++;
316                         if(efa->e3->f2 < 3) efa->e3->f2++;
317                         if(efa->e4 && efa->e4->f2 < 3) efa->e4->f2++;
318                 }
319         }
320         // now eed->f1 becomes tagged edge
321         for(eed= em->edges.first; eed; eed= eed->next) {
322                 if(eed->f1==2 && eed->f2==0) eed->f1= 1;
323                 else eed->f1= 0;
324         }
325         
326         // no vertices allowed with only tagged edges
327         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
328         for(eed= em->edges.first; eed; eed= eed->next) {
329                 if(eed->f1) {
330                         eed->v1->f1 |= 1;
331                         eed->v2->f1 |= 1;
332                 }
333                 else {
334                         eed->v1->f1 |= 2;
335                         eed->v2->f1 |= 2;
336                 }
337         }
338         for(eve= em->verts.first; eve; eve= eve->next) {
339                 if(eve->f1==1) break;
340         }
341         if(eve) {
342                 error("Cannot make polygon with interior vertices");
343                 return;
344         }
345         
346         // check for faces
347         if(nor==NULL) {
348                 error("No faces selected to make FGon");
349                 return;
350         }
351
352         // and there we go
353         for(eed= em->edges.first; eed; eed= eed->next) {
354                 if(eed->f1) {
355                         eed->h |= EM_FGON;
356                         done= 1;
357                 }
358         }
359         
360         if(done==0) {
361                 error("Didn't find FGon to create");
362         }
363         else {
364                 EM_fgon_flags();        // redo flags and indices for fgons
365
366                 allqueue(REDRAWVIEW3D, 0);
367                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
368                 BIF_undo_push("Make FGon");
369         }
370 }
371
372 /* precondition; 4 vertices selected, check for 4 edges and create face */
373 static EditFace *addface_from_edges(void)
374 {
375         EditMesh *em = G.editMesh;
376         EditEdge *eed, *eedar[4]={NULL, NULL, NULL, NULL};
377         EditVert *v1=NULL, *v2=NULL, *v3=NULL, *v4=NULL;
378         int a;
379         
380         /* find the 4 edges */
381         for(eed= em->edges.first; eed; eed= eed->next) {
382                 if( (eed->f & SELECT) || (eed->v1->f & eed->v2->f & SELECT) ) {
383                         if(eedar[0]==NULL) eedar[0]= eed;
384                         else if(eedar[1]==NULL) eedar[1]= eed;
385                         else if(eedar[2]==NULL) eedar[2]= eed;
386                         else eedar[3]= eed;
387                         
388                 }
389         }
390         
391         
392         if(eedar[3]) {
393                 /* first 2 points */
394                 v1= eedar[0]->v1;
395                 v2= eedar[0]->v2;
396                 
397                 /* find the 2 edges connected to first edge */
398                 for(a=1; a<4; a++) {
399                         if( eedar[a]->v1 == v2) v3= eedar[a]->v2;
400                         else if(eedar[a]->v2 == v2) v3= eedar[a]->v1;
401                         else if( eedar[a]->v1 == v1) v4= eedar[a]->v2;
402                         else if(eedar[a]->v2 == v1) v4= eedar[a]->v1;
403                 }
404                 
405                 /* verify if last edge exists */
406                 if(v3 && v4) {
407                         for(a=1; a<4; a++) {
408                                 if( eedar[a]->v1==v3 && eedar[a]->v2==v4) break;
409                                 if( eedar[a]->v2==v3 && eedar[a]->v1==v4) break;
410                         }
411                         if(a!=4) {
412                                 return addfacelist(v1, v2, v3, v4, NULL, NULL);
413                         }
414                 }
415         }
416         return NULL;
417 }
418
419 /* this also allows to prevent triangles being made in quads */
420 static int compareface_overlaps(EditFace *vl1, EditFace *vl2)
421 {
422         EditVert *v1, *v2, *v3, *v4;
423         int equal= 0;
424         
425         v1= vl2->v1;
426         v2= vl2->v2;
427         v3= vl2->v3;
428         v4= vl2->v4;
429         
430         if(vl1==vl2) return 0;
431         
432         if(v4==NULL && vl1->v4==NULL) {
433                 if(vl1->v1==v1 || vl1->v2==v1 || vl1->v3==v1) equal++;
434                 if(vl1->v1==v2 || vl1->v2==v2 || vl1->v3==v2) equal++;
435                 if(vl1->v1==v3 || vl1->v2==v3 || vl1->v3==v3) equal++;
436         }
437         else {
438                 if(vl1->v1==v1 || vl1->v2==v1 || vl1->v3==v1 || vl1->v4==v1) equal++;
439                 if(vl1->v1==v2 || vl1->v2==v2 || vl1->v3==v2 || vl1->v4==v2) equal++;
440                 if(vl1->v1==v3 || vl1->v2==v3 || vl1->v3==v3 || vl1->v4==v3) equal++;
441                 if(vl1->v1==v4 || vl1->v2==v4 || vl1->v3==v4 || vl1->v4==v4) equal++;
442         }
443
444         if(v4 && vl1->v4) {
445                 if(equal==4) return 1;
446         }
447         else 
448                 if(equal>=3) return 1;
449         
450         return 0;
451 }
452
453 /* checks for existance, and for tria overlapping inside quad */
454 static EditFace *exist_face_overlaps(EditVert *v1, EditVert *v2, EditVert *v3, EditVert *v4)
455 {
456         EditMesh *em = G.editMesh;
457         EditFace *efa, efatest;
458         
459         efatest.v1= v1;
460         efatest.v2= v2;
461         efatest.v3= v3;
462         efatest.v4= v4;
463         
464         efa= em->faces.first;
465         while(efa) {
466                 if(compareface_overlaps(&efatest, efa)) return efa;
467                 efa= efa->next;
468         }
469         return NULL;
470 }
471
472 /* will be new face smooth or solid? depends on smoothness of face neighbours
473  * of new face, if function return 1, then new face will be smooth, when functio
474  * will return zero, then new face will be solid */
475 static void fix_new_face(EditFace *eface)
476 {
477         struct EditMesh *em = G.editMesh;
478         struct EditFace *efa;
479         struct EditEdge *eed=NULL;
480         struct EditVert *v1 = eface->v1, *v2 = eface->v2, *v3 = eface->v3, *v4 = eface->v4;
481         struct EditVert *ev1=NULL, *ev2=NULL;
482         short smooth=0; /* "total smoothnes" of faces in neighbourhood */
483         short coef;     /* "weight" of smoothness */
484         short count=0;  /* number of edges with same direction as eface */
485         short vi00=0, vi01=0, vi10=0, vi11=0; /* vertex indexes */
486
487         efa = em->faces.first;
488
489         while(efa) {
490
491                 if(efa==eface) {
492                         efa = efa->next;
493                         continue;
494                 }
495
496                 coef = 0;
497                 ev1 = ev2 = NULL;
498                 eed = NULL;
499
500                 if(efa->v1==v1 || efa->v2==v1 || efa->v3==v1 || efa->v4==v1) {
501                         ev1 = v1;
502                         coef++;
503                 }
504                 if(efa->v1==v2 || efa->v2==v2 || efa->v3==v2 || efa->v4==v2) {
505                         if(ev1) ev2 = v2;
506                         else ev1 = v2;
507                         coef++;
508                 }
509                 if(efa->v1==v3 || efa->v2==v3 || efa->v3==v3 || efa->v4==v3) {
510                         if(coef<2) {
511                                 if(ev1) ev2 = v3;
512                                 else ev1 = v3;
513                         }
514                         coef++;
515                 }
516                 if((v4) && (efa->v1==v4 || efa->v2==v4 || efa->v3==v4 || efa->v4==v4)) {
517                         if(ev1 && coef<2) ev2 = v4;
518                         coef++;
519                 }
520
521                 /* "democracy" of smoothness */
522                 if(efa->flag & ME_SMOOTH)
523                         smooth += coef;
524                 else
525                         smooth -= coef;
526
527                 /* try to find edge using vertexes ev1 and ev2 */
528                 if((ev1) && (ev2) && (ev1!=ev2)) eed = findedgelist(ev1, ev2);
529
530                 /* has bordering edge of efa same direction as edge of eface ? */
531                 if(eed) {
532                         if(eed->v1==v1) vi00 = 1;
533                         else if(eed->v1==v2) vi00 = 2;
534                         else if(eed->v1==v3) vi00 = 3;
535                         else if(v4 && eed->v1==v4) vi00 = 4;
536
537                         if(eed->v2==v1) vi01 = 1;
538                         else if(eed->v2==v2) vi01 = 2;
539                         else if(eed->v2==v3) vi01 = 3;
540                         else if(v4 && eed->v2==v4) vi01 = 4;
541
542                         if(v4) {
543                                 if(vi01==1 && vi00==4) vi00 = 0;
544                                 if(vi01==4 && vi00==1) vi01 = 0;
545                         }
546                         else {
547                                 if(vi01==1 && vi00==3) vi00 = 0;
548                                 if(vi01==3 && vi00==1) vi01 = 0;
549                         }
550
551                         if(eed->v1==efa->v1) vi10 = 1;
552                         else if(eed->v1==efa->v2) vi10 = 2;
553                         else if(eed->v1==efa->v3) vi10 = 3;
554                         else if(efa->v4 && eed->v1==efa->v4) vi10 = 4;
555
556                         if(eed->v2==efa->v1) vi11 = 1;
557                         else if(eed->v2==efa->v2) vi11 = 2;
558                         else if(eed->v2==efa->v3) vi11 = 3;
559                         else if(efa->v4 && eed->v2==efa->v4) vi11 = 4;
560
561                         if(efa->v4) {
562                                 if(vi11==1 && vi10==4) vi10 = 0;
563                                 if(vi11==4 && vi10==1) vi11 = 0;
564                         }
565                         else {
566                                 if(vi11==1 && vi10==3) vi10 = 0;
567                                 if(vi11==3 && vi10==1) vi11 = 0;
568                         }
569
570                         if(((vi00>vi01) && (vi10>vi11)) ||
571                                 ((vi00<vi01) && (vi10<vi11)))
572                                 count++;
573                         else
574                                 count--;
575                 }
576
577                 efa = efa->next;
578         }
579
580         /* set up smoothness according voting of face in neighbourhood */
581         if(smooth >= 0)
582                 eface->flag |= ME_SMOOTH;
583         else
584                 eface->flag &= ~ME_SMOOTH;
585
586         /* flip face, when too much "face normals" in neighbourhood is different */
587         if(count > 0) {
588                 flipface(eface);
589 #ifdef WITH_VERSE
590                 if(eface->vface) {
591                         struct VNode *vnode;
592                         struct VLayer *vlayer;
593                         vnode = (VNode*)((Mesh*)G.obedit->data)->vnode;
594                         vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
595                         add_item_to_send_queue(&(vlayer->queue), (void*)eface->vface, VERSE_FACE);
596                 }
597 #endif
598         }
599 }
600
601 void addfaces_from_edgenet(void)
602 {
603         EditVert *eve1, *eve2, *eve3, *eve4;
604         EditMesh *em= G.editMesh;
605         
606         for(eve1= em->verts.first; eve1; eve1= eve1->next) {
607                 for(eve2= em->verts.first; (eve1->f & 1) && eve2; eve2= eve2->next) {
608                         if(findedgelist(eve1,eve2)) {
609                                 for(eve3= em->verts.first; (eve2->f & 1) && eve3; eve3= eve3->next) {
610                                         if((eve2!=eve3 && (eve3->f & 1) && findedgelist(eve1,eve3))) {
611                                                 EditEdge *sh_edge= NULL;
612                                                 EditVert *sh_vert= NULL;
613                                                 
614                                                 sh_edge= findedgelist(eve2,eve3);
615                                                 
616                                                 if(sh_edge) { /* Add a triangle */
617                                                         if(!exist_face_overlaps(eve1,eve2,eve3,NULL))
618                                                                 fix_new_face(addfacelist(eve1,eve2,eve3,NULL,NULL,NULL));
619                                                 }
620                                                 else { /* Check for a shared vertex */
621                                                         for(eve4= em->verts.first; eve4; eve4= eve4->next) {
622                                                                 if(eve4!=eve1 && eve4!=eve2 && eve4!=eve3 && (eve4->f & 1) &&
623                                                                    !findedgelist(eve1,eve4) && findedgelist(eve2,eve4) &&
624                                                                    findedgelist(eve3,eve4)) {
625                                                                         sh_vert= eve4;
626                                                                         break;
627                                                                 }
628                                                         }
629                                                         
630                                                         if(sh_vert) {
631                                                                 if(sh_vert) {
632                                                                         if(!exist_face_overlaps(eve1,eve2,eve4,eve3))
633                                                                                 fix_new_face(addfacelist(eve1,eve2,eve4,eve3,NULL,NULL));
634                                                                 }
635                                                         }
636                                                 }
637                                         }
638                                 }
639                         }
640                 }
641         }
642
643         countall();
644
645         EM_select_flush();
646         
647         BIF_undo_push("Add faces");
648         allqueue(REDRAWVIEW3D, 0);
649         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
650 }
651
652 void addedgeface_mesh(void)
653 {
654         EditMesh *em = G.editMesh;
655         EditVert *eve, *neweve[4];
656         EditEdge *eed;
657         EditFace *efa;
658         short amount=0;
659
660         if( (G.vd->lay & G.obedit->lay)==0 ) return;
661         if(multires_test()) return;
662
663         /* how many selected ? */
664         if(G.scene->selectmode & SCE_SELECT_EDGE) {
665                 /* in edge mode finding selected vertices means flushing down edge codes... */
666                 /* can't make face with only edge selection info... */
667                 EM_selectmode_set();
668         }
669         
670         for(eve= em->verts.first; eve; eve= eve->next) {
671                 if(eve->f & SELECT) {
672                         amount++;
673                         if(amount>4) break;                     
674                         neweve[amount-1]= eve;
675                 }
676         }
677
678         if(amount==2) {
679                 eed= addedgelist(neweve[0], neweve[1], NULL);
680                 EM_select_edge(eed, 1);
681                 BIF_undo_push("Add edge");
682                 allqueue(REDRAWVIEW3D, 0);
683                 countall();
684                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
685                 return;
686         }
687         else if(amount > 4) {
688                 
689                 /* Python Menu */
690                 BPyMenu *pym;
691                 char menu_number[3];
692                 int i=0, has_pymenu=0, ret;
693                 
694                 /* facemenu, will add python items */
695                 char facemenu[4096]= "Make Faces%t|Auto%x1|Make FGon%x2|Clear FGon%x3";
696                 
697                 /* note that we account for the 10 previous entries with i+4: */
698                 for (pym = BPyMenuTable[PYMENU_MESHFACEKEY]; pym; pym = pym->next, i++) {
699                         
700                         if (!has_pymenu) {
701                                 strcat(facemenu, "|%l");
702                                 has_pymenu = 1;
703                         }
704                         
705                         strcat(facemenu, "|");
706                         strcat(facemenu, pym->name);
707                         strcat(facemenu, " %x");
708                         sprintf(menu_number, "%d", i+4);
709                         strcat(facemenu, menu_number);
710                 }
711                 
712                 ret= pupmenu(facemenu);
713                 
714                 if(ret==1) addfaces_from_edgenet();
715                 else if(ret==2) make_fgon(1);
716                 else if(ret==3) make_fgon(0);
717                 else if (ret >= 4) {
718                         BPY_menu_do_python(PYMENU_MESHFACEKEY, ret - 4);
719                         return;
720                 }
721                 return;
722         }
723         else if(amount<2) {
724                 error("Incorrect number of vertices to make edge/face");
725                 return;
726         }
727
728         efa= NULL; // check later
729
730         if(amount==3) {
731                 
732                 if(exist_face_overlaps(neweve[0], neweve[1], neweve[2], NULL)==0) {
733                         efa= addfacelist(neweve[0], neweve[1], neweve[2], 0, NULL, NULL);
734                         EM_select_face(efa, 1);
735                 }
736                 else error("The selected vertices already form a face");
737         }
738         else if(amount==4) {
739                 /* this test survives when theres 2 triangles */
740                 if(exist_face(neweve[0], neweve[1], neweve[2], neweve[3])==0) {
741                         int tria= 0;
742                         
743                         /* remove trias if they exist, 4 cases.... */
744                         if(exist_face(neweve[0], neweve[1], neweve[2], NULL)) tria++;
745                         if(exist_face(neweve[0], neweve[1], neweve[3], NULL)) tria++;
746                         if(exist_face(neweve[0], neweve[2], neweve[3], NULL)) tria++;
747                         if(exist_face(neweve[1], neweve[2], neweve[3], NULL)) tria++;
748                 
749                         if(tria==2) join_triangles();
750                         else if(exist_face_overlaps(neweve[0], neweve[1], neweve[2], neweve[3])==0) {
751                                  /* If there are 4 Verts, But more selected edges, we need to call addfaces_from_edgenet */
752                                         EditEdge *eedcheck;
753                                         int count;
754                                         count = 0;
755                                         for(eedcheck= em->edges.first; eedcheck; eedcheck= eedcheck->next) {
756                                                 if(eedcheck->f & SELECT) {
757                                                         count++;
758                                                 }
759                                         }       
760                                 
761                                 if(count++ > 4){
762                                         addfaces_from_edgenet();
763                                         return;
764                                 } else {
765                                 /* if 4 edges exist, we just create the face, convex or not */
766                                         efa= addface_from_edges();
767                                         if(efa==NULL) {
768                                                 
769                                                 /* the order of vertices can be anything, 6 cases to check */
770                                                 if( convex(neweve[0]->co, neweve[1]->co, neweve[2]->co, neweve[3]->co) ) {
771                                                         efa= addfacelist(neweve[0], neweve[1], neweve[2], neweve[3], NULL, NULL);
772                                                 }
773                                                 else if( convex(neweve[0]->co, neweve[2]->co, neweve[3]->co, neweve[1]->co) ) {
774                                                         efa= addfacelist(neweve[0], neweve[2], neweve[3], neweve[1], NULL, NULL);
775                                                 }
776                                                 else if( convex(neweve[0]->co, neweve[2]->co, neweve[1]->co, neweve[3]->co) ) {
777                                                         efa= addfacelist(neweve[0], neweve[2], neweve[1], neweve[3], NULL, NULL);
778                                                 }
779                                                 else if( convex(neweve[0]->co, neweve[1]->co, neweve[3]->co, neweve[2]->co) ) {
780                                                         efa= addfacelist(neweve[0], neweve[1], neweve[3], neweve[2], NULL, NULL);
781                                                 }
782                                                 else if( convex(neweve[0]->co, neweve[3]->co, neweve[2]->co, neweve[1]->co) ) {
783                                                         efa= addfacelist(neweve[0], neweve[3], neweve[2], neweve[1], NULL, NULL);
784                                                 }
785                                                 else if( convex(neweve[0]->co, neweve[3]->co, neweve[1]->co, neweve[2]->co) ) {
786                                                         efa= addfacelist(neweve[0], neweve[3], neweve[1], neweve[2], NULL, NULL);
787                                                 }
788                                                 else printf("cannot find nice quad from concave set of vertices\n");
789                                         }
790                                 }
791                         }
792                         else error("The selected vertices already form a face");
793                 }
794                 else error("The selected vertices already form a face");
795         }
796         
797         if(efa) {
798                 EM_select_face(efa, 1);
799
800                 fix_new_face(efa);
801                 
802                 recalc_editnormals();
803                 BIF_undo_push("Add face");
804         }
805         
806         countall();
807         allqueue(REDRAWVIEW3D, 0);
808         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
809 }
810
811
812 void adduplicate_mesh(void)
813 {
814
815         TEST_EDITMESH
816         if(multires_test()) return;
817
818         waitcursor(1);
819
820         adduplicateflag(SELECT);
821
822         waitcursor(0);
823         countall(); 
824
825                 /* We need to force immediate calculation here because 
826                 * transform may use derived objects (which are now stale).
827                 *
828                 * This shouldn't be necessary, derived queries should be
829                 * automatically building this data if invalid. Or something.
830                 */
831         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
832         object_handle_update(G.obedit);
833
834         BIF_TransformSetUndo("Add Duplicate");
835         initTransform(TFM_TRANSLATION, CTX_NO_PET);
836         Transform();
837 }
838
839 /* check whether an object to add mesh to exists, if not, create one
840 * returns 1 if new object created, else 0 */
841 int confirm_objectExists( Mesh **me, float mat[][3] )
842 {
843         int newob = 0;
844         
845         /* deselectall */
846         EM_clear_flag_all(SELECT);
847         
848         /* if no obedit: new object and enter editmode */
849         if(G.obedit==NULL) {
850                 /* add_object actually returns an object ! :-)
851                 But it also stores the added object struct in
852                 G.scene->basact->object (BASACT->object) */
853
854                 add_object_draw(OB_MESH);
855
856                 G.obedit= BASACT->object;
857                 
858                 where_is_object(G.obedit);
859                 
860                 make_editMesh();
861                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
862                 newob= 1;
863         }
864         *me = G.obedit->data;
865         
866         /* imat and center and size */
867         Mat3CpyMat4(mat, G.obedit->obmat);
868         
869         return newob;
870 }
871
872 // HACK: these can also be found in cmoview.tga.c, but are here so that they can be found by linker
873 // this hack is only used so that scons+mingw + split-sources hack works
874         // ------------------------------- start copied code
875 /* these are not the monkeys you are looking for */
876 int monkeyo= 4;
877 int monkeynv= 271;
878 int monkeynf= 250;
879 signed char monkeyv[271][3]= {
880 {-71,21,98},{-63,12,88},{-57,7,74},{-82,-3,79},{-82,4,92},
881 {-82,17,100},{-92,21,102},{-101,12,95},{-107,7,83},
882 {-117,31,84},{-109,31,95},{-96,31,102},{-92,42,102},
883 {-101,50,95},{-107,56,83},{-82,66,79},{-82,58,92},
884 {-82,46,100},{-71,42,98},{-63,50,88},{-57,56,74},
885 {-47,31,72},{-55,31,86},{-67,31,97},{-66,31,99},
886 {-70,43,100},{-82,48,103},{-93,43,105},{-98,31,105},
887 {-93,20,105},{-82,31,106},{-82,15,103},{-70,20,100},
888 {-127,55,95},{-127,45,105},{-127,-87,94},{-127,-41,100},
889 {-127,-24,102},{-127,-99,92},{-127,52,77},{-127,73,73},
890 {-127,115,-70},{-127,72,-109},{-127,9,-106},{-127,-49,-45},
891 {-101,-24,72},{-87,-56,73},{-82,-89,73},{-80,-114,68},
892 {-85,-121,67},{-104,-124,71},{-127,-126,74},{-71,-18,68},
893 {-46,-5,69},{-21,19,57},{-17,55,76},{-36,62,80},
894 {-64,77,88},{-86,97,94},{-107,92,97},{-119,63,96},
895 {-106,53,99},{-111,39,98},{-101,12,95},{-79,2,90},
896 {-64,8,86},{-47,24,83},{-45,38,83},{-50,48,85},
897 {-72,56,92},{-95,60,97},{-127,-98,94},{-113,-92,94},
898 {-112,-107,91},{-119,-113,89},{-127,-114,88},{-127,-25,96},
899 {-127,-18,95},{-114,-19,95},{-111,-29,96},{-116,-37,95},
900 {-76,-6,86},{-48,7,80},{-34,26,77},{-32,48,84},
901 {-39,53,93},{-71,70,102},{-87,82,107},{-101,79,109},
902 {-114,55,108},{-111,-13,104},{-100,-57,91},{-95,-90,88},
903 {-93,-105,85},{-97,-117,81},{-106,-119,81},{-127,-121,82},
904 {-127,6,93},{-127,27,98},{-85,61,95},{-106,18,96},
905 {-110,27,97},{-112,-88,94},{-117,-57,96},{-127,-57,96},
906 {-127,-42,95},{-115,-35,100},{-110,-29,102},{-113,-17,100},
907 {-122,-16,100},{-127,-26,106},{-121,-19,104},{-115,-20,104},
908 {-113,-29,106},{-117,-32,103},{-127,-37,103},{-94,-40,71},
909 {-106,-31,91},{-104,-40,91},{-97,-32,71},{-127,-112,88},
910 {-121,-111,88},{-115,-105,91},{-115,-95,93},{-127,-100,84},
911 {-115,-96,85},{-115,-104,82},{-121,-109,81},{-127,-110,81},
912 {-105,28,100},{-103,20,99},{-84,55,97},{-92,54,99},
913 {-73,51,99},{-55,45,89},{-52,37,88},{-53,25,87},
914 {-66,13,92},{-79,8,95},{-98,14,100},{-104,38,100},
915 {-100,48,100},{-97,46,97},{-102,38,97},{-96,16,97},
916 {-79,11,93},{-68,15,90},{-57,27,86},{-56,36,86},
917 {-59,43,87},{-74,50,96},{-91,51,98},{-84,52,96},
918 {-101,22,96},{-102,29,96},{-113,59,78},{-102,85,79},
919 {-84,88,76},{-65,71,71},{-40,58,63},{-25,52,59},
920 {-28,21,48},{-50,0,53},{-71,-12,60},{-127,115,37},
921 {-127,126,-10},{-127,-25,-86},{-127,-59,24},{-127,-125,59},
922 {-127,-103,44},{-127,-73,41},{-127,-62,36},{-18,30,7},
923 {-17,41,-6},{-28,34,-56},{-68,56,-90},{-33,-6,9},
924 {-51,-16,-21},{-45,-1,-55},{-84,7,-85},{-97,-45,52},
925 {-104,-53,33},{-90,-91,49},{-95,-64,50},{-85,-117,51},
926 {-109,-97,47},{-111,-69,46},{-106,-121,56},{-99,-36,55},
927 {-100,-29,60},{-101,-22,64},{-100,-50,21},{-89,-40,-34},
928 {-83,-19,-69},{-69,111,-49},{-69,119,-9},{-69,109,30},
929 {-68,67,55},{-34,52,43},{-46,58,36},{-45,90,7},
930 {-25,72,16},{-25,79,-15},{-45,96,-25},{-45,87,-57},
931 {-25,69,-46},{-48,42,-75},{-65,3,-70},{-22,42,-26},
932 {-75,-22,19},{-72,-25,-27},{-13,52,-30},{-28,-18,-16},
933 {6,-13,-42},{37,7,-55},{46,41,-54},{31,65,-54},
934 {4,61,-40},{3,53,-37},{25,56,-50},{35,37,-52},
935 {28,10,-52},{5,-5,-39},{-21,-9,-17},{-9,46,-28},
936 {-6,39,-37},{-14,-3,-27},{6,0,-47},{25,12,-57},
937 {31,32,-57},{23,46,-56},{4,44,-46},{-19,37,-27},
938 {-20,22,-35},{-30,12,-35},{-22,11,-35},{-19,2,-35},
939 {-23,-2,-35},{-34,0,-9},{-35,-3,-22},{-35,5,-24},
940 {-25,26,-27},{-13,31,-34},{-13,30,-41},{-23,-2,-41},
941 {-18,2,-41},{-21,10,-41},{-29,12,-41},{-19,22,-41},
942 {6,42,-53},{25,44,-62},{34,31,-63},{28,11,-62},
943 {7,0,-54},{-14,-2,-34},{-5,37,-44},{-13,14,-42},
944 {-7,8,-43},{1,16,-47},{-4,22,-45},{3,30,-48},
945 {8,24,-49},{15,27,-50},{12,35,-50},{4,56,-62},
946 {33,60,-70},{48,38,-64},{41,7,-68},{6,-11,-63},
947 {-26,-16,-42},{-17,49,-49},
948 };
949
950 signed char monkeyf[250][4]= {
951 {27,4,5,26}, {25,4,5,24}, {3,6,5,4}, {1,6,5,2}, {5,6,7,4}, 
952 {3,6,7,2}, {5,8,7,6}, {3,8,7,4}, {7,8,9,6}, 
953 {5,8,9,4}, {7,10,9,8}, {5,10,9,6}, {9,10,11,8}, 
954 {7,10,11,6}, {9,12,11,10}, {7,12,11,8}, {11,6,13,12}, 
955 {5,4,13,12}, {3,-2,13,12}, {-3,-4,13,12}, {-5,-10,13,12}, 
956 {-11,-12,14,12}, {-13,-18,14,13}, {-19,4,5,13}, {10,12,4,4}, 
957 {10,11,9,9}, {8,7,9,9}, {7,5,6,6}, {6,3,4,4}, 
958 {5,1,2,2}, {4,-1,0,0}, {3,-3,-2,-2}, {22,67,68,23}, 
959 {20,65,66,21}, {18,63,64,19}, {16,61,62,17}, {14,59,60,15}, 
960 {12,19,48,57}, {18,19,48,47}, {18,19,48,47}, {18,19,48,47}, 
961 {18,19,48,47}, {18,19,48,47}, {18,19,48,47}, {18,19,48,47}, 
962 {18,19,48,47}, {18,-9,-8,47}, {18,27,45,46}, {26,55,43,44}, 
963 {24,41,42,54}, {22,39,40,23}, {20,37,38,21}, {18,35,36,19}, 
964 {16,33,34,17}, {14,31,32,15}, {12,39,30,13}, {11,48,45,38}, 
965 {8,36,-19,9}, {8,-20,44,47}, {42,45,46,43}, {18,19,40,39}, 
966 {16,17,38,37}, {14,15,36,35}, {32,44,43,33}, {12,33,32,42}, 
967 {19,44,43,42}, {40,41,42,-27}, {8,9,39,-28}, {15,43,42,16}, 
968 {13,43,42,14}, {11,43,42,12}, {9,-30,42,10}, {37,12,38,-32}, 
969 {-33,37,45,46}, {-33,40,41,39}, {38,40,41,37}, {36,40,41,35}, 
970 {34,40,41,33}, {36,39,38,37}, {35,40,39,38}, {1,2,14,21}, 
971 {1,2,40,13}, {1,2,40,39}, {1,24,12,39}, {-34,36,38,11}, 
972 {35,38,36,37}, {-37,8,35,37}, {-11,-12,-45,40}, {-11,-12,39,38}, 
973 {-11,-12,37,36}, {-11,-12,35,34}, {33,34,40,41}, {33,34,38,39}, 
974 {33,34,36,37}, {33,-52,34,35}, {33,37,36,34}, {33,35,34,34}, 
975 {8,7,37,36}, {-32,7,35,46}, {-34,-33,45,46}, {4,-33,43,34}, 
976 {-34,-33,41,42}, {-34,-33,39,40}, {-34,-33,37,38}, {-34,-33,35,36}, 
977 {-34,-33,33,34}, {-34,-33,31,32}, {-34,-4,28,30}, {-5,-34,28,27}, 
978 {-35,-44,36,27}, {26,35,36,45}, {24,25,44,45}, {25,23,44,42}, 
979 {25,24,41,40}, {25,24,39,38}, {25,24,37,36}, {25,24,35,34}, 
980 {25,24,33,32}, {25,24,31,30}, {15,24,29,38}, {25,24,27,26}, 
981 {23,12,37,26}, {11,12,35,36}, {-86,-59,36,-80}, {-60,-61,36,35}, 
982 {-62,-63,36,35}, {-64,-65,36,35}, {-66,-67,36,35}, {-68,-69,36,35}, 
983 {-70,-71,36,35}, {-72,-73,36,35}, {-74,-75,36,35}, {42,43,53,58}, 
984 {40,41,57,56}, {38,39,55,57}, {-81,-80,37,56}, {-83,-82,55,52}, 
985 {-85,-84,51,49}, {-87,-86,48,49}, {47,50,51,48}, {46,48,51,49}, 
986 {43,46,49,44}, {-92,-91,45,42}, {-23,49,50,-20}, {-94,40,48,-24}, 
987 {-96,-22,48,49}, {-97,48,21,-90}, {-100,36,50,23}, {22,49,48,-100}, 
988 {-101,47,46,22}, {21,45,35,25}, {33,34,44,41}, {13,14,28,24}, 
989 {-107,26,30,-106}, {14,46,45,15}, {14,44,43,-110}, {-111,42,23,-110}, 
990 {6,7,45,46}, {45,44,47,46}, {45,46,47,48}, {47,46,49,48}, 
991 {17,49,47,48}, {17,36,46,48}, {35,36,44,45}, {35,36,40,43}, 
992 {35,36,38,39}, {-4,-3,37,35}, {-123,34,33,1}, {-9,-8,-7,-6}, 
993 {-10,-7,32,-125}, {-127,-11,-126,-126}, {-7,-6,5,31}, {4,5,33,30}, 
994 {4,39,33,32}, {4,35,32,38}, {20,21,39,38}, {4,37,38,5}, 
995 {-11,-10,36,3}, {-11,15,14,35}, {13,16,34,34}, {-13,14,13,13}, 
996 {-3,1,30,29}, {-3,28,29,1}, {-2,31,28,-1}, {12,13,27,30}, 
997 {-2,26,12,12}, {35,29,42,36}, {34,35,36,33}, {32,35,36,31}, 
998 {30,35,36,29}, {28,35,36,27}, {26,35,36,25}, {34,39,38,35}, 
999 {32,39,38,33}, {30,39,38,31}, {28,39,38,29}, {26,39,38,27}, 
1000 {25,31,32,38}, {-18,-17,45,44}, {-18,17,28,44}, {-24,-20,42,-23}, 
1001 {11,35,27,14}, {25,28,39,41}, {37,41,40,38}, {34,40,36,35}, 
1002 {32,40,39,33}, {30,39,31,40}, {21,29,39,22}, {-31,37,28,4}, 
1003 {-32,33,35,36}, {32,33,34,34}, {18,35,36,48}, {34,25,40,35}, 
1004 {24,25,38,39}, {24,25,36,37}, {24,25,34,35}, {24,25,32,33}, 
1005 {24,13,41,31}, {17,11,41,35}, {15,16,34,35}, {13,14,34,35}, 
1006 {11,12,34,35}, {9,10,34,35}, {7,8,34,35}, {26,25,37,36}, 
1007 {35,36,37,38}, {37,36,39,38}, {37,38,39,40}, {25,31,36,39}, 
1008 {18,34,35,30}, {17,22,30,33}, {19,29,21,20}, {16,26,29,17}, 
1009 {24,29,28,25}, {22,31,28,23}, {20,31,30,21}, {18,31,30,19}, 
1010 {16,30,17,17}, {-21,-22,35,34}, {-21,-22,33,32}, {-21,-22,31,30}, 
1011 {-21,-22,29,28}, {-21,-22,27,26}, {-28,-22,25,31}, {24,28,29,30}, 
1012 {23,24,26,27}, {23,24,25,25}, {-69,-35,-32,27}, {-70,26,25,-66}, 
1013 {-68,-67,24,-33}, 
1014 };
1015         // ------------------------------- end copied code
1016
1017
1018 void make_prim(int type, float imat[3][3], int tot, int seg,
1019                 int subdiv, float dia, float d, int ext, int fill,
1020         float cent[3])
1021 {
1022         /*
1023          * type - for the type of shape
1024          * dia - the radius for cone,sphere cylinder etc.
1025          * d - depth for the cone
1026          * ext - ?
1027          * fill - end capping, and option to fill in circle
1028          * cent[3] - center of the data. 
1029          * */
1030         
1031         EditMesh *em = G.editMesh;
1032         EditVert *eve, *v1=NULL, *v2, *v3, *v4=NULL, *vtop, *vdown;
1033         float phi, phid, vec[3];
1034         float q[4], cmat[3][3], nor[3]= {0.0, 0.0, 0.0};
1035         short a, b;
1036
1037         phid= 2*M_PI/tot;
1038         phi= .25*M_PI;
1039
1040         switch(type) {
1041         case 10: /*  grid */
1042                 /* clear flags */
1043                 eve= em->verts.first;
1044                 while(eve) {
1045                         eve->f= 0;
1046                         eve= eve->next;
1047                 }
1048                 /* one segment first: the X axis */
1049                 phi= 1.0; 
1050                 phid= 2.0/((float)tot-1);
1051                 for(a=0;a<tot;a++) {
1052                         vec[0]= cent[0]+dia*phi;
1053                         vec[1]= cent[1]- dia;
1054                         vec[2]= cent[2];
1055                         Mat3MulVecfl(imat,vec);
1056                         eve= addvertlist(vec, NULL);
1057                         eve->f= 1+2+4;
1058                         if (a) {
1059                                 addedgelist(eve->prev, eve, NULL);
1060                         }
1061                         phi-=phid;
1062                 }
1063                 /* extrude and translate */
1064                 vec[0]= vec[2]= 0.0;
1065                 vec[1]= dia*phid;
1066                 Mat3MulVecfl(imat, vec);
1067                 for(a=0;a<seg-1;a++) {
1068                         extrudeflag_vert(2, nor);       // nor unused
1069                         translateflag(2, vec);
1070                 }
1071                 break;
1072         case 11: /*  UVsphere */
1073                 
1074                 /* clear all flags */
1075                 eve= em->verts.first;
1076                 while(eve) {
1077                         eve->f= 0;
1078                         eve= eve->next;
1079                 }
1080                 
1081                 /* one segment first */
1082                 phi= 0; 
1083                 phid/=2;
1084                 for(a=0; a<=tot; a++) {
1085                         vec[0]= dia*sin(phi);
1086                         vec[1]= 0.0;
1087                         vec[2]= dia*cos(phi);
1088                         eve= addvertlist(vec, NULL);
1089                         eve->f= 1+2+4;
1090                         if(a==0) v1= eve;
1091                         else addedgelist(eve->prev, eve, NULL);
1092                         phi+= phid;
1093                 }
1094                 
1095                 /* extrude and rotate */
1096                 phi= M_PI/seg;
1097                 q[0]= cos(phi);
1098                 q[3]= sin(phi);
1099                 q[1]=q[2]= 0;
1100                 QuatToMat3(q, cmat);
1101                 
1102                 for(a=0; a<seg; a++) {
1103                         extrudeflag_vert(2, nor); // nor unused
1104                         rotateflag(2, v1->co, cmat);
1105                 }
1106
1107                 removedoublesflag(4, 0, 0.0001);
1108
1109                 /* and now do imat */
1110                 eve= em->verts.first;
1111                 while(eve) {
1112                         if(eve->f & SELECT) {
1113                                 VecAddf(eve->co,eve->co,cent);
1114                                 Mat3MulVecfl(imat,eve->co);
1115                         }
1116                         eve= eve->next;
1117                 }
1118                 break;
1119         case 12: /* Icosphere */
1120                 {
1121                         EditVert *eva[12];
1122                         EditEdge *eed;
1123                         
1124                         /* clear all flags */
1125                         eve= em->verts.first;
1126                         while(eve) {
1127                                 eve->f= 0;
1128                                 eve= eve->next;
1129                         }
1130                         dia/=200;
1131                         for(a=0;a<12;a++) {
1132                                 vec[0]= dia*icovert[a][0];
1133                                 vec[1]= dia*icovert[a][1];
1134                                 vec[2]= dia*icovert[a][2];
1135                                 eva[a]= addvertlist(vec, NULL);
1136                                 eva[a]->f= 1+2;
1137                         }
1138                         for(a=0;a<20;a++) {
1139                                 EditFace *evtemp;
1140                                 v1= eva[ icoface[a][0] ];
1141                                 v2= eva[ icoface[a][1] ];
1142                                 v3= eva[ icoface[a][2] ];
1143                                 evtemp = addfacelist(v1, v2, v3, 0, NULL, NULL);
1144                                 evtemp->e1->f = 1+2;
1145                                 evtemp->e2->f = 1+2;
1146                                 evtemp->e3->f = 1+2;
1147                         }
1148
1149                         dia*=200;
1150                         for(a=1; a<subdiv; a++) esubdivideflag(2, dia, 0,1,0);
1151                         /* and now do imat */
1152                         eve= em->verts.first;
1153                         while(eve) {
1154                                 if(eve->f & 2) {
1155                                         VecAddf(eve->co,eve->co,cent);
1156                                         Mat3MulVecfl(imat,eve->co);
1157                                 }
1158                                 eve= eve->next;
1159                         }
1160                         
1161                         // Clear the flag 2 from the edges
1162                         for(eed=em->edges.first;eed;eed=eed->next){
1163                                 if(eed->f & 2){
1164                                            eed->f &= !2;
1165                                 }   
1166                         }
1167                 }
1168                 break;
1169         case 13: /* Monkey */
1170                 {
1171                         //extern int monkeyo, monkeynv, monkeynf;
1172                         //extern signed char monkeyf[][4];
1173                         //extern signed char monkeyv[][3];
1174                         EditVert **tv= MEM_mallocN(sizeof(*tv)*monkeynv*2, "tv");
1175                         int i;
1176
1177                         for (i=0; i<monkeynv; i++) {
1178                                 float v[3];
1179                                 v[0]= (monkeyv[i][0]+127)/128.0, v[1]= monkeyv[i][1]/128.0, v[2]= monkeyv[i][2]/128.0;
1180                                 tv[i]= addvertlist(v, NULL);
1181                                 tv[i]->f |= SELECT;
1182                                 tv[monkeynv+i]= (fabs(v[0]= -v[0])<0.001)?tv[i]:addvertlist(v, NULL);
1183                                 tv[monkeynv+i]->f |= SELECT;
1184                         }
1185                         for (i=0; i<monkeynf; i++) {
1186                                 addfacelist(tv[monkeyf[i][0]+i-monkeyo], tv[monkeyf[i][1]+i-monkeyo], tv[monkeyf[i][2]+i-monkeyo], (monkeyf[i][3]!=monkeyf[i][2])?tv[monkeyf[i][3]+i-monkeyo]:NULL, NULL, NULL);
1187                                 addfacelist(tv[monkeynv+monkeyf[i][2]+i-monkeyo], tv[monkeynv+monkeyf[i][1]+i-monkeyo], tv[monkeynv+monkeyf[i][0]+i-monkeyo], (monkeyf[i][3]!=monkeyf[i][2])?tv[monkeynv+monkeyf[i][3]+i-monkeyo]:NULL, NULL, NULL);
1188                         }
1189
1190                         MEM_freeN(tv);
1191
1192                         /* and now do imat */
1193                         for(eve= em->verts.first; eve; eve= eve->next) {
1194                                 if(eve->f & SELECT) {
1195                                         VecAddf(eve->co,eve->co,cent);
1196                                         Mat3MulVecfl(imat,eve->co);
1197                                 }
1198                         }
1199                         recalc_editnormals();
1200                 }
1201                 break;
1202         default: /* all types except grid, sphere... */
1203                 if(ext==0 && type!=7) d= 0;
1204         
1205                 /* vertices */
1206                 vtop= vdown= v1= v2= 0;
1207                 for(b=0; b<=ext; b++) {
1208                         for(a=0; a<tot; a++) {
1209                                 
1210                                 vec[0]= cent[0]+dia*sin(phi);
1211                                 vec[1]= cent[1]+dia*cos(phi);
1212                                 vec[2]= cent[2]+d;
1213                                 
1214                                 Mat3MulVecfl(imat, vec);
1215                                 eve= addvertlist(vec, NULL);
1216                                 eve->f= SELECT;
1217                                 if(a==0) {
1218                                         if(b==0) v1= eve;
1219                                         else v2= eve;
1220                                 }
1221                                 phi+=phid;
1222                         }
1223                         d= -d;
1224                 }
1225                 /* center vertices */
1226                 /* type 7, a cone can only have 1 one side filled
1227                  * if the cone has no capping, dont add vtop */
1228                 if((fill && type>1) || type == 7) {
1229                         VECCOPY(vec,cent);
1230                         vec[2]-= -d;
1231                         Mat3MulVecfl(imat,vec);
1232                         vdown= addvertlist(vec, NULL);
1233                         if((ext || type==7) && fill) {
1234                                 VECCOPY(vec,cent);
1235                                 vec[2]-= d;
1236                                 Mat3MulVecfl(imat,vec);
1237                                 vtop= addvertlist(vec, NULL);
1238                         }
1239                 } else {
1240                         vdown= v1;
1241                         vtop= v2;
1242                 }
1243                 if(vtop) vtop->f= SELECT;
1244                 if(vdown) vdown->f= SELECT;
1245         
1246                 /* top and bottom face */
1247                 if(fill || type==7) {
1248                         if(tot==4 && (type==0 || type==1)) {
1249                                 v3= v1->next->next;
1250                                 if(ext) v4= v2->next->next;
1251                                 
1252                                 addfacelist(v3, v1->next, v1, v3->next, NULL, NULL);
1253                                 if(ext) addfacelist(v2, v2->next, v4, v4->next, NULL, NULL);
1254                                 
1255                         }
1256                         else {
1257                                 v3= v1;
1258                                 v4= v2;
1259                                 for(a=1; a<tot; a++) {
1260                                         addfacelist(vdown, v3, v3->next, 0, NULL, NULL);
1261                                         v3= v3->next;
1262                                         if(ext && fill) {
1263                                                 addfacelist(vtop, v4, v4->next, 0, NULL, NULL);
1264                                                 v4= v4->next;
1265                                         }
1266                                 }
1267                                 if(type>1) {
1268                                         addfacelist(vdown, v3, v1, 0, NULL, NULL);
1269                                         if(ext) addfacelist(vtop, v4, v2, 0, NULL, NULL);
1270                                 }
1271                         }
1272                 }
1273                 else if(type==4) {  /* we need edges for a circle */
1274                         v3= v1;
1275                         for(a=1;a<tot;a++) {
1276                                 addedgelist(v3, v3->next, NULL);
1277                                 v3= v3->next;
1278                         }
1279                         addedgelist(v3, v1, NULL);
1280                 }
1281                 /* side faces */
1282                 if(ext) {
1283                         v3= v1;
1284                         v4= v2;
1285                         for(a=1; a<tot; a++) {
1286                                 addfacelist(v3, v3->next, v4->next, v4, NULL, NULL);
1287                                 v3= v3->next;
1288                                 v4= v4->next;
1289                         }
1290                         addfacelist(v3, v1, v2, v4, NULL, NULL);
1291                 }
1292                 else if(type==7 && fill) {
1293                         /* add the bottom flat area of the cone
1294                          * if capping is disabled dont bother */
1295                         v3= v1;
1296                         for(a=1; a<tot; a++) {
1297                                 addfacelist(vtop, v3->next, v3, 0, NULL, NULL);
1298                                 v3= v3->next;
1299                         }
1300                         addfacelist(vtop, v1, v3, 0, NULL, NULL);
1301                 }
1302         }
1303         /* simple selection flush OK, based on fact it's a single model */
1304         EM_select_flush(); /* flushes vertex -> edge -> face selection */
1305         
1306         if(type!=0 && type!=13)
1307                 righthandfaces(1);      /* otherwise monkey has eyes in wrong direction */
1308 }
1309
1310 void add_primitiveMesh(int type)
1311 {
1312         Mesh *me;
1313         float *curs, d, dia, phi, phid, cent[3], imat[3][3], mat[3][3];
1314         float cmat[3][3];
1315         static int tot=32, seg=32, subdiv=2,
1316                 /* so each type remembers its fill setting */
1317                 fill_circle=0, fill_cone=1, fill_cylinder=1;
1318         
1319         int ext=0, fill=0, totoud, newob=0;
1320         char *undostr="Add Primitive";
1321         char *name=NULL;
1322         
1323         if(G.scene->id.lib) return;
1324         
1325         /* this function also comes from an info window */
1326         if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_INFO); else return;
1327         if(G.vd==0) return;
1328
1329         if (G.obedit && G.obedit->type==OB_MESH && multires_test()) return;
1330         
1331         /* if editmode exists for other type, it exits */
1332         check_editmode(OB_MESH);
1333         
1334         if(G.f & (G_VERTEXPAINT+G_TEXTUREPAINT)) {
1335                 G.f &= ~(G_VERTEXPAINT+G_TEXTUREPAINT);
1336                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
1337         }
1338
1339         totoud= tot; /* store, and restore when cube/plane */
1340         
1341         dia= G.vd->grid;
1342         d= G.vd->grid;
1343         
1344         /* ext==extrudeflag, tot==amount of vertices in basis */
1345         switch(type) {
1346         case 0:         /* plane */
1347                 tot= 4;
1348                 ext= 0;
1349                 fill= 1;
1350                 newob = confirm_objectExists( &me, mat );
1351                 if(newob) name = "Plane";
1352                 undostr="Add Plane";
1353                 break;
1354         case 1:         /* cube  */
1355                 tot= 4;
1356                 ext= 1;
1357                 fill= 1;
1358                 newob = confirm_objectExists( &me, mat );
1359                 if(newob) name = "Cube";
1360                 undostr="Add Cube";
1361                 break;
1362         case 4:         /* circle  */
1363                 add_numbut(0, NUM|INT, "Vertices:", 3, 500, &tot, NULL);
1364                 add_numbut(1, NUM|FLO, "Radius:", 0.001*G.vd->grid, 100*G.vd->grid, &dia, NULL);
1365                 add_numbut(2, TOG|INT, "Fill", 0, 0, &(fill_circle), NULL);
1366                 if (!(do_clever_numbuts("Add Circle", 3, REDRAW))) return;
1367                 ext= 0;
1368                 fill = fill_circle;
1369                 newob = confirm_objectExists( &me, mat );
1370                 if(newob) name = "Circle";
1371                 undostr="Add Circle";
1372                 break;
1373         case 5:         /* cylinder  */
1374                 d*=2;
1375                 add_numbut(0, NUM|INT, "Vertices:", 2, 500, &tot, NULL);
1376                 add_numbut(1, NUM|FLO, "Radius:", 0.001*G.vd->grid, 100*G.vd->grid, &dia, NULL);
1377                 add_numbut(2, NUM|FLO, "Depth:", 0.001*G.vd->grid, 100*G.vd->grid, &d, NULL);
1378                 add_numbut(3, TOG|INT, "Cap Ends", 0, 0, &(fill_cylinder), NULL);
1379                 if (!(do_clever_numbuts("Add Cylinder", 4, REDRAW))) return;
1380                 ext= 1;
1381                 fill = fill_cylinder;
1382                 d/=2;
1383                 newob = confirm_objectExists( &me, mat );
1384                 if(newob) {
1385                         if (fill)       name = "Cylinder";
1386                         else            name = "Tube";
1387                 }
1388                 undostr="Add Cylinder";
1389                 break;
1390         case 7:         /* cone  */
1391                 d*=2;
1392                 add_numbut(0, NUM|INT, "Vertices:", 2, 500, &tot, NULL);
1393                 add_numbut(1, NUM|FLO, "Radius:", 0.001*G.vd->grid, 100*G.vd->grid, &dia, NULL);
1394                 add_numbut(2, NUM|FLO, "Depth:", 0.001*G.vd->grid, 100*G.vd->grid, &d, NULL);
1395                 add_numbut(3, TOG|INT, "Cap End", 0, 0, &(fill_cone), NULL);
1396                 if (!(do_clever_numbuts("Add Cone", 4, REDRAW))) return;
1397                 d/=2;
1398                 ext= 0;
1399                 fill = fill_cone;
1400                 newob = confirm_objectExists( &me, mat );
1401                 if(newob) name = "Cone";
1402                 undostr="Add Cone";
1403                 break;
1404         case 10:        /* grid */
1405                 add_numbut(0, NUM|INT, "X res:", 3, 1000, &tot, NULL);
1406                 add_numbut(1, NUM|INT, "Y res:", 3, 1000, &seg, NULL);
1407                 if (!(do_clever_numbuts("Add Grid", 2, REDRAW))) return; 
1408                 newob = confirm_objectExists( &me, mat );
1409                 if(newob) name = "Grid";
1410                 undostr="Add Grid";
1411                 break;
1412         case 11:        /* UVsphere */
1413                 add_numbut(0, NUM|INT, "Segments:", 3, 500, &seg, NULL);
1414                 add_numbut(1, NUM|INT, "Rings:", 3, 500, &tot, NULL);
1415                 add_numbut(2, NUM|FLO, "Radius:", 0.001*G.vd->grid, 100*G.vd->grid, &dia, NULL);
1416                 
1417                 if (!(do_clever_numbuts("Add UV Sphere", 3, REDRAW))) return;
1418                 
1419                 newob = confirm_objectExists( &me, mat );
1420                 if(newob) name = "Sphere";
1421                 undostr="Add UV Sphere";
1422                 break;
1423         case 12:        /* Icosphere */
1424                 add_numbut(0, NUM|INT, "Subdivision:", 1, 8, &subdiv, NULL);
1425                 add_numbut(1, NUM|FLO, "Radius:", 0.001*G.vd->grid, 100*G.vd->grid, &dia, NULL);
1426                 if (!(do_clever_numbuts("Add Ico Sphere", 2, REDRAW))) return;
1427                 
1428                 newob = confirm_objectExists( &me, mat );
1429                 if(newob) name = "Sphere";
1430                 undostr="Add Ico Sphere";
1431                 break;
1432         case 13:        /* Monkey */
1433                 newob = confirm_objectExists( &me, mat );
1434                 if(newob) name = "Suzanne";
1435                 undostr="Add Monkey";
1436                 break;
1437         default:
1438                 newob = confirm_objectExists( &me, mat );
1439                 break;
1440         }
1441
1442         if( name!=NULL ) {
1443                 rename_id((ID *)G.obedit, name );
1444                 rename_id((ID *)me, name );
1445         }
1446         
1447         d = -d;
1448         curs= give_cursor();
1449         VECCOPY(cent, curs);
1450         cent[0]-= G.obedit->obmat[3][0];
1451         cent[1]-= G.obedit->obmat[3][1];
1452         cent[2]-= G.obedit->obmat[3][2];
1453
1454         if ( !(newob) || U.flag & USER_ADD_VIEWALIGNED) Mat3CpyMat4(imat, G.vd->viewmat);
1455         else Mat3One(imat);
1456         Mat3MulVecfl(imat, cent);
1457         Mat3MulMat3(cmat, imat, mat);
1458         Mat3Inv(imat,cmat);
1459         
1460         
1461         if(type == 0 || type == 1) /* plane, cube (diameter of 1.41 makes it unit size) */
1462                 dia *= sqrt(2.0);
1463
1464         phid= 2*M_PI/tot;
1465         phi= .25*M_PI;
1466
1467         make_prim(type, imat, tot, seg, subdiv, dia, d, ext, fill, cent);
1468
1469         if(type<2) tot = totoud;
1470
1471         /* simple selection flush OK, based on fact it's a single model */
1472         EM_select_flush(); // flushes vertex -> edge -> face selection
1473         
1474         if(type!=0 && type!=13) righthandfaces(1);      /* otherwise monkey has eyes in wrong direction... */
1475         countall();
1476
1477         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1478         
1479         /* if a new object was created, it stores it in Mesh, for reload original data and undo */
1480         if ( !(newob) || U.flag & USER_ADD_EDITMODE) {
1481                 if(newob) load_editMesh();
1482         } else {
1483                 exit_editmode(2);
1484         }
1485         
1486         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */        
1487         allqueue(REDRAWALL, 0);
1488         
1489         BIF_undo_push(undostr);
1490 }
1491