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) const;
352
353 Array to_array() const;
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
611 void colorize(Heightmap &color_level,
612 float vmin,
613 float vmax,
614 std::vector<float> positions,
615 std::vector<std::vector<float>> colormap_colors,
616 Heightmap *p_alpha = nullptr,
617 bool reverse = false,
618 Heightmap *p_noise = nullptr);
619
636
645 HeightmapRGBA &rgba2,
646 bool use_sqrt_avg);
647
655 std::vector<HeightmapRGBA *> rgba_plist,
656 bool use_sqrt_avg);
657
661 void normalize();
662
668 std::vector<uint8_t> to_img_8bit(Vec2<int> shape_img = {0, 0},
669 bool flip_y = true) const;
670};
671
702HeightmapRGBA mix_normal_map_rgba(HeightmapRGBA &nmap_base,
703 HeightmapRGBA &nmap_detail,
704 float detail_scaling = 1.f,
705 NormalMapBlendingMethod blending_method =
707
728[[deprecated]] void fill(
729 Heightmap &h,
730 Heightmap *p_noise_x,
731 Heightmap *p_noise_y,
732 std::function<
733 Array(Vec2<int>, Vec4<float>, Array *p_noise_x, Array *p_noise_y)>
734 nullary_op);
735
736[[deprecated]] void fill(Heightmap &h,
737 Heightmap &hin,
738 Heightmap *p_noise_x,
739 Heightmap *p_noise_y,
740 std::function<Array(hmap::Array &,
741 Vec2<int>,
743 hmap::Array *,
744 hmap::Array *)> nullary_op);
745
746[[deprecated]] void fill(Heightmap &h,
747 Heightmap *p_noise_x,
748 Heightmap *p_noise_y,
749 Heightmap *p_stretching,
750 std::function<Array(Vec2<int>,
752 hmap::Array *,
753 hmap::Array *,
754 hmap::Array *)> nullary_op);
755
756// shape, shift, scale and noise
757[[deprecated]] void fill(
758 Heightmap &h,
759 Heightmap *p_noise,
760 std::function<Array(Vec2<int>, Vec4<float>, Array *p_noise)> nullary_op);
761
762// shape, shift and scale
763[[deprecated]] void fill(
764 Heightmap &h,
765 std::function<Array(Vec2<int>, Vec4<float>)> nullary_op);
766
767// shape only
768[[deprecated]] void fill(Heightmap &h,
769 std::function<Array(Vec2<int>)> nullary_op);
770
771[[deprecated]] void transform(Heightmap &h,
772 std::function<void(Array &)> unary_op);
773
774[[deprecated]] void transform(
775 Heightmap &h,
776 std::function<void(Array &, Vec4<float>)> unary_op);
777
778[[deprecated]] void transform(
779 Heightmap &h,
780 Heightmap *p_noise_x,
781 std::function<void(Array &, Vec4<float>, Array *)> unary_op);
782
783[[deprecated]] void transform(
784 Heightmap &h,
785 Heightmap *p_noise_x,
786 Heightmap *p_noise_y,
787 std::function<void(Array &, Vec4<float>, Array *, Array *)> unary_op);
788
789[[deprecated]] void transform(
790 Heightmap &h,
791 std::function<void(Array &, Vec2<float>, Vec2<float>)> unary_op);
792
793// input array and mask
794[[deprecated]] void transform(Heightmap &h,
795 Heightmap *p_mask,
796 std::function<void(Array &, Array *)> unary_op);
797
798// for erosion
799[[deprecated]] void transform(
800 Heightmap &h,
801 hmap::Heightmap *p_1,
802 hmap::Heightmap *p_2,
803 hmap::Heightmap *p_3,
804 hmap::Heightmap *p_4,
805 hmap::Heightmap *p_5,
806 std::function<void(Array &, Array *, Array *, Array *, Array *, Array *)>
807 unary_op);
808
809[[deprecated]] void transform(
810 Heightmap &h,
811 hmap::Heightmap *p_1,
812 hmap::Heightmap *p_2,
813 hmap::Heightmap *p_3,
814 std::function<void(Array &, Array *, Array *, Array *)> unary_op);
815
816[[deprecated]] void transform(
817 Heightmap &h,
818 hmap::Heightmap *p_1,
819 hmap::Heightmap *p_2,
820 std::function<void(Array &, Array *, Array *)> unary_op);
821
822[[deprecated]] void transform(Heightmap &h1,
823 Heightmap &h2,
824 std::function<void(Array &, Array &)> binary_op);
825
826[[deprecated]] void transform(
827 Heightmap &h1,
828 Heightmap &h2,
829 std::function<void(Array &, Array &, Vec4<float>)> binary_op);
830
831[[deprecated]] void transform(
832 Heightmap &h1,
833 Heightmap &h2,
834 Heightmap &h3,
835 std::function<void(Array &, Array &, Array &)> ternary_op);
836
837[[deprecated]] void transform(
838 Heightmap &h1,
839 Heightmap &h2,
840 Heightmap &h3,
841 std::function<void(Array &, Array &, Array &, Vec4<float>)> ternary_op);
842
843// with returned array
844[[deprecated]] void transform(Heightmap &h_out, // output
845 Heightmap &h1, // in 1
846 std::function<Array(Array &)> unary_op);
847
848[[deprecated]] void transform(Heightmap &h_out, // output
849 Heightmap &h1, // in 1
850 Heightmap &h2, // in 2
851 std::function<Array(Array &, Array &)> binary_op);
852
853[[deprecated]] void transform(
854 Heightmap &h1,
855 Heightmap &h2,
856 Heightmap &h3,
857 Heightmap &h4,
858 std::function<void(Array &, Array &, Array &, Array &)> ternary_op);
859
860[[deprecated]] void transform(
861 Heightmap &h1,
862 Heightmap &h2,
863 Heightmap &h3,
864 Heightmap &h4,
865 Heightmap &h5,
866 Heightmap &h6,
867 std::function<void(Array &, Array &, Array &, Array &, Array &, Array &)>
868 op);
869
882void transform(std::vector<Heightmap *> p_hmaps,
883 std::function<void(const std::vector<Array *>,
884 const hmap::Vec2<int>,
885 const hmap::Vec4<float>)> op,
887
888void transform(std::vector<Heightmap *> p_hmaps,
889 std::function<void(const std::vector<Array *>)> op,
891
892} // 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
std::vector< uint8_t > to_grayscale_image_8bit()
Definition heightmap_base.cpp:405
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
Array to_array() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition heightmap_base.cpp:365
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:526
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:573
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:442
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:479
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:226
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:323
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:452
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:90
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:226
Vec2< int > shape
Shape.
Definition heightmap.hpp:527
void to_png(const std::string &fname, int depth=CV_8U)
Export the RGB heightmap to a 8bit png file.
Definition heightmap_rgba.cpp:466
void set_alpha(Heightmap new_alpha)
Set the alpha channel.
Definition heightmap_rgba.cpp:77
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:214
std::vector< uint8_t > to_img_8bit(Vec2< int > shape_img={0, 0}, bool flip_y=true) const
Convert the RGB heightmap to a 8bit RGB image.
Definition heightmap_rgba.cpp:474
HeightmapRGBA()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition heightmap_rgba.cpp:22
Heightmap luminance()
Computes the luminance of an RGBA height map.
Definition heightmap_rgba.cpp:52
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