Grid2D
Extends:
Direct Subclass:
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:
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) )
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(" ") )
}
Constructor Summary
Public Constructor | ||
public |
constructor(extents: GridSize, torus: boolean[], datatype: string) Constructor of the Grid2D object. |
Member Summary
Public Members | ||
public |
datatype: * |
Private Members | ||
private |
Array with values for each pixel stored at the position of its IndexCoordinate. |
Method Summary
Public Methods | ||
public |
gradienti(i: IndexCoordinate): number[] 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 |
* neighNeumanni(i: IndexCoordinate, torus: boolean[]): IndexCoordinate[] Return array of IndexCoordinate of von Neumann-4 neighbor pixels of the pixel at coordinate i. |
|
public |
neighi(i: IndexCoordinate, torus: boolean[]): IndexCoordinate[] 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 |
This iterator returns locations and values of all non-zero pixels. |
|
public |
* pixelsi(): IndexCoordinate This iterator returns locations and values of all pixels. |
Inherited Summary
From class Grid | ||
private get |
_pixels: Uint16Array | Float32Array: * Return the array this._pixelArray, which should be set in the grid subclass. |
|
private set |
_pixels(pixels: Uint16Array | Float32Array) Set or update the private this._pixelArray. |
|
public |
datatype: * |
|
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 |
_pixelArray: * 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 |
gradienti(i: IndexCoordinate): number[] 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 |
multiplyBy(r: number) Multiply all pixel values on the grid with a constant factor r. |
|
public |
neigh(p: ArrayCoordinate, torus: boolean[]): ArrayCoordinate[] The neigh method returns the neighborhood of a pixel p. |
|
public abstract |
* neighNeumanni(i: IndexCoordinate, torus: boolean[]): IndexCoordinate[] A method to compute the Neumann neighborhood should be implemented in the Grid subclass if the laplacian (see below) is used. |
|
public abstract |
neighi(i: IndexCoordinate, torus: boolean[]): IndexCoordinate[] 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 |
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 |
* pixelsi(): IndexCoordinate This iterator returns locations all pixels including background. |
|
public |
pixt(p: ArrayCoordinate): number The pixt method finds the current value of a pixel p on the grid. |
|
public |
pixti(i: IndexCoordinate): number The pixti method finds the current value of a pixel i on the grid. |
|
public |
setpix(p: ArrayCoordinate, t: number) The setpix method changes the value of a pixel p on the grid to t. |
|
public |
setpixi(i: IndexCoordinate, t: number) 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#constructorParams:
Name | Type | Attribute | Description |
extents | GridSize | the size of the grid in each dimension |
|
torus | boolean[] |
|
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 |
|
What datatype are the values associated with each pixel on the grid? Choose from "Uint16" or "Float32". |
Public Members
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#_pixelArrayPublic 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#gradientiParams:
Name | Type | Attribute | Description |
i | IndexCoordinate | index coordinate of a pixel to compute the gradient at |
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#i2pParams:
Name | Type | Attribute | Description |
i | IndexCoordinate | the coordinate of the pixel to convert |
Example:
let grid = new CPM.Grid2D( [100,100], [true,true] )
let p = grid.i2p( 5 )
console.log( p )
console.log( grid.p2i( p ))
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#neighNeumanniParams:
Name | Type | Attribute | Description |
i | IndexCoordinate | location of the pixel to get neighbors of. |
|
torus | boolean[] |
|
does the grid have linked borders? Defaults to the setting on this grid, see torus |
Test:
- Rigorous tests for Grid2D-specific methods. See GridExtensionSpec.js for more general tests. [ unit tests ] neighNeumanni method should return correct neighbors for specific cases:
- Rigorous tests for Grid2D-specific methods. See GridExtensionSpec.js for more general tests. [ unit tests ] neighNeumanni method should return correct neighbors
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#neighiParams:
Name | Type | Attribute | Description |
i | IndexCoordinate | location of the pixel to get neighbors of. |
|
torus | boolean[] |
|
does the grid have linked borders? Defaults to the setting on this grid, see torus |
Test:
- Rigorous tests for Grid2D-specific methods. See GridExtensionSpec.js for more general tests. [ unit tests ] neighi method should return correct neighbors for specific cases:
- Rigorous tests for Grid2D-specific methods. See GridExtensionSpec.js for more general tests. [ unit tests ] neighi method should return same results as neighiSimple
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#p2iParams:
Name | Type | Attribute | Description |
p | ArrayCoordinate | the coordinate of the pixel to convert |
Example:
let grid = new CPM.Grid2D( [100,100], [true,true] )
let p = grid.i2p( 5 )
console.log( p )
console.log( grid.p2i( p ))
public * pixels(): Pixel source
This iterator returns locations and values of all non-zero pixels.
Override:
Grid#pixelsReturn:
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#pixelsiExample:
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 )
}