MeshKernel
Loading...
Searching...
No Matches
Point.hpp
1//---- GPL ---------------------------------------------------------------------
2//
3// Copyright (C) Stichting Deltares, 2011-2023.
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation version 3.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//
17// contact: delft3d.support@deltares.nl
18// Stichting Deltares
19// P.O. Box 177
20// 2600 MH Delft, The Netherlands
21//
22// All indications and logos of, and references to, "Delft3D" and "Deltares"
23// are registered trademarks of Stichting Deltares, and remain the property of
24// Stichting Deltares. All rights reserved.
25//
26//------------------------------------------------------------------------------
27
28#pragma once
29
30#include <cmath>
31
32#include "MeshKernel/Constants.hpp"
33#include "MeshKernel/Utilities/NumericFunctions.hpp"
34#include "MeshKernel/Vector.hpp"
35
36namespace meshkernel
37{
38
40 class Point
41 {
42 public:
43 double x;
44 double y;
45
48 : x(constants::missing::doubleValue),
49 y(constants::missing::doubleValue)
50 {
51 }
52
56 Point(double x, double y)
57 : x(x),
58 y(y)
59 {
60 }
61
65 void SetInvalid();
66
68 Point& operator+=(const Point& p);
69
71 Point& operator-=(const Point& p);
72
74 Point& operator+=(const Vector& vec);
75
77 Point& operator-=(const Vector& vec);
78
82 Point& operator/=(const Point& p);
83
85 Point& operator*=(const Point& p);
86
88 Point& operator+=(const double p);
89
91 Point& operator-=(const double p);
92
96 Point& operator/=(const double p);
97
99 Point& operator*=(const double p);
100
102 [[nodiscard]] Point operator*(int const& rhs) const { return Point(x * rhs, y * rhs); }
103
105 explicit operator Vector() const
106 {
107 return Vector(x, y);
108 }
109
111 void TransformSphericalToCartesian(double referenceLatitude)
112 {
113 static double constexpr m_trans_factor =
114 constants::conversion::degToRad *
115 constants::geometric::earth_radius;
116
117 x = x * m_trans_factor * std::cos(constants::conversion::degToRad * referenceLatitude);
118 y = y * m_trans_factor;
119 }
120
122 [[nodiscard]] bool IsValid(const double missingValue = constants::missing::doubleValue) const
123 {
124 bool isInvalid = (x == missingValue || y == missingValue);
125
126 return !isInvalid;
127 }
128 };
129
133 double lengthSquared(const Point& p1);
134
138 double dot(const Point& p1, const Point& p2);
139
143 double dot(const Point& p, const Vector& v);
144
148 double dot(const Vector& v, const Point& p);
149
153 double cross(const Point& p1, const Point& p2);
154
158 Point operator-(const Point& pnt);
159
163 Point operator+(const Point& p1, const Point& p2);
164
168 Point operator+(const Point& pnt, const Vector& vec);
169
173 Point operator+(const Point& p, const double x);
174
178 Point operator+(const double x, const Point& p);
179
183 Point operator-(const Point& p1, const Point& p2);
184
188 Point operator-(const Point& pnt, const Vector& vec);
189
193 Point operator-(const Point& p, const double x);
194
198 Point operator-(const double x, const Point& p);
199
204 Point operator*(const Point& p1, const Point& p2);
205
208 Point operator*(const double x, const Point& p);
209
212 Point operator*(const Point& p, const double x);
213
219 Point operator/(const Point& p1, const Point& p2);
220
224 Point operator/(const Point& p, const double x);
225
228 bool operator==(const Point& p1, const Point& p2);
229
232 bool operator!=(const Point& p1, const Point& p2);
233
239 bool IsEqual(const Point& p1, const Point& p2, const double epsilon);
240
244 Point PointAlongLine(const Point& startPoint, const Point& endPoint, const double lambda);
245
251 static Point Rotate(const Point& point, const double angle, const Point& reference)
252 {
253 const auto translatedPoint = point - reference;
254
255 const auto angleInRad = angle * constants::conversion::degToRad;
256 const auto cosineAngle = std::cos(angleInRad);
257 const auto sinAngle = std::sin(angleInRad);
258 Point result(translatedPoint.x * cosineAngle - translatedPoint.y * sinAngle,
259 translatedPoint.x * sinAngle + translatedPoint.y * cosineAngle);
260
261 return result + reference;
262 }
263
265 double GetDeltaXCartesian(const Point& p1, const Point& p2);
266
268 double GetDeltaYCartesian(const Point& p1, const Point& p2);
269
271 Vector GetDeltaCartesian(const Point& p1, const Point& p2);
272
273} // namespace meshkernel
274
276{
277 x = constants::missing::doubleValue;
278 y = constants::missing::doubleValue;
279}
280
282{
283 x += p.x;
284 y += p.y;
285 return *this;
286}
287
289{
290 x -= p.x;
291 y -= p.y;
292 return *this;
293}
294
296{
297 x += vec.x();
298 y += vec.y();
299 return *this;
300}
301
303{
304 x -= vec.x();
305 y -= vec.y();
306 return *this;
307}
308
310{
311 x /= p.x;
312 y /= p.y;
313 return *this;
314}
315
317{
318 x *= p.x;
319 y *= p.y;
320 return *this;
321}
322
324{
325 x += p;
326 y += p;
327 return *this;
328}
329
331{
332 x -= p;
333 y -= p;
334 return *this;
335}
336
338{
339 x /= p;
340 y /= p;
341 return *this;
342}
343
345{
346 x *= p;
347 y *= p;
348 return *this;
349}
350
351inline double meshkernel::lengthSquared(const Point& p)
352{
353 return p.x * p.x + p.y * p.y;
354}
355
356inline double meshkernel::dot(const Point& p1, const Point& p2)
357{
358 return p1.x * p2.x + p1.y * p2.y;
359}
360
361inline double meshkernel::dot(const Point& p, const Vector& v)
362{
363 return p.x * v.x() + p.y * v.y();
364}
365
366inline double meshkernel::dot(const Vector& v, const Point& p)
367{
368 return dot(p, v);
369}
370
371inline double meshkernel::cross(const Point& p1, const Point& p2)
372{
373 return p1.x * p2.y - p1.y * p2.x;
374}
375
377{
378 return Point(-pnt.x, -pnt.y);
379}
380
382{
383 return Point(p1.x + p2.x, p1.y + p2.y);
384}
385
387{
388 return Point(pnt.x + vec.x(), pnt.y + vec.y());
389}
390
391inline meshkernel::Point meshkernel::operator+(const Point& p, const double x)
392{
393 return Point(p.x + x, p.y + x);
394}
395
396inline meshkernel::Point meshkernel::operator+(const double x, const Point& p)
397{
398 return Point(p.x + x, p.y + x);
399}
400
402{
403 return Point(p1.x - p2.x, p1.y - p2.y);
404}
405
407{
408 return Point(pnt.x - vec.x(), pnt.y - vec.y());
409}
410
411inline meshkernel::Point meshkernel::operator-(const Point& p, const double x)
412{
413 return Point(p.x - x, p.y - x);
414}
415
416inline meshkernel::Point meshkernel::operator-(const double x, const Point& p)
417{
418 return Point(x - p.x, x - p.y);
419}
420
422{
423 return Point(p1.x * p2.x, p1.y * p2.y);
424}
425
426inline meshkernel::Point meshkernel::operator*(const double x, const Point& p)
427{
428 return Point(x * p.x, x * p.y);
429}
430
431inline meshkernel::Point meshkernel::operator*(const Point& p, const double x)
432{
433 return Point(x * p.x, x * p.y);
434}
435
437{
438 return Point(p1.x / p2.x, p1.y / p2.y);
439}
440
441inline meshkernel::Point meshkernel::operator/(const Point& p, const double x)
442{
443 return Point(p.x / x, p.y / x);
444}
445
446inline bool meshkernel::operator==(const Point& p1, const Point& p2)
447{
448 return IsEqual(p1.x, p2.x) && IsEqual(p1.y, p2.y);
449}
450
451inline bool meshkernel::operator!=(const Point& p1, const Point& p2)
452{
453 return !(p1 == p2);
454}
455
456inline bool meshkernel::IsEqual(const Point& p1, const Point& p2, const double epsilon)
457{
458 return IsEqual(p1.x, p2.x, epsilon) && IsEqual(p1.y, p2.y, epsilon);
459}
460
461inline double meshkernel::GetDeltaXCartesian(const Point& p1, const Point& p2)
462{
463 return p2.x - p1.x;
464}
465
466inline double meshkernel::GetDeltaYCartesian(const Point& p1, const Point& p2)
467{
468 return p2.y - p1.y;
469}
470
472{
473 return Vector(GetDeltaXCartesian(p1, p2), GetDeltaYCartesian(p1, p2));
474}
475
476inline meshkernel::Point meshkernel::PointAlongLine(const Point& startPoint, const Point& endPoint, const double lambda)
477{
478 return (1.0 - lambda) * startPoint + lambda * endPoint;
479}
A struct describing a point in a two-dimensional space.
Definition Point.hpp:41
Point()
Constructor initializing with missing values.
Definition Point.hpp:47
void SetInvalid()
Set the point to be invalid.
Definition Point.hpp:275
Point & operator+=(const Point &p)
Inplace add point to point.
Definition Point.hpp:281
Point & operator*=(const Point &p)
Inplace multiply point by point.
Definition Point.hpp:316
bool IsValid(const double missingValue=constants::missing::doubleValue) const
Determines if one of the point coordinates equals to missingValue.
Definition Point.hpp:122
Point & operator/=(const Point &p)
Inplace divide point by point.
Definition Point.hpp:309
Point & operator-=(const Point &p)
Inplace subtract point from point.
Definition Point.hpp:288
void TransformSphericalToCartesian(double referenceLatitude)
Transforms spherical coordinates to cartesian.
Definition Point.hpp:111
Point operator*(int const &rhs) const
Overloads multiplication with a integer.
Definition Point.hpp:102
double x
X-coordinate.
Definition Point.hpp:43
double y
Y-coordinate.
Definition Point.hpp:44
Point(double x, double y)
Constructor initializing with given coordinates.
Definition Point.hpp:56
A class defining a vector.
Definition Vector.hpp:39
double y() const
Gets the y coordinate of the vector.
Definition Vector.hpp:66
double x() const
Gets the x coordinate of the vector.
Definition Vector.hpp:52
Contains the logic of the C++ static library.
Definition AveragingInterpolation.hpp:37
bool operator!=(const Point &p1, const Point &p2)
Test points for equality using a default tolerance.
Definition Point.hpp:451
double cross(const Point &p1, const Point &p2)
Compute the cross product of two points (as vectors).
Definition Point.hpp:371
bool operator==(const Point &p1, const Point &p2)
Test points for equality using a default tolerance.
Definition Point.hpp:446
bool IsEqual(const Point &p1, const Point &p2, const double epsilon)
Test points for equality upto a tolerance.
Definition Point.hpp:456
double GetDeltaXCartesian(const Point &p1, const Point &p2)
Get the delta-x in Cartesian coordinate system.
Definition Point.hpp:461
double dot(const Point &p1, const Point &p2)
Compute the dot product of two points.
Definition Point.hpp:356
Cartesian3DPoint operator+(const Cartesian3DPoint &p1, const Cartesian3DPoint &p2)
Add Cartesian point p2 to p1.
Definition Cartesian3DPoint.hpp:93
Point PointAlongLine(const Point &startPoint, const Point &endPoint, const double lambda)
Compute the point at some position along the line connecting start- and end-point.
Definition Point.hpp:476
double lengthSquared(const Point &p1)
Compute the dot product of a point with itself.
Definition Point.hpp:351
Vector GetDeltaCartesian(const Point &p1, const Point &p2)
Get the delta-x and -y in Cartesian coordinate system.
Definition Point.hpp:471
Cartesian3DPoint operator-(const Cartesian3DPoint &p1, const Cartesian3DPoint &p2)
Subtract Cartesian point p2 from p1.
Definition Cartesian3DPoint.hpp:98
Cartesian3DPoint operator/(const Cartesian3DPoint &p, const double value)
Divide Cartesian point p by a scalar value.
Definition Cartesian3DPoint.hpp:103
double GetDeltaYCartesian(const Point &p1, const Point &p2)
Get the delta-y in Cartesian coordinate system.
Definition Point.hpp:466
Cartesian3DPoint operator*(const Cartesian3DPoint &p, const double value)
Multiply Cartesian point p by a scalar value.
Definition Cartesian3DPoint.hpp:113