HighMap library (C++)
Loading...
Searching...
No Matches
path.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
25#pragma once
26#include "highmap/boundary.hpp"
28
29namespace hmap
30{
31
49class Path : public Cloud
50{
51public:
56 bool closed;
57
63 Path(bool closed = false) : Cloud(), closed(closed){};
64
74 Path(int npoints,
75 uint seed,
76 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
77 bool closed = false)
78 : Cloud(npoints, seed, bbox), closed(closed){};
79
86 Path(std::vector<Point> points, bool closed = false)
88
97 Path(std::vector<float> x, std::vector<float> y, bool closed = false)
98 : Cloud(x, y), closed(closed){};
99
109 Path(std::vector<float> x,
110 std::vector<float> y,
111 std::vector<float> v,
112 bool closed = false)
113 : Cloud(x, y, v), closed(closed){};
114
135 void bezier(float curvature_ratio = 0.3f, int edge_divisions = 10);
136
155 void bezier_round(float curvature_ratio = 0.3f, int edge_divisions = 10);
156
180 void bspline(int edge_divisions = 10);
181
205 void catmullrom(int edge_divisions = 10);
206
213 void clear();
214
239 void decasteljau(int edge_divisions = 10);
240
258 void decimate_cfit(int n_points_target = 3);
259
282 void decimate_vw(int n_points_target = 3);
283
316 void dijkstra(Array &array,
317 glm::vec4 bbox,
318 float elevation_ratio = 0.f,
319 float distance_exponent = 0.5f,
320 float upward_penalization = 1.f,
321 Array *p_mask_nogo = nullptr);
322
331 void divide();
332
376 void fractalize(int iterations,
377 uint seed,
378 float sigma = 0.3f,
379 int orientation = 0,
380 float persistence = 1.f,
381 Array *p_control_field = nullptr,
382 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
383
395 std::vector<float> get_arc_length();
396
408 std::vector<float> get_cumulative_distance();
409
420 std::vector<float> get_values() const;
421
431 std::vector<float> get_x() const;
432
444 std::vector<float> get_xy() const;
445
455 std::vector<float> get_y() const;
456
470 void enforce_monotonic_values(bool decreasing = true);
471
498 void meanderize(float ratio,
499 float noise_ratio = 0.1f,
500 uint seed = 1,
501 int iterations = 1,
502 int edge_divisions = 10);
503
515 void reorder_nns(int start_index = 0);
516
527 void resample(float delta);
528
537 void resample_uniform();
538
546 void reverse();
547
569 float sdf_angle_closed(float x, float y);
570
592 float sdf_angle_open(float x, float y);
593
615 float sdf_closed(float x, float y);
616
639 float sdf_elevation_closed(float x, float y, float slope);
640
663 float sdf_elevation_open(float x, float y, float slope);
664
686 float sdf_open(float x, float y);
687
721 void smooth(int navg = 1,
722 float averaging_intensity = 1.f,
723 float inertia = 0.f);
724
735 void subsample(int step);
736
752 void to_array(Array &array,
753 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
754 bool filled = false) const;
755
786 Array to_array_sdf(glm::ivec2 shape,
787 glm::vec4 bbox,
788 Array *p_noise_x = nullptr,
789 Array *p_noise_y = nullptr,
790 glm::vec4 bbox_array = {0.f, 1.f, 0.f, 1.f});
791
805 void to_png(std::string fname, glm::ivec2 shape = {512, 512});
806};
807
846void dig_path(Array &z,
847 Path &path,
848 int width = 1,
849 int decay = 2,
850 int flattening_radius = 16,
851 bool force_downhill = false,
852 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
853 float depth = 0.f);
854
885void dig_river(Array &z,
886 const std::vector<Path> &path_list,
887 float riverbank_talus,
888 int river_width = 0,
889 int merging_width = 0,
890 float depth = 0.f,
891 float riverbed_talus = 0.f,
892 float noise_ratio = 0.9f,
893 uint seed = 0,
894 Array *p_mask = nullptr);
895
896void dig_river(Array &z,
897 const Path &path,
898 float riverbank_talus,
899 int river_width = 0,
900 int merging_width = 0,
901 float depth = 0.f,
902 float riverbed_talus = 0.f,
903 float noise_ratio = 0.9f,
904 uint seed = 0,
905 Array *p_mask = nullptr);
906
931Path find_cut_path_dijkstra(const Array &z,
932 DomainBoundary start,
933 DomainBoundary end,
934 float dijk_elevation_ratio = 0.9f,
935 float dijk_distance_exponent = 2.f,
936 float dijk_upward_penalization = 100.f);
937
938Path find_cut_path_midpoint(const Array &z,
939 DomainBoundary start,
940 DomainBoundary end,
941 uint seed,
942 int midp_iterations = 4,
943 float midp_sigma = 0.2f);
944
945} // namespace hmap
unsigned int uint
Definition array.hpp:14
Header file for boundary condition functions and utilities.
Array class, helper to manipulate 2D float array with "(i, j)" indexing.
Definition array.hpp:32
Represents a collection of unordered points in 2D space.
Definition cloud.hpp:55
std::vector< Point > points
Points of the cloud.
Definition cloud.hpp:57
Represents an ordered set of points in 2D, forming a polyline (open or closed).
Definition path.hpp:50
Path(std::vector< float > x, std::vector< float > y, std::vector< float > v, bool closed=false)
Construct a new Path object based on x, y coordinates, and values. Initializes a path with the specif...
Definition path.hpp:109
void bspline(int edge_divisions=10)
Smooth the path using B-Spline curves.
Definition path.cpp:107
std::vector< float > get_xy() const
Get the coordinates of the points as a single vector.
Definition path.cpp:396
void meanderize(float ratio, float noise_ratio=0.1f, uint seed=1, int iterations=1, int edge_divisions=10)
Add "meanders" to the path.
Definition path.cpp:443
bool closed
Defines whether the path is closed or open. If true, the path is closed, forming a loop....
Definition path.hpp:56
Path(std::vector< Point > points, bool closed=false)
Construct a new Path object based on a list of points. Initializes a path with the specified points a...
Definition path.hpp:86
std::vector< float > get_arc_length()
Get the arc length of the path.
Definition path.cpp:352
void catmullrom(int edge_divisions=10)
Smooth the path using Catmull-Rom curves.
Definition path.cpp:119
void enforce_monotonic_values(bool decreasing=true)
Enforces monotonicity on the values of the points in the path.
Definition path.cpp:423
void decimate_cfit(int n_points_target=3)
Simplifies the current path using a curvature preserving algorithm.
Definition path.cpp:155
void to_array(Array &array, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, bool filled=false) const
Project path points to an array.
Definition path.cpp:807
void bezier(float curvature_ratio=0.3f, int edge_divisions=10)
Smooth the path using Bezier curves.
Definition path.cpp:25
void dijkstra(Array &array, glm::vec4 bbox, float elevation_ratio=0.f, float distance_exponent=0.5f, float upward_penalization=1.f, Array *p_mask_nogo=nullptr)
Divide the path by adding points based on the lowest elevation difference between each pair of edge e...
Definition path.cpp:212
std::vector< float > get_values() const
Get the values assigned to the points on the path.
Definition path.cpp:376
float sdf_angle_open(float x, float y)
Return the angle of the closest edge to the point (x, y), assuming an open path.
Definition path.cpp:654
float sdf_elevation_open(float x, float y, float slope)
Return the elevation value at (x, y) away from the path based on a downslope slope,...
Definition path.cpp:719
Array to_array_sdf(glm::ivec2 shape, glm::vec4 bbox, Array *p_noise_x=nullptr, Array *p_noise_y=nullptr, glm::vec4 bbox_array={0.f, 1.f, 0.f, 1.f})
Return an array filled with the signed distance function to the path.
Definition path.cpp:855
void divide()
Divide the path by adding a point between each pair of consecutive points.
Definition path.cpp:271
void decasteljau(int edge_divisions=10)
Smooth the path using De Casteljau curves.
Definition path.cpp:138
Path(bool closed=false)
Construct a new Path object with default properties. Initializes an empty path with the closed proper...
Definition path.hpp:63
void subsample(int step)
Subsample the path by keeping only every n-th point.
Definition path.cpp:791
void bezier_round(float curvature_ratio=0.3f, int edge_divisions=10)
Smooth the path using Bezier curves (alternative method).
Definition path.cpp:65
void to_png(std::string fname, glm::ivec2 shape={512, 512})
Export path as PNG image file.
Definition path.cpp:892
void reverse()
Reverse the order of points in the path.
Definition path.cpp:621
std::vector< float > get_y() const
Get the y coordinates of the points on the path.
Definition path.cpp:413
void smooth(int navg=1, float averaging_intensity=1.f, float inertia=0.f)
Applies a smoothing operation to the path points using a moving average filter.
Definition path.cpp:756
std::vector< float > get_x() const
Get the x coordinates of the points on the path.
Definition path.cpp:386
void resample(float delta)
Resample the path to achieve an approximately constant distance between points.
Definition path.cpp:552
float sdf_closed(float x, float y)
Return the signed distance function value at (x, y), assuming a closed path.
Definition path.cpp:677
void decimate_vw(int n_points_target=3)
Simplifies the current path using the Visvalingam-Whyatt algorithm.
Definition path.cpp:183
void resample_uniform()
Resample the path to achieve fairly uniform distance between consecutive points.
Definition path.cpp:600
float sdf_open(float x, float y)
Return the value of the signed distance function at (x, y), assuming an open path.
Definition path.cpp:738
void reorder_nns(int start_index=0)
Reorder points using a nearest neighbor search.
Definition path.cpp:506
void clear()
Clear the path data.
Definition path.cpp:132
void fractalize(int iterations, uint seed, float sigma=0.3f, int orientation=0, float persistence=1.f, Array *p_control_field=nullptr, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f})
Applies fractalization to the path by adding points and randomly displacing their positions.
Definition path.cpp:296
Path(std::vector< float > x, std::vector< float > y, bool closed=false)
Construct a new Path object based on x and y coordinates. Initializes a path with the specified x and...
Definition path.hpp:97
float sdf_elevation_closed(float x, float y, float slope)
Return the elevation value at (x, y) away from the path based on a downslope slope,...
Definition path.cpp:700
std::vector< float > get_cumulative_distance()
Get the cumulative distance of the path.
Definition path.cpp:361
float sdf_angle_closed(float x, float y)
Return the angle of the closest edge to the point (x, y), assuming a closed path.
Definition path.cpp:626
Path(int npoints, uint seed, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, bool closed=false)
Construct a new Path object with random positions and values. Initializes a path with a specified num...
Definition path.hpp:74
Definition of the Cloud class for manipulating sets of 2D points.
Definition algebra.hpp:22
Path find_cut_path_dijkstra(const Array &z, DomainBoundary start, DomainBoundary end, float dijk_elevation_ratio=0.9f, float dijk_distance_exponent=2.f, float dijk_upward_penalization=100.f)
Find a Dijkstra-based cut path between two domain boundaries.
Definition find_cut_path.cpp:11
DomainBoundary
Describes which domain boundary.
Definition boundary.hpp:43
void dig_path(Array &z, Path &path, int width=1, int decay=2, int flattening_radius=16, bool force_downhill=false, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, float depth=0.f)
Dig a path on a heightmap.
Definition path.cpp:903
void dig_river(Array &z, const std::vector< Path > &path_list, float riverbank_talus, int river_width=0, int merging_width=0, float depth=0.f, float riverbed_talus=0.f, float noise_ratio=0.9f, uint seed=0, Array *p_mask=nullptr)
Modifies the elevation array to carve a river along a specified path.
Definition path.cpp:953
Array slope(glm::ivec2 shape, float angle, float slope, const Array *p_ctrl_param=nullptr, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, const Array *p_stretching=nullptr, glm::vec2 center={0.5f, 0.5f}, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f})
Return an array corresponding to a slope with a given overall.
Definition primitives.cpp:279
Array step(glm::ivec2 shape, float angle, float slope, const Array *p_ctrl_param=nullptr, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, const Array *p_stretching=nullptr, glm::vec2 center={0.5f, 0.5f}, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f})
Return a step function (Heaviside with an optional talus slope at the transition).
Definition primitives.cpp:332
Path find_cut_path_midpoint(const Array &z, DomainBoundary start, DomainBoundary end, uint seed, int midp_iterations=4, float midp_sigma=0.2f)
Definition find_cut_path.cpp:120