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:
39
44 std::vector<float> vector;
45
59 Array();
60
62
63 Array(Vec2<int> shape, float value);
64
65 Array(const std::string &filename);
66
67 //----------------------------------------
68 // overload
69 //----------------------------------------
70
77 Array &operator=(const float value);
78
86 Array &operator*=(const float value);
87
88 Array &operator*=(const Array &array);
89
96 Array &operator/=(const float value);
97
98 Array &operator/=(const Array &array);
99
106 Array &operator+=(const float value);
107
108 Array &operator+=(const Array &array);
109
117 Array &operator-=(const float value);
118
119 Array &operator-=(const Array &array);
120
127 Array operator*(const float value) const;
128
136 Array operator*(const Array &array) const;
137
145 friend Array operator*(const float value, const Array &array);
146
153 Array operator/(const float value) const;
154
162 Array operator/(const Array &array) const;
163
171 friend Array operator/(const float value, const Array &array);
172
179 Array operator+(const float value) const;
180
188 Array operator+(const Array &array) const;
189
197 friend Array operator+(const float value, const Array &array);
198
204 Array operator-() const;
205
212 Array operator-(const float value) const;
213
221 Array operator-(const Array &array) const;
222
230 friend const Array operator-(const float value, const Array &array);
231
240 float &operator()(int i, int j)
241 {
242 return this->vector[j * this->shape.x + i];
243 }
244
253 const float &operator()(int i, int j) const
254 {
255 return this->vector[j * this->shape.x + i];
256 }
257
258 //----------------------------------------
259 // methods
260 //----------------------------------------
261
268 std::vector<float> col_to_vector(int j);
269
275 int count_non_zero();
276
287 void depose_amount_bilinear_at(int i, int j, float u, float v, float amount);
288
290 int j,
291 float u,
292 float v,
293 int ir,
294 float amount);
295
307 void depose_amount_kernel_at(int i, int j, const Array &kernel, float amount);
308
313 void dump(const std::string &fname = "out.png") const;
314
324 Array extract_slice(Vec4<int> idx) const;
325 Array extract_slice(int i1, int i2, int j1, int j2) const;
326
337 void from_file(const std::string &fname);
338
347 void from_numpy(const std::string &fname);
348
369 float get_gradient_x_at(int i, int j) const;
370
391 float get_gradient_y_at(int i, int j) const;
392
415 float get_gradient_x_bilinear_at(int i, int j, float u, float v) const;
416
439 float get_gradient_y_bilinear_at(int i, int j, float u, float v) const;
440
452 Vec3<float> get_normal_at(int i, int j) const;
453
460
470 size_t get_sizeof() const;
471
494 float get_value_bicubic_at(int i, int j, float u, float v) const;
495
517 float get_value_bilinear_at(int i, int j, float u, float v) const;
518
534 float get_value_nearest(float x, float y, Vec4<float> bbox);
535
541 std::vector<float> get_vector() const;
542
552 void infos(std::string msg = "") const;
553
566 int linear_index(int i, int j) const;
567
579 Vec2<int> linear_index_reverse(int k) const;
580
589 float max() const;
590
599 float mean() const;
600
609 float min() const;
610
619 void normalize();
620
635 Vec2<float> normalization_coeff(float vmin = 0.f, float vmax = 1.f) const;
636
644 void print() const;
645
656 float ptp() const;
657
675 Array resample_to_shape(Vec2<int> new_shape) const;
676
696
698
712
723 std::vector<float> row_to_vector(int i);
724
730 void set_shape(Vec2<int> new_shape);
731
744 void set_slice(Vec4<int> idx, float value);
745
746 void set_slice(Vec4<int> idx, const Array &array);
747
757 int size() const;
758
763 float std() const;
764
774 float sum() const;
775
796 cv::Mat to_cv_mat();
797
810 void to_exr(const std::string &fname) const;
811
822 void to_file(const std::string &fname) const;
823
832 void to_numpy(const std::string &fname) const;
833
853 void to_png(const std::string &fname,
854 int cmap,
855 bool hillshading = false,
856 int depth = CV_8U) const;
857
874 void to_png_grayscale(const std::string &fname, int depth = CV_8U) const;
875
888 void to_tiff(const std::string &fname) const;
889
903 void to_raw_16bit(const std::string &fname) const;
904
913 std::vector<float> unique_values();
914};
915
940Array cv_mat_to_array(const cv::Mat &mat, bool remap_values = true);
941
942} // 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:18
void dump(const std::string &fname="out.png") const
Debug tool, dump some infos and generate an output file (16bits grayscale)
Definition methods.cpp:93
Vec3< float > get_normal_at(int i, int j) const
Calculates the surface normal vector at the index (i, j).
Definition methods.cpp:155
int count_non_zero()
Return the number of non-zero elements in the array.
Definition methods.cpp:30
void set_slice(Vec4< int > idx, float value)
Set the value of a slice defined by indices {i1, i2, j1, j2} to a new value.
Definition methods.cpp:319
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:208
int size() const
Return the total number of elements in the array.
Definition methods.cpp:333
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:117
Array resample_to_shape(Vec2< int > new_shape) const
Return a resampled array of shape new_shape using bilinear interpolation.
Definition methods.cpp:282
float mean() const
Return the mean value of the elements in the array.
Definition methods.cpp:257
Vec2< int > linear_index_reverse(int k) const
Convert a linear index to its corresponding (i, j) cell coordinates.
Definition methods.cpp:244
Array & operator=(const float value)
Overloads the assignment operator for scalar assignment.
Definition array.cpp:51
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:239
friend Array operator+(const float value, const Array &array)
Overloads the addition operator for scalar addition.
Definition array.cpp:224
void infos(std::string msg="") const
Display various information about the array.
Definition io.cpp:58
void to_numpy(const std::string &fname) const
Export the array to a numpy binary file.
Definition io.cpp:112
Vec2< int > shape
The shape of the array {ni, nj}.
Definition array.hpp:38
Array & operator*=(const float value)
Overloads the multiplication-assignment operator for scalar multiplication.
Definition array.cpp:57
Array & operator+=(const float value)
Overloads the addition-assignment operator for scalar addition.
Definition array.cpp:95
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:253
float min() const
Return the value of the smallest element in the array.
Definition methods.cpp:262
Array extract_slice(Vec4< int > idx) const
Extracts a subarray defined by the slice indices {i1, i2, j1, j2} from the original array,...
Definition methods.cpp:100
friend Array operator*(const float value, const Array &array)
Overloads the multiplication operator for scalar multiplication.
Definition array.cpp:156
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:141
void from_numpy(const std::string &fname)
Import array data from a numpy binary file.
Definition io.cpp:29
Array operator-() const
Overloads the unary minus operator.
Definition array.cpp:235
std::vector< float > get_vector() const
Retrieves the underlying data vector.
Definition array.cpp:40
float max() const
Return the value of the greatest element in the array.
Definition methods.cpp:252
void to_file(const std::string &fname) const
Export the array to a raw binary file.
Definition io.cpp:100
Array resample_to_shape_bilinear(Vec2< int > new_shape) const
Definition methods.cpp:295
void to_tiff(const std::string &fname) const
Export the array as a TIFF image file.
Definition io.cpp:153
Vec2< float > 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:174
void print() const
Print the array values to the standard output (stdout).
Definition io.cpp:72
float sum() const
Return the sum of all array values.
Definition methods.cpp:346
size_t get_sizeof() const
Retrieves the number of bytes occupied by the array data.
Definition methods.cpp:169
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:127
std::vector< float > row_to_vector(int i)
Return a row i as a std::vector<float>.
Definition methods.cpp:311
std::vector< float > col_to_vector(int j)
Extracts a column 'j' as a std::vector.
Definition methods.cpp:22
void depose_amount_kernel_bilinear_at(int i, int j, float u, float v, int ir, float amount)
Definition methods.cpp:50
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:76
void set_shape(Vec2< int > new_shape)
Sets a new shape for the array.
Definition array.cpp:45
Array()
Constructs a new Array object.
Definition array.cpp:15
float get_value_nearest(float x, float y, Vec4< float > bbox)
Retrieves the nearest value at the location (x, y) within a bounded domain.
Definition methods.cpp:230
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:122
void to_exr(const std::string &fname) const
Export the array as an OpenEXR image file.
Definition io.cpp:85
Array & operator-=(const float value)
Overloads the subtraction-assignment operator for scalar subtraction.
Definition array.cpp:114
float std() const
Return the standard deviation of all array values.
Definition methods.cpp:338
friend Array operator/(const float value, const Array &array)
Overloads the division operator for scalar division.
Definition array.cpp:190
void normalize()
Normalize array values so that the sum of all elements is equal to 1.
Definition methods.cpp:267
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:148
Array & operator/=(const float value)
Overloads the division-assignment operator for scalar division.
Definition array.cpp:76
float ptp() const
Return the peak-to-peak amplitude (i.e., the difference between the maximum and minimum values) of th...
Definition methods.cpp:277
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:122
std::vector< float > unique_values()
Return the unique elements of the array.
Definition methods.cpp:351
Vec2< int > get_shape()
Retrieves the shape of the array.
Definition array.cpp:35
Array resample_to_shape_nearest(Vec2< int > new_shape) const
Return a resampled array of shape new_shape using nearest neighbor interpolation.
Definition methods.cpp:303
void to_png_grayscale(const std::string &fname, int depth=CV_8U) const
Export the array as a grayscale PNG image file with specified bit depth.
Definition io.cpp:136
float & operator()(int i, int j)
Overloads the function call operator to access the array value at index (i, j).
Definition array.hpp:240
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:186
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:38
Array resample_to_shape_bicubic(Vec2< int > new_shape) const
Return a resampled array of shape new_shape using bicubic interpolation.
Definition methods.cpp:287
std::vector< float > vector
The underlying data storage, a vector of size shape.x * shape.y.
Definition array.hpp:44
Predefined color mapping schemes for data visualization, including perceptual uniform (Viridis,...
Definition algebra.hpp:28
Array cv_mat_to_array(const cv::Mat &mat, bool remap_values=true)
Converts an OpenCV cv::Mat to a 2D Array with optional value scaling to [0, 1].
Definition opencv_wrapper.cpp:26
Vec2 class for basic manipulation of 2D vectors.
Definition algebra.hpp:40
T x
Definition algebra.hpp:41
Vec3 class for basic manipulation of 3D vectors.
Definition algebra.hpp:281
Vec4 class for basic manipulation of 4D vectors.
Definition algebra.hpp:564