gtsam_points
Loading...
Searching...
No Matches
include
glk
primitives
icosahedron.hpp
Go to the documentation of this file.
1
#ifndef GLK_PRIMITIVES_ICOSAHEDRON_HPP
2
#define GLK_PRIMITIVES_ICOSAHEDRON_HPP
3
4
#include <map>
5
#include <vector>
6
#include <Eigen/Core>
7
8
namespace
glk
{
9
10
class
Icosahedron
{
11
public
:
12
Icosahedron
() {
13
double
t
= (1.0 + std::sqrt(5.0)) / 2.0;
14
15
std::vector<Eigen::Vector3f>
vertices
= {
16
Eigen::Vector3f(-1,
t
, 0),
17
Eigen::Vector3f(1,
t
, 0),
18
Eigen::Vector3f(-1, -
t
, 0),
19
Eigen::Vector3f(1, -
t
, 0),
20
Eigen::Vector3f(0, -1,
t
),
21
Eigen::Vector3f(0, 1,
t
),
22
Eigen::Vector3f(0, -1, -
t
),
23
Eigen::Vector3f(0, 1, -
t
),
24
Eigen::Vector3f(
t
, 0, -1),
25
Eigen::Vector3f(
t
, 0, 1),
26
Eigen::Vector3f(-
t
, 0, -1),
27
Eigen::Vector3f(-
t
, 0, 1)};
28
29
std::vector<Eigen::Vector3f>
normals
(
vertices
.size());
30
for
(
int
i
= 0;
i
<
vertices
.size();
i
++) {
31
normals
[
i
] =
vertices
[
i
].normalized();
32
}
33
34
std::vector<int>
indices
= {0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11, 1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
35
3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9, 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1};
36
37
this->vertices.swap(
vertices
);
38
this->normals.swap(
normals
);
39
this->indices.swap(
indices
);
40
}
41
42
void
subdivide
() {
43
std::vector<int>
new_indices
;
44
for
(
int
i
= 0;
i
<
indices
.size();
i
+= 3) {
45
int
a
= insert_middle_point(
indices
[
i
],
indices
[
i
+ 1]);
46
int
b
= insert_middle_point(
indices
[
i
+ 1],
indices
[
i
+ 2]);
47
int
c
= insert_middle_point(
indices
[
i
+ 2],
indices
[
i
]);
48
49
std::vector<int>
tessellated
= {
indices
[
i
],
a
,
c
,
indices
[
i
+ 1],
b
,
a
,
indices
[
i
+ 2],
c
,
b
,
a
,
b
,
c
};
50
51
new_indices
.insert(
new_indices
.end(),
tessellated
.begin(),
tessellated
.end());
52
}
53
54
indices
.swap(
new_indices
);
55
56
normals
.resize(
vertices
.size());
57
for
(
int
i
= 0;
i
<
vertices
.size();
i
++) {
58
normals
[
i
] =
vertices
[
i
].normalized();
59
}
60
}
61
62
void
spherize
() {
63
for
(
auto
&
vertex
:
vertices
) {
64
vertex
.normalize();
65
}
66
67
normals
.resize(
vertices
.size());
68
for
(
int
i
= 0;
i
<
vertices
.size();
i
++) {
69
normals
[
i
] =
vertices
[
i
].normalized();
70
}
71
}
72
73
private
:
74
int
insert_middle_point(
int
v1
,
int
v2
) {
75
int
smaller
= std::min(
v1
,
v2
);
76
int
greater
= std::max(
v1
,
v2
);
77
int
key
= (
smaller
<< 16) +
greater
;
78
79
auto
found
=
middle_points_cache
.find(
key
);
80
if
(
found
!=
middle_points_cache
.end()) {
81
return
found
->second;
82
}
83
84
Eigen::Vector3f
new_v
= (
vertices
[
v1
] +
vertices
[
v2
]) / 2.0f;
85
vertices
.push_back(
new_v
);
86
87
middle_points_cache
.insert(
found
, std::make_pair(
key
,
vertices
.size() - 1));
88
return
vertices
.size() - 1;
89
}
90
91
public
:
92
std::vector<Eigen::Vector3f>
vertices
;
93
std::vector<Eigen::Vector3f>
normals
;
94
std::vector<int>
indices
;
95
96
std::map<int, int>
middle_points_cache
;
97
};
98
99
}
// namespace glk
100
101
#endif
glk::Icosahedron
Definition
icosahedron.hpp:10
glk::Icosahedron::subdivide
void subdivide()
Definition
icosahedron.hpp:42
glk::Icosahedron::Icosahedron
Icosahedron()
Definition
icosahedron.hpp:12
glk::Icosahedron::vertices
std::vector< Eigen::Vector3f > vertices
Definition
icosahedron.hpp:92
glk::Icosahedron::indices
std::vector< int > indices
Definition
icosahedron.hpp:94
glk::Icosahedron::middle_points_cache
std::map< int, int > middle_points_cache
Definition
icosahedron.hpp:96
glk::Icosahedron::spherize
void spherize()
Definition
icosahedron.hpp:62
glk::Icosahedron::normals
std::vector< Eigen::Vector3f > normals
Definition
icosahedron.hpp:93
glk
Definition
async_buffer_copy.hpp:6
glk::make_shared
std::enable_if_t< needs_aligned_allocator< T >::value, std::shared_ptr< T > > make_shared(Args &&... args)
Definition
make_shared.hpp:20
Generated by
1.9.8