QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 #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 "pointset.h"
40 #include "rtree.hpp"
41 
42 
43 namespace pal
44 {
45 
46  class FeaturePart;
47  class Pal;
48  class Label;
49 
50 
54  class CORE_EXPORT LabelPosition
55  {
56  friend class CostCalculator;
57  friend class PolygonCostCalculator;
58 
59  public:
60 
64  enum Quadrant
65  {
74  QuadrantBelowRight
75  };
76 
77  protected:
78 
79  int id;
80  double cost;
82 
83  // bug # 1 (maxence 10/23/2008)
84  int probFeat;
85 
86  int nbOverlap;
87 
88  double x[4], y[4];
89  double alpha;
90  double w;
91  double h;
92 
94  int partId;
95 
96  //True if label direction is the same as line / polygon ring direction.
97  //Could be used by the application to draw a directional arrow ('<' or '>')
98  //if the layer arrangement is P_LINE
99  bool reversed;
100 
102 
104 
105  bool isInConflictSinglePart( LabelPosition* lp );
106  bool isInConflictMultiPart( LabelPosition* lp );
107 
108  public:
109 
124  LabelPosition( int id, double x1, double y1,
125  double w, double h,
126  double alpha, double cost,
127  FeaturePart *feature, bool isReversed = false, Quadrant quadrant = QuadrantOver );
128 
130  LabelPosition( const LabelPosition& other );
131 
132  ~LabelPosition() { delete nextPart; }
133 
134 
140  bool isIn( double *bbox );
141 
147  bool isIntersect( double *bbox );
148 
154  bool isInside( double *bbox );
155 
162  bool isInConflict( LabelPosition *ls );
163 
165  void getBoundingBox( double amin[2], double amax[2] ) const;
166 
168  double getDistanceToPoint( double xp, double yp );
169 
171  bool isBorderCrossingLine( PointSet* feat );
172 
174  int getNumPointsInPolygon( int npol, double *xp, double *yp );
175 
177  void offsetPosition( double xOffset, double yOffset );
178 
179 
183  int getId() const;
184 
185 
189  FeaturePart * getFeaturePart();
190 
191  double getNumOverlaps() const { return nbOverlap; }
192  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
193 
194  int getProblemFeatureId() const { return probFeat; }
197  void setProblemIds( int probFid, int lpId )
198  {
199  probFeat = probFid; id = lpId;
200  if ( nextPart ) nextPart->setProblemIds( probFid, lpId );
201  }
202 
204  char* getLayerName() const;
205 
210  double getCost() const;
211 
213  void setCost( double newCost ) { cost = newCost; }
214 
216  void validateCost();
217 
218 
223  double getX( int i = 0 ) const;
228  double getY( int i = 0 ) const;
229 
230  double getWidth() const { return w; }
231  double getHeight() const { return h; }
232 
237  double getAlpha() const;
238  bool getReversed() const { return reversed; }
239  bool getUpsideDown() const { return upsideDown; }
240 
241  Quadrant getQuadrant() const { return quadrant; }
242 
243  void print();
244 
245  LabelPosition* getNextPart() const { return nextPart; }
246  void setNextPart( LabelPosition* next ) { nextPart = next; }
247 
248  // -1 if not multi-part
249  int getPartId() const { return partId; }
250  void setPartId( int id ) { partId = id; }
251 
252 
253  void removeFromIndex( RTree<LabelPosition*, double, 2, double> *index );
254  void insertIntoIndex( RTree<LabelPosition*, double, 2, double> *index );
255 
256  typedef struct
257  {
258  double scale;
261  } PruneCtx;
262 
264  static bool pruneCallback( LabelPosition *lp, void *ctx );
265 
266  // for sorting
267  static bool costShrink( void *l, void *r );
268  static bool costGrow( void *l, void *r );
269 
270  // for counting number of overlaps
271  typedef struct
272  {
274  int *nbOv;
275  double *cost;
276  double *inactiveCost;
277  //int *feat;
278  } CountContext;
279 
280  /*
281  * count overlap, ctx = p_lp
282  */
283  static bool countOverlapCallback( LabelPosition *lp, void *ctx );
284 
285  static bool countFullOverlapCallback( LabelPosition *lp, void *ctx );
286 
287  static bool removeOverlapCallback( LabelPosition *lp, void *ctx );
288 
289  // for polygon cost calculation
290  static bool polygonObstacleCallback( PointSet *feat, void *ctx );
291 
292  };
293 
294 } // end namespac
295 
296 #endif
FeaturePart * feature
Definition: labelposition.h:81
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:93
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:54
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:64
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