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
14typedef unsigned int uint;
15
16#include <functional>
17#include <opencv2/core/mat.hpp>
18#include <random>
19
20#include "highmap/algebra.hpp"
21#include "highmap/colormaps.hpp"
22
23namespace hmap
24{
25
31class Array
32{
33public:
38 glm::ivec2 shape;
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
65 //----------------------------------------
66 // overload
67 //----------------------------------------
68
75 Array &operator=(const float value);
76
84 Array &operator*=(const float value);
85 Array &operator*=(const Array &array);
86
93 Array &operator/=(const float value);
94 Array &operator/=(const Array &array);
95
102 Array &operator+=(const float value);
103
104 Array &operator+=(const Array &array);
105
113 Array &operator-=(const float value);
114 Array &operator-=(const Array &array);
115
122 Array operator*(const float value) const;
123
131 Array operator*(const Array &array) const;
132
140 friend Array operator*(const float value, const Array &array);
141
148 Array operator/(const float value) const;
149
157 Array operator/(const Array &array) const;
158
166 friend Array operator/(const float value, const Array &array);
167
174 Array operator+(const float value) const;
175
183 Array operator+(const Array &array) const;
184
192 friend Array operator+(const float value, const Array &array);
193
199 Array operator-() const;
200
207 Array operator-(const float value) const;
208
216 Array operator-(const Array &array) const;
217
225 friend const Array operator-(const float value, const Array &array);
226
235 float &operator()(int i, int j)
236 {
237 return this->vector[j * this->shape.x + i];
238 }
239
248 const float &operator()(int i, int j) const
249 {
250 return this->vector[j * this->shape.x + i];
251 }
252
253 float &operator()(glm::ivec2 ij)
254 {
255 return this->vector[ij.y * this->shape.x + ij.x];
256 }
257
258 const float &operator()(glm::ivec2 ij) const
259 {
260 return this->vector[ij.y * this->shape.x + ij.x];
261 }
262
270 const float &operator()(int index) const
271 {
272 return this->vector[index];
273 }
274
282 float &operator()(int index)
283 {
284 return this->vector[index];
285 }
286
287 //----------------------------------------
288 // methods
289 //----------------------------------------
290
298 void argmax(float &vmax, int &i, int &j) const;
299
306 std::vector<float> col_to_vector(int j);
307
318 void depose_amount_bilinear_at(int i, int j, float u, float v, float amount);
319
321 int j,
322 float u,
323 float v,
324 int ir,
325 float amount);
326
338 void depose_amount_kernel_at(int i, int j, const Array &kernel, float amount);
339
344 void dump(const std::string &fname = "out.png") const;
345
349 void dump_histogram(const std::string &msg = "") const;
350
360 Array extract_slice(glm::ivec4 idx) const;
361 Array extract_slice(int i1, int i2, int j1, int j2) const;
362
373 void from_file(const std::string &fname);
374
383 void from_numpy(const std::string &fname);
384
405 float get_gradient_x_at(int i, int j) const;
406
427 float get_gradient_y_at(int i, int j) const;
428
451 float get_gradient_x_bilinear_at(int i, int j, float u, float v) const;
452
475 float get_gradient_y_bilinear_at(int i, int j, float u, float v) const;
476
488 glm::vec3 get_normal_at(int i, int j) const;
489
495 glm::ivec2 get_shape();
496
506 size_t get_sizeof() const;
507
530 float get_value_bicubic_at(int i, int j, float u, float v) const;
531
553 float get_value_bilinear_at(int i, int j, float u, float v) const;
554
570 float get_value_nearest(float x, float y, glm::vec4 bbox);
571
577 std::vector<float> get_vector() const;
578
588 void infos(const std::string &msg = "") const;
589
602 int linear_index(int i, int j) const;
603
615 glm::ivec2 linear_index_reverse(int k) const;
616
625 float max() const;
626
635 float mean() const;
636
641 float median() const;
642
651 float min() const;
652
661 void normalize();
662
677 glm::vec2 normalization_coeff(float vmin = 0.f, float vmax = 1.f) const;
678
686 void print() const;
687
698 float ptp() const;
699
706 glm::vec2 range() const;
707
725 glm::vec2 range_percentile(float p_low,
726 float p_high,
727 size_t bins = 1024) const;
728
732 Array remapped() const;
733
751 Array resample_to_shape(glm::ivec2 new_shape) const;
752
771 Array resample_to_shape_bicubic(glm::ivec2 new_shape) const;
772 Array resample_to_shape_bilinear(glm::ivec2 new_shape) const;
773
786 Array resample_to_shape_nearest(glm::ivec2 new_shape) const;
787
798 std::vector<float> row_to_vector(int i);
799
805 void set_shape(glm::ivec2 new_shape);
806
819 void set_slice(glm::ivec4 idx, float value);
820 void set_slice(glm::ivec4 idx, const Array &array);
821
831 int size() const;
832
837 float std() const;
838
848 float sum() const;
849
870 cv::Mat to_cv_mat();
871
884 void to_exr(const std::string &fname) const;
885
896 void to_file(const std::string &fname) const;
897
906 void to_numpy(const std::string &fname) const;
907
927 void to_png(const std::string &fname,
928 int cmap,
929 bool hillshading = false,
930 int depth = CV_8U) const;
931
948 void to_png_grayscale(const std::string &fname,
949 int depth = CV_8U,
950 float vmin = 0.f,
951 float vmax = 0.f) const;
952
965 void to_tiff(const std::string &fname) const;
966
980 void to_raw_16bit(const std::string &fname) const;
981
990 std::vector<float> unique_values() const;
991};
992
993// ==========================================================================
994// Functions
995// ==========================================================================
996
1002size_t count_non_zero(const Array &array);
1003
1009size_t count_zero(const Array &array);
1010
1035Array cv_mat_to_array(const cv::Mat &mat,
1036 bool remap_values = true,
1037 bool flip_j = false);
1038
1039// ==========================================================================
1040// per cell operations wrapper(s)
1041// ==========================================================================
1042
1044template <typename Fn> inline void for_each_cell(Array &a, Fn &&fn)
1045{
1046 const int w = a.shape.x;
1047 const int h = a.shape.y;
1048
1049 for (int j = 0; j < h; ++j)
1050 for (int i = 0; i < w; ++i)
1051 fn(i, j, a(i, j));
1052}
1053
1055template <typename Fn> inline void for_each_cell(const Array &a, Fn &&fn)
1056{
1057 const int w = a.shape.x;
1058 const int h = a.shape.y;
1059
1060 for (int j = 0; j < h; ++j)
1061 for (int i = 0; i < w; ++i)
1062 fn(i, j, a(i, j));
1063}
1064
1066template <typename T, typename Fn, typename Reduce>
1067inline T reduce_cells(const Array &a, T init, Fn &&fn, Reduce &&reduce)
1068{
1069 T acc = init;
1070
1071 for_each_cell(a,
1072 [&](int i, int j, float v) { acc = reduce(acc, fn(i, j, v)); });
1073
1074 return acc;
1075}
1076
1078template <typename T, typename Fn, typename Reduce>
1079inline T reduce_cells(const Array &a, T init, Reduce &&reduce)
1080{
1081 T acc = init;
1082
1083 for_each_cell(a, [&](int i, int j, float v) { acc = reduce(acc, a(i, j)); });
1084
1085 return acc;
1086}
1087
1088} // namespace hmap
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