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"
29
30namespace hmap
31{
32
50class Path : public Cloud
51{
52public:
53 enum class EdgeDivisionMode : int
54 {
57 };
58
59 // ==========================================================================
60 // Constructors
61 // ==========================================================================
62
67 Path() = default;
68
77 Path(int npoints, uint seed, glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f})
78 : Cloud(npoints, seed, bbox){};
79
85 Path(std::vector<Point> points) : Cloud(points){};
86
94 Path(std::vector<float> x, std::vector<float> y) : Cloud(x, y){};
95
104 Path(std::vector<float> x, std::vector<float> y, std::vector<float> v)
105 : Cloud(x, y, v){};
106
118 Path(const std::vector<glm::ivec2> &indices,
119 const glm::ivec2 &shape,
120 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f})
121 : Cloud(indices, shape, bbox){};
122
126 Path(const std::vector<glm::vec3> &xyv) : Cloud(xyv){};
127
128 // ==========================================================================
129 // Accessors
130 // ==========================================================================
131
143 std::vector<float> get_arc_length() const;
144
156 std::vector<float> get_cumulative_distance() const;
157
164 std::vector<float> get_curvature(bool normalized = false) const;
165
173 std::vector<Point> get_edge_centers() const;
174
179 std::vector<glm::vec2> get_normals() const;
180
185 std::vector<glm::vec2> get_tangents() const;
186
197 std::vector<float> get_values() const;
198
208 std::vector<float> get_x() const;
209
221 std::vector<float> get_xy() const;
222
232 std::vector<float> get_y() const;
233
238 bool is_closed() const;
239
244 void set_closed(bool new_value);
245
246 // ==========================================================================
247 // Basic Ops
248 // ==========================================================================
249
256 void clear();
257
271 void enforce_monotonic_values(bool decreasing = true);
272
284 void reorder_nns(int start_index = 0);
285
293 void reverse();
294
295 // ==========================================================================
296 // Sampling
297 // ==========================================================================
298
307 void divide();
308
321 float delta,
323
339 void resample_interp(
340 int npoints,
342
353 void resample_uniform(
355
366 void subsample(int step);
367
368 // ==========================================================================
369 // Conversion / IO
370 // ==========================================================================
371
387 void to_array(Array &array,
388 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
389 bool filled = false) const;
390
392 Array to_array(glm::ivec2 shape,
393 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
394 bool filled = false) const;
395
397 void to_array_mask(Array &array,
398 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
399 bool filled = false) const;
400
415 void to_png(std::string fname, glm::ivec2 shape = {512, 512});
416
417private:
418 enum class PathClosure : int
419 {
420 PT_OPEN,
421 PT_CLOSE,
422 } path_closure = PathClosure::PT_OPEN;
423};
424
425// ==========================================================================
426// Functions
427// ==========================================================================
428
453Path bezier(const Path &path,
454 float curvature_ratio = 0.3f,
455 int edge_divisions = 10,
457
480Path bezier_round(
481 const Path &path,
482 float curvature_ratio = 0.3f,
483 int edge_divisions = 10,
485
512Path bspline(const Path &path,
513 int edge_divisions = 10,
515
536Path catmullrom(
537 const Path &path,
538 int edge_divisions = 10,
540
567Path decasteljau(
568 const Path &path,
569 int edge_divisions = 10,
571
594Path decimate_vw(const Path &path, int n_points_target = 3);
595
628Path fractalize(const Path &path,
629 int iterations,
630 uint seed,
631 float sigma = 0.2f,
632 int orientation = 0,
633 float persistence = 1.f,
634 Array *p_control_field = nullptr,
635 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
636
655Path inflate(const Path &path, float strength, bool resample = true);
656
685Path meanderize(
686 const Path &path,
687 float ratio,
688 float noise_ratio = 0.1f,
689 uint seed = 1,
690 int iterations = 1,
691 int edge_divisions = 10,
693
707Array path_sdf_to_array(const Path &path,
708 glm::ivec2 shape,
709 glm::vec4 bbox_array = {0.f, 1.f, 0.f, 1.f},
710 const Array *p_noise_x = nullptr,
711 const Array *p_noise_y = nullptr);
712
729Path remove_geometric_loops(const Path &path);
730
766Path smooth(const Path &path,
767 int navg = 1,
768 float averaging_intensity = 1.f,
769 float inertia = 0.f);
770
771// ==========================================================================
772// Verification Functions
773// ==========================================================================
774
786bool assert_start_end_points(const Path &path1,
787 const Path &path2,
788 float tol = 1e-6f,
789 bool verbose = false);
790
797float chamfer_distance(const Path &a, const Path &b);
798
808bool has_duplicates(const Path &path, float tol = 1e-6f);
809
810} // 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:54
std::vector< Point > points
Points of the cloud.
Definition cloud.hpp:56
Represents an ordered set of points in 2D, forming a polyline (open or closed).
Definition path.hpp:51
Path(const std::vector< glm::ivec2 > &indices, const glm::ivec2 &shape, const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Construct a point cloud from grid indices mapped to a bounding box.
Definition path.hpp:118
Path(std::vector< float > x, 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:94
std::vector< float > get_arc_length() const
Get the arc length of the path.
Definition path.cpp:76
std::vector< Point > get_edge_centers() const
Compute midpoints of path edges.
Definition path.cpp:126
std::vector< float > get_xy() const
Get the coordinates of the points as a single vector.
Definition path.cpp:194
void to_array_mask(Array &array, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, bool filled=false) const
See hmap::to_array.
Definition path.cpp:428
Path()=default
Construct a new Path object with default properties. Initializes an empty path with the closed proper...
Path(int npoints, uint seed, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f})
Construct a new Path object with random positions and values. Initializes a path with a specified num...
Definition path.hpp:77
bool is_closed() const
Check whether the path is closed.
Definition path.cpp:221
Path(std::vector< float > x, std::vector< float > y, 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:104
void enforce_monotonic_values(bool decreasing=true)
Enforces monotonicity on the values of the points in the path.
Definition path.cpp:56
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:373
EdgeDivisionMode
Definition path.hpp:54
std::vector< float > get_values() const
Get the values assigned to the points on the path.
Definition path.cpp:174
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:272
void resample_uniform(InterpolationMethod1D itp_method=InterpolationMethod1D::LINEAR)
Resample the path to achieve fairly uniform distance between consecutive points.
Definition path.cpp:321
void divide()
Divide the path by adding a point between each pair of consecutive points.
Definition path.cpp:31
void subsample(int step)
Subsample the path by keeping only every n-th point.
Definition path.cpp:357
std::vector< float > get_cumulative_distance() const
Get the cumulative distance of the path.
Definition path.cpp:85
void to_png(std::string fname, glm::ivec2 shape={512, 512})
Export path as PNG image file.
Definition path.cpp:435
std::vector< float > get_curvature(bool normalized=false) const
Computes the signed curvature at each point of the path.
Definition path.cpp:100
void reverse()
Reverse the order of points in the path.
Definition path.cpp:347
std::vector< float > get_y() const
Get the y coordinates of the points on the path.
Definition path.cpp:211
std::vector< float > get_x() const
Get the x coordinates of the points on the path.
Definition path.cpp:184
std::vector< glm::vec2 > get_normals() const
Computes unit normals at each point of the path.
Definition path.cpp:146
void reorder_nns(int start_index=0)
Reorder points using a nearest neighbor search.
Definition path.cpp:226
void clear()
Clear the path data.
Definition path.cpp:26
std::vector< glm::vec2 > get_tangents() const
Computes unit tangents at each point of the path.
Definition path.cpp:156
void set_closed(bool new_value)
Set whether the path is closed.
Definition path.cpp:352
Path(const std::vector< glm::vec3 > &xyv)
Constructs a new Path object from lists of xyz data as glm::vec3.
Definition path.hpp:126
Path(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:85
void resample_interp(int npoints, InterpolationMethod1D itp_method=InterpolationMethod1D::CUBIC)
Resamples the path using cubic interpolation along arc length.
Definition path.cpp:282
Definition of the Cloud class for manipulating sets of 2D points.
Defines a 1D interpolation class using the GSL (GNU Scientific Library).
Definition algebra.hpp:23
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
Array path_sdf_to_array(const Path &path, glm::ivec2 shape, glm::vec4 bbox_array={0.f, 1.f, 0.f, 1.f}, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr)
Compute a distance field from a point path.
Definition path_functions.cpp:392
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:86
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
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:11
Path fractalize(const Path &path, int iterations, uint seed, float sigma=0.2f, 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_functions.cpp:234
Path remove_geometric_loops(const Path &path)
Removes geometric loops in a 2D path caused by self-intersections.
Definition path_functions.cpp:445
Path inflate(const Path &path, float strength, bool resample=true)
Inflate (offset) a path along its normals using curvature.
Definition path_functions.cpp:295
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:372
Path decimate_vw(const Path &path, int n_points_target=3)
Simplifies the current path using the Visvalingam-Whyatt algorithm.
Definition path_functions.cpp:201
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:160
float chamfer_distance(const Path &a, const Path &b)
Calculate the chamfer distance between two paths.
Definition path_verification.cpp:37
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:32
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:482
bool has_duplicates(const Path &path, float tol=1e-6f)
Check if a path contains duplicate points within a given tolerance.
Definition path_verification.cpp:55
Path meanderize(const Path &path, float ratio, float noise_ratio=0.1f, uint 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:325
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:181