Skip to content
Permalink
Browse files

Use c++17 std::clamp instead of qBound

  • Loading branch information
nyalldawson committed Mar 21, 2021
1 parent f3a81eb commit 08ca000e71bc6d1043e6608d9da06c9b31bfaa06
@@ -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;

@@ -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 ) )
@@ -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;
}

@@ -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];
@@ -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();
@@ -18,6 +18,7 @@
#include "qgscoloreffect.h"
#include "qgsimageoperation.h"
#include "qgssymbollayerutils.h"
#include <algorithm>

QgsPaintEffect *QgsColorEffect::create( const QVariantMap &map )
{
@@ -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;
@@ -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.
@@ -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.
@@ -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
@@ -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
@@ -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 );
@@ -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 );
@@ -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() )
@@ -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
@@ -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;
}
@@ -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
@@ -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 );
}
@@ -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;
@@ -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
@@ -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;
@@ -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;
@@ -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();
}
@@ -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();
@@ -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;
@@ -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
@@ -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 );
}
@@ -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;
@@ -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++;
}

@@ -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

@@ -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 );
}

0 comments on commit 08ca000

Please sign in to comment.