I need to optimize a vector image with filled shapes constructed from bezier lines. Input image and how shapes look when separated:
I want to optimize the image by removing unnecessary lines and relying on stacking of shapes to preserve the look, but with much fewer vertices. Result shapes should look like this:
This problem can probably be broken down into separate steps:
Detecting stacked lines. This is more or less straightforward: calculate points along a line, find vertices along them. If vertices are stacked, it becomes trivial.
Finding bezier paths through filled areas of other shapes. An algorithm for this is likely to already exist, but I don't know it. (I really need help here.) Also it's unclear what shapes to go under. Maybe I should solve all possibilities and compare. It'll probably become clearer once I get to it. (Hints/suggestions are welcome.)
Finding optimal order of stacking so that the number of vertices is minimal. This sounds painful for someone not heavily into algorithms like me, but this seems to be some sort of minimization of number of vertices through different "paths", so can be done. (Correct me if I'm wrong.)
If a shape has a hole in it, it probably means that everything inside is to be stacked on top of it, so it's a separate simple case in which no extra calculations are needed.
Overall, the second point seems to be the most (the only?) problematic, so I need a nudge in the right direction.
In terms of the sample image, how do I find a bezier path for the potentially obscured part of the green shape to go through the blue shape (and optionally the yellow shape) and vice versa, for the blue one to go through the green one? I don't need the path to be the shortest one, I need it to have the least vertices in it.
Essentially, I need to find these paths with minimum number of vertices. Feel free to ignore the rest as irrelevant context.
[A p₁ p₂ B p₁ p₂ C ... Y p₁ p₂ Z]
, with optional holes defined the same way. If you don't want to handle holes, feel free to skip it, this case can be handled separately. – Cuttle