QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgspointxy.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspoint.h - description
3  -------------------
4  begin : Sat Jun 22 2002
5  copyright : (C) 2002 by Gary E.Sherman
6  email : sherman at mrcc.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSPOINTXY_H
19 #define QGSPOINTXY_H
20 
21 #include "qgis_core.h"
22 #include "qgsvector.h"
23 
24 #include "qgis.h"
25 
26 #include <iostream>
27 #include <QString>
28 #include <QPoint>
29 #include <QObject>
30 
31 class QgsPoint;
32 
43 class CORE_EXPORT QgsPointXY
44 {
45  Q_GADGET
46 
47  Q_PROPERTY( double x READ x WRITE setX )
48  Q_PROPERTY( double y READ y WRITE setY )
49 
50  public:
52  QgsPointXY() = default;
53 
55  QgsPointXY( const QgsPointXY &p ) SIP_HOLDGIL;
56 
62  QgsPointXY( double x, double y ) SIP_HOLDGIL
63  : mX( x )
64  , mY( y )
65  , mIsEmpty( false )
66  {}
67 
73  QgsPointXY( QPointF point ) SIP_HOLDGIL
74  : mX( point.x() )
75  , mY( point.y() )
76  , mIsEmpty( false )
77  {}
78 
84  QgsPointXY( QPoint point ) SIP_HOLDGIL
85  : mX( point.x() )
86  , mY( point.y() )
87  , mIsEmpty( false )
88  {}
89 
96  QgsPointXY( const QgsPoint &point ) SIP_HOLDGIL;
97 
98  // IMPORTANT - while QgsPointXY is inherited by QgsReferencedPointXY, we do NOT want a virtual destructor here
99  // because this class MUST be lightweight and we don't want the cost of the vtable here.
100  // see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
101  ~QgsPointXY() = default;
102 
107  void setX( double x ) SIP_HOLDGIL
108  {
109  mX = x;
110  mIsEmpty = false;
111  }
112 
117  void setY( double y ) SIP_HOLDGIL
118  {
119  mY = y;
120  mIsEmpty = false;
121  }
122 
124  void set( double x, double y ) SIP_HOLDGIL
125  {
126  mX = x;
127  mY = y;
128  mIsEmpty = false;
129  }
130 
135  double x() const SIP_HOLDGIL
136  {
137  return mX;
138  }
139 
144  double y() const SIP_HOLDGIL
145  {
146  return mY;
147  }
148 
154  QPointF toQPointF() const
155  {
156  return QPointF( mX, mY );
157  }
158 
163  QString toString( int precision = -1 ) const;
164 
169  QString asWkt() const;
170 
175  double sqrDist( double x, double y ) const SIP_HOLDGIL
176  {
177  return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y );
178  }
179 
184  double sqrDist( const QgsPointXY &other ) const SIP_HOLDGIL
185  {
186  return sqrDist( other.x(), other.y() );
187  }
188 
196  double distance( double x, double y ) const SIP_HOLDGIL
197  {
198  return std::sqrt( sqrDist( x, y ) );
199  }
200 
207  double distance( const QgsPointXY &other ) const SIP_HOLDGIL
208  {
209  return std::sqrt( sqrDist( other ) );
210  }
211 
213  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint SIP_OUT, double epsilon = DEFAULT_SEGMENT_EPSILON ) const SIP_HOLDGIL;
214 
216  double azimuth( const QgsPointXY &other ) const SIP_HOLDGIL;
217 
225  QgsPointXY project( double distance, double bearing ) const SIP_HOLDGIL;
226 
234  bool isEmpty() const SIP_HOLDGIL { return mIsEmpty; }
235 
243  bool compare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
244  {
245  return ( qgsDoubleNear( mX, other.x(), epsilon ) && qgsDoubleNear( mY, other.y(), epsilon ) );
246  }
247 
249  bool operator==( const QgsPointXY &other ) SIP_HOLDGIL
250  {
251  if ( isEmpty() && other.isEmpty() )
252  return true;
253  if ( isEmpty() && !other.isEmpty() )
254  return false;
255  if ( ! isEmpty() && other.isEmpty() )
256  return false;
257 
258  bool equal = true;
259  equal &= qgsDoubleNear( other.x(), mX, 1E-8 );
260  equal &= qgsDoubleNear( other.y(), mY, 1E-8 );
261 
262  return equal;
263  }
264 
266  bool operator!=( const QgsPointXY &other ) const SIP_HOLDGIL
267  {
268  if ( isEmpty() && other.isEmpty() )
269  return false;
270  if ( isEmpty() && !other.isEmpty() )
271  return true;
272  if ( ! isEmpty() && other.isEmpty() )
273  return true;
274 
275  bool equal = true;
276  equal &= qgsDoubleNear( other.x(), mX, 1E-8 );
277  equal &= qgsDoubleNear( other.y(), mY, 1E-8 );
278 
279  return !equal;
280  }
281 
283  void multiply( double scalar ) SIP_HOLDGIL
284  {
285  mX *= scalar;
286  mY *= scalar;
287  }
288 
291  {
292  if ( &other != this )
293  {
294  mX = other.x();
295  mY = other.y();
296  mIsEmpty = other.isEmpty();
297  }
298 
299  return *this;
300  }
301 
303  QgsVector operator-( const QgsPointXY &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
304 
306  QgsPointXY &operator+=( QgsVector v ) { *this = *this + v; return *this; }
307 
309  QgsPointXY &operator-=( QgsVector v ) { *this = *this - v; return *this; }
310 
312  QgsPointXY operator+( QgsVector v ) const { return QgsPointXY( mX + v.x(), mY + v.y() ); }
313 
315  QgsPointXY operator-( QgsVector v ) const { return QgsPointXY( mX - v.x(), mY - v.y() ); }
316 
318  QgsPointXY operator*( double scalar ) const { return QgsPointXY( mX * scalar, mY * scalar ); }
319 
321  QgsPointXY operator/( double scalar ) const { return QgsPointXY( mX / scalar, mY / scalar ); }
322 
324  QgsPointXY &operator*=( double scalar ) { mX *= scalar; mY *= scalar; return *this; }
325 
327  QgsPointXY &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }
328 
330  operator QVariant() const
331  {
332  return QVariant::fromValue( *this );
333  }
334 
335 #ifdef SIP_RUN
336  SIP_PYOBJECT __repr__();
337  % MethodCode
338  QString str = QStringLiteral( "<QgsPointXY: %1>" ).arg( sipCpp->asWkt() );
339  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
340  % End
341 
342  int __len__();
343  % MethodCode
344  sipRes = 2;
345  % End
346 
347 
348  SIP_PYOBJECT __getitem__( int );
349  % MethodCode
350  if ( a0 == 0 )
351  {
352  sipRes = Py_BuildValue( "d", sipCpp->x() );
353  }
354  else if ( a0 == 1 )
355  {
356  sipRes = Py_BuildValue( "d", sipCpp->y() );
357  }
358  else
359  {
360  QString msg = QString( "Bad index: %1" ).arg( a0 );
361  PyErr_SetString( PyExc_IndexError, msg.toLatin1().constData() );
362  }
363  % End
364 
365  long __hash__() const;
366  % MethodCode
367  sipRes = qHash( *sipCpp );
368  % End
369 #endif
370 
371  private:
372 
374  double mX = 0; //std::numeric_limits<double>::quiet_NaN();
375 
377  double mY = 0; //std::numeric_limits<double>::quiet_NaN();
378 
380  bool mIsEmpty = true;
381 
382  friend uint qHash( const QgsPointXY &pnt );
383 
384 }; // class QgsPointXY
385 
387 
388 inline bool operator==( const QgsPointXY &p1, const QgsPointXY &p2 ) SIP_SKIP
389 {
390  const bool nan1X = std::isnan( p1.x() );
391  const bool nan2X = std::isnan( p2.x() );
392  if ( nan1X != nan2X )
393  return false;
394  if ( !nan1X && !qgsDoubleNear( p1.x(), p2.x(), 1E-8 ) )
395  return false;
396 
397  const bool nan1Y = std::isnan( p1.y() );
398  const bool nan2Y = std::isnan( p2.y() );
399  if ( nan1Y != nan2Y )
400  return false;
401 
402  if ( !nan1Y && !qgsDoubleNear( p1.y(), p2.y(), 1E-8 ) )
403  return false;
404 
405  return true;
406 }
407 
408 inline std::ostream &operator << ( std::ostream &os, const QgsPointXY &p ) SIP_SKIP
409 {
410  // Use Local8Bit for printouts
411  os << p.toString().toLocal8Bit().data();
412  return os;
413 }
414 
415 inline uint qHash( const QgsPointXY &p ) SIP_SKIP
416 {
417  uint hash;
418  uint h1 = qHash( static_cast< quint64 >( p.mX ) );
419  uint h2 = qHash( static_cast< quint64 >( p.mY ) );
420  hash = h1 ^ ( h2 << 1 );
421  return hash;
422 }
423 
424 
425 #endif //QGSPOINTXY_H
A class to represent a 2D point.
Definition: qgspointxy.h:44
double distance(const QgsPointXY &other) const SIP_HOLDGIL
Returns the distance between this point and another point.
Definition: qgspointxy.h:207
bool isEmpty() const SIP_HOLDGIL
Returns true if the geometry is empty.
Definition: qgspointxy.h:234
QgsPointXY operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition: qgspointxy.h:318
bool operator==(const QgsPointXY &other) SIP_HOLDGIL
equality operator
Definition: qgspointxy.h:249
void set(double x, double y) SIP_HOLDGIL
Sets the x and y value of the point.
Definition: qgspointxy.h:124
double sqrDist(const QgsPointXY &other) const SIP_HOLDGIL
Returns the squared distance between this point another point.
Definition: qgspointxy.h:184
QgsPointXY & operator=(const QgsPointXY &other) SIP_HOLDGIL
Assignment.
Definition: qgspointxy.h:290
QgsPointXY(QPointF point) SIP_HOLDGIL
Create a point from a QPointF.
Definition: qgspointxy.h:73
QgsPointXY operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspointxy.h:315
double sqrDist(double x, double y) const SIP_HOLDGIL
Returns the squared distance between this point a specified x, y coordinate.
Definition: qgspointxy.h:175
void setX(double x) SIP_HOLDGIL
Sets the x value of the point.
Definition: qgspointxy.h:107
QgsPointXY(QPoint point) SIP_HOLDGIL
Create a point from a QPoint.
Definition: qgspointxy.h:84
bool operator!=(const QgsPointXY &other) const SIP_HOLDGIL
Inequality operator.
Definition: qgspointxy.h:266
QgsPointXY()=default
Default constructor.
QgsPointXY operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition: qgspointxy.h:321
void multiply(double scalar) SIP_HOLDGIL
Multiply x and y by the given value.
Definition: qgspointxy.h:283
QgsPointXY(double x, double y) SIP_HOLDGIL
Create a point from x,y coordinates.
Definition: qgspointxy.h:62
QgsPointXY & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition: qgspointxy.h:327
QgsPointXY & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition: qgspointxy.h:324
double y
Definition: qgspointxy.h:48
bool compare(const QgsPointXY &other, double epsilon=4 *std::numeric_limits< double >::epsilon()) const SIP_HOLDGIL
Compares this point with another point with a fuzzy tolerance.
Definition: qgspointxy.h:243
double x() const SIP_HOLDGIL
Gets the x value of the point.
Definition: qgspointxy.h:135
QgsVector operator-(const QgsPointXY &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspointxy.h:303
Q_GADGET double x
Definition: qgspointxy.h:47
double distance(double x, double y) const SIP_HOLDGIL
Returns the distance between this point and a specified x, y coordinate.
Definition: qgspointxy.h:196
QgsPointXY & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspointxy.h:306
double y() const SIP_HOLDGIL
Gets the y value of the point.
Definition: qgspointxy.h:144
~QgsPointXY()=default
QgsPointXY & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspointxy.h:309
void setY(double y) SIP_HOLDGIL
Sets the y value of the point.
Definition: qgspointxy.h:117
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:154
QgsPointXY operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspointxy.h:312
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
A class to represent a vector.
Definition: qgsvector.h:30
double y() const SIP_HOLDGIL
Returns the vector's y-component.
Definition: qgsvector.h:156
double x() const SIP_HOLDGIL
Returns the vector's x-component.
Definition: qgsvector.h:147
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:316
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:757
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_HOLDGIL
Definition: qgis_sip.h:157
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
uint qHash(const QgsPointXY &p)
Definition: qgspointxy.h:415
std::ostream & operator<<(std::ostream &os, const QgsPointXY &p)
Definition: qgspointxy.h:408
int precision