QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 "palrtree.h"
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 
105  bool isIn( double *bbox );
106 
112  bool isIntersect( double *bbox );
113 
117  bool intersects( const GEOSPreparedGeometry *geometry );
118 
122  bool within( const GEOSPreparedGeometry *geometry );
123 
129  bool isInside( double *bbox );
130 
137  bool isInConflict( const LabelPosition *ls ) const;
138 
140  void getBoundingBox( double amin[2], double amax[2] ) const;
141 
143  double getDistanceToPoint( double xp, double yp ) const;
144 
146  bool crossesLine( PointSet *line ) const;
147 
149  bool crossesBoundary( PointSet *polygon ) const;
150 
155  int polygonIntersectionCost( PointSet *polygon ) const;
156 
160  bool intersectsWithPolygon( PointSet *polygon ) const;
161 
163  void offsetPosition( double xOffset, double yOffset );
164 
168  int getId() const;
169 
170 
174  FeaturePart *getFeaturePart() const;
175 
176  int getNumOverlaps() const { return nbOverlap; }
177  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
178 
182  void incrementNumOverlaps() { nbOverlap++; }
183 
187  void decrementNumOverlaps() { nbOverlap++; }
188 
189  int getProblemFeatureId() const { return probFeat; }
190 
194  void setProblemIds( int probFid, int lpId )
195  {
196  probFeat = probFid;
197  id = lpId;
198  if ( mNextPart ) mNextPart->setProblemIds( probFid, lpId );
199  }
200 
205  double cost() const { return mCost; }
206 
212  void setCost( double newCost ) { mCost = newCost; }
213 
220  void setConflictsWithObstacle( bool conflicts );
221 
226  bool conflictsWithObstacle() const { return mHasObstacleConflict; }
227 
234  void setHasHardObstacleConflict( bool conflicts );
235 
242  bool hasHardObstacleConflict() const { return mHasHardConflict; }
243 
245  void validateCost();
246 
251  double getX( int i = 0 ) const;
252 
257  double getY( int i = 0 ) const;
258 
259  double getWidth() const { return w; }
260  double getHeight() const { return h; }
261 
265  double getAlpha() const;
266  bool getReversed() const { return reversed; }
267  bool getUpsideDown() const { return upsideDown; }
268 
269  Quadrant getQuadrant() const { return quadrant; }
270 
276  LabelPosition *nextPart() const { return mNextPart.get(); }
277 
283  void setNextPart( std::unique_ptr< LabelPosition > next ) { mNextPart = std::move( next ); }
284 
285  // -1 if not multi-part
286  int getPartId() const { return partId; }
287  void setPartId( int id ) { partId = id; }
288 
290  int incrementUpsideDownCharCount() { return ++mUpsideDownCharCount; }
291 
293  int upsideDownCharCount() const { return mUpsideDownCharCount; }
294 
298  void removeFromIndex( PalRtree<LabelPosition> &index );
299 
303  void insertIntoIndex( PalRtree<LabelPosition> &index );
304 
305  protected:
306 
307  int id;
308 
309  FeaturePart *feature = nullptr;
310 
311  // bug # 1 (maxence 10/23/2008)
312  int probFeat;
313 
315 
316  double alpha;
317  double w;
318  double h;
319 
320  int partId;
321 
322  //True if label direction is the same as line / polygon ring direction.
323  //Could be used by the application to draw a directional arrow ('<' or '>')
324  //if the layer arrangement is P_LINE
325  bool reversed;
326 
328 
330 
331  private:
332 
333  std::unique_ptr< LabelPosition > mNextPart;
334 
335  double mCost;
336  bool mHasObstacleConflict;
337  bool mHasHardConflict = false;
338  int mUpsideDownCharCount;
339 
343  int partCount() const;
344 
349  double polygonIntersectionCostForParts( PointSet *polygon ) const;
350 
351  bool isInConflictSinglePart( const LabelPosition *lp ) const;
352  bool isInConflictMultiPart( const LabelPosition *lp ) const;
353 
354  };
355 
356 } // end namespace
357 
358 #endif
LabelPosition * nextPart() const
Returns the next part of this label position (i.e.
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.
int getProblemFeatureId() const
A rtree spatial index for use in the pal labeling engine.
Definition: palrtree.h:35
double cost() const
Returns the candidate label position&#39;s geographical cost.
double getHeight() const
void decrementNumOverlaps()
Decreases the number of overlaps recorded against this position by 1.
Quadrant getQuadrant() const
Main class to handle feature.
Definition: feature.h:95
int upsideDownCharCount() const
Returns the number of upside down characters for this label position.
void setPartId(int id)
void incrementNumOverlaps()
Increases the number of overlaps recorded against this position by 1.
void setNextPart(std::unique_ptr< LabelPosition > next)
Sets the next part of this label position (i.e.
bool hasHardObstacleConflict() const
Returns whether the position is marked as having a hard conflict with an obstacle feature...
double getWidth() const
bool conflictsWithObstacle() const
Returns whether the position is marked as conflicting with an obstacle feature.
int getNumOverlaps() const
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:65
bool getUpsideDown() const
void setProblemIds(int probFid, int lpId)
Set problem feature ID and assigned label candidate ID.
bool getReversed() const
int getPartId() const
LabelPosition::Quadrant quadrant