Skip to content
Permalink
Browse files

fix mesh centroid when coordinates are very big (#40757)

(cherry picked from commit ee6d797)
  • Loading branch information
vcloarec authored and nyalldawson committed Jan 1, 2021
1 parent d9497e8 commit 9a60b470025730d0dc4d8930c4ed67d63cc9514e
Showing with 56 additions and 6 deletions.
  1. +11 −3 src/core/mesh/qgstriangularmesh.cpp
  2. +45 −3 tests/src/core/testqgstriangularmesh.cpp
@@ -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 )
@@ -23,6 +23,7 @@
#include "qgstriangularmesh.h"
#include "qgsapplication.h"
#include "qgsproject.h"
#include "qgis.h"

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

void test_triangulate();

void test_centroids();

private:
void populateMeshVertices( QgsTriangularMesh &mesh );

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



}

void TestQgsTriangularMesh::cleanupTestCase()
@@ -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 9a60b47

Please sign in to comment.
You can’t perform that action at this time.