Blender V5.3
bmo_extrude.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "MEM_guardedalloc.h"
12
13#include "DNA_meshdata_types.h"
14
15#include "BLI_math_geom_c.hh"
16#include "BLI_math_vector_c.hh"
17#include "BLI_vector.hh"
18
19#include "BKE_customdata.hh"
20
21#include "bmesh.hh"
22
23#include "intern/bmesh_operators_private.hh" /* own include */
24
25namespace blender {
26
27#define USE_EDGE_REGION_FLAGS
28
29enum {
34};
35
36#define VERT_MARK 1
37#define EDGE_MARK 1
38#define FACE_MARK 1
39#define VERT_NONMAN 2
40#define EDGE_NONMAN 2
41
43{
44 const bool use_select_history = BMO_slot_bool_get(op->slots_in, "use_select_history");
45 GHash *select_history_map = nullptr;
46
47 BMOIter siter;
48 BMFace *f_org;
49
50 if (use_select_history) {
51 select_history_map = BM_select_history_map_create(bm);
52 }
53
54 BMO_ITER (f_org, &siter, op->slots_in, "faces", BM_FACE) {
55 BMFace *f_new;
56 BMLoop *l_org, *l_org_first;
57 BMLoop *l_new;
58
60
61 f_new = BM_face_copy(bm, f_org, true, true);
63
64 if (select_history_map) {
65 BMEditSelection *ese;
66 ese = static_cast<BMEditSelection *>(BLI_ghash_lookup(select_history_map, f_org));
67 if (ese) {
68 ese->ele = reinterpret_cast<BMElem *>(f_new);
69 }
70 }
71
72 l_org = l_org_first = BM_FACE_FIRST_LOOP(f_org);
73 l_new = BM_FACE_FIRST_LOOP(f_new);
74
75 do {
76 BMFace *f_side;
77 BMLoop *l_side_iter;
78
79 BM_elem_attrs_copy(bm, l_org, l_new);
80
82 bm, l_org->next->v, l_new->next->v, l_new->v, l_org->v, f_org, BM_CREATE_NOP);
83
84 l_side_iter = BM_FACE_FIRST_LOOP(f_side);
85
86 BM_elem_attrs_copy(bm, l_org->next, l_side_iter);
87 l_side_iter = l_side_iter->next;
88 BM_elem_attrs_copy(bm, l_org->next, l_side_iter);
89 l_side_iter = l_side_iter->next;
90 BM_elem_attrs_copy(bm, l_org, l_side_iter);
91 l_side_iter = l_side_iter->next;
92 BM_elem_attrs_copy(bm, l_org, l_side_iter);
93
94 if (select_history_map) {
95 BMEditSelection *ese;
96
97 ese = static_cast<BMEditSelection *>(BLI_ghash_lookup(select_history_map, l_org->v));
98 if (ese) {
99 ese->ele = reinterpret_cast<BMElem *>(l_new->v);
100 }
101 ese = static_cast<BMEditSelection *>(BLI_ghash_lookup(select_history_map, l_org->e));
102 if (ese) {
103 ese->ele = reinterpret_cast<BMElem *>(l_new->e);
104 }
105 }
106
107 } while (((void)(l_new = l_new->next), (l_org = l_org->next)) != l_org_first);
108 }
109
110 if (select_history_map) {
111 BLI_ghash_free(select_history_map, nullptr, nullptr);
112 }
113
114 BMO_op_callf(bm, op->flag, "delete geom=%ff context=%i", EXT_DEL, DEL_ONLYFACES);
116}
117
129{
130 /* edge we are extruded from */
131 BMLoop *l_first_0 = BM_FACE_FIRST_LOOP(f);
132 BMLoop *l_first_1 = l_first_0->next;
133 BMLoop *l_first_2 = l_first_1->next;
134 BMLoop *l_first_3 = l_first_2->next;
135
136 BMLoop *l_other_0;
137 BMLoop *l_other_1;
138
139 if (l_first_0 == l_first_0->radial_next) [[unlikely]] {
140 return;
141 }
142
143 l_other_0 = BM_edge_other_loop(l_first_0->e, l_first_0);
144 l_other_1 = BM_edge_other_loop(l_first_0->e, l_first_1);
145
146 /* copy data */
147 BM_elem_attrs_copy(bm, l_other_0->f, f);
148 BM_elem_flag_disable(f, BM_ELEM_HIDDEN); /* possibly we copy from a hidden face */
149
150 BM_elem_attrs_copy(bm, l_other_0, l_first_0);
151 BM_elem_attrs_copy(bm, l_other_0, l_first_3);
152
153 BM_elem_attrs_copy(bm, l_other_1, l_first_1);
154 BM_elem_attrs_copy(bm, l_other_1, l_first_2);
155}
156
157/* Disable the skin root flag on the input vert, assumes that the vert
158 * data includes a "skin_modifier_root" layer. */
160{
161 const int offset = CustomData_get_offset_named(&bm->vdata, CD_PROP_BOOL, "skin_modifier_root");
162 BM_ELEM_CD_SET_BOOL(v, offset, false);
163}
164
166{
167 BMOIter siter;
168 BMOperator dupeop;
169 BMFace *f;
170 BMEdge *e, *e_new;
171 const bool use_normal_flip = BMO_slot_bool_get(op->slots_in, "use_normal_flip");
172
173 BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
177 }
178
180 &dupeop,
181 op->flag,
182 "duplicate geom=%fve use_select_history=%b",
183 EXT_INPUT,
184 BMO_slot_bool_get(op->slots_in, "use_select_history"));
185
186 BMO_op_exec(bm, &dupeop);
187
188 /* Disable root flag on original skin nodes. */
189 if (CustomData_has_layer_named(&bm->vdata, CD_PROP_BOOL, "skin_modifier_root")) {
190 BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
193 }
194 }
195
196 for (e = static_cast<BMEdge *>(BMO_iter_new(&siter, dupeop.slots_out, "boundary_map.out", 0)); e;
197 e = static_cast<BMEdge *>(BMO_iter_step(&siter)))
198 {
199 BMVert *f_verts[4];
200 e_new = static_cast<BMEdge *>(BMO_iter_map_value_ptr(&siter));
201
202 const bool edge_normal_flip = !(e->l && e->v1 != e->l->v);
203 if (edge_normal_flip == use_normal_flip) {
204 f_verts[0] = e->v1;
205 f_verts[1] = e->v2;
206 f_verts[2] = e_new->v2;
207 f_verts[3] = e_new->v1;
208 }
209 else {
210 f_verts[0] = e->v2;
211 f_verts[1] = e->v1;
212 f_verts[2] = e_new->v1;
213 f_verts[3] = e_new->v2;
214 }
215 /* not sure what to do about example face, pass nullptr for now */
216 f = BM_face_create_verts(bm, f_verts, 4, nullptr, BM_CREATE_NOP, true);
218
220 e = e_new;
221 }
222
227 }
228
229 BMO_op_finish(bm, &dupeop);
230
232}
233
235{
236 const bool use_select_history = BMO_slot_bool_get(op->slots_in, "use_select_history");
237 BMOIter siter;
238 BMVert *v, *dupev;
239 BMEdge *e;
240 const bool has_vskin = CustomData_has_layer_named(
241 &bm->vdata, CD_PROP_BOOL, "skin_modifier_root");
242 GHash *select_history_map = nullptr;
243
244 if (use_select_history) {
245 select_history_map = BM_select_history_map_create(bm);
246 }
247
248 for (v = static_cast<BMVert *>(BMO_iter_new(&siter, op->slots_in, "verts", BM_VERT)); v;
249 v = static_cast<BMVert *>(BMO_iter_step(&siter)))
250 {
251 dupev = BM_vert_create(bm, v->co, v, BM_CREATE_NOP);
253
254 if (has_vskin) {
256 }
257
258 if (select_history_map) {
259 BMEditSelection *ese;
260 ese = static_cast<BMEditSelection *>(BLI_ghash_lookup(select_history_map, v));
261 if (ese) {
262 ese->ele = reinterpret_cast<BMElem *>(dupev);
263 }
264 }
265
266 /* not essential, but ensures face normals from extruded edges are contiguous */
267 if (BM_vert_is_wire_endpoint(v)) {
268 if (v->e->v1 == v) {
269 std::swap(v, dupev);
270 }
271 }
272
273 e = BM_edge_create(bm, v, dupev, nullptr, BM_CREATE_NOP);
275 }
276
277 if (select_history_map) {
278 BLI_ghash_free(select_history_map, nullptr, nullptr);
279 }
280
283}
284
285#ifdef USE_EDGE_REGION_FLAGS
290static bool bm_extrude_region_edge_flag(const BMVert *v, char r_e_hflag[2])
291{
292 BMEdge *e_iter;
293 const char hflag_enable = BM_ELEM_SEAM;
294 const char hflag_disable = BM_ELEM_SMOOTH;
295 bool ok = false;
296
297 r_e_hflag[0] = 0x0;
298 r_e_hflag[1] = 0xff;
299
300 /* clear flags on both disks */
301 e_iter = v->e;
302 do {
303 if (e_iter->l && !BM_edge_is_boundary(e_iter)) {
304 r_e_hflag[0] |= e_iter->head.hflag;
305 r_e_hflag[1] &= e_iter->head.hflag;
306 ok = true;
307 }
308 } while ((e_iter = BM_DISK_EDGE_NEXT(e_iter, v)) != v->e);
309
310 if (ok) {
311 r_e_hflag[0] &= hflag_enable;
312 r_e_hflag[1] = hflag_disable & ~r_e_hflag[1];
313 }
314 return ok;
315}
316#endif /* USE_EDGE_REGION_FLAGS */
317
319{
320 BMOperator dupeop, delop;
321 BMOIter siter;
322 BMIter iter, fiter, viter;
323 BMEdge *e, *e_new;
324 BMVert *v;
325 BMFace *f;
326 bool found, delorig = false;
327 BMOpSlot *slot_facemap_out;
328 BMOpSlot *slot_edges_exclude;
329 const bool use_normal_flip = BMO_slot_bool_get(op->slots_in, "use_normal_flip");
330 const bool use_normal_from_adjacent = BMO_slot_bool_get(op->slots_in,
331 "use_normal_from_adjacent");
332 const bool use_dissolve_ortho_edges = BMO_slot_bool_get(op->slots_in,
333 "use_dissolve_ortho_edges");
334
335 /* initialize our sub-operators */
337 &dupeop,
338 op->flag,
339 "duplicate use_select_history=%b",
340 BMO_slot_bool_get(op->slots_in, "use_select_history"));
341
343
344 /* if one flagged face is bordered by an un-flagged face, then we delete
345 * original geometry unless caller explicitly asked to keep it. */
346 if (!BMO_slot_bool_get(op->slots_in, "use_keep_orig")) {
347 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
348
349 int edge_face_tot;
350
352 continue;
353 }
354
355 found = false; /* found a face that isn't input? */
356 edge_face_tot = 0; /* edge/face count */
357
358 BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
359 if (!BMO_face_flag_test(bm, f, EXT_INPUT)) {
360 found = true;
361 delorig = true;
362 break;
363 }
364
365 edge_face_tot++;
366 }
367
368 if ((edge_face_tot > 1) && (found == false)) {
369 /* edge has a face user, that face isn't extrude input */
371 }
372 }
373 }
374
375 /* calculate verts to delete */
376 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
377 if (v->e) { /* only deal with verts attached to geometry #33651. */
378 found = false;
379
380 BM_ITER_ELEM (e, &viter, v, BM_EDGES_OF_VERT) {
382 found = true;
383 break;
384 }
385 }
386
387 /* avoid an extra loop */
388 if (found == false) {
389 BM_ITER_ELEM (f, &viter, v, BM_FACES_OF_VERT) {
390 if (!BMO_face_flag_test(bm, f, EXT_INPUT)) {
391 found = true;
392 break;
393 }
394 }
395 }
396
397 if (found == false) {
399 }
400 }
401 }
402
403 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
406 }
407 }
408
409 if (delorig == true) {
410 BMO_op_initf(bm, &delop, op->flag, "delete geom=%fvef context=%i", EXT_DEL, DEL_ONLYTAGGED);
411 }
412
413 BMO_slot_copy(op, slots_in, "geom", &dupeop, slots_in, "geom");
414 BMO_op_exec(bm, &dupeop);
415
416 /* Disable root flag on original skin nodes. */
417 if (CustomData_has_layer_named(&bm->vdata, CD_PROP_BOOL, "skin_modifier_root")) {
418 BMO_ITER (e, &siter, op->slots_in, "geom", BM_EDGE) {
421 }
422 BMO_ITER (v, &siter, op->slots_in, "geom", BM_VERT) {
424 }
425 }
426
427 slot_facemap_out = BMO_slot_get(dupeop.slots_out, "face_map.out");
429 bm->act_face = static_cast<BMFace *>(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 const bool skip_input_flip = BMO_slot_bool_get(op->slots_in, "skip_input_flip");
437
438 /* Flip input faces only when originals are kept (!delorig)
439 * and the caller didn't request to skip flipping (!skip_input_flip).*/
440 if (!delorig && !skip_input_flip) {
441 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
444 }
445 }
446 }
447
448 BMVert **dissolve_verts = nullptr;
449 int dissolve_verts_len = 0;
450 float average_normal[3];
451 if (use_dissolve_ortho_edges) {
452 /* Calc average normal. */
453 zero_v3(average_normal);
454 BMO_ITER (f, &siter, dupeop.slots_out, "geom.out", BM_FACE) {
455 add_v3_v3(average_normal, f->no);
456 }
457 if (normalize_v3(average_normal) == 0.0f) {
458 average_normal[2] = 1.0f;
459 }
460
461 /* Allocate array to store possible vertices that will be dissolved. */
462 int boundary_edges_len = BMO_slot_map_len(dupeop.slots_out, "boundary_map.out");
463 /* We do not know the real number of boundary vertices. */
464 int boundary_verts_len_maybe = 2 * boundary_edges_len;
465 dissolve_verts = MEM_new_array_uninitialized<BMVert *>(boundary_verts_len_maybe, __func__);
466 }
467
468 BMO_slot_copy(&dupeop, slots_out, "geom.out", op, slots_out, "geom.out");
469
470 slot_edges_exclude = BMO_slot_get(op->slots_in, "edges_exclude");
471 for (e = static_cast<BMEdge *>(BMO_iter_new(&siter, dupeop.slots_out, "boundary_map.out", 0)); e;
472 e = static_cast<BMEdge *>(BMO_iter_step(&siter)))
473 {
474 BMVert *f_verts[4];
475#ifdef USE_EDGE_REGION_FLAGS
476 BMEdge *f_edges[4];
477#endif
478
479 /* this should always be wire, so this is mainly a speedup to avoid map lookup */
480 if (BM_edge_is_wire(e) && BMO_slot_map_contains(slot_edges_exclude, e)) {
481 BMVert *v1 = e->v1, *v2 = e->v2;
482
483 /* The original edge was excluded,
484 * this would result in a standalone wire edge - see #30399. */
485 BM_edge_kill(bm, e);
486
487 /* kill standalone vertices from this edge - see #32341. */
488 if (!v1->e) {
489 BM_vert_kill(bm, v1);
490 }
491 if (!v2->e) {
493 }
494
495 continue;
496 }
497
498 /* skip creating face for excluded edges see #35503. */
499 if (BMO_slot_map_contains(slot_edges_exclude, e)) {
500 /* simply skip creating the face */
501 continue;
502 }
503
504 e_new = static_cast<BMEdge *>(BMO_iter_map_value_ptr(&siter));
505
506 if (!e_new) {
507 continue;
508 }
509
510 BMFace *join_face = nullptr;
511 if (use_dissolve_ortho_edges) {
512 if (BM_edge_is_boundary(e)) {
513 join_face = e->l->f;
514 if (fabs(dot_v3v3(average_normal, join_face->no)) > 0.0001f) {
515 join_face = nullptr;
516 }
517 }
518 }
519
520 bool edge_normal_flip;
521 if (use_normal_from_adjacent == false) {
522 /* Orient loop to give same normal as a loop of 'e_new'
523 * if it exists (will be one of the faces from the region),
524 * else same normal as a loop of e, if it exists. */
525 edge_normal_flip = !(e_new->l ? (e_new->l->v == e_new->v1) : (!e->l || !(e->l->v == e->v1)));
526 }
527 else {
528 /* Special case, needed for repetitive extrusions
529 * that use the normals from the previously created faces. */
530 edge_normal_flip = !(e->l && e->v1 != e->l->v);
531 }
532
533 if (edge_normal_flip == use_normal_flip) {
534 f_verts[0] = e->v1;
535 f_verts[1] = e->v2;
536 f_verts[2] = e_new->v2;
537 f_verts[3] = e_new->v1;
538 }
539 else {
540 f_verts[0] = e->v2;
541 f_verts[1] = e->v1;
542 f_verts[2] = e_new->v1;
543 f_verts[3] = e_new->v2;
544 }
545
546#ifdef USE_EDGE_REGION_FLAGS
547 /* handle new edges */
548 f_edges[0] = e;
549 f_edges[2] = e_new;
550
551 f_edges[1] = BM_edge_exists(f_verts[1], f_verts[2]);
552 if (f_edges[1] == nullptr) {
553 char e_hflag[2];
554 bool e_hflag_ok = bm_extrude_region_edge_flag(f_verts[2], e_hflag);
555 f_edges[1] = BM_edge_create(bm, f_verts[1], f_verts[2], nullptr, BM_CREATE_NOP);
556 if (e_hflag_ok) {
557 BM_elem_flag_enable(f_edges[1], e_hflag[0]);
558 BM_elem_flag_disable(f_edges[1], e_hflag[1]);
559 }
560 }
561
562 f_edges[3] = BM_edge_exists(f_verts[3], f_verts[0]);
563 if (f_edges[3] == nullptr) {
564 char e_hflag[2];
565 bool e_hflag_ok = bm_extrude_region_edge_flag(f_verts[3], e_hflag);
566 f_edges[3] = BM_edge_create(bm, f_verts[3], f_verts[0], nullptr, BM_CREATE_NOP);
567 if (e_hflag_ok) {
568 BM_elem_flag_enable(f_edges[3], e_hflag[0]);
569 BM_elem_flag_disable(f_edges[3], e_hflag[1]);
570 }
571 }
572
573 f = BM_face_create(bm, f_verts, f_edges, 4, nullptr, BM_CREATE_NOP);
574#else
575 f = BM_face_create_verts(bm, f_verts, 4, nullptr, BM_CREATE_NOP, true);
576#endif
577
579 if (join_face) {
580 BMVert *v1 = e->v1;
581 BMVert *v2 = e->v2;
582 if (!BMO_elem_flag_test(bm, v1, EXT_TAG)) {
584 dissolve_verts[dissolve_verts_len++] = v1;
585 }
588 dissolve_verts[dissolve_verts_len++] = v2;
589 }
590 /* Tag the edges that can collapse. */
591 BMO_elem_flag_enable(bm, f_edges[0], EXT_TAG);
592 BMO_elem_flag_enable(bm, f_edges[1], EXT_TAG);
593 bmesh_kernel_join_face_kill_edge(bm, join_face, f, e);
594 }
595 }
596
597 /* link isolated vert */
598 for (v = static_cast<BMVert *>(BMO_iter_new(&siter, dupeop.slots_out, "isovert_map.out", 0)); v;
599 v = static_cast<BMVert *>(BMO_iter_step(&siter)))
600 {
601 BMVert *v2 = static_cast<BMVert *>(BMO_iter_map_value_ptr(&siter));
602
603 /* not essential, but ensures face normals from extruded edges are contiguous */
604 if (BM_vert_is_wire_endpoint(v)) {
605 if (v->e->v1 == v) {
606 std::swap(v, v2);
607 }
608 }
609
611 }
612
613 if (dissolve_verts) {
614 BMVert **v_iter = &dissolve_verts[0];
615 for (int i = dissolve_verts_len; i--; v_iter++) {
616 v = *v_iter;
617 e = v->e;
618 BMEdge *e_other = BM_DISK_EDGE_NEXT(e, v);
619 if ((e_other == e) || (BM_DISK_EDGE_NEXT(e_other, v) == e)) {
620 /* Loose edge or BMVert is edge pair. */
621 BM_edge_collapse(bm, BMO_elem_flag_test(bm, e, EXT_TAG) ? e : e_other, v, true, true);
622 }
623 else {
625 }
626 }
627 MEM_delete(dissolve_verts);
628 }
629
630 /* cleanup */
631 if (delorig) {
632 BMO_op_finish(bm, &delop);
633 }
634 BMO_op_finish(bm, &dupeop);
635}
636
637/*
638 * Compute higher-quality vertex normals used by solidify.
639 * Only considers geometry in the marked solidify region.
640 * Note that this does not work so well for non-manifold
641 * regions.
642 */
644{
645 BMIter viter, eiter, fiter;
646 BMVert *v;
647 BMEdge *e;
648 BMFace *f, *f1, *f2;
649 float edge_normal[3];
650 int i;
651
652 /* can't use BM_edge_face_count because we need to count only marked faces */
653 int *edge_face_count = MEM_new_array_zeroed<int>(bm->totedge, __func__);
654
655 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
657 }
658
660
661 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
662 if (!BMO_face_flag_test(bm, f, FACE_MARK)) {
663 continue;
664 }
665
666 BM_ITER_ELEM (e, &eiter, f, BM_EDGES_OF_FACE) {
667
668 /* And mark all edges and vertices on the
669 * marked faces */
673 edge_face_count[BM_elem_index_get(e)]++;
674 }
675 }
676
677 BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
679 continue;
680 }
681
682 i = edge_face_count[BM_elem_index_get(e)];
683
684 if (i == 0 || i > 2) {
685 /* Edge & vertices are non-manifold even when considering
686 * only marked faces */
690 }
691 }
692 MEM_delete(edge_face_count);
693 edge_face_count = nullptr; /* don't re-use */
694
695 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
696 if (!BM_vert_is_manifold(v)) {
698 continue;
699 }
700
702 zero_v3(v->no);
703 }
704 }
705
706 BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
707
708 /* If the edge is not part of the solidify region
709 * its normal should not be considered */
711 continue;
712 }
713
714 /* If the edge joins more than two marked faces high
715 * quality normal computation won't work */
717 continue;
718 }
719
720 f1 = f2 = nullptr;
721
722 BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
724 if (f1 == nullptr) {
725 f1 = f;
726 }
727 else {
728 BLI_assert(f2 == nullptr);
729 f2 = f;
730 }
731 }
732 }
733
734 BLI_assert(f1 != nullptr);
735
736 if (f2 != nullptr) {
737 const float angle = angle_normalized_v3v3(f1->no, f2->no);
738
739 if (angle > 0.0f) {
740 /* two faces using this edge, calculate the edge normal
741 * using the angle between the faces as a weighting */
742 add_v3_v3v3(edge_normal, f1->no, f2->no);
743 normalize_v3_length(edge_normal, angle);
744 }
745 else {
746 /* can't do anything useful here!
747 * Set the face index for a vert in case it gets a zero normal */
750 continue;
751 }
752 }
753 else {
754 /* only one face attached to that edge */
755 /* an edge without another attached- the weight on this is undefined,
756 * M_PI_2 is 90d in radians and that seems good enough */
757 copy_v3_v3(edge_normal, f1->no);
758 mul_v3_fl(edge_normal, M_PI_2);
759 }
760
761 add_v3_v3(e->v1->no, edge_normal);
762 add_v3_v3(e->v2->no, edge_normal);
763 }
764
765 /* normalize accumulated vertex normal */
766 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
768 continue;
769 }
770
772 /* use standard normals for vertices connected to non-manifold edges */
774 }
775 else if (normalize_v3(v->no) == 0.0f && !BM_elem_flag_test(v, BM_ELEM_TAG)) {
776 /* exceptional case, totally flat. use the normal
777 * of any marked face around the vertex */
778 BM_ITER_ELEM (f, &fiter, v, BM_FACES_OF_VERT) {
780 break;
781 }
782 }
783 copy_v3_v3(v->no, f->no);
784 }
785 }
786}
787
788static void solidify_add_thickness(BMesh *bm, const float dist)
789{
790 BMFace *f;
791 BMVert *v;
792 BMLoop *l;
793 BMIter iter, loopIter;
794 float *vert_angles = MEM_new_array_zeroed<float>(size_t(bm->totvert) * 2,
795 "solidify"); /* 2 in 1 */
796 float *vert_accum = vert_angles + bm->totvert;
797 int i, index;
798
801
803
804 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
806
807 /* array for passing verts to angle_poly_v3 */
808 face_angles.resize(f->len);
809 /* array for receiving angles from angle_poly_v3 */
810 verts.resize(f->len);
811
812 BM_ITER_ELEM_INDEX (l, &loopIter, f, BM_LOOPS_OF_FACE, i) {
813 verts[i] = l->v->co;
814 }
815
816 angle_poly_v3(face_angles.data(), (const float **)verts.data(), f->len);
817
818 i = 0;
819 BM_ITER_ELEM (l, &loopIter, f, BM_LOOPS_OF_FACE) {
820 v = l->v;
822 vert_accum[index] += face_angles[i];
823 vert_angles[index] += shell_v3v3_normalized_to_dist(v->no, f->no) * face_angles[i];
824 i++;
825 }
826 }
827 }
828
829 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
831 if (vert_accum[index]) { /* zero if unselected */
832 madd_v3_v3fl(v->co, v->no, dist * (vert_angles[index] / vert_accum[index]));
833 }
834 }
835
836 MEM_delete(vert_angles);
837}
838
840{
841 BMOperator extrudeop;
842 BMOperator reverseop;
843 float thickness;
844
845 thickness = BMO_slot_float_get(op->slots_in, "thickness");
846
847 /* Flip original faces (so the shell is extruded inward) */
848 BMO_op_init(bm, &reverseop, op->flag, "reverse_faces");
849 BMO_slot_bool_set(reverseop.slots_in, "flip_multires", true);
850 BMO_slot_copy(op, slots_in, "geom", &reverseop, slots_in, "faces");
851 BMO_op_exec(bm, &reverseop);
852 BMO_op_finish(bm, &reverseop);
853
854 /* Extrude the region */
855 BMO_op_initf(bm, &extrudeop, op->flag, "extrude_face_region use_keep_orig=%b", true);
856 BMO_slot_copy(op, slots_in, "geom", &extrudeop, slots_in, "geom");
857 BMO_op_exec(bm, &extrudeop);
858
859 /* Push the verts of the extruded faces inward to create thickness */
860 BMO_slot_buffer_flag_enable(bm, extrudeop.slots_out, "geom.out", BM_FACE, FACE_MARK);
862 solidify_add_thickness(bm, thickness);
863
864 BMO_slot_copy(&extrudeop, slots_out, "geom.out", op, slots_out, "geom.out");
865
866 BMO_op_finish(bm, &extrudeop);
867}
868
869} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define M_PI_2
Read Blender memory allocation and freeing.
#define BM_DISK_EDGE_NEXT(e, v)
#define BM_ALL_NOLOOP
#define BM_FACE_FIRST_LOOP(p)
#define BM_ELEM_CD_SET_BOOL(ele, offset, f)
#define BM_elem_index_get(ele)
#define BM_elem_flag_disable(ele, hflag)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_ELEM_INDEX(ele, iter, data, itype, indexvar)
#define BMO_edge_flag_test(bm, e, oflag)
#define BMO_edge_flag_enable(bm, e, oflag)
#define BMO_vert_flag_enable(bm, e, 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)
#define BMO_ITER(ele, iter, slot_args, slot_name, restrict_flag)
#define BMO_vert_flag_test(bm, e, oflag)
#define BMO_elem_flag_enable(bm, ele, oflag)
#define BMO_elem_flag_test(bm, ele, oflag)
#define BMO_face_flag_test(bm, e, oflag)
#define FACE_MARK
#define EDGE_MARK
Definition bmo_bridge.cc:28
#define VERT_MARK
#define EDGE_NONMAN
#define VERT_NONMAN
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
void resize(const int64_t new_size)
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
BMesh Iterators.
BMFace * BM_face_copy(BMesh *bm_dst, const BMCustomDataCopyMap &cd_face_map, const BMCustomDataCopyMap &cd_loop_map, BMFace *f, const bool copy_verts, const bool copy_edges)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
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)
MINLINE float shell_v3v3_normalized_to_dist(const float a[3], const float b[3])
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BM_face_normal_flip(BMesh *bm, BMFace *f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static bool bm_extrude_region_edge_flag(const BMVert *v, char r_e_hflag[2])
ATTR_WARN_UNUSED_RESULT const BMLoop * l
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void BM_vert_kill(BMesh *bm, BMVert *v)
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:735
float BMO_slot_float_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
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.
void bmo_extrude_discrete_faces_exec(BMesh *bm, BMOperator *op)
bool BMO_op_initf(BMesh *bm, BMOperator *op, int flag, const char *fmt,...)
void * BMO_iter_step(BMOIter *iter)
void bmo_extrude_face_region_exec(BMesh *bm, BMOperator *op)
void * BMO_iter_map_value_ptr(BMOIter *iter)
ATTR_WARN_UNUSED_RESULT const BMVert * v
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.
void bmo_extrude_edge_only_exec(BMesh *bm, BMOperator *op)
BMFace * BM_face_create(BMesh *bm, BMVert *const *verts, BMEdge *const *edges, const int len, const BMFace *f_example, const eBMCreateFlag create_flag)
void bmo_extrude_vert_indiv_exec(BMesh *bm, BMOperator *op)
void BMO_op_exec(BMesh *bm, BMOperator *op)
BMESH OPSTACK EXEC OP.
BMVert * BM_edge_collapse(BMesh *bm, BMEdge *e_kill, BMVert *v_kill, const bool do_del, const bool kill_degenerate_faces)
MINLINE void mul_v3_fl(float r[3], float f)
void BM_vert_normal_update(BMVert *v)
GHash * BM_select_history_map_create(BMesh *bm)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
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.
@ BM_CREATE_NO_DOUBLE
Definition bmesh_core.hh:36
@ BM_CREATE_NOP
Definition bmesh_core.hh:34
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:866
void BM_edge_kill(BMesh *bm, BMEdge *e)
BMFace * bmesh_kernel_join_face_kill_edge(BMesh *bm, BMFace *f1, BMFace *f2, BMEdge *e)
Join Face Kill Edge (JFKE).
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)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
void BMO_slot_bool_set(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, bool i)
BLI_INLINE bool BM_edge_is_boundary(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BMO_op_callf(BMesh *bm, int flag, const char *fmt,...)
void bmo_solidify_face_region_exec(BMesh *bm, BMOperator *op)
MINLINE void zero_v3(float r[3])
BMLoop * BM_edge_other_loop(BMEdge *e, BMLoop *l)
BMOpSlot * BMO_slot_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *identifier)
BMESH OPSTACK GET SLOT.
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
static void bm_extrude_disable_skin_root(BMesh *bm, BMVert *v)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE float normalize_v3_length(float n[3], float unit_length)
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.
bool BM_vert_is_edge_pair(const BMVert *v)
int BMO_slot_map_len(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BMO_op_init(BMesh *bm, BMOperator *op, int flag, const char *opname)
BMESH OPSTACK INIT OP.
static void calc_solidify_normals(BMesh *bm)
static void solidify_add_thickness(BMesh *bm, const float dist)
void BM_elem_attrs_copy(BMesh *bm, const BMCustomDataCopyMap &map, const BMVert *src, BMVert *dst)
void BMO_op_finish(BMesh *bm, BMOperator *op)
BMESH OPSTACK FINISH OP.
bool BM_vert_is_manifold(const BMVert *v)
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
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.
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void * BMO_iter_new(BMOIter *iter, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char restrictmask)
New Iterator.
void angle_poly_v3(float *angles, const float *verts[3], int len)
struct BMLoop * l
struct BMLoop * next
struct BMEdge * e
struct BMVert * v
struct BMLoop * radial_next
struct BMFace * f
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
struct BMEdge * e