Skip to content

Commit

Permalink
Restrict curve points to 0-1 range
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 22, 2017
1 parent dcf6104 commit 45861d3
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/core/qgspropertytransformer.cpp
Expand Up @@ -192,6 +192,7 @@ bool QgsGenericNumericTransformer::readXml( const QDomElement &transformerElem,

double QgsGenericNumericTransformer::value( double input ) const
{
input = transformNumeric( input );
if ( qgsDoubleNear( mExponent, 1.0 ) )
return mMinOutput + ( qBound( mMinValue, input, mMaxValue ) - mMinValue ) * ( mMaxOutput - mMinOutput ) / ( mMaxValue - mMinValue );
else
Expand All @@ -211,7 +212,7 @@ QVariant QgsGenericNumericTransformer::transform( const QgsExpressionContext& co
if ( ok )
{
//apply scaling to value
return value( transformNumeric( dblValue ) );
return value( dblValue );
}
else
{
Expand Down Expand Up @@ -386,6 +387,8 @@ bool QgsSizeScaleTransformer::readXml( const QDomElement &transformerElem, const

double QgsSizeScaleTransformer::size( double value ) const
{
value = transformNumeric( value );

switch ( mType )
{
case Linear:
Expand Down Expand Up @@ -433,7 +436,7 @@ QVariant QgsSizeScaleTransformer::transform( const QgsExpressionContext& context
if ( ok )
{
//apply scaling to value
return size( transformNumeric( dblValue ) );
return size( dblValue );
}
else
{
Expand Down Expand Up @@ -638,7 +641,7 @@ QVariant QgsColorRampTransformer::transform( const QgsExpressionContext &context
if ( ok )
{
//apply scaling to value
return color( transformNumeric( dblValue ) );
return color( dblValue );
}
else
{
Expand All @@ -661,6 +664,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 );

if ( !mGradientRamp )
Expand Down Expand Up @@ -733,6 +737,11 @@ void QgsCurveTransform::setControlPoints( const QList<QgsPoint>& points )
{
mControlPoints = points;
std::sort( mControlPoints.begin(), mControlPoints.end(), sortByX );
for ( int i = 0; i < mControlPoints.count(); ++i )
{
mControlPoints[ i ] = QgsPoint( qBound( 0.0, mControlPoints.at( i ).x(), 1.0 ),
qBound( 0.0, mControlPoints.at( i ).y(), 1.0 ) );
}
calcSecondDerivativeArray();
}

Expand Down Expand Up @@ -769,27 +778,27 @@ double QgsCurveTransform::y( double x ) const
{
int n = mControlPoints.count();
if ( n < 2 )
return x; // invalid
return qBound( 0.0, x, 1.0 ); // invalid
else if ( n < 3 )
{
// linear
if ( x <= mControlPoints.at( 0 ).x() )
return mControlPoints.at( 0 ).y();
return qBound( 0.0, mControlPoints.at( 0 ).y(), 1.0 );
else if ( x >= mControlPoints.at( n - 1 ).x() )
return mControlPoints.at( 1 ).y();
return qBound( 0.0, mControlPoints.at( 1 ).y(), 1.0 );
else
{
double dx = mControlPoints.at( 1 ).x() - mControlPoints.at( 0 ).x();
double dy = mControlPoints.at( 1 ).y() - mControlPoints.at( 0 ).y();
return x * ( dy / dx ) + mControlPoints.at( 0 ).y();
return qBound( 0.0, ( x - mControlPoints.at( 0 ).x() ) * ( dy / dx ) + mControlPoints.at( 0 ).y(), 1.0 );
}
}

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

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

double a = 1 - t;

return a*currentControlPoint.y() + t*nextControlPoint.y() + ( h*h / 6 )*(( a*a*a - a )*mSecondDerivativeArray[i] + ( t*t*t - t )*mSecondDerivativeArray[i+1] );
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 );
}

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

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

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

double a = 1 - t;

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

Expand Down

0 comments on commit 45861d3

Please sign in to comment.