Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolalysenko committed Jan 14, 2013
1 parent 41b5fa5 commit aaeeff5
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
49 changes: 48 additions & 1 deletion README.md
@@ -1,4 +1,51 @@
rle-stencils
============

Stencil generator code for the rle libraries
Stencil generator code for the rle libraries


Basic Usage
===========

To install the library, just do:

npm install rle-stencils

Then in your code you can import the stencils by:

//Import stencil library
var stencils = require('rle-stencils');

Example: Moore
--------------

Here is how to create a stencil for a [Moore neighborhood](http://en.wikipedia.org/wiki/Moore_neighborhood) with radius 2:

var moore_neighborhood = stencils.moore(2);

Example: von Neumann
--------------------

And here is the same idea applied to a [von Neumann neighborhood](http://en.wikipedia.org/wiki/Von_Neumann_neighborhood)

var von_eumann_neighborhood = stencils.vonNeumann(1);

Example: Sphere
---------------

var ball_neighborhood = stencils.ball(5);

Example: L^p
------------

More generally, you can create a stencil for any [Lp ball](http://en.wikipedia.org/wiki/Lp_space), with p being some fixed power. For example, here is how to make an L^3 ball with radius 6:

var cubic_neighborhood = stencils.lp(3, 6);

In general, calls to `lp(p, r)` return the collection of all integer lattice points `x` satisfying the inequality:

Math.pow(Math.abs(x[0]), p) + Math.pow(Math.abs(x[1]), p) + Math.pow(Math.abs(x[2]), p) <= Math.pow(r, p)

Acknowledgements
================
(c) 2013 Mikola Lysenko. MIT License
19 changes: 19 additions & 0 deletions package.json
@@ -0,0 +1,19 @@
{
"name": "rle-stencils",
"version": "0.0.0",
"description": "Stencils for use with rle library",
"main": "stencils.js",
"repository": {
"type": "git",
"url": "git://github.com/mikolalysenko/rle-stencils.git"
},
"keywords": [
"stencil",
"narrowband",
"levelset"
],
"author": "Mikola Lysenko",
"license": "MIT",
"readmeFilename": "README.md",
"gitHead": "41b5fa5b369a8894a6196fdea2efce0e94daead5"
}
38 changes: 38 additions & 0 deletions stencils.js
@@ -0,0 +1,38 @@
//Generates a Moore neighborhood with given radius
function moore(radius_) {
var result = []
, radius = Math.ceil(radius_);
for(var i=-radius; i<=radius; ++i) {
for(var j=-radius; j<=radius; ++j) {
for(var k=-radius; k<=radius; ++k) {
result.push([i,j,k]);
}
}
}
return result;
}

//Creates an Lp ball stencil
function lp(p, radius_) {
if(p === Number.POSITIVE_INFINITY) {
return moore(radius_);
}
var result = []
, radius = Math.ceil(radius_)
, rp = Math.pow(radius_, p);
for(var i=-radius; i<=radius; ++i) {
for(var j=-radius; j<=radius; ++j) {
for(var k=-radius; k<=radius; ++k) {
if(Math.pow(Math.abs(i), p) + Math.pow(Math.abs(i), p) + Math.pow(Math.abs(i), p) <= rp) {
result.push([i,j,k]);
}
}
}
}
return result;
}

exports.lp = lp;
exports.moore = moore;
exports.vonNeumann = lp.bind(this, 1);
exports.ball = lp.bind(this, 2);

0 comments on commit aaeeff5

Please sign in to comment.