Fast block placement algorithm, advice needed?

Posted by James Morris on Stack Overflow See other posts from Stack Overflow or by James Morris
Published on 2010-04-30T17:58:15Z Indexed on 2010/04/30 18:27 UTC
Read the original article Hit count: 467

Filed under:
|
|
|

I need to emulate the window placement strategy of the Fluxbox window manager.

As a rough guide, visualize randomly sized windows filling up the screen one at a time, where the rough size of each results in an average of 80 windows on screen without any window overlapping another.

It is important to note that windows will close and the space that closed windows previously occupied becomes available once more for the placement of new windows.

The window placement strategy has three binary options:

  • Windows build horizontal rows or vertical columns (potentially)

  • Windows are placed from left to right or right to left

  • Windows are placed from top to bottom or bottom to top

Why is the algorithm a problem?

It needs to operate to the deadlines of a real time thread in an audio application.

At this moment I am only concerned with getting a fast algorithm, don't concern yourself over the implications of real time threads and all the hurdles in programming that that brings.

So far I have two choices which I have built loose prototypes for:

1) A port of the Fluxbox placement algorithm into my code.

The problem with this is, the client (my program) gets kicked out of the audio server (JACK) when I try placing the worst case scenario of 256 blocks using the algorithm. This algorithm performs over 14000 full (linear) scans of the list of blocks already placed when placing the 256th window.

2) My alternative approach.

Only partially implemented, this approach uses a data structure for each area of rectangular free unused space (the list of windows can be entirely separate, and is not required for testing of this algorithm). The data structure acts as a node in a doubly linked list (with sorted insertion), as well as containing the coordinates of the top-left corner, and the width and height.

Furthermore, each block data structure also contains four links which connect to each immediately adjacent (touching) block on each of the four sides.

IMPORTANT RULE: Each block may only touch with one block per side.

The problem with this approach is, it's very complex. I have implemented the straightforward cases where 1) space is removed from one corner of a block, 2) splitting neighbouring blocks so that the IMPORTANT RULE is adhered to.

The less straightforward case, where the space to be removed can only be found within a column or row of boxes, is only partially implemented - if one of the blocks to be removed is an exact fit for width (ie column) or height (ie row) then problems occur. And don't even mention the fact this only checks columns one box wide, and rows one box tall.

I've implemented this algorithm in C - the language I am using for this project (I've not used C++ for a few years and am uncomfortable using it after having focused all my attention to C development, it's a hobby). The implementation is 700+ lines of code (including plenty of blank lines, brace lines, comments etc). The implementation only works for the horizontal-rows + left-right + top-bottom placement strategy.

So I've either got to add some way of making this +700 lines of code work for the other 7 placement strategy options, or I'm going to have to duplicate those +700 lines of code for the other seven options. Neither of these is attractive, the first, because the existing code is complex enough, the second, because of bloat.

The algorithm is not even at a stage where I can use it in the real time worst case scenario, because of missing functionality, so I still don't know if it actually performs better or worse than the first approach.

What else is there?

I've skimmed over and discounted:

  • Bin Packing algorithms: their emphasis on optimal fit does not match the requirements of this algorithm.

  • Recursive Bisection Placement algorithms: sounds promising, but these are for circuit design. Their emphasis is optimal wire length.

Both of these, especially the latter, all elements to be placed/packs are known before the algorithm begins.

I need an algorithm which works accumulatively with what it is given to do when it is told to do it.

What are your thoughts on this? How would you approach it? What other algorithms should I look at? Or even what concepts should I research seeing as I've not studied computer science/software engineering?

Please ask questions in comments if further information is needed.

[edit] If it makes any difference, the units for the coordinates will not be pixels. The units are unimportant, but the grid where windows/blocks/whatever can be placed will be 127 x 127 units.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about block