2007-05-16 20:28:23 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
|
|
|
Copyright (C) 2002-2007 Nuclex Development Labs
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the IBM Common Public License as
|
|
|
|
published by the IBM Corporation; either version 1.0 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
IBM Common Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the IBM Common Public
|
|
|
|
License along with this library
|
|
|
|
*/
|
|
|
|
#endregion
|
2007-07-24 20:15:19 +00:00
|
|
|
|
2007-05-16 20:28:23 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Packing {
|
|
|
|
|
2007-05-21 19:04:48 +00:00
|
|
|
/// <summary>Packer using a custom algorithm by Markus 'Cygon' Ewald</summary>
|
2007-05-20 20:39:09 +00:00
|
|
|
/// <remarks>
|
2007-05-21 19:04:48 +00:00
|
|
|
/// <para>
|
|
|
|
/// Algorithm conceived by Markus Ewald (cygon at nuclex dot org), thought
|
2007-05-28 18:11:41 +00:00
|
|
|
/// I'm quite sure I'm not the first one to come up with it :)
|
2007-05-21 19:04:48 +00:00
|
|
|
/// </para>
|
|
|
|
/// <para>
|
2007-05-28 18:11:41 +00:00
|
|
|
/// The algorithm always places rectangles as low as possible in the packing
|
|
|
|
/// area. So, for any new rectangle that is to be added into the packing area,
|
|
|
|
/// the packer has to determine the X coordinate at which the rectangle can have
|
2007-06-18 21:25:16 +00:00
|
|
|
/// the lowest overall height without intersecting any other rectangles.
|
2007-05-28 18:11:41 +00:00
|
|
|
/// </para>
|
|
|
|
/// <para>
|
|
|
|
/// To quickly discover these locations, the packer uses a sophisticated
|
|
|
|
/// data structure that stores the upper silhouette of the packing area. When
|
|
|
|
/// a new rectangle needs to be added, only the silouette edges need to be
|
|
|
|
/// analyzed to find the position where the rectangle would achieve the lowest
|
|
|
|
/// placement possible in the packing area.
|
2007-05-21 19:04:48 +00:00
|
|
|
/// </para>
|
2007-05-20 20:39:09 +00:00
|
|
|
/// </remarks>
|
2007-05-16 20:28:23 +00:00
|
|
|
public class CygonRectanglePacker : RectanglePacker {
|
|
|
|
|
2007-05-16 21:27:59 +00:00
|
|
|
#region class SliceStartComparer
|
|
|
|
|
|
|
|
/// <summary>Compares the starting position of height slices</summary>
|
|
|
|
private class SliceStartComparer : IComparer<Point> {
|
|
|
|
|
|
|
|
/// <summary>Provides a default instance for the anchor rank comparer</summary>
|
|
|
|
public static SliceStartComparer Default = new SliceStartComparer();
|
|
|
|
|
|
|
|
/// <summary>Compares the starting position of two height slices</summary>
|
|
|
|
/// <param name="left">Left slice start that will be compared</param>
|
|
|
|
/// <param name="right">Right slice start that will be compared</param>
|
|
|
|
/// <returns>The relation of the two slice starts ranks to each other</returns>
|
|
|
|
public int Compare(Point left, Point right) {
|
|
|
|
return left.X - right.X;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2007-05-16 20:28:23 +00:00
|
|
|
/// <summary>Initializes a new rectangle packer</summary>
|
2007-05-21 19:04:48 +00:00
|
|
|
/// <param name="packingAreaWidth">Maximum width of the packing area</param>
|
|
|
|
/// <param name="packingAreaHeight">Maximum height of the packing area</param>
|
|
|
|
public CygonRectanglePacker(int packingAreaWidth, int packingAreaHeight)
|
|
|
|
: base(packingAreaWidth, packingAreaHeight) {
|
2007-05-16 21:27:59 +00:00
|
|
|
|
|
|
|
this.heightSlices = new List<Point>();
|
|
|
|
|
|
|
|
// At the beginning, the packing area is a single slice of height 0
|
|
|
|
this.heightSlices.Add(new Point(0, 0));
|
|
|
|
|
|
|
|
}
|
2007-05-16 20:28:23 +00:00
|
|
|
|
|
|
|
/// <summary>Tries to allocate space for a rectangle in the packing area</summary>
|
|
|
|
/// <param name="rectangleWidth">Width of the rectangle to allocate</param>
|
|
|
|
/// <param name="rectangleHeight">Height of the rectangle to allocate</param>
|
|
|
|
/// <param name="placement">Output parameter receiving the rectangle's placement</param>
|
|
|
|
/// <returns>True if space for the rectangle could be allocated</returns>
|
2007-05-21 19:04:48 +00:00
|
|
|
public override bool TryPack(
|
2007-05-16 20:28:23 +00:00
|
|
|
int rectangleWidth, int rectangleHeight, out Point placement
|
|
|
|
) {
|
2007-05-21 19:04:48 +00:00
|
|
|
// If the rectangle is larger than the packing area in any dimension,
|
|
|
|
// it will never fit!
|
|
|
|
if(
|
|
|
|
(rectangleWidth > PackingAreaWidth) || (rectangleHeight > PackingAreaHeight)
|
|
|
|
) {
|
2007-05-20 20:39:09 +00:00
|
|
|
placement = Point.Zero;
|
|
|
|
return false;
|
2007-05-21 19:04:48 +00:00
|
|
|
}
|
2007-05-20 20:39:09 +00:00
|
|
|
|
2007-05-28 18:11:41 +00:00
|
|
|
// Determine the placement for the new rectangle
|
2007-05-31 19:15:39 +00:00
|
|
|
bool fits = tryFindBestPlacement(rectangleWidth, rectangleHeight, out placement);
|
2007-05-28 18:11:41 +00:00
|
|
|
|
|
|
|
// If a place for the rectangle could be found, update the height slice table to
|
|
|
|
// mark the region of the rectangle as being taken.
|
2007-05-21 19:04:48 +00:00
|
|
|
if(fits)
|
|
|
|
integrateRectangle(placement.X, rectangleWidth, placement.Y + rectangleHeight);
|
2007-05-20 20:39:09 +00:00
|
|
|
|
2007-05-21 19:04:48 +00:00
|
|
|
return fits;
|
2007-05-20 20:39:09 +00:00
|
|
|
}
|
|
|
|
|
2007-06-12 19:15:55 +00:00
|
|
|
/// <summary>Finds the best position for a rectangle of the given dimensions</summary>
|
2007-05-20 20:39:09 +00:00
|
|
|
/// <param name="rectangleWidth">Width of the rectangle to find a position for</param>
|
|
|
|
/// <param name="rectangleHeight">Height of the rectangle to find a position for</param>
|
2007-07-19 20:02:05 +00:00
|
|
|
/// <param name="placement">Receives the best placement found for the rectangle</param>
|
|
|
|
/// <returns>True if a valid placement for the rectangle could be found</returns>
|
2007-05-31 19:15:39 +00:00
|
|
|
private bool tryFindBestPlacement(
|
2007-05-21 19:04:48 +00:00
|
|
|
int rectangleWidth, int rectangleHeight, out Point placement
|
|
|
|
) {
|
2007-05-20 21:03:21 +00:00
|
|
|
|
2007-05-21 19:04:48 +00:00
|
|
|
// Slice index, vertical position and score of the best placement we could find
|
|
|
|
int bestSliceIndex = -1; // Slice index where the best placement was found
|
|
|
|
int bestSliceY = 0; // Y position of the best placement found
|
|
|
|
int bestScore = PackingAreaWidth * PackingAreaHeight; // lower == better!
|
2007-05-20 21:03:21 +00:00
|
|
|
|
|
|
|
// This is the counter for the currently checked position. The search works by
|
|
|
|
// skipping from slice to slice, determining the suitability of the location for the
|
|
|
|
// placement of the rectangle.
|
2007-05-20 20:39:09 +00:00
|
|
|
int leftSliceIndex = 0;
|
2007-05-20 21:03:21 +00:00
|
|
|
|
|
|
|
// Determine the slice in which the right end of the rectangle is located
|
2007-05-20 20:39:09 +00:00
|
|
|
int rightSliceIndex = this.heightSlices.BinarySearch(
|
|
|
|
new Point(rectangleWidth, 0), SliceStartComparer.Default
|
|
|
|
);
|
|
|
|
if(rightSliceIndex < 0)
|
|
|
|
rightSliceIndex = ~rightSliceIndex;
|
|
|
|
|
|
|
|
while(rightSliceIndex <= this.heightSlices.Count) {
|
2007-05-20 21:03:21 +00:00
|
|
|
|
|
|
|
// Determine the highest slice within the slices covered by the rectangle at
|
|
|
|
// its current placement. We cannot put the rectangle any lower than this without
|
|
|
|
// overlapping the other rectangles.
|
|
|
|
int highest = this.heightSlices[leftSliceIndex].Y;
|
|
|
|
for(int index = leftSliceIndex + 1; index < rightSliceIndex; ++index)
|
|
|
|
if(this.heightSlices[index].Y > highest)
|
|
|
|
highest = this.heightSlices[index].Y;
|
|
|
|
|
2007-05-28 18:11:41 +00:00
|
|
|
// Only process this position if it doesn't leave the packing area
|
2007-05-21 19:04:48 +00:00
|
|
|
if((highest + rectangleHeight < PackingAreaHeight)) {
|
2007-05-20 21:03:21 +00:00
|
|
|
int score = highest;
|
|
|
|
|
|
|
|
if(score < bestScore) {
|
|
|
|
bestSliceIndex = leftSliceIndex;
|
2007-05-21 19:04:48 +00:00
|
|
|
bestSliceY = highest;
|
2007-05-20 21:03:21 +00:00
|
|
|
bestScore = score;
|
|
|
|
}
|
|
|
|
}
|
2007-05-20 20:39:09 +00:00
|
|
|
|
|
|
|
// Advance the starting slice to the next slice start
|
|
|
|
++leftSliceIndex;
|
2007-05-20 21:03:21 +00:00
|
|
|
if(leftSliceIndex >= this.heightSlices.Count)
|
|
|
|
break;
|
2007-05-20 20:39:09 +00:00
|
|
|
|
2007-05-28 18:11:41 +00:00
|
|
|
// Advance the ending slice until we're on the proper slice again, given the new
|
2007-05-20 21:03:21 +00:00
|
|
|
// starting position of the rectangle.
|
|
|
|
int rightRectangleEnd = this.heightSlices[leftSliceIndex].X + rectangleWidth;
|
|
|
|
for(; rightSliceIndex <= this.heightSlices.Count; ++rightSliceIndex) {
|
|
|
|
int rightSliceStart;
|
2007-05-20 20:39:09 +00:00
|
|
|
if(rightSliceIndex == this.heightSlices.Count)
|
2007-05-21 19:04:48 +00:00
|
|
|
rightSliceStart = PackingAreaWidth;
|
2007-05-20 21:03:21 +00:00
|
|
|
else
|
|
|
|
rightSliceStart = this.heightSlices[rightSliceIndex].X;
|
|
|
|
|
|
|
|
// Is this the slice we're looking for?
|
|
|
|
if(rightSliceStart > rightRectangleEnd)
|
2007-05-20 20:39:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-05-20 21:03:21 +00:00
|
|
|
|
|
|
|
// If we crossed the end of the slice array, the rectangle's right end has left
|
|
|
|
// the packing area, and thus, our search ends.
|
|
|
|
if(rightSliceIndex > this.heightSlices.Count)
|
|
|
|
break;
|
|
|
|
|
2007-05-28 18:11:41 +00:00
|
|
|
} // while rightSliceIndex <= this.heightSlices.Count
|
2007-05-20 20:39:09 +00:00
|
|
|
|
2007-05-28 18:11:41 +00:00
|
|
|
// Return the best placement we found for this rectangle. If the rectangle
|
|
|
|
// didn't fit anywhere, the slice index will still have its initialization value
|
|
|
|
// of -1 and we can report that no placement could be found.
|
2007-05-21 19:04:48 +00:00
|
|
|
if(bestSliceIndex == -1) {
|
|
|
|
placement = Point.Zero;
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
placement = new Point(this.heightSlices[bestSliceIndex].X, bestSliceY);
|
|
|
|
return true;
|
|
|
|
}
|
2007-05-20 21:03:21 +00:00
|
|
|
|
2007-05-16 21:27:59 +00:00
|
|
|
}
|
2007-05-16 20:28:23 +00:00
|
|
|
|
2007-05-16 21:27:59 +00:00
|
|
|
/// <summary>Integrates a new rectangle into the height slice table</summary>
|
|
|
|
/// <param name="left">Position of the rectangle's left side</param>
|
|
|
|
/// <param name="width">Width of the rectangle</param>
|
2007-05-20 20:39:09 +00:00
|
|
|
/// <param name="bottom">Position of the rectangle's lower side</param>
|
|
|
|
private void integrateRectangle(int left, int width, int bottom) {
|
2007-05-16 21:27:59 +00:00
|
|
|
|
|
|
|
// Find the first slice that is touched by the rectangle
|
|
|
|
int startSlice = this.heightSlices.BinarySearch(
|
|
|
|
new Point(left, 0), SliceStartComparer.Default
|
|
|
|
);
|
|
|
|
int firstSliceOriginalHeight;
|
|
|
|
|
|
|
|
// Did we score a direct hit on an existing slice start?
|
|
|
|
if(startSlice >= 0) {
|
|
|
|
|
|
|
|
// We scored a direct hit, so we can replace the slice we have hit
|
|
|
|
firstSliceOriginalHeight = this.heightSlices[startSlice].Y;
|
|
|
|
this.heightSlices[startSlice] = new Point(left, bottom);
|
|
|
|
|
|
|
|
} else { // No direct hit, slice starts inside another slice
|
|
|
|
|
|
|
|
// Add a new slice after the slice in which we start
|
|
|
|
startSlice = ~startSlice;
|
|
|
|
firstSliceOriginalHeight = this.heightSlices[startSlice - 1].Y;
|
|
|
|
this.heightSlices.Insert(startSlice, new Point(left, bottom));
|
2007-05-16 20:28:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-20 20:39:09 +00:00
|
|
|
int right = left + width;
|
|
|
|
++startSlice;
|
|
|
|
|
|
|
|
// Special case, the rectangle started on the last slice, so we cannot
|
|
|
|
// use the start slice + 1 for the binary search and the possibly already
|
|
|
|
// modified start slice height now only remains in our temporary
|
|
|
|
// firstSliceOriginalHeight variable
|
2007-05-16 21:27:59 +00:00
|
|
|
if(startSlice >= this.heightSlices.Count) {
|
2007-05-20 20:39:09 +00:00
|
|
|
|
|
|
|
// If the slice ends within the last slice (usual case, unless it has the
|
2007-05-20 21:03:21 +00:00
|
|
|
// exact same width the packing area has), add another slice to return to
|
|
|
|
// the original height at the end of the rectangle.
|
2007-05-21 19:04:48 +00:00
|
|
|
if(right < PackingAreaWidth)
|
2007-05-20 20:39:09 +00:00
|
|
|
this.heightSlices.Add(new Point(right, firstSliceOriginalHeight));
|
|
|
|
|
|
|
|
} else { // The rectangle doesn't start on the last slice
|
2007-05-16 21:27:59 +00:00
|
|
|
|
|
|
|
int endSlice = this.heightSlices.BinarySearch(
|
|
|
|
startSlice, this.heightSlices.Count - startSlice,
|
|
|
|
new Point(right, 0), SliceStartComparer.Default
|
|
|
|
);
|
2007-05-16 20:28:23 +00:00
|
|
|
|
2007-05-20 20:39:09 +00:00
|
|
|
// Another direct hit on the final slice's end?
|
|
|
|
if(endSlice > 0) {
|
2007-05-16 21:27:59 +00:00
|
|
|
|
2007-05-20 20:39:09 +00:00
|
|
|
this.heightSlices.RemoveRange(startSlice, endSlice - startSlice);
|
2007-05-16 21:27:59 +00:00
|
|
|
|
2007-05-20 20:39:09 +00:00
|
|
|
} else { // No direct hit, rectangle ends inside another slice
|
2007-05-16 21:27:59 +00:00
|
|
|
|
2007-06-12 19:15:55 +00:00
|
|
|
// Make index from negative BinarySearch() result
|
2007-05-20 20:39:09 +00:00
|
|
|
endSlice = ~endSlice;
|
2007-05-16 21:27:59 +00:00
|
|
|
|
2007-05-20 20:39:09 +00:00
|
|
|
// Find out to which height we need to return at the right end of
|
|
|
|
// the rectangle
|
|
|
|
int returnHeight;
|
|
|
|
if(endSlice == startSlice)
|
|
|
|
returnHeight = firstSliceOriginalHeight;
|
|
|
|
else
|
|
|
|
returnHeight = this.heightSlices[endSlice - 1].Y;
|
2007-05-16 21:27:59 +00:00
|
|
|
|
2007-05-20 21:03:21 +00:00
|
|
|
// Remove all slices covered by the rectangle and begin a new slice at its end
|
|
|
|
// to return back to the height of the slice on which the rectangle ends.
|
2007-05-20 20:39:09 +00:00
|
|
|
this.heightSlices.RemoveRange(startSlice, endSlice - startSlice);
|
2007-05-21 19:04:48 +00:00
|
|
|
if(right < PackingAreaWidth)
|
2007-05-20 20:39:09 +00:00
|
|
|
this.heightSlices.Insert(startSlice, new Point(right, returnHeight));
|
|
|
|
|
2007-05-28 18:11:41 +00:00
|
|
|
} // if endSlice > 0
|
2007-05-16 21:27:59 +00:00
|
|
|
|
2007-05-28 18:11:41 +00:00
|
|
|
} // if startSlice >= this.heightSlices.Count
|
2007-05-20 20:39:09 +00:00
|
|
|
|
2007-05-16 20:28:23 +00:00
|
|
|
}
|
|
|
|
|
2007-05-16 21:27:59 +00:00
|
|
|
/// <summary>Stores the height silhouette of the rectangles</summary>
|
|
|
|
private List<Point> heightSlices;
|
2007-05-16 20:28:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Packing
|