QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <iostream>
22
23#include "qgis.h"
24#include "qgis_core.h"
26#include "qgsvector.h"
27
28#include <QObject>
29#include <QPoint>
30#include <QString>
31#include <qglobal.h>
32
33using namespace Qt::StringLiterals;
34
35class QgsPoint;
36
61class CORE_EXPORT QgsPointXY
62{
63 Q_GADGET
64
65 Q_PROPERTY( double x READ x WRITE setX )
66 Q_PROPERTY( double y READ y WRITE setY )
67
68 public:
69
70 QgsPointXY() = default;
71
73
79 QgsPointXY( double x, double y ) SIP_HOLDGIL
80 : mX( x )
81 , mY( y )
82 , mIsEmpty( false )
83 {}
84
89 QgsPointXY( QPointF point ) SIP_HOLDGIL
90 : mX( point.x() )
91 , mY( point.y() )
92 , mIsEmpty( false )
93 {}
94
99 QgsPointXY( QPoint point ) SIP_HOLDGIL
100 : mX( point.x() )
101 , mY( point.y() )
102 , mIsEmpty( false )
103 {}
104
110 QgsPointXY( const QgsPoint &point ) SIP_HOLDGIL;
111
112 // IMPORTANT - while QgsPointXY is inherited by QgsReferencedPointXY, we do NOT want a virtual destructor here
113 // because this class MUST be lightweight and we don't want the cost of the vtable here.
114 // see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
115 ~QgsPointXY() = default;
116
121 void setX( double x ) SIP_HOLDGIL
122 {
123 mX = x;
124 mIsEmpty = false;
125 }
126
131 void setY( double y ) SIP_HOLDGIL
132 {
133 mY = y;
134 mIsEmpty = false;
135 }
136
138 void set( double x, double y ) SIP_HOLDGIL
139 {
140 mX = x;
141 mY = y;
142 mIsEmpty = false;
143 }
144
149 double x() const SIP_HOLDGIL
150 {
151 return mX;
152 }
153
158 double y() const SIP_HOLDGIL
159 {
160 return mY;
161 }
162
167 QPointF toQPointF() const
168 {
169 return QPointF( mX, mY );
170 }
171
176 QString toString( int precision = -1 ) const;
177
182 QString asWkt() const;
183
188 double sqrDist( double x, double y ) const SIP_HOLDGIL
189 {
190 return QgsGeometryUtilsBase::sqrDistance2D( mX, mY, x, y );
191 }
192
197 double sqrDist( const QgsPointXY &other ) const SIP_HOLDGIL
198 {
199 return QgsGeometryUtilsBase::sqrDistance2D( mX, mY, other.x(), other.y() );
200 }
201
208 double distance( double x, double y ) const SIP_HOLDGIL
209 {
210 return QgsGeometryUtilsBase::distance2D( mX, mY, x, y );
211 }
212
218 double distance( const QgsPointXY &other ) const SIP_HOLDGIL
219 {
220 return QgsGeometryUtilsBase::distance2D( mX, mY, other.x(), other.y() );
221 }
222
224 double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint SIP_OUT, double epsilon = Qgis::DEFAULT_SEGMENT_EPSILON ) const SIP_HOLDGIL;
225
227 double azimuth( const QgsPointXY &other ) const SIP_HOLDGIL;
228
235 QgsPointXY project( double distance, double bearing ) const SIP_HOLDGIL;
236
244 bool isEmpty() const SIP_HOLDGIL { return mIsEmpty; }
245
255 bool compare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
256 {
257 return QgsGeometryUtilsBase::fuzzyEqual( epsilon, mX, mY, other.x(), other.y() );
258 }
259
270 bool distanceCompare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
271 {
272 return QgsGeometryUtilsBase::fuzzyDistanceEqual( epsilon, mX, mY, other.x(), other.y() );
273 }
274
275 bool operator==( const QgsPointXY &other ) const 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
287 bool operator!=( const QgsPointXY &other ) const SIP_HOLDGIL
288 {
289 if ( isEmpty() && other.isEmpty() )
290 return false;
291 if ( isEmpty() && !other.isEmpty() )
292 return true;
293 if ( ! isEmpty() && other.isEmpty() )
294 return true;
295
296 return !QgsGeometryUtilsBase::fuzzyEqual( 1E-8, mX, mY, other.x(), other.y() );
297 }
298
300 void multiply( double scalar ) SIP_HOLDGIL
301 {
302 mX *= scalar;
303 mY *= scalar;
304 }
305
307 {
308 if ( &other != this )
309 {
310 mX = other.x();
311 mY = other.y();
312 mIsEmpty = other.isEmpty();
313 }
314
315 return *this;
316 }
317
319 QgsVector operator-( const QgsPointXY &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
320
322 QgsPointXY &operator+=( QgsVector v ) { *this = *this + v; return *this; }
323
325 QgsPointXY &operator-=( QgsVector v ) { *this = *this - v; return *this; }
326
328 QgsPointXY operator+( QgsVector v ) const { return QgsPointXY( mX + v.x(), mY + v.y() ); }
329
331 QgsPointXY operator-( QgsVector v ) const { return QgsPointXY( mX - v.x(), mY - v.y() ); }
332
334 QgsPointXY operator*( double scalar ) const { return QgsPointXY( mX * scalar, mY * scalar ); }
335
337 QgsPointXY operator/( double scalar ) const { return QgsPointXY( mX / scalar, mY / scalar ); }
338
340 QgsPointXY &operator*=( double scalar ) { mX *= scalar; mY *= scalar; return *this; }
341
343 QgsPointXY &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }
344
346 operator QVariant() const
347 {
348 return QVariant::fromValue( *this );
349 }
350
351#ifdef SIP_RUN
352 SIP_PYOBJECT __repr__();
353 % MethodCode
354 QString str = u"<QgsPointXY: %1>"_s.arg( sipCpp->asWkt() );
355 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
356 % End
357
358 int __len__();
359 % MethodCode
360 sipRes = 2;
361 % End
362
363
364 SIP_PYOBJECT __getitem__( int );
365 % MethodCode
366 if ( a0 == 0 )
367 {
368 sipRes = Py_BuildValue( "d", sipCpp->x() );
369 }
370 else if ( a0 == 1 )
371 {
372 sipRes = Py_BuildValue( "d", sipCpp->y() );
373 }
374 else
375 {
376 QString msg = QString( "Bad index: %1" ).arg( a0 );
377 PyErr_SetString( PyExc_IndexError, msg.toLatin1().constData() );
378 }
379 % End
380
381 long __hash__() const;
382 % MethodCode
383 sipRes = qHash( *sipCpp );
384 % End
385#endif
386
387 private:
388
390 double mX = 0; //std::numeric_limits<double>::quiet_NaN();
391
393 double mY = 0; //std::numeric_limits<double>::quiet_NaN();
394
396 bool mIsEmpty = true;
397
398 friend uint qHash( const QgsPointXY &pnt );
399
400}; // class QgsPointXY
401
403
404inline std::ostream &operator << ( std::ostream &os, const QgsPointXY &p ) SIP_SKIP
405{
406 // Use Local8Bit for printouts
407 os << p.toString().toLocal8Bit().data();
408 return os;
409}
410
411inline uint qHash( const QgsPointXY &p ) SIP_SKIP
412{
413 uint hash;
414 const uint h1 = qHash( static_cast< quint64 >( p.mX ) );
415 const uint h2 = qHash( static_cast< quint64 >( p.mY ) );
416 hash = h1 ^ ( h2 << 1 );
417 return hash;
418}
419
420
421#endif //QGSPOINTXY_H
static const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition qgis.h:6523
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.
Represents a 2D point.
Definition qgspointxy.h:62
double x() const
Gets the x value of the point.
Definition qgspointxy.h:149
QgsPointXY(QPoint point)
Create a point from a QPoint.
Definition qgspointxy.h:99
QgsPointXY operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition qgspointxy.h:334
QgsPointXY & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition qgspointxy.h:325
QgsPointXY(QPointF point)
Create a point from a QPointF.
Definition qgspointxy.h:89
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition qgspointxy.h:188
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition qgspointxy.h:208
QgsPointXY operator-(QgsVector v) const
Subtracts a vector from this point.
Definition qgspointxy.h:331
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:131
double distance(const QgsPointXY &other) const
Returns the distance between this point and another point.
Definition qgspointxy.h:218
void multiply(double scalar)
Multiply x and y by the given value.
Definition qgspointxy.h:300
QgsPointXY()=default
QgsPointXY & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition qgspointxy.h:340
QgsPointXY operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition qgspointxy.h:337
QgsPointXY & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition qgspointxy.h:343
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:255
bool operator==(const QgsPointXY &other) const
Definition qgspointxy.h:275
bool operator!=(const QgsPointXY &other) const
Definition qgspointxy.h:287
void set(double x, double y)
Sets the x and y value of the point.
Definition qgspointxy.h:138
QgsPointXY & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition qgspointxy.h:322
double y
Definition qgspointxy.h:66
QgsPointXY & operator=(const QgsPointXY &other)
Definition qgspointxy.h:306
QgsVector operator-(const QgsPointXY &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition qgspointxy.h:319
double y() const
Gets the y value of the point.
Definition qgspointxy.h:158
double x
Definition qgspointxy.h:65
double sqrDist(const QgsPointXY &other) const
Returns the squared distance between this point another point.
Definition qgspointxy.h:197
void setX(double x)
Sets the x value of the point.
Definition qgspointxy.h:121
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:270
~QgsPointXY()=default
bool isEmpty() const
Returns true if the geometry is empty.
Definition qgspointxy.h:244
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:167
QgsPointXY operator+(QgsVector v) const
Adds a vector to this point.
Definition qgspointxy.h:328
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
Represent a 2-dimensional vector.
Definition qgsvector.h:34
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:611
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_HOLDGIL
Definition qgis_sip.h:179
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
uint qHash(const QgsPointXY &p)
Definition qgspointxy.h:411