Skip to content

Commit 5cfe487

Browse files
author
wonder
committed
First round of refactoring:
Made pal::Feature and pal::LabelPosition less friendly: prefer access to internal variables through access functions. Removed some dead functions, moved some functions, less code duplication. Shouldn't affect functionality in any way. git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11044 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 97ac08c commit 5cfe487

File tree

11 files changed

+295
-572
lines changed

11 files changed

+295
-572
lines changed

src/core/pal/feature.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ namespace pal
869869
}
870870
}
871871

872-
sort(( void** )( *lPos ), nbp, costGrow );
872+
sort(( void** )( *lPos ), nbp, LabelPosition::costGrow );
873873

874874
for ( i = rnbp;i < nbp;i++ )
875875
{
@@ -944,10 +944,7 @@ namespace pal
944944
y = NULL;
945945
for ( i = 0;i < nbSelfObs;i++ )
946946
{
947-
delete[] selfObs[i]->x;
948-
delete[] selfObs[i]->y;
949-
selfObs[i]->x = NULL;
950-
selfObs[i]->y = NULL;
947+
selfObs[i]->deleteCoords();
951948
}
952949
}
953950
}

src/core/pal/feature.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,6 @@ namespace pal
6060
class Feature : public PointSet
6161
{
6262

63-
friend class Pal;
64-
friend class Layer;
65-
friend class Problem;
66-
friend class LabelPosition;
67-
68-
friend bool extractFeatCallback( Feature *ft_ptr, void *ctx );
69-
friend bool pruneLabelPositionCallback( LabelPosition *lp, void *ctx );
70-
friend bool obstacleCallback( PointSet *feat, void *ctx );
71-
//friend void setCost (int nblp, LabelPosition **lPos, int max_p, RTree<PointSet*, double, 2, double> *obstacles, double bbx[4], double bby[4]);
72-
friend void releaseAllInIndex( RTree<PointSet*, double, 2, double, 8, 4>* );
73-
friend bool releaseCallback( PointSet *pset, void *ctx );
74-
friend bool filteringCallback( PointSet*, void* );
75-
7663
protected:
7764
//int id; /* feature no id into layer */
7865
double label_x;
@@ -96,6 +83,7 @@ namespace pal
9683

9784
SimpleMutex *accessMutex;
9885

86+
public:
9987
/**
10088
* \brief generate candidates for point feature
10189
* Generate candidates for point features
@@ -244,6 +232,23 @@ namespace pal
244232

245233
void fetchCoordinates();
246234
void releaseCoordinates();
235+
236+
237+
238+
PalGeometry* getUserGeometry() { return userGeom; }
239+
240+
void setLabelSize(double x, double y) { label_x = x; label_y = y; }
241+
double getLabelWidth() const { return label_x; }
242+
double getLabelHeight() const { return label_y; }
243+
244+
int getNumParts() const { return nPart; }
245+
246+
void setLabelDistance(double dist) { distlabel = dist; }
247+
double getLabelDistance() const { return distlabel; }
248+
249+
int getNumSelfObstacles() const { return nbSelfObs; }
250+
PointSet* getSelfObstacle(int i) { return selfObs[i]; }
251+
247252
};
248253

249254
} // end namespace pal

src/core/pal/labelposition.cpp

Lines changed: 87 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -213,37 +213,50 @@ namespace pal
213213
return cost;
214214
}
215215

216-
Feature * LabelPosition::getFeature()
217-
{
218-
return feature;
219-
}
220-
221-
bool xGrow( void *l, void *r )
216+
void LabelPosition::validateCost()
222217
{
223-
return (( LabelPosition* ) l )->x[0] > (( LabelPosition* ) r )->x[0];
218+
if ( cost >= 1 )
219+
{
220+
std::cout << " Warning: lp->cost == " << cost << " (from feat: " << feature->getUID() << "/" << getLayerName() << ")" << std::endl;
221+
cost -= int ( cost ); // label cost up to 1
222+
}
224223
}
225224

226-
bool yGrow( void *l, void *r )
225+
Feature * LabelPosition::getFeature()
227226
{
228-
return (( LabelPosition* ) l )->y[0] > (( LabelPosition* ) r )->y[0];
227+
return feature;
229228
}
230229

231-
bool xShrink( void *l, void *r )
230+
void LabelPosition::getBoundingBox(double amin[2], double amax[2]) const
232231
{
233-
return (( LabelPosition* ) l )->x[0] < (( LabelPosition* ) r )->x[0];
232+
amin[0] = DBL_MAX;
233+
amax[0] = -DBL_MAX;
234+
amin[1] = DBL_MAX;
235+
amax[1] = -DBL_MAX;
236+
for ( int c = 0;c < 4;c++ )
237+
{
238+
if ( x[c] < amin[0] )
239+
amin[0] = x[c];
240+
if ( x[c] > amax[0] )
241+
amax[0] = x[c];
242+
if ( y[c] < amin[1] )
243+
amin[1] = y[c];
244+
if ( y[c] > amax[1] )
245+
amax[1] = y[c];
246+
}
234247
}
235248

236-
bool yShrink( void *l, void *r )
249+
char* LabelPosition::getLayerName() const
237250
{
238-
return (( LabelPosition* ) l )->y[0] < (( LabelPosition* ) r )->y[0];
251+
return feature->getLayer()->name;
239252
}
240253

241-
bool costShrink( void *l, void *r )
254+
bool LabelPosition::costShrink( void *l, void *r )
242255
{
243256
return (( LabelPosition* ) l )->cost < (( LabelPosition* ) r )->cost;
244257
}
245258

246-
bool costGrow( void *l, void *r )
259+
bool LabelPosition::costGrow( void *l, void *r )
247260
{
248261
return (( LabelPosition* ) l )->cost > (( LabelPosition* ) r )->cost;
249262
}
@@ -269,31 +282,31 @@ namespace pal
269282
//#warning retourner les coord projeté ou pas ?
270283
//feature->layer->pal->proj->getLatLong(this->x[0], this->y[0], &x, &y);
271284

272-
return new Label( this->x, this->y, alpha, feature->uid, feature->layer->name, feature->userGeom );
285+
return new Label( this->x, this->y, alpha, feature->getUID(), feature->getLayer()->getName(), feature->getUserGeometry() );
273286
}
274287

275288

276-
bool obstacleCallback( PointSet *feat, void *ctx )
289+
bool LabelPosition::obstacleCallback( PointSet *feat, void *ctx )
277290
{
278291
LabelPosition::PolygonCostCalculator *pCost = ( LabelPosition::PolygonCostCalculator* ) ctx;
279292

280293
LabelPosition *lp = pCost->getLabel();
281-
if (( feat == lp->feature ) || ( feat->holeOf && feat->holeOf != lp->feature ) )
294+
if (( feat == lp->feature ) || ( feat->getHoleOf() && feat->getHoleOf() != lp->feature ) )
282295
{
283296
return true;
284297
}
285298

286299
// if the feature is not a hole we have to fetch corrdinates
287300
// otherwise holes coordinates are still in memory (feature->selfObs)
288-
if ( feat->holeOf == NULL )
301+
if ( feat->getHoleOf() == NULL )
289302
{
290303
(( Feature* ) feat )->fetchCoordinates();
291304
}
292305

293306
pCost->update( feat );
294307

295308

296-
if ( feat->holeOf == NULL )
309+
if ( feat->getHoleOf() == NULL )
297310
{
298311
(( Feature* ) feat )->releaseCoordinates();
299312
}
@@ -332,10 +345,7 @@ namespace pal
332345
amax[0] = amin[0] + 2 * dist;
333346
amax[1] = amin[1] + 2 * dist;
334347
} else {*/
335-
amin[0] = feature->xmin;
336-
amin[1] = feature->ymin;
337-
amax[0] = feature->xmax;
338-
amax[1] = feature->ymax;
348+
feature->getBoundingBox(amin, amax);
339349
//}
340350

341351
//std::cout << amin[0] << " " << amin[1] << " " << amax[0] << " " << amax[1] << std::endl;
@@ -351,24 +361,7 @@ namespace pal
351361
{
352362
double amin[2];
353363
double amax[2];
354-
int c;
355-
356-
amin[0] = DBL_MAX;
357-
amax[0] = -DBL_MAX;
358-
amin[1] = DBL_MAX;
359-
amax[1] = -DBL_MAX;
360-
for ( c = 0;c < 4;c++ )
361-
{
362-
if ( x[c] < amin[0] )
363-
amin[0] = x[c];
364-
if ( x[c] > amax[0] )
365-
amax[0] = x[c];
366-
if ( y[c] < amin[1] )
367-
amin[1] = y[c];
368-
if ( y[c] > amax[1] )
369-
amax[1] = y[c];
370-
}
371-
364+
getBoundingBox(amin, amax);
372365
index->Remove( amin, amax, this );
373366
}
374367

@@ -377,24 +370,7 @@ namespace pal
377370
{
378371
double amin[2];
379372
double amax[2];
380-
int c;
381-
382-
amin[0] = DBL_MAX;
383-
amax[0] = -DBL_MAX;
384-
amin[1] = DBL_MAX;
385-
amax[1] = -DBL_MAX;
386-
for ( c = 0;c < 4;c++ )
387-
{
388-
if ( x[c] < amin[0] )
389-
amin[0] = x[c];
390-
if ( x[c] > amax[0] )
391-
amax[0] = x[c];
392-
if ( y[c] < amin[1] )
393-
amin[1] = y[c];
394-
if ( y[c] > amax[1] )
395-
amax[1] = y[c];
396-
}
397-
373+
getBoundingBox(amin, amax);
398374
index->Insert( amin, amax, this );
399375
}
400376

@@ -603,5 +579,56 @@ namespace pal
603579
//return (a+b+c+d);
604580
return ( a*b*c*d );
605581
}
582+
583+
//////////
584+
585+
bool LabelPosition::countOverlapCallback( LabelPosition *lp, void *ctx )
586+
{
587+
LabelPosition *lp2 = ( LabelPosition* ) ctx;
588+
589+
if ( lp2->isInConflict( lp ) )
590+
{
591+
lp2->nbOverlap++;
592+
}
593+
594+
return true;
595+
}
596+
597+
bool LabelPosition::countFullOverlapCallback( LabelPosition *lp, void *ctx )
598+
{
599+
LabelPosition *lp2 = (( CountContext* ) ctx )->lp;
600+
double *cost = (( CountContext* ) ctx )->cost;
601+
//int *feat = ((CountContext*)ctx)->feat;
602+
int *nbOv = (( CountContext* ) ctx )->nbOv;
603+
double *inactiveCost = (( CountContext* ) ctx )->inactiveCost;
604+
if ( lp2->isInConflict( lp ) )
605+
{
606+
#ifdef _DEBUG_FULL_
607+
std::cout << "count overlap : " << lp->id << "<->" << lp2->id << std::endl;
608+
#endif
609+
( *nbOv ) ++;
610+
*cost += inactiveCost[lp->probFeat] + lp->getCost();
611+
612+
}
613+
614+
return true;
615+
}
616+
617+
618+
bool LabelPosition::removeOverlapCallback( LabelPosition *lp, void *ctx )
619+
{
620+
LabelPosition *lp2 = ( LabelPosition * ) ctx;
621+
622+
if ( lp2->isInConflict( lp ) )
623+
{
624+
//std::cout << " hit !" << std::endl;
625+
lp->nbOverlap--;
626+
lp2->nbOverlap--;
627+
}
628+
629+
return true;
630+
}
631+
632+
606633
} // end namespace
607634

0 commit comments

Comments
 (0)