Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Modernize some more pal code, remove unused code
  • Loading branch information
nyalldawson committed Nov 28, 2019
1 parent 3cbb3a0 commit b0f4cb4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 81 deletions.
42 changes: 21 additions & 21 deletions src/core/pal/pal.cpp
Expand Up @@ -42,6 +42,7 @@
#include "internalexception.h"
#include "util.h"
#include <cfloat>
#include <list>

using namespace pal;

Expand Down Expand Up @@ -94,7 +95,9 @@ Layer *Pal::addLayer( QgsAbstractLabelProvider *provider, const QString &layerNa
typedef struct _featCbackCtx
{
Layer *layer = nullptr;
QLinkedList<Feats *> *fFeats;

std::list< std::unique_ptr< Feats > > *fFeats = nullptr;

RTree<FeaturePart *, double, 2, double> *obstacles;
RTree<LabelPosition *, double, 2, double> *candidates;
QList<LabelPosition *> *positionsWithNoCandidates;
Expand Down Expand Up @@ -157,12 +160,12 @@ bool extractFeatCallback( FeaturePart *featurePart, void *ctx )
std::sort( candidates.begin(), candidates.end(), CostCalculator::candidateSortGrow );

// valid features are added to fFeats
Feats *ft = new Feats();
std::unique_ptr< Feats > ft = qgis::make_unique< Feats >();
ft->feature = featurePart;
ft->shape = nullptr;
ft->candidates = candidates;
ft->priority = featurePart->calculatePriority();
context->fFeats->append( ft );
context->fFeats->emplace_back( std::move( ft ) );
}
else
{
Expand Down Expand Up @@ -230,7 +233,7 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom
RTree<FeaturePart *, double, 2, double> obstacles;
std::unique_ptr< Problem > prob = qgis::make_unique< Problem >();

int i, j;
int j;

double bbx[4];
double bby[4];
Expand All @@ -249,7 +252,7 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom

prob->pal = this;

QLinkedList<Feats *> fFeats;
std::list< std::unique_ptr< Feats > > fFeats;

FeatCallBackCtx context;

Expand All @@ -270,7 +273,7 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom

// first step : extract features from layers

int previousFeatureCount = 0;
std::size_t previousFeatureCount = 0;
int previousObstacleCount = 0;

QStringList layersWithFeaturesInBBox;
Expand Down Expand Up @@ -323,11 +326,8 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom
prob->featStartId = new int [prob->mFeatureCount];
prob->inactiveCost = new double[prob->mFeatureCount];


if ( !fFeats.isEmpty() )
if ( !fFeats.empty() )
{
Feats *feat = nullptr;

// Filtering label positions against obstacles
amin[0] = amin[1] = std::numeric_limits<double>::lowest();
amax[0] = amax[1] = std::numeric_limits<double>::max();
Expand All @@ -338,20 +338,20 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom

if ( isCanceled() )
{
for ( Feats *feat : qgis::as_const( fFeats ) )
for ( const std::unique_ptr< Feats > &feat : qgis::as_const( fFeats ) )
{
qDeleteAll( feat->candidates );
feat->candidates.clear();
}

qDeleteAll( fFeats );
return nullptr;
}

int idlp = 0;
for ( i = 0; i < prob->mFeatureCount; i++ ) /* foreach feature into prob */
for ( std::size_t i = 0; i < prob->mFeatureCount; i++ ) /* foreach feature into prob */
{
feat = fFeats.takeFirst();
std::unique_ptr< Feats > feat = std::move( fFeats.front() );
fFeats.pop_front();

prob->featStartId[i] = idlp;
prob->inactiveCost[i] = std::pow( 2, 10 - 10 * feat->priority );
Expand All @@ -370,7 +370,7 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom
}

// sort candidates by cost, skip less interesting ones, calculate polygon costs (if using polygons)
max_p = CostCalculator::finalizeCandidatesCosts( feat, max_p, &obstacles, bbx, bby );
max_p = CostCalculator::finalizeCandidatesCosts( feat.get(), max_p, &obstacles, bbx, bby );

// only keep the 'max_p' best candidates
while ( feat->candidates.count() > max_p )
Expand All @@ -391,26 +391,27 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom
//lp->insertIntoIndex(prob->candidates);
lp->setProblemIds( i, idlp ); // bugfix #1 (maxence 10/23/2008)
}
fFeats.append( feat );
fFeats.emplace_back( std::move( feat ) );
}

int nbOverlaps = 0;

while ( !fFeats.isEmpty() ) // foreach feature
while ( !fFeats.empty() ) // foreach feature
{
if ( isCanceled() )
{
for ( Feats *feat : qgis::as_const( fFeats ) )
for ( const std::unique_ptr< Feats > &feat : qgis::as_const( fFeats ) )
{
qDeleteAll( feat->candidates );
feat->candidates.clear();
}

qDeleteAll( fFeats );
return nullptr;
}

feat = fFeats.takeFirst();
std::unique_ptr< Feats > feat = std::move( fFeats.front() );
fFeats.pop_front();

while ( !feat->candidates.isEmpty() ) // foreach label candidate
{
lp = feat->candidates.takeFirst();
Expand All @@ -429,7 +430,6 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom

nbOverlaps += lp->getNumOverlaps();
}
delete feat;
}
nbOverlaps /= 2;
prob->all_nblp = prob->mTotalCandidates;
Expand Down
62 changes: 5 additions & 57 deletions src/core/pal/problem.cpp
Expand Up @@ -107,7 +107,7 @@ void Problem::reduce()
while ( run )
{
run = false;
for ( i = 0; i < mFeatureCount; i++ )
for ( i = 0; i < static_cast< int >( mFeatureCount ); i++ )
{
// ok[i] = true;
for ( j = 0; j < featNbLp[i]; j++ ) // foreach candidate
Expand Down Expand Up @@ -231,7 +231,7 @@ void Problem::init_sol_falp()

LabelPosition *lp = nullptr;

for ( i = 0; i < mFeatureCount; i++ )
for ( i = 0; i < static_cast< int >( mFeatureCount ); i++ )
for ( j = 0; j < featNbLp[i]; j++ )
{
label = featStartId[i] + j;
Expand Down Expand Up @@ -292,7 +292,7 @@ void Problem::init_sol_falp()
LabelPosition *retainedLabel = nullptr;
int p;

for ( i = 0; i < mFeatureCount; i++ ) // forearch hidden feature
for ( i = 0; i < static_cast< int >( mFeatureCount ); i++ ) // forearch hidden feature
{
if ( sol.activeLabelIds[i] == -1 )
{
Expand Down Expand Up @@ -786,7 +786,7 @@ QList<LabelPosition *> Problem::getSolution( bool returnInactive, QList<LabelPos
int i;
QList<LabelPosition *> solList;

for ( i = 0; i < mFeatureCount; i++ )
for ( i = 0; i < static_cast< int >( mFeatureCount ); i++ )
{
if ( sol.activeLabelIds[i] != -1 )
{
Expand All @@ -811,58 +811,6 @@ QList<LabelPosition *> Problem::getSolution( bool returnInactive, QList<LabelPos
return solList;
}

PalStat *Problem::getStats()
{
int i, j;

PalStat *stats = new PalStat();

stats->nbObjects = mFeatureCount;
stats->nbLabelledObjects = 0;

stats->nbLayers = mLayerCount;
stats->layersNbObjects = new int[stats->nbLayers];
stats->layersNbLabelledObjects = new int[stats->nbLayers];

for ( i = 0; i < stats->nbLayers; i++ )
{
stats->layersName << labelledLayersName[i];
stats->layersNbObjects[i] = 0;
stats->layersNbLabelledObjects[i] = 0;
}

QString lyrName;
int k;
for ( i = 0; i < mFeatureCount; i++ )
{
lyrName = mLabelPositions.at( featStartId[i] )->getFeaturePart()->feature()->provider()->name();
k = -1;
for ( j = 0; j < stats->nbLayers; j++ )
{
if ( lyrName == stats->layersName[j] )
{
k = j;
break;
}
}
if ( k != -1 )
{
stats->layersNbObjects[k]++;
if ( sol.activeLabelIds[i] >= 0 )
{
stats->layersNbLabelledObjects[k]++;
stats->nbLabelledObjects++;
}
}
else
{
// unknown layer
}
}

return stats;
}

void Problem::solution_cost()
{
sol.totalCost = 0.0;
Expand All @@ -881,7 +829,7 @@ void Problem::solution_cost()

int nbHidden = 0;

for ( i = 0; i < mFeatureCount; i++ )
for ( i = 0; i < static_cast< int >( mFeatureCount ); i++ )
{
if ( sol.activeLabelIds[i] == -1 )
{
Expand Down
4 changes: 1 addition & 3 deletions src/core/pal/problem.h
Expand Up @@ -119,8 +119,6 @@ namespace pal
*/
QList<LabelPosition *> getSolution( bool returnInactive, QList<LabelPosition *> *unlabeled = nullptr );

PalStat *getStats();

/* useful only for postscript post-conversion*/
//void toFile(char *label_file);

Expand Down Expand Up @@ -162,7 +160,7 @@ namespace pal
/**
* Total number of features to label.
*/
int mFeatureCount = 0;
std::size_t mFeatureCount = 0;

/**
* if TRUE, special value -1 is prohibited
Expand Down

0 comments on commit b0f4cb4

Please sign in to comment.