[
  {
    "path": ".gitignore",
    "content": "lib-cov\n*.seed\n*.log\n*.csv\n*.dat\n*.out\n*.pid\n*.gz\n\npids\nlogs\nresults\n\nnpm-debug.log\n"
  },
  {
    "path": "LICENSE",
    "content": "\nThe MIT License (MIT)\n\nCopyright (c) 2013 Mikola Lysenko\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "isosurface\n==========\n\nIsosurface polygonizer algorithms in JavaScript.  For more details, see the following blog posts:\n\n* [What is a solid?](http://0fps.wordpress.com/2012/08/29/what-is-a-solid/)\n* Smooth voxel terrain [Part 1](http://0fps.wordpress.com/2012/07/10/smooth-voxel-terrain-part-1/) [Part 2](http://0fps.wordpress.com/2012/07/12/smooth-voxel-terrain-part-2/)\n\n[Or try out a live demo](http://mikolalysenko.github.com/Isosurface/)\n\n# Example\n\n```javascript\nvar isosurface = require(\"isosurface\")\n\nvar mesh = isosurface.surfaceNets([64,64,64], function(x,y,z) {\n  return x*x + y*y + z*z - 100\n}, [[-11,-11,-11], [11,11,11]])\n\nconsole.log(mesh)\n```\n\n\n# Install\n\n```\nnpm install isosurface\n```\n    \n# API\n\n```javascript\nvar isosurface = require(\"isosurface\")\n```\n\n#### `isosurface.surfaceNets(dims, potential[, bounds])`\nExtracts an isosurface from `potential` using surface nets with resolution given by `dims`.\n\nParams:\n* `dims`: A 3D vector of integers representing the resolution of the isosurface\n* `potential(x,y,z)`: A scalar valued potential function taking 3 coordinates as arguments returning a scalar.\n* `bounds`: A pair of 3D vectors `[lo, hi]` giving bounds on the potential to sample.  If not specified, default is `[[0,0,0], dims]`.\n\nReturns: A mesh object with the following members:\n* `positions`: The coordinates of the vertices of the mesh\n* `cells`: The faces of the mesh.\n\n#### `isosurface..marchingCubes(dims, potential[, bounds])`\n\nSame as above, except uses marching cubes instead of surface nets to extract the isosurface.\n\n#### `isosurface.marchingTetrahedra(dims, potential[, bounds])`\n\nSame as above, except uses marching tetrahedra instead of surface nets to extract the isosurface.\n\n\n# Credits\n(c) 2012-2014 Mikola Lysenko.  MIT License"
  },
  {
    "path": "index.js",
    "content": "exports.surfaceNets         = require(\"./lib/surfacenets.js\").surfaceNets;\nexports.marchingCubes       = require(\"./lib/marchingcubes.js\").marchingCubes;\nexports.marchingTetrahedra  = require(\"./lib/marchingtetrahedra.js\").marchingTetrahedra;\n"
  },
  {
    "path": "lib/marchingcubes.js",
    "content": "/**\n * Javascript Marching Cubes\n *\n * Based on Paul Bourke's classic implementation:\n *    http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/\n *\n * JS port by Mikola Lysenko\n */\n\nvar edgeTable= new Uint32Array([\n      0x0  , 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c,\n      0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00,\n      0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c,\n      0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90,\n      0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c,\n      0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30,\n      0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac,\n      0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0,\n      0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c,\n      0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60,\n      0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc,\n      0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0,\n      0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c,\n      0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950,\n      0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc ,\n      0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0,\n      0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc,\n      0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0,\n      0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c,\n      0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650,\n      0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc,\n      0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0,\n      0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c,\n      0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460,\n      0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac,\n      0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0,\n      0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c,\n      0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230,\n      0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c,\n      0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190,\n      0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c,\n      0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0   ])\n  , triTable = [\n      [],\n      [0, 8, 3],\n      [0, 1, 9],\n      [1, 8, 3, 9, 8, 1],\n      [1, 2, 10],\n      [0, 8, 3, 1, 2, 10],\n      [9, 2, 10, 0, 2, 9],\n      [2, 8, 3, 2, 10, 8, 10, 9, 8],\n      [3, 11, 2],\n      [0, 11, 2, 8, 11, 0],\n      [1, 9, 0, 2, 3, 11],\n      [1, 11, 2, 1, 9, 11, 9, 8, 11],\n      [3, 10, 1, 11, 10, 3],\n      [0, 10, 1, 0, 8, 10, 8, 11, 10],\n      [3, 9, 0, 3, 11, 9, 11, 10, 9],\n      [9, 8, 10, 10, 8, 11],\n      [4, 7, 8],\n      [4, 3, 0, 7, 3, 4],\n      [0, 1, 9, 8, 4, 7],\n      [4, 1, 9, 4, 7, 1, 7, 3, 1],\n      [1, 2, 10, 8, 4, 7],\n      [3, 4, 7, 3, 0, 4, 1, 2, 10],\n      [9, 2, 10, 9, 0, 2, 8, 4, 7],\n      [2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4],\n      [8, 4, 7, 3, 11, 2],\n      [11, 4, 7, 11, 2, 4, 2, 0, 4],\n      [9, 0, 1, 8, 4, 7, 2, 3, 11],\n      [4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1],\n      [3, 10, 1, 3, 11, 10, 7, 8, 4],\n      [1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4],\n      [4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3],\n      [4, 7, 11, 4, 11, 9, 9, 11, 10],\n      [9, 5, 4],\n      [9, 5, 4, 0, 8, 3],\n      [0, 5, 4, 1, 5, 0],\n      [8, 5, 4, 8, 3, 5, 3, 1, 5],\n      [1, 2, 10, 9, 5, 4],\n      [3, 0, 8, 1, 2, 10, 4, 9, 5],\n      [5, 2, 10, 5, 4, 2, 4, 0, 2],\n      [2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8],\n      [9, 5, 4, 2, 3, 11],\n      [0, 11, 2, 0, 8, 11, 4, 9, 5],\n      [0, 5, 4, 0, 1, 5, 2, 3, 11],\n      [2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5],\n      [10, 3, 11, 10, 1, 3, 9, 5, 4],\n      [4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10],\n      [5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3],\n      [5, 4, 8, 5, 8, 10, 10, 8, 11],\n      [9, 7, 8, 5, 7, 9],\n      [9, 3, 0, 9, 5, 3, 5, 7, 3],\n      [0, 7, 8, 0, 1, 7, 1, 5, 7],\n      [1, 5, 3, 3, 5, 7],\n      [9, 7, 8, 9, 5, 7, 10, 1, 2],\n      [10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3],\n      [8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2],\n      [2, 10, 5, 2, 5, 3, 3, 5, 7],\n      [7, 9, 5, 7, 8, 9, 3, 11, 2],\n      [9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11],\n      [2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7],\n      [11, 2, 1, 11, 1, 7, 7, 1, 5],\n      [9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11],\n      [5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0],\n      [11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0],\n      [11, 10, 5, 7, 11, 5],\n      [10, 6, 5],\n      [0, 8, 3, 5, 10, 6],\n      [9, 0, 1, 5, 10, 6],\n      [1, 8, 3, 1, 9, 8, 5, 10, 6],\n      [1, 6, 5, 2, 6, 1],\n      [1, 6, 5, 1, 2, 6, 3, 0, 8],\n      [9, 6, 5, 9, 0, 6, 0, 2, 6],\n      [5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8],\n      [2, 3, 11, 10, 6, 5],\n      [11, 0, 8, 11, 2, 0, 10, 6, 5],\n      [0, 1, 9, 2, 3, 11, 5, 10, 6],\n      [5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11],\n      [6, 3, 11, 6, 5, 3, 5, 1, 3],\n      [0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6],\n      [3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9],\n      [6, 5, 9, 6, 9, 11, 11, 9, 8],\n      [5, 10, 6, 4, 7, 8],\n      [4, 3, 0, 4, 7, 3, 6, 5, 10],\n      [1, 9, 0, 5, 10, 6, 8, 4, 7],\n      [10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4],\n      [6, 1, 2, 6, 5, 1, 4, 7, 8],\n      [1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7],\n      [8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6],\n      [7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9],\n      [3, 11, 2, 7, 8, 4, 10, 6, 5],\n      [5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11],\n      [0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6],\n      [9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6],\n      [8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6],\n      [5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11],\n      [0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7],\n      [6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9],\n      [10, 4, 9, 6, 4, 10],\n      [4, 10, 6, 4, 9, 10, 0, 8, 3],\n      [10, 0, 1, 10, 6, 0, 6, 4, 0],\n      [8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10],\n      [1, 4, 9, 1, 2, 4, 2, 6, 4],\n      [3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4],\n      [0, 2, 4, 4, 2, 6],\n      [8, 3, 2, 8, 2, 4, 4, 2, 6],\n      [10, 4, 9, 10, 6, 4, 11, 2, 3],\n      [0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6],\n      [3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10],\n      [6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1],\n      [9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3],\n      [8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1],\n      [3, 11, 6, 3, 6, 0, 0, 6, 4],\n      [6, 4, 8, 11, 6, 8],\n      [7, 10, 6, 7, 8, 10, 8, 9, 10],\n      [0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10],\n      [10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0],\n      [10, 6, 7, 10, 7, 1, 1, 7, 3],\n      [1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7],\n      [2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9],\n      [7, 8, 0, 7, 0, 6, 6, 0, 2],\n      [7, 3, 2, 6, 7, 2],\n      [2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7],\n      [2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7],\n      [1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11],\n      [11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1],\n      [8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6],\n      [0, 9, 1, 11, 6, 7],\n      [7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0],\n      [7, 11, 6],\n      [7, 6, 11],\n      [3, 0, 8, 11, 7, 6],\n      [0, 1, 9, 11, 7, 6],\n      [8, 1, 9, 8, 3, 1, 11, 7, 6],\n      [10, 1, 2, 6, 11, 7],\n      [1, 2, 10, 3, 0, 8, 6, 11, 7],\n      [2, 9, 0, 2, 10, 9, 6, 11, 7],\n      [6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8],\n      [7, 2, 3, 6, 2, 7],\n      [7, 0, 8, 7, 6, 0, 6, 2, 0],\n      [2, 7, 6, 2, 3, 7, 0, 1, 9],\n      [1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6],\n      [10, 7, 6, 10, 1, 7, 1, 3, 7],\n      [10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8],\n      [0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7],\n      [7, 6, 10, 7, 10, 8, 8, 10, 9],\n      [6, 8, 4, 11, 8, 6],\n      [3, 6, 11, 3, 0, 6, 0, 4, 6],\n      [8, 6, 11, 8, 4, 6, 9, 0, 1],\n      [9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6],\n      [6, 8, 4, 6, 11, 8, 2, 10, 1],\n      [1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6],\n      [4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9],\n      [10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3],\n      [8, 2, 3, 8, 4, 2, 4, 6, 2],\n      [0, 4, 2, 4, 6, 2],\n      [1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8],\n      [1, 9, 4, 1, 4, 2, 2, 4, 6],\n      [8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1],\n      [10, 1, 0, 10, 0, 6, 6, 0, 4],\n      [4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3],\n      [10, 9, 4, 6, 10, 4],\n      [4, 9, 5, 7, 6, 11],\n      [0, 8, 3, 4, 9, 5, 11, 7, 6],\n      [5, 0, 1, 5, 4, 0, 7, 6, 11],\n      [11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5],\n      [9, 5, 4, 10, 1, 2, 7, 6, 11],\n      [6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5],\n      [7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2],\n      [3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6],\n      [7, 2, 3, 7, 6, 2, 5, 4, 9],\n      [9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7],\n      [3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0],\n      [6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8],\n      [9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7],\n      [1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4],\n      [4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10],\n      [7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10],\n      [6, 9, 5, 6, 11, 9, 11, 8, 9],\n      [3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5],\n      [0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11],\n      [6, 11, 3, 6, 3, 5, 5, 3, 1],\n      [1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6],\n      [0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10],\n      [11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5],\n      [6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3],\n      [5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2],\n      [9, 5, 6, 9, 6, 0, 0, 6, 2],\n      [1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8],\n      [1, 5, 6, 2, 1, 6],\n      [1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6],\n      [10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0],\n      [0, 3, 8, 5, 6, 10],\n      [10, 5, 6],\n      [11, 5, 10, 7, 5, 11],\n      [11, 5, 10, 11, 7, 5, 8, 3, 0],\n      [5, 11, 7, 5, 10, 11, 1, 9, 0],\n      [10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1],\n      [11, 1, 2, 11, 7, 1, 7, 5, 1],\n      [0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11],\n      [9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7],\n      [7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2],\n      [2, 5, 10, 2, 3, 5, 3, 7, 5],\n      [8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5],\n      [9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2],\n      [9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2],\n      [1, 3, 5, 3, 7, 5],\n      [0, 8, 7, 0, 7, 1, 1, 7, 5],\n      [9, 0, 3, 9, 3, 5, 5, 3, 7],\n      [9, 8, 7, 5, 9, 7],\n      [5, 8, 4, 5, 10, 8, 10, 11, 8],\n      [5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0],\n      [0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5],\n      [10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4],\n      [2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8],\n      [0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11],\n      [0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5],\n      [9, 4, 5, 2, 11, 3],\n      [2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4],\n      [5, 10, 2, 5, 2, 4, 4, 2, 0],\n      [3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9],\n      [5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2],\n      [8, 4, 5, 8, 5, 3, 3, 5, 1],\n      [0, 4, 5, 1, 0, 5],\n      [8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5],\n      [9, 4, 5],\n      [4, 11, 7, 4, 9, 11, 9, 10, 11],\n      [0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11],\n      [1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11],\n      [3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4],\n      [4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2],\n      [9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3],\n      [11, 7, 4, 11, 4, 2, 2, 4, 0],\n      [11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4],\n      [2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9],\n      [9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7],\n      [3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10],\n      [1, 10, 2, 8, 7, 4],\n      [4, 9, 1, 4, 1, 7, 7, 1, 3],\n      [4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1],\n      [4, 0, 3, 7, 4, 3],\n      [4, 8, 7],\n      [9, 10, 8, 10, 11, 8],\n      [3, 0, 9, 3, 9, 11, 11, 9, 10],\n      [0, 1, 10, 0, 10, 8, 8, 10, 11],\n      [3, 1, 10, 11, 3, 10],\n      [1, 2, 11, 1, 11, 9, 9, 11, 8],\n      [3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9],\n      [0, 2, 11, 8, 0, 11],\n      [3, 2, 11],\n      [2, 3, 8, 2, 8, 10, 10, 8, 9],\n      [9, 10, 2, 0, 9, 2],\n      [2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8],\n      [1, 10, 2],\n      [1, 3, 8, 9, 1, 8],\n      [0, 9, 1],\n      [0, 3, 8],\n      []]\n  , cubeVerts = [\n     [0,0,0]\n    ,[1,0,0]\n    ,[1,1,0]\n    ,[0,1,0]\n    ,[0,0,1]\n    ,[1,0,1]\n    ,[1,1,1]\n    ,[0,1,1]]\n  , edgeIndex = [ [0,1],[1,2],[2,3],[3,0],[4,5],[5,6],[6,7],[7,4],[0,4],[1,5],[2,6],[3,7] ];\n\n\n\nexports.marchingCubes = function(dims, potential, bounds) {\n  if(!bounds) {\n    bounds = [[0,0,0], dims];\n  }\n  var scale     = [0,0,0];\n  var shift     = [0,0,0];\n  for(var i=0; i<3; ++i) {\n    scale[i] = (bounds[1][i] - bounds[0][i]) / dims[i];\n    shift[i] = bounds[0][i];\n  }\n\n  var vertices = []\n    , faces = []\n    , n = 0\n    , grid = new Array(8)\n    , edges = new Array(12)\n    , x = [0,0,0];\n  //March over the volume\n  for(x[2]=0; x[2]<dims[2]-1; ++x[2], n+=dims[0])\n  for(x[1]=0; x[1]<dims[1]-1; ++x[1], ++n)\n  for(x[0]=0; x[0]<dims[0]-1; ++x[0], ++n) {\n    //For each cell, compute cube mask\n    var cube_index = 0;\n    for(var i=0; i<8; ++i) {\n      var v = cubeVerts[i]\n        , s = potential(\n          scale[0]*(x[0]+v[0])+shift[0],\n          scale[1]*(x[1]+v[1])+shift[1],\n          scale[2]*(x[2]+v[2])+shift[2]);\n      grid[i] = s;\n      cube_index |= (s > 0) ? 1 << i : 0;\n    }\n    //Compute vertices\n    var edge_mask = edgeTable[cube_index];\n    if(edge_mask === 0) {\n      continue;\n    }\n    for(var i=0; i<12; ++i) {\n      if((edge_mask & (1<<i)) === 0) {\n        continue;\n      }\n      edges[i] = vertices.length;\n      var nv = [0,0,0]\n        , e = edgeIndex[i]\n        , p0 = cubeVerts[e[0]]\n        , p1 = cubeVerts[e[1]]\n        , a = grid[e[0]]\n        , b = grid[e[1]]\n        , d = a - b\n        , t = 0;\n      if(Math.abs(d) > 1e-6) {\n        t = a / d;\n      }\n      for(var j=0; j<3; ++j) {\n        nv[j] = scale[j] * ((x[j] + p0[j]) + t * (p1[j] - p0[j])) + shift[j];\n      }\n      vertices.push(nv);\n    }\n    //Add faces\n    var f = triTable[cube_index];\n    for(var i=0; i<f.length; i += 3) {\n      faces.push([edges[f[i]], edges[f[i+1]], edges[f[i+2]]]);\n    }\n  }\n  return { positions: vertices, cells: faces };\n};\n\n"
  },
  {
    "path": "lib/marchingtetrahedra.js",
    "content": "/**\n * Marching Tetrahedra in Javascript\n *\n * Based on Paul Bourke's implementation\n *  http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/\n *\n * (Several bug fixes were made to deal with oriented faces)\n *\n * Javascript port by Mikola Lysenko\n */\nvar cube_vertices = [\n        [0,0,0]\n      , [1,0,0]\n      , [1,1,0]\n      , [0,1,0]\n      , [0,0,1]\n      , [1,0,1]\n      , [1,1,1]\n      , [0,1,1] ]\n  , tetra_list = [\n        [0,2,3,7]\n      , [0,6,2,7]\n      , [0,4,6,7]\n      , [0,6,1,2]\n      , [0,1,6,4]\n      , [5,6,1,4] ];\n\nexports.marchingTetrahedra = function(dims, potential, bounds) {\n\n  if(!bounds) {\n    bounds = [[0,0,0], dims];\n  }\n  \n  var scale     = [0,0,0];\n  var shift     = [0,0,0];\n  for(var i=0; i<3; ++i) {\n    scale[i] = (bounds[1][i] - bounds[0][i]) / dims[i];\n    shift[i] = bounds[0][i];\n  }\n   \n   var vertices = []\n    , faces = []\n    , n = 0\n    , grid = new Float32Array(8)\n    , edges = new Int32Array(12)\n    , x = [0,0,0];\n    \n  function interp(i0, i1) {\n    var g0 = grid[i0]\n      , g1 = grid[i1]\n      , p0 = cube_vertices[i0]\n      , p1 = cube_vertices[i1]\n      , v  = [x[0], x[1], x[2]]\n      , t = g0 - g1;\n    if(Math.abs(t) > 1e-6) {\n      t = g0 / t;\n    }\n    for(var i=0; i<3; ++i) {\n      v[i] = scale[i] * (v[i] + p0[i] + t * (p1[i] - p0[i])) + shift[i];\n    }\n    vertices.push(v);\n    return vertices.length - 1;\n  }\n  \n  //March over the volume\n  for(x[2]=0; x[2]<dims[2]-1; ++x[2], n+=dims[0])\n  for(x[1]=0; x[1]<dims[1]-1; ++x[1], ++n)\n  for(x[0]=0; x[0]<dims[0]-1; ++x[0], ++n) {\n    //Read in cube  \n    for(var i=0; i<8; ++i) {\n      var cube_vert = cube_vertices[i];\n      grid[i] = potential(\n        scale[0]*(x[0]+cube_vert[0])+shift[0],\n        scale[1]*(x[1]+cube_vert[1])+shift[1],\n        scale[2]*(x[2]+cube_vert[2])+shift[2]);\n    }\n    for(var i=0; i<tetra_list.length; ++i) {\n      var T = tetra_list[i]\n        , triindex = 0;\n      if (grid[T[0]] < 0) triindex |= 1;\n      if (grid[T[1]] < 0) triindex |= 2;\n      if (grid[T[2]] < 0) triindex |= 4;\n      if (grid[T[3]] < 0) triindex |= 8;\n      \n      //Handle each case\n      switch (triindex) {\n        case 0x00:\n        case 0x0F:\n        break;\n        case 0x0E:\n          faces.push([ \n              interp(T[0], T[1])\n            , interp(T[0], T[3]) \n            , interp(T[0], T[2]) ]);\n        break;\n        case 0x01:\n          faces.push([ \n              interp(T[0], T[1])\n            , interp(T[0], T[2])\n            , interp(T[0], T[3])  ]);\n        break;\n        case 0x0D:\n          faces.push([ \n              interp(T[1], T[0])\n            , interp(T[1], T[2]) \n            , interp(T[1], T[3]) ]);\n        break;\n        case 0x02:\n          faces.push([ \n              interp(T[1], T[0])\n            , interp(T[1], T[3])\n            , interp(T[1], T[2]) ]);\n        break;\n        case 0x0C:\n          faces.push([ \n                interp(T[1], T[2])\n              , interp(T[1], T[3])\n              , interp(T[0], T[3])\n              , interp(T[0], T[2]) ]);\n        break;\n        case 0x03:\n          faces.push([ \n                interp(T[1], T[2])\n              , interp(T[0], T[2])\n              , interp(T[0], T[3])\n              , interp(T[1], T[3]) ]);\n        break;\n        case 0x04:\n          faces.push([ \n                interp(T[2], T[0])\n              , interp(T[2], T[1])\n              , interp(T[2], T[3]) ]);\n        break;\n        case 0x0B:\n          faces.push([ \n                interp(T[2], T[0])\n              , interp(T[2], T[3]) \n              , interp(T[2], T[1]) ]);\n        break;\n        case 0x05:\n          faces.push([ \n                interp(T[0], T[1])\n              , interp(T[1], T[2])\n              , interp(T[2], T[3])\n              , interp(T[0], T[3]) ]);\n        break;\n        case 0x0A:\n          faces.push([ \n                interp(T[0], T[1])\n              , interp(T[0], T[3])\n              , interp(T[2], T[3])\n              , interp(T[1], T[2]) ]);\n        break;\n        case 0x06:\n          faces.push([ \n                interp(T[2], T[3])\n              , interp(T[0], T[2])\n              , interp(T[0], T[1])\n              , interp(T[1], T[3]) ]);\n        break;\n        case 0x09:\n          faces.push([ \n                interp(T[2], T[3])\n              , interp(T[1], T[3])\n              , interp(T[0], T[1])\n              , interp(T[0], T[2]) ]);\n        break;\n        case 0x07:\n          faces.push([ \n                interp(T[3], T[0])\n              , interp(T[3], T[1])\n              , interp(T[3], T[2]) ]);\n        break;\n        case 0x08:\n          faces.push([ \n                interp(T[3], T[0])\n              , interp(T[3], T[2])\n              , interp(T[3], T[1]) ]);\n        break;\n      }\n    }\n  }\n  \n  return { positions: vertices, cells: faces };\n}\n\n"
  },
  {
    "path": "lib/surfacenets.js",
    "content": "/**\n * SurfaceNets in JavaScript\n *\n * Written by Mikola Lysenko (C) 2012\n *\n * MIT License\n *\n * Based on: S.F. Gibson, \"Constrained Elastic Surface Nets\". (1998) MERL Tech Report.\n */\n\"use strict\";\n\n//Precompute edge table, like Paul Bourke does.\n// This saves a bit of time when computing the centroid of each boundary cell\nvar cube_edges = new Int32Array(24)\n  , edge_table = new Int32Array(256);\n(function() {\n\n  //Initialize the cube_edges table\n  // This is just the vertex number of each cube\n  var k = 0;\n  for(var i=0; i<8; ++i) {\n    for(var j=1; j<=4; j<<=1) {\n      var p = i^j;\n      if(i <= p) {\n        cube_edges[k++] = i;\n        cube_edges[k++] = p;\n      }\n    }\n  }\n\n  //Initialize the intersection table.\n  //  This is a 2^(cube configuration) ->  2^(edge configuration) map\n  //  There is one entry for each possible cube configuration, and the output is a 12-bit vector enumerating all edges crossing the 0-level.\n  for(var i=0; i<256; ++i) {\n    var em = 0;\n    for(var j=0; j<24; j+=2) {\n      var a = !!(i & (1<<cube_edges[j]))\n        , b = !!(i & (1<<cube_edges[j+1]));\n      em |= a !== b ? (1 << (j >> 1)) : 0;\n    }\n    edge_table[i] = em;\n  }\n})();\n\n//Internal buffer, this may get resized at run time\nvar buffer = new Array(4096);\n(function() {\n  for(var i=0; i<buffer.length; ++i) {\n    buffer[i] = 0;\n  }\n})();\n\nexports.surfaceNets = function(dims, potential, bounds) {\n  if(!bounds) {\n    bounds = [[0,0,0],dims];\n  }\n  \n  var scale     = [0,0,0];\n  var shift     = [0,0,0];\n  for(var i=0; i<3; ++i) {\n    scale[i] = (bounds[1][i] - bounds[0][i]) / dims[i];\n    shift[i] = bounds[0][i];\n  }\n  \n  var vertices = []\n    , faces = []\n    , n = 0\n    , x = [0, 0, 0]\n    , R = [1, (dims[0]+1), (dims[0]+1)*(dims[1]+1)]\n    , grid = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]\n    , buf_no = 1;\n  \n   \n  //Resize buffer if necessary \n  if(R[2] * 2 > buffer.length) {\n    var ol = buffer.length;\n    buffer.length = R[2] * 2;\n    while(ol < buffer.length) {\n      buffer[ol++] = 0;\n    }\n  }\n  \n  //March over the voxel grid\n  for(x[2]=0; x[2]<dims[2]-1; ++x[2], n+=dims[0], buf_no ^= 1, R[2]=-R[2]) {\n  \n    //m is the pointer into the buffer we are going to use.  \n    //This is slightly obtuse because javascript does not have good support for packed data structures, so we must use typed arrays :(\n    //The contents of the buffer will be the indices of the vertices on the previous x/y slice of the volume\n    var m = 1 + (dims[0]+1) * (1 + buf_no * (dims[1]+1));\n    \n    for(x[1]=0; x[1]<dims[1]-1; ++x[1], ++n, m+=2)\n    for(x[0]=0; x[0]<dims[0]-1; ++x[0], ++n, ++m) {\n    \n      //Read in 8 field values around this vertex and store them in an array\n      //Also calculate 8-bit mask, like in marching cubes, so we can speed up sign checks later\n      var mask = 0, g = 0;\n      for(var k=0; k<2; ++k)\n      for(var j=0; j<2; ++j)      \n      for(var i=0; i<2; ++i, ++g) {\n        var p = potential(\n          scale[0]*(x[0]+i)+shift[0],\n          scale[1]*(x[1]+j)+shift[1],\n          scale[2]*(x[2]+k)+shift[2]);\n        grid[g] = p;\n        mask |= (p < 0) ? (1<<g) : 0;\n      }\n      \n      //Check for early termination if cell does not intersect boundary\n      if(mask === 0 || mask === 0xff) {\n        continue;\n      }\n      \n      //Sum up edge intersections\n      var edge_mask = edge_table[mask]\n        , v = [0.0,0.0,0.0]\n        , e_count = 0;\n        \n      //For every edge of the cube...\n      for(var i=0; i<12; ++i) {\n      \n        //Use edge mask to check if it is crossed\n        if(!(edge_mask & (1<<i))) {\n          continue;\n        }\n        \n        //If it did, increment number of edge crossings\n        ++e_count;\n        \n        //Now find the point of intersection\n        var e0 = cube_edges[ i<<1 ]       //Unpack vertices\n          , e1 = cube_edges[(i<<1)+1]\n          , g0 = grid[e0]                 //Unpack grid values\n          , g1 = grid[e1]\n          , t  = g0 - g1;                 //Compute point of intersection\n        if(Math.abs(t) > 1e-6) {\n          t = g0 / t;\n        } else {\n          continue;\n        }\n        \n        //Interpolate vertices and add up intersections (this can be done without multiplying)\n        for(var j=0, k=1; j<3; ++j, k<<=1) {\n          var a = e0 & k\n            , b = e1 & k;\n          if(a !== b) {\n            v[j] += a ? 1.0 - t : t;\n          } else {\n            v[j] += a ? 1.0 : 0;\n          }\n        }\n      }\n      \n      //Now we just average the edge intersections and add them to coordinate\n      var s = 1.0 / e_count;\n      for(var i=0; i<3; ++i) {\n        v[i] = scale[i] * (x[i] + s * v[i]) + shift[i];\n      }\n      \n      //Add vertex to buffer, store pointer to vertex index in buffer\n      buffer[m] = vertices.length;\n      vertices.push(v);\n      \n      //Now we need to add faces together, to do this we just loop over 3 basis components\n      for(var i=0; i<3; ++i) {\n        //The first three entries of the edge_mask count the crossings along the edge\n        if(!(edge_mask & (1<<i)) ) {\n          continue;\n        }\n        \n        // i = axes we are point along.  iu, iv = orthogonal axes\n        var iu = (i+1)%3\n          , iv = (i+2)%3;\n          \n        //If we are on a boundary, skip it\n        if(x[iu] === 0 || x[iv] === 0) {\n          continue;\n        }\n        \n        //Otherwise, look up adjacent edges in buffer\n        var du = R[iu]\n          , dv = R[iv];\n        \n        //Remember to flip orientation depending on the sign of the corner.\n        if(mask & 1) {\n          faces.push([buffer[m],    buffer[m-du],    buffer[m-dv]]);\n          faces.push([buffer[m-dv], buffer[m-du],    buffer[m-du-dv]]);\n        } else {\n          faces.push([buffer[m],    buffer[m-dv],    buffer[m-du]]);\n          faces.push([buffer[m-du], buffer[m-dv],    buffer[m-du-dv]]);\n        }\n      }\n    }\n  }\n  \n  //All done!  Return the result\n  return { positions: vertices, cells: faces };\n};\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"isosurface\",\n  \"version\": \"1.0.0\",\n  \"description\": \"3D isosurface polygonizer\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git://github.com/mikolalysenko/isosurface.git\"\n  },\n  \"keywords\": [\n    \"voxel\",\n    \"isosurface\",\n    \"level\",\n    \"set\",\n    \"0fps\",\n    \"surface\",\n    \"nets\",\n    \"marching\",\n    \"cubes\",\n    \"tetrahedra\"\n  ],\n  \"author\": \"Mikola Lysenko\",\n  \"license\": \"MIT\",\n  \"readmeFilename\": \"README.md\",\n  \"gitHead\": \"20517b13d1db64cdbf0498eb9eee728265faee9b\"\n}\n"
  },
  {
    "path": "test/sphere.js",
    "content": "console.log(require(\"../index.js\").surfaceNets([32,32,32],\n  function(x,y,z) {\n    return Math.sqrt(x*x+y*y+z*z) - 7;\n  }, [[-10,-10,-10],[10,10,10]]))\n"
  }
]