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