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
28#pragma once
29#include <cmath>
30
31#include "highmap/array.hpp"
35
36namespace hmap
37{
38
39class Graph;
40
53class Cloud
54{
55public:
56 std::vector<Point> points = {};
57
58 // ==========================================================================
59 // Constructors
60 // ==========================================================================
61
67 Cloud(){};
68
69 virtual ~Cloud() = default;
70
80 Cloud(int npoints, uint seed, glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
81
87 Cloud(const std::vector<Point> &points) : points(points){};
88
97 Cloud(const std::vector<float> &x,
98 const std::vector<float> &y,
99 float default_value = 0.f);
100
109 Cloud(const std::vector<float> &x,
110 const std::vector<float> &y,
111 const std::vector<float> &v);
112
123 Cloud(const std::vector<glm::ivec2> &indices,
124 const glm::ivec2 &shape,
125 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
126
130 Cloud(const std::vector<glm::vec3> &xyv);
131
137 void add_point(const Point &p);
138
143 void remove_point(int point_idx);
144
145 // ==========================================================================
146 // Accessors
147 // ==========================================================================
148
155 glm::vec4 get_bbox() const;
156
168 Point get_center() const;
169
183 std::vector<int> get_convex_hull() const;
184
190 std::vector<float> get_values() const;
191
196 float get_values_max() const;
197
202 float get_values_min() const;
203
209 std::vector<float> get_x() const;
210
221 std::vector<float> get_xy() const;
222
228 std::vector<float> get_y() const;
229
243 size_t nearest_point(const glm::vec2 &xy) const;
244
251 void set_points(const std::vector<float> &x, const std::vector<float> &y);
252
257 void set_values(const std::vector<float> &new_values);
258
266 void set_values(float new_value);
267
280 void set_values_from_array(const Array &array,
281 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
282
293 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
294
305
310
315 size_t size() const;
316
317 // ==========================================================================
318 // Basic Ops
319 // ==========================================================================
320
324 void clear();
325
329 void print();
330
337 void randomize(uint seed, glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
338
345 void remap_values(float vmin, float vmax);
346
358 void shuffle(float dx, float dy, uint seed, float dv = 0.f);
359
371 void snap_points_to_bounding_box(const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f},
372 float tolerance_ratio = 1.f);
373
374 // ==========================================================================
375 // Conversion / IO
376 // ==========================================================================
377
397 bool from_csv(const std::string &fname);
398
412 void to_array(Array &array, glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f}) const;
413
415 Array to_array(glm::ivec2 shape, glm::vec4 bbox) const;
416
444 void to_array_interp(Array &array,
445 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
446 InterpolationMethod2D interpolation_method =
448 Array *p_noise_x = nullptr,
449 Array *p_noise_y = nullptr,
450 glm::vec4 bbox_array = {0.f, 1.f, 0.f, 1.f}) const;
451
456 void to_csv(const std::string &fname) const;
457
462 Graph to_graph_delaunay();
463
480 void to_png(const std::string &fname,
481 int cmap,
482 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
483 int depth = CV_8U,
484 glm::ivec2 shape = {512, 512});
485
490 std::vector<glm::vec3> to_vec3() const;
491};
492
493// ==========================================================================
494// Functions
495// ==========================================================================
496
507Array cloud_sdf_to_array(const Cloud &cloud,
508 glm::ivec2 shape,
509 glm::vec4 bbox_array = {0.f, 1.f, 0.f, 1.f},
510 const Array *p_noise_x = nullptr,
511 const Array *p_noise_y = nullptr);
512
525std::vector<float> interpolate_values_from_array(const Cloud &cloud,
526 const Array &array,
527 glm::vec4 bbox);
528
540Cloud merge_cloud(const Cloud &cloud1, const Cloud &cloud2);
541
547Cloud merge_clouds(const std::vector<Cloud> &clouds);
548
570Cloud random_cloud(
571 size_t count,
572 uint seed,
574 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
575
599Cloud random_cloud_density(size_t count,
600 const Array &density,
601 uint seed,
602 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
603
627Cloud random_cloud_distance(float min_dist,
628 uint seed,
629 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
630
656Cloud random_cloud_distance(float min_dist,
657 float max_dist,
658 const Array &density,
659 uint seed,
660 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
661
688 float dist_min,
689 float dist_max,
690 float alpha,
691 uint seed,
692 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
693
720 float dist_min,
721 float lambda,
722 float k,
723 uint seed,
724 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
725
750Cloud random_cloud_jittered(size_t count,
751 const glm::vec2 &jitter_amount,
752 const glm::vec2 &stagger_ratio,
753 uint seed,
754 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
755
770void rejection_filter_density(Cloud &cloud,
771 const Array &density_mask,
772 uint seed,
773 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
774
775} // namespace hmap
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
unsigned int uint
Definition array.hpp:14
Represents a collection of unordered points in 2D space.
Definition cloud.hpp:54
void clear()
Clear all data from the cloud.
Definition cloud.cpp:90
Point get_center() const
Calculates the centroid of a set of points.
Definition cloud.cpp:174
void remap_values(float vmin, float vmax)
Remap the values of the cloud points to a target range.
Definition cloud.cpp:310
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:487
virtual ~Cloud()=default
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:362
bool from_csv(const std::string &fname)
Loads point data from a CSV file into the Cloud object.
Definition cloud.cpp:95
void randomize(uint seed, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f})
Randomize the positions and values of the cloud points.
Definition cloud.cpp:299
std::vector< float > get_values() const
Get the values assigned to the points in the cloud.
Definition cloud.cpp:200
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:356
void set_values(const std::vector< float > &new_values)
Set new values for the cloud points.
Definition cloud.cpp:341
std::vector< glm::vec3 > to_vec3() const
Convert path points to a vector of 3D positions.
Definition cloud.cpp:584
void set_points(const std::vector< float > &x, const std::vector< float > &y)
Set points of the using x, y coordinates.
Definition cloud.cpp:329
Cloud()
Default constructor for the Cloud class.
Definition cloud.hpp:67
Cloud(const std::vector< Point > &points)
Constructs a new Cloud object based on a list of existing points.
Definition cloud.hpp:87
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:409
float get_values_min() const
Get the minimum value among the points in the cloud.
Definition cloud.cpp:217
std::vector< float > get_x() const
Get the x coordinates of the points in the cloud.
Definition cloud.cpp:225
std::vector< Point > points
Points of the cloud.
Definition cloud.hpp:56
std::vector< float > get_xy() const
Get the concatenated x and y coordinates of the points in the cloud.
Definition cloud.cpp:234
void add_point(const Point &p)
Add a new point to the cloud.
Definition cloud.cpp:85
float get_values_max() const
Get the maximum value among the points in the cloud.
Definition cloud.cpp:209
std::vector< float > get_y() const
Get the y coordinates of the points in the cloud.
Definition cloud.cpp:246
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:512
size_t nearest_point(const glm::vec2 &xy) const
Find the index of the nearest point in the cloud.
Definition cloud.cpp:255
void set_values_from_min_distance()
Sets point values based on the distance to their nearest neighbor.
Definition cloud.cpp:392
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:573
size_t size() const
Get the number of points in the cloud.
Definition cloud.cpp:404
Graph to_graph_delaunay()
Convert the cloud to a graph using Delaunay triangulation.
Definition cloud.cpp:555
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:373
glm::vec4 get_bbox() const
Get the bounding box of the cloud.
Definition cloud.cpp:157
void to_csv(const std::string &fname) const
Export the cloud data to a CSV file.
Definition cloud.cpp:541
void print()
Print information about the cloud's points.
Definition cloud.cpp:275
void shuffle(float dx, float dy, uint seed, float dv=0.f)
Randomly perturbs the positions and values of all points in the cloud.
Definition cloud.cpp:474
void remove_point(int point_idx)
Remove a point from the cloud.
Definition cloud.cpp:324
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:184
Header file for 2D interpolation methods.
Definition algebra.hpp:23
Cloud random_cloud_distance(float min_dist, uint 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:661
Cloud random_cloud_density(size_t count, const Array &density, uint 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:651
Cloud random_cloud_jittered(size_t count, const glm::vec2 &jitter_amount, const glm::vec2 &stagger_ratio, uint seed, const glm::vec4 &bbox={0.f, 1.f, 0.f, 1.f})
Generates a jittered grid cloud of points.
Definition cloud.cpp:705
Cloud random_cloud_distance_power_law(float dist_min, float dist_max, float alpha, uint 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:679
InterpolationMethod2D
Enumeration of 2D interpolation methods.
Definition interpolate2d.hpp:45
@ ITP2D_DELAUNAY
Delaunay triangulation method for 2D interpolation.
Definition interpolate2d.hpp:46
Cloud random_cloud(size_t count, uint 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:641
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:14
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:59
Cloud random_cloud_distance_weibull(float dist_min, float lambda, float k, uint 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:694
PointSamplingMethod
Enumeration of point sampling methods.
Definition point_sampling.hpp:31
@ RND_LHS
Latin Hypercube Sampling.
Definition point_sampling.hpp:35
Cloud merge_cloud(const Cloud &cloud1, const Cloud &cloud2)
Merges two point clouds into one.
Definition cloud.cpp:595
void rejection_filter_density(Cloud &cloud, const Array &density_mask, uint 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:100
Cloud merge_clouds(const std::vector< Cloud > &clouds)
Merges multiple point clouds into a single cloud.
Definition cloud.cpp:612