Blender V5.3
bmo_offset_edgeloops.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
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_array.hh"
17#include "BLI_math_vector_c.hh"
19
20#include "BKE_customdata.hh"
21
22#include "bmesh.hh"
23
24#include "intern/bmesh_operators_private.hh" /* own include */
25
26namespace blender {
27
28#define USE_CAP_OPTION
29
30#define ELE_NEW (1 << 0)
31
32#ifdef USE_CAP_OPTION
33# define ELE_VERT_ENDPOINT (1 << 1)
34#endif
35
36/* set for debugging */
37#define OFFSET 0.0f
38
40{
41 float (*cos)[3];
42 BMLoop *l_dst;
43 BMFace *f;
44 int num, i;
45
46 for (l_dst = l_src->prev, num = 0; BM_elem_index_get(l_dst->prev->v) != -1;
47 l_dst = l_dst->prev, num++)
48 {
49 /* pass */
50 }
51
52 BLI_assert(num != 0);
53
55 cos = reinterpret_cast<float (*)[3]>(cos_buf.data());
56
57 for (l_dst = l_src->prev, i = 0; BM_elem_index_get(l_dst->prev->v) != -1;
58 l_dst = l_dst->prev, i++)
59 {
60 copy_v3_v3(cos[num - (i + 1)], l_dst->v->co);
61 }
62
63 f = BM_face_split_n(bm, l_src->f, l_dst->prev, l_src->next, cos, num, r_l, nullptr);
64
65 return f;
66}
67
69{
70 const int edges_num = BMO_slot_buffer_len(op->slots_in, "edges");
71 BMVert **verts;
72 STACK_DECLARE(verts);
73 int i;
74
75#ifdef USE_CAP_OPTION
76 bool use_cap_endpoint = BMO_slot_bool_get(op->slots_in, "use_cap_endpoint");
77 int v_edges_max = 0;
78#endif
79
80 BMOIter oiter;
81
82 /* only so we can detect new verts (index == -1) */
84
86
87 /* Over allocate. */
88 verts = MEM_new_array_uninitialized<BMVert *>((edges_num * 2), __func__);
89
90 STACK_INIT(verts, (edges_num * 2));
91
92 {
93 BMEdge *e;
94 BMO_ITER (e, &oiter, op->slots_in, "edges", BM_EDGE) {
95 int j;
96
98
99 for (j = 0; j < 2; j++) {
100 BMVert *v_edge = *(&(e->v1) + j);
101 if (!BM_elem_flag_test(v_edge, BM_ELEM_TAG)) {
103 STACK_PUSH(verts, v_edge);
104 }
105 }
106 }
107 }
108
109 /* -------------------------------------------------------------------- */
110 /* Remove verts only used by tagged edges */
111
112 for (i = 0; i < STACK_SIZE(verts); i++) {
113 BMIter iter;
114 int flag = 0;
115 BMEdge *e;
116
117 BM_ITER_ELEM (e, &iter, verts[i], BM_EDGES_OF_VERT) {
118 flag |= BM_elem_flag_test(e, BM_ELEM_TAG) ? 1 : 2;
119 if (flag == (1 | 2)) {
120 break;
121 }
122 }
123
124 /* only boundary verts are interesting */
125 if (flag != (1 | 2)) {
126 STACK_REMOVE(verts, i);
127 }
128 }
129
130 /* possible but unlikely we have no mixed vertices */
131 if (STACK_SIZE(verts) == 0) [[unlikely]] {
132 MEM_delete(verts);
133 return;
134 }
135
136 /* main loop */
137 for (i = 0; i < STACK_SIZE(verts); i++) {
138 int v_edges_num = 0;
139 int v_edges_num_untag = 0;
140 BMVert *v = verts[i];
141 BMIter iter;
142 BMEdge *e;
143
144 BM_ITER_ELEM (e, &iter, verts[i], BM_EDGES_OF_VERT) {
146 BMVert *v_other;
147 BMIter liter;
148 BMLoop *l;
149
150 BM_ITER_ELEM (l, &liter, e, BM_LOOPS_OF_EDGE) {
152 }
153
154 v_other = BM_edge_other_vert(e, v);
155 BM_edge_split(bm, e, v_other, nullptr, 1.0f - OFFSET);
156 }
157 else {
158 v_edges_num_untag += 1;
159 }
160
161 v_edges_num += 1;
162 }
163
164#ifdef USE_CAP_OPTION
165 if (v_edges_num_untag == 1) {
167 }
168
169 CLAMP_MIN(v_edges_max, v_edges_num);
170#endif
171 }
172
173 for (i = 0; i < STACK_SIZE(verts); i++) {
174 BMVert *v = verts[i];
175 BMIter liter;
176 BMLoop *l;
177
178 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
179 if (BM_elem_flag_test(l->f, BM_ELEM_TAG) && (l->f->len != 3)) {
180 BMFace *f_cmp = l->f;
181 if ((BM_elem_index_get(l->next->v) == -1) && (BM_elem_index_get(l->prev->v) == -1)) {
182#ifdef USE_CAP_OPTION
183 if (use_cap_endpoint || (BMO_vert_flag_test(bm, v, ELE_VERT_ENDPOINT) == 0))
184#endif
185 {
186 BMLoop *l_new;
188 (BM_face_split(bm, l->f, l->prev, l->next, &l_new, nullptr, true) != nullptr))
189 {
190 BLI_assert(f_cmp == l->f);
191 BLI_assert(f_cmp != l_new->f);
192 UNUSED_VARS_NDEBUG(f_cmp);
194 }
195 }
196 }
197 else if (l->f->len > 4) {
199 if (BM_elem_index_get(l->next->v) == -1) {
200 if (BM_elem_index_get(l->prev->prev->v) == -1) {
201 BMLoop *l_new;
203 (BM_face_split(bm, l->f, l->prev->prev, l->next, &l_new, nullptr, true) !=
204 nullptr))
205 {
206 BLI_assert(f_cmp == l->f);
207 BLI_assert(f_cmp != l_new->f);
210 }
211 }
212 else {
213 /* walk backwards */
214 BMLoop *l_new;
215 bm_face_split_walk_back(bm, l, &l_new);
216 do {
218 l_new = l_new->next;
219 } while (BM_vert_is_edge_pair(l_new->v));
221 }
222 }
223
224/* NOTE: instead of duplicate code in alternate direction,
225 * we can be sure to hit the other vertex, so the code above runs. */
226#if 0
227 else if (BM_elem_index_get(l->prev->v) == -1) {
228 if (BM_elem_index_get(l->next->next->v) == -1) {
229 /* pass */
230 }
231 }
232#endif
233 }
234 }
235 }
236 }
237 }
238
239#ifdef USE_CAP_OPTION
240 if (use_cap_endpoint == false) {
242 BMVert **varr = varr_buf.data();
243 STACK_DECLARE(varr);
244 BMVert *v;
245
246 for (i = 0; i < STACK_SIZE(verts); i++) {
247 BMIter iter;
248 BMEdge *e;
249
250 v = verts[i];
251
252 STACK_INIT(varr, v_edges_max);
253
254 BM_ITER_ELEM (e, &iter, v, BM_EDGES_OF_VERT) {
255 BMVert *v_other;
256 v_other = BM_edge_other_vert(e, v);
257 if (BM_elem_index_get(v_other) == -1) {
258 if (BM_vert_is_edge_pair(v_other)) {
259 /* defer bmesh_kernel_join_edge_kill_vert to avoid looping over data we're removing */
260 v_other->e = e;
261 STACK_PUSH(varr, v_other);
262 }
263 }
264 }
265
266 while ((v = STACK_POP(varr))) {
267 bmesh_kernel_join_edge_kill_vert(bm, v->e, v, true, false, false, true);
268 }
269 }
270 }
271#endif
272
273 MEM_delete(verts);
274
276}
277
278} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define OFFSET(i)
#define STACK_POP(stack)
#define STACK_PUSH(stack, val)
#define STACK_DECLARE(stack)
#define STACK_SIZE(stack)
#define STACK_INIT(stack, stack_num)
#define STACK_REMOVE(stack, i)
Read Blender memory allocation and freeing.
#define UNUSED_VARS_NDEBUG(...)
#define CLAMP_MIN(a, b)
#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 BMO_edge_flag_enable(bm, e, oflag)
#define BMO_vert_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 ELE_NEW
#define ELE_VERT_ENDPOINT
const T * data() const
Definition BLI_array.hh:323
#define cos
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
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 BMLoop * l
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static BMFace * bm_face_split_walk_back(BMesh *bm, BMLoop *l_src, BMLoop **r_l)
BMEdge * bmesh_kernel_join_edge_kill_vert(BMesh *bm, BMEdge *e_kill, BMVert *v_kill, const bool do_del, const bool check_edge_exists, const bool kill_degenerate_faces, const bool kill_duplicate_faces)
Join Edge Kill Vert (JEKV).
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
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)
bool BM_face_split_check_double_face(BMLoop *l_a, BMLoop *l_b, const int f_len)
ATTR_WARN_UNUSED_RESULT const size_t num
BMFace * BM_face_split_n(BMesh *bm, BMFace *f, BMLoop *l_a, BMLoop *l_b, float cos[][3], int n, BMLoop **r_l, BMEdge *example)
Face Split with intermediate points.
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
BMVert * BM_edge_split(BMesh *bm, BMEdge *e, BMVert *v, BMEdge **r_e, float fac)
Edge Split.
void bmo_offset_edgeloops_exec(BMesh *bm, BMOperator *op)
BMFace * BM_face_split(BMesh *bm, BMFace *f, BMLoop *l_a, BMLoop *l_b, BMLoop **r_l, BMEdge *example, const bool no_double)
Face Split.
bool BM_vert_is_edge_pair(const BMVert *v)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * next
struct BMLoop * prev
struct BMEdge * e
struct BMVert * v
struct BMFace * f
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
struct BMEdge * e