Blender V5.0
mesh_validate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <climits>
11#include <cstring>
12
13#include "CLG_log.h"
14
15#include "DNA_mesh_types.h"
16#include "DNA_meshdata_types.h"
17
18#include "BLI_map.hh"
19#include "BLI_math_base.h"
20#include "BLI_math_vector.h"
21#include "BLI_ordered_edge.hh"
22#include "BLI_sort.hh"
23#include "BLI_sys_types.h"
24#include "BLI_utildefines.h"
25
26#include "BKE_attribute.hh"
27#include "BKE_customdata.hh"
28#include "BKE_deform.hh"
29#include "BKE_mesh.hh"
30#include "BKE_mesh_runtime.hh"
31
32#include "DEG_depsgraph.hh"
33
34#include "MEM_guardedalloc.h"
35
36using blender::float3;
38using blender::Span;
39
40/* corner v/e are unsigned, so using max uint_32 value as invalid marker... */
41#define INVALID_CORNER_EDGE_MARKER 4294967295u
42
43static CLG_LogRef LOG = {"geom.mesh"};
44
45void strip_loose_faces_corners(Mesh *mesh, blender::BitSpan faces_to_remove);
46void mesh_strip_edges(Mesh *mesh);
47
48/* -------------------------------------------------------------------- */
51
52union EdgeUUID {
53 uint32_t verts[2];
55};
56
61
62/* Used to detect faces using exactly the same vertices. */
63/* Used to detect corners used by no (disjoint) or more than one (intersect) faces. */
64struct SortFace {
65 int *verts = nullptr;
66 int numverts = 0;
67 int corner_start = 0;
69 bool invalid = false;
70};
71
72static void edge_store_assign(uint32_t verts[2], const uint32_t v1, const uint32_t v2)
73{
74 if (v1 < v2) {
75 verts[0] = v1;
76 verts[1] = v2;
77 }
78 else {
79 verts[0] = v2;
80 verts[1] = v1;
81 }
82}
83
84static void edge_store_from_mface_quad(EdgeUUID es[4], const MFace *mf)
85{
86 edge_store_assign(es[0].verts, mf->v1, mf->v2);
87 edge_store_assign(es[1].verts, mf->v2, mf->v3);
88 edge_store_assign(es[2].verts, mf->v3, mf->v4);
89 edge_store_assign(es[3].verts, mf->v4, mf->v1);
90}
91
92static void edge_store_from_mface_tri(EdgeUUID es[4], const MFace *mf)
93{
94 edge_store_assign(es[0].verts, mf->v1, mf->v2);
95 edge_store_assign(es[1].verts, mf->v2, mf->v3);
96 edge_store_assign(es[2].verts, mf->v3, mf->v1);
97 es[3].verts[0] = es[3].verts[1] = UINT_MAX;
98}
99
100static bool search_legacy_face_cmp(const SortFaceLegacy &sfa, const SortFaceLegacy &sfb)
101{
102 if (sfa.es[0].edval != sfb.es[0].edval) {
103 return sfa.es[0].edval < sfb.es[0].edval;
104 }
105 if (sfa.es[1].edval != sfb.es[1].edval) {
106 return sfa.es[1].edval < sfb.es[1].edval;
107 }
108 if (sfa.es[2].edval != sfb.es[2].edval) {
109 return sfa.es[2].edval < sfb.es[2].edval;
110 }
111 return sfa.es[3].edval < sfb.es[3].edval;
112}
113
114static bool search_face_cmp(const SortFace &sp1, const SortFace &sp2)
115{
116 /* Reject all invalid faces at end of list! */
117 if (sp1.invalid || sp2.invalid) {
118 return sp1.invalid < sp2.invalid;
119 }
120 /* Else, sort on first non-equal verts (remember verts of valid faces are sorted). */
121 const int max_idx = std::min(sp1.numverts, sp2.numverts);
122 for (int idx = 0; idx < max_idx; idx++) {
123 const int v1_i = sp1.verts[idx];
124 const int v2_i = sp2.verts[idx];
125 if (v1_i != v2_i) {
126 return v1_i < v2_i;
127 }
128 }
129 return sp1.numverts < sp2.numverts;
130}
131
132static bool search_face_corner_cmp(const SortFace &sp1, const SortFace &sp2)
133{
134 /* Reject all invalid faces at end of list! */
135 if (sp1.invalid || sp2.invalid) {
136 return sp1.invalid < sp2.invalid;
137 }
138 /* Else, sort on corner start. */
139 return sp1.corner_start < sp2.corner_start;
140}
141
143
144/* -------------------------------------------------------------------- */
147
148#define PRINT_MSG(...) \
149 if (do_verbose) { \
150 CLOG_INFO(&LOG, __VA_ARGS__); \
151 } \
152 ((void)0)
153
154#define PRINT_ERR(...) \
155 do { \
156 is_valid = false; \
157 if (do_verbose) { \
158 CLOG_ERROR(&LOG, __VA_ARGS__); \
159 } \
160 } while (0)
161
162/* NOLINTNEXTLINE: readability-function-size */
164 float (*vert_positions)[3],
165 uint verts_num,
166 blender::int2 *edges,
167 uint edges_num,
168 MFace *legacy_faces,
169 uint legacy_faces_num,
170 const int *corner_verts,
171 int *corner_edges,
172 uint corners_num,
173 const int *face_offsets,
174 uint faces_num,
175 MDeformVert *dverts, /* assume verts_num length */
176 const bool do_verbose,
177 const bool do_fixes,
178 bool *r_changed)
179{
180 using namespace blender;
181 using namespace blender::bke;
182#define REMOVE_EDGE_TAG(_me) \
183 { \
184 _me[0] = _me[1]; \
185 free_flag.edges = do_fixes; \
186 } \
187 (void)0
188#define IS_REMOVED_EDGE(_me) (_me[0] == _me[1])
189
190#define REMOVE_CORNER_TAG(corner) \
191 { \
192 corner_edges[corner] = INVALID_CORNER_EDGE_MARKER; \
193 free_flag.face_corners = do_fixes; \
194 } \
195 (void)0
196 blender::BitVector<> faces_to_remove(faces_num);
197
198 blender::bke::AttributeWriter<int> material_indices =
199 mesh->attributes_for_write().lookup_for_write<int>("material_index");
200 blender::MutableVArraySpan<int> material_indices_span(material_indices.varray);
201
202 uint i, j;
203 int *v;
204
205 bool is_valid = true;
206
207 union {
208 struct {
209 int verts : 1;
210 int verts_weight : 1;
211 int corners_edge : 1;
212 };
213 int as_flag;
214 } fix_flag;
215
216 union {
217 struct {
218 int edges : 1;
219 int faces : 1;
220 /* This regroups corners and faces! */
221 int face_corners : 1;
222 int mselect : 1;
223 };
224 int as_flag;
225 } free_flag;
226
227 union {
228 struct {
229 int edges : 1;
230 };
231 int as_flag;
232 } recalc_flag;
233
234 Map<OrderedEdge, int> edge_hash;
235 edge_hash.reserve(edges_num);
236
237 BLI_assert(!(do_fixes && mesh == nullptr));
238
239 fix_flag.as_flag = 0;
240 free_flag.as_flag = 0;
241 recalc_flag.as_flag = 0;
242
243 PRINT_MSG("verts(%u), edges(%u), corners(%u), faces(%u)",
244 verts_num,
245 edges_num,
246 corners_num,
247 faces_num);
248
249 if (edges_num == 0 && faces_num != 0) {
250 PRINT_ERR("\tLogical error, %u faces and 0 edges", faces_num);
251 recalc_flag.edges = do_fixes;
252 }
253
254 for (i = 0; i < verts_num; i++) {
255 for (j = 0; j < 3; j++) {
256 if (!isfinite(vert_positions[i][j])) {
257 PRINT_ERR("\tVertex %u: has invalid coordinate", i);
258
259 if (do_fixes) {
260 zero_v3(vert_positions[i]);
261
262 fix_flag.verts = true;
263 }
264 }
265 }
266 }
267
268 for (i = 0; i < edges_num; i++) {
269 blender::int2 &edge = edges[i];
270 bool remove = false;
271
272 if (edge[0] == edge[1]) {
273 PRINT_ERR("\tEdge %u: has matching verts, both %d", i, edge[0]);
274 remove = do_fixes;
275 }
276 if (edge[0] >= verts_num) {
277 PRINT_ERR("\tEdge %u: v1 index out of range, %d", i, edge[0]);
278 remove = do_fixes;
279 }
280 if (edge[1] >= verts_num) {
281 PRINT_ERR("\tEdge %u: v2 index out of range, %d", i, edge[1]);
282 remove = do_fixes;
283 }
284
285 if ((edge[0] != edge[1]) && edge_hash.contains(edge)) {
286 PRINT_ERR("\tEdge %u: is a duplicate of %d", i, edge_hash.lookup(edge));
287 remove = do_fixes;
288 }
289
290 if (remove == false) {
291 if (edge[0] != edge[1]) {
292 edge_hash.add(edge, i);
293 }
294 }
295 else {
296 REMOVE_EDGE_TAG(edge);
297 }
298 }
299
300 if (legacy_faces && !face_offsets) {
301#define REMOVE_FACE_TAG(_mf) \
302 { \
303 _mf->v3 = 0; \
304 free_flag.faces = do_fixes; \
305 } \
306 (void)0
307#define CHECK_FACE_VERT_INDEX(a, b) \
308 if (mf->a == mf->b) { \
309 PRINT_ERR(" face %u: verts invalid, " STRINGIFY(a) "/" STRINGIFY(b) " both %u", i, mf->a); \
310 remove = do_fixes; \
311 } \
312 (void)0
313#define CHECK_FACE_EDGE(a, b) \
314 if (!edge_hash.contains({mf->a, mf->b})) { \
315 PRINT_ERR(" face %u: edge " STRINGIFY(a) "/" STRINGIFY(b) " (%u,%u) is missing edge data", \
316 i, \
317 mf->a, \
318 mf->b); \
319 recalc_flag.edges = do_fixes; \
320 } \
321 (void)0
322
323 MFace *mf;
324 const MFace *mf_prev;
325
326 Array<SortFaceLegacy> sort_faces(legacy_faces_num);
327 SortFaceLegacy *sf;
328 SortFaceLegacy *sf_prev;
329 uint totsortface = 0;
330
331 PRINT_ERR("No faces, only tessellated Faces");
332
333 for (i = 0, mf = legacy_faces, sf = sort_faces.data(); i < legacy_faces_num; i++, mf++) {
334 bool remove = false;
335 int fidx;
336 uint fv[4];
337
338 fidx = mf->v4 ? 3 : 2;
339 do {
340 fv[fidx] = *(&(mf->v1) + fidx);
341 if (fv[fidx] >= verts_num) {
342 PRINT_ERR("\tFace %u: 'v%d' index out of range, %u", i, fidx + 1, fv[fidx]);
343 remove = do_fixes;
344 }
345 } while (fidx--);
346
347 if (remove == false) {
348 if (mf->v4) {
350 CHECK_FACE_VERT_INDEX(v1, v3);
351 CHECK_FACE_VERT_INDEX(v1, v4);
352
355
356 CHECK_FACE_VERT_INDEX(v3, v4);
357 }
358 else {
360 CHECK_FACE_VERT_INDEX(v1, v3);
361
363 }
364
365 if (remove == false) {
366 if (edges_num) {
367 if (mf->v4) {
368 CHECK_FACE_EDGE(v1, v2);
369 CHECK_FACE_EDGE(v2, v3);
370 CHECK_FACE_EDGE(v3, v4);
371 CHECK_FACE_EDGE(v4, v1);
372 }
373 else {
374 CHECK_FACE_EDGE(v1, v2);
375 CHECK_FACE_EDGE(v2, v3);
376 CHECK_FACE_EDGE(v3, v1);
377 }
378 }
379
380 sf->index = i;
381
382 if (mf->v4) {
384 std::sort(sf->es, sf->es + 4, [](const EdgeUUID &a, const EdgeUUID &b) {
385 return a.edval < b.edval;
386 });
387 }
388 else {
390 std::sort(sf->es, sf->es + 3, [](const EdgeUUID &a, const EdgeUUID &b) {
391 return a.edval < b.edval;
392 });
393 }
394
395 totsortface++;
396 sf++;
397 }
398 }
399
400 if (remove) {
401 REMOVE_FACE_TAG(mf);
402 }
403 }
404
405 blender::parallel_sort(sort_faces.begin(), sort_faces.end(), search_legacy_face_cmp);
406
407 sf = sort_faces.data();
408 sf_prev = sf;
409 sf++;
410
411 for (i = 1; i < totsortface; i++, sf++) {
412 bool remove = false;
413
414 /* on a valid mesh, code below will never run */
415 if (memcmp(sf->es, sf_prev->es, sizeof(sf_prev->es)) == 0) {
416 mf = legacy_faces + sf->index;
417
418 if (do_verbose) {
419 mf_prev = legacy_faces + sf_prev->index;
420
421 if (mf->v4) {
422 PRINT_ERR("\tFace %u & %u: are duplicates (%u,%u,%u,%u) (%u,%u,%u,%u)",
423 sf->index,
424 sf_prev->index,
425 mf->v1,
426 mf->v2,
427 mf->v3,
428 mf->v4,
429 mf_prev->v1,
430 mf_prev->v2,
431 mf_prev->v3,
432 mf_prev->v4);
433 }
434 else {
435 PRINT_ERR("\tFace %u & %u: are duplicates (%u,%u,%u) (%u,%u,%u)",
436 sf->index,
437 sf_prev->index,
438 mf->v1,
439 mf->v2,
440 mf->v3,
441 mf_prev->v1,
442 mf_prev->v2,
443 mf_prev->v3);
444 }
445 }
446
447 remove = do_fixes;
448 }
449 else {
450 sf_prev = sf;
451 }
452
453 if (remove) {
454 REMOVE_FACE_TAG(mf);
455 }
456 }
457
458#undef REMOVE_FACE_TAG
459#undef CHECK_FACE_VERT_INDEX
460#undef CHECK_FACE_EDGE
461 }
462
463 /* Checking corners and faces is a bit tricky, as they are quite intricate...
464 *
465 * Faces must have:
466 * - a valid corner_start value.
467 * - a valid corners_num value (>= 3 and corner_start+corners_num < mesh.corners_num).
468 *
469 * corners must have:
470 * - a valid v value.
471 * - a valid e value (corresponding to the edge it defines with the next corner in face).
472 *
473 * Also, corners not used by faces can be discarded.
474 * And "intersecting" corners (i.e. corners used by more than one face) are invalid,
475 * so be sure to leave at most one face per corner!
476 */
477 {
478 BitVector<> vert_tag(mesh->verts_num);
479 Array<SortFace> sort_faces(faces_num);
480 Array<int> sort_face_verts(faces_num == 0 ? 0 : face_offsets[faces_num]);
481 int64_t sort_face_verts_offset = 0;
482
483 for (const int64_t i : blender::IndexRange(faces_num)) {
484 SortFace *sp = &sort_faces[i];
485 const int face_start = face_offsets[i];
486 const int face_size = face_offsets[i + 1] - face_start;
487 sp->index = i;
488
489 /* Material index, isolated from other tests here. While large indices are clamped,
490 * negative indices aren't supported by drawing, exporters etc.
491 * To check the indices are in range, use #BKE_mesh_validate_material_indices */
492 if (material_indices && material_indices_span[i] < 0) {
493 PRINT_ERR("\tFace %u has invalid material (%d)", sp->index, material_indices_span[i]);
494 if (do_fixes) {
495 material_indices_span[i] = 0;
496 }
497 }
498
499 if (face_start < 0 || face_size < 3) {
500 /* Invalid corner data. */
501 PRINT_ERR("\tFace %u is invalid (corner_start: %d, corners_num: %d)",
502 sp->index,
503 face_start,
504 face_size);
505 sp->invalid = true;
506 }
507 else if (face_start + face_size > corners_num) {
508 /* Invalid corner data. */
509 PRINT_ERR(
510 "\tFace %u uses corners out of range "
511 "(corner_start: %d, corner_end: %d, max number of corners: %u)",
512 sp->index,
513 face_start,
514 face_start + face_size - 1,
515 corners_num - 1);
516 sp->invalid = true;
517 }
518 else {
519 /* Face itself is valid, for now. */
520 int v1, v2; /* v1 is prev corner vert idx, v2 is current corner one. */
521 sp->invalid = false;
522 sp->verts = v = sort_face_verts.data() + sort_face_verts_offset;
523 sort_face_verts_offset += face_size;
524 sp->numverts = face_size;
525 sp->corner_start = face_start;
526
527 /* Ideally we would only have to do that once on all vertices
528 * before we start checking each face, but several faces can use same vert,
529 * so we have to ensure here all verts of current face are cleared. */
530 for (j = 0; j < face_size; j++) {
531 const int vert = corner_verts[sp->corner_start + j];
532 if (vert < verts_num) {
533 vert_tag[vert].reset();
534 }
535 }
536
537 /* Test all face's corners' vert idx. */
538 for (j = 0; j < face_size; j++, v++) {
539 const int vert = corner_verts[sp->corner_start + j];
540 if (vert >= verts_num) {
541 /* Invalid vert idx. */
542 PRINT_ERR("\tCorner %u has invalid vert reference (%d)", sp->corner_start + j, vert);
543 sp->invalid = true;
544 }
545 else if (vert_tag[vert].test()) {
546 PRINT_ERR("\tFace %u has duplicated vert reference at corner (%u)", uint(i), j);
547 sp->invalid = true;
548 }
549 else {
550 vert_tag[vert].set();
551 }
552 *v = vert;
553 }
554
555 if (sp->invalid) {
556 sp++;
557 continue;
558 }
559
560 /* Test all face's corners. */
561 for (j = 0; j < face_size; j++) {
562 const int corner = sp->corner_start + j;
563 const int vert = corner_verts[corner];
564 const int edge_i = corner_edges[corner];
565 v1 = vert;
566 v2 = corner_verts[sp->corner_start + (j + 1) % face_size];
567 if (!edge_hash.contains({v1, v2})) {
568 /* Edge not existing. */
569 PRINT_ERR("\tFace %u needs missing edge (%d, %d)", sp->index, v1, v2);
570 if (do_fixes) {
571 recalc_flag.edges = true;
572 }
573 else {
574 sp->invalid = true;
575 }
576 }
577 else if (edge_i >= edges_num) {
578 /* Invalid edge idx.
579 * We already know from previous text that a valid edge exists, use it (if allowed)! */
580 if (do_fixes) {
581 int prev_e = edge_i;
582 corner_edges[corner] = edge_hash.lookup({v1, v2});
583 fix_flag.corners_edge = true;
584 PRINT_ERR("\tCorner %d has invalid edge reference (%d), fixed using edge %d",
585 corner,
586 prev_e,
587 corner_edges[corner]);
588 }
589 else {
590 PRINT_ERR("\tCorner %d has invalid edge reference (%d)", corner, edge_i);
591 sp->invalid = true;
592 }
593 }
594 else {
595 const blender::int2 &edge = edges[edge_i];
596 if (IS_REMOVED_EDGE(edge) ||
597 !((edge[0] == v1 && edge[1] == v2) || (edge[0] == v2 && edge[1] == v1)))
598 {
599 /* The pointed edge is invalid (tagged as removed, or vert idx mismatch),
600 * and we already know from previous test that a valid one exists,
601 * use it (if allowed)! */
602 if (do_fixes) {
603 int prev_e = edge_i;
604 corner_edges[corner] = edge_hash.lookup({v1, v2});
605 fix_flag.corners_edge = true;
606 PRINT_ERR(
607 "\tFace %u has invalid edge reference (%d, is_removed: %d), fixed using edge "
608 "%d",
609 sp->index,
610 prev_e,
611 IS_REMOVED_EDGE(edge),
612 corner_edges[corner]);
613 }
614 else {
615 PRINT_ERR("\tFace %u has invalid edge reference (%d)", sp->index, edge_i);
616 sp->invalid = true;
617 }
618 }
619 }
620 }
621
622 if (!sp->invalid) {
623 /* Needed for checking faces using same verts below. */
624 std::sort(sp->verts, sp->verts + sp->numverts);
625 }
626 }
627 sp++;
628 }
629 BLI_assert(sort_face_verts_offset <= sort_face_verts.size());
630
631 vert_tag.clear_and_shrink();
632
633 /* Second check pass, testing faces using the same verts. */
634 blender::parallel_sort(sort_faces.begin(), sort_faces.end(), search_face_cmp);
635 SortFace *sp, *prev_sp;
636 sp = prev_sp = sort_faces.data();
637 sp++;
638
639 for (i = 1; i < faces_num; i++, sp++) {
640 int p1_nv = sp->numverts, p2_nv = prev_sp->numverts;
641 const int *p1_v = sp->verts, *p2_v = prev_sp->verts;
642
643 if (sp->invalid) {
644 /* Break, because all known invalid faces have been put at the end by the sort above. */
645 break;
646 }
647
648 /* Test same faces. */
649 if ((p1_nv == p2_nv) && (memcmp(p1_v, p2_v, p1_nv * sizeof(*p1_v)) == 0)) {
650 if (do_verbose) {
651 /* TODO: convert list to string */
652 PRINT_ERR("\tFaces %u and %u use same vertices (%d", prev_sp->index, sp->index, *p1_v);
653 for (j = 1; j < p1_nv; j++) {
654 PRINT_ERR(", %d", p1_v[j]);
655 }
656 PRINT_ERR("), considering face %u as invalid.", sp->index);
657 }
658 else {
659 is_valid = false;
660 }
661 sp->invalid = true;
662 }
663 else {
664 prev_sp = sp;
665 }
666 }
667
668 /* Third check pass, testing corners used by none or more than one face. */
669 blender::parallel_sort(sort_faces.begin(), sort_faces.end(), search_face_corner_cmp);
670 sp = sort_faces.data();
671 prev_sp = nullptr;
672 int prev_end = 0;
673 for (i = 0; i < faces_num; i++, sp++) {
674 /* We don't need the verts anymore, and avoid us another corner! */
675 sp->verts = nullptr;
676
677 /* Note above prev_sp: in following code, we make sure it is always valid face (or nullptr).
678 */
679 if (sp->invalid) {
680 if (do_fixes) {
681 faces_to_remove[sp->index].set();
682 free_flag.face_corners = do_fixes;
683 /* DO NOT REMOVE ITS corners!!!
684 * As already invalid faces are at the end of the SortFace list, the corners they
685 * were the only users have already been tagged as "to remove" during previous
686 * iterations, and we don't want to remove some corners that may be used by
687 * another valid face! */
688 }
689 }
690 /* Test corners users. */
691 else {
692 /* Unused corners. */
693 if (prev_end < sp->corner_start) {
694 int corner;
695 for (j = prev_end, corner = prev_end; j < sp->corner_start; j++, corner++) {
696 PRINT_ERR("\tCorner %u is unused.", j);
697 if (do_fixes) {
698 REMOVE_CORNER_TAG(corner);
699 }
700 }
701 prev_end = sp->corner_start + sp->numverts;
702 prev_sp = sp;
703 }
704 /* Multi-used corners. */
705 else if (prev_end > sp->corner_start) {
706 PRINT_ERR(
707 "\tFaces %u and %u share corners from %d to %d, considering face %u as invalid.",
708 prev_sp->index,
709 sp->index,
710 sp->corner_start,
711 prev_end,
712 sp->index);
713 if (do_fixes) {
714 faces_to_remove[sp->index].set();
715 free_flag.face_corners = do_fixes;
716 /* DO NOT REMOVE ITS corners!!!
717 * They might be used by some next, valid face!
718 * Just not updating prev_end/prev_sp vars is enough to ensure the corners
719 * effectively no more needed will be marked as "to be removed"! */
720 }
721 }
722 else {
723 prev_end = sp->corner_start + sp->numverts;
724 prev_sp = sp;
725 }
726 }
727 }
728 /* We may have some remaining unused corners to get rid of! */
729 if (prev_end < corners_num) {
730 int corner;
731 for (j = prev_end, corner = prev_end; j < corners_num; j++, corner++) {
732 PRINT_ERR("\tCorner %u is unused.", j);
733 if (do_fixes) {
734 REMOVE_CORNER_TAG(corner);
735 }
736 }
737 }
738 }
739
740 /* fix deform verts */
741 if (dverts) {
742 MDeformVert *dv;
743 for (i = 0, dv = dverts; i < verts_num; i++, dv++) {
744 MDeformWeight *dw;
745
746 for (j = 0, dw = dv->dw; j < dv->totweight; j++, dw++) {
747 /* NOTE: greater than max defgroups is accounted for in our code, but not < 0. */
748 if (!isfinite(dw->weight)) {
749 PRINT_ERR("\tVertex deform %u, group %u has weight: %f", i, dw->def_nr, dw->weight);
750 if (do_fixes) {
751 dw->weight = 0.0f;
752 fix_flag.verts_weight = true;
753 }
754 }
755 else if (dw->weight < 0.0f || dw->weight > 1.0f) {
756 PRINT_ERR("\tVertex deform %u, group %u has weight: %f", i, dw->def_nr, dw->weight);
757 if (do_fixes) {
758 CLAMP(dw->weight, 0.0f, 1.0f);
759 fix_flag.verts_weight = true;
760 }
761 }
762
763 /* Not technically incorrect since this is unsigned, however,
764 * a value over INT_MAX is almost certainly caused by wrapping an uint. */
765 if (dw->def_nr >= INT_MAX) {
766 PRINT_ERR("\tVertex deform %u, has invalid group %u", i, dw->def_nr);
767 if (do_fixes) {
769 fix_flag.verts_weight = true;
770
771 if (dv->dw) {
772 /* re-allocated, the new values compensate for stepping
773 * within the for corner and may not be valid */
774 j--;
775 dw = dv->dw + j;
776 }
777 else { /* all freed */
778 break;
779 }
780 }
781 }
782 }
783 }
784 }
785
786#undef REMOVE_EDGE_TAG
787#undef IS_REMOVED_EDGE
788#undef REMOVE_CORNER_TAG
789#undef REMOVE_FACE_TAG
790
791 if (mesh) {
792 if (free_flag.faces) {
794 }
795
796 if (free_flag.face_corners) {
797 strip_loose_faces_corners(mesh, faces_to_remove);
798 }
799
800 if (free_flag.edges) {
802 }
803
804 if (recalc_flag.edges) {
805 mesh_calc_edges(*mesh, true, false);
806 }
807 }
808
809 if (mesh && mesh->mselect) {
810 MSelect *msel;
811
812 for (i = 0, msel = mesh->mselect; i < mesh->totselect; i++, msel++) {
813 int tot_elem = 0;
814
815 if (msel->index < 0) {
816 PRINT_ERR(
817 "\tMesh select element %u type %d index is negative, "
818 "resetting selection stack.\n",
819 i,
820 msel->type);
821 free_flag.mselect = do_fixes;
822 break;
823 }
824
825 switch (msel->type) {
826 case ME_VSEL:
827 tot_elem = mesh->verts_num;
828 break;
829 case ME_ESEL:
830 tot_elem = mesh->edges_num;
831 break;
832 case ME_FSEL:
833 tot_elem = mesh->faces_num;
834 break;
835 }
836
837 if (msel->index > tot_elem) {
838 PRINT_ERR(
839 "\tMesh select element %u type %d index %d is larger than data array size %d, "
840 "resetting selection stack.\n",
841 i,
842 msel->type,
843 msel->index,
844 tot_elem);
845
846 free_flag.mselect = do_fixes;
847 break;
848 }
849 }
850
851 if (free_flag.mselect) {
852 MEM_freeN(mesh->mselect);
853 mesh->mselect = nullptr;
854 mesh->totselect = 0;
855 }
856 }
857
858 material_indices_span.save();
859 material_indices.finish();
860
861 PRINT_MSG("%s: finished\n\n", __func__);
862
863 *r_changed = (fix_flag.as_flag || free_flag.as_flag || recalc_flag.as_flag);
864
865 BLI_assert((*r_changed == false) || (do_fixes == true));
866
867 return is_valid;
868}
869
871 eCustomDataMask mask,
872 const uint totitems,
873 const bool do_verbose,
874 const bool do_fixes,
875 bool *r_change)
876{
877 bool is_valid = true;
878 bool has_fixes = false;
879 int i = 0;
880
881 PRINT_MSG("%s: Checking %d CD layers...\n", __func__, data->totlayer);
882
883 /* Set dummy values so the layer-type is always initialized on first access. */
884 int layer_num = -1;
885 int layer_num_type = -1;
886
887 while (i < data->totlayer) {
888 CustomDataLayer *layer = &data->layers[i];
889 const eCustomDataType type = eCustomDataType(layer->type);
890 bool ok = true;
891
892 /* Count layers when the type changes. */
893 if (layer_num_type != type) {
894 layer_num = CustomData_number_of_layers(data, type);
895 layer_num_type = type;
896 }
897
898 /* Validate active index, for a time this could be set to a negative value, see: #105860. */
899 int *active_index_array[] = {
900 &layer->active,
901 &layer->active_rnd,
902 &layer->active_clone,
903 &layer->active_mask,
904 };
905 for (int *active_index : Span(active_index_array, ARRAY_SIZE(active_index_array))) {
906 if (*active_index < 0) {
907 PRINT_ERR("\tCustomDataLayer type %d has a negative active index (%d)\n",
908 layer->type,
909 *active_index);
910 if (do_fixes) {
911 *active_index = 0;
912 has_fixes = true;
913 }
914 }
915 else {
916 if (*active_index >= layer_num) {
917 PRINT_ERR("\tCustomDataLayer type %d has an out of bounds active index (%d >= %d)\n",
918 layer->type,
919 *active_index,
920 layer_num);
921 if (do_fixes) {
922 BLI_assert(layer_num > 0);
923 *active_index = layer_num - 1;
924 has_fixes = true;
925 }
926 }
927 }
928 }
929
931 if (layer_num > 1) {
932 PRINT_ERR("\tCustomDataLayer type %d is a singleton, found %d in Mesh structure\n",
933 type,
934 layer_num);
935 ok = false;
936 }
937 }
938
939 if (mask != 0) {
940 eCustomDataMask layer_typemask = CD_TYPE_AS_MASK(type);
941 if ((layer_typemask & mask) == 0) {
942 PRINT_ERR("\tCustomDataLayer type %d which isn't in the mask\n", type);
943 ok = false;
944 }
945 }
946
947 if (ok == false) {
948 if (do_fixes) {
950 has_fixes = true;
951 }
952 }
953
954 if (ok) {
955 if (CustomData_layer_validate(layer, totitems, do_fixes)) {
956 PRINT_ERR("\tCustomDataLayer type %d has some invalid data\n", type);
957 has_fixes = do_fixes;
958 }
959 i++;
960 }
961 }
962
963 PRINT_MSG("%s: Finished (is_valid=%d)\n\n", __func__, int(!has_fixes));
964
965 *r_change = has_fixes;
966
967 return is_valid;
968}
969
971 const uint verts_num,
972 CustomData *edge_data,
973 const uint edges_num,
974 CustomData *corner_data,
975 const uint corners_num,
976 CustomData *face_data,
977 const uint faces_num,
978 const bool check_meshmask,
979 const bool do_verbose,
980 const bool do_fixes,
981 bool *r_change)
982{
983 bool is_valid = true;
984 bool is_change_v, is_change_e, is_change_l, is_change_p;
986 if (check_meshmask) {
988 }
989
990 is_valid &= mesh_validate_customdata(
991 vert_data, mask.vmask, verts_num, do_verbose, do_fixes, &is_change_v);
992 is_valid &= mesh_validate_customdata(
993 edge_data, mask.emask, edges_num, do_verbose, do_fixes, &is_change_e);
994 is_valid &= mesh_validate_customdata(
995 corner_data, mask.lmask, corners_num, do_verbose, do_fixes, &is_change_l);
996 is_valid &= mesh_validate_customdata(
997 face_data, mask.pmask, faces_num, do_verbose, do_fixes, &is_change_p);
998
999 const int uv_maps_num = CustomData_number_of_layers(corner_data, CD_PROP_FLOAT2);
1000 if (uv_maps_num > MAX_MTFACE) {
1001 PRINT_ERR(
1002 "\tMore UV layers than %d allowed, %d last ones won't be available for render, shaders, "
1003 "etc.\n",
1004 MAX_MTFACE,
1005 uv_maps_num - MAX_MTFACE);
1006 }
1007
1008 /* check indices of clone/stencil */
1009 if (do_fixes && CustomData_get_clone_layer(corner_data, CD_PROP_FLOAT2) >= uv_maps_num) {
1011 is_change_l = true;
1012 }
1013 if (do_fixes && CustomData_get_stencil_layer(corner_data, CD_PROP_FLOAT2) >= uv_maps_num) {
1015 is_change_l = true;
1016 }
1017
1018 *r_change = (is_change_v || is_change_e || is_change_l || is_change_p);
1019
1020 return is_valid;
1021}
1022
1023bool BKE_mesh_validate(Mesh *mesh, const bool do_verbose, const bool cddata_check_mask)
1024{
1025 bool changed;
1026
1027 if (do_verbose) {
1028 CLOG_INFO(&LOG, "Validating Mesh: %s", mesh->id.name + 2);
1029 }
1030
1032 mesh->verts_num,
1033 &mesh->edge_data,
1034 mesh->edges_num,
1035 &mesh->corner_data,
1036 mesh->corners_num,
1037 &mesh->face_data,
1038 mesh->faces_num,
1039 cddata_check_mask,
1040 do_verbose,
1041 true,
1042 &changed);
1043 MutableSpan<float3> positions = mesh->vert_positions_for_write();
1044 MutableSpan<blender::int2> edges = mesh->edges_for_write();
1045 Span<int> face_offsets = mesh->face_offsets();
1046 Span<int> corner_verts = mesh->corner_verts();
1047 MutableSpan<int> corner_edges = mesh->corner_edges_for_write();
1048
1049 MDeformVert *dverts = static_cast<MDeformVert *>(
1050 CustomData_get_layer_for_write(&mesh->vert_data, CD_MDEFORMVERT, mesh->verts_num));
1052 mesh,
1053 reinterpret_cast<float (*)[3]>(positions.data()),
1054 positions.size(),
1055 edges.data(),
1056 edges.size(),
1057 (MFace *)CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy),
1058 mesh->totface_legacy,
1059 corner_verts.data(),
1060 corner_edges.data(),
1061 corner_verts.size(),
1062 face_offsets.data(),
1063 mesh->faces_num,
1064 dverts,
1065 do_verbose,
1066 true,
1067 &changed);
1068
1069 if (changed) {
1072 return true;
1073 }
1074
1075 return false;
1076}
1077
1079{
1080 const bool do_verbose = true;
1081 const bool do_fixes = false;
1082
1083 bool is_valid = true;
1084 bool changed = true;
1085
1087 &mesh->vert_data,
1088 mesh->verts_num,
1089 &mesh->edge_data,
1090 mesh->edges_num,
1091 &mesh->corner_data,
1092 mesh->corners_num,
1093 &mesh->face_data,
1094 mesh->faces_num,
1095 false, /* setting mask here isn't useful, gives false positives */
1096 do_verbose,
1097 do_fixes,
1098 &changed);
1099
1100 MutableSpan<float3> positions = mesh->vert_positions_for_write();
1101 MutableSpan<blender::int2> edges = mesh->edges_for_write();
1102 Span<int> face_offsets = mesh->face_offsets();
1103 Span<int> corner_verts = mesh->corner_verts();
1104 MutableSpan<int> corner_edges = mesh->corner_edges_for_write();
1105
1106 MDeformVert *dverts = static_cast<MDeformVert *>(
1107 CustomData_get_layer_for_write(&mesh->vert_data, CD_MDEFORMVERT, mesh->verts_num));
1108 is_valid &= BKE_mesh_validate_arrays(
1109 mesh,
1110 reinterpret_cast<float (*)[3]>(positions.data()),
1111 positions.size(),
1112 edges.data(),
1113 edges.size(),
1114 (MFace *)CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy),
1115 mesh->totface_legacy,
1116 corner_verts.data(),
1117 corner_edges.data(),
1118 corner_verts.size(),
1119 face_offsets.data(),
1120 mesh->faces_num,
1121 dverts,
1122 do_verbose,
1123 do_fixes,
1124 &changed);
1125
1126 BLI_assert(changed == false);
1127
1128 return is_valid;
1129}
1130
1132{
1133 const int mat_nr_max = max_ii(0, mesh->totcol - 1);
1134 bool is_valid = true;
1135
1136 blender::bke::AttributeWriter<int> material_indices =
1137 mesh->attributes_for_write().lookup_for_write<int>("material_index");
1138 blender::MutableVArraySpan<int> material_indices_span(material_indices.varray);
1139 for (const int i : material_indices_span.index_range()) {
1140 if (material_indices_span[i] < 0 || material_indices_span[i] > mat_nr_max) {
1141 material_indices_span[i] = 0;
1142 is_valid = false;
1143 }
1144 }
1145 material_indices_span.save();
1146 material_indices.finish();
1147
1148 if (!is_valid) {
1150 return true;
1151 }
1152
1153 return false;
1154}
1155
1157
1158/* -------------------------------------------------------------------- */
1161
1163{
1164 /* Ensure layers are mutable so that #CustomData_copy_data can be used. */
1165 CustomData_ensure_layers_are_mutable(&mesh->face_data, mesh->faces_num);
1166 CustomData_ensure_layers_are_mutable(&mesh->corner_data, mesh->corners_num);
1167
1168 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
1169 MutableSpan<int> corner_edges = mesh->corner_edges_for_write();
1170
1171 int a, b;
1172 /* New corners idx! */
1173 int *new_idx = MEM_malloc_arrayN<int>(size_t(mesh->corners_num), __func__);
1174
1175 for (a = b = 0; a < mesh->faces_num; a++) {
1176 bool invalid = false;
1177 int start = face_offsets[a];
1178 int size = face_offsets[a + 1] - start;
1179 int stop = start + size;
1180
1181 if (faces_to_remove[a]) {
1182 invalid = true;
1183 }
1184 else if (stop > mesh->corners_num || stop < start || size < 0) {
1185 invalid = true;
1186 }
1187 else {
1188 /* If one of the face's corners is invalid, the whole face is invalid! */
1189 if (corner_edges.slice(start, size).contains(INVALID_CORNER_EDGE_MARKER)) {
1190 invalid = true;
1191 }
1192 }
1193
1194 if (size >= 3 && !invalid) {
1195 if (a != b) {
1196 face_offsets[b] = face_offsets[a];
1197 CustomData_copy_data(&mesh->face_data, &mesh->face_data, a, b, 1);
1198 }
1199 b++;
1200 }
1201 }
1202 if (a != b) {
1203 CustomData_free_elem(&mesh->face_data, b, a - b);
1204 mesh->faces_num = b;
1205 }
1206
1207 /* And now, get rid of invalid corners. */
1208 int corner = 0;
1209 for (a = b = 0; a < mesh->corners_num; a++, corner++) {
1210 if (corner_edges[corner] != INVALID_CORNER_EDGE_MARKER) {
1211 if (a != b) {
1212 CustomData_copy_data(&mesh->corner_data, &mesh->corner_data, a, b, 1);
1213 }
1214 new_idx[a] = b;
1215 b++;
1216 }
1217 else {
1218 /* XXX Theoretically, we should be able to not do this, as no remaining face
1219 * should use any stripped corner. But for security's sake... */
1220 new_idx[a] = -a;
1221 }
1222 }
1223 if (a != b) {
1224 CustomData_free_elem(&mesh->corner_data, b, a - b);
1225 mesh->corners_num = b;
1226 }
1227
1228 face_offsets[mesh->faces_num] = mesh->corners_num;
1229
1230 /* And now, update faces' start corner index. */
1231 /* NOTE: At this point, there should never be any face using a stripped corner! */
1232 for (const int i : blender::IndexRange(mesh->faces_num)) {
1233 face_offsets[i] = new_idx[face_offsets[i]];
1234 BLI_assert(face_offsets[i] >= 0);
1235 }
1236
1237 MEM_freeN(new_idx);
1238}
1239
1241{
1242 /* Ensure layers are mutable so that #CustomData_copy_data can be used. */
1243 CustomData_ensure_layers_are_mutable(&mesh->edge_data, mesh->edges_num);
1244
1246 int a, b;
1247 uint *new_idx = MEM_malloc_arrayN<uint>(size_t(mesh->edges_num), __func__);
1248 MutableSpan<blender::int2> edges = mesh->edges_for_write();
1249
1250 for (a = b = 0, e = edges.data(); a < mesh->edges_num; a++, e++) {
1251 if ((*e)[0] != (*e)[1]) {
1252 if (a != b) {
1253 memcpy(&edges[b], e, sizeof(edges[b]));
1254 CustomData_copy_data(&mesh->edge_data, &mesh->edge_data, a, b, 1);
1255 }
1256 new_idx[a] = b;
1257 b++;
1258 }
1259 else {
1260 new_idx[a] = INVALID_CORNER_EDGE_MARKER;
1261 }
1262 }
1263 if (a != b) {
1264 CustomData_free_elem(&mesh->edge_data, b, a - b);
1265 mesh->edges_num = b;
1266 }
1267
1268 /* And now, update corners' edge indices. */
1269 /* XXX We hope no corner was pointing to a stripped edge!
1270 * Else, its e will be set to INVALID_CORNER_EDGE_MARKER :/ */
1271 MutableSpan<int> corner_edges = mesh->corner_edges_for_write();
1272 for (const int i : corner_edges.index_range()) {
1273 corner_edges[i] = new_idx[corner_edges[i]];
1274 }
1275
1276 MEM_freeN(new_idx);
1277}
1278
CustomData interface, see also DNA_customdata_types.h.
int CustomData_get_clone_layer(const CustomData *data, eCustomDataType type)
eCustomDataMask CD_TYPE_AS_MASK(eCustomDataType type)
bool CustomData_free_layer(CustomData *data, eCustomDataType type, int index)
int CustomData_get_stencil_layer(const CustomData *data, eCustomDataType type)
void CustomData_set_layer_clone(CustomData *data, eCustomDataType type, int n)
bool CustomData_layer_validate(CustomDataLayer *layer, uint totitems, bool do_fixes)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
void CustomData_free_elem(CustomData *data, int index, int count)
void CustomData_copy_data(const CustomData *source, CustomData *dest, int source_index, int dest_index, int count)
void CustomData_ensure_layers_are_mutable(CustomData *data, int totelem)
bool CustomData_layertype_is_singleton(eCustomDataType type)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
const CustomData_MeshMasks CD_MASK_MESH
void CustomData_set_layer_stencil(CustomData *data, eCustomDataType type, int n)
support for deformation groups and hooks.
void BKE_defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
Definition deform.cc:880
void BKE_mesh_strip_loose_faces(Mesh *mesh)
void BKE_mesh_runtime_clear_cache(Mesh *mesh)
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE int max_ii(int a, int b)
MINLINE void zero_v3(float r[3])
unsigned int uint
#define CLAMP(a, b, c)
#define ARRAY_SIZE(arr)
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY_ALL_MODES
Definition DNA_ID.h:1181
@ CD_MDEFORMVERT
@ CD_PROP_FLOAT2
#define MAX_MTFACE
@ ME_VSEL
@ ME_FSEL
@ ME_ESEL
Read Guarded memory(de)allocation.
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
long long int int64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
int64_t size() const
Definition BLI_array.hh:256
const T * end() const
Definition BLI_array.hh:325
const T * data() const
Definition BLI_array.hh:312
const T * begin() const
Definition BLI_array.hh:321
void clear_and_shrink()
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
void reserve(int64_t n)
Definition BLI_map.hh:1028
bool contains(const Key &key) const
Definition BLI_map.hh:353
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr bool contains(const T &value) const
Definition BLI_span.hh:715
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:573
constexpr T * data() const
Definition BLI_span.hh:539
constexpr IndexRange index_range() const
Definition BLI_span.hh:670
constexpr const T * data() const
Definition BLI_span.hh:215
constexpr int64_t size() const
Definition BLI_span.hh:252
static float verts[][3]
#define UINT_MAX
Definition hash_md5.cc:44
#define LOG(level)
Definition log.h:97
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
static char faces[256]
#define CHECK_FACE_EDGE(a, b)
static bool mesh_validate_customdata(CustomData *data, eCustomDataMask mask, const uint totitems, const bool do_verbose, const bool do_fixes, bool *r_change)
static bool search_face_cmp(const SortFace &sp1, const SortFace &sp2)
void mesh_strip_edges(Mesh *mesh)
#define PRINT_ERR(...)
#define IS_REMOVED_EDGE(_me)
bool BKE_mesh_validate_all_customdata(CustomData *vert_data, const uint verts_num, CustomData *edge_data, const uint edges_num, CustomData *corner_data, const uint corners_num, CustomData *face_data, const uint faces_num, const bool check_meshmask, const bool do_verbose, const bool do_fixes, bool *r_change)
bool BKE_mesh_validate_material_indices(Mesh *mesh)
static void edge_store_from_mface_tri(EdgeUUID es[4], const MFace *mf)
static void edge_store_from_mface_quad(EdgeUUID es[4], const MFace *mf)
static bool search_face_corner_cmp(const SortFace &sp1, const SortFace &sp2)
void strip_loose_faces_corners(Mesh *mesh, blender::BitSpan faces_to_remove)
static bool search_legacy_face_cmp(const SortFaceLegacy &sfa, const SortFaceLegacy &sfb)
static void edge_store_assign(uint32_t verts[2], const uint32_t v1, const uint32_t v2)
#define CHECK_FACE_VERT_INDEX(a, b)
bool BKE_mesh_validate_arrays(Mesh *mesh, float(*vert_positions)[3], uint verts_num, blender::int2 *edges, uint edges_num, MFace *legacy_faces, uint legacy_faces_num, const int *corner_verts, int *corner_edges, uint corners_num, const int *face_offsets, uint faces_num, MDeformVert *dverts, const bool do_verbose, const bool do_fixes, bool *r_changed)
#define REMOVE_FACE_TAG(_mf)
#define REMOVE_CORNER_TAG(corner)
#define REMOVE_EDGE_TAG(_me)
bool BKE_mesh_is_valid(Mesh *mesh)
#define INVALID_CORNER_EDGE_MARKER
#define PRINT_MSG(...)
bool BKE_mesh_validate(Mesh *mesh, const bool do_verbose, const bool cddata_check_mask)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
void parallel_sort(RandomAccessIterator begin, RandomAccessIterator end)
Definition BLI_sort.hh:23
VecBase< int32_t, 2 > int2
VecBase< float, 3 > float3
struct MDeformWeight * dw
unsigned int def_nr
unsigned int v2
unsigned int v1
unsigned int v4
unsigned int v3
EdgeUUID es[4]
i
Definition text_draw.cc:230
int64_t edval
uint32_t verts[2]