QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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#ifndef QGSPALRTREE_H
22#define QGSPALRTREE_H
23
24#define SIP_NO_FILE
25
35template <typename T>
36class PalRtree : public RTree<T *, float, 2, float>
37{
38 public:
39
44 PalRtree( const QgsRectangle &maxBounds )
45 : mXMin( maxBounds.xMinimum() )
46 , mYMin( maxBounds.yMinimum() )
47 , mXRes( ( std::numeric_limits< float >::max() - 1 ) / ( maxBounds.xMaximum() - maxBounds.xMinimum() ) )
48 , mYRes( ( std::numeric_limits< float >::max() - 1 ) / ( maxBounds.yMaximum() - maxBounds.yMinimum() ) )
49 , mMaxBounds( maxBounds )
50 {
51
52 }
53
60 void insert( T *data, const QgsRectangle &bounds )
61 {
62 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
63 const float aMin[2]
64 {
65 scaledBounds[0], scaledBounds[ 1]
66 };
67 const float aMax[2]
68 {
69 scaledBounds[2], scaledBounds[ 3]
70 };
71 this->Insert(
72 aMin,
73 aMax,
74 data );
75 }
76
83 void remove( T *data, const QgsRectangle &bounds )
84 {
85 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
86 const float aMin[2]
87 {
88 scaledBounds[0], scaledBounds[ 1]
89 };
90 const float aMax[2]
91 {
92 scaledBounds[2], scaledBounds[ 3]
93 };
94 this->Remove(
95 aMin,
96 aMax,
97 data );
98 }
99
105 bool intersects( const QgsRectangle &bounds, const std::function< bool( T *data )> &callback ) const
106 {
107 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
108 const float aMin[2]
109 {
110 scaledBounds[0], scaledBounds[ 1]
111 };
112 const float aMax[2]
113 {
114 scaledBounds[2], scaledBounds[ 3]
115 };
116 this->Search(
117 aMin, aMax,
118 callback );
119 return true;
120 }
121
122 private:
123
124 // Coordinates are scaled inside the index so that they cover the maximum range for float values
125 double mXMin = 0;
126 double mYMin = 0;
127 double mXRes = 1;
128 double mYRes = 1;
129 const QgsRectangle mMaxBounds;
130 std::array<float, 4> scaleBounds( const QgsRectangle &bounds ) const
131 {
132 return
133 {
134 static_cast< float >( ( std::max( bounds.xMinimum(), mMaxBounds.xMinimum() ) - mXMin ) / mXRes ),
135 static_cast< float >( ( std::max( bounds.yMinimum(), mMaxBounds.yMinimum() ) - mYMin ) / mYRes ),
136 static_cast< float >( ( std::min( bounds.xMaximum(), mMaxBounds.xMaximum() ) - mXMin ) / mXRes ),
137 static_cast< float >( ( std::min( bounds.yMaximum(), mMaxBounds.yMaximum() ) - mYMin ) / mYRes )
138 };
139 }
140};
141
142#endif
143
void insert(T *data, const QgsRectangle &bounds)
Inserts new data into the spatial index, with the specified bounds.
Definition palrtree.h:60
void remove(T *data, const QgsRectangle &bounds)
Removes existing data from the spatial index, with the specified bounds.
Definition palrtree.h:83
PalRtree(const QgsRectangle &maxBounds)
Constructor for PalRtree.
Definition palrtree.h:44
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:105
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum