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