Blender  V3.3
bmesh_triangulate.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #include "DNA_modifier_types.h" /* for MOD_TRIANGULATE_NGON_BEAUTY only */
10 
11 #include "MEM_guardedalloc.h"
12 
13 #include "BLI_alloca.h"
14 #include "BLI_heap.h"
15 #include "BLI_linklist.h"
16 #include "BLI_memarena.h"
17 #include "BLI_utildefines.h"
18 
19 /* only for defines */
20 #include "BLI_polyfill_2d.h"
22 
23 #include "bmesh.h"
24 
25 #include "bmesh_triangulate.h" /* own include */
26 
31  BMFace *face,
32  const int quad_method,
33  const int ngon_method,
34  const bool use_tag,
35  BMOperator *op,
36  BMOpSlot *slot_facemap_out,
37  BMOpSlot *slot_facemap_double_out,
38 
39  MemArena *pf_arena,
40  /* use for MOD_TRIANGULATE_NGON_BEAUTY only! */
41  struct Heap *pf_heap)
42 {
43  int faces_array_tot = face->len - 3;
44  BMFace **faces_array = BLI_array_alloca(faces_array, faces_array_tot);
45  LinkNode *faces_double = NULL;
46  BLI_assert(face->len > 3);
47 
49  face,
50  faces_array,
51  &faces_array_tot,
52  NULL,
53  NULL,
54  &faces_double,
55  quad_method,
56  ngon_method,
57  use_tag,
58  pf_arena,
59  pf_heap);
60 
61  if (faces_array_tot) {
62  int i;
63  BMO_slot_map_elem_insert(op, slot_facemap_out, face, face);
64  for (i = 0; i < faces_array_tot; i++) {
65  BMO_slot_map_elem_insert(op, slot_facemap_out, faces_array[i], face);
66  }
67 
68  while (faces_double) {
69  LinkNode *next = faces_double->next;
70  BMO_slot_map_elem_insert(op, slot_facemap_double_out, faces_double->link, face);
71  MEM_freeN(faces_double);
72  faces_double = next;
73  }
74  }
75 }
76 
78  const int quad_method,
79  const int ngon_method,
80  const int min_vertices,
81  const bool tag_only,
82  BMOperator *op,
83  BMOpSlot *slot_facemap_out,
84  BMOpSlot *slot_facemap_double_out)
85 {
86  BMIter iter;
87  BMFace *face;
88  MemArena *pf_arena;
89  Heap *pf_heap;
90 
91  pf_arena = BLI_memarena_new(BLI_POLYFILL_ARENA_SIZE, __func__);
92 
93  if (ngon_method == MOD_TRIANGULATE_NGON_BEAUTY) {
95  }
96  else {
97  pf_heap = NULL;
98  }
99 
100  if (slot_facemap_out) {
101  /* same as below but call: bm_face_triangulate_mapping() */
102  BM_ITER_MESH (face, &iter, bm, BM_FACES_OF_MESH) {
103  if (face->len >= min_vertices) {
104  if (tag_only == false || BM_elem_flag_test(face, BM_ELEM_TAG)) {
106  face,
107  quad_method,
108  ngon_method,
109  tag_only,
110  op,
111  slot_facemap_out,
112  slot_facemap_double_out,
113  pf_arena,
114  pf_heap);
115  }
116  }
117  }
118  }
119  else {
120  LinkNode *faces_double = NULL;
121 
122  BM_ITER_MESH (face, &iter, bm, BM_FACES_OF_MESH) {
123  if (face->len >= min_vertices) {
124  if (tag_only == false || BM_elem_flag_test(face, BM_ELEM_TAG)) {
126  face,
127  NULL,
128  NULL,
129  NULL,
130  NULL,
131  &faces_double,
132  quad_method,
133  ngon_method,
134  tag_only,
135  pf_arena,
136  pf_heap);
137  }
138  }
139  }
140 
141  while (faces_double) {
142  LinkNode *next = faces_double->next;
143  BM_face_kill(bm, faces_double->link);
144  MEM_freeN(faces_double);
145  faces_double = next;
146  }
147  }
148 
149  BLI_memarena_free(pf_arena);
150 
151  if (ngon_method == MOD_TRIANGULATE_NGON_BEAUTY) {
152  BLI_heap_free(pf_heap, NULL);
153  }
154 }
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:22
#define BLI_assert(a)
Definition: BLI_assert.h:46
A min-heap / priority queue ADT.
void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp) ATTR_NONNULL(1)
Definition: BLI_heap.c:202
Heap * BLI_heap_new_ex(unsigned int reserve_num) ATTR_WARN_UNUSED_RESULT
Definition: BLI_heap.c:182
void BLI_memarena_free(struct MemArena *ma) ATTR_NONNULL(1)
Definition: BLI_memarena.c:94
struct MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
Definition: BLI_memarena.c:64
#define BLI_POLYFILL_ARENA_SIZE
#define BLI_POLYFILL_ALLOC_NGON_RESERVE
@ MOD_TRIANGULATE_NGON_BEAUTY
Read Guarded memory(de)allocation.
@ BM_ELEM_TAG
Definition: bmesh_class.h:484
void BM_face_kill(BMesh *bm, BMFace *f)
Definition: bmesh_core.c:828
#define BM_elem_flag_test(ele, hflag)
Definition: bmesh_inline.h:12
#define BM_ITER_MESH(ele, iter, bm, itype)
@ BM_FACES_OF_MESH
ATTR_WARN_UNUSED_RESULT BMesh * bm
BLI_INLINE void BMO_slot_map_elem_insert(BMOperator *op, BMOpSlot *slot, const void *element, void *val)
void BM_face_triangulate(BMesh *bm, BMFace *f, BMFace **r_faces_new, int *r_faces_new_tot, BMEdge **r_edges_new, int *r_edges_new_tot, LinkNode **r_faces_double, const int quad_method, const int ngon_method, const bool use_tag, MemArena *pf_arena, struct Heap *pf_heap)
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)
static void bm_face_triangulate_mapping(BMesh *bm, BMFace *face, const int quad_method, const int ngon_method, const bool use_tag, BMOperator *op, BMOpSlot *slot_facemap_out, BMOpSlot *slot_facemap_double_out, MemArena *pf_arena, struct Heap *pf_heap)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
static ulong * next
int len
Definition: bmesh_class.h:267
Definition: BLI_heap.c:43
void * link
Definition: BLI_linklist.h:24
struct LinkNode * next
Definition: BLI_linklist.h:23