@@ -964,12 +964,14 @@ int FeaturePart::createCandidatesAlongLineNearMidpoint( QList<LabelPosition *> &
964
964
}
965
965
966
966
967
- LabelPosition *FeaturePart::curvedPlacementAtOffset ( PointSet *path_positions, double *path_distances, int &orientation, int index, double distance , bool &reversed, bool &flip )
967
+ LabelPosition *FeaturePart::curvedPlacementAtOffset ( PointSet *path_positions, double *path_distances, int &orientation, const double offsetAlongLine , bool &reversed, bool &flip )
968
968
{
969
- // Same thing, checking if we go off the end
970
- while ( index < path_positions->nbPoints && distance > path_distances[index] )
969
+ double offsetAlongSegment = offsetAlongLine;
970
+ int index = 1 ;
971
+ // Find index of segment corresponding to starting offset
972
+ while ( index < path_positions->nbPoints && offsetAlongSegment > path_distances[index] )
971
973
{
972
- distance -= path_distances[index];
974
+ offsetAlongSegment -= path_distances[index];
973
975
index += 1 ;
974
976
}
975
977
if ( index >= path_positions->nbPoints )
@@ -981,7 +983,7 @@ LabelPosition *FeaturePart::curvedPlacementAtOffset( PointSet *path_positions, d
981
983
982
984
double string_height = li->label_height ;
983
985
984
- double segment_length = path_distances[index];
986
+ const double segment_length = path_distances[index];
985
987
if ( qgsDoubleNear ( segment_length, 0.0 ) )
986
988
{
987
989
// Not allowed to place across on 0 length segments or discontinuities
@@ -992,7 +994,7 @@ LabelPosition *FeaturePart::curvedPlacementAtOffset( PointSet *path_positions, d
992
994
{
993
995
// Calculate the orientation based on the angle of the path segment under consideration
994
996
995
- double _distance = distance ;
997
+ double _distance = offsetAlongSegment ;
996
998
int endindex = index;
997
999
998
1000
double startLabelX = 0 ;
@@ -1059,7 +1061,7 @@ LabelPosition *FeaturePart::curvedPlacementAtOffset( PointSet *path_positions, d
1059
1061
continue ;
1060
1062
1061
1063
double start_x, start_y, end_x, end_y;
1062
- if ( !nextCharPosition ( ci.width , path_distances[index], path_positions, index, distance , start_x, start_y, end_x, end_y ) )
1064
+ if ( !nextCharPosition ( ci.width , path_distances[index], path_positions, index, offsetAlongSegment , start_x, start_y, end_x, end_y ) )
1063
1065
{
1064
1066
delete slp;
1065
1067
return nullptr ;
@@ -1127,7 +1129,6 @@ LabelPosition *FeaturePart::curvedPlacementAtOffset( PointSet *path_positions, d
1127
1129
if ( render_angle > M_PI_2 && render_angle < 1.5 * M_PI )
1128
1130
slp->incrementUpsideDownCharCount ();
1129
1131
}
1130
- // END FOR
1131
1132
1132
1133
return slp;
1133
1134
}
@@ -1187,7 +1188,7 @@ int FeaturePart::createCurvedCandidatesAlongLine( QList< LabelPosition * > &lPos
1187
1188
flags = FLAG_ON_LINE; // default flag
1188
1189
1189
1190
// generate curved labels
1190
- for ( double i = 0 ; i < total_distance; i += delta )
1191
+ for ( double distanceAlongLineToStartCandidate = 0 ; distanceAlongLineToStartCandidate < total_distance; distanceAlongLineToStartCandidate += delta )
1191
1192
{
1192
1193
bool flip = false ;
1193
1194
// placements may need to be reversed if using map orientation and the line has right-to-left direction
@@ -1202,7 +1203,7 @@ int FeaturePart::createCurvedCandidatesAlongLine( QList< LabelPosition * > &lPos
1202
1203
orientation = 1 ;
1203
1204
}
1204
1205
1205
- LabelPosition *slp = curvedPlacementAtOffset ( mapShape, path_distances.get (), orientation, 1 , i , reversed, flip );
1206
+ LabelPosition *slp = curvedPlacementAtOffset ( mapShape, path_distances.get (), orientation, distanceAlongLineToStartCandidate , reversed, flip );
1206
1207
if ( !slp )
1207
1208
continue ;
1208
1209
@@ -1214,7 +1215,7 @@ int FeaturePart::createCurvedCandidatesAlongLine( QList< LabelPosition * > &lPos
1214
1215
{
1215
1216
delete slp;
1216
1217
orientation = -orientation;
1217
- slp = curvedPlacementAtOffset ( mapShape, path_distances.get (), orientation, 1 , i , reversed, flip );
1218
+ slp = curvedPlacementAtOffset ( mapShape, path_distances.get (), orientation, distanceAlongLineToStartCandidate , reversed, flip );
1218
1219
}
1219
1220
}
1220
1221
if ( !slp )
@@ -1245,7 +1246,7 @@ int FeaturePart::createCurvedCandidatesAlongLine( QList< LabelPosition * > &lPos
1245
1246
if ( cost < 0.0001 ) cost = 0.0001 ;
1246
1247
1247
1248
// penalize positions which are further from the line's midpoint
1248
- double labelCenter = i + getLabelWidth () / 2 ;
1249
+ double labelCenter = distanceAlongLineToStartCandidate + getLabelWidth () / 2 ;
1249
1250
double costCenter = std::fabs ( total_distance / 2 - labelCenter ) / total_distance; // <0, 0.5>
1250
1251
cost += costCenter / 1000 ; // < 0, 0.0005 >
1251
1252
slp->setCost ( cost );
@@ -1752,64 +1753,64 @@ bool FeaturePart::showUprightLabels() const
1752
1753
return uprightLabel;
1753
1754
}
1754
1755
1755
- bool FeaturePart::nextCharPosition ( double charWidth, double segment_length , PointSet *path_positions, int &index, double &distance ,
1756
- double &start_x , double &start_y , double &end_x , double &end_y ) const
1756
+ bool FeaturePart::nextCharPosition ( double charWidth, double segmentLength , PointSet *path_positions, int &index, double ¤tDistanceAlongSegment ,
1757
+ double &characterStartX , double &characterStartY , double &characterEndX , double &characterEndY ) const
1757
1758
{
1758
1759
// Coordinates this character will start at
1759
- if ( qgsDoubleNear ( segment_length , 0.0 ) )
1760
+ if ( qgsDoubleNear ( segmentLength , 0.0 ) )
1760
1761
{
1761
1762
// Not allowed to place across on 0 length segments or discontinuities
1762
1763
return false ;
1763
1764
}
1764
1765
1765
- double old_x = path_positions->x [index - 1 ];
1766
- double old_y = path_positions->y [index - 1 ];
1766
+ double segmentStartX = path_positions->x [index - 1 ];
1767
+ double segmentStartY = path_positions->y [index - 1 ];
1767
1768
1768
- double new_x = path_positions->x [index];
1769
- double new_y = path_positions->y [index];
1769
+ double segmentEndX = path_positions->x [index];
1770
+ double segmentEndY = path_positions->y [index];
1770
1771
1771
- double dx = new_x - old_x ;
1772
- double dy = new_y - old_y ;
1772
+ double segmentDx = segmentEndX - segmentStartX ;
1773
+ double segmentDy = segmentEndY - segmentStartY ;
1773
1774
1774
- start_x = old_x + dx * distance / segment_length ;
1775
- start_y = old_y + dy * distance / segment_length ;
1775
+ characterStartX = segmentStartX + segmentDx * currentDistanceAlongSegment / segmentLength ;
1776
+ characterStartY = segmentStartY + segmentDy * currentDistanceAlongSegment / segmentLength ;
1776
1777
1777
1778
// Coordinates this character ends at, calculated below
1778
- end_x = 0 ;
1779
- end_y = 0 ;
1779
+ characterEndX = 0 ;
1780
+ characterEndY = 0 ;
1780
1781
1781
- if ( segment_length - distance >= charWidth )
1782
+ if ( segmentLength - currentDistanceAlongSegment >= charWidth )
1782
1783
{
1783
1784
// if the distance remaining in this segment is enough, we just go further along the segment
1784
- distance += charWidth;
1785
- end_x = old_x + dx * distance / segment_length ;
1786
- end_y = old_y + dy * distance / segment_length ;
1785
+ currentDistanceAlongSegment += charWidth;
1786
+ characterEndX = segmentStartX + segmentDx * currentDistanceAlongSegment / segmentLength ;
1787
+ characterEndY = segmentStartY + segmentDy * currentDistanceAlongSegment / segmentLength ;
1787
1788
}
1788
1789
else
1789
1790
{
1790
1791
// If there isn't enough distance left on this segment
1791
1792
// then we need to search until we find the line segment that ends further than ci.width away
1792
1793
do
1793
1794
{
1794
- old_x = new_x ;
1795
- old_y = new_y ;
1795
+ segmentStartX = segmentEndX ;
1796
+ segmentStartY = segmentEndY ;
1796
1797
index++;
1797
1798
if ( index >= path_positions->nbPoints ) // Bail out if we run off the end of the shape
1798
1799
{
1799
1800
return false ;
1800
1801
}
1801
- new_x = path_positions->x [index];
1802
- new_y = path_positions->y [index];
1803
- dx = new_x - old_x ;
1804
- dy = new_y - old_y ;
1802
+ segmentEndX = path_positions->x [index];
1803
+ segmentEndY = path_positions->y [index];
1804
+ segmentDx = segmentEndX - segmentStartX ;
1805
+ segmentDy = segmentEndY - segmentStartY ;
1805
1806
}
1806
- while ( std::sqrt ( std::pow ( start_x - new_x , 2 ) + std::pow ( start_y - new_y , 2 ) ) < charWidth ); // Distance from start_ to new_
1807
+ while ( std::sqrt ( std::pow ( characterStartX - segmentEndX , 2 ) + std::pow ( characterStartY - segmentEndY , 2 ) ) < charWidth ); // Distance from start_ to new_
1807
1808
1808
1809
// Calculate the position to place the end of the character on
1809
- GeomFunction::findLineCircleIntersection ( start_x, start_y , charWidth, old_x, old_y, new_x, new_y, end_x, end_y );
1810
+ GeomFunction::findLineCircleIntersection ( characterStartX, characterStartY , charWidth, segmentStartX, segmentStartY, segmentEndX, segmentEndY, characterEndX, characterEndY );
1810
1811
1811
1812
// Need to calculate distance on the new segment
1812
- distance = std::sqrt ( std::pow ( old_x - end_x , 2 ) + std::pow ( old_y - end_y , 2 ) );
1813
+ currentDistanceAlongSegment = std::sqrt ( std::pow ( segmentStartX - characterEndX , 2 ) + std::pow ( segmentStartY - characterEndY , 2 ) );
1813
1814
}
1814
1815
return true ;
1815
1816
}
0 commit comments