Blender V4.3
polyfill_2d_beautify.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
26#include "BLI_utildefines.h"
27
28#include "BLI_heap.h"
29#include "BLI_math_geom.h"
30#include "BLI_memarena.h"
31
32#include "BLI_polyfill_2d_beautify.h" /* own include */
33
34#include "BLI_strict_flags.h" /* Keep last. */
35
36/* Used to find matching edges. */
41
42/* Half edge used for rotating in-place. */
49
50static int oedge_cmp(const void *a1, const void *a2)
51{
52 const struct OrderEdge *x1 = a1, *x2 = a2;
53 if (x1->verts[0] > x2->verts[0]) {
54 return 1;
55 }
56 if (x1->verts[0] < x2->verts[0]) {
57 return -1;
58 }
59
60 if (x1->verts[1] > x2->verts[1]) {
61 return 1;
62 }
63 if (x1->verts[1] < x2->verts[1]) {
64 return -1;
65 }
66
67 /* Only for predictability. */
68 if (x1->e_half > x2->e_half) {
69 return 1;
70 }
71 if (x1->e_half < x2->e_half) {
72 return -1;
73 }
74 /* Should never get here, no two edges should be the same. */
75 BLI_assert(false);
76 return 0;
77}
78
79BLI_INLINE bool is_boundary_edge(uint i_a, uint i_b, const uint coord_last)
80{
81 BLI_assert(i_a < i_b);
82 return ((i_a + 1 == i_b) || UNLIKELY((i_a == 0) && (i_b == coord_last)));
83}
85 const float v2[2],
86 const float v3[2],
87 const float v4[2],
88 const bool lock_degenerate,
89 float *r_area)
90{
91 /* not a loop (only to be able to break out) */
92 do {
93 /* Allow very small faces to be considered non-zero. */
94 const float eps_zero_area = 1e-12f;
95 const float area_2x_234 = cross_tri_v2(v2, v3, v4);
96 const float area_2x_241 = cross_tri_v2(v2, v4, v1);
97
98 const float area_2x_123 = cross_tri_v2(v1, v2, v3);
99 const float area_2x_134 = cross_tri_v2(v1, v3, v4);
100
101 BLI_assert((ELEM(v1, v2, v3, v4) == false) && (ELEM(v2, v1, v3, v4) == false) &&
102 (ELEM(v3, v1, v2, v4) == false) && (ELEM(v4, v1, v2, v3) == false));
103
104 if (r_area) {
105 *r_area = (fabsf(area_2x_234) + fabsf(area_2x_241) +
106 /* Include both pairs for predictable results. */
107 fabsf(area_2x_123) + fabsf(area_2x_134)) /
108 8.0f;
109 }
110
111 /*
112 * Test for unusable (1-3) state.
113 * - Area sign flipping to check faces aren't going to point in opposite directions.
114 * - Area epsilon check that the one of the faces won't be zero area.
115 */
116 if ((area_2x_123 >= 0.0f) != (area_2x_134 >= 0.0f)) {
117 break;
118 }
119 if ((fabsf(area_2x_123) <= eps_zero_area) || (fabsf(area_2x_134) <= eps_zero_area)) {
120 break;
121 }
122
123 /* Test for unusable (2-4) state (same as above). */
124 if ((area_2x_234 >= 0.0f) != (area_2x_241 >= 0.0f)) {
125 if (lock_degenerate) {
126 break;
127 }
128
129 return -FLT_MAX; /* always rotate */
130 }
131 if ((fabsf(area_2x_234) <= eps_zero_area) || (fabsf(area_2x_241) <= eps_zero_area)) {
132 return -FLT_MAX; /* always rotate */
133 }
134
135 {
136 /* testing rule: the area divided by the perimeter,
137 * check if (1-3) beats the existing (2-4) edge rotation */
138 float area_a, area_b;
139 float prim_a, prim_b;
140 float fac_24, fac_13;
141
142 float len_12, len_23, len_34, len_41, len_24, len_13;
143
144 /* edges around the quad */
145 len_12 = len_v2v2(v1, v2);
146 len_23 = len_v2v2(v2, v3);
147 len_34 = len_v2v2(v3, v4);
148 len_41 = len_v2v2(v4, v1);
149 /* edges crossing the quad interior */
150 len_13 = len_v2v2(v1, v3);
151 len_24 = len_v2v2(v2, v4);
152
153 /* NOTE: area is in fact (area * 2),
154 * but in this case its OK, since we're comparing ratios */
155
156 /* edge (2-4), current state */
157 area_a = fabsf(area_2x_234);
158 area_b = fabsf(area_2x_241);
159 prim_a = len_23 + len_34 + len_24;
160 prim_b = len_41 + len_12 + len_24;
161 fac_24 = (area_a / prim_a) + (area_b / prim_b);
162
163 /* edge (1-3), new state */
164 area_a = fabsf(area_2x_123);
165 area_b = fabsf(area_2x_134);
166 prim_a = len_12 + len_23 + len_13;
167 prim_b = len_34 + len_41 + len_13;
168 fac_13 = (area_a / prim_a) + (area_b / prim_b);
169
170 /* negative number if (1-3) is an improved state */
171 return fac_24 - fac_13;
172 }
173 } while (false);
174
175 return FLT_MAX;
176}
177
178static float polyedge_rotate_beauty_calc(const float (*coords)[2],
179 const struct HalfEdge *edges,
180 const struct HalfEdge *e_a,
181 float *r_area)
182{
183 const struct HalfEdge *e_b = &edges[e_a->e_radial];
184
185 const struct HalfEdge *e_a_other = &edges[edges[e_a->e_next].e_next];
186 const struct HalfEdge *e_b_other = &edges[edges[e_b->e_next].e_next];
187
188 const float *v1, *v2, *v3, *v4;
189
190 v1 = coords[e_a_other->v];
191 v2 = coords[e_a->v];
192 v3 = coords[e_b_other->v];
193 v4 = coords[e_b->v];
194
195 return BLI_polyfill_beautify_quad_rotate_calc_ex(v1, v2, v3, v4, false, r_area);
196}
197
198static void polyedge_beauty_cost_update_single(const float (*coords)[2],
199 const struct HalfEdge *edges,
200 struct HalfEdge *e,
201 Heap *eheap,
202 HeapNode **eheap_table)
203{
204 const uint i = e->base_index;
205 /* recalculate edge */
206 float area;
207 const float cost = polyedge_rotate_beauty_calc(coords, edges, e, &area);
208 /* We can get cases where both choices generate very small negative costs,
209 * which leads to infinite loop. Anyway, costs above that are not worth recomputing,
210 * maybe we could even optimize it to a smaller limit?
211 * Actually, FLT_EPSILON is too small in some cases, 1e-6f seems to work OK hopefully?
212 * See #43578, #49478.
213 *
214 * In fact a larger epsilon can still fail when the area of the face is very large,
215 * now the epsilon is scaled by the face area.
216 * See #56532. */
217 if (cost < -1e-6f * max_ff(area, 1.0f)) {
218 BLI_heap_insert_or_update(eheap, &eheap_table[i], cost, e);
219 }
220 else {
221 if (eheap_table[i]) {
222 BLI_heap_remove(eheap, eheap_table[i]);
223 eheap_table[i] = NULL;
224 }
225 }
226}
227
228static void polyedge_beauty_cost_update(const float (*coords)[2],
229 struct HalfEdge *edges,
230 struct HalfEdge *e,
231 Heap *eheap,
232 HeapNode **eheap_table)
233{
234 struct HalfEdge *e_arr[4];
235 e_arr[0] = &edges[e->e_next];
236 e_arr[1] = &edges[e_arr[0]->e_next];
237
238 e = &edges[e->e_radial];
239 e_arr[2] = &edges[e->e_next];
240 e_arr[3] = &edges[e_arr[2]->e_next];
241
242 for (uint i = 0; i < 4; i++) {
243 if (e_arr[i] && e_arr[i]->base_index != UINT_MAX) {
244 polyedge_beauty_cost_update_single(coords, edges, e_arr[i], eheap, eheap_table);
245 }
246 }
247}
248
249static void polyedge_rotate(struct HalfEdge *edges, const struct HalfEdge *e)
250{
266 struct HalfEdge *ed[6];
267 uint ed_index[6];
268
269 ed_index[0] = (uint)(e - edges);
270 ed[0] = &edges[ed_index[0]];
271 ed_index[1] = ed[0]->e_next;
272 ed[1] = &edges[ed_index[1]];
273 ed_index[2] = ed[1]->e_next;
274 ed[2] = &edges[ed_index[2]];
275
276 ed_index[3] = e->e_radial;
277 ed[3] = &edges[ed_index[3]];
278 ed_index[4] = ed[3]->e_next;
279 ed[4] = &edges[ed_index[4]];
280 ed_index[5] = ed[4]->e_next;
281 ed[5] = &edges[ed_index[5]];
282
283 ed[0]->e_next = ed_index[2];
284 ed[1]->e_next = ed_index[3];
285 ed[2]->e_next = ed_index[4];
286 ed[3]->e_next = ed_index[5];
287 ed[4]->e_next = ed_index[0];
288 ed[5]->e_next = ed_index[1];
289
290 ed[0]->v = ed[5]->v;
291 ed[3]->v = ed[2]->v;
292}
293
294void BLI_polyfill_beautify(const float (*coords)[2],
295 const uint coords_num,
296 uint (*tris)[3],
297
298 /* structs for reuse */
299 MemArena *arena,
300 Heap *eheap)
301{
302 const uint coord_last = coords_num - 1;
303 const uint tris_len = coords_num - 2;
304 /* internal edges only (between 2 tris) */
305 const uint edges_len = tris_len - 1;
306
307 HeapNode **eheap_table;
308
309 const uint half_edges_len = 3 * tris_len;
310 struct HalfEdge *half_edges = BLI_memarena_alloc(arena, sizeof(*half_edges) * half_edges_len);
311 struct OrderEdge *order_edges = BLI_memarena_alloc(arena,
312 sizeof(struct OrderEdge) * 2 * edges_len);
313 uint order_edges_len = 0;
314
315 /* first build edges */
316 for (uint i = 0; i < tris_len; i++) {
317 for (uint j_curr = 0, j_prev = 2; j_curr < 3; j_prev = j_curr++) {
318 const uint e_index_prev = (i * 3) + j_prev;
319 const uint e_index_curr = (i * 3) + j_curr;
320
321 half_edges[e_index_prev].v = tris[i][j_prev];
322 half_edges[e_index_prev].e_next = e_index_curr;
323 half_edges[e_index_prev].e_radial = UINT_MAX;
324 half_edges[e_index_prev].base_index = UINT_MAX;
325
326 uint e_pair[2] = {tris[i][j_prev], tris[i][j_curr]};
327 if (e_pair[0] > e_pair[1]) {
328 SWAP(uint, e_pair[0], e_pair[1]);
329 }
330
331 /* ensure internal edges. */
332 if (!is_boundary_edge(e_pair[0], e_pair[1], coord_last)) {
333 order_edges[order_edges_len].verts[0] = e_pair[0];
334 order_edges[order_edges_len].verts[1] = e_pair[1];
335 order_edges[order_edges_len].e_half = e_index_prev;
336 order_edges_len += 1;
337 }
338 }
339 }
340 BLI_assert(edges_len * 2 == order_edges_len);
341
342 qsort(order_edges, order_edges_len, sizeof(struct OrderEdge), oedge_cmp);
343
344 for (uint i = 0, base_index = 0; i < order_edges_len; base_index++) {
345 const struct OrderEdge *oe_a = &order_edges[i++];
346 const struct OrderEdge *oe_b = &order_edges[i++];
347 BLI_assert(oe_a->verts[0] == oe_b->verts[0] && oe_a->verts[1] == oe_b->verts[1]);
348 half_edges[oe_a->e_half].e_radial = oe_b->e_half;
349 half_edges[oe_b->e_half].e_radial = oe_a->e_half;
350 half_edges[oe_a->e_half].base_index = base_index;
351 half_edges[oe_b->e_half].base_index = base_index;
352 }
353 /* order_edges could be freed now. */
354
355 /* Now perform iterative rotations. */
356#if 0
357 eheap_table = BLI_memarena_alloc(arena, sizeof(HeapNode *) * (size_t)edges_len);
358#else
359 /* We can re-use this since its big enough. */
360 eheap_table = (void *)order_edges;
361 order_edges = NULL;
362#endif
363
364 /* Build heap. */
365 {
366 struct HalfEdge *e = half_edges;
367 for (uint i = 0; i < half_edges_len; i++, e++) {
368 /* Accounts for boundary edged too (UINT_MAX). */
369 if (e->e_radial < i) {
370 const float cost = polyedge_rotate_beauty_calc(coords, half_edges, e, NULL);
371 if (cost < 0.0f) {
372 eheap_table[e->base_index] = BLI_heap_insert(eheap, cost, e);
373 }
374 else {
375 eheap_table[e->base_index] = NULL;
376 }
377 }
378 }
379 }
380
381 while (BLI_heap_is_empty(eheap) == false) {
382 struct HalfEdge *e = BLI_heap_pop_min(eheap);
383 eheap_table[e->base_index] = NULL;
384
385 polyedge_rotate(half_edges, e);
386
387 /* recalculate faces connected on the heap */
388 polyedge_beauty_cost_update(coords, half_edges, e, eheap, eheap_table);
389 }
390
391 BLI_heap_clear(eheap, NULL);
392
393 // MEM_freeN(eheap_table); /* arena */
394
395 /* Get triangles from half edge. */
396 uint tri_index = 0;
397 for (uint i = 0; i < half_edges_len; i++) {
398 struct HalfEdge *e = &half_edges[i];
399 if (e->v != UINT_MAX) {
400 uint *tri = tris[tri_index++];
401
402 tri[0] = e->v;
403 e->v = UINT_MAX;
404
405 e = &half_edges[e->e_next];
406 tri[1] = e->v;
407 e->v = UINT_MAX;
408
409 e = &half_edges[e->e_next];
410 tri[2] = e->v;
411 e->v = UINT_MAX;
412 }
413 }
414}
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_INLINE
A min-heap / priority queue ADT.
void BLI_heap_insert_or_update(Heap *heap, HeapNode **node_p, float value, void *ptr) ATTR_NONNULL(1
void void bool BLI_heap_is_empty(const Heap *heap) ATTR_NONNULL(1)
Definition BLI_heap.c:269
void BLI_heap_clear(Heap *heap, HeapFreeFP ptrfreefp) ATTR_NONNULL(1)
Definition BLI_heap.c:214
void * BLI_heap_pop_min(Heap *heap) ATTR_NONNULL(1)
Definition BLI_heap.c:291
HeapNode * BLI_heap_insert(Heap *heap, float value, void *ptr) ATTR_NONNULL(1)
Definition BLI_heap.c:235
void void BLI_heap_remove(Heap *heap, HeapNode *node) ATTR_NONNULL(1
MINLINE float max_ff(float a, float b)
MINLINE float cross_tri_v2(const float v1[2], const float v2[2], const float v3[2])
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
void * BLI_memarena_alloc(struct MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
unsigned int uint
#define SWAP(type, a, b)
#define UNLIKELY(x)
#define ELEM(...)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
#define NULL
#define fabsf(x)
#define UINT_MAX
Definition hash_md5.cc:44
float BLI_polyfill_beautify_quad_rotate_calc_ex(const float v1[2], const float v2[2], const float v3[2], const float v4[2], const bool lock_degenerate, float *r_area)
static float polyedge_rotate_beauty_calc(const float(*coords)[2], const struct HalfEdge *edges, const struct HalfEdge *e_a, float *r_area)
static void polyedge_rotate(struct HalfEdge *edges, const struct HalfEdge *e)
static void polyedge_beauty_cost_update_single(const float(*coords)[2], const struct HalfEdge *edges, struct HalfEdge *e, Heap *eheap, HeapNode **eheap_table)
static void polyedge_beauty_cost_update(const float(*coords)[2], struct HalfEdge *edges, struct HalfEdge *e, Heap *eheap, HeapNode **eheap_table)
static int oedge_cmp(const void *a1, const void *a2)
void BLI_polyfill_beautify(const float(*coords)[2], const uint coords_num, uint(*tris)[3], MemArena *arena, Heap *eheap)
BLI_INLINE bool is_boundary_edge(uint i_a, uint i_b, const uint coord_last)
#define FLT_MAX
Definition stdcycles.h:14