From ca30b5c888035b311b38748e36b20d1cd2dcbc16 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Mon, 30 May 2016 23:45:27 -0700 Subject: [PATCH] Refactored height related code from CubistImageProcessor to HeightMapper.cs --- OSCADSharp/OSCADSharp/OSCADSharp.csproj | 1 + .../Utility/Images/CubistImageProcessor.cs | 50 +++---------- .../OSCADSharp/Utility/Images/HeightMapper.cs | 70 +++++++++++++++++++ 3 files changed, 79 insertions(+), 42 deletions(-) create mode 100644 OSCADSharp/OSCADSharp/Utility/Images/HeightMapper.cs diff --git a/OSCADSharp/OSCADSharp/OSCADSharp.csproj b/OSCADSharp/OSCADSharp/OSCADSharp.csproj index d04d7ac..7914d0c 100644 --- a/OSCADSharp/OSCADSharp/OSCADSharp.csproj +++ b/OSCADSharp/OSCADSharp/OSCADSharp.csproj @@ -51,6 +51,7 @@ + diff --git a/OSCADSharp/OSCADSharp/Utility/Images/CubistImageProcessor.cs b/OSCADSharp/OSCADSharp/Utility/Images/CubistImageProcessor.cs index e5a1ddf..af6325f 100644 --- a/OSCADSharp/OSCADSharp/Utility/Images/CubistImageProcessor.cs +++ b/OSCADSharp/OSCADSharp/Utility/Images/CubistImageProcessor.cs @@ -25,12 +25,12 @@ namespace OSCADSharp.Utility.Images #region Private Fields private int scannedRows = 0; private string imagePath; - private string heightMode; - private Dictionary heightMappings; + private string heightMode; List cubes = new List(); private Color[,] pixels; private bool useGrayScale; private byte simplificationAmount; + private HeightMapper htMapper; #endregion #region Internal Fields @@ -58,9 +58,11 @@ namespace OSCADSharp.Utility.Images Bitmap img = new Bitmap(Image.FromFile(this.imagePath)); this.setColorArray(img); this.simplifyColors(img); - this.setHeightMappings(img); + this.htMapper = new HeightMapper(img.Width, img.Height, pixels, this.heightMode); + this.htMapper.SetHeightMappings(); + this.ImageBounds = new Bounds(new Vector3(), new Vector3(img.Width, img.Height, 1)); - + List cubes = new List(); bool[,] visited = new bool[img.Width, img.Height]; @@ -74,14 +76,9 @@ namespace OSCADSharp.Utility.Images this.markVisited(ref visited, cube, (Point)start, img); if (color.A != 0) { - - if (this.heightMode != "None") - { - cube.Size.Z = heightMappings[color]; - } - + this.htMapper.SetHeight(color, cube); 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, this.getZTranslationForHeightMode(cube.Size.Z)) + cubes.Add(cube.Translate(((Point)start).X, ((Point)start).Y, this.htMapper.GetZTranslation(cube.Size.Z)) .Color(cubeColor, color.A)); } } @@ -92,18 +89,6 @@ namespace OSCADSharp.Utility.Images return cubes; } - private double getZTranslationForHeightMode(double cubeHeight) - { - if(this.heightMode != "Bidirectional") - { - return 0; - } - else - { - return -cubeHeight / 2; - } - } - private void simplifyColors(Bitmap img) { var simplifier = new ImageSimplifier(img.Width, img.Height, pixels); @@ -136,25 +121,6 @@ namespace OSCADSharp.Utility.Images } } - private void setHeightMappings(Bitmap img) - { - if (this.heightMode != "None") - { - 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 = pixels[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(); diff --git a/OSCADSharp/OSCADSharp/Utility/Images/HeightMapper.cs b/OSCADSharp/OSCADSharp/Utility/Images/HeightMapper.cs new file mode 100644 index 0000000..00838d1 --- /dev/null +++ b/OSCADSharp/OSCADSharp/Utility/Images/HeightMapper.cs @@ -0,0 +1,70 @@ +using OSCADSharp.Solids; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OSCADSharp.Utility.Images +{ + internal class HeightMapper + { + private int height; + private string heightMode; + private Color[,] pixels; + private int width; + private Dictionary heightMappings; + + public HeightMapper(int width, int height, Color[,] pixels, string heightMode) + { + this.width = width; + this.height = height; + this.pixels = pixels; + this.heightMode = heightMode; + } + + internal void SetHeightMappings() + { + if (this.heightMode != "None") + { + this.heightMappings = new Dictionary(); + double max = 4 * 256; + + for (int x = 0; x < this.width; x++) + { + for (int y = 0; y < this.height; y++) + { + var color = pixels[x, y]; + double csum = (double)(color.R + color.G + color.B + color.A); + heightMappings[color] = Convert.ToInt32(csum != 0 ? (csum / max) * 10 : .25); + } + } + } + } + + internal void SetHeight(Color color, Cube cube) + { + if (this.heightMode != "None") + { + cube.Size.Z = heightMappings[color]; + } + else + { + cube.Size.Z = 1.0; + } + } + + internal double GetZTranslation(double cubeHeight) + { + if (this.heightMode != "Bidirectional") + { + return 0; + } + else + { + return -cubeHeight / 2; + } + } + } +}