diff --git a/OSCADSharp/OSCADSharp/Solids/Imported/PolygonalImageProcessor.cs b/OSCADSharp/OSCADSharp/Solids/Imported/PolygonalImageProcessor.cs index 0055d6c..a810ba7 100644 --- a/OSCADSharp/OSCADSharp/Solids/Imported/PolygonalImageProcessor.cs +++ b/OSCADSharp/OSCADSharp/Solids/Imported/PolygonalImageProcessor.cs @@ -62,27 +62,37 @@ namespace OSCADSharp.Solids.Imported private List>> getContiguousSections(List> colorGrouping) { - KeyValuePair[,] grid = createGrid(colorGrouping); + Point topLeft; + Point bottomRight; + KeyValuePair?[,] grid = createGrid(colorGrouping, out topLeft, out bottomRight); var sections = new List>>(); while (colorGrouping.Count > 0) { var origin = colorGrouping[0]; colorGrouping.RemoveAt(0); - sections.Add(this.getNeighbors(origin, grid, colorGrouping)); + sections.Add(this.getNeighbors(origin, grid, colorGrouping, topLeft, bottomRight)); } return sections; } - private List> getNeighbors(KeyValuePair origin, KeyValuePair[,] grid, List> colorGrouping) + private List> getNeighbors(KeyValuePair origin, KeyValuePair?[,] grid, + List> colorGrouping, Point topLeft, Point bottomRight) { + List> neighbors = new List>(); + HashSet> traversed = new HashSet>(); Queue> nextOrigins = new Queue>(); nextOrigins.Enqueue(origin); + traversed.Add(origin); + neighbors.Add(origin); - while(nextOrigins.Count > 0) + while (nextOrigins.Count > 0) { + origin = nextOrigins.Dequeue(); + colorGrouping.Remove(origin); + List neighboringPoints = new List() { new Point(origin.Key.X, origin.Key.Y + 1), //Above new Point(origin.Key.X, origin.Key.Y - 1), //Below @@ -96,17 +106,36 @@ namespace OSCADSharp.Solids.Imported foreach (var pt in neighboringPoints) { - //Todo: Find neighboring points + //Ignore if out of bounds + if(pt.X < topLeft.X || pt.X > bottomRight.X || pt.Y < topLeft.Y || pt.Y > bottomRight.Y) + { + continue; + } + + int x = pt.X - topLeft.X; + int y = pt.Y - topLeft.Y; + + if(grid[x, y] != null) + { + var nbr = (KeyValuePair)grid[x, y]; + if (!traversed.Contains(nbr) && nbr.Value.Equals(origin.Value)) + { + colorGrouping.Remove(nbr); + nextOrigins.Enqueue(nbr); + neighbors.Add(nbr); + traversed.Add(nbr); + } + } } } return neighbors; } - private static KeyValuePair[,] createGrid(List> colorGrouping) + private static KeyValuePair?[,] createGrid(List> colorGrouping, out Point topLeft, out Point bottomRight) { - Point topLeft = new Point(int.MaxValue, int.MaxValue); - Point bottomRight = new Point(int.MinValue, int.MinValue); + topLeft = new Point(int.MaxValue, int.MaxValue); + bottomRight = new Point(int.MinValue, int.MinValue); foreach (var pair in colorGrouping) @@ -124,9 +153,17 @@ namespace OSCADSharp.Solids.Imported bottomRight.Y = pair.Key.Y; } - int width = bottomRight.X - topLeft.X; - int height = bottomRight.Y - topLeft.Y; - return new KeyValuePair[width, height]; + int width = bottomRight.X - topLeft.X + 1; + int height = bottomRight.Y - topLeft.Y + 1; + var grid = new KeyValuePair?[width, height]; + + foreach (var pair in colorGrouping) + { + var pt = pair.Key; + grid[pt.X - topLeft.X, pt.Y - topLeft.Y] = pair; + } + + return grid; } private Dictionary>> separateColors(Bitmap img)