Skip to content
Permalink
Browse files

Refactor and optimise certain QgsWKBTypes functions

Given how frequently these functions are called throughout QGIS,
it makes sense for them to be written in a way that makes it easy
for the compiler to optimise them/inline them.
  • Loading branch information
nyalldawson committed Jan 24, 2016
1 parent ba530a0 commit ac033cfc9da9adec77ee73d8a2384f0b93cb362b
Showing with 637 additions and 229 deletions.
  1. +0 −213 src/core/geometry/qgswkbtypes.cpp
  2. +637 −16 src/core/geometry/qgswkbtypes.h
@@ -29,42 +29,6 @@ QMap<QgsWKBTypes::Type, QgsWKBTypes::wkbEntry>* QgsWKBTypes::entries()
return &entries;
}

QgsWKBTypes::Type QgsWKBTypes::singleType( Type type )
{
QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
if ( it == entries()->constEnd() || it.key() == Unknown )
{
return Unknown;
}
return ( it->mSingleType );
}

QgsWKBTypes::Type QgsWKBTypes::multiType( Type type )
{
QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
if ( it == entries()->constEnd() || it.key() == Unknown )
{
return Unknown;
}
return it->mMultiType;
}

QgsWKBTypes::Type QgsWKBTypes::flatType( Type type )
{
QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
if ( it == entries()->constEnd() || it.key() == Unknown )
{
return Unknown;
}
return it->mFlatType;
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests.
* See details in QEP #17
****************************************************************************/

QgsWKBTypes::Type QgsWKBTypes::parseType( const QString &wktStr )
{
QString typestr = wktStr.left( wktStr.indexOf( '(' ) ).simplified().remove( ' ' );
@@ -79,82 +43,6 @@ QgsWKBTypes::Type QgsWKBTypes::parseType( const QString &wktStr )
return Unknown;
}

bool QgsWKBTypes::isSingleType( Type type )
{
return ( type != Unknown && !isMultiType( type ) );
}

bool QgsWKBTypes::isMultiType( Type type )
{
QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
if ( it == entries()->constEnd() )
{
return Unknown;
}
return it->mIsMultiType;
}

bool QgsWKBTypes::isCurvedType( QgsWKBTypes::Type type )
{
switch ( flatType( type ) )
{
case CircularString:
case CompoundCurve:
case CurvePolygon:
case MultiCurve:
case MultiSurface:
return true;

default:
return false;
}

return false;
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests.
* See details in QEP #17
****************************************************************************/

int QgsWKBTypes::wkbDimensions( Type type )
{
GeometryType gtype = geometryType( type );
switch ( gtype )
{
case LineGeometry:
return 1;
case PolygonGeometry:
return 2;
default: //point, no geometry, unknown geometry
return 0;
}
}

int QgsWKBTypes::coordDimensions( QgsWKBTypes::Type type )
{
if ( type == Unknown || type == NoGeometry )
return 0;

QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
if ( it == entries()->constEnd() )
{
return 0;
}
return 2 + it->mHasZ + it->mHasM;
}

QgsWKBTypes::GeometryType QgsWKBTypes::geometryType( Type type )
{
QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
if ( it == entries()->constEnd() )
{
return UnknownGeometry;
}
return it->mGeometryType;
}

QString QgsWKBTypes::displayString( Type type )
{
QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
@@ -165,107 +53,6 @@ QString QgsWKBTypes::displayString( Type type )
return it->mName;
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests.
* See details in QEP #17
****************************************************************************/

bool QgsWKBTypes::hasZ( Type type )
{
QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
if ( it == entries()->constEnd() )
{
return false;
}
return it->mHasZ;
}

bool QgsWKBTypes::hasM( Type type )
{
QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
if ( it == entries()->constEnd() )
{
return false;
}
return it->mHasM;
}

QgsWKBTypes::Type QgsWKBTypes::addZ( QgsWKBTypes::Type type )
{
if ( hasZ( type ) )
return type;
else if ( type == Unknown )
return Unknown;
else if ( type == NoGeometry )
return NoGeometry;

//upgrade with z dimension
Type flat = flatType( type );
if ( hasM( type ) )
return static_cast< QgsWKBTypes::Type >( flat + 3000 );
else
return static_cast< QgsWKBTypes::Type >( flat + 1000 );
}

QgsWKBTypes::Type QgsWKBTypes::addM( QgsWKBTypes::Type type )
{
if ( hasM( type ) )
return type;
else if ( type == Unknown )
return Unknown;
else if ( type == NoGeometry )
return NoGeometry;
else if ( type == Point25D ||
type == LineString25D ||
type == Polygon25D ||
type == MultiPoint25D ||
type == MultiLineString25D ||
type == MultiPolygon25D )
return type; //can't add M dimension to these types

//upgrade with m dimension
Type flat = flatType( type );
if ( hasZ( type ) )
return static_cast< QgsWKBTypes::Type >( flat + 3000 );
else
return static_cast< QgsWKBTypes::Type >( flat + 2000 );
}

QgsWKBTypes::Type QgsWKBTypes::dropZ( QgsWKBTypes::Type type )
{
if ( !hasZ( type ) )
return type;

QgsWKBTypes::Type returnType = flatType( type );
if ( hasM( type ) )
returnType = addM( returnType );
return returnType;
}

QgsWKBTypes::Type QgsWKBTypes::dropM( QgsWKBTypes::Type type )
{
if ( !hasM( type ) )
return type;

QgsWKBTypes::Type returnType = flatType( type );
if ( hasZ( type ) )
returnType = addZ( returnType );
return returnType;
}

QgsWKBTypes::Type QgsWKBTypes::to25D( QgsWKBTypes::Type type )
{
QgsWKBTypes::Type flat = flatType( type );

if ( flat >= Point && flat <= MultiPolygon )
return static_cast< QgsWKBTypes::Type >( flat + 0x80000000 );
else if ( type == QgsWKBTypes::NoGeometry )
return QgsWKBTypes::NoGeometry;
else
return Unknown;
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests.

0 comments on commit ac033cf

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