Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve performance of vector reprojection by transforming the coordi…
…nates of a feature together
  • Loading branch information
mhugent committed Feb 7, 2013
1 parent bb0b978 commit 863da7d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
36 changes: 36 additions & 0 deletions src/core/qgscoordinatetransform.cpp
Expand Up @@ -23,6 +23,7 @@
#include <QDomNode>
#include <QDomElement>
#include <QApplication>
#include <QPolygonF>
#include <QVector>

extern "C"
Expand Down Expand Up @@ -318,6 +319,41 @@ void QgsCoordinateTransform::transformInPlace( double& x, double& y, double& z,
}
}

void QgsCoordinateTransform::transformPolygon( QPolygonF& poly, TransformDirection direction ) const
{
//create x, y arrays
int nVertices = poly.size();
qreal x[nVertices];
qreal y[nVertices];
qreal z[nVertices];

for ( int i = 0; i < nVertices; ++i )
{
const QPointF& pt = poly.at( i );
x[i] = pt.x();
y[i] = pt.y();
z[i] = 0;
}

try
{
transformCoords( nVertices, x, y, z, direction );
}
catch ( QgsCsException &cse )
{
// rethrow the exception
QgsDebugMsg( "rethrowing exception" );
throw cse;
}

for ( int i = 0; i < nVertices; ++i )
{
QPointF& pt = poly[i];
pt.rx() = x[i];
pt.ry() = y[i];
}
}

void QgsCoordinateTransform::transformInPlace(
QVector<double>& x, QVector<double>& y, QVector<double>& z,
TransformDirection direction ) const
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgscoordinatetransform.h
Expand Up @@ -27,6 +27,7 @@
#include "qgscoordinatereferencesystem.h"
class QDomNode;
class QDomDocument;
class QPolygonF;

//non qt includes
#include <iostream>
Expand Down Expand Up @@ -158,6 +159,8 @@ class CORE_EXPORT QgsCoordinateTransform : public QObject
void transformInPlace( QVector<double>& x, QVector<double>& y, QVector<double>& z,
TransformDirection direction = ForwardTransform ) const;

void transformPolygon( QPolygonF& poly, TransformDirection direction = ForwardTransform ) const;

#ifdef ANDROID
void transformInPlace( float& x, float& y, float& z, TransformDirection direction = ForwardTransform ) const;

Expand Down
31 changes: 11 additions & 20 deletions src/core/symbology-ng/qgsrendererv2.cpp
Expand Up @@ -68,11 +68,6 @@ unsigned char* QgsFeatureRendererV2::_getLineString( QPolygonF& pts, QgsRenderCo

bool hasZValue = ( wkbType == QGis::WKBLineString25D );
double x, y;
#ifdef ANDROID
qreal z;
#else
double z;
#endif //ANDROID
const QgsCoordinateTransform* ct = context.coordinateTransform();
const QgsMapToPixel& mtp = context.mapToPixel();

Expand Down Expand Up @@ -104,14 +99,14 @@ unsigned char* QgsFeatureRendererV2::_getLineString( QPolygonF& pts, QgsRenderCo
}

//transform the QPolygonF to screen coordinates
if ( ct )
{
ct->transformPolygon( pts );
}

QPointF* ptr = pts.data();
for ( int i = 0; i < pts.size(); ++i, ++ptr )
{
if ( ct )
{
z = 0;
ct->transformInPlace( ptr->rx(), ptr->ry(), z );
}
mtp.transformInPlace( ptr->rx(), ptr->ry() );
}

Expand All @@ -136,11 +131,6 @@ unsigned char* QgsFeatureRendererV2::_getPolygon( QPolygonF& pts, QList<QPolygon

const QgsCoordinateTransform* ct = context.coordinateTransform();
const QgsMapToPixel& mtp = context.mapToPixel();
#ifdef ANDROID
qreal z = 0; // dummy variable for coordiante transform
#else
double z = 0; // dummy variable for coordiante transform
#endif
const QgsRectangle& e = context.extent();
double cw = e.width() / 10; double ch = e.height() / 10;
QgsRectangle clipRect( e.xMinimum() - cw, e.yMinimum() - ch, e.xMaximum() + cw, e.yMaximum() + ch );
Expand Down Expand Up @@ -172,14 +162,15 @@ unsigned char* QgsFeatureRendererV2::_getPolygon( QPolygonF& pts, QList<QPolygon
QgsClipper::trimPolygon( poly, clipRect );

//transform the QPolygonF to screen coordinates
if ( ct )
{
ct->transformPolygon( poly );
}


ptr = poly.data();
for ( int i = 0; i < poly.size(); ++i, ++ptr )
{
if ( ct )
{
z = 0;
ct->transformInPlace( ptr->rx(), ptr->ry(), z );
}
mtp.transformInPlace( ptr->rx(), ptr->ry() );
}

Expand Down

0 comments on commit 863da7d

Please sign in to comment.