예제 #1
0
        Rect ScoreRect(int width, int height, dfTexturePackingMethod method, ref int score1, ref int score2)
        {
            Rect newNode = new Rect();

            score1 = int.MaxValue;
            score2 = int.MaxValue;
            switch (method)
            {
            case dfTexturePackingMethod.RectBestShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2); break;

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

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

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

            case dfTexturePackingMethod.RectBestAreaFit: 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);
        }
예제 #2
0
        public void Insert(List <Rect> rects, List <Rect> dst, dfTexturePackingMethod method)
        {
            dst.Clear();

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

                for (int i = 0; i < rects.Count; ++i)
                {
                    int  score1  = 0;
                    int  score2  = 0;
                    Rect newNode = ScoreRect((int)rects[i].width, (int)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);
            }
        }
예제 #3
0
        public Rect Insert(int width, int height, dfTexturePackingMethod 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 dfTexturePackingMethod.RectBestShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2); break;

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

            case dfTexturePackingMethod.RectContactPointRule: newNode = FindPositionForNewNodeContactPoint(width, height, ref score1); break;

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

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

            PruneFreeList();

            usedRectangles.Add(newNode);
            return(newNode);
        }
예제 #4
0
		Rect ScoreRect( int width, int height, dfTexturePackingMethod method, ref int score1, ref int score2 )
		{
			Rect newNode = new Rect();
			score1 = int.MaxValue;
			score2 = int.MaxValue;
			switch( method )
			{
				case dfTexturePackingMethod.RectBestShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit( width, height, ref score1, ref score2 ); break;
				case dfTexturePackingMethod.RectBottomLeftRule: newNode = FindPositionForNewNodeBottomLeft( width, height, ref score1, ref score2 ); break;
				case dfTexturePackingMethod.RectContactPointRule: newNode = FindPositionForNewNodeContactPoint( width, height, ref score1 );
					score1 = -score1; // Reverse since we are minimizing, but for contact point score bigger is better.
					break;
				case dfTexturePackingMethod.RectBestLongSideFit: newNode = FindPositionForNewNodeBestLongSideFit( width, height, ref score2, ref score1 ); break;
				case dfTexturePackingMethod.RectBestAreaFit: 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;
		}
예제 #5
0
		public void Insert( List<Rect> rects, List<Rect> dst, dfTexturePackingMethod method )
		{
			dst.Clear();

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

				for( int i = 0; i < rects.Count; ++i )
				{
					int score1 = 0;
					int score2 = 0;
					Rect newNode = ScoreRect( (int)rects[ i ].width, (int)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 );
			}
		}
예제 #6
0
		public Rect Insert( int width, int height, dfTexturePackingMethod 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 dfTexturePackingMethod.RectBestShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit( width, height, ref score1, ref score2 ); break;
				case dfTexturePackingMethod.RectBottomLeftRule: newNode = FindPositionForNewNodeBottomLeft( width, height, ref score1, ref score2 ); break;
				case dfTexturePackingMethod.RectContactPointRule: newNode = FindPositionForNewNodeContactPoint( width, height, ref score1 ); break;
				case dfTexturePackingMethod.RectBestLongSideFit: newNode = FindPositionForNewNodeBestLongSideFit( width, height, ref score2, ref score1 ); break;
				case dfTexturePackingMethod.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 ], ref newNode ) )
				{
					freeRectangles.RemoveAt( i );
					--i;
					--numRectanglesToProcess;
				}
			}

			PruneFreeList();

			usedRectangles.Add( newNode );
			return newNode;
		}