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

bmesh_edgesplit.c « tools « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 947b77675d83a23f1c6a06646a166060da11b091 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * ***** 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/tools/bmesh_edgesplit.c
 *  \ingroup bmesh
 *
 * Edge-Split.
 *
 */

#include "MEM_guardedalloc.h"

#include "BLI_utildefines.h"

#include "bmesh.h"

#include "bmesh_edgesplit.h"  /* own include */


/**
 * Remove the BM_ELEM_TAG flag for edges we cant split
 *
 * un-tag edges not connected to other tagged edges,
 * unless they are on a boundary
 */
static void bm_edgesplit_validate_seams(BMesh *bm)
{
	BMIter iter;
	BMEdge *e;

	unsigned char *vtouch;

	BM_mesh_elem_index_ensure(bm, BM_VERT);

	vtouch = MEM_callocN(sizeof(char) * bm->totvert, __func__);

	/* tag all boundary verts so as not to untag an edge which is inbetween only 2 faces [] */
	BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {

		/* unrelated to flag assignment in this function - since this is the
		 * only place we loop over all edges, disable tag */
		BM_elem_flag_disable(e, BM_ELEM_INTERNAL_TAG);

		if (e->l == NULL) {
			BM_elem_flag_disable(e, BM_ELEM_TAG);
		}
		else if (BM_edge_is_boundary(e)) {
			unsigned char *vt;
			vt = &vtouch[BM_elem_index_get(e->v1)]; if (*vt < 2) (*vt)++;
			vt = &vtouch[BM_elem_index_get(e->v2)]; if (*vt < 2) (*vt)++;

			/* while the boundary verts need to be tagged,
			 * the edge its self can't be split */
			BM_elem_flag_disable(e, BM_ELEM_TAG);
		}
	}

	/* single marked edges unconnected to any other marked edges
	 * are illegal, go through and unmark them */
	BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
		if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
			/* lame, but we don't want the count to exceed 255,
			 * so just count to 2, its all we need */
			unsigned char *vt;
			vt = &vtouch[BM_elem_index_get(e->v1)]; if (*vt < 2) (*vt)++;
			vt = &vtouch[BM_elem_index_get(e->v2)]; if (*vt < 2) (*vt)++;
		}
	}
	BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
		if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
			if (vtouch[BM_elem_index_get(e->v1)] == 1 &&
			    vtouch[BM_elem_index_get(e->v2)] == 1)
			{
				BM_elem_flag_disable(e, BM_ELEM_TAG);
			}
		}
	}

	MEM_freeN(vtouch);
}

void BM_mesh_edgesplit(BMesh *bm, const bool use_verts, const bool tag_only, const bool copy_select)
{
	BMIter iter;
	BMEdge *e;

	bool use_ese = false;
	GHash *ese_gh = NULL;

	if (copy_select && bm->selected.first) {
		BMEditSelection *ese;

		ese_gh = BLI_ghash_ptr_new(__func__);
		for (ese = bm->selected.first; ese; ese = ese->next) {
			if (ese->htype != BM_FACE) {
				BLI_ghash_insert(ese_gh, ese->ele, ese);
			}
		}

		use_ese = true;
	}

	if (tag_only == false) {
		BM_mesh_elem_hflag_enable_all(bm, BM_EDGE | (use_verts ? BM_VERT : 0), BM_ELEM_TAG, false);
	}

	if (use_verts) {
		/* prevent one edge having both verts unflagged
		 * we could alternately disable these edges, either way its a corner case.
		 *
		 * This is needed so we don't split off the edge but then none of its verts which
		 * would leave a duplicate edge.
		 */
		BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
			if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
				if (UNLIKELY(((BM_elem_flag_test(e->v1, BM_ELEM_TAG) == false) &&
				              (BM_elem_flag_test(e->v2, BM_ELEM_TAG) == false))))
				{
					BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
					BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
				}
			}
		}
	}

	bm_edgesplit_validate_seams(bm);

	BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
		if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
			/* this flag gets copied so we can be sure duplicate edges get it too (important) */
			BM_elem_flag_enable(e, BM_ELEM_INTERNAL_TAG);

			/* keep splitting until each loop has its own edge */
			while (!BM_edge_is_boundary(e)) {
				BMLoop *l_sep = e->l;
				bmesh_edge_separate(bm, e, l_sep, copy_select);
				BLI_assert(l_sep->e != e);

				if (use_ese) {
					BMEditSelection *ese = BLI_ghash_lookup(ese_gh, e);
					if (UNLIKELY(ese)) {
						BM_select_history_store_after_notest(bm, ese, l_sep->e);
					}
				}
			}

			BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
			BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
		}
	}

	if (use_verts) {
		BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
			if (BM_elem_flag_test(e->v1, BM_ELEM_TAG) == false) {
				BM_elem_flag_disable(e->v1, BM_ELEM_TAG);
			}
			if (BM_elem_flag_test(e->v2, BM_ELEM_TAG) == false) {
				BM_elem_flag_disable(e->v2, BM_ELEM_TAG);
			}
		}
	}

	BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
		if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
			unsigned int i;
			for (i = 0; i < 2; i++) {
				BMVert *v = ((&e->v1)[i]);
				if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
					BM_elem_flag_disable(v, BM_ELEM_TAG);

					if (use_ese) {
						BMVert **vtar;
						int vtar_len;

						bmesh_vert_separate(bm, v, &vtar, &vtar_len, copy_select);

						/* first value is always in 'v' */
						if (vtar_len > 1) {
							BMEditSelection *ese = BLI_ghash_lookup(ese_gh, v);
							BLI_assert(v == vtar[0]);
							if (UNLIKELY(ese)) {
								int j;
								for (j = 1; j < vtar_len; j++) {
									BLI_assert(v != vtar[j]);
									BM_select_history_store_after_notest(bm, ese, vtar[j]);
								}
							}
						}
						MEM_freeN(vtar);
					}
					else {
						bmesh_vert_separate(bm, v, NULL, NULL, copy_select);
					}
				}
			}
		}
	}

	if (use_ese) {
		BLI_ghash_free(ese_gh, NULL, NULL);
	}
}