Point previous_previous = path->at(path->size() - 2);
Point previous_previous = path->at(path->size() - 2);
Point current = path->at(0);
Point current = path->at(0);
/* When removing a vertex, we check the height of the triangle of the area
/* When removing a vertex, we check the height of the triangle of the area
being removed from the original polygon by the simplification. However,
being removed from the original polygon by the simplification. However,
when consecutively removing multiple vertices the height of the previously
when consecutively removing multiple vertices the height of the previously
removed vertices w.r.t. the shortcut path changes.
removed vertices w.r.t. the shortcut path changes.
In order to not recompute the new height value of previously removed
In order to not recompute the new height value of previously removed
vertices we compute the height of a representative triangle, which covers
vertices we compute the height of a representative triangle, which covers
the same amount of area as the area being cut off. We use the Shoelace
the same amount of area as the area being cut off. We use the Shoelace
formula to accumulate the area under the removed segments. This works by
formula to accumulate the area under the removed segments. This works by
computing the area in a 'fan' where each of the blades of the fan go from
computing the area in a 'fan' where each of the blades of the fan go from
the origin to one of the segments. While removing vertices the area in
the origin to one of the segments. While removing vertices the area in
this fan accumulates. By subtracting the area of the blade connected to
this fan accumulates. By subtracting the area of the blade connected to
the short-cutting segment we obtain the total area of the cutoff region.
the short-cutting segment we obtain the total area of the cutoff region.
From this area we compute the height of the representative triangle using
From this area we compute the height of the representative triangle using
the standard formula for a triangle area: A = .5*b*h
the standard formula for a triangle area: A = .5*b*h
*/
*/
coord_t accumulated_area_removed = previous.X * current.Y - previous.Y * current.X; // Twice the Shoelace formula for area of polygon per line segment.
coord_t accumulated_area_removed = previous.X * current.Y - previous.Y * current.X; // Twice the Shoelace formula for area of polygon per line segment.
for (size_t point_idx = 0; point_idx < size(); point_idx++)
for (size_t point_idx = 0; point_idx < size(); point_idx++)
{
{
current = path->at(point_idx % size());
current = path->at(point_idx % size());
//Check if the accumulated area doesn't exceed the maximum.
//Check if the accumulated area doesn't exceed the maximum.
if ((height_2 <= 1 //Almost exactly colinear (barring rounding errors).
&& LinearAlg2D::getDistFromLine(current, previous, next) <= 1)) // make sure that height_2 is not small because of cancellation of positive and negative areas
{
continue;
}
if (length2 < smallest_line_segment_squared
if (base_length_2 == 0) //Two line segments form a line back and forth with no area.
&& height_2 <= allowed_error_distance_squared) // removing the vertex doesn't introduce too much error.)
&& LinearAlg2D::getDistFromLine(current, previous, next) <= 5)) // make sure that height_2 is not small because of cancellation of positive and negative areas
{
continue;
}
if (length2 < smallest_line_segment_squared
&& height_2 <= allowed_error_distance_squared) // removing the vertex doesn't introduce too much error.)