示例#1
0
 //Render a polygon
 private static void Render(DnaPolygon polygon, Graphics g, int scale)
 {
     using (Brush brush = GetGdiBrush(polygon.Brush))
     {
         Point[] points = GetGdiPoints(polygon.Points, scale);
         g.FillPolygon(brush,points);
     }
 }
示例#2
0
        public void AddPolygon()
        {
            var newPolygon = new DnaPolygon(tool);

            newPolygon.Init();

            Shapes.Add(newPolygon);
            SetDirty();
        }
示例#3
0
 //Render a polygon
 private static void Render(DnaPolygon polygon, Graphics g, int scale)
 {
     Point[] points = GetGdiPoints(polygon.Points, scale);
     using (Brush brush = GetGdiBrush(polygon.Brush))
     {
         g.FillPolygon(brush, points, FillMode.Winding);
         //g.FillClosedCurve(brush, points, FillMode.Winding);
     }
 }
示例#4
0
        /// <summary>
        /// Render a polygon
        /// </summary>
        /// <remarks>
        /// &lt;Polygon Points="100,0 75,75 100,100 125,75" Stroke="Black" StrokeThickness="2" Fill="Yellow" /&gt;
        /// </remarks>
        private static void Render(DnaPolygon polygonToRender, StringBuilder c, int scale)
        {
            string polygonPoints = "";

            for (int j = 0; j < polygonToRender.Points.Count; j++)
            {
                polygonPoints += polygonToRender.Points[j].X.ToString() + "," + polygonToRender.Points[j].Y.ToString() + " ";
            }
            c.Append(@"<Polygon Points=""" + polygonPoints + @""" Fill="""+ GetColorString(polygonToRender.Brush) + "\" />\r\n");
        }
示例#5
0
        public DnaPolygon Clone()
        {
            var newPolygon = new DnaPolygon();
            newPolygon.Points = new List<DnaPoint>();
            newPolygon.Brush = Brush.Clone();
            foreach (DnaPoint point in Points)
                newPolygon.Points.Add(point.Clone());

            return newPolygon;
        }
示例#6
0
        /// <summary>
        /// Render a polygon
        /// </summary>
        private static void Render(DnaPolygon polygonToRender, Canvas c, int scale)
        {
            System.Windows.Shapes.Polygon polygon = new Polygon();
            Brush b = GetSolidColorBrush(polygonToRender.Brush);
            polygon.StrokeThickness = 0; // no line
            polygon.Fill = b;

            for (int j = 0; j < polygonToRender.Points.Count; j++)
            {
                polygon.Points.Add(GetSysWinPoint(polygonToRender.Points[j]));
            }
            c.Children.Add(polygon);
        }
示例#7
0
        public void AddPolygon()
        {
            if (Polygons.Count < Settings.ActivePolygonsMax)
            {
                var newPolygon = new DnaPolygon();
                newPolygon.Init();

                int index = Tools.GetRandomNumber(0, Polygons.Count);

                Polygons.Insert(index, newPolygon);
                SetDirty();
            }
        }
示例#8
0
        public void AddPolygon()
        {
            if (Polygons.Count < Settings.ActivePolygonsMax)
            {
                var newPolygon = new DnaPolygon();
                newPolygon.Init();

                int index = Tools.GetRandomNumber(0, Polygons.Count);

                Polygons.Insert(index, newPolygon);
                SetDirty();
            }
        }
示例#9
0
        public DnaPolygon Clone()
        {
            var newPolygon = new DnaPolygon();

            newPolygon.Points = new List <DnaPoint>();
            newPolygon.Brush  = Brush.Clone();
            foreach (DnaPoint point in Points)
            {
                newPolygon.Points.Add(point.Clone());
            }

            return(newPolygon);
        }
示例#10
0
        public void AddPolygon(JobInfo info)
        {
            if (Polygons.Count < info.Settings.PolygonsMax)
            {
                var newPolygon = new DnaPolygon();
                newPolygon.Init(this, info);

                int index = info.GetRandomNumber(0, Polygons.Count);

                Polygons.Insert(index, newPolygon);

                SetDirty();
            }
        }
示例#11
0
        public void MovePolygon()
        {
            if (Polygons.Count < 1)
            {
                return;
            }

            int        index = Tools.GetRandomNumber(0, Polygons.Count);
            DnaPolygon poly  = Polygons[index];

            Polygons.RemoveAt(index);
            index = Tools.GetRandomNumber(0, Polygons.Count);
            Polygons.Insert(index, poly);
            SetDirty();
        }
示例#12
0
        internal void MovePolygon(JobInfo info)
        {
            if (Polygons.Count < 1)
            {
                return;
            }

            int        index = info.GetRandomNumber(0, Polygons.Count);
            DnaPolygon poly  = Polygons[index];

            Polygons.RemoveAt(index);
            index = info.GetRandomNumber(0, Polygons.Count);
            Polygons.Insert(index, poly);
            SetDirty();
        }
示例#13
0
        internal void Mutate(JobInfo info)
        {
            IsDirty = false;
            while (!IsDirty)
            {
                if (!info.Settings.MuteAddPolygonNew)
                {
                    if (info.WillMutate(info.Settings.AddPolygonMutationRate))
                    {
                        AddPolygon(info);
                    }
                }

                if (!info.Settings.MuteAddPolygonClone)
                {
                    if (info.WillMutate(info.Settings.AddPolygonCloneMutationRate))
                    {
                        AddPolygonClone(info);
                    }
                }

                if (!info.Settings.MuteRemovePolygon)
                {
                    if (info.WillMutate(info.Settings.RemovePolygonMutationRate))
                    {
                        RemovePolygon(info);
                    }
                }

                if (!info.Settings.MuteMovePolygon)
                {
                    if (info.WillMutate(info.Settings.MovePolygonMutationRate))
                    {
                        MovePolygon(info);
                    }
                }

                unchecked
                {
                    for (int i = 0; i < Polygons.Count; i++)
                    {
                        DnaPolygon polygon = Polygons[i];
                        polygon.Mutate(this, info);
                    }
                }
            }
        }
示例#14
0
        internal DnaDrawing Clone()
        {
            var drawing = new DnaDrawing
            {
                Polygons = new List <DnaPolygon>(),
                //SourceImage = SourceImage,
            };

            unchecked
            {
                for (int i = 0; i < Polygons.Count; i++)
                {
                    DnaPolygon polygon = Polygons[i];
                    drawing.Polygons.Add(polygon.Clone());
                }
            }
            return(drawing);
        }
示例#15
0
        internal DnaPolygon Clone()
        {
            var newPolygon = new DnaPolygon
            {
                Points = new List <DnaPoint>(),
                Brush  = Brush.Clone()
            };

            unchecked
            {
                for (int i = 0; i < Points.Count; i++)
                {
                    DnaPoint point = Points[i];
                    newPolygon.Points.Add(point.Clone());
                }
            }

            return(newPolygon);
        }
示例#16
0
        internal DnaPolygon Clone()
        {
            var newPolygon = new DnaPolygon
                                 {
                                     Points = new List<DnaPoint>(),
                                     Brush = Brush.Clone()
                                 };

            unchecked
            {
                for (int i = 0; i < Points.Count; i++)
                {
                    DnaPoint point = Points[i];
                    newPolygon.Points.Add(point.Clone());
                }
            }

            return newPolygon;
        }
示例#17
0
        internal void AddPolygonClone(JobInfo info)
        {
            if (Polygons.Count < info.Settings.PolygonsMax)
            {
                if (Polygons.Count < 1)
                {
                    AddPolygon(info);
                }
                else
                {
                    DnaPolygon parent     = Polygons[info.GetRandomNumber(0, Polygons.Count)];
                    DnaPolygon newPolygon = parent.Clone();
                    Polygons.Insert(Polygons.IndexOf(parent), newPolygon);

                    newPolygon.Offset(info.GetRandomNumber(-6, 6), info.GetRandomNumber(-6, 6));

                    SetDirty();
                }
            }
        }
示例#18
0
        public DnaPolygon Clone()
        {
            var newPolygon = new DnaPolygon
            {
                Points = new List <DnaPoint>(),
                Brush  = Brush.Clone()
            };

            foreach (DnaPoint point in Points)
            {
                newPolygon.Points.Add(point.Clone());
            }

            newPolygon.Width   = Width;
            newPolygon.Tension = Tension;
            newPolygon.Splines = Splines;
            newPolygon.Filled  = Filled;

            return(newPolygon);
        }
示例#19
0
        //Render a polygon
        private static void Render(DnaPolygon polygon, Graphics g, int scale)
        {
            if (polygon.IsComplex)
                return;

            Point[] points = GetGdiPoints(polygon.Points, scale);
            using (Brush brush = GetGdiBrush(polygon.Brush))
            {
                if (polygon.Splines)
                {
                    if (polygon.Filled)
                    {
                        g.FillClosedCurve(brush, points, FillMode.Winding);
                    }
                    else
                    {
                        using (Pen pen = new Pen(brush, Math.Max(1, polygon.Width)))
                        {
                            g.DrawCurve(pen, points, 3F);
                        }
                    }
                }
                else
                {
                    if (polygon.Filled)
                    {
                        g.FillPolygon(brush, points, FillMode.Winding);
                    }
                    else
                    {
                        using (Pen pen = new Pen(brush, Math.Max(1, polygon.Width)))
                        {
                            g.DrawPolygon(pen, points);
                        }
                    }
                }
            }
        }
        public void decompress()
        {
            QuickLZ qlz = new QuickLZ();
            byte[] decompressed = qlz.Decompress(File.ReadAllBytes(InputFile));
            Console.WriteLine("dec" + decompressed.Length.ToString());
            BitArray bits = new BitArray(decompressed);
            Console.WriteLine("bits" + bits.Length.ToString());
            int progress = 0;
            BitArray width = new BitArray(16); //Max width
            bitArrayCopy(bits, progress, width, 0, 16);
            Tools.MaxWidth = unwritebits(width); //create maxwidth
            progress += 16;
            BitArray height = new BitArray(16); //Max height
            bitArrayCopy(bits, progress, height, 0, 16);
            Tools.MaxHeight = unwritebits(height); //create maxheight
            progress += 16;
            BitArray avgreds = new BitArray(8); //red colour average
            bitArrayCopy(bits, progress, avgreds, 0, 8);
            int avgred = unwritebits(avgreds);
            progress += 8;
            BitArray avggreens = new BitArray(8); //green colour average
            bitArrayCopy(bits, progress, avggreens, 0, 8);
            int avggreen = unwritebits(avggreens);
            progress += 8;
            BitArray avgblues = new BitArray(8); //blue colour average
            bitArrayCopy(bits, progress, avgblues, 0, 8);
            int avgblue = unwritebits(avgblues);
            progress += 8;
            Tools.avgColour = System.Drawing.Color.FromArgb(avgred, avggreen, avgblue); //create average colour
            DnaDrawing newDrawing = new DnaDrawing(); //Create dnadrawing
            //newDrawing.Init();
            BitArray polynumbers = new BitArray(10); //number of polygons
            bitArrayCopy(bits, progress, polynumbers, 0, 10);
            int polynumber = unwritebits(polynumbers);
            progress += 10;
            newDrawing.Polygons = new List<DnaPolygon>();
            for (int i = 0; i < polynumber; i++)
            {
                Console.WriteLine(i);
                DnaPolygon polygon = new DnaPolygon(); //create dnapolygon
                polygon.Points = new List<DnaPoint>();
                BitArray pointnumbers = new BitArray(5); //number of points
                bitArrayCopy(bits, progress, pointnumbers, 0, 5);
                int pointnumber = unwritebits(pointnumbers);
                progress += 5;
                BitArray alphas = new BitArray(8); //alpha colour
                bitArrayCopy(bits, progress, alphas, 0, 8);
                int alpha = unwritebits(alphas);
                progress += 8;
                BitArray reds = new BitArray(8); //red colour
                bitArrayCopy(bits, progress, reds, 0, 8);
                int red = unwritebits(reds);
                progress += 8;
                BitArray greens = new BitArray(8); //green colour
                bitArrayCopy(bits, progress, greens, 0, 8);
                int green = unwritebits(greens);
                progress += 8;
                BitArray blues = new BitArray(8); //blue colour
                bitArrayCopy(bits, progress, blues, 0, 8);
                int blue = unwritebits(blues);
                progress += 8;
                DnaBrush brush = new DnaBrush(); //create dnabrush
                brush.Alpha=alpha;
                brush.Red = red;
                brush.Green = green;
                brush.Blue=blue;
                polygon.Brush=brush; //assign brush
                for (int j = 0; j < pointnumber; j++)
                {
                    DnaPoint point = new DnaPoint();
                    BitArray Xs = new BitArray(12); //x position
                    bitArrayCopy(bits, progress, Xs, 0, 12);
                    int x = unwritebits(Xs);
                    progress += 12;
                    point.X = x;
                    BitArray Ys = new BitArray(12); //y position
                    bitArrayCopy(bits, progress, Ys, 0, 12);
                    int y = unwritebits(Ys);
                    progress += 12;
                    point.Y = y;
                    polygon.Points.Add(point);
                }
                newDrawing.Polygons.Add(polygon);
            }
            int[,] DiffArray = new int[Tools.MaxWidth * Tools.MaxWidth, 4];
            int[,] refArray = new int[Tools.MaxWidth * Tools.MaxWidth, 5];
            for (int i = 0; i < Tools.MaxWidth * Tools.MaxWidth; i++)
            {
                BitArray firsts = new BitArray(1); //normal or reference
                bitArrayCopy(bits, progress, firsts, 0, 1);
                int first = unwritebits(firsts);
                progress += 1;
                if (first == 1)
                {
                    BitArray reffs = new BitArray(11); //reference number
                    bitArrayCopy(bits, progress, reffs, 0, 11);
                    int reff = unwritebits(reffs);
                    progress += 11;
                    refArray[i,4]=reff;
                }
                else
                {
                    //BitArray asizes = new BitArray(3); //alpha size
                    //bitArrayCopy(bits, progress, asizes, 0, 3);
                    int asize = 9;//unwritebits(asizes);
                    //progress += 3;
                    //BitArray rsizes = new BitArray(3); //red size
                    //bitArrayCopy(bits, progress, rsizes, 0, 3);
                    int rsize = 9;//unwritebits(rsizes);
                    //progress += 3;
                    //BitArray gsizes = new BitArray(3); //green size
                    //bitArrayCopy(bits, progress, gsizes, 0, 3);
                    int gsize = 9;//unwritebits(gsizes);
                    //progress += 3;
                    //BitArray bsizes = new BitArray(3); //blue size
                    //bitArrayCopy(bits, progress, bsizes, 0, 3);
                    int bsize = 9;//unwritebits(bsizes);
                    //progress += 3;

                    BitArray a = new BitArray(asize); //alpha
                    bitArrayCopy(bits, progress, a, 0, asize);
                    int alpha = unwritebits(a);
                    progress += asize;
                    DiffArray[i, 0] = alpha-256;
                    BitArray r = new BitArray(rsize); //red
                    bitArrayCopy(bits, progress, r, 0, rsize);
                    int red = unwritebits(r);
                    progress += rsize;
                    DiffArray[i, 1] = red-256;
                    BitArray g = new BitArray(gsize); //green
                    bitArrayCopy(bits, progress, g, 0, gsize);
                    int green = unwritebits(g);
                    progress += gsize;
                    DiffArray[i, 2] = green-256;
                    BitArray b = new BitArray(bsize); //blue
                    bitArrayCopy(bits, progress, b, 0, bsize);
                    int blue = unwritebits(b);
                    progress += bsize;
                    DiffArray[i, 3] = blue-256;
                }
            }
            //int[,] fullArray = (int[,]) refArray.Clone();
            for (int k = 0; k < refArray.Length / 5; k++)
            {
                if (refArray[k, 4] != 0)
                {
                    DiffArray[k, 0] = refArray[k - refArray[k, 4], 0];
                    DiffArray[k, 1] = refArray[k - refArray[k, 4], 1];
                    DiffArray[k, 2] = refArray[k - refArray[k, 4], 2];
                    DiffArray[k, 3] = refArray[k - refArray[k, 4], 3];
                    Console.WriteLine(refArray[k, 1]);
                }
            }

            Console.WriteLine(newDrawing.Polygons.Count);
            Console.WriteLine("done with drawing");

            Bitmap _bmp;
            Graphics _g;
            _bmp = new System.Drawing.Bitmap(Tools.MaxWidth, Tools.MaxHeight);
            _g = Graphics.FromImage(_bmp);
            Renderer.Render(newDrawing, _g, 1);
            BitmapData bd = _bmp.LockBits(new System.Drawing.Rectangle(0, 0, Tools.MaxWidth, Tools.MaxHeight), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            _bmp.Save("finally.png", System.Drawing.Imaging.ImageFormat.Png);
            _bmp.UnlockBits(bd);

            Bitmap _bmp2 = new System.Drawing.Bitmap(Tools.MaxWidth, Tools.MaxHeight);
            int arraycounter2 = 0;
            for (int y = 0; y < Tools.MaxHeight; y++)
            {
                for (int x = 0; x < Tools.MaxWidth; x++)
                {
                    System.Drawing.Color c1 = _bmp.GetPixel(x, y);
                    int a = c1.A + DiffArray[arraycounter2, 0];
                    int r = c1.R + DiffArray[arraycounter2, 1];
                    int g = c1.G + DiffArray[arraycounter2, 2];
                    int b = c1.B + DiffArray[arraycounter2, 3];
                    System.Drawing.Color c3 = System.Drawing.Color.FromArgb(a, r, g, b);
                    _bmp2.SetPixel(x, y, c3);
                    arraycounter2++;
                }
            }
            _bmp2.Save("success.png", System.Drawing.Imaging.ImageFormat.Png);
            System.Diagnostics.Process.Start(@"success.png");
        }
示例#21
0
        public void AddPolygon(JobInfo info)
        {
            if (Polygons.Count < info.Settings.PolygonsMax)
            {
                var newPolygon = new DnaPolygon();
                newPolygon.Init(this, info);

                int index = info.GetRandomNumber(0, Polygons.Count);

                Polygons.Insert(index, newPolygon);

                SetDirty();
            }
        }
示例#22
0
        public DnaPolygon Clone()
        {
            var newPolygon = new DnaPolygon
                                 {
                                     Points = new List<DnaPoint>(),
                                     Brush = Brush.Clone()
                                 };
            foreach (DnaPoint point in Points)
                newPolygon.Points.Add(point.Clone());

            newPolygon.Width = Width;
            newPolygon.Tension = Tension;
            newPolygon.Splines = Splines;
            newPolygon.Filled = Filled;

            return newPolygon;
        }
示例#23
0
        internal virtual void WorkLoop(Intracommunicator comm)
        {
            startTime = DateTime.Now;
            Console.WriteLine("Starting worker {0} ", comm.Rank);
            var timer = new Stopwatch();


            int partitionHeight = info.SourceImage.Height / comm.Size;
            int partitionY = partitionHeight * comm.Rank;

            for (int i = 0; i < info.Settings.PolygonsMax; i++)
            {
                var polygon = new DnaPolygon();
                parentDrawing.Polygons.Add(polygon);
                polygon.Init(parentDrawing, info);

                //foreach (DnaPoint p in polygon.Points)
                //{
                //    p.Y = partitionY + partitionHeight / 2;
                //}
            }

            int waitTimer = 0;
            while (true)
            {
                if (comm.Rank == 0)
                    NotifyProgress(generation);

                timer.Reset();
                timer.Start();

                while (timer.ElapsedMilliseconds < waitTimer)
                {                   
                    
                    var currentDrawing = parentDrawing.GetMutatedChild(info);
                    var currentErrorLevel = GetFitnessForDrawing(currentDrawing);

                    if (currentErrorLevel < parentErrorLevel)
                    {
                        parentErrorLevel = currentErrorLevel;
                        parentDrawing = currentDrawing;
                    }
                }

                if (waitTimer < 6000)
                    waitTimer += 500;

                timer.Stop();

                generation++;               

                var drawingInfo = new MpiWorkerDrawingInfo
                                      {
                                          Drawing = parentDrawing
                                      };


            //    Stopwatch swSync = new Stopwatch();
            //    swSync.Start();
                MpiWorkerDrawingInfo[] allResults = comm.Allgather(drawingInfo);

                if (bgImage != null)
                    bgImage.Dispose();

                bgImage = new Bitmap(info.SourceImage.Width, info.SourceImage.Height,
                                         PixelFormat.Format32bppArgb);
                var bgGraphics = Graphics.FromImage(bgImage);

                bgGraphics.Clear(Color.Black);
                bgGraphics.SmoothingMode = SmoothingMode.HighQuality;
                for (int i = 0; i < comm.Rank; i++)
                {
                    Renderer.Render(allResults[i].Drawing, bgGraphics, 1);
                }

                if (fgImage != null)
                    fgImage.Dispose();

                fgImage = new Bitmap(info.SourceImage.Width, info.SourceImage.Height,
                                         PixelFormat.Format32bppArgb);
                var fgGraphics = Graphics.FromImage(fgImage);

                fgGraphics.Clear(Color.Transparent);
                fgGraphics.SmoothingMode = SmoothingMode.HighQuality;
                for (int i = comm.Rank + 1; i < comm.Size; i++)
                {
                    Renderer.Render(allResults[i].Drawing, fgGraphics, 1);
                }

                fgGraphics.Dispose();
                bgGraphics.Dispose();
               

                //recalc the new parent error level
                parentErrorLevel = GetFitnessForDrawing(parentDrawing);
            //    swSync.Stop();

            //    Console.WriteLine("sync {0}", swSync.Elapsed);
            }
        }