3

We have some polylines (list of points, has start and end point, not cyclic) and polygons (list of points, cyclic, no such thing as endpoints).

We want to map each polyline to a new polyline and each polygon to a new polygon so the total number of edges is small enough.

Let's say the number of edges originally is N, and we want our result to have M edges. N is much larger than M.

Polylines need to keep their start and end points, so they contribute at least 1 edge, one less than their vertex count. Polygons need to still be polygons, so they contribute at least 3 edges, equal to their vertex count. M will be at least large enough for this requirement.

The outputs should be as close as possible to the inputs. This would end up being an optimization problem of minimizing some metric to within some small tolerance of the true optimal solution. Originally I'd have used the area of the symmetric difference of the original and result (area between), but if another metric makes this easier to do I'll gladly take that instead.

It's okay if the results only include vertices in the original, the fit will be a little worse but it might be necessary to keep the time complexity down.

Since I'm asking for an algorithm, it'd be nice to also see an implementation. I'll likely have to re-implement it for where I'll be using it anyway, so details like what language or what data structures won't matter too much.


As for how good the approximation needs to be, about what you'd expect from getting a vector image from a bitmap image. The actual use here is for a tool for a game though, there's some strange details for the specific game, that's why the output edge count is fixed rather than the tolerance.

It's pretty hard to find any information on this kind of thing, so without even providing a full workable algorithm, just some pointers would be very much appreciated.

1 Answers1

0

Ramer–Douglas–Peucker algorithm (mentioned in the comment) is definitely good, but it has some disadvantages:

  • It requires open polyline on input, for closed polygon one has to fix an arbitrary point, which either decreases final quality or forces to test many points and decreases the performance.
  • The vertices of simplified polyline are a subset of original polyline vertices, other options are not considered. This permits very fast implementations, but again decreases the precision of simplified polyline.

Another alternative is to take well known algorithm for simplification of triangular meshes Surface Simplification Using Quadric Error Metrics and adapt it for polylines:

  • distances to planes containing triangles are replaced with distances to lines containing polyline segments,
  • quadratic forms lose one dimension if the polyline is two dimensional.

But the majority of the algorithm is kept including the queue of edge contraction (minimal heap) according to the estimated distortion such contraction produces in the polyline.

Here is an example of this algorithm application:

Polyline simplification

Red - original polyline, blue - simplified polyline, and one can see that all its vertices do not lie on the original polyline, while general shape is preserved as much as possible with so few line segments.

it'd be nice to also see an implementation.

One can find an implementation in MeshLib, see MRPolylineDecimate.h/.cpp

Fedor
  • 17,146
  • 13
  • 40
  • 131