Blender V5.0
bmesh_boolean.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 <functional>
12
13#include "BLI_array.hh"
14#include "BLI_math_base.h"
15#include "BLI_math_mpq.hh"
16#include "BLI_mesh_boolean.hh"
17#include "BLI_mesh_intersect.hh"
18
19#include "bmesh.hh"
20#include "bmesh_boolean.hh"
21#include "bmesh_edgesplit.hh"
22
23// #define PERF_DEBUG
24
25namespace blender::meshintersect {
26
27#ifdef WITH_GMP
28
38static IMesh mesh_from_bm(BMesh *bm,
39 const Span<std::array<BMLoop *, 3>> looptris,
40 IMesh *r_triangulated,
41 IMeshArena *arena)
42{
43 BLI_assert(r_triangulated != nullptr);
46 /* Account for triangulation and intersects. */
47 const int estimate_num_outv = 3 * bm->totvert;
48 const int estimate_num_outf = 4 * bm->totface;
49 arena->reserve(estimate_num_outv, estimate_num_outf);
50 Array<const Vert *> vert(bm->totvert);
51 for (int v = 0; v < bm->totvert; ++v) {
52 BMVert *bmv = BM_vert_at_index(bm, v);
53 vert[v] = arena->add_or_find_vert(mpq3(bmv->co[0], bmv->co[1], bmv->co[2]), v);
54 }
55 Array<Face *> face(bm->totface);
56 constexpr int estimated_max_facelen = 100;
57 Vector<const Vert *, estimated_max_facelen> face_vert;
58 Vector<int, estimated_max_facelen> face_edge_orig;
59 for (int f = 0; f < bm->totface; ++f) {
60 BMFace *bmf = BM_face_at_index(bm, f);
61 int flen = bmf->len;
62 face_vert.clear();
63 face_edge_orig.clear();
64 BMLoop *l = bmf->l_first;
65 for (int i = 0; i < flen; ++i) {
66 const Vert *v = vert[BM_elem_index_get(l->v)];
67 face_vert.append(v);
68 int e_index = BM_elem_index_get(l->e);
69 face_edge_orig.append(e_index);
70 l = l->next;
71 }
72 face[f] = arena->add_face(face_vert, f, face_edge_orig);
73 }
74 /* Now do the triangulation mesh.
75 * The loop_tris have accurate v and f members for the triangles,
76 * but their next and e pointers are not correct for the loops
77 * that start added-diagonal edges. */
78 Array<Face *> tri_face(looptris.size());
79 face_vert.resize(3);
80 face_edge_orig.resize(3);
81 for (const int i : looptris.index_range()) {
82 BMFace *bmf = looptris[i][0]->f;
83 int f = BM_elem_index_get(bmf);
84 for (int j = 0; j < 3; ++j) {
85 BMLoop *l = looptris[i][j];
86 int v_index = BM_elem_index_get(l->v);
87 int e_index;
88 if (l->next->v == looptris[i][(j + 1) % 3]->v) {
89 e_index = BM_elem_index_get(l->e);
90 }
91 else {
92 e_index = NO_INDEX;
93 }
94 face_vert[j] = vert[v_index];
95 face_edge_orig[j] = e_index;
96 }
97 tri_face[i] = arena->add_face(face_vert, f, face_edge_orig);
98 }
99 r_triangulated->set_faces(tri_face);
100 return IMesh(face);
101}
102
103static bool bmvert_attached_to_wire(const BMVert *bmv)
104{
105 /* This is not quite right. It returns true if the only edges
106 * Attached to \a bmv are wire edges. TODO: iterate through edges
107 * attached to \a bmv and check #BM_edge_is_wire. */
108 return BM_vert_is_wire(bmv);
109}
110
111static bool bmvert_attached_to_hidden_face(BMVert *bmv)
112{
113 BMIter iter;
114 for (BMFace *bmf = static_cast<BMFace *>(BM_iter_new(&iter, nullptr, BM_FACES_OF_VERT, bmv));
115 bmf;
116 bmf = static_cast<BMFace *>(BM_iter_step(&iter)))
117 {
119 return true;
120 }
121 }
122 return false;
123}
124
125static bool face_has_verts_in_order(BMesh *bm, BMFace *bmf, const BMVert *v1, const BMVert *v2)
126{
127 BMIter liter;
128 BMLoop *l = static_cast<BMLoop *>(BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, bmf));
129 while (l != nullptr) {
130 if (l->v == v1 && l->next->v == v2) {
131 return true;
132 }
133 l = static_cast<BMLoop *>(BM_iter_step(&liter));
134 }
135 return false;
136}
137
139constexpr uint KEEP_FLAG = (1 << 6);
140
148static bool apply_mesh_output_to_bmesh(BMesh *bm, IMesh &m_out, bool keep_hidden)
149{
150 bool any_change = false;
151
152 m_out.populate_vert();
153
154 /* Initially mark all existing verts as "don't keep", except hidden verts
155 * (if keep_hidden is true), and verts attached to wire edges. */
156 for (int v = 0; v < bm->totvert; ++v) {
157 BMVert *bmv = BM_vert_at_index(bm, v);
158 if ((keep_hidden &&
159 (BM_elem_flag_test(bmv, BM_ELEM_HIDDEN) || bmvert_attached_to_hidden_face(bmv))) ||
160 bmvert_attached_to_wire(bmv))
161 {
162 BM_elem_flag_enable(bmv, KEEP_FLAG);
163 }
164 else {
165 BM_elem_flag_disable(bmv, KEEP_FLAG);
166 }
167 }
168
169 /* Reuse old or make new #BMVert's, depending on if there's an orig or not.
170 * For those reused, mark them "keep".
171 * Store needed old #BMVert's in new_bmvs first, as the table may be unusable after
172 * creating a new #BMVert. */
173 Array<BMVert *> new_bmvs(m_out.vert_size());
174 for (int v : m_out.vert_index_range()) {
175 const Vert *vertp = m_out.vert(v);
176 int orig = vertp->orig;
177 if (orig != NO_INDEX) {
178 BLI_assert(orig >= 0 && orig < bm->totvert);
179 BMVert *bmv = BM_vert_at_index(bm, orig);
180 new_bmvs[v] = bmv;
181 BM_elem_flag_enable(bmv, KEEP_FLAG);
182 }
183 else {
184 new_bmvs[v] = nullptr;
185 }
186 }
187 for (int v : m_out.vert_index_range()) {
188 const Vert *vertp = m_out.vert(v);
189 if (new_bmvs[v] == nullptr) {
190 float co[3];
191 const double3 &d_co = vertp->co;
192 for (int i = 0; i < 3; ++i) {
193 co[i] = float(d_co[i]);
194 }
195 BMVert *bmv = BM_vert_create(bm, co, nullptr, BM_CREATE_NOP);
196 new_bmvs[v] = bmv;
197 BM_elem_flag_enable(bmv, KEEP_FLAG);
198 any_change = true;
199 }
200 }
201
202 /* Initially mark all existing faces as "don't keep", except hidden faces (if keep_hidden).
203 * Also, save current #BMFace pointers as creating faces will disturb the table. */
204 Array<BMFace *> old_bmfs(bm->totface);
206 for (int f = 0; f < bm->totface; ++f) {
207 BMFace *bmf = BM_face_at_index(bm, f);
208 old_bmfs[f] = bmf;
209 if (keep_hidden && BM_elem_flag_test(bmf, BM_ELEM_HIDDEN)) {
210 BM_elem_flag_enable(bmf, KEEP_FLAG);
211 }
212 else {
213 BM_elem_flag_disable(bmf, KEEP_FLAG);
214 }
215 }
216
217 /* Save the original #BMEdge's so we can use them as examples. */
218 Array<BMEdge *> old_edges(bm->totedge);
219 std::copy_n(bm->etable, bm->totedge, old_edges.begin());
220
221 /* Reuse or make new #BMFace's, as the faces are identical to old ones or not.
222 * If reusing, mark them as "keep". First find the maximum face length
223 * so we can declare some arrays outside of the face-creating loop. */
224 int maxflen = 0;
225 for (const Face *f : m_out.faces()) {
226 maxflen = max_ii(maxflen, f->size());
227 }
228 Array<BMVert *> face_bmverts(maxflen);
229 Array<BMEdge *> face_bmedges(maxflen);
230 for (const Face *f : m_out.faces()) {
231 const Face &face = *f;
232 int flen = face.size();
233 for (int i = 0; i < flen; ++i) {
234 const Vert *v = face[i];
235 int v_index = m_out.lookup_vert(v);
236 BLI_assert(v_index < new_bmvs.size());
237 face_bmverts[i] = new_bmvs[v_index];
238 }
239 BMFace *bmf = BM_face_exists(face_bmverts.data(), flen);
240 /* #BM_face_exists checks if the face exists with the vertices in either order.
241 * We can only reuse the face if the orientations are the same. */
242 if (bmf != nullptr && face_has_verts_in_order(bm, bmf, face_bmverts[0], face_bmverts[1])) {
243 BM_elem_flag_enable(bmf, KEEP_FLAG);
244 }
245 else {
246 int orig = face.orig;
247 BMFace *orig_face;
248 /* There should always be an orig face, but just being extra careful here. */
249 if (orig != NO_INDEX) {
250 orig_face = old_bmfs[orig];
251 }
252 else {
253 orig_face = nullptr;
254 }
255 /* Make or find #BMEdge's. */
256 for (int i = 0; i < flen; ++i) {
257 BMVert *bmv1 = face_bmverts[i];
258 BMVert *bmv2 = face_bmverts[(i + 1) % flen];
259 BMEdge *bme = BM_edge_exists(bmv1, bmv2);
260 if (bme == nullptr) {
261 BMEdge *orig_edge = nullptr;
262 if (face.edge_orig[i] != NO_INDEX) {
263 orig_edge = old_edges[face.edge_orig[i]];
264 }
265 bme = BM_edge_create(bm, bmv1, bmv2, orig_edge, BM_CREATE_NOP);
266 if (orig_edge != nullptr) {
267 BM_elem_select_copy(bm, bme, orig_edge);
268 }
269 }
270 face_bmedges[i] = bme;
271 if (face.is_intersect[i]) {
273 }
274 else {
276 }
277 }
278 BMFace *bmf = BM_face_create(
279 bm, face_bmverts.data(), face_bmedges.data(), flen, orig_face, BM_CREATE_NOP);
280 if (orig_face != nullptr) {
281 BM_elem_select_copy(bm, bmf, orig_face);
282 }
283 BM_elem_flag_enable(bmf, KEEP_FLAG);
284 /* Now do interpolation of loop data (e.g., UVs) using the example face. */
285 if (orig_face != nullptr) {
286 BMIter liter;
287 BMLoop *l = static_cast<BMLoop *>(BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, bmf));
288 while (l != nullptr) {
289 BM_loop_interp_from_face(bm, l, orig_face, false, true);
290 l = static_cast<BMLoop *>(BM_iter_step(&liter));
291 }
292 }
293 any_change = true;
294 }
295 }
296
297 /* Now kill the unused faces and verts, and clear flags for kept ones. */
298 /* #BM_ITER_MESH_MUTABLE macro needs type casts for C++, so expand here.
299 * TODO(howard): make some nice C++ iterators for #BMesh. */
300 BMIter iter;
301 BMFace *bmf = static_cast<BMFace *>(BM_iter_new(&iter, bm, BM_FACES_OF_MESH, nullptr));
302 while (bmf != nullptr) {
303# ifndef NDEBUG
305# endif
306 BMFace *bmf_next = static_cast<BMFace *>(BM_iter_step(&iter));
307 if (BM_elem_flag_test(bmf, KEEP_FLAG)) {
308 BM_elem_flag_disable(bmf, KEEP_FLAG);
309 }
310 else {
312# if 0
313 BM_face_kill(bm, bmf);
314# endif
315 any_change = true;
316 }
317 bmf = bmf_next;
318 }
319 BMVert *bmv = static_cast<BMVert *>(BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, nullptr));
320 while (bmv != nullptr) {
321# ifndef NDEBUG
323# endif
324 BMVert *bmv_next = static_cast<BMVert *>(BM_iter_step(&iter));
325 if (BM_elem_flag_test(bmv, KEEP_FLAG)) {
326 BM_elem_flag_disable(bmv, KEEP_FLAG);
327 }
328 else {
329 BM_vert_kill(bm, bmv);
330 any_change = true;
331 }
332 bmv = bmv_next;
333 }
334
335 return any_change;
336}
337
338static bool bmesh_boolean(BMesh *bm,
339 const Span<std::array<BMLoop *, 3>> looptris,
340 int (*test_fn)(BMFace *f, void *user_data),
341 void *user_data,
342 int nshapes,
343 const bool use_self,
344 const bool use_separate_all,
345 const bool keep_hidden,
346 const bool hole_tolerant,
347 const BoolOpType boolean_mode)
348{
349 IMeshArena arena;
350 IMesh m_triangulated;
351# ifdef PERF_DEBUG
352 double start_time = BLI_time_now_seconds();
353# endif
354 IMesh m_in = mesh_from_bm(bm, looptris, &m_triangulated, &arena);
355# ifdef PERF_DEBUG
356 double mesh_time = BLI_time_now_seconds();
357 std::cout << "bmesh_boolean, imesh_from_bm done, time = " << mesh_time - start_time << "\n";
358# endif
359 std::function<int(int)> shape_fn;
360 if (use_self && boolean_mode == BoolOpType::None) {
361 /* Unary knife operation. Want every face where test_fn doesn't return -1. */
362 BLI_assert(nshapes == 1);
363 shape_fn = [bm, test_fn, user_data](int f) {
364 BMFace *bmf = BM_face_at_index(bm, f);
365 if (test_fn(bmf, user_data) != -1) {
366 return 0;
367 }
368 return -1;
369 };
370 }
371 else {
372 shape_fn = [bm, test_fn, user_data](int f) {
373 BMFace *bmf = BM_face_at_index(bm, f);
374 int test_val = test_fn(bmf, user_data);
375 if (test_val >= 0) {
376 return test_val;
377 }
378 return -1;
379 };
380 }
381 IMesh m_out = boolean_mesh(
382 m_in, boolean_mode, nshapes, shape_fn, use_self, hole_tolerant, &m_triangulated, &arena);
383# ifdef PERF_DEBUG
384 double boolean_time = BLI_time_now_seconds();
385 std::cout << "boolean done, time = " << boolean_time - mesh_time << "\n";
386# endif
387 bool any_change = apply_mesh_output_to_bmesh(bm, m_out, keep_hidden);
388# ifdef PERF_DEBUG
389 double apply_mesh_time = BLI_time_now_seconds();
390 std::cout << "applied boolean output to bmesh, time = " << apply_mesh_time - boolean_time
391 << "\n";
392# endif
393 if (use_separate_all) {
394 /* We are supposed to separate all faces that are incident on intersection edges. */
395 BM_mesh_edgesplit(bm, false, true, false);
396 }
397 return any_change;
398}
399
400#endif // WITH_GMP
401
402} // namespace blender::meshintersect
403
420#ifdef WITH_GMP
422 const blender::Span<std::array<BMLoop *, 3>> looptris,
423 int (*test_fn)(BMFace *f, void *user_data),
424 void *user_data,
425 const int nshapes,
426 const bool use_self,
427 const bool keep_hidden,
428 const bool hole_tolerant,
429 const int boolean_mode)
430{
431 return blender::meshintersect::bmesh_boolean(
432 bm,
433 looptris,
434 test_fn,
435 user_data,
436 nshapes,
437 use_self,
438 false,
439 keep_hidden,
440 hole_tolerant,
441 static_cast<blender::meshintersect::BoolOpType>(boolean_mode));
442}
443
445 const blender::Span<std::array<BMLoop *, 3>> looptris,
446 int (*test_fn)(BMFace *f, void *user_data),
447 void *user_data,
448 const int nshapes,
449 const bool use_self,
450 const bool use_separate_all,
451 const bool hole_tolerant,
452 const bool keep_hidden)
453{
454 return blender::meshintersect::bmesh_boolean(bm,
455 looptris,
456 test_fn,
457 user_data,
458 nshapes,
459 use_self,
460 use_separate_all,
461 keep_hidden,
462 hole_tolerant,
463 blender::meshintersect::BoolOpType::None);
464}
465#else
466bool BM_mesh_boolean(BMesh * /*bm*/,
467 blender::Span<std::array<BMLoop *, 3>> looptris,
468 int (*test_fn)(BMFace *, void *),
469 void * /*user_data*/,
470 const int /*nshapes*/,
471 const bool /*use_self*/,
472 const bool /*keep_hidden*/,
473 const bool /*hole_tolerant*/,
474 const int /*boolean_mode*/)
475{
476 UNUSED_VARS(looptris, test_fn);
477 return false;
478}
479
489 blender::Span<std::array<BMLoop *, 3>> looptris,
490 int (*test_fn)(BMFace *, void *),
491 void * /*user_data*/,
492 const int /*nshapes*/,
493 const bool /*use_self*/,
494 const bool /*use_separate_all*/,
495 const bool /*hole_tolerant*/,
496 const bool /*keep_hidden*/)
497{
498 UNUSED_VARS(looptris, test_fn);
499 return false;
500}
501#endif
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE int max_ii(int a, int b)
unsigned int uint
double BLI_time_now_seconds(void)
Definition time.cc:113
#define UNUSED_VARS(...)
bool BM_mesh_boolean_knife(BMesh *, blender::Span< std::array< BMLoop *, 3 > > looptris, int(*test_fn)(BMFace *, void *), void *, const int, const bool, const bool, const bool, const bool)
bool BM_mesh_boolean(BMesh *, blender::Span< std::array< BMLoop *, 3 > > looptris, int(*test_fn)(BMFace *, void *), void *, const int, const bool, const bool, const bool, const int)
@ BM_ELEM_HIDDEN
@ BM_ELEM_TAG
void BM_elem_select_copy(BMesh *bm_dst, void *ele_dst_v, const void *ele_src_v)
void BM_vert_kill(BMesh *bm, BMVert *v)
void BM_face_kill(BMesh *bm, BMFace *f)
void BM_face_kill_loose(BMesh *bm, BMFace *f)
BMFace * BM_face_create(BMesh *bm, BMVert *const *verts, BMEdge *const *edges, const int len, const BMFace *f_example, const eBMCreateFlag create_flag)
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.cc:41
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.
@ BM_CREATE_NOP
Definition bmesh_core.hh:28
void BM_mesh_edgesplit(BMesh *bm, const bool use_verts, const bool tag_only, const bool copy_select)
#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)
void BM_loop_interp_from_face(BMesh *bm, BMLoop *l_dst, const BMFace *f_src, const bool do_vertex, const bool do_multires)
int BM_iter_mesh_count(const char itype, BMesh *bm)
@ BM_FACES_OF_VERT
@ BM_VERTS_OF_MESH
@ BM_FACES_OF_MESH
@ BM_LOOPS_OF_FACE
#define BM_iter_new(iter, bm, itype, data)
BMesh * bm
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
BLI_INLINE BMFace * BM_face_at_index(BMesh *bm, const int index)
#define BM_FACE
#define BM_EDGE
#define BM_VERT
bool BM_vert_is_wire(const BMVert *v)
BMFace * BM_face_exists(BMVert *const *varr, int len)
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert * v
void append(const T &value)
void resize(const int64_t new_size)
void clear()
nullptr float
VecBase< double, 3 > double3
BMLoop * l_first
struct BMVert * v
struct BMEdge * e
struct BMLoop * next
float co[3]
int totvert
BMEdge ** etable
int totedge
int totface
i
Definition text_draw.cc:230