29 return c1->cost() < c2->cost();
45 else if ( dist < distlabel )
85 switch (
pal->placementVersion() )
97 if ( n > 0 && ( priority < obstaclePriority && !
qgsDoubleNear( priority, obstaclePriority, 0.001 ) ) )
113 QHash< LabelPosition *, double > polygonRingDistances;
114 double minCandidateRingDistance = std::numeric_limits< double >::max();
115 double maxCandidateRingDistance = std::numeric_limits< double >::lowest();
116 for (
const std::unique_ptr< LabelPosition > &pos : lPos )
120 minCandidateRingDistance = std::min( minCandidateRingDistance, candidatePolygonRingDistance );
121 maxCandidateRingDistance = std::max( maxCandidateRingDistance, candidatePolygonRingDistance );
123 polygonRingDistances.insert( pos.get(), candidatePolygonRingDistance );
127 const double costRange = maxCandidateRingDistance - minCandidateRingDistance;
131 const double normalizer = 0.0020 / costRange;
135 for ( std::unique_ptr< LabelPosition > &pos : lPos )
137 const double polygonRingDistanceCost = polygonRingDistances.value( pos.get() );
138 pos->setCost( pos->cost() + 0.002 - ( polygonRingDistanceCost - minCandidateRingDistance ) * normalizer );
149 QHash< LabelPosition *, double > polygonCentroidDistances;
150 double minCandidateCentroidDistance = std::numeric_limits< double >::max();
151 double maxCandidateCentroidDistance = std::numeric_limits< double >::lowest();
152 for ( std::unique_ptr< LabelPosition > &pos : lPos )
154 const double lPosX = ( pos->x[0] + pos->x[2] ) / 2.0;
155 const double lPosY = ( pos->y[0] + pos->y[2] ) / 2.0;
157 const double candidatePolygonCentroidDistance = std::sqrt( ( cx - lPosX ) * ( cx - lPosX ) + ( cy - lPosY ) * ( cy - lPosY ) );
159 minCandidateCentroidDistance = std::min( minCandidateCentroidDistance, candidatePolygonCentroidDistance );
160 maxCandidateCentroidDistance = std::max( maxCandidateCentroidDistance, candidatePolygonCentroidDistance );
162 polygonCentroidDistances.insert( pos.get(), candidatePolygonCentroidDistance );
166 const double costRange = maxCandidateCentroidDistance - minCandidateCentroidDistance;
170 const double normalizer = 0.001 / costRange;
175 for ( std::unique_ptr< LabelPosition > &pos : lPos )
177 const double polygonCentroidDistance = polygonCentroidDistances.value( pos.get() );
178 pos->setCost( pos->cost() + ( polygonCentroidDistance - minCandidateCentroidDistance ) * normalizer );
197 const PointSet extent( 4, bbx, bby );
198 ringDistanceCalculator.
addRing( &extent );
221 double discrim = 0.0;
222 std::size_t stop = 0;
226 for ( stop = 0; stop < feat->
candidates.size() && feat->
candidates[ stop ]->cost() < discrim; stop++ )
229 while ( stop == 0 && discrim < feat->candidates.back()->cost() + 2.0 );
234 for ( std::size_t k = 0; k < stop; k++ )
262 : mPx( ( candidate->x[0] + candidate->x[2] ) / 2.0 )
263 , mPy( ( candidate->y[0] + candidate->y[2] ) / 2.0 )
270 if ( d < mMinDistance )
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
@ Free
Arranges candidates scattered throughout a polygon feature. Candidates are rotated to respect the pol...
@ Horizontal
Arranges horizontal candidates scattered throughout a polygon feature. Applies to polygon layers only...
@ Version2
Version 2 (default for new projects since QGIS 3.12)
@ Version1
Version 1, matches placement from QGIS <= 3.10.1.
double factor() const
Returns the obstacle factor, where 1.0 = default, < 1.0 more likely to be covered by labels,...
Calculates distance from a label candidate to nearest polygon ring.
void addRing(const pal::PointSet *ring)
Adds a ring to the calculation, updating the minimumDistance() value if the rings is closer to the ca...
double minimumDistance() const
Returns the minimum distance between the candidate and all added rings.
CandidatePolygonRingDistanceCalculator(LabelPosition *candidate)
Constructor for PolygonRingDistanceCalculator, for the specified label candidate.
static double calculatePolygonRingDistance(LabelPosition *candidate, double bbx[4], double bby[4])
Calculates the distance between a label candidate and the closest ring for a polygon feature.
static void addObstacleCostPenalty(pal::LabelPosition *lp, pal::FeaturePart *obstacle, Pal *pal)
Increase candidate's cost according to its collision with passed feature.
static void finalizeCandidatesCosts(Feats *feat, double bbx[4], double bby[4])
Sort candidates by costs, skip the worse ones, evaluate polygon candidates.
static bool candidateSortGrow(const std::unique_ptr< pal::LabelPosition > &c1, const std::unique_ptr< pal::LabelPosition > &c2)
Sorts label candidates in ascending order of cost.
static void calculateCandidatePolygonRingDistanceCosts(std::vector< std::unique_ptr< pal::LabelPosition > > &lPos, double bbx[4], double bby[4])
Updates the costs for polygon label candidates by considering the distance between the candidates and...
static void calculateCandidatePolygonCentroidDistanceCosts(pal::FeaturePart *feature, std::vector< std::unique_ptr< pal::LabelPosition > > &lPos)
Updates the costs for polygon label candidates by considering the distance between the candidates and...
For usage in problem solving algorithm.
std::vector< std::unique_ptr< LabelPosition > > candidates
Main class to handle feature.
double getLabelDistance() const
Returns the distance from the anchor point to the label.
int getNumSelfObstacles() const
Gets number of holes (inner rings) - they are considered as obstacles.
const QgsLabelObstacleSettings & obstacleSettings() const
Returns the feature's obstacle settings.
Layer * layer()
Returns the layer that feature belongs to.
FeaturePart * getSelfObstacle(int i)
Gets hole (inner ring) - considered as obstacle.
void addSizePenalty(std::vector< std::unique_ptr< LabelPosition > > &lPos, double bbx[4], double bby[4]) const
Increases the cost of the label candidates for this feature, based on the size of the feature.
double calculatePriority() const
Calculates the priority for the feature.
LabelPosition is a candidate feature label position.
bool intersectsWithPolygon(PointSet *polygon) const
Returns true if any intersection between polygon and position exists.
void setCost(double newCost)
Sets the candidate label position's geographical cost.
bool crossesLine(PointSet *line) const
Returns true if this label crosses the specified line.
double cost() const
Returns the candidate label position's geographical cost.
void setConflictsWithObstacle(bool conflicts)
Sets whether the position is marked as conflicting with an obstacle feature.
void setHasHardObstacleConflict(bool conflicts)
Sets whether the position is marked as having a hard conflict with an obstacle feature.
bool crossesBoundary(PointSet *polygon) const
Returns true if this label crosses the boundary of the specified polygon.
double getDistanceToPoint(double xp, double yp, bool useOuterBounds) const
Gets distance from this label to a point.
int polygonIntersectionCost(PointSet *polygon) const
Returns cost of position intersection with polygon (testing area of intersection and center).
Qgis::LabelPlacement arrangement() const
Returns the layer's arrangement policy.
QgsLabelObstacleSettings::ObstacleType obstacleType() const
Returns the obstacle type, which controls how features within the layer act as obstacles for labels.
The underlying raw pal geometry class.
void getCentroid(double &px, double &py, bool forceInside=false) const
double minDistanceToPoint(double px, double py, double *rx=nullptr, double *ry=nullptr) const
Returns the squared minimum distance between the point set geometry and the point (px,...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)