QGIS API Documentation  2.6.0-Brighton
 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 "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  protected:
59 
60  int id;
61  double cost;
63 
64  // bug # 1 (maxence 10/23/2008)
65  int probFeat;
66 
67  int nbOverlap;
68 
69  double x[4], y[4];
70  double alpha;
71  double w;
72  double h;
73 
75  int partId;
76 
77  //True if label direction is the same as line / polygon ring direction.
78  //Could be used by the application to draw a directional arrow ('<' or '>')
79  //if the layer arrangement is P_LINE
80  bool reversed;
81 
82  bool upsideDown;
83 
84  bool isInConflictSinglePart( LabelPosition* lp );
85  bool isInConflictMultiPart( LabelPosition* lp );
86 
87  public:
101  LabelPosition( int id, double x1, double y1,
102  double w, double h,
103  double alpha, double cost,
104  FeaturePart *feature, bool isReversed = false );
105 
107  LabelPosition( const LabelPosition& other );
108 
109  ~LabelPosition() { delete nextPart; }
110 
111 
117  bool isIn( double *bbox );
118 
124  bool isIntersect( double *bbox );
125 
131  bool isInside( double *bbox );
132 
139  bool isInConflict( LabelPosition *ls );
140 
142  void getBoundingBox( double amin[2], double amax[2] ) const;
143 
145  double getDistanceToPoint( double xp, double yp );
146 
148  bool isBorderCrossingLine( PointSet* feat );
149 
151  int getNumPointsInPolygon( int npol, double *xp, double *yp );
152 
154  void offsetPosition( double xOffset, double yOffset );
155 
156 
160  int getId() const;
161 
162 
166  FeaturePart * getFeaturePart();
167 
168  double getNumOverlaps() const { return nbOverlap; }
169  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
170 
171  int getProblemFeatureId() const { return probFeat; }
174  void setProblemIds( int probFid, int lpId )
175  {
176  probFeat = probFid; id = lpId;
177  if ( nextPart ) nextPart->setProblemIds( probFid, lpId );
178  }
179 
181  char* getLayerName() const;
182 
187  double getCost() const;
188 
190  void setCost( double newCost ) { cost = newCost; }
191 
193  void validateCost();
194 
195 
200  double getX( int i = 0 ) const;
205  double getY( int i = 0 ) const;
206 
207  double getWidth() const { return w; }
208  double getHeight() const { return h; }
209 
214  double getAlpha() const;
215  bool getReversed() const { return reversed; }
216  bool getUpsideDown() const { return upsideDown; }
217 
218  void print();
219 
220  LabelPosition* getNextPart() const { return nextPart; }
221  void setNextPart( LabelPosition* next ) { nextPart = next; }
222 
223  // -1 if not multi-part
224  int getPartId() const { return partId; }
225  void setPartId( int id ) { partId = id; }
226 
227 
228  void removeFromIndex( RTree<LabelPosition*, double, 2, double> *index );
229  void insertIntoIndex( RTree<LabelPosition*, double, 2, double> *index );
230 
231  typedef struct
232  {
233  double scale;
236  } PruneCtx;
237 
239  static bool pruneCallback( LabelPosition *lp, void *ctx );
240 
241  // for sorting
242  static bool costShrink( void *l, void *r );
243  static bool costGrow( void *l, void *r );
244 
245  // for counting number of overlaps
246  typedef struct
247  {
249  int *nbOv;
250  double *cost;
251  double *inactiveCost;
252  //int *feat;
253  } CountContext;
254 
255  /*
256  * count overlap, ctx = p_lp
257  */
258  static bool countOverlapCallback( LabelPosition *lp, void *ctx );
259 
260  static bool countFullOverlapCallback( LabelPosition *lp, void *ctx );
261 
262  static bool removeOverlapCallback( LabelPosition *lp, void *ctx );
263 
264  // for polygon cost calculation
265  static bool polygonObstacleCallback( PointSet *feat, void *ctx );
266 
267  };
268 
269 } // end namespac
270 
271 #endif