Skip to content

Commit 077c507

Browse files
committedNov 28, 2019
More pal modernization of memory management, finally ownership is starting to become clear...
1 parent b0f4cb4 commit 077c507

File tree

6 files changed

+117
-161
lines changed

6 files changed

+117
-161
lines changed
 

‎src/core/pal/costcalculator.cpp

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525

2626
using namespace pal;
2727

28-
bool CostCalculator::candidateSortGrow( const LabelPosition *c1, const LabelPosition *c2 )
28+
bool CostCalculator::candidateSortGrow( const std::unique_ptr< LabelPosition > &c1, const std::unique_ptr< LabelPosition > &c2 )
2929
{
3030
return c1->cost() < c2->cost();
3131
}
3232

33-
bool CostCalculator::candidateSortShrink( const LabelPosition *c1, const LabelPosition *c2 )
33+
bool CostCalculator::candidateSortShrink( const std::unique_ptr< LabelPosition > &c1, const std::unique_ptr< LabelPosition > &c2 )
3434
{
3535
return c1->cost() > c2->cost();
3636
}
@@ -93,31 +93,21 @@ void CostCalculator::addObstacleCostPenalty( LabelPosition *lp, FeaturePart *obs
9393
lp->setCost( lp->cost() + obstacleCost );
9494
}
9595

96-
void CostCalculator::setPolygonCandidatesCost( int nblp, QList< LabelPosition * > &lPos, RTree<FeaturePart *, double, 2, double> *obstacles, double bbx[4], double bby[4] )
96+
void CostCalculator::setPolygonCandidatesCost( std::size_t nblp, std::vector< std::unique_ptr< LabelPosition > > &lPos, RTree<FeaturePart *, double, 2, double> *obstacles, double bbx[4], double bby[4] )
9797
{
9898
double normalizer;
9999
// compute raw cost
100-
for ( int i = 0; i < nblp; ++i )
101-
setCandidateCostFromPolygon( lPos.at( i ), obstacles, bbx, bby );
100+
for ( std::size_t i = 0; i < nblp; ++i )
101+
setCandidateCostFromPolygon( lPos[ i ].get(), obstacles, bbx, bby );
102102

103103
// lPos with big values came first (value = min distance from label to Polygon's Perimeter)
104104
// IMPORTANT - only want to sort first nblp positions. The rest have not had the cost
105105
// calculated so will have nonsense values
106-
QList< LabelPosition * > toSort;
107-
toSort.reserve( nblp );
108-
for ( int i = 0; i < nblp; ++i )
109-
{
110-
toSort << lPos.at( i );
111-
}
112-
std::sort( toSort.begin(), toSort.end(), candidateSortShrink );
113-
for ( int i = 0; i < nblp; ++i )
114-
{
115-
lPos[i] = toSort.at( i );
116-
}
106+
std::sort( lPos.begin(), lPos.begin() + nblp, candidateSortShrink );
117107

118108
// define the value's range
119-
double cost_max = lPos.at( 0 )->cost();
120-
double cost_min = lPos.at( nblp - 1 )->cost();
109+
double cost_max = lPos.front()->cost();
110+
double cost_min = lPos.back()->cost();
121111

122112
cost_max -= cost_min;
123113

@@ -132,17 +122,18 @@ void CostCalculator::setPolygonCandidatesCost( int nblp, QList< LabelPosition *
132122

133123
// adjust cost => the best is 0.0001, the worst is 0.0021
134124
// others are set proportionally between best and worst
135-
for ( int i = 0; i < nblp; ++i )
125+
for ( std::size_t i = 0; i < nblp; ++i )
136126
{
127+
LabelPosition *pos = lPos[ i ].get();
137128
//if (cost_max - cost_min < EPSILON)
138129
if ( cost_max > EPSILON )
139130
{
140-
lPos.at( i )->setCost( 0.0021 - ( lPos.at( i )->cost() - cost_min ) * normalizer );
131+
pos->setCost( 0.0021 - ( pos->cost() - cost_min ) * normalizer );
141132
}
142133
else
143134
{
144-
//lPos[i]->cost = 0.0001 + (lPos[i]->cost - cost_min) * normalizer;
145-
lPos.at( i )->setCost( 0.0001 );
135+
//pos->cost = 0.0001 + (pos->cost - cost_min) * normalizer;
136+
pos->setCost( 0.0001 );
146137
}
147138
}
148139
}
@@ -174,31 +165,30 @@ void CostCalculator::setCandidateCostFromPolygon( LabelPosition *lp, RTree <Feat
174165
delete pCost;
175166
}
176167

177-
int CostCalculator::finalizeCandidatesCosts( Feats *feat, int max_p, RTree <FeaturePart *, double, 2, double> *obstacles, double bbx[4], double bby[4] )
168+
std::size_t CostCalculator::finalizeCandidatesCosts( Feats *feat, std::size_t max_p, RTree <FeaturePart *, double, 2, double> *obstacles, double bbx[4], double bby[4] )
178169
{
179170
// If candidates list is smaller than expected
180-
if ( max_p > feat->candidates.count() )
181-
max_p = feat->candidates.count();
171+
if ( max_p > feat->candidates.size() )
172+
max_p = feat->candidates.size();
182173
//
183174
// sort candidates list, best label to worst
184175
std::sort( feat->candidates.begin(), feat->candidates.end(), candidateSortGrow );
185176

186177
// try to exclude all conflitual labels (good ones have cost < 1 by pruning)
187178
double discrim = 0.0;
188-
int stop;
179+
std::size_t stop = 0;
189180
do
190181
{
191182
discrim += 1.0;
192-
for ( stop = 0; stop < feat->candidates.count() && feat->candidates.at( stop )->cost() < discrim; stop++ )
183+
for ( stop = 0; stop < feat->candidates.size() && feat->candidates[ stop ]->cost() < discrim; stop++ )
193184
;
194185
}
195-
while ( stop == 0 && discrim < feat->candidates.last()->cost() + 2.0 );
186+
while ( stop == 0 && discrim < feat->candidates.back()->cost() + 2.0 );
196187

197188
if ( discrim > 1.5 )
198189
{
199-
int k;
200-
for ( k = 0; k < stop; k++ )
201-
feat->candidates.at( k )->setCost( 0.0021 );
190+
for ( std::size_t k = 0; k < stop; k++ )
191+
feat->candidates[ k ]->setCost( 0.0021 );
202192
}
203193

204194
if ( max_p > stop )

‎src/core/pal/costcalculator.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
namespace pal
2929
{
3030
class Feats;
31+
class LabelPosition;
3132

3233
/**
3334
* \ingroup core
@@ -38,23 +39,23 @@ namespace pal
3839
//! Increase candidate's cost according to its collision with passed feature
3940
static void addObstacleCostPenalty( LabelPosition *lp, pal::FeaturePart *obstacle );
4041

41-
static void setPolygonCandidatesCost( int nblp, QList< LabelPosition * > &lPos, RTree<pal::FeaturePart *, double, 2, double> *obstacles, double bbx[4], double bby[4] );
42+
static void setPolygonCandidatesCost( std::size_t nblp, std::vector<std::unique_ptr<pal::LabelPosition> > &lPos, RTree<pal::FeaturePart *, double, 2, double> *obstacles, double bbx[4], double bby[4] );
4243

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

4647
//! Sort candidates by costs, skip the worse ones, evaluate polygon candidates
47-
static int finalizeCandidatesCosts( Feats *feat, int max_p, RTree<pal::FeaturePart *, double, 2, double> *obstacles, double bbx[4], double bby[4] );
48+
static std::size_t finalizeCandidatesCosts( Feats *feat, std::size_t max_p, RTree<pal::FeaturePart *, double, 2, double> *obstacles, double bbx[4], double bby[4] );
4849

4950
/**
5051
* Sorts label candidates in ascending order of cost
5152
*/
52-
static bool candidateSortGrow( const LabelPosition *c1, const LabelPosition *c2 );
53+
static bool candidateSortGrow( const std::unique_ptr<pal::LabelPosition> &c1, const std::unique_ptr<pal::LabelPosition> &c2 );
5354

5455
/**
5556
* Sorts label candidates in descending order of cost
5657
*/
57-
static bool candidateSortShrink( const LabelPosition *c1, const LabelPosition *c2 );
58+
static bool candidateSortShrink( const std::unique_ptr<pal::LabelPosition> &c1, const std::unique_ptr<pal::LabelPosition> &c2 );
5859
};
5960

6061
/**

0 commit comments

Comments
 (0)
Please sign in to comment.