QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
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"
24
25#include "qgis.h"
26
27#include <iostream>
28#include <QString>
29#include <QPoint>
30#include <QObject>
31#include <qglobal.h>
32
33class QgsPoint;
34
59class CORE_EXPORT QgsPointXY
60{
61 Q_GADGET
62
63 Q_PROPERTY( double x READ x WRITE setX )
64 Q_PROPERTY( double y READ y WRITE setY )
65
66 public:
68 QgsPointXY() = default;
69
72
78 QgsPointXY( double x, double y ) SIP_HOLDGIL
79 : mX( x )
80 , mY( y )
81 , mIsEmpty( false )
82 {}
83
88 QgsPointXY( QPointF point ) SIP_HOLDGIL
89 : mX( point.x() )
90 , mY( point.y() )
91 , mIsEmpty( false )
92 {}
93
98 QgsPointXY( QPoint point ) SIP_HOLDGIL
99 : mX( point.x() )
100 , mY( point.y() )
101 , mIsEmpty( false )
102 {}
103
109 QgsPointXY( const QgsPoint &point ) SIP_HOLDGIL;
110
111 // IMPORTANT - while QgsPointXY is inherited by QgsReferencedPointXY, we do NOT want a virtual destructor here
112 // because this class MUST be lightweight and we don't want the cost of the vtable here.
113 // see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
114 ~QgsPointXY() = default;
115
120 void setX( double x ) SIP_HOLDGIL
121 {
122 mX = x;
123 mIsEmpty = false;
124 }
125
130 void setY( double y ) SIP_HOLDGIL
131 {
132 mY = y;
133 mIsEmpty = false;
134 }
135
137 void set( double x, double y ) SIP_HOLDGIL
138 {
139 mX = x;
140 mY = y;
141 mIsEmpty = false;
142 }
143
148 double x() const SIP_HOLDGIL
149 {
150 return mX;
151 }
152
157 double y() const SIP_HOLDGIL
158 {
159 return mY;
160 }
161
166 QPointF toQPointF() const
167 {
168 return QPointF( mX, mY );
169 }
170
175 QString toString( int precision = -1 ) const;
176
181 QString asWkt() const;
182
187 double sqrDist( double x, double y ) const SIP_HOLDGIL
188 {
189 return QgsGeometryUtilsBase::sqrDistance2D( mX, mY, x, y );
190 }
191
196 double sqrDist( const QgsPointXY &other ) const SIP_HOLDGIL
197 {
198 return QgsGeometryUtilsBase::sqrDistance2D( mX, mY, other.x(), other.y() );
199 }
200
207 double distance( double x, double y ) const SIP_HOLDGIL
208 {
209 return QgsGeometryUtilsBase::distance2D( mX, mY, x, y );
210 }
211
217 double distance( const QgsPointXY &other ) const SIP_HOLDGIL
218 {
219 return QgsGeometryUtilsBase::distance2D( mX, mY, other.x(), other.y() );
220 }
221
223 double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint SIP_OUT, double epsilon = DEFAULT_SEGMENT_EPSILON ) const SIP_HOLDGIL;
224
226 double azimuth( const QgsPointXY &other ) const SIP_HOLDGIL;
227
234 QgsPointXY project( double distance, double bearing ) const SIP_HOLDGIL;
235
243 bool isEmpty() const SIP_HOLDGIL { return mIsEmpty; }
244
254 bool compare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
255 {
256 return QgsGeometryUtilsBase::fuzzyEqual( epsilon, mX, mY, other.x(), other.y() );
257 }
258
269 bool distanceCompare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
270 {
271 return QgsGeometryUtilsBase::fuzzyDistanceEqual( epsilon, mX, mY, other.x(), other.y() );
272 }
273
275 bool operator==( const QgsPointXY &other ) SIP_HOLDGIL
276 {
277 if ( isEmpty() && other.isEmpty() )
278 return true;
279 if ( isEmpty() && !other.isEmpty() )
280 return false;
281 if ( ! isEmpty() && other.isEmpty() )
282 return false;
283
284 return QgsGeometryUtilsBase::fuzzyEqual( 1E-8, mX, mY, other.x(), other.y() );
285 }
286
288 bool operator!=( const QgsPointXY &other ) const SIP_HOLDGIL
289 {
290 if ( isEmpty() && other.isEmpty() )
291 return false;
292 if ( isEmpty() && !other.isEmpty() )
293 return true;
294 if ( ! isEmpty() && other.isEmpty() )
295 return true;
296
297 return !QgsGeometryUtilsBase::fuzzyEqual( 1E-8, mX, mY, other.x(), other.y() );
298 }
299
301 void multiply( double scalar ) SIP_HOLDGIL
302 {
303 mX *= scalar;
304 mY *= scalar;
305 }
306
309 {
310 if ( &other != this )
311 {
312 mX = other.x();
313 mY = other.y();
314 mIsEmpty = other.isEmpty();
315 }
316
317 return *this;
318 }
319
321 QgsVector operator-( const QgsPointXY &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
322
324 QgsPointXY &operator+=( QgsVector v ) { *this = *this + v; return *this; }
325
327 QgsPointXY &operator-=( QgsVector v ) { *this = *this - v; return *this; }
328
330 QgsPointXY operator+( QgsVector v ) const { return QgsPointXY( mX + v.x(), mY + v.y() ); }
331
333 QgsPointXY operator-( QgsVector v ) const { return QgsPointXY( mX - v.x(), mY - v.y() ); }
334
336 QgsPointXY operator*( double scalar ) const { return QgsPointXY( mX * scalar, mY * scalar ); }
337
339 QgsPointXY operator/( double scalar ) const { return QgsPointXY( mX / scalar, mY / scalar ); }
340
342 QgsPointXY &operator*=( double scalar ) { mX *= scalar; mY *= scalar; return *this; }
343
345 QgsPointXY &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }
346
348 operator QVariant() const
349 {
350 return QVariant::fromValue( *this );
351 }
352
353#ifdef SIP_RUN
354 SIP_PYOBJECT __repr__();
355 % MethodCode
356 QString str = QStringLiteral( "<QgsPointXY: %1>" ).arg( sipCpp->asWkt() );
357 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
358 % End
359
360 int __len__();
361 % MethodCode
362 sipRes = 2;
363 % End
364
365
366 SIP_PYOBJECT __getitem__( int );
367 % MethodCode
368 if ( a0 == 0 )
369 {
370 sipRes = Py_BuildValue( "d", sipCpp->x() );
371 }
372 else if ( a0 == 1 )
373 {
374 sipRes = Py_BuildValue( "d", sipCpp->y() );
375 }
376 else
377 {
378 QString msg = QString( "Bad index: %1" ).arg( a0 );
379 PyErr_SetString( PyExc_IndexError, msg.toLatin1().constData() );
380 }
381 % End
382
383 long __hash__() const;
384 % MethodCode
385 sipRes = qHash( *sipCpp );
386 % End
387#endif
388
389 private:
390
392 double mX = 0; //std::numeric_limits<double>::quiet_NaN();
393
395 double mY = 0; //std::numeric_limits<double>::quiet_NaN();
396
398 bool mIsEmpty = true;
399
400 friend uint qHash( const QgsPointXY &pnt );
401
402}; // class QgsPointXY
403
405
406inline bool operator==( const QgsPointXY &p1, const QgsPointXY &p2 ) SIP_SKIP
407{
408 const bool nan1X = std::isnan( p1.x() );
409 const bool nan2X = std::isnan( p2.x() );
410 if ( nan1X != nan2X )
411 return false;
412 if ( !nan1X && !qgsDoubleNear( p1.x(), p2.x(), 1E-8 ) )
413 return false;
414
415 const bool nan1Y = std::isnan( p1.y() );
416 const bool nan2Y = std::isnan( p2.y() );
417 if ( nan1Y != nan2Y )
418 return false;
419
420 if ( !nan1Y && !qgsDoubleNear( p1.y(), p2.y(), 1E-8 ) )
421 return false;
422
423 return true;
424}
425
426inline std::ostream &operator << ( std::ostream &os, const QgsPointXY &p ) SIP_SKIP
427{
428 // Use Local8Bit for printouts
429 os << p.toString().toLocal8Bit().data();
430 return os;
431}
432
433inline uint qHash( const QgsPointXY &p ) SIP_SKIP
434{
435 uint hash;
436 const uint h1 = qHash( static_cast< quint64 >( p.mX ) );
437 const uint h2 = qHash( static_cast< quint64 >( p.mY ) );
438 hash = h1 ^ ( h2 << 1 );
439 return hash;
440}
441
442
443#endif //QGSPOINTXY_H
static double sqrDistance2D(double x1, double y1, double x2, double y2)
Returns the squared 2D distance between (x1, y1) and (x2, y2).
static double distance2D(double x1, double y1, double x2, double y2)
Returns the 2D distance between (x1, y1) and (x2, y2).
static bool fuzzyEqual(T epsilon, const Args &... args) noexcept
Performs fuzzy comparison between pairs of values within a specified epsilon.
static bool fuzzyDistanceEqual(T epsilon, const Args &... args) noexcept
Compare equality between multiple pairs of values with a specified epsilon.
A class to represent a 2D point.
Definition qgspointxy.h:60
double x() const
Gets the x value of the point.
Definition qgspointxy.h:148
QgsPointXY(QPoint point)
Create a point from a QPoint.
Definition qgspointxy.h:98
QgsPointXY operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition qgspointxy.h:336
QgsPointXY & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition qgspointxy.h:327
QgsPointXY(QPointF point)
Create a point from a QPointF.
Definition qgspointxy.h:88
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition qgspointxy.h:187
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition qgspointxy.h:207
QgsPointXY operator-(QgsVector v) const
Subtracts a vector from this point.
Definition qgspointxy.h:333
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:130
double distance(const QgsPointXY &other) const
Returns the distance between this point and another point.
Definition qgspointxy.h:217
void multiply(double scalar)
Multiply x and y by the given value.
Definition qgspointxy.h:301
QgsPointXY & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition qgspointxy.h:342
QgsPointXY operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition qgspointxy.h:339
QgsPointXY & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition qgspointxy.h:345
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:254
bool operator!=(const QgsPointXY &other) const
Inequality operator.
Definition qgspointxy.h:288
void set(double x, double y)
Sets the x and y value of the point.
Definition qgspointxy.h:137
QgsPointXY & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition qgspointxy.h:324
double y
Definition qgspointxy.h:64
QgsPointXY & operator=(const QgsPointXY &other)
Assignment.
Definition qgspointxy.h:308
QgsVector operator-(const QgsPointXY &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition qgspointxy.h:321
double y() const
Gets the y value of the point.
Definition qgspointxy.h:157
double x
Definition qgspointxy.h:63
double sqrDist(const QgsPointXY &other) const
Returns the squared distance between this point another point.
Definition qgspointxy.h:196
void setX(double x)
Sets the x value of the point.
Definition qgspointxy.h:120
bool distanceCompare(const QgsPointXY &other, double epsilon=4 *std::numeric_limits< double >::epsilon()) const
Compares this point with another point with a fuzzy tolerance using distance comparison.
Definition qgspointxy.h:269
~QgsPointXY()=default
bool isEmpty() const
Returns true if the geometry is empty.
Definition qgspointxy.h:243
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:166
QgsPointXY operator+(QgsVector v) const
Adds a vector to this point.
Definition qgspointxy.h:330
bool operator==(const QgsPointXY &other)
equality operator
Definition qgspointxy.h:275
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
Returns the vector's y-component.
Definition qgsvector.h:152
double x() const
Returns the vector's x-component.
Definition qgsvector.h:143
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition qgis.cpp:198
#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:5445
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition qgis.h:5981
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_HOLDGIL
Definition qgis_sip.h:171
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
uint qHash(const QgsPointXY &p)
Definition qgspointxy.h:433
std::ostream & operator<<(std::ostream &os, const QgsPointXY &p)
Definition qgspointxy.h:426
int precision