CGPdocs

grid_2D

2D grid container (dynamic size).

module 04_grid_containersource: grid_2D.hpp
Element (i, j) of a grid_2D is stored at offset i + dimension.x * j in data: i varies fastest and each row j is one contiguous block.
Element (i, j) of a grid_2D is stored at offset i + dimension.x * j in data: i varies fastest and each row j is one contiguous block. Tap to enlarge.

Fields and methods

Members of the struct grid_2D. Most of what you do with a CGP type goes through the free functions listed below.

Fields

int2 dimensionNumber of elements (Nx, Ny) along each of the two directions.
numarray<T> dataElements stored contiguously as a 1D numarray, the element (k1,k2) at offset k1 + Nx*k2.

Methods

grid_2D()Builds an empty grid of dimension (0,0).4 overloads src
grid_2D(int size)Builds a square grid of dimension (size, size) with value-initialized elements; asserts size>0, so unlike the other constructors an empty grid is rejected. src
grid_2D(int2 const& size)Builds a grid of dimension (size.x, size.y) with value-initialized elements (sizes asserted non-negative). src
grid_2D(int size_1, int size_2)Builds a grid of dimension (size_1, size_2) with value-initialized elements (sizes asserted non-negative). src
Builds a grid of dimension (size_1, size_2) holding a copy of a 1D buffer, whose size must be size_1*size_2 (asserted). src
Resizes the grid to the dimension (0,0), which removes all its elements. src
Total number of elements, Nx*Ny. src
Sets every element of the grid to the given value, keeping its dimension. src
void resize(int size)Resizes the grid to the square dimension (size, size); values are kept by 1D offset.3 overloads src
void resize(int2 const& size)Sets the dimension to size and resizes the 1D buffer; existing values are kept by 1D offset, not by (k1, k2), and new elements are value-initialized. src
void resize(int size_1, int size_2)Sets the dimension to (size_1, size_2) and resizes the 1D buffer; existing values are kept by 1D offset, not by (k1, k2), and new elements are value-initialized. src
Returns a const reference to the element of index (x, y), stored at offset x + Nx*y, with bound checking unless CGP_NO_DEBUG is defined. src
T const& operator()(int2 const& index) const
T& operator()(int2 const& index)
Returns a const reference to the element of index (x, y), stored at offset x + Nx*y, with bound checking unless CGP_NO_DEBUG is defined.2 overloads src
T const& operator()(int k1, int k2) const
T& operator()(int k1, int k2)
Returns a const reference to the element of index (k1, k2), stored at offset k1 + Nx*k2, with bound checking unless CGP_NO_DEBUG is defined. src
Returns the 1D offset k1 + Nx*k2 of the index (k1, k2), without bound checking. src
Returns the 2D index (k1, k2) of a 1D offset, inverse of index_to_offset (no bound checking). src
Returns a const iterator to the first element of the 1D buffer, in which the first index varies fastest. src
Returns a const iterator one past the last element of the 1D buffer. src
Returns a const iterator to the first element of the 1D buffer, in which the first index varies fastest. src
Returns a const iterator one past the last element of the 1D buffer. src
Returns a const reference to the element at the given 1D offset, without bound checking. src

Functions taking a grid_2D

Compute 1

template <typename T>
T interpolation_bilinear(grid_2D<T> const& value, float x, float y)

Bilinear interpolation of a grid_2D at continuous index coordinates (x,y), each in [0, dimension-1).

Convert 3

str

genericsource
template <typename T>
std::string str(
    grid_2D<T> const& v,
    std::string const& separator = " ",
    std::string const& begin = "",
    std::string const& end = ""
)

Text representation of all elements in storage order, joined by separator and surrounded by begin and end.

template <typename T>
std::string type_str(grid_2D<T> const&)

Type name as a string, e.g. "grid_2D<float>".

Documented on another page

  • convertConvert an RGB or RGBA image into a grid_2D of vec3 colors in [0,1], dropping the alpha channel.on image_structure

I/O 1

GLuint opengl_load_texture_image(
    grid_2D<vec3> const& im,
    GLint wrap_s = GL_CLAMP_TO_EDGE,
    GLint wrap_t = GL_CLAMP_TO_EDGE
)

Upload a grid of vec3 colors as a new mipmapped 2D OpenGL texture (32-bit float RGB) with the given wrap modes, returning its id.

Access 2

template <typename T>
void check_index_bounds(int index1, int index2, grid_2D<T> const& data)

Raises an error if (x,y) is outside the grid dimensions; no-op when CGP_NO_DEBUG is defined.

template <typename T1, typename T2>
bool is_equal(grid_2D<T1> const& a, grid_2D<T2> const& b)

Returns true if both grids have the same dimension and equal elements (approximate comparison for floating-point values).

Other 1

void opengl_update_texture_image(GLuint texture_id, grid_2D<vec3> const& im)

Overwrite the content of an existing 2D texture with a grid of vec3 colors of the same size and regenerate its mipmaps.

Functions returning a grid_2D

None.

Operators

Show the 20 operators (+, -, *, ==, ...)

operator*

generic3 overloads
template <typename T>
grid_2D<T> operator*(grid_2D<T> const& a, grid_2D<T> const& b)
source

Element-wise product of two grids; dimensions must match (asserted in debug).

template <typename T>
grid_2D<T> operator*(grid_2D<T> const& a, float b)
source

Grid with each element multiplied by a scalar.

template <typename T>
grid_2D<T> operator*(float a, grid_2D<T> const& b)
source

Grid with each element multiplied by a scalar.

operator*=

generic2 overloads
template <typename T>
grid_2D<T>& operator*=(grid_2D<T>& a, grid_2D<T> const& b)
source

Multiplies by a grid element-wise in place; dimensions must match (asserted in debug).

template <typename T>
grid_2D<T>& operator*=(grid_2D<T>& a, float b)
source

Multiplies each element by a scalar in place.

operator+

generic3 overloads
template <typename T>
grid_2D<T> operator+(grid_2D<T> const& a, grid_2D<T> const& b)
source

Element-wise sum of two grids; dimensions must match (asserted in debug).

template <typename T>
grid_2D<T> operator+(grid_2D<T> const& a, T const& b)
source

Grid with a value added to each element.

template <typename T>
grid_2D<T> operator+(T const& a, grid_2D<T> const& b)
source

Grid with a value added to each element.

operator+=

generic2 overloads
template <typename T>
grid_2D<T>& operator+=(grid_2D<T>& a, grid_2D<T> const& b)
source

Adds a grid element-wise in place; dimensions must match (asserted in debug).

template <typename T>
grid_2D<T>& operator+=(grid_2D<T>& a, T const& b)
source

Adds a value to each element in place.

operator-

generic3 overloads
template <typename T>
grid_2D<T> operator-(grid_2D<T> const& a, grid_2D<T> const& b)
source

Element-wise difference of two grids; dimensions must match (asserted in debug).

template <typename T>
grid_2D<T> operator-(grid_2D<T> const& a, T const& b)
source

Grid with a value subtracted from each element.

template <typename T>
grid_2D<T> operator-(T const& a, grid_2D<T> const& b)
source

Grid of a value minus each element.

operator-=

generic2 overloads
template <typename T>
grid_2D<T>& operator-=(grid_2D<T>& a, grid_2D<T> const& b)
source

Subtracts a grid element-wise in place; dimensions must match (asserted in debug).

template <typename T>
grid_2D<T>& operator-=(grid_2D<T>& a, T const& b)
source

Subtracts a value from each element in place.

operator/

generic2 overloads
template <typename T>
grid_2D<T> operator/(grid_2D<T> const& a, grid_2D<T> const& b)
source

Element-wise quotient of two grids; dimensions must match (asserted in debug).

template <typename T>
grid_2D<T> operator/(grid_2D<T> const& a, float b)
source

Grid with each element divided by a scalar.

operator/=

generic2 overloads
template <typename T>
grid_2D<T>& operator/=(grid_2D<T>& a, grid_2D<T> const& b)
source

Divides by a grid element-wise in place; dimensions must match (asserted in debug).

template <typename T>
grid_2D<T>& operator/=(grid_2D<T>& a, float b)
source

Divides each element by a scalar in place.

template <typename T>
std::ostream& operator<<(std::ostream& s, grid_2D<T> const& v)

Writes all elements of the grid to the stream, in storage order separated by spaces.

CGP - Computer Graphics Programming.