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