surface-nets
============
Extract a simplicial level set from an [ndarray](https://github.com/mikolalysenko/ndarray) in any dimension using naive surface nets. This module works in both node.js and with [browserify](http://browserify.org/)!
# Example
Here is a 2D example:
```javascript
//Load modules
var surfaceNets = require("surface-nets")
var ndarray = require("ndarray")
var fill = require("ndarray-fill")
//Initialize array to a circle
var array = ndarray(new Float32Array(32*32), [32,32])
fill(array, function(i,j) {
return Math.pow(i-16,2) + Math.pow(j-16,2)
})
//Extract 2D contour (this is all there is to it!)
var complex = surfaceNets(array, 15*15)
//Write SVG image to stdout
var svgFile = ['')
console.log(svgFile.join(""))
```
And here is the output SVG:
This module also works in 3D. Here is an example:
```javascript
//Load modules
var surfaceNets = require("surface-nets")
var ndarray = require("ndarray")
var fill = require("ndarray-fill")
var mat4 = require("gl-matrix").mat4
//Initialize array
var array = ndarray(new Float32Array(32*32*32), [32,32,32])
fill(array, function(i,j,k) {
return Math.pow(i-16,2) + Math.pow(j-16,2) + Math.pow(k-16,2)
})
//Generate surface! (again, just one line)
var complex = surfaceNets(array, 100)
//Render the implicit surface to stdout
console.log('")
```
And here is the result:
And while it is a bit trivial, you can also generate surfaces in 1D:
```javascript
var surfaceNets = require("surface-nets")
var ndarray = require("ndarray")
console.log(surfaceNets(ndarray([1, -1, 0, 5, -10])))
```
Output:
```javascript
{ positions: [ [ 0.5 ], [ 2 ], [ 3.3333333333333335 ] ],
cells: [ [ 0 ], [ 1 ], [ 2 ] ] }
```
The code *should* work in 4D and higher dimensions, but this is not well tested and it is harder to visualize. (Also, why would you want to bother!?!)
# Install
```
npm install surface-nets
```
# API
#### `require("surface-nets")(array[,level])`
Extracts the level set at `level` from `array` as a simplicial complex.
* `array` is an [ndarray](https://github.com/mikolalysenko/ndarray)
* `level` is an optional number which determines the level at which the levelset is evaluated (default `0`)
**Returns** An object with a pair of properties representing a simplicial complex:
* `positions` is an array encoding the positions of the vertices. The coordinates of the positions are with respect to the indices in `array`.
* `cells` is an array encoding the cells of the simplicial complex as tuples of indices into the `position` array.
# Credits
(c) 2014 Mikola Lysenko. MIT License