Exemple #1
0
        public void Set(double l, double t, double r, double b)
        {
            var rect = new RectLtrb
            {
                Left    = l,
                Top     = t,
                FRight  = r,
                FBottom = b
            };

            if (PNode == null)
            {
                PNode = new RectNode
                {
                    Rectangle = rect
                }
            }
            ;
            else
            {
                PNode.Rectangle = rect;
                PNode.Id        = false;
            }
            Fill = true;
        }
Exemple #2
0
        public static Vector2D PackBin(RectXywhFlipped[] vlist, double w, double h, double discardbelow, List <RectXywhFlipped> succ, List <RectXywhFlipped> unsucc, bool allowflip)
        {
            RectNode root    = new RectNode();
            var      funcs   = CompFuncs();
            var      ordered = new List <List <RectXywhFlipped> >();

            for (int f = 0; f < funcs.Count; f++)
            {
                var ii    = f;
                var clist = ((RectXywhFlipped[])vlist.Clone()).ToList();
                ordered.Add(clist);
                clist.Sort((a, b) => funcs[ii](a, b));
                clist.Reverse();
            }

            var minbin = new RectXywh
            {
                Left    = 0,
                Top     = 0,
                FWidth  = w,
                FHeight = h
            };

            var  minfunc  = -1;
            var  bestfunc = 0;
            var  bestarea = 0.0;
            var  area     = 0.0;
            var  step     = 0.0;
            int  fit      = 0;
            bool fail     = false;

            for (int f = 0; f < funcs.Count; f++)
            {
                var v = ordered[f];
                step = minbin.Width / 2;
                root.Reset(minbin);
                while (true)
                {
                    if (root.Rectangle.Width > minbin.Width)
                    {
                        if (minfunc > -1)
                        {
                            break;
                        }
                        area = 0;
                        root.Reset(minbin);
                        for (int i = 0; i < vlist.Length; i++)
                        {
                            if (root.Insert(v[i], allowflip) != null)
                            {
                                area += v[i].Area();
                            }
                        }
                        fail = true;
                        break;
                    }
                    fit = -1;
                    for (int i = 0; i < vlist.Length; i++)
                    {
                        if (root.Insert(v[i], allowflip) == null)
                        {
                            fit = 1;
                            break;
                        }
                    }
                    if (fit == -1 && step <= discardbelow)
                    {
                        break;
                    }

                    root.Reset(new RectXywh
                    {
                        FWidth  = root.Rectangle.Width + fit * step,
                        FHeight = root.Rectangle.Height + fit * step,
                        Top     = 0,
                        Left    = 0
                    });
                    step /= 2;
                    step  = Math.Max(step, 0.0001);
                }
                if (!fail && (minbin.Area() >= root.Rectangle.Area()))
                {
                    minbin = new RectXywh
                    {
                        FWidth  = root.Rectangle.Width,
                        FHeight = root.Rectangle.Height,
                        Top     = 0, Left = 0
                    };
                    minfunc = f;
                }
                else if (fail && (area > bestarea))
                {
                    bestarea = area;
                    bestfunc = f;
                }
                fail = false;
            }

            var sv = ordered[minfunc == -1 ? bestfunc : minfunc];

            double   clipx = 0.0, clipy = 0.0;
            RectNode ret;

            root.Reset(minbin);

            for (int i = 0; i < vlist.Length; i++)
            {
                ret = root.Insert(sv[i], allowflip);
                if (ret != null)
                {
                    sv[i].Left = ret.Rectangle.Left;
                    sv[i].Top  = ret.Rectangle.Top;

                    if (sv[i].Flipped)
                    {
                        sv[i].Flipped = false;
                        sv[i].Flip();
                    }

                    clipx = Math.Max(clipx, ret.Rectangle.Right);
                    clipy = Math.Max(clipy, ret.Rectangle.Bottom);
                    succ.Add(sv[i]);
                }
                else
                {
                    unsucc.Add(sv[i]);
                    sv[i].Flipped = false;
                }
            }
            return(new Vector2D(clipx, clipy));
        }