import Grid2D from 'Artistoo/src/grid/Grid2D.js'
public class | source

Grid2D

Extends:

Grid → Grid2D

Direct Subclass:

CoarseGrid

A class containing (mostly static) utility functions for dealing with 2D grids. Extends the Grid class but implements functions specific to the 2D grid (such as neighborhoods).

Example:

Diffusion on a 2D chemokine grid
 let CPM = require("path/to/build")

 // Make a grid with a chemokine, add some chemokine at the middle pixel
let chemoGrid = new CPM.Grid2D( [100,100], [true,true], "Float32" )
chemoGrid.setpix( [50,50], 100 )

// Measure chemokine at different spots before and after diffusion
let p1 = [50,50], p2 = [50,51]
console.log( "p1: " + chemoGrid.pixt( p1 ) + ", p2: " + chemoGrid.pixt(p2) )
chemoGrid.diffusion( 0.001 )
console.log( "p1: " + chemoGrid.pixt( p1 ) + ", p2: " + chemoGrid.pixt(p2) )
chemoGrid.multiplyBy( 0.9 )	 // decay of the chemokine
console.log( "p1: " + chemoGrid.pixt( p1 ) + ", p2: " + chemoGrid.pixt(p2) )
Neighborhoods on a 2D grid

let CPM = require("path/to/build")

// Make a grid with a torus, and find the neighborhood of the upper left pixel [0,0]
let grid = new CPM.Grid2D( [100,100], [true,true] )
console.log( grid.neigh( [0,0] ) ) // returns coordinates of 8 neighbors

// Now try a grid without torus; the corner now has fewer neighbors.
let grid2 = new CPM.Grid2D( [100,100], [false,false] )
console.log( grid2.neigh( [0,0] ) ) // returns only 3 neighbors

// Or try a Neumann neighborhood using the iterator
for( let i of grid.neighNeumanni( 0 ) ){
	console.log( grid.i2p(i).join(" ") )
}

Test:

Constructor Summary

Public Constructor
public

constructor(extents: GridSize, torus: boolean[], datatype: string)

Constructor of the Grid2D object.

Member Summary

Public Members
public
Private Members
private

Array with values for each pixel stored at the position of its IndexCoordinate.

Method Summary

Public Methods
public

Method to compute the gradient at location i on the grid (location given as an IndexCoordinate).

public

Method for conversion from an IndexCoordinate to an ArrayCoordinate.

public

Return array of IndexCoordinate of von Neumann-4 neighbor pixels of the pixel at coordinate i.

public

Return array of IndexCoordinate of Moore-8 neighbor pixels of the pixel at coordinate i.

public

Method for conversion from an ArrayCoordinate to an IndexCoordinate.

public

* pixels(): Pixel

This iterator returns locations and values of all non-zero pixels.

public

This iterator returns locations and values of all pixels.

Inherited Summary

From class Grid
private get

Return the array this._pixelArray, which should be set in the grid subclass.

private set

Set or update the private this._pixelArray.

public
public

field_size array of field size in each dimension.

public

Array coordinates to the middle pixel on the grid.

public

Number of dimensions of the grid.

public

Should the borders of the grid be linked, so that a cell moving out on the left reappears on the right? Warning: setting to false can give artifacts if done incorrectly.

private

Array with values for each pixel stored at the position of its IndexCoordinate.

private

For storing a copy of all pixel values; eg for synchronous updating of some sort.

public

Apply a function to all pixel values on the grid.

public

Method to check if a given ArrayCoordinate falls within the bounds of this grid.

public

Method to correct an ArrayCoordinate outside the grid dimensions when the grid is wrapped (torus = true).

public

Perform a diffusion step on the grid, updating the values of all pixels according to Fick's second law of diffusion.

public

Method to compute the gradient at location p on the grid (location given in array coordinates).

public

Template method to compute the gradient at location i on the grid (location given in index coordinates).

public abstract

Method for conversion from an IndexCoordinate to an ArrayCoordinate.

public

Method to compute the laplacian at location p on the grid (location given in array coordinates).

public

Method to compute the laplacian at location i on the grid (location given in index coordinates).

public

Multiply all pixel values on the grid with a constant factor r.

public

The neigh method returns the neighborhood of a pixel p.

public abstract

A method to compute the Neumann neighborhood should be implemented in the Grid subclass if the laplacian (see below) is used.

public abstract

Method returning the (Moore) neighborhood of a pixel based on its IndexCoordinate.

public abstract

Method for conversion from an ArrayCoordinate to an IndexCoordinate.

public abstract

* pixels(): Pixel

This iterator returns locations and values of all non-zero pixels.

public

This method pre-allocates an array of the correct datatype to make a copy of the current pixel values.

public abstract

This iterator returns locations all pixels including background.

public

The pixt method finds the current value of a pixel p on the grid.

public

The pixti method finds the current value of a pixel i on the grid.

public

The setpix method changes the value of a pixel p on the grid to t.

public

The setpixi method changes the value of a pixel i on the grid to t.

private

_isValidValue(t: number, tol: number): void

Check if a value is valid on this type of grid.

Public Constructors

public constructor(extents: GridSize, torus: boolean[], datatype: string) source

Constructor of the Grid2D object.

Override:

Grid#constructor

Params:

NameTypeAttributeDescription
extents GridSize

the size of the grid in each dimension

torus boolean[]
  • optional
  • default: [true,true]

should the borders of the grid be linked, so that a cell moving out on the left reappears on the right? Should be an array specifying whether the torus holds in each dimension; eg [true,false] for a torus in x but not y dimension. Warning: setting the torus to false can give artifacts if not done properly, see Grid#torus.

datatype string
  • optional
  • default: "Uint16"

What datatype are the values associated with each pixel on the grid? Choose from "Uint16" or "Float32".

Public Members

public datatype: * source

Override:

Grid#datatype

Private Members

private _pixelArray: Uint16Array | Float32Array source

Array with values for each pixel stored at the position of its IndexCoordinate. E.g. the value of pixel with coordinate i is stored as this._pixelArray[i]. Note that this array is accessed indirectly via the _pixels set- and get methods.

Override:

Grid#_pixelArray

Public Methods

public gradienti(i: IndexCoordinate): number[] source

Method to compute the gradient at location i on the grid (location given as an IndexCoordinate).

Override:

Grid#gradienti

Params:

NameTypeAttributeDescription
i IndexCoordinate

index coordinate of a pixel to compute the gradient at

Return:

number[]

the gradient at position i.

Test:

See:

public i2p(i: IndexCoordinate): ArrayCoordinate source

Method for conversion from an IndexCoordinate to an ArrayCoordinate. See also Grid2D#p2i for the backward conversion.

Override:

Grid#i2p

Params:

NameTypeAttributeDescription
i IndexCoordinate

the coordinate of the pixel to convert

Return:

ArrayCoordinate

the converted coordinate.

Example:

let grid = new CPM.Grid2D( [100,100], [true,true] )
let p = grid.i2p( 5 )
console.log( p )
console.log( grid.p2i( p ))

Test:

public * neighNeumanni(i: IndexCoordinate, torus: boolean[]): IndexCoordinate[] source

Return array of IndexCoordinate of von Neumann-4 neighbor pixels of the pixel at coordinate i. This function takes the 2D Neumann-4 neighborhood, excluding the pixel itself.

Override:

Grid#neighNeumanni

Params:

NameTypeAttributeDescription
i IndexCoordinate

location of the pixel to get neighbors of.

torus boolean[]
  • optional
  • default: [true,true]

does the grid have linked borders? Defaults to the setting on this grid, see torus

Return:

IndexCoordinate[]

an array of coordinates for all the neighbors of i.

Test:

See:

public neighi(i: IndexCoordinate, torus: boolean[]): IndexCoordinate[] source

Return array of IndexCoordinate of Moore-8 neighbor pixels of the pixel at coordinate i. This function takes the 2D Moore-8 neighborhood, excluding the pixel itself.

Override:

Grid#neighi

Params:

NameTypeAttributeDescription
i IndexCoordinate

location of the pixel to get neighbors of.

torus boolean[]
  • optional

does the grid have linked borders? Defaults to the setting on this grid, see torus

Return:

IndexCoordinate[]

an array of coordinates for all the neighbors of i.

Test:

See:

public p2i(p: ArrayCoordinate): IndexCoordinate source

Method for conversion from an ArrayCoordinate to an IndexCoordinate.

See also Grid2D#i2p for the backward conversion.

Override:

Grid#p2i

Params:

NameTypeAttributeDescription
p ArrayCoordinate

the coordinate of the pixel to convert

Return:

IndexCoordinate

the converted coordinate.

Example:

let grid = new CPM.Grid2D( [100,100], [true,true] )
let p = grid.i2p( 5 )
console.log( p )
console.log( grid.p2i( p ))

Test:

public * pixels(): Pixel source

This iterator returns locations and values of all non-zero pixels.

Override:

Grid#pixels

Return:

Pixel

for each pixel, return an array [p,v] where p are the pixel's array coordinates on the grid, and v its value.

Example:

let CPM = require( "path/to/build" )
// make a grid and set some values
let grid = new CPM.Grid2D( [100,100], [true,true] )
grid.setpix( [0,0], 1 )
grid.setpix( [0,1], 5 )

// iterator
for( let p of grid.pixels() ){
	console.log( p )
}

public * pixelsi(): IndexCoordinate source

This iterator returns locations and values of all pixels. Whereas the pixels generator yields only non-background pixels and specifies both their ArrayCoordinate and value, this generator yields all pixels by IndexCoordinate and does not report value.

Override:

Grid#pixelsi

Return:

IndexCoordinate

for each pixel on the grid (including background pixels).

Example:

let CPM = require( "path/to/build" )
// make a grid and set some values
let grid = new CPM.Grid2D( [100,100], [true,true] )
grid.setpixi( 0, 1 )
grid.setpixi( 1, 5 )

// iterator
for( let i of grid.pixelsi() ){
	console.log( i )
}

Test: