HighMap library (C++)
Loading...
Searching...
No Matches
array.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
13#pragma once
14
15#include <functional>
16#include <random>
17
18#include <opencv2/core.hpp>
19
20#include "highmap/algebra.hpp"
21#include "highmap/colormaps.hpp"
22
23namespace hmap
24{
25
31class Array
32{
33public:
38 glm::ivec2 shape = glm::ivec2(0, 0);
39
44 std::vector<float> vector;
45
59 Array();
60 Array(glm::ivec2 shape);
61 Array(glm::ivec2 shape, float value);
62 Array(const std::string &filename, bool flip_j = false);
63 Array(const std::vector<std::vector<float>> &data);
64 Array(const std::initializer_list<std::initializer_list<float>>
65 &data);
66
67 //----------------------------------------
68 // overload
69 //----------------------------------------
70
77 Array &operator=(const float value);
78
86 Array &operator*=(const float value);
87 Array &operator*=(const Array &array);
88
95 Array &operator/=(const float value);
96 Array &operator/=(const Array &array);
97
104 Array &operator+=(const float value);
105 Array &operator+=(const Array &array);
106
114 Array &operator-=(const float value);
115 Array &operator-=(const Array &array);
116
123 Array operator*(const float value) const;
124
132 Array operator*(const Array &array) const;
133
141 friend Array operator*(const float value, const Array &array);
142
149 Array operator/(const float value) const;
150
158 Array operator/(const Array &array) const;
159
167 friend Array operator/(const float value, const Array &array);
168
175 Array operator+(const float value) const;
176
184 Array operator+(const Array &array) const;
185
193 friend Array operator+(const float value, const Array &array);
194
200 Array operator-() const;
201
208 Array operator-(const float value) const;
209
217 Array operator-(const Array &array) const;
218
226 friend const Array operator-(const float value, const Array &array);
227
236 float &operator()(int i, int j)
237 {
238 return this->vector[j * this->shape.x + i];
239 }
240
249 const float &operator()(int i, int j) const
250 {
251 return this->vector[j * this->shape.x + i];
252 }
253
254 float &operator()(glm::ivec2 ij)
255 {
256 return this->vector[ij.y * this->shape.x + ij.x];
257 }
258
259 const float &operator()(glm::ivec2 ij) const
260 {
261 return this->vector[ij.y * this->shape.x + ij.x];
262 }
263
271 const float &operator()(int index) const
272 {
273 return this->vector[index];
274 }
275
283 float &operator()(int index)
284 {
285 return this->vector[index];
286 }
287
288 //----------------------------------------
289 // methods
290 //----------------------------------------
291
299 void argmax(float &vmax, int &i, int &j) const;
300
307 std::vector<float> col_to_vector(int j);
308
319 void depose_amount_bilinear_at(int i, int j, float u, float v, float amount);
320
322 int j,
323 float u,
324 float v,
325 int ir,
326 float amount);
327
339 void depose_amount_kernel_at(int i, int j, const Array &kernel, float amount);
340
345 void dump(const std::string &fname = "out.png") const;
346
350 void dump_histogram(int bins = 32, const std::string &msg = "") const;
351
361 Array extract_slice(glm::ivec4 idx) const;
362 Array extract_slice(int i1, int i2, int j1, int j2) const;
363
374 void from_file(const std::string &fname);
375
384 void from_numpy(const std::string &fname);
385
406 float get_gradient_x_at(int i, int j) const;
407
428 float get_gradient_y_at(int i, int j) const;
429
452 float get_gradient_x_bilinear_at(int i, int j, float u, float v) const;
453
476 float get_gradient_y_bilinear_at(int i, int j, float u, float v) const;
477
489 glm::vec3 get_normal_at(int i, int j) const;
490
496 glm::ivec2 get_shape();
497
507 size_t get_sizeof() const;
508
531 float get_value_bicubic_at(int i, int j, float u, float v) const;
532
554 float get_value_bilinear_at(int i, int j, float u, float v) const;
555
571 float get_value_nearest(float x, float y, glm::vec4 bbox);
572
578 std::vector<float> get_vector() const;
579
589 void infos(const std::string &msg = "") const;
590
603 int linear_index(int i, int j) const;
604
616 glm::ivec2 linear_index_reverse(int k) const;
617
626 float max() const;
627
636 float mean() const;
637
642 float median() const;
643
652 float min() const;
653
662 void normalize();
663
678 glm::vec2 normalization_coeff(float vmin = 0.f, float vmax = 1.f) const;
679
687 void print() const;
688
699 float ptp() const;
700
707 glm::vec2 range() const;
708
726 glm::vec2 range_percentile(float p_low,
727 float p_high,
728 size_t bins = 1024) const;
729
733 Array remapped() const;
734
752 Array resample_to_shape(glm::ivec2 new_shape) const;
753
772 Array resample_to_shape_bicubic(glm::ivec2 new_shape) const;
773 Array resample_to_shape_bilinear(glm::ivec2 new_shape) const;
774
787 Array resample_to_shape_nearest(glm::ivec2 new_shape) const;
788
799 std::vector<float> row_to_vector(int i);
800
806 void set_shape(glm::ivec2 new_shape);
807
820 void set_slice(glm::ivec4 idx, float value);
821 void set_slice(glm::ivec4 idx, const Array &array);
822
832 int size() const;
833
838 float std() const;
839
849 float sum() const;
850
871 cv::Mat to_cv_mat();
872
885 void to_exr(const std::string &fname) const;
886
897 void to_file(const std::string &fname) const;
898
907 void to_numpy(const std::string &fname) const;
908
928 void to_png(const std::string &fname,
929 int cmap,
930 bool hillshading = false,
931 int depth = CV_8U) const;
932
949 void to_png_grayscale(const std::string &fname,
950 int depth = CV_8U,
951 float vmin = 0.f,
952 float vmax = 0.f) const;
953
966 void to_tiff(const std::string &fname) const;
967
981 void to_raw_16bit(const std::string &fname) const;
982
991 std::vector<float> unique_values() const;
992};
993
994// ==========================================================================
995// Functions
996// ==========================================================================
997
1003size_t count_non_zero(const Array &array);
1004
1010size_t count_zero(const Array &array);
1011
1036Array cv_mat_to_array(const cv::Mat &mat,
1037 bool remap_values = true,
1038 bool flip_j = false);
1039
1040std::string get_opencv_build_information();
1041
1042// ==========================================================================
1043// per cell operations wrapper(s)
1044// ==========================================================================
1045
1047template <typename Fn> inline void for_each_cell(Array &a, Fn &&fn)
1048{
1049 const int w = a.shape.x;
1050 const int h = a.shape.y;
1051
1052 for (int j = 0; j < h; ++j)
1053 for (int i = 0; i < w; ++i)
1054 fn(i, j, a(i, j));
1055}
1056
1058template <typename Fn> inline void for_each_cell(const Array &a, Fn &&fn)
1059{
1060 const int w = a.shape.x;
1061 const int h = a.shape.y;
1062
1063 for (int j = 0; j < h; ++j)
1064 for (int i = 0; i < w; ++i)
1065 fn(i, j, a(i, j));
1066}
1067
1068} // namespace hmap
Header file defining basic vector and matrix manipulation classes.
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:28
void dump(const std::string &fname="out.png") const
Debug tool, dump some infos and generate an output file (16bits grayscale)
Definition methods.cpp:105
Array resample_to_shape(glm::ivec2 new_shape) const
Return a resampled array of shape new_shape using bilinear interpolation.
Definition methods.cpp:364
void infos(const std::string &msg="") const
Display various information about the array.
Definition io.cpp:68
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:225
glm::ivec2 linear_index_reverse(int k) const
Convert a linear index to its corresponding (i, j) cell coordinates.
Definition methods.cpp:261
int size() const
Return the total number of elements in the array.
Definition methods.cpp:415
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:134
float mean() const
Return the mean value of the elements in the array.
Definition methods.cpp:274
Array & operator=(const float value)
Overloads the assignment operator for scalar assignment.
Definition array.cpp:79
glm::ivec2 get_shape()
Retrieves the shape of the array.
Definition array.cpp:63
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:256
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:117
friend Array operator+(const float value, const Array &array)
Overloads the addition operator for scalar addition.
Definition array.cpp:252
void to_numpy(const std::string &fname) const
Export the array to a numpy binary file.
Definition io.cpp:123
void dump_histogram(int bins=32, const std::string &msg="") const
Debug tool, dump array content as an ASCII histogram to the consol.
Definition methods.cpp:111
Array & operator*=(const float value)
Overloads the multiplication-assignment operator for scalar multiplication.
Definition array.cpp:85
Array & operator+=(const float value)
Overloads the addition-assignment operator for scalar addition.
Definition array.cpp:123
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:249
float & operator()(int index)
Overloads the function call operator to access the array value at the linear index 'index'.
Definition array.hpp:283
float min() const
Return the value of the smallest element in the array.
Definition methods.cpp:284
float & operator()(glm::ivec2 ij)
Definition array.hpp:254
friend Array operator*(const float value, const Array &array)
Overloads the multiplication operator for scalar multiplication.
Definition array.cpp:184
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:158
void from_numpy(const std::string &fname)
Import array data from a numpy binary file.
Definition io.cpp:39
Array operator-() const
Overloads the unary minus operator.
Definition array.cpp:263
std::vector< float > get_vector() const
Retrieves the underlying data vector.
Definition array.cpp:68
float max() const
Return the value of the greatest element in the array.
Definition methods.cpp:269
void to_file(const std::string &fname) const
Export the array to a raw binary file.
Definition io.cpp:111
void to_tiff(const std::string &fname) const
Export the array as a TIFF image file.
Definition io.cpp:171
std::vector< float > unique_values() const
Return the unique elements of the array.
Definition methods.cpp:433
void print() const
Print the array values to the standard output (stdout).
Definition io.cpp:84
float sum() const
Return the sum of all array values.
Definition methods.cpp:428
size_t get_sizeof() const
Retrieves the number of bytes occupied by the array data.
Definition methods.cpp:186
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:144
std::vector< float > row_to_vector(int i)
Return a row i as a std::vector<float>.
Definition methods.cpp:393
std::vector< float > col_to_vector(int j)
Extracts a column 'j' as a std::vector.
Definition methods.cpp:42
Array remapped() const
Return an array remapped to [0, 1].
Definition methods.cpp:357
void depose_amount_kernel_bilinear_at(int i, int j, float u, float v, int ir, float amount)
Definition methods.cpp:62
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:88
Array()
Constructs a new Array object.
Definition array.cpp:16
void argmax(float &vmax, int &i, int &j) const
Finds the maximum value in the array and its coordinates.
Definition methods.cpp:23
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:133
const float & operator()(glm::ivec2 ij) const
Definition array.hpp:259
void to_exr(const std::string &fname) const
Export the array as an OpenEXR image file.
Definition io.cpp:97
Array & operator-=(const float value)
Overloads the subtraction-assignment operator for scalar subtraction.
Definition array.cpp:142
float std() const
Return the standard deviation of all array values.
Definition methods.cpp:420
float median() const
Return the median value of the elements in the array.
Definition methods.cpp:279
friend Array operator/(const float value, const Array &array)
Overloads the division operator for scalar division.
Definition array.cpp:218
Array resample_to_shape_bilinear(glm::ivec2 new_shape) const
Definition methods.cpp:377
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:147
void normalize()
Normalize array values so that the sum of all elements is equal to 1.
Definition methods.cpp:289
cv::Mat to_cv_mat()
Converts a 2D Array to an OpenCV cv::Mat.
Definition opencv_wrapper.cpp:16
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:166
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:312
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:247
Array & operator/=(const float value)
Overloads the division-assignment operator for scalar division.
Definition array.cpp:104
float ptp() const
Return the peak-to-peak amplitude (i.e., the difference between the maximum and minimum values) of th...
Definition methods.cpp:300
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:191
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:139
float & operator()(int i, int j)
Overloads the function call operator to access the array value at index (i, j).
Definition array.hpp:236
Array resample_to_shape_bicubic(glm::ivec2 new_shape) const
Return a resampled array of shape new_shape using bicubic interpolation.
Definition methods.cpp:369
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:271
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:401
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:203
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:385
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:50
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:305
glm::vec3 get_normal_at(int i, int j) const
Calculates the surface normal vector at the index (i, j).
Definition methods.cpp:172
void set_shape(glm::ivec2 new_shape)
Sets a new shape for the array.
Definition array.cpp:73
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:1047
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:34
size_t count_non_zero(const Array &array)
Count the number of non-zero elements in the array.
Definition array_functions.cpp:12
std::string get_opencv_build_information()
Definition opencv_wrapper.cpp:56
size_t count_zero(const Array &array)
Count the number of zero elements in the array.
Definition array_functions.cpp:20