QGIS API Documentation  3.4.15-Madeira (e83d02e274)
labelposition.h
Go to the documentation of this file.
1 /*
2  * libpal - Automated Placement of Labels Library
3  *
4  * Copyright (C) 2008 Maxence Laurent, MIS-TIC, HEIG-VD
5  * University of Applied Sciences, Western Switzerland
6  * http://www.hes-so.ch
7  *
8  * Contact:
9  * maxence.laurent <at> heig-vd <dot> ch
10  * or
11  * eric.taillard <at> heig-vd <dot> ch
12  *
13  * This file is part of libpal.
14  *
15  * libpal is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * libpal is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with libpal. If not, see <http://www.gnu.org/licenses/>.
27  *
28  */
29 
30 #ifndef LABELPOSITION_H
31 #define LABELPOSITION_H
32 
33 #define SIP_NO_FILE
34 
35 
36 #include "qgis_core.h"
37 #include "pointset.h"
38 #include "rtree.hpp"
39 #include <fstream>
40 
41 namespace pal
42 {
43 
44  class FeaturePart;
45  class Pal;
46  class Label;
47 
48 
55  class CORE_EXPORT LabelPosition : public PointSet
56  {
57  friend class CostCalculator;
58  friend class PolygonCostCalculator;
59 
60  public:
61 
65  enum Quadrant
66  {
75  QuadrantBelowRight
76  };
77 
92  LabelPosition( int id, double x1, double y1,
93  double w, double h,
94  double alpha, double cost,
95  FeaturePart *feature, bool isReversed = false, Quadrant quadrant = QuadrantOver );
96 
98  LabelPosition( const LabelPosition &other );
99 
100  ~LabelPosition() override { delete nextPart; }
101 
107  bool isIn( double *bbox );
108 
114  bool isIntersect( double *bbox );
115 
119  bool intersects( const GEOSPreparedGeometry *geometry );
120 
124  bool within( const GEOSPreparedGeometry *geometry );
125 
131  bool isInside( double *bbox );
132 
139  bool isInConflict( LabelPosition *ls );
140 
142  void getBoundingBox( double amin[2], double amax[2] ) const;
143 
145  double getDistanceToPoint( double xp, double yp ) const;
146 
148  bool crossesLine( PointSet *line ) const;
149 
151  bool crossesBoundary( PointSet *polygon ) const;
152 
157  int polygonIntersectionCost( PointSet *polygon ) const;
158 
162  bool intersectsWithPolygon( PointSet *polygon ) const;
163 
165  void offsetPosition( double xOffset, double yOffset );
166 
170  int getId() const;
171 
172 
176  FeaturePart *getFeaturePart();
177 
178  int getNumOverlaps() const { return nbOverlap; }
179  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
180 
181  int getProblemFeatureId() const { return probFeat; }
182 
186  void setProblemIds( int probFid, int lpId )
187  {
188  probFeat = probFid;
189  id = lpId;
190  if ( nextPart ) nextPart->setProblemIds( probFid, lpId );
191  }
192 
197  double cost() const { return mCost; }
198 
204  void setCost( double newCost ) { mCost = newCost; }
205 
212  void setConflictsWithObstacle( bool conflicts );
213 
218  bool conflictsWithObstacle() const { return mHasObstacleConflict; }
219 
221  void validateCost();
222 
227  double getX( int i = 0 ) const;
228 
233  double getY( int i = 0 ) const;
234 
235  double getWidth() const { return w; }
236  double getHeight() const { return h; }
237 
241  double getAlpha() const;
242  bool getReversed() const { return reversed; }
243  bool getUpsideDown() const { return upsideDown; }
244 
245  Quadrant getQuadrant() const { return quadrant; }
246  LabelPosition *getNextPart() const { return nextPart; }
247  void setNextPart( LabelPosition *next ) { nextPart = next; }
248 
249  // -1 if not multi-part
250  int getPartId() const { return partId; }
251  void setPartId( int id ) { partId = id; }
252 
254  int incrementUpsideDownCharCount() { return ++mUpsideDownCharCount; }
255 
257  int upsideDownCharCount() const { return mUpsideDownCharCount; }
258 
259  void removeFromIndex( RTree<LabelPosition *, double, 2, double> *index );
260  void insertIntoIndex( RTree<LabelPosition *, double, 2, double> *index );
261 
262  typedef struct
263  {
264  Pal *pal = nullptr;
265  FeaturePart *obstacle = nullptr;
266  } PruneCtx;
267 
269  static bool pruneCallback( LabelPosition *candidatePosition, void *ctx );
270 
271  // for counting number of overlaps
272  typedef struct
273  {
274  LabelPosition *lp = nullptr;
275  int *nbOv = nullptr;
276  double *cost = nullptr;
277  double *inactiveCost = nullptr;
278  //int *feat;
279  } CountContext;
280 
281  /*
282  * count overlap, ctx = p_lp
283  */
284  static bool countOverlapCallback( LabelPosition *lp, void *ctx );
285 
286  static bool countFullOverlapCallback( LabelPosition *lp, void *ctx );
287 
288  static bool removeOverlapCallback( LabelPosition *lp, void *ctx );
289 
290  // for polygon cost calculation
291  static bool polygonObstacleCallback( pal::FeaturePart *obstacle, void *ctx );
292 
293  protected:
294 
295  int id;
296 
297  FeaturePart *feature = nullptr;
298 
299  // bug # 1 (maxence 10/23/2008)
300  int probFeat;
301 
303 
304  double alpha;
305  double w;
306  double h;
307 
308  LabelPosition *nextPart = nullptr;
309  int partId;
310 
311  //True if label direction is the same as line / polygon ring direction.
312  //Could be used by the application to draw a directional arrow ('<' or '>')
313  //if the layer arrangement is P_LINE
314  bool reversed;
315 
317 
319 
320  bool isInConflictSinglePart( LabelPosition *lp );
321  bool isInConflictMultiPart( LabelPosition *lp );
322 
323  private:
324  double mCost;
325  bool mHasObstacleConflict;
326  int mUpsideDownCharCount;
327 
331  int partCount() const;
332 
337  double polygonIntersectionCostForParts( PointSet *polygon ) const;
338 
339  };
340 
341 } // end namespace
342 
343 #endif
int upsideDownCharCount() const
Returns the number of upside down characters for this label position.
double getWidth() const
void setCost(double newCost)
Sets the candidate label position&#39;s geographical cost.
int incrementUpsideDownCharCount()
Increases the count of upside down characters for this label position.
double cost() const
Returns the candidate label position&#39;s geographical cost.
Main Pal labeling class.
Definition: pal.h:87
bool conflictsWithObstacle() const
Returns whether the position is marked as conflicting with an obstacle feature.
int getProblemFeatureId() const
Quadrant getQuadrant() const
LabelPosition * getNextPart() const
Main class to handle feature.
Definition: feature.h:96
bool getReversed() const
void setPartId(int id)
bool getUpsideDown() const
void setNextPart(LabelPosition *next)
int getPartId() const
double getHeight() const
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
int getNumOverlaps() const
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:65
void setProblemIds(int probFid, int lpId)
Set problem feature ID and assigned label candidate ID.
Data structure to compute polygon&#39;s candidates costs.
LabelPosition::Quadrant quadrant
~LabelPosition() override