Skip to content

Commit

Permalink
fix mesh centroid when coordinates are very big (#40757)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcloarec committed Jan 1, 2021
1 parent 7aba695 commit ee6d797
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/core/mesh/qgstriangularmesh.cpp
Expand Up @@ -58,19 +58,27 @@ static void ENP_centroid( const QPolygonF &pX, double &cx, double &cy )

double signedArea = 0.0;

const QPointF &pt0 = pX.first();
QPolygonF localPolygon( pX.count() );
for ( int i = 0; i < pX.count(); ++i )
localPolygon[i] = pX.at( i ) - pt0;

// For all vertices except last
int i = 0;
for ( ; i < pX.size() - 1; ++i )
for ( ; i < localPolygon.size() - 1; ++i )
{
ENP_centroid_step( pX, cx, cy, signedArea, i, i + 1 );
ENP_centroid_step( localPolygon, cx, cy, signedArea, i, i + 1 );
}
// Do last vertex separately to avoid performing an expensive
// modulus operation in each iteration.
ENP_centroid_step( pX, cx, cy, signedArea, i, 0 );
ENP_centroid_step( localPolygon, cx, cy, signedArea, i, 0 );

signedArea *= 0.5;
cx /= ( 6.0 * signedArea );
cy /= ( 6.0 * signedArea );

cx = cx + pt0.x();
cy = cy + pt0.y();
}

void QgsTriangularMesh::triangulate( const QgsMeshFace &face, int nativeIndex )
Expand Down
48 changes: 45 additions & 3 deletions tests/src/core/testqgstriangularmesh.cpp
Expand Up @@ -23,6 +23,7 @@
#include "qgstriangularmesh.h"
#include "qgsapplication.h"
#include "qgsproject.h"
#include "qgis.h"

/**
* \ingroup UnitTests
Expand All @@ -43,6 +44,8 @@ class TestQgsTriangularMesh : public QObject

void test_triangulate();

void test_centroids();

private:
void populateMeshVertices( QgsTriangularMesh &mesh );

Expand All @@ -66,9 +69,6 @@ void TestQgsTriangularMesh::initTestCase()
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();



}

void TestQgsTriangularMesh::cleanupTestCase()
Expand Down Expand Up @@ -125,5 +125,47 @@ void TestQgsTriangularMesh::test_triangulate()
}
}

void TestQgsTriangularMesh::test_centroids()
{
QgsTriangularMesh triangularMesh;

QgsMesh nativeMesh;
nativeMesh.vertices << QgsMeshVertex( 0, 10, 0 ) << QgsMeshVertex( 10, 10, 0 ) << QgsMeshVertex( 10, 0, 0 ) << QgsMeshVertex( 0, 0, 0 )
<< QgsMeshVertex( 20, 0, 0 ) << QgsMeshVertex( 30, 10, 0 ) << QgsMeshVertex( 20, 10, 0 );

nativeMesh.faces << QgsMeshFace( {0, 1, 2, 3} ) << QgsMeshFace( {1, 2, 4, 5} );

triangularMesh.update( &nativeMesh, QgsCoordinateTransform() );

QVector<QgsMeshVertex> centroids = triangularMesh.faceCentroids();

QCOMPARE( 2, centroids.count() );

QVERIFY( qgsDoubleNear( centroids.at( 0 ).x(), 5, 0.00001 ) );
QVERIFY( qgsDoubleNear( centroids.at( 0 ).y(), 5, 0.00001 ) );
QVERIFY( qgsDoubleNear( centroids.at( 1 ).x(), 17.777777777, 0.00001 ) );
QVERIFY( qgsDoubleNear( centroids.at( 1 ).y(), 5.5555555555, 0.00001 ) );

// with big coordinates
nativeMesh.clear();
triangularMesh = QgsTriangularMesh();

nativeMesh.vertices << QgsMeshVertex( 900000000, 300000010, 0 ) << QgsMeshVertex( 900000010, 300000010, 0 ) << QgsMeshVertex( 900000010, 300000000, 0 ) << QgsMeshVertex( 900000000, 300000000, 0 )
<< QgsMeshVertex( 900000020, 300000000, 0 ) << QgsMeshVertex( 900000030, 300000010, 0 ) << QgsMeshVertex( 900000020, 300000010, 0 );

nativeMesh.faces << QgsMeshFace( {0, 1, 2, 3} ) << QgsMeshFace( {1, 2, 4, 5} );
triangularMesh.update( &nativeMesh, QgsCoordinateTransform() );

centroids = triangularMesh.faceCentroids();

QCOMPARE( 2, centroids.count() );

QVERIFY( qgsDoubleNear( centroids.at( 0 ).x(), 900000005, 0.00001 ) );
QVERIFY( qgsDoubleNear( centroids.at( 0 ).y(), 300000005, 0.00001 ) );
QVERIFY( qgsDoubleNear( centroids.at( 1 ).x(), 900000017.777777, 0.00001 ) );
QVERIFY( qgsDoubleNear( centroids.at( 1 ).y(), 300000005.555555, 0.00001 ) );

}

QGSTEST_MAIN( TestQgsTriangularMesh )
#include "testqgstriangularmesh.moc"

0 comments on commit ee6d797

Please sign in to comment.