-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClusterToVox.cpp
205 lines (186 loc) · 5.9 KB
/
ClusterToVox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// Created by User on 2024/2/22.
//
#include "ClusterToVox.h"
#include "ifcComp.h"
template <class HDS>
class Build_cube : public CGAL::Modifier_base<HDS>
{
public:
Build_cube(Eigen::Vector3i position,double resolution) {
this->position = position;
this->resolution = resolution;
}
void operator()(HDS &hds)
{
bool debug = false;
// Postcondition: hds is a valid polyhedral surface.
CGAL::Polyhedron_incremental_builder_3<HDS> B(hds, true);
B.begin_surface(8, 2, 16);//vertices, facets, halfedges
typedef typename HDS::Vertex Vertex;
typedef typename Vertex::Point Point;
//create a box using resolution as size and position
Eigen::Vector3d minBound(
(position.x()-0.5)*resolution,
(position.y()-0.5)*resolution,
(position.z()-0.5)*resolution);
Eigen::Vector3d maxBound(
(position.x()+0.5)*resolution,
(position.y()+0.5)*resolution,
(position.z()+0.5)*resolution);
for (size_t i = 0; i < 8; i++)
{
B.add_vertex(Point(
(i & 1) ? maxBound.x() : minBound.x(),
(i & 2) ? maxBound.y() : minBound.y(),
(i & 4) ? maxBound.z() : minBound.z()));
if (debug)
{
std::cout << "vertex: " << i << " "
<< ((i & 1) ? 1:0) << " "
<< ((i & 2) ? 1:0) << " "
<< ((i & 4) ? 1:0) << std::endl;
}
}
/*
6--------7
4--------5
2--------3
0--------1
*/
//clockwise
//lower
B.begin_facet();
B.add_vertex_to_facet(0);
B.add_vertex_to_facet(1);
B.add_vertex_to_facet(3);
B.add_vertex_to_facet(2);
B.end_facet();
//upper
B.begin_facet();
B.add_vertex_to_facet(4);
B.add_vertex_to_facet(6);
B.add_vertex_to_facet(7);
B.add_vertex_to_facet(5);
B.end_facet();
//front
B.begin_facet();
B.add_vertex_to_facet(0);
B.add_vertex_to_facet(4);
B.add_vertex_to_facet(5);
B.add_vertex_to_facet(1);
B.end_facet();
//back
B.begin_facet();
B.add_vertex_to_facet(3);
B.add_vertex_to_facet(7);
B.add_vertex_to_facet(6);
B.add_vertex_to_facet(2);
B.end_facet();
//left
B.begin_facet();
B.add_vertex_to_facet(2);
B.add_vertex_to_facet(6);
B.add_vertex_to_facet(4);
B.add_vertex_to_facet(0);
B.end_facet();
//right
B.begin_facet();
B.add_vertex_to_facet(1);
B.add_vertex_to_facet(5);
B.add_vertex_to_facet(7);
B.add_vertex_to_facet(3);
B.end_facet();
B.end_surface();
}
private:
Eigen::Vector3i position;
double resolution;
};
template <typename T>
struct hash_eigen {
std::size_t operator()(T const& matrix) const {
size_t hash_seed = 0;
for (int i = 0; i < (int)matrix.size(); i++) {
auto elem = *(matrix.data() + i);
hash_seed ^= std::hash<typename T::Scalar>()(elem) + 0x9e3779b9 +
(hash_seed << 6) + (hash_seed >> 2);
}
return hash_seed;
}
};
void ClusterToVox::clusterToVox()
{
}
Polyhedron ClusterToVox::cluster(const Polyhedron &mesh, double resolution)
{
double scale = 1.0f / resolution;
auto poly = mesh;
std::vector<Eigen::Vector3d> points;
auto f = poly.facets_begin();
// do {
// auto v = f->facet_begin();
// std::queue<Eigen::Vector3d> fvertices;
// do
// {
// } while (v != f.vertices_end());
// //get the distance between the two points
// double distance = CGAL::sqrt(CGAL::squared_distance(startPoint, endPoint));
// auto diff = endPoint - startPoint;
// auto step = Normalize(diff)*resolution;
// for (double i = 0; i < distance; i += resolution)
// {
// points.push_back(startPoint + diff * i * resolution);
// }
// } while (++f != poly.facets_end());
std::unordered_set<Eigen::Vector3i,hash_eigen<Eigen::Vector3i>> clusters;
for (const auto &p : poly.points())
{
Eigen::Vector3i v;
v << static_cast<int>(floor(p.x() * scale)), static_cast<int>(floor(p.y() * scale)), static_cast<int>(floor(p.z() * scale));
if (clusters.find(v) == clusters.end())
{
clusters.insert(v);
}
}
Polyhedron result;
//build a tree, connect the points in the clusters if the distance is less than 1
//for each cluster, find the nearest cluster, and connect them
while (clusters.size() > 0)
{
Eigen::Vector3i v =*clusters.begin();
clusters.erase(clusters.begin());
GenerateVoxel(v, resolution, result);
}
return result;
}
void ClusterToVox::GenerateVoxel(Eigen::Vector3i v, double resolution, Polyhedron &result)
{
//build a cube
Build_cube<HalfedgeDS> cube(v,resolution);
result.delegate(cube);
}
template <class HDS>
class Remove_triangle : public CGAL::Modifier_base<HDS>
{
public:
Remove_triangle(Eigen::Vector3i posi,double resolution) {
}
void operator()(HDS &hds)
{
// Postcondition: hds is a valid polyhedral surface.
CGAL::Polyhedron_incremental_builder_3<HDS> B(hds, true);
B.begin_surface(3, 1, 6);
typedef typename HDS::Vertex Vertex;
typedef typename Vertex::Point Point;
B.add_vertex(Point(0, 0, 0));
B.add_vertex(Point(1, 0, 0));
B.add_vertex(Point(0, 1, 0));
B.begin_facet();
B.add_vertex_to_facet(0);
B.add_vertex_to_facet(1);
B.add_vertex_to_facet(2);
B.end_facet();
B.end_surface();
}
};