QGIS API Documentation  2.14.0-Essen
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 "pointset.h"
34 #include "rtree.hpp"
35 #include <fstream>
36 
37 namespace pal
38 {
39 
40  class FeaturePart;
41  class Pal;
42  class Label;
43 
44 
50  class CORE_EXPORT LabelPosition : public PointSet
51  {
52  friend class CostCalculator;
53  friend class PolygonCostCalculator;
54 
55  public:
56 
60  enum Quadrant
61  {
70  QuadrantBelowRight
71  };
72 
87  LabelPosition( int id, double x1, double y1,
88  double w, double h,
89  double alpha, double cost,
90  FeaturePart *feature, bool isReversed = false, Quadrant quadrant = QuadrantOver );
91 
93  LabelPosition( const LabelPosition& other );
94 
95  ~LabelPosition() { delete nextPart; }
96 
102  bool isIn( double *bbox );
103 
109  bool isIntersect( double *bbox );
110 
116  bool isInside( double *bbox );
117 
124  bool isInConflict( LabelPosition *ls );
125 
127  void getBoundingBox( double amin[2], double amax[2] ) const;
128 
130  double getDistanceToPoint( double xp, double yp ) const;
131 
133  bool crossesLine( PointSet* line ) const;
134 
136  bool crossesBoundary( PointSet* polygon ) const;
137 
141  int polygonIntersectionCost( PointSet* polygon ) const;
142 
145  bool intersectsWithPolygon( PointSet* polygon ) const;
146 
148  void offsetPosition( double xOffset, double yOffset );
149 
153  int getId() const;
154 
155 
159  FeaturePart * getFeaturePart();
160 
161  int getNumOverlaps() const { return nbOverlap; }
162  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
163 
164  int getProblemFeatureId() const { return probFeat; }
167  void setProblemIds( int probFid, int lpId )
168  {
169  probFeat = probFid;
170  id = lpId;
171  if ( nextPart ) nextPart->setProblemIds( probFid, lpId );
172  }
173 
177  double cost() const { return mCost; }
178 
183  void setCost( double newCost ) { mCost = newCost; }
184 
190  void setConflictsWithObstacle( bool conflicts );
191 
195  bool conflictsWithObstacle() const { return mHasObstacleConflict; }
196 
198  void validateCost();
199 
204  double getX( int i = 0 ) const;
209  double getY( int i = 0 ) const;
210 
211  double getWidth() const { return w; }
212  double getHeight() const { return h; }
213 
218  double getAlpha() const;
219  bool getReversed() const { return reversed; }
220  bool getUpsideDown() const { return upsideDown; }
221 
222  Quadrant getQuadrant() const { return quadrant; }
223  LabelPosition* getNextPart() const { return nextPart; }
224  void setNextPart( LabelPosition* next ) { nextPart = next; }
225 
226  // -1 if not multi-part
227  int getPartId() const { return partId; }
228  void setPartId( int id ) { partId = id; }
229 
230 
231  void removeFromIndex( RTree<LabelPosition*, double, 2, double> *index );
232  void insertIntoIndex( RTree<LabelPosition*, double, 2, double> *index );
233 
234  typedef struct
235  {
238  } PruneCtx;
239 
241  static bool pruneCallback( LabelPosition *candidatePosition, void *ctx );
242 
243  // for counting number of overlaps
244  typedef struct
245  {
247  int *nbOv;
248  double *cost;
249  double *inactiveCost;
250  //int *feat;
251  } CountContext;
252 
253  /*
254  * count overlap, ctx = p_lp
255  */
256  static bool countOverlapCallback( LabelPosition *lp, void *ctx );
257 
258  static bool countFullOverlapCallback( LabelPosition *lp, void *ctx );
259 
260  static bool removeOverlapCallback( LabelPosition *lp, void *ctx );
261 
262  // for polygon cost calculation
263  static bool polygonObstacleCallback( pal::FeaturePart *obstacle, void *ctx );
264 
265  protected:
266 
267  int id;
268 
270 
271  // bug # 1 (maxence 10/23/2008)
272  int probFeat;
273 
275 
276  double alpha;
277  double w;
278  double h;
279 
281  int partId;
282 
283  //True if label direction is the same as line / polygon ring direction.
284  //Could be used by the application to draw a directional arrow ('<' or '>')
285  //if the layer arrangement is P_LINE
286  bool reversed;
287 
289 
291 
292  bool isInConflictSinglePart( LabelPosition* lp );
293  bool isInConflictMultiPart( LabelPosition* lp );
294 
295  private:
296  double mCost;
297  bool mHasObstacleConflict;
298 
301  int partCount() const;
302 
306  double polygonIntersectionCostForParts( PointSet* polygon ) const;
307 
308  };
309 
310 } // end namespace
311 
312 #endif
FeaturePart * feature
static unsigned index
double getWidth() const
void setCost(double newCost)
Sets the candidate label position&#39;s geographical cost.
double cost() const
Returns the candidate label position&#39;s geographical cost.
Main Pal labelling class.
Definition: pal.h:84
bool conflictsWithObstacle() const
Returns whether the position is marked as conflicting with an obstacle feature.
int getProblemFeatureId() const
LabelPosition * nextPart
Quadrant getQuadrant() const
LabelPosition * getNextPart() const
Main class to handle feature.
Definition: feature.h:90
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:50
int getNumOverlaps() const
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:60
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