#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 rectangle packers can be found here:
/// http://www.csc.liv.ac.uk/~epa/surveyhtml.html
///
///
public abstract class RectanglePacker {
/// Initializes a new rectangle packer
/// Maximum width of the packing area
/// Maximum height of the packing area
protected RectanglePacker(int maxPackingAreaWidth, int maxPackingAreaHeight) {
this.maxPackingAreaWidth = maxPackingAreaWidth;
this.maxPackingAreaHeight = maxPackingAreaHeight;
}
/// 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 Allocate(int rectangleWidth, int rectangleHeight) {
Point point;
if(!TryAllocate(rectangleWidth, rectangleHeight, out point))
throw new Exception("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 TryAllocate(
int rectangleWidth, int rectangleHeight, out Point placement
);
/// Maximum width the packing area is allowed to have
protected int MaxPackingAreaWidth {
get { return this.maxPackingAreaWidth; }
}
/// Maximum height the packing area is allowed to have
protected int MaxPackingAreaHeight {
get { return this.maxPackingAreaHeight; }
}
/// Maximum allowed width of the packing area
private int maxPackingAreaWidth;
/// Maximum allowed height of the packing area
private int maxPackingAreaHeight;
}
} // namespace Nuclex.Support.Packing