Blender  V3.3
bmo_extrude.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #include "MEM_guardedalloc.h"
10 
11 #include "DNA_meshdata_types.h"
12 
13 #include "BLI_buffer.h"
14 #include "BLI_math.h"
15 
16 #include "BKE_customdata.h"
17 
18 #include "bmesh.h"
19 
20 #include "intern/bmesh_operators_private.h" /* own include */
21 
22 #define USE_EDGE_REGION_FLAGS
23 
24 enum {
25  EXT_INPUT = 1,
26  EXT_KEEP = 2,
27  EXT_DEL = 4,
28  EXT_TAG = 8,
29 };
30 
31 #define VERT_MARK 1
32 #define EDGE_MARK 1
33 #define FACE_MARK 1
34 #define VERT_NONMAN 2
35 #define EDGE_NONMAN 2
36 
38 {
39  const bool use_select_history = BMO_slot_bool_get(op->slots_in, "use_select_history");
40  GHash *select_history_map = NULL;
41 
42  BMOIter siter;
43  BMFace *f_org;
44 
45  if (use_select_history) {
46  select_history_map = BM_select_history_map_create(bm);
47  }
48 
49  BMO_ITER (f_org, &siter, op->slots_in, "faces", BM_FACE) {
50  BMFace *f_new;
51  BMLoop *l_org, *l_org_first;
52  BMLoop *l_new;
53 
55 
56  f_new = BM_face_copy(bm, bm, f_org, true, true);
58 
59  if (select_history_map) {
60  BMEditSelection *ese;
61  ese = BLI_ghash_lookup(select_history_map, f_org);
62  if (ese) {
63  ese->ele = (BMElem *)f_new;
64  }
65  }
66 
67  l_org = l_org_first = BM_FACE_FIRST_LOOP(f_org);
68  l_new = BM_FACE_FIRST_LOOP(f_new);
69 
70  do {
71  BMFace *f_side;
72  BMLoop *l_side_iter;
73 
74  BM_elem_attrs_copy(bm, bm, l_org, l_new);
75 
76  f_side = BM_face_create_quad_tri(
77  bm, l_org->next->v, l_new->next->v, l_new->v, l_org->v, f_org, BM_CREATE_NOP);
78 
79  l_side_iter = BM_FACE_FIRST_LOOP(f_side);
80 
81  BM_elem_attrs_copy(bm, bm, l_org->next, l_side_iter);
82  l_side_iter = l_side_iter->next;
83  BM_elem_attrs_copy(bm, bm, l_org->next, l_side_iter);
84  l_side_iter = l_side_iter->next;
85  BM_elem_attrs_copy(bm, bm, l_org, l_side_iter);
86  l_side_iter = l_side_iter->next;
87  BM_elem_attrs_copy(bm, bm, l_org, l_side_iter);
88 
89  if (select_history_map) {
90  BMEditSelection *ese;
91 
92  ese = BLI_ghash_lookup(select_history_map, l_org->v);
93  if (ese) {
94  ese->ele = (BMElem *)l_new->v;
95  }
96  ese = BLI_ghash_lookup(select_history_map, l_org->e);
97  if (ese) {
98  ese->ele = (BMElem *)l_new->e;
99  }
100  }
101 
102  } while (((void)(l_new = l_new->next), (l_org = l_org->next)) != l_org_first);
103  }
104 
105  if (select_history_map) {
106  BLI_ghash_free(select_history_map, NULL, NULL);
107  }
108 
109  BMO_op_callf(bm, op->flag, "delete geom=%ff context=%i", EXT_DEL, DEL_ONLYFACES);
111 }
112 
124 {
125  /* edge we are extruded from */
126  BMLoop *l_first_0 = BM_FACE_FIRST_LOOP(f);
127  BMLoop *l_first_1 = l_first_0->next;
128  BMLoop *l_first_2 = l_first_1->next;
129  BMLoop *l_first_3 = l_first_2->next;
130 
131  BMLoop *l_other_0;
132  BMLoop *l_other_1;
133 
134  if (UNLIKELY(l_first_0 == l_first_0->radial_next)) {
135  return;
136  }
137 
138  l_other_0 = BM_edge_other_loop(l_first_0->e, l_first_0);
139  l_other_1 = BM_edge_other_loop(l_first_0->e, l_first_1);
140 
141  /* copy data */
142  BM_elem_attrs_copy(bm, bm, l_other_0->f, f);
143  BM_elem_flag_disable(f, BM_ELEM_HIDDEN); /* possibly we copy from a hidden face */
144 
145  BM_elem_attrs_copy(bm, bm, l_other_0, l_first_0);
146  BM_elem_attrs_copy(bm, bm, l_other_0, l_first_3);
147 
148  BM_elem_attrs_copy(bm, bm, l_other_1, l_first_1);
149  BM_elem_attrs_copy(bm, bm, l_other_1, l_first_2);
150 }
151 
152 /* Disable the skin root flag on the input vert, assumes that the vert
153  * data includes an CD_MVERT_SKIN layer */
155 {
156  MVertSkin *vs;
157 
159  vs->flag &= ~MVERT_SKIN_ROOT;
160 }
161 
163 {
164  BMOIter siter;
165  BMOperator dupeop;
166  BMFace *f;
167  BMEdge *e, *e_new;
168  const bool use_normal_flip = BMO_slot_bool_get(op->slots_in, "use_normal_flip");
169 
170  BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
174  }
175 
177  &dupeop,
178  op->flag,
179  "duplicate geom=%fve use_select_history=%b",
180  EXT_INPUT,
181  BMO_slot_bool_get(op->slots_in, "use_select_history"));
182 
183  BMO_op_exec(bm, &dupeop);
184 
185  /* disable root flag on all new skin nodes */
187  BMVert *v;
188  BMO_ITER (v, &siter, dupeop.slots_out, "geom.out", BM_VERT) {
190  }
191  }
192 
193  for (e = BMO_iter_new(&siter, dupeop.slots_out, "boundary_map.out", 0); e;
194  e = BMO_iter_step(&siter)) {
195  BMVert *f_verts[4];
196  e_new = BMO_iter_map_value_ptr(&siter);
197 
198  const bool edge_normal_flip = !(e->l && e->v1 != e->l->v);
199  if (edge_normal_flip == use_normal_flip) {
200  f_verts[0] = e->v1;
201  f_verts[1] = e->v2;
202  f_verts[2] = e_new->v2;
203  f_verts[3] = e_new->v1;
204  }
205  else {
206  f_verts[0] = e->v2;
207  f_verts[1] = e->v1;
208  f_verts[2] = e_new->v1;
209  f_verts[3] = e_new->v2;
210  }
211  /* not sure what to do about example face, pass NULL for now */
212  f = BM_face_create_verts(bm, f_verts, 4, NULL, BM_CREATE_NOP, true);
214 
215  if (BMO_edge_flag_test(bm, e, EXT_INPUT)) {
216  e = e_new;
217  }
218 
223  }
224 
225  BMO_op_finish(bm, &dupeop);
226 
228 }
229 
231 {
232  const bool use_select_history = BMO_slot_bool_get(op->slots_in, "use_select_history");
233  BMOIter siter;
234  BMVert *v, *dupev;
235  BMEdge *e;
236  const bool has_vskin = CustomData_has_layer(&bm->vdata, CD_MVERT_SKIN);
237  GHash *select_history_map = NULL;
238 
239  if (use_select_history) {
240  select_history_map = BM_select_history_map_create(bm);
241  }
242 
243  for (v = BMO_iter_new(&siter, op->slots_in, "verts", BM_VERT); v; v = BMO_iter_step(&siter)) {
244  dupev = BM_vert_create(bm, v->co, v, BM_CREATE_NOP);
246 
247  if (has_vskin) {
249  }
250 
251  if (select_history_map) {
252  BMEditSelection *ese;
253  ese = BLI_ghash_lookup(select_history_map, v);
254  if (ese) {
255  ese->ele = (BMElem *)dupev;
256  }
257  }
258 
259  /* not essential, but ensures face normals from extruded edges are contiguous */
260  if (BM_vert_is_wire_endpoint(v)) {
261  if (v->e->v1 == v) {
262  SWAP(BMVert *, v, dupev);
263  }
264  }
265 
266  e = BM_edge_create(bm, v, dupev, NULL, BM_CREATE_NOP);
268  }
269 
270  if (select_history_map) {
271  BLI_ghash_free(select_history_map, NULL, NULL);
272  }
273 
276 }
277 
278 #ifdef USE_EDGE_REGION_FLAGS
283 static bool bm_extrude_region_edge_flag(const BMVert *v, char r_e_hflag[2])
284 {
285  BMEdge *e_iter;
286  const char hflag_enable = BM_ELEM_SEAM;
287  const char hflag_disable = BM_ELEM_SMOOTH;
288  bool ok = false;
289 
290  r_e_hflag[0] = 0x0;
291  r_e_hflag[1] = 0xff;
292 
293  /* clear flags on both disks */
294  e_iter = v->e;
295  do {
296  if (e_iter->l && !BM_edge_is_boundary(e_iter)) {
297  r_e_hflag[0] |= e_iter->head.hflag;
298  r_e_hflag[1] &= e_iter->head.hflag;
299  ok = true;
300  }
301  } while ((e_iter = BM_DISK_EDGE_NEXT(e_iter, v)) != v->e);
302 
303  if (ok) {
304  r_e_hflag[0] &= hflag_enable;
305  r_e_hflag[1] = hflag_disable & ~r_e_hflag[1];
306  }
307  return ok;
308 }
309 #endif /* USE_EDGE_REGION_FLAGS */
310 
312 {
313  BMOperator dupeop, delop;
314  BMOIter siter;
315  BMIter iter, fiter, viter;
316  BMEdge *e, *e_new;
317  BMVert *v;
318  BMFace *f;
319  bool found, delorig = false;
320  BMOpSlot *slot_facemap_out;
321  BMOpSlot *slot_edges_exclude;
322  const bool use_normal_flip = BMO_slot_bool_get(op->slots_in, "use_normal_flip");
323  const bool use_normal_from_adjacent = BMO_slot_bool_get(op->slots_in,
324  "use_normal_from_adjacent");
325  const bool use_dissolve_ortho_edges = BMO_slot_bool_get(op->slots_in,
326  "use_dissolve_ortho_edges");
327 
328  /* initialize our sub-operators */
330  &dupeop,
331  op->flag,
332  "duplicate use_select_history=%b",
333  BMO_slot_bool_get(op->slots_in, "use_select_history"));
334 
336 
337  /* if one flagged face is bordered by an un-flagged face, then we delete
338  * original geometry unless caller explicitly asked to keep it. */
339  if (!BMO_slot_bool_get(op->slots_in, "use_keep_orig")) {
340  BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
341 
342  int edge_face_tot;
343 
344  if (!BMO_edge_flag_test(bm, e, EXT_INPUT)) {
345  continue;
346  }
347 
348  found = false; /* found a face that isn't input? */
349  edge_face_tot = 0; /* edge/face count */
350 
351  BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
352  if (!BMO_face_flag_test(bm, f, EXT_INPUT)) {
353  found = true;
354  delorig = true;
355  break;
356  }
357 
358  edge_face_tot++;
359  }
360 
361  if ((edge_face_tot > 1) && (found == false)) {
362  /* edge has a face user, that face isn't extrude input */
364  }
365  }
366  }
367 
368  /* calculate verts to delete */
369  BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
370  if (v->e) { /* only deal with verts attached to geometry T33651. */
371  found = false;
372 
373  BM_ITER_ELEM (e, &viter, v, BM_EDGES_OF_VERT) {
375  found = true;
376  break;
377  }
378  }
379 
380  /* avoid an extra loop */
381  if (found == true) {
382  BM_ITER_ELEM (f, &viter, v, BM_FACES_OF_VERT) {
383  if (!BMO_face_flag_test(bm, f, EXT_INPUT)) {
384  found = true;
385  break;
386  }
387  }
388  }
389 
390  if (found == false) {
392  }
393  }
394  }
395 
396  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
397  if (BMO_face_flag_test(bm, f, EXT_INPUT)) {
399  }
400  }
401 
402  if (delorig == true) {
403  BMO_op_initf(bm, &delop, op->flag, "delete geom=%fvef context=%i", EXT_DEL, DEL_ONLYTAGGED);
404  }
405 
406  BMO_slot_copy(op, slots_in, "geom", &dupeop, slots_in, "geom");
407  BMO_op_exec(bm, &dupeop);
408 
409  /* disable root flag on all new skin nodes */
411  BMO_ITER (v, &siter, dupeop.slots_out, "geom.out", BM_VERT) {
413  }
414  }
415 
416  slot_facemap_out = BMO_slot_get(dupeop.slots_out, "face_map.out");
418  bm->act_face = BMO_slot_map_elem_get(slot_facemap_out, bm->act_face);
419  }
420 
421  if (delorig) {
422  BMO_op_exec(bm, &delop);
423  }
424 
425  /* if not delorig, reverse loops of original face */
426  if (!delorig) {
427  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
428  if (BMO_face_flag_test(bm, f, EXT_INPUT)) {
430  }
431  }
432  }
433 
434  BMVert **dissolve_verts = NULL;
435  int dissolve_verts_len = 0;
436  float average_normal[3];
437  if (use_dissolve_ortho_edges) {
438  /* Calc average normal. */
439  zero_v3(average_normal);
440  BMO_ITER (f, &siter, dupeop.slots_out, "geom.out", BM_FACE) {
441  add_v3_v3(average_normal, f->no);
442  }
443  if (normalize_v3(average_normal) == 0.0f) {
444  average_normal[2] = 1.0f;
445  }
446 
447  /* Allocate array to store possible vertices that will be dissolved. */
448  int boundary_edges_len = BMO_slot_map_len(dupeop.slots_out, "boundary_map.out");
449  /* We do not know the real number of boundary vertices. */
450  int boundary_verts_len_maybe = 2 * boundary_edges_len;
451  dissolve_verts = MEM_mallocN(boundary_verts_len_maybe * sizeof(*dissolve_verts), __func__);
452  }
453 
454  BMO_slot_copy(&dupeop, slots_out, "geom.out", op, slots_out, "geom.out");
455 
456  slot_edges_exclude = BMO_slot_get(op->slots_in, "edges_exclude");
457  for (e = BMO_iter_new(&siter, dupeop.slots_out, "boundary_map.out", 0); e;
458  e = BMO_iter_step(&siter)) {
459  BMVert *f_verts[4];
460 #ifdef USE_EDGE_REGION_FLAGS
461  BMEdge *f_edges[4];
462 #endif
463 
464  /* this should always be wire, so this is mainly a speedup to avoid map lookup */
465  if (BM_edge_is_wire(e) && BMO_slot_map_contains(slot_edges_exclude, e)) {
466  BMVert *v1 = e->v1, *v2 = e->v2;
467 
468  /* The original edge was excluded,
469  * this would result in a standalone wire edge - see T30399. */
470  BM_edge_kill(bm, e);
471 
472  /* kill standalone vertices from this edge - see T32341. */
473  if (!v1->e) {
474  BM_vert_kill(bm, v1);
475  }
476  if (!v2->e) {
477  BM_vert_kill(bm, v2);
478  }
479 
480  continue;
481  }
482 
483  /* skip creating face for excluded edges see T35503. */
484  if (BMO_slot_map_contains(slot_edges_exclude, e)) {
485  /* simply skip creating the face */
486  continue;
487  }
488 
489  e_new = BMO_iter_map_value_ptr(&siter);
490 
491  if (!e_new) {
492  continue;
493  }
494 
495  BMFace *join_face = NULL;
496  if (use_dissolve_ortho_edges) {
497  if (BM_edge_is_boundary(e)) {
498  join_face = e->l->f;
499  if (fabs(dot_v3v3(average_normal, join_face->no)) > 0.0001f) {
500  join_face = NULL;
501  }
502  }
503  }
504 
505  bool edge_normal_flip;
506  if (use_normal_from_adjacent == false) {
507  /* Orient loop to give same normal as a loop of 'e_new'
508  * if it exists (will be one of the faces from the region),
509  * else same normal as a loop of e, if it exists. */
510  edge_normal_flip = !(e_new->l ? (e_new->l->v == e_new->v1) : (!e->l || !(e->l->v == e->v1)));
511  }
512  else {
513  /* Special case, needed for repetitive extrusions
514  * that use the normals from the previously created faces. */
515  edge_normal_flip = !(e->l && e->v1 != e->l->v);
516  }
517 
518  if (edge_normal_flip == use_normal_flip) {
519  f_verts[0] = e->v1;
520  f_verts[1] = e->v2;
521  f_verts[2] = e_new->v2;
522  f_verts[3] = e_new->v1;
523  }
524  else {
525  f_verts[0] = e->v2;
526  f_verts[1] = e->v1;
527  f_verts[2] = e_new->v1;
528  f_verts[3] = e_new->v2;
529  }
530 
531 #ifdef USE_EDGE_REGION_FLAGS
532  /* handle new edges */
533  f_edges[0] = e;
534  f_edges[2] = e_new;
535 
536  f_edges[1] = BM_edge_exists(f_verts[1], f_verts[2]);
537  if (f_edges[1] == NULL) {
538  char e_hflag[2];
539  bool e_hflag_ok = bm_extrude_region_edge_flag(f_verts[2], e_hflag);
540  f_edges[1] = BM_edge_create(bm, f_verts[1], f_verts[2], NULL, BM_CREATE_NOP);
541  if (e_hflag_ok) {
542  BM_elem_flag_enable(f_edges[1], e_hflag[0]);
543  BM_elem_flag_disable(f_edges[1], e_hflag[1]);
544  }
545  }
546 
547  f_edges[3] = BM_edge_exists(f_verts[3], f_verts[0]);
548  if (f_edges[3] == NULL) {
549  char e_hflag[2];
550  bool e_hflag_ok = bm_extrude_region_edge_flag(f_verts[3], e_hflag);
551  f_edges[3] = BM_edge_create(bm, f_verts[3], f_verts[0], NULL, BM_CREATE_NOP);
552  if (e_hflag_ok) {
553  BM_elem_flag_enable(f_edges[3], e_hflag[0]);
554  BM_elem_flag_disable(f_edges[3], e_hflag[1]);
555  }
556  }
557 
558  f = BM_face_create(bm, f_verts, f_edges, 4, NULL, BM_CREATE_NOP);
559 #else
560  f = BM_face_create_verts(bm, f_verts, 4, NULL, BM_CREATE_NOP, true);
561 #endif
562 
564  if (join_face) {
565  BMVert *v1 = e->v1;
566  BMVert *v2 = e->v2;
567  if (!BMO_elem_flag_test(bm, v1, EXT_TAG)) {
569  dissolve_verts[dissolve_verts_len++] = v1;
570  }
571  if (!BMO_elem_flag_test(bm, v2, EXT_TAG)) {
573  dissolve_verts[dissolve_verts_len++] = v2;
574  }
575  /* Tag the edges that can collapse. */
576  BMO_elem_flag_enable(bm, f_edges[0], EXT_TAG);
577  BMO_elem_flag_enable(bm, f_edges[1], EXT_TAG);
578  bmesh_kernel_join_face_kill_edge(bm, join_face, f, e);
579  }
580  }
581 
582  /* link isolated vert */
583  for (v = BMO_iter_new(&siter, dupeop.slots_out, "isovert_map.out", 0); v;
584  v = BMO_iter_step(&siter)) {
585  BMVert *v2 = BMO_iter_map_value_ptr(&siter);
586 
587  /* not essential, but ensures face normals from extruded edges are contiguous */
588  if (BM_vert_is_wire_endpoint(v)) {
589  if (v->e->v1 == v) {
590  SWAP(BMVert *, v, v2);
591  }
592  }
593 
595  }
596 
597  if (dissolve_verts) {
598  BMVert **v_iter = &dissolve_verts[0];
599  for (int i = dissolve_verts_len; i--; v_iter++) {
600  v = *v_iter;
601  e = v->e;
602  BMEdge *e_other = BM_DISK_EDGE_NEXT(e, v);
603  if ((e_other == e) || (BM_DISK_EDGE_NEXT(e_other, v) == e)) {
604  /* Loose edge or BMVert is edge pair. */
605  BM_edge_collapse(bm, BMO_elem_flag_test(bm, e, EXT_TAG) ? e : e_other, v, true, true);
606  }
607  else {
609  }
610  }
611  MEM_freeN(dissolve_verts);
612  }
613 
614  /* cleanup */
615  if (delorig) {
616  BMO_op_finish(bm, &delop);
617  }
618  BMO_op_finish(bm, &dupeop);
619 }
620 
621 /*
622  * Compute higher-quality vertex normals used by solidify.
623  * Only considers geometry in the marked solidify region.
624  * Note that this does not work so well for non-manifold
625  * regions.
626  */
628 {
629  BMIter viter, eiter, fiter;
630  BMVert *v;
631  BMEdge *e;
632  BMFace *f, *f1, *f2;
633  float edge_normal[3];
634  int i;
635 
636  /* can't use BM_edge_face_count because we need to count only marked faces */
637  int *edge_face_count = MEM_callocN(sizeof(int) * bm->totedge, __func__);
638 
639  BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
641  }
642 
644 
645  BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
646  if (!BMO_face_flag_test(bm, f, FACE_MARK)) {
647  continue;
648  }
649 
650  BM_ITER_ELEM (e, &eiter, f, BM_EDGES_OF_FACE) {
651 
652  /* And mark all edges and vertices on the
653  * marked faces */
657  edge_face_count[BM_elem_index_get(e)]++;
658  }
659  }
660 
661  BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
662  if (!BMO_edge_flag_test(bm, e, EDGE_MARK)) {
663  continue;
664  }
665 
666  i = edge_face_count[BM_elem_index_get(e)]++;
667 
668  if (i == 0 || i > 2) {
669  /* Edge & vertices are non-manifold even when considering
670  * only marked faces */
674  }
675  }
676  MEM_freeN(edge_face_count);
677  edge_face_count = NULL; /* don't re-use */
678 
679  BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
680  if (!BM_vert_is_manifold(v)) {
682  continue;
683  }
684 
685  if (BMO_vert_flag_test(bm, v, VERT_MARK)) {
686  zero_v3(v->no);
687  }
688  }
689 
690  BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
691 
692  /* If the edge is not part of a the solidify region
693  * its normal should not be considered */
694  if (!BMO_edge_flag_test(bm, e, EDGE_MARK)) {
695  continue;
696  }
697 
698  /* If the edge joins more than two marked faces high
699  * quality normal computation won't work */
701  continue;
702  }
703 
704  f1 = f2 = NULL;
705 
706  BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
707  if (BMO_face_flag_test(bm, f, FACE_MARK)) {
708  if (f1 == NULL) {
709  f1 = f;
710  }
711  else {
712  BLI_assert(f2 == NULL);
713  f2 = f;
714  }
715  }
716  }
717 
718  BLI_assert(f1 != NULL);
719 
720  if (f2 != NULL) {
721  const float angle = angle_normalized_v3v3(f1->no, f2->no);
722 
723  if (angle > 0.0f) {
724  /* two faces using this edge, calculate the edge normal
725  * using the angle between the faces as a weighting */
726  add_v3_v3v3(edge_normal, f1->no, f2->no);
727  normalize_v3_length(edge_normal, angle);
728  }
729  else {
730  /* can't do anything useful here!
731  * Set the face index for a vert in case it gets a zero normal */
734  continue;
735  }
736  }
737  else {
738  /* only one face attached to that edge */
739  /* an edge without another attached- the weight on this is undefined,
740  * M_PI_2 is 90d in radians and that seems good enough */
741  copy_v3_v3(edge_normal, f1->no);
742  mul_v3_fl(edge_normal, M_PI_2);
743  }
744 
745  add_v3_v3(e->v1->no, edge_normal);
746  add_v3_v3(e->v2->no, edge_normal);
747  }
748 
749  /* normalize accumulated vertex normal */
750  BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
751  if (!BMO_vert_flag_test(bm, v, VERT_MARK)) {
752  continue;
753  }
754 
756  /* use standard normals for vertices connected to non-manifold edges */
758  }
759  else if (normalize_v3(v->no) == 0.0f && !BM_elem_flag_test(v, BM_ELEM_TAG)) {
760  /* exceptional case, totally flat. use the normal
761  * of any marked face around the vertex */
762  BM_ITER_ELEM (f, &fiter, v, BM_FACES_OF_VERT) {
763  if (BMO_face_flag_test(bm, f, FACE_MARK)) {
764  break;
765  }
766  }
767  copy_v3_v3(v->no, f->no);
768  }
769  }
770 }
771 
772 static void solidify_add_thickness(BMesh *bm, const float dist)
773 {
774  BMFace *f;
775  BMVert *v;
776  BMLoop *l;
777  BMIter iter, loopIter;
778  float *vert_angles = MEM_callocN(sizeof(float) * bm->totvert * 2, "solidify"); /* 2 in 1 */
779  float *vert_accum = vert_angles + bm->totvert;
780  int i, index;
781 
784 
786 
787  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
788  if (BMO_face_flag_test(bm, f, FACE_MARK)) {
789 
790  /* array for passing verts to angle_poly_v3 */
791  float *face_angles = BLI_buffer_reinit_data(&face_angles_buf, float, f->len);
792  /* array for receiving angles from angle_poly_v3 */
793  float **verts = BLI_buffer_reinit_data(&verts_buf, float *, f->len);
794 
795  BM_ITER_ELEM_INDEX (l, &loopIter, f, BM_LOOPS_OF_FACE, i) {
796  verts[i] = l->v->co;
797  }
798 
799  angle_poly_v3(face_angles, (const float **)verts, f->len);
800 
801  i = 0;
802  BM_ITER_ELEM (l, &loopIter, f, BM_LOOPS_OF_FACE) {
803  v = l->v;
804  index = BM_elem_index_get(v);
805  vert_accum[index] += face_angles[i];
806  vert_angles[index] += shell_v3v3_normalized_to_dist(v->no, f->no) * face_angles[i];
807  i++;
808  }
809  }
810  }
811 
812  BLI_buffer_free(&face_angles_buf);
813  BLI_buffer_free(&verts_buf);
814 
815  BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
816  index = BM_elem_index_get(v);
817  if (vert_accum[index]) { /* zero if unselected */
818  madd_v3_v3fl(v->co, v->no, dist * (vert_angles[index] / vert_accum[index]));
819  }
820  }
821 
822  MEM_freeN(vert_angles);
823 }
824 
826 {
827  BMOperator extrudeop;
828  BMOperator reverseop;
829  float thickness;
830 
831  thickness = BMO_slot_float_get(op->slots_in, "thickness");
832 
833  /* Flip original faces (so the shell is extruded inward) */
834  BMO_op_init(bm, &reverseop, op->flag, "reverse_faces");
835  BMO_slot_bool_set(reverseop.slots_in, "flip_multires", true);
836  BMO_slot_copy(op, slots_in, "geom", &reverseop, slots_in, "faces");
837  BMO_op_exec(bm, &reverseop);
838  BMO_op_finish(bm, &reverseop);
839 
840  /* Extrude the region */
841  BMO_op_initf(bm, &extrudeop, op->flag, "extrude_face_region use_keep_orig=%b", true);
842  BMO_slot_copy(op, slots_in, "geom", &extrudeop, slots_in, "geom");
843  BMO_op_exec(bm, &extrudeop);
844 
845  /* Push the verts of the extruded faces inward to create thickness */
846  BMO_slot_buffer_flag_enable(bm, extrudeop.slots_out, "geom.out", BM_FACE, FACE_MARK);
848  solidify_add_thickness(bm, thickness);
849 
850  BMO_slot_copy(&extrudeop, slots_out, "geom.out", op, slots_out, "geom.out");
851 
852  BMO_op_finish(bm, &extrudeop);
853 }
CustomData interface, see also DNA_customdata_types.h.
bool CustomData_has_layer(const struct CustomData *data, int type)
void * CustomData_bmesh_get(const struct CustomData *data, void *block, int type)
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_buffer_reinit_data(buffer_, type_, new_count_)
Definition: BLI_buffer.h:47
@ BLI_BUFFER_NOP
Definition: BLI_buffer.h:21
#define BLI_buffer_declare_static(type_, name_, flag_, static_count_)
Definition: BLI_buffer.h:25
#define BLI_buffer_free(name_)
Definition: BLI_buffer.h:94
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:734
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:863
#define M_PI_2
Definition: BLI_math_base.h:23
MINLINE float shell_v3v3_normalized_to_dist(const float a[3], const float b[3])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE float normalize_v3(float r[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE float normalize_v3_length(float r[3], float unit_scale)
void angle_poly_v3(float *angles, const float *verts[3], int len)
Definition: math_vector.c:573
MINLINE void zero_v3(float r[3])
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
Definition: math_vector.c:445
MINLINE void add_v3_v3(float r[3], const float a[3])
#define SWAP(type, a, b)
#define UNLIKELY(x)
@ CD_MVERT_SKIN
@ MVERT_SKIN_ROOT
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
Read Guarded memory(de)allocation.
#define BM_DISK_EDGE_NEXT(e, v)
Definition: bmesh_class.h:625
#define BM_DEFAULT_NGON_STACK_SIZE
Definition: bmesh_class.h:640
#define BM_ALL_NOLOOP
Definition: bmesh_class.h:411
#define BM_FACE_FIRST_LOOP(p)
Definition: bmesh_class.h:622
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_VERT
Definition: bmesh_class.h:383
@ BM_EDGE
Definition: bmesh_class.h:384
@ BM_ELEM_HIDDEN
Definition: bmesh_class.h:472
@ BM_ELEM_SEAM
Definition: bmesh_class.h:473
@ BM_ELEM_SMOOTH
Definition: bmesh_class.h:477
@ BM_ELEM_TAG
Definition: bmesh_class.h:484
void BM_elem_attrs_copy(BMesh *bm_src, BMesh *bm_dst, const void *ele_src, void *ele_dst)
BMFace * BM_face_create_quad_tri(BMesh *bm, BMVert *v1, BMVert *v2, BMVert *v3, BMVert *v4, const BMFace *f_example, const eBMCreateFlag create_flag)
Make Quad/Triangle.
BMVert * BM_vert_create(BMesh *bm, const float co[3], const BMVert *v_example, const eBMCreateFlag create_flag)
Main function for creating a new vertex.
Definition: bmesh_core.c:41
BMFace * BM_face_create_verts(BMesh *bm, BMVert **vert_arr, const int len, const BMFace *f_example, const eBMCreateFlag create_flag, const bool create_edges)
Definition: bmesh_core.c:464
BMFace * bmesh_kernel_join_face_kill_edge(BMesh *bm, BMFace *f1, BMFace *f2, BMEdge *e)
Join Face Kill Edge (JFKE)
Definition: bmesh_core.c:1881
void BM_vert_kill(BMesh *bm, BMVert *v)
Definition: bmesh_core.c:939
void BM_edge_kill(BMesh *bm, BMEdge *e)
Definition: bmesh_core.c:927
BMFace * BM_face_copy(BMesh *bm_dst, BMesh *bm_src, BMFace *f, const bool copy_verts, const bool copy_edges)
Definition: bmesh_core.c:279
BMFace * BM_face_create(BMesh *bm, BMVert **verts, BMEdge **edges, const int len, const BMFace *f_example, const eBMCreateFlag create_flag)
Definition: bmesh_core.c:395
BMEdge * BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *e_example, const eBMCreateFlag create_flag)
Main function for creating a new edge.
Definition: bmesh_core.c:123
@ BM_CREATE_NOP
Definition: bmesh_core.h:12
@ BM_CREATE_NO_DOUBLE
Definition: bmesh_core.h:14
#define BM_elem_index_get(ele)
Definition: bmesh_inline.h:110
#define BM_elem_flag_disable(ele, hflag)
Definition: bmesh_inline.h:15
#define BM_elem_flag_test(ele, hflag)
Definition: bmesh_inline.h:12
#define BM_elem_flag_enable(ele, hflag)
Definition: bmesh_inline.h:14
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
@ BM_FACES_OF_EDGE
@ BM_FACES_OF_VERT
@ BM_EDGES_OF_MESH
@ BM_VERTS_OF_MESH
@ BM_FACES_OF_MESH
@ BM_EDGES_OF_VERT
@ BM_EDGES_OF_FACE
@ BM_LOOPS_OF_FACE
#define BM_ITER_ELEM_INDEX(ele, iter, data, itype, indexvar)
ATTR_WARN_UNUSED_RESULT BMesh * bm
GHash * BM_select_history_map_create(BMesh *bm)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
Definition: bmesh_mesh.cc:446
BMVert * BM_edge_collapse(BMesh *bm, BMEdge *e_kill, BMVert *v_kill, const bool do_del, const bool kill_degenerate_faces)
Definition: bmesh_mods.c:442
void BMO_slot_buffer_flag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, short oflag)
BMO_FLAG_BUFFER.
#define BMO_edge_flag_test(bm, e, oflag)
void * BMO_iter_step(BMOIter *iter)
#define BMO_edge_flag_enable(bm, e, oflag)
@ DEL_ONLYTAGGED
@ DEL_ONLYFACES
int BMO_slot_map_len(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
float BMO_slot_float_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void * BMO_iter_new(BMOIter *iter, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char restrictmask)
New Iterator.
#define BMO_vert_flag_enable(bm, e, oflag)
void BMO_op_exec(BMesh *bm, BMOperator *op)
BMESH OPSTACK EXEC OP.
void BMO_slot_buffer_from_enabled_flag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, short oflag)
#define BMO_face_flag_enable(bm, e, oflag)
#define BMO_slot_copy(op_src, slots_src, slot_name_src, op_dst, slots_dst, slot_name_dst)
void BMO_op_init(BMesh *bm, BMOperator *op, int flag, const char *opname)
BMESH OPSTACK INIT OP.
#define BMO_ITER(ele, iter, slot_args, slot_name, restrict_flag)
#define BMO_vert_flag_test(bm, e, oflag)
bool BMO_op_initf(BMesh *bm, BMOperator *op, int flag, const char *fmt,...)
void BMO_op_finish(BMesh *bm, BMOperator *op)
BMESH OPSTACK FINISH OP.
void * BMO_iter_map_value_ptr(BMOIter *iter)
#define BMO_elem_flag_enable(bm, ele, oflag)
bool BMO_op_callf(BMesh *bm, int flag, const char *fmt,...)
BMOpSlot * BMO_slot_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *identifier)
BMESH OPSTACK GET SLOT.
void BMO_slot_bool_set(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, bool i)
#define BMO_elem_flag_test(bm, ele, oflag)
#define BMO_face_flag_test(bm, e, oflag)
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BM_vert_normal_update(BMVert *v)
void BM_face_normal_flip(BMesh *bm, BMFace *f)
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
Definition: bmesh_query.c:1553
bool BM_vert_is_manifold(const BMVert *v)
Definition: bmesh_query.c:705
BMLoop * BM_edge_other_loop(BMEdge *e, BMLoop *l)
Definition: bmesh_query.c:436
bool BM_vert_is_edge_pair(const BMVert *v)
Definition: bmesh_query.c:568
BLI_INLINE bool BM_edge_is_boundary(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define VERT_MARK
Definition: bmo_extrude.c:31
static void solidify_add_thickness(BMesh *bm, const float dist)
Definition: bmo_extrude.c:772
void bmo_extrude_discrete_faces_exec(BMesh *bm, BMOperator *op)
Definition: bmo_extrude.c:37
static void bm_extrude_copy_face_loop_attributes(BMesh *bm, BMFace *f)
Copy the loop pair from an adjacent face to both sides of this quad.
Definition: bmo_extrude.c:123
static bool bm_extrude_region_edge_flag(const BMVert *v, char r_e_hflag[2])
Definition: bmo_extrude.c:283
void bmo_extrude_edge_only_exec(BMesh *bm, BMOperator *op)
Definition: bmo_extrude.c:162
@ EXT_INPUT
Definition: bmo_extrude.c:25
@ EXT_TAG
Definition: bmo_extrude.c:28
@ EXT_KEEP
Definition: bmo_extrude.c:26
@ EXT_DEL
Definition: bmo_extrude.c:27
void bmo_extrude_vert_indiv_exec(BMesh *bm, BMOperator *op)
Definition: bmo_extrude.c:230
#define FACE_MARK
Definition: bmo_extrude.c:33
void bmo_solidify_face_region_exec(BMesh *bm, BMOperator *op)
Definition: bmo_extrude.c:825
#define EDGE_MARK
Definition: bmo_extrude.c:32
#define EDGE_NONMAN
Definition: bmo_extrude.c:35
#define VERT_NONMAN
Definition: bmo_extrude.c:34
static void bm_extrude_disable_skin_root(BMesh *bm, BMVert *v)
Definition: bmo_extrude.c:154
static void calc_solidify_normals(BMesh *bm)
Definition: bmo_extrude.c:627
void bmo_extrude_face_region_exec(BMesh *bm, BMOperator *op)
Definition: bmo_extrude.c:311
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
static float verts[][3]
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
ccl_device_inline float2 fabs(const float2 &a)
Definition: math_float2.h:222
BMHeader head
Definition: bmesh_class.h:111
BMVert * v1
Definition: bmesh_class.h:122
BMVert * v2
Definition: bmesh_class.h:122
struct BMLoop * l
Definition: bmesh_class.h:128
int len
Definition: bmesh_class.h:267
float no[3]
Definition: bmesh_class.h:271
char hflag
Definition: bmesh_class.h:66
void * data
Definition: bmesh_class.h:51
struct BMVert * v
Definition: bmesh_class.h:153
struct BMEdge * e
Definition: bmesh_class.h:164
struct BMLoop * radial_next
Definition: bmesh_class.h:204
struct BMFace * f
Definition: bmesh_class.h:171
struct BMLoop * next
Definition: bmesh_class.h:233
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
float co[3]
Definition: bmesh_class.h:87
struct BMEdge * e
Definition: bmesh_class.h:97
float no[3]
Definition: bmesh_class.h:88
BMHeader head
Definition: bmesh_class.h:85
int totvert
Definition: bmesh_class.h:297
CustomData vdata
Definition: bmesh_class.h:337
int totedge
Definition: bmesh_class.h:297
BMFace * act_face
Definition: bmesh_class.h:366