HighMap library (C++)
Loading...
Searching...
No Matches
point.hpp
Go to the documentation of this file.
1/* Copyright (c) 2025 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
9#pragma once
10#include <cmath>
11#include <optional>
12
13#include "highmap/array.hpp"
15
16namespace hmap
17{
18
25class Point
26{
27public:
28 float x;
29 float y;
30 float v;
31
32 // ==========================================================================
33 // Constructors
34 // ==========================================================================
35
39 constexpr Point() noexcept : x(0.f), y(0.f), v(0.f)
40 {
41 }
42
49 constexpr Point(float x, float y, float v = 0.f) noexcept : x(x), y(y), v(v)
50 {
51 }
52
58 constexpr explicit Point(const glm::vec2 &xy, float v = 0.f) noexcept
59 : x(xy.x), y(xy.y), v(v)
60 {
61 }
62
67 constexpr explicit Point(const glm::vec3 &xyv) noexcept
68 : x(xyv.x), y(xyv.y), v(xyv.z)
69 {
70 }
71
72 // ==========================================================================
73 // Conversions
74 // ==========================================================================
75
80 constexpr glm::vec2 to_vec2() const noexcept
81 {
82 return glm::vec2(x, y);
83 }
84
89 constexpr glm::vec3 to_vec3() const noexcept
90 {
91 return glm::vec3(x, y, v);
92 }
93
94 // ==========================================================================
95 // Operators
96 // ==========================================================================
97
104 constexpr bool operator==(const Point &other) const noexcept
105 {
106 return (x == other.x && y == other.y && v == other.v);
107 }
108
115 constexpr bool operator!=(const Point &other) const noexcept
116 {
117 return !(*this == other);
118 }
119
125 constexpr Point operator+(const Point &other) const noexcept
126 {
127 return Point(x + other.x, y + other.y, v + other.v);
128 }
129
135 constexpr Point operator-(const Point &other) const noexcept
136 {
137 return Point(x - other.x, y - other.y, v - other.v);
138 }
139
145 constexpr Point operator*(float scalar) const noexcept
146 {
147 return Point(x * scalar, y * scalar, v * scalar);
148 }
149
155 constexpr Point operator/(float scalar) const noexcept
156 {
157 return Point(x / scalar, y / scalar, v / scalar);
158 }
159
165 constexpr Point &operator+=(const Point &other) noexcept
166 {
167 x += other.x;
168 y += other.y;
169 v += other.v;
170 return *this;
171 }
172
178 constexpr Point &operator-=(const Point &other) noexcept
179 {
180 x -= other.x;
181 y -= other.y;
182 v -= other.v;
183 return *this;
184 }
185
191 constexpr Point &operator*=(float scalar) noexcept
192 {
193 x *= scalar;
194 y *= scalar;
195 v *= scalar;
196 return *this;
197 }
198
204 constexpr Point &operator/=(float scalar) noexcept
205 {
206 x /= scalar;
207 y /= scalar;
208 v /= scalar;
209 return *this;
210 }
211
219 friend constexpr Point operator*(float scalar, const Point &point) noexcept
220 {
221 return Point(scalar * point.x, scalar * point.y, scalar * point.v);
222 }
223
224 // ==========================================================================
225 // Methods
226 // ==========================================================================
227
234 void print() const;
235
244 void set_value_from_array(const Array &array, const BBox2D &bbox);
245};
246
247// ==========================================================================
248// Free Functions
249// ==========================================================================
250
258float angle(const Point &p1, const Point &p2);
259
268float angle(const Point &p0, const Point &p1, const Point &p2);
269
273float classify_point(const Point &p_prev,
274 const Point &p,
275 const Point &p_next,
276 const Point &pq);
277
286float cross_product(const Point &p0, const Point &p1, const Point &p2);
287
295float cross_product(const Point &p1, const Point &p2);
296
305float curvature(const Point &p1, const Point &p2, const Point &p3);
306
310float curvature_signed(const Point &p1, const Point &p2, const Point &p3);
311
319float distance(const Point &p1, const Point &p2);
320
328float distance_squared(const Point &p1, const Point &p2);
329
340Point interp_bezier(const Point &p_start,
341 const Point &p_ctrl_start,
342 const Point &p_ctrl_end,
343 const Point &p_end,
344 float t);
345
356Point interp_bspline(const Point &p0,
357 const Point &p1,
358 const Point &p2,
359 const Point &p3,
360 float t);
361
372Point interp_catmullrom(const Point &p0,
373 const Point &p1,
374 const Point &p2,
375 const Point &p3,
376 float t);
377
386Point interp_decasteljau(const std::vector<Point> &points, float t);
387
395BBox2D intersect_bounding_boxes(const BBox2D &bbox1, const BBox2D &bbox2);
396
404bool is_point_within_bounding_box(const Point &p, const BBox2D &bbox);
405
415bool is_point_within_bounding_box(float x, float y, const BBox2D &bbox);
416
425Point lerp(const Point &p1, const Point &p2, float t);
426
437Point midpoint(const Point &p1,
438 const Point &p2,
439 int orientation,
440 float distance_ratio,
441 float t = 0.5f);
442
451Point scale(const Point &p, glm::vec2 scale, glm::vec2 center = {0.5f, 0.5f});
452
456Point scale(const Point &p, float scale, glm::vec2 center = {0.5f, 0.5f});
457
468std::optional<Point> segment_intersection(const Point &p1,
469 const Point &p2,
470 const Point &q1,
471 const Point &q2);
472
483float side(const Point &p1,
484 const Point &p2,
485 const Point &p3,
486 const Point &p_query);
487
493void sort_points(std::vector<Point> &points);
494
503float triangle_area(const Point &p1, const Point &p2, const Point &p3);
504
513float triangle_area_signed(const Point &p1, const Point &p2, const Point &p3);
514
521
522} // namespace hmap
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Axis-aligned 2D bounding box in world coordinates.
Array class, helper to manipulate 2D float array with "(i, j)" indexing.
Definition array.hpp:31
A class to represent and manipulate 2D points that can carry a value.
Definition point.hpp:26
constexpr Point operator-(const Point &other) const noexcept
Subtracts two points.
Definition point.hpp:135
void print() const
Prints the coordinates and value of the Point object.
Definition points.cpp:19
constexpr Point & operator/=(float scalar) noexcept
Divides this point by a scalar in-place.
Definition point.hpp:204
constexpr glm::vec2 to_vec2() const noexcept
Converts the point's coordinates to glm::vec2.
Definition point.hpp:80
constexpr glm::vec3 to_vec3() const noexcept
Converts the point's coordinates and value to glm::vec3.
Definition point.hpp:89
constexpr Point & operator-=(const Point &other) noexcept
Subtracts another point from this point in-place.
Definition point.hpp:178
constexpr bool operator==(const Point &other) const noexcept
Equality operator to check if two points are the same.
Definition point.hpp:104
constexpr Point & operator*=(float scalar) noexcept
Multiplies this point by a scalar in-place.
Definition point.hpp:191
void set_value_from_array(const Array &array, const BBox2D &bbox)
Updates the point's value based on bilinear interpolation from an array.
Definition points.cpp:25
friend constexpr Point operator*(float scalar, const Point &point) noexcept
Scalar multiplication (scalar * Point).
Definition point.hpp:219
constexpr Point operator+(const Point &other) const noexcept
Adds two points.
Definition point.hpp:125
constexpr Point(const glm::vec2 &xy, float v=0.f) noexcept
Constructs a Point from a glm::vec2 and an optional value.
Definition point.hpp:58
constexpr Point & operator+=(const Point &other) noexcept
Adds another point to this point in-place.
Definition point.hpp:165
float y
The y-coordinate of the point.
Definition point.hpp:29
constexpr Point(float x, float y, float v=0.f) noexcept
Parameterized constructor initializing the point to given values.
Definition point.hpp:49
float x
The x-coordinate of the point.
Definition point.hpp:28
constexpr Point(const glm::vec3 &xyv) noexcept
Constructs a Point from a glm::vec3 (x, y, v).
Definition point.hpp:67
float v
The value at the point.
Definition point.hpp:30
constexpr Point operator*(float scalar) const noexcept
Multiplies the point by a scalar.
Definition point.hpp:145
constexpr Point() noexcept
Default constructor initializing the point to (0, 0, 0).
Definition point.hpp:39
constexpr Point operator/(float scalar) const noexcept
Divides the point by a scalar.
Definition point.hpp:155
constexpr bool operator!=(const Point &other) const noexcept
Inequality operator to check if two points are different.
Definition point.hpp:115
float f(int i, float gi)
Definition distance_transform.cpp:14
Definition opencv.hpp:20
bool is_point_within_bounding_box(const Point &p, const BBox2D &bbox)
Checks if a point is within a specified bounding box.
Definition points.cpp:253
float side(const Point &p1, const Point &p2, const Point &p3, const Point &p_query)
Determines the relative side of a query point with respect to a curve segment.
Definition points.cpp:332
float curvature(const Point &p1, const Point &p2, const Point &p3)
Calculates the curvature formed by three points in 2D space.
Definition points.cpp:119
Point midpoint(const Point &p1, const Point &p2, int orientation, float distance_ratio, float t=0.5f)
Computes the midpoint displacement with a perpendicular offset.
Definition points.cpp:272
float distance(const Point &p1, const Point &p2)
Calculates the Euclidean distance between two points.
Definition points.cpp:145
float triangle_area_signed(const Point &p1, const Point &p2, const Point &p3)
Calculates the signed area of a triangle formed by three points.
Definition points.cpp:373
float classify_point(const Point &p_prev, const Point &p, const Point &p_next, const Point &pq)
Classify point convexity relative to neighboring curve vertices.
Definition points.cpp:94
void sort_points(std::vector< Point > &points)
Sorts a vector of points in ascending order (x, then y, then v).
Definition points.cpp:363
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
float cross_product(const Point &p0, const Point &p1, const Point &p2)
Computes the 2D cross product of vectors (p1 - p0) and (p2 - p0).
Definition points.cpp:109
float distance_squared(const Point &p1, const Point &p2)
Calculates the squared Euclidean distance between two points.
Definition points.cpp:150
float triangle_area(const Point &p1, const Point &p2, const Point &p3)
Calculates the unsigned area of a triangle formed by three points.
Definition points.cpp:368
Point interp_decasteljau(const std::vector< Point > &points, float t)
Performs a De Casteljau algorithm-based interpolation for Bezier curves.
Definition points.cpp:218
float angle(const Point &p1, const Point &p2)
Computes the angle between two points relative to the x-axis.
Definition points.cpp:65
float curvature_signed(const Point &p1, const Point &p2, const Point &p3)
Calculates the signed curvature formed by three points in 2D space.
Definition points.cpp:132
Point lerp(const Point &p1, const Point &p2, float t)
Linearly interpolates between two points.
Definition points.cpp:265
BBox2D unit_square_bbox()
Constructs a 4D bounding box for a unit square {0, 1, 0, 1}.
Definition points.cpp:379
Point interp_bezier(const Point &p_start, const Point &p_ctrl_start, const Point &p_ctrl_end, const Point &p_end, float t)
Performs a cubic Bezier interpolation.
Definition points.cpp:157
Point interp_catmullrom(const Point &p0, const Point &p1, const Point &p2, const Point &p3, float t)
Performs a Catmull-Rom spline interpolation.
Definition points.cpp:199
BBox2D intersect_bounding_boxes(const BBox2D &bbox1, const BBox2D &bbox2)
Determines the intersection of two bounding boxes.
Definition points.cpp:237
std::optional< Point > segment_intersection(const Point &p1, const Point &p2, const Point &q1, const Point &q2)
Computes the intersection point of two 2D segments, if it exists.
Definition points.cpp:305
Point interp_bspline(const Point &p0, const Point &p1, const Point &p2, const Point &p3, float t)
Performs a cubic B-spline interpolation.
Definition points.cpp:180
Axis-aligned 2D bounding box in world coordinates.
Definition bbox2d.hpp:25