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