Began rolling out a new, more effective rectangle packer; renamed the previous 'Cygon' algorithm to 'Simple' because of its simplicity

git-svn-id: file:///srv/devel/repo-conversion/nusu@20 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2007-05-16 21:27:59 +00:00
parent d09bb30cec
commit 62d54b5795
6 changed files with 272 additions and 47 deletions

View File

@ -150,6 +150,15 @@
<Name>RectanglePacker.Test</Name> <Name>RectanglePacker.Test</Name>
<DependentUpon>RectanglePacker.cs</DependentUpon> <DependentUpon>RectanglePacker.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Packing\SimpleRectanglePacker.cs">
<XNAUseContentPipeline>false</XNAUseContentPipeline>
<Name>SimpleRectanglePacker</Name>
</Compile>
<Compile Include="Source\Packing\SimpleRectanglePacker.Test.cs">
<XNAUseContentPipeline>false</XNAUseContentPipeline>
<Name>SimpleRectanglePacker.Test</Name>
<DependentUpon>SimpleRectanglePacker.cs</DependentUpon>
</Compile>
<Compile Include="Source\Serialization\BinarySerializer.Test.cs"> <Compile Include="Source\Serialization\BinarySerializer.Test.cs">
<XNAUseContentPipeline>false</XNAUseContentPipeline> <XNAUseContentPipeline>false</XNAUseContentPipeline>
<Name>BinarySerializer.Test</Name> <Name>BinarySerializer.Test</Name>

View File

@ -86,9 +86,8 @@ namespace Nuclex.Support.Packing {
/// <param name="right">Right anchor point that will be compared</param> /// <param name="right">Right anchor point that will be compared</param>
/// <returns>The relation of the two anchor point's ranks to each other</returns> /// <returns>The relation of the two anchor point's ranks to each other</returns>
public int Compare(Point left, Point right) { public int Compare(Point left, Point right) {
return 0;
//return Math.Min(left.X, left.Y) - Math.Min(right.X, right.Y); //return Math.Min(left.X, left.Y) - Math.Min(right.X, right.Y);
//return (left.X + left.Y) - (right.X + right.Y); return (left.X + left.Y) - (right.X + right.Y);
} }
} }

View File

@ -28,7 +28,7 @@ using NUnit.Framework;
namespace Nuclex.Support.Packing { namespace Nuclex.Support.Packing {
/// <summary>Unit test for the arevalo rectangle packer class</summary> /// <summary>Unit test for the cygon rectangle packer class</summary>
[TestFixture] [TestFixture]
public class CygonRectanglePackerTest : RectanglePackerTest { public class CygonRectanglePackerTest : RectanglePackerTest {

View File

@ -24,20 +24,41 @@ using Microsoft.Xna.Framework;
namespace Nuclex.Support.Packing { namespace Nuclex.Support.Packing {
/// <summary>Simplified packer for rectangles which don't vary greatly in size</summary> /// <summary>Packer using a custom algorithm by Markus Ewald</summary>
/// <remarks>
/// This is a highly performant packer that sacrifices space efficiency for
/// low memory usage and runtime performance. It achieves good results with
/// near-uniform sized rectangles but will waste lots of space with rectangles
/// of varying dimensions.
/// </remarks>
public class CygonRectanglePacker : RectanglePacker { public class CygonRectanglePacker : RectanglePacker {
#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
/// <summary>Initializes a new rectangle packer</summary> /// <summary>Initializes a new rectangle packer</summary>
/// <param name="maxPackingAreaWidth">Maximum width of the packing area</param> /// <param name="maxPackingAreaWidth">Maximum width of the packing area</param>
/// <param name="maxPackingAreaHeight">Maximum height of the packing area</param> /// <param name="maxPackingAreaHeight">Maximum height of the packing area</param>
public CygonRectanglePacker(int maxPackingAreaWidth, int maxPackingAreaHeight) public CygonRectanglePacker(int maxPackingAreaWidth, int maxPackingAreaHeight)
: base(maxPackingAreaWidth, maxPackingAreaHeight) { } : base(maxPackingAreaWidth, maxPackingAreaHeight) {
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));
}
/// <summary>Tries to allocate space for a rectangle in the packing area</summary> /// <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="rectangleWidth">Width of the rectangle to allocate</param>
@ -47,45 +68,102 @@ namespace Nuclex.Support.Packing {
public override bool TryAllocate( public override bool TryAllocate(
int rectangleWidth, int rectangleHeight, out Point placement int rectangleWidth, int rectangleHeight, out Point placement
) { ) {
integrateRectangle(0, 1, 5);
integrateRectangle(20, 5, 30);
integrateRectangle(10, 10, 50);
integrateRectangle(10, 15, 25);
integrateRectangle(35, 20, 25);
integrateRectangle(40, 25, 15);
// If the rectangle is larger than the packing area in any dimension,
// it will never fit!
if(
(rectangleWidth > MaxPackingAreaWidth) || (rectangleHeight > MaxPackingAreaHeight)
) {
placement = Point.Zero; placement = Point.Zero;
return false; return false;
} }
// Do we have to start a new line ? /// <summary>Integrates a new rectangle into the height slice table</summary>
if(this.column + rectangleWidth > MaxPackingAreaWidth) { /// <param name="left">Position of the rectangle's left side</param>
this.currentLine += this.lineHeight; /// <param name="bottom">Position of the rectangle's lower side</param>
this.lineHeight = 0; /// <param name="width">Width of the rectangle</param>
this.column = 0; private void integrateRectangle(int left, int bottom, int width) {
// 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);
++startSlice;
} 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));
} }
// If it doesn't fit vertically now, the packing area is considered full // Special case, the rectangle was on the last slice, so we cannot
if(this.currentLine + rectangleHeight > MaxPackingAreaHeight) { // use the start slice + 1 as start index for the binary search
placement = Point.Zero; if(startSlice >= this.heightSlices.Count) {
return false; } else {
int right = left + width;
int endSlice = this.heightSlices.BinarySearch(
startSlice, this.heightSlices.Count - startSlice,
new Point(right, 0), SliceStartComparer.Default
);
} }
// The rectangle appears to fit at the current location //this.heightSlices.RemoveRange(startSlice, endSlice - startSlice);
placement = new Point(this.column, this.currentLine); /*
int nextSlice = firstSlice + 1;
bool isLastSlice = (nextSlice >= this.heightSlices.Count);
this.column += rectangleWidth; // Can be larger than cache width till next run */
if(rectangleHeight > this.lineHeight) /*
this.lineHeight = rectangleHeight;
return true;
int nextSlice = firstSlice + 1;
bool isLastSlice = (nextSlice >= this.heightSlices.Count);
//
bool endsInFirstSlice;
if(isLastSlice)
endsInFirstSlice = right < MaxPackingAreaWidth;
else
endsInFirstSlice = right < this.heightSlices[nextSlice].X;
if(endsInFirstSlice) {
this.heightSlices.Insert(
nextSlice, new Point(right, this.heightSlices[firstSlice].Y)
);
this.heightSlices[firstSlice] = new Point(left, bottom);
return;
} else { // Integrated rect continues beyond the discovered slice
this.heightSlices[firstSlice] = new Point(left, bottom);
} }
/// <summary>Current packing line</summary> } else {
private int currentLine;
/// <summary>Height of the current packing line</summary> firstSlice = ~firstSlice;
private int lineHeight;
/// <summary>Current column in the current packing line</summary> //firstSliceOriginalHeight = this.heightSlices[firstSlice].Y;
private int column;
this.heightSlices.Insert(firstSlice, new Point(left, bottom));
++firstSlice;
}
*/
}
/// <summary>Stores the height silhouette of the rectangles</summary>
private List<Point> heightSlices;
} }

View File

@ -0,0 +1,47 @@
#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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
#if UNITTEST
using NUnit.Framework;
namespace Nuclex.Support.Packing {
/// <summary>Unit test for the simple rectangle packer class</summary>
[TestFixture]
public class SimpleRectanglePackerTest : RectanglePackerTest {
/// <summary>Tests the packer's efficiency using a deterministic benchmark</summary>
[Test]
public void TestSpaceEfficiency() {
float efficiency = calculateEfficiency(new SimpleRectanglePacker(70, 70));
Assert.GreaterOrEqual(efficiency, 0.75, "Packer achieves 75% efficiency");
}
}
} // namespace Nuclex.Support.Packing
#endif // UNITTEST

View File

@ -0,0 +1,92 @@
#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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace Nuclex.Support.Packing {
/// <summary>Simplified packer for rectangles which don't vary greatly in size</summary>
/// <remarks>
/// This is a highly performant packer that sacrifices space efficiency for
/// low memory usage and runtime performance. It achieves good results with
/// near-uniform sized rectangles but will waste lots of space with rectangles
/// of varying dimensions.
/// </remarks>
public class SimpleRectanglePacker : RectanglePacker {
/// <summary>Initializes a new rectangle packer</summary>
/// <param name="maxPackingAreaWidth">Maximum width of the packing area</param>
/// <param name="maxPackingAreaHeight">Maximum height of the packing area</param>
public SimpleRectanglePacker(int maxPackingAreaWidth, int maxPackingAreaHeight)
: base(maxPackingAreaWidth, maxPackingAreaHeight) { }
/// <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>
public override bool TryAllocate(
int rectangleWidth, int rectangleHeight, out Point placement
) {
// If the rectangle is larger than the packing area in any dimension,
// it will never fit!
if(
(rectangleWidth > MaxPackingAreaWidth) || (rectangleHeight > MaxPackingAreaHeight)
) {
placement = Point.Zero;
return false;
}
// Do we have to start a new line ?
if(this.column + rectangleWidth > MaxPackingAreaWidth) {
this.currentLine += this.lineHeight;
this.lineHeight = 0;
this.column = 0;
}
// If it doesn't fit vertically now, the packing area is considered full
if(this.currentLine + rectangleHeight > MaxPackingAreaHeight) {
placement = Point.Zero;
return false;
}
// The rectangle appears to fit at the current location
placement = new Point(this.column, this.currentLine);
this.column += rectangleWidth; // Can be larger than cache width till next run
if(rectangleHeight > this.lineHeight)
this.lineHeight = rectangleHeight;
return true;
}
/// <summary>Current packing line</summary>
private int currentLine;
/// <summary>Height of the current packing line</summary>
private int lineHeight;
/// <summary>Current column in the current packing line</summary>
private int column;
}
} // namespace Nuclex.Support.Packing