skip assigning vars for inline bmesh flag funcs, just cast.
[blender.git] / source / blender / bmesh / operators / primitiveops.c
1 #include "MEM_guardedalloc.h"
2
3 #include "DNA_meshdata_types.h"
4 #include "DNA_mesh_types.h"
5 #include "DNA_object_types.h"
6 #include "DNA_scene_types.h"
7
8 #include "BLI_utildefines.h"
9
10 #include "BLI_math.h"
11 #include "BLI_ghash.h"
12 #include "BLI_blenlib.h"
13 #include "BLI_array.h"
14
15 #include "ED_mesh.h"
16
17 #include "bmesh.h"
18 #include "mesh_intern.h"
19 #include "bmesh_private.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25
26 /* ************************ primitives ******************* */
27
28 static float icovert[12][3] = {
29         {0.0f,0.0f,-200.0f}, 
30         {144.72f, -105.144f,-89.443f},
31         {-55.277f, -170.128,-89.443f}, 
32         {-178.885f,0.0f,-89.443f},
33         {-55.277f,170.128f,-89.443f}, 
34         {144.72f,105.144f,-89.443f},
35         {55.277f,-170.128f,89.443f},
36         {-144.72f,-105.144f,89.443f},
37         {-144.72f,105.144f,89.443f},
38         {55.277f,170.128f,89.443f},
39         {178.885f,0.0f,89.443f},
40         {0.0f,0.0f,200.0f}
41 };
42
43 static short icoface[20][3] = {
44         {1,0,2},
45         {1,0,5},
46         {2,0,3},
47         {3,0,4},
48         {4,0,5},
49         {1,5,10},
50         {2,1,6},
51         {3,2,7},
52         {4,3,8},
53         {5,4,9},
54         {10,1,6},
55         {6,2,7},
56         {7,3,8},
57         {8,4,9},
58         {9,5,10},
59         {6,10,11},
60         {7,6,11},
61         {8,7,11},
62         {9,8,11},
63         {10,9,11}
64 };
65
66 // HACK: these can also be found in cmoview.tga.c, but are here so that they can be found by linker
67 // this hack is only used so that scons+mingw + split-sources hack works
68         // ------------------------------- start copied code
69 /* these are not the monkeys you are looking for */
70 static int monkeyo= 4;
71 static int monkeynv= 271;
72 static int monkeynf= 250;
73 static signed char monkeyv[271][3]= {
74 {-71,21,98},{-63,12,88},{-57,7,74},{-82,-3,79},{-82,4,92},
75 {-82,17,100},{-92,21,102},{-101,12,95},{-107,7,83},
76 {-117,31,84},{-109,31,95},{-96,31,102},{-92,42,102},
77 {-101,50,95},{-107,56,83},{-82,66,79},{-82,58,92},
78 {-82,46,100},{-71,42,98},{-63,50,88},{-57,56,74},
79 {-47,31,72},{-55,31,86},{-67,31,97},{-66,31,99},
80 {-70,43,100},{-82,48,103},{-93,43,105},{-98,31,105},
81 {-93,20,105},{-82,31,106},{-82,15,103},{-70,20,100},
82 {-127,55,95},{-127,45,105},{-127,-87,94},{-127,-41,100},
83 {-127,-24,102},{-127,-99,92},{-127,52,77},{-127,73,73},
84 {-127,115,-70},{-127,72,-109},{-127,9,-106},{-127,-49,-45},
85 {-101,-24,72},{-87,-56,73},{-82,-89,73},{-80,-114,68},
86 {-85,-121,67},{-104,-124,71},{-127,-126,74},{-71,-18,68},
87 {-46,-5,69},{-21,19,57},{-17,55,76},{-36,62,80},
88 {-64,77,88},{-86,97,94},{-107,92,97},{-119,63,96},
89 {-106,53,99},{-111,39,98},{-101,12,95},{-79,2,90},
90 {-64,8,86},{-47,24,83},{-45,38,83},{-50,48,85},
91 {-72,56,92},{-95,60,97},{-127,-98,94},{-113,-92,94},
92 {-112,-107,91},{-119,-113,89},{-127,-114,88},{-127,-25,96},
93 {-127,-18,95},{-114,-19,95},{-111,-29,96},{-116,-37,95},
94 {-76,-6,86},{-48,7,80},{-34,26,77},{-32,48,84},
95 {-39,53,93},{-71,70,102},{-87,82,107},{-101,79,109},
96 {-114,55,108},{-111,-13,104},{-100,-57,91},{-95,-90,88},
97 {-93,-105,85},{-97,-117,81},{-106,-119,81},{-127,-121,82},
98 {-127,6,93},{-127,27,98},{-85,61,95},{-106,18,96},
99 {-110,27,97},{-112,-88,94},{-117,-57,96},{-127,-57,96},
100 {-127,-42,95},{-115,-35,100},{-110,-29,102},{-113,-17,100},
101 {-122,-16,100},{-127,-26,106},{-121,-19,104},{-115,-20,104},
102 {-113,-29,106},{-117,-32,103},{-127,-37,103},{-94,-40,71},
103 {-106,-31,91},{-104,-40,91},{-97,-32,71},{-127,-112,88},
104 {-121,-111,88},{-115,-105,91},{-115,-95,93},{-127,-100,84},
105 {-115,-96,85},{-115,-104,82},{-121,-109,81},{-127,-110,81},
106 {-105,28,100},{-103,20,99},{-84,55,97},{-92,54,99},
107 {-73,51,99},{-55,45,89},{-52,37,88},{-53,25,87},
108 {-66,13,92},{-79,8,95},{-98,14,100},{-104,38,100},
109 {-100,48,100},{-97,46,97},{-102,38,97},{-96,16,97},
110 {-79,11,93},{-68,15,90},{-57,27,86},{-56,36,86},
111 {-59,43,87},{-74,50,96},{-91,51,98},{-84,52,96},
112 {-101,22,96},{-102,29,96},{-113,59,78},{-102,85,79},
113 {-84,88,76},{-65,71,71},{-40,58,63},{-25,52,59},
114 {-28,21,48},{-50,0,53},{-71,-12,60},{-127,115,37},
115 {-127,126,-10},{-127,-25,-86},{-127,-59,24},{-127,-125,59},
116 {-127,-103,44},{-127,-73,41},{-127,-62,36},{-18,30,7},
117 {-17,41,-6},{-28,34,-56},{-68,56,-90},{-33,-6,9},
118 {-51,-16,-21},{-45,-1,-55},{-84,7,-85},{-97,-45,52},
119 {-104,-53,33},{-90,-91,49},{-95,-64,50},{-85,-117,51},
120 {-109,-97,47},{-111,-69,46},{-106,-121,56},{-99,-36,55},
121 {-100,-29,60},{-101,-22,64},{-100,-50,21},{-89,-40,-34},
122 {-83,-19,-69},{-69,111,-49},{-69,119,-9},{-69,109,30},
123 {-68,67,55},{-34,52,43},{-46,58,36},{-45,90,7},
124 {-25,72,16},{-25,79,-15},{-45,96,-25},{-45,87,-57},
125 {-25,69,-46},{-48,42,-75},{-65,3,-70},{-22,42,-26},
126 {-75,-22,19},{-72,-25,-27},{-13,52,-30},{-28,-18,-16},
127 {6,-13,-42},{37,7,-55},{46,41,-54},{31,65,-54},
128 {4,61,-40},{3,53,-37},{25,56,-50},{35,37,-52},
129 {28,10,-52},{5,-5,-39},{-21,-9,-17},{-9,46,-28},
130 {-6,39,-37},{-14,-3,-27},{6,0,-47},{25,12,-57},
131 {31,32,-57},{23,46,-56},{4,44,-46},{-19,37,-27},
132 {-20,22,-35},{-30,12,-35},{-22,11,-35},{-19,2,-35},
133 {-23,-2,-35},{-34,0,-9},{-35,-3,-22},{-35,5,-24},
134 {-25,26,-27},{-13,31,-34},{-13,30,-41},{-23,-2,-41},
135 {-18,2,-41},{-21,10,-41},{-29,12,-41},{-19,22,-41},
136 {6,42,-53},{25,44,-62},{34,31,-63},{28,11,-62},
137 {7,0,-54},{-14,-2,-34},{-5,37,-44},{-13,14,-42},
138 {-7,8,-43},{1,16,-47},{-4,22,-45},{3,30,-48},
139 {8,24,-49},{15,27,-50},{12,35,-50},{4,56,-62},
140 {33,60,-70},{48,38,-64},{41,7,-68},{6,-11,-63},
141 {-26,-16,-42},{-17,49,-49},
142 };
143
144 static signed char monkeyf[250][4]= {
145 {27,4,5,26}, {25,4,5,24}, {3,6,5,4}, {1,6,5,2}, {5,6,7,4}, 
146 {3,6,7,2}, {5,8,7,6}, {3,8,7,4}, {7,8,9,6}, 
147 {5,8,9,4}, {7,10,9,8}, {5,10,9,6}, {9,10,11,8}, 
148 {7,10,11,6}, {9,12,11,10}, {7,12,11,8}, {11,6,13,12}, 
149 {5,4,13,12}, {3,-2,13,12}, {-3,-4,13,12}, {-5,-10,13,12}, 
150 {-11,-12,14,12}, {-13,-18,14,13}, {-19,4,5,13}, {10,12,4,4}, 
151 {10,11,9,9}, {8,7,9,9}, {7,5,6,6}, {6,3,4,4}, 
152 {5,1,2,2}, {4,-1,0,0}, {3,-3,-2,-2}, {22,67,68,23}, 
153 {20,65,66,21}, {18,63,64,19}, {16,61,62,17}, {14,59,60,15}, 
154 {12,19,48,57}, {18,19,48,47}, {18,19,48,47}, {18,19,48,47}, 
155 {18,19,48,47}, {18,19,48,47}, {18,19,48,47}, {18,19,48,47}, 
156 {18,19,48,47}, {18,-9,-8,47}, {18,27,45,46}, {26,55,43,44}, 
157 {24,41,42,54}, {22,39,40,23}, {20,37,38,21}, {18,35,36,19}, 
158 {16,33,34,17}, {14,31,32,15}, {12,39,30,13}, {11,48,45,38}, 
159 {8,36,-19,9}, {8,-20,44,47}, {42,45,46,43}, {18,19,40,39}, 
160 {16,17,38,37}, {14,15,36,35}, {32,44,43,33}, {12,33,32,42}, 
161 {19,44,43,42}, {40,41,42,-27}, {8,9,39,-28}, {15,43,42,16}, 
162 {13,43,42,14}, {11,43,42,12}, {9,-30,42,10}, {37,12,38,-32}, 
163 {-33,37,45,46}, {-33,40,41,39}, {38,40,41,37}, {36,40,41,35}, 
164 {34,40,41,33}, {36,39,38,37}, {35,40,39,38}, {1,2,14,21}, 
165 {1,2,40,13}, {1,2,40,39}, {1,24,12,39}, {-34,36,38,11}, 
166 {35,38,36,37}, {-37,8,35,37}, {-11,-12,-45,40}, {-11,-12,39,38}, 
167 {-11,-12,37,36}, {-11,-12,35,34}, {33,34,40,41}, {33,34,38,39}, 
168 {33,34,36,37}, {33,-52,34,35}, {33,37,36,34}, {33,35,34,34}, 
169 {8,7,37,36}, {-32,7,35,46}, {-34,-33,45,46}, {4,-33,43,34}, 
170 {-34,-33,41,42}, {-34,-33,39,40}, {-34,-33,37,38}, {-34,-33,35,36}, 
171 {-34,-33,33,34}, {-34,-33,31,32}, {-34,-4,28,30}, {-5,-34,28,27}, 
172 {-35,-44,36,27}, {26,35,36,45}, {24,25,44,45}, {25,23,44,42}, 
173 {25,24,41,40}, {25,24,39,38}, {25,24,37,36}, {25,24,35,34}, 
174 {25,24,33,32}, {25,24,31,30}, {15,24,29,38}, {25,24,27,26}, 
175 {23,12,37,26}, {11,12,35,36}, {-86,-59,36,-80}, {-60,-61,36,35}, 
176 {-62,-63,36,35}, {-64,-65,36,35}, {-66,-67,36,35}, {-68,-69,36,35}, 
177 {-70,-71,36,35}, {-72,-73,36,35}, {-74,-75,36,35}, {42,43,53,58}, 
178 {40,41,57,56}, {38,39,55,57}, {-81,-80,37,56}, {-83,-82,55,52}, 
179 {-85,-84,51,49}, {-87,-86,48,49}, {47,50,51,48}, {46,48,51,49}, 
180 {43,46,49,44}, {-92,-91,45,42}, {-23,49,50,-20}, {-94,40,48,-24}, 
181 {-96,-22,48,49}, {-97,48,21,-90}, {-100,36,50,23}, {22,49,48,-100}, 
182 {-101,47,46,22}, {21,45,35,25}, {33,34,44,41}, {13,14,28,24}, 
183 {-107,26,30,-106}, {14,46,45,15}, {14,44,43,-110}, {-111,42,23,-110}, 
184 {6,7,45,46}, {45,44,47,46}, {45,46,47,48}, {47,46,49,48}, 
185 {17,49,47,48}, {17,36,46,48}, {35,36,44,45}, {35,36,40,43}, 
186 {35,36,38,39}, {-4,-3,37,35}, {-123,34,33,1}, {-9,-8,-7,-6}, 
187 {-10,-7,32,-125}, {-127,-11,-126,-126}, {-7,-6,5,31}, {4,5,33,30}, 
188 {4,39,33,32}, {4,35,32,38}, {20,21,39,38}, {4,37,38,5}, 
189 {-11,-10,36,3}, {-11,15,14,35}, {13,16,34,34}, {-13,14,13,13}, 
190 {-3,1,30,29}, {-3,28,29,1}, {-2,31,28,-1}, {12,13,27,30}, 
191 {-2,26,12,12}, {35,29,42,36}, {34,35,36,33}, {32,35,36,31}, 
192 {30,35,36,29}, {28,35,36,27}, {26,35,36,25}, {34,39,38,35}, 
193 {32,39,38,33}, {30,39,38,31}, {28,39,38,29}, {26,39,38,27}, 
194 {25,31,32,38}, {-18,-17,45,44}, {-18,17,28,44}, {-24,-20,42,-23}, 
195 {11,35,27,14}, {25,28,39,41}, {37,41,40,38}, {34,40,36,35}, 
196 {32,40,39,33}, {30,39,31,40}, {21,29,39,22}, {-31,37,28,4}, 
197 {-32,33,35,36}, {32,33,34,34}, {18,35,36,48}, {34,25,40,35}, 
198 {24,25,38,39}, {24,25,36,37}, {24,25,34,35}, {24,25,32,33}, 
199 {24,13,41,31}, {17,11,41,35}, {15,16,34,35}, {13,14,34,35}, 
200 {11,12,34,35}, {9,10,34,35}, {7,8,34,35}, {26,25,37,36}, 
201 {35,36,37,38}, {37,36,39,38}, {37,38,39,40}, {25,31,36,39}, 
202 {18,34,35,30}, {17,22,30,33}, {19,29,21,20}, {16,26,29,17}, 
203 {24,29,28,25}, {22,31,28,23}, {20,31,30,21}, {18,31,30,19}, 
204 {16,30,17,17}, {-21,-22,35,34}, {-21,-22,33,32}, {-21,-22,31,30}, 
205 {-21,-22,29,28}, {-21,-22,27,26}, {-28,-22,25,31}, {24,28,29,30}, 
206 {23,24,26,27}, {23,24,25,25}, {-69,-35,-32,27}, {-70,26,25,-66}, 
207 {-68,-67,24,-33}, 
208 };
209
210 #define VERT_MARK       1
211
212 #define EDGE_ORIG       1
213 #define EDGE_MARK       2
214
215 #define FACE_MARK       1
216 #define FACE_NEW        2
217
218 void bmesh_create_grid_exec(BMesh *bm, BMOperator *op)
219 {
220         BMOperator bmop, prevop;
221         BMVert *eve, *preveve;
222         BMEdge *e;
223         float vec[3], mat[4][4], phi, phid, dia=BMO_Get_Float(op, "size");
224         int a, tot=BMO_Get_Int(op, "xsegments"), seg=BMO_Get_Int(op, "ysegments");
225
226         if (tot < 2) tot = 2;
227         if (seg < 2) seg = 2;
228
229         BMO_Get_Mat4(op, "mat", mat);
230
231         /* one segment first: the X axis */
232         phi= 1.0; 
233         phid= 2.0/((float)tot-1);
234         for(a=0;a<tot;a++) {
235                 vec[0]= dia*phi;
236                 vec[1]= - dia;
237                 vec[2]= 0.0f;
238                 mul_m4_v3(mat,vec);
239
240                 eve= BM_Make_Vert(bm, vec, NULL);
241                 BM_Select(bm, eve, 1);
242
243                 if (a) {
244                         e = BM_Make_Edge(bm, preveve, eve, NULL, 1);
245                         BMO_SetFlag(bm, e, EDGE_ORIG);
246                 }
247
248                 preveve = eve;
249                 phi-=phid;
250         }
251
252         /* extrude and translate */
253         vec[0]= vec[2]= 0.0;
254         vec[1]= dia*phid;
255         mul_mat3_m4_v3(mat, vec);
256                 
257         for(a=0;a<seg-1;a++) {
258                 if (a) {
259                         BMO_InitOpf(bm, &bmop, "extrude_edge_only edges=%s", &prevop, "geomout");
260                         BMO_Exec_Op(bm, &bmop);
261                         BMO_Finish_Op(bm, &prevop);
262
263                         BMO_Flag_Buffer(bm, &bmop, "geomout", VERT_MARK, BM_VERT);
264                 } else {
265                         BMO_InitOpf(bm, &bmop, "extrude_edge_only edges=%fe", EDGE_ORIG);
266                         BMO_Exec_Op(bm, &bmop);
267                         BMO_Flag_Buffer(bm, &bmop, "geomout", VERT_MARK, BM_VERT);
268                 }
269
270                 BMO_CallOpf(bm, "translate vec=%v verts=%s", vec, &bmop, "geomout");
271                 prevop = bmop;
272         }
273
274         if (a)
275                 BMO_Finish_Op(bm, &bmop);
276
277         BMO_Flag_To_Slot(bm, op, "vertout", VERT_MARK, BM_VERT);
278 }
279
280 void bmesh_create_uvsphere_exec(BMesh *bm, BMOperator *op)
281 {
282         BMOperator bmop, prevop;
283         BMVert *eve, *preveve, *v1;
284         BMEdge *e;
285         BMIter iter;
286         float vec[3], mat[4][4], cmat[3][3], phi, q[4];
287         float phid, dia=BMO_Get_Float(op, "diameter");
288         int a, seg=BMO_Get_Int(op, "segments"), tot=BMO_Get_Int(op, "revolutions");
289
290         BMO_Get_Mat4(op, "mat", mat);
291
292         phid= 2.0f*(float)M_PI/tot;
293         phi= .25f*(float)M_PI;
294
295         /* one segment first */
296         phi= 0; 
297         phid/=2;
298         for(a=0; a<=tot; a++) {
299                 vec[0]= dia*sin(phi);
300                 vec[1]= 0.0;
301                 vec[2]= dia*cos(phi);
302                 eve= BM_Make_Vert(bm, vec, NULL);
303                 BMO_SetFlag(bm, eve, VERT_MARK);
304
305                 if(a==0) v1= eve;
306                 else {
307                         e = BM_Make_Edge(bm, preveve, eve, NULL, 0);
308                         BMO_SetFlag(bm, e, EDGE_ORIG);
309                 }
310
311                 phi+= phid;
312                 preveve = eve;
313         }
314
315         /* extrude and rotate */
316         phi= M_PI/seg;
317         q[0]= cos(phi);
318         q[3]= sin(phi);
319         q[1]=q[2]= 0;
320         quat_to_mat3(cmat, q);
321
322         for(a=0; a<seg; a++) {
323                 if (a) {
324                         BMO_InitOpf(bm, &bmop, "extrude_edge_only edges=%s", &prevop, "geomout");
325                         BMO_Exec_Op(bm, &bmop);
326                         BMO_Finish_Op(bm, &prevop);
327                 } else {
328                         BMO_InitOpf(bm, &bmop, "extrude_edge_only edges=%fe", EDGE_ORIG);
329                         BMO_Exec_Op(bm, &bmop);
330                 }
331
332                 BMO_Flag_Buffer(bm, &bmop, "geomout", VERT_MARK, BM_VERT);
333                 BMO_CallOpf(bm, "rotate cent=%v mat=%m3 verts=%s", vec, cmat, &bmop, "geomout");
334                 
335                 prevop = bmop;
336         }
337
338         if (a)
339                 BMO_Finish_Op(bm, &bmop);
340
341         BMO_CallOpf(bm, "removedoubles verts=%fv dist=%f", VERT_MARK, 0.001f);
342
343         /* and now do imat */
344         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
345                 if(BMO_TestFlag(bm, eve, VERT_MARK)) {
346                         mul_m4_v3(mat, eve->co);
347                 }
348         }
349
350         BMO_Flag_To_Slot(bm, op, "vertout", VERT_MARK, BM_VERT);
351 }
352
353 void bmesh_create_icosphere_exec(BMesh *bm, BMOperator *op)
354 {
355         BMVert *eva[12];
356         BMIter liter;
357         BMLoop *l;
358         float vec[3], mat[4][4], phi, phid;
359         float dia = BMO_Get_Float(op, "diameter");
360         int a, subdiv = BMO_Get_Int(op, "subdivisions");
361
362         BMO_Get_Mat4(op, "mat", mat);
363
364         phid= 2.0f*(float)M_PI/subdiv;
365         phi= .25f*(float)M_PI;
366
367         dia/=200;
368         for(a=0;a<12;a++) {
369                 vec[0]= dia*icovert[a][0];
370                 vec[1]= dia*icovert[a][1];
371                 vec[2]= dia*icovert[a][2];
372                 eva[a]= BM_Make_Vert(bm, vec, NULL);
373
374                 mul_m4_v3(mat, eva[a]->co);
375                 BM_Select(bm, eva[a], 1);
376         }
377
378         for(a=0;a<20;a++) {
379                 BMFace *eftemp;
380                 BMVert *v1, *v2, *v3;
381
382                 v1= eva[ icoface[a][0] ];
383                 v2= eva[ icoface[a][1] ];
384                 v3= eva[ icoface[a][2] ];
385
386                 eftemp = BM_Make_QuadTri(bm, v1, v2, v3, NULL, NULL, 0);
387                 
388                 BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, eftemp) {
389                         BMO_SetFlag(bm, l->e, EDGE_MARK);
390                 }
391
392                 BMO_SetFlag(bm, eftemp, FACE_MARK);
393         }
394
395         dia*=200;
396
397         for(a=1; a<subdiv; a++) {
398                 BMOperator bmop;
399
400                 BMO_InitOpf(bm, &bmop, 
401                         "esubd edges=%fe smooth=%f numcuts=%i gridfill=%i beauty=%i", 
402                         EDGE_MARK, dia, 1, 1, B_SPHERE);
403                 BMO_Exec_Op(bm, &bmop);
404                 BMO_Flag_Buffer(bm, &bmop, "geomout", VERT_MARK, BM_VERT);
405                 BMO_Flag_Buffer(bm, &bmop, "geomout", EDGE_MARK, BM_EDGE);
406                 BMO_Finish_Op(bm, &bmop);
407         }
408
409         BMO_Flag_To_Slot(bm, op, "vertout", VERT_MARK, BM_VERT);
410 }
411
412 void bmesh_create_monkey_exec(BMesh *bm, BMOperator *op)
413 {
414         BMVert *eve;
415         BMVert **tv= MEM_mallocN(sizeof(*tv)*monkeynv*2, "tv");
416         float mat[4][4];
417         int i;
418
419         BMO_Get_Mat4(op, "mat", mat);
420
421         for (i=0; i<monkeynv; i++) {
422                 float v[3];
423
424                 v[0]= (monkeyv[i][0]+127)/128.0, v[1]= monkeyv[i][1]/128.0, v[2]= monkeyv[i][2]/128.0;
425
426                 tv[i]= BM_Make_Vert(bm, v, NULL);
427                 BMO_SetFlag(bm, tv[i], VERT_MARK);
428
429                 tv[monkeynv+i]= (fabs(v[0]= -v[0])<0.001)?tv[i]: (eve=BM_Make_Vert(bm, v, NULL), mul_m4_v3(mat, eve->co), eve);
430                 BMO_SetFlag(bm, tv[monkeynv+i], VERT_MARK);
431
432                 mul_m4_v3(mat, tv[i]->co);
433         }
434
435         for (i=0; i<monkeynf; i++) {
436                 BM_Make_QuadTri(bm, 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, 0);
437                 BM_Make_QuadTri(bm, 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, 0);
438         }
439
440         MEM_freeN(tv);
441
442         BMO_Flag_To_Slot(bm, op, "vertout", VERT_MARK, BM_VERT);
443 }
444
445
446 void bmesh_create_circle_exec(BMesh *bm, BMOperator *op)
447 {
448         BMVert *v1, *lastv1=NULL, *cent1, *firstv1=NULL;
449         float vec[3], mat[4][4], phi, phid;
450         float dia = BMO_Get_Float(op, "diameter");
451         int cap_ends = BMO_Get_Int(op, "cap_ends"), segs=BMO_Get_Int(op, "segments");
452         int cap_tris=BMO_Get_Int(op, "cap_tris");
453         int a;
454         
455         if (!segs)
456                 return;
457         
458         BMO_Get_Mat4(op, "mat", mat);
459
460         phid= 2.0f*(float)M_PI/segs;
461         phi= .25f*(float)M_PI;
462
463         if (cap_ends) {
464                 vec[0] = vec[1] = 0.0f;
465                 vec[2] = 0.0;
466                 mul_m4_v3(mat, vec);
467                 
468                 cent1 = BM_Make_Vert(bm, vec, NULL);
469         }
470
471         for (a=0; a<segs; a++, phi+=phid) {
472                 vec[0]= dia*sin(phi);
473                 vec[1]= dia*cos(phi);
474                 vec[2]= 0.0f;
475                 mul_m4_v3(mat, vec);
476                 v1 = BM_Make_Vert(bm, vec, NULL);
477
478                 BMO_SetFlag(bm, v1, VERT_MARK);
479                 
480                 if (lastv1)
481                         BM_Make_Edge(bm, v1, lastv1, NULL, 0);
482                 
483                 if (a && cap_ends) {
484                         BMFace *f;
485                         
486                         f = BM_Make_QuadTri(bm, cent1, lastv1, v1, NULL, NULL, 0);
487                         BMO_SetFlag(bm, f, FACE_NEW);
488                 }
489                 
490                 if (!firstv1)
491                         firstv1 = v1;
492
493                 lastv1 = v1;
494         }
495
496         if (!a)
497                 return;
498
499         BM_Make_Edge(bm, lastv1, firstv1, NULL, 0);
500
501         if (cap_ends) {
502                 BMFace *f;
503                 
504                 f = BM_Make_QuadTri(bm, cent1, firstv1, v1, NULL, NULL, 0);
505                 BMO_SetFlag(bm, f, FACE_NEW);
506         }
507         
508         if (!cap_tris) {
509                 BMO_CallOpf(bm, "dissolvefaces faces=%ff", FACE_NEW);
510         }
511         
512         BMO_Flag_To_Slot(bm, op, "vertout", VERT_MARK, BM_VERT);
513 }
514
515 void bmesh_create_cone_exec(BMesh *bm, BMOperator *op)
516 {
517         BMVert *v1, *v2, *lastv1=NULL, *lastv2=NULL, *cent1, *cent2, *firstv1, *firstv2;
518         float vec[3], mat[4][4], phi, phid;
519         float dia1 = BMO_Get_Float(op, "diameter1");
520         float dia2 = BMO_Get_Float(op, "diameter2");
521         float depth = BMO_Get_Float(op, "depth");
522         int cap_ends = BMO_Get_Int(op, "cap_ends"), segs=BMO_Get_Int(op, "segments");
523         int cap_tris=BMO_Get_Int(op, "cap_tris");
524         int a;
525         
526         if (!segs)
527                 return;
528         
529         BMO_Get_Mat4(op, "mat", mat);
530
531         phid= 2.0f*(float)M_PI/segs;
532         phi= .25f*(float)M_PI;
533
534         depth *= 0.5f;
535         if (cap_ends) {
536                 vec[0] = vec[1] = 0.0f;
537                 vec[2] = -depth;
538                 mul_m4_v3(mat, vec);
539                 
540                 cent1 = BM_Make_Vert(bm, vec, NULL);
541
542                 vec[0] = vec[1] = 0.0f;
543                 vec[2] = depth;
544                 mul_m4_v3(mat, vec);
545                 
546                 cent2 = BM_Make_Vert(bm, vec, NULL);
547
548                 BMO_SetFlag(bm, cent1, VERT_MARK);
549                 BMO_SetFlag(bm, cent2, VERT_MARK);
550         }
551
552         for (a=0; a<segs; a++, phi+=phid) {
553                 vec[0]= dia1*sin(phi);
554                 vec[1]= dia1*cos(phi);
555                 vec[2]= -depth;
556                 mul_m4_v3(mat, vec);
557                 v1 = BM_Make_Vert(bm, vec, NULL);
558
559                 vec[0]= dia2*sin(phi);
560                 vec[1]= dia2*cos(phi);
561                 vec[2]= depth;
562                 mul_m4_v3(mat, vec);
563                 v2 = BM_Make_Vert(bm, vec, NULL);
564
565                 BMO_SetFlag(bm, v1, VERT_MARK);
566                 BMO_SetFlag(bm, v2, VERT_MARK);
567
568                 if (a) {
569                         if (cap_ends) {
570                                 BMFace *f;
571                                 
572                                 f = BM_Make_QuadTri(bm, cent1, lastv1, v1, NULL, NULL, 0);
573                                 BMO_SetFlag(bm, f, FACE_NEW);
574                                 f = BM_Make_QuadTri(bm, v2, lastv2, cent2, NULL, NULL, 0);
575                                 BMO_SetFlag(bm, f, FACE_NEW);
576                         }
577                         BM_Make_QuadTri(bm, lastv1, lastv2, v2, v1, NULL, 0);
578                 } else {
579                         firstv1 = v1;
580                         firstv2 = v2;
581                 }
582
583                 lastv1 = v1;
584                 lastv2 = v2;
585         }
586
587         if (!a)
588                 return;
589
590         if (cap_ends) {
591                 BMFace *f;
592                 
593                 f = BM_Make_QuadTri(bm, cent1, firstv1, v1, NULL, NULL, 0);
594                 BMO_SetFlag(bm, f, FACE_NEW);
595                 f = BM_Make_QuadTri(bm, v2, firstv2, cent2, NULL, NULL, 0);
596                 BMO_SetFlag(bm, f, FACE_NEW);
597         }
598         
599         if (!cap_tris) {
600                 BMO_CallOpf(bm, "dissolvefaces faces=%ff", FACE_NEW);
601         }
602         
603         BM_Make_QuadTri(bm, firstv1, firstv2, v2, v1, NULL, 0);
604
605         BMO_CallOpf(bm, "removedoubles verts=%fv dist=%f", VERT_MARK, 0.000001);
606         BMO_Flag_To_Slot(bm, op, "vertout", VERT_MARK, BM_VERT);
607 }
608
609 void bmesh_create_cube_exec(BMesh *bm, BMOperator *op)
610 {
611         BMVert *v1, *v2, *v3, *v4, *v5, *v6, *v7, *v8;
612         float vec[3], mat[4][4], off = BMO_Get_Float(op, "size") / 2.0f;
613
614         BMO_Get_Mat4(op, "mat", mat);
615
616         if (!off) off = 0.5f;
617
618         vec[0] = -off;
619         vec[1] = -off;
620         vec[2] = -off;
621         mul_m4_v3(mat, vec);
622         v1 = BM_Make_Vert(bm, vec, NULL);
623         BMO_SetFlag(bm, v1, VERT_MARK);
624
625         vec[0] = -off;
626         vec[1] = off;
627         vec[2] = -off;
628         mul_m4_v3(mat, vec);
629         v2 = BM_Make_Vert(bm, vec, NULL);
630         BMO_SetFlag(bm, v2, VERT_MARK);
631
632         vec[0] = off;
633         vec[1] = off;
634         vec[2] = -off;
635         mul_m4_v3(mat, vec);
636         v3 = BM_Make_Vert(bm, vec, NULL);
637         BMO_SetFlag(bm, v3, VERT_MARK);
638
639         vec[0] = off;
640         vec[1] = -off;
641         vec[2] = -off;
642         mul_m4_v3(mat, vec);
643         v4 = BM_Make_Vert(bm, vec, NULL);
644         BMO_SetFlag(bm, v4, VERT_MARK);
645
646         vec[0] = -off;
647         vec[1] = -off;
648         vec[2] = off;
649         mul_m4_v3(mat, vec);
650         v5 = BM_Make_Vert(bm, vec, NULL);
651         BMO_SetFlag(bm, v5, VERT_MARK);
652
653         vec[0] = -off;
654         vec[1] = off;
655         vec[2] = off;
656         mul_m4_v3(mat, vec);
657         v6 = BM_Make_Vert(bm, vec, NULL);
658         BMO_SetFlag(bm, v6, VERT_MARK);
659
660         vec[0] = off;
661         vec[1] = off;
662         vec[2] = off;
663         mul_m4_v3(mat, vec);
664         v7 = BM_Make_Vert(bm, vec, NULL);
665         BMO_SetFlag(bm, v7, VERT_MARK);
666
667         vec[0] = off;
668         vec[1] = -off;
669         vec[2] = off;
670         mul_m4_v3(mat, vec);
671         v8 = BM_Make_Vert(bm, vec, NULL);
672         BMO_SetFlag(bm, v8, VERT_MARK);
673
674         /*the four sides*/
675         BM_Make_QuadTri(bm, v5, v6, v2, v1, NULL, 0);
676         BM_Make_QuadTri(bm, v6, v7, v3, v2, NULL, 0);
677         BM_Make_QuadTri(bm, v7, v8, v4, v3, NULL, 0);
678         BM_Make_QuadTri(bm, v8, v5, v1, v4, NULL, 0);
679         
680         /*top/bottom*/
681         BM_Make_QuadTri(bm, v1, v2, v3, v4, NULL, 0);
682         BM_Make_QuadTri(bm, v8, v7, v6, v5, NULL, 0);
683
684         BMO_Flag_To_Slot(bm, op, "vertout", VERT_MARK, BM_VERT);
685 }