Faster (but less precise) implementation of simplify_by_visibility(), since it was the bottleneck of avoid_crossing_perimeters. #2777

This commit is contained in:
Alessandro Ranellucci 2015-05-18 19:28:59 +02:00
parent c64308a5e7
commit 70ec433e67
2 changed files with 27 additions and 5 deletions

View file

@ -139,11 +139,31 @@ Polyline::simplify_by_visibility(const T &area)
Points &pp = this->points;
// find first point in area
size_t start = 0;
while (start < pp.size() && !area.contains(pp[start])) {
start++;
size_t s = 0;
while (s < pp.size() && !area.contains(pp[s])) {
++s;
}
// find last point in area
size_t e = pp.size()-1;
while (e > 0 && !area.contains(pp[e])) {
--e;
}
// this ugly algorithm resembles a binary search
while (e > s + 1) {
size_t mid = (s + e) / 2;
if (area.contains(Line(pp[s], pp[mid]))) {
pp.erase(pp.begin() + s + 1, pp.begin() + mid);
// repeat recursively until no further simplification is possible
++s;
e = pp.size()-1;
} else {
e = mid;
}
}
/*
// The following implementation is complete but it's not efficient at all:
for (size_t s = start; s < pp.size() && !pp.empty(); ++s) {
// find the farthest point to which we can build
// a line that is contained in the supplied area
@ -158,6 +178,7 @@ Polyline::simplify_by_visibility(const T &area)
}
}
}
*/
}
template void Polyline::simplify_by_visibility<ExPolygon>(const ExPolygon &area);
template void Polyline::simplify_by_visibility<ExPolygonCollection>(const ExPolygonCollection &area);