QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
feature.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 FEATURE_H
31 #define FEATURE_H
32 
33 #define SIP_NO_FILE
34 
35 
36 #include "qgis_core.h"
37 #include "pointset.h"
38 #include "labelposition.h" // for LabelPosition enum
39 #include "qgslabelfeature.h"
40 #include <iostream>
41 #include <fstream>
42 #include <cmath>
43 #include <QString>
44 
51 namespace pal
52 {
54  class CORE_EXPORT LabelInfo
55  {
56  public:
58  {
59  double width;
60  };
61 
62  LabelInfo( int num, double height, double maxinangle = 20.0, double maxoutangle = -20.0 )
63  {
64  max_char_angle_inside = maxinangle;
65  // outside angle should be negative
66  max_char_angle_outside = maxoutangle > 0 ? -maxoutangle : maxoutangle;
67  label_height = height;
68  char_num = num;
69  char_info = new CharacterInfo[num];
70  }
71  ~LabelInfo() { delete [] char_info; }
72 
74  LabelInfo( const LabelInfo &rh ) = delete;
76  LabelInfo &operator=( const LabelInfo &rh ) = delete;
77 
80  double label_height;
81  int char_num;
82  CharacterInfo *char_info = nullptr;
83 
84  };
85 
86  class LabelPosition;
87  class FeaturePart;
88 
95  class CORE_EXPORT FeaturePart : public PointSet
96  {
97 
98  public:
99 
105  FeaturePart( QgsLabelFeature *lf, const GEOSGeometry *geom );
106 
107  FeaturePart( const FeaturePart &other );
108 
112  ~FeaturePart() override;
113 
117  QgsLabelFeature *feature() { return mLF; }
118 
122  Layer *layer();
123 
127  QgsFeatureId featureId() const;
128 
132  std::size_t maximumPointCandidates() const;
133 
137  std::size_t maximumLineCandidates() const;
138 
142  std::size_t maximumPolygonCandidates() const;
143 
147  std::vector<std::unique_ptr<LabelPosition> > createCandidates( Pal *pal );
148 
157  std::size_t createCandidatesAroundPoint( double x, double y, std::vector<std::unique_ptr<LabelPosition> > &lPos, double angle );
158 
167  std::size_t createCandidatesOverPoint( double x, double y, std::vector<std::unique_ptr<LabelPosition> > &lPos, double angle );
168 
177  std::size_t createCandidateCenteredOverPoint( double x, double y, std::vector<std::unique_ptr<LabelPosition> > &lPos, double angle );
178 
185  std::unique_ptr< LabelPosition > createCandidatePointOnSurface( PointSet *mapShape );
186 
195  std::size_t createCandidatesAtOrderedPositionsOverPoint( double x, double y, std::vector<std::unique_ptr<LabelPosition> > &lPos, double angle );
196 
205  std::size_t createCandidatesAlongLine( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, bool allowOverrun, Pal *pal );
206 
214  std::size_t createHorizontalCandidatesAlongLine( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, Pal *pal );
215 
224  std::size_t createCandidatesAlongLineNearStraightSegments( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, Pal *pal );
225 
236  std::size_t createCandidatesAlongLineNearMidpoint( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, double initialCost = 0.0, Pal *pal = nullptr );
237 
249  std::unique_ptr< LabelPosition > curvedPlacementAtOffset( PointSet *path_positions, double *path_distances,
250  int &orientation, double distance, bool &reversed, bool &flip, bool applyAngleConstraints );
251 
260  std::size_t createCurvedCandidatesAlongLine( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, bool allowOverrun, Pal *pal );
261 
269  std::size_t createCandidatesForPolygon( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, Pal *pal );
270 
277  std::size_t createCandidatesOutsidePolygon( std::vector<std::unique_ptr<LabelPosition> > &lPos, Pal *pal );
278 
285  bool hasSameLabelFeatureAs( FeaturePart *part ) const;
286 
291  double getLabelWidth( double angle = 0.0 ) const { return mLF->size( angle ).width(); }
292 
297  double getLabelHeight( double angle = 0.0 ) const { return mLF->size( angle ).height(); }
298 
303  double getLabelDistance() const { return mLF->distLabel(); }
304 
306  bool hasFixedRotation() const { return mLF->hasFixedAngle(); }
307 
309  double fixedAngle() const { return mLF->fixedAngle(); }
310 
312  bool hasFixedPosition() const { return mLF->hasFixedPosition(); }
313 
318  bool alwaysShow() const { return mLF->alwaysShow(); }
319 
323  const QgsLabelObstacleSettings &obstacleSettings() const { return mLF->obstacleSettings(); }
324 
326  double repeatDistance() const { return mLF->repeatDistance(); }
327 
329  int getNumSelfObstacles() const { return mHoles.count(); }
331  FeaturePart *getSelfObstacle( int i ) { return mHoles.at( i ); }
332 
334  bool isConnected( FeaturePart *p2 );
335 
340  bool mergeWithFeaturePart( FeaturePart *other );
341 
347  void addSizePenalty( std::vector<std::unique_ptr<LabelPosition> > &lPos, double bbx[4], double bby[4] );
348 
353  double calculatePriority() const;
354 
356  bool showUprightLabels() const;
357 
359  bool nextCharPosition( double charWidth, double segmentLength, PointSet *path_positions, int &index, double &currentDistanceAlongSegment,
360  double &characterStartX, double &characterStartY, double &characterEndX, double &characterEndY ) const;
361 
366  int totalRepeats() const;
367 
372  void setTotalRepeats( int repeats );
373 
374  protected:
375 
376  QgsLabelFeature *mLF = nullptr;
377  QList<FeaturePart *> mHoles;
378 
380  void extractCoords( const GEOSGeometry *geom );
381 
382  private:
383 
384  LabelPosition::Quadrant quadrantFromOffset() const;
385 
386  int mTotalRepeats = 0;
387 
388  mutable std::size_t mCachedMaxLineCandidates = 0;
389  mutable std::size_t mCachedMaxPolygonCandidates = 0;
390 
391  FeaturePart &operator= ( const FeaturePart & ) = delete;
392  };
393 
394 } // end namespace pal
395 
396 #endif
pal::LabelInfo
Optional additional info about label (for curved labels)
Definition: feature.h:55
pal::LabelPosition::Quadrant
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:66
pal::LabelInfo::CharacterInfo
Definition: feature.h:58
pal::FeaturePart::getLabelWidth
double getLabelWidth(double angle=0.0) const
Returns the width of the label, optionally taking an angle into account.
Definition: feature.h:291
pal::LabelInfo::max_char_angle_inside
double max_char_angle_inside
Definition: feature.h:78
labelposition.h
pal::FeaturePart::repeatDistance
double repeatDistance() const
Returns the distance between repeating labels for this feature.
Definition: feature.h:326
pal::LabelPosition
LabelPosition is a candidate feature label position.
Definition: labelposition.h:56
qgslabelfeature.h
QgsLabelObstacleSettings
Contains settings related to how the label engine treats features as obstacles.
Definition: qgslabelobstaclesettings.h:35
pal::LabelInfo::char_num
int char_num
Definition: feature.h:81
pal::FeaturePart::hasFixedRotation
bool hasFixedRotation() const
Returns true if the feature's label has a fixed rotation.
Definition: feature.h:306
pal
Definition: qgsdiagramrenderer.h:49
pal::LabelInfo::LabelInfo
LabelInfo(const LabelInfo &rh)=delete
LabelInfo cannot be copied.
pal::FeaturePart::alwaysShow
bool alwaysShow() const
Returns true if the feature's label should always been shown, even when it collides with other labels...
Definition: feature.h:318
pal::FeaturePart::obstacleSettings
const QgsLabelObstacleSettings & obstacleSettings() const
Returns the feature's obstacle settings.
Definition: feature.h:323
pal::FeaturePart::getNumSelfObstacles
int getNumSelfObstacles() const
Gets number of holes (inner rings) - they are considered as obstacles.
Definition: feature.h:329
pal::FeaturePart::getLabelDistance
double getLabelDistance() const
Returns the distance from the anchor point to the label.
Definition: feature.h:303
pointset.h
pal::FeaturePart
Main class to handle feature.
Definition: feature.h:96
pal::FeaturePart::getSelfObstacle
FeaturePart * getSelfObstacle(int i)
Gets hole (inner ring) - considered as obstacle.
Definition: feature.h:331
pal::LabelInfo::~LabelInfo
~LabelInfo()
Definition: feature.h:71
pal::LabelInfo::max_char_angle_outside
double max_char_angle_outside
Definition: feature.h:79
pal::PointSet
Definition: pointset.h:72
pal::LabelInfo::operator=
LabelInfo & operator=(const LabelInfo &rh)=delete
LabelInfo cannot be copied.
pal::FeaturePart::hasFixedPosition
bool hasFixedPosition() const
Returns true if the feature's label has a fixed position.
Definition: feature.h:312
pal::Pal
Main Pal labeling class.
Definition: pal.h:80
pal::Layer
A set of features which influence the labeling process.
Definition: layer.h:62
pal::FeaturePart::getLabelHeight
double getLabelHeight(double angle=0.0) const
Returns the height of the label, optionally taking an angle into account.
Definition: feature.h:297
pal::LabelInfo::LabelInfo
LabelInfo(int num, double height, double maxinangle=20.0, double maxoutangle=-20.0)
Definition: feature.h:62
QgsLabelFeature
The QgsLabelFeature class describes a feature that should be used within the labeling engine.
Definition: qgslabelfeature.h:57
pal::LabelInfo::label_height
double label_height
Definition: feature.h:80
pal::FeaturePart::fixedAngle
double fixedAngle() const
Returns the fixed angle for the feature's label.
Definition: feature.h:309
pal::FeaturePart::mHoles
QList< FeaturePart * > mHoles
Definition: feature.h:377
pal::FeaturePart::feature
QgsLabelFeature * feature()
Returns the parent feature.
Definition: feature.h:117
MathUtils::angle
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
pal::LabelInfo::CharacterInfo::width
double width
Definition: feature.h:59