QGIS API Documentation  3.0.2-Girona (307d082)
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 QGSPOINT_H
19 #define QGSPOINT_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  {}
66 
72  QgsPointXY( QPointF point )
73  : mX( point.x() )
74  , mY( point.y() )
75  {}
76 
82  QgsPointXY( QPoint point )
83  : mX( point.x() )
84  , mY( point.y() )
85  {}
86 
93  QgsPointXY( const QgsPoint &point );
94 
95  // IMPORTANT - while QgsPointXY is inherited by QgsReferencedPointXY, we do NOT want a virtual destructor here
96  // because this class MUST be lightweight and we don't want the cost of the vtable here.
97  // see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
98  ~QgsPointXY() = default;
99 
104  void setX( double x )
105  {
106  mX = x;
107  }
108 
113  void setY( double y )
114  {
115  mY = y;
116  }
117 
119  void set( double x, double y )
120  {
121  mX = x;
122  mY = y;
123  }
124 
129  double x() const
130  {
131  return mX;
132  }
133 
138  double y() const
139  {
140  return mY;
141  }
142 
148  QPointF toQPointF() const;
149 
154  QString toString( int precision = -1 ) const;
155 
160  QString asWkt() const;
161 
166  double sqrDist( double x, double y ) const;
167 
172  double sqrDist( const QgsPointXY &other ) const;
173 
181  double distance( double x, double y ) const;
182 
189  double distance( const QgsPointXY &other ) const;
190 
192  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint SIP_OUT, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
193 
195  double azimuth( const QgsPointXY &other ) const;
196 
204  QgsPointXY project( double distance, double bearing ) const;
205 
213  bool compare( const QgsPointXY &other, double epsilon = 4 * DBL_EPSILON ) const;
214 
216  bool operator==( const QgsPointXY &other );
217 
219  bool operator!=( const QgsPointXY &other ) const;
220 
222  void multiply( double scalar );
223 
225  QgsPointXY &operator=( const QgsPointXY &other );
226 
228  QgsVector operator-( const QgsPointXY &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
229 
231  QgsPointXY &operator+=( QgsVector v ) { *this = *this + v; return *this; }
232 
234  QgsPointXY &operator-=( QgsVector v ) { *this = *this - v; return *this; }
235 
237  QgsPointXY operator+( QgsVector v ) const { return QgsPointXY( mX + v.x(), mY + v.y() ); }
238 
240  QgsPointXY operator-( QgsVector v ) const { return QgsPointXY( mX - v.x(), mY - v.y() ); }
241 
243  QgsPointXY operator*( double scalar ) const { return QgsPointXY( mX * scalar, mY * scalar ); }
244 
246  QgsPointXY operator/( double scalar ) const { return QgsPointXY( mX / scalar, mY / scalar ); }
247 
249  QgsPointXY &operator*=( double scalar ) { mX *= scalar; mY *= scalar; return *this; }
250 
252  QgsPointXY &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }
253 
255  operator QVariant() const
256  {
257  return QVariant::fromValue( *this );
258  }
259 
260 #ifdef SIP_RUN
261  SIP_PYOBJECT __repr__();
262  % MethodCode
263  QString str = "(" + QString::number( sipCpp->x() ) + "," + QString::number( sipCpp->y() ) + ")";
264  //QString str("(%f,%f)").arg(sipCpp->x()).arg(sipCpp->y());
265  sipRes = PyUnicode_FromString( str.toUtf8().data() );
266  % End
267 
268  int __len__();
269  % MethodCode
270  sipRes = 2;
271  % End
272 
273 
274  SIP_PYOBJECT __getitem__( int );
275  % MethodCode
276  if ( a0 == 0 )
277  {
278  sipRes = Py_BuildValue( "d", sipCpp->x() );
279  }
280  else if ( a0 == 1 )
281  {
282  sipRes = Py_BuildValue( "d", sipCpp->y() );
283  }
284  else
285  {
286  QString msg = QString( "Bad index: %1" ).arg( a0 );
287  PyErr_SetString( PyExc_IndexError, msg.toAscii().constData() );
288  }
289  % End
290 
291  long __hash__() const;
292  % MethodCode
293  sipRes = qHash( *sipCpp );
294  % End
295 #endif
296 
297  private:
298 
300  double mX = 0.0;
301 
303  double mY = 0.0;
304 
305  friend uint qHash( const QgsPointXY &pnt );
306 
307 }; // class QgsPoint
308 
310 
311 inline bool operator==( const QgsPointXY &p1, const QgsPointXY &p2 ) SIP_SKIP
312 {
313  if ( qgsDoubleNear( p1.x(), p2.x() ) && qgsDoubleNear( p1.y(), p2.y() ) )
314  { return true; }
315  else
316  { return false; }
317 }
318 
319 inline std::ostream &operator << ( std::ostream &os, const QgsPointXY &p ) SIP_SKIP
320 {
321  // Use Local8Bit for printouts
322  os << p.toString().toLocal8Bit().data();
323  return os;
324 }
325 
326 inline uint qHash( const QgsPointXY &p ) SIP_SKIP
327 {
328  uint hash;
329  uint h1 = qHash( static_cast< quint64 >( p.mX ) );
330  uint h2 = qHash( static_cast< quint64 >( p.mY ) );
331  hash = h1 ^ ( h2 << 1 );
332  return hash;
333 }
334 
335 
336 #endif //QGSPOINT_H
QgsPointXY & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition: qgspointxy.h:249
uint qHash(const QgsPointXY &p)
Definition: qgspointxy.h:326
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool operator==(const QgsPointXY &p1, const QgsPointXY &p2)
Definition: qgspointxy.h:311
QgsVector operator-(const QgsPointXY &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspointxy.h:228
Q_DECLARE_METATYPE(QModelIndex)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:251
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:465
#define SIP_SKIP
Definition: qgis_sip.h:119
void setY(double y)
Sets the y value of the point.
Definition: qgspointxy.h:113
double x() const
Get the x value of the point.
Definition: qgspointxy.h:129
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
void setX(double x)
Sets the x value of the point.
Definition: qgspointxy.h:104
QgsPointXY(QPoint point)
Create a point from a QPoint.
Definition: qgspointxy.h:82
A class to represent a vector.
Definition: qgsvector.h:27
QgsPointXY & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspointxy.h:231
std::ostream & operator<<(std::ostream &os, const QgsPointXY &p)
Definition: qgspointxy.h:319
QgsPointXY & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspointxy.h:234
QgsPointXY operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspointxy.h:237
QgsPointXY(QPointF point)
Create a point from a QPointF.
Definition: qgspointxy.h:72
#define SIP_OUT
Definition: qgis_sip.h:51
QgsPointXY operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspointxy.h:240
double y() const
Get the y value of the point.
Definition: qgspointxy.h:138
QgsPointXY operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition: qgspointxy.h:243
QgsPointXY operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition: qgspointxy.h:246
QgsPointXY & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition: qgspointxy.h:252
double x() const
Returns the vector&#39;s x-component.
Definition: qgsvector.cpp:76
double y() const
Returns the vector&#39;s y-component.
Definition: qgsvector.cpp:81