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