SimpleFastOpenAtomicVisualiser
Loading...
Searching...
No Matches
hierarchicalTriangularMesh.h
Go to the documentation of this file.
1#ifndef HIERARCHICALTRIANGULARMESH_H
2#define HIERARCHICALTRIANGULARMESH_H
3
4#include <iostream>
5#include <fstream>
6#include <string>
7#include <cstdint>
8#include <vector>
9#include <array>
10#include <cmath>
11#include <memory>
12#include <limits>
13#include <numeric>
14
15#include <glm/glm.hpp>
16
17#include <trixel.h>
18#include <meshes.h>
19
34
35std::ostream & operator<<(std::ostream & o, const BASE_MESH & mesh)
36{
37 if (mesh == BASE_MESH::ICOSAHEDRON)
38 {
39 o << "ICOSAHEDRON";
40 }
41 if (mesh == BASE_MESH::OCTAHEDRON)
42 {
43 o << "OCTAHEDRON";
44 }
45 if (mesh == BASE_MESH::DODECAHEDRON)
46 {
47 o << "DODECAHEDRON";
48 }
49 if (mesh == BASE_MESH::CUBE)
50 {
51 o << "CUBE";
52 }
53 if (mesh == BASE_MESH::TETRAHEDRON)
54 {
55 o << "TETRAHEDRON";
56 }
58 {
59 o << "TRIAUGMENTED_TRIANGULAR_PRISM";
60 }
61 if (mesh == BASE_MESH::ANY)
62 {
63 o << "ANY";
64 }
65 return o;
66}
67
73template <class T>
75{
76public:
77
84 : depth(0)
85 {
86 // depth 0 hard coded
87 if (mesh == BASE_MESH::OCTAHEDRON)
88 {
89 this->mesh = OCTAHEDRON<T>;
90 }
91 else if (mesh == BASE_MESH::ICOSAHEDRON)
92 {
93 this->mesh = ICOSAHEDRON<T>;
94 }
95 else if (mesh == BASE_MESH::CUBE)
96 {
97 this->mesh = CUBE<T>;
98 }
99 else if (mesh == BASE_MESH::DODECAHEDRON)
100 {
101 this->mesh = DODECAHEDRON<T>;
102 }
103 else if (mesh == BASE_MESH::TETRAHEDRON)
104 {
105 this->mesh = TETRAHEDRON<T>;
106 }
108 {
110 }
111 centreMesh<T>(this->mesh);
112 rootMeshSize = this->mesh.size();
113 };
114
121 : depth(0), mesh(baseMesh)
122 {
123 rootMeshSize = this->mesh.size();
124 centreMesh<T>(this->mesh);
125 };
126
132 uint32_t size() const { return depth; }
133
139 uint32_t triangles() const { return rootMeshSize * std::pow(4, depth); }
140
146 void build(uint32_t depth = 0)
147 {
148 this->depth = depth;
149 if (depth == 0)
150 {
151 return;
152 }
153 for (uint32_t i = 0; i < depth; i++)
154 {
155 uint32_t start = mesh.size()-rootMeshSize*pow(4,i);
156 uint32_t end = mesh.size();
157 for (uint32_t j = start; j < end; j++)
158 {
159 std::array<Trixel<T>, 4> newTrixels = subdivideTrixel<T> (mesh[j]);
160 std::vector<uint32_t> children(4,0);
161
162 for (uint32_t k = 0; k < newTrixels.size(); k++)
163 {
164 newTrixels[k].setParent(j);
165 mesh.push_back(newTrixels[k]);
166 children[k] = mesh.size()-1;
167 }
168 mesh[j].setChildren(children);
169 }
170 }
171 return;
172 }
173
179 std::vector<Trixel<T>> leaves() const
180 {
181 std::vector<Trixel<T>> l;
182 std::vector<uint32_t> indices = leafIndices();
183 for (uint32_t i = 0; i < indices.size(); i++)
184 {
185 l.push_back(mesh[i]);
186 }
187 return l;
188 }
189
195 std::vector<T> vertices() const
196 {
197 std::vector<T> v;
198 std::vector<uint32_t> leaves = leafIndices();
199 for (uint32_t i = 0; i < leaves.size(); i++)
200 {
201 Trixel<T> trix = mesh[leaves[i]];
202 auto u = trix.getVertices();
203 for (uint32_t j = 0; j < u.size(); j++)
204 {
205 for (uint32_t k = 0; k < 3; k++)
206 {
207 v.push_back(u[j][k]);
208 }
209 }
210 }
211 return v;
212 }
213
219 std::vector<T> vertexNormals() const
220 {
221 std::vector<T> n;
222 std::vector<uint32_t> leaves = leafIndices();
223 for (uint32_t i = 0; i < leaves.size(); i++)
224 {
225 Trixel<T> trix = mesh[leaves[i]];
226 auto u = trix.normal();
227 for (uint32_t j = 0; j < 3; j++)
228 {
229 for (uint32_t k = 0; k < 3; k++)
230 {
231 n.push_back(u[k]);
232 }
233 }
234 }
235 return n;
236 }
237
238private:
239
240 uint32_t depth;
241 uint32_t rootMeshSize;
242 std::vector<Trixel<T>> mesh;
243
244 std::vector<uint32_t> leafIndices() const
245 {
246 std::vector<uint32_t> leaves;
247 std::vector<uint32_t> stack;
248 stack.resize(rootMeshSize);
249 std::iota(stack.begin(), stack.end(), 0);
250
251 while (stack.size() > 0)
252 {
253 uint32_t ptr = stack[stack.size()-1];
254 stack.pop_back();
255 Trixel<T> trix = mesh[ptr];
256 if ((trix.getChildren()).size() == 0)
257 {
258 leaves.push_back(ptr);
259 continue;
260 }
261
262 bool cont = false;
263 for (uint32_t i = 0; i < (trix.getChildren()).size(); i++)
264 {
265 if ((trix.getChildren())[i] == NULL_ID)
266 {
267 leaves.push_back(ptr);
268 cont = true;
269 break;
270 }
271 }
272 if (cont)
273 {
274 continue;
275 }
276 else
277 {
278 for (uint32_t i = 0; i < (trix.getChildren()).size(); i++)
279 {
280 stack.push_back((trix.getChildren())[i]);
281 }
282 }
283 }
284 return leaves;
285 }
286};
287
298template <class T>
300
301#endif /* HIERARCHICALTRIANGULARMESH_H */
A refineable mesh of triangles.
Definition hierarchicalTriangularMesh.h:75
void build(uint32_t depth=0)
Refine the mesh to a give depth.
Definition hierarchicalTriangularMesh.h:146
std::vector< T > vertices() const
Get the vertices of the mesh.
Definition hierarchicalTriangularMesh.h:195
HierarchicalTriangularMesh(BASE_MESH mesh)
Construct a new Hierarchical Triangular Mesh from a library base.
Definition hierarchicalTriangularMesh.h:83
uint32_t size() const
Get the number of refinements.
Definition hierarchicalTriangularMesh.h:132
uint32_t triangles() const
Get the number of triangles at the largest refinement.
Definition hierarchicalTriangularMesh.h:139
std::vector< T > vertexNormals() const
Get the normal vectors for each triangle.
Definition hierarchicalTriangularMesh.h:219
HierarchicalTriangularMesh(const std::vector< Trixel< T > > &baseMesh)
Construct a new Hierarchical Triangular Mesh from a user base mesh.
Definition hierarchicalTriangularMesh.h:120
std::vector< Trixel< T > > leaves() const
The triangulation at the current refinement.
Definition hierarchicalTriangularMesh.h:179
glm::vec< L, float, glm::qualifier::highp > vec
Definition commandLine.h:214
bool operator<(const HierarchicalTriangularMesh< T > &a, const HierarchicalTriangularMesh< T > &b)
HierarchicalTriangularMesh ordering.
Definition hierarchicalTriangularMesh.h:299
BASE_MESH
Base mesh types for refinement.
Definition hierarchicalTriangularMesh.h:25
@ TRIAUGMENTED_TRIANGULAR_PRISM
std::ostream & operator<<(std::ostream &o, const BASE_MESH &mesh)
Definition hierarchicalTriangularMesh.h:35
const uint32_t NULL_ID
The null Trixel identity.
Definition trixel.h:10