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

Grid3D

Extends:

Grid → Grid3D

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

Example:

Neighborhoods on a 3D grid

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

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

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

// Or try a torus in one dimension
let grid2 = new CPM.Grid3D( [100,100,100], [true,false,false] )
console.log( grid2.neigh( [0,0,0] ) )

Test:

Constructor Summary

Public Constructor
public

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

Constructor of the Grid3D 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 for conversion from an IndexCoordinate to an ArrayCoordinate.

public

Return array of IndexCoordinate of the Moore 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 non-zero 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[]) source

Constructor of the Grid3D object.

Override:

Grid#constructor

Params:

NameTypeAttributeDescription
extents GridSize

the size of the grid in each dimension

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

should the borders of the grid be linked, so that a cell moving out on the left reappears on the right? Warning: setting the torus to false can give artifacts if not done properly, see Grid#torus.

Public Members

public datatype: string source

Override:

Grid#datatype

Private Members

private _pixelArray: Uint16Array 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 i2p(i: IndexCoordinate): ArrayCoordinate source

Method for conversion from an IndexCoordinate to an ArrayCoordinate.

See also Grid3D#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.Grid3D( [100,100,100], [true,true,true] )
let p = grid.i2p( 5 )
console.log( p )
console.log( grid.p2i( p ))

Test:

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

Return array of IndexCoordinate of the Moore neighbor pixels of the pixel at coordinate i. This function takes the 3D equivalent of 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
  • default: [true,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 p2i(p: ArrayCoordinate): IndexCoordinate source

Method for conversion from an ArrayCoordinate to an IndexCoordinate.

See also Grid3D#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.Grid3D( [100,100,100], [true,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.Grid3D( [100,100,100], [true,true,true] )
grid.setpix( [0,0,0], 1 )
grid.setpix( [0,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 non-zero 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, return its IndexCoordinate.

Example:

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

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

Test: