Iso-surface extraction into polygons.

This class, with only one static method provides an interface to an algorithm which will, when given a function mapping each point in a given AABB to a scalar value extract approximated polygons which represent the region of the AABB where the function returns a negative value.

This function could be a mathematical function like the equation of a circle: function (x, y) return (xx + yy) - r*r
Or something more practical like the biased alpha value interpolated from a Bitmap:

function (x, y) {
var ix = if (x < 0) 0 else if (x >= bitmap.width - 1) bitmap.width - 2 else;
var iy = if (y < 0) 0 else if (y >= bitmap.height - 1) bitmap.height - 2 else;
var fx = x - ix;
var fy = y - iy;
var gx = 1 - fx;
var gy = 1 - fy;

var a00 = bitmap.getPixel32(ix, iy) >>> 24; var a01 = bitmap.getPixel32(ix, iy + 1) >>> 24; var a10 = bitmap.getPixel32(ix + 1, iy) >>> 24; var a11 = bitmap.getPixel32(ix + 1, iy + 1) >>> 24;

return 0x80 - (gxgya00 + fxgya10 + gxfya01 + fxfya11); }

For 'flash', we must wrap this in an IsoFunction interface to be used by MarchingSquares for performance reasons:
class BitmapIsoFunction implements nape.geom.IsoFunction {

public function iso(x:Float, y:Float):Float {


This function is converted into a set of polygons by sampling along regular grid points, and then recursively interpolating along cell edges based on the function provided to find the point in space along that edge where the function is approximately 0.

From this we generate polygons in each grid cell, which are then by default combined into larger, weakly simply polygons suitable for use in the decomposition routines of GeomPoly like convexDecomposition!

The runtime of the algorithm is O(N+K) for N number of cells and K number of output vertices (A final pass is made to remove unnecessary vertices).

Static methods

@:has_untyped@:value({ output : null, combine : true, subgrid : null, quality : 2 })staticrun(iso:IsoFunctionDef, bounds:AABB, cellsize:Vec2, quality:Int = 2, ?subgrid:Vec2, combine:Bool = true, ?output:GeomPolyList):Null<GeomPolyList>

Execute marching squares algorithm over region of space.

We can, optionally provide a subgrid argument which, when non null will invoke this algorithm seperately on each subgrid cell of the region in space, instead of on the entire region at once. This can be very useful as shown in the DestructibleTerrain demo where regions of a terrain are recomputed with marching squares without needing to regenerate the whole of the terrain.



The iso-function defining the regions where polygons should be extracted, a negative return indicates a region to be extracted. This function need not be continuous, but if it is continuous then more accurate results will be given for the same input parameters.


The AABB representing the region of space to be converted. The arguments to the iso-function will be in the same region.


The dimensions of each cell used individual polygon extraction. Smaller cells will give more accurate results at a greater cost permitting smaller features to be extracted.


This is the number of recursive interpolations which will be performed along cell edges. If the iso-function is not continuous, then this value should be increased to get better accuracy. (default 2)


When supplied, the region of space will be first subdivided into cells with these given dimensions, and each cell treated as a seperate invocation of this method, this value should obviously be greater than cellsize or it would be a bit pointless. (default null)


When True, the polygons generated in each cell of the grid will be combined into the largest possible weakly-simple polygons representing the same area. These polygons will always be suitable for decomposition in Nape. (default true)


When supplied, GeomPoly will be inserted into the list (via add) instead of creating a new GeomPolyList object.


A list of GeomPoly representing the results of the extraction.



If iso, bounds or cellsize argument is null.


If cellsize is disposed, or its components have 0, or negative values.


If quality is less than 0.


If subgrid is not null, but is disposed or has zero or negative component values.