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"
14
15namespace hmap
16{
17
24class Point
25{
26public:
27 float x;
28 float y;
29 float v;
30
34 Point() : x(0.f), y(0.f), v(0.f)
35 {
36 }
37
44 Point(float x, float y, float v = 0.f) : x(x), y(y), v(v)
45 {
46 }
47
54 bool operator==(const Point &other) const
55 {
56 return (x == other.x && y == other.y && v == other.v);
57 }
58
65 bool operator!=(const Point &other) const
66 {
67 return !(*this == other);
68 }
69
75 Point operator+(const Point &other) const
76 {
77 return Point(x + other.x, y + other.y, v + other.v);
78 }
79
85 Point operator-(const Point &other) const
86 {
87 return Point(x - other.x, y - other.y, v - other.v);
88 }
89
95 Point operator*(float scalar) const
96 {
97 return Point(x * scalar, y * scalar, v * scalar);
98 }
99
105 Point operator/(float scalar) const
106 {
107 return Point(x / scalar, y / scalar, v / scalar);
108 }
109
122 friend Point operator*(float scalar, const Point &point)
123 {
124 return Point(scalar * point.x, scalar * point.y, scalar * point.v);
125 }
126
134 void print();
135
160 void set_value_from_array(const Array &array, glm::vec4 bbox);
161};
162
175float angle(const Point &p1, const Point &p2);
176
196float angle(const Point &p0, const Point &p1, const Point &p2);
197
198float classify_point(const Point &p_prev,
199 const Point &p,
200 const Point &p_next,
201 const Point &pq);
202
220float cross_product(const Point &p0, const Point &p1, const Point &p2);
221float cross_product(const Point &p1, const Point &p2);
222
231float curvature(const Point &p1, const Point &p2, const Point &p3);
232
233/* see hmap::curvature */
234float curvature_signed(const Point &p1, const Point &p2, const Point &p3);
235
245float distance(const Point &p1, const Point &p2);
246
269Point interp_bezier(const Point &p_start,
270 const Point &p_ctrl_start,
271 const Point &p_ctrl_end,
272 const Point &p_end,
273 float t);
274
298Point interp_bspline(const Point &p0,
299 const Point &p1,
300 const Point &p2,
301 const Point &p3,
302 float t);
303
327Point interp_catmullrom(const Point &p0,
328 const Point &p1,
329 const Point &p2,
330 const Point &p3,
331 float t);
332
356Point interp_decasteljau(const std::vector<Point> &points, float t);
357
371glm::vec4 intersect_bounding_boxes(const glm::vec4 &bbox1,
372 const glm::vec4 &bbox2);
373
388bool is_point_within_bounding_box(Point p, glm::vec4 bbox);
389
404bool is_point_within_bounding_box(float x, float y, glm::vec4 bbox);
405
421Point lerp(const Point &p1, const Point &p2, float t);
422
448Point midpoint(const Point &p1,
449 const Point &p2,
450 int orientation,
451 float distance_ratio,
452 float t = 0.5f);
453
464std::optional<Point> segment_intersection(const Point &p1,
465 const Point &p2,
466 const Point &q1,
467 const Point &q2);
468
484float side(const Point &p1,
485 const Point &p2,
486 const Point &p3,
487 const Point &p_query);
488
500void sort_points(std::vector<Point> &points);
501
515float triangle_area(const Point &p1, const Point &p2, const Point &p3);
516
517/* see hmap::triangle_area */
518float triangle_area_signed(const Point &p1, const Point &p2, const Point &p3);
519
529glm::vec4 unit_square_bbox();
530
531} // 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:32
A class to represent and manipulate 2D points that can carry a value.
Definition point.hpp:25
void set_value_from_array(const Array &array, glm::vec4 bbox)
Updates the point's value based on bilinear interpolation from an array.
Definition points.cpp:17
bool operator==(const Point &other) const
Equality operator to check if two points are the same.
Definition point.hpp:54
Point operator-(const Point &other) const
Subtracts two points.
Definition point.hpp:85
Point operator/(float scalar) const
Divides the point by a scalar.
Definition point.hpp:105
Point()
Default constructor initializing the point to (0, 0, 0).
Definition point.hpp:34
float y
The y-coordinate of the point.
Definition point.hpp:28
friend Point operator*(float scalar, const Point &point)
Scalar multiplication (scalar * Vec2).
Definition point.hpp:122
Point operator+(const Point &other) const
Adds two points.
Definition point.hpp:75
Point operator*(float scalar) const
Multiplies the point by a scalar.
Definition point.hpp:95
void print()
Prints the coordinates and value of the Point object.
Definition points.cpp:40
float x
The x-coordinate of the point.
Definition point.hpp:27
float v
The value at the point.
Definition point.hpp:29
bool operator!=(const Point &other) const
Inequality operator to check if two points are different.
Definition point.hpp:65
Point(float x, float y, float v=0.f)
Parameterized constructor initializing the point to given values.
Definition point.hpp:44
Definition algebra.hpp:23
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 at a given point.
Definition points.cpp:281
float curvature(const Point &p1, const Point &p2, const Point &p3)
Calculates the curvature formed by three points in 2D space.
Definition points.cpp:107
Point midpoint(const Point &p1, const Point &p2, int orientation, float distance_ratio, float t=0.5f)
Computes the midpoint displacement in 1D with a perpendicular displacement.
Definition points.cpp:224
float distance(const Point &p1, const Point &p2)
Calculates the distance between two points.
Definition points.cpp:127
float triangle_area_signed(const Point &p1, const Point &p2, const Point &p3)
Definition points.cpp:312
glm::vec4 intersect_bounding_boxes(const glm::vec4 &bbox1, const glm::vec4 &bbox2)
Determines the intersection of two bounding boxes.
Definition points.cpp:190
float classify_point(const Point &p_prev, const Point &p, const Point &p_next, const Point &pq)
Definition points.cpp:75
void sort_points(std::vector< Point > &points)
Sorts a vector of points in ascending order based on their coordinates.
Definition points.cpp:302
bool is_point_within_bounding_box(Point p, glm::vec4 bbox)
Checks if a point is within a specified bounding box.
Definition points.cpp:209
float cross_product(const Point &p0, const Point &p1, const Point &p2)
Computes the 2D cross product of vectors formed by three points.
Definition points.cpp:90
float triangle_area(const Point &p1, const Point &p2, const Point &p3)
Calculates the area of a triangle formed by three points in 2D space.
Definition points.cpp:307
Point interp_decasteljau(const std::vector< Point > &points, float t)
Performs a De Casteljau algorithm-based interpolation for Bezier curves.
Definition points.cpp:179
float angle(const Point &p1, const Point &p2)
Computes the angle between two points relative to the x-axis.
Definition points.cpp:46
float curvature_signed(const Point &p1, const Point &p2, const Point &p3)
Definition points.cpp:117
Point lerp(const Point &p1, const Point &p2, float t)
Linearly interpolates between two points.
Definition points.cpp:219
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:134
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:165
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:253
glm::vec4 unit_square_bbox()
Constructs a 4D bounding box for a unit square.
Definition points.cpp:319
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:150