Add examples project.
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
|
||||
namespace TriangleNet.Examples
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TriangleNet;
|
||||
using TriangleNet.Meshing.Iterators;
|
||||
using TriangleNet.Tools;
|
||||
|
||||
/// <summary>
|
||||
/// Compute the adjacency matrix of the mesh vertices.
|
||||
/// </summary>
|
||||
public class Example8
|
||||
{
|
||||
public static void Run()
|
||||
{
|
||||
var mesh = (Mesh)Example3.CreateMesh();
|
||||
|
||||
FindAdjacencyMatrix(mesh);
|
||||
}
|
||||
|
||||
private static void FindAdjacencyMatrix(Mesh mesh)
|
||||
{
|
||||
mesh.Renumber();
|
||||
|
||||
var ap = new List<int>(mesh.Vertices.Count); // Column pointers.
|
||||
var ai = new List<int>(4 * mesh.Vertices.Count); // Row indices.
|
||||
|
||||
var circulator = new VertexCirculator(mesh);
|
||||
|
||||
int k = 0;
|
||||
|
||||
foreach (var vertex in mesh.Vertices)
|
||||
{
|
||||
var star = circulator.EnumerateVertices(vertex);
|
||||
|
||||
ap.Add(k);
|
||||
|
||||
// Each vertex is adjacent to itself.
|
||||
ai.Add(vertex.ID);
|
||||
k++;
|
||||
|
||||
foreach (var item in star)
|
||||
{
|
||||
ai.Add(item.ID);
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
ap.Add(k);
|
||||
|
||||
var matrix1 = new AdjacencyMatrix(ap.ToArray(), ai.ToArray());
|
||||
var matrix2 = new AdjacencyMatrix(mesh);
|
||||
|
||||
// Column pointers should be exactly the same.
|
||||
if (!CompareArray(matrix1.ColumnPointers, matrix2.ColumnPointers))
|
||||
{
|
||||
Console.WriteLine("Something's wrong in here ...");
|
||||
}
|
||||
}
|
||||
|
||||
private static bool CompareArray(int[] a, int[] b)
|
||||
{
|
||||
int length = a.Length;
|
||||
|
||||
if (b.Length != length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
if (a[i] != b[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user