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