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
17#pragma once
18#include "highmap/boundary.hpp"
21
22namespace hmap
23{
24
42class Path : public Cloud
43{
44public:
45 enum class EdgeDivisionMode : int
46 {
49 };
50
51 // ==========================================================================
52 // Constructors
53 // ==========================================================================
54
59 Path() = default;
60
65 Path(const Cloud &cloud) : Cloud(cloud) {};
66
71 Path(Cloud &&cloud) noexcept : Cloud(std::move(cloud)) {};
72
81 Path(int npoints, std::uint32_t seed, const BBox2D &bbox = BBox2D{})
82 : Cloud(npoints, seed, bbox) {};
83
89 Path(const std::vector<Point> &points) : Cloud(points) {};
90
95 Path(std::vector<Point> &&points) noexcept : Cloud(std::move(points)) {};
96
104 Path(const std::vector<float> &x, const std::vector<float> &y)
105 : Cloud(x, y) {};
106
115 Path(const std::vector<float> &x,
116 const std::vector<float> &y,
117 const std::vector<float> &v)
118 : Cloud(x, y, v) {};
119
131 Path(const std::vector<glm::ivec2> &indices,
132 const glm::ivec2 &shape,
133 const BBox2D &bbox = BBox2D{})
134 : Cloud(indices, shape, bbox) {};
135
139 Path(const std::vector<glm::vec3> &xyv) : Cloud(xyv) {};
140
141 // ==========================================================================
142 // Accessors
143 // ==========================================================================
144
156 std::vector<float> get_arc_length() const;
157
169 std::vector<float> get_cumulative_distance() const;
170
177 std::vector<float> get_curvature(bool normalized = false) const;
178
186 std::vector<Point> get_edge_centers() const;
187
192 std::vector<glm::vec2> get_normals() const;
193
198 std::vector<glm::vec2> get_tangents() const;
199
210 std::vector<float> get_values() const;
211
221 std::vector<float> get_x() const;
222
234 std::vector<float> get_xy() const;
235
245 std::vector<float> get_y() const;
246
251 bool is_closed() const;
252
257 void set_closed(bool new_value);
258
259 // ==========================================================================
260 // Basic Ops
261 // ==========================================================================
262
269 void clear();
270
284 void enforce_monotonic_values(bool decreasing = true);
285
297 void rasterize_capsules(Array &out_dist_sq,
298 float radius,
299 const BBox2D &bbox = BBox2D{}) const;
300
312 void reorder_nns(int start_index = 0);
313
321 void reverse();
322
323 // ==========================================================================
324 // Sampling
325 // ==========================================================================
326
335 void divide();
336
349 float delta,
351
367 void resample_interp(
368 int npoints,
370
381 void resample_uniform(
383
397 glm::vec3 sample_at(float t,
398 const std::vector<float> *p_arc = nullptr,
399 glm::vec2 *p_tangent = nullptr) const;
400
411 void subsample(int step);
412
413 // ==========================================================================
414 // Conversion / IO
415 // ==========================================================================
416
432 void to_array(Array &array,
433 const BBox2D &bbox = BBox2D{},
434 bool filled = false) const;
435
437 Array to_array(glm::ivec2 shape,
438 const BBox2D &bbox = BBox2D{},
439 bool filled = false) const;
440
442 void to_array_mask(Array &array,
443 const BBox2D &bbox = BBox2D{},
444 bool filled = false) const;
445
460 void to_png(std::string fname, glm::ivec2 shape = {512, 512});
461
462private:
463 enum class PathClosure : int
464 {
465 PT_OPEN,
466 PT_CLOSE,
467 } path_closure = PathClosure::PT_OPEN;
468};
469
470// ==========================================================================
471// Functions
472// ==========================================================================
473
498Path bezier(const Path &path,
499 float curvature_ratio = 0.3f,
500 int edge_divisions = 10,
502
526 const Path &path,
527 float curvature_ratio = 0.3f,
528 int edge_divisions = 10,
530
557Path bspline(const Path &path,
558 int edge_divisions = 10,
560
582 const Path &path,
583 int edge_divisions = 10,
585
613 const Path &path,
614 int edge_divisions = 10,
616
639Path decimate_vw(const Path &path, int n_points_target = 3);
640
676Path fractalize(const Path &path,
677 int iterations,
678 std::uint32_t seed,
679 float sigma = 0.2f,
680 int orientation = 0,
681 float persistence = 1.f,
682 Array *p_control_field = nullptr,
683 const BBox2D &bbox = BBox2D{},
684 bool bounded = false);
685
704Path inflate(const Path &path, float strength, bool resample = true);
705
735 const Path &path,
736 float ratio,
737 float noise_ratio = 0.1f,
738 std::uint32_t seed = 1,
739 int iterations = 1,
740 int edge_divisions = 10,
742
760 glm::ivec2 shape,
761 const BBox2D &bbox_array = BBox2D::unit(),
762 const Array *p_noise_x = nullptr,
763 const Array *p_noise_y = nullptr,
764 bool use_bvh = true);
765
767 const Path &path,
768 glm::ivec2 shape,
769 const BBox2D &bbox_array = BBox2D::unit(),
770 const Array *p_noise_x = nullptr,
771 const Array *p_noise_y = nullptr);
772
774 glm::ivec2 shape,
775 const BBox2D &bbox_array = BBox2D::unit(),
776 const Array *p_noise_x = nullptr,
777 const Array *p_noise_y = nullptr);
778
793Array path_sdf_to_array(const Path &path,
794 glm::ivec2 shape,
795 const BBox2D &bbox_array = BBox2D::unit(),
796 const Array *p_noise_x = nullptr,
797 const Array *p_noise_y = nullptr,
798 bool use_bvh = true);
799
801 glm::ivec2 shape,
802 const BBox2D &bbox_array = BBox2D::unit(),
803 const Array *p_noise_x = nullptr,
804 const Array *p_noise_y = nullptr);
805
807 glm::ivec2 shape,
808 const BBox2D &bbox_array = BBox2D::unit(),
809 const Array *p_noise_x = nullptr,
810 const Array *p_noise_y = nullptr);
811
812namespace gpu
813{
814
816Array path_sdf_bezier_to_array(const Path &path,
817 glm::ivec2 shape,
818 const UvRect &uv_rect_array = UvRect::unit(),
819 const Array *p_noise_x = nullptr,
820 const Array *p_noise_y = nullptr);
821
823Array path_sdf_to_array(const Path &path,
824 glm::ivec2 shape,
825 const UvRect &uv_rect_array = UvRect::unit(),
826 const Array *p_noise_x = nullptr,
827 const Array *p_noise_y = nullptr);
828
829} // namespace gpu
830
847Path remove_geometric_loops(const Path &path);
848
869Path scale(const Path &path, glm::vec2 scale, glm::vec2 center = {0.5f, 0.5f});
870
871Path scale(const Path &path, float scale, glm::vec2 center = {0.5f, 0.5f});
872
908Path smooth(const Path &path,
909 int navg = 1,
910 float averaging_intensity = 1.f,
911 float inertia = 0.f);
912
946Path squiggle(const Path &path,
947 int iterations = 4,
948 std::uint32_t seed = 0,
949 float height_ratio = 0.5f,
950 int orientation = 0,
951 const Array *p_weights = nullptr,
952 const Array *p_mask = nullptr,
953 const BBox2D &bbox = BBox2D{});
954
975std::vector<Path> squiggle_branches(const Path &path,
976 int iterations = 4,
977 std::uint32_t seed = 0,
978 float branch_probability = 0.3f,
979 int max_branches = 8,
980 float height_ratio = 0.5f,
981 int orientation = 0,
982 const Array *p_weights = nullptr,
983 const Array *p_mask = nullptr,
984 const BBox2D &bbox = BBox2D{});
985
997 const std::vector<Path> &existing_paths);
998
999// ==========================================================================
1000// Verification Functions
1001// ==========================================================================
1002
1014bool assert_start_end_points(const Path &path1,
1015 const Path &path2,
1016 float tol = 1e-6f,
1017 bool verbose = false);
1018
1025float chamfer_distance(const Path &a, const Path &b);
1026
1036bool has_duplicates(const Path &path, float tol = 1e-6f);
1037
1038} // namespace hmap
Header file for boundary condition functions and utilities.
Array class, helper to manipulate 2D float array with "(i, j)" indexing.
Definition array.hpp:31
Represents a collection of unordered points in 2D space.
Definition cloud.hpp:61
Cloud()
Default constructor for the Cloud class.
std::vector< Point > points
Points of the cloud.
Definition cloud.hpp:663
Represents an ordered set of points in 2D, forming a polyline (open or closed).
Definition path.hpp:43
std::vector< float > get_arc_length() const
Get the arc length of the path.
Definition path.cpp:166
std::vector< Point > get_edge_centers() const
Compute midpoints of path edges.
Definition path.cpp:231
std::vector< float > get_xy() const
Get the coordinates of the points as a single vector.
Definition path.cpp:318
Path(const std::vector< Point > &points)
Construct a new Path object based on a list of points. Initializes a path with the specified points a...
Definition path.hpp:89
Path()=default
Construct a new Path object with default properties. Initializes an empty path with the closed proper...
bool is_closed() const
Check whether the path is closed.
Definition path.cpp:345
Path(const Cloud &cloud)
Construct a new Path object based on a cloud of points.
Definition path.hpp:65
Path(const std::vector< float > &x, const std::vector< float > &y, const std::vector< float > &v)
Construct a new Path object based on x, y coordinates, and values. Initializes a path with the specif...
Definition path.hpp:115
void enforce_monotonic_values(bool decreasing=true)
Enforces monotonicity on the values of the points in the path.
Definition path.cpp:59
EdgeDivisionMode
Definition path.hpp:46
@ EDM_PER_EDGE
Definition path.hpp:47
@ EDM_FULL_ARC
Definition path.hpp:48
void to_array(Array &array, const BBox2D &bbox=BBox2D{}, bool filled=false) const
Project path points to an array.
Definition path.cpp:547
std::vector< float > get_values() const
Get the values assigned to the points on the path.
Definition path.cpp:298
void resample_by_spacing(float delta, InterpolationMethod1D itp_method=InterpolationMethod1D::LINEAR)
Resample the path to achieve an approximately constant distance between points.
Definition path.cpp:398
void to_array_mask(Array &array, const BBox2D &bbox=BBox2D{}, bool filled=false) const
See hmap::to_array.
Definition path.cpp:605
Path(int npoints, std::uint32_t seed, const BBox2D &bbox=BBox2D{})
Construct a new Path object with random positions and values. Initializes a path with a specified num...
Definition path.hpp:81
void resample_uniform(InterpolationMethod1D itp_method=InterpolationMethod1D::LINEAR)
Resample the path to achieve fairly uniform distance between consecutive points.
Definition path.cpp:447
void divide()
Divide the path by adding a point between each pair of consecutive points.
Definition path.cpp:32
void subsample(int step)
Subsample the path by keeping only every n-th point.
Definition path.cpp:526
Path(Cloud &&cloud) noexcept
Move-construct a new Path object based on a cloud of points.
Definition path.hpp:71
std::vector< float > get_cumulative_distance() const
Get the cumulative distance of the path.
Definition path.cpp:186
void to_png(std::string fname, glm::ivec2 shape={512, 512})
Export path as PNG image file.
Definition path.cpp:614
std::vector< float > get_curvature(bool normalized=false) const
Computes the signed curvature at each point of the path.
Definition path.cpp:203
void reverse()
Reverse the order of points in the path.
Definition path.cpp:471
std::vector< float > get_y() const
Get the y coordinates of the points on the path.
Definition path.cpp:335
glm::vec3 sample_at(float t, const std::vector< float > *p_arc=nullptr, glm::vec2 *p_tangent=nullptr) const
Sample the path using normalized arc-length parameterization.
Definition path.cpp:476
std::vector< float > get_x() const
Get the x coordinates of the points on the path.
Definition path.cpp:308
std::vector< glm::vec2 > get_normals() const
Computes unit normals at each point of the path.
Definition path.cpp:251
void reorder_nns(int start_index=0)
Reorder points using a nearest neighbor search.
Definition path.cpp:350
void clear()
Clear the path data.
Definition path.cpp:27
std::vector< glm::vec2 > get_tangents() const
Computes unit tangents at each point of the path.
Definition path.cpp:261
Path(const std::vector< float > &x, const std::vector< float > &y)
Construct a new Path object based on x and y coordinates. Initializes a path with the specified x and...
Definition path.hpp:104
void set_closed(bool new_value)
Set whether the path is closed.
Definition path.cpp:521
Path(const std::vector< glm::vec3 > &xyv)
Constructs a new Path object from lists of xyz data as glm::vec3.
Definition path.hpp:139
void rasterize_capsules(Array &out_dist_sq, float radius, const BBox2D &bbox=BBox2D{}) const
Rasterize path segments as capsules with a given radius onto a distance map.
Definition path.cpp:81
Path(std::vector< Point > &&points) noexcept
Move-construct a new Path object based on a list of points.
Definition path.hpp:95
Path(const std::vector< glm::ivec2 > &indices, const glm::ivec2 &shape, const BBox2D &bbox=BBox2D{})
Construct a point cloud from grid indices mapped to a bounding box.
Definition path.hpp:131
void resample_interp(int npoints, InterpolationMethod1D itp_method=InterpolationMethod1D::CUBIC)
Resamples the path using cubic interpolation along arc length.
Definition path.cpp:408
Definition of the Cloud class for manipulating sets of 2D points.
Defines a 1D interpolation class using the GSL (GNU Scientific Library).
Definition array_blending.hpp:225
Array path_sdf_to_array(const Path &path, glm::ivec2 shape, const UvRect &uv_rect_array=UvRect::unit(), const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr)
See hmap::path_sdf_to_array.
Definition sdf_2d_polyline_gpu.cpp:75
Array path_sdf_bezier_to_array(const Path &path, glm::ivec2 shape, const UvRect &uv_rect_array=UvRect::unit(), const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr)
See hmap::path_sdf_bezier_to_array.
Definition sdf_2d_polyline_gpu.cpp:166
Definition opencv.hpp:20
Array path_sdf_bezier_to_array_brute_force(const Path &path, glm::ivec2 shape, const BBox2D &bbox_array=BBox2D::unit(), const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr)
Definition path_functions.cpp:436
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, glm::vec2 center={0.5f, 0.5f}, const UvRect &uv_rect=UvRect::unit())
Return a step function (Heaviside with an optional talus slope at the transition).
Definition primitives.cpp:424
bool has_duplicates(const Cloud &cloud, float eps=1e-9f, bool xy_only=true)
Checks whether the point cloud contains duplicate points.
Definition cloud_functions.cpp:65
Path bspline(const Path &path, int edge_divisions=10, Path::EdgeDivisionMode edm=Path::EdgeDivisionMode::EDM_PER_EDGE)
Smooth the path using B-Spline curves.
Definition path_functions.cpp:142
Path bezier_round(const Path &path, float curvature_ratio=0.3f, int edge_divisions=10, Path::EdgeDivisionMode edm=Path::EdgeDivisionMode::EDM_PER_EDGE)
Smooth the path using Bezier curves (alternative method).
Definition path_functions.cpp:95
Array path_sdf_to_array(const Path &path, glm::ivec2 shape, const BBox2D &bbox_array=BBox2D::unit(), const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, bool use_bvh=true)
Compute a distance field from a point path.
Definition path_functions.cpp:462
Array normalized(const Array &array, NormalizationMethod method)
Returns a normalized copy of an array.
Definition normalize.cpp:51
Array path_sdf_to_array_bvh(const Path &path, glm::ivec2 shape, const BBox2D &bbox_array=BBox2D::unit(), const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr)
Definition path_functions.cpp:540
InterpolationMethod1D
Enumeration of the available 1D interpolation methods.
Definition interpolate1d.hpp:36
@ CUBIC
Cubic spline interpolation.
Definition interpolate1d.hpp:39
@ LINEAR
Linear interpolation.
Definition interpolate1d.hpp:41
Path meanderize(const Path &path, float ratio, float noise_ratio=0.1f, std::uint32_t seed=1, int iterations=1, int edge_divisions=10, Path::EdgeDivisionMode edm=Path::EdgeDivisionMode::EDM_PER_EDGE)
Add "meanders" to the path.
Definition path_functions.cpp:356
Array path_sdf_to_array_brute_force(const Path &path, glm::ivec2 shape, const BBox2D &bbox_array=BBox2D::unit(), const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr)
Definition path_functions.cpp:479
Path squiggle(const Path &path, int iterations=4, std::uint32_t seed=0, float height_ratio=0.5f, int orientation=0, const Array *p_weights=nullptr, const Array *p_mask=nullptr, const BBox2D &bbox=BBox2D{})
Generates a continuous, non-self-intersecting squiggle curve by applying the squiggle recursive trian...
Definition path_squiggle.cpp:182
bool assert_start_end_points(const Path &path1, const Path &path2, float tol=1e-6f, bool verbose=false)
Asserts that the start and end points of two paths are within a specified tolerance.
Definition path_verification.cpp:17
std::vector< Path > squiggle_branches(const Path &path, int iterations=4, std::uint32_t seed=0, float branch_probability=0.3f, int max_branches=8, float height_ratio=0.5f, int orientation=0, const Array *p_weights=nullptr, const Array *p_mask=nullptr, const BBox2D &bbox=BBox2D{})
Generates squiggle paths with secondary tributary branches along each edge.
Definition path_squiggle.cpp:296
Path remove_geometric_loops(const Path &path)
Removes geometric loops in a 2D path caused by self-intersections.
Definition path_functions.cpp:681
Path inflate(const Path &path, float strength, bool resample=true)
Inflate (offset) a path along its normals using curvature.
Definition path_functions.cpp:326
Cloud scale(const Cloud &cloud, glm::vec2 scale, glm::vec2 center={0.5f, 0.5f})
Scales the point coordinates in a cloud relative to a center point.
Definition cloud_functions.cpp:163
Path decimate_vw(const Path &path, int n_points_target=3)
Simplifies the current path using the Visvalingam-Whyatt algorithm.
Definition path_functions.cpp:206
Path catmullrom(const Path &path, int edge_divisions=10, Path::EdgeDivisionMode edm=Path::EdgeDivisionMode::EDM_PER_EDGE)
Smooth the path using Catmull-Rom curves.
Definition path_functions.cpp:162
Array path_sdf_bezier_to_array_bvh(const Path &path, glm::ivec2 shape, const BBox2D &bbox_array=BBox2D::unit(), const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr)
Definition path_functions.cpp:449
float chamfer_distance(const Path &a, const Path &b)
Calculate the chamfer distance between two paths.
Definition path_verification.cpp:43
Path fractalize(const Path &path, int iterations, std::uint32_t seed, float sigma=0.2f, int orientation=0, float persistence=1.f, Array *p_control_field=nullptr, const BBox2D &bbox=BBox2D{}, bool bounded=false)
Applies fractalization to the path by adding points and randomly displacing their positions.
Definition path_functions.cpp:235
Path bezier(const Path &path, float curvature_ratio=0.3f, int edge_divisions=10, Path::EdgeDivisionMode edm=Path::EdgeDivisionMode::EDM_PER_EDGE)
Smooth the path using Bezier curves.
Definition path_functions.cpp:51
Path truncate_at_first_intersection(const Path &path, const std::vector< Path > &existing_paths)
Truncates a path at its first intersection with any path in a given set of existing paths....
Definition path_functions.cpp:823
Path smooth(const Path &path, 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_functions.cpp:732
Array path_sdf_bezier_to_array(const Path &path, glm::ivec2 shape, const BBox2D &bbox_array=BBox2D::unit(), const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, bool use_bvh=true)
Converts a 2D path treated as a piecewise quadratic Bezier curve to a signed distance field (SDF) arr...
Definition path_functions.cpp:421
Path decasteljau(const Path &path, int edge_divisions=10, Path::EdgeDivisionMode edm=Path::EdgeDivisionMode::EDM_PER_EDGE)
Smooth the path using De Casteljau curves.
Definition path_functions.cpp:184
Axis-aligned 2D bounding box in world coordinates.
Definition bbox2d.hpp:25
static constexpr BBox2D unit()
Definition bbox2d.hpp:39
static constexpr UvRect unit()
Definition uv_rect.hpp:46