Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[pal] When set to draw all label candidates, shade candidates which
conflict with obstacles in red
  • Loading branch information
nyalldawson committed Aug 3, 2015
1 parent 47e3e20 commit dfff125
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 52 deletions.
17 changes: 10 additions & 7 deletions src/core/pal/costcalculator.cpp
Expand Up @@ -72,11 +72,14 @@ namespace pal
break;
}

if ( n > 0 )
lp->setConflictsWithObstacle( true );

//scale cost by obstacle's factor
double obstacleCost = obstacle->getFeature()->obstacleFactor() * double( n );

// label cost is penalized
lp->setCost( lp->getCost() + obstacleCost );
lp->setCost( lp->cost() + obstacleCost );
}


Expand All @@ -101,8 +104,8 @@ namespace pal


// define the value's range
double cost_max = lPos[0]->getCost();
double cost_min = lPos[max_p-1]->getCost();
double cost_max = lPos[0]->cost();
double cost_min = lPos[max_p-1]->cost();

cost_max -= cost_min;

Expand All @@ -125,12 +128,12 @@ namespace pal
//if (cost_max - cost_min < EPSILON)
if ( cost_max > EPSILON )
{
lPos[i]->cost = 0.0021 - ( lPos[i]->getCost() - cost_min ) * normalizer;
lPos[i]->mCost = 0.0021 - ( lPos[i]->cost() - cost_min ) * normalizer;
}
else
{
//lPos[i]->cost = 0.0001 + (lPos[i]->cost - cost_min) * normalizer;
lPos[i]->cost = 0.0001;
lPos[i]->mCost = 0.0001;
}

#ifdef _DEBUG_
Expand Down Expand Up @@ -183,10 +186,10 @@ namespace pal
do
{
discrim += 1.0;
for ( stop = 0; stop < feat->nblp && feat->lPos[stop]->getCost() < discrim; stop++ )
for ( stop = 0; stop < feat->nblp && feat->lPos[stop]->cost() < discrim; stop++ )
;
}
while ( stop == 0 && discrim < feat->lPos[feat->nblp-1]->getCost() + 2.0 );
while ( stop == 0 && discrim < feat->lPos[feat->nblp-1]->cost() + 2.0 );

if ( discrim > 1.5 )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/pal/feature.cpp
Expand Up @@ -1349,7 +1349,7 @@ namespace pal
// apply the penalty
for ( int i = 0; i < nbp; i++ )
{
lPos[i]->setCost( lPos[i]->getCost() + sizeCost / 100 );
lPos[i]->setCost( lPos[i]->cost() + sizeCost / 100 );
}
}

Expand Down
23 changes: 10 additions & 13 deletions src/core/pal/labelposition.cpp
Expand Up @@ -50,7 +50,6 @@ namespace pal
LabelPosition::LabelPosition( int id, double x1, double y1, double w, double h, double alpha, double cost, FeaturePart *feature, bool isReversed, Quadrant quadrant )
: PointSet()
, id( id )
, cost( cost )
, feature( feature )
, probFeat( 0 )
, nbOverlap( 0 )
Expand All @@ -62,6 +61,8 @@ namespace pal
, reversed( isReversed )
, upsideDown( false )
, quadrant( quadrant )
, mCost( cost )
, mHasObstacleConflict( false )
{
type = GEOS_POLYGON;
nbPoints = 4;
Expand Down Expand Up @@ -167,7 +168,7 @@ namespace pal
: PointSet( other )
{
id = other.id;
cost = other.cost;
mCost = other.mCost;
feature = other.feature;
probFeat = other.probFeat;
nbOverlap = other.nbOverlap;
Expand All @@ -186,6 +187,7 @@ namespace pal
upsideDown = other.upsideDown;
reversed = other.reversed;
quadrant = other.quadrant;
mHasObstacleConflict = other.mHasObstacleConflict;
}

bool LabelPosition::isIn( double *bbox )
Expand Down Expand Up @@ -242,7 +244,7 @@ namespace pal
void LabelPosition::print()
{
// std::cout << feature->getLayer()->getName() << "/" << feature->getUID() << "/" << id;
std::cout << " cost: " << cost;
std::cout << " cost: " << mCost;
std::cout << " alpha" << alpha << std::endl;
std::cout << x[0] << ", " << y[0] << std::endl;
std::cout << x[1] << ", " << y[1] << std::endl;
Expand Down Expand Up @@ -329,17 +331,12 @@ namespace pal
return alpha;
}

double LabelPosition::getCost() const
{
return cost;
}

void LabelPosition::validateCost()
{
if ( cost >= 1 )
if ( mCost >= 1 )
{
// std::cout << " Warning: lp->cost == " << cost << " (from feat: " << feature->getUID() << "/" << getLayerName() << ")" << std::endl;
cost -= int ( cost ); // label cost up to 1
mCost -= int ( mCost ); // label cost up to 1
}
}

Expand Down Expand Up @@ -382,12 +379,12 @@ namespace pal

bool LabelPosition::costShrink( void *l, void *r )
{
return (( LabelPosition* ) l )->cost < (( LabelPosition* ) r )->cost;
return (( LabelPosition* ) l )->mCost < (( LabelPosition* ) r )->mCost;
}

bool LabelPosition::costGrow( void *l, void *r )
{
return (( LabelPosition* ) l )->cost > (( LabelPosition* ) r )->cost;
return (( LabelPosition* ) l )->mCost > (( LabelPosition* ) r )->mCost;
}


Expand Down Expand Up @@ -469,7 +466,7 @@ namespace pal
std::cout << "count overlap : " << lp->id << "<->" << lp2->id << std::endl;
#endif
( *nbOv ) ++;
*cost += inactiveCost[lp->probFeat] + lp->getCost();
*cost += inactiveCost[lp->probFeat] + lp->cost();

}

Expand Down
32 changes: 24 additions & 8 deletions src/core/pal/labelposition.h
Expand Up @@ -92,7 +92,6 @@ namespace pal

~LabelPosition() { delete nextPart; }


/**
* \brief Is the labelposition in the bounding-box ? (intersect or inside????)
*
Expand Down Expand Up @@ -163,14 +162,27 @@ namespace pal
/** Return pointer to layer's name. used for stats */
QString getLayerName() const;

/**
* \brief get the position geographical cost
* \return geographical cost
/** Returns the candidate label position's geographical cost.
* @see setCost
*/
double cost() const { return mCost; }

/** Sets the candidate label position's geographical cost.
* @param newCost new cost for position
* @see cost
*/
void setCost( double newCost ) { mCost = newCost; }

/** Sets whether the position is marked as conflicting with an obstacle feature.
* @param conflicts set to true to mark candidate as being in conflict
* @see conflictsWithObstacle
*/
double getCost() const;
void setConflictsWithObstacle( bool conflicts ) { mHasObstacleConflict = conflicts; }

/** Modify candidate's cost */
void setCost( double newCost ) { cost = newCost; }
/** Returns whether the position is marked as conflicting with an obstacle feature.
* @see setConflictsWithObstacle
*/
bool conflictsWithObstacle() const { return mHasObstacleConflict; }

/** Make sure the cost is less than 1 */
void validateCost();
Expand Down Expand Up @@ -250,7 +262,7 @@ namespace pal
protected:

int id;
double cost;

FeaturePart *feature;

// bug # 1 (maxence 10/23/2008)
Expand All @@ -277,6 +289,10 @@ namespace pal
bool isInConflictSinglePart( LabelPosition* lp );
bool isInConflictMultiPart( LabelPosition* lp );

private:
double mCost;
bool mHasObstacleConflict;

};

} // end namespace
Expand Down
42 changes: 21 additions & 21 deletions src/core/pal/problem.cpp
Expand Up @@ -758,7 +758,7 @@ namespace pal
context.lp = lp;
candidates_subsol->Search( amin, amax, LabelPosition::countFullOverlapCallback, ( void* ) &context );

cost += lp->getCost();
cost += lp->cost();
}
else
{
Expand Down Expand Up @@ -1068,13 +1068,13 @@ namespace pal
else
{
delta = -labelPositionCost[sol[feat_id]];
delta -= nbOlap[sol[feat_id]] * ( inactiveCost[feat_sub_id] + labelpositions[label_id]->getCost() );
delta -= nbOlap[sol[feat_id]] * ( inactiveCost[feat_sub_id] + labelpositions[label_id]->cost() );
}

if ( j >= 0 )
{
delta += labelPositionCost[featStartId[feat_sub_id] + j];
delta += nbOlap[featStartId[feat_sub_id] + j] * ( inactiveCost[feat_sub_id] + labelpositions[featStartId[feat_sub_id] + j]->getCost() );
delta += nbOlap[featStartId[feat_sub_id] + j] * ( inactiveCost[feat_sub_id] + labelpositions[featStartId[feat_sub_id] + j]->cost() );
}
else
{
Expand Down Expand Up @@ -1190,7 +1190,7 @@ namespace pal

lp->getBoundingBox( amin, amax );

context.diff_cost = -local_inactive - labelpositions[old_label]->getCost();
context.diff_cost = -local_inactive - labelpositions[old_label]->cost();
context.lp = labelpositions[old_label];

candidates->Search( amin, amax, updateCandidatesCost, &context );
Expand All @@ -1202,7 +1202,7 @@ namespace pal

lp->getBoundingBox( amin, amax );

context.diff_cost = local_inactive + labelpositions[choosed_label]->getCost();
context.diff_cost = local_inactive + labelpositions[choosed_label]->cost();
context.lp = labelpositions[choosed_label];


Expand Down Expand Up @@ -1344,7 +1344,7 @@ namespace pal
if ( !ctx->conflicts->contains( feat ) )
{
ctx->conflicts->append( feat );
*ctx->delta_tmp += lp->getCost() + ctx->inactiveCost[rfeat];
*ctx->delta_tmp += lp->cost() + ctx->inactiveCost[rfeat];
}
}
return true;
Expand Down Expand Up @@ -1414,7 +1414,7 @@ namespace pal
if ( tmpsol[seed] == -1 )
delta -= inactiveCost[subseed];
else
delta -= labelpositions[tmpsol[seed]]->getCost();
delta -= labelpositions[tmpsol[seed]]->cost();

// TODO modify to handle displayAll param
for ( i = -1; i < seedNbLp; i++ )
Expand Down Expand Up @@ -1448,7 +1448,7 @@ namespace pal
// no conflict -> end of chain
if ( conflicts->size() == 0 )
{
if ( !retainedChain || delta + labelpositions[lid]->getCost() < delta_best )
if ( !retainedChain || delta + labelpositions[lid]->cost() < delta_best )
{

if ( retainedChain )
Expand All @@ -1461,7 +1461,7 @@ namespace pal
retainedChain = new Chain(); // HERE
}

delta_best = delta + labelpositions[lid]->getCost();
delta_best = delta + labelpositions[lid]->cost();

retainedChain->degree = currentChain->size() + 1;
retainedChain->feat = new int[retainedChain->degree]; // HERE
Expand All @@ -1479,7 +1479,7 @@ namespace pal
}
retainedChain->feat[j] = seed;
retainedChain->label[j] = lid;
retainedChain->delta = delta + labelpositions[retainedChain->label[j]]->getCost();
retainedChain->delta = delta + labelpositions[retainedChain->label[j]]->cost();
}
}

Expand Down Expand Up @@ -1519,7 +1519,7 @@ namespace pal

newChain->feat[j] = seed;
newChain->label[j] = lid;
newChain->delta = delta + labelpositions[newChain->label[j]]->getCost();
newChain->delta = delta + labelpositions[newChain->label[j]]->cost();
j++;


Expand Down Expand Up @@ -1622,7 +1622,7 @@ namespace pal
}

tmpsol[seed] = retainedLabel;
delta += labelpositions[retainedLabel]->getCost();
delta += labelpositions[retainedLabel]->cost();
seed = next_seed;
}
}
Expand Down Expand Up @@ -1707,7 +1707,7 @@ namespace pal
if ( tmpsol[seed] == -1 )
delta -= inactiveCost[seed];
else
delta -= labelpositions[tmpsol[seed]]->getCost();
delta -= labelpositions[tmpsol[seed]]->cost();

for ( i = -1; i < seedNbLp; i++ )
{
Expand Down Expand Up @@ -1735,7 +1735,7 @@ namespace pal
// no conflict -> end of chain
if ( conflicts->size() == 0 )
{
if ( !retainedChain || delta + labelpositions[lid]->getCost() < delta_best )
if ( !retainedChain || delta + labelpositions[lid]->cost() < delta_best )
{
if ( retainedChain )
{
Expand All @@ -1747,7 +1747,7 @@ namespace pal
retainedChain = new Chain();
}

delta_best = delta + labelpositions[lid]->getCost();
delta_best = delta + labelpositions[lid]->cost();

retainedChain->degree = currentChain->size() + 1;
retainedChain->feat = new int[retainedChain->degree];
Expand All @@ -1765,7 +1765,7 @@ namespace pal
}
retainedChain->feat[j] = seed;
retainedChain->label[j] = lid;
retainedChain->delta = delta + labelpositions[lid]->getCost();
retainedChain->delta = delta + labelpositions[lid]->cost();
}
}

Expand Down Expand Up @@ -1807,7 +1807,7 @@ namespace pal
// add the current candidates into the chain
newChain->feat[j] = seed;
newChain->label[j] = lid;
newChain->delta = delta + labelpositions[newChain->label[j]]->getCost();
newChain->delta = delta + labelpositions[newChain->label[j]]->cost();
j++;

// hide all conflictual candidates
Expand Down Expand Up @@ -1910,7 +1910,7 @@ namespace pal


tmpsol[seed] = retainedLabel;
delta += labelpositions[retainedLabel]->getCost();
delta += labelpositions[retainedLabel]->cost();
seed = next_seed;
}
}
Expand Down Expand Up @@ -2170,7 +2170,7 @@ namespace pal
candidates[i]->feat_id = i + borderSize;
candidatesUnsorted[i] = candidates[i];

candidates[i]->cost = ( sol[i+borderSize] == -1 ? inactiveCost[i+borderSize] : labelpositions[sol[i+borderSize]]->getCost() );
candidates[i]->cost = ( sol[i+borderSize] == -1 ? inactiveCost[i+borderSize] : labelpositions[sol[i+borderSize]]->cost() );
}

sort(( void** ) candidates, probSize, decreaseCost );
Expand Down Expand Up @@ -2287,7 +2287,7 @@ namespace pal
std::cout << "label[lid]->cost: " << labelpositions[lid]->cost << std::endl;
}
#endif
candidatesUnsorted[fid-borderSize]->cost = ( lid == -1 ? inactiveCost[sub[fid]] : labelpositions[lid]->getCost() );
candidatesUnsorted[fid-borderSize]->cost = ( lid == -1 ? inactiveCost[sub[fid]] : labelpositions[lid]->cost() );

}

Expand Down Expand Up @@ -2733,7 +2733,7 @@ namespace pal
context.lp = lp;
candidates_sol->Search( amin, amax, LabelPosition::countFullOverlapCallback, &context );

sol->cost += lp->getCost();
sol->cost += lp->cost();

if ( nbOv == 0 )
nbActive++;
Expand Down

0 comments on commit dfff125

Please sign in to comment.