QGIS API Documentation 3.27.0-Master (0a97e3138f)
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 this->Insert(
63 {
64 scaledBounds[0], scaledBounds[ 1]
65 },
66 {
67 scaledBounds[2], scaledBounds[3]
68 },
69 data );
70 }
71
78 void remove( T *data, const QgsRectangle &bounds )
79 {
80 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
81 this->Remove(
82 {
83 scaledBounds[0], scaledBounds[ 1]
84 },
85 {
86 scaledBounds[2], scaledBounds[3]
87 },
88 data );
89 }
90
96 bool intersects( const QgsRectangle &bounds, const std::function< bool( T *data )> &callback ) const
97 {
98 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
99 this->Search(
100 {
101 scaledBounds[0], scaledBounds[ 1]
102 },
103 {
104 scaledBounds[2], scaledBounds[3]
105 },
106 callback );
107 return true;
108 }
109
110 private:
111
112 // Coordinates are scaled inside the index so that they cover the maximum range for float values
113 double mXMin = 0;
114 double mYMin = 0;
115 double mXRes = 1;
116 double mYRes = 1;
117 const QgsRectangle mMaxBounds;
118 std::array<float, 4> scaleBounds( const QgsRectangle &bounds ) const
119 {
120 return
121 {
122 static_cast< float >( ( std::max( bounds.xMinimum(), mMaxBounds.xMinimum() ) - mXMin ) / mXRes ),
123 static_cast< float >( ( std::max( bounds.yMinimum(), mMaxBounds.yMinimum() ) - mYMin ) / mYRes ),
124 static_cast< float >( ( std::min( bounds.xMaximum(), mMaxBounds.xMaximum() ) - mXMin ) / mXRes ),
125 static_cast< float >( ( std::min( bounds.yMaximum(), mMaxBounds.yMaximum() ) - mYMin ) / mYRes )
126 };
127 }
128};
129
130#endif
131
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:78
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:96
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:183
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198