Skip to content

Commit 45b9183

Browse files
committed
[algorithms] export face sizes to buffer
1 parent 839472a commit 45b9183

File tree

5 files changed

+224
-10
lines changed

5 files changed

+224
-10
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
v 0.0 0.0 1.026054
2+
v 0.447838 0.0 0.9231617
3+
v -0.02363976 0.4472136 0.9231617
4+
v -0.4050732 0.190983 0.9231617
5+
v -0.1693344 -0.4145898 0.9231617
6+
v 0.4241982 0.4472136 0.8202696
7+
v 0.7673818 0.190983 0.6537868
8+
v 0.5552827 -0.4145898 0.7566788
9+
v -0.2312241 0.7562306 0.6537868
10+
v -0.5744076 -0.2236068 0.8202696
11+
v -0.6126576 0.5 0.6537868
12+
v 0.1738492 -0.6708204 0.7566788
13+
v -0.4669629 -0.6381966 0.6537868
14+
v 0.493393 0.7562306 0.4873039
15+
v 0.8748265 -0.2236068 0.4873039
16+
v 0.8365765 0.5 0.320821
17+
v 0.7054921 -0.6381966 0.3844118
18+
v 0.08831973 0.9472136 0.3844118
19+
v -0.5434628 0.809017 0.320821
20+
v -0.8866463 -0.1708204 0.4873039
21+
v -0.9102861 0.2763932 0.3844118
22+
v -0.1237794 -0.8944272 0.4873039
23+
v 0.3240586 -0.8944272 0.3844118
24+
v -0.7792016 -0.5854102 0.320821
25+
v 0.6289922 0.809017 0.05144604
26+
v 1.010426 -0.1708204 0.05144604
27+
v 0.9867859 0.2763932 -0.05144604
28+
v 0.8410913 -0.5854102 -0.05144604
29+
v -0.223919 1.0 0.05144604
30+
v 0.223919 1.0 -0.05144604
31+
v -0.8410913 0.5854102 0.05144604
32+
v -0.9867859 -0.2763932 0.05144604
33+
v -1.010426 0.1708204 -0.05144604
34+
v -0.223919 -1.0 0.05144604
35+
v 0.223919 -1.0 -0.05144604
36+
v -0.6289922 -0.809017 -0.05144604
37+
v 0.7792016 0.5854102 -0.320821
38+
v 0.9102861 -0.2763932 -0.3844118
39+
v 0.8866463 0.1708204 -0.4873039
40+
v 0.5434628 -0.809017 -0.320821
41+
v -0.3240586 0.8944272 -0.3844118
42+
v 0.1237794 0.8944272 -0.4873039
43+
v -0.7054921 0.6381966 -0.3844118
44+
v -0.8365765 -0.5 -0.320821
45+
v -0.8748265 0.2236068 -0.4873039
46+
v -0.08831973 -0.9472136 -0.3844118
47+
v -0.493393 -0.7562306 -0.4873039
48+
v 0.4669629 0.6381966 -0.6537868
49+
v 0.6126576 -0.5 -0.6537868
50+
v 0.5744076 0.2236068 -0.8202696
51+
v 0.2312241 -0.7562306 -0.6537868
52+
v -0.1738492 0.6708204 -0.7566788
53+
v -0.5552827 0.4145898 -0.7566788
54+
v -0.7673818 -0.190983 -0.6537868
55+
v -0.4241982 -0.4472136 -0.8202696
56+
v 0.1693344 0.4145898 -0.9231617
57+
v 0.4050732 -0.190983 -0.9231617
58+
v 0.02363976 -0.4472136 -0.9231617
59+
v -0.447838 0.0 -0.9231617
60+
v 0.0 0.0 -1.026054
61+
f 1 3 4
62+
f 2 7 6
63+
f 5 10 13
64+
f 8 17 15
65+
f 9 19 11
66+
f 12 22 23
67+
f 14 16 25
68+
f 18 30 29
69+
f 20 32 24
70+
f 21 31 33
71+
f 26 28 38
72+
f 27 39 37
73+
f 34 46 35
74+
f 36 44 47
75+
f 40 51 49
76+
f 41 42 52
77+
f 43 53 45
78+
f 48 50 56
79+
f 54 59 55
80+
f 57 58 60
81+
f 1 2 6 3
82+
f 1 4 10 5
83+
f 2 8 15 7
84+
f 3 9 11 4
85+
f 5 13 22 12
86+
f 6 7 16 14
87+
f 8 12 23 17
88+
f 9 18 29 19
89+
f 10 20 24 13
90+
f 11 19 31 21
91+
f 14 25 30 18
92+
f 15 17 28 26
93+
f 16 27 37 25
94+
f 20 21 33 32
95+
f 22 34 35 23
96+
f 24 32 44 36
97+
f 26 38 39 27
98+
f 28 40 49 38
99+
f 29 30 42 41
100+
f 31 43 45 33
101+
f 34 36 47 46
102+
f 35 46 51 40
103+
f 37 39 50 48
104+
f 41 52 53 43
105+
f 42 48 56 52
106+
f 44 54 55 47
107+
f 45 53 59 54
108+
f 49 51 58 57
109+
f 50 57 60 56
110+
f 55 59 60 58
111+
f 1 5 12 8 2
112+
f 3 6 14 18 9
113+
f 4 11 21 20 10
114+
f 7 15 26 27 16
115+
f 13 24 36 34 22
116+
f 17 23 35 40 28
117+
f 19 29 41 43 31
118+
f 25 37 48 42 30
119+
f 32 33 45 54 44
120+
f 38 49 57 50 39
121+
f 46 47 55 58 51
122+
f 52 56 60 59 53

tests/core/006-load-save-mesh-obj/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,24 @@ TEMPLATE_TEST_CASE(
143143
REQUIRE(pm.faceNumber() == 12);
144144
}
145145

146+
SECTION("TriMesh - Rhombicosidodecahedron")
147+
{
148+
TriMesh pm;
149+
vcl::loadObj(
150+
pm, VCLIB_EXAMPLE_MESHES_PATH "/rhombicosidodecahedron.obj");
151+
REQUIRE(pm.vertexNumber() == 60);
152+
REQUIRE(pm.faceNumber() == 116);
153+
}
154+
155+
SECTION("PolyMesh - Rhombicosidodecahedron")
156+
{
157+
PolyMesh pm;
158+
vcl::loadObj(
159+
pm, VCLIB_EXAMPLE_MESHES_PATH "/rhombicosidodecahedron.obj");
160+
REQUIRE(pm.vertexNumber() == 60);
161+
REQUIRE(pm.faceNumber() == 62);
162+
}
163+
146164
SECTION("EdgeMesh - PolyCube")
147165
{
148166
EdgeMesh em;

tests/core/019-export-matrix/main.cpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ void testTrianglesMatrix(const auto& tm)
133133
}
134134
}
135135

136+
template<typename VectorType>
137+
void testFaceSizesVector(const auto& pm)
138+
{
139+
auto sizes = vcl::faceSizesVector<VectorType>(pm);
140+
141+
REQUIRE(sizes.size() == pm.faceNumber());
142+
143+
vcl::uint i = 0;
144+
for (const auto& f : pm.faces()) {
145+
REQUIRE(sizes[i] == f.vertexNumber());
146+
++i;
147+
}
148+
}
149+
136150
template<typename VectorType>
137151
void testVertexSelectionVector(const auto& tm)
138152
{
@@ -289,18 +303,29 @@ void testFaceQualityVector(const auto& tm)
289303
}
290304
}
291305

306+
using Meshes = std::tuple<vcl::TriMesh, vcl::PolyMesh, vcl::EdgeMesh>;
307+
using Meshesf = std::tuple<vcl::TriMeshf, vcl::PolyMeshf, vcl::EdgeMeshf>;
308+
using MeshesIndexed =
309+
std::tuple<vcl::TriMeshIndexed, vcl::PolyMeshIndexed, vcl::EdgeMeshIndexed>;
310+
using MeshesIndexedf = std::
311+
tuple<vcl::TriMeshIndexedf, vcl::PolyMeshIndexedf, vcl::EdgeMeshIndexedf>;
312+
292313
TEMPLATE_TEST_CASE(
293314
"Export TriMesh to Matrix",
294315
"",
295-
vcl::TriMesh,
296-
vcl::TriMeshf,
297-
vcl::TriMeshIndexed,
298-
vcl::TriMeshIndexedf)
316+
Meshes,
317+
Meshesf,
318+
MeshesIndexed,
319+
MeshesIndexedf)
299320
{
300-
using TriMesh = TestType;
321+
using TriMesh = std::tuple_element_t<0, TestType>;
322+
using PolyMesh = std::tuple_element_t<1, TestType>;
323+
using EdgeMesh = std::tuple_element_t<2, TestType>;
301324

302325
TriMesh tm =
303326
vcl::loadPly<TriMesh>(VCLIB_EXAMPLE_MESHES_PATH "/cube_tri.ply");
327+
PolyMesh pm = vcl::loadObj<PolyMesh>(VCLIB_EXAMPLE_MESHES_PATH
328+
"/rhombicosidodecahedron.obj");
304329

305330
SECTION("Coordinates...")
306331
{
@@ -352,6 +377,24 @@ TEMPLATE_TEST_CASE(
352377
}
353378
}
354379

380+
SECTION("Face sizes...")
381+
{
382+
SECTION("Eigen Vector<vcl::uint>")
383+
{
384+
testFaceSizesVector<Eigen::VectorX<vcl::uint>>(pm);
385+
}
386+
387+
SECTION("std vector<vcl::uint>")
388+
{
389+
testFaceSizesVector<std::vector<vcl::uint>>(pm);
390+
}
391+
392+
SECTION("vcl::Vector<vcl::uint>")
393+
{
394+
testFaceSizesVector<vcl::Vector<vcl::uint, -1>>(pm);
395+
}
396+
}
397+
355398
SECTION("Vertex selection...")
356399
{
357400
randomSelection<vcl::ElemId::VERTEX>(tm);

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,40 @@ void triangleIndicesToBuffer(
117117
}
118118
}
119119

120+
/**
121+
* @brief Export into a buffer the sizes of the faces of a Mesh, and return the
122+
* sum of the sizes.
123+
*
124+
* This function could be useful when dealing with polygonal meshes: it exports
125+
* the sizes of the faces of a mesh to a buffer. Sizes are stored following the
126+
* order the faces appear in the mesh. The buffer must be preallocated with the
127+
* correct size (number of faces).
128+
*
129+
* The return value is the sum of the sizes of the faces. This value is useful
130+
* when you need to allocate a buffer to store the vertex indices of the faces
131+
* (its size is the sum of the face sizes).
132+
*
133+
* @note This function does not guarantee that the rows of the matrix
134+
* correspond to the face indices of the mesh. This scenario is possible
135+
* when the mesh has deleted faces. To be sure to have a direct
136+
* correspondence, compact the face container before calling this function.
137+
*
138+
* @param mesh
139+
* @param buffer
140+
*/
141+
template<FaceMeshConcept MeshType>
142+
uint faceSizesToBuffer(const MeshType& mesh, auto* buffer)
143+
{
144+
uint sum = 0;
145+
uint i = 0;
146+
for (const auto& f : mesh.faces()) {
147+
buffer[i] = f.vertexNumber();
148+
sum += f.vertexNumber();
149+
++i;
150+
}
151+
return sum;
152+
}
153+
120154
/**
121155
* @brief Export into a buffer the vertex indices for each edge of a Mesh.
122156
*

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,8 @@ Vect faceSizesVector(const MeshType& mesh)
186186

187187
Vect fM(mesh.faceNumber());
188188

189-
uint i = 0;
190-
for (const auto& f : mesh.faces()) {
191-
fM(i) = f.vertexNumber();
192-
++i;
193-
}
189+
faceSizesToBuffer(mesh, fM.data());
190+
194191
return fM;
195192
}
196193

0 commit comments

Comments
 (0)