/*
         * This function try to rearranges all objects on the build platform so they
         * do not touch each other
         */
        public void RearrangeObjects()
        {
            BinPacker bp = new BinPacker();
            List<BinPacker.BinRect> rects = new List<BinPacker.BinRect>();
            foreach (Object3d obj in m_objects)
            {
                if (obj.tag != Object3d.OBJ_NORMAL)
                    continue;
                float w = obj.m_max.x - obj.m_min.x;
                float h = obj.m_max.y - obj.m_min.y;
                BinPacker.BinRect rc = new BinPacker.BinRect(w, h, obj);
                rects.Add(rc);
            }

            float pw = (float)UVDLPApp.Instance().m_printerinfo.m_PlatXSize;
            float ph = (float)UVDLPApp.Instance().m_printerinfo.m_PlatYSize;
            bp.Pack(rects, (int)pw, (int)ph, true);

            // find pack size
            int maxw = 0;
            int maxh = 0;
            foreach (BinPacker.BinRect rc in rects)
            {
                if (rc.packed && ((rc.x + rc.w) > maxw))
                    maxw = rc.x + rc.w;
                if (rc.packed && ((rc.y + rc.h) > maxh))
                    maxh = rc.y + rc.h;
            }

            // find offsets to center all objects
            float offsx = -(float)maxw / 2f + 0.5f;
            float offsy = -(float)maxh / 2f + 0.5f;

            // move all objects to new positions
            foreach (BinPacker.BinRect rc in rects)
            {
                Object3d obj = (Object3d)rc.obj;
                if (rc.packed)
                {
                    if (rc.rotated)
                        obj.Rotate(0, 0, 1.570796326f);
                    float dx = rc.x - obj.m_min.x + offsx;
                    float dy = rc.y - obj.m_min.y + offsy;
                    obj.Translate(dx, dy, 0, true);
                }
                else
                {
                    // object could not fit, place outside platform
                    obj.Translate(pw / 2f - obj.m_min.x, 0, 0, true);
                }
            }
            UVDLPApp.Instance().RaiseAppEvent(eAppEvent.eReDraw,"Redraw new arrangement");
        }
Example #2
0
        public void GenereteUserTexture()
        {
            if (mUserTexture >= 0)
                DeleteTexture(mUserTexture);

            // step 1, arrange all textures in a one big bitmap;
            Bitmap bmp = new Bitmap(2048, 2048, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics gr = Graphics.FromImage(bmp);
            gr.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
            //gr.ResetTransform();
            //gr.PageUnit = GraphicsUnit.Pixel;
            using (Brush br = new SolidBrush(Color.FromArgb(0, 0, 0, 0)))
            {
                gr.FillRectangle(br, new Rectangle(0,0,bmp.Width,bmp.Height));
            }
            float tw = bmp.Width;
            float th = bmp.Height;

            BinPacker bp = new BinPacker();
            List<BinPacker.BinRect> rects = new List<BinPacker.BinRect>();
            foreach (C2DImage img in mNewImages)
            {
                BinPacker.BinRect rc = new BinPacker.BinRect(img.w, img.h, img);
                rects.Add(rc);
            }

            bp.Pack(rects, bmp.Width, bmp.Height, false);

            List<C2DImage> packedImages = new List<C2DImage>();
            foreach (BinPacker.BinRect rc in rects)
            {
                C2DImage img = (C2DImage)rc.obj;
                if (rc.packed)
                {
                    img.x = rc.x;
                    img.y = rc.y;
                    img.x1 = (img.x + 0.5f) / tw;
                    img.y1 = (img.y + 0.5f) / th;
                    img.x2 = img.x1 + (float)(img.w - 1) / tw;
                    img.y2 = img.y1 + (float)(img.h - 1) / th;
                    img.scalex = tw;
                    img.scaley = th;
                    gr.DrawImage(img.bmp, img.x, img.y, img.bmp.Width, img.bmp.Height);
                    //img.bmp.Save("origbmp.png", System.Drawing.Imaging.ImageFormat.Png);
                    packedImages.Add(img);
                    mNewImages.Remove(img);
                }
            }
            if (mNewImages.Count != 0)
            {
                DebugLogger.Instance().LogError("Not all user images could be loaded");
            }
            //bmp.Save("packedbmp.png", System.Drawing.Imaging.ImageFormat.Png);
            mUserTexture = LoadTextureImage(bmp);
            foreach (C2DImage img in packedImages)
            {
                img.tex = mUserTexture;
                ImgDbase[img.name] = img;
            }
            needPacking = false;
        }
Example #3
0
        public void GenereteUserTexture()
        {
            if (mUserTexture >= 0)
            {
                DeleteTexture(mUserTexture);
            }

            // step 1, arrange all textures in a one big bitmap;
            Bitmap bmp = new Bitmap(2048, 2048, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            float  tw  = bmp.Width;
            float  th  = bmp.Height;

            BinPacker bp = new BinPacker();
            List <BinPacker.BinRect> rects = new List <BinPacker.BinRect>();

            foreach (C2DImage img in mNewImages)
            {
                BinPacker.BinRect rc = new BinPacker.BinRect(img.w, img.h, img);
                rects.Add(rc);
            }

            bp.Pack(rects, bmp.Width, bmp.Height, false);

            List <C2DImage> packedImages = new List <C2DImage>();

            foreach (BinPacker.BinRect rc in rects)
            {
                C2DImage img = (C2DImage)rc.obj;
                if (rc.packed)
                {
                    img.x      = rc.x;
                    img.y      = rc.y;
                    img.x1     = (img.x + 0.5f) / tw;
                    img.y1     = (img.y + 0.5f) / th;
                    img.x2     = img.x1 + (float)(img.w - 1) / tw;
                    img.y2     = img.y1 + (float)(img.h - 1) / th;
                    img.scalex = tw;
                    img.scaley = th;
                    for (int i = 0; i < img.bmp.Height; i++)
                    {
                        System.Drawing.Imaging.BitmapData ddata = bmp.LockBits(new Rectangle((int)img.x, (int)img.y + i, img.bmp.Width, 1),
                                                                               System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        System.Drawing.Imaging.BitmapData sdata = img.bmp.LockBits(new Rectangle(0, i, img.bmp.Width, 1),
                                                                                   System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        int    numBytes  = 4 * img.bmp.Width;
                        byte[] rgbValues = new byte[numBytes];
                        Marshal.Copy(sdata.Scan0, rgbValues, 0, numBytes);
                        Marshal.Copy(rgbValues, 0, ddata.Scan0, numBytes);
                        bmp.UnlockBits(ddata);
                        img.bmp.UnlockBits(sdata);
                    }
                    packedImages.Add(img);
                    mNewImages.Remove(img);
                }
            }
            if (mNewImages.Count != 0)
            {
                DebugLogger.Instance().LogError("Not all user images could be loaded");
            }
            //bmp.Save("packedbmp.png", System.Drawing.Imaging.ImageFormat.Png);
            mUserTexture = LoadTextureImage(bmp);
            foreach (C2DImage img in packedImages)
            {
                img.tex            = mUserTexture;
                ImgDbase[img.name] = img;
            }
            needPacking = false;
        }
        /*
         * This function try to rearranges all objects on the build platform so they
         * do not touch each other
         */
        public void RearrangeObjects()
        {
            BinPacker bp = new BinPacker();
            List <BinPacker.BinRect> rects = new List <BinPacker.BinRect>();

            foreach (Object3d obj in m_objects)
            {
                if (obj.tag != Object3d.OBJ_NORMAL)
                {
                    continue;
                }
                float             w  = obj.m_max.x - obj.m_min.x;
                float             h  = obj.m_max.y - obj.m_min.y;
                BinPacker.BinRect rc = new BinPacker.BinRect(w, h, obj);
                rects.Add(rc);
            }

            float pw = (float)UVDLPApp.Instance().m_printerinfo.m_PlatXSize;
            float ph = (float)UVDLPApp.Instance().m_printerinfo.m_PlatYSize;

            bp.Pack(rects, (int)pw, (int)ph, true);

            // find pack size
            int maxw = 0;
            int maxh = 0;

            foreach (BinPacker.BinRect rc in rects)
            {
                if (rc.packed && ((rc.x + rc.w) > maxw))
                {
                    maxw = rc.x + rc.w;
                }
                if (rc.packed && ((rc.y + rc.h) > maxh))
                {
                    maxh = rc.y + rc.h;
                }
            }

            // find offsets to center all objects
            float offsx = -(float)maxw / 2f + 0.5f;
            float offsy = -(float)maxh / 2f + 0.5f;

            // move all objects to new positions
            foreach (BinPacker.BinRect rc in rects)
            {
                Object3d obj = (Object3d)rc.obj;
                if (rc.packed)
                {
                    if (rc.rotated)
                    {
                        obj.Rotate(0, 0, 1.570796326f);
                    }
                    float dx = rc.x - obj.m_min.x + offsx;
                    float dy = rc.y - obj.m_min.y + offsy;
                    obj.Translate(dx, dy, 0, true);
                }
                else
                {
                    // object could not fit, place outside platform
                    obj.Translate(pw / 2f - obj.m_min.x, 0, 0, true);
                }
            }
            UVDLPApp.Instance().RaiseAppEvent(eAppEvent.eReDraw, "Redraw new arrangement");
        }