17#include <opencv2/core/mat.hpp>
62 Array(
const std::string &filename,
bool flip_j =
false);
63 Array(
const std::vector<std::vector<float>> &data);
237 return this->vector[j * this->shape.x + i];
250 return this->vector[j * this->shape.x + i];
255 return this->vector[ij.y * this->shape.x + ij.x];
260 return this->vector[ij.y * this->shape.x + ij.x];
272 return this->vector[index];
284 return this->vector[index];
298 void argmax(
float &vmax,
int &i,
int &j)
const;
344 void dump(
const std::string &fname =
"out.png")
const;
373 void from_file(
const std::string &fname);
588 void infos(
const std::string &msg =
"")
const;
706 glm::vec2
range()
const;
727 size_t bins = 1024)
const;
819 void set_slice(glm::ivec4 idx,
float value);
884 void to_exr(
const std::string &fname)
const;
896 void to_file(
const std::string &fname)
const;
906 void to_numpy(
const std::string &fname)
const;
927 void to_png(
const std::string &fname,
929 bool hillshading =
false,
930 int depth = CV_8U)
const;
951 float vmax = 0.f)
const;
965 void to_tiff(
const std::string &fname)
const;
1036 bool remap_values =
true,
1037 bool flip_j =
false);
1046 const int w = a.
shape.x;
1047 const int h = a.
shape.y;
1049 for (
int j = 0; j < h; ++j)
1050 for (
int i = 0; i < w; ++i)
1057 const int w = a.
shape.x;
1058 const int h = a.
shape.y;
1060 for (
int j = 0; j < h; ++j)
1061 for (
int i = 0; i < w; ++i)
1066template <
typename T,
typename Fn,
typename Reduce>
1072 [&](
int i,
int j,
float v) { acc = reduce(acc, fn(i, j, v)); });
1078template <
typename T,
typename Fn,
typename Reduce>
1083 for_each_cell(a, [&](
int i,
int j,
float v) { acc = reduce(acc, a(i, j)); });
Header file defining basic vector and matrix manipulation classes.
unsigned int uint
Definition array.hpp:14
Array class, helper to manipulate 2D float array with "(i, j)" indexing.
Definition array.hpp:32
void from_file(const std::string &fname)
Import array data from a raw binary file.
Definition io.cpp:19
void dump(const std::string &fname="out.png") const
Debug tool, dump some infos and generate an output file (16bits grayscale)
Definition methods.cpp:107
Array resample_to_shape(glm::ivec2 new_shape) const
Return a resampled array of shape new_shape using bilinear interpolation.
Definition methods.cpp:365
void infos(const std::string &msg="") const
Display various information about the array.
Definition io.cpp:59
float get_value_bilinear_at(int i, int j, float u, float v) const
Retrieves the array value at the location (x, y) near the index (i, j) using bilinear interpolation.
Definition methods.cpp:227
glm::ivec2 linear_index_reverse(int k) const
Convert a linear index to its corresponding (i, j) cell coordinates.
Definition methods.cpp:263
int size() const
Return the total number of elements in the array.
Definition methods.cpp:416
void dump_histogram(const std::string &msg="") const
Debug tool, dump array content as an ASCII histogram to the consol.
Definition methods.cpp:113
float get_gradient_x_at(int i, int j) const
Calculates the gradient in the 'x' (or 'i') direction at a specified index (i, j) using a 2nd order c...
Definition methods.cpp:136
float mean() const
Return the mean value of the elements in the array.
Definition methods.cpp:276
Array & operator=(const float value)
Overloads the assignment operator for scalar assignment.
Definition array.cpp:61
glm::ivec2 get_shape()
Retrieves the shape of the array.
Definition array.cpp:45
int linear_index(int i, int j) const
Return the linear index corresponding to the (i, j) cell in a 2D array.
Definition methods.cpp:258
Array extract_slice(glm::ivec4 idx) const
Extracts a subarray defined by the slice indices {i1, i2, j1, j2} from the original array,...
Definition methods.cpp:119
friend Array operator+(const float value, const Array &array)
Overloads the addition operator for scalar addition.
Definition array.cpp:234
void to_numpy(const std::string &fname) const
Export the array to a numpy binary file.
Definition io.cpp:115
Array & operator*=(const float value)
Overloads the multiplication-assignment operator for scalar multiplication.
Definition array.cpp:67
Array & operator+=(const float value)
Overloads the addition-assignment operator for scalar addition.
Definition array.cpp:105
const float & operator()(int i, int j) const
Overloads the function call operator to access the array value at index (i, j) (const version).
Definition array.hpp:248
float & operator()(int index)
Overloads the function call operator to access the array value at the linear index 'index'.
Definition array.hpp:282
float min() const
Return the value of the smallest element in the array.
Definition methods.cpp:286
float & operator()(glm::ivec2 ij)
Definition array.hpp:253
friend Array operator*(const float value, const Array &array)
Overloads the multiplication operator for scalar multiplication.
Definition array.cpp:166
float get_gradient_y_bilinear_at(int i, int j, float u, float v) const
Calculates the gradient in the 'y' (or 'j') direction at a location (x, y) near the index (i,...
Definition methods.cpp:160
void from_numpy(const std::string &fname)
Import array data from a numpy binary file.
Definition io.cpp:30
Array operator-() const
Overloads the unary minus operator.
Definition array.cpp:245
std::vector< float > get_vector() const
Retrieves the underlying data vector.
Definition array.cpp:50
float max() const
Return the value of the greatest element in the array.
Definition methods.cpp:271
void to_file(const std::string &fname) const
Export the array to a raw binary file.
Definition io.cpp:103
void to_tiff(const std::string &fname) const
Export the array as a TIFF image file.
Definition io.cpp:163
std::vector< float > unique_values() const
Return the unique elements of the array.
Definition methods.cpp:434
void print() const
Print the array values to the standard output (stdout).
Definition io.cpp:75
float sum() const
Return the sum of all array values.
Definition methods.cpp:429
size_t get_sizeof() const
Retrieves the number of bytes occupied by the array data.
Definition methods.cpp:188
float get_gradient_x_bilinear_at(int i, int j, float u, float v) const
Calculates the gradient in the 'x' (or 'i') direction at a location (x, y) near the index (i,...
Definition methods.cpp:146
std::vector< float > row_to_vector(int i)
Return a row i as a std::vector<float>.
Definition methods.cpp:394
std::vector< float > col_to_vector(int j)
Extracts a column 'j' as a std::vector.
Definition methods.cpp:44
Array remapped() const
Return an array remapped to [0, 1].
Definition methods.cpp:358
void depose_amount_kernel_bilinear_at(int i, int j, float u, float v, int ir, float amount)
Definition methods.cpp:64
void depose_amount_kernel_at(int i, int j, const Array &kernel, float amount)
Distributes a specified amount of value around the cell located at (i, j) using a 1D deposition kerne...
Definition methods.cpp:90
Array()
Constructs a new Array object.
Definition array.cpp:15
void argmax(float &vmax, int &i, int &j) const
Finds the maximum value in the array and its coordinates.
Definition methods.cpp:25
glm::ivec2 shape
The shape of the array {ni, nj}.
Definition array.hpp:38
void to_png(const std::string &fname, int cmap, bool hillshading=false, int depth=CV_8U) const
Export the array as a PNG image file with a specified colormap and hillshading.
Definition io.cpp:125
const float & operator()(glm::ivec2 ij) const
Definition array.hpp:258
void to_exr(const std::string &fname) const
Export the array as an OpenEXR image file.
Definition io.cpp:88
Array & operator-=(const float value)
Overloads the subtraction-assignment operator for scalar subtraction.
Definition array.cpp:124
float std() const
Return the standard deviation of all array values.
Definition methods.cpp:421
float median() const
Return the median value of the elements in the array.
Definition methods.cpp:281
friend Array operator/(const float value, const Array &array)
Overloads the division operator for scalar division.
Definition array.cpp:200
Array resample_to_shape_bilinear(glm::ivec2 new_shape) const
Definition methods.cpp:378
void to_png_grayscale(const std::string &fname, int depth=CV_8U, float vmin=0.f, float vmax=0.f) const
Export the array as a grayscale PNG image file with specified bit depth.
Definition io.cpp:139
void normalize()
Normalize array values so that the sum of all elements is equal to 1.
Definition methods.cpp:291
cv::Mat to_cv_mat()
Converts a 2D Array to an OpenCV cv::Mat.
Definition opencv_wrapper.cpp:12
void to_raw_16bit(const std::string &fname) const
Export the array as a 16-bit raw file for Unity terrain import.
Definition io.cpp:158
glm::vec2 range_percentile(float p_low, float p_high, size_t bins=1024) const
Computes an approximate percentile range using a histogram.
Definition methods.cpp:313
float get_value_nearest(float x, float y, glm::vec4 bbox)
Retrieves the nearest value at the location (x, y) within a bounded domain.
Definition methods.cpp:249
Array & operator/=(const float value)
Overloads the division-assignment operator for scalar division.
Definition array.cpp:86
float ptp() const
Return the peak-to-peak amplitude (i.e., the difference between the maximum and minimum values) of th...
Definition methods.cpp:301
glm::vec2 normalization_coeff(float vmin=0.f, float vmax=1.f) const
Computes normalization coefficients (a, b) such that a * array + b maps the values to the range [vmin...
Definition methods.cpp:193
float get_gradient_y_at(int i, int j) const
Calculates the gradient in the 'y' (or 'j') direction at a specified index (i, j) using a 2nd order c...
Definition methods.cpp:141
float & operator()(int i, int j)
Overloads the function call operator to access the array value at index (i, j).
Definition array.hpp:235
Array resample_to_shape_bicubic(glm::ivec2 new_shape) const
Return a resampled array of shape new_shape using bicubic interpolation.
Definition methods.cpp:370
const float & operator()(int index) const
Overloads the function call operator to access the array value at the linear index 'index' (const ver...
Definition array.hpp:270
void set_slice(glm::ivec4 idx, float value)
Set the value of a slice defined by indices {i1, i2, j1, j2} to a new value.
Definition methods.cpp:402
float get_value_bicubic_at(int i, int j, float u, float v) const
Retrieves the array value at the location (x, y) near the index (i, j) using bicubic interpolation.
Definition methods.cpp:205
Array resample_to_shape_nearest(glm::ivec2 new_shape) const
Return a resampled array of shape new_shape using nearest neighbor interpolation.
Definition methods.cpp:386
void depose_amount_bilinear_at(int i, int j, float u, float v, float amount)
Distribute a value 'amount' around the four cells (i, j), (i + 1, j), (i, j + 1), (i + 1,...
Definition methods.cpp:52
std::vector< float > vector
The underlying data storage, a vector of size shape.x * shape.y.
Definition array.hpp:44
glm::vec2 range() const
Computes the minimum and maximum values of the array.
Definition methods.cpp:306
glm::vec3 get_normal_at(int i, int j) const
Calculates the surface normal vector at the index (i, j).
Definition methods.cpp:174
void set_shape(glm::ivec2 new_shape)
Sets a new shape for the array.
Definition array.cpp:55
Predefined color mapping schemes for data visualization, including perceptual uniform (Viridis,...
Definition algebra.hpp:23
void for_each_cell(Array &a, Fn &&fn)
Apply a function to every cell (mutable).
Definition array.hpp:1044
Array cv_mat_to_array(const cv::Mat &mat, bool remap_values=true, bool flip_j=false)
Converts an OpenCV cv::Mat to a 2D Array with optional value scaling to [0, 1].
Definition opencv_wrapper.cpp:30
T reduce_cells(const Array &a, T init, Fn &&fn, Reduce &&reduce)
Reduce all cells to a single value.
Definition array.hpp:1067
size_t count_non_zero(const Array &array)
Count the number of non-zero elements in the array.
Definition array_functions.cpp:9
size_t count_zero(const Array &array)
Count the number of zero elements in the array.
Definition array_functions.cpp:17