Update examples.

This commit is contained in:
wo80
2022-03-05 20:49:49 +01:00
parent 4484c6c31a
commit 1383b57f0a
13 changed files with 115 additions and 44 deletions
+3 -1
View File
@@ -11,7 +11,7 @@ namespace TriangleNet.Examples
/// </summary>
public class Example1
{
public static void Run(bool print = false)
public static bool Run(bool print = false)
{
// Generate points.
var points = Generate.RandomPoints(50, new Rectangle(0, 0, 100, 100));
@@ -26,6 +26,8 @@ namespace TriangleNet.Examples
var mesh = mesher.Triangulate(points);
if (print) SvgImage.Save(mesh, "example-1.svg", 500);
return true;
}
}
}
+3 -1
View File
@@ -18,7 +18,7 @@ namespace TriangleNet.Examples
/// <summary>
/// Reads all .poly files from given directory and processes them in parallel.
/// </summary>
public static void Run(string dir)
public static bool Run(string dir)
{
var files = Directory.EnumerateFiles(dir, "*.poly", SearchOption.AllDirectories);
@@ -75,6 +75,8 @@ namespace TriangleNet.Examples
{
Console.WriteLine(" Number of invalid triangulations: {0}", invalid);
}
return true;
}
private static void ProcessMesh(IMesh mesh, MeshResult result)
+3 -1
View File
@@ -9,7 +9,7 @@
/// </summary>
public static class Example2
{
public static void Run(bool print = false)
public static bool Run(bool print = false)
{
// Generate the input geometry.
var poly = CreatePolygon();
@@ -21,6 +21,8 @@
var mesh = poly.Triangulate(quality);
if (print) SvgImage.Save(mesh, "example-2.svg", 500);
return true;
}
public static IPolygon CreatePolygon(double h = 0.2)
+3 -1
View File
@@ -12,12 +12,14 @@ namespace TriangleNet.Examples
/// </summary>
public class Example3
{
public static void Run(bool print = false)
public static bool Run(bool print = false)
{
// Generate mesh.
var mesh = CreateMesh();
if (print) SvgImage.Save(mesh, "example-3.svg", 500);
return true;
}
public static IMesh CreateMesh()
+3 -1
View File
@@ -12,7 +12,7 @@ namespace TriangleNet.Examples
/// </summary>
public class Example4
{
public static void Run(bool print = false)
public static bool Run(bool print = false)
{
// Generate the input geometry.
var poly = CreatePolygon();
@@ -42,6 +42,8 @@ namespace TriangleNet.Examples
smoother.Smooth(mesh, 5);
if (print) SvgImage.Save(mesh, "example-4.svg", 500);
return true;
}
public static IPolygon CreatePolygon()
+3 -1
View File
@@ -13,7 +13,7 @@ namespace TriangleNet.Examples
/// </summary>
public static class Example5
{
public static void Run(bool print = false)
public static bool Run(bool print = false)
{
var mesh = Example3.CreateMesh();
@@ -24,6 +24,8 @@ namespace TriangleNet.Examples
FindBoundary2(mesh);
if (print) SvgImage.Save(mesh, "example-5-2.svg", 500, true, false);
return true;
}
/// <summary>
+5 -3
View File
@@ -12,14 +12,14 @@
/// </summary>
public static class Example6
{
public static void Run()
public static bool Run()
{
// Generate the input geometry.
var polygon = new Polygon(8, true);
// Two intersecting rectangles.
var A = Generate.Rectangle(0.0, 4.0, 4.0, 0.0, 1);
var B = Generate.Rectangle(1.0, 5.0, 3.0, 1.0, 2);
var A = Generate.Rectangle(0.0, 0.0, 4.0, 4.0, 1);
var B = Generate.Rectangle(1.0, 1.0, 4.0, 4.0, 2);
polygon.Add(A);
polygon.Add(B);
@@ -46,6 +46,8 @@
// The xor of A and B.
var xor = mesh.Triangles.Where(t => t.Label == 1 || t.Label == 2);
return true;
}
}
}
+4 -2
View File
@@ -14,12 +14,12 @@ namespace TriangleNet.Examples
{
const double MAX_EDGE_LENGTH = 0.2;
public static void Run(bool print = false)
public static bool Run(bool print = false)
{
var poly = new Polygon();
// Generate the input geometry.
poly.Add(Generate.Rectangle(0.0, 1.0, 1.0, 0.0));
poly.Add(Generate.Rectangle(0.0, 0.0, 1.0, 1.0));
// Set minimum angle quality option, ignoring holes.
var quality = new QualityOptions()
@@ -42,6 +42,8 @@ namespace TriangleNet.Examples
}
if (print) SvgImage.Save(mesh, "example-7.svg", 500);
return true;
}
static bool MaxEdgeLength(ITriangle tri, double area)
+3 -1
View File
@@ -12,11 +12,13 @@ namespace TriangleNet.Examples
/// </summary>
public class Example8
{
public static void Run()
public static bool Run()
{
var mesh = (Mesh)Example3.CreateMesh();
FindAdjacencyMatrix(mesh);
return true;
}
private static void FindAdjacencyMatrix(Mesh mesh)
+3 -1
View File
@@ -11,7 +11,7 @@ namespace TriangleNet.Examples
/// </summary>
public class Example9
{
public static void Run(bool print = false)
public static bool Run(bool print = false)
{
var pts = new List<Vertex>
{
@@ -56,6 +56,8 @@ namespace TriangleNet.Examples
// Random rotation.
poly = Rotate(pts, Math.PI * r.NextDouble());
}
return true;
}
/// <summary>
+61 -13
View File
@@ -7,10 +7,9 @@ namespace TriangleNet
static class Generate
{
private const int RANDOM_SEED = 63841;
private static readonly Random random = new Random(63841);
public static List<Vertex> RandomPoints(int n, Rectangle bounds,
int seed = RANDOM_SEED)
public static List<Vertex> RandomPoints(int n, Rectangle bounds)
{
var points = new List<Vertex>(n);
@@ -20,8 +19,6 @@ namespace TriangleNet
var width = bounds.Width;
var height = bounds.Height;
var random = new Random(seed);
for (int i = 0; i < n; i++)
{
double x = random.NextDouble();
@@ -32,17 +29,68 @@ namespace TriangleNet
return points;
}
public static Contour Rectangle(double left, double top,
double right, double bottom, int mark = 0)
/// <summary>
/// Creates a rectangle contour.
/// </summary>
public static Contour Rectangle(Rectangle rect, double size = 0d, int label = 0)
{
var points = new List<Vertex>(4);
return Rectangle(rect.X, rect.Y, rect.Width, rect.Height, size, label);
}
points.Add(new Vertex(left, top, mark));
points.Add(new Vertex(right, top, mark));
points.Add(new Vertex(right, bottom, mark));
points.Add(new Vertex(left, bottom, mark));
/// <summary>
/// Creates a rectangle contour.
/// </summary>
public static Contour Rectangle(double x, double y, double width, double height,
double size = 0d, int label = 0)
{
// Horizontal and vertical step sizes.
double stepH = 0d;
double stepV = 0d;
return new Contour(points, mark, true);
int nH = 1;
int nV = 1;
if (size > 0d)
{
size = Math.Min(size, Math.Min(width, height));
nH = (int)Math.Ceiling(width / size);
nV = (int)Math.Ceiling(height / size);
stepH = width / nH;
stepV = height / nV;
}
var points = new List<Vertex>(2 * nH + 2 * nV);
double right = x + width;
double top = y + height;
// Left box boundary points
for (int i = 0; i < nV; i++)
{
points.Add(new Vertex(x, y + i * stepV, label));
}
// Top box boundary points
for (int i = 0; i < nH; i++)
{
points.Add(new Vertex(x + i * stepH, top, label));
}
// Right box boundary points
for (int i = 0; i < nV; i++)
{
points.Add(new Vertex(right, top - i * stepV, label));
}
// Bottom box boundary points
for (int i = 0; i < nH; i++)
{
points.Add(new Vertex(right - i * stepH, y, label));
}
return new Contour(points, label, true);
}
/// <summary>
+20 -9
View File
@@ -1,21 +1,32 @@
namespace TriangleNet
{
using System;
using TriangleNet.Examples;
class Program
{
static void Main(string[] args)
{
Example1.Run();
Example2.Run();
Example3.Run();
Example4.Run();
Example5.Run();
Example8.Run();
Example6.Run();
Example7.Run();
Example9.Run();
Check("Example 1", Example1.Run());
Check("Example 2", Example2.Run());
Check("Example 3", Example3.Run());
Check("Example 4", Example4.Run());
Check("Example 5", Example5.Run());
Check("Example 6", Example8.Run());
Check("Example 7", Example6.Run());
Check("Example 8", Example7.Run());
Check("Example 9", Example9.Run());
}
static void Check(string item, bool success)
{
var color = Console.ForegroundColor;
Console.Write(item + " ");
Console.ForegroundColor = success ? ConsoleColor.DarkGreen : ConsoleColor.DarkRed;
Console.WriteLine(success ? "OK" : "Failed");
Console.ForegroundColor = color;
}
}
}
@@ -6,17 +6,9 @@
<RootNamespace>TriangleNet</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Triangle.Rendering\Text\FormattingStreamWriter.cs" Link="Rendering\FormattingStreamWriter.cs" />
<Compile Include="..\Triangle.Rendering\Text\SvgImage.cs" Link="Rendering\SvgImage.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Rendering\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Triangle\Triangle.csproj" />
<ProjectReference Include="..\Triangle.Rendering\Triangle.Rendering.csproj" />
</ItemGroup>
</Project>