Visual Computing Library
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/polygon.h>
27#include <vclib/mesh/requirements.h>
28#include <vclib/mesh/tmp_meshes.h>
29#include <vclib/misc/logger.h>
30
31namespace vcl {
32
42template<PolygonMeshConcept MeshType, LoggerConcept LogType = NullLogger>
44{
45 using VertexType = MeshType::VertexType;
46 using CoordType = VertexType::CoordType;
47 using ScalarType = CoordType::ScalarType;
48
49 MeshType mesh;
50
51 const ScalarType fi = (1 + std::sqrt(5)) / 2;
52 const ScalarType s0 = 0;
53 const ScalarType s1 = fi;
54 const ScalarType s2 = std::pow(fi, 2);
55 const ScalarType s3 = 1;
56
57 log.log(0, "Adding vertices to PolyMesh...");
58
59 mesh.addVertices(
60 CoordType(-s1, -s1, s1),
61 CoordType(s2, s3, s0),
62 CoordType(s2, -s3, s0),
63 CoordType(-s2, s3, s0),
64 CoordType(-s2, -s3, s0),
65 CoordType(s0, s2, s3),
66 CoordType(s0, s2, -s3),
67 CoordType(s3, s0, -s2),
68 CoordType(-s3, s0, -s2),
69 CoordType(s0, -s2, -s3),
70 CoordType(s0, -s2, s3),
71 CoordType(s3, s0, s2),
72 CoordType(-s3, s0, s2),
73 CoordType(s1, s1, -s1),
74 CoordType(s1, s1, s1),
75 CoordType(-s1, s1, -s1),
76 CoordType(-s1, s1, s1),
77 CoordType(s1, -s1, -s1),
78 CoordType(s1, -s1, s1),
79 CoordType(-s1, -s1, -s1));
80
81 log.log(50, "Vertices added to PolyMesh.");
82 log.log(50, "Adding faces to PolyMesh...");
83
84 mesh.reserveFaces(12);
85 mesh.addFace(14, 11, 18, 2, 1);
86 mesh.addFace(2, 17, 7, 13, 1);
87 mesh.addFace(15, 8, 19, 4, 3);
88 mesh.addFace(4, 0, 12, 16, 3);
89 mesh.addFace(16, 5, 6, 15, 3);
90 mesh.addFace(13, 6, 5, 14, 1);
91 mesh.addFace(18, 10, 9, 17, 2);
92 mesh.addFace(19, 9, 10, 0, 4);
93 mesh.addFace(17, 9, 19, 8, 7);
94 mesh.addFace(13, 7, 8, 15, 6);
95 mesh.addFace(16, 12, 11, 14, 5);
96 mesh.addFace(18, 11, 12, 0, 10);
97
98 log.log(100, "Faces added to PolyMesh.");
99
100 return mesh;
101}
102
115template<TriangleMeshConcept MeshType, LoggerConcept LogType = NullLogger>
116MeshType createDodecahedron(LogType& log = nullLogger)
117{
118 log.startNewTask(0, 75, "Create Polygonal Dodecahedron.");
119
120 detail::TMPSimplePolyMesh pmesh =
121 createDodecahedron<detail::TMPSimplePolyMesh>(log);
122
123 log.endTask("Create Polygonal Dodecahedron.");
124 log.log(75, "Copying vertices into TriMesh...");
125
126 MeshType mesh;
127 using ST = MeshType::VertexType::CoordType::ScalarType;
128 mesh.reserveVertices(pmesh.vertexNumber());
129 for (const auto& v : pmesh.vertices()) {
130 mesh.addVertex(v.coord().cast<ST>());
131 }
132
133 log.log(80, "Vertices copied into TriMesh.");
134 log.log(80, "Triangularize and copy Faces into TriMesh...");
135
136 for (const auto& f : pmesh.faces()) {
137 std::vector<uint> ind = earCut(f);
138 for (uint i = 0; i < ind.size(); i += 3) {
139 mesh.addFace(
140 pmesh.index(f.vertex(ind[i])),
141 pmesh.index(f.vertex(ind[i + 1])),
142 pmesh.index(f.vertex(ind[i + 2])));
143 }
144 }
145
146 log.log(100, "Faces triangularized and copied into TriMesh.");
147
148 return mesh;
149}
150
151} // namespace vcl
152
153#endif // VCL_ALGORITHMS_MESH_CREATE_DODECAHEDRON_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
std::vector< uint > earCut(Iterator begin, Iterator end)
Triangulates a simple polygon with no holes using the ear-cutting algorithm.
Definition ear_cut.h:92
MeshType createDodecahedron(LogType &log=nullLogger)
Creates and returns a Polygon Mesh containing a Dodecahedron.
Definition dodecahedron.h:43
NullLogger nullLogger
The nullLogger object is an object of type NullLogger that is used as default argument in the functio...
Definition null_logger.h:125
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:52