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