Skip to content

Commit

Permalink
Move a bunch of QgsMapToPixel methods to header so that they can be
Browse files Browse the repository at this point in the history
optimised by the compiler, and add equality operator to QgsMapToPixel
  • Loading branch information
nyalldawson committed Jan 28, 2021
1 parent ee70cc0 commit ebf16b0
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 117 deletions.
4 changes: 4 additions & 0 deletions python/core/auto_generated/qgsmaptopixel.sip.in
Expand Up @@ -205,6 +205,10 @@ Returns the center y-coordinate for the transform.
.. versionadded:: 3.0
%End

bool operator==( const QgsMapToPixel &other ) const;

bool operator!=( const QgsMapToPixel &other ) const;

};


Expand Down
108 changes: 10 additions & 98 deletions src/core/qgsmaptopixel.cpp
Expand Up @@ -37,8 +37,8 @@ QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
, mRotation( rotation )
, mXCenter( xc )
, mYCenter( yc )
, xMin( xc - ( mWidth * mMapUnitsPerPixel / 2.0 ) )
, yMin( yc - ( mHeight * mMapUnitsPerPixel / 2.0 ) )
, mXMin( xc - ( mWidth * mMapUnitsPerPixel / 2.0 ) )
, mYMin( yc - ( mHeight * mMapUnitsPerPixel / 2.0 ) )
{
Q_ASSERT( mapUnitsPerPixel > 0 );
updateMatrix();
Expand Down Expand Up @@ -66,16 +66,6 @@ QgsMapToPixel::QgsMapToPixel()
updateMatrix();
}

int QgsMapToPixel::mapHeight() const
{
return mHeight;
}

int QgsMapToPixel::mapWidth() const
{
return mWidth;
}

bool QgsMapToPixel::updateMatrix()
{
QTransform newMatrix = transform();
Expand All @@ -88,33 +78,6 @@ bool QgsMapToPixel::updateMatrix()
return true;
}

QgsPointXY QgsMapToPixel::toMapCoordinates( double x, double y ) const
{
bool invertible;
QTransform matrix = mMatrix.inverted( &invertible );
assert( invertible );
qreal mx, my;
qreal x_qreal = x, y_qreal = y;
matrix.map( x_qreal, y_qreal, &mx, &my );
return QgsPointXY( mx, my );
}

QgsPointXY QgsMapToPixel::toMapCoordinates( QPoint p ) const
{
QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
return QgsPointXY( mapPt );
}

QgsPointXY QgsMapToPixel::toMapCoordinates( int x, int y ) const
{
return toMapCoordinates( static_cast<double>( x ), static_cast<double>( y ) );
}

QgsPointXY QgsMapToPixel::toMapPoint( double x, double y ) const
{
return toMapCoordinates( x, y );
}

void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
{
double oldUnits = mMapUnitsPerPixel;
Expand All @@ -125,11 +88,6 @@ void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
}
}

double QgsMapToPixel::mapUnitsPerPixel() const
{
return mMapUnitsPerPixel;
}

void QgsMapToPixel::setMapRotation( double degrees, double cx, double cy )
{
double oldRotation = mRotation;
Expand All @@ -143,7 +101,7 @@ void QgsMapToPixel::setMapRotation( double degrees, double cx, double cy )
if ( mWidth < 0 )
{
// set width not that we can compute it
mWidth = ( ( mXCenter - xMin ) * 2 ) / mMapUnitsPerPixel;
mWidth = ( ( mXCenter - mXMin ) * 2 ) / mMapUnitsPerPixel;
}

if ( !updateMatrix() )
Expand All @@ -155,11 +113,6 @@ void QgsMapToPixel::setMapRotation( double degrees, double cx, double cy )
}
}

double QgsMapToPixel::mapRotation() const
{
return mRotation;
}

void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
double xc,
double yc,
Expand All @@ -173,17 +126,17 @@ void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
double oldWidth = mWidth;
double oldHeight = mHeight;
double oldRotation = mRotation;
double oldXMin = xMin;
double oldYMin = yMin;
double oldXMin = mXMin;
double oldYMin = mYMin;

mMapUnitsPerPixel = mapUnitsPerPixel;
mXCenter = xc;
mYCenter = yc;
mWidth = width;
mHeight = height;
mRotation = rotation;
xMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
yMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
mXMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
mYMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );

if ( !updateMatrix() )
{
Expand All @@ -193,8 +146,8 @@ void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
mWidth = oldWidth;
mHeight = oldHeight;
mRotation = oldRotation;
xMin = oldXMin;
yMin = oldYMin;
mXMin = oldXMin;
mYMin = oldYMin;
}
}

Expand All @@ -208,47 +161,6 @@ QString QgsMapToPixel::showParameters() const
return rep;
}

QgsPointXY QgsMapToPixel::transform( qreal x, qreal y ) const
{
transformInPlace( x, y );
return QgsPointXY( x, y );
}

QgsPointXY QgsMapToPixel::transform( const QgsPointXY &p ) const
{
qreal x = p.x(), y = p.y();
transformInPlace( x, y );
// QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy));
return QgsPointXY( x, y );
}

void QgsMapToPixel::transform( QgsPointXY *p ) const
{
qreal x = p->x(), y = p->y();
transformInPlace( x, y );
// QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p->x()).arg(x).arg(p->y()).arg(y));
p->set( x, y );
}

void QgsMapToPixel::transformInPlace( double &x, double &y ) const
{
// Map 2 Pixel
qreal mx, my;
qreal x_qreal = x, y_qreal = y;
mMatrix.map( x_qreal, y_qreal, &mx, &my );
//QgsDebugMsg(QString("XXX transformInPlace X : %1-->%2, Y: %3 -->%4").arg(x).arg(mx).arg(y).arg(my));
x = mx;
y = my;
}

void QgsMapToPixel::transformInPlace( float &x, float &y ) const
{
double mx = x, my = y;
transformInPlace( mx, my );
x = mx;
y = my;
}

QTransform QgsMapToPixel::transform() const
{
// NOTE: operations are done in the reverse order in which
Expand All @@ -261,7 +173,7 @@ QTransform QgsMapToPixel::transform() const
{
//no rotation, return a simplified matrix
return QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
.translate( -xMin, - ( yMin + mHeight * mMapUnitsPerPixel ) );
.translate( -mXMin, - ( mYMin + mHeight * mMapUnitsPerPixel ) );
}
else
{
Expand Down

0 comments on commit ebf16b0

Please sign in to comment.