Skip to content

Commit

Permalink
Use c++17 std::clamp instead of qBound
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 21, 2021
1 parent f3a81eb commit 08ca000
Show file tree
Hide file tree
Showing 29 changed files with 119 additions and 90 deletions.
16 changes: 8 additions & 8 deletions src/3d/terrain/qgsdemterraintilegeometry_p.cpp
Expand Up @@ -64,14 +64,14 @@ static QByteArray createPlaneVertexData( int res, float side, float vertScale, f
// Iterate over z
for ( int j = -1; j <= resolution.height(); ++j )
{
int jBound = qBound( 0, j, jMax );
int jBound = std::clamp( j, 0, jMax );
const float z = z0 + static_cast<float>( jBound ) * dz;
const float v = static_cast<float>( jBound ) * dv;

// Iterate over x
for ( int i = -1; i <= resolution.width(); ++i )
{
int iBound = qBound( 0, i, iMax );
int iBound = std::clamp( i, 0, iMax );
const float x = x0 + static_cast<float>( iBound ) * dx;
const float u = static_cast<float>( iBound ) * du;

Expand All @@ -95,10 +95,10 @@ static QByteArray createPlaneVertexData( int res, float side, float vertScale, f

// calculate normal coordinates
#define zAt( ii, jj ) zData[ jj * resolution.width() + ii ] * vertScale
float zi0 = zAt( qBound( 0, i - 1, iMax ), jBound );
float zi1 = zAt( qBound( 0, i + 1, iMax ), jBound );
float zj0 = zAt( iBound, qBound( 0, j - 1, jMax ) );
float zj1 = zAt( iBound, qBound( 0, j + 1, jMax ) );
float zi0 = zAt( std::clamp( i - 1, 0, iMax ), jBound );
float zi1 = zAt( std::clamp( i + 1, 0, iMax ), jBound );
float zj0 = zAt( iBound, std::clamp( j - 1, 0, jMax ) );
float zj1 = zAt( iBound, std::clamp( j + 1, 0, jMax ) );

QVector3D n;
if ( std::isnan( zi0 ) || std::isnan( zi1 ) || std::isnan( zj0 ) || std::isnan( zj1 ) )
Expand Down Expand Up @@ -137,8 +137,8 @@ static QByteArray createPlaneVertexData( int res, float side, float vertScale, f

inline int ijToHeightMapIndex( int i, int j, int resX, int resZ )
{
i = qBound( 1, i, resX ) - 1;
j = qBound( 1, j, resZ ) - 1;
i = std::clamp( i, 1, resX ) - 1;
j = std::clamp( j, 1, resZ ) - 1;
return j * resX + i;
}

Expand Down
4 changes: 2 additions & 2 deletions src/3d/terrain/qgsdemterraintileloader_p.cpp
Expand Up @@ -293,8 +293,8 @@ float QgsDemHeightMapGenerator::heightAt( double x, double y )

int cellX = ( int )( ( x - rect.xMinimum() ) / rect.width() * res + .5f );
int cellY = ( int )( ( rect.yMaximum() - y ) / rect.height() * res + .5f );
cellX = qBound( 0, cellX, res - 1 );
cellY = qBound( 0, cellY, res - 1 );
cellX = std::clamp( cellX, 0, res - 1 );
cellY = std::clamp( cellY, 0, res - 1 );

const float *data = ( const float * ) mDtmCoarseData.constData();
return data[cellX + cellY * res];
Expand Down
2 changes: 1 addition & 1 deletion src/core/classification/qgsclassificationmethod.cpp
Expand Up @@ -142,7 +142,7 @@ void QgsClassificationMethod::setSymmetricMode( bool enabled, double symmetryPoi
void QgsClassificationMethod::setLabelPrecision( int precision )
{
// Limit the range of decimal places to a reasonable range
precision = qBound( MIN_PRECISION, precision, MAX_PRECISION );
precision = std::clamp( precision, MIN_PRECISION, MAX_PRECISION );
mLabelPrecision = precision;
mLabelNumberScale = 1.0;
mLabelNumberSuffix.clear();
Expand Down
11 changes: 11 additions & 0 deletions src/core/effects/qgscoloreffect.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgscoloreffect.h"
#include "qgsimageoperation.h"
#include "qgssymbollayerutils.h"
#include <algorithm>

QgsPaintEffect *QgsColorEffect::create( const QVariantMap &map )
{
Expand Down Expand Up @@ -119,6 +120,16 @@ QgsColorEffect *QgsColorEffect::clone() const
return newEffect;
}

void QgsColorEffect::setBrightness( int brightness )
{
mBrightness = std::clamp( brightness, -255, 255 );
}

void QgsColorEffect::setContrast( int contrast )
{
mContrast = std::clamp( contrast, -100, 100 );
}

void QgsColorEffect::setColorizeColor( const QColor &colorizeColor )
{
mColorizeColor = colorizeColor;
Expand Down
4 changes: 2 additions & 2 deletions src/core/effects/qgscoloreffect.h
Expand Up @@ -58,7 +58,7 @@ class CORE_EXPORT QgsColorEffect : public QgsPaintEffect SIP_NODEFAULTCTORS
* lightening
* \see setBrightness
*/
void setBrightness( int brightness ) { mBrightness = qBound( -255, brightness, 255 ); }
void setBrightness( int brightness );

/**
* Returns the brightness modification for the effect.
Expand All @@ -76,7 +76,7 @@ class CORE_EXPORT QgsColorEffect : public QgsPaintEffect SIP_NODEFAULTCTORS
* greater contrast
* \see setContrast
*/
void setContrast( int contrast ) { mContrast = qBound( -100, contrast, 100 ); }
void setContrast( int contrast );

/**
* Returns the contrast modification for the effect.
Expand Down
8 changes: 4 additions & 4 deletions src/core/effects/qgsimageoperation.cpp
Expand Up @@ -260,7 +260,7 @@ void QgsImageOperation::BrightnessContrastPixelOperation::operator()( QRgb &rgb,

int QgsImageOperation::adjustColorComponent( int colorComponent, int brightness, double contrastFactor )
{
return qBound( 0, static_cast< int >( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ), 255 );
return std::clamp( static_cast< int >( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ), 0, 255 );
}

//hue/saturation
Expand Down Expand Up @@ -348,7 +348,7 @@ void QgsImageOperation::MultiplyOpacityPixelOperation::operator()( QRgb &rgb, co
{
Q_UNUSED( x )
Q_UNUSED( y )
rgb = qRgba( qRed( rgb ), qGreen( rgb ), qBlue( rgb ), qBound( 0.0, std::round( mFactor * qAlpha( rgb ) ), 255.0 ) );
rgb = qRgba( qRed( rgb ), qGreen( rgb ), qBlue( rgb ), std::clamp( std::round( mFactor * qAlpha( rgb ) ), 0.0, 255.0 ) );
}

// overlay color
Expand Down Expand Up @@ -694,7 +694,7 @@ inline QRgb QgsImageOperation::GaussianBlurOperation::gaussianBlurVertical( cons

for ( int i = 0; i <= mRadius * 2; ++i )
{
y = qBound( 0, posy + ( i - mRadius ), height - 1 );
y = std::clamp( posy + ( i - mRadius ), 0, height - 1 );
ref = sourceFirstLine + sourceBpl * y;

QRgb *refRgb = reinterpret_cast< QRgb * >( ref );
Expand All @@ -718,7 +718,7 @@ inline QRgb QgsImageOperation::GaussianBlurOperation::gaussianBlurHorizontal( co

for ( int i = 0; i <= mRadius * 2; ++i )
{
x = qBound( 0, posx + ( i - mRadius ), width - 1 );
x = std::clamp( posx + ( i - mRadius ), 0, width - 1 );
ref = sourceFirstLine + x * 4;

QRgb *refRgb = reinterpret_cast< QRgb * >( ref );
Expand Down
6 changes: 3 additions & 3 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -2741,9 +2741,9 @@ static QVariant fcnSmooth( const QVariantList &values, const QgsExpressionContex
return QVariant();

int iterations = std::min( QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent ), 10 );
double offset = qBound( 0.0, QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent ), 0.5 );
double offset = std::clamp( QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent ), 0.0, 0.5 );
double minLength = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
double maxAngle = qBound( 0.0, QgsExpressionUtils::getDoubleValue( values.at( 4 ), parent ), 180.0 );
double maxAngle = std::clamp( QgsExpressionUtils::getDoubleValue( values.at( 4 ), parent ), 0.0, 180.0 );

QgsGeometry smoothed = geom.smooth( static_cast<unsigned int>( iterations ), offset, minLength, maxAngle );
if ( smoothed.isNull() )
Expand Down Expand Up @@ -4055,7 +4055,7 @@ static QVariant fcnHausdorffDistance( const QVariantList &values, const QgsExpre
if ( values.length() == 3 && values.at( 2 ).isValid() )
{
double densify = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
densify = qBound( 0.0, densify, 1.0 );
densify = std::clamp( densify, 0.0, 1.0 );
res = g1.hausdorffDistanceDensify( g2, densify );
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/core/labeling/qgslabelobstaclesettings.cpp
Expand Up @@ -40,7 +40,7 @@ void QgsLabelObstacleSettings::updateDataDefinedProperties( const QgsPropertyCol
double factorD = exprVal.toDouble( &ok );
if ( ok )
{
factorD = qBound( 0.0, factorD, 10.0 );
factorD = std::clamp( factorD, 0.0, 10.0 );
factorD = factorD / 5.0 + 0.0001; // convert 0 -> 10 to 0.0001 -> 2.0
mObstacleFactor = factorD;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/labeling/qgspallabeling.cpp
Expand Up @@ -1937,8 +1937,8 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
const QPointF maxcharanglePt = QgsSymbolLayerUtils::toPoint( exprVal, &ok );
if ( ok )
{
maxcharanglein = qBound( 20.0, static_cast< double >( maxcharanglePt.x() ), 60.0 );
maxcharangleout = qBound( 20.0, static_cast< double >( maxcharanglePt.y() ), 95.0 );
maxcharanglein = std::clamp( static_cast< double >( maxcharanglePt.x() ), 20.0, 60.0 );
maxcharangleout = std::clamp( static_cast< double >( maxcharanglePt.y() ), 20.0, 95.0 );
}
}
// make sure maxcharangleout is always negative
Expand Down Expand Up @@ -2592,7 +2592,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
double priorityD = exprVal.toDouble( &ok );
if ( ok )
{
priorityD = qBound( 0.0, priorityD, 10.0 );
priorityD = std::clamp( priorityD, 0.0, 10.0 );
priorityD = 1 - priorityD / 10.0; // convert 0..10 --> 1..0
( *labelFeature )->setPriority( priorityD );
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgscolorramp.cpp
Expand Up @@ -411,9 +411,9 @@ QList<QColor> QgsLimitedRandomColorRamp::randomColors( int count,
//see http://basecase.org/env/on-rainbows for more details
currentHueAngle += 137.50776;
//scale hue to between hueMax and hueMin
h = qBound( 0.0, std::round( ( std::fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 359.0 );
s = qBound( 0, ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 255 );
v = qBound( 0, ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 );
h = std::clamp( std::round( ( std::fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 0.0, 359.0 );
s = std::clamp( ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 0, 255 );
v = std::clamp( ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 0, 255 );
colors.append( QColor::fromHsv( h, s, v ) );
}
return colors;
Expand Down
40 changes: 20 additions & 20 deletions src/core/qgspropertytransformer.cpp
Expand Up @@ -178,13 +178,13 @@ bool QgsGenericNumericTransformer::loadVariant( const QVariant &transformer )
double QgsGenericNumericTransformer::value( double input ) const
{
if ( qgsDoubleNear( mMaxValue, mMinValue ) )
return qBound( mMinOutput, input, mMaxOutput );
return std::clamp( input, mMinOutput, mMaxOutput );

input = transformNumeric( input );
if ( qgsDoubleNear( mExponent, 1.0 ) )
return mMinOutput + ( qBound( mMinValue, input, mMaxValue ) - mMinValue ) * ( mMaxOutput - mMinOutput ) / ( mMaxValue - mMinValue );
return mMinOutput + ( std::clamp( input, mMinValue, mMaxValue ) - mMinValue ) * ( mMaxOutput - mMinOutput ) / ( mMaxValue - mMinValue );
else
return mMinOutput + std::pow( qBound( mMinValue, input, mMaxValue ) - mMinValue, mExponent ) * ( mMaxOutput - mMinOutput ) / std::pow( mMaxValue - mMinValue, mExponent );
return mMinOutput + std::pow( std::clamp( input, mMinValue, mMaxValue ) - mMinValue, mExponent ) * ( mMaxOutput - mMinOutput ) / std::pow( mMaxValue - mMinValue, mExponent );
}

QVariant QgsGenericNumericTransformer::transform( const QgsExpressionContext &context, const QVariant &v ) const
Expand Down Expand Up @@ -360,12 +360,12 @@ double QgsSizeScaleTransformer::size( double value ) const
switch ( mType )
{
case Linear:
return mMinSize + ( qBound( mMinValue, value, mMaxValue ) - mMinValue ) * ( mMaxSize - mMinSize ) / ( mMaxValue - mMinValue );
return mMinSize + ( std::clamp( value, mMinValue, mMaxValue ) - mMinValue ) * ( mMaxSize - mMinSize ) / ( mMaxValue - mMinValue );

case Area:
case Flannery:
case Exponential:
return mMinSize + std::pow( qBound( mMinValue, value, mMaxValue ) - mMinValue, mExponent ) * ( mMaxSize - mMinSize ) / std::pow( mMaxValue - mMinValue, mExponent );
return mMinSize + std::pow( std::clamp( value, mMinValue, mMaxValue ) - mMinValue, mExponent ) * ( mMaxSize - mMinSize ) / std::pow( mMaxValue - mMinValue, mExponent );

}
return 0;
Expand Down Expand Up @@ -631,7 +631,7 @@ QString QgsColorRampTransformer::toExpression( const QString &baseExpression ) c
QColor QgsColorRampTransformer::color( double value ) const
{
value = transformNumeric( value );
double scaledVal = qBound( 0.0, ( value - mMinValue ) / ( mMaxValue - mMinValue ), 1.0 );
double scaledVal = std::clamp( ( value - mMinValue ) / ( mMaxValue - mMinValue ), 0.0, 1.0 );

if ( !mGradientRamp )
return mNullColor;
Expand Down Expand Up @@ -708,8 +708,8 @@ void QgsCurveTransform::setControlPoints( const QList<QgsPointXY> &points )
std::sort( mControlPoints.begin(), mControlPoints.end(), sortByX );
for ( int i = 0; i < mControlPoints.count(); ++i )
{
mControlPoints[ i ] = QgsPointXY( qBound( 0.0, mControlPoints.at( i ).x(), 1.0 ),
qBound( 0.0, mControlPoints.at( i ).y(), 1.0 ) );
mControlPoints[ i ] = QgsPointXY( std::clamp( mControlPoints.at( i ).x(), 0.0, 1.0 ),
std::clamp( mControlPoints.at( i ).y(), 0.0, 1.0 ) );
}
calcSecondDerivativeArray();
}
Expand Down Expand Up @@ -747,27 +747,27 @@ double QgsCurveTransform::y( double x ) const
{
int n = mControlPoints.count();
if ( n < 2 )
return qBound( 0.0, x, 1.0 ); // invalid
return std::clamp( x, 0.0, 1.0 ); // invalid
else if ( n < 3 )
{
// linear
if ( x <= mControlPoints.at( 0 ).x() )
return qBound( 0.0, mControlPoints.at( 0 ).y(), 1.0 );
return std::clamp( mControlPoints.at( 0 ).y(), 0.0, 1.0 );
else if ( x >= mControlPoints.at( n - 1 ).x() )
return qBound( 0.0, mControlPoints.at( 1 ).y(), 1.0 );
return std::clamp( mControlPoints.at( 1 ).y(), 0.0, 1.0 );
else
{
double dx = mControlPoints.at( 1 ).x() - mControlPoints.at( 0 ).x();
double dy = mControlPoints.at( 1 ).y() - mControlPoints.at( 0 ).y();
return qBound( 0.0, ( x - mControlPoints.at( 0 ).x() ) * ( dy / dx ) + mControlPoints.at( 0 ).y(), 1.0 );
return std::clamp( ( x - mControlPoints.at( 0 ).x() ) * ( dy / dx ) + mControlPoints.at( 0 ).y(), 0.0, 1.0 );
}
}

// safety check
if ( x <= mControlPoints.at( 0 ).x() )
return qBound( 0.0, mControlPoints.at( 0 ).y(), 1.0 );
return std::clamp( mControlPoints.at( 0 ).y(), 0.0, 1.0 );
if ( x >= mControlPoints.at( n - 1 ).x() )
return qBound( 0.0, mControlPoints.at( n - 1 ).y(), 1.0 );
return std::clamp( mControlPoints.at( n - 1 ).y(), 0.0, 1.0 );

// find corresponding segment
QList<QgsPointXY>::const_iterator pointIt = mControlPoints.constBegin();
Expand All @@ -785,8 +785,8 @@ double QgsCurveTransform::y( double x ) const

double a = 1 - t;

return qBound( 0.0, a * currentControlPoint.y() + t * nextControlPoint.y() + ( h * h / 6 ) * ( ( a * a * a - a ) * mSecondDerivativeArray[i] + ( t * t * t - t ) * mSecondDerivativeArray[i + 1] ),
1.0 );
return std::clamp( a * currentControlPoint.y() + t * nextControlPoint.y() + ( h * h / 6 ) * ( ( a * a * a - a ) * mSecondDerivativeArray[i] + ( t * t * t - t ) * mSecondDerivativeArray[i + 1] ),
0.0, 1.0 );
}

++pointIt;
Expand All @@ -798,7 +798,7 @@ double QgsCurveTransform::y( double x ) const
}

//should not happen
return qBound( 0.0, x, 1.0 );
return std::clamp( x, 0.0, 1.0 );
}

// this code is adapted from https://github.com/OpenFibers/Photoshop-Curves
Expand Down Expand Up @@ -831,7 +831,7 @@ QVector<double> QgsCurveTransform::y( const QVector<double> &x ) const
// safety check
while ( currentX <= currentControlPoint.x() )
{
result << qBound( 0.0, currentControlPoint.y(), 1.0 );
result << std::clamp( currentControlPoint.y(), 0.0, 1.0 );
xIndex++;
currentX = x.at( xIndex );
}
Expand All @@ -847,7 +847,7 @@ QVector<double> QgsCurveTransform::y( const QVector<double> &x ) const

double a = 1 - t;

result << qBound( 0.0, a * currentControlPoint.y() + t * nextControlPoint.y() + ( h * h / 6 ) * ( ( a * a * a - a )*mSecondDerivativeArray[i] + ( t * t * t - t )*mSecondDerivativeArray[i + 1] ), 1.0 );
result << std::clamp( a * currentControlPoint.y() + t * nextControlPoint.y() + ( h * h / 6 ) * ( ( a * a * a - a )*mSecondDerivativeArray[i] + ( t * t * t - t )*mSecondDerivativeArray[i + 1] ), 0.0, 1.0 );
xIndex++;
if ( xIndex == x.count() )
return result;
Expand All @@ -866,7 +866,7 @@ QVector<double> QgsCurveTransform::y( const QVector<double> &x ) const
// safety check
while ( xIndex < x.count() )
{
result << qBound( 0.0, nextControlPoint.y(), 1.0 );
result << std::clamp( nextControlPoint.y(), 0.0, 1.0 );
xIndex++;
}

Expand Down
16 changes: 8 additions & 8 deletions src/core/qgstiles.cpp
Expand Up @@ -53,10 +53,10 @@ QgsPointXY QgsTileMatrix::tileCenter( QgsTileXYZ id ) const

QgsTileRange QgsTileMatrix::tileRangeFromExtent( const QgsRectangle &r )
{
double x0 = qBound( mExtent.xMinimum(), r.xMinimum(), mExtent.xMaximum() );
double y0 = qBound( mExtent.yMinimum(), r.yMinimum(), mExtent.yMaximum() );
double x1 = qBound( mExtent.xMinimum(), r.xMaximum(), mExtent.xMaximum() );
double y1 = qBound( mExtent.yMinimum(), r.yMaximum(), mExtent.yMaximum() );
double x0 = std::clamp( r.xMinimum(), mExtent.xMinimum(), mExtent.xMaximum() );
double y0 = std::clamp( r.yMinimum(), mExtent.yMinimum(), mExtent.yMaximum() );
double x1 = std::clamp( r.xMaximum(), mExtent.xMinimum(), mExtent.xMaximum() );
double y1 = std::clamp( r.yMaximum(), mExtent.yMinimum(), mExtent.yMaximum() );
if ( x0 >= x1 || y0 >= y1 )
return QgsTileRange(); // nothing to display

Expand All @@ -68,10 +68,10 @@ QgsTileRange QgsTileMatrix::tileRangeFromExtent( const QgsRectangle &r )
QgsDebugMsgLevel( QStringLiteral( "Tile range of edges [%1,%2] - [%3,%4]" ).arg( tileX1 ).arg( tileY1 ).arg( tileX2 ).arg( tileY2 ), 2 );

// figure out tile range from zoom
int startColumn = qBound( 0, static_cast<int>( floor( tileX1 ) ), mMatrixWidth - 1 );
int endColumn = qBound( 0, static_cast<int>( floor( tileX2 ) ), mMatrixWidth - 1 );
int startRow = qBound( 0, static_cast<int>( floor( tileY1 ) ), mMatrixHeight - 1 );
int endRow = qBound( 0, static_cast<int>( floor( tileY2 ) ), mMatrixHeight - 1 );
int startColumn = std::clamp( static_cast<int>( floor( tileX1 ) ), 0, mMatrixWidth - 1 );
int endColumn = std::clamp( static_cast<int>( floor( tileX2 ) ), 0, mMatrixWidth - 1 );
int startRow = std::clamp( static_cast<int>( floor( tileY1 ) ), 0, mMatrixHeight - 1 );
int endRow = std::clamp( static_cast<int>( floor( tileY2 ) ), 0, mMatrixHeight - 1 );
return QgsTileRange( startColumn, endColumn, startRow, endRow );
}

Expand Down

0 comments on commit 08ca000

Please sign in to comment.