Blender V5.3
bmo_fill_edgeloop.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 "bmesh.hh"
14
15#include "intern/bmesh_operators_private.hh" /* own include */
16
17namespace blender {
18
19#define VERT_USED 1
20#define EDGE_MARK 2
21#define ELE_OUT 4
22
24{
25 /* first collect an array of unique from the edges */
26 const int tote = BMO_slot_buffer_len(op->slots_in, "edges");
27 const int totv = tote; /* these should be the same */
28 BMVert **verts = MEM_new_array_uninitialized<BMVert *>(totv, __func__);
29
30 BMVert *v;
31 BMEdge *e;
32 int i;
33 bool ok = true;
34
35 BMOIter oiter;
36
37 const short mat_nr = BMO_slot_int_get(op->slots_in, "mat_nr");
38 const bool use_smooth = BMO_slot_bool_get(op->slots_in, "use_smooth");
39
40 /* 'VERT_USED' will be disabled, so enable and fill the array */
41 i = 0;
42 BMO_ITER (e, &oiter, op->slots_in, "edges", BM_EDGE) {
43 BMIter viter;
45 BM_ITER_ELEM (v, &viter, e, BM_VERTS_OF_EDGE) {
46 if (BMO_vert_flag_test(bm, v, VERT_USED) == false) {
47 if (i == tote) {
48 goto cleanup;
49 }
50
52 verts[i++] = v;
53 }
54 }
55 }
56
57 /* we have a different number of verts to edges */
58 if (i != tote) {
59 goto cleanup;
60 }
61
62 /* loop over connected flagged edges and fill in faces, this is made slightly more
63 * complicated because there may be multiple disconnected loops to fill. */
64
65 /* sanity check - that each vertex has 2 edge users */
66 for (i = 0; i < totv; i++) {
67 v = verts[i];
68 /* count how many flagged edges this vertex uses */
70 ok = false;
71 break;
72 }
73 }
74
75 if (ok) {
76 /* NOTE: in the case of multiple loops, this over-allocates (which is fine). */
77 BMVert **f_verts = MEM_new_array_uninitialized<BMVert *>(totv, __func__);
78 BMIter eiter;
79
80 /* build array of connected verts and edges */
81 BMEdge *e_prev = nullptr;
82 BMEdge *e_next = nullptr;
83 int totv_used = 0;
84
85 while (totv_used < totv) {
86 for (i = 0; i < totv; i++) {
87 v = verts[i];
89 break;
90 }
91 }
92
93 /* this should never fail, as long as (totv_used < totv)
94 * we should have marked verts available */
96
97 /* watch it, 'i' is used for final face length */
98 i = 0;
99 do {
100 /* we know that there are 2 edges per vertex so no need to check */
101 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
103 if (e != e_prev) {
104 e_next = e;
105 break;
106 }
107 }
108 }
109
110 /* fill in the array */
111 f_verts[i] = v;
113 totv_used++;
114
115 /* step over the edges */
116 v = BM_edge_other_vert(e_next, v);
117 e_prev = e_next;
118 i++;
119 } while (v != f_verts[0]);
120
121 if (!BM_face_exists(f_verts, i)) {
122 BMFace *f;
123
124 /* don't use calc_edges option because we already have the edges */
125 f = BM_face_create_ngon_verts(bm, f_verts, i, nullptr, BM_CREATE_NOP, true, false);
127 f->mat_nr = mat_nr;
128 if (use_smooth) {
130 }
131 }
132 }
133 MEM_delete(f_verts);
134
136 }
137
138cleanup:
139 MEM_delete(verts);
140}
141
142} // namespace blender
Read Blender memory allocation and freeing.
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BMO_vert_flag_disable(bm, e, oflag)
#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_ITER(ele, iter, slot_args, slot_name, restrict_flag)
#define BMO_vert_flag_test(bm, e, oflag)
#define EDGE_MARK
Definition bmo_bridge.cc:28
#define ELE_OUT
Definition bmo_create.cc:22
#define VERT_USED
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int BMO_slot_int_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
int BMO_slot_buffer_len(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
ATTR_WARN_UNUSED_RESULT const BMVert * v
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BMO_iter_elem_count_flag(BMesh *bm, const char itype, void *data, const short oflag, const bool value)
Elem Iter Tool Flag Count.
@ BM_CREATE_NOP
Definition bmesh_core.hh:34
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)
BMFace * BM_face_exists(BMVert *const *varr, int len)
BMFace * BM_face_create_ngon_verts(BMesh *bm, BMVert **vert_arr, const int len, const BMFace *f_example, const eBMCreateFlag create_flag, const bool calc_winding, const bool create_edges)
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void bmo_edgeloop_fill_exec(BMesh *bm, BMOperator *op)
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]