QGIS API Documentation  2.9.0-Master
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 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #ifndef _LABELPOSITION_H
35 #define _LABELPOSITION_H
36 
37 #include <fstream>
38 
39 #include "rtree.hpp"
40 
41 
42 namespace pal
43 {
44 
45  class FeaturePart;
46  class Pal;
47  class Label;
48 
49 
53  class CORE_EXPORT LabelPosition
54  {
55  friend class CostCalculator;
56  friend class PolygonCostCalculator;
57 
58  public:
59 
63  enum Quadrant
64  {
73  QuadrantBelowRight
74  };
75 
76  protected:
77 
78  int id;
79  double cost;
81 
82  // bug # 1 (maxence 10/23/2008)
83  int probFeat;
84 
85  int nbOverlap;
86 
87  double x[4], y[4];
88  double alpha;
89  double w;
90  double h;
91 
93  int partId;
94 
95  //True if label direction is the same as line / polygon ring direction.
96  //Could be used by the application to draw a directional arrow ('<' or '>')
97  //if the layer arrangement is P_LINE
98  bool reversed;
99 
101 
103 
104  bool isInConflictSinglePart( LabelPosition* lp );
105  bool isInConflictMultiPart( LabelPosition* lp );
106 
107  public:
108 
123  LabelPosition( int id, double x1, double y1,
124  double w, double h,
125  double alpha, double cost,
126  FeaturePart *feature, bool isReversed = false, Quadrant quadrant = QuadrantOver );
127 
129  LabelPosition( const LabelPosition& other );
130 
131  ~LabelPosition() { delete nextPart; }
132 
133 
139  bool isIn( double *bbox );
140 
146  bool isIntersect( double *bbox );
147 
153  bool isInside( double *bbox );
154 
161  bool isInConflict( LabelPosition *ls );
162 
164  void getBoundingBox( double amin[2], double amax[2] ) const;
165 
167  double getDistanceToPoint( double xp, double yp );
168 
170  bool isBorderCrossingLine( PointSet* feat );
171 
173  int getNumPointsInPolygon( int npol, double *xp, double *yp );
174 
176  void offsetPosition( double xOffset, double yOffset );
177 
178 
182  int getId() const;
183 
184 
188  FeaturePart * getFeaturePart();
189 
190  double getNumOverlaps() const { return nbOverlap; }
191  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
192 
193  int getProblemFeatureId() const { return probFeat; }
196  void setProblemIds( int probFid, int lpId )
197  {
198  probFeat = probFid; id = lpId;
199  if ( nextPart ) nextPart->setProblemIds( probFid, lpId );
200  }
201 
203  char* getLayerName() const;
204 
209  double getCost() const;
210 
212  void setCost( double newCost ) { cost = newCost; }
213 
215  void validateCost();
216 
217 
222  double getX( int i = 0 ) const;
227  double getY( int i = 0 ) const;
228 
229  double getWidth() const { return w; }
230  double getHeight() const { return h; }
231 
236  double getAlpha() const;
237  bool getReversed() const { return reversed; }
238  bool getUpsideDown() const { return upsideDown; }
239 
240  Quadrant getQuadrant() const { return quadrant; }
241 
242  void print();
243 
244  LabelPosition* getNextPart() const { return nextPart; }
245  void setNextPart( LabelPosition* next ) { nextPart = next; }
246 
247  // -1 if not multi-part
248  int getPartId() const { return partId; }
249  void setPartId( int id ) { partId = id; }
250 
251 
252  void removeFromIndex( RTree<LabelPosition*, double, 2, double> *index );
253  void insertIntoIndex( RTree<LabelPosition*, double, 2, double> *index );
254 
255  typedef struct
256  {
257  double scale;
260  } PruneCtx;
261 
263  static bool pruneCallback( LabelPosition *lp, void *ctx );
264 
265  // for sorting
266  static bool costShrink( void *l, void *r );
267  static bool costGrow( void *l, void *r );
268 
269  // for counting number of overlaps
270  typedef struct
271  {
273  int *nbOv;
274  double *cost;
275  double *inactiveCost;
276  //int *feat;
277  } CountContext;
278 
279  /*
280  * count overlap, ctx = p_lp
281  */
282  static bool countOverlapCallback( LabelPosition *lp, void *ctx );
283 
284  static bool countFullOverlapCallback( LabelPosition *lp, void *ctx );
285 
286  static bool removeOverlapCallback( LabelPosition *lp, void *ctx );
287 
288  // for polygon cost calculation
289  static bool polygonObstacleCallback( PointSet *feat, void *ctx );
290 
291  };
292 
293 } // end namespac
294 
295 #endif
FeaturePart * feature
Definition: labelposition.h:80
static unsigned index
double getWidth() const
void setCost(double newCost)
Modify candidate's cost.
Pal main class.
Definition: pal.h:126
int getProblemFeatureId() const
LabelPosition * nextPart
Definition: labelposition.h:92
Quadrant getQuadrant() const
LabelPosition * getNextPart() const
Main class to handle feature.
Definition: feature.h:138
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:53
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:63
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