views:

1499

answers:

2

I have N items of 2D image data that will be rectangular and I want to pack them into a single power of 2 texture as efficiently as possible.

A simple non-efficient and naive implementation of an algorithm to pack these rects would be easy to whip up, but I'm sure people have come up with algorithms to do this as space efficiently as possible. I've found various references to lightmap packing which is similar to what I'm looking for, but the algorithms for lightmapping tend to take non-rectangular images into account which actually complicates things more than I need them to be.

Does anyone have hints? Names of algorithms or paper authors I should google?

Thanks.

+2  A: 

Your problem in 1D is called Bin Packing. Perhaps that's a good start for your search.

Note that the problem you want to solve is really hard (it is NP-hard). So you should not search for the optimal solution, but some clever heuristical algorithm.

I think bottom-up dynamic programming is possible for 1D bin packing, but not for the 2D case.

You could think of simplifying your problem by only solving the 1D problem, making restrictions like cutting the textures into several (variable sized) slices in 1 dimension.

Another possiblity is running meta-heuristic optimization on it, like Evolutionary Algorithms or Particle Swarm Optimization.

ypnos
+2  A: 

I've needed to do this very thing you describe.

This is the Python code I used, it's a recipe in the Python Cookbook:

Recipe 442299: pack multiple images of different sizes into one image

This code doesn't look like it is searching for an optimal solution. Also it seems to want to pack all the images into one single texture.
ypnos