QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 );
56 
62  QgsPointXY( double x, double y )
63  : mX( x )
64  , mY( y )
65  , mIsEmpty( false )
66  {}
67 
73  QgsPointXY( QPointF point )
74  : mX( point.x() )
75  , mY( point.y() )
76  , mIsEmpty( false )
77  {}
78 
84  QgsPointXY( QPoint point )
85  : mX( point.x() )
86  , mY( point.y() )
87  , mIsEmpty( false )
88  {}
89 
96  QgsPointXY( const QgsPoint &point );
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 )
108  {
109  mX = x;
110  mIsEmpty = false;
111  }
112 
117  void setY( double y )
118  {
119  mY = y;
120  mIsEmpty = false;
121  }
122 
124  void set( double x, double y )
125  {
126  mX = x;
127  mY = y;
128  mIsEmpty = false;
129  }
130 
135  double x() const
136  {
137  return mX;
138  }
139 
144  double y() const
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
176  {
177  return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y );
178  }
179 
184  double sqrDist( const QgsPointXY &other ) const
185  {
186  return sqrDist( other.x(), other.y() );
187  }
188 
196  double distance( double x, double y ) const
197  {
198  return std::sqrt( sqrDist( x, y ) );
199  }
200 
207  double distance( const QgsPointXY &other ) const
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;
214 
216  double azimuth( const QgsPointXY &other ) const;
217 
225  QgsPointXY project( double distance, double bearing ) const;
226 
234  bool isEmpty() const { return mIsEmpty; }
235 
243  bool compare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const
244  {
245  return ( qgsDoubleNear( mX, other.x(), epsilon ) && qgsDoubleNear( mY, other.y(), epsilon ) );
246  }
247 
249  bool operator==( const QgsPointXY &other )
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
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 )
284  {
285  mX *= scalar;
286  mY *= scalar;
287  }
288 
290  QgsPointXY &operator=( const QgsPointXY &other )
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
QgsPointXY::operator=
QgsPointXY & operator=(const QgsPointXY &other)
Assignment.
Definition: qgspointxy.h:290
QgsPointXY::y
double y
Definition: qgspointxy.h:48
QgsPointXY::operator+
QgsPointXY operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspointxy.h:312
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
QgsPointXY::sqrDist
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition: qgspointxy.h:175
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsPointXY::operator+=
QgsPointXY & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspointxy.h:306
QgsPointXY::operator/=
QgsPointXY & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition: qgspointxy.h:327
qgis.h
QgsPointXY::operator-
QgsVector operator-(const QgsPointXY &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspointxy.h:303
QgsPointXY::toQPointF
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:154
qgsvector.h
DEFAULT_SEGMENT_EPSILON
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:712
QgsPointXY::operator==
bool operator==(const QgsPointXY &other)
equality operator
Definition: qgspointxy.h:249
QgsPointXY::operator!=
bool operator!=(const QgsPointXY &other) const
Inequality operator.
Definition: qgspointxy.h:266
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
precision
int precision
Definition: qgswfsgetfeature.cpp:103
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsVector::x
double x() const
Returns the vector's x-component.
Definition: qgsvector.cpp:76
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsPointXY::operator-=
QgsPointXY & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspointxy.h:309
QgsPointXY::distance
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition: qgspointxy.h:196
QgsPointXY::distance
double distance(const QgsPointXY &other) const
Returns the distance between this point and another point.
Definition: qgspointxy.h:207
QgsPointXY::compare
bool compare(const QgsPointXY &other, double epsilon=4 *std::numeric_limits< double >::epsilon()) const
Compares this point with another point with a fuzzy tolerance.
Definition: qgspointxy.h:243
QgsPointXY::setY
void setY(double y)
Sets the y value of the point.
Definition: qgspointxy.h:117
QgsPointXY::x
double x() const
Gets the x value of the point.
Definition: qgspointxy.h:135
QgsPointXY::multiply
void multiply(double scalar)
Multiply x and y by the given value.
Definition: qgspointxy.h:283
QgsPointXY::operator/
QgsPointXY operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition: qgspointxy.h:321
QgsPointXY
Definition: qgspointxy.h:43
QgsPointXY::operator*=
QgsPointXY & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition: qgspointxy.h:324
QgsPointXY::operator*
QgsPointXY operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition: qgspointxy.h:318
QgsVector::y
double y() const
Returns the vector's y-component.
Definition: qgsvector.cpp:81
QgsVector
Definition: qgsvector.h:29
QgsPointXY::x
double x
Definition: qgspointxy.h:47
QgsPointXY::isEmpty
bool isEmpty() const
Returns true if the geometry is empty.
Definition: qgspointxy.h:234
QgsPointXY::sqrDist
double sqrDist(const QgsPointXY &other) const
Returns the squared distance between this point another point.
Definition: qgspointxy.h:184
operator<<
std::ostream & operator<<(std::ostream &os, const QgsPointXY &p)
Definition: qgspointxy.h:408
QgsPointXY::setX
void setX(double x)
Sets the x value of the point.
Definition: qgspointxy.h:107
QgsPointXY::QgsPointXY
QgsPointXY(QPoint point)
Create a point from a QPoint.
Definition: qgspointxy.h:84
QgsPointXY::QgsPointXY
QgsPointXY(QPointF point)
Create a point from a QPointF.
Definition: qgspointxy.h:73
qHash
uint qHash(const QgsPointXY &p)
Definition: qgspointxy.h:415
QgsPointXY::y
double y() const
Gets the y value of the point.
Definition: qgspointxy.h:144
QgsPointXY::operator-
QgsPointXY operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspointxy.h:315
QgsPointXY::set
void set(double x, double y)
Sets the x and y value of the point.
Definition: qgspointxy.h:124