Skip to content

Commit

Permalink
bugfix: fix exporting of the mesh contours for polygons. now the expo…
Browse files Browse the repository at this point in the history
…rted polygons does not match triangular mesh
  • Loading branch information
PeterPetrik committed Jan 15, 2020
1 parent a2a8097 commit cf36172
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
35 changes: 31 additions & 4 deletions src/analysis/mesh/qgsmeshcontours.cpp
Expand Up @@ -196,17 +196,44 @@ QgsGeometry QgsMeshContours::exportPolygons(
}
else
{
// nothing here, we are outside the range
continue;
// last option we need to consider is that both min and max are between
// value i and j, and in that case we need to calculate both point
double value1 = max_value;
double value2 = max_value;
if ( values[i] < values[j] )
{
if ( ( min_value < values[i] ) || ( max_value > values[j] ) )
{
continue;
}
value1 = min_value;
}
else
{
if ( ( min_value < values[j] ) || ( max_value > values[i] ) )
{
continue;
}
value2 = min_value;
}

const double fraction1 = ( value1 - values[i] ) / ( values[j] - values[i] );
const QgsPoint xy1 = QgsGeometryUtils::interpolatePointOnLine( coords[i], coords[j], fraction1 );
if ( !ring.contains( xy1 ) )
ring.push_back( xy1 );

const double fraction2 = ( value2 - values[i] ) / ( values[j] - values[i] );
const QgsPoint xy2 = QgsGeometryUtils::interpolatePointOnLine( coords[i], coords[j], fraction2 );
if ( !ring.contains( xy2 ) )
ring.push_back( xy2 );
}
}
}

// add if the polygon is not degraded
if ( ring.size() > 2 )
{
ring.push_back( coords[0] );
std::unique_ptr< QgsLineString > ext = qgis::make_unique< QgsLineString> ( coords );
std::unique_ptr< QgsLineString > ext = qgis::make_unique< QgsLineString> ( ring );
std::unique_ptr< QgsPolygon > poly = qgis::make_unique< QgsPolygon >();
poly->setExteriorRing( ext.release() );
multiPolygon.push_back( QgsGeometry( std::move( poly ) ) );
Expand Down
13 changes: 7 additions & 6 deletions tests/src/analysis/testqgsmeshcontours.cpp
Expand Up @@ -168,9 +168,10 @@ void TestQgsMeshContours::testQuadAndTriangleVertexScalarPoly_data()
QTest::addColumn< double >( "max_value" );
QTest::addColumn< QgsGeometry >( "expected" );

QTest::newRow( "left" ) << 1.0 << 1.5 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((1000 2000 20, 1000 3000 10, 2000 3000 50, 2000 2000 30, 1000 2000 20))" ) );
QTest::newRow( "middle" ) << 1.5 << 2.0 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((1000 2000 20, 1000 3000 10, 2000 3000 50, 2000 2000 30, 1000 2000 20))" ) );
QTest::newRow( "right" ) << 2.0 << 2.5 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((3000 2000 40, 2000 3000 50, 2000 2000 30, 3000 2000 40))" ) );
QTest::newRow( "left" ) << 1.0 << 1.5 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((1500 2500 35, 1500 2000 25, 1000 2000 20, 1000 3000 10, 1500 3000 30, 1500 2500 35))" ) );
QTest::newRow( "bothinsquare" ) << 1.2 << 1.4 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((1400 2400 32, 1400 2000 24, 1200 2000 22, 1200 2200 26, 1200 3000 18, 1400 3000 25.99999999999999645, 1400 2400 32))" ) );
QTest::newRow( "middle" ) << 1.5 << 2.0 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((2000 3000 50, 2000 2000 30, 1500 2000 25, 1500 2500 35, 1500 3000 30, 2000 3000 50))" ) );
QTest::newRow( "right" ) << 2.0 << 2.5 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((2500 2500 45, 2000 3000 50, 2000 2000 30, 2500 2000 35, 2500 2500 45))" ) );

QTest::newRow( "outside_left_edge" ) << 0.0 << 1.0 << QgsGeometry();
QTest::newRow( "only one point" ) << 3.0 << 3.0 << QgsGeometry();
Expand Down Expand Up @@ -198,9 +199,9 @@ void TestQgsMeshContours::testQuadAndTriangleFaceScalarPoly_data()
QTest::addColumn< double >( "max_value" );
QTest::addColumn< QgsGeometry >( "expected" );

QTest::newRow( "left" ) << 1.0 << 1.25 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((1000 2000 20, 1000 3000 10, 2000 3000 50, 2000 2000 30, 1000 2000 20))" ) );
QTest::newRow( "middle" ) << 1.25 << 1.5 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((1000 2000 20, 1000 3000 10, 2000 3000 50, 2000 2000 30, 1000 2000 20))" ) );
QTest::newRow( "right" ) << 1.5 << 1.75 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((3000 2000 40, 2000 3000 50, 2000 2000 30, 3000 2000 40))" ) );
QTest::newRow( "left" ) << 1.0 << 1.25 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((1500 2500 35, 1500 2000 25, 1000 2000 20, 1000 3000 10, 1500 3000 30, 1500 2500 35))" ) );
QTest::newRow( "middle" ) << 1.25 << 1.5 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((2000 3000 50, 2000 2000 30, 1500 2000 25, 1500 2500 35, 1500 3000 30, 2000 3000 50))" ) );
QTest::newRow( "right" ) << 1.5 << 1.75 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((2500 2500 45, 2000 3000 50, 2000 2000 30, 2500 2000 35, 2500 2500 45))" ) );

QTest::newRow( "only one point" ) << 2.0 << 2.0 << QgsGeometry();
QTest::newRow( "outside lower" ) << -3.0 << -4.0 << QgsGeometry();
Expand Down

0 comments on commit cf36172

Please sign in to comment.