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