URL: https://graphicscomputing.fr/cgp/reference/numarray/
Description: Dynamic-size array with numerical operations.

# numarray

Dynamic-size array with numerical operations.

Fields:

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

Functions taking it:

- `std::string type_str(numarray<T> const&)` - Type name as a string, e.g. "numarray<float>".
- `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.
- `int size_in_memory(numarray<T> const& v)` - Size in bytes of the array content, summed over its elements.
- `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).
- `bool is_equal(numarray<T> const& a, numarray<T> const& b)` - Element-wise equality of two arrays, approximate for floating-point elements; false if sizes differ.
- `bool is_equal(numarray<T1> const& a, numarray<T2> const& b)` - Element-wise equality of two arrays of possibly different element types (e.g. float and int); false if sizes differ.
- `T max(numarray<T> const& v)` - Largest element.
- `T min(numarray<T> const& v)` - Smallest element.
- `T average(numarray<T> const& a)` - Mean of the elements.
- `T sum(numarray<T> const& a)` - Sum of the elements.
- `numarray<T> sub(numarray<T> const& a, T const& b)` - Array with a value subtracted from each element.
- `numarray<T> add(numarray<T> const& a, T const& b)` - Array with a value added to each element.
- `numarray<T> mul(numarray<T> const& a, T const& b)` - Array with each element multiplied by a value.
- `numarray<T> div(numarray<T> const& a, T const& b)` - Array with each element divided by a value.
- `numarray<T> sub(T const& a, numarray<T> const& b)` - Array of a value minus each element.
- `numarray<T> add(T const& a, numarray<T> const& b)` - Array with a value added to each element.
- `numarray<T> mul(T const& a, numarray<T> const& b)` - Array with each element multiplied by a value.
- `numarray<T> div(T const& a, numarray<T> const& b)` - Array of a value divided by each element.
- `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.
- `int size_in_memory(numarray<numarray_stack<T,N>> const& v)` - Size in bytes of an array of fixed-size vectors (number of elements times the size of one).
- `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.
- `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.
- `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.
- `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).
- `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.
- `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).
- `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).
- `intersection_structure intersection_ray_spheres_closest(vec3 const& ray_origin, vec3 const& ray_direction, numarray<vec3> const& sphere_centers, float sphere_radius, int* shape_index = nullptr)` - Closest 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.
- `picking_structure picking_spheres(vec2 const& screen_click, numarray<vec3> const& spheres_centers, float spheres_radius, camera_generic_base const& camera, camera_projection_perspective const& projection)` - Cast 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.
- `picking_structure picking_mesh_vertex_as_sphere(vec2 const& screen_click, numarray<vec3> const& vertex_position, numarray<vec3> const& vertex_normal, float picking_distance, camera_generic_base const& camera, camera_projection_perspective const& projection)` - Casts 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).
- `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).

Functions returning it:

- `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.
- `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:

- `std::ostream& operator<<(std::ostream& s, numarray<T> const& v)` - Writes all elements of the array to the stream, separated by spaces.
- `numarray<T> operator-(numarray<T> const& a)` - Element-wise negation of an array.
- `numarray<T>& operator+=(numarray<T>& a, numarray<T> const& b)` - Adds an array element-wise in place; sizes must match (asserted in debug).
- `numarray<T>& operator+=(numarray<T>& a, T const& b)` - Adds a value to each element in place.
- `numarray<T> operator+(numarray<T> const& a, numarray<T> const& b)` - Element-wise sum of two arrays; sizes must match and be non-zero (asserted in debug).
- `numarray<T> operator+(numarray<T> const& a, T const& b)` - Array with a value added to each element.
- `numarray<T> operator+(T const& a, numarray<T> const& b)` - Array with a value added to each element.
- `numarray<T>& operator-=(numarray<T>& a, numarray<T> const& b)` - Subtracts an array element-wise in place; sizes must match (asserted in debug).
- `numarray<T>& operator-=(numarray<T>& a, T const& b)` - Subtracts a value from each element in place.
- `numarray<T> operator-(numarray<T> const& a, numarray<T> const& b)` - Element-wise difference of two arrays; sizes must match and be non-zero (asserted in debug).
- `numarray<T>& operator*=(numarray<T>& a, numarray<T> const& b)` - Multiplies by an array element-wise in place; sizes must match and be non-zero (asserted in debug).
- `numarray<T> operator*(numarray<T> const& a, numarray<T> const& b)` - Element-wise product of two arrays; sizes must match and be non-zero (asserted in debug).
- `numarray<T>& operator*=(numarray<T>& a, float b)` - Multiplies each element by a scalar in place.
- `numarray<T> operator*(numarray<T> const& a, float b)` - Array with each element multiplied by a scalar.
- `numarray<T> operator*(float a, numarray<T> const& b)` - Array with each element multiplied by a scalar.
- `numarray<T>& operator/=(numarray<T>& a, numarray<T> const& b)` - Divides by an array element-wise in place; sizes must match and be non-zero (asserted in debug).
- `numarray<T>& operator/=(numarray<T>& a, float b)` - Divides each element by a scalar in place; the array must not be empty (asserted in debug).
- `numarray<T> operator/(numarray<T> const& a, numarray<T> const& b)` - Element-wise quotient of two arrays; sizes must match and be non-zero (asserted in debug).
- `numarray<T> operator/(numarray<T> const& a, float b)` - Array with each element divided by a scalar; the array must not be empty (asserted in debug).
