abey79 / vpype

The Swiss-Army-knife command-line tool for plotter vector graphics.
https://vpype.readthedocs.io/
MIT License
699 stars 61 forks source link

Split input paths option #19

Closed kcajf closed 4 years ago

kcajf commented 4 years ago

Hi,

Really enjoying using vpype - it's almost perfect for my usecase. Thanks for working on it. The only thing it's missing is the ability to split apart compound SVG paths like the following:

    <path
       d="M863.0296385949076 41.86503242499518 L862.9623748837308 41.521120608316096,862.9623748837308 41.521120608316096,862.8971895200468 41.25611307188438,862.8971895200468 41.25611307188438,862.7908929844685 40.93345873920916,862.7908929844685 40.93345873920916,862.7095183876838 40.891964713829545,862.7095183876838 40.891964713829545,862.5895211666606 40.769597447420004,862.5895211666606 40.769597447420004,862.5164508442873 40.62141491741736,862.5164508442873 40.62141491741736,862.4923675369382 40.420106908523685,862.4923675369382 40.420106908523685,862.5280731822396 40.2623348102656,862.5280731822396 40.2623348102656,862.6102864104688 40.12071548287142,862.6102864104688 40.12071548287142,862.7352607268034 40.0074273134035,862.7352607268034 40.0074273134035,862.8324143565258 39.96596975026054,862.8324143565258 39.96596975026054,862.9071528262286 39.93410175537325,862.9071528262286 39.93410175537325,863.0906672636063 39.9234912444897,863.0906672636063 39.9234912444897,863.2766793641988 39.97960662679129,863.2766793641988 39.97960662679129,863.3198597680265 40.0134800446979,863.3198597680265 40.0134800446979,863.2833154912806 39.75303028785544,863.2833154912806 39.75303028785544,863.2368170239122 39.39952890292069,863.2368170239122 39.39952890292069,863.1458893211085 38.9134872875336,863.1458893211085 38.9134872875336,863.055371818468 39.39497112333156,863.055371818468 39.39497112333156,862.9810526644872 39.613452845717234"
       id="path13" />
    <path
       d="M863.1458893211085 38.9134872875336 L863.1280319406782 36.573924331066564,863.1280319406782 36.573924331066564,863.0744780305056 35.94783126446387,863.0744780305056 35.94783126446387,862.8506819371191 35.37533768582962,862.8506819371191 35.37533768582962,862.5197689078283 34.94646885760864,862.5197689078283 34.94646885760864,862.1622840235327 34.63340409318897,862.1622840235327 34.63340409318897,861.6789953070174 34.39165946378033,861.6789953070174 34.39165946378033,861.0441604191626 34.239429625502716,861.0441604191626 34.239429625502716,860.4894512964853 34.239429625502716,860.4894512964853 34.239429625502716,859.4352642241919 34.22579274897197,859.4352642241919 34.22579274897197,859.3277279725654 40.401912252403804,859.3277279725654 40.401912252403804,860.4280033120643 40.420617379837736,860.4280033120643 40.420617379837736,861.8479795430648 40.43378024729109"
       id="path15" />
    <path
       d="M859.3277279725654 40.401912252403804 L857.3887573797482 40.36953378620246,857.3887573797482 40.36953378620246,857.2999080244373 43.88905118494593,857.2999080244373 43.88905118494593,855.819313324901 43.84908857350825,855.819313324901 43.84908857350825,855.655306184165 43.89156707927919,855.655306184165 43.89156707927919,855.5569037228353 43.96846593650734,855.5569037228353 43.96846593650734,855.4456300919456 43.88296199141486,855.4456300919456 43.88296199141486,855.2812127510466 43.8384780626247,855.2812127510466 43.8384780626247,854.7302501231915 43.82684660911316"
       id="path17" />

Splitting these apart into many single-segment <path> elements can, for certain types of drawing, allow for significantly more optimal draw paths. Currently, vpype doesn't seem to split these apart, and instead just re-orders them amongst themselves.

svgsort does have an option for this: https://github.com/inconvergent/svgsort/blob/master/svgsort/__init__.py#L86 For now, I've been using this as a pre-processor before vpype, and it's helped a lot for my drawings. Having this as a layer in vpype would be super cool!

kcajf commented 4 years ago

Here is a full example of when this would be useful:

raw.svg.txt opt_basic.svg.txt opt_split.svg.txt

(github doesn't allow uploading .svgs, hence the .txt extension)

raw is the very original svg - no sorting applied. opt_basic is after linesorting the original svg with vpype (and some scaling etc) opt_split is after splitting the original SVG paths into single-segment paths using svgsort, then linesorting with vpype.

The reason the original file has large multi-segment paths is that they are administrative streets from OSM, not straight subsegments. The third file is far more optimal, due to the fact that street segments have many more intersections with other street segments that full streets with full streets.

abey79 commented 4 years ago

I'm looking at the files right now. May I ask the exact options you used when calling svgsort before vpype in the third case?