CGVIS Utils
Loading...
Searching...
No Matches
Grid2D Class Reference

#include <Grid2D.h>

Public Member Functions

 Grid2D (size_t width, size_t height)
 Construct an empty grid initialized to zeros.
 Grid2D (size_t width, size_t height, const std::vector< float > data)
 Construct from raw data (size must equal width*height).
 Grid2D (const Grid2D &other)
 Copy‑construct from another grid.
 Grid2D (const Image &image)
 Construct from the luminance of an Image.
 Grid2D (std::istream &is)
 Read grid from a binary stream written by save().
void save (std::ostream &os) const
 Write grid to a binary stream.
std::string toString () const
 String dump of values in row‑major order.
std::vector< uint8_t > toByteArray () const
 Convert to an 8‑bit RGB byte array (grayscale replicated to RGB).
Grid2D toSignedDistance (float threshold) const
 Compute a signed distance field from a binary threshold.
GLTexture2D toTexture () const
 Upload to an OpenGL texture (1 channel).
void setValue (size_t x, size_t y, float value)
 Set one value.
float getValueNormalized (float x, float y) const
 Fetch value using normalized [0,1] coordinates (nearest‑neighbor via index mapping).
float getValue (size_t x, size_t y) const
 Fetch value by integer coordinates.
float sample (float x, float y) const
 Bilinear sample at normalized coordinates in [0,1].
float sample (const Vec2 &pos) const
 Bilinear sample at normalized coordinates in [0,1].
Vec3 normal (float x, float y) const
 Estimate a normal from local height differences at normalized coords.
Vec3 normal (const Vec2 &pos) const
 Estimate a normal from local height differences at normalized coords.
Grid2D operator* (const float &value) const
 Per‑element multiply by scalar.
Grid2D operator/ (const float &value) const
 Per‑element divide by scalar.
Grid2D operator+ (const float &value) const
 Per‑element add scalar.
Grid2D operator- (const float &value) const
 Per‑element subtract scalar.
Grid2D operator+ (const Grid2D &other) const
 Per‑element addition with resampling if needed.
Grid2D operator/ (const Grid2D &other) const
 Per‑element division with resampling if needed.
Grid2D operator* (const Grid2D &other) const
 Per‑element multiplication with resampling if needed.
Grid2D operator- (const Grid2D &other) const
 Per‑element subtraction with resampling if needed.
void normalize (const float maxVal=1)
 Normalize values to [0,maxVal] by affine remapping.
Vec2t< size_t > maxValue () const
 Location of the maximum value.
Vec2t< size_t > minValue () const
 Location of the minimum value.
void fill (float value)
 Fill all elements with a constant.
Geometry
size_t getWidth () const
 Grid width in elements.
size_t getHeight () const
 Grid height in elements.

Static Public Member Functions

static Grid2D genRandom (size_t x, size_t y, uint32_t seed)
 Generate a grid of random values in [0,1) using a fixed seed.
static Grid2D genRandom (size_t x, size_t y)
 Generate a grid of random values in [0,1) using a global RNG.
static Grid2D fromBMP (const std::string &filename)
 Create a grid from the luminance of a BMP file.

Friends

std::ostream & operator<< (std::ostream &os, const Grid2D &v)
 Stream insertion (delegates to toString()).

Constructor & Destructor Documentation

◆ Grid2D() [1/5]

Grid2D::Grid2D ( size_t width,
size_t height )

Construct an empty grid initialized to zeros.

Parameters
widthNumber of columns.
heightNumber of rows.
Here is the caller graph for this function:

◆ Grid2D() [2/5]

Grid2D::Grid2D ( size_t width,
size_t height,
const std::vector< float > data )

Construct from raw data (size must equal width*height).

Parameters
widthNumber of columns.
heightNumber of rows.
dataRow‑major values copied into the grid.
Exceptions
std::runtime_errorIf data.size() != width*height.

◆ Grid2D() [3/5]

Grid2D::Grid2D ( const Grid2D & other)

Copy‑construct from another grid.

Parameters
otherSource grid.
Here is the call graph for this function:

◆ Grid2D() [4/5]

Grid2D::Grid2D ( const Image & image)

Construct from the luminance of an Image.

Parameters
imageSource image; the first channel is sampled and normalized to [0,1]. Width/height match the image.

◆ Grid2D() [5/5]

Grid2D::Grid2D ( std::istream & is)

Read grid from a binary stream written by save().

Parameters
isInput stream; reads width, height, then width*height floats.

Member Function Documentation

◆ fill()

void Grid2D::fill ( float value)

Fill all elements with a constant.

Parameters
valueValue to assign.

◆ fromBMP()

Grid2D Grid2D::fromBMP ( const std::string & filename)
static

Create a grid from the luminance of a BMP file.

Parameters
filenamePath to the BMP image.
Returns
Grid with width/height of the image; values in [0,1].
Here is the call graph for this function:

◆ genRandom() [1/2]

Grid2D Grid2D::genRandom ( size_t x,
size_t y )
static

Generate a grid of random values in [0,1) using a global RNG.

Parameters
xWidth.
yHeight.
Here is the call graph for this function:

◆ genRandom() [2/2]

Grid2D Grid2D::genRandom ( size_t x,
size_t y,
uint32_t seed )
static

Generate a grid of random values in [0,1) using a fixed seed.

Parameters
xWidth.
yHeight.
seedSeed for the RNG.
Here is the call graph for this function:

◆ getHeight()

size_t Grid2D::getHeight ( ) const

Grid height in elements.

◆ getValue()

float Grid2D::getValue ( size_t x,
size_t y ) const

Fetch value by integer coordinates.

Parameters
xColumn index.
yRow index.
Returns
Stored value.
Here is the caller graph for this function:

◆ getValueNormalized()

float Grid2D::getValueNormalized ( float x,
float y ) const

Fetch value using normalized [0,1] coordinates (nearest‑neighbor via index mapping).

Parameters
xNormalized x.
yNormalized y.
Returns
Value at floor(x*(width−1)), floor(y*(height−1)).

◆ getWidth()

size_t Grid2D::getWidth ( ) const

Grid width in elements.

◆ maxValue()

Vec2t< size_t > Grid2D::maxValue ( ) const

Location of the maximum value.

Returns
Integer coordinates (x,y) of the first maximum encountered.
Here is the caller graph for this function:

◆ minValue()

Vec2t< size_t > Grid2D::minValue ( ) const

Location of the minimum value.

Returns
Integer coordinates (x,y) of the first minimum encountered.
Here is the caller graph for this function:

◆ normal() [1/2]

Vec3 Grid2D::normal ( const Vec2 & pos) const

Estimate a normal from local height differences at normalized coords.

Parameters
pos(x,y) normalized position.
Here is the call graph for this function:

◆ normal() [2/2]

Vec3 Grid2D::normal ( float x,
float y ) const

Estimate a normal from local height differences at normalized coords.

Parameters
x,yNormalized coordinates.
Returns
Unit normal approximating ∇height.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ normalize()

void Grid2D::normalize ( const float maxVal = 1)

Normalize values to [0,maxVal] by affine remapping.

Parameters
maxValUpper bound after normalization (default 1).
Here is the call graph for this function:

◆ operator*() [1/2]

Grid2D Grid2D::operator* ( const float & value) const

Per‑element multiply by scalar.

Here is the call graph for this function:

◆ operator*() [2/2]

Grid2D Grid2D::operator* ( const Grid2D & other) const

Per‑element multiplication with resampling if needed.

Here is the call graph for this function:

◆ operator+() [1/2]

Grid2D Grid2D::operator+ ( const float & value) const

Per‑element add scalar.

Here is the call graph for this function:

◆ operator+() [2/2]

Grid2D Grid2D::operator+ ( const Grid2D & other) const

Per‑element addition with resampling if needed.

Here is the call graph for this function:

◆ operator-() [1/2]

Grid2D Grid2D::operator- ( const float & value) const

Per‑element subtract scalar.

Here is the call graph for this function:

◆ operator-() [2/2]

Grid2D Grid2D::operator- ( const Grid2D & other) const

Per‑element subtraction with resampling if needed.

Here is the call graph for this function:

◆ operator/() [1/2]

Grid2D Grid2D::operator/ ( const float & value) const

Per‑element divide by scalar.

Here is the call graph for this function:

◆ operator/() [2/2]

Grid2D Grid2D::operator/ ( const Grid2D & other) const

Per‑element division with resampling if needed.

Here is the call graph for this function:

◆ sample() [1/2]

float Grid2D::sample ( const Vec2 & pos) const

Bilinear sample at normalized coordinates in [0,1].

Parameters
pos(x,y) normalized position.
Here is the call graph for this function:

◆ sample() [2/2]

float Grid2D::sample ( float x,
float y ) const

Bilinear sample at normalized coordinates in [0,1].

Parameters
xNormalized x.
yNormalized y.
Returns
Interpolated value.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ save()

void Grid2D::save ( std::ostream & os) const

Write grid to a binary stream.

Parameters
osOutput stream; writes width, height, then width*height floats.

◆ setValue()

void Grid2D::setValue ( size_t x,
size_t y,
float value )

Set one value.

Parameters
xColumn index in [0,width).
yRow index in [0,height).
valueNew value.

◆ toByteArray()

std::vector< uint8_t > Grid2D::toByteArray ( ) const

Convert to an 8‑bit RGB byte array (grayscale replicated to RGB).

Returns
Vector of size width*height*3 with bytes in 0..255.

◆ toSignedDistance()

Grid2D Grid2D::toSignedDistance ( float threshold) const

Compute a signed distance field from a binary threshold.

Parameters
thresholdValues ≥ threshold are considered inside.
Returns
Grid whose values are distances to the nearest boundary, positive inside and negative outside.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ toString()

std::string Grid2D::toString ( ) const

String dump of values in row‑major order.

Returns
CSV‑like multiline representation.
Here is the caller graph for this function:

◆ toTexture()

GLTexture2D Grid2D::toTexture ( ) const

Upload to an OpenGL texture (1 channel).

Returns
GLTexture2D with dimensions width×height.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const Grid2D & v )
friend

Stream insertion (delegates to toString()).


The documentation for this class was generated from the following files: