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