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