1. -- 
  2. -- Copyright (c) 2012 Kevin Wellwood 
  3. -- All rights reserved. 
  4. -- 
  5. -- This source code is distributed under the Modified BSD License. For terms and 
  6. -- conditions, see license.txt. 
  7. -- 
  8.  
  9. private with Ada.Containers.Vectors; 
  10.  
  11. private package Tiles.Atlases.Bins is 
  12.  
  13.     -- A Rect represents the size and location of a rectangle, packed in a Bin. 
  14.     type Rect is 
  15.         record 
  16.             x, y   : Integer := 0; 
  17.             width, 
  18.             height : Integer := 0; 
  19.         end record; 
  20.  
  21.     -- A Bin represents a rectangular 2D space that can be filled with smaller 
  22.     -- 2D rectangles. Each rectangle added to the bin via Insert() receives a 
  23.     -- location in the bin where it resides, according to the bin packing 
  24.     -- algorithm. This class uses the MAXRECTS bin packing algorithm with the 
  25.     -- BSSF (Best Short Side Fit) heuristic. 
  26.     type Bin is tagged limited private; 
  27.     type A_Bin is access all Bin'Class; 
  28.  
  29.     -- Creates a new bin of the specified size. 
  30.     function Create_Bin( width, height : Positive ) return A_Bin; 
  31.  
  32.     -- Inserts a single rectangle into the bin. 
  33.     function Insert( this          : not null access Bin'Class; 
  34.                      width, height : Natural ) return Rect; 
  35.  
  36.     -- Deletes the Bin. 
  37.     procedure Delete( this : in out A_Bin ); 
  38.  
  39. private 
  40.  
  41.     package Rect_Vectors is new Ada.Containers.Vectors( Positive, Rect, "=" ); 
  42.  
  43.     type Bin is tagged limited 
  44.         record 
  45.             width, 
  46.             height    : Integer; 
  47.             freeRects : Rect_Vectors.Vector; 
  48.         end record; 
  49.  
  50.     -- Determines the position for a new rectangle using the BSSF (Best Short 
  51.     -- Side Fit) heuristic. 
  52.     function Find_Position_BSSF( this          : not null access Bin'Class; 
  53.                                  width, height : Natural ) return Rect; 
  54.  
  55.     -- Removes redundant rectangles from the free list. 
  56.     procedure Prune_Free_List( this : not null access Bin'Class ); 
  57.  
  58.     -- Splits 'freeNode' into smaller free rectangles, adding them to the free 
  59.     -- list, if 'usedNode' overlaps. Returns True if the node was split or 
  60.     -- False if the rectangles don't overlap. 
  61.     function Split_Free_Node( this     : not null access Bin'Class; 
  62.                               freeNode, 
  63.                               usedNode : Rect ) return Boolean; 
  64.  
  65. end Tiles.Atlases.Bins;