public static Bitmap[,] ReconstructFromExample(int ndx, List <Bitmap> bitmaps, int size) { var example = bitmaps[ndx]; var width = example.Width; var height = example.Height; var edgeTypes = Enum.GetValues(typeof(EdgeType)).Cast <EdgeType>(); var templates = edgeTypes.Select(x => new EdgeTemplate(x, width, height)).ToList(); var edges = bitmaps.SelectMany(bmp => templates.Select(tmpl => new Edge(bmp, tmpl))) .Where(GoodEdge) .ToList(); var result = new Bitmap[size * 2 + 3, size * 2 + 3]; var edgeCol = new EdgeCollection(edges); result[size + 1, size + 1] = example; ReconstructFrom(result, templates, size + 1, size + 1, edgeCol); return(result); }
private static bool TryReconstructFrom(Bitmap[,] result, List<EdgeTemplate> templates, int x, int y, EdgeCollection edges) { var current = result[x, y]; var currentEdges = templates.Select(tmpl => new Edge(current, tmpl)); foreach (var edge in edges.Edges) { foreach (var curr in currentEdges) { var dir = Edge.ToDirection(curr.Type); var x2 = x + dir.X; var y2 = y + dir.Y; if (result[x2, y2] == null && curr.Matches(edge) && MatchesInPosition(result, templates, edge.Source, x2, y2)) { result[x2, y2] = edge.Source; ReconstructFrom(result, templates, x2, y2, edges); return true; } } } return false; }
private static void ReconstructFrom(Bitmap[,] result, List<EdgeTemplate> templates, int x, int y, EdgeCollection edges) { var current = result[x, y]; edges.RemoveBySource(current); while (TryReconstructFrom(result, templates, x, y, edges)) ; }
public static Bitmap[,] ReconstructFromExample(int ndx, List<Bitmap> bitmaps, int size) { var example = bitmaps[ndx]; var width = example.Width; var height = example.Height; var edgeTypes = Enum.GetValues(typeof(EdgeType)).Cast<EdgeType>(); var templates = edgeTypes.Select(x => new EdgeTemplate(x, width, height)).ToList(); var edges = bitmaps.SelectMany(bmp => templates.Select(tmpl => new Edge(bmp, tmpl))) .Where(GoodEdge) .ToList(); var result = new Bitmap[size * 2 + 3, size * 2 + 3]; var edgeCol = new EdgeCollection(edges); result[size + 1, size + 1] = example; ReconstructFrom(result, templates, size + 1, size + 1, edgeCol); return result; }
private static bool TryReconstructFrom(Bitmap[,] result, List <EdgeTemplate> templates, int x, int y, EdgeCollection edges) { var current = result[x, y]; var currentEdges = templates.Select(tmpl => new Edge(current, tmpl)); foreach (var edge in edges.Edges) { foreach (var curr in currentEdges) { var dir = Edge.ToDirection(curr.Type); var x2 = x + dir.X; var y2 = y + dir.Y; if (result[x2, y2] == null && curr.Matches(edge) && MatchesInPosition(result, templates, edge.Source, x2, y2)) { result[x2, y2] = edge.Source; ReconstructFrom(result, templates, x2, y2, edges); return(true); } } } return(false); }
private static void ReconstructFrom(Bitmap[,] result, List <EdgeTemplate> templates, int x, int y, EdgeCollection edges) { var current = result[x, y]; edges.RemoveBySource(current); while (TryReconstructFrom(result, templates, x, y, edges)) { ; } }