From ed04eda51bea802ea04e0156b5052b3cf5041fac Mon Sep 17 00:00:00 2001 From: assiduous Date: Thu, 16 Jan 2025 19:16:17 -0800 Subject: [PATCH] HnMeshUtils: added GetTotalIndexCount function --- Hydrogent/include/HnMeshUtils.hpp | 13 +++++++++++++ Hydrogent/src/HnMeshUtils.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/Hydrogent/include/HnMeshUtils.hpp b/Hydrogent/include/HnMeshUtils.hpp index ffbb1990..d0988381 100644 --- a/Hydrogent/include/HnMeshUtils.hpp +++ b/Hydrogent/include/HnMeshUtils.hpp @@ -28,6 +28,7 @@ #include "pxr/imaging/hd/types.h" #include "pxr/imaging/hd/meshTopology.h" +#include "FlagEnum.h" namespace Diligent { @@ -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. @@ -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 diff --git a/Hydrogent/src/HnMeshUtils.cpp b/Hydrogent/src/HnMeshUtils.cpp index 6b4e4c69..9cb3357a 100644 --- a/Hydrogent/src/HnMeshUtils.cpp +++ b/Hydrogent/src/HnMeshUtils.cpp @@ -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); @@ -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 { @@ -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) @@ -320,6 +323,7 @@ pxr::VtIntArray HnMeshUtils::ComputePointIndices(bool ConvertToFaceVarying) cons pxr::VtIntArray PointIndices; PointIndices.reserve(NumPoints); + VERIFY_EXPR(static_cast(NumPoints) == GetTotalIndexCount(GET_TOTAL_INDEX_COUNT_FLAG_POINTS)); if (ConvertToFaceVarying) { const int* FaceVertexIndices = m_Topology.GetFaceVertexIndices().cdata(); @@ -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 pxr::VtValue ConvertVertexArrayToFaceVaryingArray(const pxr::VtIntArray& FaceVertexIndices, const pxr::VtArray& VertexArray, size_t ValuesPerVertex) {