Exemplo n.º 1
0
        public SKSurface GenerateImage(Color background, int targetWidth = 0, bool borders = false, bool expanded = false, int xShift = 5, int yShift = 5, int colorType = 0)
        {
            DateTime time          = DateTime.Now;
            double   scalingFactor = 1;
            int      width         = shapes.Max(s => s.GetContainer().x2);
            int      heigth        = shapes.Max(s => s.GetContainer().y2);

            if (targetWidth != 0)
            {
                // get dimensions of the structure

                //int x_min = shapes.Min(s => s.GetContainer().x1);
                //int y_min = shapes.Min(s => s.GetContainer().y1);
                scalingFactor = Math.Min((double)targetWidth / width, (double)targetWidth / heigth);
            }
            var       info = new SKImageInfo((int)(width * scalingFactor) + 2 * xShift, (int)(heigth * scalingFactor) + 2 * yShift);
            SKSurface surf = SKSurface.Create(info);

            SKCanvas canvas = surf.Canvas;

            canvas.Clear(new SKColor(background.R, background.G, background.B, background.A));

            Parallel.For(0, shapes.Length, (i) =>
            {
                Color c;
                if (colorType == 0)
                {
                    c = colors[shapes[i].Color].mediaColor;
                }
                else if (colorType == 1)
                {
                    c = Color.FromArgb((byte)shapes[i].PrimaryDitherColor.Alpha, (byte)shapes[i].PrimaryDitherColor.Red,
                                       (byte)shapes[i].PrimaryDitherColor.Green, (byte)shapes[i].PrimaryDitherColor.Blue);
                }
                else
                {
                    c = Color.FromArgb((byte)shapes[i].PrimaryOriginalColor.Alpha, (byte)shapes[i].PrimaryOriginalColor.Red,
                                       (byte)shapes[i].PrimaryOriginalColor.Green, (byte)shapes[i].PrimaryOriginalColor.Blue);
                }
                if (shapes[i] is RectangleDomino)
                {
                    DominoRectangle rect = shapes[i].GetContainer(scalingFactor, expanded);
                    if (c.A != 0)
                    {
                        canvas.DrawRect((float)rect.x + xShift, (float)rect.y + yShift, (float)rect.width, (float)rect.height,
                                        new SKPaint()
                        {
                            Color = new SKColor(c.R, c.G, c.B, c.A), IsAntialias = true
                        });
                    }
                    if (borders)
                    {
                        canvas.DrawRect((float)rect.x + xShift, (float)rect.y + yShift, (float)rect.width, (float)rect.height,
                                        new SKPaint()
                        {
                            Color = new SKColor(0, 0, 0, 255), IsAntialias = true, IsStroke = true, StrokeWidth = 1
                        });
                    }
                }
                else
                {
                    DominoPath shape = shapes[i].GetPath(scalingFactor);
                    var sdpoints     = shape.getSDPath(xShift, yShift);
                    if (sdpoints.Length != 0)
                    {
                        var path = new SKPath();
                        path.MoveTo(sdpoints[0].X, sdpoints[0].Y);
                        foreach (var line in sdpoints.Skip(0))
                        {
                            path.LineTo(line.X, line.Y);
                        }
                        path.Close();

                        if (c.A != 0)
                        {
                            canvas.DrawPath(path,
                                            new SKPaint()
                            {
                                Color = new SKColor(c.R, c.G, c.B, c.A), IsAntialias = true, IsStroke = false
                            });
                        }
                        if (borders)
                        {
                            canvas.DrawPath(path,
                                            new SKPaint()
                            {
                                Color = new SKColor(0, 0, 0, 255), IsAntialias = true, IsStroke = true, StrokeWidth = 1
                            });
                        }
                    }
                }
            });
            Debug.WriteLine("Image export took " + (DateTime.Now - time).TotalMilliseconds + "ms");
            return(surf);
        }
Exemplo n.º 2
0
        public Mat GenerateImage(Color background, int targetWidth = 0, bool borders = false, bool expanded = false, int xShift = 5, int yShift = 5, int colorType = 0)
        {
            double scalingFactor = 1;
            int    width         = shapes.Max(s => s.GetContainer().x2);
            int    heigth        = shapes.Max(s => s.GetContainer().y2);

            if (targetWidth != 0)
            {
                // get dimensions of the structure

                //int x_min = shapes.Min(s => s.GetContainer().x1);
                //int y_min = shapes.Min(s => s.GetContainer().y1);
                scalingFactor = Math.Min((double)targetWidth / width, (double)targetWidth / heigth);
            }
            Image <Emgu.CV.Structure.Bgra, byte> bitmap
                = new Image <Emgu.CV.Structure.Bgra, byte>((int)(width * scalingFactor) + 2 * xShift, (int)(heigth * scalingFactor) + 2 * yShift,
                                                           new Emgu.CV.Structure.Bgra()
            {
                Alpha = background.A, Blue = background.B, Green = background.G, Red = background.R
            });


            Parallel.For(0, shapes.Length, (i) =>
            {
                Color c;
                if (colorType == 0)
                {
                    c = colors[shapes[i].color].mediaColor;
                }
                else if (colorType == 1)
                {
                    c = Color.FromArgb((byte)shapes[i].PrimaryDitherColor.Alpha, (byte)shapes[i].PrimaryDitherColor.Red,
                                       (byte)shapes[i].PrimaryDitherColor.Green, (byte)shapes[i].PrimaryDitherColor.Blue);
                }
                else
                {
                    c = Color.FromArgb((byte)shapes[i].PrimaryOriginalColor.Alpha, (byte)shapes[i].PrimaryOriginalColor.Red,
                                       (byte)shapes[i].PrimaryOriginalColor.Green, (byte)shapes[i].PrimaryOriginalColor.Blue);
                }
                if (shapes[i] is RectangleDomino)
                {
                    DominoRectangle rect = shapes[i].GetContainer(scalingFactor, expanded);
                    if (c.A != 0)
                    {
                        CvInvoke.Rectangle(bitmap, new System.Drawing.Rectangle()
                        {
                            X      = (int)rect.x + xShift,
                            Y      = (int)rect.y + yShift,
                            Width  = (int)rect.width,
                            Height = (int)rect.height
                        }, new Emgu.CV.Structure.MCvScalar(c.B, c.G, c.R, c.A), -1, Emgu.CV.CvEnum.LineType.AntiAlias);
                    }
                    if (borders)
                    {
                        CvInvoke.Rectangle(bitmap, new System.Drawing.Rectangle()
                        {
                            X      = (int)rect.x + xShift,
                            Y      = (int)rect.y + yShift,
                            Width  = (int)rect.width,
                            Height = (int)rect.height
                        }, new Emgu.CV.Structure.MCvScalar(0, 0, 0, 255), 1, Emgu.CV.CvEnum.LineType.AntiAlias);
                    }
                }
                else
                {
                    DominoPath shape = shapes[i].GetPath(scalingFactor);
                    if (c.A != 0)
                    {
                        bitmap.FillConvexPoly(shape.getSDPath(xShift, yShift),
                                              new Emgu.CV.Structure.Bgra(c.B, c.G, c.R, c.A), Emgu.CV.CvEnum.LineType.AntiAlias);
                    }
                    if (borders)
                    {
                        bitmap.DrawPolyline(shape.getSDPath(xShift, yShift), true,
                                            new Emgu.CV.Structure.Bgra(0, 0, 0, 255), 1, Emgu.CV.CvEnum.LineType.AntiAlias);
                    }
                }
            });
            return(bitmap.Mat);
        }
Exemplo n.º 3
0
 public double SizeOfCommonBoundingRectangle(DominoRectangle other)
 => (Math.Max(x2, other.x2) - Math.Min(x1, other.x1)) * (Math.Max(y2, other.y2) - Math.Min(y1, other.y1));
Exemplo n.º 4
0
 public double OverlapArea(DominoRectangle other)
 => Math.Max((Math.Min(x2, other.x2) - Math.Max(x, other.x)) * (Math.Min(y2, other.y2) - Math.Max(y, other.y)), 0);
Exemplo n.º 5
0
 public bool Intersects(DominoRectangle other) => x <other.x2 && x2> other.x1 && y1 <other.y2 && y2> other.y1;
Exemplo n.º 6
0
 // assumes that y axis points upwards
 public bool Contains(DominoRectangle other) => x <other.x && x2> other.x2 && y > other.y && y2 < other.y2;