Skip to content

Commit

Permalink
[pal] Fix regression in placement for free/horizontal polygon labels
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 22, 2015
1 parent 0559939 commit 307b1b9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions src/core/pal/costcalculator.cpp
Expand Up @@ -95,26 +95,35 @@ namespace pal

////////

void CostCalculator::setPolygonCandidatesCost( int nblp, QList< LabelPosition* >& lPos, int max_p, RTree<FeaturePart*, double, 2, double> *obstacles, double bbx[4], double bby[4] )
void CostCalculator::setPolygonCandidatesCost( int nblp, QList< LabelPosition* >& lPos, RTree<FeaturePart*, double, 2, double> *obstacles, double bbx[4], double bby[4] )
{
int i;

double normalizer;
// compute raw cost
#ifdef _DEBUG_
std::cout << "LabelPosition for feat: " << lPos[0]->feature->uid << std::endl;
#endif

for ( i = 0; i < nblp; i++ )
setCandidateCostFromPolygon( lPos[i], obstacles, bbx, bby );
for ( int i = 0; i < nblp; ++i )
setCandidateCostFromPolygon( lPos.at( i ), obstacles, bbx, bby );

// lPos with big values came first (value = min distance from label to Polygon's Perimeter)
qSort( lPos.begin(), lPos.end(), candidateSortShrink );
// IMPORTANT - only want to sort first nblp positions. The rest have not had the cost
// calculated so will have nonsense values
QList< LabelPosition* > toSort;
for ( int i = 0; i < nblp; ++i )
{
toSort << lPos.at( i );
}
qSort( toSort.begin(), toSort.end(), candidateSortShrink );
for ( int i = 0; i < nblp; ++i )
{
lPos[i] = toSort.at( i );
}


// define the value's range
double cost_max = lPos.at( 0 )->cost();
double cost_min = lPos.at( max_p - 1 )->cost();
double cost_min = lPos.at( nblp - 1 )->cost();

cost_max -= cost_min;

Expand All @@ -129,20 +138,20 @@ namespace pal

// adjust cost => the best is 0.0001, the worst is 0.0021
// others are set proportionally between best and worst
for ( i = 0; i < max_p; i++ )
for ( int i = 0; i < nblp; ++i )
{
#ifdef _DEBUG_
std::cout << " lpos[" << i << "] = " << lPos[i]->cost;
#endif
//if (cost_max - cost_min < EPSILON)
if ( cost_max > EPSILON )
{
lPos[i]->mCost = 0.0021 - ( lPos.at( i )->cost() - cost_min ) * normalizer;
lPos.at( i )->setCost( 0.0021 - ( lPos.at( i )->cost() - cost_min ) * normalizer );
}
else
{
//lPos[i]->cost = 0.0001 + (lPos[i]->cost - cost_min) * normalizer;
lPos[i]->mCost = 0.0001;
lPos.at( i )->setCost( 0.0001 );
}

#ifdef _DEBUG_
Expand Down Expand Up @@ -220,7 +229,7 @@ namespace pal
{
int arrangement = feat->feature->layer()->arrangement();
if ( arrangement == P_FREE || arrangement == P_HORIZ )
setPolygonCandidatesCost( stop, feat->lPos, max_p, obstacles, bbx, bby );
setPolygonCandidatesCost( stop, feat->lPos, obstacles, bbx, bby );
}

// add size penalty (small lines/polygons get higher cost)
Expand Down
2 changes: 1 addition & 1 deletion src/core/pal/costcalculator.h
Expand Up @@ -28,7 +28,7 @@ namespace pal
/** Increase candidate's cost according to its collision with passed feature */
static void addObstacleCostPenalty( LabelPosition* lp, pal::FeaturePart *obstacle );

static void setPolygonCandidatesCost( int nblp, QList< LabelPosition* >& lPos, int max_p, RTree<pal::FeaturePart*, double, 2, double> *obstacles, double bbx[4], double bby[4] );
static void setPolygonCandidatesCost( int nblp, QList< LabelPosition* >& lPos, RTree<pal::FeaturePart*, double, 2, double> *obstacles, double bbx[4], double bby[4] );

/** Set cost to the smallest distance between lPos's centroid and a polygon stored in geoetry field */
static void setCandidateCostFromPolygon( LabelPosition* lp, RTree<pal::FeaturePart *, double, 2, double> *obstacles, double bbx[4], double bby[4] );
Expand Down

0 comments on commit 307b1b9

Please sign in to comment.