QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
48  class CORE_EXPORT LabelPosition : public PointSet
49  {
50  friend class CostCalculator;
51  friend class PolygonCostCalculator;
52 
53  public:
54 
58  enum Quadrant
59  {
68  QuadrantBelowRight
69  };
70 
85  LabelPosition( int id, double x1, double y1,
86  double w, double h,
87  double alpha, double cost,
88  FeaturePart *feature, bool isReversed = false, Quadrant quadrant = QuadrantOver );
89 
91  LabelPosition( const LabelPosition& other );
92 
93  ~LabelPosition() { delete nextPart; }
94 
95 
101  bool isIn( double *bbox );
102 
108  bool isIntersect( double *bbox );
109 
115  bool isInside( double *bbox );
116 
123  bool isInConflict( LabelPosition *ls );
124 
126  void getBoundingBox( double amin[2], double amax[2] ) const;
127 
129  double getDistanceToPoint( double xp, double yp ) const;
130 
132  bool isBorderCrossingLine( PointSet* line ) const;
133 
135  int getNumPointsInPolygon( PointSet* polygon ) const;
136 
138  void offsetPosition( double xOffset, double yOffset );
139 
143  int getId() const;
144 
145 
149  FeaturePart * getFeaturePart();
150 
151  double getNumOverlaps() const { return nbOverlap; }
152  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
153 
154  int getProblemFeatureId() const { return probFeat; }
157  void setProblemIds( int probFid, int lpId )
158  {
159  probFeat = probFid; id = lpId;
160  if ( nextPart ) nextPart->setProblemIds( probFid, lpId );
161  }
162 
164  QString getLayerName() const;
165 
170  double getCost() const;
171 
173  void setCost( double newCost ) { cost = newCost; }
174 
176  void validateCost();
177 
182  double getX( int i = 0 ) const;
187  double getY( int i = 0 ) const;
188 
189  double getWidth() const { return w; }
190  double getHeight() const { return h; }
191 
196  double getAlpha() const;
197  bool getReversed() const { return reversed; }
198  bool getUpsideDown() const { return upsideDown; }
199 
200  Quadrant getQuadrant() const { return quadrant; }
201 
202  void print();
203 
204  LabelPosition* getNextPart() const { return nextPart; }
205  void setNextPart( LabelPosition* next ) { nextPart = next; }
206 
207  // -1 if not multi-part
208  int getPartId() const { return partId; }
209  void setPartId( int id ) { partId = id; }
210 
211 
212  void removeFromIndex( RTree<LabelPosition*, double, 2, double> *index );
213  void insertIntoIndex( RTree<LabelPosition*, double, 2, double> *index );
214 
215  typedef struct
216  {
219  } PruneCtx;
220 
222  static bool pruneCallback( LabelPosition *lp, void *ctx );
223 
224  // for sorting
225  static bool costShrink( void *l, void *r );
226  static bool costGrow( void *l, void *r );
227 
228  // for counting number of overlaps
229  typedef struct
230  {
232  int *nbOv;
233  double *cost;
234  double *inactiveCost;
235  //int *feat;
236  } CountContext;
237 
238  /*
239  * count overlap, ctx = p_lp
240  */
241  static bool countOverlapCallback( LabelPosition *lp, void *ctx );
242 
243  static bool countFullOverlapCallback( LabelPosition *lp, void *ctx );
244 
245  static bool removeOverlapCallback( LabelPosition *lp, void *ctx );
246 
247  // for polygon cost calculation
248  static bool polygonObstacleCallback( pal::FeaturePart *obstacle, void *ctx );
249 
250  protected:
251 
252  int id;
253  double cost;
255 
256  // bug # 1 (maxence 10/23/2008)
257  int probFeat;
258 
260 
261  double alpha;
262  double w;
263  double h;
264 
266  int partId;
267 
268  //True if label direction is the same as line / polygon ring direction.
269  //Could be used by the application to draw a directional arrow ('<' or '>')
270  //if the layer arrangement is P_LINE
271  bool reversed;
272 
274 
276 
277  bool isInConflictSinglePart( LabelPosition* lp );
278  bool isInConflictMultiPart( LabelPosition* lp );
279 
280  };
281 
282 } // end namespace
283 
284 #endif
FeaturePart * feature
static unsigned index
double getWidth() const
void setCost(double newCost)
Modify candidate's cost.
Pal main class.
Definition: pal.h:109
int getProblemFeatureId() const
LabelPosition * nextPart
Quadrant getQuadrant() const
LabelPosition * getNextPart() const
Main class to handle feature.
Definition: feature.h:203
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:48
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:58
double getNumOverlaps() const
void setProblemIds(int probFid, int lpId)
Set problem feature ID and assigned label candidate ID.
Data structure to compute polygon's candidates costs.
LabelPosition::Quadrant quadrant