-
Notifications
You must be signed in to change notification settings - Fork 0
/
marchingcubes.h
253 lines (219 loc) · 9.35 KB
/
marchingcubes.h
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
HiroIshida modified orignal source code in https://github.com/pmneila/PyMCubes by P. M. Neila.
(The oriinal code is distributed with BSD 3-clause license)
Copyright (c) 2012-2015, P. M. Neila
*/
#ifndef _MARCHING_CUBES_H
#define _MARCHING_CUBES_H
#include <stddef.h>
#include <array>
#include <Eigen/Dense>
#include <vector>
#include <iostream>
#include "tablemanager.h"
namespace mc
{
using namespace Eigen;
using namespace std;
extern int edge_table[256];
extern int triangle_table[256][16];
namespace private_
{
double mc_isovalue_interpolation(double isovalue, double f1, double f2,
double x1, double x2);
size_t mc_add_vertex(double x1, double y1, double z1, double c2,
int axis, double f1, double f2, double isovalue, std::vector<double>* vertices);
}
template<typename vector3, typename formula>
tuple<MatrixXd, MatrixXi, vector<uint>, vector<uint>, vector<bool>>
marching_cubes(const vector3& lower, const vector3& upper, int numx, int numy, int numz, formula f, double isovalue, TableManager& tm
)
{
using coord_type = typename vector3::value_type;
using size_type = typename vector3::size_type;
using namespace private_;
/* TODO raise exception
// Some initial checks
if(numx < 2 || numy < 2 || numz < 2)
return;
if(!std::equal(std::begin(lower), std::end(lower), std::begin(upper),
[](double a, double b)->bool {return a <= b;}))
return;
*/
// numx, numy and numz are the numbers of evaluations in each direction
--numx; --numy; --numz;
coord_type dx = (upper[0] - lower[0]) / static_cast<coord_type>(numx);
coord_type dy = (upper[1] - lower[1]) / static_cast<coord_type>(numy);
coord_type dz = (upper[2] - lower[2]) / static_cast<coord_type>(numz);
const int num_shared_indices = 2 * (numy + 1) * (numz + 1);
std::vector<size_type> shared_indices_x(num_shared_indices);
std::vector<size_type> shared_indices_y(num_shared_indices);
std::vector<size_type> shared_indices_z(num_shared_indices);
auto _offset = [&](size_t i, size_t j, size_t k){return i*(numy+1)*(numz+1) + j*(numz+1) + k;};
vector<double> vertices;
vector<int> polygons;
vector<vector<unsigned int>> neighbor_faces;
for(int i=0; i<numx; ++i)
{
coord_type x = lower[0] + dx*i;
coord_type x_dx = lower[0] + dx*(i+1);
const int i_mod_2 = i % 2;
const int i_mod_2_inv = (i_mod_2 ? 0 : 1);
for(int j=0; j<numy; ++j)
{
coord_type y = lower[1] + dy*j;
coord_type y_dy = lower[1] + dy*(j+1);
double v[8];
v[4] = f(x, y, lower[2]); v[5] = f(x_dx, y, lower[2]);
v[6] = f(x_dx, y_dy, lower[2]); v[7] = f(x, y_dy, lower[2]);
for(int k=0; k<numz; ++k)
{
coord_type z = lower[2] + dz*k;
coord_type z_dz = lower[2] + dz*(k+1);
v[0] = v[4]; v[1] = v[5];
v[2] = v[6]; v[3] = v[7];
v[4] = f(x, y, z_dz); v[5] = f(x_dx, y, z_dz);
v[6] = f(x_dx, y_dy, z_dz); v[7] = f(x, y_dy, z_dz);
unsigned int cubeindex = 0;
for(int m=0; m<8; ++m)
if(v[m] <= isovalue)
cubeindex |= 1<<m;
// Generate vertices AVOIDING DUPLICATES.
int edges = edge_table[cubeindex];
std::array<size_type, 12> indices;
if(edges & 0x040)
{
size_t index = mc_add_vertex(x_dx, y_dy, z_dz, x, 0, v[6], v[7], isovalue, &vertices);
indices[6] = index;
shared_indices_x[_offset(i_mod_2_inv, j+1, k+1)] = index;
}
if(edges & 0x020)
{
size_t index = mc_add_vertex(x_dx, y, z_dz, y_dy, 1, v[5], v[6], isovalue, &vertices);
indices[5] = index;
shared_indices_y[_offset(i_mod_2_inv, j+1, k+1)] = index;
}
if(edges & 0x400)
{
size_t index = mc_add_vertex(x_dx, y_dy, z, z_dz, 2, v[2], v[6], isovalue, &vertices);
indices[10] = index;
shared_indices_z[_offset(i_mod_2_inv, j+1, k+1)] = index;
}
if(edges & 0x001)
{
if(j == 0 && k == 0)
{
size_t index = mc_add_vertex(x, y, z, x_dx, 0, v[0], v[1], isovalue, &vertices);
indices[0] = index;
}
else
indices[0] = shared_indices_x[_offset(i_mod_2_inv, j, k)];
}
if(edges & 0x002)
{
if(k == 0)
{
size_t index = mc_add_vertex(x_dx, y, z, y_dy, 1, v[1], v[2], isovalue, &vertices);
indices[1] = index;
shared_indices_y[_offset(i_mod_2_inv, j+1, k)] = index;
}
else
indices[1] = shared_indices_y[_offset(i_mod_2_inv, j+1, k)];
}
if(edges & 0x004)
{
if(k == 0)
{
size_t index = mc_add_vertex(x_dx, y_dy, z, x, 0, v[2], v[3], isovalue, &vertices);
indices[2] = index;
shared_indices_x[_offset(i_mod_2_inv, j+1, k)] = index;
}
else
indices[2] = shared_indices_x[_offset(i_mod_2_inv, j+1, k)];
}
if(edges & 0x008)
{
if(i == 0 && k == 0)
{
size_t index = mc_add_vertex(x, y_dy, z, y, 1, v[3], v[0], isovalue, &vertices);
indices[3] = index;
}
else
indices[3] = shared_indices_y[_offset(i_mod_2, j+1, k)];
}
if(edges & 0x010)
{
if(j == 0)
{
size_t index = mc_add_vertex(x, y, z_dz, x_dx, 0, v[4], v[5], isovalue, &vertices);
indices[4] = index;
shared_indices_x[_offset(i_mod_2_inv, j, k+1)] = index;
}
else
indices[4] = shared_indices_x[_offset(i_mod_2_inv, j, k+1)];
}
if(edges & 0x080)
{
if(i == 0)
{
size_t index = mc_add_vertex(x, y_dy, z_dz, y, 1, v[7], v[4], isovalue, &vertices);
indices[7] = index;
shared_indices_y[_offset(i_mod_2, j+1, k+1)] = index;
}
else
indices[7] = shared_indices_y[_offset(i_mod_2, j+1, k+1)];
}
if(edges & 0x100)
{
if(i == 0 && j == 0)
{
size_t index = mc_add_vertex(x, y, z, z_dz, 2, v[0], v[4], isovalue, &vertices);
indices[8] = index;
}
else
indices[8] = shared_indices_z[_offset(i_mod_2, j, k+1)];
}
if(edges & 0x200)
{
if(j == 0)
{
size_t index = mc_add_vertex(x_dx, y, z, z_dz, 2, v[1], v[5], isovalue, &vertices);
indices[9] = index;
shared_indices_z[_offset(i_mod_2_inv, j, k+1)] = index;
}
else
indices[9] = shared_indices_z[_offset(i_mod_2_inv, j, k+1)];
}
if(edges & 0x800)
{
if(i == 0)
{
size_t index = mc_add_vertex(x, y_dy, z, z_dz, 2, v[3], v[7], isovalue, &vertices);
indices[11] = index;
shared_indices_z[_offset(i_mod_2, j+1, k+1)] = index;
}
else
indices[11] = shared_indices_z[_offset(i_mod_2, j+1, k+1)];
}
int tri;
int* triangle_table_ptr = triangle_table[cubeindex];
for(int m=0; tri = triangle_table_ptr[m], tri != -1; ++m){
int idx_vertex = indices[tri];
polygons.push_back(idx_vertex);
int idx_polygon = (polygons.size() - 1)/ 3;
tm.add_element(idx_vertex, idx_polygon);
}
}
}
}
auto res_tuple = tm.connected_components(vertices, polygons);
//auto& vertex_color_vector = std::get<0>(res_tuple);
auto& vertex_color_vector = std::get<0>(res_tuple);
auto& facet_color_vector = std::get<1>(res_tuple);
auto& is_closed_vec = std::get<2>(res_tuple);
MatrixXd V = Map<Matrix<double, Dynamic, Dynamic, RowMajor>>(vertices.data(), vertices.size()/3, 3);
MatrixXi P = Map<Matrix<int, Dynamic, Dynamic, RowMajor>>(polygons.data(), polygons.size()/3, 3);
return std::make_tuple(V, P, vertex_color_vector, facet_color_vector, is_closed_vec);
}
}
#endif // _MARCHING_CUBES_H