npm package: @gov.nasa.jpl.honeycomb/sampler-2d
Description: Utility for sampling 2D float arrays in row major or column major order.
Latest version: 0.0.6

# Sampler 2D

Utility for sampling 2D float arrays in row major or column major order.

# Use

```js
import { Sampler2D } from '@gov.nasa.jpl.honeycomb/sampler-2d';

const data = new Float32Array( [
    0, 0, 1, 0,
    0, 1, 1, 1
] );

const sampler = new Sampler2D( data, 2, 2 );
const result = new Array();
sampler.sample( 0.5, 0.5, result ); // 0.5, 1
sampler.samplePixel( 0, 1, result ); // 0, 1
```

# API

<!-- START_AUTOGENERATED_DOCS -->
## Sampler2D

Texture-like object with an API for sampling image data.

For simplicity image data should be thought about as `0 0` being the bottom left pixel
of the image. Use `xInvert` or `yInvert` to flip the image if necessary. Data is assumed to
be interleaved.

### channels<a name="Sampler2D#channels"></a>

```js
channels : Number
```


Return the number of channels in the image.


### data<a name="Sampler2D#data"></a>

```js
data : TypedArray
```


The image data.


### stride<a name="Sampler2D#stride"></a>

```js
stride : Number = 4
```


The stride of a single pixel vector in the array.


### width<a name="Sampler2D#width"></a>

```js
width : Number
```


The width of the data grid.


### height<a name="Sampler2D#height"></a>

```js
height : Number
```


The height of the data grid as derived from the data length, stride, and width.


### rowMajor<a name="Sampler2D#rowMajor"></a>

```js
rowMajor : Number = true
```


Whether the data should be sampled as row major (`true`) or column major (`false`).


### invertX<a name="Sampler2D#invertX"></a>

```js
invertX : Number = false
```


Whether the data should be inverted along X.


### invertY<a name="Sampler2D#invertY"></a>

```js
invertY : Number = false
```


Whether the data should be inverted along Y.


### interpolate<a name="Sampler2D#interpolate"></a>

```js
interpolate : Number = true
```


If `true`, then the result of sampling is bilinearly interpolated between the adjacent pixels.


### constructor

```js
constructor( data : TypedArray, width : Number, stride : Number = 4 ) : void
```

### samplePixelChannel<a name="Sampler2D#samplePixelChannel"></a>

```js
samplePixelChannel( x : Number, y : Number, channel : Number ) : Number | null
```

X is expected to be in the range `[ 0, width ]` and Y within `[ 0, height ]`. Sets `target` to the pixel value at
the given pixel. Decimal values are not supported.

Returns `null` if the provided values are outside a valid range. Returns the sample value otherwise.

### samplePixel<a name="Sampler2D#samplePixel"></a>

```js
samplePixel( x : Number, y : Number, target : Array ) : Boolean
```

X is expected to be in the range `[ 0, width ]` and Y within `[ 0, height ]`. Sets `target` to the pixel value at
the given pixel. Decimal values are not supported.

Returns `false` if the provided values are outside a valid range. Returns `true` otherwise.

### sampleChannel<a name="Sampler2D#sampleChannel"></a>

```js
sampleChannel( u : Number, v : Number, channel : Number ) : Number | null
```

U  and V are expected to be in the range `[ 0, 1 ]`. Reads and interpolates the pixels if
[interpolate](#Sampler2D#interpolate) is true.

Returns `null` if the provided values are outside a valid range. Returns the sample value otherwise.

### sample<a name="Sampler2D#sample"></a>

```js
sample( u : Number, v : Number, target : Array ) : Boolean
```

U  and V are expected to be in the range `[ 0, 1 ]`. Reads and interpolates the pixels if
[interpolate](#Sampler2D#interpolate) is true.

Returns `false` if the provided values are outside a valid range. Returns `true` otherwise.

### generateMipMap<a name="Sampler2D#generateMipMap"></a>

```js
generateMipMap( target : Sampler2D = null ) : void
```

Creates a mip map of this Sampler2D. If the dimensions of this sampler are not
a factor of two then weighted sampling over multiple pixels are used and a
square mipmap is generated.

## SpatialSampler2D

A sampler that contains members and functions for sampling with a 2D transform
into the samplers texture space.

_extends Sampler2D_

### inverseMatrix<a name="SpatialSampler2D#inverseMatrix"></a>

```js
inverseMatrix : Array<Number> = identity matrix
```


A 3x3 matrix that transforms from the given coordinate frame into the local
[ 0, 1 ] texture space of the sampler for use in the spatial sample functions.


### constructor

```js
constructor( args : ...rest ) : void
```

### spatialSampleChannel<a name="SpatialSampler2D#spatialSampleChannel"></a>

```js
spatialSampleChannel( x : Number, y : Number, channel : Number ) : Number | null
```

Transforms X and Y by [inverseMatrix](#SpatialSampler2D#inverseMatrix) before
sampling the texture using [@Sampler2D#sampleChannel](@Sampler2D#sampleChannel) and returning the result.

### spatialSample<a name="SpatialSampler2D#spatialSample"></a>

```js
spatialSample( x : Number, y : Number, target : Array ) : Boolean
```

Transforms X and Y by [inverseMatrix](#SpatialSampler2D#inverseMatrix) before
sampling the texture using [@Sampler2D#sample](@Sampler2D#sample) and returning the result.

### setMinMax<a name="SpatialSampler2D#setMinMax"></a>

```js
setMinMax( minX : Number, minY : Number, maxX : Number, maxY : Number ) : void
```

Sets `inverseMatrix` to transform from a span of min to max x and y into
the span [ 0, 1 ].

## BandSampler2D

A sampler that stores each channel as a separate sampler and samples
data accordingly. Use this if image data is not provided as interleaved.

_implements BandSampler2D_

### rowMajor<a name="BandSampler2D#rowMajor"></a>

```js
rowMajor : null
```



### invertX<a name="BandSampler2D#invertX"></a>

```js
invertX : null
```



### invertY<a name="BandSampler2D#invertY"></a>

```js
invertY : null
```



### bands<a name="BandSampler2D#bands"></a>

```js
bands : Array<Sampler2D> = []
```


Array of sampler 2d instances where each band corresponds to a different channel.


### constructor

```js
constructor( bands : Array<Sampler2D> = [] ) : void
```


<!-- END_AUTOGENERATED_DOCS -->