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
28#include "highmap/array.hpp"
29
30namespace hmap
31{
32
37class Point
38{
39public:
40 float x;
41 float y;
42 float v;
43
47 Point() : x(0.f), y(0.f), v(0.f)
48 {
49 }
50
57 Point(float x, float y, float v = 0.f) : x(x), y(y), v(v)
58 {
59 }
60
67 bool operator==(const Point &other) const
68 {
69 return (x == other.x && y == other.y && v == other.v);
70 }
71
78 bool operator!=(const Point &other) const
79 {
80 return !(*this == other);
81 }
82
88 Point operator+(const Point &other) const
89 {
90 return Point(x + other.x, y + other.y, v + other.v);
91 }
92
98 Point operator-(const Point &other) const
99 {
100 return Point(x - other.x, y - other.y, v - other.v);
101 }
102
108 Point operator*(float scalar) const
109 {
110 return Point(x * scalar, y * scalar, v * scalar);
111 }
112
118 Point operator/(float scalar) const
119 {
120 return Point(x / scalar, y / scalar, v / scalar);
121 }
122
135 friend Point operator*(float scalar, const Point &point)
136 {
137 return Point(scalar * point.x, scalar * point.y, scalar * point.v);
138 }
139
147 void print();
148
173 void set_value_from_array(const Array &array, Vec4<float> bbox);
174};
175
188float angle(const Point &p1, const Point &p2);
189
209float angle(const Point &p0, const Point &p1, const Point &p2);
210
228float cross_product(const Point &p0, const Point &p1, const Point &p2);
229
238float curvature(const Point &p1, const Point &p2, const Point &p3);
239
249float distance(const Point &p1, const Point &p2);
250
273Point interp_bezier(const Point &p_start,
274 const Point &p_ctrl_start,
275 const Point &p_ctrl_end,
276 const Point &p_end,
277 float t);
278
302Point interp_bspline(const Point &p0,
303 const Point &p1,
304 const Point &p2,
305 const Point &p3,
306 float t);
307
331Point interp_catmullrom(const Point &p0,
332 const Point &p1,
333 const Point &p2,
334 const Point &p3,
335 float t);
336
360Point interp_decasteljau(const std::vector<Point> &points, float t);
361
375Vec4<float> intersect_bounding_boxes(const Vec4<float> &bbox1,
376 const Vec4<float> &bbox2);
377
392bool is_point_within_bounding_box(Point p, Vec4<float> bbox);
393
408bool is_point_within_bounding_box(float x, float y, Vec4<float> bbox);
409
425Point lerp(const Point &p1, const Point &p2, float t);
426
452Point midpoint(const Point &p1,
453 const Point &p2,
454 int orientation,
455 float distance_ratio,
456 float t = 0.5f);
457
469void sort_points(std::vector<Point> &points);
470
484float triangle_area(const Point &p1, const Point &p2, const Point &p3);
485
495Vec4<float> unit_square_bbox();
496
497} // 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:38
bool operator==(const Point &other) const
Equality operator to check if two points are the same.
Definition point.hpp:67
Point operator-(const Point &other) const
Subtracts two points.
Definition point.hpp:98
void set_value_from_array(const Array &array, Vec4< float > bbox)
Updates the point's value based on bilinear interpolation from an array.
Definition points.cpp:13
Point operator/(float scalar) const
Divides the point by a scalar.
Definition point.hpp:118
Point()
Default constructor initializing the point to (0, 0, 0).
Definition point.hpp:47
float y
The y-coordinate of the point.
Definition point.hpp:41
friend Point operator*(float scalar, const Point &point)
Scalar multiplication (scalar * Vec2).
Definition point.hpp:135
Point operator+(const Point &other) const
Adds two points.
Definition point.hpp:88
Point operator*(float scalar) const
Multiplies the point by a scalar.
Definition point.hpp:108
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:40
float v
The value at the point.
Definition point.hpp:42
bool operator!=(const Point &other) const
Inequality operator to check if two points are different.
Definition point.hpp:78
Point(float x, float y, float v=0.f)
Parameterized constructor initializing the point to given values.
Definition point.hpp:57
Definition algebra.hpp:28
bool is_point_within_bounding_box(Point p, Vec4< float > bbox)
Checks if a point is within a specified bounding box.
Definition points.cpp:175
float curvature(const Point &p1, const Point &p2, const Point &p3)
Calculates the curvature formed by three points in 2D space.
Definition points.cpp:83
Vec4< float > unit_square_bbox()
Constructs a 4D bounding box for a unit square.
Definition points.cpp:219
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:190
float distance(const Point &p1, const Point &p2)
Calculates the distance between two points.
Definition points.cpp:93
Vec4< float > intersect_bounding_boxes(const Vec4< float > &bbox1, const Vec4< float > &bbox2)
Determines the intersection of two bounding boxes.
Definition points.cpp:156
void sort_points(std::vector< Point > &points)
Sorts a vector of points in ascending order based on their coordinates.
Definition points.cpp:231
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:236
Point interp_decasteljau(const std::vector< Point > &points, float t)
Performs a De Casteljau algorithm-based interpolation for Bezier curves.
Definition points.cpp:145
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:185
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:100
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:131
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:116
Vec4 class for basic manipulation of 4D vectors.
Definition algebra.hpp:564