private void SetPathValue(Prefab.Point currNode, Mode mode) { switch (mode) { case Mode.Up: if (MinY > currNode.Y) { MinY = currNode.Y; } break; case Mode.Right: if (MaxX < currNode.X) { MaxX = currNode.X; } break; case Mode.Down: if (MaxY < currNode.Y) { MaxY = currNode.Y; } break; case Mode.Left: if (MinX > currNode.X) { MinX = currNode.X; } break; } }
private List <Prefab.Point> NexTurn(Prefab.Point turn, List <Prefab.Point> turns) { List <Prefab.Point> cpy = new List <Prefab.Point>(); cpy.AddRange(turns); cpy.Add(turn); return(cpy); }
/// <summary> /// /// </summary> /// <param name="bitmap"></param> /// <param name="location"></param> /// <param name="grid"></param> public static void AddEachPixelOfBitmapToGrid(Bitmap bitmap, Prefab.Point location, Grid grid) { for (int row = 0; row < bitmap.Height; row++) { for (int col = 0; col < bitmap.Width; col++) { AddPixelToGrid(bitmap[row, col], new Prefab.Point(location.X + col, location.Y + row), grid); } } }
private bool Done(Prefab.Point currNode, Mode mode) { if (currNode.X == Start.X && currNode.Y - 1 == Start.Y) { return(true); } return(false); }
/// <summary> /// Renders a corner part to a grid at the specified location. Each pixel /// of the corner is used to color a cell of the grid. /// </summary> /// <param name="corner">The corner feature to render.</param> /// <param name="location">The location in the grid where the corner will be rendered.</param> /// <param name="grid">The grid object that will be populated with the corner's pixels</param> public static void AddFeatureToGrid(Feature feature, Prefab.Point location, Grid grid) { for (int row = 0; row < feature.Bitmap.Height; row++) { for (int col = 0; col < feature.Bitmap.Width; col++) { AddPixelToGrid(feature.Bitmap[row, col], new Prefab.Point(location.X + col, location.Y + row), grid); } } }
/// <summary> /// Adds an image to a grid. The ColumnSpan and RowSpan properties are set equal to the /// dimensions of the bitmap. /// </summary> /// <param name="image"></param> /// <param name="location"></param> /// <param name="grid"></param> public static void AddImageToGrid(Bitmap image, Prefab.Point location, Grid grid) { Image img = ViewablePrototypeItem.ToImage(image); grid.Children.Add(img); Grid.SetColumn(img, location.X); Grid.SetRow(img, location.Y); Grid.SetColumnSpan(img, image.Width); Grid.SetRowSpan(img, image.Height); }
/// <summary> /// Adds either the left or right region to the grid. /// </summary> /// <param name="region"></param> /// <param name="upperleft"></param> /// <param name="grid"></param> public static void AddVerticalRegionToGrid(Bitmap verticalPattern, Prefab.Point upperleft, Grid grid) { for (int row = 0; row < verticalPattern.Height; row++) { for (int col = 0; col < verticalPattern.Width; col++) { Prefab.Point location = new Prefab.Point(upperleft.X + col, upperleft.Y + row); AddPixelToGrid(verticalPattern[row, col], location, grid); } } }
private List <Int32Rect> FindRects() { bool closed = false; int count = 0; while (Start.X >= 0) { ResetPathValues(); //_Bitmaps = CopyBitmaps(_BitmapsCpy); int value = Bitmap(Start, Mode.Left); if (value > Thresh) { closed = false; } if (!closed && value <= Thresh) { SetPathValue(Start, Mode.Left); ReturnValue path = Run(Start, Mode.Up); if (path == ReturnValue.FailedNormal) { closed = false; } else if (path == ReturnValue.Closed) { closed = true; Int32Rect rect = Rectangle(); ClearRectangle(rect); rect.X += 1; rect.Width -= 1; if (rect.Width > 4 && rect.Height > 4) { FoundRects.Add(rect); } } else { closed = false; Int32Rect rect = new Int32Rect(); ClearRectangle(rect); } } Start = new Prefab.Point(Start.X - 1, Start.Y); count++; } return(FoundRects); }
private ReturnValue Run(Prefab.Point currNode, Mode mode) { #region escape cases //check if an edge or was visited if (!BitmapContainsOffset(Bitmaps[0], currNode) || Bitmap(currNode, mode) > Thresh) { return(ReturnValue.FailedNormal); } //mark visited SetVisited(currNode, Thresh + 1); //Set a property defining our path SetPathValue(currNode, mode); //If we're not surrounding the mouse clicks, we fail if (!AroundPoints(currNode, mode)) { return(ReturnValue.FailedNormal); } //Check if we closed the rectangle if (Done(currNode, mode)) { return(ReturnValue.Closed); } #endregion Prefab.Point child = Child(currNode, mode); ReturnValue value = Run(child, NextMode(mode)); if (value == ReturnValue.Closed) { return(value); } else if (value == ReturnValue.FailedDontContinue) { return(value); } else if (value == ReturnValue.FailedNormal && mode == Mode.Left && currNode.X == Start.X && currNode.Y > Start.Y) { return(ReturnValue.FailedDontContinue); } return(Run(Sibling(currNode, mode), mode)); }
private Prefab.Point Child(Prefab.Point currNode, Mode currMode) { switch (currMode) { case Mode.Up: return(new Prefab.Point(currNode.X + 1, currNode.Y)); case Mode.Right: return(new Prefab.Point(currNode.X, currNode.Y + 1)); case Mode.Down: return(new Prefab.Point(currNode.X - 1, currNode.Y)); case Mode.Left: return(new Prefab.Point(currNode.X, currNode.Y - 1)); } throw new Exception("Invalid number of turns"); }
private bool AroundPoints(Prefab.Point currNode, Mode mode) { switch (mode) { case Mode.Up: return(currNode.X <= Start.X); case Mode.Right: return(currNode.Y < FarthestUpClick); case Mode.Down: return(currNode.X > FarthestRightClick); case Mode.Left: return(currNode.Y > FarthestDownClick); } return(false); }
private Prefab.Point Sibling(Prefab.Point currNode, Mode currMode) { switch (currMode) { case Mode.Up: return(new Prefab.Point(currNode.X, currNode.Y - 1)); case Mode.Right: return(new Prefab.Point(currNode.X + 1, currNode.Y)); case Mode.Down: return(new Prefab.Point(currNode.X, currNode.Y + 1)); case Mode.Left: return(new Prefab.Point(currNode.X - 1, currNode.Y)); } return(currNode); throw new Exception("Invalid mode"); }
/// <summary> /// Adds a pixel to the grid. It assignes the specified cell with the /// specified pixel value by adding a colored rectangle in that cell. /// </summary> /// <param name="pixel"></param> /// <param name="location"></param> /// <param name="grid"></param> public static void AddPixelToGrid(Int32 pixel, Prefab.Point location, Grid grid) { Rectangle rect = new Rectangle(); if (pixel == Feature.TRANSPARENT_VALUE) { rect.Fill = Brushes.Transparent; } else { rect.Fill = new SolidColorBrush(Color.FromArgb((byte)Bitmap.Alpha(pixel), (byte)Bitmap.Red(pixel), (byte)Bitmap.Green(pixel), (byte)Bitmap.Blue(pixel))); } rect.Stroke = Brushes.Black; rect.StrokeThickness = 0.5; grid.Children.Add(rect); Grid.SetRow(rect, location.Y); Grid.SetColumn(rect, location.X); }
private void SetVisited(Prefab.Point offset, int value) { SetVisited(offset.Y, offset.X, value); }
/// <summary> /// /// </summary> /// <param name="visual"></param> /// <param name="location"></param> /// <param name="grid"></param> public static void AddVisualToGrid(UIElement visual, Prefab.Point location, Grid grid) { grid.Children.Add(visual); Grid.SetColumn(visual, location.X); Grid.SetRow(visual, location.Y); }
private int Bitmap(Prefab.Point currNode, Mode mode) { return(Bitmap(currNode.Y, currNode.X, mode)); }
public Visual Visualize(Ptype ptype, object parameter = null) { if (!ptype.Model.Name.Equals("ninepart")) { throw new Exception("Nine Part Model cannot render prototypes created by other models."); } Feature bottomleftFeature = ptype.Feature("bottomleft"); Feature toprightFeature = ptype.Feature("topright"); Feature bottomrightFeature = ptype.Feature("bottomright"); Feature topleftFeature = ptype.Feature("topleft"); Region topregion = ptype.Region("top"); Region leftregion = ptype.Region("left"); Region bottomregion = ptype.Region("bottom"); Region rightregion = ptype.Region("right"); Region interiorRegion = ptype.Region("interior"); Grid grid = new Grid(); ////Find the biggest dimensions for each Part object so that ////we can figure out how many cells to add into the grid. int longestTopOrBottom = (int)Math.Max(topregion.Bitmap.Width, bottomregion.Bitmap.Width); int longestLeftOrRight = (int)Math.Max(leftregion.Bitmap.Height, rightregion.Bitmap.Height); int widestTLOrBL = (int)Math.Max(topleftFeature.Bitmap.Width, bottomleftFeature.Bitmap.Width); int widestTROrBR = (int)Math.Max(toprightFeature.Bitmap.Width, bottomrightFeature.Bitmap.Width); int tallestTLOrTR = (int)Math.Max(topleftFeature.Bitmap.Height, toprightFeature.Bitmap.Height); int tallestBLOrBR = (int)Math.Max(bottomleftFeature.Bitmap.Height, bottomrightFeature.Bitmap.Height); int interiorWidth = 0; int interiorHeight = 0; if (interiorRegion != null) { interiorHeight = interiorRegion.Bitmap.Height; interiorWidth = interiorRegion.Bitmap.Width; } //Assign the width and height of the grid. int width = Math.Max(widestTLOrBL + longestTopOrBottom + widestTROrBR + 2, interiorWidth + leftregion.Bitmap.Width + rightregion.Bitmap.Width + 2); int height = Math.Max(tallestTLOrTR + longestLeftOrRight + tallestBLOrBR + 2, interiorHeight + topregion.Bitmap.Height + bottomregion.Bitmap.Height + 2); //Set the rows and columns of the grid. for (int row = 0; row < height; row++) { RowDefinition rowdef = new RowDefinition(); grid.RowDefinitions.Add(rowdef); } for (int col = 0; col < width; col++) { ColumnDefinition coldef = new ColumnDefinition(); grid.ColumnDefinitions.Add(coldef); } //Add each Part to the grid (cells = pixels). PtypeVisualizerHelper.AddFeatureToGrid(topleftFeature, new Prefab.Point(0, 0), grid); PtypeVisualizerHelper.AddFeatureToGrid(toprightFeature, new Prefab.Point(width - toprightFeature.Bitmap.Width, 0), grid); PtypeVisualizerHelper.AddFeatureToGrid(bottomleftFeature, new Prefab.Point(0, height - bottomleftFeature.Bitmap.Height), grid); PtypeVisualizerHelper.AddFeatureToGrid(bottomrightFeature, new Prefab.Point(width - bottomrightFeature.Bitmap.Width, height - bottomrightFeature.Bitmap.Height), grid); PtypeVisualizerHelper.AddHorizontalRegionToGrid(topregion.Bitmap, new Prefab.Point(topleftFeature.Bitmap.Width + 1, 0), grid); PtypeVisualizerHelper.AddHorizontalRegionToGrid(bottomregion.Bitmap, new Prefab.Point(bottomleftFeature.Bitmap.Width + 1, height - bottomregion.Bitmap.Height), grid); PtypeVisualizerHelper.AddVerticalRegionToGrid(leftregion.Bitmap, new Prefab.Point(0, topleftFeature.Bitmap.Height + 1), grid); PtypeVisualizerHelper.AddVerticalRegionToGrid(rightregion.Bitmap, new Prefab.Point(width - rightregion.Bitmap.Width, toprightFeature.Bitmap.Height + 1), grid); if (interiorRegion != null) { Prefab.Point location; if (interiorRegion.MatchStrategy.Equals("horizontal")) { location = new Prefab.Point(topleftFeature.Bitmap.Width + 1, (height - interiorHeight) / 2); } else { location = new Prefab.Point((width - interiorWidth) / 2, topleftFeature.Bitmap.Height + 1); } PtypeVisualizerHelper.AddEachPixelOfBitmapToGrid(interiorRegion.Bitmap, location, grid); } return(grid); }
/// <summary> /// Returns the distance from a point to a rectangle. /// </summary> /// <param name="p"></param> /// <param name="bb"></param> /// <returns></returns> public static double DistanceBetweenPointAndRectangle(int x, int y, IBoundingBox bb) { Prefab.Point closest = ClosestPointOnRectangleToPoint(x, y, bb); return(DistanceBetweenTwoPoints(x, y, closest.X, closest.Y)); }
public Visual Visualize(Ptype ptype, object parameter = null) { if (!ptype.Model.Name.Equals("ninepart")) { throw new Exception("Nine Part Model cannot render prototypes created by other models."); } Feature bottomleftFeature = ptype.Feature("bottomleft"); Feature toprightFeature = ptype.Feature("topright"); Feature bottomrightFeature = ptype.Feature("bottomright"); Feature topleftFeature = ptype.Feature("topleft"); Region topregion = ptype.Region("top"); Region leftregion = ptype.Region("left"); Region bottomregion = ptype.Region("bottom"); Region rightregion = ptype.Region("right"); Region interiorRegion = ptype.Region("interior"); Grid grid = new Grid(); ////Find the biggest dimensions for each Part object so that ////we can figure out how many cells to add into the grid. int longestTopOrBottom = (int)Math.Max(topregion.Bitmap.Width, bottomregion.Bitmap.Width); int longestLeftOrRight = (int)Math.Max(leftregion.Bitmap.Height, rightregion.Bitmap.Height); int widestTLOrBL = (int)Math.Max(topleftFeature.Bitmap.Width, bottomleftFeature.Bitmap.Width); int widestTROrBR = (int)Math.Max(toprightFeature.Bitmap.Width, bottomrightFeature.Bitmap.Width); int tallestTLOrTR = (int)Math.Max(topleftFeature.Bitmap.Height, toprightFeature.Bitmap.Height); int tallestBLOrBR = (int)Math.Max(bottomleftFeature.Bitmap.Height, bottomrightFeature.Bitmap.Height); int interiorWidth = 0; int interiorHeight = 0; if (interiorRegion != null) { interiorHeight = interiorRegion.Bitmap.Height; interiorWidth = interiorRegion.Bitmap.Width; } //Assign the width and height of the grid. int width = Math.Max(widestTLOrBL + longestTopOrBottom + widestTROrBR + 2, interiorWidth + leftregion.Bitmap.Width + rightregion.Bitmap.Width + 2); int height = Math.Max(tallestTLOrTR + longestLeftOrRight + tallestBLOrBR + 2, interiorHeight + topregion.Bitmap.Height + bottomregion.Bitmap.Height + 2); //Set the rows and columns of the grid. for (int row = 0; row < height; row++) { RowDefinition rowdef = new RowDefinition(); grid.RowDefinitions.Add(rowdef); } for (int col = 0; col < width; col++) { ColumnDefinition coldef = new ColumnDefinition(); grid.ColumnDefinitions.Add(coldef); } //Add each Part to the grid (cells = pixels). PtypeVisualizerHelper.AddFeatureToGrid(topleftFeature, new Prefab.Point(0, 0), grid); PtypeVisualizerHelper.AddFeatureToGrid(toprightFeature, new Prefab.Point(width - toprightFeature.Bitmap.Width, 0), grid); PtypeVisualizerHelper.AddFeatureToGrid(bottomleftFeature, new Prefab.Point(0, height - bottomleftFeature.Bitmap.Height), grid); PtypeVisualizerHelper.AddFeatureToGrid(bottomrightFeature, new Prefab.Point(width - bottomrightFeature.Bitmap.Width, height - bottomrightFeature.Bitmap.Height), grid); PtypeVisualizerHelper.AddHorizontalRegionToGrid(topregion.Bitmap, new Prefab.Point(topleftFeature.Bitmap.Width + 1, 0), grid); PtypeVisualizerHelper.AddHorizontalRegionToGrid(bottomregion.Bitmap, new Prefab.Point(bottomleftFeature.Bitmap.Width + 1, height - bottomregion.Bitmap.Height), grid); PtypeVisualizerHelper.AddVerticalRegionToGrid(leftregion.Bitmap, new Prefab.Point(0, topleftFeature.Bitmap.Height + 1), grid); PtypeVisualizerHelper.AddVerticalRegionToGrid(rightregion.Bitmap, new Prefab.Point(width - rightregion.Bitmap.Width, toprightFeature.Bitmap.Height + 1), grid); if (interiorRegion != null) { Prefab.Point location; if (interiorRegion.MatchStrategy.Equals("horizontal")) location = new Prefab.Point(topleftFeature.Bitmap.Width + 1, (height - interiorHeight) / 2); else location = new Prefab.Point((width - interiorWidth) / 2, topleftFeature.Bitmap.Height + 1); PtypeVisualizerHelper.AddEachPixelOfBitmapToGrid(interiorRegion.Bitmap, location, grid); } return grid; }
private List<Int32Rect> FindRects() { bool closed = false; int count = 0; while (Start.X >= 0) { ResetPathValues(); //_Bitmaps = CopyBitmaps(_BitmapsCpy); int value = Bitmap(Start, Mode.Left); if (value > Thresh) closed = false; if (!closed && value <= Thresh) { SetPathValue(Start, Mode.Left); ReturnValue path = Run(Start, Mode.Up); if (path == ReturnValue.FailedNormal) closed = false; else if (path == ReturnValue.Closed) { closed = true; Int32Rect rect = Rectangle(); ClearRectangle(rect); rect.X += 1; rect.Width -= 1; if(rect.Width > 4 && rect.Height > 4) FoundRects.Add(rect); } else { closed = false; Int32Rect rect = new Int32Rect(); ClearRectangle(rect); } } Start = new Prefab.Point(Start.X - 1, Start.Y); count++; } return FoundRects; }
private bool BitmapContainsOffset(Bitmap bmp, Prefab.Point location) { return(location.Y < bmp.Height && location.X < bmp.Width && location.X >= 0 && location.Y >= 0); }