HighMap library (C++)
Loading...
Searching...
No Matches
heightmap.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
15#pragma once
16#include <functional>
17
18#include "highmap/array.hpp"
19#include "highmap/export.hpp"
20
21namespace hmap
22{
23
32
33static std::map<std::string, int> normal_map_blending_method_as_string = {
34 {"Linear", NMAP_LINEAR},
35 {"Partial derivative", NMAP_DERIVATIVE},
36 {"Unreal Developer Network", NMAP_UDN},
37 {"Unity", NMAP_UNITY},
38 {"Whiteout", NMAP_WHITEOUT},
39};
40
47
48static std::map<std::string, int> transform_mode_as_string = {
49 {"Distributed", DISTRIBUTED},
50 {"Sequential", SEQUENTIAL},
51 {"Single array", SINGLE_ARRAY},
52};
53
54// --- forward declarations
55class HeightmapRGBA;
56HeightmapRGBA mix_heightmap_rgba(HeightmapRGBA &rgba1,
57 HeightmapRGBA &rgba2,
58 bool use_sqrt_avg = true);
59HeightmapRGBA mix_heightmap_rgba(std::vector<HeightmapRGBA *> p_rgba_list,
60 bool use_sqrt_avg = true);
61
62// --- classes
63
68class Tile : public Array
69{
70public:
77
84
89
97
98 Tile();
99
105 void operator=(const Array &array);
106
113 void from_array_interp(Array &array);
114
121 void from_array_interp_bicubic(Array &array);
122
130
137 void from_array_interp_nearest(Array &array);
138
142 void infos() const;
143};
144
150{
151public:
156
161
165 float overlap = 0.f;
166
170 std::vector<Tile> tiles = {};
171
173 float overlap);
174
177 float overlap,
178 float fill_value);
179
181
183
184 Heightmap();
185
186 //----------------------------------------
187 // accessors
188 //----------------------------------------
189
190 //----------------------------------------
191 // methods
192 //----------------------------------------
193
199 size_t get_ntiles() const;
200
208 int get_tile_index(int i, int j) const;
209
210 float get_value_bilinear(float x, float y) const;
211
212 float get_value_nearest(float x, float y) const;
213
219 void set_overlap(float new_overlap);
220
226 void set_shape(Vec2<int> new_shape);
227
235 void set_sto(Vec2<int> new_shape, Vec2<int> new_tiling, float new_overlap);
236
242 void set_tiling(Vec2<int> new_tiling);
243
257 void from_array_interp(Array &array);
258
265 void from_array_interp_bicubic(Array &array);
266
274
281 void from_array_interp_nearest(Array &array);
282
286 void infos();
287
291 void inverse();
292
298 float max();
299
305 float mean();
306
312 float min();
313
326 void remap(float vmin = 0.f, float vmax = 1.f);
327
328 void remap(float vmin,
329 float vmax,
330 float from_min,
331 float from_max);
332
337
343 float sum();
344
351 Array to_array(Vec2<int> shape_export);
352
353 Array to_array();
354
361 std::vector<uint16_t> to_grayscale_image_16bit();
362
363 std::vector<uint16_t> to_grayscale_image_16bit_multithread();
364
365 std::vector<uint8_t> to_grayscale_image_8bit();
366
372 std::vector<float> unique_values();
373
378};
379
385{
398 std::vector<Heightmap> rgb;
399
403 Vec2<int> shape = {0, 0};
404
412
413 HeightmapRGB();
414
422 void set_sto(Vec2<int> new_shape, Vec2<int> new_tiling, float new_overlap);
423
433 void colorize(Heightmap &h,
434 float vmin,
435 float vmax,
436 int cmap,
437 bool reverse = false);
438
448 void colorize(Heightmap &h,
449 float vmin,
450 float vmax,
451 std::vector<std::vector<float>> colormap_colors,
452 bool reverse = false);
453
457 void normalize();
458
464 std::vector<uint8_t> to_img_8bit(Vec2<int> shape_img = {0, 0});
465
470 void to_png(const std::string &fname, int depth = CV_8U);
471
480 HeightmapRGB &rgb2,
481 Heightmap &t);
482
484 HeightmapRGB &rgb2,
485 float t);
486
495 HeightmapRGB &rgb2,
496 Heightmap &t);
497
499 HeightmapRGB &rgb2,
500 float t);
501};
502
518{
522 std::vector<Heightmap> rgba;
523
527 Vec2<int> shape = {0, 0};
528
537
539 Vec2<int> tiling,
540 float overlap,
541 Array array_r,
542 Array array_g,
543 Array array_b,
544 Array array_a);
545
546 HeightmapRGBA(Vec2<int> shape, Vec2<int> tiling, float overlap);
547
548 HeightmapRGBA();
549
554 void set_alpha(Heightmap new_alpha);
555
556 void set_alpha(float new_alpha);
557
565 void set_sto(Vec2<int> new_shape, Vec2<int> new_tiling, float new_overlap);
566
571 void to_png(const std::string &fname, int depth = CV_8U);
572
584 void colorize(Heightmap &color_level,
585 float vmin,
586 float vmax,
587 int cmap,
588 Heightmap *p_alpha = nullptr,
589 bool reverse = false,
590 Heightmap *p_noise = nullptr);
591
603 void colorize(Heightmap &color_level,
604 float vmin,
605 float vmax,
606 std::vector<std::vector<float>> colormap_colors,
607 Heightmap *p_alpha = nullptr,
608 bool reverse = false,
609 Heightmap *p_noise = nullptr);
610
627
636 HeightmapRGBA &rgba2,
637 bool use_sqrt_avg);
638
646 std::vector<HeightmapRGBA *> rgba_plist,
647 bool use_sqrt_avg);
648
652 void normalize();
653
659 std::vector<uint8_t> to_img_8bit(Vec2<int> shape_img = {0, 0});
660};
661
692HeightmapRGBA mix_normal_map_rgba(HeightmapRGBA &nmap_base,
693 HeightmapRGBA &nmap_detail,
694 float detail_scaling = 1.f,
695 NormalMapBlendingMethod blending_method =
697
718[[deprecated]] void fill(
719 Heightmap &h,
720 Heightmap *p_noise_x,
721 Heightmap *p_noise_y,
722 std::function<
723 Array(Vec2<int>, Vec4<float>, Array *p_noise_x, Array *p_noise_y)>
724 nullary_op);
725
726[[deprecated]] void fill(Heightmap &h,
727 Heightmap &hin,
728 Heightmap *p_noise_x,
729 Heightmap *p_noise_y,
730 std::function<Array(hmap::Array &,
731 Vec2<int>,
733 hmap::Array *,
734 hmap::Array *)> nullary_op);
735
736[[deprecated]] void fill(Heightmap &h,
737 Heightmap *p_noise_x,
738 Heightmap *p_noise_y,
739 Heightmap *p_stretching,
740 std::function<Array(Vec2<int>,
742 hmap::Array *,
743 hmap::Array *,
744 hmap::Array *)> nullary_op);
745
746// shape, shift, scale and noise
747[[deprecated]] void fill(
748 Heightmap &h,
749 Heightmap *p_noise,
750 std::function<Array(Vec2<int>, Vec4<float>, Array *p_noise)> nullary_op);
751
752// shape, shift and scale
753[[deprecated]] void fill(
754 Heightmap &h,
755 std::function<Array(Vec2<int>, Vec4<float>)> nullary_op);
756
757// shape only
758[[deprecated]] void fill(Heightmap &h,
759 std::function<Array(Vec2<int>)> nullary_op);
760
761[[deprecated]] void transform(Heightmap &h,
762 std::function<void(Array &)> unary_op);
763
764[[deprecated]] void transform(
765 Heightmap &h,
766 std::function<void(Array &, Vec4<float>)> unary_op);
767
768[[deprecated]] void transform(
769 Heightmap &h,
770 Heightmap *p_noise_x,
771 std::function<void(Array &, Vec4<float>, Array *)> unary_op);
772
773[[deprecated]] void transform(
774 Heightmap &h,
775 Heightmap *p_noise_x,
776 Heightmap *p_noise_y,
777 std::function<void(Array &, Vec4<float>, Array *, Array *)> unary_op);
778
779[[deprecated]] void transform(
780 Heightmap &h,
781 std::function<void(Array &, Vec2<float>, Vec2<float>)> unary_op);
782
783// input array and mask
784[[deprecated]] void transform(Heightmap &h,
785 Heightmap *p_mask,
786 std::function<void(Array &, Array *)> unary_op);
787
788// for erosion
789[[deprecated]] void transform(
790 Heightmap &h,
791 hmap::Heightmap *p_1,
792 hmap::Heightmap *p_2,
793 hmap::Heightmap *p_3,
794 hmap::Heightmap *p_4,
795 hmap::Heightmap *p_5,
796 std::function<void(Array &, Array *, Array *, Array *, Array *, Array *)>
797 unary_op);
798
799[[deprecated]] void transform(
800 Heightmap &h,
801 hmap::Heightmap *p_1,
802 hmap::Heightmap *p_2,
803 hmap::Heightmap *p_3,
804 std::function<void(Array &, Array *, Array *, Array *)> unary_op);
805
806[[deprecated]] void transform(
807 Heightmap &h,
808 hmap::Heightmap *p_1,
809 hmap::Heightmap *p_2,
810 std::function<void(Array &, Array *, Array *)> unary_op);
811
812[[deprecated]] void transform(Heightmap &h1,
813 Heightmap &h2,
814 std::function<void(Array &, Array &)> binary_op);
815
816[[deprecated]] void transform(
817 Heightmap &h1,
818 Heightmap &h2,
819 std::function<void(Array &, Array &, Vec4<float>)> binary_op);
820
821[[deprecated]] void transform(
822 Heightmap &h1,
823 Heightmap &h2,
824 Heightmap &h3,
825 std::function<void(Array &, Array &, Array &)> ternary_op);
826
827[[deprecated]] void transform(
828 Heightmap &h1,
829 Heightmap &h2,
830 Heightmap &h3,
831 std::function<void(Array &, Array &, Array &, Vec4<float>)> ternary_op);
832
833// with returned array
834[[deprecated]] void transform(Heightmap &h_out, // output
835 Heightmap &h1, // in 1
836 std::function<Array(Array &)> unary_op);
837
838[[deprecated]] void transform(Heightmap &h_out, // output
839 Heightmap &h1, // in 1
840 Heightmap &h2, // in 2
841 std::function<Array(Array &, Array &)> binary_op);
842
843[[deprecated]] void transform(
844 Heightmap &h1,
845 Heightmap &h2,
846 Heightmap &h3,
847 Heightmap &h4,
848 std::function<void(Array &, Array &, Array &, Array &)> ternary_op);
849
850[[deprecated]] void transform(
851 Heightmap &h1,
852 Heightmap &h2,
853 Heightmap &h3,
854 Heightmap &h4,
855 Heightmap &h5,
856 Heightmap &h6,
857 std::function<void(Array &, Array &, Array &, Array &, Array &, Array &)>
858 op);
859
872void transform(std::vector<Heightmap *> p_hmaps,
873 std::function<void(const std::vector<Array *>,
874 const hmap::Vec2<int>,
875 const hmap::Vec4<float>)> op,
877
878void transform(std::vector<Heightmap *> p_hmaps,
879 std::function<void(const std::vector<Array *>)> op,
881
882} // namespace hmap
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Array class, helper to manipulate 2D float array with "(i, j)" indexing.
Definition array.hpp:32
Vec2< int > shape
The shape of the array {ni, nj}.
Definition array.hpp:38
HeightMap class, to manipulate heightmap (with contextual informations).
Definition heightmap.hpp:150
Vec2< int > tiling
Tiling setup (number of tiles in each direction).
Definition heightmap.hpp:160
Array to_array()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition heightmap_base.cpp:365
std::vector< uint8_t > to_grayscale_image_8bit()
Definition heightmap_base.cpp:406
void set_sto(Vec2< int > new_shape, Vec2< int > new_tiling, float new_overlap)
Set the shape / tiling / overlap in one pass.
Definition heightmap_base.cpp:84
std::vector< Tile > tiles
Tile storage.
Definition heightmap.hpp:170
float max()
Return the value of the greatest element in the heightmap data.
Definition heightmap_base.cpp:240
float mean()
Return the mean of the heightmap data.
Definition heightmap_base.cpp:310
float get_value_bilinear(float x, float y) const
Definition heightmap_base.cpp:148
size_t get_ntiles() const
Get the number of tiles.
Definition heightmap_base.cpp:62
void set_shape(Vec2< int > new_shape)
Set the heightmap shape.
Definition heightmap_base.cpp:78
float sum()
Return the sum of the heightmap data.
Definition heightmap_base.cpp:347
void update_tile_parameters()
Update tile parameters.
Definition heightmap_base.cpp:527
float overlap
Tile overlapping, in [0, 1[.
Definition heightmap.hpp:165
void set_overlap(float new_overlap)
Set the tile overlapping.
Definition heightmap_base.cpp:72
void inverse()
Inverse the heightmap values (max - values).
Definition heightmap_base.cpp:223
Vec2< int > shape
Heightmap global shape.
Definition heightmap.hpp:155
void from_array_interp(Array &array)
Fill tile values by interpolating (bilinear) values from another array.
Definition heightmap_base.cpp:104
int get_tile_index(int i, int j) const
Get the tile linear index.
Definition heightmap_base.cpp:67
void infos()
Print some informations about the object.
Definition heightmap_base.cpp:208
Heightmap()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition heightmap_base.cpp:57
std::vector< float > unique_values()
Returns the unique elements of the heightmap.
Definition heightmap_base.cpp:574
void from_array_interp_bicubic(Array &array)
Fill tile values by interpolating (bicubic) values from another array.
Definition heightmap_base.cpp:109
std::vector< uint16_t > to_grayscale_image_16bit()
Converts the heightmap to a 16-bit grayscale representation.
Definition heightmap_base.cpp:443
void from_array_interp_bilinear(Array &array)
Fill tile values by interpolating (bilinear) values from another array.
Definition heightmap_base.cpp:122
float get_value_nearest(float x, float y) const
Definition heightmap_base.cpp:186
void smooth_overlap_buffers()
Smooth the transitions between each tiles (when overlap > 0).
Definition heightmap_base.cpp:254
void set_tiling(Vec2< int > new_tiling)
Set the tiling setup.
Definition heightmap_base.cpp:98
void remap(float vmin=0.f, float vmax=1.f)
Remap heightmap elements from a starting range to a target range.
Definition heightmap_base.cpp:316
std::vector< uint16_t > to_grayscale_image_16bit_multithread()
Definition heightmap_base.cpp:480
float min()
Return the value of the smallest element in the heightmap data.
Definition heightmap_base.cpp:296
void from_array_interp_nearest(Array &array)
Fill tile values by interpolating (nearest neighbor) values from another array.
Definition heightmap_base.cpp:135
Tile class, to manipulate a restricted region of an heightmap (with contextual informations).
Definition heightmap.hpp:69
Tile()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tile_base.cpp:13
Vec2< float > shift
Tile shift in each direction, assuming the global domain is a unit square. For example,...
Definition heightmap.hpp:76
void from_array_interp_bicubic(Array &array)
Fill tile values by interpolating (bicubic) values from another array.
Definition tile_base.cpp:34
void infos() const
Print some informations about the object.
Definition tile_base.cpp:67
void from_array_interp(Array &array)
Fill tile values by interpolating (bilinear) values from another array.
Definition tile_base.cpp:29
Vec4< float > bbox
Tile bounding box {xmin, xmax, ymin, ymax}.
Definition heightmap.hpp:88
Vec2< float > scale
Scale of the tile in each direction, assuming the global domain is a unit square. For example,...
Definition heightmap.hpp:83
void from_array_interp_nearest(Array &array)
Fill tile values by interpolating (nearest) values from another array.
Definition tile_base.cpp:56
void operator=(const Array &array)
Assignment overloading (array).
Definition tile_base.cpp:24
void from_array_interp_bilinear(Array &array)
Fill tile values by interpolating (bilinear) values from another array.
Definition tile_base.cpp:45
Header file for asset export functionalities.
Definition algebra.hpp:28
HeightmapRGBA mix_heightmap_rgba(HeightmapRGBA &rgba1, HeightmapRGBA &rgba2, bool use_sqrt_avg=true)
Definition heightmap_rgba.cpp:205
void fill(Heightmap &h, Heightmap *p_noise_x, Heightmap *p_noise_y, std::function< Array(Vec2< int >, Vec4< float >, Array *p_noise_x, Array *p_noise_y)> nullary_op)
Fills the heightmap using the provided noise maps and operation.
HeightmapRGBA mix_normal_map_rgba(HeightmapRGBA &nmap_base, HeightmapRGBA &nmap_detail, float detail_scaling=1.f, NormalMapBlendingMethod blending_method=NormalMapBlendingMethod::NMAP_DERIVATIVE)
Mixes two normal maps in RGBA format to create a blended normal map.
Definition heightmap_rgba.cpp:302
void transform(Heightmap &h, std::function< void(Array &)> unary_op)
Definition heightmap_transform.cpp:177
TransformMode
Definition heightmap.hpp:42
@ SEQUENTIAL
Performed sequentially in a single thread.
Definition heightmap.hpp:44
@ DISTRIBUTED
Distributed across multiple processors or threads.
Definition heightmap.hpp:43
@ SINGLE_ARRAY
Transformation is applied to a single array of data.
Definition heightmap.hpp:45
NormalMapBlendingMethod
Definition heightmap.hpp:25
@ NMAP_UDN
Definition heightmap.hpp:28
@ NMAP_UNITY
Definition heightmap.hpp:29
@ NMAP_WHITEOUT
Definition heightmap.hpp:30
@ NMAP_DERIVATIVE
Definition heightmap.hpp:27
@ NMAP_LINEAR
Definition heightmap.hpp:26
HeightMap class, to manipulate a set of RGBA heightmap for heightmap texturing.
Definition heightmap.hpp:518
std::vector< Heightmap > rgba
RGBA component heightmap storage.
Definition heightmap.hpp:522
void normalize()
Normalize RGBA heightmaps amplitude.
Definition heightmap_rgba.cpp:431
void set_sto(Vec2< int > new_shape, Vec2< int > new_tiling, float new_overlap)
Set the shape / tiling / overlap in one pass.
Definition heightmap_rgba.cpp:89
friend HeightmapRGBA mix_heightmap_rgba(HeightmapRGBA &rgba1, HeightmapRGBA &rgba2, bool use_sqrt_avg)
Mix two RGBA heightmap using alpha compositing ("over").
Definition heightmap_rgba.cpp:205
Vec2< int > shape
Shape.
Definition heightmap.hpp:527
std::vector< uint8_t > to_img_8bit(Vec2< int > shape_img={0, 0})
Convert the RGB heightmap to a 8bit RGB image.
Definition heightmap_rgba.cpp:453
void to_png(const std::string &fname, int depth=CV_8U)
Export the RGB heightmap to a 8bit png file.
Definition heightmap_rgba.cpp:445
void set_alpha(Heightmap new_alpha)
Set the alpha channel.
Definition heightmap_rgba.cpp:76
void colorize(Heightmap &color_level, float vmin, float vmax, int cmap, Heightmap *p_alpha=nullptr, bool reverse=false, Heightmap *p_noise=nullptr)
Fill RGB heightmap components based on a colormap and an input reference heightmap.
Definition heightmap_rgba.cpp:193
HeightmapRGBA()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition heightmap_rgba.cpp:21
Heightmap luminance()
Computes the luminance of an RGBA height map.
Definition heightmap_rgba.cpp:51
HeightMap class, to manipulate a set of RGB heightmap for heightmap texturing.
Definition heightmap.hpp:385
Vec2< int > shape
Shape.
Definition heightmap.hpp:403
void normalize()
Normalize RGB heightmaps amplitude.
Definition heightmap_rgb.cpp:99
void to_png(const std::string &fname, int depth=CV_8U)
Export the RGB heightmap to a 16bit png file.
Definition heightmap_rgb.cpp:132
void colorize(Heightmap &h, float vmin, float vmax, int cmap, bool reverse=false)
Fill RGB heightmap components based on a colormap and an input reference heightmap.
Definition heightmap_rgb.cpp:89
HeightmapRGB()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition heightmap_rgb.cpp:20
std::vector< Heightmap > rgb
RGB component heightmap storage.
Definition heightmap.hpp:398
void set_sto(Vec2< int > new_shape, Vec2< int > new_tiling, float new_overlap)
Set the shape / tiling / overlap in one pass.
Definition heightmap_rgb.cpp:30
std::vector< uint8_t > to_img_8bit(Vec2< int > shape_img={0, 0})
Convert the RGB heightmap to a 8bit RGB image.
Definition heightmap_rgb.cpp:110
friend HeightmapRGB mix_heightmap_rgb_sqrt(HeightmapRGB &rgb1, HeightmapRGB &rgb2, Heightmap &t)
Mix two RGB heightmap using weighted quadratic averaging.
Definition heightmap_rgb.cpp:201
friend HeightmapRGB mix_heightmap_rgb(HeightmapRGB &rgb1, HeightmapRGB &rgb2, Heightmap &t)
Mix two RGB heightmap using linear interpolation.
Definition heightmap_rgb.cpp:142
Vec2 class for basic manipulation of 2D vectors.
Definition algebra.hpp:40
Vec4 class for basic manipulation of 4D vectors.
Definition algebra.hpp:564