Mostly revert ef01f37

That commit message was a lie, it did not improve performance but actually reduce it significantly...

I had accidentally added a lot of complexity due to the std::list data structure. this approach might seem more redundant, but it's much faster with rectangles (like 100x faster) and doesn't seem to make a difference in the other cases.
This commit is contained in:
karl 2020-12-07 00:17:20 +01:00
parent 23fef77aaa
commit e832407ea0

View File

@ -149,38 +149,38 @@ private:
// If the input vector is empty, we're done // If the input vector is empty, we're done
if (input.empty()) return; if (input.empty()) return;
// Find the points which are furthest away from the line // Find the point which is furthest away from the line, add it to the output
Point furthest_point;
float furthest_distance = -1.0; float furthest_distance = -1.0;
std::list<Point> furthest_points;
for (const Point &point : input) for (const Point &point : input)
{ {
float this_distance = line.distance_squared_to(point); float this_distance = line.distance_squared_to(point);
// It's possible for there to be multiple closests points (e.g. in the case)
// of a rectangle). We need to handle all these properly, so we make a list
// of all points at the furthest distance.
if (this_distance > furthest_distance) if (this_distance > furthest_distance)
{ {
furthest_distance = this_distance; furthest_distance = this_distance;
furthest_points.clear(); furthest_point = point;
furthest_points.emplace_back(point);
}
else if (this_distance == furthest_distance)
{
furthest_points.emplace_back(point);
} }
} }
for (const Point &point : furthest_points) // This seems unnecessarily complicated, but it actually yielded the best results,
// especially when the rectangle edge case is taken into account.
for (const Point &point : input)
{
float this_distance = line.distance_squared_to(point);
if (this_distance == furthest_distance)
{ {
// All furthest points we found are part of the hull, so add them to the output
// and remove them from the input.
output.emplace_back(point); output.emplace_back(point);
input.remove(point);
} }
}
// Remove the previously checked points from the input -- we can't do that in
// the previous loop because we can't delete from the list we're iterating over
input.remove_if([furthest_distance, line](Point point)
{
return furthest_distance == line.distance_squared_to(point);
});
Point furthest_point = furthest_points.front(); output.emplace_back(furthest_point);
// Build a triangle with these 3 points // Build a triangle with these 3 points