Skip to content

Commit

Permalink
[labelling] Fix suboptimal logic for polygon's default around centroi…
Browse files Browse the repository at this point in the history
…d placement
  • Loading branch information
nirvn authored and nyalldawson committed Feb 27, 2020
1 parent c413595 commit 0e5a985
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 10 deletions.
53 changes: 43 additions & 10 deletions src/core/pal/feature.cpp
Expand Up @@ -231,16 +231,41 @@ void FeaturePart::setTotalRepeats( int totalRepeats )
mTotalRepeats = totalRepeats;
}

int FeaturePart::createCandidatesOverPoint( double x, double y, QList< LabelPosition *> &lPos, double angle )
int FeaturePart::createCandidateCenteredOverPoint( double x, double y, QList< LabelPosition *> &lPos, double angle )
{
int nbp = 1;
// get from feature
double labelW = getLabelWidth( angle );
double labelH = getLabelHeight( angle );

double cost = 0.00005;
int id = lPos.size();

double xdiff = -labelW / 2.0;
double ydiff = -labelH / 2.0;

double lx = x + xdiff;
double ly = y + ydiff;

if ( mLF->permissibleZonePrepared() )
{
if ( !GeomFunction::containsCandidate( mLF->permissibleZonePrepared(), lx, ly, labelW, labelH, angle ) )
{
return 0;
}
}

lPos << new LabelPosition( id, lx, ly, labelW, labelH, angle, cost, this, false, LabelPosition::QuadrantOver );
return 1;
}

int FeaturePart::createCandidatesOverPoint( double x, double y, QList< LabelPosition *> &lPos, double angle )
{
// get from feature
double labelW = getLabelWidth( angle );
double labelH = getLabelHeight( angle );

double cost = 0.0001;
int id = 0;
int id = lPos.size();

double xdiff = -labelW / 2.0;
double ydiff = -labelH / 2.0;
Expand Down Expand Up @@ -307,7 +332,7 @@ int FeaturePart::createCandidatesOverPoint( double x, double y, QList< LabelPosi
}

lPos << new LabelPosition( id, lx, ly, labelW, labelH, angle, cost, this, false, quadrantFromOffset() );
return nbp;
return 1;
}

std::unique_ptr<LabelPosition> FeaturePart::createCandidatePointOnSurface( PointSet *mapShape )
Expand Down Expand Up @@ -473,6 +498,7 @@ int FeaturePart::createCandidatesAroundPoint( double x, double y, QList< LabelPo

int icost = 0;
int inc = 2;
int id = lPos.size();

double candidateAngleIncrement = 2 * M_PI / maxNumberCandidates; /* angle bw 2 pos */

Expand Down Expand Up @@ -588,7 +614,7 @@ int FeaturePart::createCandidatesAroundPoint( double x, double y, QList< LabelPo
}
}

candidates << new LabelPosition( i, labelX, labelY, labelWidth, labelHeight, angle, cost, this, false, quadrant );
candidates << new LabelPosition( id + i, labelX, labelY, labelWidth, labelHeight, angle, cost, this, false, quadrant );

icost += inc;

Expand Down Expand Up @@ -1643,14 +1669,21 @@ QList<LabelPosition *> FeaturePart::createCandidates( const GEOSPreparedGeometry
switch ( mLF->layer()->arrangement() )
{
case QgsPalLayerSettings::AroundPoint:
{
double cx, cy;
getCentroid( cx, cy, mLF->layer()->centroidInside() );
if ( qgsDoubleNear( mLF->distLabel(), 0.0 ) )
createCandidateCenteredOverPoint( cx, cy, lPos, angle );
createCandidatesAroundPoint( cx, cy, lPos, angle );
break;
}
case QgsPalLayerSettings::OverPoint:
{
double cx, cy;
mapShape->getCentroid( cx, cy, mLF->layer()->centroidInside() );
if ( mLF->layer()->arrangement() == QgsPalLayerSettings::OverPoint )
createCandidatesOverPoint( cx, cy, lPos, angle );
else
createCandidatesAroundPoint( cx, cy, lPos, angle );
getCentroid( cx, cy, mLF->layer()->centroidInside() );
createCandidatesOverPoint( cx, cy, lPos, angle );
break;
}
case QgsPalLayerSettings::Line:
createCandidatesAlongLine( lPos, mapShape );
break;
Expand Down
10 changes: 10 additions & 0 deletions src/core/pal/feature.h
Expand Up @@ -136,6 +136,16 @@ namespace pal
*/
QList<LabelPosition *> createCandidates( const GEOSPreparedGeometry *mapBoundary, PointSet *mapShape, RTree<LabelPosition *, double, 2, double> *candidates );

/**
* Generate one candidate centered over the specified point.
* \param x x coordinate of the point
* \param y y coordinate of the point
* \param lPos pointer to an array of candidates, will be filled by generated candidate
* \param angle orientation of the label
* \returns the number of generated candidates (always 1)
*/
int createCandidateCenteredOverPoint( double x, double y, QList<LabelPosition *> &lPos, double angle );

/**
* Generate candidates for point feature, located around a specified point.
* \param x x coordinate of the point
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0e5a985

Please sign in to comment.