Skip to content

Commit

Permalink
Penalize small lines/polygons with additional cost for all candidates…
Browse files Browse the repository at this point in the history
… of the feature part.

This has the effect that larger objects get higher chance of being labeled.


git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11317 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Aug 9, 2009
1 parent fba9efc commit 5bde13b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core/pal/costcalculator.cpp
Expand Up @@ -188,6 +188,9 @@ namespace pal
setPolygonCandidatesCost( stop, (LabelPosition**) feat->lPos, max_p, obstacles, bbx, bby );
}

// add size penalty (small lines/polygons get higher cost)
feat->feature->addSizePenalty(max_p, feat->lPos, bbx, bby);

return max_p;
}

Expand Down
38 changes: 38 additions & 0 deletions src/core/pal/feature.cpp
Expand Up @@ -1279,6 +1279,44 @@ void FeaturePart::removeDuplicatePoints()
return rnbp;
}

void FeaturePart::addSizePenalty( int nbp, LabelPosition** lPos, double bbx[4], double bby[4])
{
int geomType = GEOSGeomTypeId(the_geom);

double sizeCost = 0;
if (geomType == GEOS_LINESTRING)
{
double length;
if (GEOSLength(the_geom, &length) != 1)
return; // failed to calculate length
double bbox_length = max(bbx[2]-bbx[0], bby[2]-bby[0]);
if (length >= bbox_length/4)
return; // the line is longer than quarter of height or width - don't penalize it

sizeCost = 1 - (length / (bbox_length/4)); // < 0,1 >
}
else if (geomType == GEOS_POLYGON)
{
double area;
if (GEOSArea(the_geom, &area) != 1)
return;
double bbox_area = (bbx[2]-bbx[0])*(bby[2]-bby[0]);
if (area >= bbox_area/16)
return; // covers more than 1/16 of our view - don't penalize it

sizeCost = 1 - (area / (bbox_area/16)); // < 0, 1 >
}
else
return; // no size penalty for points

std::cout << "size cost " << sizeCost << std::endl;

// apply the penalty
for (int i = 0; i < nbp; i++)
{
lPos[i]->setCost( lPos[i]->getCost() + sizeCost / 100 );
}
}

bool FeaturePart::isConnected(FeaturePart* p2)
{
Expand Down
3 changes: 3 additions & 0 deletions src/core/pal/feature.h
Expand Up @@ -266,6 +266,9 @@ namespace pal
/** merge other (connected) part with this one and save the result in this part (other is unchanged).
* Return true on success, false if the feature wasn't modified */
bool mergeWithFeaturePart(FeaturePart* other);

void addSizePenalty( int nbp, LabelPosition** lPos, double bbx[4], double bby[4]);

};

} // end namespace pal
Expand Down

0 comments on commit 5bde13b

Please sign in to comment.