Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simplify code
  • Loading branch information
nyalldawson committed Dec 10, 2019
1 parent e28ad5a commit 617defd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 77 deletions.
6 changes: 0 additions & 6 deletions src/core/pal/labelposition.h
Expand Up @@ -295,12 +295,6 @@ namespace pal
*/
void insertIntoIndex( QgsGenericSpatialIndex<LabelPosition> &index );

struct PruneCtx
{
Pal *pal = nullptr;
FeaturePart *obstacle = nullptr;
};

//! Check whether the candidate in ctx overlap with obstacle feat
static bool pruneCallback( LabelPosition *candidatePosition, void *ctx );

Expand Down
21 changes: 5 additions & 16 deletions src/core/pal/pal.cpp
Expand Up @@ -102,11 +102,6 @@ struct ObstacleCallBackCtx
Pal *pal = nullptr;
};

struct FilterContext
{
QgsGenericSpatialIndex<LabelPosition> *allCandidatesIndex = nullptr;
Pal *pal = nullptr;
};

std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeometry &mapBoundary )
{
Expand Down Expand Up @@ -294,19 +289,13 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom
// Filtering label positions against obstacles
amin[0] = amin[1] = std::numeric_limits<double>::lowest();
amax[0] = amax[1] = std::numeric_limits<double>::max();
FilterContext filterCtx;
filterCtx.allCandidatesIndex = &prob->mAllCandidatesIndex;
filterCtx.pal = this;
obstacles.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&filterCtx]( const FeaturePart * part )->bool
obstacles.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&prob, this]( const FeaturePart * part )->bool
{
if ( filterCtx.pal->isCanceled() )
if ( isCanceled() )
return false; // do not continue searching

LabelPosition::PruneCtx pruneContext;
pruneContext.obstacle = const_cast< FeaturePart * >( part );
pruneContext.pal = filterCtx.pal;
filterCtx.allCandidatesIndex->intersects( part->boundingBox(), [&pruneContext]( const LabelPosition * candidatePosition ) -> bool{
FeaturePart *obstaclePart = pruneContext.obstacle;
prob->allCandidatesIndex().intersects( part->boundingBox(), [part, this]( const LabelPosition * candidatePosition ) -> bool{
FeaturePart *obstaclePart = const_cast< FeaturePart * >( part );

// test whether we should ignore this obstacle for the candidate. We do this if:
// 1. it's not a hole, and the obstacle belongs to the same label feature as the candidate (e.g.,
Expand All @@ -319,7 +308,7 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom
return true;
}

CostCalculator::addObstacleCostPenalty( const_cast< LabelPosition * >( candidatePosition ), obstaclePart, pruneContext.pal );
CostCalculator::addObstacleCostPenalty( const_cast< LabelPosition * >( candidatePosition ), obstaclePart, this );

return true;
} );
Expand Down
64 changes: 9 additions & 55 deletions src/core/pal/problem.cpp
Expand Up @@ -132,36 +132,20 @@ void Problem::reduce()
delete[] ok;
}

struct FalpContext
{
PriorityQueue *list = nullptr;
const LabelPosition *lp = nullptr;
QgsGenericSpatialIndex< LabelPosition > *candidatesIndex = nullptr;
};


void ignoreLabel( const LabelPosition *lp, PriorityQueue &list, QgsGenericSpatialIndex< LabelPosition > &candidatesIndex )
{
FalpContext context;
context.candidatesIndex = nullptr;
context.list = &list;

if ( list.isIn( lp->getId() ) )
{
list.remove( lp->getId() );

context.lp = lp;
double amin[2];
double amax[2];
lp->getBoundingBox( amin, amax );
candidatesIndex.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&context]( const LabelPosition * lp )->bool
candidatesIndex.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [lp, &list]( const LabelPosition * lp2 )->bool
{
const LabelPosition *lp2 = context.lp;
PriorityQueue *list = context.list;

if ( lp->getId() != lp2->getId() && list->isIn( lp->getId() ) && lp->isInConflict( lp2 ) )
if ( lp2->getId() != lp->getId() && list.isIn( lp2->getId() ) && lp2->isInConflict( lp ) )
{
list->decreaseKey( lp->getId() );
list.decreaseKey( lp2->getId() );
}
return true;
} );
Expand All @@ -183,10 +167,6 @@ void Problem::init_sol_falp()
double amin[2];
double amax[2];

FalpContext context;
context.candidatesIndex = &mAllCandidatesIndex;
context.list = &list;

LabelPosition *lp = nullptr;

for ( i = 0; i < static_cast< int >( mFeatureCount ); i++ )
Expand Down Expand Up @@ -230,15 +210,11 @@ void Problem::init_sol_falp()

lp->getBoundingBox( amin, amax );

context.lp = lp;
mAllCandidatesIndex.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&context]( const LabelPosition * lp ) ->bool
mAllCandidatesIndex.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&list, lp, this]( const LabelPosition * lp2 ) ->bool
{
const LabelPosition *lp2 = context.lp;
PriorityQueue *list = context.list;

if ( lp2->isInConflict( lp ) )
if ( lp->isInConflict( lp2 ) )
{
ignoreLabel( lp, *list, *context.candidatesIndex );
ignoreLabel( lp2, list, mAllCandidatesIndex );
}
return true;
} );
Expand Down Expand Up @@ -620,12 +596,6 @@ inline Chain *Problem::chain( int seed )
return retainedChain;
}

struct NokContext
{
LabelPosition *lp = nullptr;
bool *ok = nullptr;
int *wrap = nullptr;
};

void Problem::chain_search()
{
Expand All @@ -639,10 +609,6 @@ void Problem::chain_search()
int lid;
int popit = 0;

NokContext context;
context.ok = ok;
context.wrap = nullptr;

Chain *retainedChain = nullptr;

std::fill( ok, ok + mFeatureCount, false );
Expand Down Expand Up @@ -689,24 +655,12 @@ void Problem::chain_search()
{
LabelPosition *old = mLabelPositions[ mSol.activeLabelIds[fid] ].get();
old->removeFromIndex( mActiveCandidatesIndex );
context.lp = old;
old->getBoundingBox( amin, amax );
mAllCandidatesIndex.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&context]( const LabelPosition * lp ) ->bool
mAllCandidatesIndex.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&ok, old]( const LabelPosition * lp ) ->bool
{
LabelPosition *lp2 = context.lp;
bool *ok = context.ok;
int *wrap = context.wrap;

if ( lp2->isInConflict( lp ) )
if ( old->isInConflict( lp ) )
{
if ( wrap )
{
ok[wrap[lp->getProblemFeatureId()]] = false;
}
else
{
ok[lp->getProblemFeatureId()] = false;
}
ok[lp->getProblemFeatureId()] = false;
}

return true;
Expand Down
5 changes: 5 additions & 0 deletions src/core/pal/problem.h
Expand Up @@ -139,6 +139,11 @@ namespace pal
return &mPositionsWithNoCandidates;
}

/**
* Returns the index containing all label candidates.
*/
QgsGenericSpatialIndex<LabelPosition> &allCandidatesIndex() { return mAllCandidatesIndex; }

private:

/**
Expand Down

0 comments on commit 617defd

Please sign in to comment.