libtcod
Loading...
Searching...
No Matches
heightmap.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2025, Jice and the libtcod contributors.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
34#pragma once
35#ifndef TCOD_HEIGHTMAP_HPP_
36#define TCOD_HEIGHTMAP_HPP_
37
38#include <stddef.h>
39
40#include <vector>
41
42#include "heightmap.h"
43#include "noise.hpp"
44
45// clang-format off
54class TCODLIB_API TCODHeightMap {
55 public:
56 TCODHeightMap() = default;
57
81 TCODHeightMap(int width, int height) : w{width}, h{height}, values{new float[width * height]{}} {}
82
83 // clang-format on
84
85 TCODHeightMap(const TCODHeightMap& rhs) : TCODHeightMap{rhs.w, rhs.h} { *this = rhs; }
86 TCODHeightMap& operator=(const TCODHeightMap& rhs) {
87 if (w == rhs.w && h == rhs.h) {
88 for (ptrdiff_t i = 0; i < w * h; ++i) values[i] = rhs.values[i];
89 } else {
90 *this = TCODHeightMap(rhs);
91 }
92 return *this;
93 }
94 TCODHeightMap(TCODHeightMap&& rhs) { swap(rhs); }
95 TCODHeightMap& operator=(TCODHeightMap&& rhs) {
96 swap(rhs);
97 return *this;
98 }
99
100 ~TCODHeightMap() { delete[] values; }
101
102 void swap(TCODHeightMap& rhs) noexcept {
103 std::swap(w, rhs.w);
104 std::swap(h, rhs.h);
105 std::swap(values, rhs.values);
106 }
107 // clang-format off
126 inline void setValue(int x, int y, float v) {
127 values[w * y + x] = v;
128 }
129
140 void add(float f);
141
152 void scale(float f);
153
163 void clear(); // resets all values to 0.0
164
176 void clamp(float min, float max);
177
189 [[deprecated("Copy by value instead: 'x = y;'")]]
190 void copy(const TCODHeightMap *source) {
191 if (source) *this = *source;
192 }
193
208 void normalize(float newMin=0.0f, float newMax=1.0f); // scales the values to the range [newMin;newMax]
209
223 void lerp(const TCODHeightMap *a, const TCODHeightMap *b,float coef);
224
236 void add(const TCODHeightMap *a, const TCODHeightMap *b);
237
249 void multiply(const TCODHeightMap *a, const TCODHeightMap *b);
250
269 void addHill(float x, float y, float radius, float height); // adds a hill (half sphere) at given position
270
287 void digHill(float hx, float hy, float h_radius, float height);
288
303 void rainErosion(int nbDrops,float erosionCoef,float sedimentationCoef,TCODRandom *rnd);
304
334 void kernelTransform(int kernelSize, const int *dx, const int *dy, const float *weight, float minLevel,float maxLevel);
335
351 void addVoronoi(int nbPoints, int nbCoef, const float *coef,TCODRandom *rnd);
352
369 void addFbm(TCODNoise *noise,float mul_x, float mul_y, float add_x, float add_y, float octaves, float delta, float scale);
370
380 void scaleFbm(TCODNoise *noise,float mul_x, float mul_y, float add_x, float add_y, float octaves, float delta, float scale);
381
399 void digBezier(int px[4], int py[4], float startRadius, float startDepth, float endRadius, float endDepth);
400
417 inline float getValue(int x, int y) const {
418 return values[w * y + x];
419 }
420
434 float getInterpolatedValue(float x, float y) const;
435
449 float getSlope(int x, int y) const; // returns the slope in radian between 0 and PI/2
450
466 void getNormal(float x, float y,float n[3], float waterLevel=0.0f) const; // returns the surface normal or (0,0,1) if beyond water level.
467
479 int countCells(float min,float max) const;
480
492 bool hasLandOnBorder(float waterLevel) const;
493
505 void getMinMax(float *min, float *max) const;
506
507// void heatErosion(int nbPass,float minSlope,float erosionCoef,float sedimentationCoef,TCODRandom *rnd);
523 void midPointDisplacement(TCODRandom *rnd = NULL, float roughness=0.45f);
524 void islandify(float seaLevel,TCODRandom *rnd); // lowers the terrain near the heightmap borders
525 // TODO : checks island connectivity with floodfill
526
527 // clang-format on
528
529 int w{};
530 int h{};
531 float* values{};
532
533 private:
534};
535
536#endif // TCOD_HEIGHTMAP_HPP_
Definition heightmap.hpp:54
void scaleFbm(TCODNoise *noise, float mul_x, float mul_y, float add_x, float add_y, float octaves, float delta, float scale)
This function works exactly as the previous one, but it multiplies the resulting value instead of add...
float getSlope(int x, int y) const
This function returns the slope between 0 and PI/2 at given coordinates.
void addFbm(TCODNoise *noise, float mul_x, float mul_y, float add_x, float add_y, float octaves, float delta, float scale)
This function adds values from a simplex fbm function to the map.
float getInterpolatedValue(float x, float y) const
This function returns the interpolated height at non integer coordinates.
void addHill(float x, float y, float radius, float height)
This function adds a hill (a half spheroid) at given position.
void digBezier(int px[4], int py[4], float startRadius, float startDepth, float endRadius, float endDepth)
This function carve a path along a cubic Bezier curve using the digHill function.
void scale(float f)
void midPointDisplacement(TCODRandom *rnd=NULL, float roughness=0.45f)
This algorithm generates a realistic fractal heightmap using the diamond-square (or random midpoint d...
void lerp(const TCODHeightMap *a, const TCODHeightMap *b, float coef)
TCODHeightMap(int width, int height)
As with other modules, you have to create a heightmap object first : Note that whereas most other mod...
Definition heightmap.hpp:81
void setValue(int x, int y, float v)
Once the heightmap has been created, you can do some basic operations on the values inside it....
Definition heightmap.hpp:126
void rainErosion(int nbDrops, float erosionCoef, float sedimentationCoef, TCODRandom *rnd)
This function simulates the effect of rain drops on the terrain, resulting in erosion patterns.
bool hasLandOnBorder(float waterLevel) const
This function checks if the cells on the map border are below a certain height.
float getValue(int x, int y) const
This function returns the height value of a map cell.
Definition heightmap.hpp:417
void multiply(const TCODHeightMap *a, const TCODHeightMap *b)
void digHill(float hx, float hy, float h_radius, float height)
This function takes the highest value (if height > 0) or the lowest (if height < 0) between the map a...
void kernelTransform(int kernelSize, const int *dx, const int *dy, const float *weight, float minLevel, float maxLevel)
This function allows you to apply a generic transformation on the map, so that each resulting cell va...
void getMinMax(float *min, float *max) const
This function calculates the min and max of all values inside the map.
void getNormal(float x, float y, float n[3], float waterLevel=0.0f) const
This function returns the map normal at given coordinates.
void add(float f)
void addVoronoi(int nbPoints, int nbCoef, const float *coef, TCODRandom *rnd)
This function adds values from a Voronoi diagram to the map.
void clamp(float min, float max)
int countCells(float min, float max) const
This function returns the number of map cells which value is between min and max.
void add(const TCODHeightMap *a, const TCODHeightMap *b)
void copy(const TCODHeightMap *source)
Definition heightmap.hpp:190
void normalize(float newMin=0.0f, float newMax=1.0f)
void TCODHeightMap::normalize() void TCODHeightMap::normalize(float min) void TCODHeightMap::normaliz...
Usage example: 1D noise : the variation of a torch intensity 2D fbm : heightfield generation or cloud...
Definition noise.hpp:79
Definition mersenne.hpp:94
Terrain heightmap module.
Texture noise generator module.