HighMap library (C++)
Loading...
Searching...
No Matches
point.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
25#pragma once
26#include <cmath>
27#include <optional>
28
29#include "highmap/array.hpp"
30
31namespace hmap
32{
33
38class Point
39{
40public:
41 float x;
42 float y;
43 float v;
44
48 Point() : x(0.f), y(0.f), v(0.f)
49 {
50 }
51
58 Point(float x, float y, float v = 0.f) : x(x), y(y), v(v)
59 {
60 }
61
68 bool operator==(const Point &other) const
69 {
70 return (x == other.x && y == other.y && v == other.v);
71 }
72
79 bool operator!=(const Point &other) const
80 {
81 return !(*this == other);
82 }
83
89 Point operator+(const Point &other) const
90 {
91 return Point(x + other.x, y + other.y, v + other.v);
92 }
93
99 Point operator-(const Point &other) const
100 {
101 return Point(x - other.x, y - other.y, v - other.v);
102 }
103
109 Point operator*(float scalar) const
110 {
111 return Point(x * scalar, y * scalar, v * scalar);
112 }
113
119 Point operator/(float scalar) const
120 {
121 return Point(x / scalar, y / scalar, v / scalar);
122 }
123
136 friend Point operator*(float scalar, const Point &point)
137 {
138 return Point(scalar * point.x, scalar * point.y, scalar * point.v);
139 }
140
148 void print();
149
174 void set_value_from_array(const Array &array, glm::vec4 bbox);
175};
176
189float angle(const Point &p1, const Point &p2);
190
210float angle(const Point &p0, const Point &p1, const Point &p2);
211
229float cross_product(const Point &p0, const Point &p1, const Point &p2);
230float cross_product(const Point &p1, const Point &p2);
231
240float curvature(const Point &p1, const Point &p2, const Point &p3);
241
251float distance(const Point &p1, const Point &p2);
252
275Point interp_bezier(const Point &p_start,
276 const Point &p_ctrl_start,
277 const Point &p_ctrl_end,
278 const Point &p_end,
279 float t);
280
304Point interp_bspline(const Point &p0,
305 const Point &p1,
306 const Point &p2,
307 const Point &p3,
308 float t);
309
333Point interp_catmullrom(const Point &p0,
334 const Point &p1,
335 const Point &p2,
336 const Point &p3,
337 float t);
338
362Point interp_decasteljau(const std::vector<Point> &points, float t);
363
377glm::vec4 intersect_bounding_boxes(const glm::vec4 &bbox1,
378 const glm::vec4 &bbox2);
379
394bool is_point_within_bounding_box(Point p, glm::vec4 bbox);
395
410bool is_point_within_bounding_box(float x, float y, glm::vec4 bbox);
411
427Point lerp(const Point &p1, const Point &p2, float t);
428
454Point midpoint(const Point &p1,
455 const Point &p2,
456 int orientation,
457 float distance_ratio,
458 float t = 0.5f);
459
470std::optional<Point> segment_intersection(const Point &p1,
471 const Point &p2,
472 const Point &q1,
473 const Point &q2);
474
486void sort_points(std::vector<Point> &points);
487
501float triangle_area(const Point &p1, const Point &p2, const Point &p3);
502
512glm::vec4 unit_square_bbox();
513
514} // 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:39
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:68
Point operator-(const Point &other) const
Subtracts two points.
Definition point.hpp:99
Point operator/(float scalar) const
Divides the point by a scalar.
Definition point.hpp:119
Point()
Default constructor initializing the point to (0, 0, 0).
Definition point.hpp:48
float y
The y-coordinate of the point.
Definition point.hpp:42
friend Point operator*(float scalar, const Point &point)
Scalar multiplication (scalar * Vec2).
Definition point.hpp:136
Point operator+(const Point &other) const
Adds two points.
Definition point.hpp:89
Point operator*(float scalar) const
Multiplies the point by a scalar.
Definition point.hpp:109
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:41
float v
The value at the point.
Definition point.hpp:43
bool operator!=(const Point &other) const
Inequality operator to check if two points are different.
Definition point.hpp:79
Point(float x, float y, float v=0.f)
Parameterized constructor initializing the point to given values.
Definition point.hpp:58
Definition algebra.hpp:23
float curvature(const Point &p1, const Point &p2, const Point &p3)
Calculates the curvature formed by three points in 2D space.
Definition points.cpp:88
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:195
float distance(const Point &p1, const Point &p2)
Calculates the distance between two points.
Definition points.cpp:98
glm::vec4 intersect_bounding_boxes(const glm::vec4 &bbox1, const glm::vec4 &bbox2)
Determines the intersection of two bounding boxes.
Definition points.cpp:161
void sort_points(std::vector< Point > &points)
Sorts a vector of points in ascending order based on their coordinates.
Definition points.cpp:252
bool is_point_within_bounding_box(Point p, glm::vec4 bbox)
Checks if a point is within a specified bounding box.
Definition points.cpp:180
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:71
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:257
Point interp_decasteljau(const std::vector< Point > &points, float t)
Performs a De Casteljau algorithm-based interpolation for Bezier curves.
Definition points.cpp:150
float angle(const Point &p1, const Point &p2)
Computes the angle between two points relative to the x-axis.
Definition points.cpp:42
Point lerp(const Point &p1, const Point &p2, float t)
Linearly interpolates between two points.
Definition points.cpp:190
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:105
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:136
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:224
glm::vec4 unit_square_bbox()
Constructs a 4D bounding box for a unit square.
Definition points.cpp:263
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:121