-
Notifications
You must be signed in to change notification settings - Fork 2
/
Decimate.cpp
338 lines (274 loc) · 10.1 KB
/
Decimate.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <cassert>
#include "common.h"
#include GLUT_INCLUDE
#include "Decimate.h"
#include "Array3D.h"
static inline void drawVert(const Isosurface& surface, const Point3D& p1, const Point3D& p2, float isolevel)
{
float v1 = p1.value;
float v2 = p2.value;
float x, y, z;
if (v2 == v1) {
x = (p1.x + p2.x) / 2.0f;
y = (p1.y + p2.y) / 2.0f;
z = (p1.z + p2.z) / 2.0f;
} else {
/*
<----+-----+---+----->
v1 | v2
isolevel
<----+-----+---+----->
0 | 1
interp
*/
// interp == 0: vert should be at p1
// interp == 1: vert should be at p2
float interp = (isolevel - v1) / (v2 - v1);
float oneMinusInterp = 1 - interp;
x = p1.x * oneMinusInterp + p2.x * interp;
y = p1.y * oneMinusInterp + p2.y * interp;
z = p1.z * oneMinusInterp + p2.z * interp;
}
Vector3D normal = surface.gradientAt(x, y, z);
glNormal3f(normal.x, normal.y, normal.z);
glVertex3f(x, y, z);
}
static void drawTetrahedron(const Isosurface& surface, const Point3D p[4], float isolevel)
{
/*
Tetrahedron layout:
0
*
/|\
/ | \
3 *-----* 1
\ | /
\|/
*
2
*/
// Tricky little trick here: compute a single number that tells us which
// vertices are inside or outside. The bit for `2^i` will be 1 iff `p[i]`
// is inside the surface.
unsigned char index = 0;
for (int i = 0; i < 4; ++i) {
if (p[i].value < isolevel) {
index |= (1 << i);
}
}
// Case analysis: draw appropriate triangles based on which vertices are
// inside the shape.
switch (index) {
// we don't do anything if everyone is inside or outside
case 0x00:
case 0x0F:
break;
// only vert 0 is inside
case 0x01:
drawVert(surface, p[0], p[1], isolevel);
drawVert(surface, p[0], p[3], isolevel);
drawVert(surface, p[0], p[2], isolevel);
break;
// only vert 1 is inside
case 0x02:
drawVert(surface, p[1], p[0], isolevel);
drawVert(surface, p[1], p[2], isolevel);
drawVert(surface, p[1], p[3], isolevel);
break;
// only vert 2 is inside
case 0x04:
drawVert(surface, p[2], p[0], isolevel);
drawVert(surface, p[2], p[3], isolevel);
drawVert(surface, p[2], p[1], isolevel);
break;
// only vert 3 is inside
case 0x08:
drawVert(surface, p[3], p[1], isolevel);
drawVert(surface, p[3], p[2], isolevel);
drawVert(surface, p[3], p[0], isolevel);
break;
// verts 0, 1 are inside
case 0x03:
drawVert(surface, p[3], p[0], isolevel);
drawVert(surface, p[2], p[0], isolevel);
drawVert(surface, p[1], p[3], isolevel);
drawVert(surface, p[2], p[0], isolevel);
drawVert(surface, p[2], p[1], isolevel);
drawVert(surface, p[1], p[3], isolevel);
break;
// verts 0, 2 are inside
case 0x05:
drawVert(surface, p[3], p[0], isolevel);
drawVert(surface, p[1], p[2], isolevel);
drawVert(surface, p[1], p[0], isolevel);
drawVert(surface, p[1], p[2], isolevel);
drawVert(surface, p[3], p[0], isolevel);
drawVert(surface, p[2], p[3], isolevel);
break;
// verts 0, 3 are inside
case 0x09:
drawVert(surface, p[0], p[1], isolevel);
drawVert(surface, p[1], p[3], isolevel);
drawVert(surface, p[0], p[2], isolevel);
drawVert(surface, p[1], p[3], isolevel);
drawVert(surface, p[3], p[2], isolevel);
drawVert(surface, p[0], p[2], isolevel);
break;
// verts 1, 2 are inside
case 0x06:
drawVert(surface, p[0], p[1], isolevel);
drawVert(surface, p[0], p[2], isolevel);
drawVert(surface, p[1], p[3], isolevel);
drawVert(surface, p[1], p[3], isolevel);
drawVert(surface, p[0], p[2], isolevel);
drawVert(surface, p[3], p[2], isolevel);
break;
// verts 2, 3 are inside
case 0x0C:
drawVert(surface, p[1], p[3], isolevel);
drawVert(surface, p[2], p[0], isolevel);
drawVert(surface, p[3], p[0], isolevel);
drawVert(surface, p[2], p[0], isolevel);
drawVert(surface, p[1], p[3], isolevel);
drawVert(surface, p[2], p[1], isolevel);
break;
// verts 1, 3 are inside
case 0x0A:
drawVert(surface, p[3], p[0], isolevel);
drawVert(surface, p[1], p[0], isolevel);
drawVert(surface, p[1], p[2], isolevel);
drawVert(surface, p[1], p[2], isolevel);
drawVert(surface, p[2], p[3], isolevel);
drawVert(surface, p[3], p[0], isolevel);
break;
// verts 0, 1, 2 are inside
case 0x07:
drawVert(surface, p[3], p[0], isolevel);
drawVert(surface, p[3], p[2], isolevel);
drawVert(surface, p[3], p[1], isolevel);
break;
// verts 0, 1, 3 are inside
case 0x0B:
drawVert(surface, p[2], p[1], isolevel);
drawVert(surface, p[2], p[3], isolevel);
drawVert(surface, p[2], p[0], isolevel);
break;
// verts 0, 2, 3 are inside
case 0x0D:
drawVert(surface, p[1], p[0], isolevel);
drawVert(surface, p[1], p[3], isolevel);
drawVert(surface, p[1], p[2], isolevel);
break;
// verts 1, 2, 3 are inside
case 0x0E:
drawVert(surface, p[0], p[1], isolevel);
drawVert(surface, p[0], p[2], isolevel);
drawVert(surface, p[0], p[3], isolevel);
break;
// what is this I don't even
default:
assert(false);
}
}
void decimate(const Isosurface& surface,
float xMin, float xMax,
float yMin, float yMax,
float zMin, float zMax,
float isolevel,
std::size_t resolution) // resolution indicates # of cubes
{
std::size_t pointRes = resolution + 1; // indicates the # of points per side
float xrange = xMax - xMin;
float yrange = yMax - yMin;
float zrange = zMax - zMin;
// CPU/memory tradeoff ahead: to avoid recomputation, this implementation
// fills a giant 3D array with the suface values at each vertex of the
// grid, then queries that array in the triangle-drawing loop.
//
// If you know that your surface is very fast to query, then this is mostly
// a waste of memory.
//
// If you are willing to optimize further, you could avoid avoid
// recomputation with drastically less memory by walking through "slices"
// of the grid along, say, the x-coordinate:
//
// / +-----+
// x / +-----+|
// / +-----+||
// ||+
// ... |+
// +
//
// Only two slices at a time would be necessary.
Array3D<float> grid(pointRes, pointRes, pointRes);
for (std::size_t i = 0; i <= resolution; ++i) {
float x = (float)i/resolution * xrange + xMin;
for (std::size_t j = 0; j <= resolution; ++j) {
float y = (float)j/resolution * yrange + yMin;
for (std::size_t k = 0; k <= resolution; ++k) {
float z = (float)k/resolution * zrange + zMin;
float value = surface.valueAt(x, y, z);
grid(i, j, k) = value;
}
}
}
glBegin(GL_TRIANGLES);
for (std::size_t i = 0; i < resolution; ++i) {
float x1 = (float)i/resolution * xrange + xMin;
float x2 = (float)(i+1)/resolution * xrange + xMin;
for (std::size_t j = 0; j < resolution; ++j) {
float y1 = (float)j/resolution * yrange + yMin;
float y2 = (float)(j+1)/resolution * yrange + yMin;
for (std::size_t k = 0; k < resolution; ++k) {
float z1 = (float)k/resolution * zrange + zMin;
float z2 = (float)(k+1)/resolution * zrange + zMin;
/*
Coordinates:
z
|
|___ y
/
/
x
Cube layout:
4-------7
/| /|
/ | / |
5-------6 |
| 0----|--3
| / | /
|/ |/
1-------2
Tetrahedrons are:
0, 7, 3, 2
0, 7, 2, 6
0, 4, 6, 7
0, 6, 1, 2
0, 6, 1, 4
5, 6, 1, 4
*/
const Point3D v[8] = {
{x1, y1, z1, grid(i, j, k )},
{x2, y1, z1, grid(i + 1, j, k )},
{x2, y2, z1, grid(i + 1, j + 1, k )},
{x1, y2, z1, grid(i, j + 1, k )},
{x1, y1, z2, grid(i, j, k + 1)},
{x2, y1, z2, grid(i + 1, j, k + 1)},
{x2, y2, z2, grid(i + 1, j + 1, k + 1)},
{x1, y2, z2, grid(i, j + 1, k + 1)}
};
const Point3D tetrahedra[6][4] = {
{ v[0], v[7], v[3], v[2] },
{ v[0], v[7], v[2], v[6] },
{ v[0], v[4], v[7], v[6] },
{ v[0], v[1], v[6], v[2] },
{ v[0], v[4], v[6], v[1] },
{ v[5], v[1], v[6], v[4] }
};
for (int t = 0; t < 6; ++t)
drawTetrahedron(surface, tetrahedra[t], isolevel);
}
}
}
glEnd();
}