├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── lib ├── marchingcubes.js ├── marchingtetrahedra.js └── surfacenets.js ├── package.json └── test └── sphere.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2013 Mikola Lysenko 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | isosurface 2 | ========== 3 | 4 | Isosurface polygonizer algorithms in JavaScript. For more details, see the following blog posts: 5 | 6 | * [What is a solid?](http://0fps.wordpress.com/2012/08/29/what-is-a-solid/) 7 | * 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/) 8 | 9 | [Or try out a live demo](http://mikolalysenko.github.com/Isosurface/) 10 | 11 | # Example 12 | 13 | ```javascript 14 | var isosurface = require("isosurface") 15 | 16 | var mesh = isosurface.surfaceNets([64,64,64], function(x,y,z) { 17 | return x*x + y*y + z*z - 100 18 | }, [[-11,-11,-11], [11,11,11]]) 19 | 20 | console.log(mesh) 21 | ``` 22 | 23 | 24 | # Install 25 | 26 | ``` 27 | npm install isosurface 28 | ``` 29 | 30 | # API 31 | 32 | ```javascript 33 | var isosurface = require("isosurface") 34 | ``` 35 | 36 | #### `isosurface.surfaceNets(dims, potential[, bounds])` 37 | Extracts an isosurface from `potential` using surface nets with resolution given by `dims`. 38 | 39 | Params: 40 | * `dims`: A 3D vector of integers representing the resolution of the isosurface 41 | * `potential(x,y,z)`: A scalar valued potential function taking 3 coordinates as arguments returning a scalar. 42 | * `bounds`: A pair of 3D vectors `[lo, hi]` giving bounds on the potential to sample. If not specified, default is `[[0,0,0], dims]`. 43 | 44 | Returns: A mesh object with the following members: 45 | * `positions`: The coordinates of the vertices of the mesh 46 | * `cells`: The faces of the mesh. 47 | 48 | #### `isosurface..marchingCubes(dims, potential[, bounds])` 49 | 50 | Same as above, except uses marching cubes instead of surface nets to extract the isosurface. 51 | 52 | #### `isosurface.marchingTetrahedra(dims, potential[, bounds])` 53 | 54 | Same as above, except uses marching tetrahedra instead of surface nets to extract the isosurface. 55 | 56 | 57 | # Credits 58 | (c) 2012-2014 Mikola Lysenko. MIT License -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | exports.surfaceNets = require("./lib/surfacenets.js").surfaceNets; 2 | exports.marchingCubes = require("./lib/marchingcubes.js").marchingCubes; 3 | exports.marchingTetrahedra = require("./lib/marchingtetrahedra.js").marchingTetrahedra; 4 | -------------------------------------------------------------------------------- /lib/marchingcubes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Javascript Marching Cubes 3 | * 4 | * Based on Paul Bourke's classic implementation: 5 | * http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/ 6 | * 7 | * JS port by Mikola Lysenko 8 | */ 9 | 10 | var edgeTable= new Uint32Array([ 11 | 0x0 , 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, 12 | 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00, 13 | 0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c, 14 | 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90, 15 | 0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c, 16 | 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30, 17 | 0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac, 18 | 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0, 19 | 0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c, 20 | 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60, 21 | 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc, 22 | 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0, 23 | 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c, 24 | 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950, 25 | 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc , 26 | 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0, 27 | 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc, 28 | 0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0, 29 | 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c, 30 | 0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650, 31 | 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc, 32 | 0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0, 33 | 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c, 34 | 0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460, 35 | 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac, 36 | 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0, 37 | 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c, 38 | 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230, 39 | 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c, 40 | 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190, 41 | 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, 42 | 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0 ]) 43 | , triTable = [ 44 | [], 45 | [0, 8, 3], 46 | [0, 1, 9], 47 | [1, 8, 3, 9, 8, 1], 48 | [1, 2, 10], 49 | [0, 8, 3, 1, 2, 10], 50 | [9, 2, 10, 0, 2, 9], 51 | [2, 8, 3, 2, 10, 8, 10, 9, 8], 52 | [3, 11, 2], 53 | [0, 11, 2, 8, 11, 0], 54 | [1, 9, 0, 2, 3, 11], 55 | [1, 11, 2, 1, 9, 11, 9, 8, 11], 56 | [3, 10, 1, 11, 10, 3], 57 | [0, 10, 1, 0, 8, 10, 8, 11, 10], 58 | [3, 9, 0, 3, 11, 9, 11, 10, 9], 59 | [9, 8, 10, 10, 8, 11], 60 | [4, 7, 8], 61 | [4, 3, 0, 7, 3, 4], 62 | [0, 1, 9, 8, 4, 7], 63 | [4, 1, 9, 4, 7, 1, 7, 3, 1], 64 | [1, 2, 10, 8, 4, 7], 65 | [3, 4, 7, 3, 0, 4, 1, 2, 10], 66 | [9, 2, 10, 9, 0, 2, 8, 4, 7], 67 | [2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4], 68 | [8, 4, 7, 3, 11, 2], 69 | [11, 4, 7, 11, 2, 4, 2, 0, 4], 70 | [9, 0, 1, 8, 4, 7, 2, 3, 11], 71 | [4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1], 72 | [3, 10, 1, 3, 11, 10, 7, 8, 4], 73 | [1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4], 74 | [4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3], 75 | [4, 7, 11, 4, 11, 9, 9, 11, 10], 76 | [9, 5, 4], 77 | [9, 5, 4, 0, 8, 3], 78 | [0, 5, 4, 1, 5, 0], 79 | [8, 5, 4, 8, 3, 5, 3, 1, 5], 80 | [1, 2, 10, 9, 5, 4], 81 | [3, 0, 8, 1, 2, 10, 4, 9, 5], 82 | [5, 2, 10, 5, 4, 2, 4, 0, 2], 83 | [2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8], 84 | [9, 5, 4, 2, 3, 11], 85 | [0, 11, 2, 0, 8, 11, 4, 9, 5], 86 | [0, 5, 4, 0, 1, 5, 2, 3, 11], 87 | [2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5], 88 | [10, 3, 11, 10, 1, 3, 9, 5, 4], 89 | [4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10], 90 | [5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3], 91 | [5, 4, 8, 5, 8, 10, 10, 8, 11], 92 | [9, 7, 8, 5, 7, 9], 93 | [9, 3, 0, 9, 5, 3, 5, 7, 3], 94 | [0, 7, 8, 0, 1, 7, 1, 5, 7], 95 | [1, 5, 3, 3, 5, 7], 96 | [9, 7, 8, 9, 5, 7, 10, 1, 2], 97 | [10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3], 98 | [8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2], 99 | [2, 10, 5, 2, 5, 3, 3, 5, 7], 100 | [7, 9, 5, 7, 8, 9, 3, 11, 2], 101 | [9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11], 102 | [2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7], 103 | [11, 2, 1, 11, 1, 7, 7, 1, 5], 104 | [9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11], 105 | [5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0], 106 | [11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0], 107 | [11, 10, 5, 7, 11, 5], 108 | [10, 6, 5], 109 | [0, 8, 3, 5, 10, 6], 110 | [9, 0, 1, 5, 10, 6], 111 | [1, 8, 3, 1, 9, 8, 5, 10, 6], 112 | [1, 6, 5, 2, 6, 1], 113 | [1, 6, 5, 1, 2, 6, 3, 0, 8], 114 | [9, 6, 5, 9, 0, 6, 0, 2, 6], 115 | [5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8], 116 | [2, 3, 11, 10, 6, 5], 117 | [11, 0, 8, 11, 2, 0, 10, 6, 5], 118 | [0, 1, 9, 2, 3, 11, 5, 10, 6], 119 | [5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11], 120 | [6, 3, 11, 6, 5, 3, 5, 1, 3], 121 | [0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6], 122 | [3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9], 123 | [6, 5, 9, 6, 9, 11, 11, 9, 8], 124 | [5, 10, 6, 4, 7, 8], 125 | [4, 3, 0, 4, 7, 3, 6, 5, 10], 126 | [1, 9, 0, 5, 10, 6, 8, 4, 7], 127 | [10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4], 128 | [6, 1, 2, 6, 5, 1, 4, 7, 8], 129 | [1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7], 130 | [8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6], 131 | [7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9], 132 | [3, 11, 2, 7, 8, 4, 10, 6, 5], 133 | [5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11], 134 | [0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6], 135 | [9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6], 136 | [8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6], 137 | [5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11], 138 | [0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7], 139 | [6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9], 140 | [10, 4, 9, 6, 4, 10], 141 | [4, 10, 6, 4, 9, 10, 0, 8, 3], 142 | [10, 0, 1, 10, 6, 0, 6, 4, 0], 143 | [8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10], 144 | [1, 4, 9, 1, 2, 4, 2, 6, 4], 145 | [3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4], 146 | [0, 2, 4, 4, 2, 6], 147 | [8, 3, 2, 8, 2, 4, 4, 2, 6], 148 | [10, 4, 9, 10, 6, 4, 11, 2, 3], 149 | [0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6], 150 | [3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10], 151 | [6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1], 152 | [9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3], 153 | [8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1], 154 | [3, 11, 6, 3, 6, 0, 0, 6, 4], 155 | [6, 4, 8, 11, 6, 8], 156 | [7, 10, 6, 7, 8, 10, 8, 9, 10], 157 | [0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10], 158 | [10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0], 159 | [10, 6, 7, 10, 7, 1, 1, 7, 3], 160 | [1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7], 161 | [2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9], 162 | [7, 8, 0, 7, 0, 6, 6, 0, 2], 163 | [7, 3, 2, 6, 7, 2], 164 | [2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7], 165 | [2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7], 166 | [1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11], 167 | [11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1], 168 | [8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6], 169 | [0, 9, 1, 11, 6, 7], 170 | [7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0], 171 | [7, 11, 6], 172 | [7, 6, 11], 173 | [3, 0, 8, 11, 7, 6], 174 | [0, 1, 9, 11, 7, 6], 175 | [8, 1, 9, 8, 3, 1, 11, 7, 6], 176 | [10, 1, 2, 6, 11, 7], 177 | [1, 2, 10, 3, 0, 8, 6, 11, 7], 178 | [2, 9, 0, 2, 10, 9, 6, 11, 7], 179 | [6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8], 180 | [7, 2, 3, 6, 2, 7], 181 | [7, 0, 8, 7, 6, 0, 6, 2, 0], 182 | [2, 7, 6, 2, 3, 7, 0, 1, 9], 183 | [1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6], 184 | [10, 7, 6, 10, 1, 7, 1, 3, 7], 185 | [10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8], 186 | [0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7], 187 | [7, 6, 10, 7, 10, 8, 8, 10, 9], 188 | [6, 8, 4, 11, 8, 6], 189 | [3, 6, 11, 3, 0, 6, 0, 4, 6], 190 | [8, 6, 11, 8, 4, 6, 9, 0, 1], 191 | [9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6], 192 | [6, 8, 4, 6, 11, 8, 2, 10, 1], 193 | [1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6], 194 | [4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9], 195 | [10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3], 196 | [8, 2, 3, 8, 4, 2, 4, 6, 2], 197 | [0, 4, 2, 4, 6, 2], 198 | [1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8], 199 | [1, 9, 4, 1, 4, 2, 2, 4, 6], 200 | [8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1], 201 | [10, 1, 0, 10, 0, 6, 6, 0, 4], 202 | [4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3], 203 | [10, 9, 4, 6, 10, 4], 204 | [4, 9, 5, 7, 6, 11], 205 | [0, 8, 3, 4, 9, 5, 11, 7, 6], 206 | [5, 0, 1, 5, 4, 0, 7, 6, 11], 207 | [11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5], 208 | [9, 5, 4, 10, 1, 2, 7, 6, 11], 209 | [6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5], 210 | [7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2], 211 | [3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6], 212 | [7, 2, 3, 7, 6, 2, 5, 4, 9], 213 | [9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7], 214 | [3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0], 215 | [6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8], 216 | [9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7], 217 | [1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4], 218 | [4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10], 219 | [7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10], 220 | [6, 9, 5, 6, 11, 9, 11, 8, 9], 221 | [3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5], 222 | [0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11], 223 | [6, 11, 3, 6, 3, 5, 5, 3, 1], 224 | [1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6], 225 | [0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10], 226 | [11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5], 227 | [6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3], 228 | [5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2], 229 | [9, 5, 6, 9, 6, 0, 0, 6, 2], 230 | [1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8], 231 | [1, 5, 6, 2, 1, 6], 232 | [1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6], 233 | [10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0], 234 | [0, 3, 8, 5, 6, 10], 235 | [10, 5, 6], 236 | [11, 5, 10, 7, 5, 11], 237 | [11, 5, 10, 11, 7, 5, 8, 3, 0], 238 | [5, 11, 7, 5, 10, 11, 1, 9, 0], 239 | [10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1], 240 | [11, 1, 2, 11, 7, 1, 7, 5, 1], 241 | [0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11], 242 | [9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7], 243 | [7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2], 244 | [2, 5, 10, 2, 3, 5, 3, 7, 5], 245 | [8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5], 246 | [9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2], 247 | [9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2], 248 | [1, 3, 5, 3, 7, 5], 249 | [0, 8, 7, 0, 7, 1, 1, 7, 5], 250 | [9, 0, 3, 9, 3, 5, 5, 3, 7], 251 | [9, 8, 7, 5, 9, 7], 252 | [5, 8, 4, 5, 10, 8, 10, 11, 8], 253 | [5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0], 254 | [0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5], 255 | [10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4], 256 | [2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8], 257 | [0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11], 258 | [0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5], 259 | [9, 4, 5, 2, 11, 3], 260 | [2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4], 261 | [5, 10, 2, 5, 2, 4, 4, 2, 0], 262 | [3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9], 263 | [5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2], 264 | [8, 4, 5, 8, 5, 3, 3, 5, 1], 265 | [0, 4, 5, 1, 0, 5], 266 | [8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5], 267 | [9, 4, 5], 268 | [4, 11, 7, 4, 9, 11, 9, 10, 11], 269 | [0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11], 270 | [1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11], 271 | [3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4], 272 | [4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2], 273 | [9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3], 274 | [11, 7, 4, 11, 4, 2, 2, 4, 0], 275 | [11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4], 276 | [2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9], 277 | [9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7], 278 | [3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10], 279 | [1, 10, 2, 8, 7, 4], 280 | [4, 9, 1, 4, 1, 7, 7, 1, 3], 281 | [4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1], 282 | [4, 0, 3, 7, 4, 3], 283 | [4, 8, 7], 284 | [9, 10, 8, 10, 11, 8], 285 | [3, 0, 9, 3, 9, 11, 11, 9, 10], 286 | [0, 1, 10, 0, 10, 8, 8, 10, 11], 287 | [3, 1, 10, 11, 3, 10], 288 | [1, 2, 11, 1, 11, 9, 9, 11, 8], 289 | [3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9], 290 | [0, 2, 11, 8, 0, 11], 291 | [3, 2, 11], 292 | [2, 3, 8, 2, 8, 10, 10, 8, 9], 293 | [9, 10, 2, 0, 9, 2], 294 | [2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8], 295 | [1, 10, 2], 296 | [1, 3, 8, 9, 1, 8], 297 | [0, 9, 1], 298 | [0, 3, 8], 299 | []] 300 | , cubeVerts = [ 301 | [0,0,0] 302 | ,[1,0,0] 303 | ,[1,1,0] 304 | ,[0,1,0] 305 | ,[0,0,1] 306 | ,[1,0,1] 307 | ,[1,1,1] 308 | ,[0,1,1]] 309 | , 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] ]; 310 | 311 | 312 | 313 | exports.marchingCubes = function(dims, potential, bounds) { 314 | if(!bounds) { 315 | bounds = [[0,0,0], dims]; 316 | } 317 | var scale = [0,0,0]; 318 | var shift = [0,0,0]; 319 | for(var i=0; i<3; ++i) { 320 | scale[i] = (bounds[1][i] - bounds[0][i]) / dims[i]; 321 | shift[i] = bounds[0][i]; 322 | } 323 | 324 | var vertices = [] 325 | , faces = [] 326 | , n = 0 327 | , grid = new Array(8) 328 | , edges = new Array(12) 329 | , x = [0,0,0]; 330 | //March over the volume 331 | for(x[2]=0; x[2] 0) ? 1 << i : 0; 344 | } 345 | //Compute vertices 346 | var edge_mask = edgeTable[cube_index]; 347 | if(edge_mask === 0) { 348 | continue; 349 | } 350 | for(var i=0; i<12; ++i) { 351 | if((edge_mask & (1< 1e-6) { 364 | t = a / d; 365 | } 366 | for(var j=0; j<3; ++j) { 367 | nv[j] = scale[j] * ((x[j] + p0[j]) + t * (p1[j] - p0[j])) + shift[j]; 368 | } 369 | vertices.push(nv); 370 | } 371 | //Add faces 372 | var f = triTable[cube_index]; 373 | for(var i=0; i 1e-6) { 56 | t = g0 / t; 57 | } 58 | for(var i=0; i<3; ++i) { 59 | v[i] = scale[i] * (v[i] + p0[i] + t * (p1[i] - p0[i])) + shift[i]; 60 | } 61 | vertices.push(v); 62 | return vertices.length - 1; 63 | } 64 | 65 | //March over the volume 66 | for(x[2]=0; x[2] 2^(edge configuration) map 33 | // There is one entry for each possible cube configuration, and the output is a 12-bit vector enumerating all edges crossing the 0-level. 34 | for(var i=0; i<256; ++i) { 35 | var em = 0; 36 | for(var j=0; j<24; j+=2) { 37 | var a = !!(i & (1<> 1)) : 0; 40 | } 41 | edge_table[i] = em; 42 | } 43 | })(); 44 | 45 | //Internal buffer, this may get resized at run time 46 | var buffer = new Array(4096); 47 | (function() { 48 | for(var i=0; i buffer.length) { 76 | var ol = buffer.length; 77 | buffer.length = R[2] * 2; 78 | while(ol < buffer.length) { 79 | buffer[ol++] = 0; 80 | } 81 | } 82 | 83 | //March over the voxel grid 84 | for(x[2]=0; x[2] 1e-6) { 136 | t = g0 / t; 137 | } else { 138 | continue; 139 | } 140 | 141 | //Interpolate vertices and add up intersections (this can be done without multiplying) 142 | for(var j=0, k=1; j<3; ++j, k<<=1) { 143 | var a = e0 & k 144 | , b = e1 & k; 145 | if(a !== b) { 146 | v[j] += a ? 1.0 - t : t; 147 | } else { 148 | v[j] += a ? 1.0 : 0; 149 | } 150 | } 151 | } 152 | 153 | //Now we just average the edge intersections and add them to coordinate 154 | var s = 1.0 / e_count; 155 | for(var i=0; i<3; ++i) { 156 | v[i] = scale[i] * (x[i] + s * v[i]) + shift[i]; 157 | } 158 | 159 | //Add vertex to buffer, store pointer to vertex index in buffer 160 | buffer[m] = vertices.length; 161 | vertices.push(v); 162 | 163 | //Now we need to add faces together, to do this we just loop over 3 basis components 164 | for(var i=0; i<3; ++i) { 165 | //The first three entries of the edge_mask count the crossings along the edge 166 | if(!(edge_mask & (1<