tk2dEditor.Atlas.MaxRectsBinPack.Insert C# (CSharp) Method

Insert() public method

public Insert ( int width, int height, FreeRectChoiceHeuristic method ) : Rect
width int
height int
method FreeRectChoiceHeuristic
return Rect
        public Rect Insert(int width, int height, FreeRectChoiceHeuristic method)
        {
            Rect newNode = new Rect();
            int score1 = 0; // Unused in this function. We don't need to know the score after finding the position.
            int score2 = 0;
            switch (method)
            {
                case FreeRectChoiceHeuristic.RectBestShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2); break;
                case FreeRectChoiceHeuristic.RectBottomLeftRule: newNode = FindPositionForNewNodeBottomLeft(width, height, ref score1, ref score2); break;
                case FreeRectChoiceHeuristic.RectContactPointRule: newNode = FindPositionForNewNodeContactPoint(width, height, ref score1); break;
                case FreeRectChoiceHeuristic.RectBestLongSideFit: newNode = FindPositionForNewNodeBestLongSideFit(width, height, ref score2, ref score1); break;
                case FreeRectChoiceHeuristic.RectBestAreaFit: newNode = FindPositionForNewNodeBestAreaFit(width, height, ref score1, ref score2); break;
            }

            if (newNode.height == 0)
                return newNode;

            int numRectanglesToProcess = freeRectangles.Count;
            for (int i = 0; i < numRectanglesToProcess; ++i)
            {
                if (SplitFreeNode(freeRectangles[i], newNode))
                {
                    freeRectangles.RemoveAt(i);
                    --i;
                    --numRectanglesToProcess;
                }
            }

            PruneFreeList();

            usedRectangles.Add(newNode);
            return newNode;
        }

Same methods

MaxRectsBinPack::Insert ( List rects, FreeRectChoiceHeuristic method ) : bool

Usage Example

Example #1
0
        MaxRectsBinPack FindBestBinPacker(int width, int height, ref List <RectSize> currRects, ref bool allUsed)
        {
            List <MaxRectsBinPack>  binPackers       = new List <MaxRectsBinPack>();
            List <List <RectSize> > binPackerRects   = new List <List <RectSize> >();
            List <bool>             binPackerAllUsed = new List <bool>();

            //MaxRectsBinPack.FreeRectChoiceHeuristic[] heuristics = { MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestAreaFit,
            //                                                         MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestLongSideFit,
            //                                                         MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestShortSideFit,
            //                                                         MaxRectsBinPack.FreeRectChoiceHeuristic.RectBottomLeftRule,
            //                                                         MaxRectsBinPack.FreeRectChoiceHeuristic.RectContactPointRule };

            MaxRectsBinPack.FreeRectChoiceHeuristic[] heuristics = { MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestAreaFit,
                                                                     MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestLongSideFit,
                                                                     MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestShortSideFit,
                                                                     MaxRectsBinPack.FreeRectChoiceHeuristic.RectBottomLeftRule, };

            foreach (var heuristic in heuristics)
            {
                MaxRectsBinPack binPacker     = new MaxRectsBinPack(width, height, allowRotation);
                List <RectSize> activeRects   = new List <RectSize>(currRects);
                bool            activeAllUsed = binPacker.Insert(activeRects, heuristic);

                binPackers.Add(binPacker);
                binPackerRects.Add(activeRects);
                binPackerAllUsed.Add(activeAllUsed);
            }

            int leastWastedPixels = Int32.MaxValue;
            int leastWastedIndex  = -1;

            for (int i = 0; i < binPackers.Count; ++i)
            {
                int wastedPixels = binPackers[i].WastedBinArea();
                if (wastedPixels < leastWastedPixels)
                {
                    leastWastedPixels = wastedPixels;
                    leastWastedIndex  = i;
                    oversizeTextures  = true;
                }
            }

            currRects = binPackerRects[leastWastedIndex];
            allUsed   = binPackerAllUsed[leastWastedIndex];
            return(binPackers[leastWastedIndex]);
        }
All Usage Examples Of tk2dEditor.Atlas.MaxRectsBinPack::Insert