Skip to content

Commit cf36172

Browse files
committedJan 15, 2020
bugfix: fix exporting of the mesh contours for polygons. now the exported polygons does not match triangular mesh
1 parent a2a8097 commit cf36172

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed
 

‎src/analysis/mesh/qgsmeshcontours.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,44 @@ QgsGeometry QgsMeshContours::exportPolygons(
196196
}
197197
else
198198
{
199-
// nothing here, we are outside the range
200-
continue;
199+
// last option we need to consider is that both min and max are between
200+
// value i and j, and in that case we need to calculate both point
201+
double value1 = max_value;
202+
double value2 = max_value;
203+
if ( values[i] < values[j] )
204+
{
205+
if ( ( min_value < values[i] ) || ( max_value > values[j] ) )
206+
{
207+
continue;
208+
}
209+
value1 = min_value;
210+
}
211+
else
212+
{
213+
if ( ( min_value < values[j] ) || ( max_value > values[i] ) )
214+
{
215+
continue;
216+
}
217+
value2 = min_value;
218+
}
219+
220+
const double fraction1 = ( value1 - values[i] ) / ( values[j] - values[i] );
221+
const QgsPoint xy1 = QgsGeometryUtils::interpolatePointOnLine( coords[i], coords[j], fraction1 );
222+
if ( !ring.contains( xy1 ) )
223+
ring.push_back( xy1 );
224+
225+
const double fraction2 = ( value2 - values[i] ) / ( values[j] - values[i] );
226+
const QgsPoint xy2 = QgsGeometryUtils::interpolatePointOnLine( coords[i], coords[j], fraction2 );
227+
if ( !ring.contains( xy2 ) )
228+
ring.push_back( xy2 );
201229
}
202230
}
203231
}
204232

205233
// add if the polygon is not degraded
206234
if ( ring.size() > 2 )
207235
{
208-
ring.push_back( coords[0] );
209-
std::unique_ptr< QgsLineString > ext = qgis::make_unique< QgsLineString> ( coords );
236+
std::unique_ptr< QgsLineString > ext = qgis::make_unique< QgsLineString> ( ring );
210237
std::unique_ptr< QgsPolygon > poly = qgis::make_unique< QgsPolygon >();
211238
poly->setExteriorRing( ext.release() );
212239
multiPolygon.push_back( QgsGeometry( std::move( poly ) ) );

‎tests/src/analysis/testqgsmeshcontours.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ void TestQgsMeshContours::testQuadAndTriangleVertexScalarPoly_data()
168168
QTest::addColumn< double >( "max_value" );
169169
QTest::addColumn< QgsGeometry >( "expected" );
170170

171-
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))" ) );
172-
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))" ) );
173-
QTest::newRow( "right" ) << 2.0 << 2.5 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((3000 2000 40, 2000 3000 50, 2000 2000 30, 3000 2000 40))" ) );
171+
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))" ) );
172+
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))" ) );
173+
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))" ) );
174+
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))" ) );
174175

175176
QTest::newRow( "outside_left_edge" ) << 0.0 << 1.0 << QgsGeometry();
176177
QTest::newRow( "only one point" ) << 3.0 << 3.0 << QgsGeometry();
@@ -198,9 +199,9 @@ void TestQgsMeshContours::testQuadAndTriangleFaceScalarPoly_data()
198199
QTest::addColumn< double >( "max_value" );
199200
QTest::addColumn< QgsGeometry >( "expected" );
200201

201-
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))" ) );
202-
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))" ) );
203-
QTest::newRow( "right" ) << 1.5 << 1.75 << QgsGeometry( QgsGeometryFactory::geomFromWkt( "PolygonZ ((3000 2000 40, 2000 3000 50, 2000 2000 30, 3000 2000 40))" ) );
202+
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))" ) );
203+
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))" ) );
204+
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))" ) );
204205

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

0 commit comments

Comments
 (0)
Please sign in to comment.