DotRecastNetSim/test/DotRecast.Detour.Test/Io/MeshDataReaderWriterTest.cs

140 lines
5.5 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
recast4j Copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
DotRecast Copyright (c) 2023-2024 Choi Ikpil ikpil@naver.com
2023-03-14 08:02:43 +03:00
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System.IO;
using DotRecast.Core;
using DotRecast.Detour.Io;
using NUnit.Framework;
namespace DotRecast.Detour.Test.Io;
2024-01-21 13:27:58 +03:00
2023-03-16 19:48:49 +03:00
public class MeshDataReaderWriterTest
{
2023-03-14 08:02:43 +03:00
private const int VERTS_PER_POLYGON = 6;
private DtMeshData meshData;
2023-03-14 08:02:43 +03:00
[SetUp]
2023-05-05 02:44:48 +03:00
public void SetUp()
2023-03-16 19:48:49 +03:00
{
2024-05-25 17:01:16 +03:00
meshData = TestMeshDataFactory.Create();
2023-03-14 08:02:43 +03:00
}
[Test]
2023-05-05 02:44:48 +03:00
public void TestCCompatibility()
2023-03-16 19:48:49 +03:00
{
2023-05-10 16:44:51 +03:00
Test(true, RcByteOrder.BIG_ENDIAN);
2023-03-14 08:02:43 +03:00
}
[Test]
2023-05-05 02:44:48 +03:00
public void TestCompact()
2023-03-16 19:48:49 +03:00
{
2023-05-10 16:44:51 +03:00
Test(false, RcByteOrder.BIG_ENDIAN);
2023-03-14 08:02:43 +03:00
}
[Test]
2023-05-05 02:44:48 +03:00
public void TestCCompatibilityLE()
2023-03-16 19:48:49 +03:00
{
2023-05-10 16:44:51 +03:00
Test(true, RcByteOrder.LITTLE_ENDIAN);
2023-03-14 08:02:43 +03:00
}
[Test]
2023-05-05 02:44:48 +03:00
public void TestCompactLE()
2023-03-16 19:48:49 +03:00
{
2023-05-10 16:44:51 +03:00
Test(false, RcByteOrder.LITTLE_ENDIAN);
2023-03-14 08:02:43 +03:00
}
2023-05-10 16:44:51 +03:00
public void Test(bool cCompatibility, RcByteOrder order)
2023-03-16 19:48:49 +03:00
{
2023-03-14 08:02:43 +03:00
using var ms = new MemoryStream();
2023-08-19 09:45:05 +03:00
using var bw = new BinaryWriter(ms);
2023-03-16 19:48:49 +03:00
DtMeshDataWriter writer = new DtMeshDataWriter();
2023-08-19 09:45:05 +03:00
writer.Write(bw, meshData, order, cCompatibility);
2023-03-14 08:02:43 +03:00
ms.Seek(0, SeekOrigin.Begin);
2023-03-16 19:48:49 +03:00
2023-08-19 09:45:05 +03:00
using var br = new BinaryReader(ms);
DtMeshDataReader reader = new DtMeshDataReader();
2023-08-19 09:45:05 +03:00
DtMeshData readData = reader.Read(br, VERTS_PER_POLYGON);
2023-03-14 08:02:43 +03:00
Assert.That(readData.header.vertCount, Is.EqualTo(meshData.header.vertCount));
Assert.That(readData.header.polyCount, Is.EqualTo(meshData.header.polyCount));
Assert.That(readData.header.detailMeshCount, Is.EqualTo(meshData.header.detailMeshCount));
Assert.That(readData.header.detailTriCount, Is.EqualTo(meshData.header.detailTriCount));
Assert.That(readData.header.detailVertCount, Is.EqualTo(meshData.header.detailVertCount));
Assert.That(readData.header.bvNodeCount, Is.EqualTo(meshData.header.bvNodeCount));
Assert.That(readData.header.offMeshConCount, Is.EqualTo(meshData.header.offMeshConCount));
2023-03-16 19:48:49 +03:00
for (int i = 0; i < meshData.header.vertCount; i++)
{
2023-03-14 08:02:43 +03:00
Assert.That(readData.verts[i], Is.EqualTo(meshData.verts[i]));
}
2023-03-16 19:48:49 +03:00
for (int i = 0; i < meshData.header.polyCount; i++)
{
2023-03-14 08:02:43 +03:00
Assert.That(readData.polys[i].vertCount, Is.EqualTo(meshData.polys[i].vertCount));
Assert.That(readData.polys[i].areaAndtype, Is.EqualTo(meshData.polys[i].areaAndtype));
2023-03-16 19:48:49 +03:00
for (int j = 0; j < meshData.polys[i].vertCount; j++)
{
2023-03-14 08:02:43 +03:00
Assert.That(readData.polys[i].verts[j], Is.EqualTo(meshData.polys[i].verts[j]));
Assert.That(readData.polys[i].neis[j], Is.EqualTo(meshData.polys[i].neis[j]));
}
}
2023-03-16 19:48:49 +03:00
for (int i = 0; i < meshData.header.detailMeshCount; i++)
{
2023-03-14 08:02:43 +03:00
Assert.That(readData.detailMeshes[i].vertBase, Is.EqualTo(meshData.detailMeshes[i].vertBase));
Assert.That(readData.detailMeshes[i].vertCount, Is.EqualTo(meshData.detailMeshes[i].vertCount));
Assert.That(readData.detailMeshes[i].triBase, Is.EqualTo(meshData.detailMeshes[i].triBase));
Assert.That(readData.detailMeshes[i].triCount, Is.EqualTo(meshData.detailMeshes[i].triCount));
}
2023-03-16 19:48:49 +03:00
for (int i = 0; i < meshData.header.detailVertCount; i++)
{
2023-03-14 08:02:43 +03:00
Assert.That(readData.detailVerts[i], Is.EqualTo(meshData.detailVerts[i]));
}
2023-03-16 19:48:49 +03:00
for (int i = 0; i < meshData.header.detailTriCount; i++)
{
2023-03-14 08:02:43 +03:00
Assert.That(readData.detailTris[i], Is.EqualTo(meshData.detailTris[i]));
}
2023-03-16 19:48:49 +03:00
for (int i = 0; i < meshData.header.bvNodeCount; i++)
{
2023-03-14 08:02:43 +03:00
Assert.That(readData.bvTree[i].i, Is.EqualTo(meshData.bvTree[i].i));
2023-03-16 19:48:49 +03:00
for (int j = 0; j < 3; j++)
{
2023-03-14 08:02:43 +03:00
Assert.That(readData.bvTree[i].bmin[j], Is.EqualTo(meshData.bvTree[i].bmin[j]));
Assert.That(readData.bvTree[i].bmax[j], Is.EqualTo(meshData.bvTree[i].bmax[j]));
}
}
2023-03-16 19:48:49 +03:00
for (int i = 0; i < meshData.header.offMeshConCount; i++)
{
2023-03-14 08:02:43 +03:00
Assert.That(readData.offMeshCons[i].flags, Is.EqualTo(meshData.offMeshCons[i].flags));
Assert.That(readData.offMeshCons[i].rad, Is.EqualTo(meshData.offMeshCons[i].rad));
Assert.That(readData.offMeshCons[i].poly, Is.EqualTo(meshData.offMeshCons[i].poly));
Assert.That(readData.offMeshCons[i].side, Is.EqualTo(meshData.offMeshCons[i].side));
Assert.That(readData.offMeshCons[i].userId, Is.EqualTo(meshData.offMeshCons[i].userId));
for (int j = 0; j < 2; j++)
2023-03-16 19:48:49 +03:00
{
2023-03-14 08:02:43 +03:00
Assert.That(readData.offMeshCons[i].pos[j], Is.EqualTo(meshData.offMeshCons[i].pos[j]));
}
}
}
2023-03-16 19:48:49 +03:00
}