Blender  V2.93
bmo_triangulate.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 
23 #include "MEM_guardedalloc.h"
24 
25 #include "DNA_listBase.h"
26 
27 #include "BLI_math.h"
28 #include "BLI_scanfill.h"
29 #include "BLI_sort_utils.h"
30 
31 #include "bmesh.h"
32 #include "bmesh_tools.h"
34 
35 #define ELE_NEW 1
36 #define EDGE_MARK 4
37 
39 {
40  const int quad_method = BMO_slot_int_get(op->slots_in, "quad_method");
41  const int ngon_method = BMO_slot_int_get(op->slots_in, "ngon_method");
42 
43  BMOpSlot *slot_facemap_out = BMO_slot_get(op->slots_out, "face_map.out");
44  BMOpSlot *slot_facemap_double_out = BMO_slot_get(op->slots_out, "face_map_double.out");
45 
48 
50  bm, quad_method, ngon_method, 4, true, op, slot_facemap_out, slot_facemap_double_out);
51 
54 }
55 
56 struct SortNormal {
57  float value; /* keep first */
58  float no[3];
59 };
60 
62 {
63  const bool use_beauty = BMO_slot_bool_get(op->slots_in, "use_beauty");
64  const bool use_dissolve = BMO_slot_bool_get(op->slots_in, "use_dissolve");
65  BMOIter siter;
66  BMEdge *e;
67  ScanFillContext sf_ctx;
68  /* ScanFillEdge *sf_edge; */ /* UNUSED */
69  ScanFillFace *sf_tri;
70  GHash *sf_vert_map;
71  float normal[3];
72  const int scanfill_flag = BLI_SCANFILL_CALC_HOLES | BLI_SCANFILL_CALC_POLYS |
74  uint nors_tot;
75  bool calc_winding = false;
76 
77  sf_vert_map = BLI_ghash_ptr_new_ex(__func__, BMO_slot_buffer_count(op->slots_in, "edges"));
78 
79  BMO_slot_vec_get(op->slots_in, "normal", normal);
80 
81  BLI_scanfill_begin(&sf_ctx);
82 
83  BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
84  ScanFillVert *sf_verts[2];
85  BMVert **e_verts = &e->v1;
86  uint i;
87 
89 
90  calc_winding = (calc_winding || BM_edge_is_boundary(e));
91 
92  for (i = 0; i < 2; i++) {
93  if ((sf_verts[i] = BLI_ghash_lookup(sf_vert_map, e_verts[i])) == NULL) {
94  sf_verts[i] = BLI_scanfill_vert_add(&sf_ctx, e_verts[i]->co);
95  sf_verts[i]->tmp.p = e_verts[i];
96  BLI_ghash_insert(sf_vert_map, e_verts[i], sf_verts[i]);
97  }
98  }
99 
100  /* sf_edge = */ BLI_scanfill_edge_add(&sf_ctx, UNPACK2(sf_verts));
101  /* sf_edge->tmp.p = e; */ /* UNUSED */
102  }
103  nors_tot = BLI_ghash_len(sf_vert_map);
104  BLI_ghash_free(sf_vert_map, NULL, NULL);
105 
106  if (is_zero_v3(normal)) {
107  /* calculate the normal from the cross product of vert-edge pairs.
108  * Since we don't know winding, just accumulate */
109  ScanFillVert *sf_vert;
110  struct SortNormal *nors;
111  uint i;
112  bool is_degenerate = true;
113 
114  nors = MEM_mallocN(sizeof(*nors) * nors_tot, __func__);
115 
116  for (sf_vert = sf_ctx.fillvertbase.first, i = 0; sf_vert; sf_vert = sf_vert->next, i++) {
117  BMVert *v = sf_vert->tmp.p;
118  BMIter eiter;
119  BMEdge *e_pair[2];
120  uint e_index = 0;
121 
122  nors[i].value = -1.0f;
123 
124  /* only use if 'is_degenerate' stays true */
125  add_v3_v3(normal, v->no);
126 
127  BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
128  if (BMO_edge_flag_test(bm, e, EDGE_MARK)) {
129  if (e_index == 2) {
130  e_index = 0;
131  break;
132  }
133  e_pair[e_index++] = e;
134  }
135  }
136 
137  if (e_index == 2) {
138  float dir_a[3], dir_b[3];
139 
140  is_degenerate = false;
141 
142  sub_v3_v3v3(dir_a, v->co, BM_edge_other_vert(e_pair[0], v)->co);
143  sub_v3_v3v3(dir_b, v->co, BM_edge_other_vert(e_pair[1], v)->co);
144 
145  cross_v3_v3v3(nors[i].no, dir_a, dir_b);
146  nors[i].value = len_squared_v3(nors[i].no);
147 
148  /* only to get deterministic behavior (for initial normal) */
149  if (len_squared_v3(dir_a) > len_squared_v3(dir_b)) {
150  negate_v3(nors[i].no);
151  }
152  }
153  }
154 
155  if (UNLIKELY(is_degenerate)) {
156  /* no vertices have 2 edges?
157  * in this case fall back to the average vertex normals */
158  }
159  else {
160  qsort(nors, nors_tot, sizeof(*nors), BLI_sortutil_cmp_float_reverse);
161 
162  copy_v3_v3(normal, nors[0].no);
163  for (i = 0; i < nors_tot; i++) {
164  if (UNLIKELY(nors[i].value == -1.0f)) {
165  break;
166  }
167  if (dot_v3v3(normal, nors[i].no) < 0.0f) {
168  negate_v3(nors[i].no);
169  }
170  add_v3_v3(normal, nors[i].no);
171  }
173  }
174 
175  MEM_freeN(nors);
176  }
177  else {
178  calc_winding = false;
179  }
180 
181  /* in this case we almost certainly have degenerate geometry,
182  * better set a fallback value as a last resort */
183  if (UNLIKELY(normalize_v3(normal) == 0.0f)) {
184  normal[2] = 1.0f;
185  }
186 
187  BLI_scanfill_calc_ex(&sf_ctx, scanfill_flag, normal);
188 
189  /* if we have existing faces, base winding on those */
190  if (calc_winding) {
191  int winding_votes = 0;
192  for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
193  BMVert *v_tri[3] = {sf_tri->v1->tmp.p, sf_tri->v2->tmp.p, sf_tri->v3->tmp.p};
194  uint i, i_prev;
195 
196  for (i = 0, i_prev = 2; i < 3; i_prev = i++) {
197  e = BM_edge_exists(v_tri[i], v_tri[i_prev]);
199  winding_votes += (e->l->v == v_tri[i]) ? 1 : -1;
200  }
201  }
202  }
203 
204  if (winding_votes < 0) {
205  for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
206  SWAP(struct ScanFillVert *, sf_tri->v2, sf_tri->v3);
207  }
208  }
209  }
210 
211  for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
212  BMFace *f;
213  BMLoop *l;
214  BMIter liter;
215 
217  sf_tri->v1->tmp.p,
218  sf_tri->v2->tmp.p,
219  sf_tri->v3->tmp.p,
220  NULL,
221  NULL,
223 
225  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
226  if (!BMO_edge_flag_test(bm, l->e, EDGE_MARK)) {
228  }
229  }
230  }
231 
232  BLI_scanfill_end(&sf_ctx);
233 
234  if (use_beauty) {
235  BMOperator bmop;
236 
237  BMO_op_initf(bm, &bmop, op->flag, "beautify_fill faces=%ff edges=%Fe", ELE_NEW, EDGE_MARK);
238  BMO_op_exec(bm, &bmop);
240  BMO_op_finish(bm, &bmop);
241  }
242 
243  if (use_dissolve) {
244  BMEdge *e_next;
245  BMIter iter;
246 
247  BM_ITER_MESH_MUTABLE (e, e_next, &iter, bm, BM_EDGES_OF_MESH) {
248  if (BMO_edge_flag_test(bm, e, ELE_NEW)) {
249  /* in rare cases the edges face will have already been removed from the edge */
250  if (LIKELY(BM_edge_is_manifold(e))) {
251  BMFace *f_new = BM_faces_join_pair(bm, e->l, e->l->radial_next, false);
252  if (f_new) {
254  BM_edge_kill(bm, e);
255  }
256  else {
258  }
259  }
260  else if (e->l == NULL) {
261  BM_edge_kill(bm, e);
262  }
263  else {
264  /* Edges with 1 or 3+ faces attached,
265  * most likely caused by a degenerate mesh. */
266  }
267  }
268  }
269  }
270 
272 }
unsigned int BLI_ghash_len(GHash *gh) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:744
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
GHash * BLI_ghash_ptr_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v3(float r[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE bool is_zero_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void negate_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
void BLI_scanfill_begin(ScanFillContext *sf_ctx)
Definition: scanfill.c:794
struct ScanFillVert * BLI_scanfill_vert_add(ScanFillContext *sf_ctx, const float vec[3])
Definition: scanfill.c:129
struct ScanFillEdge * BLI_scanfill_edge_add(ScanFillContext *sf_ctx, struct ScanFillVert *v1, struct ScanFillVert *v2)
Definition: scanfill.c:151
@ BLI_SCANFILL_CALC_LOOSE
Definition: BLI_scanfill.h:113
@ BLI_SCANFILL_CALC_POLYS
Definition: BLI_scanfill.h:106
@ BLI_SCANFILL_CALC_HOLES
Definition: BLI_scanfill.h:110
void BLI_scanfill_end(ScanFillContext *sf_ctx)
Definition: scanfill.c:808
unsigned int BLI_scanfill_calc_ex(ScanFillContext *sf_ctx, const int flag, const float nor_proj[3])
Definition: scanfill.c:828
int BLI_sortutil_cmp_float_reverse(const void *a_, const void *b_)
Definition: sort_utils.c:54
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNPACK2(a)
#define SWAP(type, a, b)
#define UNLIKELY(x)
#define LIKELY(x)
These structs are the foundation for all linked lists in the library system.
Read Guarded memory(de)allocation.
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_EDGE
Definition: bmesh_class.h:384
@ BM_ELEM_TAG
Definition: bmesh_class.h:484
BMFace * BM_face_create_quad_tri(BMesh *bm, BMVert *v1, BMVert *v2, BMVert *v3, BMVert *v4, const BMFace *f_example, const eBMCreateFlag create_flag)
Make Quad/Triangle.
void BM_edge_kill(BMesh *bm, BMEdge *e)
Definition: bmesh_core.c:987
@ BM_CREATE_NO_DOUBLE
Definition: bmesh_core.h:29
void BMO_error_clear(BMesh *bm)
#define BM_ITER_ELEM(ele, iter, data, itype)
@ BM_EDGES_OF_MESH
@ BM_EDGES_OF_VERT
@ BM_LOOPS_OF_FACE
#define BM_ITER_MESH_MUTABLE(ele, ele_next, iter, bm, itype)
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
BMFace * BM_faces_join_pair(BMesh *bm, BMLoop *l_a, BMLoop *l_b, const bool do_del)
Faces Join Pair.
Definition: bmesh_mods.c:221
void BMO_slot_buffer_hflag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char htype, const char hflag, const bool do_flush)
BMO_FLAG_BUFFER.
void BMO_slot_buffer_flag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char htype, const short oflag)
BMO_FLAG_BUFFER.
void BMO_slot_buffer_from_enabled_hflag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char htype, const char hflag)
#define BMO_edge_flag_test(bm, e, oflag)
void BMO_slot_vec_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, float r_vec[3])
#define BMO_edge_flag_enable(bm, e, oflag)
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_face_flag_enable(bm, e, oflag)
#define BMO_ITER(ele, iter, slot_args, slot_name, restrict_flag)
int BMO_slot_buffer_count(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
int BMO_slot_int_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
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.
void BMO_slot_buffer_from_enabled_flag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char htype, const short oflag)
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
Definition: bmesh_query.c:1995
BLI_INLINE bool BM_edge_is_manifold(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE bool BM_edge_is_boundary(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
void BM_mesh_triangulate(BMesh *bm, const int quad_method, const int ngon_method, const int min_vertices, const bool tag_only, BMOperator *op, BMOpSlot *slot_facemap_out, BMOpSlot *slot_facemap_double_out)
#define ELE_NEW
void bmo_triangle_fill_exec(BMesh *bm, BMOperator *op)
#define EDGE_MARK
void bmo_triangulate_exec(BMesh *bm, BMOperator *op)
IconTextureDrawCall normal
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
struct BMEdge * e
Definition: bmesh_class.h:176
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
float co[3]
Definition: bmesh_class.h:99
float no[3]
Definition: bmesh_class.h:100
void * first
Definition: DNA_listBase.h:47
ListBase fillvertbase
Definition: BLI_scanfill.h:33
ListBase fillfacebase
Definition: BLI_scanfill.h:35
struct ScanFillFace * next
Definition: BLI_scanfill.h:89
struct ScanFillVert * v2
Definition: BLI_scanfill.h:90
struct ScanFillVert * v3
Definition: BLI_scanfill.h:90
struct ScanFillVert * v1
Definition: BLI_scanfill.h:90
struct ScanFillVert * next
Definition: BLI_scanfill.h:55
union ScanFillVert::@119 tmp
float no[3]