Blender V5.3
bmo_create.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 "BLI_listbase.hh"
14
15#include "bmesh.hh"
16
17#include "intern/bmesh_operators_private.hh" /* own include */
18
19namespace blender {
20
21#define ELE_NEW 1
22#define ELE_OUT 2
23
25{
26 /* NOTE(@ideasman42): doing the best thing here isn't always easy create vs dissolve,
27 * its nice to support but it _really_ gives issues we might have to not call dissolve. */
28
29 BMOIter oiter;
30 BMHeader *h;
31 int totv = 0, tote = 0, totf = 0;
32 const short mat_nr = BMO_slot_int_get(op->slots_in, "mat_nr");
33 const bool use_smooth = BMO_slot_bool_get(op->slots_in, "use_smooth");
34
35 /* count number of each element type we were passe */
36 BMO_ITER (h, &oiter, op->slots_in, "geom", BM_VERT | BM_EDGE | BM_FACE) {
37 switch (h->htype) {
38 case BM_VERT:
40 totv++;
41 break;
42 case BM_EDGE:
44 tote++;
45 break;
46 case BM_FACE:
48 totf++;
49 break;
50 }
51 }
52
53 /* --- Support Edge Creation ---
54 * simple case when we only have 2 verts selected.
55 */
56 if (totv == 2 && tote == 0 && totf == 0) {
57 BMVert *verts[2];
58 BMEdge *e;
59
60 if (BMO_iter_as_array(op->slots_in, "geom", BM_VERT, reinterpret_cast<void **>(verts), 2) == 2)
61 {
62 /* create edge */
63 e = BM_edge_create(bm, verts[0], verts[1], nullptr, BM_CREATE_NO_DOUBLE);
65 tote += 1;
67 }
68 return;
69 }
70
71 /* --- Support for Special Case ---
72 * where there is a contiguous edge ring with one isolated vertex.
73 *
74 * This example shows 2 edges created from 3 verts
75 * with 1 free standing vertex. Dotted lines denote the 2 edges that are created.
76 *
77 * note that this works for any sided shape.
78 *
79 * +--------+
80 * | .
81 * | .
82 * | .
83 * | .
84 * +........+ <-- starts out free standing.
85 */
86
87 /* Here we check for consistency and create 2 edges */
88 if (totf == 0 && totv >= 4 && totv == tote + 2) {
89 /* find a free standing vertex and 2 endpoint verts */
90 BMVert *v, *v_free = nullptr, *v_a = nullptr, *v_b = nullptr;
91 bool ok = true;
92
93 BMO_ITER (v, &oiter, op->slots_in, "geom", BM_VERT) {
94 /* count how many flagged edges this vertex uses */
95 const int tot_edges = BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, v, ELE_NEW, true);
96 if (tot_edges == 0) {
97 /* only accept 1 free vert */
98 if (v_free == nullptr) {
99 v_free = v;
100 }
101 else {
102 ok = false;
103 } /* only ever want one of these */
104 }
105 else if (tot_edges == 1) {
106 if (v_a == nullptr) {
107 v_a = v;
108 }
109 else if (v_b == nullptr) {
110 v_b = v;
111 }
112 else {
113 ok = false;
114 } /* only ever want 2 of these */
115 }
116 else if (tot_edges == 2) {
117 /* do nothing, regular case */
118 }
119 else {
120 ok = false; /* if a vertex has 3+ edge users then cancel - this is only simple cases */
121 }
122
123 if (ok == false) {
124 break;
125 }
126 }
127
128 if (ok == true && v_free && v_a && v_b) {
129 BMEdge *e;
130
131 e = BM_edge_create(bm, v_free, v_a, nullptr, BM_CREATE_NO_DOUBLE);
133
134 e = BM_edge_create(bm, v_free, v_b, nullptr, BM_CREATE_NO_DOUBLE);
136 tote += 2;
137 }
138 }
139 /* --- end special case support, continue as normal --- */
140
141 /* -------------------------------------------------------------------- */
142 /* EdgeNet Create */
143 if (tote != 0) {
144 /* call edgenet prepare op so additional face creation cases work */
145
147 BMO_op_initf(bm, &op_sub, op->flag, "edgenet_prepare edges=%fe", ELE_NEW);
151
153 &op_sub,
154 op->flag,
155 "edgenet_fill edges=%fe mat_nr=%i use_smooth=%b sides=%i",
156 ELE_NEW,
157 mat_nr,
158 use_smooth,
159 10000);
160
162
163 /* return if edge net create did something */
164 if (BMO_slot_buffer_len(op_sub.slots_out, "faces.out")) {
165 BMO_slot_copy(&op_sub, slots_out, "faces.out", op, slots_out, "faces.out");
167 return;
168 }
169
171 }
172
173 /* -------------------------------------------------------------------- */
174 /* Dissolve Face */
175 if (totf != 0) { /* should be (totf > 1)... see below */
176 /* NOTE: allow this to run on single faces so running on a single face
177 * won't go on to create a face, treating them as random */
179 BMO_op_initf(bm, &op_sub, op->flag, "dissolve_faces faces=%ff", ELE_NEW);
181
182 /* if we dissolved anything, then return */
183 if (BMO_slot_buffer_len(op_sub.slots_out, "region.out")) {
184 BMO_slot_copy(&op_sub, slots_out, "region.out", op, slots_out, "faces.out");
186 return;
187 }
188
190 }
191
192 /* -------------------------------------------------------------------- */
193 /* Fill EdgeLoop's - fills isolated loops, different from edgenet */
194 if (tote > 2) {
196 /* NOTE: in most cases 'edgenet_fill' will handle this case since in common cases
197 * users fill in empty spaces, however its possible to have an edge selection around
198 * existing geometry that makes 'edgenet_fill' fail. */
199 BMO_op_initf(bm, &op_sub, op->flag, "edgeloop_fill edges=%fe", ELE_NEW);
201
202 /* return if edge loop fill did something */
203 if (BMO_slot_buffer_len(op_sub.slots_out, "faces.out")) {
204 BMO_slot_copy(&op_sub, slots_out, "faces.out", op, slots_out, "faces.out");
206 return;
207 }
208
210 }
211
212 /* -------------------------------------------------------------------- */
213 /* Continue with ad-hoc fill methods since operators fail,
214 * edge, vcloud... may add more */
215
216 if (false) { /* nice feature but perhaps it should be a different tool? */
217
218 /* tricky feature for making a line/edge from selection history...
219 *
220 * Rather than do nothing, when 5+ verts are selected, check if they are in our history,
221 * when this is so, we can make edges from them, but _not_ a face,
222 * if it is the intention to make a face the user can just hit F again
223 * since there will be edges next time around.
224 *
225 * if all history verts have ELE_NEW flagged and the total number of history verts == totv,
226 * then we know the history contains all verts here and we can continue...
227 */
228 int tot_ese_v = 0;
229
230 for (BMEditSelection &ese : bm->selected) {
231 if (ese.htype == BM_VERT) {
232 if (BMO_vert_flag_test(bm, (BMVert *)ese.ele, ELE_NEW)) {
233 tot_ese_v++;
234 }
235 else {
236 /* unflagged vert means we are not in sync */
237 tot_ese_v = -1;
238 break;
239 }
240 }
241 }
242
243 if (tot_ese_v == totv) {
244 BMVert *v_prev = nullptr;
245 /* yes, all select-history verts are accounted for, now make edges */
246
247 for (BMEditSelection &ese : bm->selected) {
248 if (ese.htype == BM_VERT) {
249 BMVert *v = reinterpret_cast<BMVert *>(ese.ele);
250 if (v_prev) {
251 BMEdge *e = BM_edge_create(bm, v, v_prev, nullptr, BM_CREATE_NO_DOUBLE);
253 }
254 v_prev = v;
255 }
256 }
257 }
259 /* done creating edges */
260
261 return;
262 }
263
264 /* -------------------------------------------------------------------- */
265 /* Fill Vertex Cloud
266 *
267 * last resort when all else fails.
268 */
269 if (totv > 2) {
270 /* TODO: some of these vertices may be connected by edges,
271 * this connectivity could be used rather than treating
272 * them as a bunch of isolated verts. */
273
274 BMVert **vert_arr = MEM_new_array_uninitialized<BMVert *>(totv, __func__);
275 BMFace *f;
276
277 totv = BMO_iter_as_array(
278 op->slots_in, "geom", BM_VERT, reinterpret_cast<void **>(vert_arr), totv);
279
280 BM_verts_sort_radial_plane(vert_arr, totv);
281
282 /* create edges and find the winding (if faces are attached to any existing edges) */
283 f = BM_face_create_ngon_verts(bm, vert_arr, totv, nullptr, BM_CREATE_NO_DOUBLE, true, true);
284
285 if (f) {
287 f->mat_nr = mat_nr;
288 if (use_smooth) {
290 }
291 BM_face_copy_shared(bm, f, nullptr, nullptr);
293 }
294
295 MEM_delete(vert_arr);
296 }
297}
298
299} // namespace blender
Read Blender memory allocation and freeing.
#define BM_elem_flag_enable(ele, hflag)
#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 ELE_NEW
#define ELE_OUT
Definition bmo_create.cc:22
BMesh Iterators.
int BMO_slot_int_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void bmo_contextual_create_exec(BMesh *bm, BMOperator *op)
Definition bmo_create.cc:24
static double op_sub(double a, double b)
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
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.
bool BMO_op_initf(BMesh *bm, BMOperator *op, int flag, const char *fmt,...)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void BMO_op_exec(BMesh *bm, BMOperator *op)
BMESH OPSTACK EXEC OP.
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_NO_DOUBLE
Definition bmesh_core.hh:36
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)
void BM_face_copy_shared(BMesh *bm, BMFace *f, BMLoopFilterFunc filter_fn, void *user_data)
copies face loop data from shared adjacent faces.
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)
void BM_verts_sort_radial_plane(BMVert **vert_arr, int len)
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
int BMO_iter_as_array(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char restrictmask, void **array, const int len)
Operator Iterator as Array.
void BMO_op_finish(BMesh *bm, BMOperator *op)
BMESH OPSTACK FINISH OP.
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
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
ListBaseT< BMEditSelection > selected