QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
palrtree.h
Go to the documentation of this file.
1/***************************************************************************
2 parlrtree.h
3 ------------------------
4 Date : December 2019
5 Copyright : (C) 2019 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include <array>
17
18#include "RTree.h"
19#include "qgsrectangle.h"
20
21#define SIP_NO_FILE
22
23#ifndef QGSPALRTREE_H
24#define QGSPALRTREE_H
25
26
36template<typename T> class PalRtree : public RTree<T *, float, 2, float>
37{
38 public:
43 PalRtree( const QgsRectangle &maxBounds )
44 : mXMin( maxBounds.xMinimum() )
45 , mYMin( maxBounds.yMinimum() )
46 , mXRes( ( std::numeric_limits< float >::max() - 1 ) / ( maxBounds.xMaximum() - maxBounds.xMinimum() ) )
47 , mYRes( ( std::numeric_limits< float >::max() - 1 ) / ( maxBounds.yMaximum() - maxBounds.yMinimum() ) )
48 , mMaxBounds( maxBounds )
49 {}
50
57 void insert( T *data, const QgsRectangle &bounds )
58 {
59 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
60 const float aMin[2] { scaledBounds[0], scaledBounds[1] };
61 const float aMax[2] { scaledBounds[2], scaledBounds[3] };
62 this->Insert( aMin, aMax, data );
63 }
64
71 void remove( T *data, const QgsRectangle &bounds )
72 {
73 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
74 const float aMin[2] { scaledBounds[0], scaledBounds[1] };
75 const float aMax[2] { scaledBounds[2], scaledBounds[3] };
76 this->Remove( aMin, aMax, data );
77 }
78
84 bool intersects( const QgsRectangle &bounds, const std::function< bool( T *data )> &callback ) const
85 {
86 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
87 const float aMin[2] { scaledBounds[0], scaledBounds[1] };
88 const float aMax[2] { scaledBounds[2], scaledBounds[3] };
89 this->Search( aMin, aMax, callback );
90 return true;
91 }
92
93 private:
94 // Coordinates are scaled inside the index so that they cover the maximum range for float values
95 double mXMin = 0;
96 double mYMin = 0;
97 double mXRes = 1;
98 double mYRes = 1;
99 const QgsRectangle mMaxBounds;
100 std::array<float, 4> scaleBounds( const QgsRectangle &bounds ) const
101 {
102 return {
103 static_cast< float >( ( std::max( bounds.xMinimum(), mMaxBounds.xMinimum() ) - mXMin ) / mXRes ),
104 static_cast< float >( ( std::max( bounds.yMinimum(), mMaxBounds.yMinimum() ) - mYMin ) / mYRes ),
105 static_cast< float >( ( std::min( bounds.xMaximum(), mMaxBounds.xMaximum() ) - mXMin ) / mXRes ),
106 static_cast< float >( ( std::min( bounds.yMaximum(), mMaxBounds.yMaximum() ) - mYMin ) / mYRes )
107 };
108 }
109};
110
111#endif
void insert(T *data, const QgsRectangle &bounds)
Inserts new data into the spatial index, with the specified bounds.
Definition palrtree.h:57
void remove(T *data, const QgsRectangle &bounds)
Removes existing data from the spatial index, with the specified bounds.
Definition palrtree.h:71
PalRtree(const QgsRectangle &maxBounds)
Constructor for PalRtree.
Definition palrtree.h:43
bool intersects(const QgsRectangle &bounds, const std::function< bool(T *data)> &callback) const
Performs an intersection check against the index, for data intersecting the specified bounds.
Definition palrtree.h:84
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum