2013-03-25 22:04:12 +00:00
|
|
|
/*
|
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): Joseph Eagar.
|
|
|
|
|
*
|
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** \file blender/bmesh/operators/bmo_beautify.c
|
|
|
|
|
* \ingroup bmesh
|
2013-03-30 08:54:50 +00:00
|
|
|
*
|
2013-06-25 22:58:23 +00:00
|
|
|
* Beautify the mesh by rotating edges between triangles
|
2013-03-30 08:54:50 +00:00
|
|
|
* to more attractive positions until no more rotations can be made.
|
|
|
|
|
*
|
2013-06-25 22:58:23 +00:00
|
|
|
* In principle this is very simple however there is the possibility of
|
2013-03-30 08:54:50 +00:00
|
|
|
* going into an eternal loop where edges keep rotating.
|
2013-08-25 20:03:45 +00:00
|
|
|
* To avoid this - each edge stores a set of it previous
|
2013-03-30 08:54:50 +00:00
|
|
|
* states so as not to rotate back.
|
|
|
|
|
*
|
|
|
|
|
* TODO
|
|
|
|
|
* - Take face normals into account.
|
2013-03-25 22:04:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "BLI_math.h"
|
2013-03-30 11:05:57 +00:00
|
|
|
#include "BLI_heap.h"
|
2013-03-25 22:04:12 +00:00
|
|
|
|
2013-03-25 23:21:16 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2013-03-25 22:04:12 +00:00
|
|
|
#include "bmesh.h"
|
2013-03-25 22:40:11 +00:00
|
|
|
#include "intern/bmesh_operators_private.h"
|
2013-03-25 22:04:12 +00:00
|
|
|
|
2013-03-25 23:21:16 +00:00
|
|
|
// #define DEBUG_TIME
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_TIME
|
|
|
|
|
# include "PIL_time.h"
|
2013-09-03 21:22:43 +00:00
|
|
|
# include "PIL_time_utildefines.h"
|
2013-03-25 23:21:16 +00:00
|
|
|
#endif
|
2013-03-25 22:04:12 +00:00
|
|
|
|
2013-05-08 12:56:41 +00:00
|
|
|
enum {
|
|
|
|
|
VERT_RESTRICT_TAG = (1 << 0),
|
|
|
|
|
};
|
|
|
|
|
|
2013-03-26 00:29:57 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
2013-08-25 20:03:45 +00:00
|
|
|
/* GSet for edge rotation */
|
2013-03-26 00:29:57 +00:00
|
|
|
|
|
|
|
|
typedef struct EdRotState {
|
|
|
|
|
int v1, v2; /* edge vert, small -> large */
|
|
|
|
|
int f1, f2; /* face vert, small -> large */
|
|
|
|
|
} EdRotState;
|
|
|
|
|
|
2013-08-25 20:03:45 +00:00
|
|
|
static unsigned int erot_gsetutil_hash(const void *ptr)
|
2013-03-26 00:29:57 +00:00
|
|
|
{
|
|
|
|
|
const EdRotState *e_state = (const EdRotState *)ptr;
|
|
|
|
|
unsigned int
|
|
|
|
|
hash = BLI_ghashutil_inthash(SET_INT_IN_POINTER(e_state->v1));
|
|
|
|
|
hash ^= BLI_ghashutil_inthash(SET_INT_IN_POINTER(e_state->v2));
|
|
|
|
|
hash ^= BLI_ghashutil_inthash(SET_INT_IN_POINTER(e_state->f1));
|
|
|
|
|
hash ^= BLI_ghashutil_inthash(SET_INT_IN_POINTER(e_state->f2));
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
2013-08-25 20:03:45 +00:00
|
|
|
static int erot_gsetutil_cmp(const void *a, const void *b)
|
2013-03-26 00:29:57 +00:00
|
|
|
{
|
|
|
|
|
const EdRotState *e_state_a = (const EdRotState *)a;
|
|
|
|
|
const EdRotState *e_state_b = (const EdRotState *)b;
|
|
|
|
|
if (e_state_a->v1 < e_state_b->v1) return -1;
|
|
|
|
|
else if (e_state_a->v1 > e_state_b->v1) return 1;
|
|
|
|
|
else if (e_state_a->v2 < e_state_b->v2) return -1;
|
|
|
|
|
else if (e_state_a->v2 > e_state_b->v2) return 1;
|
|
|
|
|
else if (e_state_a->f1 < e_state_b->f1) return -1;
|
|
|
|
|
else if (e_state_a->f1 > e_state_b->f1) return 1;
|
|
|
|
|
else if (e_state_a->f2 < e_state_b->f2) return -1;
|
|
|
|
|
else if (e_state_a->f2 > e_state_b->f2) return 1;
|
|
|
|
|
else return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-25 20:03:45 +00:00
|
|
|
static GSet *erot_gset_new(void)
|
2013-03-26 00:29:57 +00:00
|
|
|
{
|
2013-08-25 20:03:45 +00:00
|
|
|
return BLI_gset_new(erot_gsetutil_hash, erot_gsetutil_cmp, __func__);
|
2013-03-26 00:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ensure v0 is smaller */
|
|
|
|
|
#define EDGE_ORD(v0, v1) \
|
|
|
|
|
if (v0 > v1) { \
|
|
|
|
|
v0 ^= v1; \
|
|
|
|
|
v1 ^= v0; \
|
|
|
|
|
v0 ^= v1; \
|
|
|
|
|
} (void)0
|
|
|
|
|
|
|
|
|
|
static void erot_state_ex(const BMEdge *e, int v_index[2], int f_index[2])
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(BM_edge_is_manifold((BMEdge *)e));
|
2013-03-26 01:49:55 +00:00
|
|
|
BLI_assert(BM_vert_in_edge(e, e->l->prev->v) == false);
|
|
|
|
|
BLI_assert(BM_vert_in_edge(e, e->l->radial_next->prev->v) == false);
|
|
|
|
|
|
2013-03-26 00:29:57 +00:00
|
|
|
/* verts of the edge */
|
|
|
|
|
v_index[0] = BM_elem_index_get(e->v1);
|
|
|
|
|
v_index[1] = BM_elem_index_get(e->v2);
|
|
|
|
|
EDGE_ORD(v_index[0], v_index[1]);
|
|
|
|
|
|
|
|
|
|
/* verts of each of the 2 faces attached to this edge
|
|
|
|
|
* (that are not apart of this edge) */
|
|
|
|
|
f_index[0] = BM_elem_index_get(e->l->prev->v);
|
|
|
|
|
f_index[1] = BM_elem_index_get(e->l->radial_next->prev->v);
|
|
|
|
|
EDGE_ORD(f_index[0], f_index[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void erot_state_current(const BMEdge *e, EdRotState *e_state)
|
|
|
|
|
{
|
|
|
|
|
erot_state_ex(e, &e_state->v1, &e_state->f1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void erot_state_alternate(const BMEdge *e, EdRotState *e_state)
|
|
|
|
|
{
|
|
|
|
|
erot_state_ex(e, &e_state->f1, &e_state->v1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-30 09:57:35 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/* Calculate the improvement of rotating the edge */
|
|
|
|
|
|
|
|
|
|
/**
|
2013-03-30 11:05:57 +00:00
|
|
|
* \return a negative value means the edge can be rotated.
|
2013-03-30 09:57:35 +00:00
|
|
|
*/
|
2013-05-08 12:56:41 +00:00
|
|
|
static float bm_edge_calc_rotate_beauty(const BMEdge *e, const int flag)
|
2013-03-30 09:57:35 +00:00
|
|
|
{
|
|
|
|
|
/* not a loop (only to be able to break out) */
|
|
|
|
|
do {
|
|
|
|
|
float v1_xy[2], v2_xy[2], v3_xy[2], v4_xy[2];
|
|
|
|
|
|
|
|
|
|
/* first get the 2d values */
|
|
|
|
|
{
|
|
|
|
|
const float *v1, *v2, *v3, *v4;
|
|
|
|
|
bool is_zero_a, is_zero_b;
|
|
|
|
|
float no[3];
|
|
|
|
|
float axis_mat[3][3];
|
|
|
|
|
|
|
|
|
|
v1 = e->l->prev->v->co; /* first face co */
|
|
|
|
|
v2 = e->l->v->co; /* e->v1 or e->v2*/
|
|
|
|
|
v3 = e->l->radial_next->prev->v->co; /* second face co */
|
|
|
|
|
v4 = e->l->next->v->co; /* e->v1 or e->v2*/
|
|
|
|
|
|
2013-05-08 12:56:41 +00:00
|
|
|
if (flag & VERT_RESTRICT_TAG) {
|
|
|
|
|
BMVert *v_a = e->l->prev->v, *v_b = e->l->radial_next->prev->v;
|
|
|
|
|
if (BM_elem_flag_test(v_a, BM_ELEM_TAG) == BM_elem_flag_test(v_b, BM_ELEM_TAG)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-30 09:57:35 +00:00
|
|
|
if (UNLIKELY(v1 == v3)) {
|
|
|
|
|
// printf("This should never happen, but does sometimes!\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// printf("%p %p %p %p - %p %p\n", v1, v2, v3, v4, e->l->f, e->l->radial_next->f);
|
|
|
|
|
BLI_assert((ELEM3(v1, v2, v3, v4) == false) &&
|
2013-03-31 03:28:46 +00:00
|
|
|
(ELEM3(v2, v1, v3, v4) == false) &&
|
|
|
|
|
(ELEM3(v3, v1, v2, v4) == false) &&
|
|
|
|
|
(ELEM3(v4, v1, v2, v3) == false));
|
2013-03-30 09:57:35 +00:00
|
|
|
|
|
|
|
|
is_zero_a = area_tri_v3(v2, v3, v4) <= FLT_EPSILON;
|
|
|
|
|
is_zero_b = area_tri_v3(v2, v4, v1) <= FLT_EPSILON;
|
|
|
|
|
|
|
|
|
|
if (LIKELY(is_zero_a == false && is_zero_b == false)) {
|
|
|
|
|
float no_a[3], no_b[3];
|
|
|
|
|
normal_tri_v3(no_a, v2, v3, v4); /* a */
|
|
|
|
|
normal_tri_v3(no_b, v2, v4, v1); /* b */
|
|
|
|
|
add_v3_v3v3(no, no_a, no_b);
|
|
|
|
|
if (UNLIKELY(normalize_v3(no) <= FLT_EPSILON)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (is_zero_a == false) {
|
|
|
|
|
normal_tri_v3(no, v2, v3, v4); /* a */
|
|
|
|
|
}
|
|
|
|
|
else if (is_zero_b == false) {
|
|
|
|
|
normal_tri_v3(no, v2, v4, v1); /* b */
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* both zero area, no useful normal can be calculated */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// { float a = angle_normalized_v3v3(no_a, no_b); printf("~ %.7f\n", a); fflush(stdout);}
|
|
|
|
|
|
|
|
|
|
axis_dominant_v3_to_m3(axis_mat, no);
|
|
|
|
|
mul_v2_m3v3(v1_xy, axis_mat, v1);
|
|
|
|
|
mul_v2_m3v3(v2_xy, axis_mat, v2);
|
|
|
|
|
mul_v2_m3v3(v3_xy, axis_mat, v3);
|
|
|
|
|
mul_v2_m3v3(v4_xy, axis_mat, v4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// printf("%p %p %p %p - %p %p\n", v1, v2, v3, v4, e->l->f, e->l->radial_next->f);
|
|
|
|
|
|
|
|
|
|
if (is_quad_convex_v2(v1_xy, v2_xy, v3_xy, v4_xy)) {
|
2013-09-17 22:48:08 +00:00
|
|
|
/* testing rule: the area divided by the perimeter,
|
|
|
|
|
* check if (1-3) beats the existing (2-4) edge rotation */
|
|
|
|
|
float area_a, area_b;
|
|
|
|
|
float prim_a, prim_b;
|
|
|
|
|
float fac_24, fac_13;
|
|
|
|
|
|
|
|
|
|
float len_12, len_23, len_34, len_41, len_24, len_13;
|
|
|
|
|
|
|
|
|
|
/* edges around the quad */
|
|
|
|
|
len_12 = len_v2v2(v1_xy, v2_xy);
|
|
|
|
|
len_23 = len_v2v2(v2_xy, v3_xy);
|
|
|
|
|
len_34 = len_v2v2(v3_xy, v4_xy);
|
|
|
|
|
len_41 = len_v2v2(v4_xy, v1_xy);
|
|
|
|
|
/* edges crossing the quad interior */
|
|
|
|
|
len_13 = len_v2v2(v1_xy, v3_xy);
|
|
|
|
|
len_24 = len_v2v2(v2_xy, v4_xy);
|
|
|
|
|
|
|
|
|
|
/* edge (2-4), current state */
|
|
|
|
|
area_a = area_tri_v2(v2_xy, v3_xy, v4_xy);
|
|
|
|
|
area_b = area_tri_v2(v2_xy, v4_xy, v1_xy);
|
|
|
|
|
prim_a = len_23 + len_34 + len_24;
|
|
|
|
|
prim_b = len_24 + len_41 + len_12;
|
|
|
|
|
fac_24 = (area_a / prim_a) + (area_b / prim_b);
|
|
|
|
|
|
|
|
|
|
/* edge (1-3), new state */
|
|
|
|
|
area_a = area_tri_v2(v1_xy, v2_xy, v3_xy);
|
|
|
|
|
area_b = area_tri_v2(v1_xy, v3_xy, v4_xy);
|
|
|
|
|
prim_a = len_12 + len_23 + len_13;
|
|
|
|
|
prim_b = len_34 + len_41 + len_13;
|
|
|
|
|
fac_13 = (area_a / prim_a) + (area_b / prim_b);
|
|
|
|
|
|
|
|
|
|
/* negative number if (1-3) is an improved state */
|
|
|
|
|
return fac_24 - fac_13;
|
2013-03-30 09:57:35 +00:00
|
|
|
}
|
|
|
|
|
} while (false);
|
|
|
|
|
|
2013-03-30 11:05:57 +00:00
|
|
|
return FLT_MAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/* Update the edge cost of rotation in the heap */
|
|
|
|
|
|
|
|
|
|
/* recalc an edge in the heap (surrounding geometry has changed) */
|
2013-08-25 20:03:45 +00:00
|
|
|
static void bm_edge_update_beauty_cost_single(BMEdge *e, Heap *eheap, HeapNode **eheap_table, GSet **edge_state_arr,
|
2013-05-08 12:56:41 +00:00
|
|
|
const int flag)
|
2013-03-30 11:05:57 +00:00
|
|
|
{
|
|
|
|
|
if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
|
|
|
|
|
const int i = BM_elem_index_get(e);
|
2013-08-25 20:03:45 +00:00
|
|
|
GSet *e_state_set = edge_state_arr[i];
|
2013-03-30 11:05:57 +00:00
|
|
|
|
|
|
|
|
if (eheap_table[i]) {
|
|
|
|
|
BLI_heap_remove(eheap, eheap_table[i]);
|
|
|
|
|
eheap_table[i] = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check if we can add it back */
|
|
|
|
|
BLI_assert(BM_edge_is_manifold(e) == true);
|
|
|
|
|
//BLI_assert(BMO_elem_flag_test(bm, e->l->f, FACE_MARK) &&
|
|
|
|
|
// BMO_elem_flag_test(bm, e->l->radial_next->f, FACE_MARK));
|
|
|
|
|
|
|
|
|
|
/* check we're not moving back into a state we have been in before */
|
2013-08-25 20:03:45 +00:00
|
|
|
if (e_state_set != NULL) {
|
2013-03-30 11:05:57 +00:00
|
|
|
EdRotState e_state_alt;
|
|
|
|
|
erot_state_alternate(e, &e_state_alt);
|
2013-08-25 20:03:45 +00:00
|
|
|
if (BLI_gset_haskey(e_state_set, (void *)&e_state_alt)) {
|
2013-03-30 11:05:57 +00:00
|
|
|
// printf(" skipping, we already have this state\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
/* recalculate edge */
|
2013-05-08 12:56:41 +00:00
|
|
|
const float cost = bm_edge_calc_rotate_beauty(e, flag);
|
2013-03-30 11:05:57 +00:00
|
|
|
if (cost < 0.0f) {
|
|
|
|
|
eheap_table[i] = BLI_heap_insert(eheap, cost, e);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
eheap_table[i] = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-27 07:02:27 +00:00
|
|
|
/* we have rotated an edge, tag other edges and clear this one */
|
2013-08-25 20:03:45 +00:00
|
|
|
static void bm_edge_update_beauty_cost(BMEdge *e, Heap *eheap, HeapNode **eheap_table, GSet **edge_state_arr,
|
2013-05-08 12:56:41 +00:00
|
|
|
const int flag)
|
2013-03-30 11:05:57 +00:00
|
|
|
{
|
|
|
|
|
BMLoop *l;
|
|
|
|
|
BLI_assert(e->l->f->len == 3 &&
|
|
|
|
|
e->l->radial_next->f->len == 3);
|
|
|
|
|
|
|
|
|
|
l = e->l;
|
2013-05-08 12:56:41 +00:00
|
|
|
bm_edge_update_beauty_cost_single(l->next->e, eheap, eheap_table, edge_state_arr, flag);
|
|
|
|
|
bm_edge_update_beauty_cost_single(l->prev->e, eheap, eheap_table, edge_state_arr, flag);
|
2013-03-30 11:05:57 +00:00
|
|
|
l = l->radial_next;
|
2013-05-08 12:56:41 +00:00
|
|
|
bm_edge_update_beauty_cost_single(l->next->e, eheap, eheap_table, edge_state_arr, flag);
|
|
|
|
|
bm_edge_update_beauty_cost_single(l->prev->e, eheap, eheap_table, edge_state_arr, flag);
|
2013-03-30 09:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-26 00:29:57 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/* Beautify Fill */
|
|
|
|
|
|
2013-03-25 22:04:12 +00:00
|
|
|
#define ELE_NEW 1
|
|
|
|
|
#define FACE_MARK 2
|
|
|
|
|
|
2013-03-26 00:29:57 +00:00
|
|
|
/**
|
|
|
|
|
* \note All edges in \a edge_array must be tagged and
|
|
|
|
|
* have their index values set according to their position in the array.
|
|
|
|
|
*/
|
2013-05-08 12:56:41 +00:00
|
|
|
static void bm_mesh_beautify_fill(BMesh *bm, BMEdge **edge_array, const int edge_array_len,
|
|
|
|
|
const int flag)
|
2013-03-25 22:04:12 +00:00
|
|
|
{
|
2013-03-30 11:05:57 +00:00
|
|
|
Heap *eheap; /* edge heap */
|
|
|
|
|
HeapNode **eheap_table; /* edge index aligned table pointing to the eheap */
|
|
|
|
|
|
2013-08-25 20:03:45 +00:00
|
|
|
GSet **edge_state_arr = MEM_callocN(edge_array_len * sizeof(GSet *), __func__);
|
2013-03-26 00:29:57 +00:00
|
|
|
BLI_mempool *edge_state_pool = BLI_mempool_create(sizeof(EdRotState), 512, 512, BLI_MEMPOOL_SYSMALLOC);
|
|
|
|
|
int i;
|
2013-03-25 22:04:12 +00:00
|
|
|
|
2013-03-25 23:21:16 +00:00
|
|
|
#ifdef DEBUG_TIME
|
|
|
|
|
TIMEIT_START(beautify_fill);
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-03-30 11:05:57 +00:00
|
|
|
eheap = BLI_heap_new_ex(edge_array_len);
|
2013-04-01 07:57:33 +00:00
|
|
|
eheap_table = MEM_mallocN(sizeof(HeapNode *) * edge_array_len, __func__);
|
2013-03-25 22:04:12 +00:00
|
|
|
|
2013-03-30 11:05:57 +00:00
|
|
|
/* build heap */
|
|
|
|
|
for (i = 0; i < edge_array_len; i++) {
|
|
|
|
|
BMEdge *e = edge_array[i];
|
2013-05-08 12:56:41 +00:00
|
|
|
const float cost = bm_edge_calc_rotate_beauty(e, flag);
|
2013-03-30 11:05:57 +00:00
|
|
|
if (cost < 0.0f) {
|
|
|
|
|
eheap_table[i] = BLI_heap_insert(eheap, cost, e);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
eheap_table[i] = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-26 00:29:57 +00:00
|
|
|
|
2013-03-30 11:05:57 +00:00
|
|
|
while (BLI_heap_is_empty(eheap) == false) {
|
|
|
|
|
BMEdge *e = BLI_heap_popmin(eheap);
|
|
|
|
|
i = BM_elem_index_get(e);
|
|
|
|
|
eheap_table[i] = NULL;
|
|
|
|
|
|
|
|
|
|
e = BM_edge_rotate(bm, e, false, BM_EDGEROT_CHECK_EXISTS);
|
|
|
|
|
if (LIKELY(e)) {
|
2013-08-25 20:03:45 +00:00
|
|
|
GSet *e_state_set = edge_state_arr[i];
|
2013-03-30 11:05:57 +00:00
|
|
|
|
2013-08-25 20:03:45 +00:00
|
|
|
/* add the new state into the set so we don't move into this state again
|
2013-03-30 11:05:57 +00:00
|
|
|
* note: we could add the previous state too but this isn't essential)
|
|
|
|
|
* for avoiding eternal loops */
|
|
|
|
|
EdRotState *e_state = BLI_mempool_alloc(edge_state_pool);
|
|
|
|
|
erot_state_current(e, e_state);
|
2013-08-25 20:03:45 +00:00
|
|
|
if (UNLIKELY(e_state_set == NULL)) {
|
|
|
|
|
edge_state_arr[i] = e_state_set = erot_gset_new(); /* store previous state */
|
2013-03-26 00:29:57 +00:00
|
|
|
}
|
2013-08-25 20:03:45 +00:00
|
|
|
BLI_assert(BLI_gset_haskey(e_state_set, (void *)e_state) == false);
|
|
|
|
|
BLI_gset_insert(e_state_set, e_state);
|
2013-03-26 00:29:57 +00:00
|
|
|
|
2013-03-26 01:49:55 +00:00
|
|
|
|
2013-03-30 11:05:57 +00:00
|
|
|
// printf(" %d -> %d, %d\n", i, BM_elem_index_get(e->v1), BM_elem_index_get(e->v2));
|
2013-03-26 01:49:55 +00:00
|
|
|
|
2013-03-30 11:05:57 +00:00
|
|
|
/* maintain the index array */
|
|
|
|
|
edge_array[i] = e;
|
|
|
|
|
BM_elem_index_set(e, i);
|
2013-03-25 22:04:12 +00:00
|
|
|
|
2013-03-30 11:05:57 +00:00
|
|
|
/* recalculate faces connected on the heap */
|
2013-05-08 12:56:41 +00:00
|
|
|
bm_edge_update_beauty_cost(e, eheap, eheap_table, edge_state_arr, flag);
|
2013-03-25 22:04:12 +00:00
|
|
|
|
2013-03-30 11:05:57 +00:00
|
|
|
/* update flags */
|
|
|
|
|
BMO_elem_flag_enable(bm, e, ELE_NEW);
|
|
|
|
|
BMO_elem_flag_enable(bm, e->l->f, FACE_MARK | ELE_NEW);
|
|
|
|
|
BMO_elem_flag_enable(bm, e->l->radial_next->f, FACE_MARK | ELE_NEW);
|
2013-03-25 22:04:12 +00:00
|
|
|
}
|
2013-03-30 11:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_heap_free(eheap, NULL);
|
|
|
|
|
MEM_freeN(eheap_table);
|
2013-03-25 23:21:16 +00:00
|
|
|
|
2013-03-26 00:29:57 +00:00
|
|
|
for (i = 0; i < edge_array_len; i++) {
|
|
|
|
|
if (edge_state_arr[i]) {
|
2013-08-25 20:03:45 +00:00
|
|
|
BLI_gset_free(edge_state_arr[i], NULL);
|
2013-03-26 00:29:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MEM_freeN(edge_state_arr);
|
|
|
|
|
BLI_mempool_destroy(edge_state_pool);
|
|
|
|
|
|
2013-03-25 23:21:16 +00:00
|
|
|
#ifdef DEBUG_TIME
|
|
|
|
|
TIMEIT_END(beautify_fill);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void bmo_beautify_fill_exec(BMesh *bm, BMOperator *op)
|
|
|
|
|
{
|
2013-04-09 00:07:47 +00:00
|
|
|
BMIter iter;
|
2013-03-25 23:21:16 +00:00
|
|
|
BMOIter siter;
|
|
|
|
|
BMFace *f;
|
|
|
|
|
BMEdge *e;
|
2013-05-08 12:56:41 +00:00
|
|
|
const bool use_restrict_tag = BMO_slot_bool_get(op->slots_in, "use_restrict_tag");
|
|
|
|
|
const int flag = (use_restrict_tag ? VERT_RESTRICT_TAG : 0);
|
2013-03-25 23:21:16 +00:00
|
|
|
|
|
|
|
|
BMEdge **edge_array;
|
|
|
|
|
int edge_array_len = 0;
|
|
|
|
|
|
|
|
|
|
BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
|
|
|
|
|
if (f->len == 3) {
|
|
|
|
|
BMO_elem_flag_enable(bm, f, FACE_MARK);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-09 00:07:47 +00:00
|
|
|
BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
|
|
|
|
|
BM_elem_flag_disable(e, BM_ELEM_TAG);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 23:21:16 +00:00
|
|
|
/* will over alloc if some edges can't be rotated */
|
|
|
|
|
edge_array = MEM_mallocN(sizeof(*edge_array) * BMO_slot_buffer_count(op->slots_in, "edges"), __func__);
|
|
|
|
|
|
|
|
|
|
BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
|
|
|
|
|
|
|
|
|
|
/* edge is manifold and can be rotated */
|
|
|
|
|
if (BM_edge_rotate_check(e) &&
|
2013-07-25 07:00:07 +00:00
|
|
|
/* faces are tagged */
|
|
|
|
|
BMO_elem_flag_test(bm, e->l->f, FACE_MARK) &&
|
|
|
|
|
BMO_elem_flag_test(bm, e->l->radial_next->f, FACE_MARK))
|
2013-03-25 23:21:16 +00:00
|
|
|
{
|
|
|
|
|
BM_elem_index_set(e, edge_array_len); /* set_dirty */
|
2013-03-26 00:29:57 +00:00
|
|
|
BM_elem_flag_enable(e, BM_ELEM_TAG);
|
2013-03-25 23:21:16 +00:00
|
|
|
edge_array[edge_array_len] = e;
|
|
|
|
|
edge_array_len++;
|
|
|
|
|
}
|
2013-03-25 22:04:12 +00:00
|
|
|
}
|
2013-03-25 23:21:16 +00:00
|
|
|
bm->elem_index_dirty |= BM_EDGE;
|
|
|
|
|
|
2013-05-08 12:56:41 +00:00
|
|
|
bm_mesh_beautify_fill(bm, edge_array, edge_array_len, flag);
|
2013-03-25 23:21:16 +00:00
|
|
|
|
|
|
|
|
MEM_freeN(edge_array);
|
|
|
|
|
|
2013-03-25 22:04:12 +00:00
|
|
|
BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
|
|
|
|
|
}
|