Minor updates to Triangle.Rendering code.

This commit is contained in:
wo80
2022-02-14 14:01:35 +01:00
parent 66197a01e8
commit a67b518b2d
5 changed files with 144 additions and 5 deletions
+136 -3
View File
@@ -25,11 +25,44 @@ namespace TriangleNet.Rendering.GDI
public bool EnableRegions { get; set; }
public bool EnablePoints { get; set; }
/// <summary>
/// Export the mesh to PNG format.
/// Exports a polygon to PNG format.
/// </summary>
/// <param name="mesh">The current mesh.</param>
/// <param name="poly">The polygon.</param>
/// <param name="width">The desired width (pixel) of the image.</param>
/// <param name="file">The PNG filename.</param>
/// <param name="regions">Enable rendering of regions.</param>
/// <param name="points">Enable rendering of points.</param>
public static void Save(Geometry.IPolygon poly, string file = null, int width = 800,
bool points = true)
{
// Check file name
if (string.IsNullOrWhiteSpace(file))
{
file = string.Format("poly-{0}.png", DateTime.Now.ToString("yyyy-M-d-hh-mm-ss"));
}
// Ensure .png extension.
if (file.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
{
Path.ChangeExtension(file, ".png");
}
var renderer = new ImageRenderer();
renderer.EnableRegions = false;
renderer.EnablePoints = points;
var bitmap = renderer.Render(poly, width);
bitmap.Save(file, ImageFormat.Png);
}
/// <summary>
/// Exports a mesh to PNG format.
/// </summary>
/// <param name="mesh">The mesh.</param>
/// <param name="width">The desired width (pixel) of the image.</param>
/// <param name="file">The PNG filename.</param>
/// <param name="regions">Enable rendering of regions.</param>
@@ -59,6 +92,58 @@ namespace TriangleNet.Rendering.GDI
bitmap.Save(file, ImageFormat.Png);
}
/// <summary>
/// Renders the polygon to a bitmap.
/// </summary>
/// <param name="poly">The polygon.</param>
/// <param name="width">The desired width (pixel) of the image.</param>
/// <returns>The bitmap.</returns>
/// <remarks>
/// The width has to be at least 2 * sqrt(n), n the number of vertices.
/// Otherwise, an empty bitmap
/// </remarks>
public Bitmap Render(Geometry.IPolygon poly, int width = 800)
{
Bitmap bitmap;
// Check if the specified width is reasonable
if (width < 2 * Math.Sqrt(poly.Points.Count))
{
return new Bitmap(1, 1);
}
var bounds = poly.Bounds();
// World margin on each side
float margin = (float)bounds.Height * 0.05f;
float scale = width / ((float)bounds.Width + 2 * margin);
var target = new Rectangle(0, 0, width, (int)((bounds.Height + 2 * margin) * scale));
bitmap = new Bitmap(width, target.Height, PixelFormat.Format32bppPArgb);
using (var g = Graphics.FromImage(bitmap))
{
g.Clear(colors.Background);
g.SmoothingMode = SmoothingMode.HighQuality;
var context = new RenderContext(new Projection(target), colors);
context.Add(poly);
if (!EnablePoints)
{
context.Enable(3, false);
}
var renderer = new LayerRenderer();
renderer.Context = context;
renderer.RenderTarget = g;
renderer.Render();
}
return bitmap;
}
/// <summary>
/// Renders the mesh to a bitmap.
/// </summary>
@@ -116,6 +201,54 @@ namespace TriangleNet.Rendering.GDI
return bitmap;
}
public Bitmap Render(IMesh mesh, Topology.DCEL.DcelMesh dcel, int width = 800)
{
Bitmap bitmap;
// Check if the specified width is reasonable
if (width < 2 * Math.Sqrt(mesh.Vertices.Count))
{
return new Bitmap(1, 1);
}
var bounds = mesh.Bounds;
// World margin on each side
float margin = (float)bounds.Height * 0.05f;
float scale = width / ((float)bounds.Width + 2 * margin);
var target = new Rectangle(0, 0, width, (int)((bounds.Height + 2 * margin) * scale));
bitmap = new Bitmap(width, target.Height, PixelFormat.Format32bppPArgb);
using (var g = Graphics.FromImage(bitmap))
{
g.Clear(colors.Background);
g.SmoothingMode = SmoothingMode.HighQuality;
var context = new RenderContext(new Projection(target), colors);
context.Add(mesh, true);
context.Add(dcel.Vertices.ToArray(), dcel.Edges, false);
if (EnableRegions)
{
context.Add(GetRegions(mesh));
}
if (!EnablePoints)
{
context.Enable(3, false);
}
var renderer = new LayerRenderer();
renderer.Context = context;
renderer.RenderTarget = g;
renderer.Render();
}
return bitmap;
}
private int[] GetRegions(IMesh mesh)
{
mesh.Renumber();
+2
View File
@@ -28,5 +28,7 @@ namespace TriangleNet.Rendering
void Add(int[] partition);
void Enable(int layer, bool enabled);
void Clear();
}
}
-1
View File
@@ -13,7 +13,6 @@ namespace TriangleNet.Rendering
{
int Count { get; }
// Points can be set, because layers may share vertices.
IBuffer<float> Points { get; }
IBuffer<int> Indices { get; }
-1
View File
@@ -32,7 +32,6 @@ namespace TriangleNet.Rendering
public IBuffer<float> Points
{
get { return points; }
set { points = value; }
}
public IBuffer<int> Indices
+6
View File
@@ -77,6 +77,12 @@ namespace TriangleNet.Rendering
control.HandleResize();
}
public void Clear()
{
context.Clear();
control.Refresh();
}
public void Enable(int layer, bool enabled)
{
context.Enable(layer, enabled);