diff --git a/plugins/draco/module/vtkF3DGLTFDocumentLoader.cxx b/plugins/draco/module/vtkF3DGLTFDocumentLoader.cxx index 6ea4774cec..6ca55dbae5 100644 --- a/plugins/draco/module/vtkF3DGLTFDocumentLoader.cxx +++ b/plugins/draco/module/vtkF3DGLTFDocumentLoader.cxx @@ -8,46 +8,24 @@ namespace { - -//---------------------------------------------------------------------------- -template -std::vector DecodeIndexBuffer(const std::unique_ptr& mesh) -{ - std::vector outBuffer(mesh->num_faces() * 3 * sizeof(T)); - - for (draco::FaceIndex f(0); f < mesh->num_faces(); ++f) - { - const draco::Mesh::Face& face = mesh->face(f); - - T indices[3] = { static_cast(face[0].value()), static_cast(face[1].value()), - static_cast(face[2].value()) }; - - std::copy( - indices, indices + 3, reinterpret_cast(outBuffer.data() + f.value() * sizeof(indices))); - } - - return outBuffer; -} - //---------------------------------------------------------------------------- -std::vector DecodeIndexBuffer( - const std::unique_ptr& mesh, vtkGLTFDocumentLoader::ComponentType compType) +template +std::vector ComponentDispatcher(vtkGLTFDocumentLoader::ComponentType compType, Args&&... args) { - // indexing using float does not make sense - assert(compType != vtkGLTFDocumentLoader::ComponentType::FLOAT); - switch (compType) { case vtkGLTFDocumentLoader::ComponentType::BYTE: - return DecodeIndexBuffer(mesh); + return Decoder().template decode(args...); case vtkGLTFDocumentLoader::ComponentType::UNSIGNED_BYTE: - return DecodeIndexBuffer(mesh); + return Decoder().template decode(args...); case vtkGLTFDocumentLoader::ComponentType::SHORT: - return DecodeIndexBuffer(mesh); + return Decoder().template decode(args...); case vtkGLTFDocumentLoader::ComponentType::UNSIGNED_SHORT: - return DecodeIndexBuffer(mesh); + return Decoder().template decode(args...); case vtkGLTFDocumentLoader::ComponentType::UNSIGNED_INT: - return DecodeIndexBuffer(mesh); + return Decoder().template decode(args...); + case vtkGLTFDocumentLoader::ComponentType::FLOAT: + return Decoder().template decode(args...); default: break; } @@ -56,38 +34,69 @@ std::vector DecodeIndexBuffer( } //---------------------------------------------------------------------------- -template -std::vector DecodeVertexBuffer( - const std::unique_ptr& mesh, const draco::PointAttribute* attribute) +struct IndexBufferDecoder { - std::vector outBuffer(mesh->num_points() * attribute->num_components() * sizeof(T)); - std::vector values(attribute->num_components()); - - size_t byteOffset = 0; - for (draco::PointIndex i(0); i < mesh->num_points(); ++i) + template + std::vector decode(const std::unique_ptr& mesh) { - attribute->ConvertValue( - attribute->mapped_index(i), attribute->num_components(), values.data()); + std::vector outBuffer(mesh->num_faces() * 3 * sizeof(T)); + + for (draco::FaceIndex f(0); f < mesh->num_faces(); ++f) + { + const draco::Mesh::Face& face = mesh->face(f); + + T indices[3] = { static_cast(face[0].value()), static_cast(face[1].value()), + static_cast(face[2].value()) }; - std::copy(values.begin(), values.end(), reinterpret_cast(outBuffer.data() + byteOffset)); + std::copy( + indices, indices + 3, reinterpret_cast(outBuffer.data() + f.value() * sizeof(indices))); + } - byteOffset += sizeof(T) * attribute->num_components(); + return outBuffer; } +}; + +//---------------------------------------------------------------------------- +std::vector DecodeIndexBuffer( + const std::unique_ptr& mesh, vtkGLTFDocumentLoader::ComponentType compType) +{ + // indexing using float does not make sense + assert(compType != vtkGLTFDocumentLoader::ComponentType::FLOAT); - return outBuffer; + return ComponentDispatcher(compType, mesh); } //---------------------------------------------------------------------------- -std::vector DecodeVertexBuffer(vtkGLTFDocumentLoader::ComponentType compType, - const std::unique_ptr& mesh, int attIndex) +struct VertexBufferDecoder { - (void)compType; + template + std::vector decode( + const std::unique_ptr& mesh, const draco::PointAttribute* attribute) + { + std::vector outBuffer(mesh->num_points() * attribute->num_components() * sizeof(T)); + std::vector values(attribute->num_components()); + + size_t byteOffset = 0; + for (draco::PointIndex i(0); i < mesh->num_points(); ++i) + { + attribute->ConvertValue( + attribute->mapped_index(i), attribute->num_components(), values.data()); - const draco::PointAttribute* attribute = mesh->GetAttributeByUniqueId(attIndex); + std::copy(values.begin(), values.end(), reinterpret_cast(outBuffer.data() + byteOffset)); + + byteOffset += sizeof(T) * attribute->num_components(); + } - assert(compType == vtkGLTFDocumentLoader::ComponentType::FLOAT); + return outBuffer; + } +}; - return DecodeVertexBuffer(mesh, attribute); +//---------------------------------------------------------------------------- +std::vector DecodeVertexBuffer(vtkGLTFDocumentLoader::ComponentType compType, + const std::unique_ptr& mesh, int attIndex) +{ + return ComponentDispatcher( + compType, mesh, mesh->GetAttributeByUniqueId(attIndex)); } }