MeshKernel
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 
36 namespace 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 = IsEqual(x, missingValue) ||
119  IsEqual(y, missingValue);
120 
121  return !isInvalid;
122  }
123  };
124 
128  double lengthSquared(const Point& p1);
129 
133  double dot(const Point& p1, const Point& p2);
134 
138  double dot(const Point& p, const Vector& v);
139 
143  double dot(const Vector& v, const Point& p);
144 
148  double cross(const Point& p1, const Point& p2);
149 
153  Point operator-(const Point& pnt);
154 
158  Point operator+(const Point& p1, const Point& p2);
159 
163  Point operator+(const Point& pnt, const Vector& vec);
164 
168  Point operator+(const Point& p, const double x);
169 
173  Point operator+(const double x, const Point& p);
174 
178  Point operator-(const Point& p1, const Point& p2);
179 
183  Point operator-(const Point& pnt, const Vector& vec);
184 
188  Point operator-(const Point& p, const double x);
189 
193  Point operator-(const double x, const Point& p);
194 
199  Point operator*(const Point& p1, const Point& p2);
200 
203  Point operator*(const double x, const Point& p);
204 
207  Point operator*(const Point& p, const double x);
208 
214  Point operator/(const Point& p1, const Point& p2);
215 
219  Point operator/(const Point& p, const double x);
220 
223  bool operator==(const Point& p1, const Point& p2);
224 
227  bool operator!=(const Point& p1, const Point& p2);
228 
234  bool IsEqual(const Point& p1, const Point& p2, const double epsilon);
235 
239  Point PointAlongLine(const Point& startPoint, const Point& endPoint, const double lambda);
240 
246  static Point Rotate(const Point& point, const double angle, const Point& reference)
247  {
248  const auto translatedPoint = point - reference;
249 
250  const auto angleInRad = angle * constants::conversion::degToRad;
251  const auto cosineAngle = std::cos(angleInRad);
252  const auto sinAngle = std::sin(angleInRad);
253  Point result(translatedPoint.x * cosineAngle - translatedPoint.y * sinAngle,
254  translatedPoint.x * sinAngle + translatedPoint.y * cosineAngle);
255 
256  return result + reference;
257  }
258 
260  double GetDeltaXCartesian(const Point& p1, const Point& p2);
261 
263  double GetDeltaYCartesian(const Point& p1, const Point& p2);
264 
266  Vector GetDeltaCartesian(const Point& p1, const Point& p2);
267 
268 } // namespace meshkernel
269 
271 {
272  x = constants::missing::doubleValue;
273  y = constants::missing::doubleValue;
274 }
275 
277 {
278  x += p.x;
279  y += p.y;
280  return *this;
281 }
282 
284 {
285  x -= p.x;
286  y -= p.y;
287  return *this;
288 }
289 
291 {
292  x += vec.x();
293  y += vec.y();
294  return *this;
295 }
296 
298 {
299  x -= vec.x();
300  y -= vec.y();
301  return *this;
302 }
303 
305 {
306  x /= p.x;
307  y /= p.y;
308  return *this;
309 }
310 
312 {
313  x *= p.x;
314  y *= p.y;
315  return *this;
316 }
317 
319 {
320  x += p;
321  y += p;
322  return *this;
323 }
324 
326 {
327  x -= p;
328  y -= p;
329  return *this;
330 }
331 
333 {
334  x /= p;
335  y /= p;
336  return *this;
337 }
338 
340 {
341  x *= p;
342  y *= p;
343  return *this;
344 }
345 
346 inline double meshkernel::lengthSquared(const Point& p)
347 {
348  return p.x * p.x + p.y * p.y;
349 }
350 
351 inline double meshkernel::dot(const Point& p1, const Point& p2)
352 {
353  return p1.x * p2.x + p1.y * p2.y;
354 }
355 
356 inline double meshkernel::dot(const Point& p, const Vector& v)
357 {
358  return p.x * v.x() + p.y * v.y();
359 }
360 
361 inline double meshkernel::dot(const Vector& v, const Point& p)
362 {
363  return dot(p, v);
364 }
365 
366 inline double meshkernel::cross(const Point& p1, const Point& p2)
367 {
368  return p1.x * p2.y - p1.y * p2.x;
369 }
370 
372 {
373  return Point(-pnt.x, -pnt.y);
374 }
375 
377 {
378  return Point(p1.x + p2.x, p1.y + p2.y);
379 }
380 
381 inline meshkernel::Point meshkernel::operator+(const Point& pnt, const Vector& vec)
382 {
383  return Point(pnt.x + vec.x(), pnt.y + vec.y());
384 }
385 
386 inline meshkernel::Point meshkernel::operator+(const Point& p, const double x)
387 {
388  return Point(p.x + x, p.y + x);
389 }
390 
391 inline meshkernel::Point meshkernel::operator+(const double x, const Point& p)
392 {
393  return Point(p.x + x, p.y + x);
394 }
395 
397 {
398  return Point(p1.x - p2.x, p1.y - p2.y);
399 }
400 
401 inline meshkernel::Point meshkernel::operator-(const Point& pnt, const Vector& vec)
402 {
403  return Point(pnt.x - vec.x(), pnt.y - vec.y());
404 }
405 
406 inline meshkernel::Point meshkernel::operator-(const Point& p, const double x)
407 {
408  return Point(p.x - x, p.y - x);
409 }
410 
411 inline meshkernel::Point meshkernel::operator-(const double x, const Point& p)
412 {
413  return Point(x - p.x, x - p.y);
414 }
415 
417 {
418  return Point(p1.x * p2.x, p1.y * p2.y);
419 }
420 
421 inline meshkernel::Point meshkernel::operator*(const double x, const Point& p)
422 {
423  return Point(x * p.x, x * p.y);
424 }
425 
426 inline meshkernel::Point meshkernel::operator*(const Point& p, const double x)
427 {
428  return Point(x * p.x, x * p.y);
429 }
430 
432 {
433  return Point(p1.x / p2.x, p1.y / p2.y);
434 }
435 
436 inline meshkernel::Point meshkernel::operator/(const Point& p, const double x)
437 {
438  return Point(p.x / x, p.y / x);
439 }
440 
441 inline bool meshkernel::operator==(const Point& p1, const Point& p2)
442 {
443  return IsEqual(p1.x, p2.x) && IsEqual(p1.y, p2.y);
444 }
445 
446 inline bool meshkernel::operator!=(const Point& p1, const Point& p2)
447 {
448  return !(p1 == p2);
449 }
450 
451 inline bool meshkernel::IsEqual(const Point& p1, const Point& p2, const double epsilon)
452 {
453  return IsEqual(p1.x, p2.x, epsilon) && IsEqual(p1.y, p2.y, epsilon);
454 }
455 
456 inline double meshkernel::GetDeltaXCartesian(const Point& p1, const Point& p2)
457 {
458  return p2.x - p1.x;
459 }
460 
461 inline double meshkernel::GetDeltaYCartesian(const Point& p1, const Point& p2)
462 {
463  return p2.y - p1.y;
464 }
465 
467 {
468  return Vector(GetDeltaXCartesian(p1, p2), GetDeltaYCartesian(p1, p2));
469 }
470 
471 inline meshkernel::Point meshkernel::PointAlongLine(const Point& startPoint, const Point& endPoint, const double lambda)
472 {
473  return (1.0 - lambda) * startPoint + lambda * endPoint;
474 }
meshkernel::Point::operator+=
Point & operator+=(const Point &p)
Inplace add point to point.
Definition: Point.hpp:276
meshkernel::operator!=
bool operator!=(const Point &p1, const Point &p2)
Test points for equality using a default tolerance.
Definition: Point.hpp:446
meshkernel::Point::operator/=
Point & operator/=(const Point &p)
Inplace divide point by point.
Definition: Point.hpp:304
meshkernel::IsEqual
bool IsEqual(const Point &p1, const Point &p2, const double epsilon)
Test points for equality upto a tolerance.
Definition: Point.hpp:451
meshkernel::operator/
Cartesian3DPoint operator/(const Cartesian3DPoint &p, const double value)
Divide Cartesian point p by a scalar value.
Definition: Operations.hpp:634
meshkernel::Point::Point
Point()
Constructor initializing with missing values.
Definition: Point.hpp:47
meshkernel::Point::operator*
Point operator*(int const &rhs) const
Overloads multiplication with a integer.
Definition: Point.hpp:102
meshkernel::Point
A struct describing a point in a two-dimensional space.
Definition: Point.hpp:40
meshkernel::operator==
bool operator==(const Point &p1, const Point &p2)
Test points for equality using a default tolerance.
Definition: Point.hpp:441
meshkernel::Point::x
double x
X-coordinate.
Definition: Point.hpp:43
meshkernel::Vector
A class defining a vector.
Definition: Vector.hpp:38
meshkernel::operator-
Cartesian3DPoint operator-(const Cartesian3DPoint &p1, const Cartesian3DPoint &p2)
Subtract Cartesian point p2 from p1.
Definition: Operations.hpp:629
meshkernel::operator*
Cartesian3DPoint operator*(const Cartesian3DPoint &p, const double value)
Multiply Cartesian point p by a scalar value.
Definition: Operations.hpp:644
meshkernel::Point::y
double y
Y-coordinate.
Definition: Point.hpp:44
meshkernel::GetDeltaXCartesian
double GetDeltaXCartesian(const Point &p1, const Point &p2)
Get the delta-x in Cartesian coordinate system.
Definition: Point.hpp:456
meshkernel::GetDeltaYCartesian
double GetDeltaYCartesian(const Point &p1, const Point &p2)
Get the delta-y in Cartesian coordinate system.
Definition: Point.hpp:461
meshkernel::cross
double cross(const Point &p1, const Point &p2)
Compute the cross product of two points (as vectors).
Definition: Point.hpp:366
meshkernel
Contains the logic of the C++ static library.
Definition: AveragingInterpolation.hpp:36
meshkernel::Point::TransformSphericalToCartesian
void TransformSphericalToCartesian(double referenceLatitude)
Transforms spherical coordinates to cartesian.
Definition: Point.hpp:105
meshkernel::Point::IsValid
bool IsValid(const double missingValue=constants::missing::doubleValue) const
Determines if one of the point coordinates equals to missingValue.
Definition: Point.hpp:116
meshkernel::Vector::y
double y() const
Gets the y coordinate of the vector.
Definition: Vector.hpp:66
meshkernel::operator+
Cartesian3DPoint operator+(const Cartesian3DPoint &p1, const Cartesian3DPoint &p2)
Add Cartesian point p2 to p1.
Definition: Operations.hpp:624
meshkernel::lengthSquared
double lengthSquared(const Point &p1)
Compute the dot product of a point with itself.
Definition: Point.hpp:346
meshkernel::PointAlongLine
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:471
meshkernel::Point::operator*=
Point & operator*=(const Point &p)
Inplace multiply point by point.
Definition: Point.hpp:311
meshkernel::Point::Point
Point(double x, double y)
Constructor initializing with given coordinates.
Definition: Point.hpp:56
meshkernel::Vector::x
double x() const
Gets the x coordinate of the vector.
Definition: Vector.hpp:52
meshkernel::dot
double dot(const Point &p1, const Point &p2)
Compute the dot product of two points.
Definition: Point.hpp:351
meshkernel::GetDeltaCartesian
Vector GetDeltaCartesian(const Point &p1, const Point &p2)
Get the delta-x and -y in Cartesian coordinate system.
Definition: Point.hpp:466
meshkernel::Point::operator-=
Point & operator-=(const Point &p)
Inplace subtract point from point.
Definition: Point.hpp:283
meshkernel::Point::SetInvalid
void SetInvalid()
Set the point to be invalid.
Definition: Point.hpp:270