Skip to content
Permalink
Browse files

Move midpoint to QgsGeometryUtils

  • Loading branch information
lbartoletti authored and nyalldawson committed Feb 14, 2017
1 parent e851b68 commit 3703bf7396ec955eb4a5b1e74cac023992f89429
@@ -65,4 +65,6 @@ class QgsGeometryUtils

static double averageAngle( double a1, double a2 );

static QgsPointV2 midpoint (const QgsPointV2& pt1, const QgsPointV2& pt2);

};
@@ -243,27 +243,6 @@ class QgsPointV2: public QgsAbstractGeometry
*/
QgsPointV2 project( double distance, double azimuth, double inclination = 90.0 ) const;

/** Returns a middle point between this point and other one.
* Z value is computed if one of this point have Z.
* M value is computed if one of this point have M.
* @param other other point.
* @return New point at middle between this and other one.
* Example:
* \code{.py}
* p = QgsPointV2( 4, 6 ) # 2D point
* pr = p.midpoint ( QgsPointV2( 2, 2 ) )
* # pr is a 2D point: 'Point (3 4)'
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointZ, 2, 2, 2 ) )
* # pr is a 3D point: 'PointZ (3 4 1)'
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointM, 2, 2, 0, 2 ) )
* # pr is a 3D point: 'PointM (3 4 1)'
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointZM, 2, 2, 2, 2 ) )
* # pr is a 4D point: 'PointZM (3 4 1 1)'
* \endcode
* @note added in QGIS 3.0
*/
QgsPointV2 midpoint (const QgsPointV2& other) const;

/**
* Calculates the vector obtained by subtracting a point from this point.
* @note added in QGIS 3.0
@@ -819,6 +819,31 @@ QStringList QgsGeometryUtils::wktGetChildBlocks( const QString &wkt, const QStri
return blocks;
}

QgsPointV2 QgsGeometryUtils::midpoint(const QgsPointV2 &pt1, const QgsPointV2 &pt2)
{
QgsWkbTypes::Type pType( QgsWkbTypes::Point );


double x = ( pt1.x() + pt2.x() ) / 2.0;
double y = ( pt1.y() + pt2.y() ) / 2.0;
double z = 0.0;
double m = 0.0;

if ( pt1.is3D() || pt2.is3D() )
{
pType = QgsWkbTypes::addZ( pType );
z = ( pt1.z() + pt2.z()) / 2.0;
}

if ( pt1.isMeasure() || pt2.isMeasure() )
{
pType = QgsWkbTypes::addM( pType );
m = ( pt1.m() + pt2.m()) / 2.0;
}

return QgsPointV2( pType, x, y, z, m );
}

double QgsGeometryUtils::lineAngle( double x1, double y1, double x2, double y2 )
{
double at = atan2( y2 - y1, x2 - x1 );
@@ -269,6 +269,28 @@ class CORE_EXPORT QgsGeometryUtils
*/
static QStringList wktGetChildBlocks( const QString& wkt , const QString &defaultType = "" );

/** Returns a middle point between points pt1 and pt2.
* Z value is computed if one of this point have Z.
* M value is computed if one of this point have M.
* @param pt1 first point.
* @param pt2 second point.
* @return New point at middle between points pt1 and pt2.
* * Example:
* \code{.py}
* p = QgsPointV2( 4, 6 ) # 2D point
* pr = midpoint ( p, QgsPointV2( 2, 2 ) )
* # pr is a 2D point: 'Point (3 4)'
* pr = midpoint ( p, QgsPointV2( QgsWkbTypes.PointZ, 2, 2, 2 ) )
* # pr is a 3D point: 'PointZ (3 4 1)'
* pr = midpoint ( p, QgsPointV2( QgsWkbTypes.PointM, 2, 2, 0, 2 ) )
* # pr is a 3D point: 'PointM (3 4 1)'
* pr = midpoint ( p, QgsPointV2( QgsWkbTypes.PointZM, 2, 2, 2, 2 ) )
* # pr is a 3D point: 'PointZM (3 4 1 1)'
* \endcode
* @note added in QGIS 3.0
*/
static QgsPointV2 midpoint( const QgsPointV2& pt1, const QgsPointV2& pt2 );

//! @note not available in Python bindings
enum ComponentType
{
@@ -514,28 +514,3 @@ QgsPointV2 QgsPointV2::project( double distance, double azimuth, double inclinat

return QgsPointV2( pType, mX + dx, mY + dy, mZ + dz, mM );
}

QgsPointV2 QgsPointV2::midpoint (const QgsPointV2& other) const
{
QgsWkbTypes::Type pType( QgsWkbTypes::Point );


double x = ( mX + other.x() ) / 2.0;
double y = ( mY + other.y() ) / 2.0;
double z = 0.0;
double m = 0.0;

if ( is3D() || other.is3D() )
{
pType = QgsWkbTypes::addZ( pType );
z = ( mZ + other.z()) / 2.0;
}

if ( isMeasure() || other.isMeasure() )
{
pType = QgsWkbTypes::addM( pType );
m = ( mM + other.m()) / 2.0;
}

return QgsPointV2( pType, x, y, z, m );
}
@@ -257,27 +257,6 @@ class CORE_EXPORT QgsPointV2: public QgsAbstractGeometry
*/
QgsPointV2 project( double distance, double azimuth, double inclination = 90.0 ) const;

/** Returns a middle point between this point and other one.
* Z value is computed if one of this point have Z.
* M value is computed if one of this point have M.
* @param other other point.
* @return New point at middle between this and other one.
* Example:
* \code{.py}
* p = QgsPointV2( 4, 6 ) # 2D point
* pr = p.midpoint ( QgsPointV2( 2, 2 ) )
* # pr is a 2D point: 'Point (3 4)'
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointZ, 2, 2, 2 ) )
* # pr is a 3D point: 'PointZ (3 4 1)'
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointM, 2, 2, 0, 2 ) )
* # pr is a 3D point: 'PointM (3 4 1)'
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointZM, 2, 2, 2, 2 ) )
* # pr is a 4D point: 'PointZM (3 4 1 1)'
* \endcode
* @note added in QGIS 3.0
*/
QgsPointV2 midpoint (const QgsPointV2& other) const;

/**
* Calculates the vector obtained by subtracting a point from this point.
* @note added in QGIS 3.0
@@ -910,13 +910,6 @@ void TestQgsGeometry::point()
QCOMPARE( p34.project( 5, 450 ), QgsPointV2( QgsWkbTypes::PointZM, 6, 2, 2, 5 ) );
QCOMPARE( p34.project( 5, 450, 450 ), QgsPointV2( QgsWkbTypes::PointZM, 6, 2, 2, 5 ) );

// midpoint
QgsPointV2 p35 = QgsPointV2( 4, 6 );
QCOMPARE( p35.midpoint( QgsPointV2( 2, 2 ) ), QgsPointV2( 3, 4 ) );
QCOMPARE( p35.midpoint( QgsPointV2( QgsWkbTypes::PointZ, 2, 2, 2 ) ), QgsPointV2( QgsWkbTypes::PointZ, 3, 4, 1 ) );
QCOMPARE( p35.midpoint( QgsPointV2( QgsWkbTypes::PointM, 2, 2, 0, 2 ) ), QgsPointV2( QgsWkbTypes::PointM, 3, 4, 0, 1 ) );
QCOMPARE( p35.midpoint( QgsPointV2( QgsWkbTypes::PointZM, 2, 2, 2, 2 ) ), QgsPointV2( QgsWkbTypes::PointZM, 3, 4, 1, 1 ) );

}

void TestQgsGeometry::lineString()
@@ -51,6 +51,7 @@ class TestQgsGeometryUtils: public QObject
void testCircleCenterRadius();
void testSqrDistToLine();
void testAngleThreePoints();
void testMidPoint();
};


@@ -539,7 +540,14 @@ void TestQgsGeometryUtils::testAngleThreePoints()
( void )QgsGeometryUtils::angleBetweenThreePoints( p1.x(), p1.y(), p2.x(), p2.y(), p3.x(), p3.y() );
}


void TestQgsGeometryUtils::testMidPoint()
{
QgsPointV2 p1( 4, 6 );
QCOMPARE( QgsGeometryUtils::midpoint( p1, QgsPointV2( 2, 2 ) ), QgsPointV2( 3, 4 ) );
QCOMPARE( QgsGeometryUtils::midpoint( p1, QgsPointV2( QgsWkbTypes::PointZ, 2, 2, 2 ) ), QgsPointV2( QgsWkbTypes::PointZ, 3, 4, 1 ) );
QCOMPARE( QgsGeometryUtils::midpoint( p1, QgsPointV2( QgsWkbTypes::PointM, 2, 2, 0, 2 ) ), QgsPointV2( QgsWkbTypes::PointM, 3, 4, 0, 1 ) );
QCOMPARE( QgsGeometryUtils::midpoint( p1, QgsPointV2( QgsWkbTypes::PointZM, 2, 2, 2, 2 ) ), QgsPointV2( QgsWkbTypes::PointZM, 3, 4, 1, 1 ) );
}

QGSTEST_MAIN( TestQgsGeometryUtils )
#include "testqgsgeometryutils.moc"

0 comments on commit 3703bf7

Please sign in to comment.
You can’t perform that action at this time.