34 if (!isfinite(co[0])) [[unlikely]] {
37 if (!isfinite(co[1])) [[unlikely]] {
40 if (!isfinite(co[2])) [[unlikely]] {
55static IMesh mesh_from_bm(BMesh *
bm,
56 const Span<std::array<BMLoop *, 3>> looptris,
57 IMesh *r_triangulated,
64 const int estimate_num_outv = 3 *
bm->
totvert;
65 const int estimate_num_outf = 4 *
bm->
totface;
66 arena->reserve(estimate_num_outv, estimate_num_outf);
70 const float3 co = clean_float3(bmv->co);
71 vert[
v] = arena->add_or_find_vert(mpq3(co[0], co[1], co[2]),
v);
74 constexpr int estimated_max_facelen = 100;
75 Vector<const Vert *, estimated_max_facelen> face_vert;
76 Vector<int, estimated_max_facelen> face_edge_orig;
81 face_edge_orig.clear();
82 BMLoop *
l = bmf->l_first;
83 for (
int i = 0; i < flen; ++i) {
87 face_edge_orig.append(e_index);
90 face[f] = arena->add_face(face_vert, f, face_edge_orig);
96 Array<Face *> tri_face(looptris.size());
98 face_edge_orig.resize(3);
99 for (
const int i : looptris.index_range()) {
100 BMFace *bmf = looptris[i][0]->f;
102 for (
int j = 0; j < 3; ++j) {
103 BMLoop *
l = looptris[i][j];
106 if (
l->
next->
v == looptris[i][(j + 1) % 3]->v) {
112 face_vert[j] =
vert[v_index];
113 face_edge_orig[j] = e_index;
115 tri_face[i] = arena->add_face(face_vert, f, face_edge_orig);
117 r_triangulated->set_faces(tri_face);
121static bool bmvert_attached_to_wire(
const BMVert *bmv)
129static bool bmvert_attached_to_hidden_face(BMVert *bmv)
134 bmf =
static_cast<BMFace *
>(BM_iter_step(&iter)))
144constexpr uint KEEP_FLAG = (1 << 6);
153static bool apply_mesh_output_to_bmesh(BMesh *
bm, IMesh &m_out,
bool keep_hidden)
155 bool any_change =
false;
157 m_out.populate_vert();
165 bmvert_attached_to_wire(bmv))
178 Array<BMVert *> new_bmvs(m_out.vert_size());
179 for (
int v : m_out.vert_index_range()) {
180 const Vert *vertp = m_out.vert(
v);
181 int orig = vertp->orig;
182 if (orig != NO_INDEX) {
189 new_bmvs[
v] =
nullptr;
192 for (
int v : m_out.vert_index_range()) {
193 const Vert *vertp = m_out.vert(
v);
194 if (new_bmvs[
v] ==
nullptr) {
196 const double3 &d_co = vertp->co;
197 for (
int i = 0; i < 3; ++i) {
198 co[i] =
float(d_co[i]);
230 for (
const Face *f : m_out.faces()) {
231 maxflen =
max_ii(maxflen, f->size());
233 Array<BMVert *> face_bmverts(maxflen);
234 Array<BMEdge *> face_bmedges(maxflen);
235 for (
const Face *f : m_out.faces()) {
236 const Face &face = *f;
237 int flen = face.size();
238 for (
int i = 0; i < flen; ++i) {
239 const Vert *
v = face[i];
240 int v_index = m_out.lookup_vert(
v);
242 face_bmverts[i] = new_bmvs[v_index];
246 if (bmf !=
nullptr) {
250 int orig = face.orig;
253 if (orig != NO_INDEX) {
254 orig_face = old_bmfs[orig];
260 for (
int i = 0; i < flen; ++i) {
261 BMVert *bmv1 = face_bmverts[i];
262 BMVert *bmv2 = face_bmverts[(i + 1) % flen];
264 if (bme ==
nullptr) {
265 BMEdge *orig_edge =
nullptr;
266 if (face.edge_orig[i] != NO_INDEX) {
267 orig_edge = old_edges[face.edge_orig[i]];
270 if (orig_edge !=
nullptr) {
274 face_bmedges[i] = bme;
275 if (face.is_intersect[i]) {
283 bm, face_bmverts.data(), face_bmedges.data(), flen, orig_face,
BM_CREATE_NOP);
284 if (orig_face !=
nullptr) {
289 if (orig_face !=
nullptr) {
292 while (
l !=
nullptr) {
294 l =
static_cast<BMLoop *
>(BM_iter_step(&liter));
306 while (bmf !=
nullptr) {
310 BMFace *bmf_next =
static_cast<BMFace *
>(BM_iter_step(&iter));
324 while (bmv !=
nullptr) {
328 BMVert *bmv_next =
static_cast<BMVert *
>(BM_iter_step(&iter));
342static bool bmesh_boolean(BMesh *
bm,
343 const Span<std::array<BMLoop *, 3>> looptris,
344 int (*test_fn)(BMFace *f,
void *user_data),
348 const bool use_separate_all,
349 const bool keep_hidden,
350 const bool hole_tolerant,
351 const BoolOpType boolean_mode)
354 IMesh m_triangulated;
358 IMesh m_in = mesh_from_bm(
bm, looptris, &m_triangulated, &arena);
361 std::cout <<
"bmesh_boolean, imesh_from_bm done, time = " << mesh_time - start_time <<
"\n";
363 std::function<int(
int)> shape_fn;
364 if (use_self && boolean_mode == BoolOpType::None) {
367 shape_fn = [
bm, test_fn, user_data](
int f) {
369 if (test_fn(bmf, user_data) != -1) {
376 shape_fn = [
bm, test_fn, user_data](
int f) {
378 int test_val = test_fn(bmf, user_data);
385 IMesh m_out = boolean_mesh(
386 m_in, boolean_mode, nshapes, shape_fn, use_self, hole_tolerant, &m_triangulated, &arena);
389 std::cout <<
"boolean done, time = " << boolean_time - mesh_time <<
"\n";
391 bool any_change = apply_mesh_output_to_bmesh(
bm, m_out, keep_hidden);
394 std::cout <<
"applied boolean output to bmesh, time = " << apply_mesh_time - boolean_time
397 if (use_separate_all) {
426 const Span<std::array<BMLoop *, 3>> looptris,
427 int (*test_fn)(
BMFace *f,
void *user_data),
431 const bool keep_hidden,
432 const bool hole_tolerant,
433 const int boolean_mode)
435 return meshintersect::bmesh_boolean(
bm,
444 static_cast<meshintersect::BoolOpType
>(boolean_mode));
448 const Span<std::array<BMLoop *, 3>> looptris,
449 int (*test_fn)(
BMFace *f,
void *user_data),
453 const bool use_separate_all,
454 const bool hole_tolerant,
455 const bool keep_hidden)
457 return meshintersect::bmesh_boolean(
bm,
466 meshintersect::BoolOpType::None);
470 Span<std::array<BMLoop *, 3>> looptris,
471 int (*test_fn)(
BMFace *,
void *),
484 Span<std::array<BMLoop *, 3>> looptris,
485 int (*test_fn)(
BMFace *,
void *),
#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_new(iter, bm, itype, data)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
bool BM_mesh_boolean(BMesh *, Span< std::array< BMLoop *, 3 > > looptris, int(*test_fn)(BMFace *, void *), void *, const int, const bool, const bool, const bool, const int)
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
void BM_vert_kill(BMesh *bm, BMVert *v)
bool BM_vert_is_wire(const BMVert *v)
MINLINE int max_ii(int a, int b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
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.
BMFace * BM_face_create(BMesh *bm, BMVert *const *verts, BMEdge *const *edges, const int len, const BMFace *f_example, const eBMCreateFlag create_flag)
int BM_iter_mesh_count(const char itype, BMesh *bm)
bool BM_mesh_boolean_knife(BMesh *, Span< std::array< BMLoop *, 3 > > looptris, int(*test_fn)(BMFace *, void *), void *, const int, const bool, const bool, const bool, const bool)
void BM_elem_select_copy(BMesh *bm_dst, void *ele_dst_v, const void *ele_src_v)
void BM_face_kill_loose(BMesh *bm, BMFace *f)
VecBase< double, 3 > double3
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
BMFace * BM_face_exists(BMVert *const *varr, int len)
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
void BM_face_kill(BMesh *bm, BMFace *f)
void BM_mesh_edgesplit(BMesh *bm, const bool use_verts, const bool tag_only, const bool copy_select)
double BLI_time_now_seconds()
BLI_INLINE BMFace * BM_face_at_index(BMesh *bm, const int index)
VecBase< float, 3 > float3
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.
void BM_loop_interp_from_face(BMesh *bm, BMLoop *l_dst, const BMFace *f_src, const bool do_vertex, const bool do_multires)
void vert(const int vert_id, float4 &position)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float