QGIS API Documentation 3.35.0-Master (274c3da7543)
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
60class CORE_EXPORT QgsPointXY
61{
62 Q_GADGET
63
64 Q_PROPERTY( double x READ x WRITE setX )
65 Q_PROPERTY( double y READ y WRITE setY )
66
67 public:
69 QgsPointXY() = default;
70
73
79 QgsPointXY( double x, double y ) SIP_HOLDGIL
80 : mX( x )
81 , mY( y )
82 , mIsEmpty( false )
83 {}
84
90 QgsPointXY( QPointF point ) SIP_HOLDGIL
91 : mX( point.x() )
92 , mY( point.y() )
93 , mIsEmpty( false )
94 {}
95
101 QgsPointXY( QPoint point ) SIP_HOLDGIL
102 : mX( point.x() )
103 , mY( point.y() )
104 , mIsEmpty( false )
105 {}
106
113 QgsPointXY( const QgsPoint &point ) SIP_HOLDGIL;
114
115 // IMPORTANT - while QgsPointXY is inherited by QgsReferencedPointXY, we do NOT want a virtual destructor here
116 // because this class MUST be lightweight and we don't want the cost of the vtable here.
117 // see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
118 ~QgsPointXY() = default;
119
124 void setX( double x ) SIP_HOLDGIL
125 {
126 mX = x;
127 mIsEmpty = false;
128 }
129
134 void setY( double y ) SIP_HOLDGIL
135 {
136 mY = y;
137 mIsEmpty = false;
138 }
139
141 void set( double x, double y ) SIP_HOLDGIL
142 {
143 mX = x;
144 mY = y;
145 mIsEmpty = false;
146 }
147
152 double x() const SIP_HOLDGIL
153 {
154 return mX;
155 }
156
161 double y() const SIP_HOLDGIL
162 {
163 return mY;
164 }
165
171 QPointF toQPointF() const
172 {
173 return QPointF( mX, mY );
174 }
175
180 QString toString( int precision = -1 ) const;
181
186 QString asWkt() const;
187
192 double sqrDist( double x, double y ) const SIP_HOLDGIL
193 {
194 return QgsGeometryUtilsBase::sqrDistance2D( mX, mY, x, y );
195 }
196
201 double sqrDist( const QgsPointXY &other ) const SIP_HOLDGIL
202 {
203 return QgsGeometryUtilsBase::sqrDistance2D( mX, mY, other.x(), other.y() );
204 }
205
213 double distance( double x, double y ) const SIP_HOLDGIL
214 {
215 return QgsGeometryUtilsBase::distance2D( mX, mY, x, y );
216 }
217
224 double distance( const QgsPointXY &other ) const SIP_HOLDGIL
225 {
226 return QgsGeometryUtilsBase::distance2D( mX, mY, other.x(), other.y() );
227 }
228
230 double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint SIP_OUT, double epsilon = DEFAULT_SEGMENT_EPSILON ) const SIP_HOLDGIL;
231
233 double azimuth( const QgsPointXY &other ) const SIP_HOLDGIL;
234
242 QgsPointXY project( double distance, double bearing ) const SIP_HOLDGIL;
243
251 bool isEmpty() const SIP_HOLDGIL { return mIsEmpty; }
252
263 bool compare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
264 {
265 return QgsGeometryUtilsBase::fuzzyEqual( epsilon, mX, mY, other.x(), other.y() );
266 }
267
278 bool distanceCompare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
279 {
280 return QgsGeometryUtilsBase::fuzzyDistanceEqual( epsilon, mX, mY, other.x(), other.y() );
281 }
282
284 bool operator==( const QgsPointXY &other ) SIP_HOLDGIL
285 {
286 if ( isEmpty() && other.isEmpty() )
287 return true;
288 if ( isEmpty() && !other.isEmpty() )
289 return false;
290 if ( ! isEmpty() && other.isEmpty() )
291 return false;
292
293 return QgsGeometryUtilsBase::fuzzyEqual( 1E-8, mX, mY, other.x(), other.y() );
294 }
295
297 bool operator!=( const QgsPointXY &other ) const SIP_HOLDGIL
298 {
299 if ( isEmpty() && other.isEmpty() )
300 return false;
301 if ( isEmpty() && !other.isEmpty() )
302 return true;
303 if ( ! isEmpty() && other.isEmpty() )
304 return true;
305
306 return !QgsGeometryUtilsBase::fuzzyEqual( 1E-8, mX, mY, other.x(), other.y() );
307 }
308
310 void multiply( double scalar ) SIP_HOLDGIL
311 {
312 mX *= scalar;
313 mY *= scalar;
314 }
315
318 {
319 if ( &other != this )
320 {
321 mX = other.x();
322 mY = other.y();
323 mIsEmpty = other.isEmpty();
324 }
325
326 return *this;
327 }
328
330 QgsVector operator-( const QgsPointXY &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
331
333 QgsPointXY &operator+=( QgsVector v ) { *this = *this + v; return *this; }
334
336 QgsPointXY &operator-=( QgsVector v ) { *this = *this - v; return *this; }
337
339 QgsPointXY operator+( QgsVector v ) const { return QgsPointXY( mX + v.x(), mY + v.y() ); }
340
342 QgsPointXY operator-( QgsVector v ) const { return QgsPointXY( mX - v.x(), mY - v.y() ); }
343
345 QgsPointXY operator*( double scalar ) const { return QgsPointXY( mX * scalar, mY * scalar ); }
346
348 QgsPointXY operator/( double scalar ) const { return QgsPointXY( mX / scalar, mY / scalar ); }
349
351 QgsPointXY &operator*=( double scalar ) { mX *= scalar; mY *= scalar; return *this; }
352
354 QgsPointXY &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }
355
357 operator QVariant() const
358 {
359 return QVariant::fromValue( *this );
360 }
361
362#ifdef SIP_RUN
363 SIP_PYOBJECT __repr__();
364 % MethodCode
365 QString str = QStringLiteral( "<QgsPointXY: %1>" ).arg( sipCpp->asWkt() );
366 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
367 % End
368
369 int __len__();
370 % MethodCode
371 sipRes = 2;
372 % End
373
374
375 SIP_PYOBJECT __getitem__( int );
376 % MethodCode
377 if ( a0 == 0 )
378 {
379 sipRes = Py_BuildValue( "d", sipCpp->x() );
380 }
381 else if ( a0 == 1 )
382 {
383 sipRes = Py_BuildValue( "d", sipCpp->y() );
384 }
385 else
386 {
387 QString msg = QString( "Bad index: %1" ).arg( a0 );
388 PyErr_SetString( PyExc_IndexError, msg.toLatin1().constData() );
389 }
390 % End
391
392 long __hash__() const;
393 % MethodCode
394 sipRes = qHash( *sipCpp );
395 % End
396#endif
397
398 private:
399
401 double mX = 0; //std::numeric_limits<double>::quiet_NaN();
402
404 double mY = 0; //std::numeric_limits<double>::quiet_NaN();
405
407 bool mIsEmpty = true;
408
409 friend uint qHash( const QgsPointXY &pnt );
410
411}; // class QgsPointXY
412
414
415inline bool operator==( const QgsPointXY &p1, const QgsPointXY &p2 ) SIP_SKIP
416{
417 const bool nan1X = std::isnan( p1.x() );
418 const bool nan2X = std::isnan( p2.x() );
419 if ( nan1X != nan2X )
420 return false;
421 if ( !nan1X && !qgsDoubleNear( p1.x(), p2.x(), 1E-8 ) )
422 return false;
423
424 const bool nan1Y = std::isnan( p1.y() );
425 const bool nan2Y = std::isnan( p2.y() );
426 if ( nan1Y != nan2Y )
427 return false;
428
429 if ( !nan1Y && !qgsDoubleNear( p1.y(), p2.y(), 1E-8 ) )
430 return false;
431
432 return true;
433}
434
435inline std::ostream &operator << ( std::ostream &os, const QgsPointXY &p ) SIP_SKIP
436{
437 // Use Local8Bit for printouts
438 os << p.toString().toLocal8Bit().data();
439 return os;
440}
441
442inline uint qHash( const QgsPointXY &p ) SIP_SKIP
443{
444 uint hash;
445 const uint h1 = qHash( static_cast< quint64 >( p.mX ) );
446 const uint h2 = qHash( static_cast< quint64 >( p.mY ) );
447 hash = h1 ^ ( h2 << 1 );
448 return hash;
449}
450
451
452#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:61
double x() const
Gets the x value of the point.
Definition qgspointxy.h:152
QgsPointXY(QPoint point)
Create a point from a QPoint.
Definition qgspointxy.h:101
QgsPointXY operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition qgspointxy.h:345
QgsPointXY & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition qgspointxy.h:336
QgsPointXY(QPointF point)
Create a point from a QPointF.
Definition qgspointxy.h:90
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition qgspointxy.h:192
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition qgspointxy.h:213
QgsPointXY operator-(QgsVector v) const
Subtracts a vector from this point.
Definition qgspointxy.h:342
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:134
double distance(const QgsPointXY &other) const
Returns the distance between this point and another point.
Definition qgspointxy.h:224
void multiply(double scalar)
Multiply x and y by the given value.
Definition qgspointxy.h:310
QgsPointXY & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition qgspointxy.h:351
QgsPointXY operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition qgspointxy.h:348
QgsPointXY & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition qgspointxy.h:354
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:263
bool operator!=(const QgsPointXY &other) const
Inequality operator.
Definition qgspointxy.h:297
void set(double x, double y)
Sets the x and y value of the point.
Definition qgspointxy.h:141
QgsPointXY & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition qgspointxy.h:333
double y
Definition qgspointxy.h:65
QgsPointXY & operator=(const QgsPointXY &other)
Assignment.
Definition qgspointxy.h:317
QgsVector operator-(const QgsPointXY &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition qgspointxy.h:330
double y() const
Gets the y value of the point.
Definition qgspointxy.h:161
double x
Definition qgspointxy.h:64
double sqrDist(const QgsPointXY &other) const
Returns the squared distance between this point another point.
Definition qgspointxy.h:201
void setX(double x)
Sets the x value of the point.
Definition qgspointxy.h:124
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:278
~QgsPointXY()=default
bool isEmpty() const
Returns true if the geometry is empty.
Definition qgspointxy.h:251
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:171
QgsPointXY operator+(QgsVector v) const
Adds a vector to this point.
Definition qgspointxy.h:339
bool operator==(const QgsPointXY &other)
equality operator
Definition qgspointxy.h:284
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:50
A class to represent a vector.
Definition qgsvector.h:30
double y() const
Returns the vector's y-component.
Definition qgsvector.h:156
double x() const
Returns the vector's x-component.
Definition qgsvector.h:147
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:4831
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition qgis.h:5362
#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:442
std::ostream & operator<<(std::ostream &os, const QgsPointXY &p)
Definition qgspointxy.h:435
int precision