Triangle.Rendering simplify IRenderLayer and buffer handling.

This commit is contained in:
wo80
2022-03-01 16:02:22 +01:00
parent e35b465330
commit 80428bfa40
7 changed files with 131 additions and 281 deletions
-68
View File
@@ -1,68 +0,0 @@
namespace TriangleNet.Rendering
{
using System.Drawing;
public class BoundingBox
{
public float Left;
public float Right;
public float Bottom;
public float Top;
public float Width
{
get { return Right - Left; }
}
public float Height
{
get { return Top - Bottom; }
}
public BoundingBox()
{
Reset();
}
public BoundingBox(float left, float right, float bottom, float top)
{
Left = left;
Right = right;
Bottom = bottom;
Top = top;
}
public void Update(Point pt)
{
Update(pt.X, pt.Y);
}
public void Update(PointF pt)
{
Update(pt.X, pt.Y);
}
public void Update(double x, double y)
{
Update((float)x, (float)y);
}
public void Update(float x, float y)
{
// Update bounding box
if (Left > x) Left = x;
if (Right < x) Right = x;
if (Bottom > y) Bottom = y;
if (Top < y) Top = y;
}
public void Reset()
{
Left = float.MaxValue;
Right = -float.MaxValue;
Bottom = float.MaxValue;
Top = -float.MaxValue;
}
}
}
@@ -1,8 +1,56 @@
namespace TriangleNet.Rendering.Buffer
{
using System.Collections.Generic;
using System.Linq;
using TriangleNet.Geometry;
using TriangleNet.Topology;
public class IndexBuffer : BufferBase<int>
{
#region Static methods
public static IBuffer<int> Create(IEnumerable<IEdge> edges, int size)
{
var buffer = new IndexBuffer(size * edges.Count(), size);
var data = buffer.Data;
int i = 0;
foreach (var e in edges)
{
data[size * i + 0] = e.P0;
data[size * i + 1] = e.P1;
i++;
}
return buffer;
}
public static IBuffer<int> Create(ICollection<Triangle> elements, int size)
{
var buffer = new IndexBuffer(size * elements.Count, size);
var data = buffer.Data;
int i = 0;
foreach (var e in elements)
{
data[size * i + 0] = e.GetVertexID(0);
data[size * i + 1] = e.GetVertexID(1);
data[size * i + 2] = e.GetVertexID(2);
i++;
}
return buffer;
}
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="IndexBuffer"/> class.
/// </summary>
@@ -1,8 +1,70 @@
namespace TriangleNet.Rendering.Buffer
{
using System.Collections.Generic;
using TriangleNet.Geometry;
public class VertexBuffer : BufferBase<float>
{
#region Static methods
/// <summary>
/// Create a vertex buffer from given point collection.
/// </summary>
/// <param name="points">The points to render.</param>
/// <param name="bounds">The points bounding box.</param>
/// <returns></returns>
public static IBuffer<float> Create(ICollection<Point> points, Rectangle bounds)
{
var buffer = new VertexBuffer(2 * points.Count);
var data = buffer.Data;
float x, y;
int i = 0;
foreach (var p in points)
{
x = (float)p.X;
y = (float)p.Y;
data[2 * i] = x;
data[2 * i + 1] = y;
i++;
}
return buffer;
}
/// <summary>
/// Create a vertex buffer from given vertex collection.
/// </summary>
/// <param name="points">The vertices to render.</param>
/// <param name="bounds">The vertices bounding box.</param>
/// <returns></returns>
public static IBuffer<float> Create(ICollection<Vertex> points, Rectangle bounds)
{
var buffer = new VertexBuffer(2 * points.Count);
var data = buffer.Data;
int i = 0;
foreach (var p in points)
{
data[2 * i] = (float)p.X;
data[2 * i + 1] = (float)p.Y;
i++;
}
return buffer;
}
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="VertexBuffer"/> class.
/// </summary>
+2 -57
View File
@@ -1,9 +1,6 @@
namespace TriangleNet.Rendering
{
using System.Collections.Generic;
using TriangleNet.Geometry;
using TriangleNet.Meshing;
using TriangleNet.Rendering.Buffer;
using TriangleNet.Rendering.Util;
@@ -46,61 +43,9 @@ namespace TriangleNet.Rendering
/// <param name="clear">If true, all buffers will be set to <c>null</c>.</param>
void Reset(bool clear);
// TODO: add boolean: reset
void SetPoints(IBuffer<float> buffer, bool reset = true);
/// <summary>
/// Replaces the current points with the given buffer.
/// </summary>
/// <param name="buffer">The new points buffer.</param>
void SetPoints(IBuffer<float> buffer);
/// <summary>
/// Copy the points of the given <see cref="IPolygon"/> to the layers point buffer.
/// </summary>
/// <param name="poly">The polygon to render.</param>
void SetPoints(IPolygon poly);
/// <summary>
/// Copy the points of the given <see cref="IMesh"/> to the layers point buffer.
/// </summary>
/// <param name="mesh">The mesh to render.</param>
void SetPoints(IMesh mesh);
/// <summary>
/// Copy the points of the given collection to the layers point buffer.
/// </summary>
/// <param name="points">The point set to render.</param>
void SetPoints(ICollection<Point> points);
/// <summary>
/// Copy the segment indices of the given polygon to the layers index buffer.
/// </summary>
/// <param name="poly">The polygon to render.</param>
void SetPolygon(IPolygon poly);
/// <summary>
/// Copy the segment indices of the given mesh to the layers index buffer.
/// </summary>
/// <param name="mesh">The mesh to render.</param>
void SetPolygon(IMesh mesh);
/// <summary>
/// Copy the indices of the given mesh triangles to the layers index buffer.
/// </summary>
/// <param name="mesh">The mesh to render.</param>
/// <param name="elements">If true, all triangle indices are copied. Otherwise,
/// only edge indices are copied.</param>
/// <remarks>
/// Use <c>elements = true</c> for layers rendering filled triangles (3 indices per buffer item).
/// Use <c>elements = false</c> if only edges are rendered (wireframe, 2 indices per buffer item).
/// </remarks>
void SetMesh(IMesh mesh, bool elements);
/// <summary>
/// Copy the indices of the given edges to the layers index buffer.
/// </summary>
/// <param name="edges">The edges to render.</param>
void SetMesh(IEnumerable<IEdge> edges);
void SetIndices(IBuffer<int> buffer);
#region Attached data (mesh partitioning and heat map rendering)
+15 -10
View File
@@ -5,6 +5,7 @@ namespace TriangleNet.Rendering
using System.Linq;
using TriangleNet.Geometry;
using TriangleNet.Meshing;
using TriangleNet.Rendering.Buffer;
/// <summary>
/// The RenderContext class brings all the rendering parts together.
@@ -76,8 +77,9 @@ namespace TriangleNet.Rendering
zoom.Initialize(bounds);
RenderLayers[2].SetPoints(data);
RenderLayers[2].SetPolygon(data);
RenderLayers[2].SetPoints(VertexBuffer.Create(data.Points, bounds));
RenderLayers[2].SetIndices(IndexBuffer.Create(data.Segments, 2));
RenderLayers[3].SetPoints(RenderLayers[2].Points);
}
@@ -96,22 +98,25 @@ namespace TriangleNet.Rendering
mesh = data;
bounds = data.Bounds;
// Ensure linear numbering of vertices.
mesh.Renumber();
zoom.Initialize(bounds);
RenderLayers[1].SetPoints(data);
RenderLayers[1].SetMesh(data, false);
RenderLayers[1].SetPoints(VertexBuffer.Create(data.Vertices, bounds));
RenderLayers[1].SetIndices(IndexBuffer.Create(data.Edges, 2));
RenderLayers[2].SetPoints(RenderLayers[1].Points);
RenderLayers[2].SetPolygon(data);
RenderLayers[2].SetIndices(IndexBuffer.Create(data.Segments, 2));
RenderLayers[3].SetPoints(RenderLayers[1].Points);
RenderLayers[3].SetPoints(RenderLayers[1].Points, false);
}
/// <inheritdoc />
public void Add(ICollection<Point> points, IEnumerable<IEdge> edges, bool reset)
{
RenderLayers[4].SetPoints(points);
RenderLayers[4].SetMesh(edges);
RenderLayers[4].SetPoints(VertexBuffer.Create(points, bounds));
RenderLayers[4].SetIndices(IndexBuffer.Create(edges, 2));
RenderLayers[4].IsEnabled = true;
}
@@ -120,7 +125,7 @@ namespace TriangleNet.Rendering
{
// Add function values for filled mesh.
RenderLayers[0].SetPoints(RenderLayers[1].Points);
RenderLayers[0].SetMesh(this.mesh, true);
RenderLayers[0].SetIndices(IndexBuffer.Create(mesh.Triangles, 3));
RenderLayers[0].AttachLayerData(data, colorManager.ColorMap);
RenderLayers[0].IsEnabled = true;
@@ -131,7 +136,7 @@ namespace TriangleNet.Rendering
{
// Add partition data for filled mesh.
RenderLayers[0].SetPoints(RenderLayers[1].Points);
RenderLayers[0].SetMesh(this.mesh, true);
RenderLayers[0].SetIndices(IndexBuffer.Create(mesh.Triangles, 3));
RenderLayers[0].AttachLayerData(data);
RenderLayers[0].IsEnabled = true;
+4 -52
View File
@@ -63,9 +63,9 @@ namespace TriangleNet.Rendering
}
/// <inheritdoc />
public void SetPoints(IBuffer<float> buffer)
public void SetPoints(IBuffer<float> buffer, bool reset = true)
{
if (points != null && points.Count < buffer.Count)
if (!reset && points != null && points.Count < buffer.Count)
{
// NOTE: we keep the old size to be able to render new Steiner
// points in a different color than existing points.
@@ -80,57 +80,9 @@ namespace TriangleNet.Rendering
}
/// <inheritdoc />
public void SetPoints(IPolygon poly)
public void SetIndices(IBuffer<int> buffer)
{
points = BufferHelper.CreateVertexBuffer(poly.Points);
count = points.Count / points.Size;
}
/// <inheritdoc />
public void SetPoints(IMesh mesh)
{
points = BufferHelper.CreateVertexBuffer(mesh.Vertices);
count = points.Count / points.Size;
}
/// <inheritdoc />
public void SetPoints(ICollection<Point> vertices)
{
points = BufferHelper.CreateVertexBuffer(vertices);
count = points.Count / points.Size;
}
/// <inheritdoc />
public void SetPolygon(IPolygon poly)
{
indices = BufferHelper.CreateIndexBuffer(poly.Segments, 2);
}
public void SetPolygon(IMesh mesh)
{
indices = BufferHelper.CreateIndexBuffer(mesh.Segments, 2);
}
/// <inheritdoc />
public void SetMesh(IEnumerable<IEdge> edges)
{
indices = BufferHelper.CreateIndexBuffer(edges, 2);
}
/// <inheritdoc />
public void SetMesh(IMesh mesh, bool elements)
{
mesh.Renumber();
if (!elements)
{
indices = BufferHelper.CreateIndexBuffer(mesh.Edges, 2);
}
if (elements || indices.Count == 0)
{
indices = BufferHelper.CreateIndexBuffer(mesh.Triangles, 3);
}
indices = buffer;
}
/// <inheritdoc />
@@ -1,94 +0,0 @@
namespace TriangleNet.Rendering.Util
{
using System.Collections.Generic;
using TriangleNet.Topology;
using TriangleNet.Geometry;
using TriangleNet.Rendering.Buffer;
using System.Linq;
internal static class BufferHelper
{
public static IBuffer<float> CreateVertexBuffer(ICollection<Point> points)
{
var buffer = new VertexBuffer(2 * points.Count);
var data = buffer.Data;
float x, y;
int i = 0;
foreach (var p in points)
{
x = (float)p.X;
y = (float)p.Y;
data[2 * i] = x;
data[2 * i + 1] = y;
i++;
}
return buffer;
}
public static IBuffer<float> CreateVertexBuffer(ICollection<Vertex> points)
{
var buffer = new VertexBuffer(2 * points.Count);
var data = buffer.Data;
int i = 0;
foreach (var p in points)
{
data[2 * i] = (float)p.X;
data[2 * i + 1] = (float)p.Y;
i++;
}
return buffer;
}
public static IBuffer<int> CreateIndexBuffer(IEnumerable<IEdge> edges, int size)
{
var buffer = new IndexBuffer(size * edges.Count(), size);
var data = buffer.Data;
int i = 0;
foreach (var e in edges)
{
data[size * i + 0] = e.P0;
data[size * i + 1] = e.P1;
i++;
}
return buffer;
}
public static IBuffer<int> CreateIndexBuffer(ICollection<Triangle> elements, int size)
{
var buffer = new IndexBuffer(size * elements.Count, size);
var data = buffer.Data;
int i = 0;
foreach (var e in elements)
{
data[size * i + 0] = e.GetVertexID(0);
data[size * i + 1] = e.GetVertexID(1);
data[size * i + 2] = e.GetVertexID(2);
i++;
}
return buffer;
}
}
}