Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bmo_fill_holes.c « operators « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dca864452196de58a3093a23395a9c09c8a688ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * ***** 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): Campbell Barton.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/bmesh/operators/bmo_fill_holes.c
 *  \ingroup bmesh
 *
 * Fill boundary edge loop(s) with faces.
 */

#include "MEM_guardedalloc.h"

#include "BLI_listbase.h"
#include "BLI_array.h"
#include "BLI_math.h"

#include "bmesh.h"

#include "intern/bmesh_operators_private.h" /* own include */

#define EDGE_MARK	2
#define ELE_OUT		4

/**
 * Clone of BM_face_find_longest_loop that ensures the loop has an adjacent face
 */
static BMLoop *bm_face_find_longest_loop_manifold(BMFace *f)
{
	BMLoop *longest_loop = NULL;
	float longest_len = 0.0f;
	BMLoop *l_iter, *l_first;

	l_iter = l_first = BM_FACE_FIRST_LOOP(f);

	do {
		if (BM_edge_is_wire(l_iter->e) == false) {
			const float len = len_squared_v3v3(l_iter->v->co, l_iter->next->v->co);
			if (len >= longest_len) {
				longest_loop = l_iter;
				longest_len = len;
			}
		}
	} while ((l_iter = l_iter->next) != l_first);

	return longest_loop;
}

static BMFace *bm_face_from_eloop(BMesh *bm, struct BMEdgeLoopStore *el_store)
{
	LinkData *node = BM_edgeloop_verts_get(el_store)->first;
	const int len = BM_edgeloop_length_get(el_store);
	BMVert **f_verts = BLI_array_alloca(f_verts, len);
	BMFace *f;
	BMLoop *l;
	unsigned int i = 0;

	do {
		f_verts[i++] = node->data;
	} while ((node = node->next));

	f = BM_face_create_ngon_verts(bm, f_verts, len, 0, true, false);
	BM_face_copy_shared(bm, f);

	l = bm_face_find_longest_loop_manifold(f);
	if (l) {
		BMFace *f_other = l->radial_next->f;
		BLI_assert(l->radial_next != l);
		BM_elem_attrs_copy(bm, bm, f_other, f);
	}

	return f;
}

static bool bm_edge_test_cb(BMEdge *e, void *bm_v)
{
	return BMO_elem_flag_test((BMesh *)bm_v, e, EDGE_MARK);
}

void bmo_holes_fill_exec(BMesh *bm, BMOperator *op)
{
	ListBase eloops = {NULL, NULL};
	LinkData *el_store;

	BMEdge *e;
	int count;

	BMOIter siter;

	const int  sides    = BMO_slot_int_get(op->slots_in,  "sides");

	/* clear tags */

	BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, false);

	/* tag edges that may be apart of loops */
	BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
		BMO_elem_flag_set(bm, e, EDGE_MARK, BM_edge_is_boundary(e));
	}

	count = BM_mesh_edgeloops_find(bm, &eloops, bm_edge_test_cb, (void *)bm);

	for (el_store = eloops.first; el_store; el_store = el_store->next) {
		if (BM_edgeloop_is_closed((struct BMEdgeLoopStore *)el_store)) {
			const int len = BM_edgeloop_length_get((struct BMEdgeLoopStore *)el_store);
			if ((sides == 0) || (len <= sides)) {
				BMFace *f;

				f = bm_face_from_eloop(bm, (struct BMEdgeLoopStore *)el_store);
				BMO_elem_flag_enable(bm, f, ELE_OUT);
			}
		}
	}

	(void)count;

	BM_mesh_edgeloops_free(&eloops);

	BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, ELE_OUT);
}