QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #define SIP_NO_FILE
34 
35 
36 #include "qgis_core.h"
37 #include "pointset.h"
38 #include "palrtree.h"
39 #include <fstream>
40 
41 namespace pal
42 {
43 
44  class FeaturePart;
45  class Pal;
46  class Label;
47 
48 
55  class CORE_EXPORT LabelPosition : public PointSet
56  {
57  friend class CostCalculator;
58  friend class PolygonCostCalculator;
59 
60  public:
61 
65  enum Quadrant
66  {
75  QuadrantBelowRight
76  };
77 
92  LabelPosition( int id, double x1, double y1,
93  double w, double h,
94  double alpha, double cost,
95  FeaturePart *feature, bool isReversed = false, Quadrant quadrant = QuadrantOver );
96 
98  LabelPosition( const LabelPosition &other );
99 
105  bool isIn( double *bbox );
106 
112  bool isIntersect( double *bbox );
113 
117  bool intersects( const GEOSPreparedGeometry *geometry );
118 
122  bool within( const GEOSPreparedGeometry *geometry );
123 
129  bool isInside( double *bbox );
130 
137  bool isInConflict( const LabelPosition *ls ) const;
138 
140  void getBoundingBox( double amin[2], double amax[2] ) const;
141 
143  double getDistanceToPoint( double xp, double yp ) const;
144 
146  bool crossesLine( PointSet *line ) const;
147 
149  bool crossesBoundary( PointSet *polygon ) const;
150 
155  int polygonIntersectionCost( PointSet *polygon ) const;
156 
160  bool intersectsWithPolygon( PointSet *polygon ) const;
161 
163  void offsetPosition( double xOffset, double yOffset );
164 
168  int getId() const;
169 
170 
174  FeaturePart *getFeaturePart() const;
175 
176  int getNumOverlaps() const { return nbOverlap; }
177  void resetNumOverlaps() { nbOverlap = 0; } // called from problem.cpp, pal.cpp
178 
182  void incrementNumOverlaps() { nbOverlap++; }
183 
187  void decrementNumOverlaps() { nbOverlap++; }
188 
189  int getProblemFeatureId() const { return probFeat; }
190 
195  void setProblemIds( int probFid, int lpId )
196  {
197  probFeat = probFid;
198  id = lpId;
199  if ( mNextPart ) mNextPart->setProblemIds( probFid, lpId );
200  }
201 
206  double cost() const { return mCost; }
207 
213  void setCost( double newCost ) { mCost = newCost; }
214 
221  void setConflictsWithObstacle( bool conflicts );
222 
227  bool conflictsWithObstacle() const { return mHasObstacleConflict; }
228 
235  void setHasHardObstacleConflict( bool conflicts );
236 
243  bool hasHardObstacleConflict() const { return mHasHardConflict; }
244 
246  void validateCost();
247 
252  double getX( int i = 0 ) const;
253 
258  double getY( int i = 0 ) const;
259 
260  double getWidth() const { return w; }
261  double getHeight() const { return h; }
262 
266  double getAlpha() const;
267  bool getReversed() const { return reversed; }
268  bool getUpsideDown() const { return upsideDown; }
269 
270  Quadrant getQuadrant() const { return quadrant; }
271 
277  LabelPosition *nextPart() const { return mNextPart.get(); }
278 
284  void setNextPart( std::unique_ptr< LabelPosition > next ) { mNextPart = std::move( next ); }
285 
286  // -1 if not multi-part
287  int getPartId() const { return partId; }
288  void setPartId( int id ) { partId = id; }
289 
295  void setUpsideDownCharCount( int count ) { mUpsideDownCharCount = count ; }
296 
302  int upsideDownCharCount() const { return mUpsideDownCharCount; }
303 
307  void removeFromIndex( PalRtree<LabelPosition> &index );
308 
312  void insertIntoIndex( PalRtree<LabelPosition> &index );
313 
319  const GEOSPreparedGeometry *preparedMultiPartGeom() const;
320 
329  unsigned int globalId() const { return mGlobalId; }
330 
337  void setGlobalId( unsigned int id ) { mGlobalId = id; }
338 
342  double angleDifferential();
343 
344  protected:
345 
346  int id;
347 
348  FeaturePart *feature = nullptr;
349 
350  // bug # 1 (maxence 10/23/2008)
351  int probFeat;
352 
354 
355  double alpha;
356  double w;
357  double h;
358 
359  int partId;
360 
361  //True if label direction is the same as line / polygon ring direction.
362  //Could be used by the application to draw a directional arrow ('<' or '>')
363  //if the layer arrangement is P_LINE
364  bool reversed;
365 
367 
369 
370  private:
371 
372  unsigned int mGlobalId = 0;
373  std::unique_ptr< LabelPosition > mNextPart;
374 
375  double mCost;
376  bool mHasObstacleConflict;
377  bool mHasHardConflict = false;
378  int mUpsideDownCharCount;
379 
383  int partCount() const;
384 
389  double polygonIntersectionCostForParts( PointSet *polygon ) const;
390 
394  void createMultiPartGeosGeom() const;
395 
396  bool isInConflictMultiPart( const LabelPosition *lp ) const;
397 
398  LabelPosition &operator=( const LabelPosition & ) = delete;
399  };
400 
401 } // end namespace
402 
403 #endif
pal::LabelPosition::quadrant
LabelPosition::Quadrant quadrant
Definition: labelposition.h:368
pal::LabelPosition::Quadrant
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:65
pal::LabelPosition::getProblemFeatureId
int getProblemFeatureId() const
Definition: labelposition.h:189
pal::LabelPosition::partId
int partId
Definition: labelposition.h:359
pal::LabelPosition::cost
double cost() const
Returns the candidate label position's geographical cost.
Definition: labelposition.h:206
pal::LabelPosition
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
pal::LabelPosition::QuadrantOver
@ QuadrantOver
Definition: labelposition.h:71
pal::LabelPosition::QuadrantAboveLeft
@ QuadrantAboveLeft
Definition: labelposition.h:67
pal::LabelPosition::w
double w
Definition: labelposition.h:356
pal::LabelPosition::getQuadrant
Quadrant getQuadrant() const
Definition: labelposition.h:270
pal::LabelPosition::nextPart
LabelPosition * nextPart() const
Returns the next part of this label position (i.e.
Definition: labelposition.h:277
pal::LabelPosition::setCost
void setCost(double newCost)
Sets the candidate label position's geographical cost.
Definition: labelposition.h:213
pal
Definition: qgsdiagramrenderer.h:50
pal::LabelPosition::nbOverlap
int nbOverlap
Definition: labelposition.h:353
pal::LabelPosition::QuadrantBelowLeft
@ QuadrantBelowLeft
Definition: labelposition.h:73
pal::LabelPosition::upsideDown
bool upsideDown
Definition: labelposition.h:366
palrtree.h
pal::LabelPosition::reversed
bool reversed
Definition: labelposition.h:364
pal::LabelPosition::globalId
unsigned int globalId() const
Returns the global ID for the candidate, which is unique for a single run of the pal labelling engine...
Definition: labelposition.h:329
pal::CostCalculator
Calculates label candidate costs considering different factors.
Definition: costcalculator.h:38
pointset.h
pal::LabelPosition::h
double h
Definition: labelposition.h:357
pal::FeaturePart
Main class to handle feature.
Definition: feature.h:64
pal::LabelPosition::hasHardObstacleConflict
bool hasHardObstacleConflict() const
Returns whether the position is marked as having a hard conflict with an obstacle feature.
Definition: labelposition.h:243
pal::LabelPosition::setNextPart
void setNextPart(std::unique_ptr< LabelPosition > next)
Sets the next part of this label position (i.e.
Definition: labelposition.h:284
pal::LabelPosition::setPartId
void setPartId(int id)
Definition: labelposition.h:288
pal::LabelPosition::decrementNumOverlaps
void decrementNumOverlaps()
Decreases the number of overlaps recorded against this position by 1.
Definition: labelposition.h:187
pal::LabelPosition::getUpsideDown
bool getUpsideDown() const
Definition: labelposition.h:268
pal::LabelPosition::setProblemIds
void setProblemIds(int probFid, int lpId)
Set problem feature ID and assigned label candidate ID.
Definition: labelposition.h:195
pal::LabelPosition::QuadrantAboveRight
@ QuadrantAboveRight
Definition: labelposition.h:69
pal::LabelPosition::getNumOverlaps
int getNumOverlaps() const
Definition: labelposition.h:176
pal::LabelPosition::id
int id
Definition: labelposition.h:346
pal::PointSet
The underlying raw pal geometry class.
Definition: pointset.h:76
pal::LabelPosition::incrementNumOverlaps
void incrementNumOverlaps()
Increases the number of overlaps recorded against this position by 1.
Definition: labelposition.h:182
pal::LabelPosition::QuadrantLeft
@ QuadrantLeft
Definition: labelposition.h:70
pal::LabelPosition::getHeight
double getHeight() const
Definition: labelposition.h:261
pal::LabelPosition::conflictsWithObstacle
bool conflictsWithObstacle() const
Returns whether the position is marked as conflicting with an obstacle feature.
Definition: labelposition.h:227
PalRtree
A rtree spatial index for use in the pal labeling engine.
Definition: palrtree.h:35
pal::LabelPosition::getReversed
bool getReversed() const
Definition: labelposition.h:267
pal::LabelPosition::QuadrantRight
@ QuadrantRight
Definition: labelposition.h:72
pal::LabelPosition::upsideDownCharCount
int upsideDownCharCount() const
Returns the number of upside down characters for this label position.
Definition: labelposition.h:302
pal::LabelPosition::resetNumOverlaps
void resetNumOverlaps()
Definition: labelposition.h:177
pal::LabelPosition::alpha
double alpha
Definition: labelposition.h:355
pal::LabelPosition::QuadrantBelow
@ QuadrantBelow
Definition: labelposition.h:74
pal::LabelPosition::QuadrantAbove
@ QuadrantAbove
Definition: labelposition.h:68
pal::LabelPosition::setGlobalId
void setGlobalId(unsigned int id)
Sets the global id for the candidate, which is unique for a single run of the pal labelling engine.
Definition: labelposition.h:337
pal::LabelPosition::getPartId
int getPartId() const
Definition: labelposition.h:287
pal::LabelPosition::setUpsideDownCharCount
void setUpsideDownCharCount(int count)
Sets the count of upside down characters for this label position.
Definition: labelposition.h:295
pal::LabelPosition::probFeat
int probFeat
Definition: labelposition.h:351
pal::LabelPosition::getWidth
double getWidth() const
Definition: labelposition.h:260