2 metasurf - a library for implicit surface polygonization
3 Copyright (C) 2011-2015 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #if (defined(USE_MTETRA) && defined(USE_MCUBES)) || (!defined(USE_MTETRA) && !defined(USE_MCUBES))
27 #error "pick either USE_MTETRA or USE_MCUBES, not both..."
30 typedef float vec3[3];
37 msurf_eval_func_t eval;
38 msurf_vertex_func_t vertex;
39 msurf_normal_func_t normal;
49 static int msurf_init(struct metasurface *ms);
50 static void process_cell(struct metasurface *ms, vec3 pos, vec3 sz);
52 static void process_tetra(struct metasurface *ms, int *idx, vec3 *pos, float *val);
55 static void process_cube(struct metasurface *ms, vec3 *pos, float *val);
59 struct metasurface *msurf_create(void)
61 struct metasurface *ms;
63 if(!(ms = malloc(sizeof *ms))) {
66 if(msurf_init(ms) == -1) {
72 void msurf_free(struct metasurface *ms)
77 static int msurf_init(struct metasurface *ms)
84 ms->min[0] = ms->min[1] = ms->min[2] = -1.0;
85 ms->max[0] = ms->max[1] = ms->max[2] = 1.0;
86 ms->res[0] = ms->res[1] = ms->res[2] = 40;
89 ms->dx = ms->dy = ms->dz = 0.001;
95 void msurf_set_user_data(struct metasurface *ms, void *udata)
100 void *msurf_get_user_data(struct metasurface *ms)
105 void msurf_set_inside(struct metasurface *ms, int inside)
117 fprintf(stderr, "msurf_inside expects MSURF_GREATER or MSURF_LESS\n");
121 int msurf_get_inside(struct metasurface *ms)
123 return ms->flip ? MSURF_LESS : MSURF_GREATER;
126 void msurf_eval_func(struct metasurface *ms, msurf_eval_func_t func)
131 void msurf_vertex_func(struct metasurface *ms, msurf_vertex_func_t func)
136 void msurf_normal_func(struct metasurface *ms, msurf_normal_func_t func)
141 void msurf_set_bounds(struct metasurface *ms, float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
151 void msurf_get_bounds(struct metasurface *ms, float *xmin, float *ymin, float *zmin, float *xmax, float *ymax, float *zmax)
161 void msurf_set_resolution(struct metasurface *ms, int xres, int yres, int zres)
168 void msurf_get_resolution(struct metasurface *ms, int *xres, int *yres, int *zres)
175 void msurf_set_threshold(struct metasurface *ms, float thres)
180 float msurf_get_threshold(struct metasurface *ms)
186 int msurf_polygonize(struct metasurface *ms)
191 if(!ms->eval || !ms->vertex) {
192 fprintf(stderr, "you need to set eval and vertex callbacks before calling msurf_polygonize\n");
197 delta[i] = (ms->max[i] - ms->min[i]) / (float)ms->res[i];
201 for(i=0; i<ms->res[0] - 1; i++) {
203 for(j=0; j<ms->res[1] - 1; j++) {
206 for(k=0; k<ms->res[2] - 1; k++) {
208 process_cell(ms, pos, delta);
220 static void process_cell(struct metasurface *ms, vec3 pos, vec3 sz)
227 static int tetra[][4] = {
237 static const float offs[][3] = {
249 p[i][0] = pos[0] + sz[0] * offs[i][2];
250 p[i][1] = pos[1] + sz[1] * offs[i][1];
251 p[i][2] = pos[2] + sz[2] * offs[i][0];
253 val[i] = ms->eval(ms, p[i][0], p[i][1], p[i][2]);
258 process_tetra(ms, tetra[i], p, val);
262 process_cube(ms, p, val);
267 /* ---- marching cubes implementation ---- */
270 static unsigned int mc_bitcode(float *val, float thres);
272 static void process_cube(struct metasurface *ms, vec3 *pos, float *val)
274 static const int pidx[12][2] = {
275 {0, 1}, {1, 2}, {2, 3}, {3, 0}, {4, 5}, {5, 6},
276 {6, 7}, {7, 4}, {0, 4}, {1, 5}, {2, 6}, {3, 7}
280 unsigned int code = mc_bitcode(val, ms->thres);
286 if(mc_edge_table[code] == 0) {
290 for(i=0; i<12; i++) {
291 if(mc_edge_table[code] & (1 << i)) {
295 float t = (ms->thres - val[p0]) / (val[p1] - val[p0]);
296 vert[i][0] = pos[p0][0] + (pos[p1][0] - pos[p0][0]) * t;
297 vert[i][1] = pos[p0][1] + (pos[p1][1] - pos[p0][1]) * t;
298 vert[i][2] = pos[p0][2] + (pos[p1][2] - pos[p0][2]) * t;
302 for(i=0; mc_tri_table[code][i] != -1; i+=3) {
304 float *v = vert[mc_tri_table[code][i + j]];
307 float dfdx, dfdy, dfdz;
308 dfdx = ms->eval(ms, v[0] - ms->dx, v[1], v[2]) - ms->eval(ms, v[0] + ms->dx, v[1], v[2]);
309 dfdy = ms->eval(ms, v[0], v[1] - ms->dy, v[2]) - ms->eval(ms, v[0], v[1] + ms->dy, v[2]);
310 dfdz = ms->eval(ms, v[0], v[1], v[2] - ms->dz) - ms->eval(ms, v[0], v[1], v[2] + ms->dz);
317 ms->normal(ms, dfdx, dfdy, dfdz);
320 /* TODO multithreadied polygon emmit */
321 ms->vertex(ms, v[0], v[1], v[2]);
326 static unsigned int mc_bitcode(float *val, float thres)
328 unsigned int i, res = 0;
337 #endif /* USE_MCUBES */
340 /* ---- marching tetrahedra implementation (incomplete) ---- */
343 static unsigned int mt_bitcode(float v0, float v1, float v2, float v3, float thres);
344 static void emmit(struct metasurface *ms, float v0, float v1, vec3 p0, vec3 p1, int rev)
347 #define REVBIT(x) ((x) & 8)
348 #define INV(x) (~(x) & 0xf)
349 #define EDGE(a, b) emmit(ms, val[idx[a]], val[idx[b]], pos[idx[a]], pos[idx[b]], REVBIT(code))
350 static void process_tetra(struct metasurface *ms, int *idx, vec3 *pos, float *val)
352 unsigned int code = mt_bitcode(val[idx[0]], val[idx[1]], val[idx[2]], val[idx[3]], ms->thres);
417 break; /* cases 0 and 15 */
421 #define BIT(i) ((v##i > thres) ? (1 << i) : 0)
422 static unsigned int mt_bitcode(float v0, float v1, float v2, float v3, float thres)
424 return BIT(0) | BIT(1) | BIT(2) | BIT(3);
427 static void emmit(struct metasurface *ms, float v0, float v1, vec3 p0, vec3 p1, int rev)
430 float t = (ms->thres - v0) / (v1 - v0);
434 p[i] = p0[i] + (p1[i] - p0[i]) * t;
436 ms->vertex(ms, p[0], p[1], p[2]);
438 /*for(i=0; i<3; i++) {
439 ms->vbuf[ms->nverts][i] = p0[i] + (p1[i] - p0[i]) * t;
442 if(++ms->nverts >= 3) {
446 int idx = rev ? (2 - i) : i;
447 ms->vertex(ms, ms->vbuf[idx][0], ms->vbuf[idx][1], ms->vbuf[idx][2]);
451 #endif /* USE_MTETRA */