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 "highmap/algebra.hpp"
21
22namespace hmap
23{
24
30class Array
31{
32public:
37 glm::ivec2 shape = glm::ivec2(0, 0);
38
43 std::vector<float> vector;
44
58 Array();
59
61 Array(glm::ivec2 shape);
62
64 Array(glm::ivec2 shape, float value);
65
67 Array(const std::string &filename, bool flip_j = false);
68
70 Array(const std::vector<std::vector<float>> &data);
71
73 Array(const std::initializer_list<std::initializer_list<float>> &data);
74 //----------------------------------------
75 // overload
76 //----------------------------------------
77
84 Array &operator=(const float value);
85
93 Array &operator*=(const float value);
94
96 Array &operator*=(const Array &array);
97
104 Array &operator/=(const float value);
105
107 Array &operator/=(const Array &array);
108
115 Array &operator+=(const float value);
116
118 Array &operator+=(const Array &array);
119
127 Array &operator-=(const float value);
128
130 Array &operator-=(const Array &array);
131
138 Array operator*(const float value) const;
139
147 Array operator*(const Array &array) const;
148
156 friend Array operator*(const float value, const Array &array);
157
164 Array operator/(const float value) const;
165
173 Array operator/(const Array &array) const;
174
182 friend Array operator/(const float value, const Array &array);
183
190 Array operator+(const float value) const;
191
199 Array operator+(const Array &array) const;
200
208 friend Array operator+(const float value, const Array &array);
209
215 Array operator-() const;
216
223 Array operator-(const float value) const;
224
232 Array operator-(const Array &array) const;
233
241 friend const Array operator-(const float value, const Array &array);
242
251 float &operator()(int i, int j)
252 {
253 return this->vector[j * this->shape.x + i];
254 }
255
264
266 const float &operator()(int i, int j) const
267 {
268 return this->vector[j * this->shape.x + i];
269 }
270
271 float &operator()(glm::ivec2 ij)
272 {
273 return this->vector[ij.y * this->shape.x + ij.x];
274 }
275
276 const float &operator()(glm::ivec2 ij) const
277 {
278 return this->vector[ij.y * this->shape.x + ij.x];
279 }
280
288
290 const float &operator()(int index) const
291 {
292 return this->vector[index];
293 }
294
302 float &operator()(int index)
303 {
304 return this->vector[index];
305 }
306
307 //----------------------------------------
308 // methods
309 //----------------------------------------
310
318 void argmax(float &vmax, int &i, int &j) const;
319
326 std::vector<float> col_to_vector(int j);
327
338 void depose_amount_bilinear_at(int i, int j, float u, float v, float amount);
339
341 int j,
342 float u,
343 float v,
344 int ir,
345 float amount);
346
358 void depose_amount_kernel_at(int i, int j, const Array &kernel, float amount);
359
364 void dump(const std::string &fname = "out.png") const;
365
369 void dump_histogram(int bins = 32, const std::string &msg = "") const;
370
380 Array extract_slice(glm::ivec4 idx) const;
381
383 Array extract_slice(int i1, int i2, int j1, int j2) const;
384
395 void from_file(const std::string &fname);
396
405 void from_numpy(const std::string &fname);
406
427 float get_gradient_x_at(int i, int j) const;
428
449 float get_gradient_y_at(int i, int j) const;
450
473 float get_gradient_x_bilinear_at(int i, int j, float u, float v) const;
474
497 float get_gradient_y_bilinear_at(int i, int j, float u, float v) const;
498
510 glm::vec3 get_normal_at(int i, int j) const;
511
517 glm::ivec2 get_shape();
518
528 size_t get_sizeof() const;
529
552 float get_value_bicubic_at(int i, int j, float u, float v) const;
553
575 float get_value_bilinear_at(int i, int j, float u, float v) const;
576
587 float get_value_bilinear(float x,
588 float y,
589 const UvRect &uv_rect = UvRect::unit()) const;
590
606 float get_value_nearest(float x,
607 float y,
608 const UvRect &uv_rect = UvRect::unit()) const;
609
615 std::vector<float> get_vector() const;
616
626 void infos(const std::string &msg = "") const;
627
640 int linear_index(int i, int j) const;
641
653 glm::ivec2 linear_index_reverse(int k) const;
654
664 float max(ICommunicator &comm = SingleArrayComm::instance()) const;
665
675 float mean(ICommunicator &comm = SingleArrayComm::instance()) const;
676
681 float median() const;
682
692 float min(ICommunicator &comm = SingleArrayComm::instance()) const;
693
705
720 glm::vec2 normalization_coeff(float vmin = 0.f, float vmax = 1.f) const;
721
729 void print() const;
730
742 float ptp(ICommunicator &comm = SingleArrayComm::instance()) const;
743
751 glm::vec2 range(ICommunicator &comm = SingleArrayComm::instance()) const;
752
772 glm::vec2 range_percentile(float p_low,
773 float p_high,
774 size_t bins = 1024,
775 bool ignore_zeros = false) const;
776
780 Array remapped() const;
781
799 Array resample_to_shape(glm::ivec2 new_shape) const;
800
819 Array resample_to_shape_bicubic(glm::ivec2 new_shape) const;
820 Array resample_to_shape_bilinear(glm::ivec2 new_shape) const;
821
834 Array resample_to_shape_nearest(glm::ivec2 new_shape) const;
835
846 std::vector<float> row_to_vector(int i);
847
853 void set_shape(glm::ivec2 new_shape);
854
867 void set_slice(glm::ivec4 idx, float value);
868
870 void set_slice(glm::ivec4 idx, const Array &array);
871
881 int size() const;
882
890 float std(float *p_mean = nullptr,
892
903 float sum(ICommunicator &comm = SingleArrayComm::instance()) const;
904
917 void to_exr(const std::string &fname) const;
918
929 void to_file(const std::string &fname) const;
930
939 void to_numpy(const std::string &fname) const;
940
960 void to_png(const std::string &fname,
961 int cmap,
962 bool hillshading = false,
963 int depth = 0) const;
964
981 void to_png_grayscale(const std::string &fname,
982 int depth = 0,
983 float vmin = 0.f,
984 float vmax = 0.f) const;
985
998 void to_tiff(const std::string &fname) const;
999
1013 void to_raw_16bit(const std::string &fname) const;
1014
1025 std::vector<float> unique_values(
1027};
1028
1029// ==========================================================================
1030// Functions
1031// ==========================================================================
1032
1038size_t count_non_zero(const Array &array);
1039
1045size_t count_zero(const Array &array);
1046
1047// ==========================================================================
1048// per cell operations wrapper(s)
1049// ==========================================================================
1050
1052template <typename Fn> inline void for_each_cell(Array &a, Fn &&fn)
1053{
1054 const int w = a.shape.x;
1055 const int h = a.shape.y;
1056
1057 for (int j = 0; j < h; ++j)
1058 for (int i = 0; i < w; ++i)
1059 fn(i, j, a(i, j));
1060}
1061
1063template <typename Fn> inline void for_each_cell(const Array &a, Fn &&fn)
1064{
1065 const int w = a.shape.x;
1066 const int h = a.shape.y;
1067
1068 for (int j = 0; j < h; ++j)
1069 for (int i = 0; i < w; ++i)
1070 fn(i, j, a(i, j));
1071}
1072
1073} // 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:31
void from_file(const std::string &fname)
Import array data from a raw binary file.
Definition io.cpp:27
void dump(const std::string &fname="out.png") const
Debug tool, dump some infos and generate an output file (16bits grayscale).
Definition methods.cpp:109
glm::vec2 range(ICommunicator &comm=SingleArrayComm::instance()) const
Computes the minimum and maximum values of the array.
Definition methods.cpp:347
float mean(ICommunicator &comm=SingleArrayComm::instance()) const
Return the mean value of the elements in the array.
Definition methods.cpp:300
Array resample_to_shape(glm::ivec2 new_shape) const
Return a resampled array of shape new_shape using bilinear interpolation.
Definition methods.cpp:460
void normalize(ICommunicator &comm=SingleArrayComm::instance())
Normalize array values so that the sum of all elements is equal to 1.
Definition methods.cpp:328
void infos(const std::string &msg="") const
Display various information about the array.
Definition io.cpp:67
glm::vec2 range_percentile(float p_low, float p_high, size_t bins=1024, bool ignore_zeros=false) const
Computes an approximate percentile range using a histogram.
Definition methods.cpp:362
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:231
glm::ivec2 linear_index_reverse(int k) const
Convert a linear index to its corresponding (i, j) cell coordinates.
Definition methods.cpp:285
int size() const
Return the total number of elements in the array.
Definition methods.cpp:527
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:140
float sum(ICommunicator &comm=SingleArrayComm::instance()) const
Return the sum of all array values.
Definition methods.cpp:558
Array & operator=(const float value)
Overloads the assignment operator for scalar assignment.
Definition array.cpp:102
glm::ivec2 get_shape()
Retrieves the shape of the array.
Definition array.cpp:80
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:280
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:121
void to_numpy(const std::string &fname) const
Export the array to a numpy binary file.
Definition io.cpp:122
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:115
Array operator+(const float value) const
Overloads the addition operator for scalar addition.
Definition array.cpp:268
Array & operator*=(const float value)
Overloads the multiplication-assignment operator for scalar multiplication.
Definition array.cpp:108
Array & operator+=(const float value)
Overloads the addition-assignment operator for scalar addition.
Definition array.cpp:152
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:266
float & operator()(int index)
Overloads the function call operator to access the array value at the linear index 'index'.
Definition array.hpp:302
float & operator()(glm::ivec2 ij)
Definition array.hpp:271
void to_png(const std::string &fname, int cmap, bool hillshading=false, int depth=0) const
Export the array as a PNG image file with a specified colormap and hillshading.
Definition io.cpp:132
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:164
void to_png_grayscale(const std::string &fname, int depth=0, 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:146
void from_numpy(const std::string &fname)
Import array data from a numpy binary file.
Definition io.cpp:38
Array operator-() const
Overloads the unary minus operator.
Definition array.cpp:304
std::vector< float > get_vector() const
Retrieves the underlying data vector.
Definition array.cpp:85
void to_file(const std::string &fname) const
Export the array to a raw binary file.
Definition io.cpp:110
void to_tiff(const std::string &fname) const
Export the array as a TIFF image file.
Definition io.cpp:170
float std(float *p_mean=nullptr, ICommunicator &comm=SingleArrayComm::instance()) const
Return the standard deviation of all array values.
Definition methods.cpp:532
void print() const
Print the array values to the standard output (stdout).
Definition io.cpp:83
size_t get_sizeof() const
Retrieves the number of bytes occupied by the array data.
Definition methods.cpp:192
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:150
std::vector< float > row_to_vector(int i)
Return a row i as a std::vector<float>.
Definition methods.cpp:501
std::vector< float > col_to_vector(int j)
Extracts a column 'j' as a std::vector.
Definition methods.cpp:46
Array remapped() const
Return an array remapped to [0, 1].
Definition methods.cpp:453
void depose_amount_kernel_bilinear_at(int i, int j, float u, float v, int ir, float amount)
Definition methods.cpp:66
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:92
float min(ICommunicator &comm=SingleArrayComm::instance()) const
Return the value of the smallest element in the array.
Definition methods.cpp:321
Array()
Constructs a new Array object.
Definition array.cpp:17
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:37
const float & operator()(glm::ivec2 ij) const
Definition array.hpp:276
void to_exr(const std::string &fname) const
Export the array as an OpenEXR image file.
Definition io.cpp:96
Array & operator-=(const float value)
Overloads the subtraction-assignment operator for scalar subtraction.
Definition array.cpp:173
float median() const
Return the median value of the elements in the array.
Definition methods.cpp:315
Array resample_to_shape_bilinear(glm::ivec2 new_shape) const
Definition methods.cpp:479
std::vector< float > unique_values(ICommunicator &comm=SingleArrayComm::instance()) const
Return the unique elements of the array.
Definition methods.cpp:568
float ptp(ICommunicator &comm=SingleArrayComm::instance()) const
Return the peak-to-peak amplitude (i.e., the difference between the maximum and minimum values) of th...
Definition methods.cpp:341
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:165
Array & operator/=(const float value)
Overloads the division-assignment operator for scalar division.
Definition array.cpp:129
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:197
float get_value_nearest(float x, float y, const UvRect &uv_rect=UvRect::unit()) const
Retrieves the nearest value at the location (x, y) within a bounded domain.
Definition methods.cpp:272
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:145
Array operator/(const float value) const
Overloads the division operator for scalar division.
Definition array.cpp:230
Array operator*(const float value) const
Overloads the multiplication operator for scalar multiplication.
Definition array.cpp:194
float & operator()(int i, int j)
Overloads the function call operator to access the array value at index (i, j).
Definition array.hpp:251
Array resample_to_shape_bicubic(glm::ivec2 new_shape) const
Return a resampled array of shape new_shape using bicubic interpolation.
Definition methods.cpp:468
float get_value_bilinear(float x, float y, const UvRect &uv_rect=UvRect::unit()) const
Retrieves the value at the location (x, y) within a bounded domain using bilinear interpolation.
Definition methods.cpp:253
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:290
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:509
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:209
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:490
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:54
std::vector< float > vector
The underlying data storage, a vector of size shape.x * shape.y.
Definition array.hpp:43
glm::vec3 get_normal_at(int i, int j) const
Calculates the surface normal vector at the index (i, j).
Definition methods.cpp:178
void set_shape(glm::ivec2 new_shape)
Sets a new shape for the array.
Definition array.cpp:90
float max(ICommunicator &comm=SingleArrayComm::instance()) const
Return the value of the greatest element in the array.
Definition methods.cpp:293
Abstract communicator interface providing MPI-style collective directives for single-source SPMD Arra...
Definition communicator_interface.hpp:60
static SingleArrayComm & instance()
Definition communicator.cpp:78
Lightweight abstract SPMD communicator interface for Array and VirtualArray collective operations.
Definition opencv.hpp:20
void for_each_cell(Array &a, Fn &&fn)
Apply a function to every cell (mutable).
Definition array.hpp:1052
size_t count_non_zero(const Array &array)
Count the number of non-zero elements in the array.
Definition array_functions.cpp:12
size_t count_zero(const Array &array)
Count the number of zero elements in the array.
Definition array_functions.cpp:20
Axis-aligned rectangle in normalized [0, 1]^2 domain/UV space.
Definition uv_rect.hpp:30
static constexpr UvRect unit()
Definition uv_rect.hpp:46
Normalized domain/UV rectangle [u_min, u_max] x [v_min, v_max].