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