mirror of
https://github.com/eliasstepanik/OSCADSharpDotnet7.git
synced 2026-01-11 21:48:34 +00:00
More in-progress polygon-based image processing.
This commit is contained in:
parent
3a0eb13166
commit
4f962ca257
@ -15,6 +15,8 @@ namespace OSCADSharp.Solids.Imported
|
||||
private Point topLeft;
|
||||
private Point bottomRight;
|
||||
private KeyValuePair<Point, Color>?[,] grid;
|
||||
private int height;
|
||||
private int width;
|
||||
|
||||
public Point TopLeft { get { return this.topLeft; } }
|
||||
public Point BottomRight { get { return this.bottomRight; } }
|
||||
@ -22,8 +24,10 @@ namespace OSCADSharp.Solids.Imported
|
||||
internal AdjacentPixelMatrix(List<KeyValuePair<Point, Color>> pixelGrouping)
|
||||
{
|
||||
this.createGrid(pixelGrouping);
|
||||
this.height = bottomRight.Y - topLeft.Y+1;
|
||||
this.width = bottomRight.X - topLeft.X+1;
|
||||
}
|
||||
|
||||
|
||||
public bool IsOutOfBounds(Point pt)
|
||||
{
|
||||
if (pt.X < topLeft.X || pt.X > bottomRight.X || pt.Y < topLeft.Y || pt.Y > bottomRight.Y)
|
||||
@ -34,6 +38,19 @@ namespace OSCADSharp.Solids.Imported
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool IsInBoundsAndNotNull(Point pt, bool useRelativePtForNullCheck = false)
|
||||
{
|
||||
if (useRelativePtForNullCheck)
|
||||
{
|
||||
var relativePt = new Point(pt.X - this.topLeft.X, pt.Y - this.topLeft.Y);
|
||||
return !IsOutOfBounds(pt) && this.At(relativePt.X, relativePt.Y) != null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return !IsOutOfBounds(pt) && !(pt.X > this.width) && !(pt.Y > this.height) && this.At(pt.X, pt.Y) != null;
|
||||
}
|
||||
}
|
||||
|
||||
public KeyValuePair<Point, Color>? At(int x, int y)
|
||||
{
|
||||
return this.grid[x, y];
|
||||
|
||||
@ -140,27 +140,28 @@ namespace OSCADSharp.Solids.Imported
|
||||
{
|
||||
List<OSCADObject> objects = new List<OSCADObject>();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
foreach (var section in contiguousSections)
|
||||
var orderedSections = contiguousSections.OrderBy(sec => sec.Count);
|
||||
var largest = orderedSections.Last().Count;
|
||||
|
||||
foreach (var section in orderedSections)
|
||||
{
|
||||
//TODO: Reorder sections for correct polygon winding
|
||||
var color = section[0].Value;
|
||||
var orderedSection = this.getOrderedPoints(section);
|
||||
OSCADObject pgon = new Polygon(orderedSection);
|
||||
pgon = pgon.Color(String.Format("[{0}, {1}, {2}]", color.R == 0 ? 0 : color.R / 255, color.G == 0 ? 0 : color.G / 255, color.B == 0 ? 0 : color.B / 255), color.A);
|
||||
//pgon = pgon.Scale(1, 1, (1.0 / section.Count/ largest) * 100);
|
||||
objects.Add(pgon);
|
||||
|
||||
//var color = section[0].Value;
|
||||
//var orderedSection = this.getOrderedPoints(section);
|
||||
//OSCADObject pgon = new Polygon(section.Select(sec => sec.Key).ToList());
|
||||
//pgon = pgon.Color(String.Format("[{0}, {1}, {2}]", color.R == 0 ? 0 : color.R / 255, color.G == 0 ? 0 : color.G / 255, color.B == 0 ? 0 : color.B / 255), color.A);
|
||||
//objects.Add(pgon);
|
||||
|
||||
foreach (var pair in section)
|
||||
{
|
||||
var position = pair.Key;
|
||||
var color = pair.Value;
|
||||
//foreach (var pair in section)
|
||||
//{
|
||||
// var position = pair.Key;
|
||||
// var color = pair.Value;
|
||||
|
||||
|
||||
var cube = new Cube().Color(String.Format("[{0}, {1}, {2}]", color.R == 0 ? 0 : color.R / 255, color.G == 0 ? 0 : color.G / 255, color.B == 0 ? 0 : color.B / 255), color.A);
|
||||
cube = cube.Translate(position.X, position.Y, 0);
|
||||
objects.Add(cube);
|
||||
}
|
||||
// var cube = new Cube().Color(String.Format("[{0}, {1}, {2}]", color.R == 0 ? 0 : color.R / 255, color.G == 0 ? 0 : color.G / 255, color.B == 0 ? 0 : color.B / 255), color.A);
|
||||
// cube = cube.Translate(position.X, position.Y, 0);
|
||||
// objects.Add(cube);
|
||||
//}
|
||||
}
|
||||
|
||||
return objects;
|
||||
@ -173,18 +174,40 @@ namespace OSCADSharp.Solids.Imported
|
||||
var traversed = new HashSet<Point>();
|
||||
var neighborFinder = new NeighboringPointFinder(true);
|
||||
Stack<Point> toTraverse = new Stack<Point>();
|
||||
toTraverse.Push(section[0].Key);
|
||||
Point? origin = section[0].Key;
|
||||
|
||||
while(toTraverse.Count > 0)
|
||||
while(origin != null)
|
||||
{
|
||||
var origin = toTraverse.Pop();
|
||||
traversed.Add(origin);
|
||||
orderedPoints.Add(origin);
|
||||
var orig = (Point)origin;
|
||||
traversed.Add(orig);
|
||||
orderedPoints.Add(orig);
|
||||
|
||||
var below = neighborFinder.Below(origin);
|
||||
if (!grid.IsOutOfBounds(below) && grid.At(below.X, below.Y) != null)
|
||||
var below = neighborFinder.Below(orig);
|
||||
var right = neighborFinder.Right(orig);
|
||||
var above = neighborFinder.Above(orig);
|
||||
var left = neighborFinder.Left(orig);
|
||||
|
||||
|
||||
if (grid.IsInBoundsAndNotNull(above, true) && !traversed.Contains(above))
|
||||
{
|
||||
continue;
|
||||
origin = above;
|
||||
}
|
||||
else if (grid.IsInBoundsAndNotNull(right, true) && !traversed.Contains(right))
|
||||
{
|
||||
origin = right;
|
||||
}
|
||||
else if (grid.IsInBoundsAndNotNull(below, true) && !traversed.Contains(below))
|
||||
{
|
||||
origin = below;
|
||||
}
|
||||
else if (grid.IsInBoundsAndNotNull(left, true) && !traversed.Contains(left))
|
||||
{
|
||||
origin = left;
|
||||
}
|
||||
else
|
||||
{
|
||||
//TODO: Handle additional paths
|
||||
origin = null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -213,7 +236,7 @@ namespace OSCADSharp.Solids.Imported
|
||||
|
||||
private void removeCenterPixels(List<KeyValuePair<Point, Color>> section, AdjacentPixelMatrix grid)
|
||||
{
|
||||
var neighborFinder = new NeighboringPointFinder(true);
|
||||
var neighborFinder = new NeighboringPointFinder();
|
||||
|
||||
for (int i = section.Count - 1; i >= 0; i--)
|
||||
{
|
||||
@ -234,24 +257,24 @@ namespace OSCADSharp.Solids.Imported
|
||||
break;
|
||||
}
|
||||
|
||||
int x = pt.X - grid.TopLeft.X;
|
||||
int y = pt.Y - grid.TopLeft.Y;
|
||||
//int x = pt.X - grid.TopLeft.X;
|
||||
//int y = pt.Y - grid.TopLeft.Y;
|
||||
|
||||
if(grid.At(x, y) == null)
|
||||
{
|
||||
isOnanEdge = true;
|
||||
break;
|
||||
}
|
||||
//if(grid.At(x, y) == null)
|
||||
//{
|
||||
// isOnanEdge = true;
|
||||
// break;
|
||||
//}
|
||||
|
||||
if (grid.At(x, y) != null)
|
||||
{
|
||||
var nbr = (KeyValuePair<Point, Color>)grid.At(x, y);
|
||||
if(!nbr.Value.Equals(color))
|
||||
{
|
||||
isOnanEdge = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//if (grid.At(x, y) != null)
|
||||
//{
|
||||
// var nbr = (KeyValuePair<Point, Color>)grid.At(x, y);
|
||||
// if(!nbr.Value.Equals(color))
|
||||
// {
|
||||
// isOnanEdge = true;
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
if (!isOnanEdge)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user