-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc_glue.cpp
executable file
·313 lines (255 loc) · 8.2 KB
/
mc_glue.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "mc_glue.h"
mc_glue::mc_glue(int x_grid_num,
int y_grid_num,
int z_grid_num,
std::string sdf_path_in,
std::string mesh_path_out)
{
all_points_record.clear();
all_faces.clear();
fTargetValue=0.0;
sdf_file_ = sdf_path_in;
mesh_path_out_ = mesh_path_out;
NX=x_grid_num;
NY=y_grid_num;
NZ=z_grid_num;
number_record_=(int*)malloc(2*sizeof(int));
number_record_[0]=0;
number_record_[1]=0;
cuda_init();
set_length_and_read_voxel();
}
void mc_glue::set_length_and_read_voxel()
{
sdf_readin_=(float*)malloc(NX*NY*NZ*sizeof(float));
FILE *fid;
fid = fopen(sdf_file_.c_str(),"rb");
for(int i=0;i<NX;i++)
{
for(int j=0;j<NY;j++)
{
for(int k=0;k<NZ;k++)
{
float oc;
fread(&oc,sizeof(float),1,fid);
sdf_readin_[i*NY*NZ+j*NZ+k]=oc;
}
}
}
fclose(fid);
edge_point_state=(int*)malloc(NX*NY*NZ*3*sizeof(int));
for (int i=0;i<NX;i++)
{
for (int j=0;j<NY;j++)
{
for(int k=0;k<NZ;k++)
{
for(int l=0;l<3;l++)
{
edge_point_state[i*NY*NZ*3+j*NZ*3+k*3+l]=-1;
}
}
}
}
cuda_get_sdf_values();
}
float mc_glue::fGetOffset(float fValue1, float fValue2, float fValueDesired)
{
double fDelta = fValue2 - fValue1;
if(fDelta == 0.0)
{
return 0.5;
}
return (fValueDesired - fValue1)/fDelta;
}
void mc_glue::vMarchCube1(float fX, float fY, float fZ)
{
//cout<<"dealing with "<<fX<<" "<<fY<<" "<<fZ<<endl;
int iCorner, iVertex, iVertexTest, iEdge, iTriangle, iFlagIndex, iEdgeFlags;
float fOffset;
float afCubeValue[8];
float asEdgeVertex[12][3];
//coor_vector asEdgeNorm[12];
for(iVertex = 0; iVertex < 8; iVertex++)
{
afCubeValue[iVertex] =(float)sdf_readin_[(int)(fX + a2fVertexOffset[iVertex][0])*NY*NZ+(int)(fY + a2fVertexOffset[iVertex][1])*NZ+(int)(fZ + a2fVertexOffset[iVertex][2])];
//cout<<a2fVertexOffset[iVertex][0]<<","<<a2fVertexOffset[iVertex][1]<<","<<a2fVertexOffset[iVertex][2]<<":"<<afCubeValue[iVertex]<<endl;
}
iFlagIndex = 0;
for(iVertexTest = 0; iVertexTest < 8; iVertexTest++)
{
if(afCubeValue[iVertexTest] < fTargetValue)
iFlagIndex |= 1<<iVertexTest;
}
iEdgeFlags = aiCubeEdgeFlags[iFlagIndex];
if(iEdgeFlags == 0)
{
return;
}
for(iEdge = 0; iEdge < 12; iEdge++)
{
if(iEdgeFlags & (1<<iEdge))
{
fOffset = fGetOffset(afCubeValue[ a2iEdgeConnection[iEdge][0] ],
afCubeValue[ a2iEdgeConnection[iEdge][1] ], fTargetValue);
asEdgeVertex[iEdge][0] = fX + (a2fVertexOffset[ a2iEdgeConnection[iEdge][0] ][0] + fOffset * a2fEdgeDirection[iEdge][0]) ;//* fScale;
asEdgeVertex[iEdge][1] = fY + (a2fVertexOffset[ a2iEdgeConnection[iEdge][0] ][1] + fOffset * a2fEdgeDirection[iEdge][1]) ;//* fScale;
asEdgeVertex[iEdge][2] = fZ + (a2fVertexOffset[ a2iEdgeConnection[iEdge][0] ][2] + fOffset * a2fEdgeDirection[iEdge][2]) ;//* fScale;
//vGetNormal(asEdgeNorm[iEdge], asEdgeVertex[iEdge].fX, asEdgeVertex[iEdge].fY, asEdgeVertex[iEdge].fZ);
}
}
for(iTriangle = 0; iTriangle < 5; iTriangle++)
{
//std::cout<<"There is a new triangle."<<std::endl;
if(a2iTriangleConnectionTable[iFlagIndex][3*iTriangle] < 0)
break;
Eigen::Vector3i af;
for(iCorner = 0; iCorner < 3; iCorner++)
{
iVertex = a2iTriangleConnectionTable[iFlagIndex][3*iTriangle+iCorner];
int basex,basey,basez;
int direction;
if(iVertex==0)
{
basex=fX;
basey=fY;
basez=fZ;
direction=0;
}else if(iVertex==1)
{
basex=fX+1;
basey=fY;
basez=fZ;
direction=1;
}else if(iVertex==2)
{
basex=fX;
basey=fY+1;
basez=fZ;
direction=0;
}else if(iVertex==3)
{
basex=fX;
basey=fY;
basez=fZ;
direction=1;
}else if(iVertex==4)
{
basex=fX;
basey=fY;
basez=fZ+1;
direction=0;
}else if(iVertex==5)
{
basex=fX+1;
basey=fY;
basez=fZ+1;
direction=1;
}else if(iVertex==6)
{
basex=fX;
basey=fY+1;
basez=fZ+1;
direction=0;
}else if(iVertex==7)
{
basex=fX;
basey=fY;
basez=fZ+1;
direction=1;
}else if(iVertex==8)
{
basex=fX;
basey=fY;
basez=fZ;
direction=2;
}else if(iVertex==9)
{
basex=fX+1;
basey=fY;
basez=fZ;
direction=2;
}else if(iVertex==10)
{
basex=fX+1;
basey=fY+1;
basez=fZ;
direction=2;
}else if(iVertex==11)
{
basex=fX;
basey=fY+1;
basez=fZ;
direction=2;
}
if(edge_point_state[basex*NY*NZ*3+basey*NZ*3+basez*3+direction]==-1)
{
edge_point_state[basex*NY*NZ*3+basey*NZ*3+basez*3+direction]=all_points_record.size();
Eigen::Vector3f av;
av<<
asEdgeVertex[iVertex][0],asEdgeVertex[iVertex][1],asEdgeVertex[iVertex][2];
all_points_record.push_back(av);
af(iCorner)=all_points_record.size()-1;
}else
{
af(iCorner)=edge_point_state[basex*NY*NZ*3+basey*NZ*3+basez*3+direction];
}
}
all_faces.push_back(af);
}
}
void mc_glue::mc_get_mesh()
{
clock_t s,e;
s=clock();
for(int i=0;i<NX-1;i++)
{
for(int j=0;j<NY-1;j++)
{
for(int k=0;k<NZ-1;k++)
{
vMarchCube1(i,j,k);
}
}
}
std::ofstream ouf;
ouf.open(mesh_path_out_);
for (int v =0; v<all_points_record.size();v++)
{
ouf<<"v "<<all_points_record[v](0)<<" "<<all_points_record[v](1)<<" "<<all_points_record[v](2)<<std::endl;
}
for (int f =0; f<all_faces.size();f++)
{
ouf<<"f "<<all_faces[f](2)+1<<" "<<all_faces[f](1)+1<<" "<<all_faces[f](0)+1<<std::endl;
}
ouf.close();
std::cout<<"The result mesh has been saved."<<std::endl;
std::cout<<std::endl;
std::cout<<std::endl;
}
void mc_glue::mc_get_mesh_cuda()
{
mc_get_mesh_on_gpu();
cudaMemcpy(number_record_,d_number_record_,sizeof(int)*2,cudaMemcpyDeviceToHost);
// std::cout<<number_record_[0]<<" "<<number_record_[1]<<std::endl;
convert_ijkd_to_pindex_on_gpu();
cudaMemcpy(number_record_,d_number_record_,sizeof(int)*2,cudaMemcpyDeviceToHost);
faces_index_ = (int*)malloc(number_record_[1]*3*sizeof(int));
points_coor_ = (float*)malloc(number_record_[0]*3*sizeof(float));;
cudaMemcpy(faces_index_,d_faces_index_,sizeof(int)*number_record_[1]*3,cudaMemcpyDeviceToHost);
cudaMemcpy(points_coor_,d_points_coor_,sizeof(float)*number_record_[0]*3,cudaMemcpyDeviceToHost);
std::ofstream ouf;
ouf.open(mesh_path_out_);
for (int v =0; v<number_record_[0];v++)
{
ouf<<"v "<<points_coor_[v*3]<<" "<<points_coor_[v*3+1]<<" "<<points_coor_[v*3+2]<<std::endl;
}
for (int f =0; f<number_record_[1];f++)
{
ouf<<"f "<<faces_index_[f*3]+1<<" "<<faces_index_[f*3+1]+1<<" "<<faces_index_[f*3+2]+1<<std::endl;
}
ouf.close();
std::cout<<"The result mesh has been saved."<<std::endl;
std::cout<<std::endl;
std::cout<<std::endl;
}