Example #1
0
            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;



            }
Example #2
0
        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;
        }
Example #3
0
        /// <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);
                }
            }
        }