Skip to content

WeldVertices

Chuck Walbourn edited this page Jan 21, 2022 · 13 revisions
DirectXMesh

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 );

Parameters

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.

Return value

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.

Remarks

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.

Example

auto mesh = std::make_unique<WaveFrontReader<uint16_t>>();

if ( FAILED( mesh->Load( L"test.obj" ) ) )
   // Error

size_t nFaces = mesh->indices.size() / 3;
size_t nVerts = mesh->vertices.size();

auto pos = std::make_unique<XMFLOAT3[]>(nVerts);
for( size_t j = 0; j < nVerts; ++j )
   pos[ j ] = mesh->vertices[ j ].position;

auto preps = std::make_unique<uint32_t[]>(nVerts);
if ( FAILED( GenerateAdjacencyAndPointReps( mesh->indices.data(), nFaces,
   pos.get(), nVerts, 0.f, preps.get(), nullptr ) ) )
   // Error

auto newIndices = std::make_unique<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

auto vertRemap = std::make_unique<uint32_t[]>(nVerts);
size_t trailingUnused = 0;
if ( FAILED( OptimizeVertices( newIndices.get(), nFaces, nVerts,
    vertRemap.get(), &trailingUnused ) ) )
   // Error

auto vb = std::make_unique<WaveFrontReader<uint16_t>::Vertex>(nVerts - trailingUnused);
if ( FAILED( CompactVB( mesh->vertices.data(),
   sizeof(WaveFrontReader<uint16_t>::Vertex),
   nVerts, trailingUnused, vertRemap.get(), vb.get() ) ) )
   // Error

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Windows 8.1
  • Xbox One
  • Xbox Series X|S
  • Windows Subsystem for Linux

Architecture

  • x86
  • x64
  • ARM64

For Development

  • 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

Related Projects

DirectX Tool Kit for DirectX 11

DirectX Tool Kit for DirectX 12

DirectXTex

DirectXMath

Tools

Test Suite

Content Exporter

DxCapsViewer

See also

DirectX Landing Page

Clone this wiki locally