Skip to content

Commit 45861d3

Browse files
committed
Restrict curve points to 0-1 range
1 parent dcf6104 commit 45861d3

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/core/qgspropertytransformer.cpp

+24-14
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ bool QgsGenericNumericTransformer::readXml( const QDomElement &transformerElem,
192192

193193
double QgsGenericNumericTransformer::value( double input ) const
194194
{
195+
input = transformNumeric( input );
195196
if ( qgsDoubleNear( mExponent, 1.0 ) )
196197
return mMinOutput + ( qBound( mMinValue, input, mMaxValue ) - mMinValue ) * ( mMaxOutput - mMinOutput ) / ( mMaxValue - mMinValue );
197198
else
@@ -211,7 +212,7 @@ QVariant QgsGenericNumericTransformer::transform( const QgsExpressionContext& co
211212
if ( ok )
212213
{
213214
//apply scaling to value
214-
return value( transformNumeric( dblValue ) );
215+
return value( dblValue );
215216
}
216217
else
217218
{
@@ -386,6 +387,8 @@ bool QgsSizeScaleTransformer::readXml( const QDomElement &transformerElem, const
386387

387388
double QgsSizeScaleTransformer::size( double value ) const
388389
{
390+
value = transformNumeric( value );
391+
389392
switch ( mType )
390393
{
391394
case Linear:
@@ -433,7 +436,7 @@ QVariant QgsSizeScaleTransformer::transform( const QgsExpressionContext& context
433436
if ( ok )
434437
{
435438
//apply scaling to value
436-
return size( transformNumeric( dblValue ) );
439+
return size( dblValue );
437440
}
438441
else
439442
{
@@ -638,7 +641,7 @@ QVariant QgsColorRampTransformer::transform( const QgsExpressionContext &context
638641
if ( ok )
639642
{
640643
//apply scaling to value
641-
return color( transformNumeric( dblValue ) );
644+
return color( dblValue );
642645
}
643646
else
644647
{
@@ -661,6 +664,7 @@ QString QgsColorRampTransformer::toExpression( const QString& baseExpression ) c
661664

662665
QColor QgsColorRampTransformer::color( double value ) const
663666
{
667+
value = transformNumeric( value );
664668
double scaledVal = qBound( 0.0, ( value - mMinValue ) / ( mMaxValue - mMinValue ), 1.0 );
665669

666670
if ( !mGradientRamp )
@@ -733,6 +737,11 @@ void QgsCurveTransform::setControlPoints( const QList<QgsPoint>& points )
733737
{
734738
mControlPoints = points;
735739
std::sort( mControlPoints.begin(), mControlPoints.end(), sortByX );
740+
for ( int i = 0; i < mControlPoints.count(); ++i )
741+
{
742+
mControlPoints[ i ] = QgsPoint( qBound( 0.0, mControlPoints.at( i ).x(), 1.0 ),
743+
qBound( 0.0, mControlPoints.at( i ).y(), 1.0 ) );
744+
}
736745
calcSecondDerivativeArray();
737746
}
738747

@@ -769,27 +778,27 @@ double QgsCurveTransform::y( double x ) const
769778
{
770779
int n = mControlPoints.count();
771780
if ( n < 2 )
772-
return x; // invalid
781+
return qBound( 0.0, x, 1.0 ); // invalid
773782
else if ( n < 3 )
774783
{
775784
// linear
776785
if ( x <= mControlPoints.at( 0 ).x() )
777-
return mControlPoints.at( 0 ).y();
786+
return qBound( 0.0, mControlPoints.at( 0 ).y(), 1.0 );
778787
else if ( x >= mControlPoints.at( n - 1 ).x() )
779-
return mControlPoints.at( 1 ).y();
788+
return qBound( 0.0, mControlPoints.at( 1 ).y(), 1.0 );
780789
else
781790
{
782791
double dx = mControlPoints.at( 1 ).x() - mControlPoints.at( 0 ).x();
783792
double dy = mControlPoints.at( 1 ).y() - mControlPoints.at( 0 ).y();
784-
return x * ( dy / dx ) + mControlPoints.at( 0 ).y();
793+
return qBound( 0.0, ( x - mControlPoints.at( 0 ).x() ) * ( dy / dx ) + mControlPoints.at( 0 ).y(), 1.0 );
785794
}
786795
}
787796

788797
// safety check
789798
if ( x <= mControlPoints.at( 0 ).x() )
790-
return mControlPoints.at( 0 ).y();
799+
return qBound( 0.0, mControlPoints.at( 0 ).y(), 1.0 );
791800
if ( x >= mControlPoints.at( n - 1 ).x() )
792-
return mControlPoints.at( n - 1 ).y();
801+
return qBound( 0.0, mControlPoints.at( n - 1 ).y(), 1.0 );
793802

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

808817
double a = 1 - t;
809818

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

813823
++pointIt;
@@ -819,7 +829,7 @@ double QgsCurveTransform::y( double x ) const
819829
}
820830

821831
//should not happen
822-
return x;
832+
return qBound( 0.0, x, 1.0 );
823833
}
824834

825835
// this code is adapted from https://github.com/OpenFibers/Photoshop-Curves
@@ -851,7 +861,7 @@ QVector<double> QgsCurveTransform::y( const QVector<double>& x ) const
851861
// safety check
852862
while ( currentX <= currentControlPoint.x() )
853863
{
854-
result << currentControlPoint.y();
864+
result << qBound( 0.0, currentControlPoint.y(), 1.0 );
855865
xIndex++;
856866
currentX = x.at( xIndex );
857867
}
@@ -867,7 +877,7 @@ QVector<double> QgsCurveTransform::y( const QVector<double>& x ) const
867877

868878
double a = 1 - t;
869879

870-
result << a*currentControlPoint.y() + t*nextControlPoint.y() + ( h*h / 6 )*(( a*a*a - a )*mSecondDerivativeArray[i] + ( t*t*t - t )*mSecondDerivativeArray[i+1] );
880+
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 );
871881
xIndex++;
872882
if ( xIndex == x.count() )
873883
return result;
@@ -886,7 +896,7 @@ QVector<double> QgsCurveTransform::y( const QVector<double>& x ) const
886896
// safety check
887897
while ( xIndex < x.count() )
888898
{
889-
result << nextControlPoint.y();
899+
result << qBound( 0.0, nextControlPoint.y(), 1.0 );
890900
xIndex++;
891901
}
892902

0 commit comments

Comments
 (0)