Fix #31139: fractal mesh subdivide was only working along normal where previously
[blender.git] / source / blender / bmesh / operators / bmo_subdivide.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 /** \file blender/bmesh/operators/bmo_subdivide.c
24  *  \ingroup bmesh
25  */
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_math.h"
30 #include "BLI_rand.h"
31 #include "BLI_array.h"
32 #include "BLI_noise.h"
33
34 #include "BKE_customdata.h"
35
36 #include "DNA_object_types.h"
37
38 #include "bmesh.h"
39 #include "intern/bmesh_private.h"
40
41 #include "intern/bmesh_operators_private.h" /* own include */
42
43 #include "bmo_subdivide.h" /* own include */
44
45 /* flags for all elements share a common bitfield space */
46 #define SUBD_SPLIT      1
47
48 #define EDGE_PERCENT    2
49
50 /* I don't think new faces are flagged, currently, but
51  * better safe than sorry. */
52 #define FACE_CUSTOMFILL 4
53 #define ELE_INNER       8
54 #define ELE_SPLIT       16
55
56 /*
57  * NOTE: beauty has been renamed to flag!
58  */
59
60 /* generic subdivision rules:
61  *
62  * - two selected edges in a face should make a link
63  *   between them.
64  *
65  * - one edge should do, what? make pretty topology, or just
66  *   split the edge only?
67  */
68
69 /* connects face with smallest len, which I think should always be correct for
70  * edge subdivision */
71 static BMEdge *connect_smallest_face(BMesh *bm, BMVert *v1, BMVert *v2, BMFace **r_nf)
72 {
73         BMIter iter, iter2;
74         BMVert *v;
75         BMLoop *nl;
76         BMFace *face, *curf = NULL;
77
78         /* this isn't the best thing in the world.  it doesn't handle cases where there's
79          * multiple faces yet.  that might require a convexity test to figure out which
80          * face is "best" and who knows what for non-manifold conditions. */
81         for (face = BM_iter_new(&iter, bm, BM_FACES_OF_VERT, v1); face; face = BM_iter_step(&iter)) {
82                 for (v = BM_iter_new(&iter2, bm, BM_VERTS_OF_FACE, face); v; v = BM_iter_step(&iter2)) {
83                         if (v == v2) {
84                                 if (!curf || face->len < curf->len) curf = face;
85                         }
86                 }
87         }
88
89         if (curf) {
90                 face = BM_face_split(bm, curf, v1, v2, &nl, NULL, FALSE);
91                 
92                 if (r_nf) *r_nf = face;
93                 return nl ? nl->e : NULL;
94         }
95
96         return NULL;
97 }
98 /* calculates offset for co, based on fractal, sphere or smooth settings  */
99 static void alter_co(BMesh *bm, BMVert *v, BMEdge *UNUSED(origed), const SubDParams *params, float perc,
100                      BMVert *vsta, BMVert *vend)
101 {
102         float tvec[3], prev_co[3], fac;
103         float *co = NULL;
104         int i, totlayer = CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY);
105         
106         BM_vert_normal_update_all(v);
107
108         co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, CD_SHAPEKEY, params->origkey);
109         copy_v3_v3(co, v->co);
110         copy_v3_v3(prev_co, co);
111
112         if (UNLIKELY(params->use_sphere)) { /* subdivide sphere */
113                 normalize_v3(co);
114                 mul_v3_fl(co, params->smooth);
115         }
116         else if (params->use_smooth) {
117                 /* we calculate an offset vector vec1[], to be added to *co */
118                 float len, nor[3], nor1[3], nor2[3], smooth = params->smooth;
119
120                 sub_v3_v3v3(nor, vsta->co, vend->co);
121                 len = 0.5f * normalize_v3(nor);
122
123                 copy_v3_v3(nor1, vsta->no);
124                 copy_v3_v3(nor2, vend->no);
125
126                 /* cosine angle */
127                 fac = dot_v3v3(nor, nor1);
128                 mul_v3_v3fl(tvec, nor1, fac);
129
130                 /* cosine angle */
131                 fac = -dot_v3v3(nor, nor2);
132                 madd_v3_v3fl(tvec, nor2, fac);
133
134                 /* falloff for multi subdivide */
135                 smooth *= sqrtf(fabsf(1.0f - 2.0f * fabsf(0.5f-perc)));
136
137                 mul_v3_fl(tvec, smooth * len);
138
139                 add_v3_v3(co, tvec);
140         }
141
142         if (params->use_fractal) {
143                 float len = len_v3v3(vsta->co, vend->co);
144                 float normal[3] = {0.0f, 0.0f, 0.0f}, co2[3], base1[3], base2[3];
145
146                 fac = params->fractal * len;
147
148                 mid_v3_v3v3(normal, vsta->no, vend->no);
149                 ortho_basis_v3v3_v3(base1, base2, normal);
150
151                 add_v3_v3v3(co2, v->co, params->off);
152                 mul_v3_fl(co2, 10.0f);
153
154                 tvec[0] = fac * (BLI_gTurbulence(1.0, co2[0], co2[1], co2[2], 15, 0, 2) - 0.5f);
155                 tvec[1] = fac * (BLI_gTurbulence(1.0, co2[1], co2[0], co2[2], 15, 0, 2) - 0.5f);
156                 tvec[2] = fac * (BLI_gTurbulence(1.0, co2[1], co2[2], co2[0], 15, 0, 2) - 0.5f);
157
158                 /* add displacement */
159                 madd_v3_v3fl(co, normal, tvec[0]);
160                 madd_v3_v3fl(co, base1, tvec[1] * (1.0f - params->along_normal));
161                 madd_v3_v3fl(co, base2, tvec[2] * (1.0f - params->along_normal));
162         }
163
164         /* apply the new difference to the rest of the shape keys,
165          * note that this doent take rotations into account, we _could_ support
166          * this by getting the normals and coords for each shape key and
167          * re-calculate the smooth value for each but this is quite involved.
168          * for now its ok to simply apply the difference IMHO - campbell */
169         sub_v3_v3v3(tvec, prev_co, co);
170
171         for (i = 0; i < totlayer; i++) {
172                 if (params->origkey != i) {
173                         co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, CD_SHAPEKEY, i);
174                         sub_v3_v3(co, tvec);
175                 }
176         }
177
178 }
179
180 /* assumes in the edge is the correct interpolated vertices already */
181 /* percent defines the interpolation, rad and flag are for special options */
182 /* results in new vertex with correct coordinate, vertex normal and weight group info */
183 static BMVert *bm_subdivide_edge_addvert(BMesh *bm, BMEdge *edge, BMEdge *oedge,
184                                          const SubDParams *params, float percent,
185                                          float percent2,
186                                          BMEdge **out, BMVert *vsta, BMVert *vend)
187 {
188         BMVert *ev;
189         
190         ev = BM_edge_split(bm, edge, edge->v1, out, percent);
191
192         BMO_elem_flag_enable(bm, ev, ELE_INNER);
193
194         /* offset for smooth or sphere or fractal */
195         alter_co(bm, ev, oedge, params, percent2, vsta, vend);
196
197 #if 0 //BMESH_TODO
198         /* clip if needed by mirror modifier */
199         if (edge->v1->f2) {
200                 if (edge->v1->f2 & edge->v2->f2 & 1) {
201                         co[0] = 0.0f;
202                 }
203                 if (edge->v1->f2 & edge->v2->f2 & 2) {
204                         co[1] = 0.0f;
205                 }
206                 if (edge->v1->f2 & edge->v2->f2 & 4) {
207                         co[2] = 0.0f;
208                 }
209         }
210 #endif
211         
212         interp_v3_v3v3(ev->no, vsta->no, vend->no, percent2);
213         normalize_v3(ev->no);
214
215         return ev;
216 }
217
218 static BMVert *subdivideedgenum(BMesh *bm, BMEdge *edge, BMEdge *oedge,
219                                 int curpoint, int totpoint, const SubDParams *params,
220                                 BMEdge **newe, BMVert *vsta, BMVert *vend)
221 {
222         BMVert *ev;
223         float percent, percent2 = 0.0f;
224
225         if (BMO_elem_flag_test(bm, edge, EDGE_PERCENT) && totpoint == 1)
226                 percent = BMO_slot_map_float_get(bm, params->op, "edgepercents", edge);
227         else {
228                 percent = 1.0f / (float)(totpoint + 1-curpoint);
229                 percent2 = (float)(curpoint + 1) / (float)(totpoint + 1);
230
231         }
232         
233         ev = bm_subdivide_edge_addvert(bm, edge, oedge, params, percent,
234                                        percent2, newe, vsta, vend);
235         return ev;
236 }
237
238 static void bm_subdivide_multicut(BMesh *bm, BMEdge *edge, const SubDParams *params,
239                                   BMVert *vsta, BMVert *vend)
240 {
241         BMEdge *eed = edge, *newe, temp = *edge;
242         BMVert *v, ov1 = *edge->v1, ov2 = *edge->v2, *v1 = edge->v1, *v2 = edge->v2;
243         int i, numcuts = params->numcuts;
244
245         temp.v1 = &ov1;
246         temp.v2 = &ov2;
247         
248         for (i = 0; i < numcuts; i++) {
249                 v = subdivideedgenum(bm, eed, &temp, i, params->numcuts, params, &newe, vsta, vend);
250
251                 BMO_elem_flag_enable(bm, v, SUBD_SPLIT);
252                 BMO_elem_flag_enable(bm, eed, SUBD_SPLIT);
253                 BMO_elem_flag_enable(bm, newe, SUBD_SPLIT);
254
255                 BMO_elem_flag_enable(bm, v, ELE_SPLIT);
256                 BMO_elem_flag_enable(bm, eed, ELE_SPLIT);
257                 BMO_elem_flag_enable(bm, newe, SUBD_SPLIT);
258
259                 BM_CHECK_ELEMENT(v);
260                 if (v->e) BM_CHECK_ELEMENT(v->e);
261                 if (v->e && v->e->l) BM_CHECK_ELEMENT(v->e->l->f);
262         }
263         
264         alter_co(bm, v1, &temp, params, 0, &ov1, &ov2);
265         alter_co(bm, v2, &temp, params, 1.0, &ov1, &ov2);
266 }
267
268 /* note: the patterns are rotated as necessary to
269  * match the input geometry.  they're based on the
270  * pre-split state of the  face */
271
272 /*
273  *  v3---------v2
274  *  |          |
275  *  |          |
276  *  |          |
277  *  |          |
278  *  v4---v0---v1
279  */
280 static void quad_1edge_split(BMesh *bm, BMFace *UNUSED(face),
281                              BMVert **verts, const SubDParams *params)
282 {
283         BMFace *nf;
284         int i, add, numcuts = params->numcuts;
285
286         /* if it's odd, the middle face is a quad, otherwise it's a triangle */
287         if ((numcuts % 2) == 0) {
288                 add = 2;
289                 for (i = 0; i < numcuts; i++) {
290                         if (i == numcuts / 2) {
291                                 add -= 1;
292                         }
293                         connect_smallest_face(bm, verts[i], verts[numcuts + add], &nf);
294                 }
295         }
296         else {
297                 add = 2;
298                 for (i = 0; i < numcuts; i++) {
299                         connect_smallest_face(bm, verts[i], verts[numcuts + add], &nf);
300                         if (i == numcuts / 2) {
301                                 add -= 1;
302                                 connect_smallest_face(bm, verts[i], verts[numcuts + add], &nf);
303                         }
304                 }
305
306         }
307 }
308
309 static SubDPattern quad_1edge = {
310         {1, 0, 0, 0},
311         quad_1edge_split,
312         4,
313 };
314
315
316 /*
317  *  v6--------v5
318  *  |          |
319  *  |          |v4s
320  *  |          |v3s
321  *  |   s  s   |
322  *  v7-v0--v1-v2
323  */
324 static void quad_2edge_split_path(BMesh *bm, BMFace *UNUSED(face), BMVert **verts,
325                                   const SubDParams *params)
326 {
327         BMFace *nf;
328         int i, numcuts = params->numcuts;
329         
330         for (i = 0; i < numcuts; i++) {
331                 connect_smallest_face(bm, verts[i], verts[numcuts + (numcuts - i)], &nf);
332         }
333         connect_smallest_face(bm, verts[numcuts * 2 + 3], verts[numcuts * 2 + 1], &nf);
334 }
335
336 static SubDPattern quad_2edge_path = {
337         {1, 1, 0, 0},
338         quad_2edge_split_path,
339         4,
340 };
341
342 /*
343  *  v6--------v5
344  *  |          |
345  *  |          |v4s
346  *  |          |v3s
347  *  |   s  s   |
348  *  v7-v0--v1-v2
349  */
350 static void quad_2edge_split_innervert(BMesh *bm, BMFace *UNUSED(face), BMVert **verts,
351                                        const SubDParams *params)
352 {
353         BMFace *nf;
354         BMVert *v, *lastv;
355         BMEdge *e, *ne, olde;
356         int i, numcuts = params->numcuts;
357         
358         lastv = verts[numcuts];
359
360         for (i = numcuts - 1; i >= 0; i--) {
361                 e = connect_smallest_face(bm, verts[i], verts[numcuts + (numcuts - i)], &nf);
362
363                 olde = *e;
364                 v = bm_subdivide_edge_addvert(bm, e, &olde, params, 0.5f, 0.5f, &ne, e->v1, e->v2);
365
366                 if (i != numcuts - 1) {
367                         connect_smallest_face(bm, lastv, v, &nf);
368                 }
369
370                 lastv = v;
371         }
372
373         connect_smallest_face(bm, lastv, verts[numcuts * 2 + 2], &nf);
374 }
375
376 static SubDPattern quad_2edge_innervert = {
377         {1, 1, 0, 0},
378         quad_2edge_split_innervert,
379         4,
380 };
381
382 /*
383  *  v6--------v5
384  *  |          |
385  *  |          |v4s
386  *  |          |v3s
387  *  |   s  s   |
388  *  v7-v0--v1-v2
389  *
390  */
391 static void quad_2edge_split_fan(BMesh *bm, BMFace *UNUSED(face), BMVert **verts,
392                                  const SubDParams *params)
393 {
394         BMFace *nf;
395         /* BMVert *v; */               /* UNUSED */
396         /* BMVert *lastv = verts[2]; */ /* UNUSED */
397         /* BMEdge *e, *ne; */          /* UNUSED */
398         int i, numcuts = params->numcuts;
399
400         for (i = 0; i < numcuts; i++) {
401                 connect_smallest_face(bm, verts[i], verts[numcuts * 2 + 2], &nf);
402                 connect_smallest_face(bm, verts[numcuts + (numcuts - i)], verts[numcuts * 2 + 2], &nf);
403         }
404 }
405
406 static SubDPattern quad_2edge_fan = {
407         {1, 1, 0, 0},
408         quad_2edge_split_fan,
409         4,
410 };
411
412 /*
413  *      s   s
414  *  v8--v7--v6-v5
415  *  |          |
416  *  |          v4 s
417  *  |          |
418  *  |          v3 s
419  *  |   s  s   |
420  *  v9-v0--v1-v2
421  */
422 static void quad_3edge_split(BMesh *bm, BMFace *UNUSED(face), BMVert **verts,
423                              const SubDParams *params)
424 {
425         BMFace *nf;
426         int i, add = 0, numcuts = params->numcuts;
427         
428         for (i = 0; i < numcuts; i++) {
429                 if (i == numcuts / 2) {
430                         if (numcuts % 2 != 0) {
431                                 connect_smallest_face(bm, verts[numcuts - i - 1 + add], verts[i + numcuts + 1], &nf);
432                         }
433                         add = numcuts * 2 + 2;
434                 }
435                 connect_smallest_face(bm, verts[numcuts - i - 1 + add], verts[i + numcuts + 1], &nf);
436         }
437
438         for (i = 0; i < numcuts / 2 + 1; i++) {
439                 connect_smallest_face(bm, verts[i], verts[(numcuts - i) + numcuts * 2 + 1], &nf);
440         }
441 }
442
443 static SubDPattern quad_3edge = {
444         {1, 1, 1, 0},
445         quad_3edge_split,
446         4,
447 };
448
449 /*
450  *            v8--v7-v6--v5
451  *            |     s    |
452  *            |v9 s     s|v4
453  * first line |          |   last line
454  *            |v10s s   s|v3
455  *            v11-v0--v1-v2
456  *
457  *            it goes from bottom up
458  */
459 static void quad_4edge_subdivide(BMesh *bm, BMFace *UNUSED(face), BMVert **verts,
460                                  const SubDParams *params)
461 {
462         BMFace *nf;
463         BMVert *v, *v1, *v2;
464         BMEdge *e, *ne, temp;
465         BMVert **lines;
466         int numcuts = params->numcuts;
467         int i, j, a, b, s = numcuts + 2 /* , totv = numcuts * 4 + 4 */;
468
469         lines = MEM_callocN(sizeof(BMVert *) * (numcuts + 2) * (numcuts + 2), "q_4edge_split");
470         /* build a 2-dimensional array of verts,
471          * containing every vert (and all new ones)
472          * in the face */
473
474         /* first line */
475         for (i = 0; i < numcuts + 2; i++) {
476                 lines[i] = verts[numcuts * 3 + 2 + (numcuts - i + 1)];
477         }
478
479         /* last line */
480         for (i = 0; i < numcuts + 2; i++) {
481                 lines[(s - 1) * s + i] = verts[numcuts + i];
482         }
483         
484         /* first and last members of middle lines */
485         for (i = 0; i < numcuts; i++) {
486                 a = i;
487                 b = numcuts + 1 + numcuts + 1 + (numcuts - i - 1);
488                 
489                 e = connect_smallest_face(bm, verts[a], verts[b], &nf);
490                 if (!e)
491                         continue;
492
493                 BMO_elem_flag_enable(bm, e, ELE_INNER);
494                 BMO_elem_flag_enable(bm, nf, ELE_INNER);
495
496                 
497                 v1 = lines[(i + 1) * s] = verts[a];
498                 v2 = lines[(i + 1) * s + s - 1] = verts[b];
499                 
500                 temp = *e;
501                 for (a = 0; a < numcuts; a++) {
502                         v = subdivideedgenum(bm, e, &temp, a, numcuts, params, &ne,
503                                              v1, v2);
504
505                         BMESH_ASSERT(v != NULL);
506
507                         BMO_elem_flag_enable(bm, ne, ELE_INNER);
508                         lines[(i + 1) * s + a + 1] = v;
509                 }
510         }
511
512         for (i = 1; i < numcuts + 2; i++) {
513                 for (j = 1; j < numcuts + 1; j++) {
514                         a = i * s + j;
515                         b = (i - 1) * s + j;
516                         e = connect_smallest_face(bm, lines[a], lines[b], &nf);
517                         if (!e)
518                                 continue;
519
520                         BMO_elem_flag_enable(bm, e, ELE_INNER);
521                         BMO_elem_flag_enable(bm, nf, ELE_INNER);
522                 }
523         }
524
525         MEM_freeN(lines);
526 }
527
528 /*
529  *        v3
530  *       / \
531  *      /   \
532  *     /     \
533  *    /       \
534  *   /         \
535  *  v4--v0--v1--v2
536  *      s    s
537  */
538 static void tri_1edge_split(BMesh *bm, BMFace *UNUSED(face), BMVert **verts,
539                             const SubDParams *params)
540 {
541         BMFace *nf;
542         int i, numcuts = params->numcuts;
543         
544         for (i = 0; i < numcuts; i++) {
545                 connect_smallest_face(bm, verts[i], verts[numcuts + 1], &nf);
546         }
547 }
548
549 static SubDPattern tri_1edge = {
550         {1, 0, 0},
551         tri_1edge_split,
552         3,
553 };
554
555 /*         v5
556  *        / \
557  *   s v6/---\ v4 s
558  *      / \ / \
559  *  sv7/---v---\ v3 s
560  *    /  \/  \/ \
561  *   v8--v0--v1--v2
562  *      s    s
563  */
564 static void tri_3edge_subdivide(BMesh *bm, BMFace *UNUSED(face), BMVert **verts,
565                                 const SubDParams *params)
566 {
567         BMFace *nf;
568         BMEdge *e, *ne, temp;
569         BMVert ***lines, *v, ov1, ov2;
570         void *stackarr[1];
571         int i, j, a, b, numcuts = params->numcuts;
572         
573         /* number of verts in each lin */
574         lines = MEM_callocN(sizeof(void *) * (numcuts + 2), "triangle vert table");
575         
576         lines[0] = (BMVert **) stackarr;
577         lines[0][0] = verts[numcuts * 2 + 1];
578         
579         lines[numcuts + 1] = MEM_callocN(sizeof(void *) * (numcuts + 2), "triangle vert table 2");
580         for (i = 0; i < numcuts; i++) {
581                 lines[numcuts + 1][i + 1] = verts[i];
582         }
583         lines[numcuts + 1][0] = verts[numcuts * 3 + 2];
584         lines[numcuts + 1][numcuts + 1] = verts[numcuts];
585
586         for (i = 0; i < numcuts; i++) {
587                 lines[i + 1] = MEM_callocN(sizeof(void *) * (2 + i), "triangle vert table row");
588                 a = numcuts * 2 + 2 + i;
589                 b = numcuts + numcuts - i;
590                 e = connect_smallest_face(bm, verts[a], verts[b], &nf);
591                 if (!e) goto cleanup;
592
593                 BMO_elem_flag_enable(bm, e, ELE_INNER);
594                 BMO_elem_flag_enable(bm, nf, ELE_INNER);
595
596                 lines[i + 1][0] = verts[a];
597                 lines[i + 1][i + 1] = verts[b];
598                 
599                 temp = *e;
600                 ov1 = *verts[a];
601                 ov2 = *verts[b];
602                 temp.v1 = &ov1;
603                 temp.v2 = &ov2;
604                 for (j = 0; j < i; j++) {
605                         v = subdivideedgenum(bm, e, &temp, j, i, params, &ne,
606                                              verts[a], verts[b]);
607                         lines[i + 1][j + 1] = v;
608
609                         BMO_elem_flag_enable(bm, ne, ELE_INNER);
610                 }
611         }
612         
613         /*
614          *         v5
615          *        / \
616          *   s v6/---\ v4 s
617          *      / \ / \
618          *  sv7/---v---\ v3 s
619          *    /  \/  \/ \
620          *   v8--v0--v1--v2
621          *      s    s
622          */
623         for (i = 1; i < numcuts + 1; i++) {
624                 for (j = 0; j < i; j++) {
625                         e = connect_smallest_face(bm, lines[i][j], lines[i + 1][j + 1], &nf);
626
627                         BMO_elem_flag_enable(bm, e, ELE_INNER);
628                         BMO_elem_flag_enable(bm, nf, ELE_INNER);
629
630                         e = connect_smallest_face(bm, lines[i][j + 1], lines[i + 1][j + 1], &nf);
631
632                         BMO_elem_flag_enable(bm, e, ELE_INNER);
633                         BMO_elem_flag_enable(bm, nf, ELE_INNER);
634                 }
635         }
636
637 cleanup:
638         for (i = 1; i < numcuts + 2; i++) {
639                 if (lines[i]) MEM_freeN(lines[i]);
640         }
641
642         MEM_freeN(lines);
643 }
644
645 static SubDPattern tri_3edge = {
646         {1, 1, 1},
647         tri_3edge_subdivide,
648         3,
649 };
650
651
652 static SubDPattern quad_4edge = {
653         {1, 1, 1, 1},
654         quad_4edge_subdivide,
655         4,
656 };
657
658 static SubDPattern *patterns[] = {
659         NULL, //quad single edge pattern is inserted here
660         NULL, //quad corner vert pattern is inserted here
661         NULL, //tri single edge pattern is inserted here
662         NULL,
663         &quad_3edge,
664         NULL,
665 };
666
667 #define PLEN  (sizeof(patterns) / sizeof(void *))
668
669 typedef struct SubDFaceData {
670         BMVert *start; SubDPattern *pat;
671         int totedgesel; //only used if pat was NULL, e.g. no pattern was found
672         BMFace *face;
673 } SubDFaceData;
674
675 void bmo_esubd_exec(BMesh *bm, BMOperator *op)
676 {
677         BMOpSlot *einput;
678         SubDPattern *pat;
679         SubDParams params;
680         SubDFaceData *facedata = NULL;
681         BMIter viter, fiter, liter;
682         BMVert *v, **verts = NULL;
683         BMEdge *edge, **edges = NULL;
684         BMLoop *nl, *l, **splits = NULL, **loops = NULL;
685         BMFace *face;
686         BLI_array_declare(splits);
687         BLI_array_declare(loops);
688         BLI_array_declare(facedata);
689         BLI_array_declare(edges);
690         BLI_array_declare(verts);
691         float smooth, fractal, along_normal;
692         int use_sphere, cornertype, use_singleedge, use_gridfill;
693         int skey, seed, i, j, matched, a, b, numcuts, totesel;
694         
695         BMO_slot_buffer_flag_enable(bm, op, "edges", BM_EDGE, SUBD_SPLIT);
696         
697         numcuts = BMO_slot_int_get(op, "numcuts");
698         seed = BMO_slot_int_get(op, "seed");
699         smooth = BMO_slot_float_get(op, "smooth");
700         fractal = BMO_slot_float_get(op, "fractal");
701         along_normal = BMO_slot_float_get(op, "along_normal");
702         cornertype = BMO_slot_int_get(op, "quadcornertype");
703
704         use_singleedge = BMO_slot_bool_get(op, "use_singleedge");
705         use_gridfill   = BMO_slot_bool_get(op, "use_gridfill");
706         use_sphere     = BMO_slot_bool_get(op, "use_sphere");
707         
708         BLI_srandom(seed);
709         
710         patterns[1] = NULL;
711         //straight cut is patterns[1] == NULL
712         switch (cornertype) {
713                 case SUBD_PATH:
714                         patterns[1] = &quad_2edge_path;
715                         break;
716                 case SUBD_INNERVERT:
717                         patterns[1] = &quad_2edge_innervert;
718                         break;
719                 case SUBD_FAN:
720                         patterns[1] = &quad_2edge_fan;
721                         break;
722         }
723         
724         if (use_singleedge) {
725                 patterns[0] = &quad_1edge;
726                 patterns[2] = &tri_1edge;
727         }
728         else {
729                 patterns[0] = NULL;
730                 patterns[2] = NULL;
731         }
732
733         if (use_gridfill) {
734                 patterns[3] = &quad_4edge;
735                 patterns[5] = &tri_3edge;
736         }
737         else {
738                 patterns[3] = NULL;
739                 patterns[5] = NULL;
740         }
741         
742         /* add a temporary shapekey layer to store displacements on current geometry */
743         BM_data_layer_add(bm, &bm->vdata, CD_SHAPEKEY);
744         skey = CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY) - 1;
745         
746         BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
747                 float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, CD_SHAPEKEY, skey);
748                 copy_v3_v3(co, v->co);
749         }
750
751         /* first go through and tag edges */
752         BMO_slot_buffer_from_enabled_flag(bm, op, "edges", BM_EDGE, SUBD_SPLIT);
753
754         params.numcuts = numcuts;
755         params.op = op;
756         params.smooth = smooth;
757         params.seed = seed;
758         params.fractal = fractal;
759         params.along_normal = along_normal;
760         params.use_smooth  = (smooth  != 0.0f);
761         params.use_fractal = (fractal != 0.0f);
762         params.use_sphere  = use_sphere;
763         params.origkey = skey;
764         params.off[0] = (float)BLI_drand() * 200.0f;
765         params.off[1] = (float)BLI_drand() * 200.0f;
766         params.off[2] = (float)BLI_drand() * 200.0f;
767         
768         BMO_slot_map_to_flag(bm, op, "custompatterns",
769                              BM_FACE, FACE_CUSTOMFILL);
770
771         BMO_slot_map_to_flag(bm, op, "edgepercents",
772                              BM_EDGE, EDGE_PERCENT);
773
774
775         BM_ITER_MESH (face, &fiter, bm, BM_FACES_OF_MESH) {
776                 BMEdge *e1 = NULL, *e2 = NULL;
777                 float vec1[3], vec2[3];
778
779                 /* figure out which pattern to use */
780
781                 BLI_array_empty(edges);
782                 BLI_array_empty(verts);
783
784                 BLI_array_grow_items(edges, face->len);
785                 BLI_array_grow_items(verts, face->len);
786
787                 matched = 0;
788
789                 totesel = 0;
790                 BM_ITER_ELEM_INDEX (nl, &liter, face, BM_LOOPS_OF_FACE, i) {
791                         edges[i] = nl->e;
792                         verts[i] = nl->v;
793
794                         if (BMO_elem_flag_test(bm, edges[i], SUBD_SPLIT)) {
795                                 if (!e1) e1 = edges[i];
796                                 else     e2 = edges[i];
797
798                                 totesel++;
799                         }
800                 }
801
802                 /* make sure the two edges have a valid angle to each other */
803                 if (totesel == 2 && BM_edge_share_vert_count(e1, e2)) {
804                         float angle;
805
806                         sub_v3_v3v3(vec1, e1->v2->co, e1->v1->co);
807                         sub_v3_v3v3(vec2, e2->v2->co, e2->v1->co);
808                         normalize_v3(vec1);
809                         normalize_v3(vec2);
810
811                         angle = dot_v3v3(vec1, vec2);
812                         angle = fabsf(angle);
813                         if (fabsf(angle - 1.0f) < 0.01f) {
814                                 totesel = 0;
815                         }
816                 }
817
818                 if (BMO_elem_flag_test(bm, face, FACE_CUSTOMFILL)) {
819                         pat = BMO_slot_map_data_get(bm, op,
820                                                     "custompatterns", face);
821                         for (i = 0; i < pat->len; i++) {
822                                 matched = 1;
823                                 for (j = 0; j < pat->len; j++) {
824                                         a = (j + i) % pat->len;
825                                         if ((!!BMO_elem_flag_test(bm, edges[a], SUBD_SPLIT)) != (!!pat->seledges[j])) {
826                                                 matched = 0;
827                                                 break;
828                                         }
829                                 }
830                                 if (matched) {
831                                         BLI_array_grow_one(facedata);
832                                         b = BLI_array_count(facedata) - 1;
833                                         facedata[b].pat = pat;
834                                         facedata[b].start = verts[i];
835                                         facedata[b].face = face;
836                                         facedata[b].totedgesel = totesel;
837                                         BMO_elem_flag_enable(bm, face, SUBD_SPLIT);
838                                         break;
839                                 }
840                         }
841
842                         /* obvously don't test for other patterns matching */
843                         continue;
844                 }
845
846                 for (i = 0; i < PLEN; i++) {
847                         pat = patterns[i];
848                         if (!pat) {
849                                 continue;
850                         }
851
852                         if (pat->len == face->len) {
853                                 for (a = 0; a < pat->len; a++) {
854                                         matched = 1;
855                                         for (b = 0; b < pat->len; b++) {
856                                                 j = (b + a) % pat->len;
857                                                 if ((!!BMO_elem_flag_test(bm, edges[j], SUBD_SPLIT)) != (!!pat->seledges[b])) {
858                                                         matched = 0;
859                                                         break;
860                                                 }
861                                         }
862                                         if (matched) {
863                                                 break;
864                                         }
865                                 }
866                                 if (matched) {
867                                         BLI_array_grow_one(facedata);
868                                         j = BLI_array_count(facedata) - 1;
869
870                                         BMO_elem_flag_enable(bm, face, SUBD_SPLIT);
871
872                                         facedata[j].pat = pat;
873                                         facedata[j].start = verts[a];
874                                         facedata[j].face = face;
875                                         facedata[j].totedgesel = totesel;
876                                         break;
877                                 }
878                         }
879
880                 }
881                 
882                 if (!matched && totesel) {
883                         BLI_array_grow_one(facedata);
884                         j = BLI_array_count(facedata) - 1;
885                         
886                         BMO_elem_flag_enable(bm, face, SUBD_SPLIT);
887                         facedata[j].totedgesel = totesel;
888                         facedata[j].face = face;
889                 }
890         }
891
892         einput = BMO_slot_get(op, "edges");
893
894         /* go through and split edges */
895         for (i = 0; i < einput->len; i++) {
896                 edge = ((BMEdge **)einput->data.p)[i];
897                 bm_subdivide_multicut(bm, edge, &params, edge->v1, edge->v2);
898         }
899
900         /* copy original-geometry displacements to current coordinates */
901         BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
902                 float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, CD_SHAPEKEY, skey);
903                 copy_v3_v3(v->co, co);
904         }
905
906         i = 0;
907         for (i = 0; i < BLI_array_count(facedata); i++) {
908                 face = facedata[i].face;
909
910                 /* figure out which pattern to use */
911                 BLI_array_empty(verts);
912
913                 pat = facedata[i].pat;
914
915                 if (!pat && facedata[i].totedgesel == 2) {
916                         int vlen;
917                         
918                         /* ok, no pattern.  we still may be able to do something */
919                         BLI_array_empty(loops);
920                         BLI_array_empty(splits);
921
922                         /* for case of two edges, connecting them shouldn't be too hard */
923                         BM_ITER_ELEM (l, &liter, face, BM_LOOPS_OF_FACE) {
924                                 BLI_array_grow_one(loops);
925                                 loops[BLI_array_count(loops) - 1] = l;
926                         }
927                         
928                         vlen = BLI_array_count(loops);
929
930                         /* find the boundary of one of the split edges */
931                         for (a = 1; a < vlen; a++) {
932                                 if (!BMO_elem_flag_test(bm, loops[a - 1]->v, ELE_INNER) &&
933                                     BMO_elem_flag_test(bm, loops[a]->v, ELE_INNER))
934                                 {
935                                         break;
936                                 }
937                         }
938                         
939                         if (BMO_elem_flag_test(bm, loops[(a + numcuts + 1) % vlen]->v, ELE_INNER)) {
940                                 b = (a + numcuts + 1) % vlen;
941                         }
942                         else {
943                                 /* find the boundary of the other edge. */
944                                 for (j = 0; j < vlen; j++) {
945                                         b = (j + a + numcuts + 1) % vlen;
946                                         if (!BMO_elem_flag_test(bm, loops[b == 0 ? vlen - 1 : b - 1]->v, ELE_INNER) &&
947                                             BMO_elem_flag_test(bm, loops[b]->v, ELE_INNER))
948                                         {
949                                                 break;
950                                         }
951                                 }
952                         }
953                         
954                         b += numcuts - 1;
955
956                         for (j = 0; j < numcuts; j++) {
957                                 BLI_array_grow_one(splits);
958                                 splits[BLI_array_count(splits) - 1] = loops[a];
959                                 
960                                 BLI_array_grow_one(splits);
961                                 splits[BLI_array_count(splits) - 1] = loops[b];
962
963                                 b = (b - 1) % vlen;
964                                 a = (a + 1) % vlen;
965                         }
966                         
967                         //BM_face_legal_splits(bmesh, face, splits, BLI_array_count(splits) / 2);
968
969                         for (j = 0; j < BLI_array_count(splits) / 2; j++) {
970                                 if (splits[j * 2]) {
971                                         /* BMFace *nf = */ /* UNUSED */
972                                         BM_face_split(bm, face, splits[j * 2]->v, splits[j * 2 + 1]->v, &nl, NULL, FALSE);
973                                 }
974                         }
975
976                         continue;
977                 }
978                 else if (!pat) {
979                         continue;
980                 }
981
982                 j = a = 0;
983                 for (nl = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, face);
984                      nl;
985                      nl = BM_iter_step(&liter))
986                 {
987                         if (nl->v == facedata[i].start) {
988                                 a = j + 1;
989                                 break;
990                         }
991                         j++;
992                 }
993
994                 for (j = 0; j < face->len; j++) {
995                         BLI_array_grow_one(verts);
996                 }
997                 
998                 j = 0;
999                 for (nl = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, face); nl; nl = BM_iter_step(&liter)) {
1000                         b = (j - a + face->len) % face->len;
1001                         verts[b] = nl->v;
1002                         j += 1;
1003                 }
1004
1005                 BM_CHECK_ELEMENT(face);
1006                 pat->connectexec(bm, face, verts, &params);
1007         }
1008
1009         /* copy original-geometry displacements to current coordinates */
1010         BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
1011                 float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, CD_SHAPEKEY, skey);
1012                 copy_v3_v3(v->co, co);
1013         }
1014
1015         BM_data_layer_free_n(bm, &bm->vdata, CD_SHAPEKEY, skey);
1016         
1017         if (facedata) BLI_array_free(facedata);
1018         if (edges) BLI_array_free(edges);
1019         if (verts) BLI_array_free(verts);
1020         BLI_array_free(splits);
1021         BLI_array_free(loops);
1022
1023         BMO_slot_buffer_from_enabled_flag(bm, op, "outinner", BM_ALL, ELE_INNER);
1024         BMO_slot_buffer_from_enabled_flag(bm, op, "outsplit", BM_ALL, ELE_SPLIT);
1025         
1026         BMO_slot_buffer_from_enabled_flag(bm, op, "geomout", BM_ALL, ELE_INNER|ELE_SPLIT|SUBD_SPLIT);
1027 }
1028
1029 /* editmesh-emulating function */
1030 void BM_mesh_esubdivide(BMesh *bm, const char edge_hflag,
1031                         float smooth, float fractal, float along_normal,
1032                         int numcuts,
1033                         int seltype, int cornertype,
1034                         const short use_singleedge, const short use_gridfill,
1035                         int seed)
1036 {
1037         BMOperator op;
1038         
1039         /* use_sphere isnt exposed here since its only used for new primitives */
1040         BMO_op_initf(bm, &op,
1041                      "esubd edges=%he "
1042                      "smooth=%f fractal=%f along_normal=%f "
1043                      "numcuts=%i "
1044                      "quadcornertype=%i "
1045                      "use_singleedge=%b use_gridfill=%b "
1046                      "seed=%i",
1047                      edge_hflag,
1048                      smooth, fractal, along_normal,
1049                      numcuts,
1050                      cornertype,
1051                      use_singleedge, use_gridfill,
1052                      seed);
1053         
1054         BMO_op_exec(bm, &op);
1055         
1056         if (seltype == SUBDIV_SELECT_INNER) {
1057                 BMOIter iter;
1058                 BMElem *ele;
1059
1060                 for (ele = BMO_iter_new(&iter, bm, &op, "outinner", BM_EDGE | BM_VERT); ele; ele = BMO_iter_step(&iter)) {
1061                         BM_elem_select_set(bm, ele, TRUE);
1062                 }
1063         }
1064         else if (seltype == SUBDIV_SELECT_LOOPCUT) {
1065                 BMOIter iter;
1066                 BMElem *ele;
1067                 
1068                 /* deselect input */
1069                 BM_mesh_elem_hflag_disable_all(bm, BM_VERT | BM_EDGE | BM_FACE, BM_ELEM_SELECT, FALSE);
1070
1071                 for (ele = BMO_iter_new(&iter, bm, &op, "outinner", BM_EDGE | BM_VERT); ele; ele = BMO_iter_step(&iter)) {
1072                         BM_elem_select_set(bm, ele, TRUE);
1073
1074                         if (ele->head.htype == BM_VERT) {
1075                                 BMEdge *e;
1076                                 BMIter eiter;
1077
1078                                 BM_ITER_ELEM (e, &eiter, ele, BM_EDGES_OF_VERT) {
1079                                         if (!BM_elem_flag_test(e, BM_ELEM_SELECT) &&
1080                                              BM_elem_flag_test(e->v1, BM_ELEM_SELECT) &&
1081                                              BM_elem_flag_test(e->v2, BM_ELEM_SELECT))
1082                                         {
1083                                                 BM_edge_select_set(bm, e, TRUE);
1084                                         }
1085                                         else if (BM_elem_flag_test(e, BM_ELEM_SELECT) &&
1086                                                  (!BM_elem_flag_test(e->v1, BM_ELEM_SELECT) ||
1087                                                   !BM_elem_flag_test(e->v2, BM_ELEM_SELECT)))
1088                                         {
1089                                                 BM_edge_select_set(bm, e, FALSE);
1090                                         }
1091                                 }
1092                         }
1093                 }
1094         }
1095
1096         BMO_op_finish(bm, &op);
1097 }
1098
1099 void bmo_edgebisect_exec(BMesh *bm, BMOperator *op)
1100 {
1101         BMOIter siter;
1102         BMEdge *e;
1103         SubDParams params = {0};
1104         int skey;
1105         
1106         params.numcuts = BMO_slot_int_get(op, "numcuts");
1107         params.op = op;
1108         
1109         BM_data_layer_add(bm, &bm->vdata, CD_SHAPEKEY);
1110         skey = CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY) - 1;
1111         
1112         params.origkey = skey;
1113
1114         /* go through and split edges */
1115         BMO_ITER (e, &siter, bm, op, "edges", BM_EDGE) {
1116                 bm_subdivide_multicut(bm, e, &params, e->v1, e->v2);
1117         }
1118
1119         BMO_slot_buffer_from_enabled_flag(bm, op, "outsplit", BM_ALL, ELE_SPLIT);
1120
1121         BM_data_layer_free_n(bm, &bm->vdata, CD_SHAPEKEY, skey);
1122 }