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