Cleanup: spelling
[blender-staging.git] / source / blender / bmesh / operators / bmo_extrude.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_extrude.c
24  *  \ingroup bmesh
25  *
26  * Extrude faces and solidify.
27  */
28
29 #include "MEM_guardedalloc.h"
30
31 #include "DNA_meshdata_types.h"
32
33 #include "BLI_math.h"
34 #include "BLI_buffer.h"
35
36 #include "BKE_customdata.h"
37
38 #include "bmesh.h"
39
40 #include "intern/bmesh_operators_private.h" /* own include */
41
42 #define USE_EDGE_REGION_FLAGS
43
44 enum {
45         EXT_INPUT   = 1,
46         EXT_KEEP    = 2,
47         EXT_DEL     = 4
48 };
49
50 #define VERT_MARK 1
51 #define EDGE_MARK 1
52 #define FACE_MARK 1
53 #define VERT_NONMAN 2
54 #define EDGE_NONMAN 2
55
56 void bmo_extrude_discrete_faces_exec(BMesh *bm, BMOperator *op)
57 {
58         const bool use_select_history = BMO_slot_bool_get(op->slots_in, "use_select_history");
59         GHash *select_history_map = NULL;
60
61         BMOIter siter;
62         BMFace *f_org;
63
64         if (use_select_history) {
65                 select_history_map = BM_select_history_map_create(bm);
66         }
67
68         BMO_ITER (f_org, &siter, op->slots_in, "faces", BM_FACE) {
69                 BMFace *f_new;
70                 BMLoop *l_org, *l_org_first;
71                 BMLoop *l_new;
72
73                 BMO_elem_flag_enable(bm, f_org, EXT_DEL);
74
75                 f_new = BM_face_copy(bm, bm, f_org, true, true);
76                 BMO_elem_flag_enable(bm, f_new, EXT_KEEP);
77
78                 if (select_history_map) {
79                         BMEditSelection *ese;
80                         ese = BLI_ghash_lookup(select_history_map, f_org);
81                         if (ese) {
82                                 ese->ele = (BMElem *)f_new;
83                         }
84                 }
85
86                 l_org = l_org_first = BM_FACE_FIRST_LOOP(f_org);
87                 l_new = BM_FACE_FIRST_LOOP(f_new);
88
89                 do {
90                         BMFace *f_side;
91                         BMLoop *l_side_iter;
92
93                         BM_elem_attrs_copy(bm, bm, l_org, l_new);
94
95                         f_side = BM_face_create_quad_tri(bm,
96                                                          l_org->next->v, l_new->next->v, l_new->v, l_org->v,
97                                                          f_org, BM_CREATE_NOP);
98
99                         l_side_iter = BM_FACE_FIRST_LOOP(f_side);
100
101                         BM_elem_attrs_copy(bm, bm, l_org->next, l_side_iter);  l_side_iter = l_side_iter->next;
102                         BM_elem_attrs_copy(bm, bm, l_org->next, l_side_iter);  l_side_iter = l_side_iter->next;
103                         BM_elem_attrs_copy(bm, bm, l_org, l_side_iter);        l_side_iter = l_side_iter->next;
104                         BM_elem_attrs_copy(bm, bm, l_org, l_side_iter);
105
106                         if (select_history_map) {
107                                 BMEditSelection *ese;
108
109                                 ese = BLI_ghash_lookup(select_history_map, l_org->v);
110                                 if (ese) {
111                                         ese->ele = (BMElem *)l_new->v;
112                                 }
113                                 ese = BLI_ghash_lookup(select_history_map, l_org->e);
114                                 if (ese) {
115                                         ese->ele = (BMElem *)l_new->e;
116                                 }
117                         }
118
119                 } while (((l_new = l_new->next),
120                           (l_org = l_org->next)) != l_org_first);
121         }
122
123         if (select_history_map) {
124                 BLI_ghash_free(select_history_map, NULL, NULL);
125         }
126
127         BMO_op_callf(bm, op->flag,
128                      "delete geom=%ff context=%i",
129                      EXT_DEL, DEL_ONLYFACES);
130         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, EXT_KEEP);
131 }
132
133 /**
134  * \brief Copy the loop pair from an adjacent face to both sides of this quad.
135  *
136  * The face is assumed to be a quad, created by extruding.
137  * This function won't crash if its not but won't work right either.
138  * \a e_b is the new edge.
139  *
140  * \note The edge this face comes from needs to be from the first and second verts to the face.
141  * The caller must ensure this else we will copy from the wrong source.
142  */
143 static void bm_extrude_copy_face_loop_attributes(BMesh *bm, BMFace *f)
144 {
145         /* edge we are extruded from */
146         BMLoop *l_first_0 = BM_FACE_FIRST_LOOP(f);
147         BMLoop *l_first_1 = l_first_0->next;
148         BMLoop *l_first_2 = l_first_1->next;
149         BMLoop *l_first_3 = l_first_2->next;
150
151         BMLoop *l_other_0;
152         BMLoop *l_other_1;
153
154         if (UNLIKELY(l_first_0 == l_first_0->radial_next)) {
155                 return;
156         }
157
158         l_other_0 = BM_edge_other_loop(l_first_0->e, l_first_0);
159         l_other_1 = BM_edge_other_loop(l_first_0->e, l_first_1);
160
161         /* copy data */
162         BM_elem_attrs_copy(bm, bm, l_other_0->f, f);
163         BM_elem_flag_disable(f, BM_ELEM_HIDDEN);  /* possibly we copy from a hidden face */
164
165         BM_elem_attrs_copy(bm, bm, l_other_0, l_first_0);
166         BM_elem_attrs_copy(bm, bm, l_other_0, l_first_3);
167
168         BM_elem_attrs_copy(bm, bm, l_other_1, l_first_1);
169         BM_elem_attrs_copy(bm, bm, l_other_1, l_first_2);
170 }
171
172 /* Disable the skin root flag on the input vert, assumes that the vert
173  * data includes an CD_MVERT_SKIN layer */
174 static void bm_extrude_disable_skin_root(BMesh *bm, BMVert *v)
175 {
176         MVertSkin *vs;
177         
178         vs = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_MVERT_SKIN);
179         vs->flag &= ~MVERT_SKIN_ROOT;
180 }
181
182 void bmo_extrude_edge_only_exec(BMesh *bm, BMOperator *op)
183 {
184         BMOIter siter;
185         BMOperator dupeop;
186         BMFace *f;
187         BMEdge *e, *e_new;
188         
189         BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
190                 BMO_elem_flag_enable(bm, e, EXT_INPUT);
191                 BMO_elem_flag_enable(bm, e->v1, EXT_INPUT);
192                 BMO_elem_flag_enable(bm, e->v2, EXT_INPUT);
193         }
194
195         BMO_op_initf(
196                 bm, &dupeop, op->flag,
197                 "duplicate geom=%fve use_select_history=%b",
198                 EXT_INPUT, BMO_slot_bool_get(op->slots_in, "use_select_history"));
199
200         BMO_op_exec(bm, &dupeop);
201
202         /* disable root flag on all new skin nodes */
203         if (CustomData_has_layer(&bm->vdata, CD_MVERT_SKIN)) {
204                 BMVert *v;
205                 BMO_ITER (v, &siter, dupeop.slots_out, "geom.out", BM_VERT) {
206                         bm_extrude_disable_skin_root(bm, v);
207                 }
208         }
209
210         for (e = BMO_iter_new(&siter, dupeop.slots_out, "boundary_map.out", 0); e; e = BMO_iter_step(&siter)) {
211                 BMVert *f_verts[4];
212                 e_new = BMO_iter_map_value_ptr(&siter);
213
214                 if (e->l && e->v1 != e->l->v) {
215                         f_verts[0] = e->v1;
216                         f_verts[1] = e->v2;
217                         f_verts[2] = e_new->v2;
218                         f_verts[3] = e_new->v1;
219                 }
220                 else {
221                         f_verts[0] = e->v2;
222                         f_verts[1] = e->v1;
223                         f_verts[2] = e_new->v1;
224                         f_verts[3] = e_new->v2;
225                 }
226                 /* not sure what to do about example face, pass NULL for now */
227                 f = BM_face_create_verts(bm, f_verts, 4, NULL, BM_CREATE_NOP, true);
228                 bm_extrude_copy_face_loop_attributes(bm, f);
229                 
230                 if (BMO_elem_flag_test(bm, e, EXT_INPUT))
231                         e = e_new;
232                 
233                 BMO_elem_flag_enable(bm, f, EXT_KEEP);
234                 BMO_elem_flag_enable(bm, e, EXT_KEEP);
235                 BMO_elem_flag_enable(bm, e->v1, EXT_KEEP);
236                 BMO_elem_flag_enable(bm, e->v2, EXT_KEEP);
237                 
238         }
239
240         BMO_op_finish(bm, &dupeop);
241
242         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_ALL_NOLOOP, EXT_KEEP);
243 }
244
245 void bmo_extrude_vert_indiv_exec(BMesh *bm, BMOperator *op)
246 {
247         const bool use_select_history = BMO_slot_bool_get(op->slots_in, "use_select_history");
248         BMOIter siter;
249         BMVert *v, *dupev;
250         BMEdge *e;
251         const bool has_vskin = CustomData_has_layer(&bm->vdata, CD_MVERT_SKIN);
252         GHash *select_history_map = NULL;
253
254         if (use_select_history) {
255                 select_history_map = BM_select_history_map_create(bm);
256         }
257
258         for (v = BMO_iter_new(&siter, op->slots_in, "verts", BM_VERT); v; v = BMO_iter_step(&siter)) {
259                 dupev = BM_vert_create(bm, v->co, v, BM_CREATE_NOP);
260                 BMO_elem_flag_enable(bm, dupev, EXT_KEEP);
261
262                 if (has_vskin)
263                         bm_extrude_disable_skin_root(bm, v);
264
265                 if (select_history_map) {
266                         BMEditSelection *ese;
267                         ese = BLI_ghash_lookup(select_history_map, v);
268                         if (ese) {
269                                 ese->ele = (BMElem *)dupev;
270                         }
271                 }
272
273                 /* not essential, but ensures face normals from extruded edges are contiguous */
274                 if (BM_vert_is_wire_endpoint(v)) {
275                         if (v->e->v1 == v) {
276                                 SWAP(BMVert *, v, dupev);
277                         }
278                 }
279
280                 e = BM_edge_create(bm, v, dupev, NULL, BM_CREATE_NOP);
281                 BMO_elem_flag_enable(bm, e, EXT_KEEP);
282         }
283
284         if (select_history_map) {
285                 BLI_ghash_free(select_history_map, NULL, NULL);
286         }
287
288         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "verts.out", BM_VERT, EXT_KEEP);
289         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EXT_KEEP);
290 }
291
292 #ifdef USE_EDGE_REGION_FLAGS
293 /**
294  * When create an edge for an extruded face region
295  * check surrounding edge flags before creating a new edge.
296  */
297 static bool bm_extrude_region_edge_flag(const BMVert *v, char r_e_hflag[2])
298 {
299         BMEdge *e_iter;
300         const char hflag_enable  = BM_ELEM_SEAM;
301         const char hflag_disable = BM_ELEM_SMOOTH;
302         bool ok = false;
303
304         r_e_hflag[0] = 0x0;
305         r_e_hflag[1] = 0xff;
306
307         /* clear flags on both disks */
308         e_iter = v->e;
309         do {
310                 if (e_iter->l && !BM_edge_is_boundary(e_iter)) {
311                         r_e_hflag[0] |= e_iter->head.hflag;
312                         r_e_hflag[1] &= e_iter->head.hflag;
313                         ok = true;
314                 }
315         } while ((e_iter = BM_DISK_EDGE_NEXT(e_iter, v)) != v->e);
316
317         if (ok) {
318                 r_e_hflag[0] &= hflag_enable;
319                 r_e_hflag[1]  = hflag_disable & ~r_e_hflag[1];
320         }
321         return ok;
322 }
323 #endif  /* USE_EDGE_REGION_FLAGS */
324
325 void bmo_extrude_face_region_exec(BMesh *bm, BMOperator *op)
326 {
327         BMOperator dupeop, delop;
328         BMOIter siter;
329         BMIter iter, fiter, viter;
330         BMEdge *e, *e_new;
331         BMVert *v;
332         BMFace *f;
333         bool found, fwd, delorig = false;
334         BMOpSlot *slot_facemap_out;
335         BMOpSlot *slot_edges_exclude;
336
337         /* initialize our sub-operators */
338         BMO_op_initf(
339                 bm, &dupeop, op->flag,
340                 "duplicate use_select_history=%b",
341                 BMO_slot_bool_get(op->slots_in, "use_select_history"));
342
343         BMO_slot_buffer_flag_enable(bm, op->slots_in, "geom", BM_EDGE | BM_FACE, EXT_INPUT);
344         
345         /* if one flagged face is bordered by an un-flagged face, then we delete
346          * original geometry unless caller explicitly asked to keep it. */
347         if (!BMO_slot_bool_get(op->slots_in, "use_keep_orig")) {
348                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
349
350                         int edge_face_tot;
351
352                         if (!BMO_elem_flag_test(bm, e, EXT_INPUT)) {
353                                 continue;
354                         }
355
356                         found = false; /* found a face that isn't input? */
357                         edge_face_tot = 0; /* edge/face count */
358
359                         BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
360                                 if (!BMO_elem_flag_test(bm, f, EXT_INPUT)) {
361                                         found = true;
362                                         delorig = true;
363                                         break;
364                                 }
365
366                                 edge_face_tot++;
367                         }
368
369                         if ((edge_face_tot > 1) && (found == false)) {
370                                 /* edge has a face user, that face isn't extrude input */
371                                 BMO_elem_flag_enable(bm, e, EXT_DEL);
372                         }
373                 }
374         }
375
376         /* calculate verts to delete */
377         BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
378                 if (v->e) {  /* only deal with verts attached to geometry [#33651] */
379                         found = false;
380
381                         BM_ITER_ELEM (e, &viter, v, BM_EDGES_OF_VERT) {
382                                 if (!BMO_elem_flag_test(bm, e, EXT_INPUT) || !BMO_elem_flag_test(bm, e, EXT_DEL)) {
383                                         found = true;
384                                         break;
385                                 }
386                         }
387
388                         /* avoid an extra loop */
389                         if (found == true) {
390                                 BM_ITER_ELEM (f, &viter, v, BM_FACES_OF_VERT) {
391                                         if (!BMO_elem_flag_test(bm, f, EXT_INPUT)) {
392                                                 found = true;
393                                                 break;
394                                         }
395                                 }
396                         }
397
398                         if (found == false) {
399                                 BMO_elem_flag_enable(bm, v, EXT_DEL);
400                         }
401                 }
402         }
403         
404         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
405                 if (BMO_elem_flag_test(bm, f, EXT_INPUT)) {
406                         BMO_elem_flag_enable(bm, f, EXT_DEL);
407                 }
408         }
409
410         if (delorig == true) {
411                 BMO_op_initf(bm, &delop, op->flag,
412                              "delete geom=%fvef context=%i",
413                              EXT_DEL, DEL_ONLYTAGGED);
414         }
415
416         BMO_slot_copy(op,      slots_in, "geom",
417                       &dupeop, slots_in, "geom");
418         BMO_op_exec(bm, &dupeop);
419
420         /* disable root flag on all new skin nodes */
421         if (CustomData_has_layer(&bm->vdata, CD_MVERT_SKIN)) {
422                 BMO_ITER (v, &siter, dupeop.slots_out, "geom.out", BM_VERT) {
423                         bm_extrude_disable_skin_root(bm, v);
424                 }
425         }
426
427         slot_facemap_out = BMO_slot_get(dupeop.slots_out, "face_map.out");
428         if (bm->act_face && BMO_elem_flag_test(bm, bm->act_face, EXT_INPUT)) {
429                 bm->act_face = BMO_slot_map_elem_get(slot_facemap_out, bm->act_face);
430         }
431
432         if (delorig) {
433                 BMO_op_exec(bm, &delop);
434         }
435         
436         /* if not delorig, reverse loops of original face */
437         if (!delorig) {
438                 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
439                         if (BMO_elem_flag_test(bm, f, EXT_INPUT)) {
440                                 BM_face_normal_flip(bm, f);
441                         }
442                 }
443         }
444         
445         BMO_slot_copy(&dupeop, slots_out, "geom.out",
446                       op,      slots_out, "geom.out");
447
448         slot_edges_exclude = BMO_slot_get(op->slots_in, "edges_exclude");
449         for (e = BMO_iter_new(&siter, dupeop.slots_out, "boundary_map.out", 0); e; e = BMO_iter_step(&siter)) {
450                 BMVert *f_verts[4];
451 #ifdef USE_EDGE_REGION_FLAGS
452                 BMEdge *f_edges[4];
453 #endif
454
455                 /* this should always be wire, so this is mainly a speedup to avoid map lookup */
456                 if (BM_edge_is_wire(e) && BMO_slot_map_contains(slot_edges_exclude, e)) {
457                         BMVert *v1 = e->v1, *v2 = e->v2;
458
459                         /* The original edge was excluded,
460                          * this would result in a standalone wire edge - see [#30399] */
461                         BM_edge_kill(bm, e);
462
463                         /* kill standalone vertices from this edge - see [#32341] */
464                         if (!v1->e)
465                                 BM_vert_kill(bm, v1);
466                         if (!v2->e)
467                                 BM_vert_kill(bm, v2);
468
469                         continue;
470                 }
471
472                 /* skip creating face for excluded edges see [#35503] */
473                 if (BMO_slot_map_contains(slot_edges_exclude, e)) {
474                         /* simply skip creating the face */
475                         continue;
476                 }
477
478                 e_new = BMO_iter_map_value_ptr(&siter);
479
480                 if (!e_new) {
481                         continue;
482                 }
483
484                 /* orient loop to give same normal as a loop of newedge
485                  * if it exists (will be an extruded face),
486                  * else same normal as a loop of e, if it exists */
487                 if (!e_new->l)
488                         fwd = !e->l || !(e->l->v == e->v1);
489                 else
490                         fwd = (e_new->l->v == e_new->v1);
491
492                 
493                 if (fwd) {
494                         f_verts[0] = e->v1;
495                         f_verts[1] = e->v2;
496                         f_verts[2] = e_new->v2;
497                         f_verts[3] = e_new->v1;
498                 }
499                 else {
500                         f_verts[0] = e->v2;
501                         f_verts[1] = e->v1;
502                         f_verts[2] = e_new->v1;
503                         f_verts[3] = e_new->v2;
504                 }
505
506 #ifdef USE_EDGE_REGION_FLAGS
507                 /* handle new edges */
508                 f_edges[0] = e;
509                 f_edges[2] = e_new;
510
511                 f_edges[1] = BM_edge_exists(f_verts[1], f_verts[2]);
512                 if (f_edges[1] == NULL) {
513                         char e_hflag[2];
514                         bool e_hflag_ok = bm_extrude_region_edge_flag(f_verts[2], e_hflag);
515                         f_edges[1] = BM_edge_create(bm, f_verts[1], f_verts[2], NULL, BM_CREATE_NOP);
516                         if (e_hflag_ok) {
517                                 BM_elem_flag_enable(f_edges[1], e_hflag[0]);
518                                 BM_elem_flag_disable(f_edges[1], e_hflag[1]);
519                         }
520                 }
521
522                 f_edges[3] = BM_edge_exists(f_verts[3], f_verts[0]);
523                 if (f_edges[3] == NULL) {
524                         char e_hflag[2];
525                         bool e_hflag_ok = bm_extrude_region_edge_flag(f_verts[3], e_hflag);
526                         f_edges[3] = BM_edge_create(bm, f_verts[3], f_verts[0], NULL, BM_CREATE_NOP);
527                         if (e_hflag_ok) {
528                                 BM_elem_flag_enable(f_edges[3], e_hflag[0]);
529                                 BM_elem_flag_disable(f_edges[3], e_hflag[1]);
530                         }
531                 }
532
533                 f = BM_face_create(bm, f_verts, f_edges, 4, NULL, BM_CREATE_NOP);
534 #else
535                 f = BM_face_create_verts(bm, f_verts, 4, NULL, BM_CREATE_NOP, true);
536 #endif
537
538                 bm_extrude_copy_face_loop_attributes(bm, f);
539         }
540
541         /* link isolated vert */
542         for (v = BMO_iter_new(&siter, dupeop.slots_out, "isovert_map.out", 0); v; v = BMO_iter_step(&siter)) {
543                 BMVert *v2 = BMO_iter_map_value_ptr(&siter);
544
545                 /* not essential, but ensures face normals from extruded edges are contiguous */
546                 if (BM_vert_is_wire_endpoint(v)) {
547                         if (v->e->v1 == v) {
548                                 SWAP(BMVert *, v, v2);
549                         }
550                 }
551
552                 BM_edge_create(bm, v, v2, NULL, BM_CREATE_NO_DOUBLE);
553         }
554
555         /* cleanup */
556         if (delorig) BMO_op_finish(bm, &delop);
557         BMO_op_finish(bm, &dupeop);
558 }
559
560 /*
561  * Compute higher-quality vertex normals used by solidify.
562  * Only considers geometry in the marked solidify region.
563  * Note that this does not work so well for non-manifold
564  * regions.
565  */
566 static void calc_solidify_normals(BMesh *bm)
567 {
568         BMIter viter, eiter, fiter;
569         BMVert *v;
570         BMEdge *e;
571         BMFace *f, *f1, *f2;
572         float edge_normal[3];
573         int i;
574
575         /* can't use BM_edge_face_count because we need to count only marked faces */
576         int *edge_face_count = MEM_callocN(sizeof(int) * bm->totedge, __func__);
577
578         BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
579                 BM_elem_flag_enable(v, BM_ELEM_TAG);
580         }
581
582         BM_mesh_elem_index_ensure(bm, BM_EDGE);
583
584         BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
585                 if (!BMO_elem_flag_test(bm, f, FACE_MARK)) {
586                         continue;
587                 }
588
589                 BM_ITER_ELEM (e, &eiter, f, BM_EDGES_OF_FACE) {
590
591                         /* And mark all edges and vertices on the
592                          * marked faces */
593                         BMO_elem_flag_enable(bm, e, EDGE_MARK);
594                         BMO_elem_flag_enable(bm, e->v1, VERT_MARK);
595                         BMO_elem_flag_enable(bm, e->v2, VERT_MARK);
596                         edge_face_count[BM_elem_index_get(e)]++;
597                 }
598         }
599
600         BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
601                 if (!BMO_elem_flag_test(bm, e, EDGE_MARK)) {
602                         continue;
603                 }
604
605                 i = edge_face_count[BM_elem_index_get(e)]++;
606
607                 if (i == 0 || i > 2) {
608                         /* Edge & vertices are non-manifold even when considering
609                          * only marked faces */
610                         BMO_elem_flag_enable(bm, e, EDGE_NONMAN);
611                         BMO_elem_flag_enable(bm, e->v1, VERT_NONMAN);
612                         BMO_elem_flag_enable(bm, e->v2, VERT_NONMAN);
613                 }
614         }
615         MEM_freeN(edge_face_count);
616         edge_face_count = NULL; /* don't re-use */
617
618         BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
619                 if (!BM_vert_is_manifold(v)) {
620                         BMO_elem_flag_enable(bm, v, VERT_NONMAN);
621                         continue;
622                 }
623
624                 if (BMO_elem_flag_test(bm, v, VERT_MARK)) {
625                         zero_v3(v->no);
626                 }
627         }
628
629         BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
630
631                 /* If the edge is not part of a the solidify region
632                  * its normal should not be considered */
633                 if (!BMO_elem_flag_test(bm, e, EDGE_MARK)) {
634                         continue;
635                 }
636
637                 /* If the edge joins more than two marked faces high
638                  * quality normal computation won't work */
639                 if (BMO_elem_flag_test(bm, e, EDGE_NONMAN)) {
640                         continue;
641                 }
642
643                 f1 = f2 = NULL;
644
645                 BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
646                         if (BMO_elem_flag_test(bm, f, FACE_MARK)) {
647                                 if (f1 == NULL) {
648                                         f1 = f;
649                                 }
650                                 else {
651                                         BLI_assert(f2 == NULL);
652                                         f2 = f;
653                                 }
654                         }
655                 }
656
657                 BLI_assert(f1 != NULL);
658
659                 if (f2 != NULL) {
660                         const float angle = angle_normalized_v3v3(f1->no, f2->no);
661
662                         if (angle > 0.0f) {
663                                 /* two faces using this edge, calculate the edge normal
664                                  * using the angle between the faces as a weighting */
665                                 add_v3_v3v3(edge_normal, f1->no, f2->no);
666                                 normalize_v3(edge_normal);
667                                 mul_v3_fl(edge_normal, angle);
668                         }
669                         else {
670                                 /* can't do anything useful here!
671                                  * Set the face index for a vert in case it gets a zero normal */
672                                 BM_elem_flag_disable(e->v1, BM_ELEM_TAG);
673                                 BM_elem_flag_disable(e->v2, BM_ELEM_TAG);
674                                 continue;
675                         }
676                 }
677                 else {
678                         /* only one face attached to that edge */
679                         /* an edge without another attached- the weight on this is undefined,
680                          * M_PI_2 is 90d in radians and that seems good enough */
681                         copy_v3_v3(edge_normal, f1->no);
682                         mul_v3_fl(edge_normal, M_PI_2);
683                 }
684
685                 add_v3_v3(e->v1->no, edge_normal);
686                 add_v3_v3(e->v2->no, edge_normal);
687         }
688
689         /* normalize accumulated vertex normal */
690         BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
691                 if (!BMO_elem_flag_test(bm, v, VERT_MARK)) {
692                         continue;
693                 }
694
695                 if (BMO_elem_flag_test(bm, v, VERT_NONMAN)) {
696                         /* use standard normals for vertices connected to non-manifold edges */
697                         BM_vert_normal_update(v);
698                 }
699                 else if (normalize_v3(v->no) == 0.0f && !BM_elem_flag_test(v, BM_ELEM_TAG)) {
700                         /* exceptional case, totally flat. use the normal
701                          * of any marked face around the vertex */
702                         BM_ITER_ELEM (f, &fiter, v, BM_FACES_OF_VERT) {
703                                 if (BMO_elem_flag_test(bm, f, FACE_MARK)) {
704                                         break;
705                                 }
706                         }
707                         copy_v3_v3(v->no, f->no);
708                 }
709         }
710 }
711
712 static void solidify_add_thickness(BMesh *bm, const float dist)
713 {
714         BMFace *f;
715         BMVert *v;
716         BMLoop *l;
717         BMIter iter, loopIter;
718         float *vert_angles = MEM_callocN(sizeof(float) * bm->totvert * 2, "solidify"); /* 2 in 1 */
719         float *vert_accum = vert_angles + bm->totvert;
720         int i, index;
721
722         BLI_buffer_declare_static(float,   face_angles_buf, BLI_BUFFER_NOP, BM_DEFAULT_NGON_STACK_SIZE);
723         BLI_buffer_declare_static(float *, verts_buf,       BLI_BUFFER_NOP, BM_DEFAULT_NGON_STACK_SIZE);
724
725         BM_mesh_elem_index_ensure(bm, BM_VERT);
726
727         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
728                 if (BMO_elem_flag_test(bm, f, FACE_MARK)) {
729
730                         /* array for passing verts to angle_poly_v3 */
731                         float  *face_angles = BLI_buffer_resize_data(&face_angles_buf, float, f->len);
732                         /* array for receiving angles from angle_poly_v3 */
733                         float **verts = BLI_buffer_resize_data(&verts_buf, float *, f->len);
734
735                         BM_ITER_ELEM_INDEX (l, &loopIter, f, BM_LOOPS_OF_FACE, i) {
736                                 verts[i] = l->v->co;
737                         }
738
739                         angle_poly_v3(face_angles, (const float **)verts, f->len);
740
741                         i = 0;
742                         BM_ITER_ELEM (l, &loopIter, f, BM_LOOPS_OF_FACE) {
743                                 v = l->v;
744                                 index = BM_elem_index_get(v);
745                                 vert_accum[index] += face_angles[i];
746                                 vert_angles[index] += shell_v3v3_normalized_to_dist(v->no, f->no) * face_angles[i];
747                                 i++;
748                         }
749                 }
750         }
751
752         BLI_buffer_free(&face_angles_buf);
753         BLI_buffer_free(&verts_buf);
754
755         BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
756                 index = BM_elem_index_get(v);
757                 if (vert_accum[index]) { /* zero if unselected */
758                         madd_v3_v3fl(v->co, v->no, dist * (vert_angles[index] / vert_accum[index]));
759                 }
760         }
761
762         MEM_freeN(vert_angles);
763 }
764
765 void bmo_solidify_face_region_exec(BMesh *bm, BMOperator *op)
766 {
767         BMOperator extrudeop;
768         BMOperator reverseop;
769         float thickness;
770
771         thickness = BMO_slot_float_get(op->slots_in, "thickness");
772
773         /* Flip original faces (so the shell is extruded inward) */
774         BMO_op_init(bm, &reverseop, op->flag, "reverse_faces");
775         BMO_slot_copy(op,         slots_in, "geom",
776                       &reverseop, slots_in, "faces");
777         BMO_op_exec(bm, &reverseop);
778         BMO_op_finish(bm, &reverseop);
779
780         /* Extrude the region */
781         BMO_op_initf(bm, &extrudeop, op->flag, "extrude_face_region use_keep_orig=%b", true);
782         BMO_slot_copy(op,         slots_in, "geom",
783                       &extrudeop, slots_in, "geom");
784         BMO_op_exec(bm, &extrudeop);
785
786         /* Push the verts of the extruded faces inward to create thickness */
787         BMO_slot_buffer_flag_enable(bm, extrudeop.slots_out, "geom.out", BM_FACE, FACE_MARK);
788         calc_solidify_normals(bm);
789         solidify_add_thickness(bm, thickness);
790
791         BMO_slot_copy(&extrudeop, slots_out, "geom.out",
792                       op,         slots_out, "geom.out");
793
794         BMO_op_finish(bm, &extrudeop);
795 }