Loading [MathJax]/extensions/tex2jax.js
MeshKernel
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages Concepts
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 void TransformSphericalToCartesian(double referenceLatitude)
106 {
107 static double constexpr m_trans_factor =
108 constants::conversion::degToRad *
109 constants::geometric::earth_radius;
110
111 x = x * m_trans_factor * std::cos(constants::conversion::degToRad * referenceLatitude);
112 y = y * m_trans_factor;
113 }
114
116 [[nodiscard]] bool IsValid(const double missingValue = constants::missing::doubleValue) const
117 {
118 bool isInvalid = (x == missingValue || y == missingValue);
119
120 return !isInvalid;
121 }
122 };
123
127 double lengthSquared(const Point& p1);
128
132 double dot(const Point& p1, const Point& p2);
133
137 double dot(const Point& p, const Vector& v);
138
142 double dot(const Vector& v, const Point& p);
143
147 double cross(const Point& p1, const Point& p2);
148
152 Point operator-(const Point& pnt);
153
157 Point operator+(const Point& p1, const Point& p2);
158
162 Point operator+(const Point& pnt, const Vector& vec);
163
167 Point operator+(const Point& p, const double x);
168
172 Point operator+(const double x, const Point& p);
173
177 Point operator-(const Point& p1, const Point& p2);
178
182 Point operator-(const Point& pnt, const Vector& vec);
183
187 Point operator-(const Point& p, const double x);
188
192 Point operator-(const double x, const Point& p);
193
198 Point operator*(const Point& p1, const Point& p2);
199
202 Point operator*(const double x, const Point& p);
203
206 Point operator*(const Point& p, const double x);
207
213 Point operator/(const Point& p1, const Point& p2);
214
218 Point operator/(const Point& p, const double x);
219
222 bool operator==(const Point& p1, const Point& p2);
223
226 bool operator!=(const Point& p1, const Point& p2);
227
233 bool IsEqual(const Point& p1, const Point& p2, const double epsilon);
234
238 Point PointAlongLine(const Point& startPoint, const Point& endPoint, const double lambda);
239
245 static Point Rotate(const Point& point, const double angle, const Point& reference)
246 {
247 const auto translatedPoint = point - reference;
248
249 const auto angleInRad = angle * constants::conversion::degToRad;
250 const auto cosineAngle = std::cos(angleInRad);
251 const auto sinAngle = std::sin(angleInRad);
252 Point result(translatedPoint.x * cosineAngle - translatedPoint.y * sinAngle,
253 translatedPoint.x * sinAngle + translatedPoint.y * cosineAngle);
254
255 return result + reference;
256 }
257
259 double GetDeltaXCartesian(const Point& p1, const Point& p2);
260
262 double GetDeltaYCartesian(const Point& p1, const Point& p2);
263
265 Vector GetDeltaCartesian(const Point& p1, const Point& p2);
266
267} // namespace meshkernel
268
270{
271 x = constants::missing::doubleValue;
272 y = constants::missing::doubleValue;
273}
274
276{
277 x += p.x;
278 y += p.y;
279 return *this;
280}
281
283{
284 x -= p.x;
285 y -= p.y;
286 return *this;
287}
288
290{
291 x += vec.x();
292 y += vec.y();
293 return *this;
294}
295
297{
298 x -= vec.x();
299 y -= vec.y();
300 return *this;
301}
302
304{
305 x /= p.x;
306 y /= p.y;
307 return *this;
308}
309
311{
312 x *= p.x;
313 y *= p.y;
314 return *this;
315}
316
318{
319 x += p;
320 y += p;
321 return *this;
322}
323
325{
326 x -= p;
327 y -= p;
328 return *this;
329}
330
332{
333 x /= p;
334 y /= p;
335 return *this;
336}
337
339{
340 x *= p;
341 y *= p;
342 return *this;
343}
344
345inline double meshkernel::lengthSquared(const Point& p)
346{
347 return p.x * p.x + p.y * p.y;
348}
349
350inline double meshkernel::dot(const Point& p1, const Point& p2)
351{
352 return p1.x * p2.x + p1.y * p2.y;
353}
354
355inline double meshkernel::dot(const Point& p, const Vector& v)
356{
357 return p.x * v.x() + p.y * v.y();
358}
359
360inline double meshkernel::dot(const Vector& v, const Point& p)
361{
362 return dot(p, v);
363}
364
365inline double meshkernel::cross(const Point& p1, const Point& p2)
366{
367 return p1.x * p2.y - p1.y * p2.x;
368}
369
371{
372 return Point(-pnt.x, -pnt.y);
373}
374
376{
377 return Point(p1.x + p2.x, p1.y + p2.y);
378}
379
381{
382 return Point(pnt.x + vec.x(), pnt.y + vec.y());
383}
384
385inline meshkernel::Point meshkernel::operator+(const Point& p, const double x)
386{
387 return Point(p.x + x, p.y + x);
388}
389
390inline meshkernel::Point meshkernel::operator+(const double x, const Point& p)
391{
392 return Point(p.x + x, p.y + x);
393}
394
396{
397 return Point(p1.x - p2.x, p1.y - p2.y);
398}
399
401{
402 return Point(pnt.x - vec.x(), pnt.y - vec.y());
403}
404
405inline meshkernel::Point meshkernel::operator-(const Point& p, const double x)
406{
407 return Point(p.x - x, p.y - x);
408}
409
410inline meshkernel::Point meshkernel::operator-(const double x, const Point& p)
411{
412 return Point(x - p.x, x - p.y);
413}
414
416{
417 return Point(p1.x * p2.x, p1.y * p2.y);
418}
419
420inline meshkernel::Point meshkernel::operator*(const double x, const Point& p)
421{
422 return Point(x * p.x, x * p.y);
423}
424
425inline meshkernel::Point meshkernel::operator*(const Point& p, const double x)
426{
427 return Point(x * p.x, x * p.y);
428}
429
431{
432 return Point(p1.x / p2.x, p1.y / p2.y);
433}
434
435inline meshkernel::Point meshkernel::operator/(const Point& p, const double x)
436{
437 return Point(p.x / x, p.y / x);
438}
439
440inline bool meshkernel::operator==(const Point& p1, const Point& p2)
441{
442 return IsEqual(p1.x, p2.x) && IsEqual(p1.y, p2.y);
443}
444
445inline bool meshkernel::operator!=(const Point& p1, const Point& p2)
446{
447 return !(p1 == p2);
448}
449
450inline bool meshkernel::IsEqual(const Point& p1, const Point& p2, const double epsilon)
451{
452 return IsEqual(p1.x, p2.x, epsilon) && IsEqual(p1.y, p2.y, epsilon);
453}
454
455inline double meshkernel::GetDeltaXCartesian(const Point& p1, const Point& p2)
456{
457 return p2.x - p1.x;
458}
459
460inline double meshkernel::GetDeltaYCartesian(const Point& p1, const Point& p2)
461{
462 return p2.y - p1.y;
463}
464
466{
467 return Vector(GetDeltaXCartesian(p1, p2), GetDeltaYCartesian(p1, p2));
468}
469
470inline meshkernel::Point meshkernel::PointAlongLine(const Point& startPoint, const Point& endPoint, const double lambda)
471{
472 return (1.0 - lambda) * startPoint + lambda * endPoint;
473}
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:269
Point & operator+=(const Point &p)
Inplace add point to point.
Definition Point.hpp:275
Point & operator*=(const Point &p)
Inplace multiply point by point.
Definition Point.hpp:310
bool IsValid(const double missingValue=constants::missing::doubleValue) const
Determines if one of the point coordinates equals to missingValue.
Definition Point.hpp:116
Point & operator/=(const Point &p)
Inplace divide point by point.
Definition Point.hpp:303
Point & operator-=(const Point &p)
Inplace subtract point from point.
Definition Point.hpp:282
void TransformSphericalToCartesian(double referenceLatitude)
Transforms spherical coordinates to cartesian.
Definition Point.hpp:105
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:445
double cross(const Point &p1, const Point &p2)
Compute the cross product of two points (as vectors).
Definition Point.hpp:365
bool operator==(const Point &p1, const Point &p2)
Test points for equality using a default tolerance.
Definition Point.hpp:440
bool IsEqual(const Point &p1, const Point &p2, const double epsilon)
Test points for equality upto a tolerance.
Definition Point.hpp:450
double GetDeltaXCartesian(const Point &p1, const Point &p2)
Get the delta-x in Cartesian coordinate system.
Definition Point.hpp:455
double dot(const Point &p1, const Point &p2)
Compute the dot product of two points.
Definition Point.hpp:350
Cartesian3DPoint operator+(const Cartesian3DPoint &p1, const Cartesian3DPoint &p2)
Add Cartesian point p2 to p1.
Definition Cartesian3DPoint.hpp:83
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:470
double lengthSquared(const Point &p1)
Compute the dot product of a point with itself.
Definition Point.hpp:345
Vector GetDeltaCartesian(const Point &p1, const Point &p2)
Get the delta-x and -y in Cartesian coordinate system.
Definition Point.hpp:465
Cartesian3DPoint operator-(const Cartesian3DPoint &p1, const Cartesian3DPoint &p2)
Subtract Cartesian point p2 from p1.
Definition Cartesian3DPoint.hpp:88
Cartesian3DPoint operator/(const Cartesian3DPoint &p, const double value)
Divide Cartesian point p by a scalar value.
Definition Cartesian3DPoint.hpp:93
double GetDeltaYCartesian(const Point &p1, const Point &p2)
Get the delta-y in Cartesian coordinate system.
Definition Point.hpp:460
Cartesian3DPoint operator*(const Cartesian3DPoint &p, const double value)
Multiply Cartesian point p by a scalar value.
Definition Cartesian3DPoint.hpp:103