From fccd61f7f9b609d705d0800e767c84e66653e898 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Tue, 10 May 2016 21:37:06 -0700 Subject: [PATCH] + Added height mappings to CubistImageProcessor + Disabled Polygonal image import mode --- .../Solids/Imported/CubistImageProcessor.cs | 40 ++++++++++++++++--- .../Solids/Imported/ImportedImage.cs | 19 +++++---- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/OSCADSharp/OSCADSharp/Solids/Imported/CubistImageProcessor.cs b/OSCADSharp/OSCADSharp/Solids/Imported/CubistImageProcessor.cs index adddb07..3e34e14 100644 --- a/OSCADSharp/OSCADSharp/Solids/Imported/CubistImageProcessor.cs +++ b/OSCADSharp/OSCADSharp/Solids/Imported/CubistImageProcessor.cs @@ -16,6 +16,8 @@ namespace OSCADSharp.Solids.Imported #region Private Fields private int scannedRows = 0; private string imagePath; + private bool includeHeight; + private Dictionary heightMappings; List cubes = new List(); #endregion @@ -23,15 +25,16 @@ namespace OSCADSharp.Solids.Imported public Bounds ImageBounds { get; set; } #endregion - internal CubistImageProcessor(string imagePath) + internal CubistImageProcessor(string imagePath, bool includeHeight = true) { + this.includeHeight = includeHeight; this.imagePath = imagePath; } public OSCADObject ProcessImage() { this.cubes = this.processImage(); - OSCADObject obj = new OSCADObject.MultiStatementObject("union()", cubes); + OSCADObject obj = new OSCADObject.MultiStatementObject("union()", cubes); return obj.Scale(1, -1, 1).Translate(0, ImageBounds.Width, 0); } @@ -39,6 +42,7 @@ namespace OSCADSharp.Solids.Imported private List processImage() { Bitmap img = new Bitmap(Image.FromFile(this.imagePath)); + this.setHeightMappings(img); this.ImageBounds = new Bounds(new Vector3(), new Vector3(img.Width, img.Height, 1)); List cubes = new List(); @@ -55,8 +59,15 @@ namespace OSCADSharp.Solids.Imported this.markVisited(ref visited, cube, (Point)start, img); if (color.A != 0) { + + if (this.includeHeight) + { + cube.Size.Z = heightMappings[color]; + } + + string cubeColor = String.Format("[{0}, {1}, {2}]", color.R == 0 ? 0 : color.R / 255.0, color.G == 0 ? 0 : color.G / 255.0, color.B == 0 ? 0 : color.B / 255.0); cubes.Add(cube.Translate(((Point)start).X, ((Point)start).Y, 0) - .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)); + .Color(cubeColor, color.A)); } } @@ -66,6 +77,25 @@ namespace OSCADSharp.Solids.Imported return cubes; } + private void setHeightMappings(Bitmap img) + { + if (this.includeHeight) + { + this.heightMappings = new Dictionary(); + double max = 4 * 256; + + for (int x = 0; x < img.Width; x++) + { + for (int y = 0; y < img.Height; y++) + { + var color = img.GetPixel(x, y); + double csum = (double)(color.R + color.G + color.B + color.A); + heightMappings[color] = Convert.ToInt32(csum != 0 ? (csum / max) * 10: .25); + } + } + } + } + private void markVisited(ref bool[,] visited, Cube cube, Point start, Bitmap img) { var bounds = cube.Bounds(); @@ -151,8 +181,6 @@ namespace OSCADSharp.Solids.Imported } - - private Point? getNextPoint(Bitmap img, ref bool[,] visited, int width, int height) { int rowStart = this.scannedRows; @@ -171,7 +199,7 @@ namespace OSCADSharp.Solids.Imported return null; } - + #endregion } } diff --git a/OSCADSharp/OSCADSharp/Solids/Imported/ImportedImage.cs b/OSCADSharp/OSCADSharp/Solids/Imported/ImportedImage.cs index f1aab61..5d46ee0 100644 --- a/OSCADSharp/OSCADSharp/Solids/Imported/ImportedImage.cs +++ b/OSCADSharp/OSCADSharp/Solids/Imported/ImportedImage.cs @@ -24,19 +24,18 @@ namespace OSCADSharp.Solids.Imported /// Creates an imported image from the specified file /// /// - /// /// - public static ImportedImage FromFile(string imagePath, ImageImportMode mode = ImageImportMode.Cubist) + public static ImportedImage FromFile(string imagePath) { IImageProcessor processor; - if(mode == ImageImportMode.Cubist) - { - processor = new CubistImageProcessor(imagePath); - } - else - { - processor = new PolygonalImageProcessor(imagePath); - } + //if(mode == ImageImportMode.Cubist) + //{ + processor = new CubistImageProcessor(imagePath); + //} + //else + //{ + // processor = new PolygonalImageProcessor(imagePath); + //} var obj = processor.ProcessImage();