QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
51  class CORE_EXPORT LabelPosition : public PointSet
52  {
53  friend class CostCalculator;
54  friend class PolygonCostCalculator;
55 
56  public:
57 
61  enum Quadrant
62  {
71  QuadrantBelowRight
72  };
73 
88  LabelPosition( int id, double x1, double y1,
89  double w, double h,
90  double alpha, double cost,
91  FeaturePart *feature, bool isReversed = false, Quadrant quadrant = QuadrantOver );
92 
94  LabelPosition( const LabelPosition& other );
95 
96  ~LabelPosition() { delete nextPart; }
97 
103  bool isIn( double *bbox );
104 
110  bool isIntersect( double *bbox );
111 
117  bool isInside( double *bbox );
118 
125  bool isInConflict( LabelPosition *ls );
126 
128  void getBoundingBox( double amin[2], double amax[2] ) const;
129 
131  double getDistanceToPoint( double xp, double yp ) const;
132 
134  bool crossesLine( PointSet* line ) const;
135 
137  bool crossesBoundary( PointSet* polygon ) const;
138 
142  int polygonIntersectionCost( PointSet* polygon ) const;
143 
146  bool intersectsWithPolygon( PointSet* polygon ) const;
147 
149  void offsetPosition( double xOffset, double yOffset );
150 
154  int getId() const;
155 
156 
160  FeaturePart * getFeaturePart();
161 
162  int getNumOverlaps() const { return nbOverlap; }
163  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
164 
165  int getProblemFeatureId() const { return probFeat; }
168  void setProblemIds( int probFid, int lpId )
169  {
170  probFeat = probFid;
171  id = lpId;
172  if ( nextPart ) nextPart->setProblemIds( probFid, lpId );
173  }
174 
178  double cost() const { return mCost; }
179 
184  void setCost( double newCost ) { mCost = newCost; }
185 
191  void setConflictsWithObstacle( bool conflicts );
192 
196  bool conflictsWithObstacle() const { return mHasObstacleConflict; }
197 
199  void validateCost();
200 
205  double getX( int i = 0 ) const;
210  double getY( int i = 0 ) const;
211 
212  double getWidth() const { return w; }
213  double getHeight() const { return h; }
214 
219  double getAlpha() const;
220  bool getReversed() const { return reversed; }
221  bool getUpsideDown() const { return upsideDown; }
222 
223  Quadrant getQuadrant() const { return quadrant; }
224  LabelPosition* getNextPart() const { return nextPart; }
225  void setNextPart( LabelPosition* next ) { nextPart = next; }
226 
227  // -1 if not multi-part
228  int getPartId() const { return partId; }
229  void setPartId( int id ) { partId = id; }
230 
232  int incrementUpsideDownCharCount() { return ++mUpsideDownCharCount; }
233 
235  int upsideDownCharCount() const { return mUpsideDownCharCount; }
236 
237  void removeFromIndex( RTree<LabelPosition*, double, 2, double> *index );
238  void insertIntoIndex( RTree<LabelPosition*, double, 2, double> *index );
239 
240  typedef struct
241  {
244  } PruneCtx;
245 
247  static bool pruneCallback( LabelPosition *candidatePosition, void *ctx );
248 
249  // for counting number of overlaps
250  typedef struct
251  {
253  int *nbOv;
254  double *cost;
255  double *inactiveCost;
256  //int *feat;
257  } CountContext;
258 
259  /*
260  * count overlap, ctx = p_lp
261  */
262  static bool countOverlapCallback( LabelPosition *lp, void *ctx );
263 
264  static bool countFullOverlapCallback( LabelPosition *lp, void *ctx );
265 
266  static bool removeOverlapCallback( LabelPosition *lp, void *ctx );
267 
268  // for polygon cost calculation
269  static bool polygonObstacleCallback( pal::FeaturePart *obstacle, void *ctx );
270 
271  protected:
272 
273  int id;
274 
276 
277  // bug # 1 (maxence 10/23/2008)
278  int probFeat;
279 
281 
282  double alpha;
283  double w;
284  double h;
285 
287  int partId;
288 
289  //True if label direction is the same as line / polygon ring direction.
290  //Could be used by the application to draw a directional arrow ('<' or '>')
291  //if the layer arrangement is P_LINE
292  bool reversed;
293 
295 
297 
298  bool isInConflictSinglePart( LabelPosition* lp );
299  bool isInConflictMultiPart( LabelPosition* lp );
300 
301  private:
302  double mCost;
303  bool mHasObstacleConflict;
304  int mUpsideDownCharCount;
305 
308  int partCount() const;
309 
313  double polygonIntersectionCostForParts( PointSet* polygon ) const;
314 
315  };
316 
317 } // end namespace
318 
319 #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 labelling class.
Definition: pal.h:84
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:91
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:51
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:61
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