Blender  V2.93
editmesh_automerge.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 
29 #include "BKE_editmesh.h"
30 
31 #include "DNA_object_types.h"
32 
33 #include "ED_mesh.h"
34 
36 
37 //#define DEBUG_TIME
38 #ifdef DEBUG_TIME
39 # include "PIL_time.h"
40 #endif
41 
42 /* use bmesh operator flags for a few operators */
43 #define BMO_ELE_TAG 1
44 
45 /* -------------------------------------------------------------------- */
51 void EDBM_automerge(Object *obedit, bool update, const char hflag, const float dist)
52 {
54  BMesh *bm = em->bm;
55  int totvert_prev = bm->totvert;
56 
57  BMOperator findop, weldop;
58 
59  /* Search for doubles among all vertices, but only merge non-VERT_KEEP
60  * vertices into VERT_KEEP vertices. */
62  &findop,
64  "find_doubles verts=%av keep_verts=%Hv dist=%f",
65  hflag,
66  dist);
67 
68  BMO_op_exec(bm, &findop);
69 
70  /* weld the vertices */
71  BMO_op_init(bm, &weldop, BMO_FLAG_DEFAULTS, "weld_verts");
72  BMO_slot_copy(&findop, slots_out, "targetmap.out", &weldop, slots_in, "targetmap");
73  BMO_op_exec(bm, &weldop);
74 
75  BMO_op_finish(bm, &findop);
76  BMO_op_finish(bm, &weldop);
77 
78  if ((totvert_prev != bm->totvert) && update) {
79  EDBM_update_generic(obedit->data, true, true);
80  }
81 }
82 
85 /* -------------------------------------------------------------------- */
92  const bool UNUSED(split_edges),
93  const bool split_faces,
94  const bool update,
95  const char hflag,
96  const float dist)
97 {
98  bool ok = false;
99 
100  BMEditMesh *em = BKE_editmesh_from_object(obedit);
101  BMesh *bm = em->bm;
102 
103 #ifdef DEBUG_TIME
104  em->bm = BM_mesh_copy(bm);
105 
106  double t1 = PIL_check_seconds_timer();
107  EDBM_automerge(obedit, false, hflag, dist);
108  t1 = PIL_check_seconds_timer() - t1;
109 
110  BM_mesh_free(em->bm);
111  em->bm = bm;
112  double t2 = PIL_check_seconds_timer();
113 #endif
114 
115  BMOperator weldop;
116  BMOpSlot *slot_targetmap;
117 
118  BMO_op_init(bm, &weldop, BMO_FLAG_DEFAULTS, "weld_verts");
119  slot_targetmap = BMO_slot_get(weldop.slots_in, "targetmap");
120 
121  GHash *ghash_targetmap = BMO_SLOT_AS_GHASH(slot_targetmap);
122 
123  ok = BM_mesh_intersect_edges(bm, hflag, dist, split_faces, ghash_targetmap);
124 
125  if (ok) {
126  BMO_op_exec(bm, &weldop);
127  }
128 
129  BMO_op_finish(bm, &weldop);
130 
131 #ifdef DEBUG_TIME
132  t2 = PIL_check_seconds_timer() - t2;
133  printf("t1: %lf; t2: %lf; fac: %lf\n", t1, t2, t1 / t2);
134 #endif
135 
136  if (LIKELY(ok) && update) {
137  EDBM_update_generic(obedit->data, true, true);
138  }
139 }
140 
BMEditMesh * BKE_editmesh_from_object(struct Object *ob)
Return the BMEditMesh for a given object.
Definition: editmesh.c:85
#define UNUSED(x)
#define LIKELY(x)
Object is a sort of wrapper for general info.
void EDBM_update_generic(struct Mesh *me, const bool do_tessellation, const bool is_destructive)
Platform independent time functions.
BMesh * BM_mesh_copy(BMesh *bm_old)
bool BM_mesh_intersect_edges(BMesh *bm, const char hflag, const float dist, const bool split_faces, GHash *r_targetmap)
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
Definition: bmesh_mesh.c:307
void BMO_op_exec(BMesh *bm, BMOperator *op)
BMESH OPSTACK EXEC OP.
bool BMO_op_initf(BMesh *bm, BMOperator *op, const int flag, const char *fmt,...)
#define BMO_slot_copy(op_src, slots_src, slot_name_src, op_dst, slots_dst, slot_name_dst)
#define BMO_SLOT_AS_GHASH(slot)
void BMO_op_finish(BMesh *bm, BMOperator *op)
BMESH OPSTACK FINISH OP.
BMOpSlot * BMO_slot_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *identifier)
BMESH OPSTACK GET SLOT.
#define BMO_FLAG_DEFAULTS
void BMO_op_init(BMesh *bm, BMOperator *op, const int flag, const char *opname)
BMESH OPSTACK INIT OP.
void EDBM_automerge_and_split(Object *obedit, const bool UNUSED(split_edges), const bool split_faces, const bool update, const char hflag, const float dist)
void EDBM_automerge(Object *obedit, bool update, const char hflag, const float dist)
static void update(bNodeTree *ntree)
struct BMesh * bm
Definition: BKE_editmesh.h:52
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
int totvert
Definition: bmesh_class.h:297
void * data
double PIL_check_seconds_timer(void)
Definition: time.c:80