Added IImageProcessor, PolygonalImageProcessor

This commit is contained in:
Michael Smith 2016-05-03 23:44:16 -07:00
parent d951b5bea0
commit 3ea6f2cb08
7 changed files with 150 additions and 13 deletions

View File

@ -58,14 +58,16 @@ namespace OSCADSharp.ConsoleTests
static void Main(string[] args)
{
var img = ImportedImage.FromFile("seahawks coaster.png").Scale(1, 1, Inches.Quarter + Inches.Eigth);
var imgPos = img.Position();
var _base = new Cylinder(img.Bounds().Width + Inches.Quarter, Inches.Quarter);
img.ToFile("testImg").Open();
var rim = _base.Clone().Scale(1, 1, 1.25) - _base.Clone().Scale(.9, .9, 3.5).Translate(0, 0, -Inches.Eigth);
var coaster = img + _base.Translate(imgPos.X, imgPos.Y, 0) + rim.Translate(imgPos.X, imgPos.Y, Inches.Quarter); ;
//var imgPos = img.Position();
//var _base = new Cylinder(img.Bounds().Width + Inches.Quarter, Inches.Quarter);
//var rim = _base.Clone().Scale(1, 1, 1.25) - _base.Clone().Scale(.9, .9, 3.5).Translate(0, 0, -Inches.Eigth);
//var coaster = img + _base.Translate(imgPos.X, imgPos.Y, 0) + rim.Translate(imgPos.X, imgPos.Y, Inches.Quarter); ;
coaster.ToFile("seaImg").Open();
//coaster.ToFile("seaImg").Open();
//makePeg();

View File

@ -49,8 +49,11 @@
<Compile Include="OSCADObject.BasicTransforms.cs" />
<Compile Include="OSCADObject.Booleans.cs" />
<Compile Include="Solids\Imported\CubistImageProcessor.cs" />
<Compile Include="Solids\Imported\IImageProcessor.cs" />
<Compile Include="Solids\Imported\ImageImportMode.cs" />
<Compile Include="Solids\Imported\ImportedImage.cs" />
<Compile Include="Solids\Imported\ImportedModel.cs" />
<Compile Include="Solids\Imported\PolygonalImageProcessor.cs" />
<Compile Include="Utility\Dependencies.cs" />
<Compile Include="IO\DefaultFileInvoker.cs" />
<Compile Include="IO\DefaultFileWriter.cs" />

View File

@ -11,7 +11,7 @@ namespace OSCADSharp.Solids.Imported
/// <summary>
/// Processes a bitmap image by treating contiguous same-color regions as cubes
/// </summary>
internal class CubistImageProcessor
internal class CubistImageProcessor : IImageProcessor
{
#region Private Fields
private string imagePath;
@ -19,15 +19,15 @@ namespace OSCADSharp.Solids.Imported
#endregion
#region Internal Fields
internal Bounds ImageBounds { get; set; }
public Bounds ImageBounds { get; set; }
#endregion
public CubistImageProcessor(string imagePath)
internal CubistImageProcessor(string imagePath)
{
this.imagePath = imagePath;
}
internal OSCADObject ProcessImage()
public OSCADObject ProcessImage()
{
this.cubes = this.processImage();
OSCADObject obj = new OSCADObject.MultiStatementObject("union()", cubes);
@ -36,8 +36,9 @@ namespace OSCADSharp.Solids.Imported
return obj;
}
#region Private Methods
private List<OSCADObject> processImage()
{
Bitmap img = new Bitmap(Image.FromFile(this.imagePath));
@ -174,5 +175,8 @@ namespace OSCADSharp.Solids.Imported
return null;
}
#endregion
}
}

View File

@ -0,0 +1,15 @@
using OSCADSharp.Spatial;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Solids.Imported
{
internal interface IImageProcessor
{
OSCADObject ProcessImage();
Bounds ImageBounds { get; set; }
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Solids.Imported
{
/// <summary>
/// Ways to process imported images into 3D models
/// </summary>
public enum ImageImportMode
{
/// <summary>
/// Cubist import mode subdivides the image into rectangular sections,
/// forming the bitmap from groupings of cubes representing its pixels
/// </summary>
Cubist,
/// <summary>
/// Polygonal import model scans the image for sections that can be represented
/// by whole polygons for each contiguous area
/// </summary>
Polygonal
}
}

View File

@ -24,19 +24,30 @@ namespace OSCADSharp.Solids.Imported
/// Creates an imported image from the specified file
/// </summary>
/// <param name="imagePath"></param>
/// <param name="mode"></param>
/// <returns></returns>
public static ImportedImage FromFile(string imagePath)
public static ImportedImage FromFile(string imagePath, ImageImportMode mode = ImageImportMode.Cubist)
{
IImageProcessor processor;
if(mode == ImageImportMode.Cubist)
{
processor = new CubistImageProcessor(imagePath);
}
else
{
processor = new PolygonalImageProcessor(imagePath);
}
var processor = new CubistImageProcessor(imagePath);
var obj = processor.ProcessImage();
var img = new ImportedImage() {
var img = new ImportedImage()
{
m_Object = obj,
m_Bounds = processor.ImageBounds
};
return img;
}
#endregion

View File

@ -0,0 +1,76 @@
using OSCADSharp.Spatial;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Solids.Imported
{
/// <summary>
/// Processes a bitmap image by treating contiguous same-color regions as cubes
/// </summary>
internal class PolygonalImageProcessor : IImageProcessor
{
#region Private Fields
private string imagePath;
#endregion
#region Public Fields
public Bounds ImageBounds { get; set; }
#endregion
#region Constructors
internal PolygonalImageProcessor(string imagePath)
{
this.imagePath = imagePath;
}
#endregion
public OSCADObject ProcessImage()
{
var polygons = this.processImage();
OSCADObject obj = new OSCADObject.MultiStatementObject("union()", polygons);
obj = obj.Rotate(0, 0, 180);
obj = obj.Translate(ImageBounds.Length, ImageBounds.Width, 0);
return obj;
}
private List<OSCADObject> processImage()
{
Bitmap img = new Bitmap(Image.FromFile(this.imagePath));
if (img.Width > 200 || img.Height > 200)
{
throw new InvalidOperationException("Cannot process images larger greater than 200x200 pixels");
}
this.ImageBounds = new Bounds(new Vector3(), new Vector3(img.Width, img.Height, 1));
var separatedColors = this.separateColors(img);
throw new NotImplementedException();
}
private Dictionary<string, List<KeyValuePair<Point, Color>>> separateColors(Bitmap img)
{
var colorGroupings = new Dictionary<string, List<KeyValuePair<Point, Color>>>();
for (int column = 0; column < img.Width; column++)
{
for (int row = 0; row < img.Height; row++)
{
var color = img.GetPixel(column, row);
if (!colorGroupings.ContainsKey(color.Name))
{
colorGroupings[color.Name] = new List<KeyValuePair<Point, Color>>();
}
colorGroupings[color.Name].Add(new KeyValuePair<Point, Color>(new Point(column, row), color));
}
}
return colorGroupings;
}
}
}