CGPdocs

numarray

Dynamic-size array with numerical operations.

module 02_numarraysource: numarray.hpp
A numarray stores its elements contiguously in data; push_back appends an element, and +, *= and sum work element by element on arrays of the same size.
A numarray stores its elements contiguously in data; push_back appends an element, and +, *= and sum work element by element on arrays of the same size. Tap to enlarge.

Fields and methods

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

Fields

std::vector<T> datastd::vector holding the elements, which can be used directly (e.g. data.data() for a raw pointer).

Methods

numarray()Builds an empty array.4 overloads src
numarray(int size)Builds an array of the given size with value-initialized elements (zero for numbers); not explicit, so an int converts implicitly to a numarray. src
numarray(std::initializer_list<T> arg)Builds an array from a brace list of values. src
numarray(std::vector<T> const& arg)Builds an array by copying a std::vector. src
Returns N evenly spaced values from p1 to p2 included, like linspace in MATLAB (N must be at least 2). src
Number of elements of the array. src
Resizes the array (size asserted non-negative), keeping the first values and value-initializing the new ones, and returns the array. src
Clears the array then resizes it, so that all its elements are value-initialized (zero for numbers), and returns the array. src
numarray<T>& push_back(T const& value)Appends one element at the end and returns the array.2 overloads src
numarray<T>& push_back(numarray<T> const& value)Appends all elements of another array at the end and returns the array. src
Removes all elements, so that the size becomes 0, and returns the array. src
Sets every element to the given value, keeping the size, and returns the array. src
Returns the element at index, bound-checked unless CGP_NO_DEBUG is defined (at() and coeff() do not check). src
Returns a const reference to the element at the given index, with bound checking unless CGP_NO_DEBUG is defined. src
Returns a const reference to the element at index, without bound checking. src
Returns a const iterator to the first element. src
Returns a const iterator one past the last element. src
Returns a const iterator to the first element. src
Returns a const iterator one past the last element. src
Returns a const reference to the element at index, without bound checking (deprecated, use at). src

Functions taking a numarray

Transform 10

add

generic2 overloads
template <typename T>
numarray<T> add(numarray<T> const& a, T const& b)
source
template <typename T>
numarray<T> add(T const& a, numarray<T> const& b)
source

Array with a value added to each element.

numarray<vec3> curve_to_segments(numarray<vec3> const& curve_in)

Convert a polyline (p0,p1,...,pn) into a list of segment endpoints (p0,p1, p1,p2, ..., pn-1,pn).

div

generic2 overloads
template <typename T>
numarray<T> div(numarray<T> const& a, T const& b)
source

Array with each element divided by a value.

template <typename T>
numarray<T> div(T const& a, numarray<T> const& b)
source

Array of a value divided by each element.

mul

generic2 overloads
template <typename T>
numarray<T> mul(numarray<T> const& a, T const& b)
source
template <typename T>
numarray<T> mul(T const& a, numarray<T> const& b)
source

Array with each element multiplied by a value.

numarray<vec3> normal_per_vertex(
    numarray<vec3> const& position,
    numarray<uint3> const& connectivity,
    bool invert = false
)

Per-vertex normals averaging the unit normals of adjacent triangles, flipped if invert is true.

sub

generic2 overloads
template <typename T>
numarray<T> sub(numarray<T> const& a, T const& b)
source

Array with a value subtracted from each element.

template <typename T>
numarray<T> sub(T const& a, numarray<T> const& b)
source

Array of a value minus each element.

Compute 8

template <typename T>
T average(numarray<T> const& a)

Mean of the elements.

numarray<numarray<int>> connectivity_one_ring(numarray<uint3> const& connectivity, int N_vertex)

For each vertex, sorted indices of the vertices sharing a triangle with it (one-ring neighborhood).

max

genericsource
template <typename T>
T max(numarray<T> const& v)

Largest element.

min

genericsource
template <typename T>
T min(numarray<T> const& v)

Smallest element.

sum

genericsource
template <typename T>
T sum(numarray<T> const& a)

Sum of the elements.

Documented on another page

  • intersection_ray_spheres_closestClosest intersection of a ray with a set of spheres of common radius, the index of the hit sphere (or -1) being written to shape_index if given.on vec3
  • picking_mesh_vertex_as_sphereCasts the camera ray through the screen position and returns the closest vertex whose sphere of radius picking_distance it hits, with the hit point, the vertex normal and the vertex index (active is false if none).on vec2
  • picking_spheresCast a ray through a screen position (normalized device coordinates) and return the closest hit among spheres of common radius, with its index, position and normal.on vec2

Convert 2

str

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

Text representation of the elements, joined by separator and surrounded by begin and end.

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

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

I/O 4

mesh mesh_load_file_obj(
    std::string const& filename,
    numarray<numarray<int>>& vertex_correspondance
)

Load a mesh from an .obj file like mesh_load_file_obj, also returning for each file vertex the indices of the mesh vertices created from it (empty fields are not filled).

template <typename T>
void read_from_file(std::string const& filename, numarray<numarray<T>>& data)

Reads a file line by line, appending one inner array of values per line.

template <typename T>
std::istream& read_from_stream(std::istream& stream, numarray<T>& data)

Reads values until the end of the stream and appends them to the array.

template <typename T>
std::istream& read_from_stream_per_line(std::istream& stream, numarray<T>& data)

Reads the stream line by line, parsing each line into one element appended to the array.

Access 6

template <typename T>
void check_index_bounds(int index, numarray<T> const& data)

Raises an error if the index is outside [0, size-1]; no-op when CGP_NO_DEBUG is defined.

is_equal

generic2 overloads
template <typename T>
bool is_equal(numarray<T> const& a, numarray<T> const& b)
source

Element-wise equality of two arrays, approximate for floating-point elements.

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

Element-wise equality of two arrays of possibly different element types (e.g. float and int).

False if sizes differ.

ptr

genericsource
template <typename T>
auto const* ptr(numarray<T> const& v)

Pointer to the first scalar of the array content; the array must not be empty (asserted in debug).

size_in_memory

generic2 overloads
template <typename T>
int size_in_memory(numarray<T> const& v)
source

Size in bytes of the array content, summed over its elements.

template <typename T, int N>
int size_in_memory(numarray<numarray_stack<T,N>> const& v)
source

Size in bytes of an array of fixed-size vectors (number of elements times the size of one).

Other 1

void normal_per_vertex(
    numarray<vec3> const& position,
    numarray<uint3> const& connectivity,
    numarray<vec3>& normals_to_fill,
    bool invert = false
)

Fill normals_to_fill with per-vertex normals averaging the unit normals of adjacent triangles (resized only if needed, flipped if invert is true).

Functions returning a numarray

Construct 1

numarray<vec3> curve_primitive_circle(
    float radius = 1.0f,
    vec3 const& center = {0,0,0},
    vec3 const& normal = {0,0,1},
    int N_sample = 20
)

Closed circle polyline of the given radius and center in the plane orthogonal to the unit normal, the last sample repeating the first one.

I/O 1

numarray<numarray<int3>> obj_read_faces(std::string const& filename, obj_type const type)

Read the faces of an .obj file as polygons of 0-based (position, uv, normal) index triplets, -1 marking a missing index.

Operators

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

operator*

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

Element-wise product of two arrays; sizes must match and be non-zero (asserted in debug).

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

Array with each element multiplied by a scalar.

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

Array with each element multiplied by a scalar.

operator*=

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

Multiplies by an array element-wise in place; sizes must match and be non-zero (asserted in debug).

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

Multiplies each element by a scalar in place.

operator+

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

Element-wise sum of two arrays; sizes must match and be non-zero (asserted in debug).

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

Array with a value added to each element.

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

Array with a value added to each element.

operator+=

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

Adds an array element-wise in place; sizes must match (asserted in debug).

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

Adds a value to each element in place.

operator-

generic2 overloads
template <typename T>
numarray<T> operator-(numarray<T> const& a)
source

Element-wise negation of an array.

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

Element-wise difference of two arrays; sizes must match and be non-zero (asserted in debug).

operator-=

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

Subtracts an array element-wise in place; sizes must match (asserted in debug).

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

Subtracts a value from each element in place.

operator/

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

Element-wise quotient of two arrays; sizes must match and be non-zero (asserted in debug).

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

Array with each element divided by a scalar; the array must not be empty (asserted in debug).

operator/=

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

Divides by an array element-wise in place; sizes must match and be non-zero (asserted in debug).

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

Divides each element by a scalar in place; the array must not be empty (asserted in debug).

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

Writes all elements of the array to the stream, separated by spaces.

CGP - Computer Graphics Programming.