Grid
Indirect Subclass:
This base class defines a general grid and provides grid methods that do not depend on the coordinate system used. This class is never used on its own, as it does not yet contain methods for neighborhood computation etc (which depend on the coordinate system). Coordinate system-dependent methods are implemented in extensions of the Grid class, for example Grid2D and Grid3D.
Test:
Constructor Summary
Public Constructor | ||
public |
constructor(field_size: GridSize, torus: boolean[]) The grid constructor. |
Member Summary
Public Members | ||
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 Members | ||
private |
_pixelArray: * Array with values for each pixel stored at the position of its IndexCoordinate. |
|
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. |
|
private |
For storing a copy of all pixel values; eg for synchronous updating of some sort. |
Method Summary
Public Methods | ||
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 Methods | ||
private |
_isValidValue(t: number, tol: number): void Check if a value is valid on this type of grid. |
Public Constructors
public constructor(field_size: GridSize, torus: boolean[]) source
The grid constructor.
Params:
Name | Type | Attribute | Description |
field_size | GridSize | array of field size in each dimension. E.g. [100,200] for a grid that is 100 pixels wide and 200 pixels high. Entries must be positive integer numbers. |
|
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. |
Public Members
public datatype: * source
public extents: GridSize source
field_size array of field size in each dimension. E.g. [100,200] for a grid that is 100 pixels wide and 200 pixels high. Entries must be positive integer numbers.
public torus: boolean[] source
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. If torus is set to false, artifacts arise because cells at the border have fewer neighbors. Cells will then stick to the grid borders where they have fewer neighbors to get adhesion and/or perimeter penalties from. You will need to specify how to handle the borders explicitly; see the examples/ folder for details on how to do this. Torus can be specified for each dimension separately.
Private Members
private _pixelArray: * 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._pixels[i]. This should be implemented in the grid subclass; see e.g. Grid2D#_pixelArray. Note that this array is accessed indirectly via the _pixels set- and get methods.
private get _pixels: Uint16Array | Float32Array: * source
Return the array this._pixelArray, which should be set in the grid subclass.
private set _pixels(pixels: Uint16Array | Float32Array) source
Set or update the private this._pixelArray.
private _pixelsbuffer: Uint16Array | Float32Array source
For storing a copy of all pixel values; eg for synchronous updating of some sort.
Public Methods
public applyLocally(f: updatePixelFunction) source
Apply a function to all pixel values on the grid. It acts on this._pixels, which is implemented in the grid subclass.
Params:
Name | Type | Attribute | Description |
f | updatePixelFunction | the function to apply to each pixel. |
public checkOnGrid(p: ArrayCoordinate) source
Method to check if a given ArrayCoordinate falls within the bounds of this grid. Returns an error if this is not the case.
Params:
Name | Type | Attribute | Description |
p | ArrayCoordinate | the coordinate to check. |
public correctPosition(p: ArrayCoordinate): ArrayCoordinate source
Method to correct an ArrayCoordinate outside the grid dimensions when the grid is wrapped (torus = true). If the coordinate falls inside the grid, it is returned unchanged. If it falls outside the grid and the grid is periodic in that dimension, a corrected coordinate is returned. If the pixel falls outside the grid which is not periodic in that dimension, the function returns 'undefined'.
Params:
Name | Type | Attribute | Description |
p | ArrayCoordinate | the coordinate of the pixel to correct |
public diffusion(D: number) source
Perform a diffusion step on the grid, updating the values of all pixels according to Fick's second law of diffusion.
Params:
Name | Type | Attribute | Description |
D | number | diffusion coefficient |
public gradient(p: ArrayCoordinate): number source
Method to compute the gradient at location p on the grid (location given in array coordinates). It internally calls the gradienti method using index coordinates, which should be implemented in the grid subclass.
Params:
Name | Type | Attribute | Description |
p | ArrayCoordinate | array coordinates of a pixel p to compute the gradient at |
Test:
public gradienti(i: IndexCoordinate): number[] source
Template method to compute the gradient at location i on the grid (location given in index coordinates). This method throws an error, which is overwritten when a subclass implements a gradienti method.
Params:
Name | Type | Attribute | Description |
i | IndexCoordinate | index coordinate of a pixel to compute the gradient at. |
public abstract i2p(i: IndexCoordinate): ArrayCoordinate source
Method for conversion from an IndexCoordinate to an ArrayCoordinate. This method should be implemented in the subclass, see Grid2D#i2p for an example.
Params:
Name | Type | Attribute | Description |
i | IndexCoordinate | the coordinate of the pixel to convert |
public laplacian(p: ArrayCoordinate): number source
Method to compute the laplacian at location p on the grid (location given in array coordinates). It internally calls the laplaciani method that does the same but uses index coordinates.
Params:
Name | Type | Attribute | Description |
p | ArrayCoordinate | array coordinates of a pixel p to compute the laplacian at |
Test:
public laplaciani(i: IndexCoordinate): number source
Method to compute the laplacian at location i on the grid (location given in index coordinates). It internally uses the neighNeumanni method to compute a Neumann neighborhood, which should be implemented in the grid subclass. It then uses the finite difference method (see link) with h = 1.
Params:
Name | Type | Attribute | Description |
i | IndexCoordinate | index coordinates of a pixel to compute the laplacian at |
Test:
- Grid [ unit tests ] laplacian(i) method
- Grid [ class extension ] laplaciani should throw error when neighNeumanni not implemented
- Grid [ class extension ] laplacian should throw error when p2i/neighNeumanni not implemented
- Grid [ class extension ] laplacian(i) should work if p2i, neighNeumanni, and _pixelArray exist
public multiplyBy(r: number) source
Multiply all pixel values on the grid with a constant factor r. This method acts on this._pixels, which is implemented in the grid subclass.
Params:
Name | Type | Attribute | Description |
r | number | the multiplication factor. |
public neigh(p: ArrayCoordinate, torus: boolean[]): ArrayCoordinate[] source
The neigh method returns the neighborhood of a pixel p. This function uses array coordinates as input and output arguments, but internally calls a method 'neighi' which computes neighborhoods using index- coordinates. Since neighborhoods depend on the coordinate system, the 'neighi' method is defined in the extension class for that specific coordinate system.
Params:
Name | Type | Attribute | Description |
p | ArrayCoordinate | array coordinates of a specific pixel |
|
torus | boolean[] | are borders of the grid linked so that a cell leaving on the right re-enters the grid on the left? |
Return:
ArrayCoordinate[] | an array of neighbors of pixel p, where each element contains the array coordinates of the neighbor in question. |
public abstract * neighNeumanni(i: IndexCoordinate, torus: boolean[]): IndexCoordinate[] source
A method to compute the Neumann neighborhood should be implemented in the Grid subclass if the laplacian (see below) is used. This mock function ensures that an error is thrown when there is no method called neighNeumanni in the grid subclass.
Params:
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 |
public abstract neighi(i: IndexCoordinate, torus: boolean[]): IndexCoordinate[] source
Method returning the (Moore) neighborhood of a pixel based on its IndexCoordinate. This method should be implemented in the subclass, see Grid2D#neighi for an example.
Params:
Name | Type | Attribute | Description |
i | IndexCoordinate | the coordinate of the pixel to get neighbors for. |
|
torus | boolean[] | are borders of the grid linked so that a cell leaving on the right re-enters the grid on the left? |
public abstract p2i(p: ArrayCoordinate): IndexCoordinate source
Method for conversion from an ArrayCoordinate to an IndexCoordinate. This method should be implemented in the subclass, see Grid2D#p2i for an example.
Params:
Name | Type | Attribute | Description |
p | ArrayCoordinate | the coordinate of the pixel to convert |
public abstract * pixels(): Pixel source
This iterator returns locations and values of all non-zero pixels. This method isn't actually called because the subclasses implement it themselves due to efficiency reasons. It serves as a template to document the functionality.
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. |
public pixelsBuffer(): Uint16Array | Float32Array source
This method pre-allocates an array of the correct datatype to make a copy of the current pixel values. Values are not actually copied yet.
Return:
Uint16Array | Float32Array | an array with an element for each pixel. The datatype is determined by the datatype of this._pixels (implemented in the subclass), which can be either Uint16Array or Float32Array. |
public abstract * pixelsi(): IndexCoordinate source
This iterator returns locations all pixels including background. This method isn't actually called because the subclasses implement it themselves due to efficiency reasons. It serves as a template to document the functionality.
Return:
IndexCoordinate | for each pixel, because this method should be implemented in a grid subclass. |
public pixt(p: ArrayCoordinate): number source
The pixt method finds the current value of a pixel p on the grid.
Params:
Name | Type | Attribute | Description |
p | ArrayCoordinate | array coordinates of the pixel to find the value of |
Return:
number | t the value of p on the grid. This can be integers or floating point numbers, depending on the grid subclass used (see eg Grid2D). |
public pixti(i: IndexCoordinate): number source
The pixti method finds the current value of a pixel i on the grid.
Params:
Name | Type | Attribute | Description |
i | IndexCoordinate | index coordinates of the pixel to find the value of |
Return:
number | t the value of i on the grid. This can be integers or floating point numbers, depending on the grid subclass used (see eg Grid2D). |
public setpix(p: ArrayCoordinate, t: number) source
The setpix method changes the value of a pixel p on the grid to t.
Params:
Name | Type | Attribute | Description |
p | ArrayCoordinate | array coordinates of the pixel to change the value of |
|
t | number | the value to assign to this pixel. This can be integers or floating point numbers, depending on the grid subclass used (see eg Grid2D). |
public setpixi(i: IndexCoordinate, t: number) source
The setpixi method changes the value of a pixel i on the grid to t.
Params:
Name | Type | Attribute | Description |
i | IndexCoordinate | index coordinates of the pixel to change the value of |
|
t | number | the value to assign to this pixel. This can be integers or floating point numbers, depending on the grid subclass used (see eg Grid2D). |
Private Methods
private _isValidValue(t: number, tol: number): void source
Check if a value is valid on this type of grid. This function forbids trying to set forbidden (negative/float) values on an integer grid, which could cause bugs if the setpix(i) methods try to set such a value unnoticed.
Params:
Name | Type | Attribute | Description |
t | number | the value that would be stored on the grid. |
|
tol | number |
|
numeric tolerance for comparing a number with its rounded version, to check if it is integer (e.g. setting a value 1.5 on an integer grid would throw an error, but setting 1.000000000001 would not if the tolerance is 1e-6. |
Return:
void | return without problem or throw an error when an incorrect value is set. |