From 90e4d2fa3e0d2f488f786859a4d69b17e7db7f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Bartoletti?= Date: Fri, 10 Jan 2025 15:31:54 +0100 Subject: [PATCH] chore(spatial index): Remove duplicate rectangleToRegion code --- src/core/mesh/qgsmeshspatialindex.cpp | 10 ++------- src/core/qgspointlocator.cpp | 29 ++++++++++----------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/core/mesh/qgsmeshspatialindex.cpp b/src/core/mesh/qgsmeshspatialindex.cpp index eca5d8463261..dde9fb8569be 100644 --- a/src/core/mesh/qgsmeshspatialindex.cpp +++ b/src/core/mesh/qgsmeshspatialindex.cpp @@ -15,6 +15,7 @@ #include "qgsmeshspatialindex.h" #include "qgsrectangle.h" +#include "qgsspatialindexutils.h" #include "qgslogger.h" #include "qgsfeedback.h" @@ -74,13 +75,6 @@ static Region edgeToRegion( const QgsMesh &mesh, int id, bool &ok ) return SpatialIndex::Region( pt1, pt2, 2 ); } -static Region rectToRegion( const QgsRectangle &rect ) -{ - double pt1[2] = { rect.xMinimum(), rect.yMinimum() }; - double pt2[2] = { rect.xMaximum(), rect.yMaximum() }; - return SpatialIndex::Region( pt1, pt2, 2 ); -} - /** * \ingroup core * \class QgisMeshVisitor @@ -376,7 +370,7 @@ QList QgsMeshSpatialIndex::intersects( const QgsRectangle &rect ) const QList list; QgisMeshVisitor visitor( list ); - const SpatialIndex::Region r = rectToRegion( rect ); + const SpatialIndex::Region r = QgsSpatialIndexUtils::rectangleToRegion( rect ); const QMutexLocker locker( &d->mMutex ); d->mRTree->intersectsWithQuery( r, visitor ); diff --git a/src/core/qgspointlocator.cpp b/src/core/qgspointlocator.cpp index 2528fdf792a1..b1a5ab905d0e 100644 --- a/src/core/qgspointlocator.cpp +++ b/src/core/qgspointlocator.cpp @@ -30,6 +30,7 @@ #include "qgscurvepolygon.h" #include "qgsrendercontext.h" #include "qgspointlocatorinittask.h" +#include "qgsspatialindexutils.h" #include #include @@ -46,14 +47,6 @@ static SpatialIndex::Point point2point( const QgsPointXY &point ) } -static SpatialIndex::Region rect2region( const QgsRectangle &rect ) -{ - double pLow[2] = { rect.xMinimum(), rect.yMinimum() }; - double pHigh[2] = { rect.xMaximum(), rect.yMaximum() }; - return SpatialIndex::Region( pLow, pHigh, 2 ); -} - - // Ahh.... another magic number. Taken from QgsVectorLayer::snapToGeometry() call to closestSegmentWithContext(). // The default epsilon used for sqrDistToSegment (1e-8) is too high when working with lat/lon coordinates // I still do not fully understand why the sqrDistToSegment() code uses epsilon and if the square distance @@ -1128,7 +1121,7 @@ bool QgsPointLocator::rebuildIndex( int maxFeaturesToIndex ) const QgsRectangle bbox = f.geometry().boundingBox(); if ( bbox.isFinite() ) { - SpatialIndex::Region r( rect2region( bbox ) ); + SpatialIndex::Region r( QgsSpatialIndexUtils::rectangleToRegion( bbox ) ); dataList << new RTree::Data( 0, nullptr, r, f.id() ); auto it = mGeoms.find( f.id() ); @@ -1262,7 +1255,7 @@ void QgsPointLocator::onFeatureAdded( QgsFeatureId fid ) const QgsRectangle bbox = f.geometry().boundingBox(); if ( bbox.isFinite() ) { - const SpatialIndex::Region r( rect2region( bbox ) ); + const SpatialIndex::Region r( QgsSpatialIndexUtils::rectangleToRegion( bbox ) ); mRTree->insertData( 0, nullptr, r, f.id() ); auto it = mGeoms.find( f.id() ); @@ -1301,7 +1294,7 @@ void QgsPointLocator::onFeatureDeleted( QgsFeatureId fid ) auto it = mGeoms.find( fid ); if ( it != mGeoms.end() ) { - mRTree->deleteData( rect2region( ( *it )->boundingBox() ), fid ); + mRTree->deleteData( QgsSpatialIndexUtils::rectangleToRegion( ( *it )->boundingBox() ), fid ); delete *it; mGeoms.erase( it ); } @@ -1335,7 +1328,7 @@ QgsPointLocator::Match QgsPointLocator::nearestVertex( const QgsPointXY &point, Match m; QgsPointLocator_VisitorNearestVertex visitor( this, m, point, filter ); const QgsRectangle rect( point.x() - tolerance, point.y() - tolerance, point.x() + tolerance, point.y() + tolerance ); - mRTree->intersectsWithQuery( rect2region( rect ), visitor ); + mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor ); if ( m.isValid() && m.distance() > tolerance ) return Match(); // make sure that only match strictly within the tolerance is returned return m; @@ -1350,7 +1343,7 @@ QgsPointLocator::Match QgsPointLocator::nearestCentroid( const QgsPointXY &point QgsPointLocator_VisitorNearestCentroid visitor( this, m, point, filter ); const QgsRectangle rect( point.x() - tolerance, point.y() - tolerance, point.x() + tolerance, point.y() + tolerance ); - mRTree->intersectsWithQuery( rect2region( rect ), visitor ); + mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor ); if ( m.isValid() && m.distance() > tolerance ) return Match(); // make sure that only match strictly within the tolerance is returned return m; @@ -1365,7 +1358,7 @@ QgsPointLocator::Match QgsPointLocator::nearestMiddleOfSegment( const QgsPointXY QgsPointLocator_VisitorNearestMiddleOfSegment visitor( this, m, point, filter ); const QgsRectangle rect( point.x() - tolerance, point.y() - tolerance, point.x() + tolerance, point.y() + tolerance ); - mRTree->intersectsWithQuery( rect2region( rect ), visitor ); + mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor ); if ( m.isValid() && m.distance() > tolerance ) return Match(); // make sure that only match strictly within the tolerance is returned return m; @@ -1380,7 +1373,7 @@ QgsPointLocator::Match QgsPointLocator::nearestLineEndpoints( const QgsPointXY & QgsPointLocator_VisitorNearestLineEndpoint visitor( this, m, point, filter ); const QgsRectangle rect( point.x() - tolerance, point.y() - tolerance, point.x() + tolerance, point.y() + tolerance ); - mRTree->intersectsWithQuery( rect2region( rect ), visitor ); + mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor ); if ( m.isValid() && m.distance() > tolerance ) return Match(); // make sure that only match strictly within the tolerance is returned return m; @@ -1398,7 +1391,7 @@ QgsPointLocator::Match QgsPointLocator::nearestEdge( const QgsPointXY &point, do Match m; QgsPointLocator_VisitorNearestEdge visitor( this, m, point, filter ); const QgsRectangle rect( point.x() - tolerance, point.y() - tolerance, point.x() + tolerance, point.y() + tolerance ); - mRTree->intersectsWithQuery( rect2region( rect ), visitor ); + mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor ); if ( m.isValid() && m.distance() > tolerance ) return Match(); // make sure that only match strictly within the tolerance is returned return m; @@ -1445,7 +1438,7 @@ QgsPointLocator::MatchList QgsPointLocator::edgesInRect( const QgsRectangle &rec MatchList lst; QgsPointLocator_VisitorEdgesInRect visitor( this, lst, rect, filter ); - mRTree->intersectsWithQuery( rect2region( rect ), visitor ); + mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor ); return lst; } @@ -1463,7 +1456,7 @@ QgsPointLocator::MatchList QgsPointLocator::verticesInRect( const QgsRectangle & MatchList lst; QgsPointLocator_VisitorVerticesInRect visitor( this, lst, rect, filter ); - mRTree->intersectsWithQuery( rect2region( rect ), visitor ); + mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor ); return lst; }