QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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
31class QgsPoint;
32
58class CORE_EXPORT QgsPointXY
59{
60 Q_GADGET
61
62 Q_PROPERTY( double x READ x WRITE setX )
63 Q_PROPERTY( double y READ y WRITE setY )
64
65 public:
67 QgsPointXY() = default;
68
71
77 QgsPointXY( double x, double y ) SIP_HOLDGIL
78 : mX( x )
79 , mY( y )
80 , mIsEmpty( false )
81 {}
82
88 QgsPointXY( QPointF point ) SIP_HOLDGIL
89 : mX( point.x() )
90 , mY( point.y() )
91 , mIsEmpty( false )
92 {}
93
99 QgsPointXY( QPoint point ) SIP_HOLDGIL
100 : mX( point.x() )
101 , mY( point.y() )
102 , mIsEmpty( false )
103 {}
104
111 QgsPointXY( const QgsPoint &point ) SIP_HOLDGIL;
112
113 // IMPORTANT - while QgsPointXY is inherited by QgsReferencedPointXY, we do NOT want a virtual destructor here
114 // because this class MUST be lightweight and we don't want the cost of the vtable here.
115 // see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
116 ~QgsPointXY() = default;
117
122 void setX( double x ) SIP_HOLDGIL
123 {
124 mX = x;
125 mIsEmpty = false;
126 }
127
132 void setY( double y ) SIP_HOLDGIL
133 {
134 mY = y;
135 mIsEmpty = false;
136 }
137
139 void set( double x, double y ) SIP_HOLDGIL
140 {
141 mX = x;
142 mY = y;
143 mIsEmpty = false;
144 }
145
150 double x() const SIP_HOLDGIL
151 {
152 return mX;
153 }
154
159 double y() const SIP_HOLDGIL
160 {
161 return mY;
162 }
163
169 QPointF toQPointF() const
170 {
171 return QPointF( mX, mY );
172 }
173
178 QString toString( int precision = -1 ) const;
179
184 QString asWkt() const;
185
190 double sqrDist( double x, double y ) const SIP_HOLDGIL
191 {
192 return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y );
193 }
194
199 double sqrDist( const QgsPointXY &other ) const SIP_HOLDGIL
200 {
201 return sqrDist( other.x(), other.y() );
202 }
203
211 double distance( double x, double y ) const SIP_HOLDGIL
212 {
213 return std::sqrt( sqrDist( x, y ) );
214 }
215
222 double distance( const QgsPointXY &other ) const SIP_HOLDGIL
223 {
224 return std::sqrt( sqrDist( other ) );
225 }
226
228 double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint SIP_OUT, double epsilon = DEFAULT_SEGMENT_EPSILON ) const SIP_HOLDGIL;
229
231 double azimuth( const QgsPointXY &other ) const SIP_HOLDGIL;
232
240 QgsPointXY project( double distance, double bearing ) const SIP_HOLDGIL;
241
249 bool isEmpty() const SIP_HOLDGIL { return mIsEmpty; }
250
258 bool compare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
259 {
260 return ( qgsDoubleNear( mX, other.x(), epsilon ) && qgsDoubleNear( mY, other.y(), epsilon ) );
261 }
262
264 bool operator==( const QgsPointXY &other ) SIP_HOLDGIL
265 {
266 if ( isEmpty() && other.isEmpty() )
267 return true;
268 if ( isEmpty() && !other.isEmpty() )
269 return false;
270 if ( ! isEmpty() && other.isEmpty() )
271 return false;
272
273 bool equal = true;
274 equal &= qgsDoubleNear( other.x(), mX, 1E-8 );
275 equal &= qgsDoubleNear( other.y(), mY, 1E-8 );
276
277 return equal;
278 }
279
281 bool operator!=( const QgsPointXY &other ) const SIP_HOLDGIL
282 {
283 if ( isEmpty() && other.isEmpty() )
284 return false;
285 if ( isEmpty() && !other.isEmpty() )
286 return true;
287 if ( ! isEmpty() && other.isEmpty() )
288 return true;
289
290 bool equal = true;
291 equal &= qgsDoubleNear( other.x(), mX, 1E-8 );
292 equal &= qgsDoubleNear( other.y(), mY, 1E-8 );
293
294 return !equal;
295 }
296
298 void multiply( double scalar ) SIP_HOLDGIL
299 {
300 mX *= scalar;
301 mY *= scalar;
302 }
303
306 {
307 if ( &other != this )
308 {
309 mX = other.x();
310 mY = other.y();
311 mIsEmpty = other.isEmpty();
312 }
313
314 return *this;
315 }
316
318 QgsVector operator-( const QgsPointXY &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
319
321 QgsPointXY &operator+=( QgsVector v ) { *this = *this + v; return *this; }
322
324 QgsPointXY &operator-=( QgsVector v ) { *this = *this - v; return *this; }
325
327 QgsPointXY operator+( QgsVector v ) const { return QgsPointXY( mX + v.x(), mY + v.y() ); }
328
330 QgsPointXY operator-( QgsVector v ) const { return QgsPointXY( mX - v.x(), mY - v.y() ); }
331
333 QgsPointXY operator*( double scalar ) const { return QgsPointXY( mX * scalar, mY * scalar ); }
334
336 QgsPointXY operator/( double scalar ) const { return QgsPointXY( mX / scalar, mY / scalar ); }
337
339 QgsPointXY &operator*=( double scalar ) { mX *= scalar; mY *= scalar; return *this; }
340
342 QgsPointXY &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }
343
345 operator QVariant() const
346 {
347 return QVariant::fromValue( *this );
348 }
349
350#ifdef SIP_RUN
351 SIP_PYOBJECT __repr__();
352 % MethodCode
353 QString str = QStringLiteral( "<QgsPointXY: %1>" ).arg( sipCpp->asWkt() );
354 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
355 % End
356
357 int __len__();
358 % MethodCode
359 sipRes = 2;
360 % End
361
362
363 SIP_PYOBJECT __getitem__( int );
364 % MethodCode
365 if ( a0 == 0 )
366 {
367 sipRes = Py_BuildValue( "d", sipCpp->x() );
368 }
369 else if ( a0 == 1 )
370 {
371 sipRes = Py_BuildValue( "d", sipCpp->y() );
372 }
373 else
374 {
375 QString msg = QString( "Bad index: %1" ).arg( a0 );
376 PyErr_SetString( PyExc_IndexError, msg.toLatin1().constData() );
377 }
378 % End
379
380 long __hash__() const;
381 % MethodCode
382 sipRes = qHash( *sipCpp );
383 % End
384#endif
385
386 private:
387
389 double mX = 0; //std::numeric_limits<double>::quiet_NaN();
390
392 double mY = 0; //std::numeric_limits<double>::quiet_NaN();
393
395 bool mIsEmpty = true;
396
397 friend uint qHash( const QgsPointXY &pnt );
398
399}; // class QgsPointXY
400
402
403inline bool operator==( const QgsPointXY &p1, const QgsPointXY &p2 ) SIP_SKIP
404{
405 const bool nan1X = std::isnan( p1.x() );
406 const bool nan2X = std::isnan( p2.x() );
407 if ( nan1X != nan2X )
408 return false;
409 if ( !nan1X && !qgsDoubleNear( p1.x(), p2.x(), 1E-8 ) )
410 return false;
411
412 const bool nan1Y = std::isnan( p1.y() );
413 const bool nan2Y = std::isnan( p2.y() );
414 if ( nan1Y != nan2Y )
415 return false;
416
417 if ( !nan1Y && !qgsDoubleNear( p1.y(), p2.y(), 1E-8 ) )
418 return false;
419
420 return true;
421}
422
423inline std::ostream &operator << ( std::ostream &os, const QgsPointXY &p ) SIP_SKIP
424{
425 // Use Local8Bit for printouts
426 os << p.toString().toLocal8Bit().data();
427 return os;
428}
429
430inline uint qHash( const QgsPointXY &p ) SIP_SKIP
431{
432 uint hash;
433 const uint h1 = qHash( static_cast< quint64 >( p.mX ) );
434 const uint h2 = qHash( static_cast< quint64 >( p.mY ) );
435 hash = h1 ^ ( h2 << 1 );
436 return hash;
437}
438
439
440#endif //QGSPOINTXY_H
A class to represent a 2D point.
Definition: qgspointxy.h:59
QgsPointXY & operator=(const QgsPointXY &other) SIP_HOLDGIL
Assignment.
Definition: qgspointxy.h:305
double distance(const QgsPointXY &other) const SIP_HOLDGIL
Returns the distance between this point and another point.
Definition: qgspointxy.h:222
bool isEmpty() const SIP_HOLDGIL
Returns true if the geometry is empty.
Definition: qgspointxy.h:249
QgsPointXY operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition: qgspointxy.h:333
bool operator==(const QgsPointXY &other) SIP_HOLDGIL
equality operator
Definition: qgspointxy.h:264
QgsPointXY & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspointxy.h:324
void set(double x, double y) SIP_HOLDGIL
Sets the x and y value of the point.
Definition: qgspointxy.h:139
double sqrDist(const QgsPointXY &other) const SIP_HOLDGIL
Returns the squared distance between this point another point.
Definition: qgspointxy.h:199
QgsPointXY(QPointF point) SIP_HOLDGIL
Create a point from a QPointF.
Definition: qgspointxy.h:88
QgsPointXY operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspointxy.h:330
double sqrDist(double x, double y) const SIP_HOLDGIL
Returns the squared distance between this point a specified x, y coordinate.
Definition: qgspointxy.h:190
void setX(double x) SIP_HOLDGIL
Sets the x value of the point.
Definition: qgspointxy.h:122
QgsPointXY(QPoint point) SIP_HOLDGIL
Create a point from a QPoint.
Definition: qgspointxy.h:99
bool operator!=(const QgsPointXY &other) const SIP_HOLDGIL
Inequality operator.
Definition: qgspointxy.h:281
QgsPointXY()=default
Default constructor.
QgsPointXY & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition: qgspointxy.h:339
QgsPointXY operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition: qgspointxy.h:336
QgsPointXY & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition: qgspointxy.h:342
void multiply(double scalar) SIP_HOLDGIL
Multiply x and y by the given value.
Definition: qgspointxy.h:298
QgsPointXY(double x, double y) SIP_HOLDGIL
Create a point from x,y coordinates.
Definition: qgspointxy.h:77
QgsPointXY & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspointxy.h:321
double y
Definition: qgspointxy.h:63
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:258
double x() const SIP_HOLDGIL
Gets the x value of the point.
Definition: qgspointxy.h:150
QgsVector operator-(const QgsPointXY &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspointxy.h:318
Q_GADGET double x
Definition: qgspointxy.h:62
double distance(double x, double y) const SIP_HOLDGIL
Returns the distance between this point and a specified x, y coordinate.
Definition: qgspointxy.h:211
double y() const SIP_HOLDGIL
Gets the y value of the point.
Definition: qgspointxy.h:159
~QgsPointXY()=default
void setY(double y) SIP_HOLDGIL
Sets the y value of the point.
Definition: qgspointxy.h:132
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:169
QgsPointXY operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspointxy.h:327
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
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
#define str(x)
Definition: qgis.cpp:38
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:3988
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:4531
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_HOLDGIL
Definition: qgis_sip.h:166
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
uint qHash(const QgsPointXY &p)
Definition: qgspointxy.h:430
std::ostream & operator<<(std::ostream &os, const QgsPointXY &p)
Definition: qgspointxy.h:423
int precision