Skip to content

Commit

Permalink
Optimise two heavily used methods in QgsLineString
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 11, 2021
1 parent 4617852 commit 43d5b0c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/core/geometry/qgslinestring.cpp
Expand Up @@ -375,6 +375,18 @@ bool QgsLineString::removeDuplicateNodes( double epsilon, bool useZValues )
return result; return result;
} }


bool QgsLineString::isClosed() const
{
if ( mX.empty() )
return false;

bool closed = qgsDoubleNear( mX.first(), mX.last() ) &&
qgsDoubleNear( mY.first(), mY.last() );
if ( is3D() && closed )
closed &= qgsDoubleNear( mZ.first(), mZ.last() ) || ( std::isnan( mZ.first() ) && std::isnan( mZ.last() ) );
return closed;
}

QVector< QgsVertexId > QgsLineString::collectDuplicateNodes( double epsilon, bool useZValues ) const QVector< QgsVertexId > QgsLineString::collectDuplicateNodes( double epsilon, bool useZValues ) const
{ {
QVector< QgsVertexId > res; QVector< QgsVertexId > res;
Expand Down Expand Up @@ -448,23 +460,15 @@ bool QgsLineString::fromWkb( QgsConstWkbPtr &wkbPtr )


QgsRectangle QgsLineString::calculateBoundingBox() const QgsRectangle QgsLineString::calculateBoundingBox() const
{ {
double xmin = std::numeric_limits<double>::max(); if ( mX.empty() )
double ymin = std::numeric_limits<double>::max(); return QgsRectangle();
double xmax = -std::numeric_limits<double>::max();
double ymax = -std::numeric_limits<double>::max();


const int nb = mX.size(); auto result = std::minmax_element( mX.begin(), mX.end() );
const double *x = mX.constData(); const double xmin = *result.first;
const double *y = mY.constData(); const double xmax = *result.second;
for ( int i = 0; i < nb; ++i ) result = std::minmax_element( mY.begin(), mY.end() );
{ const double ymin = *result.first;
const double px = *x++; const double ymax = *result.second;
xmin = std::min( xmin, px );
xmax = std::max( xmax, px );
const double py = *y++;
ymin = std::min( ymin, py );
ymax = std::max( ymax, py );
}
return QgsRectangle( xmin, ymin, xmax, ymax, false ); return QgsRectangle( xmin, ymin, xmax, ymax, false );
} }


Expand Down
1 change: 1 addition & 0 deletions src/core/geometry/qgslinestring.h
Expand Up @@ -590,6 +590,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
bool isValid( QString &error SIP_OUT, int flags = 0 ) const override; bool isValid( QString &error SIP_OUT, int flags = 0 ) const override;
QgsLineString *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const override SIP_FACTORY; QgsLineString *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const override SIP_FACTORY;
bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override; bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
bool isClosed() const override SIP_HOLDGIL;


/** /**
* Returns a list of any duplicate nodes contained in the geometry, within the specified tolerance. * Returns a list of any duplicate nodes contained in the geometry, within the specified tolerance.
Expand Down

0 comments on commit 43d5b0c

Please sign in to comment.