Skip to content

Commit b3d29cc

Browse files
committedJun 10, 2019
[labeling] Update confusing/vague variable names
(cherry picked from commit f9b8c6f)
1 parent 8fbd3b0 commit b3d29cc

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed
 

‎src/core/pal/feature.cpp

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -964,12 +964,14 @@ int FeaturePart::createCandidatesAlongLineNearMidpoint( QList<LabelPosition *> &
964964
}
965965

966966

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 )
968968
{
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] )
971973
{
972-
distance -= path_distances[index];
974+
offsetAlongSegment -= path_distances[index];
973975
index += 1;
974976
}
975977
if ( index >= path_positions->nbPoints )
@@ -981,7 +983,7 @@ LabelPosition *FeaturePart::curvedPlacementAtOffset( PointSet *path_positions, d
981983

982984
double string_height = li->label_height;
983985

984-
double segment_length = path_distances[index];
986+
const double segment_length = path_distances[index];
985987
if ( qgsDoubleNear( segment_length, 0.0 ) )
986988
{
987989
// Not allowed to place across on 0 length segments or discontinuities
@@ -992,7 +994,7 @@ LabelPosition *FeaturePart::curvedPlacementAtOffset( PointSet *path_positions, d
992994
{
993995
// Calculate the orientation based on the angle of the path segment under consideration
994996

995-
double _distance = distance;
997+
double _distance = offsetAlongSegment;
996998
int endindex = index;
997999

9981000
double startLabelX = 0;
@@ -1059,7 +1061,7 @@ LabelPosition *FeaturePart::curvedPlacementAtOffset( PointSet *path_positions, d
10591061
continue;
10601062

10611063
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 ) )
10631065
{
10641066
delete slp;
10651067
return nullptr;
@@ -1127,7 +1129,6 @@ LabelPosition *FeaturePart::curvedPlacementAtOffset( PointSet *path_positions, d
11271129
if ( render_angle > M_PI_2 && render_angle < 1.5 * M_PI )
11281130
slp->incrementUpsideDownCharCount();
11291131
}
1130-
// END FOR
11311132

11321133
return slp;
11331134
}
@@ -1187,7 +1188,7 @@ int FeaturePart::createCurvedCandidatesAlongLine( QList< LabelPosition * > &lPos
11871188
flags = FLAG_ON_LINE; // default flag
11881189

11891190
// generate curved labels
1190-
for ( double i = 0; i < total_distance; i += delta )
1191+
for ( double distanceAlongLineToStartCandidate = 0; distanceAlongLineToStartCandidate < total_distance; distanceAlongLineToStartCandidate += delta )
11911192
{
11921193
bool flip = false;
11931194
// 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
12021203
orientation = 1;
12031204
}
12041205

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 );
12061207
if ( !slp )
12071208
continue;
12081209

@@ -1214,7 +1215,7 @@ int FeaturePart::createCurvedCandidatesAlongLine( QList< LabelPosition * > &lPos
12141215
{
12151216
delete slp;
12161217
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 );
12181219
}
12191220
}
12201221
if ( !slp )
@@ -1245,7 +1246,7 @@ int FeaturePart::createCurvedCandidatesAlongLine( QList< LabelPosition * > &lPos
12451246
if ( cost < 0.0001 ) cost = 0.0001;
12461247

12471248
// penalize positions which are further from the line's midpoint
1248-
double labelCenter = i + getLabelWidth() / 2;
1249+
double labelCenter = distanceAlongLineToStartCandidate + getLabelWidth() / 2;
12491250
double costCenter = std::fabs( total_distance / 2 - labelCenter ) / total_distance; // <0, 0.5>
12501251
cost += costCenter / 1000; // < 0, 0.0005 >
12511252
slp->setCost( cost );
@@ -1752,64 +1753,64 @@ bool FeaturePart::showUprightLabels() const
17521753
return uprightLabel;
17531754
}
17541755

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 &currentDistanceAlongSegment,
1757+
double &characterStartX, double &characterStartY, double &characterEndX, double &characterEndY ) const
17571758
{
17581759
// Coordinates this character will start at
1759-
if ( qgsDoubleNear( segment_length, 0.0 ) )
1760+
if ( qgsDoubleNear( segmentLength, 0.0 ) )
17601761
{
17611762
// Not allowed to place across on 0 length segments or discontinuities
17621763
return false;
17631764
}
17641765

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];
17671768

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];
17701771

1771-
double dx = new_x - old_x;
1772-
double dy = new_y - old_y;
1772+
double segmentDx = segmentEndX - segmentStartX;
1773+
double segmentDy = segmentEndY - segmentStartY;
17731774

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;
17761777

17771778
// Coordinates this character ends at, calculated below
1778-
end_x = 0;
1779-
end_y = 0;
1779+
characterEndX = 0;
1780+
characterEndY = 0;
17801781

1781-
if ( segment_length - distance >= charWidth )
1782+
if ( segmentLength - currentDistanceAlongSegment >= charWidth )
17821783
{
17831784
// 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;
17871788
}
17881789
else
17891790
{
17901791
// If there isn't enough distance left on this segment
17911792
// then we need to search until we find the line segment that ends further than ci.width away
17921793
do
17931794
{
1794-
old_x = new_x;
1795-
old_y = new_y;
1795+
segmentStartX = segmentEndX;
1796+
segmentStartY = segmentEndY;
17961797
index++;
17971798
if ( index >= path_positions->nbPoints ) // Bail out if we run off the end of the shape
17981799
{
17991800
return false;
18001801
}
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;
18051806
}
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_
18071808

18081809
// 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 );
18101811

18111812
// 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 ) );
18131814
}
18141815
return true;
18151816
}

‎src/core/pal/feature.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,13 @@ namespace pal
200200
* \param path_positions line path to place label on
201201
* \param path_distances array of distances to each segment on path
202202
* \param orientation can be 0 for automatic calculation of orientation, or -1/+1 for a specific label orientation
203-
* \param index
204203
* \param distance distance to offset label along curve by
205204
* \param reversed if true label is reversed from lefttoright to righttoleft
206205
* \param flip if true label is placed on the other side of the line
207206
* \returns calculated label position
208207
*/
209208
LabelPosition *curvedPlacementAtOffset( PointSet *path_positions, double *path_distances,
210-
int &orientation, int index, double distance, bool &reversed, bool &flip );
209+
int &orientation, double distance, bool &reversed, bool &flip );
211210

212211
/**
213212
* Generate curved candidates for line features.
@@ -298,8 +297,8 @@ namespace pal
298297
bool showUprightLabels() const;
299298

300299
//! Returns true if the next char position is found. The referenced parameters are updated.
301-
bool nextCharPosition( double charWidth, double segment_length, PointSet *path_positions, int &index, double &distance,
302-
double &start_x, double &start_y, double &end_x, double &end_y ) const;
300+
bool nextCharPosition( double charWidth, double segmentLength, PointSet *path_positions, int &index, double &currentDistanceAlongSegment,
301+
double &characterStartX, double &characterStartY, double &characterEndX, double &characterEndY ) const;
303302

304303
protected:
305304

0 commit comments

Comments
 (0)