Exemplo n.º 1
0
        private CustomRectangle FindPositionForNewNodeContactPoint(int width, int height, ref int bestContactScore)
        {
            CustomRectangle customRectangle = new CustomRectangle((object)null);

            bestContactScore = -1;
            for (int index = 0; index < this.freeRectangles.Count; ++index)
            {
                if (this.freeRectangles[index].Width >= width && this.freeRectangles[index].Height >= height)
                {
                    int num = this.ContactPointScoreNode(this.freeRectangles[index].X, this.freeRectangles[index].Y, width, height);
                    if (num > bestContactScore)
                    {
                        customRectangle.X      = this.freeRectangles[index].X;
                        customRectangle.Y      = this.freeRectangles[index].Y;
                        customRectangle.Width  = width;
                        customRectangle.Height = height;
                        bestContactScore       = num;
                    }
                }
                if (this.allowRotations && this.freeRectangles[index].Width >= height && this.freeRectangles[index].Height >= width)
                {
                    int num = this.ContactPointScoreNode(this.freeRectangles[index].X, this.freeRectangles[index].Y, height, width);
                    if (num > bestContactScore)
                    {
                        customRectangle.X      = this.freeRectangles[index].X;
                        customRectangle.Y      = this.freeRectangles[index].Y;
                        customRectangle.Width  = height;
                        customRectangle.Height = width;
                        bestContactScore       = num;
                    }
                }
            }
            return(customRectangle);
        }
Exemplo n.º 2
0
 public void Insert(List <CustomRectangle> rects, List <CustomRectangle> dst, MaxRectsBinPack.FreeRectChoiceHeuristic method)
 {
     dst.Clear();
     while (rects.Count > 0)
     {
         int             num1             = int.MaxValue;
         int             num2             = int.MaxValue;
         int             index1           = -1;
         CustomRectangle customRectangle1 = new CustomRectangle((object)null);
         for (int index2 = 0; index2 < rects.Count; ++index2)
         {
             int             score1           = 0;
             int             score2           = 0;
             CustomRectangle customRectangle2 = this.ScoreRect(rects[index2].Width, rects[index2].Height, method, ref score1, ref score2);
             if (score1 < num1 || score1 == num1 && score2 < num2)
             {
                 num1                      = score1;
                 num2                      = score2;
                 customRectangle1          = customRectangle2;
                 customRectangle1.UserData = rects[index2].UserData;
                 customRectangle1.Rotated  = customRectangle2.Rotated;
                 index1                    = index2;
             }
         }
         if (index1 == -1)
         {
             break;
         }
         this.PlaceRect(customRectangle1);
         this.CaculateEdge(customRectangle1);
         rects.RemoveAt(index1);
     }
 }
Exemplo n.º 3
0
 private void CaculateEdge(CustomRectangle latestnode)
 {
     if (latestnode.Right > this.RightEdge)
     {
         this.RightEdge = latestnode.Right;
     }
     if (latestnode.Bottom <= this.BottomEdge)
     {
         return;
     }
     this.BottomEdge = latestnode.Bottom;
 }
Exemplo n.º 4
0
        public CustomRectangle Insert(int width, int height, MaxRectsBinPack.FreeRectChoiceHeuristic method)
        {
            CustomRectangle usedNode = new CustomRectangle((object)null);
            int             num1     = 0;
            int             num2     = 0;

            switch (method)
            {
            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestShortSideFit:
                usedNode = this.FindPositionForNewNodeBestShortSideFit(width, height, ref num1, ref num2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestLongSideFit:
                usedNode = this.FindPositionForNewNodeBestLongSideFit(width, height, ref num2, ref num1);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestAreaFit:
                usedNode = this.FindPositionForNewNodeBestAreaFit(width, height, ref num1, ref num2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBottomLeftRule:
                usedNode = this.FindPositionForNewNodeBottomLeft(width, height, ref num1, ref num2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectContactPointRule:
                usedNode = this.FindPositionForNewNodeContactPoint(width, height, ref num1);
                break;
            }
            if (usedNode.Height == 0)
            {
                return(usedNode);
            }
            int count = this.freeRectangles.Count;

            for (int index = 0; index < count; ++index)
            {
                if (this.SplitFreeNode(this.freeRectangles[index], ref usedNode))
                {
                    this.freeRectangles.RemoveAt(index);
                    --index;
                    --count;
                }
            }
            this.PruneFreeList();
            this.usedRectangles.Add(usedNode);
            this.CaculateEdge(usedNode);
            return(usedNode);
        }
Exemplo n.º 5
0
        public void Init(int width, int height, bool rotations = true)
        {
            this.binWidth       = width;
            this.binHeight      = height;
            this.BottomEdge     = this.RightEdge = 0;
            this.allowRotations = rotations;
            CustomRectangle customRectangle = new CustomRectangle((object)null);

            customRectangle.X      = 0;
            customRectangle.Y      = 0;
            customRectangle.Width  = width;
            customRectangle.Height = height;
            this.usedRectangles.Clear();
            this.freeRectangles.Clear();
            this.freeRectangles.Add(customRectangle);
        }
Exemplo n.º 6
0
        private void PlaceRect(CustomRectangle node)
        {
            int count = this.freeRectangles.Count;

            for (int index = 0; index < count; ++index)
            {
                if (this.SplitFreeNode(this.freeRectangles[index], ref node))
                {
                    this.freeRectangles.RemoveAt(index);
                    --index;
                    --count;
                }
            }
            this.PruneFreeList();
            this.usedRectangles.Add(node);
        }
Exemplo n.º 7
0
        private CustomRectangle FindPositionForNewNodeBestLongSideFit(int width, int height, ref int bestShortSideFit, ref int bestLongSideFit)
        {
            CustomRectangle customRectangle = new CustomRectangle((object)null);

            bestLongSideFit = int.MaxValue;
            for (int index = 0; index < this.freeRectangles.Count; ++index)
            {
                if (this.freeRectangles[index].Width >= width && this.freeRectangles[index].Height >= height)
                {
                    int val1 = Math.Abs(this.freeRectangles[index].Width - width);
                    int val2 = Math.Abs(this.freeRectangles[index].Height - height);
                    int num1 = Math.Min(val1, val2);
                    int num2 = Math.Max(val1, val2);
                    if (num2 < bestLongSideFit || num2 == bestLongSideFit && num1 < bestShortSideFit)
                    {
                        customRectangle.X      = this.freeRectangles[index].X;
                        customRectangle.Y      = this.freeRectangles[index].Y;
                        customRectangle.Width  = width;
                        customRectangle.Height = height;
                        bestShortSideFit       = num1;
                        bestLongSideFit        = num2;
                    }
                }
                if (this.allowRotations && this.freeRectangles[index].Width >= height && this.freeRectangles[index].Height >= width)
                {
                    int val1 = Math.Abs(this.freeRectangles[index].Width - height);
                    int val2 = Math.Abs(this.freeRectangles[index].Height - width);
                    int num1 = Math.Min(val1, val2);
                    int num2 = Math.Max(val1, val2);
                    if (num2 < bestLongSideFit || num2 == bestLongSideFit && num1 < bestShortSideFit)
                    {
                        customRectangle.X      = this.freeRectangles[index].X;
                        customRectangle.Y      = this.freeRectangles[index].Y;
                        customRectangle.Width  = height;
                        customRectangle.Height = width;
                        bestShortSideFit       = num1;
                        bestLongSideFit        = num2;
                    }
                }
            }
            return(customRectangle);
        }
Exemplo n.º 8
0
 private bool SplitFreeNode(CustomRectangle freeNode, ref CustomRectangle usedNode)
 {
     if (usedNode.X >= freeNode.X + freeNode.Width || usedNode.X + usedNode.Width <= freeNode.X || usedNode.Y >= freeNode.Y + freeNode.Height || usedNode.Y + usedNode.Height <= freeNode.Y)
     {
         return(false);
     }
     if (usedNode.X < freeNode.X + freeNode.Width && usedNode.X + usedNode.Width > freeNode.X)
     {
         if (usedNode.Y > freeNode.Y && usedNode.Y < freeNode.Y + freeNode.Height)
         {
             CustomRectangle customRectangle = freeNode.Copy();
             customRectangle.Height = usedNode.Y - customRectangle.Y;
             this.freeRectangles.Add(customRectangle);
         }
         if (usedNode.Y + usedNode.Height < freeNode.Y + freeNode.Height)
         {
             CustomRectangle customRectangle = freeNode.Copy();
             customRectangle.Y      = usedNode.Y + usedNode.Height;
             customRectangle.Height = freeNode.Y + freeNode.Height - (usedNode.Y + usedNode.Height);
             this.freeRectangles.Add(customRectangle);
         }
     }
     if (usedNode.Y < freeNode.Y + freeNode.Height && usedNode.Y + usedNode.Height > freeNode.Y)
     {
         if (usedNode.X > freeNode.X && usedNode.X < freeNode.X + freeNode.Width)
         {
             CustomRectangle customRectangle = freeNode.Copy();
             customRectangle.Width = usedNode.X - customRectangle.X;
             this.freeRectangles.Add(customRectangle);
         }
         if (usedNode.X + usedNode.Width < freeNode.X + freeNode.Width)
         {
             CustomRectangle customRectangle = freeNode.Copy();
             customRectangle.X     = usedNode.X + usedNode.Width;
             customRectangle.Width = freeNode.X + freeNode.Width - (usedNode.X + usedNode.Width);
             this.freeRectangles.Add(customRectangle);
         }
     }
     return(true);
 }
Exemplo n.º 9
0
        private CustomRectangle FindPositionForNewNodeBottomLeft(int width, int height, ref int bestY, ref int bestX)
        {
            CustomRectangle customRectangle = new CustomRectangle((object)null);

            bestY = int.MaxValue;
            for (int index = 0; index < this.freeRectangles.Count; ++index)
            {
                if (this.freeRectangles[index].Width >= width && this.freeRectangles[index].Height >= height)
                {
                    int num = this.freeRectangles[index].Y + height;
                    if (num < bestY || num == bestY && this.freeRectangles[index].X < bestX)
                    {
                        customRectangle.X      = this.freeRectangles[index].X;
                        customRectangle.Y      = this.freeRectangles[index].Y;
                        customRectangle.Width  = width;
                        customRectangle.Height = height;
                        bestY = num;
                        bestX = this.freeRectangles[index].X;
                    }
                }
                if (this.allowRotations && this.freeRectangles[index].Width >= height && this.freeRectangles[index].Height >= width)
                {
                    int num = this.freeRectangles[index].Y + width;
                    if (num < bestY || num == bestY && this.freeRectangles[index].X < bestX)
                    {
                        customRectangle.X      = this.freeRectangles[index].X;
                        customRectangle.Y      = this.freeRectangles[index].Y;
                        customRectangle.Width  = height;
                        customRectangle.Height = width;
                        bestY = num;
                        bestX = this.freeRectangles[index].X;
                    }
                }
            }
            return(customRectangle);
        }
Exemplo n.º 10
0
        private CustomRectangle ScoreRect(int width, int height, MaxRectsBinPack.FreeRectChoiceHeuristic method, ref int score1, ref int score2)
        {
            CustomRectangle customRectangle = new CustomRectangle((object)null);

            score1 = int.MaxValue;
            score2 = int.MaxValue;
            switch (method)
            {
            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestShortSideFit:
                customRectangle = this.FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestLongSideFit:
                customRectangle = this.FindPositionForNewNodeBestLongSideFit(width, height, ref score2, ref score1);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestAreaFit:
                customRectangle = this.FindPositionForNewNodeBestAreaFit(width, height, ref score1, ref score2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBottomLeftRule:
                customRectangle = this.FindPositionForNewNodeBottomLeft(width, height, ref score1, ref score2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectContactPointRule:
                customRectangle = this.FindPositionForNewNodeContactPoint(width, height, ref score1);
                score1          = -score1;
                break;
            }
            if (customRectangle.Height == 0)
            {
                score1 = int.MaxValue;
                score2 = int.MaxValue;
            }
            return(customRectangle);
        }
Exemplo n.º 11
0
 public bool Contains(CustomRectangle other)
 {
     return(this.inner.Contains(other.inner));
 }
Exemplo n.º 12
0
 private bool IsContainedIn(CustomRectangle a, CustomRectangle b)
 {
     return(a.X >= b.X && a.Y >= b.Y && a.X + a.Width <= b.X + b.Width && a.Y + a.Height <= b.Y + b.Height);
 }