-
Notifications
You must be signed in to change notification settings - Fork 152
WeldVertices
Welds together replicated vertices that have equal attributes using a supplied test function.
HRESULT WeldVertices(
uint16_t* indices, size_t nFaces,
size_t nVerts, const uint32_t* pointRep,
uint32_t* vertexRemap,
std::function<bool (uint32_t v0, uint32_t v1)> weldTest );
HRESULT WeldVertices(
uint32_t* indices, size_t nFaces,
size_t nVerts, const uint32_t* pointRep,
uint32_t* vertexRemap,
std::function<bool (uint32_t v0, uint32_t v1)> weldTest );
The indices array is provided as an input, and is modified by the weld operation.
pointRep is required for the weld vertices operation and should be computed using GenerateAdjacencyAndPointReps. The choice of the epsilon parameter passed to that function determines the candidate set of potential vertices to weld, and if 0 is used topographical point-representatives are computed.
vertexRemap is an array describing the welded vertices, and must be nVerts in size: oldLoc = vertexRemap[newLoc]
. Removed vertices will be set to uint32_t(-1)
. This parameter is optional and can be nullptr.
weldTest is invoked to compare two vertices which are candidates for welding based on the point-representatives.
The function returns an HRESULT
value for both success and failure. The value of S_FALSE
indicates that no welding took place either because there were no candidates or because the weldTest function always returned false. In case this, any vertexRemap returned will be the identity.
It is recommended that after you perform welding that you use OptimizeVertices followed by CompactVB to remove all the unused vertices that result from welding.
auto mesh = std::unique_ptr<WaveFrontReader<uint16_t>>();
if ( FAILED( mesh->Load( L"test.obj" ) ) )
// Error
size_t nFaces = mesh->indices.size() / 3;
size_t nVerts = mesh->vertices.size();
std::unique_ptr<XMFLOAT3[]> pos( new XMFLOAT3[ nVerts ] );
for( size_t j = 0; j < nVerts; ++j )
pos[ j ] = mesh->vertices[ j ].position;
std::unique_ptr<uint32_t[]> preps( new uint32_t[ nVerts ] );
if ( FAILED( GenerateAdjacencyAndPointReps( mesh->indices.data(), nFaces,
pos.get(), nVerts, 0.f, preps.get(), nullptr ) ) )
// Error
std::unique_ptr<uint16_t[]> newIndices( new uint16_t[ nFaces * 3 ] );
memcpy(newIndices.get(), mesh->indices.data(), sizeof(uint16_t) * nFaces * 3;
if ( FAILED( WeldVertices(newIndices.get(), nFaces, nVerts, preps.get(), nullptr,
{
static const XMVECTORF32 s_Epsilon = { { { 1e-20f, 1e-20f, 1e-20f, 1e-20f } } };
XMVECTOR vA = XMLoadFloat3(&mesh->vertices[ v0 ].position);
XMVECTOR vB = XMLoadFloat3(&mesh->vertices[ v1 ].position);
XMVECTOR nA = XMLoadFloat3(&mesh->vertices[ v0 ].normal);
XMVECTOR nB = XMLoadFloat3(&mesh->vertices[ v1 ].normal);
XMVECTOR uvA = XMLoadFloat2(&mesh->vertices[ v0 ].textureCoordinate);
XMVECTOR uvB = XMLoadFloat2(&mesh->vertices[ v1 ].textureCoordinate);
if (XMVector3NearEqual(vA, vB, s_Epsilon))
{
if (XMVector3NearEqual(nA, nB, s_Epsilon))
{
if (XMVector2NearEqual(uvA, uvB, s_Epsilon))
{
return true;
}
}
}
return false;
}) ) )
// Error
std::unique_ptr<uint32_t[]> vertRemap( new uint32_t[ nVerts ] );
size_t trailingUnused = 0;
if ( FAILED( OptimizeVertices( newIndices.get(), nFaces, nVerts,
vertRemap.get(), &trailingUnused ) ) )
// Error
std::unique_ptr<WaveFrontReader<uint16_t>::Vertex> vb(
new WaveFrontReader<uint16_t>::Vertex[ nVerts - trailingUnused ] );
if ( FAILED( CompactVB( mesh->vertices.data(),
sizeof(WaveFrontReader<uint16_t>::Vertex),
nVerts, trailingUnused, vertRemap.get(), vb.get() ) ) )
// Error
All content and source code for this package are subject to the terms of the MIT License.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
- Universal Windows Platform apps
- Windows desktop apps
- Windows 11
- Windows 10
- Windows 8.1
- Xbox One
- Xbox Series X|S
- Windows Subsystem for Linux
- x86
- x64
- ARM64
- Visual Studio 2022
- Visual Studio 2019 (16.11)
- clang/LLVM v12 - v18
- GCC 10.5, 11.4, 12.3
- MinGW 12.2, 13.2
- CMake 3.20
DirectX Tool Kit for DirectX 11