Commit 412453b1 authored by Volker Krause's avatar Volker Krause
Browse files

Produce valid results when simplifying areas close to the threshold

When the simplified area extends not significantly more than the size
threshold in one direction, the result of the Douglas Peucker algorithm
can deteriorate into a area-less polygon. In this case we fall back to
the bounding box instead now.

It's not that those small features have much practical impact, but the
resulting broken polygons cause problems in subsequent use.
parent 24760e75
...@@ -39,9 +39,18 @@ static QPolygonF douglasPeucker(const QPolygonF::const_iterator &begin, const QP ...@@ -39,9 +39,18 @@ static QPolygonF douglasPeucker(const QPolygonF::const_iterator &begin, const QP
return QPolygonF(QVector<QPointF>({*begin, (*std::prev(end))})); return QPolygonF(QVector<QPointF>({*begin, (*std::prev(end))}));
} }
QPolygonF PolygonSimplifier::douglasPeucker(const QPolygonF &poly, double distance) QPolygonF PolygonSimplifier::douglasPeucker(const QPolygonF &poly, double threshold)
{ {
auto result = douglasPeucker(poly.begin(), poly.end(), distance); auto result = douglasPeucker(poly.begin(), poly.end(), threshold);
qDebug() << "got" << poly.size() << "dropped" << poly.size() - result.size(); qDebug() << "got" << poly.size() << "dropped" << poly.size() - result.size() << "remaining" << result.size();
// if the polygon itself is smaller than the threshold, the result can deteriorate into
// an area-less structure, take the bounding rect in that case
if (result.size() < 4) {
qDebug() << " result deteriorated, taking bounding rect instead";
const auto bbox = poly.boundingRect();
return QPolygonF({ bbox.topLeft(), bbox.topRight(), bbox.bottomRight(), bbox.bottomLeft(), bbox.topLeft() });
}
return result; return result;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment