import Grid from 'Artistoo/src/grid/Grid.js'
public class

# Grid

#### Indirect Subclass:

CoarseGrid

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.

## 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 torus: boolean[] 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 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 diffusion(D: number) 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[]): The neigh method returns the neighborhood of a pixel p. public abstract * neighNeumanni(i: IndexCoordinate, torus: boolean[]): 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[]): 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 pixt The pixt method finds the current value of a pixel p on the grid. public pixti 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

### publicconstructor(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[] 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.

## Public Members

### publicextents: 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.

### publicmidpoint: ArrayCoordinate source

Array coordinates to the middle pixel on the grid.

### publicndim: number source

Number of dimensions of the grid.

### publictorus: 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.

### privateget_pixels: Uint16Array | Float32Array: * source

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

### privateset_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

### publicapplyLocally(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.

### publiccheckOnGrid(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.

### publiccorrectPosition(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

#### Return:

 ArrayCoordinate the corrected coordinate.

### publicdiffusion(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

#### Test:

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

#### Return:

 number the gradient at position p.

#### See:

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.

### publicabstracti2p(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

#### Return:

 ArrayCoordinate the converted coordinate.

### publiclaplacian(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

#### Return:

 number the laplacian at position p.

### publiclaplaciani(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

#### Return:

 number the laplacian at position p.

### publicmultiplyBy(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.

### publicneigh(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:

 an array of neighbors of pixel p, where each element contains the array coordinates of the neighbor in question.

### publicabstract*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[] optional default: [true,true] does the grid have linked borders? Defaults to the setting on this grid, see torus

#### Return:

 an array of coordinates for all the neighbors of i.

### publicabstractneighi(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?

#### Return:

 an array of neighbors.

### publicabstractp2i(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

#### Return:

 IndexCoordinate the converted coordinate.

### publicabstract*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.

### publicpixelsBuffer(): 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.

### publicabstract*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.

### publicpixt(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).

### publicpixti(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).

### publicsetpix(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).

### publicsetpixi(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 optional default: 1e-6 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.