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