Skip to content

Commit

Permalink
render mesh in hight dpi
Browse files Browse the repository at this point in the history
  • Loading branch information
vcloarec committed Jun 30, 2023
1 parent b69d361 commit 5294e51
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/core/mesh/qgsmeshlayerinterpolator.cpp
Expand Up @@ -101,6 +101,8 @@ QgsRasterBlock *QgsMeshLayerInterpolator::block( int, const QgsRectangle &extent
if ( mDataType == QgsMeshDatasetGroupMetadata::DataType::DataOnVertices )
Q_ASSERT( mDatasetValues.count() == mTriangularMesh.vertices().count() );

double pixelRatio = mContext.devicePixelRatio();

for ( int i = 0; i < indexCount; ++i )
{
if ( feedback && feedback->isCanceled() )
Expand Down Expand Up @@ -134,7 +136,7 @@ QgsRasterBlock *QgsMeshLayerInterpolator::block( int, const QgsRectangle &extent

// Get the BBox of the element in pixels
int topLim, bottomLim, leftLim, rightLim;
QgsMeshLayerUtils::boundingBoxToScreenRectangle( mContext.mapToPixel(), mOutputSize, bbox, leftLim, rightLim, topLim, bottomLim );
QgsMeshLayerUtils::boundingBoxToScreenRectangle( mContext.mapToPixel(), mOutputSize, bbox, leftLim, rightLim, topLim, bottomLim, pixelRatio );

double value( 0 ), value1( 0 ), value2( 0 ), value3( 0 );
const int faceIdx = mTriangularMesh.trianglesToNativeFaces()[triangleIndex];
Expand All @@ -155,7 +157,7 @@ QgsRasterBlock *QgsMeshLayerInterpolator::block( int, const QgsRectangle &extent
for ( int k = leftLim; k <= rightLim; k++ )
{
double val;
const QgsPointXY p = mContext.mapToPixel().toMapCoordinates( k, j );
const QgsPointXY p = mContext.mapToPixel().toMapCoordinates( k / pixelRatio, j / pixelRatio );
if ( mDataType == QgsMeshDatasetGroupMetadata::DataType::DataOnVertices )
val = QgsMeshLayerUtils::interpolateFromVerticesData(
p1,
Expand Down
5 changes: 3 additions & 2 deletions src/core/mesh/qgsmeshlayerrenderer.cpp
Expand Up @@ -108,7 +108,7 @@ void QgsMeshLayerRenderer::calculateOutputSize()
const QgsRenderContext &context = *renderContext();
const QgsRectangle extent = context.mapExtent();
const QgsMapToPixel mapToPixel = context.mapToPixel();
const QgsRectangle screenBBox = QgsMeshLayerUtils::boundingBoxToScreenRectangle( mapToPixel, extent );
const QgsRectangle screenBBox = QgsMeshLayerUtils::boundingBoxToScreenRectangle( mapToPixel, extent, context.devicePixelRatio() );
const int width = int( screenBBox.width() );
const int height = int( screenBBox.height() );
mOutputSize = QSize( width, height );
Expand Down Expand Up @@ -569,7 +569,8 @@ void QgsMeshLayerRenderer::renderScalarDatasetOnFaces( const QgsMeshRendererScal
renderer.setOpacity( scalarSettings.opacity() );

std::unique_ptr<QgsRasterBlock> bl( renderer.block( 0, context.mapExtent(), mOutputSize.width(), mOutputSize.height(), mFeedback.get() ) );
const QImage img = bl->image();
QImage img = bl->image();
img.setDevicePixelRatio( context.devicePixelRatio() );

context.painter()->drawImage( 0, 0, img );
}
Expand Down
16 changes: 9 additions & 7 deletions src/core/mesh/qgsmeshlayerutils.cpp
Expand Up @@ -199,13 +199,14 @@ QVector<double> QgsMeshLayerUtils::calculateMagnitudes( const QgsMeshDataBlock &

QgsRectangle QgsMeshLayerUtils::boundingBoxToScreenRectangle(
const QgsMapToPixel &mtp,
const QgsRectangle &bbox
const QgsRectangle &bbox,
double devicePixelRatio
)
{
const QgsPointXY topLeft = mtp.transform( bbox.xMinimum(), bbox.yMaximum() );
const QgsPointXY topRight = mtp.transform( bbox.xMaximum(), bbox.yMaximum() );
const QgsPointXY bottomLeft = mtp.transform( bbox.xMinimum(), bbox.yMinimum() );
const QgsPointXY bottomRight = mtp.transform( bbox.xMaximum(), bbox.yMinimum() );
const QgsPointXY topLeft = mtp.transform( bbox.xMinimum(), bbox.yMaximum() ) * devicePixelRatio;
const QgsPointXY topRight = mtp.transform( bbox.xMaximum(), bbox.yMaximum() ) * devicePixelRatio;
const QgsPointXY bottomLeft = mtp.transform( bbox.xMinimum(), bbox.yMinimum() ) * devicePixelRatio;
const QgsPointXY bottomRight = mtp.transform( bbox.xMaximum(), bbox.yMinimum() ) * devicePixelRatio;

const double xMin = std::min( {topLeft.x(), topRight.x(), bottomLeft.x(), bottomRight.x()} );
const double xMax = std::max( {topLeft.x(), topRight.x(), bottomLeft.x(), bottomRight.x()} );
Expand All @@ -223,9 +224,10 @@ void QgsMeshLayerUtils::boundingBoxToScreenRectangle(
int &leftLim,
int &rightLim,
int &bottomLim,
int &topLim )
int &topLim,
double devicePixelRatio )
{
const QgsRectangle screenBBox = boundingBoxToScreenRectangle( mtp, bbox );
const QgsRectangle screenBBox = boundingBoxToScreenRectangle( mtp, bbox, devicePixelRatio );

bottomLim = std::max( int( screenBBox.yMinimum() ), 0 );
topLim = std::min( int( screenBBox.yMaximum() ), outputSize.height() - 1 );
Expand Down
6 changes: 4 additions & 2 deletions src/core/mesh/qgsmeshlayerutils.h
Expand Up @@ -127,7 +127,8 @@ class CORE_EXPORT QgsMeshLayerUtils
int &leftLim,
int &rightLim,
int &bottomLim,
int &topLim );
int &topLim,
double devicePixelRatio = 1.0 );

/**
* Transformes the bounding box to rectangle in screen coordinates (in pixels)
Expand All @@ -136,7 +137,8 @@ class CORE_EXPORT QgsMeshLayerUtils
*/
static QgsRectangle boundingBoxToScreenRectangle(
const QgsMapToPixel &mtp,
const QgsRectangle &bbox
const QgsRectangle &bbox,
double devicePixelRatio = 1.0
);

/**
Expand Down

0 comments on commit 5294e51

Please sign in to comment.