Skip to content
Permalink
Browse files

Add method to determine orientation of closed curves

(cherry picked from commit 53db15e)
  • Loading branch information
nyalldawson committed Dec 2, 2018
1 parent 1abbcb4 commit 73cf308c128ecb6b82ed52d27f53f296e3674029
@@ -229,6 +229,23 @@ If a curve isClosed(), it has infinite sinuosity and will return NaN.
.. versionadded:: 3.2
%End

enum Orientation
{
Clockwise,
CounterClockwise,
};

Orientation orientation() const;
%Docstring
Returns the curve's orientation, e.g. clockwise or counter-clockwise.

.. warning::

The result is not predictable for non-closed curves.

.. versionadded:: 3.6
%End



protected:
@@ -214,6 +214,13 @@ double QgsCurve::sinuosity() const
return length() / d;
}

QgsCurve::Orientation QgsCurve::orientation() const
{
double a = 0;
sumUpArea( a );
return a < 0 ? Clockwise : CounterClockwise;
}

void QgsCurve::clearCache() const
{
mBoundingBox = QgsRectangle();
@@ -213,6 +213,22 @@ class CORE_EXPORT QgsCurve: public QgsAbstractGeometry
*/
double sinuosity() const;

//! Curve orientation
enum Orientation
{
Clockwise, //!< Clockwise orientation
CounterClockwise, //!< Counter-clockwise orientation
};

/**
* Returns the curve's orientation, e.g. clockwise or counter-clockwise.
*
* \warning The result is not predictable for non-closed curves.
*
* \since QGIS 3.6
*/
Orientation orientation() const;

#ifndef SIP_RUN

/**
@@ -2788,6 +2788,13 @@ void TestQgsGeometry::circularString()
interpolateResult.reset( interpolate.interpolatePoint( 1 ) );
QCOMPARE( interpolateResult->asWkt( 2 ), QStringLiteral( "Point (10.46 0.84)" ) );

// orientation
QgsCircularString orientation;
( void )orientation.orientation(); // no crash
orientation.setPoints( QgsPointSequence() << QgsPoint( 0, 0 ) << QgsPoint( 0, 1 ) << QgsPoint( 1, 1 ) << QgsPoint( 1, 0 ) << QgsPoint( 0, 0 ) );
QCOMPARE( orientation.orientation(), QgsCurve::Clockwise );
orientation.setPoints( QgsPointSequence() << QgsPoint( 0, 0 ) << QgsPoint( 1, 0 ) << QgsPoint( 1, 1 ) << QgsPoint( 0, 1 ) << QgsPoint( 0, 0 ) );
QCOMPARE( orientation.orientation(), QgsCurve::CounterClockwise );
}


@@ -4795,6 +4802,14 @@ void TestQgsGeometry::lineString()
interpolate.setPoints( QgsPointSequence() << QgsPoint( 11, 2 ) << QgsPoint( 11, 12 ) << QgsPoint( 111, 12 ) );
interpolateResult.reset( interpolate.interpolatePoint( 1 ) );
QCOMPARE( interpolateResult->asWkt( 2 ), QStringLiteral( "Point (11 3)" ) );

// orientation
QgsLineString orientation;
( void )orientation.orientation(); // no crash
orientation.setPoints( QgsPointSequence() << QgsPoint( 0, 0 ) << QgsPoint( 0, 1 ) << QgsPoint( 1, 1 ) << QgsPoint( 1, 0 ) << QgsPoint( 0, 0 ) );
QCOMPARE( orientation.orientation(), QgsCurve::Clockwise );
orientation.setPoints( QgsPointSequence() << QgsPoint( 0, 0 ) << QgsPoint( 1, 0 ) << QgsPoint( 1, 1 ) << QgsPoint( 0, 1 ) << QgsPoint( 0, 0 ) );
QCOMPARE( orientation.orientation(), QgsCurve::CounterClockwise );
}

void TestQgsGeometry::polygon()
interpolateResult.reset( interpolate.interpolatePoint( 1 ) );
QCOMPARE( interpolateResult->asWkt( 2 ), QStringLiteral( "Point (6 0)" ) );

// orientation
QgsCompoundCurve orientation;
( void )orientation.orientation(); // no crash
orientation.fromWkt( QStringLiteral( "CompoundCurve( ( 0 0, 0 1), CircularString (0 1, 1 1, 1 0), (1 0, 0 0))" ) );
QCOMPARE( orientation.orientation(), QgsCurve::Clockwise );
orientation.fromWkt( QStringLiteral( "CompoundCurve( ( 0 0, 1 0), CircularString (1 0, 1 1, 0 1), (0 1, 0 0))" ) );
QCOMPARE( orientation.orientation(), QgsCurve::CounterClockwise );
}

void TestQgsGeometry::multiPoint()

0 comments on commit 73cf308

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