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

bmesh_mesh_validate.c « intern « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c9ebc800a343a6685397db4ef66892f38c23459 (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
/*
 * ***** 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.
 *
 * The Original Code is Copyright (C) 2012 Blender Foundation.
 * All rights reserved.
 *
 * Contributor(s): Campbell Barton
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/bmesh/intern/bmesh_mesh_validate.c
 *  \ingroup bmesh
 *
 * BM mesh validation function.
 */

/* debug builds only */
#ifdef DEBUG

#include "BLI_utildefines.h"
#include "BLI_edgehash.h"

#include "bmesh.h"

#include "bmesh_mesh_validate.h"


/* macro which inserts the function name */
#if defined __GNUC__ || defined __sun
#  define ERRMSG(format, args...) { fprintf(stderr, "%s: " format ", " AT "\n", __func__, ##args); errtot++; } (void)0
#else
#  define ERRMSG(format, ...) { fprintf(stderr, "%s: " format ", " AT "\n", __func__, __VA_ARGS__); errtot++; } (void)0
#endif

/**
 * Check of this BMesh is valid, this function can be slow since its intended to help with debugging.
 *
 * \return true when the mesh is valid.
 */
bool BM_mesh_validate(BMesh *bm)
{
	EdgeHash *edge_hash = BLI_edgehash_new_ex(__func__, bm->totedge);
	int errtot;

	BMIter iter;
	BMVert *v;
	BMEdge *e;
	BMFace *f;

	int i, j;

	errtot = -1; /* 'ERRMSG' next line will set at zero */
	fprintf(stderr, "\n");
	ERRMSG("This is a debugging function and not intended for general use, running slow test!");

	/* force recalc, even if tagged as valid, since this mesh is suspect! */
	bm->elem_index_dirty |= BM_ALL;
	BM_mesh_elem_index_ensure(bm, BM_ALL);

	BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
		if (BM_elem_flag_test(v, BM_ELEM_SELECT | BM_ELEM_HIDDEN) == (BM_ELEM_SELECT | BM_ELEM_HIDDEN)) {
			ERRMSG("vert %d: is hidden and selected", i);
		}

		if (v->e) {
			if (!BM_vert_in_edge(v->e, v)) {
				ERRMSG("vert %d: is not in its referenced edge: %d", i, BM_elem_index_get(v->e));
			}
		}
	}

	/* check edges */
	BM_ITER_MESH_INDEX (e, &iter, bm, BM_EDGES_OF_MESH, i) {
		void **val_p;

		if (e->v1 == e->v2) {
			ERRMSG("edge %d: duplicate index: %d", i, BM_elem_index_get(e->v1));
		}

		/* build edgehash at the same time */
		if (BLI_edgehash_ensure_p(edge_hash, BM_elem_index_get(e->v1), BM_elem_index_get(e->v2), &val_p)) {
			BMEdge *e_other = *val_p;
			ERRMSG("edge %d, %d: are duplicates", i, BM_elem_index_get(e_other));
		}
		else {
			*val_p = e;
		}
	}

	/* edge radial structure */
	BM_ITER_MESH_INDEX (e, &iter, bm, BM_EDGES_OF_MESH, i) {
		if (BM_elem_flag_test(e, BM_ELEM_SELECT | BM_ELEM_HIDDEN) == (BM_ELEM_SELECT | BM_ELEM_HIDDEN)) {
			ERRMSG("edge %d: is hidden and selected", i);
		}

		if (e->l) {
			BMLoop *l_iter;
			BMLoop *l_first;

			j = 0;

			l_iter = l_first = e->l;
			/* we could do more checks here, but save for face checks */
			do {
				if (l_iter->e != e) {
					ERRMSG("edge %d: has invalid loop, loop is of face %d", i, BM_elem_index_get(l_iter->f));
				}
				else if (BM_vert_in_edge(e, l_iter->v) == false) {
					ERRMSG("edge %d: has invalid loop with vert not in edge, loop is of face %d",
					       i, BM_elem_index_get(l_iter->f));
				}
				else if (BM_vert_in_edge(e, l_iter->next->v) == false) {
					ERRMSG("edge %d: has invalid loop with next vert not in edge, loop is of face %d",
					       i, BM_elem_index_get(l_iter->f));
				}
			} while ((l_iter = l_iter->radial_next) != l_first);
		}
	}

	/* face structure */
	BM_ITER_MESH_INDEX (f, &iter, bm, BM_FACES_OF_MESH, i) {
		BMLoop *l_iter;
		BMLoop *l_first;

		if (BM_elem_flag_test(f, BM_ELEM_SELECT | BM_ELEM_HIDDEN) == (BM_ELEM_SELECT | BM_ELEM_HIDDEN)) {
			ERRMSG("face %d: is hidden and selected", i);
		}

		l_iter = l_first = BM_FACE_FIRST_LOOP(f);

		do {
			BM_elem_flag_disable(l_iter,    BM_ELEM_INTERNAL_TAG);
			BM_elem_flag_disable(l_iter->v, BM_ELEM_INTERNAL_TAG);
			BM_elem_flag_disable(l_iter->e, BM_ELEM_INTERNAL_TAG);
		} while ((l_iter = l_iter->next) != l_first);

		j = 0;

		l_iter = l_first = BM_FACE_FIRST_LOOP(f);
		do {
			if (BM_elem_flag_test(l_iter, BM_ELEM_INTERNAL_TAG)) {
				ERRMSG("face %d: has duplicate loop at corner: %d", i, j);
			}
			if (BM_elem_flag_test(l_iter->v, BM_ELEM_INTERNAL_TAG)) {
				ERRMSG("face %d: has duplicate vert: %d, at corner: %d", i, BM_elem_index_get(l_iter->v), j);
			}
			if (BM_elem_flag_test(l_iter->e, BM_ELEM_INTERNAL_TAG)) {
				ERRMSG("face %d: has duplicate edge: %d, at corner: %d", i,  BM_elem_index_get(l_iter->e), j);
			}

			/* adjacent data checks */
			if (l_iter->f != f) {
				ERRMSG("face %d: has loop that points to face: %d at corner: %d", i, BM_elem_index_get(l_iter->f), j);
			}
			if (l_iter != l_iter->prev->next) {
				ERRMSG("face %d: has invalid 'prev/next' at corner: %d", i, j);
			}
			if (l_iter != l_iter->next->prev) {
				ERRMSG("face %d: has invalid 'next/prev' at corner: %d", i, j);
			}
			if (l_iter != l_iter->radial_prev->radial_next) {
				ERRMSG("face %d: has invalid 'radial_prev/radial_next' at corner: %d", i, j);
			}
			if (l_iter != l_iter->radial_next->radial_prev) {
				ERRMSG("face %d: has invalid 'radial_next/radial_prev' at corner: %d", i, j);
			}

			BM_elem_flag_enable(l_iter,    BM_ELEM_INTERNAL_TAG);
			BM_elem_flag_enable(l_iter->v, BM_ELEM_INTERNAL_TAG);
			BM_elem_flag_enable(l_iter->e, BM_ELEM_INTERNAL_TAG);
			j++;
		} while ((l_iter = l_iter->next) != l_first);

		if (j != f->len) {
			ERRMSG("face %d: has length of %d but should be %d", i, f->len, j);
		}

		/* leave elements un-tagged, not essential but nice to avoid unintended dirty tag use later. */
		do {
			BM_elem_flag_disable(l_iter,    BM_ELEM_INTERNAL_TAG);
			BM_elem_flag_disable(l_iter->v, BM_ELEM_INTERNAL_TAG);
			BM_elem_flag_disable(l_iter->e, BM_ELEM_INTERNAL_TAG);
		} while ((l_iter = l_iter->next) != l_first);
	}

	BLI_edgehash_free(edge_hash, NULL);

	const bool is_valid = (errtot == 0);
	ERRMSG("Finished - errors %d", errtot);
	return is_valid;
}


#endif