Visual Computing Library  devel
Loading...
Searching...
No Matches
dodecahedron.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_ALGORITHMS_MESH_CREATE_DODECAHEDRON_H
24#define VCL_ALGORITHMS_MESH_CREATE_DODECAHEDRON_H
25
26#include <vclib/algorithms/core.h>
27#include <vclib/mesh.h>
28
29namespace vcl {
30
40template<PolygonMeshConcept MeshType, LoggerConcept LogType = NullLogger>
42{
43 using VertexType = MeshType::VertexType;
44 using PositionType = VertexType::PositionType;
45 using ScalarType = PositionType::ScalarType;
46
47 MeshType mesh;
48
49 const ScalarType fi = (1 + std::sqrt(5)) / 2;
50 const ScalarType s0 = 0;
51 const ScalarType s1 = fi;
52 const ScalarType s2 = std::pow(fi, 2);
53 const ScalarType s3 = 1;
54
55 log.log(0, "Adding vertices to PolyMesh...");
56
57 mesh.addVertices(
58 PositionType(-s1, -s1, s1),
59 PositionType(s2, s3, s0),
60 PositionType(s2, -s3, s0),
61 PositionType(-s2, s3, s0),
62 PositionType(-s2, -s3, s0),
63 PositionType(s0, s2, s3),
64 PositionType(s0, s2, -s3),
65 PositionType(s3, s0, -s2),
66 PositionType(-s3, s0, -s2),
67 PositionType(s0, -s2, -s3),
68 PositionType(s0, -s2, s3),
69 PositionType(s3, s0, s2),
70 PositionType(-s3, s0, s2),
71 PositionType(s1, s1, -s1),
72 PositionType(s1, s1, s1),
73 PositionType(-s1, s1, -s1),
74 PositionType(-s1, s1, s1),
75 PositionType(s1, -s1, -s1),
76 PositionType(s1, -s1, s1),
77 PositionType(-s1, -s1, -s1));
78
79 log.log(50, "Vertices added to PolyMesh.");
80 log.log(50, "Adding faces to PolyMesh...");
81
82 mesh.reserveFaces(12);
83 mesh.addFace(14, 11, 18, 2, 1);
84 mesh.addFace(2, 17, 7, 13, 1);
85 mesh.addFace(15, 8, 19, 4, 3);
86 mesh.addFace(4, 0, 12, 16, 3);
87 mesh.addFace(16, 5, 6, 15, 3);
88 mesh.addFace(13, 6, 5, 14, 1);
89 mesh.addFace(18, 10, 9, 17, 2);
90 mesh.addFace(19, 9, 10, 0, 4);
91 mesh.addFace(17, 9, 19, 8, 7);
92 mesh.addFace(13, 7, 8, 15, 6);
93 mesh.addFace(16, 12, 11, 14, 5);
94 mesh.addFace(18, 11, 12, 0, 10);
95
96 log.log(100, "Faces added to PolyMesh.");
97
98 return mesh;
99}
100
113template<TriangleMeshConcept MeshType, LoggerConcept LogType = NullLogger>
114MeshType createDodecahedron(LogType& log = nullLogger)
115{
116 log.startNewTask(0, 75, "Create Polygonal Dodecahedron.");
117
118 detail::TMPSimplePolyMesh pmesh =
119 createDodecahedron<detail::TMPSimplePolyMesh>(log);
120
121 log.endTask("Create Polygonal Dodecahedron.");
122 log.log(75, "Copying vertices into TriMesh...");
123
124 MeshType mesh;
125 using ST = MeshType::VertexType::PositionType::ScalarType;
126 mesh.reserveVertices(pmesh.vertexNumber());
127 for (const auto& v : pmesh.vertices()) {
128 mesh.addVertex(v.position().cast<ST>());
129 }
130
131 log.log(80, "Vertices copied into TriMesh.");
132 log.log(80, "Triangularize and copy Faces into TriMesh...");
133
134 for (const auto& f : pmesh.faces()) {
135 std::vector<uint> ind = earCut(f);
136 for (uint i = 0; i < ind.size(); i += 3) {
137 mesh.addFace(
138 pmesh.index(f.vertex(ind[i])),
139 pmesh.index(f.vertex(ind[i + 1])),
140 pmesh.index(f.vertex(ind[i + 2])));
141 }
142 }
143
144 log.log(100, "Faces triangularized and copied into TriMesh.");
145
146 return mesh;
147}
148
149} // namespace vcl
150
151#endif // VCL_ALGORITHMS_MESH_CREATE_DODECAHEDRON_H
A class representing a box in N-dimensional space.
Definition box.h:46
NullLogger nullLogger
The nullLogger object is an object of type NullLogger that is used as default argument in the functio...
Definition null_logger.h:123
std::vector< uint > earCut(Iterator begin, Iterator end)
Triangulates a simple polygon with no holes using the ear-cutting algorithm.
Definition ear_cut.h:90
MeshType createDodecahedron(LogType &log=nullLogger)
Creates and returns a Polygon Mesh containing a Dodecahedron.
Definition dodecahedron.h:41
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:84