SkillAgentSearch skills...

MarchingSquares.js

A JavaScript implementation of the Marching Squares algorithm featuring IsoContour and IsoBand computation

Install / Use

/learn @RaumZeit/MarchingSquares.js
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

GitHub tag Build Status npm License: AGPL v3

MarchingSquaresJS

A JavaScript implementation of the Marching Squares algorithm featuring IsoLines and IsoBand computation.

The implementation computes iso lines (iso contours) or iso bands for rectangular 2-dimensional scalar fields and returns an array of (closed) paths that enclose the respective threshold(s). To speed-up computations when multiple iso lines/iso bands are required, the implementation makes use of a Quad-Tree data structure for fast look-ups of those cells in the scalar field that actually contribute to the iso line or iso band, respectively.

Table of contents

  1. Availability
  2. Installation
  3. Usage
  4. API Description
  5. Examples
  6. License

Availability

The source code of this module is available through github. This module is also available as an npm package.


Installation

While this module only consists of ECMAScript 5 language elements, it already makes use of the ECMAScript 6 module specification. To provide maximum compatibility and allow for loading with node and web browsers, the library is bundled with rollup.js and wrapped in a Universal Module Definition (UMD) API by default.

Quick Start

npm install marchingsquares

Build from Repository:

To (re-)build the distribution bundles run rollup -c or

npm run-script build

Download Precompiled Files

Pre-compiled (minified) versions are available at the MarchingSquares.js release page.


Usage

In most cases, you may want to load the entire library to expose all implemented algorithms at once. Alternatively, you may include only one of the isoLines or isoBands algorithms. In this case, however, you have to sacrifice the possibility to pass pre-processed data to effectively circumvent redundant Quad-Tree construction since the QuadTree constructor will be unavailable.

The library exposes the following function attributes (see also API description)

MarchingSquaresJS = {
    isoLines : function(data, threshold, options){},
    isoBands : function(data, lowerBound, bandwidth, options){},
    QuadTree : function(data){}
};

and can easily be integrated into your project (see below)

Loading with Node:

var MarchingSquaresJS = require('./marchingsquares.js');

Loading with AMD (e.g. RequireJS)

The MarchingSquaresJS module works fine with the Asynchronous Module Definition (AMD) API. This enables easy integration with module loaders such as RequireJS

var MarchingSquaresJS = require('./marchingsquares-isobands.js');

Loading with Web Browser

To use the library in a web browser you simply load the library using the <script> tag to expose a global variable MarchingSquaresJS:

<script src="marchingsquares.min.js"></script>

Loading a Single Implementation

It is possible to require only one of the implementations, isoLines or isoBands, by requiring the corresponding implementation directly, e.g.:

var MarchingSquaresJS = require('./marchingsquares-isobands.js');

or

<script src="marchingsquares-isobands.min.js"></script>

This creates the same object as before but without the isoLines function.


API Description

Computing Iso Lines

function isoLines(data, threshold, options)

Compute iso lines and iso contours for a 2-dimensional scalar field and a (list of) thresholds.

| Parameter | Description | | ----------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | data | 2-dimensional input data, i.e. the scalar field (must be array of arrays, or pre-processed data object obtained from new QuadTree()). This parameter is mandatory. | | threshold | A constant numerical value (or array of numerical values) defining the curve function for the iso line(s). This parameter is mandatory | | options | An object with attributes allowing for changes in the behavior of this function (See below). This parameter is optional |

Returns:

  1. If threshold is a single scalar, an array of paths representing the iso lines for the given threshold and input data.
  2. If threshold is an array of scalars, an additional array layer wraps the individual arrays of paths for each threshold value.

A single path is an array of coordinates where each coordinate, again, is an array with two entries [ x, y ] denoting the x and y position, respectively.

Note, that the paths resemble linear Rings by default, i.e. they are closed and have identical first and last coordinates. (see the options parameter to change the output)

Furthermore, if all values at the border of the input data are below the threshold, a rectangular frame path with coordinates [ 0, 0 ], [0, rows], [cols, rows], [cols, 0], i.e. enclosing the entire scalar field, will be added as first element of the returned array. Here, the values of rows and cols are the number of rows and columns of the input data, respectively. To disable this behavior, the user may pass the options.noFrame=true.

Computing Iso Bands

function isoBands(data, lowerBound, bandWidth, options)

Compute iso bands for a 2-dimensional scalar field, a (list of) lowerBound(s), and a (list of) bandWidth(s).

| Parameter | Description | | ------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | data | 2-dimensional input data, i.e. the scalar field (must be array of arrays, or pre-processed data object obtained from new QuadTree()). This parameter is mandatory. | | lowerBound | A constant numerical value (or array of numerical values) that define(s) the lower bound of the iso band. This parameter is mandatory. | | bandWidth | A constant numerical value (or array of numerical values) that defines the width(s) the iso band, i.e. the range of values. This parameter is mandatory. | | options | An object with attributes allowing for changes in the behavior of this function (See below). This parameter is optional. |

Returns:

  1. If lowerBound is a single scalar, an array of paths representing the iso lines which enclose the iso band of size bandWidth.
  2. If lowerBound is an array of scalars, an additional array layer wraps the individual arrays of paths for each threshold-bandWidth pair. Note, that if bandWidth is a scalar it will be applied to all entries in the lowerBound array.

A single path is an array of coordinates where each coordinate, again, is an array with two entries [ x, y ] denoting the x and y position, respectively.

Note, that the paths resemble linear Rings by default, i.e. they are closed and have identical first and last coordinates. (see the options parameter to change the output)

Pre-process Data

function QuadTree(data)

Pre-compute a Quad-Tree for the scalar field data.

To speed-up consecutive calls of the isoLines and isoBands functions using the same scalar field but different threshold or band levels, users can pass pre-processed data. The pre-processing step essentially creates a Quad-Tree data structure for the scalar field, and glues it together with the scalar field. Consequently, when passing pre-processed data, the isoLines andisoBands functions do not need to create the same Quad-Tree (for the same scalar field) over and over again.

| Parameter | Description | | ------------- | :------------------------------------------------------------------------ | | data | 2-dimensional input data (scalar field). This parameter is mandatory. |

Returns:

An object that glues together the scalar field data and the corresponding pre-computed Quad-Tree.

Note:

This is a constructor function! Thus, to generate an object with pre-processed data, one has to create a new QuadTree object:

var prepData = new MarchingSquaresJS.QuadTree(data);

Furthermore, when passing pre-processed data to one of the isoLines or isoBands function, they will always perform Quad-Tree look-ups to speed-up the computation, unless the options.noQuadTree flag is set.

The Options Object

The options object may have the following fiel

View on GitHub
GitHub Stars174
CategoryDevelopment
Updated2mo ago
Forks26

Languages

JavaScript

Security Score

80/100

Audited on Jan 19, 2026

No findings