QGIS API Documentation  2.99.0-Master (5753576)
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 #include "qgis_core.h"
34 #include "pointset.h"
35 #include "rtree.hpp"
36 #include <fstream>
37 
38 namespace pal
39 {
40 
41  class FeaturePart;
42  class Pal;
43  class Label;
44 
45 
52  class CORE_EXPORT LabelPosition : public PointSet
53  {
54  friend class CostCalculator;
55  friend class PolygonCostCalculator;
56 
57  public:
58 
62  enum Quadrant
63  {
72  QuadrantBelowRight
73  };
74 
89  LabelPosition( int id, double x1, double y1,
90  double w, double h,
91  double alpha, double cost,
92  FeaturePart *feature, bool isReversed = false, Quadrant quadrant = QuadrantOver );
93 
95  LabelPosition( const LabelPosition& other );
96 
97  ~LabelPosition() { delete nextPart; }
98 
104  bool isIn( double *bbox );
105 
111  bool isIntersect( double *bbox );
112 
118  bool isInside( double *bbox );
119 
126  bool isInConflict( LabelPosition *ls );
127 
129  void getBoundingBox( double amin[2], double amax[2] ) const;
130 
132  double getDistanceToPoint( double xp, double yp ) const;
133 
135  bool crossesLine( PointSet* line ) const;
136 
138  bool crossesBoundary( PointSet* polygon ) const;
139 
143  int polygonIntersectionCost( PointSet* polygon ) const;
144 
147  bool intersectsWithPolygon( PointSet* polygon ) const;
148 
150  void offsetPosition( double xOffset, double yOffset );
151 
155  int getId() const;
156 
157 
161  FeaturePart * getFeaturePart();
162 
163  int getNumOverlaps() const { return nbOverlap; }
164  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
165 
166  int getProblemFeatureId() const { return probFeat; }
167 
170  void setProblemIds( int probFid, int lpId )
171  {
172  probFeat = probFid;
173  id = lpId;
174  if ( nextPart ) nextPart->setProblemIds( probFid, lpId );
175  }
176 
180  double cost() const { return mCost; }
181 
186  void setCost( double newCost ) { mCost = newCost; }
187 
193  void setConflictsWithObstacle( bool conflicts );
194 
198  bool conflictsWithObstacle() const { return mHasObstacleConflict; }
199 
201  void validateCost();
202 
207  double getX( int i = 0 ) const;
208 
213  double getY( int i = 0 ) const;
214 
215  double getWidth() const { return w; }
216  double getHeight() const { return h; }
217 
222  double getAlpha() const;
223  bool getReversed() const { return reversed; }
224  bool getUpsideDown() const { return upsideDown; }
225 
226  Quadrant getQuadrant() const { return quadrant; }
227  LabelPosition* getNextPart() const { return nextPart; }
228  void setNextPart( LabelPosition* next ) { nextPart = next; }
229 
230  // -1 if not multi-part
231  int getPartId() const { return partId; }
232  void setPartId( int id ) { partId = id; }
233 
235  int incrementUpsideDownCharCount() { return ++mUpsideDownCharCount; }
236 
238  int upsideDownCharCount() const { return mUpsideDownCharCount; }
239 
240  void removeFromIndex( RTree<LabelPosition*, double, 2, double> *index );
241  void insertIntoIndex( RTree<LabelPosition*, double, 2, double> *index );
242 
243  typedef struct
244  {
247  } PruneCtx;
248 
250  static bool pruneCallback( LabelPosition *candidatePosition, void *ctx );
251 
252  // for counting number of overlaps
253  typedef struct
254  {
256  int *nbOv;
257  double *cost;
258  double *inactiveCost;
259  //int *feat;
260  } CountContext;
261 
262  /*
263  * count overlap, ctx = p_lp
264  */
265  static bool countOverlapCallback( LabelPosition *lp, void *ctx );
266 
267  static bool countFullOverlapCallback( LabelPosition *lp, void *ctx );
268 
269  static bool removeOverlapCallback( LabelPosition *lp, void *ctx );
270 
271  // for polygon cost calculation
272  static bool polygonObstacleCallback( pal::FeaturePart *obstacle, void *ctx );
273 
274  protected:
275 
276  int id;
277 
279 
280  // bug # 1 (maxence 10/23/2008)
281  int probFeat;
282 
284 
285  double alpha;
286  double w;
287  double h;
288 
290  int partId;
291 
292  //True if label direction is the same as line / polygon ring direction.
293  //Could be used by the application to draw a directional arrow ('<' or '>')
294  //if the layer arrangement is P_LINE
295  bool reversed;
296 
298 
300 
301  bool isInConflictSinglePart( LabelPosition* lp );
302  bool isInConflictMultiPart( LabelPosition* lp );
303 
304  private:
305  double mCost;
306  bool mHasObstacleConflict;
307  int mUpsideDownCharCount;
308 
311  int partCount() const;
312 
316  double polygonIntersectionCostForParts( PointSet* polygon ) const;
317 
318  };
319 
320 } // end namespace
321 
322 #endif
FeaturePart * feature
static unsigned index
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.
Main Pal labeling class.
Definition: pal.h:85
int getProblemFeatureId() const
double cost() const
Returns the candidate label position&#39;s geographical cost.
LabelPosition * nextPart
double getHeight() const
LabelPosition * getNextPart() const
Quadrant getQuadrant() const
Main class to handle feature.
Definition: feature.h:92
int upsideDownCharCount() const
Returns the number of upside down characters for this label position.
void setPartId(int id)
void setNextPart(LabelPosition *next)
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:52
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:62
bool getUpsideDown() const
void setProblemIds(int probFid, int lpId)
Set problem feature ID and assigned label candidate ID.
bool getReversed() const
Data structure to compute polygon&#39;s candidates costs.
int getPartId() const
LabelPosition::Quadrant quadrant