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, bool flip_j = false);
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
269 void argmax(float &vmax, int &i, int &j) const;
270
277 std::vector<float> col_to_vector(int j);
278
284 int count_non_zero();
285
296 void depose_amount_bilinear_at(int i, int j, float u, float v, float amount);
297
299 int j,
300 float u,
301 float v,
302 int ir,
303 float amount);
304
316 void depose_amount_kernel_at(int i, int j, const Array &kernel, float amount);
317
322 void dump(const std::string &fname = "out.png") const;
323
327 void dump_histogram(const std::string &msg = "") const;
328
339 Array extract_slice(int i1, int i2, int j1, int j2) const;
340
351 void from_file(const std::string &fname);
352
361 void from_numpy(const std::string &fname);
362
383 float get_gradient_x_at(int i, int j) const;
384
405 float get_gradient_y_at(int i, int j) const;
406
429 float get_gradient_x_bilinear_at(int i, int j, float u, float v) const;
430
453 float get_gradient_y_bilinear_at(int i, int j, float u, float v) const;
454
466 Vec3<float> get_normal_at(int i, int j) const;
467
474
484 size_t get_sizeof() const;
485
508 float get_value_bicubic_at(int i, int j, float u, float v) const;
509
531 float get_value_bilinear_at(int i, int j, float u, float v) const;
532
548 float get_value_nearest(float x, float y, Vec4<float> bbox);
549
555 std::vector<float> get_vector() const;
556
566 void infos(std::string msg = "") const;
567
580 int linear_index(int i, int j) const;
581
593 Vec2<int> linear_index_reverse(int k) const;
594
603 float max() const;
604
613 float mean() const;
614
623 float min() const;
624
633 void normalize();
634
649 Vec2<float> normalization_coeff(float vmin = 0.f, float vmax = 1.f) const;
650
658 void print() const;
659
670 float ptp() const;
671
675 Array remapped() const;
676
695
715
717
731
742 std::vector<float> row_to_vector(int i);
743
750
763 void set_slice(Vec4<int> idx, float value);
764
765 void set_slice(Vec4<int> idx, const Array &array);
766
776 int size() const;
777
782 float std() const;
783
793 float sum() const;
794
815 cv::Mat to_cv_mat();
816
829 void to_exr(const std::string &fname) const;
830
841 void to_file(const std::string &fname) const;
842
851 void to_numpy(const std::string &fname) const;
852
872 void to_png(const std::string &fname,
873 int cmap,
874 bool hillshading = false,
875 int depth = CV_8U) const;
876
893 void to_png_grayscale(const std::string &fname, int depth = CV_8U) const;
894
907 void to_tiff(const std::string &fname) const;
908
922 void to_raw_16bit(const std::string &fname) const;
923
932 std::vector<float> unique_values();
933};
934
959Array cv_mat_to_array(const cv::Mat &mat,
960 bool remap_values = true,
961 bool flip_j = false);
962
963} // 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:116
Vec3< float > get_normal_at(int i, int j) const
Calculates the surface normal vector at the index (i, j).
Definition methods.cpp:184
int count_non_zero()
Return the number of non-zero elements in the array.
Definition methods.cpp:53
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:355
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:237
int size() const
Return the total number of elements in the array.
Definition methods.cpp:369
void dump_histogram(const std::string &msg="") const
Debug tool, dump array content as an ASCII histogram to the consol.
Definition methods.cpp:123
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:146
Array resample_to_shape(Vec2< int > new_shape) const
Return a resampled array of shape new_shape using bilinear interpolation.
Definition methods.cpp:318
float mean() const
Return the mean value of the elements in the array.
Definition methods.cpp:286
Vec2< int > linear_index_reverse(int k) const
Convert a linear index to its corresponding (i, j) cell coordinates.
Definition methods.cpp:273
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:268
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:59
void to_numpy(const std::string &fname) const
Export the array to a numpy binary file.
Definition io.cpp:113
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:291
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:129
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:170
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: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:281
void to_file(const std::string &fname) const
Export the array to a raw binary file.
Definition io.cpp:101
Array resample_to_shape_bilinear(Vec2< int > new_shape) const
Definition methods.cpp:331
void to_tiff(const std::string &fname) const
Export the array as a TIFF image file.
Definition io.cpp:154
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:203
void print() const
Print the array values to the standard output (stdout).
Definition io.cpp:73
float sum() const
Return the sum of all array values.
Definition methods.cpp:382
size_t get_sizeof() const
Retrieves the number of bytes occupied by the array data.
Definition methods.cpp:198
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:156
std::vector< float > row_to_vector(int i)
Return a row i as a std::vector<float>.
Definition methods.cpp:347
std::vector< float > col_to_vector(int j)
Extracts a column 'j' as a std::vector.
Definition methods.cpp:45
Array remapped() const
Return an array remapped to [0, 1].
Definition methods.cpp:311
void depose_amount_kernel_bilinear_at(int i, int j, float u, float v, int ir, float amount)
Definition methods.cpp:73
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:99
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
void argmax(float &vmax, int &i, int &j) const
Finds the maximum value in the array and its coordinates.
Definition methods.cpp:26
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:259
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:123
void to_exr(const std::string &fname) const
Export the array as an OpenEXR image file.
Definition io.cpp:86
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:374
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:296
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:149
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:306
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:151
std::vector< float > unique_values()
Return the unique elements of the array.
Definition methods.cpp:387
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:339
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:137
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:215
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:61
Array resample_to_shape_bicubic(Vec2< int > new_shape) const
Return a resampled array of shape new_shape using bicubic interpolation.
Definition methods.cpp:323
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, 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
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