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 <fstream>
34 
35 #include "pointset.h"
36 #include "rtree.hpp"
37 
38 
39 namespace pal
40 {
41 
42  class FeaturePart;
43  class Pal;
44  class Label;
45 
46 
50  class CORE_EXPORT LabelPosition
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 
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 );
132 
134  bool isBorderCrossingLine( PointSet* feat );
135 
137  int getNumPointsInPolygon( int npol, double *xp, double *yp );
138 
140  void offsetPosition( double xOffset, double yOffset );
141 
145  int getId() const;
146 
147 
151  FeaturePart * getFeaturePart();
152 
153  double getNumOverlaps() const { return nbOverlap; }
154  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
155 
156  int getProblemFeatureId() const { return probFeat; }
159  void setProblemIds( int probFid, int lpId )
160  {
161  probFeat = probFid; id = lpId;
162  if ( nextPart ) nextPart->setProblemIds( probFid, lpId );
163  }
164 
166  char* getLayerName() const;
167 
172  double getCost() const;
173 
175  void setCost( double newCost ) { cost = newCost; }
176 
178  void validateCost();
179 
184  double getX( int i = 0 ) const;
189  double getY( int i = 0 ) const;
190 
191  double getWidth() const { return w; }
192  double getHeight() const { return h; }
193 
198  double getAlpha() const;
199  bool getReversed() const { return reversed; }
200  bool getUpsideDown() const { return upsideDown; }
201 
202  Quadrant getQuadrant() const { return quadrant; }
203 
204  void print();
205 
206  LabelPosition* getNextPart() const { return nextPart; }
207  void setNextPart( LabelPosition* next ) { nextPart = next; }
208 
209  // -1 if not multi-part
210  int getPartId() const { return partId; }
211  void setPartId( int id ) { partId = id; }
212 
213 
214  void removeFromIndex( RTree<LabelPosition*, double, 2, double> *index );
215  void insertIntoIndex( RTree<LabelPosition*, double, 2, double> *index );
216 
217  typedef struct
218  {
219  double scale;
222  } PruneCtx;
223 
225  static bool pruneCallback( LabelPosition *lp, void *ctx );
226 
227  // for sorting
228  static bool costShrink( void *l, void *r );
229  static bool costGrow( void *l, void *r );
230 
231  // for counting number of overlaps
232  typedef struct
233  {
235  int *nbOv;
236  double *cost;
237  double *inactiveCost;
238  //int *feat;
239  } CountContext;
240 
241  /*
242  * count overlap, ctx = p_lp
243  */
244  static bool countOverlapCallback( LabelPosition *lp, void *ctx );
245 
246  static bool countFullOverlapCallback( LabelPosition *lp, void *ctx );
247 
248  static bool removeOverlapCallback( LabelPosition *lp, void *ctx );
249 
250  // for polygon cost calculation
251  static bool polygonObstacleCallback( PointSet *feat, void *ctx );
252 
253  protected:
254 
255  int id;
256  double cost;
258 
259  // bug # 1 (maxence 10/23/2008)
260  int probFeat;
261 
263 
264  double x[4], y[4];
265  double alpha;
266  double w;
267  double h;
268 
270  int partId;
271 
272  //True if label direction is the same as line / polygon ring direction.
273  //Could be used by the application to draw a directional arrow ('<' or '>')
274  //if the layer arrangement is P_LINE
275  bool reversed;
276 
278 
280 
281  bool isInConflictSinglePart( LabelPosition* lp );
282  bool isInConflictMultiPart( LabelPosition* lp );
283 
284  };
285 
286 } // end namespace
287 
288 #endif
FeaturePart * feature
static unsigned index
double getWidth() const
void setCost(double newCost)
Modify candidate's cost.
Pal main class.
Definition: pal.h:121
int getProblemFeatureId() const
LabelPosition * nextPart
Quadrant getQuadrant() const
LabelPosition * getNextPart() const
Main class to handle feature.
Definition: feature.h:133
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
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:60
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