Blender V5.3
editmesh_automerge.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#include "BKE_editmesh.hh"
15
16#include "DNA_mesh_types.h"
17#include "DNA_object_types.h"
18
19#include "ED_mesh.hh"
20
22
23namespace blender {
24
25// #define DEBUG_TIME
26#ifdef DEBUG_TIME
27# include "BLI_time.hh"
28#endif
29
30/* use bmesh operator flags for a few operators */
31#define BMO_ELE_TAG 1
32
33/* -------------------------------------------------------------------- */
38
39static bool edbm_automerge_impl(Object *obedit,
40 bool update,
41 const char hflag,
42 const float dist,
43 const bool use_connected,
44 const bool use_centroid)
45{
47 int totvert_prev = bm->totvert;
48
49 BMOperator findop, weldop;
50
51 /* Search for doubles among all vertices, but only merge non-VERT_KEEP
52 * vertices into VERT_KEEP vertices. */
54 &findop,
56 "find_doubles verts=%av keep_verts=%Hv dist=%f use_connected=%b",
57 hflag,
58 dist,
59 use_connected);
60
61 BMO_op_exec(bm, &findop);
62
63 /* weld the vertices */
64 BMO_op_initf(bm, &weldop, BMO_FLAG_DEFAULTS, "weld_verts use_centroid=%b", use_centroid);
65 BMO_slot_copy(&findop, slots_out, "targetmap.out", &weldop, slots_in, "targetmap");
66 BMO_op_exec(bm, &weldop);
67
68 BMO_op_finish(bm, &findop);
69 BMO_op_finish(bm, &weldop);
70
71 bool changed = totvert_prev != bm->totvert;
72 if (changed && update) {
73 EDBMUpdate_Params params{};
74 params.calc_looptris = true;
75 params.calc_normals = false;
76 params.is_destructive = true;
77 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
78 }
79 return changed;
80}
81
83 Object *obedit, bool update, const char hflag, const float dist, const bool use_centroid)
84{
85 return edbm_automerge_impl(obedit, update, hflag, dist, false, use_centroid);
86}
87
88bool EDBM_automerge_connected(Object *obedit, bool update, const char hflag, const float dist)
89{
90 return edbm_automerge_impl(obedit, update, hflag, dist, true, false);
91}
92
94
95/* -------------------------------------------------------------------- */
100
102 const bool /*split_edges*/,
103 const bool split_faces,
104 const bool update,
105 const char hflag,
106 const float dist)
107{
108 bool ok = false;
109
111
112#ifdef DEBUG_TIME
114 em->bm = BM_mesh_copy(bm);
115
116 double t1 = BLI_time_now_seconds();
117 EDBM_automerge(obedit, false, hflag, dist);
118 t1 = BLI_time_now_seconds() - t1;
119
120 BM_mesh_free(em->bm);
121 em->bm = bm;
122 double t2 = BLI_time_now_seconds();
123#endif
124
125 BMOperator weldop;
126 BMOpSlot *slot_targetmap;
127
128 BMO_op_init(bm, &weldop, BMO_FLAG_DEFAULTS, "weld_verts");
129 slot_targetmap = BMO_slot_get(weldop.slots_in, "targetmap");
130
131 GHash *ghash_targetmap = BMO_SLOT_AS_GHASH(slot_targetmap);
132
133 ok = BM_mesh_intersect_edges(bm, hflag, dist, split_faces, ghash_targetmap);
134
135 if (ok) {
136 BMO_op_exec(bm, &weldop);
137 }
138
139 BMO_op_finish(bm, &weldop);
140
141#ifdef DEBUG_TIME
142 t2 = BLI_time_now_seconds() - t2;
143 printf("t1: %lf; t2: %lf; fac: %lf\n", t1, t2, t1 / t2);
144#endif
145
146 if (LIKELY(ok) && update) {
147 EDBMUpdate_Params params{};
148 params.calc_looptris = true;
149 params.calc_normals = false;
150 params.is_destructive = true;
151 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
152 }
153
154 return ok;
155}
156
158
159} // namespace blender
Platform independent time functions.
Object is a sort of wrapper for general info.
#define LIKELY(x)
#define BMO_slot_copy(op_src, slots_src, slot_name_src, op_dst, slots_dst, slot_name_dst)
#define BMO_SLOT_AS_GHASH(slot)
#define BMO_FLAG_DEFAULTS
BMesh Iterators.
bool EDBM_automerge(Object *obedit, bool update, char hflag, float dist, bool use_centroid)
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void EDBM_update(Mesh *mesh, const EDBMUpdate_Params *params)
bool BMO_op_initf(BMesh *bm, BMOperator *op, int flag, const char *fmt,...)
void BMO_op_exec(BMesh *bm, BMOperator *op)
BMESH OPSTACK EXEC OP.
bool EDBM_automerge_connected(Object *obedit, bool update, char hflag, float dist)
BMesh * BM_mesh_copy(BMesh *bm_old)
static bool edbm_automerge_impl(Object *obedit, bool update, const char hflag, const float dist, const bool use_connected, const bool use_centroid)
bool EDBM_automerge_and_split(Object *obedit, bool split_edges, bool split_faces, bool update, char hflag, float dist)
BMOpSlot * BMO_slot_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *identifier)
BMESH OPSTACK GET SLOT.
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:59
bool BM_mesh_intersect_edges(BMesh *bm, const char hflag, const float dist, const bool split_faces, GHash *r_targetmap)
double BLI_time_now_seconds()
Definition time.cc:30
void BMO_op_init(BMesh *bm, BMOperator *op, int flag, const char *opname)
BMESH OPSTACK INIT OP.
void BMO_op_finish(BMesh *bm, BMOperator *op)
BMESH OPSTACK FINISH OP.
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]