QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 QgsPointXY() = default;
70
71 // clang-format off
73 // clang-format on
74
80 QgsPointXY( double x, double y ) SIP_HOLDGIL
81 : mX( x )
82 , mY( y )
83 , mIsEmpty( false )
84 {}
85
90 QgsPointXY( QPointF point ) SIP_HOLDGIL
91 : mX( point.x() )
92 , mY( point.y() )
93 , mIsEmpty( false )
94 {}
95
100 QgsPointXY( QPoint point ) SIP_HOLDGIL
101 : mX( point.x() )
102 , mY( point.y() )
103 , mIsEmpty( false )
104 {}
105
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
168 QPointF toQPointF() const
169 {
170 return QPointF( mX, mY );
171 }
172
177 QString toString( int precision = -1 ) const;
178
183 QString asWkt() const;
184
189 double sqrDist( double x, double y ) const SIP_HOLDGIL
190 {
191 return QgsGeometryUtilsBase::sqrDistance2D( mX, mY, x, y );
192 }
193
198 double sqrDist( const QgsPointXY &other ) const SIP_HOLDGIL
199 {
200 return QgsGeometryUtilsBase::sqrDistance2D( mX, mY, other.x(), other.y() );
201 }
202
209 double distance( double x, double y ) const SIP_HOLDGIL
210 {
211 return QgsGeometryUtilsBase::distance2D( mX, mY, x, y );
212 }
213
219 double distance( const QgsPointXY &other ) const SIP_HOLDGIL
220 {
221 return QgsGeometryUtilsBase::distance2D( mX, mY, other.x(), other.y() );
222 }
223
225 double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint SIP_OUT, double epsilon = Qgis::DEFAULT_SEGMENT_EPSILON ) const SIP_HOLDGIL;
226
228 double azimuth( const QgsPointXY &other ) const SIP_HOLDGIL;
229
236 QgsPointXY project( double distance, double bearing ) const SIP_HOLDGIL;
237
245 bool isEmpty() const SIP_HOLDGIL { return mIsEmpty; }
246
256 bool compare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
257 {
258 return QgsGeometryUtilsBase::fuzzyEqual( epsilon, mX, mY, other.x(), other.y() );
259 }
260
271 bool distanceCompare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
272 {
273 return QgsGeometryUtilsBase::fuzzyDistanceEqual( epsilon, mX, mY, other.x(), other.y() );
274 }
275
276 bool operator==( const QgsPointXY &other ) const SIP_HOLDGIL
277 {
278 if ( isEmpty() && other.isEmpty() )
279 return true;
280 if ( isEmpty() && !other.isEmpty() )
281 return false;
282 if ( ! isEmpty() && other.isEmpty() )
283 return false;
284
285 return QgsGeometryUtilsBase::fuzzyEqual( 1E-8, mX, mY, other.x(), other.y() );
286 }
287
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
308 {
309 if ( &other != this )
310 {
311 mX = other.x();
312 mY = other.y();
313 mIsEmpty = other.isEmpty();
314 }
315
316 return *this;
317 }
318
320 QgsVector operator-( const QgsPointXY &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
321
323 QgsPointXY &operator+=( QgsVector v ) { *this = *this + v; return *this; }
324
326 QgsPointXY &operator-=( QgsVector v ) { *this = *this - v; return *this; }
327
329 QgsPointXY operator+( QgsVector v ) const { return QgsPointXY( mX + v.x(), mY + v.y() ); }
330
332 QgsPointXY operator-( QgsVector v ) const { return QgsPointXY( mX - v.x(), mY - v.y() ); }
333
335 QgsPointXY operator*( double scalar ) const { return QgsPointXY( mX * scalar, mY * scalar ); }
336
338 QgsPointXY operator/( double scalar ) const { return QgsPointXY( mX / scalar, mY / scalar ); }
339
341 QgsPointXY &operator*=( double scalar ) { mX *= scalar; mY *= scalar; return *this; }
342
344 QgsPointXY &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }
345
347 operator QVariant() const
348 {
349 return QVariant::fromValue( *this );
350 }
351
352#ifdef SIP_RUN
353// clang-format off
354 SIP_PYOBJECT __repr__();
355 % MethodCode
356 QString str = u"<QgsPointXY: %1>"_s.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// clang-format on
388#endif
389
390 private:
391
393 double mX = 0; //std::numeric_limits<double>::quiet_NaN();
394
396 double mY = 0; //std::numeric_limits<double>::quiet_NaN();
397
399 bool mIsEmpty = true;
400
401 friend uint qHash( const QgsPointXY &pnt );
402
403}; // class QgsPointXY
404
406
407inline std::ostream &operator << ( std::ostream &os, const QgsPointXY &p ) SIP_SKIP
408{
409 // Use Local8Bit for printouts
410 os << p.toString().toLocal8Bit().data();
411 return os;
412}
413
414inline uint qHash( const QgsPointXY &p ) SIP_SKIP
415{
416 uint hash;
417 const uint h1 = qHash( static_cast< quint64 >( p.mX ) );
418 const uint h2 = qHash( static_cast< quint64 >( p.mY ) );
419 hash = h1 ^ ( h2 << 1 );
420 return hash;
421}
422
423
424#endif //QGSPOINTXY_H
static const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition qgis.h:6615
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:150
QgsPointXY(QPoint point)
Create a point from a QPoint.
Definition qgspointxy.h:100
QgsPointXY operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition qgspointxy.h:335
QgsPointXY & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition qgspointxy.h:326
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:189
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition qgspointxy.h:209
QgsPointXY operator-(QgsVector v) const
Subtracts a vector from this point.
Definition qgspointxy.h:332
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:132
double distance(const QgsPointXY &other) const
Returns the distance between this point and another point.
Definition qgspointxy.h:219
void multiply(double scalar)
Multiply x and y by the given value.
Definition qgspointxy.h:301
QgsPointXY()=default
QgsPointXY & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition qgspointxy.h:341
QgsPointXY operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition qgspointxy.h:338
QgsPointXY & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition qgspointxy.h:344
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:256
bool operator==(const QgsPointXY &other) const
Definition qgspointxy.h:276
bool operator!=(const QgsPointXY &other) const
Definition qgspointxy.h:288
void set(double x, double y)
Sets the x and y value of the point.
Definition qgspointxy.h:139
QgsPointXY & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition qgspointxy.h:323
double y
Definition qgspointxy.h:66
QgsPointXY & operator=(const QgsPointXY &other)
Definition qgspointxy.h:307
QgsVector operator-(const QgsPointXY &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition qgspointxy.h:320
double y() const
Gets the y value of the point.
Definition qgspointxy.h:159
double x
Definition qgspointxy.h:65
double sqrDist(const QgsPointXY &other) const
Returns the squared distance between this point another point.
Definition qgspointxy.h:198
void setX(double x)
Sets the x value of the point.
Definition qgspointxy.h:122
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:271
~QgsPointXY()=default
bool isEmpty() const
Returns true if the geometry is empty.
Definition qgspointxy.h:245
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:168
QgsPointXY operator+(QgsVector v) const
Adds a vector to this point.
Definition qgspointxy.h:329
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:155
double x() const
Returns the vector's x-component.
Definition qgsvector.h:146
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition qgis.cpp:611
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_HOLDGIL
Definition qgis_sip.h:178
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
uint qHash(const QgsPointXY &p)
Definition qgspointxy.h:414