-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsoSurface.cpp
191 lines (171 loc) · 7.22 KB
/
IsoSurface.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
#include "Lattice.h"
//#include "stdlib.h"
//#include "fcntl.h"
//#include "sys\stat.h"
//#include "io.h"
void getVoxels_d(double *Vm, int sizeX, int sizeY, int sizeZ, double *** voxels)
{
// printf("forming voxels\t");
for(int x = 0; x < sizeX; x++)
{
for(int y = 0; y < sizeY; y++)
{
for (int z=0; z<sizeZ; z++)
{
voxels[x][y][z] = Vm[z*sizeX*sizeY+y*sizeX+x];
}
}
}
// printf("Voxels formed\t");
}
vertex interpolate(double isolevel, vertex p1, vertex p2, int valp1, int valp2)
{
if(fabs(isolevel - valp1) < 0.00001)
return p1;
if(fabs(isolevel - valp2) < 0.00001)
return p2;
if(fabs(valp1 - valp2) < 0.00001)
return p1;
vertex p;
double diff = (double)(isolevel - valp1) / (valp2 - valp1);
p.x = p1.x + diff * (p2.x - p1.x);
p.y = p1.y + diff * (p2.y - p1.y);
p.z = p1.z + diff * (p2.z - p1.z);
p.normal_x = p1.normal_x + diff * (p2.normal_x - p1.normal_x);
p.normal_y = p1.normal_y + diff * (p2.normal_y - p1.normal_y);
p.normal_z = p1.normal_z + diff * (p2.normal_z - p1.normal_z);
return p;
}
void processCube(cube cube, double isolevel, vector<vertex> *vertexList)
{
int cubeindex = 0;
if(cube.val[0] > isolevel) cubeindex |= 1;
if(cube.val[1] > isolevel) cubeindex |= 2;
if(cube.val[2] > isolevel) cubeindex |= 4;
if(cube.val[3] > isolevel) cubeindex |= 8;
if(cube.val[4] > isolevel) cubeindex |= 16;
if(cube.val[5] > isolevel) cubeindex |= 32;
if(cube.val[6] > isolevel) cubeindex |= 64;
if(cube.val[7] > isolevel) cubeindex |= 128;
// Cube is entirely in/out of the surface
if(edgeTable[cubeindex] == 0 || edgeTable[cubeindex] == 255)
return;
vertex vertlist[12];
// Find the vertices where the surface intersects the cube
if(edgeTable[cubeindex] & 1)
vertlist[0] = interpolate(isolevel,cube.p[0],cube.p[1],cube.val[0],cube.val[1]);
if(edgeTable[cubeindex] & 2)
vertlist[1] = interpolate(isolevel,cube.p[1],cube.p[2],cube.val[1],cube.val[2]);
if(edgeTable[cubeindex] & 4)
vertlist[2] = interpolate(isolevel,cube.p[2],cube.p[3],cube.val[2],cube.val[3]);
if(edgeTable[cubeindex] & 8)
vertlist[3] = interpolate(isolevel,cube.p[3],cube.p[0],cube.val[3],cube.val[0]);
if(edgeTable[cubeindex] & 16)
vertlist[4] = interpolate(isolevel,cube.p[4],cube.p[5],cube.val[4],cube.val[5]);
if(edgeTable[cubeindex] & 32)
vertlist[5] = interpolate(isolevel,cube.p[5],cube.p[6],cube.val[5],cube.val[6]);
if(edgeTable[cubeindex] & 64)
vertlist[6] = interpolate(isolevel,cube.p[6],cube.p[7],cube.val[6],cube.val[7]);
if(edgeTable[cubeindex] & 128)
vertlist[7] = interpolate(isolevel,cube.p[7],cube.p[4],cube.val[7],cube.val[4]);
if(edgeTable[cubeindex] & 256)
vertlist[8] = interpolate(isolevel,cube.p[0],cube.p[4],cube.val[0],cube.val[4]);
if(edgeTable[cubeindex] & 512)
vertlist[9] = interpolate(isolevel,cube.p[1],cube.p[5],cube.val[1],cube.val[5]);
if(edgeTable[cubeindex] & 1024)
vertlist[10] = interpolate(isolevel,cube.p[2],cube.p[6],cube.val[2],cube.val[6]);
if(edgeTable[cubeindex] & 2048)
vertlist[11] = interpolate(isolevel,cube.p[3],cube.p[7],cube.val[3],cube.val[7]);
for(int i = 0; triTable[cubeindex][i] != -1; i++) {
(*vertexList).push_back(vertlist[triTable[cubeindex][i]]);
}
}
vector<vertex> runMarchingCubes_d(double ***voxels, double *Der, double isovalue)
{
vector<vertex> vertexList;
// Run the processCube function on every cube in the grid
int sign;
for(int x = 1; x < N-2; x++)
{
for(int y = 1; y < N-2; y++)
{
for(int z = 1; z < H-2; z++)
{
sign = -1;
cube c =
{
{
{
x,y,z,
(double)(-voxels[x+1][y][z]+voxels[x-1][y][z]) ,
(double)(-voxels[x][y+1][z]+voxels[x][y-1][z]) ,
(double)(-voxels[x][y][z+1]+voxels[x][y][z-1])
},
{
x+1,y,z,
(double)(-voxels[x+2*1][y][z]+voxels[x][y][z]) ,
(double)(-voxels[x+1][y+1][z]+voxels[x+1][y-1][z]),
(double)(-voxels[x+1][y][z+1]+voxels[x+1][y][z-1])
},
{
x+1,y,z+1,
(double)(-voxels[x+1][y][z+1]+voxels[x][y][z+1]) ,
(double)(-voxels[x+1][y+1][z+1]+voxels[x+1][y-1][z+1]) ,
(double)(-voxels[x+1][y][z+1]+voxels[x+1][y][z])
},
{
x,y,z+1,
(double)(-voxels[x+1][y][z+1]+voxels[x-1][y][z+1]) ,
(double)(-voxels[x][y+1][z+1]+voxels[x][y-1][z+1]) ,
(double)(-voxels[x][y][z+1]+voxels[x][y][z])
},
{
x,y+1,z,
(double)(-voxels[x+1][y+1][z]+voxels[x-1][y+1][z]) ,
(double)(-voxels[x][y+1][z]+voxels[x][y][z]) ,
(double)(-voxels[x][y+1][z+1]+voxels[x][y+1][z-1])
},
{
x+1,y+1,z,
(double)(-voxels[x+1][y+1][z]+voxels[x+1][y+1][z]) ,
(double)(-voxels[x+1][y+1][z]+voxels[x+1][y][z]) ,
(double)(-voxels[x+1][y+1][z+1]+voxels[x+1][y+1][z-1])
},
{x+1,y+1,z+1,
(double)(-voxels[x+1][y+1][z+1]+voxels[x][y+1][z+1]) ,
(double)(-voxels[x+1][y+1][z+1]+voxels[x+1][y][z+1]) ,
(double)(-voxels[x+1][y+1][z+1]+voxels[x+1][y+1][z])
},
{
x,y+1,z+1,
(double)(-voxels[x+1][y+1][z+1]+voxels[x-1][y+1][z+1]) ,
(double)(-voxels[x][y+1][z+1]+voxels[x][y][z+1]) ,
(double)(-voxels[x][y+1][z+1]+voxels[x][y+1][z])
}
},
{
voxels[x][y][z],
voxels[x+1][y][z],
voxels[x+1][y][z+1],
voxels[x][y][z+1],
voxels[x][y+1][z],
voxels[x+1][y+1][z],
voxels[x+1][y+1][z+1],
voxels[x][y+1][z+1]
}
};
int c1,c2,c3;
for (int i=0; i<8; i++)
{
c1 = c.p[i].x;
c2 = c.p[i].y;
c3 = c.p[i].z;
if (Der[c3*N*N+c2*N+c1]>0) {sign = 1;break;}
}
if (sign == 1)
processCube(c, isovalue,&vertexList);
}
}
}
return vertexList;
}