Skip to content

Commit 04c5d60

Browse files
committed
[algorithms] some utility topology functions
1 parent 4bf73cb commit 04c5d60

File tree

2 files changed

+108
-34
lines changed

2 files changed

+108
-34
lines changed

vclib/core/include/vclib/algorithms/mesh/import_export/export_buffer.h

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ namespace vcl {
3131
/**
3232
* @brief Export the vertex coordinates of a mesh to a buffer.
3333
*
34-
* This function exports the vertex coordinates of a mesh to a buffer. The
34+
* This function exports the vertex coordinates of a mesh to a buffer. Vertices
35+
* are stored in the buffer following the order they appear in the mesh. The
3536
* buffer must be preallocated with the correct size (number of vertices times
3637
* the number of coordinates per vertex).
3738
*
@@ -75,9 +76,10 @@ void vertexCoordsToBuffer(
7576
* @brief Export into a buffer the vertex indices for each triangle of a Mesh.
7677
*
7778
* This function exports the vertex indices of the triangles of a mesh to a
78-
* buffer. The buffer must be preallocated with the correct size (number of
79-
* faces times 3). The function assumes that the input mesh is a triangle mesh
80-
* (if there are polygonal faces, only the first three vertices are considered).
79+
* buffer. Indices are stored following the order the faces appear in the mesh.
80+
* The buffer must be preallocated with the correct size (number of faces times
81+
* 3). The function assumes that the input mesh is a triangle mesh (if there are
82+
* polygonal faces, only the first three vertices are considered).
8183
*
8284
* @note This function does not guarantee that the rows of the matrix
8385
* correspond to the face indices of the mesh. This scenario is possible
@@ -120,8 +122,9 @@ void trianglesToBuffer(
120122
* a mesh to a buffer.
121123
*
122124
* This function exports the selection status of the elements identified by
123-
* `ELEM_ID` of a mesh to a buffer. The buffer must be preallocated with the
124-
* correct size (number of elements).
125+
* `ELEM_ID` of a mesh to a buffer. Values are stored in the buffer following
126+
* the order the elements appear in the mesh. The buffer must be preallocated
127+
* with the correct size (number of elements).
125128
*
126129
* Usage example with std::vector<bool>:
127130
*
@@ -152,8 +155,9 @@ void elementSelectionToBuffer(const MeshType& mesh, auto* buffer)
152155
* @brief Export the selection status of the vertices of a mesh to a buffer.
153156
*
154157
* This function exports the selection status of the vertices of a mesh to a
155-
* buffer. The buffer must be preallocated with the correct size (number of
156-
* elements).
158+
* buffer. Values are stored in the buffer following the order the vertices
159+
* appear in the mesh. The buffer must be preallocated with the correct size
160+
* (number of elements).
157161
*
158162
* Usage example with std::vector<bool>:
159163
*
@@ -180,7 +184,8 @@ void vertexSelectionToBuffer(const MeshType& mesh, auto* buffer)
180184
* @brief Export the selection status of the faces of a mesh to a buffer.
181185
*
182186
* This function exports the selection status of the faces of a mesh to a
183-
* buffer. The buffer must be preallocated with the correct size (number of
187+
* buffer. Values are stored in the buffer following the order the faces appear
188+
* in the mesh. The buffer must be preallocated with the correct size (number of
184189
* elements).
185190
*
186191
* Usage example with std::vector<bool>:
@@ -208,7 +213,8 @@ void faceSelectionToBuffer(const MeshType& mesh, auto* buffer)
208213
* @brief Export the selection status of the edges of a mesh to a buffer.
209214
*
210215
* This function exports the selection status of the edges of a mesh to a
211-
* buffer. The buffer must be preallocated with the correct size (number of
216+
* buffer. Values are stored in the buffer following the order the edges appear
217+
* in the mesh. The buffer must be preallocated with the correct size (number of
212218
* elements).
213219
*
214220
* Usage example with std::vector<bool>:
@@ -237,8 +243,9 @@ void edgeSelectionToBuffer(const MeshType& mesh, auto* buffer)
237243
* buffer.
238244
*
239245
* This function exports the element normals identified by `ELEM_ID` of a mesh
240-
* to a buffer. The buffer must be preallocated with the correct size (number of
241-
* elements times 3).
246+
* to a buffer. Normals are stored in the buffer following the order the
247+
* elements appear in the mesh. The buffer must be preallocated with the correct
248+
* size (number of elements times 3).
242249
*
243250
* @note This function does not guarantee that the rows of the matrix
244251
* correspond to the element indices of the mesh. This scenario is possible
@@ -283,8 +290,10 @@ void elementNormalsToBuffer(
283290
/**
284291
* @brief Export the vertex normals of a mesh to a buffer.
285292
*
286-
* This function exports the vertex normals of a mesh to a buffer. The buffer
287-
* must be preallocated with the correct size (number of vertices times 3).
293+
* This function exports the vertex normals of a mesh to a buffer. Normals are
294+
* stored in the buffer following the order the vertices appear in the mesh. The
295+
* buffer must be preallocated with the correct size (number of vertices times
296+
* 3).
288297
*
289298
* @note This function does not guarantee that the rows of the matrix
290299
* correspond to the vertex indices of the mesh. This scenario is possible
@@ -307,8 +316,9 @@ void vertexNormalsToBuffer(
307316
/**
308317
* @brief Export the face normals of a mesh to a buffer.
309318
*
310-
* This function exports the face normals of a mesh to a buffer. The buffer must
311-
* be preallocated with the correct size (number of faces times 3).
319+
* This function exports the face normals of a mesh to a buffer. Normals are
320+
* stored in the buffer following the order the faces appear in the mesh. The
321+
* buffer must be preallocated with the correct size (number of faces times 3).
312322
*
313323
* @note This function does not guarantee that the rows of the matrix
314324
* correspond to the face indices of the mesh. This scenario is possible when
@@ -333,8 +343,9 @@ void faceNormalsToBuffer(
333343
* buffer having a value for each color component (RGBA).
334344
*
335345
* This function exports the element colors identified by `ELEM_ID` of a mesh
336-
* to a buffer. The buffer must be preallocated with the correct size (number of
337-
* elements times 4).
346+
* to a buffer. Colors are stored in the buffer following the order the elements
347+
* appear in the mesh. The buffer must be preallocated with the correct size
348+
* (number of elements times 4).
338349
*
339350
* @note This function does not guarantee that the rows of the matrix
340351
* correspond to the element indices of the mesh. This scenario is possible
@@ -389,8 +400,9 @@ void elementColorsToBuffer(
389400
* value using the provided format).
390401
*
391402
* This function exports the element colors identified by `ELEM_ID` of a mesh to
392-
* a buffer. The buffer must be preallocated with the correct size (number of
393-
* elements).
403+
* a buffer. Colors are stored in the buffer following the order the elements
404+
* appear in the mesh. The buffer must be preallocated with the correct size
405+
* (number of elements).
394406
*
395407
* @note This function does not guarantee that the rows of the matrix
396408
* correspond to the element indices of the mesh. This scenario is possible when
@@ -425,8 +437,10 @@ void elementColorsToBuffer(
425437
* @brief Export the vertex colors of a mesh to a buffer having a value for each
426438
* color component (RGBA).
427439
*
428-
* This function exports the vertex colors of a mesh to a buffer. The buffer
429-
* must be preallocated with the correct size (number of vertices times 4).
440+
* This function exports the vertex colors of a mesh to a buffer. Colors are
441+
* stored in the buffer following the order the vertices appear in the mesh. The
442+
* buffer must be preallocated with the correct size (number of vertices times
443+
* 4).
430444
*
431445
* @note This function does not guarantee that the rows of the matrix
432446
* correspond to the vertex indices of the mesh. This scenario is possible
@@ -455,8 +469,9 @@ void vertexColorsToBuffer(
455469
* color (the color is packed in a single 32 bit value using the provided
456470
* format).
457471
*
458-
* This function exports the vertex colors of a mesh to a buffer. The buffer
459-
* must be preallocated with the correct size (number of vertices).
472+
* This function exports the vertex colors of a mesh to a buffer. Colors are
473+
* stored in the buffer following the order the vertices appear in the mesh. The
474+
* buffer must be preallocated with the correct size (number of vertices).
460475
*
461476
* @note This function does not guarantee that the rows of the matrix
462477
* correspond to the vertex indices of the mesh. This scenario is possible when
@@ -480,8 +495,9 @@ void vertexColorsToBuffer(
480495
* @brief Export the face colors of a mesh to a buffer having a value for each
481496
* color component (RGBA).
482497
*
483-
* This function exports the face colors of a mesh to a buffer. The buffer
484-
* must be preallocated with the correct size (number of faces times 4).
498+
* This function exports the face colors of a mesh to a buffer. Colors are
499+
* stored in the buffer following the order the faces appear in the mesh. The
500+
* buffer must be preallocated with the correct size (number of faces times 4).
485501
*
486502
* @note This function does not guarantee that the rows of the matrix
487503
* correspond to the face indices of the mesh. This scenario is possible
@@ -509,8 +525,9 @@ void faceColorsToBuffer(
509525
* color (the color is packed in a single 32 bit value using the provided
510526
* format).
511527
*
512-
* This function exports the face colors of a mesh to a buffer. The buffer
513-
* must be preallocated with the correct size (number of faces).
528+
* This function exports the face colors of a mesh to a buffer. Colors are
529+
* stored in the buffer following the order the faces appear in the mesh. The
530+
* buffer must be preallocated with the correct size (number of faces).
514531
*
515532
* @note This function does not guarantee that the rows of the matrix
516533
* correspond to the face indices of the mesh. This scenario is possible when
@@ -535,8 +552,9 @@ void faceColorsToBuffer(
535552
* buffer.
536553
*
537554
* This function exports the element quality identified by `ELEM_ID` of a mesh
538-
* to a buffer. The buffer must be preallocated with the correct size (number of
539-
* elements).
555+
* to a buffer. Quality values are stored in the buffer following the order the
556+
* elements appear in the mesh. The buffer must be preallocated with the correct
557+
* size (number of elements).
540558
*
541559
* @note This function does not guarantee that the rows of the vector
542560
* correspond to the element indices of the mesh. This scenario is possible when
@@ -562,8 +580,10 @@ void elementQualityToBuffer(const MeshType& mesh, auto* buffer)
562580
/**
563581
* @brief Export the vertex quality of a mesh to a buffer.
564582
*
565-
* This function exports the vertex quality of a mesh to a buffer. The buffer
566-
* must be preallocated with the correct size (number of vertices).
583+
* This function exports the vertex quality of a mesh to a buffer. Quality
584+
* values are stored in the buffer following the order the vertices appear in
585+
* the mesh. The buffer must be preallocated with the correct size (number of
586+
* vertices).
567587
*
568588
* @note This function does not guarantee that the rows of the vector
569589
* correspond to the vertex indices of the mesh. This scenario is possible when
@@ -582,8 +602,9 @@ void vertexQualityToBuffer(const MeshType& mesh, auto* buffer)
582602
/**
583603
* @brief Export the face quality of a mesh to a buffer.
584604
*
585-
* This function exports the face quality of a mesh to a buffer. The buffer
586-
* must be preallocated with the correct size (number of faces).
605+
* This function exports the face quality of a mesh to a buffer. Quality values
606+
* are stored in the buffer following the order the faces appear in the mesh.
607+
* The buffer must be preallocated with the correct size (number of faces).
587608
*
588609
* @note This function does not guarantee that the rows of the vector
589610
* correspond to the face indices of the mesh. This scenario is possible when

vclib/core/include/vclib/algorithms/mesh/stat/topology.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,59 @@ void setReferencedVertices(
7070

7171
} // namespace detail
7272

73+
/**
74+
* @brief Count the number of references to vertices in the mesh faces.
75+
*
76+
* If the mesh is a TriangleMesh, the number of references is equal to the
77+
* number of faces times 3. Otherwise, the function counts the number of
78+
* references to vertices in each face of the mesh.
79+
*
80+
* @param[in] mesh: The input mesh. It must satisfy the MeshConcept.
81+
* @return The number of references to vertices in the mesh faces.
82+
*/
83+
uint countPerFaceVertexReferences(const MeshConcept auto& mesh)
84+
{
85+
using MeshType = decltype(mesh);
86+
87+
uint nRefs = 0;
88+
89+
if constexpr (FaceMeshConcept<MeshType>) {
90+
if constexpr (TriangleMeshConcept<MeshType>) {
91+
return mesh.faceNumber() * 3;
92+
}
93+
else {
94+
for (const auto& f : mesh.faces()) {
95+
nRefs += f.vertexNumber();
96+
}
97+
}
98+
}
99+
100+
return nRefs;
101+
}
102+
103+
/**
104+
* @brief Counts the number of resulting triangles if the input mesh would be
105+
* triangulated by splitting each face into triangles.
106+
*
107+
* @param[in] mesh: The input mesh. It must satisfy the MeshConcept.
108+
* @return The number of resulting triangles if the input mesh would be
109+
* triangulated by splitting each face into triangles.
110+
*/
111+
uint countTriangulatedTriangles(const MeshConcept auto& mesh)
112+
{
113+
using MeshType = decltype(mesh);
114+
115+
uint nTris = 0;
116+
117+
if constexpr (FaceMeshConcept<MeshType>) {
118+
for (const auto& f : mesh.faces()) {
119+
nTris += f.vertexNumber() - 2;
120+
}
121+
}
122+
123+
return nTris;
124+
}
125+
73126
/**
74127
* @brief Returns a Container of values interpreted as booleans telling, for
75128
* each vertex of the mesh, if it is referenced.

0 commit comments

Comments
 (0)