Blender V5.3
bmesh_mesh_validate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2012 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11/* debug builds only */
12#ifndef NDEBUG
13
14# include "BLI_map.hh"
15# include "BLI_ordered_edge.hh"
16# include "BLI_set.hh"
17# include "BLI_utildefines.hh"
18
19# include "bmesh.hh"
20
21# include "bmesh_mesh_validate.hh"
22
23namespace blender {
24
25/* macro which inserts the function name */
26# if defined __GNUC__
27# define ERRMSG(format, args...) \
28 { \
29 fprintf(stderr, \
30 "%s: " format ", " AT "\n", \
31 __func__, /* NOLINT(bugprone-lambda-function-name) */ \
32 ##args); \
33 errtot++; \
34 } \
35 (void)0
36# elif defined(_MSVC_TRADITIONAL) && !_MSVC_TRADITIONAL
37# define ERRMSG(format, ...) \
38 { \
39 fprintf(stderr, "%s: " format ", " AT "\n", __func__, ##__VA_ARGS__); \
40 errtot++; \
41 } \
42 (void)0
43# else
44# define ERRMSG(format, ...) \
45 { \
46 fprintf(stderr, "%s: " format ", " AT "\n", __func__, __VA_ARGS__); \
47 errtot++; \
48 } \
49 (void)0
50# endif
51
52template<> struct DefaultHash<Set<Vector<int>>> {
53 uint64_t operator()(const Vector<int> &value) const
54 {
55 uint64_t hash = 0;
56 for (const int v : value) {
58 }
59 return hash;
60 }
61};
62
64{
66 edge_hash.reserve(bm->totedge);
67 int errtot;
68
69 BMIter iter;
70 BMVert *v;
71 BMEdge *e;
72 BMFace *f;
73
74 int i, j;
75
76 errtot = -1; /* 'ERRMSG' next line will set at zero */
77 fprintf(stderr, "\n");
78 ERRMSG("This is a debugging function and not intended for general use, running slow test!");
79
80 /* force recalc, even if tagged as valid, since this mesh is suspect! */
83
86 {
87 ERRMSG("vert %d: is hidden and selected", i);
88 }
89
90 if (v->e) {
91 if (!BM_vert_in_edge(v->e, v)) {
92 ERRMSG("vert %d: is not in its referenced edge: %d", i, BM_elem_index_get(v->e));
93 }
94 }
95 }
96
97 /* check edges */
99 if (e->v1 == e->v2) {
100 ERRMSG("edge %d: duplicate index: %d", i, BM_elem_index_get(e->v1));
101 }
102
103 /* Build edge-hash at the same time. */
104 edge_hash.add_or_modify(
106 [&](BMEdge **value) { *value = e; },
107 [&](BMEdge **value) {
108 ERRMSG("edge %d, %d: are duplicates", i, BM_elem_index_get(*value));
109 });
110 }
111
112 /* edge radial structure */
115 {
116 ERRMSG("edge %d: is hidden and selected", i);
117 }
118
119 if (e->l) {
120 BMLoop *l_iter;
121 BMLoop *l_first;
122
123 j = 0;
124
125 l_iter = l_first = e->l;
126 /* we could do more checks here, but save for face checks */
127 do {
128 if (l_iter->e != e) {
129 ERRMSG("edge %d: has invalid loop, loop is of face %d", i, BM_elem_index_get(l_iter->f));
130 }
131 else if (BM_vert_in_edge(e, l_iter->v) == false) {
132 ERRMSG("edge %d: has invalid loop with vert not in edge, loop is of face %d",
133 i,
134 BM_elem_index_get(l_iter->f));
135 }
136 else if (BM_vert_in_edge(e, l_iter->next->v) == false) {
137 ERRMSG("edge %d: has invalid loop with next vert not in edge, loop is of face %d",
138 i,
139 BM_elem_index_get(l_iter->f));
140 }
141 } while ((l_iter = l_iter->radial_next) != l_first);
142 }
143 }
144
145 /* face structure */
146 Map<Vector<int>, int> face_map;
147 BM_ITER_MESH_INDEX (f, &iter, bm, BM_FACES_OF_MESH, i) {
148 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
149 BMLoop *l_iter;
150
152 {
153 ERRMSG("face %d: is hidden and selected", i);
154 }
155
156 j = 0;
157 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
158 do {
162 j++;
163 } while ((l_iter = l_iter->next) != l_first);
164
165 const int f_len = j;
166 if (f_len != f->len) {
167 ERRMSG("face %d: has length of %d but should be %d", i, f->len, f_len);
168 }
169
170 /* Store the loop with the minimum index to create a list of vertex indices. */
171 BMLoop *l_vert_min = l_first;
172 j = 0;
173 l_iter = l_first;
174 do {
176 ERRMSG("face %d: has duplicate loop at corner: %d", i, j);
177 }
179 ERRMSG(
180 "face %d: has duplicate vert: %d, at corner: %d", i, BM_elem_index_get(l_iter->v), j);
181 }
183 ERRMSG(
184 "face %d: has duplicate edge: %d, at corner: %d", i, BM_elem_index_get(l_iter->e), j);
185 }
186
187 /* adjacent data checks */
188 if (l_iter->f != f) {
189 ERRMSG("face %d: has loop that points to face: %d at corner: %d",
190 i,
191 BM_elem_index_get(l_iter->f),
192 j);
193 }
194 if (l_iter != l_iter->prev->next) {
195 ERRMSG("face %d: has invalid 'prev/next' at corner: %d", i, j);
196 }
197 if (l_iter != l_iter->next->prev) {
198 ERRMSG("face %d: has invalid 'next/prev' at corner: %d", i, j);
199 }
200 if (l_iter != l_iter->radial_prev->radial_next) {
201 ERRMSG("face %d: has invalid 'radial_prev/radial_next' at corner: %d", i, j);
202 }
203 if (l_iter != l_iter->radial_next->radial_prev) {
204 ERRMSG("face %d: has invalid 'radial_next/radial_prev' at corner: %d", i, j);
205 }
206
210
211 if (BM_elem_index_get(l_iter->v) < BM_elem_index_get(l_vert_min->v)) {
212 l_vert_min = l_iter;
213 }
214 j++;
215 } while ((l_iter = l_iter->next) != l_first);
216
217 /* Store ordered face verts, walking over the lowest index first
218 * so faces with flipped winding still match. */
219 Vector<int> face_verts;
220 face_verts.reserve(f_len);
221 if (BM_elem_index_get(l_vert_min->next->v) < BM_elem_index_get(l_vert_min->prev->v)) {
222 l_iter = l_vert_min;
223 do {
224 face_verts.append_unchecked(BM_elem_index_get(l_iter->v));
225 } while ((l_iter = l_iter->next) != l_vert_min);
226 }
227 else {
228 l_iter = l_vert_min;
229 do {
230 face_verts.append_unchecked(BM_elem_index_get(l_iter->v));
231 } while ((l_iter = l_iter->prev) != l_vert_min);
232 }
233
234 face_map.add_or_modify(
235 std::move(face_verts),
236 [&](int *value) { *value = i; },
237 [&](const int *value) { ERRMSG("face %d: duplicate of %d", i, *value); });
238
239 /* leave elements un-tagged, not essential but nice to avoid unintended dirty tag use later. */
240 do {
244 } while ((l_iter = l_iter->next) != l_first);
245 }
246
247 const bool is_valid = (errtot == 0);
248 ERRMSG("Finished - errors %d", errtot);
249 return is_valid;
250}
251
252} // namespace blender
253
254#endif
#define BM_ALL
#define BM_FACE_FIRST_LOOP(p)
#define BM_elem_index_get(ele)
#define BM_elem_flag_disable(ele, hflag)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
#define ERRMSG(format,...)
void reserve(int64_t n)
Definition BLI_map.hh:1028
auto add_or_modify(const Key &key, const CreateValueF &create_value, const ModifyValueF &modify_value) -> decltype(create_value(nullptr))
Definition BLI_map.hh:481
void append_unchecked(const T &value)
void reserve(const int64_t min_capacity)
BMesh Iterators.
unsigned __int64 uint64_t
Definition BKE_text.h:91
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
ATTR_WARN_UNUSED_RESULT const BMVert * v
bool BM_mesh_is_valid(BMesh *bm)
BLI_INLINE bool BM_vert_in_edge(const BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
@ BM_ELEM_INTERNAL_TAG
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
#define hash
Definition noise_c.cc:156
struct BMLoop * next
struct BMLoop * prev
struct BMLoop * radial_prev
struct BMEdge * e
struct BMVert * v
struct BMLoop * radial_next
struct BMFace * f
uint64_t operator()(const Vector< int > &value) const
struct BMLoop * radial_next
struct BMLoop * radial_prev