Blender V5.3
bmesh_separate.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
11
12#include "bmesh_separate.hh" /* own include */
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_vector.hh"
17
18#include "bmesh.hh"
20
21namespace blender {
22
23void BM_mesh_separate_faces(BMesh *bm, BMFaceFilterFunc filter_fn, void *user_data)
24{
25 BMFace **faces_array_all = MEM_new_array_uninitialized<BMFace *>(bm->totface, __func__);
26 /*
27 * - Create an array of faces based on 'filter_fn'.
28 * First part of array for match, for non-match.
29 *
30 * - Enable all vertex tags, then clear all tagged vertices from 'faces_b'.
31 *
32 * - Loop over 'faces_a', checking each vertex,
33 * splitting out any which aren't tagged (and therefore shared), disabling tags as we go.
34 */
35
36 BMFace *f;
37 BMIter iter;
38
39 uint faces_a_len = 0;
40 uint faces_b_len = 0;
41 {
42 int i_a = 0;
43 int i_b = bm->totface;
44 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
45 faces_array_all[filter_fn(f, user_data) ? i_a++ : --i_b] = f;
46 }
47 faces_a_len = i_a;
48 faces_b_len = bm->totface - i_a;
49 }
50
51 BMFace **faces_a = faces_array_all;
52 BMFace **faces_b = faces_array_all + faces_a_len;
53
54 /* Enable for all. */
56
57 /* Disable vert tag on faces_b */
58 for (uint i = 0; i < faces_b_len; i++) {
59 BMLoop *l_iter, *l_first;
60 l_iter = l_first = BM_FACE_FIRST_LOOP(faces_b[i]);
61 do {
63 } while ((l_iter = l_iter->next) != l_first);
64 }
65
66 Vector<BMLoop *, 128> loop_split;
67
68 /* Check shared verts ('faces_a' tag and disable) */
69 for (uint i = 0; i < faces_a_len; i++) {
70 BMLoop *l_iter, *l_first;
71 l_iter = l_first = BM_FACE_FIRST_LOOP(faces_a[i]);
72 do {
73 if (!BM_elem_flag_test(l_iter->v, BM_ELEM_TAG)) {
74 BMVert *v = l_iter->v;
75 /* Enable, since we may visit this vertex again on other faces */
77
78 /* We know the vertex is shared, collect all vertices and split them off. */
79
80 /* Fill 'loop_split' */
81 {
82 BMEdge *e_first, *e_iter;
83 e_iter = e_first = l_iter->e;
84 do {
85 if (e_iter->l != nullptr) {
86 BMLoop *l_radial_first, *l_radial_iter;
87 l_radial_first = l_radial_iter = e_iter->l;
88 do {
89 if (l_radial_iter->v == v) {
90 if (filter_fn(l_radial_iter->f, user_data)) {
91 loop_split.append(l_radial_iter);
92 }
93 }
94 } while ((l_radial_iter = l_radial_iter->radial_next) != l_radial_first);
95 }
96 } while ((e_iter = bmesh_disk_edge_next(e_iter, v)) != e_first);
97 }
98
99 /* Perform the split */
100 BM_face_loop_separate_multi(bm, loop_split.data(), loop_split.size());
101
102 loop_split.clear();
103 }
104 } while ((l_iter = l_iter->next) != l_first);
105 }
106
107 MEM_delete(faces_array_all);
108}
109
110} // namespace blender
Read Blender memory allocation and freeing.
#define BM_FACE_FIRST_LOOP(p)
#define BM_elem_flag_disable(ele, hflag)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_MESH(ele, iter, bm, itype)
int64_t size() const
void append(const T &value)
BMesh Iterators.
void BM_mesh_elem_hflag_enable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
BMVert * BM_face_loop_separate_multi(BMesh *bm, BMLoop **larr, int larr_len)
unsigned int uint
Definition BKE_text.h:55
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
void BM_mesh_separate_faces(BMesh *bm, BMFaceFilterFunc filter_fn, void *user_data)
BLI_INLINE BMEdge * bmesh_disk_edge_next(const BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool(*)(const BMFace *, void *user_data) BMFaceFilterFunc
struct BMLoop * l
struct BMLoop * next
struct BMEdge * e
struct BMVert * v
struct BMLoop * radial_next
struct BMFace * f