121 lines
3.4 KiB
C#
121 lines
3.4 KiB
C#
// -----------------------------------------------------------------------------
|
|
// file="TriangulatedSurface"
|
|
// Project: StgSharp
|
|
// Copyright (c) Nitload.
|
|
// SPDX-License-Identifier: MIT
|
|
// -----------------------------------------------------------------------------
|
|
|
|
using StgSharp.Mathematics;
|
|
using StgSharp.Mathematics;
|
|
using StgSharp.Mathematics.Graphics;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
using System.Runtime.Intrinsics;
|
|
using System.Text;
|
|
|
|
namespace StgSharp.Geometries
|
|
{
|
|
public class TriangulatedSurface
|
|
{
|
|
|
|
private HashSet<(int begin,int end)> _sides;
|
|
|
|
private List<List<int>> _sideIndices;
|
|
private List<int> _triangleIndices;
|
|
private List<Vec3> _vertices;
|
|
|
|
public TriangulatedSurface()
|
|
{
|
|
_triangleIndices = new List<int>();
|
|
_vertices = new List<Vec3>();
|
|
_sides = new HashSet<(int,int)>();
|
|
_sideIndices = new List<List<int>>();
|
|
}
|
|
|
|
/**/
|
|
public IEnumerable<List<int>> SideIndicesEnumeratble => _sideIndices;
|
|
|
|
/*
|
|
public Triangle GetTriangle( int index )
|
|
{
|
|
Vec3HP v1,v2,v3;
|
|
int t = index * 3;
|
|
v1 = _vertices[ t + 0 ];
|
|
v2 = _vertices[ t + 1 ];
|
|
v3 = _vertices[ t + 2 ];
|
|
return new Triangle( v1, v2, v3 );
|
|
}
|
|
/**/
|
|
|
|
public Span<Vec3> VertexSpan => CollectionsMarshal.AsSpan(_vertices);
|
|
|
|
public Span<int> TriangleIndicesSpan => CollectionsMarshal.AsSpan(_triangleIndices);
|
|
|
|
/**/
|
|
public void AddIndicesByOrder(
|
|
int v1,
|
|
int v2,
|
|
int v3
|
|
)
|
|
{
|
|
_triangleIndices.Add(v1);
|
|
_triangleIndices.Add(v2);
|
|
_triangleIndices.Add(v3);
|
|
if (!(_sides.Remove((v1,v2)) || _sides.Remove((v2, v1)))) {
|
|
_ = _sides.Add((v1, v2));
|
|
}
|
|
if (!(_sides.Remove((v2, v3)) || _sides.Remove((v3, v2)))) {
|
|
_ = _sides.Add((v2, v3));
|
|
}
|
|
if (!(_sides.Remove((v3, v1)) || _sides.Remove((v1, v3)))) {
|
|
_ = _sides.Add((v3, v1));
|
|
}
|
|
}
|
|
|
|
public void AddVertexByOrder(
|
|
Vec3 vertex
|
|
)
|
|
{
|
|
_vertices.Add(vertex);
|
|
}
|
|
|
|
public int TriangleCount()
|
|
{
|
|
return _triangleIndices.Count / 3;
|
|
}
|
|
|
|
public void TrimExcess()
|
|
{
|
|
_triangleIndices.TrimExcess();
|
|
_vertices.TrimExcess();
|
|
Dictionary<int, int> side_dictionary = _sides.ToDictionary(x => x.begin, x => x.end);
|
|
int begin = side_dictionary.First().Key;
|
|
List<int> sideLoop = new(begin);
|
|
_sideIndices.Add(sideLoop);
|
|
while (side_dictionary.Count > 0)
|
|
{
|
|
if (side_dictionary.Remove(begin, out begin))
|
|
{
|
|
sideLoop.Add(begin);
|
|
} else
|
|
{
|
|
begin = side_dictionary.First().Key;
|
|
sideLoop = new List<int>(begin);
|
|
_sideIndices.Add(sideLoop);
|
|
}
|
|
}
|
|
_sideIndices.TrimExcess();
|
|
}
|
|
|
|
public int VertexCount()
|
|
{
|
|
return _vertices.Count;
|
|
}
|
|
|
|
}
|
|
}
|