Skip to content

Commit

Permalink
pal: created StraightLabelPosition derived from LabelPosition.
Browse files Browse the repository at this point in the history
Return LabelPosition array instead of Label array.


git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11126 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Jul 21, 2009
1 parent dc046b5 commit 4786e54
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 125 deletions.
8 changes: 4 additions & 4 deletions src/core/pal/costcalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace pal

////////

void CostCalculator::setPolygonCandidatesCost( int nblp, LabelPosition **lPos, int max_p, RTree<PointSet*, double, 2, double> *obstacles, double bbx[4], double bby[4] )
void CostCalculator::setPolygonCandidatesCost( int nblp, StraightLabelPosition **lPos, int max_p, RTree<PointSet*, double, 2, double> *obstacles, double bbx[4], double bby[4] )
{
int i;

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


void CostCalculator::setCandidateCostFromPolygon( LabelPosition* lp, RTree <PointSet*, double, 2, double> *obstacles, double bbx[4], double bby[4] )
void CostCalculator::setCandidateCostFromPolygon( StraightLabelPosition* lp, RTree <PointSet*, double, 2, double> *obstacles, double bbx[4], double bby[4] )
{

double amin[2];
Expand Down Expand Up @@ -188,7 +188,7 @@ namespace pal
{
int arrangement = feat->feature->getLayer()->getArrangement();
if ( arrangement == P_FREE || arrangement == P_HORIZ )
setPolygonCandidatesCost( stop, feat->lPos, max_p, obstacles, bbx, bby );
setPolygonCandidatesCost( stop, (StraightLabelPosition**) feat->lPos, max_p, obstacles, bbx, bby );
}

return max_p;
Expand All @@ -198,7 +198,7 @@ namespace pal

//////////

PolygonCostCalculator::PolygonCostCalculator( LabelPosition *lp ) : lp( lp )
PolygonCostCalculator::PolygonCostCalculator( StraightLabelPosition *lp ) : lp( lp )
{
int i;
double hyp = max( lp->feature->xmax - lp->feature->xmin, lp->feature->ymax - lp->feature->ymin );
Expand Down
9 changes: 5 additions & 4 deletions src/core/pal/costcalculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
namespace pal
{
class Feats;
class StraightLabelPosition;

class CostCalculator
{
public:
/** increase candidate's cost according to its collision with passed feature */
static void addObstacleCostPenalty(LabelPosition* lp, PointSet* feat);

static void setPolygonCandidatesCost( int nblp, LabelPosition **lPos, int max_p, RTree<PointSet*, double, 2, double> *obstacles, double bbx[4], double bby[4] );
static void setPolygonCandidatesCost( int nblp, StraightLabelPosition **lPos, int max_p, RTree<PointSet*, double, 2, double> *obstacles, double bbx[4], double bby[4] );

/** Set cost to the smallest distance between lPos's centroid and a polygon stored in geoetry field */
static void setCandidateCostFromPolygon( LabelPosition* lp, RTree <PointSet*, double, 2, double> *obstacles, double bbx[4], double bby[4] );
static void setCandidateCostFromPolygon( StraightLabelPosition* lp, RTree <PointSet*, double, 2, double> *obstacles, double bbx[4], double bby[4] );

/** sort candidates by costs, skip the worse ones, evaluate polygon candidates */
static int finalizeCandidatesCosts( Feats* feat, int max_p, RTree <PointSet*, double, 2, double> *obstacles, double bbx[4], double bby[4] );
Expand All @@ -31,7 +32,7 @@ namespace pal
*/
class PolygonCostCalculator
{
LabelPosition *lp;
StraightLabelPosition *lp;
double px, py;
double dist[8];
double rpx[8];
Expand All @@ -41,7 +42,7 @@ namespace pal
void updatePoint( PointSet *pset );
double updateLinePoly( PointSet *pset );
public:
PolygonCostCalculator( LabelPosition *lp );
PolygonCostCalculator( StraightLabelPosition *lp );

void update( PointSet *pset );

Expand Down
18 changes: 9 additions & 9 deletions src/core/pal/feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ namespace pal
double offset = label_x / 4;

// at the center
( *lPos )[0] = new LabelPosition( id, lx, ly, label_x, label_y, alpha, cost, this );
( *lPos )[0] = new StraightLabelPosition( id, lx, ly, label_x, label_y, alpha, cost, this );
// shifted to the sides - with higher cost
cost = 0.0021;
( *lPos )[1] = new LabelPosition( id, lx+offset, ly, label_x, label_y, alpha, cost, this );
( *lPos )[2] = new LabelPosition( id, lx-offset, ly, label_x, label_y, alpha, cost, this );
( *lPos )[1] = new StraightLabelPosition( id, lx+offset, ly, label_x, label_y, alpha, cost, this );
( *lPos )[2] = new StraightLabelPosition( id, lx-offset, ly, label_x, label_y, alpha, cost, this );
return nbp;
}

Expand Down Expand Up @@ -316,7 +316,7 @@ namespace pal
else
cost = 0.0001 + 0.0020 * double( icost ) / double( nbp - 1 );

( *lPos )[i] = new LabelPosition( i, lx, ly, xrm, yrm, 0, cost, this );
( *lPos )[i] = new StraightLabelPosition( i, lx, ly, xrm, yrm, 0, cost, this );

icost += inc;

Expand Down Expand Up @@ -488,15 +488,15 @@ namespace pal
reversed = ( alpha >= M_PI/2 || alpha < -M_PI/2 );

if ( (!reversed && (flags & FLAG_ABOVE_LINE)) || (reversed && (flags & FLAG_BELOW_LINE)) )
positions->push_back( new LabelPosition( i, bx + cos( beta ) *distlabel , by + sin( beta ) *distlabel, xrm, yrm, alpha, cost, this ) ); // Line
positions->push_back( new StraightLabelPosition( i, bx + cos( beta ) *distlabel , by + sin( beta ) *distlabel, xrm, yrm, alpha, cost, this ) ); // Line
if ( (!reversed && (flags & FLAG_BELOW_LINE)) || (reversed && (flags & FLAG_ABOVE_LINE)) )
positions->push_back( new LabelPosition( i, bx - cos( beta ) * ( distlabel + yrm ) , by - sin( beta ) * ( distlabel + yrm ), xrm, yrm, alpha, cost, this ) ); // Line
positions->push_back( new StraightLabelPosition( i, bx - cos( beta ) * ( distlabel + yrm ) , by - sin( beta ) * ( distlabel + yrm ), xrm, yrm, alpha, cost, this ) ); // Line
if ( flags & FLAG_ON_LINE )
positions->push_back( new LabelPosition( i, bx - yrm*cos( beta ) / 2, by - yrm*sin( beta ) / 2, xrm, yrm, alpha, cost, this ) ); // Line
positions->push_back( new StraightLabelPosition( i, bx - yrm*cos( beta ) / 2, by - yrm*sin( beta ) / 2, xrm, yrm, alpha, cost, this ) ); // Line
}
else if (layer->arrangement == P_HORIZ)
{
positions->push_back( new LabelPosition(i, bx - xrm/2, by - yrm/2, xrm, yrm, 0, cost, this) ); // Line
positions->push_back( new StraightLabelPosition(i, bx - xrm/2, by - yrm/2, xrm, yrm, 0, cost, this) ); // Line
//positions->push_back( new LabelPosition(i, bx -yrm/2, by - yrm*sin(beta)/2, xrm, yrm, alpha, cost, this, line)); // Line
}
else
Expand Down Expand Up @@ -735,7 +735,7 @@ namespace pal
if ( isPointInPolygon( mapShape->nbPoints, mapShape->x, mapShape->y, rx, ry ) )
{
// cost is set to minimal value, evaluated later
positions->push_back( new LabelPosition( id++, rx - dlx, ry - dly , xrm, yrm, alpha, 0.0001, this ) ); // Polygon
positions->push_back( new StraightLabelPosition( id++, rx - dlx, ry - dly , xrm, yrm, alpha, 0.0001, this ) ); // Polygon
}
}
}
Expand Down
41 changes: 21 additions & 20 deletions src/core/pal/labelposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@

namespace pal
{

LabelPosition::LabelPosition( int id, double x1, double y1, double w, double h, double alpha, double cost, Feature *feature ) : id( id ), cost( cost ), /*workingCost (0),*/ alpha( alpha ), feature( feature ), nbOverlap( 0 ), w( w ), h( h )
LabelPosition::LabelPosition( int id, double cost, Feature *feature )
: id( id ), cost( cost ), feature( feature ), nbOverlap( 0 )
{
}

StraightLabelPosition::StraightLabelPosition( int id, double x1, double y1, double w, double h, double alpha, double cost, Feature *feature )
: LabelPosition( id, cost, feature ), alpha( alpha ), w( w ), h( h )
{

// alpha take his value bw 0 and 2*pi rad
while ( this->alpha > 2*M_PI )
Expand Down Expand Up @@ -119,7 +123,7 @@ namespace pal
}
}

bool LabelPosition::isIn( double *bbox )
bool StraightLabelPosition::isIn( double *bbox )
{
int i;

Expand All @@ -134,7 +138,7 @@ namespace pal

}

void LabelPosition::print()
void StraightLabelPosition::print()
{
std::cout << feature->getLayer()->getName() << "/" << feature->getUID() << "/" << id;
std::cout << " cost: " << cost;
Expand All @@ -146,8 +150,11 @@ namespace pal
std::cout << std::endl;
}

bool LabelPosition::isInConflict( LabelPosition *ls )
bool StraightLabelPosition::isInConflict( LabelPosition *lp )
{
// TODO: more types of labelposition
StraightLabelPosition* ls = (StraightLabelPosition*) lp;

int i, i2, j;
int d1, d2;

Expand Down Expand Up @@ -194,17 +201,17 @@ namespace pal
return id;
}

double LabelPosition::getX() const
double StraightLabelPosition::getX( int i ) const
{
return x[0];
return (i >= 0 && i < 4 ? x[i] : -1);
}

double LabelPosition::getY() const
double StraightLabelPosition::getY( int i ) const
{
return y[0];
return (i >= 0 && i < 4 ? y[i] : -1);
}

double LabelPosition::getAlpha() const
double StraightLabelPosition::getAlpha() const
{
return alpha;
}
Expand All @@ -228,7 +235,7 @@ namespace pal
return feature;
}

void LabelPosition::getBoundingBox(double amin[2], double amax[2]) const
void StraightLabelPosition::getBoundingBox(double amin[2], double amax[2]) const
{
amin[0] = DBL_MAX;
amax[0] = -DBL_MAX;
Expand Down Expand Up @@ -263,12 +270,6 @@ namespace pal
}


Label *LabelPosition::toLabel( bool active )
{
return new Label( this->x, this->y, alpha, feature->getUID(), feature->getLayer()->getName(), feature->getUserGeometry() );
}


bool LabelPosition::polygonObstacleCallback( PointSet *feat, void *ctx )
{
PolygonCostCalculator *pCost = ( PolygonCostCalculator* ) ctx;
Expand Down Expand Up @@ -385,7 +386,7 @@ namespace pal



double LabelPosition::getDistanceToPoint( double xp, double yp )
double StraightLabelPosition::getDistanceToPoint( double xp, double yp )
{
int i;
int j;
Expand Down Expand Up @@ -436,7 +437,7 @@ namespace pal
}


bool LabelPosition::isBorderCrossingLine( PointSet* feat )
bool StraightLabelPosition::isBorderCrossingLine( PointSet* feat )
{
double ca, cb;
for ( int i = 0;i < 4;i++ )
Expand All @@ -462,7 +463,7 @@ namespace pal
return false;
}

int LabelPosition::getNumPointsInPolygon( int npol, double *xp, double *yp )
int StraightLabelPosition::getNumPointsInPolygon( int npol, double *xp, double *yp )
{
int a, k, count = 0;
double px, py;
Expand Down
Loading

0 comments on commit 4786e54

Please sign in to comment.