svn merge ^/trunk/blender -r44235:44250
[blender.git] / source / blender / bmesh / operators / bmo_primitive.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Joseph Eagar.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include "MEM_guardedalloc.h"
24
25 #include "BLI_math.h"
26
27 #include "ED_mesh.h"
28
29 #include "bmesh.h"
30 #include "bmesh_private.h"
31
32
33
34 /* ************************ primitives ******************* */
35
36 static float icovert[12][3] = {
37         {0.0f,0.0f,-200.0f},
38         {144.72f, -105.144f,-89.443f},
39         {-55.277f, -170.128,-89.443f},
40         {-178.885f,0.0f,-89.443f},
41         {-55.277f,170.128f,-89.443f},
42         {144.72f,105.144f,-89.443f},
43         {55.277f,-170.128f,89.443f},
44         {-144.72f,-105.144f,89.443f},
45         {-144.72f,105.144f,89.443f},
46         {55.277f,170.128f,89.443f},
47         {178.885f,0.0f,89.443f},
48         {0.0f,0.0f,200.0f}
49 };
50
51 static short icoface[20][3] = {
52         {0,1,2},
53         {1,0,5},
54         {0,2,3},
55         {0,3,4},
56         {0,4,5},
57         {1,5,10},
58         {2,1,6},
59         {3,2,7},
60         {4,3,8},
61         {5,4,9},
62         {1,10,6},
63         {2,6,7},
64         {3,7,8},
65         {4,8,9},
66         {5,9,10},
67         {6,10,11},
68         {7,6,11},
69         {8,7,11},
70         {9,8,11},
71         {10,9,11}
72 };
73
74 // HACK: these can also be found in cmoview.tga.c, but are here so that they can be found by linker
75 // this hack is only used so that scons & mingw + split-sources hack works
76 // ------------------------------- start copied code
77 /* these are not the monkeys you are looking for */
78 static int monkeyo = 4;
79 static int monkeynv = 271;
80 static int monkeynf = 250;
81 static signed char monkeyv[271][3] = {
82         {-71,21,98},{-63,12,88},{-57,7,74},{-82,-3,79},{-82,4,92},
83         {-82,17,100},{-92,21,102},{-101,12,95},{-107,7,83},
84         {-117,31,84},{-109,31,95},{-96,31,102},{-92,42,102},
85         {-101,50,95},{-107,56,83},{-82,66,79},{-82,58,92},
86         {-82,46,100},{-71,42,98},{-63,50,88},{-57,56,74},
87         {-47,31,72},{-55,31,86},{-67,31,97},{-66,31,99},
88         {-70,43,100},{-82,48,103},{-93,43,105},{-98,31,105},
89         {-93,20,105},{-82,31,106},{-82,15,103},{-70,20,100},
90         {-127,55,95},{-127,45,105},{-127,-87,94},{-127,-41,100},
91         {-127,-24,102},{-127,-99,92},{-127,52,77},{-127,73,73},
92         {-127,115,-70},{-127,72,-109},{-127,9,-106},{-127,-49,-45},
93         {-101,-24,72},{-87,-56,73},{-82,-89,73},{-80,-114,68},
94         {-85,-121,67},{-104,-124,71},{-127,-126,74},{-71,-18,68},
95         {-46,-5,69},{-21,19,57},{-17,55,76},{-36,62,80},
96         {-64,77,88},{-86,97,94},{-107,92,97},{-119,63,96},
97         {-106,53,99},{-111,39,98},{-101,12,95},{-79,2,90},
98         {-64,8,86},{-47,24,83},{-45,38,83},{-50,48,85},
99         {-72,56,92},{-95,60,97},{-127,-98,94},{-113,-92,94},
100         {-112,-107,91},{-119,-113,89},{-127,-114,88},{-127,-25,96},
101         {-127,-18,95},{-114,-19,95},{-111,-29,96},{-116,-37,95},
102         {-76,-6,86},{-48,7,80},{-34,26,77},{-32,48,84},
103         {-39,53,93},{-71,70,102},{-87,82,107},{-101,79,109},
104         {-114,55,108},{-111,-13,104},{-100,-57,91},{-95,-90,88},
105         {-93,-105,85},{-97,-117,81},{-106,-119,81},{-127,-121,82},
106         {-127,6,93},{-127,27,98},{-85,61,95},{-106,18,96},
107         {-110,27,97},{-112,-88,94},{-117,-57,96},{-127,-57,96},
108         {-127,-42,95},{-115,-35,100},{-110,-29,102},{-113,-17,100},
109         {-122,-16,100},{-127,-26,106},{-121,-19,104},{-115,-20,104},
110         {-113,-29,106},{-117,-32,103},{-127,-37,103},{-94,-40,71},
111         {-106,-31,91},{-104,-40,91},{-97,-32,71},{-127,-112,88},
112         {-121,-111,88},{-115,-105,91},{-115,-95,93},{-127,-100,84},
113         {-115,-96,85},{-115,-104,82},{-121,-109,81},{-127,-110,81},
114         {-105,28,100},{-103,20,99},{-84,55,97},{-92,54,99},
115         {-73,51,99},{-55,45,89},{-52,37,88},{-53,25,87},
116         {-66,13,92},{-79,8,95},{-98,14,100},{-104,38,100},
117         {-100,48,100},{-97,46,97},{-102,38,97},{-96,16,97},
118         {-79,11,93},{-68,15,90},{-57,27,86},{-56,36,86},
119         {-59,43,87},{-74,50,96},{-91,51,98},{-84,52,96},
120         {-101,22,96},{-102,29,96},{-113,59,78},{-102,85,79},
121         {-84,88,76},{-65,71,71},{-40,58,63},{-25,52,59},
122         {-28,21,48},{-50,0,53},{-71,-12,60},{-127,115,37},
123         {-127,126,-10},{-127,-25,-86},{-127,-59,24},{-127,-125,59},
124         {-127,-103,44},{-127,-73,41},{-127,-62,36},{-18,30,7},
125         {-17,41,-6},{-28,34,-56},{-68,56,-90},{-33,-6,9},
126         {-51,-16,-21},{-45,-1,-55},{-84,7,-85},{-97,-45,52},
127         {-104,-53,33},{-90,-91,49},{-95,-64,50},{-85,-117,51},
128         {-109,-97,47},{-111,-69,46},{-106,-121,56},{-99,-36,55},
129         {-100,-29,60},{-101,-22,64},{-100,-50,21},{-89,-40,-34},
130         {-83,-19,-69},{-69,111,-49},{-69,119,-9},{-69,109,30},
131         {-68,67,55},{-34,52,43},{-46,58,36},{-45,90,7},
132         {-25,72,16},{-25,79,-15},{-45,96,-25},{-45,87,-57},
133         {-25,69,-46},{-48,42,-75},{-65,3,-70},{-22,42,-26},
134         {-75,-22,19},{-72,-25,-27},{-13,52,-30},{-28,-18,-16},
135         {6,-13,-42},{37,7,-55},{46,41,-54},{31,65,-54},
136         {4,61,-40},{3,53,-37},{25,56,-50},{35,37,-52},
137         {28,10,-52},{5,-5,-39},{-21,-9,-17},{-9,46,-28},
138         {-6,39,-37},{-14,-3,-27},{6,0,-47},{25,12,-57},
139         {31,32,-57},{23,46,-56},{4,44,-46},{-19,37,-27},
140         {-20,22,-35},{-30,12,-35},{-22,11,-35},{-19,2,-35},
141         {-23,-2,-35},{-34,0,-9},{-35,-3,-22},{-35,5,-24},
142         {-25,26,-27},{-13,31,-34},{-13,30,-41},{-23,-2,-41},
143         {-18,2,-41},{-21,10,-41},{-29,12,-41},{-19,22,-41},
144         {6,42,-53},{25,44,-62},{34,31,-63},{28,11,-62},
145         {7,0,-54},{-14,-2,-34},{-5,37,-44},{-13,14,-42},
146         {-7,8,-43},{1,16,-47},{-4,22,-45},{3,30,-48},
147         {8,24,-49},{15,27,-50},{12,35,-50},{4,56,-62},
148         {33,60,-70},{48,38,-64},{41,7,-68},{6,-11,-63},
149         {-26,-16,-42},{-17,49,-49},
150 };
151
152 static signed char monkeyf[250][4] = {
153         {27,4,5,26}, {25,4,5,24}, {3,6,5,4}, {1,6,5,2}, {5,6,7,4},
154         {3,6,7,2}, {5,8,7,6}, {3,8,7,4}, {7,8,9,6},
155         {5,8,9,4}, {7,10,9,8}, {5,10,9,6}, {9,10,11,8},
156         {7,10,11,6}, {9,12,11,10}, {7,12,11,8}, {11,6,13,12},
157         {5,4,13,12}, {3,-2,13,12}, {-3,-4,13,12}, {-5,-10,13,12},
158         {-11,-12,14,12}, {-13,-18,14,13}, {-19,4,5,13}, {10,12,4,4},
159         {10,11,9,9}, {8,7,9,9}, {7,5,6,6}, {6,3,4,4},
160         {5,1,2,2}, {4,-1,0,0}, {3,-3,-2,-2}, {22,67,68,23},
161         {20,65,66,21}, {18,63,64,19}, {16,61,62,17}, {14,59,60,15},
162         {12,19,48,57}, {18,19,48,47}, {18,19,48,47}, {18,19,48,47},
163         {18,19,48,47}, {18,19,48,47}, {18,19,48,47}, {18,19,48,47},
164         {18,19,48,47}, {18,-9,-8,47}, {18,27,45,46}, {26,55,43,44},
165         {24,41,42,54}, {22,39,40,23}, {20,37,38,21}, {18,35,36,19},
166         {16,33,34,17}, {14,31,32,15}, {12,39,30,13}, {11,48,45,38},
167         {8,36,-19,9}, {8,-20,44,47}, {42,45,46,43}, {18,19,40,39},
168         {16,17,38,37}, {14,15,36,35}, {32,44,43,33}, {12,33,32,42},
169         {19,44,43,42}, {40,41,42,-27}, {8,9,39,-28}, {15,43,42,16},
170         {13,43,42,14}, {11,43,42,12}, {9,-30,42,10}, {37,12,38,-32},
171         {-33,37,45,46}, {-33,40,41,39}, {38,40,41,37}, {36,40,41,35},
172         {34,40,41,33}, {36,39,38,37}, {35,40,39,38}, {1,2,14,21},
173         {1,2,40,13}, {1,2,40,39}, {1,24,12,39}, {-34,36,38,11},
174         {35,38,36,37}, {-37,8,35,37}, {-11,-12,-45,40}, {-11,-12,39,38},
175         {-11,-12,37,36}, {-11,-12,35,34}, {33,34,40,41}, {33,34,38,39},
176         {33,34,36,37}, {33,-52,34,35}, {33,37,36,34}, {33,35,34,34},
177         {8,7,37,36}, {-32,7,35,46}, {-34,-33,45,46}, {4,-33,43,34},
178         {-34,-33,41,42}, {-34,-33,39,40}, {-34,-33,37,38}, {-34,-33,35,36},
179         {-34,-33,33,34}, {-34,-33,31,32}, {-34,-4,28,30}, {-5,-34,28,27},
180         {-35,-44,36,27}, {26,35,36,45}, {24,25,44,45}, {25,23,44,42},
181         {25,24,41,40}, {25,24,39,38}, {25,24,37,36}, {25,24,35,34},
182         {25,24,33,32}, {25,24,31,30}, {15,24,29,38}, {25,24,27,26},
183         {23,12,37,26}, {11,12,35,36}, {-86,-59,36,-80}, {-60,-61,36,35},
184         {-62,-63,36,35}, {-64,-65,36,35}, {-66,-67,36,35}, {-68,-69,36,35},
185         {-70,-71,36,35}, {-72,-73,36,35}, {-74,-75,36,35}, {42,43,53,58},
186         {40,41,57,56}, {38,39,55,57}, {-81,-80,37,56}, {-83,-82,55,52},
187         {-85,-84,51,49}, {-87,-86,48,49}, {47,50,51,48}, {46,48,51,49},
188         {43,46,49,44}, {-92,-91,45,42}, {-23,49,50,-20}, {-94,40,48,-24},
189         {-96,-22,48,49}, {-97,48,21,-90}, {-100,36,50,23}, {22,49,48,-100},
190         {-101,47,46,22}, {21,45,35,25}, {33,34,44,41}, {13,14,28,24},
191         {-107,26,30,-106}, {14,46,45,15}, {14,44,43,-110}, {-111,42,23,-110},
192         {6,7,45,46}, {45,44,47,46}, {45,46,47,48}, {47,46,49,48},
193         {17,49,47,48}, {17,36,46,48}, {35,36,44,45}, {35,36,40,43},
194         {35,36,38,39}, {-4,-3,37,35}, {-123,34,33,1}, {-9,-8,-7,-6},
195         {-10,-7,32,-125}, {-127,-11,-126,-126}, {-7,-6,5,31}, {4,5,33,30},
196         {4,39,33,32}, {4,35,32,38}, {20,21,39,38}, {4,37,38,5},
197         {-11,-10,36,3}, {-11,15,14,35}, {13,16,34,34}, {-13,14,13,13},
198         {-3,1,30,29}, {-3,28,29,1}, {-2,31,28,-1}, {12,13,27,30},
199         {-2,26,12,12}, {35,29,42,36}, {34,35,36,33}, {32,35,36,31},
200         {30,35,36,29}, {28,35,36,27}, {26,35,36,25}, {34,39,38,35},
201         {32,39,38,33}, {30,39,38,31}, {28,39,38,29}, {26,39,38,27},
202         {25,31,32,38}, {-18,-17,45,44}, {-18,17,28,44}, {-24,-20,42,-23},
203         {11,35,27,14}, {25,28,39,41}, {37,41,40,38}, {34,40,36,35},
204         {32,40,39,33}, {30,39,31,40}, {21,29,39,22}, {-31,37,28,4},
205         {-32,33,35,36}, {32,33,34,34}, {18,35,36,48}, {34,25,40,35},
206         {24,25,38,39}, {24,25,36,37}, {24,25,34,35}, {24,25,32,33},
207         {24,13,41,31}, {17,11,41,35}, {15,16,34,35}, {13,14,34,35},
208         {11,12,34,35}, {9,10,34,35}, {7,8,34,35}, {26,25,37,36},
209         {35,36,37,38}, {37,36,39,38}, {37,38,39,40}, {25,31,36,39},
210         {18,34,35,30}, {17,22,30,33}, {19,29,21,20}, {16,26,29,17},
211         {24,29,28,25}, {22,31,28,23}, {20,31,30,21}, {18,31,30,19},
212         {16,30,17,17}, {-21,-22,35,34}, {-21,-22,33,32}, {-21,-22,31,30},
213         {-21,-22,29,28}, {-21,-22,27,26}, {-28,-22,25,31}, {24,28,29,30},
214         {23,24,26,27}, {23,24,25,25}, {-69,-35,-32,27}, {-70,26,25,-66},
215         {-68,-67,24,-33},
216 };
217
218 #define VERT_MARK       1
219
220 #define EDGE_ORIG       1
221 #define EDGE_MARK       2
222
223 #define FACE_MARK       1
224 #define FACE_NEW        2
225
226 void bmesh_create_grid_exec(BMesh *bm, BMOperator *op)
227 {
228         BMOperator bmop, prevop;
229         BMVert *eve, *preveve;
230         BMEdge *e;
231         float vec[3], mat[4][4], phi, phid, dia = BMO_slot_float_get(op, "size");
232         int a, tot = BMO_slot_int_get(op, "xsegments"), seg = BMO_slot_int_get(op, "ysegments");
233
234         if (tot < 2) tot = 2;
235         if (seg < 2) seg = 2;
236
237         BMO_slot_mat4_get(op, "mat", mat);
238
239         /* one segment first: the X axis */
240         phi = 1.0f;
241         phid = 2.0f / ((float)tot - 1);
242         for (a = 0; a < tot; a++) {
243                 vec[0] = dia * phi;
244                 vec[1] = -dia;
245                 vec[2] = 0.0f;
246                 mul_m4_v3(mat, vec);
247
248                 eve = BM_vert_create(bm, vec, NULL);
249                 BMO_elem_flag_enable(bm, eve, VERT_MARK);
250
251                 if (a) {
252                         e = BM_edge_create(bm, preveve, eve, NULL, TRUE);
253                         BMO_elem_flag_enable(bm, e, EDGE_ORIG);
254                 }
255
256                 preveve = eve;
257                 phi -= phid;
258         }
259
260         /* extrude and translate */
261         vec[0] = vec[2] = 0.0f;
262         vec[1] = dia * phid;
263         mul_mat3_m4_v3(mat, vec);
264
265         for (a = 0; a < seg - 1; a++) {
266                 if (a) {
267                         BMO_op_initf(bm, &bmop, "extrude_edge_only edges=%s", &prevop, "geomout");
268                         BMO_op_exec(bm, &bmop);
269                         BMO_op_finish(bm, &prevop);
270
271                         BMO_slot_buffer_flag_enable(bm, &bmop, "geomout", VERT_MARK, BM_VERT);
272                 }
273                 else {
274                         BMO_op_initf(bm, &bmop, "extrude_edge_only edges=%fe", EDGE_ORIG);
275                         BMO_op_exec(bm, &bmop);
276                         BMO_slot_buffer_flag_enable(bm, &bmop, "geomout", VERT_MARK, BM_VERT);
277                 }
278
279                 BMO_op_callf(bm, "translate vec=%v verts=%s", vec, &bmop, "geomout");
280                 prevop = bmop;
281         }
282
283         if (a)
284                 BMO_op_finish(bm, &bmop);
285
286         BMO_slot_from_flag(bm, op, "vertout", VERT_MARK, BM_VERT);
287 }
288
289 void bmesh_create_uvsphere_exec(BMesh *bm, BMOperator *op)
290 {
291         BMOperator bmop, prevop;
292         BMVert *eve, *preveve;
293         BMEdge *e;
294         BMIter iter;
295         float vec[3], mat[4][4], cmat[3][3], phi, q[4];
296         float phid, dia = BMO_slot_float_get(op, "diameter");
297         int a, seg = BMO_slot_int_get(op, "segments"), tot = BMO_slot_int_get(op, "revolutions");
298
299         BMO_slot_mat4_get(op, "mat", mat);
300
301         phid = 2.0f * (float)M_PI / tot;
302         phi = 0.25f * (float)M_PI;
303
304         /* one segment first */
305         phi = 0;
306         phid /= 2;
307         for (a = 0; a <= tot; a++) {
308                 /* Going in this direction, then edge extruding, makes normals face outward */
309                 vec[0] = -dia * sinf(phi);
310                 vec[1] = 0.0;
311                 vec[2] = dia * cosf(phi);
312                 eve = BM_vert_create(bm, vec, NULL);
313                 BMO_elem_flag_enable(bm, eve, VERT_MARK);
314
315                 if (a != 0) {
316                         e = BM_edge_create(bm, preveve, eve, NULL, FALSE);
317                         BMO_elem_flag_enable(bm, e, EDGE_ORIG);
318                 }
319
320                 phi+= phid;
321                 preveve = eve;
322         }
323
324         /* extrude and rotate; negative phi to make normals face outward */
325         phi = -M_PI / seg;
326         q[0] = cosf(phi);
327         q[3] = sinf(phi);
328         q[1] = q[2] = 0.0f;
329         quat_to_mat3(cmat, q);
330
331         for (a = 0; a < seg; a++) {
332                 if (a) {
333                         BMO_op_initf(bm, &bmop, "extrude_edge_only edges=%s", &prevop, "geomout");
334                         BMO_op_exec(bm, &bmop);
335                         BMO_op_finish(bm, &prevop);
336                 }
337                 else {
338                         BMO_op_initf(bm, &bmop, "extrude_edge_only edges=%fe", EDGE_ORIG);
339                         BMO_op_exec(bm, &bmop);
340                 }
341
342                 BMO_slot_buffer_flag_enable(bm, &bmop, "geomout", VERT_MARK, BM_VERT);
343                 BMO_op_callf(bm, "rotate cent=%v mat=%m3 verts=%s", vec, cmat, &bmop, "geomout");
344                 
345                 prevop = bmop;
346         }
347
348         if (a)
349                 BMO_op_finish(bm, &bmop);
350
351         {
352                 float len, len2, vec2[3];
353
354                 len= 2*dia*sinf(phid / 2.0f);
355
356                 /* length of one segment in shortest parallen */
357                 vec[0]= dia*sinf(phid);
358                 vec[1]= 0.0;
359                 vec[2]= dia*cosf(phid);
360
361                 mul_v3_m3v3(vec2, cmat, vec);
362                 len2= len_v3v3(vec, vec2);
363
364                 /* use shortest segment length divided by 3 as merge threshold */
365                 BMO_op_callf(bm, "removedoubles verts=%fv dist=%f", VERT_MARK, MIN2(len, len2) / 3.0f);
366         }
367
368         /* and now do imat */
369         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
370                 if (BMO_elem_flag_test(bm, eve, VERT_MARK)) {
371                         mul_m4_v3(mat, eve->co);
372                 }
373         }
374
375         BMO_slot_from_flag(bm, op, "vertout", VERT_MARK, BM_VERT);
376 }
377
378 void bmesh_create_icosphere_exec(BMesh *bm, BMOperator *op)
379 {
380         BMVert *eva[12];
381         BMVert *v;
382         BMIter liter;
383         BMIter viter;
384         BMLoop *l;
385         float vec[3], mat[4][4] /* , phi, phid */;
386         float dia = BMO_slot_float_get(op, "diameter");
387         int a, subdiv = BMO_slot_int_get(op, "subdivisions");
388
389         BMO_slot_mat4_get(op, "mat", mat);
390
391         /* phid = 2.0f * (float)M_PI / subdiv; */ /* UNUSED */
392         /* phi = 0.25f * (float)M_PI; */         /* UNUSED */
393
394         dia /= 200.0f;
395         for (a = 0; a < 12; a++) {
396                 vec[0] = dia * icovert[a][0];
397                 vec[1] = dia * icovert[a][1];
398                 vec[2] = dia * icovert[a][2];
399                 eva[a] = BM_vert_create(bm, vec, NULL);
400
401                 BMO_elem_flag_enable(bm, eva[a], VERT_MARK);
402         }
403
404         for (a = 0; a < 20; a++) {
405                 BMFace *eftemp;
406                 BMVert *v1, *v2, *v3;
407
408                 v1 = eva[icoface[a][0]];
409                 v2 = eva[icoface[a][1]];
410                 v3 = eva[icoface[a][2]];
411
412                 eftemp = BM_face_create_quad_tri(bm, v1, v2, v3, NULL, NULL, FALSE);
413                 
414                 BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, eftemp) {
415                         BMO_elem_flag_enable(bm, l->e, EDGE_MARK);
416                 }
417
418                 BMO_elem_flag_enable(bm, eftemp, FACE_MARK);
419         }
420
421         dia *= 200.0f;
422
423         for (a = 1; a < subdiv; a++) {
424                 BMOperator bmop;
425
426                 BMO_op_initf(bm, &bmop,
427                              "esubd edges=%fe smooth=%f numcuts=%i gridfill=%i beauty=%i",
428                              EDGE_MARK, dia, 1, 1, B_SPHERE);
429                 BMO_op_exec(bm, &bmop);
430                 BMO_slot_buffer_flag_enable(bm, &bmop, "geomout", VERT_MARK, BM_VERT);
431                 BMO_slot_buffer_flag_enable(bm, &bmop, "geomout", EDGE_MARK, BM_EDGE);
432                 BMO_op_finish(bm, &bmop);
433         }
434
435         /* must transform after becayse of sphere subdivision */
436         BM_ITER(v, &viter, bm, BM_VERTS_OF_MESH, NULL) {
437                 if (BMO_elem_flag_test(bm, v, VERT_MARK)) {
438                         mul_m4_v3(mat, v->co);
439                 }
440         }
441
442         BMO_slot_from_flag(bm, op, "vertout", VERT_MARK, BM_VERT);
443 }
444
445 void bmesh_create_monkey_exec(BMesh *bm, BMOperator *op)
446 {
447         BMVert *eve;
448         BMVert **tv = MEM_mallocN(sizeof(*tv)*monkeynv * 2, "tv");
449         float mat[4][4];
450         int i;
451
452         BMO_slot_mat4_get(op, "mat", mat);
453
454         for (i = 0; i < monkeynv; i++) {
455                 float v[3];
456
457                 v[0] = (monkeyv[i][0] + 127) / 128.0, v[1] = monkeyv[i][1] / 128.0, v[2] = monkeyv[i][2] / 128.0;
458
459                 tv[i] = BM_vert_create(bm, v, NULL);
460                 BMO_elem_flag_enable(bm, tv[i], VERT_MARK);
461
462                 tv[monkeynv + i] = (fabsf(v[0] = -v[0]) < 0.001f) ?
463                             tv[i] :
464                             (eve = BM_vert_create(bm, v, NULL), mul_m4_v3(mat, eve->co), eve);
465
466                 BMO_elem_flag_enable(bm, tv[monkeynv + i], VERT_MARK);
467
468                 mul_m4_v3(mat, tv[i]->co);
469         }
470
471         for (i = 0; i < monkeynf; i++) {
472                 BM_face_create_quad_tri(bm,
473                                         tv[monkeyf[i][0] + i - monkeyo],
474                                         tv[monkeyf[i][1] + i - monkeyo],
475                                         tv[monkeyf[i][2] + i - monkeyo],
476                                         (monkeyf[i][3] != monkeyf[i][2]) ? tv[monkeyf[i][3] + i - monkeyo] : NULL,
477                                         NULL, FALSE);
478
479                 BM_face_create_quad_tri(bm,
480                                         tv[monkeynv + monkeyf[i][2] + i - monkeyo],
481                                         tv[monkeynv + monkeyf[i][1] + i - monkeyo],
482                                         tv[monkeynv + monkeyf[i][0] + i - monkeyo],
483                                         (monkeyf[i][3] != monkeyf[i][2]) ? tv[monkeynv + monkeyf[i][3] + i - monkeyo]: NULL,
484                                         NULL, FALSE);
485         }
486
487         MEM_freeN(tv);
488
489         BMO_slot_from_flag(bm, op, "vertout", VERT_MARK, BM_VERT);
490 }
491
492
493 void bmesh_create_circle_exec(BMesh *bm, BMOperator *op)
494 {
495         BMVert *v1, *lastv1 = NULL, *cent1, *firstv1 = NULL;
496         float vec[3], mat[4][4], phi, phid;
497         float dia = BMO_slot_float_get(op, "diameter");
498         int cap_ends = BMO_slot_int_get(op, "cap_ends"), segs = BMO_slot_int_get(op, "segments");
499         int cap_tris = BMO_slot_int_get(op, "cap_tris");
500         int a;
501         
502         if (!segs)
503                 return;
504         
505         BMO_slot_mat4_get(op, "mat", mat);
506
507         phid = 2.0f * (float)M_PI / segs;
508         phi = .25f * (float)M_PI;
509
510         if (cap_ends) {
511                 vec[0] = vec[1] = 0.0f;
512                 vec[2] = 0.0;
513                 mul_m4_v3(mat, vec);
514                 
515                 cent1 = BM_vert_create(bm, vec, NULL);
516         }
517
518         for (a = 0; a < segs; a++, phi += phid) {
519                 /* Going this way ends up with normal(s) upward */
520                 vec[0] = -dia * sinf(phi);
521                 vec[1] = dia * cosf(phi);
522                 vec[2] = 0.0f;
523                 mul_m4_v3(mat, vec);
524                 v1 = BM_vert_create(bm, vec, NULL);
525
526                 BMO_elem_flag_enable(bm, v1, VERT_MARK);
527                 
528                 if (lastv1)
529                         BM_edge_create(bm, v1, lastv1, NULL, FALSE);
530                 
531                 if (a && cap_ends) {
532                         BMFace *f;
533                         
534                         f = BM_face_create_quad_tri(bm, cent1, lastv1, v1, NULL, NULL, FALSE);
535                         BMO_elem_flag_enable(bm, f, FACE_NEW);
536                 }
537                 
538                 if (!firstv1)
539                         firstv1 = v1;
540
541                 lastv1 = v1;
542         }
543
544         if (!a)
545                 return;
546
547         BM_edge_create(bm, lastv1, firstv1, NULL, FALSE);
548
549         if (cap_ends) {
550                 BMFace *f;
551                 
552                 f = BM_face_create_quad_tri(bm, cent1, v1, firstv1, NULL, NULL, FALSE);
553                 BMO_elem_flag_enable(bm, f, FACE_NEW);
554         }
555         
556         if (!cap_tris) {
557                 BMO_op_callf(bm, "dissolvefaces faces=%ff", FACE_NEW);
558         }
559         
560         BMO_slot_from_flag(bm, op, "vertout", VERT_MARK, BM_VERT);
561 }
562
563 void bmesh_create_cone_exec(BMesh *bm, BMOperator *op)
564 {
565         BMVert *v1, *v2, *lastv1 = NULL, *lastv2 = NULL, *cent1, *cent2, *firstv1, *firstv2;
566         float vec[3], mat[4][4], phi, phid;
567         float dia1 = BMO_slot_float_get(op, "diameter1");
568         float dia2 = BMO_slot_float_get(op, "diameter2");
569         float depth = BMO_slot_float_get(op, "depth");
570         int cap_ends = BMO_slot_int_get(op, "cap_ends"), segs = BMO_slot_int_get(op, "segments");
571         int cap_tris = BMO_slot_int_get(op, "cap_tris");
572         int a;
573         
574         if (!segs)
575                 return;
576         
577         BMO_slot_mat4_get(op, "mat", mat);
578
579         phid = 2.0f * (float)M_PI / segs;
580         phi = 0.25f * (float)M_PI;
581
582         depth *= 0.5f;
583         if (cap_ends) {
584                 vec[0] = vec[1] = 0.0f;
585                 vec[2] = -depth;
586                 mul_m4_v3(mat, vec);
587                 
588                 cent1 = BM_vert_create(bm, vec, NULL);
589
590                 vec[0] = vec[1] = 0.0f;
591                 vec[2] = depth;
592                 mul_m4_v3(mat, vec);
593                 
594                 cent2 = BM_vert_create(bm, vec, NULL);
595
596                 BMO_elem_flag_enable(bm, cent1, VERT_MARK);
597                 BMO_elem_flag_enable(bm, cent2, VERT_MARK);
598         }
599
600         for (a = 0; a < segs; a++, phi += phid) {
601                 vec[0] = dia1 * sinf(phi);
602                 vec[1] = dia1 * cosf(phi);
603                 vec[2] = -depth;
604                 mul_m4_v3(mat, vec);
605                 v1 = BM_vert_create(bm, vec, NULL);
606
607                 vec[0] = dia2 * sinf(phi);
608                 vec[1] = dia2 * cosf(phi);
609                 vec[2] = depth;
610                 mul_m4_v3(mat, vec);
611                 v2 = BM_vert_create(bm, vec, NULL);
612
613                 BMO_elem_flag_enable(bm, v1, VERT_MARK);
614                 BMO_elem_flag_enable(bm, v2, VERT_MARK);
615
616                 if (a) {
617                         if (cap_ends) {
618                                 BMFace *f;
619                                 
620                                 f = BM_face_create_quad_tri(bm, cent1, lastv1, v1, NULL, NULL, FALSE);
621                                 BMO_elem_flag_enable(bm, f, FACE_NEW);
622                                 f = BM_face_create_quad_tri(bm, cent2, v2, lastv2, NULL, NULL, FALSE);
623                                 BMO_elem_flag_enable(bm, f, FACE_NEW);
624                         }
625                         BM_face_create_quad_tri(bm, lastv1, lastv2, v2, v1, NULL, FALSE);
626                 }
627                 else {
628                         firstv1 = v1;
629                         firstv2 = v2;
630                 }
631
632                 lastv1 = v1;
633                 lastv2 = v2;
634         }
635
636         if (!a)
637                 return;
638
639         if (cap_ends) {
640                 BMFace *f;
641                 
642                 f = BM_face_create_quad_tri(bm, cent1, v1, firstv1, NULL, NULL, FALSE);
643                 BMO_elem_flag_enable(bm, f, FACE_NEW);
644                 f = BM_face_create_quad_tri(bm, cent2, firstv2, v2, NULL, NULL, FALSE);
645                 BMO_elem_flag_enable(bm, f, FACE_NEW);
646         }
647         
648         if (!cap_tris) {
649                 BMO_op_callf(bm, "dissolvefaces faces=%ff", FACE_NEW);
650         }
651         
652         BM_face_create_quad_tri(bm, v1, v2, firstv2, firstv1, NULL, FALSE);
653
654         BMO_op_callf(bm, "removedoubles verts=%fv dist=%f", VERT_MARK, 0.000001);
655         BMO_slot_from_flag(bm, op, "vertout", VERT_MARK, BM_VERT);
656 }
657
658 void bmesh_create_cube_exec(BMesh *bm, BMOperator *op)
659 {
660         BMVert *v1, *v2, *v3, *v4, *v5, *v6, *v7, *v8;
661         float vec[3], mat[4][4], off = BMO_slot_float_get(op, "size") / 2.0f;
662
663         BMO_slot_mat4_get(op, "mat", mat);
664
665         if (!off) off = 0.5f;
666
667         vec[0] = -off;
668         vec[1] = -off;
669         vec[2] = -off;
670         mul_m4_v3(mat, vec);
671         v1 = BM_vert_create(bm, vec, NULL);
672         BMO_elem_flag_enable(bm, v1, VERT_MARK);
673
674         vec[0] = -off;
675         vec[1] = off;
676         vec[2] = -off;
677         mul_m4_v3(mat, vec);
678         v2 = BM_vert_create(bm, vec, NULL);
679         BMO_elem_flag_enable(bm, v2, VERT_MARK);
680
681         vec[0] = off;
682         vec[1] = off;
683         vec[2] = -off;
684         mul_m4_v3(mat, vec);
685         v3 = BM_vert_create(bm, vec, NULL);
686         BMO_elem_flag_enable(bm, v3, VERT_MARK);
687
688         vec[0] = off;
689         vec[1] = -off;
690         vec[2] = -off;
691         mul_m4_v3(mat, vec);
692         v4 = BM_vert_create(bm, vec, NULL);
693         BMO_elem_flag_enable(bm, v4, VERT_MARK);
694
695         vec[0] = -off;
696         vec[1] = -off;
697         vec[2] = off;
698         mul_m4_v3(mat, vec);
699         v5 = BM_vert_create(bm, vec, NULL);
700         BMO_elem_flag_enable(bm, v5, VERT_MARK);
701
702         vec[0] = -off;
703         vec[1] = off;
704         vec[2] = off;
705         mul_m4_v3(mat, vec);
706         v6 = BM_vert_create(bm, vec, NULL);
707         BMO_elem_flag_enable(bm, v6, VERT_MARK);
708
709         vec[0] = off;
710         vec[1] = off;
711         vec[2] = off;
712         mul_m4_v3(mat, vec);
713         v7 = BM_vert_create(bm, vec, NULL);
714         BMO_elem_flag_enable(bm, v7, VERT_MARK);
715
716         vec[0] = off;
717         vec[1] = -off;
718         vec[2] = off;
719         mul_m4_v3(mat, vec);
720         v8 = BM_vert_create(bm, vec, NULL);
721         BMO_elem_flag_enable(bm, v8, VERT_MARK);
722
723         /* the four sides */
724         BM_face_create_quad_tri(bm, v5, v6, v2, v1, NULL, FALSE);
725         BM_face_create_quad_tri(bm, v6, v7, v3, v2, NULL, FALSE);
726         BM_face_create_quad_tri(bm, v7, v8, v4, v3, NULL, FALSE);
727         BM_face_create_quad_tri(bm, v8, v5, v1, v4, NULL, FALSE);
728         
729         /* top/bottom */
730         BM_face_create_quad_tri(bm, v1, v2, v3, v4, NULL, FALSE);
731         BM_face_create_quad_tri(bm, v8, v7, v6, v5, NULL, FALSE);
732
733         BMO_slot_from_flag(bm, op, "vertout", VERT_MARK, BM_VERT);
734 }