numarray
Dynamic-size array with numerical operations.
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> data | std::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
template <typename T>
numarray<T> add(numarray<T> const& a, T const& b)sourcetemplate <typename T>
numarray<T> add(T const& a, numarray<T> const& b)sourceArray 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).
template <typename T>
numarray<T> div(numarray<T> const& a, T const& b)sourceArray with each element divided by a value.
template <typename T>
numarray<T> div(T const& a, numarray<T> const& b)sourceArray of a value divided by each element.
template <typename T>
numarray<T> mul(numarray<T> const& a, T const& b)sourcetemplate <typename T>
numarray<T> mul(T const& a, numarray<T> const& b)sourceArray 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.
Compute 8
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).
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.onvec3picking_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).onvec2picking_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.onvec2
Convert 2
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.
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.
template <typename T>
bool is_equal(numarray<T> const& a, numarray<T> const& b)sourceElement-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)sourceElement-wise equality of two arrays of possibly different element types (e.g. float and int).
False if sizes differ.
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).
template <typename T>
int size_in_memory(numarray<T> const& v)sourceSize 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)sourceSize 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 (+, -, *, ==, ...)
template <typename T>
numarray<T> operator*(numarray<T> const& a, numarray<T> const& b)sourceElement-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)sourceArray with each element multiplied by a scalar.
template <typename T>
numarray<T> operator*(float a, numarray<T> const& b)sourceArray with each element multiplied by a scalar.
template <typename T>
numarray<T>& operator*=(numarray<T>& a, numarray<T> const& b)sourceMultiplies 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)sourceMultiplies each element by a scalar in place.
template <typename T>
numarray<T> operator+(numarray<T> const& a, numarray<T> const& b)sourceElement-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)sourceArray with a value added to each element.
template <typename T>
numarray<T> operator+(T const& a, numarray<T> const& b)sourceArray with a value added to each element.
template <typename T>
numarray<T>& operator+=(numarray<T>& a, numarray<T> const& b)sourceAdds an array element-wise in place; sizes must match (asserted in debug).
template <typename T>
numarray<T>& operator+=(numarray<T>& a, T const& b)sourceAdds a value to each element in place.
template <typename T>
numarray<T> operator-(numarray<T> const& a)sourceElement-wise negation of an array.
template <typename T>
numarray<T> operator-(numarray<T> const& a, numarray<T> const& b)sourceElement-wise difference of two arrays; sizes must match and be non-zero (asserted in debug).
template <typename T>
numarray<T>& operator-=(numarray<T>& a, numarray<T> const& b)sourceSubtracts an array element-wise in place; sizes must match (asserted in debug).
template <typename T>
numarray<T>& operator-=(numarray<T>& a, T const& b)sourceSubtracts a value from each element in place.
template <typename T>
numarray<T> operator/(numarray<T> const& a, numarray<T> const& b)sourceElement-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)sourceArray with each element divided by a scalar; the array must not be empty (asserted in debug).
template <typename T>
numarray<T>& operator/=(numarray<T>& a, numarray<T> const& b)sourceDivides 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)sourceDivides 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.