Skip to content

Commit 1bf2873

Browse files
HnMeshUtils: added GetNumTriangles and GetNumEdges functions
1 parent 4119379 commit 1bf2873

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

Hydrogent/include/HnMeshUtils.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ class HnMeshUtils final
4444
{}
4545
~HnMeshUtils();
4646

47+
48+
/// Computes the number of triangles in the triangulated mesh.
49+
/// If NumFaces is not nullptr, it will be set to the number of faces.
50+
///
51+
/// \param[out] NumFaces - The number of faces.
52+
/// \return The number of triangles.
53+
///
54+
/// \remarks The number of faces excludes the faces with less than 3 vertices.
55+
size_t GetNumTriangles(size_t* NumFaces = nullptr) const;
56+
57+
58+
/// Computes the number of edges in the mesh.
59+
/// If NumFaces is not nullptr, it will be set to the number of faces.
60+
///
61+
/// \param[out] NumFaces - The number of faces.
62+
/// \return The number of triangles.
63+
///
64+
/// \remarks The number of faces excludes the faces with less than 3 vertices.
65+
size_t GetNumEdges(size_t* NumFaces = nullptr) const;
66+
67+
4768
/// Triangulates the mesh and returns the triangle indices and the start of each subset.
4869
///
4970
/// \param[in] UseFaceVertexIndices - Whether to use face vertex indices.

Hydrogent/src/HnMeshUtils.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ void HnMeshUtils::ProcessFaces(HandleFaceType&& HandleFace) const
7474
}
7575
}
7676

77+
size_t HnMeshUtils::GetNumTriangles(size_t* pNumFaces) const
78+
{
79+
size_t NumTriangles = 0;
80+
size_t NumFaces = 0;
81+
ProcessFaces(
82+
[&](size_t FaceId, int StartVertex, int VertCount) {
83+
NumTriangles += VertCount - 2;
84+
++NumFaces;
85+
});
86+
if (pNumFaces != nullptr)
87+
{
88+
*pNumFaces = NumFaces;
89+
}
90+
return NumTriangles;
91+
}
92+
7793
void HnMeshUtils::Triangulate(bool UseFaceVertexIndices,
7894
const pxr::VtValue* PointsPrimvar,
7995
pxr::VtVec3iArray& TriangleIndices,
@@ -88,11 +104,7 @@ void HnMeshUtils::Triangulate(bool UseFaceVertexIndices,
88104
nullptr;
89105

90106
// Count the number of triangles
91-
size_t NumTriangles = 0;
92-
ProcessFaces(
93-
[&](size_t FaceId, int StartVertex, int VertCount) {
94-
NumTriangles += VertCount - 2;
95-
});
107+
const size_t NumTriangles = GetNumTriangles();
96108

97109
// Triangulate faces
98110
TriangleIndices.reserve(NumTriangles);
@@ -233,15 +245,26 @@ void HnMeshUtils::Triangulate(bool UseFaceVertexIndices,
233245
}
234246
}
235247

236-
pxr::VtIntArray HnMeshUtils::ComputeEdgeIndices(bool UseFaceVertexIndices, bool UseLineStrip) const
248+
size_t HnMeshUtils::GetNumEdges(size_t* pNumFaces) const
237249
{
250+
size_t NumFaces = 0;
238251
size_t NumEdges = 0;
239-
size_t NumFaces = 0; // Count the actual number of faces
240252
ProcessFaces(
241253
[&](size_t FaceId, int StartVertex, int VertCount) {
242254
NumEdges += VertCount;
243255
++NumFaces;
244256
});
257+
if (pNumFaces != nullptr)
258+
{
259+
*pNumFaces = NumFaces;
260+
}
261+
return NumEdges;
262+
}
263+
264+
pxr::VtIntArray HnMeshUtils::ComputeEdgeIndices(bool UseFaceVertexIndices, bool UseLineStrip) const
265+
{
266+
size_t NumFaces = 0; // Count the actual number of faces
267+
size_t NumEdges = GetNumEdges(&NumFaces);
245268

246269
pxr::VtIntArray EdgeIndices;
247270
if (UseLineStrip)

0 commit comments

Comments
 (0)