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 pathDirectXMeshClean.cpp
More file actions
443 lines (368 loc) · 14.8 KB
/
Copy pathDirectXMeshClean.cpp
File metadata and controls
443 lines (368 loc) · 14.8 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//-------------------------------------------------------------------------------------
// DirectXMeshClean.cpp
//
// DirectX Mesh Geometry Library - Mesh clean-up
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkID=324981
//-------------------------------------------------------------------------------------
#include "DirectXMeshP.h"
using namespace DirectX;
namespace
{
template<class index_t>
HRESULT CleanImpl(
_Inout_updates_all_(nFaces * 3) index_t* indices,
size_t nFaces, size_t nVerts,
_Inout_updates_all_opt_(nFaces * 3) uint32_t* adjacency,
_In_reads_opt_(nFaces) const uint32_t* attributes,
_Inout_ std::vector<uint32_t>& dupVerts, bool breakBowties)
{
if (!adjacency && !attributes)
return E_INVALIDARG;
if ((uint64_t(nFaces) * 3) >= UINT32_MAX)
return HRESULT_E_ARITHMETIC_OVERFLOW;
dupVerts.clear();
size_t curNewVert = nVerts;
const size_t tsize = (sizeof(bool) * nFaces * 3) + (sizeof(uint32_t) * nVerts) + (sizeof(index_t) * nFaces * 3);
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 ids = reinterpret_cast<uint32_t*>(temp.get() + sizeof(bool) * nFaces * 3);
// UNUSED/DEGENERATE cleanup
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 == index_t(-1)
|| i1 == index_t(-1)
|| i2 == index_t(-1))
{
// ensure all index entries in the unused face are 'unused'
indices[face * 3] =
indices[face * 3 + 1] =
indices[face * 3 + 2] = index_t(-1);
// ensure no neighbor references the unused face
if (adjacency)
{
for (uint32_t point = 0; point < 3; ++point)
{
uint32_t k = adjacency[face * 3 + point];
if (k != UNUSED32)
{
assert(k < nFaces);
_Analysis_assume_(k < nFaces);
if (adjacency[k * 3] == face)
adjacency[k * 3] = UNUSED32;
if (adjacency[k * 3 + 1] == face)
adjacency[k * 3 + 1] = UNUSED32;
if (adjacency[k * 3 + 2] == face)
adjacency[k * 3 + 2] = UNUSED32;
adjacency[face * 3 + point] = UNUSED32;
}
}
}
}
else if (i0 == i1
|| i0 == i2
|| i1 == i2)
{
// Clean doesn't trim out degenerates as most other functions ignore them
// ensure no neighbor references the degenerate face
if (adjacency)
{
for (uint32_t point = 0; point < 3; ++point)
{
uint32_t k = adjacency[face * 3 + point];
if (k != UNUSED32)
{
assert(k < nFaces);
_Analysis_assume_(k < nFaces);
if (adjacency[k * 3] == face)
adjacency[k * 3] = UNUSED32;
if (adjacency[k * 3 + 1] == face)
adjacency[k * 3 + 1] = UNUSED32;
if (adjacency[k * 3 + 2] == face)
adjacency[k * 3 + 2] = UNUSED32;
adjacency[face * 3 + point] = UNUSED32;
}
}
}
}
}
// ASYMMETRIC ADJ cleanup
if (adjacency)
{
for (;;)
{
bool unlinked = false;
for (uint32_t face = 0; face < nFaces; ++face)
{
for (uint32_t point = 0; point < 3; ++point)
{
const uint32_t k = adjacency[face * 3 + point];
if (k != UNUSED32)
{
assert(k < nFaces);
_Analysis_assume_(k < nFaces);
const uint32_t edge = find_edge<uint32_t>(&adjacency[k * 3], face);
if (edge >= 3)
{
unlinked = true;
adjacency[face * 3 + point] = UNUSED32;
}
}
}
}
if (!unlinked)
break;
}
}
// BACKFACING cleanup
if (adjacency)
{
for (size_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 == index_t(-1)
|| i1 == index_t(-1)
|| i2 == index_t(-1))
{
// ignore unused faces
continue;
}
assert(i0 < nVerts);
assert(i1 < nVerts);
assert(i2 < nVerts);
if (i0 == i1
|| i0 == i2
|| i1 == i2)
{
// ignore degenerate faces
continue;
}
const uint32_t j0 = adjacency[face * 3];
const uint32_t j1 = adjacency[face * 3 + 1];
const uint32_t j2 = adjacency[face * 3 + 2];
if ((j0 == j1 && j0 != UNUSED32)
|| (j0 == j2 && j0 != UNUSED32)
|| (j1 == j2 && j1 != UNUSED32))
{
uint32_t neighbor = (j0 == j1 || j0 == j2) ? j0 : j1;
// remove links then break bowties will clean up any remaining issues
for (uint32_t edge = 0; edge < 3; ++edge)
{
if (adjacency[face * 3 + edge] == neighbor)
{
adjacency[face * 3 + edge] = UNUSED32;
}
if (adjacency[neighbor * 3 + edge] == face)
{
adjacency[neighbor * 3 + edge] = UNUSED32;
}
}
}
}
}
auto indicesNew = reinterpret_cast<index_t*>(reinterpret_cast<uint8_t*>(ids) + sizeof(uint32_t) * nVerts);
memcpy(indicesNew, indices, sizeof(index_t) * nFaces * 3);
// BOWTIES cleanup
if (adjacency && breakBowties)
{
memset(faceSeen, 0, sizeof(bool) * nFaces * 3);
memset(ids, 0xFF, sizeof(uint32_t) * nVerts);
orbit_iterator<index_t> ovi(adjacency, indices, nFaces);
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 == index_t(-1)
|| i1 == index_t(-1)
|| i2 == index_t(-1))
{
// ignore unused faces
faceSeen[face * 3] = true;
faceSeen[face * 3 + 1] = true;
faceSeen[face * 3 + 2] = true;
continue;
}
assert(i0 < nVerts);
assert(i1 < nVerts);
assert(i2 < nVerts);
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];
if (i == index_t(-1))
continue;
assert(i < nVerts);
ovi.initialize(face, i, orbit_iterator<index_t>::ALL);
ovi.moveToCCW();
index_t replaceVertex = index_t(-1);
index_t replaceValue = index_t(-1);
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;
index_t j = indices[curFace * 3 + curPoint];
if (j == index_t(-1))
continue;
assert(j < nVerts);
if (j == replaceVertex)
{
indicesNew[curFace * 3 + curPoint] = replaceValue;
}
else if (ids[j] == UNUSED32)
{
ids[j] = face;
}
else if (ids[j] != face)
{
// We found a bowtie, duplicate a vert
replaceVertex = j;
replaceValue = index_t(curNewVert);
indicesNew[curFace * 3 + curPoint] = replaceValue;
++curNewVert;
dupVerts.push_back(j);
}
}
}
}
assert((nVerts + dupVerts.size()) == curNewVert);
}
// Ensure no vertex is used by more than one attribute
if (attributes)
{
memset(ids, 0xFF, sizeof(uint32_t) * nVerts);
std::vector<uint32_t> dupAttr;
dupAttr.reserve(dupVerts.size());
for (size_t j = 0; j < dupVerts.size(); ++j)
{
dupAttr.push_back(UNUSED32);
}
std::unordered_multimap<uint32_t, size_t> dups;
for (size_t face = 0; face < nFaces; ++face)
{
uint32_t a = attributes[face];
for (size_t point = 0; point < 3; ++point)
{
uint32_t j = indicesNew[face * 3 + point];
const uint32_t k = (j >= nVerts) ? dupAttr[j - nVerts] : ids[j];
if (k == UNUSED32)
{
if (j >= nVerts)
dupAttr[j - nVerts] = a;
else
ids[j] = a;
}
else if (k != a)
{
// Look for a dup with the correct attribute
auto range = dups.equal_range(j);
auto it = range.first;
for (; it != range.second; ++it)
{
const uint32_t m = (it->second >= nVerts) ? dupAttr[it->second - nVerts] : ids[it->second];
if (m == a)
{
indicesNew[face * 3 + point] = index_t(it->second);
break;
}
}
if (it == range.second)
{
// Duplicate the vert
auto dv = std::pair<uint32_t, size_t>(j, curNewVert);
dups.insert(dv);
indicesNew[face * 3 + point] = index_t(curNewVert);
++curNewVert;
if (j >= nVerts)
{
dupVerts.push_back(dupVerts[j - nVerts]);
}
else
{
dupVerts.push_back(j);
}
dupAttr.push_back(a);
assert(dupVerts.size() == dupAttr.size());
}
}
}
}
assert((nVerts + dupVerts.size()) == curNewVert);
#ifndef NDEBUG
for (const auto it : dupVerts)
{
assert(it < nVerts);
}
#endif
}
if ((uint64_t(nVerts) + uint64_t(dupVerts.size())) >= index_t(-1))
return HRESULT_E_ARITHMETIC_OVERFLOW;
if (!dupVerts.empty())
{
memcpy(indices, indicesNew, sizeof(index_t) * nFaces * 3);
}
return S_OK;
}
}
//=====================================================================================
// Entry-points
//=====================================================================================
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::Clean(
uint16_t* indices,
size_t nFaces,
size_t nVerts,
uint32_t* adjacency,
const uint32_t* attributes,
std::vector<uint32_t>& dupVerts,
bool breakBowties)
{
HRESULT hr = Validate(indices, nFaces, nVerts, adjacency, VALIDATE_DEFAULT);
if (FAILED(hr))
return hr;
return CleanImpl<uint16_t>(indices, nFaces, nVerts, adjacency, attributes, dupVerts, breakBowties);
}
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::Clean(
uint32_t* indices,
size_t nFaces,
size_t nVerts,
uint32_t* adjacency,
const uint32_t* attributes,
std::vector<uint32_t>& dupVerts,
bool breakBowties)
{
HRESULT hr = Validate(indices, nFaces, nVerts, adjacency, VALIDATE_DEFAULT);
if (FAILED(hr))
return hr;
return CleanImpl<uint32_t>(indices, nFaces, nVerts, adjacency, attributes, dupVerts, breakBowties);
}
You can’t perform that action at this time.
