7

I have SVG abirtrary paths which i need to pack as efficiently as possible within a given rectangle(as less waste of space as possible). After some research i found the bin packing algorithms which seems to be dealing with boxes and not curved random shapes(my SVG shapes are quite complex and include beziers etc.).

AFAIK, there is no deterministic algorithm for actually packing abstract shapes.

I wish to be proven wrong here which would be ideal(having a mathematical deterministic method for packing them). In case I am right however and there is not, what would be the best approach to this problem

The subject name is Shape Nesting, Nesting Problem or Nesting Process.

In Shape Nesting there is no single/uniform algorithm or mathematical method for nesting shapes and getting the least space waste possible.

  • The 1st method is the packing algorithm(creates an imaginary bounding box for each shape and uses a rectangular 2D algorithm to pack the bounding boxes). This method is fast but the least efficient in regards to space waste.

  • The 2nd method is some kind of incremental rotation. The algorithm rotates the shape at incremental steps and checks if it fits in the space. This is better than the packing method in regards to space waste but it is painstakingly slow,

What are some other classroom examples for achieving a solution to this problem?

5
  • This question appears to be off-topic because it is about mathematical algorithms. Sounds like it would be better on math.stackexchange.com Sep 9, 2013 at 7:22
  • Efficiency of an algorithm is still within the realm of mathematics. Sep 9, 2013 at 9:21
  • 1
    what if it was not asking for a library recommendation but merely asking a question about boolean operations? Would you still vote to close it? Sep 9, 2013 at 9:25
  • 1
    If it was about implementation of, union, subtraction and provided the OP showed some attempt to solve it then no, otherwise yes. Sep 9, 2013 at 9:28
  • im convinced. I'll still leave it on until votes are gathered to close it down. Sep 9, 2013 at 9:29

2 Answers 2

3

[Edit1] new answer

as mentioned before bin-packing is NP complete (hard) so forget about algebraic solution known approaches are:

  1. generate and test

    either you test all possibility of the problem and remember the best solution or incrementally add items (not all at once) one by one with the same way. It is basically what you are doing now without proper heuristic is unusably slow. But has the best space efficiency (the first one is much better but much slower) O(N!)

  2. take advantage of sorting items by size

    something like this it is much faster almost O(N.log(N)) (according to used sorting algorithm). Space efficiency strongly depends on the items size range and count. For rectangular shapes is this the best approach (fastest and usable even for N>1000). For complex shapes is this not a good way but look at it anyway maybe you get some idea ...

  3. use of Neural network

    This is extremly vague approach without any warrant of solution but possible best space efficiency/runtime ratio

  4. I think there could be some field approach out there

    I sow a few for generating graph layouts. All items create fields (booth attractive and repulsive) so they are moving to semi-stable state.

    • At first all items are at random locations
    • When the movement stop remember best solution and shake all items a little or randomize their position again.
    • Cycle this few times

    This approach is much faster then genere and test and can provide very close solution to it but it can hang in local min/max or oscillate if the fields are not optimally choosed. For example all items can have constant attractive force to each other and repulsive force getting stronger only when the items are very close. You have to prevent overlapping of items (either by stronger repulsion or by collision tests). You have also to create some rotation moment for example with that repulsive force. It differs on any vertex so it creates a rotation moment (that can automatically align similar sides closer together). Also you can have semi-stable state with big distances between items and after finding best solution just turn off repulsion fields so they stick together. Sometimes it can have better results some times not ... here is nice example for graph layout computation

    And here solver for placing sliders in 2D:


[Edit0] old answer before reformulating the question

I am not clear what you want to achieve.

  1. have SVG picture and want to separate its parts to rectangular regions
    • as filled as can be
    • least empty space in them
    • no shape change in picture
  2. have svg picture and want to change its shapes according to some purpose
    • if this is the case some additional info is needed

[solution for 1]

  1. create a list of points for whole SVG in global SVG space (all points are transformed)
    • for line you need add 2 points
    • for rectangles 4 points
    • circle/elipse/bezier/eliptic arc 8 points
  2. find local centres of mass
    • use classical approach
    • or can speed things up by computing the average density of points per x,y axis separately and after that just check all combinations of found positions of local max of densities if they really are sub cluster center or not.
  3. all sub cluster center is the center of your region
    • now find the most far points which are still part of your cluster (the are close enough to neighbour points)
    • create rectangular area that cover all points from sub cluster.
    • you also can remove all used points from list
  4. repeat fro all valid sub clusters
    • until all points are used

another not precise but simpler approach is:

  1. find SVG size
  2. create planar map of svg with some precision for example int map[256][256].
    • size of map can be constant or with the same aspect as SVG
  3. clear map with 0
  4. for any point of SVG set related map point to 1 (or inc or whatever)
  5. now just segmentate map and you will have find your objects
    • after segmentation you have position and size of all objects
    • so finding of bounding boxes should be easy
4
  • yeah i know what you mean now ... it has nothing to do with svg,... i am not shore if there is some uniform algorithm for this. Afraid so that trial and error is your only safe option. if you have to create the same shapes maybe some user predefined filling pattern will be better than any algorithm. (that is what i use for plasma cutting)
    – Spektre
    Sep 9, 2013 at 18:29
  • @Nicholas Kyriakides finally had time to reedit my answer so check it out if it helps. but as I can see you have moved to better point with your problem anyway :)
    – Spektre
    Jan 30, 2014 at 14:47
  • @Spektre....Unless you have something all cooked up and ready that you want to pass to me:) Jan 30, 2014 at 15:46
  • @Nicholas Kyriakides sadly no I did only that rectangular packing and my field implementations are for different use. For complex shapes I use predefined patterns but as I wrote before they are suitable only for the same shape packing (which is what I needed for plasma cutters)
    – Spektre
    Jan 30, 2014 at 18:30
1

You can start with a variant of the rectangle bin-packing algorithm and add rotation. There is a method "Guillotine bin packer" and you can download a paper and a library at github.

3
  • the bin packing algorithm creates bounding boxes. Highly inefficient for irregular shapes because the shape e.g is a ''star''. Creating a bounding box wastes the spaces between the star spokes for example. Nov 19, 2013 at 7:13
  • Why not use a circle packing for a star? There is a d3 jquery plugin? You can mix rectangle and circles and ellipsoid? Is this only a star shape?
    – Micromega
    Nov 19, 2013 at 10:16
  • No i am talking about irregular shapes which can be anything. Circle packing still has a lot of space waste for a star:). Nov 19, 2013 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.