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
26#include "highmap/array.hpp"
30
31namespace hmap
32{
33
34class Graph;
35
48class Cloud
49{
50public:
51 std::vector<Point> points = {};
52
53 // ==========================================================================
54 // Constructors
55 // ==========================================================================
56
62 Cloud(){};
63
64 virtual ~Cloud() = default;
65
75 Cloud(int npoints, std::uint32_t seed, glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
76
82 Cloud(const std::vector<Point> &points) : points(points){};
83
92 Cloud(const std::vector<float> &x,
93 const std::vector<float> &y,
94 float default_value = 0.f);
95
104 Cloud(const std::vector<float> &x,
105 const std::vector<float> &y,
106 const std::vector<float> &v);
107
118 Cloud(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
125 Cloud(const std::vector<glm::vec3> &xyv);
126
132 void add_point(const Point &p);
133
138 void remove_point(int point_idx);
139
140 // ==========================================================================
141 // Accessors
142 // ==========================================================================
143
150 glm::vec4 get_bbox() const;
151
163 Point get_center() const;
164
178 std::vector<int> get_convex_hull() const;
179
185 std::vector<float> get_values() const;
186
191 float get_values_max() const;
192
197 float get_values_min() const;
198
204 std::vector<float> get_x() const;
205
216 std::vector<float> get_xy() const;
217
223 std::vector<float> get_y() const;
224
238 size_t nearest_point(const glm::vec2 &xy) const;
239
246 void set_points(const std::vector<float> &x, const std::vector<float> &y);
247
252 void set_values(const std::vector<float> &new_values);
253
261 void set_values(float new_value);
262
275 void set_values_from_array(const Array &array,
276 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
277
288 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
289
300
305
310 size_t size() const;
311
312 // ==========================================================================
313 // Basic Ops
314 // ==========================================================================
315
319 void clear();
320
324 void print();
325
332 void randomize(std::uint32_t seed, glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
333
340 void remap_values(float vmin, float vmax);
341
353 void shuffle(float dx, float dy, std::uint32_t seed, float dv = 0.f);
354
366 void snap_points_to_bounding_box(const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f},
367 float tolerance_ratio = 1.f);
368
369 // ==========================================================================
370 // Conversion / IO
371 // ==========================================================================
372
392 bool from_csv(const std::string &fname);
393
407 void to_array(Array &array, glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f}) const;
408
410 Array to_array(glm::ivec2 shape, glm::vec4 bbox) const;
411
439 void to_array_interp(Array &array,
440 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
441 InterpolationMethod2D interpolation_method =
443 Array *p_noise_x = nullptr,
444 Array *p_noise_y = nullptr,
445 glm::vec4 bbox_array = {0.f, 1.f, 0.f, 1.f}) const;
446
451 void to_csv(const std::string &fname) const;
452
457 Graph to_graph_delaunay();
458
475 void to_png(const std::string &fname,
476 int cmap,
477 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
478 int depth = CV_8U,
479 glm::ivec2 shape = {512, 512});
480
485 std::vector<glm::vec3> to_vec3() const;
486};
487
488// ==========================================================================
489// Functions
490// ==========================================================================
491
502Array cloud_sdf_to_array(const Cloud &cloud,
503 glm::ivec2 shape,
504 glm::vec4 bbox_array = {0.f, 1.f, 0.f, 1.f},
505 const Array *p_noise_x = nullptr,
506 const Array *p_noise_y = nullptr);
507
520std::vector<float> interpolate_values_from_array(const Cloud &cloud,
521 const Array &array,
522 glm::vec4 bbox);
523
535Cloud merge_cloud(const Cloud &cloud1, const Cloud &cloud2);
536
542Cloud merge_clouds(const std::vector<Cloud> &clouds);
543
565Cloud random_cloud(
566 size_t count,
567 std::uint32_t seed,
569 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
570
594Cloud random_cloud_density(size_t count,
595 const Array &density,
596 std::uint32_t seed,
597 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
598
622Cloud random_cloud_distance(float min_dist,
623 std::uint32_t seed,
624 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
625
651Cloud random_cloud_distance(float min_dist,
652 float max_dist,
653 const Array &density,
654 std::uint32_t seed,
655 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
656
683 float dist_min,
684 float dist_max,
685 float alpha,
686 std::uint32_t seed,
687 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
688
715 float dist_min,
716 float lambda,
717 float k,
718 std::uint32_t seed,
719 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
720
745Cloud random_cloud_jittered(size_t count,
746 const glm::vec2 &jitter_amount,
747 const glm::vec2 &stagger_ratio,
748 std::uint32_t seed,
749 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
750
765void rejection_filter_density(Cloud &cloud,
766 const Array &density_mask,
767 std::uint32_t seed,
768 const glm::vec4 &bbox = {0.f, 1.f, 0.f, 1.f});
769
770} // 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:49
void clear()
Clear all data from the cloud.
Definition cloud.cpp:102
Point get_center() const
Calculates the centroid of a set of points.
Definition cloud.cpp:186
void remap_values(float vmin, float vmax)
Remap the values of the cloud points to a target range.
Definition cloud.cpp:322
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:499
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:486
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:374
bool from_csv(const std::string &fname)
Loads point data from a CSV file into the Cloud object.
Definition cloud.cpp:107
std::vector< float > get_values() const
Get the values assigned to the points in the cloud.
Definition cloud.cpp:212
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:368
void set_values(const std::vector< float > &new_values)
Set new values for the cloud points.
Definition cloud.cpp:353
std::vector< glm::vec3 > to_vec3() const
Convert path points to a vector of 3D positions.
Definition cloud.cpp:596
void set_points(const std::vector< float > &x, const std::vector< float > &y)
Set points of the using x, y coordinates.
Definition cloud.cpp:341
Cloud()
Default constructor for the Cloud class.
Definition cloud.hpp:62
Cloud(const std::vector< Point > &points)
Constructs a new Cloud object based on a list of existing points.
Definition cloud.hpp:82
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:421
float get_values_min() const
Get the minimum value among the points in the cloud.
Definition cloud.cpp:229
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:311
std::vector< float > get_x() const
Get the x coordinates of the points in the cloud.
Definition cloud.cpp:237
std::vector< Point > points
Points of the cloud.
Definition cloud.hpp:51
std::vector< float > get_xy() const
Get the concatenated x and y coordinates of the points in the cloud.
Definition cloud.cpp:246
void add_point(const Point &p)
Add a new point to the cloud.
Definition cloud.cpp:97
float get_values_max() const
Get the maximum value among the points in the cloud.
Definition cloud.cpp:221
std::vector< float > get_y() const
Get the y coordinates of the points in the cloud.
Definition cloud.cpp:258
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:524
size_t nearest_point(const glm::vec2 &xy) const
Find the index of the nearest point in the cloud.
Definition cloud.cpp:267
void set_values_from_min_distance()
Sets point values based on the distance to their nearest neighbor.
Definition cloud.cpp:404
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:585
size_t size() const
Get the number of points in the cloud.
Definition cloud.cpp:416
Graph to_graph_delaunay()
Convert the cloud to a graph using Delaunay triangulation.
Definition cloud.cpp:567
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:385
glm::vec4 get_bbox() const
Get the bounding box of the cloud.
Definition cloud.cpp:169
void to_csv(const std::string &fname) const
Export the cloud data to a CSV file.
Definition cloud.cpp:553
void print()
Print information about the cloud's points.
Definition cloud.cpp:287
void remove_point(int point_idx)
Remove a point from the cloud.
Definition cloud.cpp:336
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:196
Header file for 2D interpolation methods.
Definition algebra.hpp:23
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:708
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:693
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:108
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:719
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:22
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:67
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:673
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:607
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:653
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:663
Cloud merge_clouds(const std::vector< Cloud > &clouds)
Merges multiple point clouds into a single cloud.
Definition cloud.cpp:624