28bool CostCalculator::candidateSortGrow(
const std::unique_ptr< LabelPosition > &c1,
const std::unique_ptr< LabelPosition > &c2 )
30 return c1->cost() < c2->cost();
46 else if ( dist < distlabel )
86 switch (
pal->placementVersion() )
98 if ( n > 0 && ( priority < obstaclePriority && !
qgsDoubleNear( priority, obstaclePriority, 0.001 ) ) )
114 QHash< LabelPosition *, double > polygonRingDistances;
115 double minCandidateRingDistance = std::numeric_limits< double >::max();
116 double maxCandidateRingDistance = std::numeric_limits< double >::lowest();
117 for (
const std::unique_ptr< LabelPosition > &pos : lPos )
121 minCandidateRingDistance = std::min( minCandidateRingDistance, candidatePolygonRingDistance );
122 maxCandidateRingDistance = std::max( maxCandidateRingDistance, candidatePolygonRingDistance );
124 polygonRingDistances.insert( pos.get(), candidatePolygonRingDistance );
128 const double costRange = maxCandidateRingDistance - minCandidateRingDistance;
132 const double normalizer = 0.0020 / costRange;
136 for ( std::unique_ptr< LabelPosition > &pos : lPos )
138 const double polygonRingDistanceCost = polygonRingDistances.value( pos.get() );
139 pos->setCost( pos->cost() + 0.002 - ( polygonRingDistanceCost - minCandidateRingDistance ) * normalizer );
150 QHash< LabelPosition *, double > polygonCentroidDistances;
151 double minCandidateCentroidDistance = std::numeric_limits< double >::max();
152 double maxCandidateCentroidDistance = std::numeric_limits< double >::lowest();
153 for ( std::unique_ptr< LabelPosition > &pos : lPos )
155 const double lPosX = ( pos->x[0] + pos->x[2] ) / 2.0;
156 const double lPosY = ( pos->y[0] + pos->y[2] ) / 2.0;
158 const double candidatePolygonCentroidDistance = std::sqrt( ( cx - lPosX ) * ( cx - lPosX ) + ( cy - lPosY ) * ( cy - lPosY ) );
160 minCandidateCentroidDistance = std::min( minCandidateCentroidDistance, candidatePolygonCentroidDistance );
161 maxCandidateCentroidDistance = std::max( maxCandidateCentroidDistance, candidatePolygonCentroidDistance );
163 polygonCentroidDistances.insert( pos.get(), candidatePolygonCentroidDistance );
167 const double costRange = maxCandidateCentroidDistance - minCandidateCentroidDistance;
171 const double normalizer = 0.001 / costRange;
176 for ( std::unique_ptr< LabelPosition > &pos : lPos )
178 const double polygonCentroidDistance = polygonCentroidDistances.value( pos.get() );
179 pos->setCost( pos->cost() + ( polygonCentroidDistance - minCandidateCentroidDistance ) * normalizer );
198 const PointSet extent( 4, bbx, bby );
199 ringDistanceCalculator.
addRing( &extent );
222 double discrim = 0.0;
223 std::size_t stop = 0;
227 for ( stop = 0; stop < feat->
candidates.size() && feat->
candidates[ stop ]->cost() < discrim; stop++ )
230 while ( stop == 0 && discrim < feat->candidates.back()->cost() + 2.0 );
235 for ( std::size_t k = 0; k < stop; k++ )
263 : mPx( ( candidate->x[0] + candidate->x[2] ) / 2.0 )
264 , mPy( ( candidate->y[0] + candidate->y[2] ) / 2.0 )
271 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...
double factor() const
Returns the obstacle factor, where 1.0 = default, < 1.0 more likely to be covered by labels,...
@ PlacementEngineVersion1
Version 1, matches placement from QGIS <= 3.10.1.
@ PlacementEngineVersion2
Version 2 (default for new projects since QGIS 3.12)
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) const
Gets distance from this label to a point. If point lies inside, returns negative number.
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)