/** * SurfaceNets in JavaScript * * Written by Mikola Lysenko (C) 2012 * * MIT License * * Based on: S.F. Gibson, 'Constrained Elastic Surface Nets'. (1998) MERL Tech Report. * from https://github.com/mikolalysenko/isosurface/tree/master * */ let surfaceNet = ( dims, potential, bounds ) => { //Precompute edge table, like Paul Bourke does. // This saves a bit of time when computing the centroid of each boundary cell var cube_edges = new Int32Array(24) , edge_table = new Int32Array(256); (function() { //Initialize the cube_edges table // This is just the vertex number of each cube var k = 0; for(var i=0; i<8; ++i) { for(var j=1; j<=4; j<<=1) { var p = i^j; if(i <= p) { cube_edges[k++] = i; cube_edges[k++] = p; } } } //Initialize the intersection table. // This is a 2^(cube configuration) -> 2^(edge configuration) map // There is one entry for each possible cube configuration, and the output is a 12-bit vector enumerating all edges crossing the 0-level. for(var i=0; i<256; ++i) { var em = 0; for(var j=0; j<24; j+=2) { var a = !!(i & (1<> 1)) : 0; } edge_table[i] = em; } })(); //Internal buffer, this may get resized at run time var buffer = new Array(4096); (function() { for(var i=0; i buffer.length) { var ol = buffer.length; buffer.length = R[2] * 2; while(ol < buffer.length) { buffer[ol++] = 0; } } //March over the voxel grid for(x[2]=0; x[2] 1e-6) { t = g0 / t; } else { continue; } //Interpolate vertices and add up intersections (this can be done without multiplying) for(var j=0, k=1; j<3; ++j, k<<=1) { var a = e0 & k , b = e1 & k; if(a !== b) { v[j] += a ? 1.0 - t : t; } else { v[j] += a ? 1.0 : 0; } } } //Now we just average the edge intersections and add them to coordinate var s = 1.0 / e_count; for(var i=0; i<3; ++i) { v[i] = scale[i] * (x[i] + s * v[i]) + shift[i]; } //Add vertex to buffer, store pointer to vertex index in buffer buffer[m] = vertices.length; vertices.push(v); //Now we need to add faces together, to do this we just loop over 3 basis components for(var i=0; i<3; ++i) { //The first three entries of the edge_mask count the crossings along the edge if(!(edge_mask & (1<