Example #1
0
    public void Insert(List <PRRect> rects, List <PRRect> dst, ChoiceHeuristic method)
    {
        dst.Clear();

        while (rects.Count > 0)
        {
            int    bestScore1    = int.MaxValue;
            int    bestScore2    = int.MaxValue;
            int    bestRectIndex = -1;
            PRRect bestNode      = new PRRect();

            for (int i = 0; i < rects.Count; ++i)
            {
                int    score1  = 0;
                int    score2  = 0;
                PRRect newNode = ScoreRect(rects[i].width, rects[i].height, method, ref score1, ref score2);

                if (score1 < bestScore1 || (score1 == bestScore1 && score2 < bestScore2))
                {
                    bestScore1    = score1;
                    bestScore2    = score2;
                    bestNode      = newNode;
                    bestRectIndex = i;
                }
            }

            if (bestRectIndex == -1)
            {
                return;
            }

            PlaceRect(bestNode);
            rects.RemoveAt(bestRectIndex);
        }
    }
Example #2
0
    PRRect ScoreRect(int width, int height, ChoiceHeuristic method, ref int score1, ref int score2)
    {
        PRRect newNode = new PRRect();

        score1 = int.MaxValue;
        score2 = int.MaxValue;

        switch (method)
        {
        case ChoiceHeuristic.ShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2); break;

        case ChoiceHeuristic.BottomLeftRule: newNode = FindPositionForNewNodeBottomLeft(width, height, ref score1, ref score2); break;

        case ChoiceHeuristic.ContactPointRule: newNode = FindPositionForNewNodeContactPoint(width, height, ref score1);
            score1 = -score1;             // Reverse since we are minimizing, but for contact point score bigger is better.
            break;

        case ChoiceHeuristic.LongSideFit: newNode = FindPositionForNewNodeBestLongSideFit(width, height, ref score2, ref score1); break;

        case ChoiceHeuristic.AreaFit: newNode = FindPositionForNewNodeBestAreaFit(width, height, ref score1, ref score2); break;
        }

        // Cannot fit the current rectangle.
        if (newNode.height == 0)
        {
            score1 = int.MaxValue;
            score2 = int.MaxValue;
        }

        return(newNode);
    }
Example #3
0
	public PRRect Insert(int width, int height, ChoiceHeuristic method) 
	{
		PRRect newNode = new PRRect();
		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 ChoiceHeuristic.ShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2); break;
			case ChoiceHeuristic.BottomLeftRule: newNode = FindPositionForNewNodeBottomLeft(width, height, ref score1, ref score2); break;
			case ChoiceHeuristic.ContactPointRule: newNode = FindPositionForNewNodeContactPoint(width, height, ref score1); break;
			case ChoiceHeuristic.LongSideFit: newNode = FindPositionForNewNodeBestLongSideFit(width, height, ref score2, ref score1); break;
			case ChoiceHeuristic.AreaFit: 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], ref newNode)) 
			{
				freeRectangles.RemoveAt(i);
				--i;
				--numRectanglesToProcess;
			}
		}
		
		PruneFreeList();
		
		usedRectangles.Add(newNode);
		return newNode;
	}
Example #4
0
        public bool Insert(int width, int height, ChoiceHeuristic method, out Rect rect)
        {
            var newNode = new Rect(0, 0, 0, 0);
            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 ChoiceHeuristic.BestShortSideFit:
                newNode = FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2);
                break;

            case ChoiceHeuristic.BottomLeftRule:
                newNode = FindPositionForNewNodeBottomLeft(width, height, ref score1, ref score2);
                break;

            case ChoiceHeuristic.ContactPointRule:
                newNode = FindPositionForNewNodeContactPoint(width, height, ref score1);
                break;

            case ChoiceHeuristic.BestLongSideFit:
                newNode = FindPositionForNewNodeBestLongSideFit(width, height, ref score2, ref score1);
                break;

            case ChoiceHeuristic.BestAreaFit:
                newNode = FindPositionForNewNodeBestAreaFit(width, height, ref score1, ref score2);
                break;
            }

            if (newNode.H == 0)
            {
                rect = Rect.Zero;
                return(false);
            }

            int numRectanglesToProcess = _freeRects.Count;

            for (int i = 0; i < numRectanglesToProcess; ++i)
            {
                if (SplitFreeNode(ref _freeRects.GetReferenceAt(i), ref newNode))
                {
                    _freeRects.RemoveAt(i);
                    --i;
                    --numRectanglesToProcess;
                }
            }

            PruneFreeList();
            _usedRects.Add(newNode);
            rect = newNode;
            return(true);
        }
        public AtlasPackerState(int maxSize, ImageSpacing spacing, bool isSingle)
        {
            Width  = STEP_SIZE;
            Height = STEP_SIZE;

            MaxSize   = maxSize;
            Spacing   = spacing;
            IsSingle  = isSingle;
            Items     = new List <Item>();
            Heuristic = ChoiceHeuristic.BestAreaFit;

            if (!IsSingle)
            {
                Packer = new RectPacker(Width, Height, false);
            }
        }
Example #6
0
    public PRRect Insert(int width, int height, ChoiceHeuristic method)
    {
        PRRect newNode = new PRRect();
        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 ChoiceHeuristic.ShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2); break;

        case ChoiceHeuristic.BottomLeftRule: newNode = FindPositionForNewNodeBottomLeft(width, height, ref score1, ref score2); break;

        case ChoiceHeuristic.ContactPointRule: newNode = FindPositionForNewNodeContactPoint(width, height, ref score1); break;

        case ChoiceHeuristic.LongSideFit: newNode = FindPositionForNewNodeBestLongSideFit(width, height, ref score2, ref score1); break;

        case ChoiceHeuristic.AreaFit: 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], ref newNode))
            {
                freeRectangles.RemoveAt(i);
                --i;
                --numRectanglesToProcess;
            }
        }

        PruneFreeList();

        usedRectangles.Add(newNode);
        return(newNode);
    }
Example #7
0
	public void Insert(List<PRRect> rects, List<PRRect> dst, ChoiceHeuristic method) 
	{
		dst.Clear();
		
		while(rects.Count > 0)
		{
			int bestScore1 = int.MaxValue;
			int bestScore2 = int.MaxValue;
			int bestRectIndex = -1;
			PRRect bestNode = new PRRect();
			
			for(int i = 0; i < rects.Count; ++i) 
			{
				int score1 = 0;
				int score2 = 0;
				PRRect newNode = ScoreRect(rects[i].width, rects[i].height, method, ref score1, ref score2);
				
				if (score1 < bestScore1 || (score1 == bestScore1 && score2 < bestScore2)) 
				{
					bestScore1 = score1;
					bestScore2 = score2;
					bestNode = newNode;
					bestRectIndex = i;
				}
			}
			
			if (bestRectIndex == -1)
				return;
			
			PlaceRect(bestNode);
			rects.RemoveAt(bestRectIndex);
		}
	}
Example #8
0
	PRRect ScoreRect(int width, int height, ChoiceHeuristic method, ref int score1, ref int score2) 
	{
		PRRect newNode = new PRRect();
		score1 = int.MaxValue;
		score2 = int.MaxValue;

		switch(method) 
		{
			case ChoiceHeuristic.ShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2); break;
			case ChoiceHeuristic.BottomLeftRule: newNode = FindPositionForNewNodeBottomLeft(width, height, ref score1, ref score2); break;
			case ChoiceHeuristic.ContactPointRule: newNode = FindPositionForNewNodeContactPoint(width, height, ref score1); 
			score1 = -score1; // Reverse since we are minimizing, but for contact point score bigger is better.
			break;
			case ChoiceHeuristic.LongSideFit: newNode = FindPositionForNewNodeBestLongSideFit(width, height, ref score2, ref score1); break;
			case ChoiceHeuristic.AreaFit: newNode = FindPositionForNewNodeBestAreaFit(width, height, ref score1, ref score2); break;
		}
		
		// Cannot fit the current rectangle.
		if (newNode.height == 0) 
		{
			score1 = int.MaxValue;
			score2 = int.MaxValue;
		}
		
		return newNode;
	}