HighMap library (C++)
Loading...
Searching...
No Matches
cloud.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
23#pragma once
24#include <cmath>
25#include <optional>
26
27#include "highmap/array.hpp"
31
32namespace hmap
33{
34
35class Graph;
36
49class Cloud
50{
51public:
52 std::vector<Point> points = {};
53
54 // ==========================================================================
55 // Constructors
56 // ==========================================================================
57
63 Cloud(){};
64
65 virtual ~Cloud() = default;
66
76 Cloud(int npoints, std::uint32_t seed, glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
77
83 Cloud(const std::vector<Point> &points) : points(points){};
84
93 Cloud(const std::vector<float> &x,
94 const std::vector<float> &y,
95 float default_value = 0.f);
96
105 Cloud(const std::vector<float> &x,
106 const std::vector<float> &y,
107 const std::vector<float> &v);
108
119 Cloud(const std::vector<glm::ivec2> &indices,
120 const glm::ivec2 &shape,
121 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
122
126 Cloud(const std::vector<glm::vec3> &xyv);
127
133 void add_point(const Point &p);
134
139 void remove_point(int point_idx);
140
141 // ==========================================================================
142 // Accessors
143 // ==========================================================================
144
151 glm::vec4 get_bbox() const;
152
164 Point get_center() const;
165
179 std::vector<int> get_convex_hull() const;
180
186 std::vector<float> get_values() const;
187
192 float get_values_max() const;
193
198 float get_values_min() const;
199
205 std::vector<float> get_x() const;
206
217 std::vector<float> get_xy() const;
218
224 std::vector<float> get_y() const;
225
239 size_t nearest_point(const glm::vec2 &xy) const;
240
247 void set_points(const std::vector<float> &x, const std::vector<float> &y);
248
253 void set_values(const std::vector<float> &new_values);
254
262 void set_values(float new_value);
263
276 void set_values_from_array(const Array &array,
277 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
278
289 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
290
301
306
311 bool empty() const;
312
317 size_t size() const;
318
319 // ==========================================================================
320 // Basic Ops
321 // ==========================================================================
322
326 void clear();
327
331 void print();
332
339 void randomize(std::uint32_t seed, glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
340
347 void remap_values(float vmin, float vmax);
348
360 void shuffle(float dx, float dy, std::uint32_t seed, float dv = 0.f);
361
373 void snap_points_to_bounding_box(const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f},
374 float tolerance_ratio = 1.f);
375
376 // ==========================================================================
377 // Conversion / IO
378 // ==========================================================================
379
399 bool from_csv(const std::string &fname);
400
414 void to_array(Array &array, glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f}) const;
415
417 Array to_array(glm::ivec2 shape, glm::vec4 bbox) const;
418
446 void to_array_interp(Array &array,
447 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
448 InterpolationMethod2D interpolation_method =
450 Array *p_noise_x = nullptr,
451 Array *p_noise_y = nullptr,
452 glm::vec4 bbox_array = {0.f, 1.f, 0.f, 1.f}) const;
453
458 void to_csv(const std::string &fname) const;
459
464 Graph to_graph_delaunay();
465
482 void to_png(const std::string &fname,
483 int cmap,
484 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
485 int depth = CV_8U,
486 glm::ivec2 shape = {512, 512});
487
492 std::vector<glm::vec3> to_vec3() const;
493};
494
495// ==========================================================================
496// Functions
497// ==========================================================================
498
509Array cloud_sdf_to_array(const Cloud &cloud,
510 glm::ivec2 shape,
511 glm::vec4 bbox_array = {0.f, 1.f, 0.f, 1.f},
512 const Array *p_noise_x = nullptr,
513 const Array *p_noise_y = nullptr);
514
523bool has_duplicates(const Cloud &cloud, float eps = 1e-9f, bool xy_only = true);
524
537std::vector<float> interpolate_values_from_array(const Cloud &cloud,
538 const Array &array,
539 glm::vec4 bbox);
540
552Cloud merge_cloud(const Cloud &cloud1, const Cloud &cloud2);
553
559Cloud merge_clouds(const std::vector<Cloud> &clouds);
560
582Cloud random_cloud(
583 size_t count,
584 std::uint32_t seed,
586 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
587
611Cloud random_cloud_density(size_t count,
612 const Array &density,
613 std::uint32_t seed,
614 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
615
639Cloud random_cloud_distance(float min_dist,
640 std::uint32_t seed,
641 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
642
668Cloud random_cloud_distance(float min_dist,
669 float max_dist,
670 const Array &density,
671 std::uint32_t seed,
672 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
673
700 float dist_min,
701 float dist_max,
702 float alpha,
703 std::uint32_t seed,
704 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
705
732 float dist_min,
733 float lambda,
734 float k,
735 std::uint32_t seed,
736 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
737
762Cloud random_cloud_jittered(size_t count,
763 const glm::vec2 &jitter_amount,
764 const glm::vec2 &stagger_ratio,
765 std::uint32_t seed,
766 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
767
782void rejection_filter_density(Cloud &cloud,
783 const Array &density_mask,
784 std::uint32_t seed,
785 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
786
801Cloud scale(const Cloud &cloud,
802 glm::vec2 scale,
803 glm::vec2 center = {0.5f, 0.5f});
804
805Cloud scale(const Cloud &cloud, float scale, glm::vec2 center = {0.5f, 0.5f});
806
807} // namespace hmap
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Represents a collection of unordered points in 2D space.
Definition cloud.hpp:50
void clear()
Clear all data from the cloud.
Definition cloud.cpp:103
Point get_center() const
Calculates the centroid of a set of points.
Definition cloud.cpp:187
void remap_values(float vmin, float vmax)
Remap the values of the cloud points to a target range.
Definition cloud.cpp:323
void to_array(Array &array, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}) const
Project the cloud points onto an array.
Definition cloud.cpp:507
virtual ~Cloud()=default
void shuffle(float dx, float dy, std::uint32_t seed, float dv=0.f)
Randomly perturbs the positions and values of all points in the cloud.
Definition cloud.cpp:494
void set_values_from_border_distance(const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Sets point values based on their distance to the bounding box border.
Definition cloud.cpp:377
bool from_csv(const std::string &fname)
Loads point data from a CSV file into the Cloud object.
Definition cloud.cpp:108
std::vector< float > get_values() const
Get the values assigned to the points in the cloud.
Definition cloud.cpp:213
void set_values_from_array(const Array &array, const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Set the values of the cloud points using values from an underlying array.
Definition cloud.cpp:369
void set_values(const std::vector< float > &new_values)
Set new values for the cloud points.
Definition cloud.cpp:354
std::vector< glm::vec3 > to_vec3() const
Convert path points to a vector of 3D positions.
Definition cloud.cpp:614
void set_points(const std::vector< float > &x, const std::vector< float > &y)
Set points of the using x, y coordinates.
Definition cloud.cpp:342
Cloud()
Default constructor for the Cloud class.
Definition cloud.hpp:63
Cloud(const std::vector< Point > &points)
Constructs a new Cloud object based on a list of existing points.
Definition cloud.hpp:83
void snap_points_to_bounding_box(const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f}, float tolerance_ratio=1.f)
Snap points to the bounding box edges and corners.
Definition cloud.cpp:429
bool empty() const
Check whether the cloud has no points.
Definition cloud.cpp:419
float get_values_min() const
Get the minimum value among the points in the cloud.
Definition cloud.cpp:230
void randomize(std::uint32_t seed, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f})
Randomize the positions and values of the cloud points.
Definition cloud.cpp:312
std::vector< float > get_x() const
Get the x coordinates of the points in the cloud.
Definition cloud.cpp:238
std::vector< Point > points
Points of the cloud.
Definition cloud.hpp:52
std::vector< float > get_xy() const
Get the concatenated x and y coordinates of the points in the cloud.
Definition cloud.cpp:247
void add_point(const Point &p)
Add a new point to the cloud.
Definition cloud.cpp:98
float get_values_max() const
Get the maximum value among the points in the cloud.
Definition cloud.cpp:222
std::vector< float > get_y() const
Get the y coordinates of the points in the cloud.
Definition cloud.cpp:259
void to_array_interp(Array &array, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, InterpolationMethod2D interpolation_method=InterpolationMethod2D::ITP2D_DELAUNAY, Array *p_noise_x=nullptr, Array *p_noise_y=nullptr, glm::vec4 bbox_array={0.f, 1.f, 0.f, 1.f}) const
Interpolate the values of an array using the cloud points.
Definition cloud.cpp:536
size_t nearest_point(const glm::vec2 &xy) const
Find the index of the nearest point in the cloud.
Definition cloud.cpp:268
void set_values_from_min_distance()
Sets point values based on the distance to their nearest neighbor.
Definition cloud.cpp:407
void to_png(const std::string &fname, int cmap, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, int depth=CV_8U, glm::ivec2 shape={512, 512})
Saves the current data as a PNG image file.
Definition cloud.cpp:601
size_t size() const
Get the number of points in the cloud.
Definition cloud.cpp:424
Graph to_graph_delaunay()
Convert the cloud to a graph using Delaunay triangulation.
Definition cloud.cpp:583
void set_values_from_chull_distance()
Set the values of the cloud points based on the distance to the convex hull of the cloud.
Definition cloud.cpp:388
glm::vec4 get_bbox() const
Get the bounding box of the cloud.
Definition cloud.cpp:170
void to_csv(const std::string &fname) const
Export the cloud data to a CSV file.
Definition cloud.cpp:569
void print()
Print information about the cloud's points.
Definition cloud.cpp:288
void remove_point(int point_idx)
Remove a point from the cloud.
Definition cloud.cpp:337
std::vector< int > get_convex_hull() const
Computes the indices of the points that form the convex hull of a set of points.
Definition cloud.cpp:197
Header file for 2D interpolation methods.
Definition algebra.hpp:23
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:72
InterpolationMethod2D
Enumeration of 2D interpolation methods.
Definition interpolate2d.hpp:47
@ ITP2D_DELAUNAY
Delaunay triangulation method for 2D interpolation.
Definition interpolate2d.hpp:48
Cloud random_cloud_distance_weibull(float dist_min, float lambda, float k, std::uint32_t seed, const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Generates a random cloud of points with distances drawn from a Weibull distribution.
Definition cloud.cpp:730
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:165
Cloud random_cloud_distance_power_law(float dist_min, float dist_max, float alpha, std::uint32_t seed, const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Generates a random cloud of points with distances drawn from a power-law distribution.
Definition cloud.cpp:715
void rejection_filter_density(Cloud &cloud, const Array &density_mask, std::uint32_t seed, const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Filter a point cloud using rejection sampling based on a density mask.
Definition cloud_functions.cpp:144
Cloud random_cloud_jittered(size_t count, const glm::vec2 &jitter_amount, const glm::vec2 &stagger_ratio, std::uint32_t seed, const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Generates a jittered grid cloud of points.
Definition cloud.cpp:741
Array cloud_sdf_to_array(const Cloud &cloud, 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 cloud.
Definition cloud_functions.cpp:23
std::vector< float > interpolate_values_from_array(const Cloud &cloud, const Array &array, glm::vec4 bbox)
Interpolate values from an array at the points' (x, y) locations.
Definition cloud_functions.cpp:100
Cloud random_cloud_distance(float min_dist, std::uint32_t seed, const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Generates a random cloud of points separated by at least a given minimum distance.
Definition cloud.cpp:693
PointSamplingMethod
Enumeration of point sampling methods.
Definition point_sampling.hpp:32
@ RND_LHS
Latin Hypercube Sampling.
Definition point_sampling.hpp:36
Cloud merge_cloud(const Cloud &cloud1, const Cloud &cloud2)
Merges two point clouds into one.
Definition cloud.cpp:625
Cloud random_cloud(size_t count, std::uint32_t seed, const PointSamplingMethod &method=PointSamplingMethod::RND_LHS, const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Generates a random cloud of points within a bounding box.
Definition cloud.cpp:671
Cloud random_cloud_density(size_t count, const Array &density, std::uint32_t seed, const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Generates a random cloud of points based on a spatial density map.
Definition cloud.cpp:681
Cloud merge_clouds(const std::vector< Cloud > &clouds)
Merges multiple point clouds into a single cloud.
Definition cloud.cpp:642