From 14fccd1121bb59ead29159af4f90bdaae90eba1d Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 19 Jul 2023 11:58:57 +1000 Subject: [PATCH] Improve 2d geometry representation of QgsTiledMeshNodeBoundingVolumeBox --- .../tiledmesh/qgstiledmeshboundingvolume.cpp | 26 +++++-------------- .../python/test_qgstiledmeshboundingvolume.py | 6 ++--- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/src/core/tiledmesh/qgstiledmeshboundingvolume.cpp b/src/core/tiledmesh/qgstiledmeshboundingvolume.cpp index 80c5a49759f3..885b655e3749 100644 --- a/src/core/tiledmesh/qgstiledmeshboundingvolume.cpp +++ b/src/core/tiledmesh/qgstiledmeshboundingvolume.cpp @@ -19,6 +19,8 @@ #include "qgscircle.h" #include "qgscoordinatetransform.h" #include "qgsvector3d.h" +#include "qgsmultipoint.h" +#include "qgsgeos.h" QgsAbstractTiledMeshNodeBoundingVolume::~QgsAbstractTiledMeshNodeBoundingVolume() = default; @@ -163,31 +165,15 @@ QgsAbstractGeometry *QgsTiledMeshNodeBoundingVolumeBox::as2DGeometry( const QgsC y.append( corners[i].y() ); z.append( corners[i].z() ); } + if ( transform.isValid() && !transform.isShortCircuited() ) { transform.transformInPlace( x, y, z, direction ); } - const QVector x2DCenter - { - 0.5 * ( x[0] + x[4] ), - 0.5 * ( x[1] + x[5] ), - 0.5 * ( x[2] + x[6] ), - 0.5 * ( x[3] + x[7] ), - 0.5 * ( x[0] + x[4] ), - }; - const QVector y2DCenter - { - 0.5 * ( y[0] + y[4] ), - 0.5 * ( y[1] + y[5] ), - 0.5 * ( y[2] + y[6] ), - 0.5 * ( y[3] + y[7] ), - 0.5 * ( y[0] + y[4] ), - }; - - std::unique_ptr< QgsLineString > ext = std::make_unique< QgsLineString >( x2DCenter, y2DCenter ); - polygon->setExteriorRing( ext.release() ); - return polygon.release(); + std::unique_ptr< QgsMultiPoint > mp = std::make_unique< QgsMultiPoint >( x, y ); + QgsGeos geosMp( mp.get() ); + return geosMp.convexHull(); } // diff --git a/tests/src/python/test_qgstiledmeshboundingvolume.py b/tests/src/python/test_qgstiledmeshboundingvolume.py index 2eb5dcf452e3..5406fc8dad32 100644 --- a/tests/src/python/test_qgstiledmeshboundingvolume.py +++ b/tests/src/python/test_qgstiledmeshboundingvolume.py @@ -94,7 +94,7 @@ def test_box(self): self.assertEqual(bounds.zMaximum(), 33) geometry_2d = volume.as2DGeometry() - self.assertEqual(geometry_2d.asWkt(), 'Polygon ((11 22, -9 22, 11 -18, -9 -18, 11 22))') + self.assertEqual(geometry_2d.asWkt(), 'Polygon ((-9 -18, -9 22, 11 22, 11 -18, -9 -18))') # with transform volume = QgsTiledMeshNodeBoundingVolumeBox(QgsOrientedBox3D([-4595750, 2698725, -3493318], @@ -113,8 +113,8 @@ def test_box(self): self.assertAlmostEqual(bounds.zMaximum(), 3153.6759909, 3) geometry_2d = volume.as2DGeometry(transform) - self.assertEqual(geometry_2d.asWkt(3), - 'Polygon ((149.558 -33.421, 149.569 -33.412, 149.586 -33.428, 149.597 -33.42, 149.558 -33.421))') + self.assertEqual(geometry_2d.asWkt(5), + 'Polygon ((149.58608 -33.44312, 149.55826 -33.43557, 149.55826 -33.40547, 149.56915 -33.39692, 149.59696 -33.40445, 149.59696 -33.43455, 149.58608 -33.44312))') def test_sphere(self): volume = QgsTiledMeshNodeBoundingVolumeSphere(QgsSphere(1, 2, 3, 10))