#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 {
/// Base class for rectangle packing algorithms
///
///
/// By uniting all rectangle packers under this common base class, you can
/// easily switch between different algorithms to find the most efficient or
/// performant one for a given job.
///
///
/// An almost exhaustive list of packing algorithms can be found here:
/// http://www.csc.liv.ac.uk/~epa/surveyhtml.html
///
///
public abstract class RectanglePacker {
/// Initializes a new rectangle packer
/// Width of the packing area
/// Height of the packing area
protected RectanglePacker(int packingAreaWidth, int packingAreaHeight) {
this.packingAreaWidth = packingAreaWidth;
this.packingAreaHeight = packingAreaHeight;
}
/// Allocates space for a rectangle in the packing area
/// Width of the rectangle to allocate
/// Height of the rectangle to allocate
/// The location at which the rectangle has been placed
public virtual Point Pack(int rectangleWidth, int rectangleHeight) {
Point point;
if(!TryPack(rectangleWidth, rectangleHeight, out point))
throw new OutOfSpaceException("Rectangle does not fit in packing area");
return point;
}
/// Tries to allocate space for a rectangle in the packing area
/// Width of the rectangle to allocate
/// Height of the rectangle to allocate
/// Output parameter receiving the rectangle's placement
/// True if space for the rectangle could be allocated
public abstract bool TryPack(
int rectangleWidth, int rectangleHeight, out Point placement
);
/// Maximum width the packing area is allowed to have
protected int PackingAreaWidth {
get { return this.packingAreaWidth; }
}
/// Maximum height the packing area is allowed to have
protected int PackingAreaHeight {
get { return this.packingAreaHeight; }
}
/// Maximum allowed width of the packing area
private int packingAreaWidth;
/// Maximum allowed height of the packing area
private int packingAreaHeight;
}
} // namespace Nuclex.Support.Packing