Skip to content

Commit

Permalink
HnMeshUtils: added GetTotalIndexCount function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Jan 17, 2025
1 parent 1bf2873 commit ed04eda
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Hydrogent/include/HnMeshUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "pxr/imaging/hd/types.h"
#include "pxr/imaging/hd/meshTopology.h"
#include "FlagEnum.h"

namespace Diligent
{
Expand Down Expand Up @@ -65,6 +66,17 @@ class HnMeshUtils final
size_t GetNumEdges(size_t* NumFaces = nullptr) const;


enum GET_TOTAL_INDEX_COUNT_FLAGS : uint32_t
{
GET_TOTAL_INDEX_COUNT_FLAG_NONE = 0u,
GET_TOTAL_INDEX_COUNT_FLAG_TRIANGLES = 1u << 0u,
GET_TOTAL_INDEX_COUNT_FLAG_EDGES_LIST = 1u << 1u,
GET_TOTAL_INDEX_COUNT_FLAG_EDGES_STRIP = 1u << 2u,
GET_TOTAL_INDEX_COUNT_FLAG_POINTS = 1u << 3u,
};
/// Returns the total number of indices (triangles + edges + points)
size_t GetTotalIndexCount(GET_TOTAL_INDEX_COUNT_FLAGS Flags) const;

/// Triangulates the mesh and returns the triangle indices and the start of each subset.
///
/// \param[in] UseFaceVertexIndices - Whether to use face vertex indices.
Expand Down Expand Up @@ -207,6 +219,7 @@ class HnMeshUtils final
const pxr::HdMeshTopology& m_Topology;
const pxr::SdfPath& m_MeshId;
};
DEFINE_FLAG_ENUM_OPERATORS(HnMeshUtils::GET_TOTAL_INDEX_COUNT_FLAGS);

} // namespace USD

Expand Down
31 changes: 31 additions & 0 deletions Hydrogent/src/HnMeshUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void HnMeshUtils::Triangulate(bool UseFaceVertexIndices,

// Count the number of triangles
const size_t NumTriangles = GetNumTriangles();
VERIFY_EXPR(NumTriangles * 3 == GetTotalIndexCount(GET_TOTAL_INDEX_COUNT_FLAG_TRIANGLES));

// Triangulate faces
TriangleIndices.reserve(NumTriangles);
Expand Down Expand Up @@ -280,6 +281,7 @@ pxr::VtIntArray HnMeshUtils::ComputeEdgeIndices(bool UseFaceVertexIndices, bool
EdgeIndices.push_back(-1);
});
VERIFY_EXPR(EdgeIndices.size() == NumEdges + NumFaces * 2);
VERIFY_EXPR(EdgeIndices.size() == GetTotalIndexCount(GET_TOTAL_INDEX_COUNT_FLAG_EDGES_STRIP));
}
else
{
Expand All @@ -295,6 +297,7 @@ pxr::VtIntArray HnMeshUtils::ComputeEdgeIndices(bool UseFaceVertexIndices, bool
EdgeIndices.push_back(StartVertex);
});
VERIFY_EXPR(EdgeIndices.size() == NumEdges * 2);
VERIFY_EXPR(EdgeIndices.size() == GetTotalIndexCount(GET_TOTAL_INDEX_COUNT_FLAG_EDGES_LIST));
}

if (UseFaceVertexIndices)
Expand All @@ -320,6 +323,7 @@ pxr::VtIntArray HnMeshUtils::ComputePointIndices(bool ConvertToFaceVarying) cons

pxr::VtIntArray PointIndices;
PointIndices.reserve(NumPoints);
VERIFY_EXPR(static_cast<size_t>(NumPoints) == GetTotalIndexCount(GET_TOTAL_INDEX_COUNT_FLAG_POINTS));
if (ConvertToFaceVarying)
{
const int* FaceVertexIndices = m_Topology.GetFaceVertexIndices().cdata();
Expand Down Expand Up @@ -356,6 +360,33 @@ pxr::VtIntArray HnMeshUtils::ComputePointIndices(bool ConvertToFaceVarying) cons
return PointIndices;
}

size_t HnMeshUtils::GetTotalIndexCount(GET_TOTAL_INDEX_COUNT_FLAGS Flags) const
{
VERIFY((Flags & (GET_TOTAL_INDEX_COUNT_FLAG_EDGES_LIST | GET_TOTAL_INDEX_COUNT_FLAG_EDGES_STRIP)) != (GET_TOTAL_INDEX_COUNT_FLAG_EDGES_LIST | GET_TOTAL_INDEX_COUNT_FLAG_EDGES_STRIP),
"COUNT_EDGES_LIST and COUNT_EDGES_STRIP flags are mutually exclusive");

size_t NumIndices = 0;
if (Flags & GET_TOTAL_INDEX_COUNT_FLAG_TRIANGLES)
{
size_t NumTriangles = GetNumTriangles();
NumIndices += NumTriangles * 3;
}

if (Flags & (GET_TOTAL_INDEX_COUNT_FLAG_EDGES_LIST | GET_TOTAL_INDEX_COUNT_FLAG_EDGES_STRIP))
{
size_t NumFaces = 0;
size_t NumEdges = GetNumEdges(&NumFaces);
NumIndices += (Flags & GET_TOTAL_INDEX_COUNT_FLAG_EDGES_STRIP) ? NumEdges + NumFaces * 2 : NumEdges * 2;
}

if (Flags & GET_TOTAL_INDEX_COUNT_FLAG_POINTS)
{
NumIndices += m_Topology.GetNumPoints();
}

return NumIndices;
}

template <typename T>
pxr::VtValue ConvertVertexArrayToFaceVaryingArray(const pxr::VtIntArray& FaceVertexIndices, const pxr::VtArray<T>& VertexArray, size_t ValuesPerVertex)
{
Expand Down

0 comments on commit ed04eda

Please sign in to comment.