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