HighMap library (C++)
Loading...
Searching...
No Matches
point.hpp
Go to the documentation of this file.
1
7#pragma once
8#include <cmath>
9#include <optional>
10
11#include "highmap/array.hpp"
12
13namespace hmap
14{
15
22class Point
23{
24public:
25 float x;
26 float y;
27 float v;
28
32 Point() : x(0.f), y(0.f), v(0.f)
33 {
34 }
35
42 Point(float x, float y, float v = 0.f) : x(x), y(y), v(v)
43 {
44 }
45
52 bool operator==(const Point &other) const
53 {
54 return (x == other.x && y == other.y && v == other.v);
55 }
56
63 bool operator!=(const Point &other) const
64 {
65 return !(*this == other);
66 }
67
73 Point operator+(const Point &other) const
74 {
75 return Point(x + other.x, y + other.y, v + other.v);
76 }
77
83 Point operator-(const Point &other) const
84 {
85 return Point(x - other.x, y - other.y, v - other.v);
86 }
87
93 Point operator*(float scalar) const
94 {
95 return Point(x * scalar, y * scalar, v * scalar);
96 }
97
103 Point operator/(float scalar) const
104 {
105 return Point(x / scalar, y / scalar, v / scalar);
106 }
107
120 friend Point operator*(float scalar, const Point &point)
121 {
122 return Point(scalar * point.x, scalar * point.y, scalar * point.v);
123 }
124
132 void print();
133
158 void set_value_from_array(const Array &array, glm::vec4 bbox);
159};
160
173float angle(const Point &p1, const Point &p2);
174
194float angle(const Point &p0, const Point &p1, const Point &p2);
195
196float classify_point(const Point &p_prev,
197 const Point &p,
198 const Point &p_next,
199 const Point &pq);
200
218float cross_product(const Point &p0, const Point &p1, const Point &p2);
219float cross_product(const Point &p1, const Point &p2);
220
229float curvature(const Point &p1, const Point &p2, const Point &p3);
230
231/* see hmap::curvature */
232float curvature_signed(const Point &p1, const Point &p2, const Point &p3);
233
243float distance(const Point &p1, const Point &p2);
244
267Point interp_bezier(const Point &p_start,
268 const Point &p_ctrl_start,
269 const Point &p_ctrl_end,
270 const Point &p_end,
271 float t);
272
296Point interp_bspline(const Point &p0,
297 const Point &p1,
298 const Point &p2,
299 const Point &p3,
300 float t);
301
325Point interp_catmullrom(const Point &p0,
326 const Point &p1,
327 const Point &p2,
328 const Point &p3,
329 float t);
330
354Point interp_decasteljau(const std::vector<Point> &points, float t);
355
369glm::vec4 intersect_bounding_boxes(const glm::vec4 &bbox1,
370 const glm::vec4 &bbox2);
371
386bool is_point_within_bounding_box(Point p, glm::vec4 bbox);
387
402bool is_point_within_bounding_box(float x, float y, glm::vec4 bbox);
403
419Point lerp(const Point &p1, const Point &p2, float t);
420
446Point midpoint(const Point &p1,
447 const Point &p2,
448 int orientation,
449 float distance_ratio,
450 float t = 0.5f);
451
462std::optional<Point> segment_intersection(const Point &p1,
463 const Point &p2,
464 const Point &q1,
465 const Point &q2);
466
482float side(const Point &p1,
483 const Point &p2,
484 const Point &p3,
485 const Point &p_query);
486
498void sort_points(std::vector<Point> &points);
499
513float triangle_area(const Point &p1, const Point &p2, const Point &p3);
514
515/* see hmap::triangle_area */
516float triangle_area_signed(const Point &p1, const Point &p2, const Point &p3);
517
527glm::vec4 unit_square_bbox();
528
529} // 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:23
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:13
bool operator==(const Point &other) const
Equality operator to check if two points are the same.
Definition point.hpp:52
Point operator-(const Point &other) const
Subtracts two points.
Definition point.hpp:83
Point operator/(float scalar) const
Divides the point by a scalar.
Definition point.hpp:103
Point()
Default constructor initializing the point to (0, 0, 0).
Definition point.hpp:32
float y
The y-coordinate of the point.
Definition point.hpp:26
friend Point operator*(float scalar, const Point &point)
Scalar multiplication (scalar * Vec2).
Definition point.hpp:120
Point operator+(const Point &other) const
Adds two points.
Definition point.hpp:73
Point operator*(float scalar) const
Multiplies the point by a scalar.
Definition point.hpp:93
void print()
Prints the coordinates and value of the Point object.
Definition points.cpp:36
float x
The x-coordinate of the point.
Definition point.hpp:25
float v
The value at the point.
Definition point.hpp:27
bool operator!=(const Point &other) const
Inequality operator to check if two points are different.
Definition point.hpp:63
Point(float x, float y, float v=0.f)
Parameterized constructor initializing the point to given values.
Definition point.hpp:42
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:277
float curvature(const Point &p1, const Point &p2, const Point &p3)
Calculates the curvature formed by three points in 2D space.
Definition points.cpp:103
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:220
float distance(const Point &p1, const Point &p2)
Calculates the distance between two points.
Definition points.cpp:123
float triangle_area_signed(const Point &p1, const Point &p2, const Point &p3)
Definition points.cpp:308
glm::vec4 intersect_bounding_boxes(const glm::vec4 &bbox1, const glm::vec4 &bbox2)
Determines the intersection of two bounding boxes.
Definition points.cpp:186
float classify_point(const Point &p_prev, const Point &p, const Point &p_next, const Point &pq)
Definition points.cpp:71
void sort_points(std::vector< Point > &points)
Sorts a vector of points in ascending order based on their coordinates.
Definition points.cpp:298
bool is_point_within_bounding_box(Point p, glm::vec4 bbox)
Checks if a point is within a specified bounding box.
Definition points.cpp:205
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:86
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:303
Point interp_decasteljau(const std::vector< Point > &points, float t)
Performs a De Casteljau algorithm-based interpolation for Bezier curves.
Definition points.cpp:175
float angle(const Point &p1, const Point &p2)
Computes the angle between two points relative to the x-axis.
Definition points.cpp:42
float curvature_signed(const Point &p1, const Point &p2, const Point &p3)
Definition points.cpp:113
Point lerp(const Point &p1, const Point &p2, float t)
Linearly interpolates between two points.
Definition points.cpp:215
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:130
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:161
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:249
glm::vec4 unit_square_bbox()
Constructs a 4D bounding box for a unit square.
Definition points.cpp:315
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:146