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