Skip to content

Commit

Permalink
Fix draco decoder and factorize (#1584)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meakk authored Aug 23, 2024
1 parent c77fdcd commit 7a37328
Showing 1 changed file with 59 additions and 50 deletions.
109 changes: 59 additions & 50 deletions plugins/draco/module/vtkF3DGLTFDocumentLoader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,24 @@

namespace
{

//----------------------------------------------------------------------------
template<typename T>
std::vector<char> DecodeIndexBuffer(const std::unique_ptr<draco::Mesh>& mesh)
{
std::vector<char> 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<T>(face[0].value()), static_cast<T>(face[1].value()),
static_cast<T>(face[2].value()) };

std::copy(
indices, indices + 3, reinterpret_cast<T*>(outBuffer.data() + f.value() * sizeof(indices)));
}

return outBuffer;
}

//----------------------------------------------------------------------------
std::vector<char> DecodeIndexBuffer(
const std::unique_ptr<draco::Mesh>& mesh, vtkGLTFDocumentLoader::ComponentType compType)
template<typename Decoder, typename... Args>
std::vector<char> 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<int8_t>(mesh);
return Decoder().template decode<int8_t>(args...);
case vtkGLTFDocumentLoader::ComponentType::UNSIGNED_BYTE:
return DecodeIndexBuffer<uint16_t>(mesh);
return Decoder().template decode<uint16_t>(args...);
case vtkGLTFDocumentLoader::ComponentType::SHORT:
return DecodeIndexBuffer<int16_t>(mesh);
return Decoder().template decode<int16_t>(args...);
case vtkGLTFDocumentLoader::ComponentType::UNSIGNED_SHORT:
return DecodeIndexBuffer<uint16_t>(mesh);
return Decoder().template decode<uint16_t>(args...);
case vtkGLTFDocumentLoader::ComponentType::UNSIGNED_INT:
return DecodeIndexBuffer<uint32_t>(mesh);
return Decoder().template decode<uint32_t>(args...);
case vtkGLTFDocumentLoader::ComponentType::FLOAT:
return Decoder().template decode<float>(args...);
default:
break;
}
Expand All @@ -56,38 +34,69 @@ std::vector<char> DecodeIndexBuffer(
}

//----------------------------------------------------------------------------
template<typename T>
std::vector<char> DecodeVertexBuffer(
const std::unique_ptr<draco::Mesh>& mesh, const draco::PointAttribute* attribute)
struct IndexBufferDecoder
{
std::vector<char> outBuffer(mesh->num_points() * attribute->num_components() * sizeof(T));
std::vector<T> values(attribute->num_components());

size_t byteOffset = 0;
for (draco::PointIndex i(0); i < mesh->num_points(); ++i)
template<typename T>
std::vector<char> decode(const std::unique_ptr<draco::Mesh>& mesh)
{
attribute->ConvertValue<T>(
attribute->mapped_index(i), attribute->num_components(), values.data());
std::vector<char> 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<T>(face[0].value()), static_cast<T>(face[1].value()),
static_cast<T>(face[2].value()) };

std::copy(values.begin(), values.end(), reinterpret_cast<T*>(outBuffer.data() + byteOffset));
std::copy(
indices, indices + 3, reinterpret_cast<T*>(outBuffer.data() + f.value() * sizeof(indices)));
}

byteOffset += sizeof(T) * attribute->num_components();
return outBuffer;
}
};

//----------------------------------------------------------------------------
std::vector<char> DecodeIndexBuffer(
const std::unique_ptr<draco::Mesh>& mesh, vtkGLTFDocumentLoader::ComponentType compType)
{
// indexing using float does not make sense
assert(compType != vtkGLTFDocumentLoader::ComponentType::FLOAT);

return outBuffer;
return ComponentDispatcher<IndexBufferDecoder>(compType, mesh);
}

//----------------------------------------------------------------------------
std::vector<char> DecodeVertexBuffer(vtkGLTFDocumentLoader::ComponentType compType,
const std::unique_ptr<draco::Mesh>& mesh, int attIndex)
struct VertexBufferDecoder
{
(void)compType;
template<typename T>
std::vector<char> decode(
const std::unique_ptr<draco::Mesh>& mesh, const draco::PointAttribute* attribute)
{
std::vector<char> outBuffer(mesh->num_points() * attribute->num_components() * sizeof(T));
std::vector<T> values(attribute->num_components());

size_t byteOffset = 0;
for (draco::PointIndex i(0); i < mesh->num_points(); ++i)
{
attribute->ConvertValue<T>(
attribute->mapped_index(i), attribute->num_components(), values.data());

const draco::PointAttribute* attribute = mesh->GetAttributeByUniqueId(attIndex);
std::copy(values.begin(), values.end(), reinterpret_cast<T*>(outBuffer.data() + byteOffset));

byteOffset += sizeof(T) * attribute->num_components();
}

assert(compType == vtkGLTFDocumentLoader::ComponentType::FLOAT);
return outBuffer;
}
};

return DecodeVertexBuffer<float>(mesh, attribute);
//----------------------------------------------------------------------------
std::vector<char> DecodeVertexBuffer(vtkGLTFDocumentLoader::ComponentType compType,
const std::unique_ptr<draco::Mesh>& mesh, int attIndex)
{
return ComponentDispatcher<VertexBufferDecoder>(
compType, mesh, mesh->GetAttributeByUniqueId(attIndex));
}
}

Expand Down

0 comments on commit 7a37328

Please sign in to comment.