Skip to content
Navigation Menu
{{ message }}
forked from microsoft/DirectXMesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectXMeshValidate.cpp
More file actions
428 lines (351 loc) · 14.1 KB
/
Copy pathDirectXMeshValidate.cpp
File metadata and controls
428 lines (351 loc) · 14.1 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//-------------------------------------------------------------------------------------
// DirectXMeshValidate.cpp
//
// DirectX Mesh Geometry Library - Mesh validation
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkID=324981
//-------------------------------------------------------------------------------------
#include "DirectXMeshP.h"
using namespace DirectX;
namespace
{
//---------------------------------------------------------------------------------
// Validates indices and optionally the adjacency information
//---------------------------------------------------------------------------------
template<class index_t>
HRESULT ValidateIndices(
_In_reads_(nFaces * 3) const index_t* indices, _In_ size_t nFaces,
_In_ size_t nVerts, _In_reads_opt_(nFaces * 3) const uint32_t* adjacency,
_In_ DWORD flags, _In_opt_ std::wstring* msgs)
{
bool result = true;
if (!adjacency)
{
if (flags & VALIDATE_BACKFACING)
{
if (msgs)
*msgs += L"Missing adjacency information required to check for BACKFACING\n";
result = false;
}
if (flags & VALIDATE_ASYMMETRIC_ADJ)
{
if (msgs)
*msgs += L"Missing adjacency information required to check for ASYMMETRIC_ADJ\n";
result = false;
}
if (!result)
return E_INVALIDARG;
}
for (size_t face = 0; face < nFaces; ++face)
{
// Check for values in-range
for (size_t point = 0; point < 3; ++point)
{
index_t i = indices[face * 3 + point];
if (i >= nVerts && i != index_t(-1))
{
if (!msgs)
return E_FAIL;
result = false;
wchar_t buff[128];
swprintf_s(buff, L"An invalid index value (%u) was found on face %zu\n", i, face);
*msgs += buff;
}
if (adjacency)
{
uint32_t j = adjacency[face * 3 + point];
if (j >= nFaces && j != UNUSED32)
{
if (!msgs)
return E_FAIL;
result = false;
wchar_t buff[128];
swprintf_s(buff, L"An invalid neighbor index value (%u) was found on face %zu\n", j, face);
*msgs += buff;
}
}
}
// Check for unused faces
index_t i0 = indices[face * 3];
index_t i1 = indices[face * 3 + 1];
index_t i2 = indices[face * 3 + 2];
if (i0 == index_t(-1)
|| i1 == index_t(-1)
|| i2 == index_t(-1))
{
if (flags & VALIDATE_UNUSED)
{
if (i0 != i1
|| i0 != i2
|| i1 != i2)
{
if (!msgs)
return E_FAIL;
result = false;
wchar_t buff[128];
swprintf_s(buff, L"An unused face (%zu) contains 'valid' but ignored vertices (%u,%u,%u)\n", face, i0, i1, i2);
*msgs += buff;
}
if (adjacency)
{
for (size_t point = 0; point < 3; ++point)
{
uint32_t k = adjacency[face * 3 + point];
if (k != UNUSED32)
{
if (!msgs)
return E_FAIL;
result = false;
wchar_t buff[128];
swprintf_s(buff, L"An unused face (%zu) has a neighbor %u\n", face, k);
*msgs += buff;
}
}
}
}
// ignore unused triangles for remaining tests
continue;
}
// Check for degenerate triangles
if (i0 == i1
|| i0 == i2
|| i1 == i2)
{
if (flags & VALIDATE_DEGENERATE)
{
if (!msgs)
return E_FAIL;
result = false;
index_t bad;
if (i0 == i1)
bad = i0;
else if (i1 == i2)
bad = i2;
else
bad = i0;
wchar_t buff[128];
swprintf_s(buff, L"A point (%u) was found more than once in triangle %zu\n", bad, face);
*msgs += buff;
if (adjacency)
{
for (size_t point = 0; point < 3; ++point)
{
uint32_t k = adjacency[face * 3 + point];
if (k != UNUSED32)
{
result = false;
swprintf_s(buff, L"A degenerate face (%zu) has a neighbor %u\n", face, k);
*msgs += buff;
}
}
}
}
// ignore degenerate triangles for remaining tests
continue;
}
// Check for symmetric neighbors
if ((flags & VALIDATE_ASYMMETRIC_ADJ) && adjacency)
{
for (size_t point = 0; point < 3; ++point)
{
uint32_t k = adjacency[face * 3 + point];
if (k == UNUSED32)
continue;
assert(k < nFaces);
uint32_t edge = find_edge<uint32_t>(&adjacency[k * 3], uint32_t(face));
if (edge >= 3)
{
if (!msgs)
return E_FAIL;
result = false;
wchar_t buff[256];
swprintf_s(buff, L"A neighbor triangle (%u) does not reference back to this face (%zu) as expected\n", k, face);
*msgs += buff;
}
}
}
// Check for duplicate neighbor
if ((flags & VALIDATE_BACKFACING) && adjacency)
{
uint32_t j0 = adjacency[face * 3];
uint32_t j1 = adjacency[face * 3 + 1];
uint32_t j2 = adjacency[face * 3 + 2];
if ((j0 == j1 && j0 != UNUSED32)
|| (j0 == j2 && j0 != UNUSED32)
|| (j1 == j2 && j1 != UNUSED32))
{
if (!msgs)
return E_FAIL;
result = false;
uint32_t bad;
if (j0 == j1 && j0 != UNUSED32)
bad = j0;
else if (j0 == j2 && j0 != UNUSED32)
bad = j0;
else
bad = j1;
wchar_t buff[256] = {};
swprintf_s(buff, L"A neighbor triangle (%u) was found more than once on triangle %zu\n"
L"\t(likley problem is that two triangles share same points with opposite direction)\n", bad, face);
*msgs += buff;
}
}
}
return result ? S_OK : E_FAIL;
}
//---------------------------------------------------------------------------------
// Validates mesh contains no bowties
// (i.e. a vertex is the apex of two separate triangle fans)
//---------------------------------------------------------------------------------
template<class index_t>
HRESULT ValidateNoBowties(
_In_reads_(nFaces * 3) const index_t* indices, _In_ size_t nFaces,
_In_ size_t nVerts, _In_reads_opt_(nFaces * 3) const uint32_t* adjacency,
_In_opt_ std::wstring* msgs)
{
if (!adjacency)
{
if (msgs)
*msgs += L"Missing adjacency information required to check for BOWTIES\n";
return E_INVALIDARG;
}
size_t tsize = (sizeof(bool) * nFaces * 3) + (sizeof(index_t) * nVerts * 2) + (sizeof(bool) * nVerts);
std::unique_ptr<uint8_t[]> temp(new (std::nothrow) uint8_t[tsize]);
if (!temp)
return E_OUTOFMEMORY;
auto faceSeen = reinterpret_cast<bool*>(temp.get());
auto faceIds = reinterpret_cast<index_t*>(temp.get() + sizeof(bool) * nFaces * 3);
auto faceUsing = reinterpret_cast<index_t*>(reinterpret_cast<uint8_t*>(faceIds) + sizeof(index_t) * nVerts);
auto vertexBowtie = reinterpret_cast<bool*>(reinterpret_cast<uint8_t*>(faceUsing) + sizeof(index_t) * nVerts);
memset(faceSeen, 0, sizeof(bool) * nFaces * 3);
memset(faceIds, 0xFF, sizeof(index_t) * nVerts);
memset(faceUsing, 0, sizeof(index_t) * nVerts);
memset(vertexBowtie, 0, sizeof(bool) * nVerts);
orbit_iterator<index_t> ovi(adjacency, indices, nFaces);
bool result = true;
for (uint32_t face = 0; face < nFaces; ++face)
{
index_t i0 = indices[face * 3];
index_t i1 = indices[face * 3 + 1];
index_t i2 = indices[face * 3 + 2];
if (i0 == i1
|| i0 == i2
|| i1 == i2)
{
// ignore degenerate faces
faceSeen[face * 3] = true;
faceSeen[face * 3 + 1] = true;
faceSeen[face * 3 + 2] = true;
continue;
}
for (uint32_t point = 0; point < 3; ++point)
{
if (faceSeen[face * 3 + point])
continue;
faceSeen[face * 3 + point] = true;
index_t i = indices[face * 3 + point];
ovi.initialize(face, i, orbit_iterator<index_t>::ALL);
ovi.moveToCCW();
while (!ovi.done())
{
uint32_t curFace = ovi.nextFace();
if (curFace >= nFaces)
return E_FAIL;
uint32_t curPoint = ovi.getpoint();
if (curPoint > 2)
return E_FAIL;
faceSeen[curFace * 3 + curPoint] = true;
uint32_t j = indices[curFace * 3 + curPoint];
if (faceIds[j] == index_t(-1))
{
faceIds[j] = index_t(face);
faceUsing[j] = index_t(curFace);
}
else if ((faceIds[j] != index_t(face)) && !vertexBowtie[j])
{
// We found a (unique) bowtie!
if (!msgs)
return E_FAIL;
if (result)
{
// If this is the first bowtie found, add a quick explanation
*msgs += L"A bowtie was found. Bowties can be fixed by calling Clean\n"
L" A bowtie is the usage of a single vertex by two separate fans of triangles.\n"
L" The fix is to duplicate the vertex so that each fan has its own vertex.\n";
result = false;
}
vertexBowtie[j] = true;
wchar_t buff[256] = {};
swprintf_s(buff, L"\nBowtie found around vertex %u shared by faces %u and %u\n", j, curFace, faceUsing[j]);
*msgs += buff;
}
}
}
}
return result ? S_OK : E_FAIL;
}
}
//=====================================================================================
// Entry-points
//=====================================================================================
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::Validate(
const uint16_t* indices,
size_t nFaces,
size_t nVerts,
const uint32_t* adjacency,
DWORD flags,
std::wstring* msgs)
{
if (!indices || !nFaces || !nVerts)
return E_INVALIDARG;
if (nVerts >= UINT16_MAX)
return E_INVALIDARG;
if ((uint64_t(nFaces) * 3) >= UINT32_MAX)
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
if (msgs)
msgs->clear();
HRESULT hr = ValidateIndices<uint16_t>(indices, nFaces, nVerts, adjacency, flags, msgs);
if (FAILED(hr))
return hr;
if (flags & VALIDATE_BOWTIES)
{
hr = ValidateNoBowties<uint16_t>(indices, nFaces, nVerts, adjacency, msgs);
if (FAILED(hr))
return hr;
}
return S_OK;
}
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::Validate(
const uint32_t* indices,
size_t nFaces,
size_t nVerts,
const uint32_t* adjacency,
DWORD flags,
std::wstring* msgs)
{
if (!indices || !nFaces || !nVerts)
return E_INVALIDARG;
if (nVerts >= UINT32_MAX)
return E_INVALIDARG;
if ((uint64_t(nFaces) * 3) >= UINT32_MAX)
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
if (msgs)
msgs->clear();
HRESULT hr = ValidateIndices<uint32_t>(indices, nFaces, nVerts, adjacency, flags, msgs);
if (FAILED(hr))
return hr;
if (flags & VALIDATE_BOWTIES)
{
hr = ValidateNoBowties<uint32_t>(indices, nFaces, nVerts, adjacency, msgs);
if (FAILED(hr))
return hr;
}
return S_OK;
}
You can’t perform that action at this time.
