diff --git a/src/Triangle.Rendering/ColorManager.cs b/src/Triangle.Rendering/ColorManager.cs index bee0b2c..14a5147 100644 --- a/src/Triangle.Rendering/ColorManager.cs +++ b/src/Triangle.Rendering/ColorManager.cs @@ -68,7 +68,7 @@ namespace TriangleNet.Rendering return colors; } - public void CreateColorDictionary(int length) + public Dictionary CreateColorDictionary(int length) { var keys = new int[length]; @@ -77,34 +77,35 @@ namespace TriangleNet.Rendering keys[i] = i; } - CreateColorDictionary(keys); + return CreateColorDictionary(keys); } - public void CreateColorDictionary(IEnumerable keys) + public Dictionary CreateColorDictionary(IEnumerable keys) { - this.ColorDictionary = new Dictionary(); + ColorDictionary = new Dictionary(); int i = 0, n = regionColors.Length; foreach (var key in keys) { - this.ColorDictionary.Add(key, regionColors[i]); + ColorDictionary.Add(key, regionColors[i]); i = (i + 1) % n; } + + return ColorDictionary; } // Change or add as many colors as you like... private static Color[] regionColors = { - Color.Transparent, - Color.FromArgb(200, 0, 255, 0), - Color.FromArgb(200, 255, 0, 0), - Color.FromArgb(200, 0, 0, 255), - Color.FromArgb(200, 0, 255, 255), - Color.FromArgb(200, 255, 255, 0), - Color.FromArgb(200, 255, 0, 255), - Color.FromArgb(200, 127, 0, 255), - Color.FromArgb(200, 0, 127, 255) + Color.FromArgb(127, 0, 255, 0), + Color.FromArgb(127, 255, 0, 0), + Color.FromArgb(127, 0, 0, 255), + Color.FromArgb(127, 0, 255, 255), + Color.FromArgb(127, 255, 255, 0), + Color.FromArgb(127, 255, 0, 255), + Color.FromArgb(127, 127, 0, 255), + Color.FromArgb(127, 0, 127, 255) }; } } diff --git a/src/Triangle.Rendering/Text/SvgImage.cs b/src/Triangle.Rendering/Text/SvgImage.cs index 5b63777..67ae375 100644 --- a/src/Triangle.Rendering/Text/SvgImage.cs +++ b/src/Triangle.Rendering/Text/SvgImage.cs @@ -7,9 +7,9 @@ namespace TriangleNet.Rendering.Text { using System; + using System.Collections.Generic; using System.IO; using System.Text; - using TriangleNet; using TriangleNet.Geometry; using TriangleNet.Meshing; using TriangleNet.Meshing.Iterators; @@ -19,7 +19,7 @@ namespace TriangleNet.Rendering.Text /// public class SvgImage { - // Iterations to insert a linebreak in SVG path. + // Iterations to insert a line break in SVG path. private const int LINEBREAK_COUNT = 10; float scale = 1f; @@ -35,7 +35,7 @@ namespace TriangleNet.Rendering.Text public static void Save(IMesh mesh, string file = null, int width = 800, bool regions = false, bool points = true) { - new SvgImage().Export(mesh, file, width); + new SvgImage().Export(mesh, file, width, regions, points); } /// @@ -44,7 +44,10 @@ namespace TriangleNet.Rendering.Text /// The current mesh. /// The SVG filename. /// The desired width of the image. - public void Export(IMesh mesh, string filename, int width) + /// Enable rendering of regions. + /// Enable rendering of points. + public void Export(IMesh mesh, string filename, int width, + bool regions = false, bool points = true) { // Check file name if (string.IsNullOrWhiteSpace(filename)) @@ -68,10 +71,10 @@ namespace TriangleNet.Rendering.Text scale = width / ((float)bounds.Width + 2 * margin); - int x_offset = -(int)((bounds.Left - margin) * scale); - int y_offset = (int)((bounds.Top + margin) * scale); + int x_offset = -(int)((bounds.Left - margin) * scale - 0.5); + int y_offset = (int)((bounds.Top + margin) * scale + 0.5); - int height = (int)((bounds.Height + 2 * margin) * scale); + int height = (int)((bounds.Height + 2 * margin) * scale + 0.5); using (var svg = new FormattingStreamWriter(filename)) { @@ -81,12 +84,15 @@ namespace TriangleNet.Rendering.Text svg.WriteLine("", x_offset, y_offset); - DrawTriangles(svg, mesh, false); + DrawTriangles(svg, mesh, regions, false); //DrawEdges(svg, mesh); DrawSegments(svg, mesh); - DrawPoints(svg, mesh, false); + if (points) + { + DrawPoints(svg, mesh, false); + } svg.WriteLine(""); @@ -94,17 +100,22 @@ namespace TriangleNet.Rendering.Text } } - private void DrawTriangles(StreamWriter svg, IMesh mesh, bool label) + private void DrawTriangles(StreamWriter svg, IMesh mesh, bool regions, bool label) { - svg.Write("\t(); Vertex v1, v2, v3; double x1, y1, x2, y2, x3, y3, xa, ya; int i = 1; + edges.Append("\t{2}", + labels.AppendFormat(format, "{2}", xa, ya, tri.ID); labels.AppendLine(); } } - svg.WriteLine("\" style=\"stroke:#c2c2c2; fill:none; stroke-linejoin:bevel;\"/>"); + edges.AppendLine("\" style=\"stroke:#c2c2c2; fill:none; stroke-linejoin:bevel;\"/>"); + + if (regions) + { + var colors = new ColorManager().CreateColorDictionary(filled.Keys); + + foreach (var r in filled) + { + var c = colors[r.Key]; + svg.Write("\t", c.R, c.G, c.B, c.A / 255f); + } + } + + svg.Write(edges.ToString()); // Label the triangles. if (label)