@@ -60,8 +60,8 @@ QgsPoint::QgsPoint( const QgsPointXY &p )
6060 : QgsAbstractGeometry()
6161 , mX( p.x() )
6262 , mY( p.y() )
63- , mZ( 0.0 )
64- , mM( 0.0 )
63+ , mZ( std::numeric_limits< double >::quiet_NaN() )
64+ , mM( std::numeric_limits< double >::quiet_NaN() )
6565{
6666 mWkbType = QgsWkbTypes::Point;
6767}
@@ -70,8 +70,8 @@ QgsPoint::QgsPoint( QPointF p )
7070 : QgsAbstractGeometry()
7171 , mX( p.x() )
7272 , mY( p.y() )
73- , mZ( 0.0 )
74- , mM( 0.0 )
73+ , mZ( std::numeric_limits< double >::quiet_NaN() )
74+ , mM( std::numeric_limits< double >::quiet_NaN() )
7575{
7676 mWkbType = QgsWkbTypes::Point;
7777}
@@ -272,7 +272,17 @@ void QgsPoint::draw( QPainter &p ) const
272272
273273void QgsPoint::clear ()
274274{
275- mX = mY = mZ = mM = 0 .;
275+ mX = mY = 0 .;
276+ if ( is3D () )
277+ mZ = 0 .;
278+ else
279+ mZ = std::numeric_limits<double >::quiet_NaN ();
280+
281+ if ( isMeasure () )
282+ mM = 0 .;
283+ else
284+ mM = std::numeric_limits<double >::quiet_NaN ();
285+
276286 clearCache ();
277287}
278288
@@ -404,7 +414,7 @@ bool QgsPoint::dropZValue()
404414 return false ;
405415
406416 mWkbType = QgsWkbTypes::dropZ ( mWkbType );
407- mZ = 0.0 ;
417+ mZ = std::numeric_limits< double >:: quiet_NaN () ;
408418 clearCache ();
409419 return true ;
410420}
@@ -415,7 +425,7 @@ bool QgsPoint::dropMValue()
415425 return false ;
416426
417427 mWkbType = QgsWkbTypes::dropM ( mWkbType );
418- mM = 0.0 ;
428+ mM = std::numeric_limits< double >:: quiet_NaN () ;
419429 clearCache ();
420430 return true ;
421431}
@@ -430,17 +440,17 @@ bool QgsPoint::convertTo( QgsWkbTypes::Type type )
430440 switch ( type )
431441 {
432442 case QgsWkbTypes::Point:
433- mZ = 0.0 ;
434- mM = 0.0 ;
443+ mZ = std::numeric_limits< double >:: quiet_NaN () ;
444+ mM = std::numeric_limits< double >:: quiet_NaN () ;
435445 mWkbType = type;
436446 return true ;
437447 case QgsWkbTypes::PointZ:
438448 case QgsWkbTypes::Point25D:
439- mM = 0.0 ;
449+ mM = std::numeric_limits< double >:: quiet_NaN () ;
440450 mWkbType = type;
441451 return true ;
442452 case QgsWkbTypes::PointM:
443- mZ = 0.0 ;
453+ mZ = std::numeric_limits< double >:: quiet_NaN () ;
444454 mWkbType = type;
445455 return true ;
446456 case QgsWkbTypes::PointZM:
@@ -481,22 +491,38 @@ double QgsPoint::distanceSquared( const QgsPoint &other ) const
481491
482492double QgsPoint::distance3D ( double x, double y, double z ) const
483493{
484- return sqrt ( ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ) + ( mZ - z ) * ( mZ - z ) );
494+ double zDistSquared = 0.0 ;
495+ if ( is3D () || !qIsNaN ( z ) )
496+ zDistSquared = ( mZ - z ) * ( mZ - z );
497+
498+ return sqrt ( ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ) + zDistSquared );
485499}
486500
487501double QgsPoint::distance3D ( const QgsPoint &other ) const
488502{
489- return sqrt ( ( mX - other.x () ) * ( mX - other.x () ) + ( mY - other.y () ) * ( mY - other.y () ) + ( mZ - other.z () ) * ( mZ - other.z () ) );
503+ double zDistSquared = 0.0 ;
504+ if ( is3D () || other.is3D () )
505+ zDistSquared = ( mZ - other.z () ) * ( mZ - other.z () );
506+
507+ return sqrt ( ( mX - other.x () ) * ( mX - other.x () ) + ( mY - other.y () ) * ( mY - other.y () ) + zDistSquared );
490508}
491509
492510double QgsPoint::distanceSquared3D ( double x, double y, double z ) const
493511{
494- return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ) + ( mZ - z ) * ( mZ - z );
512+ double zDistSquared = 0.0 ;
513+ if ( is3D () || !qIsNaN ( z ) )
514+ zDistSquared = ( mZ - z ) * ( mZ - z );
515+
516+ return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ) + zDistSquared;
495517}
496518
497519double QgsPoint::distanceSquared3D ( const QgsPoint &other ) const
498520{
499- return ( mX - other.x () ) * ( mX - other.x () ) + ( mY - other.y () ) * ( mY - other.y () ) + ( mZ - other.z () ) * ( mZ - other.z () );
521+ double zDistSquared = 0.0 ;
522+ if ( is3D () || other.is3D () )
523+ zDistSquared = ( mZ - other.z () ) * ( mZ - other.z () );
524+
525+ return ( mX - other.x () ) * ( mX - other.x () ) + ( mY - other.y () ) * ( mY - other.y () ) + zDistSquared;
500526}
501527
502528double QgsPoint::azimuth ( const QgsPoint &other ) const
@@ -520,8 +546,6 @@ double QgsPoint::inclination( const QgsPoint &other ) const
520546
521547QgsPoint QgsPoint::project ( double distance, double azimuth, double inclination ) const
522548{
523- QgsWkbTypes::Type pType ( QgsWkbTypes::Point );
524-
525549 double radsXy = azimuth * M_PI / 180.0 ;
526550 double dx = 0.0 , dy = 0.0 , dz = 0.0 ;
527551
@@ -534,18 +558,11 @@ QgsPoint QgsPoint::project( double distance, double azimuth, double inclination
534558 }
535559 else
536560 {
537- pType = QgsWkbTypes::addZ ( pType );
538561 double radsZ = inclination * M_PI / 180.0 ;
539562 dx = distance * sin ( radsZ ) * sin ( radsXy );
540563 dy = distance * sin ( radsZ ) * cos ( radsXy );
541564 dz = distance * cos ( radsZ );
542565 }
543566
544- if ( isMeasure () )
545- {
546- pType = QgsWkbTypes::addM ( pType );
547- }
548-
549- double z = qIsNaN ( mZ ) ? 0 : mZ ;
550- return QgsPoint ( mX + dx, mY + dy, z + dz, mM , pType );
567+ return QgsPoint ( mX + dx, mY + dy, mZ + dz, mM , mWkbType );
551568}
0 commit comments