Files
2026-06-04 11:42:34 +02:00

487 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using TriangleNet.Geometry;
using TriangleNet.Log;
namespace TriangleNet.IO
{
// Token: 0x0200002A RID: 42
public static class FileReader
{
// Token: 0x06000154 RID: 340 RVA: 0x000171D4 File Offset: 0x000153D4
private static bool TryReadLine(StreamReader reader, out string[] token)
{
token = null;
if (reader.EndOfStream)
{
return false;
}
string text = reader.ReadLine().Trim();
while (string.IsNullOrEmpty(text.Trim()) || text.StartsWith("#"))
{
if (reader.EndOfStream)
{
return false;
}
text = reader.ReadLine().Trim();
}
token = text.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
return true;
}
// Token: 0x06000155 RID: 341 RVA: 0x00017248 File Offset: 0x00015448
private static void ReadVertex(InputGeometry data, int index, string[] line, int attributes, int marks)
{
double num = double.Parse(line[1], FileReader.nfi);
double num2 = double.Parse(line[2], FileReader.nfi);
int num3 = 0;
double[] array = ((attributes == 0) ? null : new double[attributes]);
for (int i = 0; i < attributes; i++)
{
if (line.Length > 3 + i)
{
array[i] = double.Parse(line[3 + i], FileReader.nfi);
}
}
if (marks > 0 && line.Length > 3 + attributes)
{
num3 = int.Parse(line[3 + attributes]);
}
data.AddPoint(num, num2, num3, array);
}
// Token: 0x06000156 RID: 342 RVA: 0x000172D0 File Offset: 0x000154D0
public static void Read(string filename, out InputGeometry geometry)
{
geometry = null;
string text = Path.ChangeExtension(filename, ".poly");
if (File.Exists(text))
{
geometry = FileReader.ReadPolyFile(text);
return;
}
text = Path.ChangeExtension(filename, ".node");
geometry = FileReader.ReadNodeFile(text);
}
// Token: 0x06000157 RID: 343 RVA: 0x00017314 File Offset: 0x00015514
public static void Read(string filename, out InputGeometry geometry, out List<ITriangle> triangles)
{
triangles = null;
FileReader.Read(filename, out geometry);
string text = Path.ChangeExtension(filename, ".ele");
if (File.Exists(text) && geometry != null)
{
triangles = FileReader.ReadEleFile(text);
}
}
// Token: 0x06000158 RID: 344 RVA: 0x0001734C File Offset: 0x0001554C
public static InputGeometry Read(string filename)
{
InputGeometry inputGeometry = null;
FileReader.Read(filename, out inputGeometry);
return inputGeometry;
}
// Token: 0x06000159 RID: 345 RVA: 0x00017364 File Offset: 0x00015564
public static InputGeometry ReadNodeFile(string nodefilename)
{
return FileReader.ReadNodeFile(nodefilename, false);
}
// Token: 0x0600015A RID: 346 RVA: 0x00017370 File Offset: 0x00015570
public static InputGeometry ReadNodeFile(string nodefilename, bool readElements)
{
FileReader.startIndex = 0;
int num = 0;
int num2 = 0;
InputGeometry inputGeometry;
using (StreamReader streamReader = new StreamReader(nodefilename))
{
string[] array;
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file.");
}
int num3 = int.Parse(array[0]);
if (num3 < 3)
{
throw new Exception("Input must have at least three input vertices.");
}
if (array.Length > 1 && int.Parse(array[1]) != 2)
{
throw new Exception("Triangle only works with two-dimensional meshes.");
}
if (array.Length > 2)
{
num = int.Parse(array[2]);
}
if (array.Length > 3)
{
num2 = int.Parse(array[3]);
}
inputGeometry = new InputGeometry(num3);
if (num3 > 0)
{
for (int i = 0; i < num3; i++)
{
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (vertices).");
}
if (array.Length < 3)
{
throw new Exception("Invalid vertex.");
}
if (i == 0)
{
FileReader.startIndex = int.Parse(array[0], FileReader.nfi);
}
FileReader.ReadVertex(inputGeometry, i, array, num, num2);
}
}
}
if (readElements)
{
string text = Path.ChangeExtension(nodefilename, ".ele");
if (File.Exists(text))
{
FileReader.ReadEleFile(text, true);
}
}
return inputGeometry;
}
// Token: 0x0600015B RID: 347 RVA: 0x000174A0 File Offset: 0x000156A0
public static InputGeometry ReadPolyFile(string polyfilename)
{
return FileReader.ReadPolyFile(polyfilename, false, false);
}
// Token: 0x0600015C RID: 348 RVA: 0x000174AA File Offset: 0x000156AA
public static InputGeometry ReadPolyFile(string polyfilename, bool readElements)
{
return FileReader.ReadPolyFile(polyfilename, readElements, false);
}
// Token: 0x0600015D RID: 349 RVA: 0x000174B4 File Offset: 0x000156B4
public static InputGeometry ReadPolyFile(string polyfilename, bool readElements, bool readArea)
{
FileReader.startIndex = 0;
int num = 0;
int num2 = 0;
InputGeometry inputGeometry;
using (StreamReader streamReader = new StreamReader(polyfilename))
{
string[] array;
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file.");
}
int num3 = int.Parse(array[0]);
if (array.Length > 1 && int.Parse(array[1]) != 2)
{
throw new Exception("Triangle only works with two-dimensional meshes.");
}
if (array.Length > 2)
{
num = int.Parse(array[2]);
}
if (array.Length > 3)
{
num2 = int.Parse(array[3]);
}
if (num3 > 0)
{
inputGeometry = new InputGeometry(num3);
for (int i = 0; i < num3; i++)
{
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (vertices).");
}
if (array.Length < 3)
{
throw new Exception("Invalid vertex.");
}
if (i == 0)
{
FileReader.startIndex = int.Parse(array[0], FileReader.nfi);
}
FileReader.ReadVertex(inputGeometry, i, array, num, num2);
}
}
else
{
inputGeometry = FileReader.ReadNodeFile(Path.ChangeExtension(polyfilename, ".node"));
num3 = inputGeometry.Count;
}
if (inputGeometry.Points == null)
{
throw new Exception("No nodes available.");
}
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (segments).");
}
int num4 = int.Parse(array[0]);
int num5 = 0;
if (array.Length > 1)
{
num5 = int.Parse(array[1]);
}
for (int j = 0; j < num4; j++)
{
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (segments).");
}
if (array.Length < 3)
{
throw new Exception("Segment has no endpoints.");
}
int num6 = int.Parse(array[1]) - FileReader.startIndex;
int num7 = int.Parse(array[2]) - FileReader.startIndex;
int num8 = 0;
if (num5 > 0 && array.Length > 3)
{
num8 = int.Parse(array[3]);
}
if (num6 < 0 || num6 >= num3)
{
if (Behavior.Verbose)
{
SimpleLog.Instance.Warning("Invalid first endpoint of segment.", "MeshReader.ReadPolyfile()");
}
}
else if (num7 < 0 || num7 >= num3)
{
if (Behavior.Verbose)
{
SimpleLog.Instance.Warning("Invalid second endpoint of segment.", "MeshReader.ReadPolyfile()");
}
}
else
{
inputGeometry.AddSegment(num6, num7, num8);
}
}
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (holes).");
}
int num9 = int.Parse(array[0]);
if (num9 > 0)
{
for (int k = 0; k < num9; k++)
{
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (holes).");
}
if (array.Length < 3)
{
throw new Exception("Invalid hole.");
}
inputGeometry.AddHole(double.Parse(array[1], FileReader.nfi), double.Parse(array[2], FileReader.nfi));
}
}
if (FileReader.TryReadLine(streamReader, out array))
{
int num10 = int.Parse(array[0]);
if (num10 > 0)
{
for (int l = 0; l < num10; l++)
{
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (region).");
}
if (array.Length < 4)
{
throw new Exception("Invalid region attributes.");
}
inputGeometry.AddRegion(double.Parse(array[1], FileReader.nfi), double.Parse(array[2], FileReader.nfi), int.Parse(array[3]));
}
}
}
}
if (readElements)
{
string text = Path.ChangeExtension(polyfilename, ".ele");
if (File.Exists(text))
{
FileReader.ReadEleFile(text, readArea);
}
}
return inputGeometry;
}
// Token: 0x0600015E RID: 350 RVA: 0x00017808 File Offset: 0x00015A08
public static List<ITriangle> ReadEleFile(string elefilename)
{
return FileReader.ReadEleFile(elefilename, false);
}
// Token: 0x0600015F RID: 351 RVA: 0x00017814 File Offset: 0x00015A14
private static List<ITriangle> ReadEleFile(string elefilename, bool readArea)
{
int num = 0;
List<ITriangle> list;
using (StreamReader streamReader = new StreamReader(elefilename))
{
bool flag = false;
string[] array;
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (elements).");
}
num = int.Parse(array[0]);
int num2 = 0;
if (array.Length > 2)
{
num2 = int.Parse(array[2]);
flag = true;
}
if (num2 > 1)
{
SimpleLog.Instance.Warning("Triangle attributes not supported.", "FileReader.Read");
}
list = new List<ITriangle>(num);
for (int i = 0; i < num; i++)
{
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (elements).");
}
if (array.Length < 4)
{
throw new Exception("Triangle has no nodes.");
}
InputTriangle inputTriangle = new InputTriangle(int.Parse(array[1]) - FileReader.startIndex, int.Parse(array[2]) - FileReader.startIndex, int.Parse(array[3]) - FileReader.startIndex);
if (num2 > 0 && flag)
{
int num3 = 0;
flag = int.TryParse(array[4], out num3);
inputTriangle.region = num3;
}
list.Add(inputTriangle);
}
}
if (readArea)
{
string text = Path.ChangeExtension(elefilename, ".area");
if (File.Exists(text))
{
FileReader.ReadAreaFile(text, num);
}
}
return list;
}
// Token: 0x06000160 RID: 352 RVA: 0x00017960 File Offset: 0x00015B60
private static double[] ReadAreaFile(string areafilename, int intriangles)
{
double[] array = null;
using (StreamReader streamReader = new StreamReader(areafilename))
{
string[] array2;
if (!FileReader.TryReadLine(streamReader, out array2))
{
throw new Exception("Can't read input file (area).");
}
if (int.Parse(array2[0]) != intriangles)
{
SimpleLog.Instance.Warning("Number of area constraints doesn't match number of triangles.", "ReadAreaFile()");
return null;
}
array = new double[intriangles];
for (int i = 0; i < intriangles; i++)
{
if (!FileReader.TryReadLine(streamReader, out array2))
{
throw new Exception("Can't read input file (area).");
}
if (array2.Length != 2)
{
throw new Exception("Triangle has no nodes.");
}
array[i] = double.Parse(array2[1], FileReader.nfi);
}
}
return array;
}
// Token: 0x06000161 RID: 353 RVA: 0x00017A1C File Offset: 0x00015C1C
public static List<Edge> ReadEdgeFile(string edgeFile, int invertices)
{
List<Edge> list = null;
FileReader.startIndex = 0;
using (StreamReader streamReader = new StreamReader(edgeFile))
{
string[] array;
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (segments).");
}
int num = int.Parse(array[0]);
int num2 = 0;
if (array.Length > 1)
{
num2 = int.Parse(array[1]);
}
if (num > 0)
{
list = new List<Edge>(num);
}
for (int i = 0; i < num; i++)
{
if (!FileReader.TryReadLine(streamReader, out array))
{
throw new Exception("Can't read input file (segments).");
}
if (array.Length < 3)
{
throw new Exception("Segment has no endpoints.");
}
int num3 = int.Parse(array[1]) - FileReader.startIndex;
int num4 = int.Parse(array[2]) - FileReader.startIndex;
int num5 = 0;
if (num2 > 0 && array.Length > 3)
{
num5 = int.Parse(array[3]);
}
if (num3 < 0 || num3 >= invertices)
{
if (Behavior.Verbose)
{
SimpleLog.Instance.Warning("Invalid first endpoint of segment.", "MeshReader.ReadPolyfile()");
}
}
else if (num4 < 0 || num4 >= invertices)
{
if (Behavior.Verbose)
{
SimpleLog.Instance.Warning("Invalid second endpoint of segment.", "MeshReader.ReadPolyfile()");
}
}
else
{
list.Add(new Edge(num3, num4, num5));
}
}
}
return list;
}
// Token: 0x040000EC RID: 236
private static NumberFormatInfo nfi = CultureInfo.InvariantCulture.NumberFormat;
// Token: 0x040000ED RID: 237
private static int startIndex = 0;
}
}