aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/BinPacker.java
blob: 9fc2e09373c8d1b4e28650d095517f320077a433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ide.eclipse.adt.internal.editors.layout.gle2;

import com.android.annotations.Nullable;
import com.android.ide.common.api.Rect;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * This class implements 2D bin packing: packing rectangles into a given area as
 * tightly as "possible" (bin packing in general is NP hard, so this class uses
 * heuristics).
 * <p>
 * The algorithm implemented is to keep a set of (possibly overlapping)
 * available areas for placement. For each newly inserted rectangle, we first
 * pick which available space to occupy, and we then subdivide the
 * current rectangle into all the possible remaining unoccupied sub-rectangles.
 * We also remove any other space rectangles which are no longer eligible if
 * they are intersecting the newly placed rectangle.
 * <p>
 * This algorithm is not very fast, so should not be used for a large number of
 * rectangles.
 */
class BinPacker {
    /**
     * When enabled, the successive passes are dumped as PNG images showing the
     * various available and occupied rectangles)
     */
    private static final boolean DEBUG = false;

    private final List<Rect> mSpace = new ArrayList<Rect>();
    private final int mMinHeight;
    private final int mMinWidth;

    /**
     * Creates a new {@linkplain BinPacker}. To use it, first add one or more
     * initial available space rectangles with {@link #addSpace(Rect)}, and then
     * place the rectangles with {@link #occupy(int, int)}. The returned
     * {@link Rect} from {@link #occupy(int, int)} gives the coordinates of the
     * positioned rectangle.
     *
     * @param minWidth the smallest width of any rectangle placed into this bin
     * @param minHeight the smallest height of any rectangle placed into this bin
     */
    BinPacker(int minWidth, int minHeight) {
        mMinWidth = minWidth;
        mMinHeight = minHeight;

        if (DEBUG) {
            mAllocated = new ArrayList<Rect>();
            sLayoutId++;
            sRectId = 1;
        }
    }

    /** Adds more available space */
    void addSpace(Rect rect) {
        if (rect.w >= mMinWidth && rect.h >= mMinHeight) {
            mSpace.add(rect);
        }
    }

    /** Attempts to place a rectangle of the given dimensions, if possible */
    @Nullable
    Rect occupy(int width, int height) {
        int index = findBest(width, height);
        if (index == -1) {
            return null;
        }

        return split(index, width, height);
    }

    /**
     * Finds the best available space rectangle to position a new rectangle of
     * the given size in.
     */
    private int findBest(int width, int height) {
        if (mSpace.isEmpty()) {
            return -1;
        }

        // Try to pack as far up as possible first
        int bestIndex = -1;
        boolean multipleAtSameY = false;
        int minY = Integer.MAX_VALUE;
        for (int i = 0, n = mSpace.size(); i < n; i++) {
            Rect rect = mSpace.get(i);
            if (rect.y <= minY) {
                if (rect.w >= width && rect.h >= height) {
                    if (rect.y < minY) {
                        minY = rect.y;
                        multipleAtSameY = false;
                        bestIndex = i;
                    } else if (minY == rect.y) {
                        multipleAtSameY = true;
                    }
                }
            }
        }

        if (!multipleAtSameY) {
            return bestIndex;
        }

        bestIndex = -1;

        // Pick a rectangle. This currently tries to find the rectangle whose shortest
        // side most closely matches the placed rectangle's size.
        // Attempt to find the best short side fit
        int bestShortDistance = Integer.MAX_VALUE;
        int bestLongDistance = Integer.MAX_VALUE;

        for (int i = 0, n = mSpace.size(); i < n; i++) {
            Rect rect = mSpace.get(i);
            if (rect.y != minY) { // Only comparing elements at same y
                continue;
            }
            if (rect.w >= width && rect.h >= height) {
                if (width < height) {
                    int distance = rect.w - width;
                    if (distance < bestShortDistance ||
                            distance == bestShortDistance &&
                            (rect.h - height) < bestLongDistance) {
                        bestShortDistance = distance;
                        bestLongDistance = rect.h - height;
                        bestIndex = i;
                    }
                } else {
                    int distance = rect.w - width;
                    if (distance < bestShortDistance ||
                            distance == bestShortDistance &&
                            (rect.h - height) < bestLongDistance) {
                        bestShortDistance = distance;
                        bestLongDistance = rect.h - height;
                        bestIndex = i;
                    }
                }
            }
        }

        return bestIndex;
    }

    /**
     * Removes the rectangle at the given index. Since the rectangles are in an
     * {@link ArrayList}, removing a rectangle in the normal way is slow (it
     * would involve shifting all elements), but since we don't care about
     * order, this always swaps the to-be-deleted element to the last position
     * in the array first, <b>then</b> it deletes it (which should be
     * immediate).
     *
     * @param index the index in the {@link #mSpace} list to remove a rectangle
     *            from
     */
    private void removeRect(int index) {
        assert !mSpace.isEmpty();
        int lastIndex = mSpace.size() - 1;
        if (index != lastIndex) {
            // Swap before remove to make deletion faster since we don't
            // care about order
            Rect temp = mSpace.get(index);
            mSpace.set(index, mSpace.get(lastIndex));
            mSpace.set(lastIndex, temp);
        }

        mSpace.remove(lastIndex);
    }

    /**
     * Splits the rectangle at the given rectangle index such that it can contain
     * a rectangle of the given width and height. */
    private Rect split(int index, int width, int height) {
        Rect rect = mSpace.get(index);
        assert rect.w >= width && rect.h >= height : rect;

        Rect r = new Rect(rect);
        r.w = width;
        r.h = height;

        // Remove all rectangles that intersect my rectangle
        for (int i = 0; i < mSpace.size(); i++) {
            Rect other = mSpace.get(i);
            if (other.intersects(r)) {
                removeRect(i);
                i--;
            }
        }


        // Split along vertical line x = rect.x + width:
        // (rect.x,rect.y)
        //     +-------------+-------------------------+
        //     |             |                         |
        //     |             |                         |
        //     |             | height                  |
        //     |             |                         |
        //     |             |                         |
        //     +-------------+           B             | rect.h
        //     |   width                               |
        //     |             |                         |
        //     |      A                                |
        //     |             |                         |
        //     |                                       |
        //     +---------------------------------------+
        //                    rect.w
        int remainingHeight = rect.h - height;
        int remainingWidth = rect.w - width;
        if (remainingHeight >= mMinHeight) {
            mSpace.add(new Rect(rect.x, rect.y + height, width, remainingHeight));
        }
        if (remainingWidth >= mMinWidth) {
            mSpace.add(new Rect(rect.x + width, rect.y, remainingWidth, rect.h));
        }

        // Split along horizontal line y = rect.y + height:
        //     +-------------+-------------------------+
        //     |             |                         |
        //     |             | height                  |
        //     |             |          A              |
        //     |             |                         |
        //     |             |                         | rect.h
        //     +-------------+ - - - - - - - - - - - - |
        //     |  width                                |
        //     |                                       |
        //     |                B                      |
        //     |                                       |
        //     |                                       |
        //     +---------------------------------------+
        //                   rect.w
        if (remainingHeight >= mMinHeight) {
            mSpace.add(new Rect(rect.x, rect.y + height, rect.w, remainingHeight));
        }
        if (remainingWidth >= mMinWidth) {
            mSpace.add(new Rect(rect.x + width, rect.y, remainingWidth, height));
        }

        // Remove redundant rectangles. This is not very efficient.
        for (int i = 0; i < mSpace.size() - 1; i++) {
            for (int j = i + 1; j < mSpace.size(); j++) {
                Rect iRect = mSpace.get(i);
                Rect jRect = mSpace.get(j);
                if (jRect.contains(iRect)) {
                    removeRect(i);
                    i--;
                    break;
                }
                if (iRect.contains(jRect)) {
                    removeRect(j);
                    j--;
                }
            }
        }

        if (DEBUG) {
            mAllocated.add(r);
            dumpImage();
        }

        return r;
    }

    // DEBUGGING CODE: Enable with DEBUG

    private List<Rect> mAllocated;
    private static int sLayoutId;
    private static int sRectId;
    private void dumpImage() {
        if (DEBUG) {
            int width = 100;
            int height = 100;
            for (Rect rect : mSpace) {
                width = Math.max(width, rect.w);
                height = Math.max(height, rect.h);
            }
            width += 10;
            height += 10;

            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = image.createGraphics();
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, image.getWidth(), image.getHeight());

            Color[] colors = new Color[] {
                    Color.blue, Color.cyan,
                    Color.green, Color.magenta, Color.orange,
                    Color.pink, Color.red, Color.white, Color.yellow, Color.darkGray,
                    Color.lightGray, Color.gray,
            };

            char allocated = 'A';
            for (Rect rect : mAllocated) {
                Color color = new Color(0x9FFFFFFF, true);
                g.setColor(color);
                g.setBackground(color);
                g.fillRect(rect.x, rect.y, rect.w, rect.h);
                g.setColor(Color.WHITE);
                g.drawRect(rect.x, rect.y, rect.w, rect.h);
                g.drawString("" + (allocated++),
                        rect.x + rect.w / 2, rect.y + rect.h / 2);
            }

            int colorIndex = 0;
            for (Rect rect : mSpace) {
                Color color = colors[colorIndex];
                colorIndex = (colorIndex + 1) % colors.length;

                color = new Color(color.getRed(), color.getGreen(), color.getBlue(), 128);
                g.setColor(color);

                g.fillRect(rect.x, rect.y, rect.w, rect.h);
                g.setColor(Color.WHITE);
                g.drawString(Integer.toString(colorIndex),
                        rect.x + rect.w / 2, rect.y + rect.h / 2);
            }


            g.dispose();

            File file = new File("/tmp/layout" + sLayoutId + "_pass" + sRectId + ".png");
            try {
                ImageIO.write(image, "PNG", file);
                System.out.println("Wrote diagnostics image " + file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            sRectId++;
        }
    }
}