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