Skip to content

Commit ff43a5d

Browse files
committedOct 13, 2015
Merge pull request #2367 from manisandro/qgsround
Introduce qgsRound since std::round is available only in C++11 onwards
2 parents 3daa57f + 2ddc071 commit ff43a5d

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed
 

‎src/core/geometry/qgsgeos.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,11 +1422,11 @@ GEOSCoordSequence* QgsGeos::createCoordinateSequence( const QgsCurveV2* curve, d
14221422
for ( int i = 0; i < numPoints; ++i )
14231423
{
14241424
QgsPointV2 pt = line->pointN( i ); //todo: create method to get const point reference
1425-
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, i, std::round( pt.x() / precision ) * precision );
1426-
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, i, std::round( pt.y() / precision ) * precision );
1425+
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, i, qgsRound( pt.x() / precision ) * precision );
1426+
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, i, qgsRound( pt.y() / precision ) * precision );
14271427
if ( hasZ )
14281428
{
1429-
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, i, 2, std::round( pt.z() / precision ) * precision );
1429+
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, i, 2, qgsRound( pt.z() / precision ) * precision );
14301430
}
14311431
if ( hasM )
14321432
{
@@ -1474,11 +1474,11 @@ GEOSGeometry* QgsGeos::createGeosPoint( const QgsAbstractGeometryV2* point, int
14741474
GEOSCoordSequence* coordSeq = GEOSCoordSeq_create_r( geosinit.ctxt, 1, coordDims );
14751475
if ( precision > 0. )
14761476
{
1477-
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, 0, std::round( pt->x() / precision ) * precision );
1478-
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, 0, std::round( pt->y() / precision ) * precision );
1477+
GEOSCoordSeq_setX_r( geosinit.ctxt, coordSeq, 0, qgsRound( pt->x() / precision ) * precision );
1478+
GEOSCoordSeq_setY_r( geosinit.ctxt, coordSeq, 0, qgsRound( pt->y() / precision ) * precision );
14791479
if ( pt->is3D() )
14801480
{
1481-
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, 0, 2, std::round( pt->z() / precision ) * precision );
1481+
GEOSCoordSeq_setOrdinate_r( geosinit.ctxt, coordSeq, 0, 2, qgsRound( pt->z() / precision ) * precision );
14821482
}
14831483
}
14841484
else

‎src/core/qgis.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ inline bool qgsDoubleNearSig( double a, double b, int significantDigits = 10 )
289289
qRound( ar * pow( 10.0, significantDigits ) ) == qRound( br * pow( 10.0, significantDigits ) );
290290
}
291291

292+
//
293+
// a round function which returns a double to guard against overflows
294+
//
295+
inline double qgsRound( double x )
296+
{
297+
return x < 0.0 ? std::ceil( x - 0.5 ) : std::floor( x + 0.5 );
298+
}
299+
292300
bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs );
293301

294302
bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs );

‎tests/src/core/testqgis.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TestQGis : public QObject
3636
void permissiveToDouble();
3737
void permissiveToInt();
3838
void doubleToString();
39+
void qgsround();
3940

4041
private:
4142
QString mReport;
@@ -136,5 +137,19 @@ void TestQGis::doubleToString()
136137
QCOMPARE( qgsDoubleToString( 12345, -1 ), QString( "12345" ) );
137138
}
138139

140+
void TestQGis::qgsround()
141+
{
142+
QCOMPARE( qgsRound( 3.141592653589793 ), 3. );
143+
QCOMPARE( qgsRound( 2.718281828459045 ), 3. );
144+
QCOMPARE( qgsRound( -3.141592653589793 ), -3. );
145+
QCOMPARE( qgsRound( -2.718281828459045 ), -3. );
146+
QCOMPARE( qgsRound( 314159265358979.3 ), 314159265358979. );
147+
QCOMPARE( qgsRound( 2718281828459.045 ), 2718281828459. );
148+
QCOMPARE( qgsRound( -314159265358979.3 ), -314159265358979. );
149+
QCOMPARE( qgsRound( -2718281828459.045 ), -2718281828459. );
150+
QCOMPARE( qgsRound( 1.5 ), 2. );
151+
QCOMPARE( qgsRound( -1.5 ), -2. );
152+
}
153+
139154
QTEST_MAIN( TestQGis )
140155
#include "testqgis.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.