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
45
46class Graph;
47
60class Cloud
61{
62public:
63 // ==========================================================================
64 // Constructors
65 // ==========================================================================
66
73
74 virtual ~Cloud() = default;
75
85 Cloud(int npoints, std::uint32_t seed, const BBox2D &bbox = BBox2D{});
86
92 Cloud(const std::vector<Point> &points);
93
99 Cloud(std::vector<Point> &&points) noexcept;
100
109 Cloud(const std::vector<float> &x,
110 const std::vector<float> &y,
111 float default_value = 0.f);
112
121 Cloud(const std::vector<float> &x,
122 const std::vector<float> &y,
123 const std::vector<float> &v);
124
135 Cloud(const std::vector<glm::ivec2> &indices,
136 const glm::ivec2 &shape,
137 const BBox2D &bbox = BBox2D{});
138
142 Cloud(const std::vector<glm::vec3> &xyv);
143
144 // ==========================================================================
145 // Accessors
146 // ==========================================================================
147
154 BBox2D get_bbox() const;
155
167 Point get_center() const;
168
182 std::vector<int> get_convex_hull() const;
183
189 std::vector<float> get_values() const;
190
195 float get_values_max() const;
196
201 float get_values_min() const;
202
208 std::vector<float> get_x() const;
209
220 std::vector<float> get_xy() const;
221
227 std::vector<float> get_y() const;
228
242 size_t nearest_point(const glm::vec2 &xy) const;
243
250 void set_points(const std::vector<float> &x, const std::vector<float> &y);
251
256 void set_values(const std::vector<float> &new_values);
257
265 void set_values(float new_value);
266
279 void set_values_from_array(const Array &array, const BBox2D &bbox = BBox2D{});
280
294 void set_values_from_array(const Array &array,
295 ArraySampling sampling,
296 float radius = 0.f,
297 const BBox2D &bbox = BBox2D{});
298
308 void set_values_from_border_distance(const BBox2D &bbox = BBox2D{});
309
320
325
326 // ==========================================================================
327 // Container Interface
328 // ==========================================================================
329
333 Point &operator[](size_t index);
334
338 const Point &operator[](size_t index) const;
339
343 Point &at(size_t index);
344
348 const Point &at(size_t index) const;
349
353 Point &back();
354
358 const Point &back() const;
359
363 std::vector<Point>::iterator begin() noexcept;
364
368 std::vector<Point>::const_iterator begin() const noexcept;
369
374 size_t capacity() const noexcept;
375
379 std::vector<Point>::const_iterator cbegin() const noexcept;
380
384 std::vector<Point>::const_iterator cend() const noexcept;
385
389 void clear();
390
394 Point *data() noexcept;
395
399 const Point *data() const noexcept;
400
404 void emplace_back(float x, float y, float v = 0.f);
405
410 bool empty() const;
411
415 std::vector<Point>::iterator end() noexcept;
416
420 std::vector<Point>::const_iterator end() const noexcept;
421
425 std::vector<Point>::iterator erase(std::vector<Point>::const_iterator pos);
426
430 std::vector<Point>::iterator erase(std::vector<Point>::const_iterator first,
431 std::vector<Point>::const_iterator last);
432
436 Point &front();
437
441 const Point &front() const;
442
446 std::vector<Point>::iterator insert(std::vector<Point>::const_iterator pos,
447 const Point &value);
448
452 std::vector<Point>::iterator insert(std::vector<Point>::const_iterator pos,
453 Point &&value);
454
458 template <typename InputIt>
459 std::vector<Point>::iterator insert(std::vector<Point>::const_iterator pos,
460 InputIt first,
461 InputIt last)
462 {
463 return this->points.insert(pos, first, last);
464 }
465
469 void pop_back();
470
474 void push_back(const Point &p);
475
479 void push_back(Point &&p);
480
484 void reserve(size_t new_cap);
485
490 size_t size() const;
491
492 // ==========================================================================
493 // Basic Ops
494 // ==========================================================================
495
499 void print() const;
500
507 void randomize(std::uint32_t seed, const BBox2D &bbox = BBox2D{});
508
515 void remap_values(float vmin, float vmax);
516
528 void shuffle(float dx, float dy, std::uint32_t seed, float dv = 0.f);
529
541 void snap_points_to_bounding_box(const BBox2D &bbox = BBox2D{},
542 float tolerance_ratio = 1.f);
543
544 // ==========================================================================
545 // Conversion / IO
546 // ==========================================================================
547
567 bool from_csv(const std::string &fname);
568
582 void to_array(Array &array, const BBox2D &bbox = BBox2D{}) const;
583
585 Array to_array(glm::ivec2 shape, const BBox2D &bbox) const;
586
614 void to_array_interp(Array &array,
615 const BBox2D &bbox = BBox2D{},
616 InterpolationMethod2D interpolation_method =
618 const Array *p_noise_x = nullptr,
619 const Array *p_noise_y = nullptr,
620 const BBox2D &bbox_array = BBox2D{}) const;
621
626 void to_csv(const std::string &fname) const;
627
632 Graph to_graph_delaunay() const;
633
650 void to_png(const std::string &fname,
651 int cmap,
652 const BBox2D &bbox = BBox2D{},
653 int depth = 0,
654 glm::ivec2 shape = {512, 512}) const;
655
660 std::vector<glm::vec3> to_vec3() const;
661
662protected:
663 std::vector<Point> points = {};
664};
665
666// ==========================================================================
667// Functions
668// ==========================================================================
669
680Array cloud_sdf_to_array(const Cloud &cloud,
681 glm::ivec2 shape,
682 const BBox2D &bbox_array = BBox2D::unit(),
683 const Array *p_noise_x = nullptr,
684 const Array *p_noise_y = nullptr);
685
694bool has_duplicates(const Cloud &cloud, float eps = 1e-9f, bool xy_only = true);
695
708std::vector<float> interpolate_values_from_array(const Cloud &cloud,
709 const Array &array,
710 const BBox2D &bbox);
711
723Cloud merge_cloud(const Cloud &cloud1, const Cloud &cloud2);
724
730Cloud merge_clouds(const std::vector<Cloud> &clouds);
731
753Cloud random_cloud(
754 size_t count,
755 std::uint32_t seed,
757 const BBox2D &bbox = BBox2D{});
758
782Cloud random_cloud_density(size_t count,
783 const Array &density,
784 std::uint32_t seed,
785 const BBox2D &bbox = BBox2D{});
786
800 const Array &density,
801 std::uint32_t seed,
802 const BBox2D &bbox = BBox2D{});
803
827Cloud random_cloud_distance(float min_dist,
828 std::uint32_t seed,
829 const BBox2D &bbox = BBox2D{});
830
856Cloud random_cloud_distance(float min_dist,
857 float max_dist,
858 const Array &density,
859 std::uint32_t seed,
860 const BBox2D &bbox = BBox2D{});
861
888 float dist_max,
889 float alpha,
890 std::uint32_t seed,
891 const BBox2D &bbox = BBox2D{});
892
919 float lambda,
920 float k,
921 std::uint32_t seed,
922 const BBox2D &bbox = BBox2D{});
923
949Cloud random_cloud_jittered(size_t count,
950 const glm::vec2 &jitter_amount,
951 const glm::vec2 &stagger_ratio,
952 std::uint32_t seed,
953 const BBox2D &bbox = BBox2D{});
954
970 const Array &density_mask,
971 std::uint32_t seed,
972 const BBox2D &bbox = BBox2D{});
973
988Cloud scale(const Cloud &cloud,
989 glm::vec2 scale,
990 glm::vec2 center = {0.5f, 0.5f});
991
992Cloud scale(const Cloud &cloud, float scale, glm::vec2 center = {0.5f, 0.5f});
993
994} // namespace hmap
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
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
void pop_back()
Removes the last point.
Definition cloud.cpp:658
Point & back()
Access the last point.
Definition cloud.cpp:551
std::vector< Point >::iterator end() noexcept
Returns an iterator to the end.
Definition cloud.cpp:611
size_t capacity() const noexcept
Returns the number of points that can be held in currently allocated storage.
Definition cloud.cpp:571
void clear()
Clear all data from the cloud.
Definition cloud.cpp:586
std::vector< Point >::const_iterator cend() const noexcept
Returns a const iterator to the end.
Definition cloud.cpp:581
Point get_center() const
Calculates the centroid of a set of points.
Definition cloud.cpp:188
void remap_values(float vmin, float vmax)
Remap the values of the cloud points to a target range.
Definition cloud.cpp:335
virtual ~Cloud()=default
void print() const
Print information about the cloud's points.
Definition cloud.cpp:300
std::vector< Point >::iterator begin() noexcept
Returns an iterator to the first point.
Definition cloud.cpp:561
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:731
bool from_csv(const std::string &fname)
Loads point data from a CSV file into the Cloud object.
Definition cloud.cpp:109
Point * data() noexcept
Direct pointer to the underlying point array.
Definition cloud.cpp:591
std::vector< float > get_values() const
Get the values assigned to the points in the cloud.
Definition cloud.cpp:219
void to_png(const std::string &fname, int cmap, const BBox2D &bbox=BBox2D{}, int depth=0, glm::ivec2 shape={512, 512}) const
Saves the current data as a PNG image file.
Definition cloud.cpp:840
void set_values(const std::vector< float > &new_values)
Set new values for the cloud points.
Definition cloud.cpp:363
std::vector< glm::vec3 > to_vec3() const
Convert path points to a vector of 3D positions.
Definition cloud.cpp:853
void reserve(size_t new_cap)
Reserves storage for at least the specified number of points.
Definition cloud.cpp:673
std::vector< Point >::iterator insert(std::vector< Point >::const_iterator pos, const Point &value)
Inserts a point before pos.
Definition cloud.cpp:644
void set_points(const std::vector< float > &x, const std::vector< float > &y)
Set points of the using x, y coordinates.
Definition cloud.cpp:351
Cloud()
Default constructor for the Cloud class.
BBox2D get_bbox() const
Get the bounding box of the cloud.
Definition cloud.cpp:168
void to_array_interp(Array &array, const BBox2D &bbox=BBox2D{}, InterpolationMethod2D interpolation_method=InterpolationMethod2D::ITP2D_DELAUNAY, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, const BBox2D &bbox_array=BBox2D{}) const
Interpolate the values of an array using the cloud points.
Definition cloud.cpp:777
Point & at(size_t index)
Access point with bounds checking.
Definition cloud.cpp:541
bool empty() const
Check whether the cloud has no points.
Definition cloud.cpp:606
Point & front()
Access the first point.
Definition cloud.cpp:634
float get_values_min() const
Get the minimum value among the points in the cloud.
Definition cloud.cpp:239
void to_array(Array &array, const BBox2D &bbox=BBox2D{}) const
Project the cloud points onto an array.
Definition cloud.cpp:744
void randomize(std::uint32_t seed, const BBox2D &bbox=BBox2D{})
Randomize the positions and values of the cloud points.
Definition cloud.cpp:324
std::vector< float > get_x() const
Get the x coordinates of the points in the cloud.
Definition cloud.cpp:250
Graph to_graph_delaunay() const
Convert the cloud to a graph using Delaunay triangulation.
Definition cloud.cpp:822
std::vector< Point > points
Points of the cloud.
Definition cloud.hpp:663
std::vector< float > get_xy() const
Get the concatenated x and y coordinates of the points in the cloud.
Definition cloud.cpp:259
Point & operator[](size_t index)
Access point by index.
Definition cloud.cpp:531
float get_values_max() const
Get the maximum value among the points in the cloud.
Definition cloud.cpp:228
std::vector< float > get_y() const
Get the y coordinates of the points in the cloud.
Definition cloud.cpp:271
void set_values_from_array(const Array &array, const BBox2D &bbox=BBox2D{})
Set the values of the cloud points using values from an underlying array.
Definition cloud.cpp:378
void push_back(const Point &p)
Appends a point.
Definition cloud.cpp:663
size_t nearest_point(const glm::vec2 &xy) const
Find the index of the nearest point in the cloud.
Definition cloud.cpp:280
void set_values_from_min_distance()
Sets point values based on the distance to their nearest neighbor.
Definition cloud.cpp:504
std::vector< Point >::const_iterator cbegin() const noexcept
Returns a const iterator to the first point.
Definition cloud.cpp:576
size_t size() const
Get the number of points in the cloud.
Definition cloud.cpp:678
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:482
void snap_points_to_bounding_box(const BBox2D &bbox=BBox2D{}, float tolerance_ratio=1.f)
Snap points to the bounding box edges and corners.
Definition cloud.cpp:683
void to_csv(const std::string &fname) const
Export the cloud data to a CSV file.
Definition cloud.cpp:808
void set_values_from_border_distance(const BBox2D &bbox=BBox2D{})
Sets point values based on their distance to the bounding box border.
Definition cloud.cpp:471
void emplace_back(float x, float y, float v=0.f)
Appends a new point in-place.
Definition cloud.cpp:601
std::vector< Point >::iterator erase(std::vector< Point >::const_iterator pos)
Erases the point at the specified position.
Definition cloud.cpp:621
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:198
A class to represent and manipulate 2D points that can carry a value.
Definition point.hpp:26
float f(int i, float gi)
Definition distance_transform.cpp:14
Header file for 2D interpolation methods.
Definition opencv.hpp:20
Cloud random_cloud_jittered(size_t count, const glm::vec2 &jitter_amount, const glm::vec2 &stagger_ratio, std::uint32_t seed, const BBox2D &bbox=BBox2D{})
Generates a jittered grid cloud of points.
Definition cloud.cpp:971
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
Cloud random_cloud_distance_weibull(float dist_min, float lambda, float k, std::uint32_t seed, const BBox2D &bbox=BBox2D{})
Generates a random cloud of points with distances drawn from a Weibull distribution.
Definition cloud.cpp:960
ArraySampling
Strategy used to extract a point value from an array.
Definition cloud.hpp:39
@ AS_MEAN
Average of the array values within a radius.
Definition cloud.hpp:43
@ AS_MIN
Minimum of the array values within a radius.
Definition cloud.hpp:41
@ AS_MAX
Maximum of the array values within a radius.
Definition cloud.hpp:42
@ AS_POINT
Array value at the point (interpolated).
Definition cloud.hpp:40
Cloud random_cloud_distance(float min_dist, std::uint32_t seed, const BBox2D &bbox=BBox2D{})
Generates a random cloud of points separated by at least a given minimum distance.
Definition cloud.cpp:923
InterpolationMethod2D
Enumeration of 2D interpolation methods.
Definition interpolate2d.hpp:43
@ ITP2D_DELAUNAY
Delaunay triangulation method for 2D interpolation.
Definition interpolate2d.hpp:44
Cloud random_cloud_inverse_sampling(size_t count, const Array &density, std::uint32_t seed, const BBox2D &bbox=BBox2D{})
Generates a random cloud of points using inverse transform sampling on a 2D density array.
Definition cloud.cpp:911
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
Cloud random_cloud(size_t count, std::uint32_t seed, const PointSamplingMethod &method=PointSamplingMethod::RND_LHS, const BBox2D &bbox=BBox2D{})
Generates a random cloud of points within a bounding box.
Definition cloud.cpp:889
PointSamplingMethod
Enumeration of point sampling methods.
Definition point_sampling.hpp:34
@ RND_LHS
Latin Hypercube Sampling.
Definition point_sampling.hpp:38
Cloud merge_cloud(const Cloud &cloud1, const Cloud &cloud2)
Merges two point clouds into one.
Definition cloud.cpp:864
std::vector< float > interpolate_values_from_array(const Cloud &cloud, const Array &array, const BBox2D &bbox)
Interpolate values from an array at the points' (x, y) locations.
Definition cloud_functions.cpp:98
void rejection_filter_density(Cloud &cloud, const Array &density_mask, std::uint32_t seed, const BBox2D &bbox=BBox2D{})
Filter a point cloud using rejection sampling based on a density mask.
Definition cloud_functions.cpp:141
Array cloud_sdf_to_array(const Cloud &cloud, glm::ivec2 shape, const BBox2D &bbox_array=BBox2D::unit(), 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
Cloud random_cloud_distance_power_law(float dist_min, float dist_max, float alpha, std::uint32_t seed, const BBox2D &bbox=BBox2D{})
Generates a random cloud of points with distances drawn from a power-law distribution.
Definition cloud.cpp:945
Cloud random_cloud_density(size_t count, const Array &density, std::uint32_t seed, const BBox2D &bbox=BBox2D{})
Generates a random cloud of points based on a spatial density map.
Definition cloud.cpp:899
Cloud merge_clouds(const std::vector< Cloud > &clouds)
Merges multiple point clouds into a single cloud.
Definition cloud.cpp:873
Axis-aligned 2D bounding box in world coordinates.
Definition bbox2d.hpp:25
static constexpr BBox2D unit()
Definition bbox2d.hpp:39