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

126 lines
5.6 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
recast4j Copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
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.Collections.Generic;
using System.IO;
using DotRecast.Core;
using DotRecast.Detour.Io;
using DotRecast.Recast;
using DotRecast.Recast.Geom;
using NUnit.Framework;
2023-05-10 16:44:51 +03:00
using static DotRecast.Core.RcMath;
2023-03-14 08:02:43 +03:00
namespace DotRecast.Detour.Test.Io;
2023-04-25 17:22:44 +03:00
[Parallelizable]
2023-03-16 19:48:49 +03:00
public class MeshSetReaderWriterTest
{
private readonly DtMeshSetWriter writer = new DtMeshSetWriter();
private readonly DtMeshSetReader reader = new DtMeshSetReader();
2023-03-14 08:02:43 +03:00
private const float m_cellSize = 0.3f;
private const float m_cellHeight = 0.2f;
private const float m_agentHeight = 2.0f;
private const float m_agentRadius = 0.6f;
private const float m_agentMaxClimb = 0.9f;
private const float m_agentMaxSlope = 45.0f;
private const int m_regionMinSize = 8;
private const int m_regionMergeSize = 20;
private const float m_regionMinArea = m_regionMinSize * m_regionMinSize * m_cellSize * m_cellSize;
private const float m_regionMergeArea = m_regionMergeSize * m_regionMergeSize * m_cellSize * m_cellSize;
private const float m_edgeMaxLen = 12.0f;
private const float m_edgeMaxError = 1.3f;
private const int m_vertsPerPoly = 6;
private const float m_detailSampleDist = 6.0f;
private const float m_detailSampleMaxError = 1.0f;
private const int m_tileSize = 32;
private const int m_maxTiles = 128;
private const int m_maxPolysPerTile = 0x8000;
[Test]
2023-05-05 02:44:48 +03:00
public void Test()
2023-03-16 19:48:49 +03:00
{
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load("dungeon.obj"));
2023-03-14 08:02:43 +03:00
NavMeshSetHeader header = new NavMeshSetHeader();
header.magic = NavMeshSetHeader.NAVMESHSET_MAGIC;
header.version = NavMeshSetHeader.NAVMESHSET_VERSION;
2023-05-05 02:44:48 +03:00
header.option.orig = geom.GetMeshBoundsMin();
2023-03-14 08:02:43 +03:00
header.option.tileWidth = m_tileSize * m_cellSize;
header.option.tileHeight = m_tileSize * m_cellSize;
header.option.maxTiles = m_maxTiles;
header.option.maxPolys = m_maxPolysPerTile;
header.numTiles = 0;
DtNavMesh mesh = new DtNavMesh(header.option, 6);
2023-03-14 08:02:43 +03:00
RcVec3f bmin = geom.GetMeshBoundsMin();
RcVec3f bmax = geom.GetMeshBoundsMax();
2023-09-22 17:42:21 +03:00
RcCommons.CalcTileCount(bmin, bmax, m_cellSize, m_tileSize, m_tileSize, out var tw, out var th);
2023-03-16 19:48:49 +03:00
for (int y = 0; y < th; ++y)
{
for (int x = 0; x < tw; ++x)
{
RcConfig cfg = new RcConfig(true, m_tileSize, m_tileSize,
2023-09-02 07:31:30 +03:00
RcConfig.CalcBorder(m_agentRadius, m_cellSize),
RcPartition.WATERSHED,
m_cellSize, m_cellHeight,
m_agentMaxSlope, m_agentHeight, m_agentRadius, m_agentMaxClimb,
m_regionMinArea, m_regionMergeArea,
m_edgeMaxLen, m_edgeMaxError,
m_vertsPerPoly,
m_detailSampleDist, m_detailSampleMaxError,
true, true, true,
SampleAreaModifications.SAMPLE_AREAMOD_GROUND, true);
2023-09-22 17:42:21 +03:00
RcBuilderConfig bcfg = new RcBuilderConfig(cfg, bmin, bmax, x, y);
2023-03-14 08:02:43 +03:00
TestDetourBuilder db = new TestDetourBuilder();
DtMeshData data = db.Build(geom, bcfg, m_agentHeight, m_agentRadius, m_agentMaxClimb, x, y, true);
2023-03-16 19:48:49 +03:00
if (data != null)
{
2023-05-05 02:44:48 +03:00
mesh.RemoveTile(mesh.GetTileRefAt(x, y, 0));
mesh.AddTile(data, 0, 0);
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);
writer.Write(bw, mesh, RcByteOrder.LITTLE_ENDIAN, true);
2023-03-14 08:02:43 +03:00
ms.Seek(0, SeekOrigin.Begin);
2023-08-19 09:45:05 +03:00
using var br = new BinaryReader(ms);
mesh = reader.Read(br, 6);
2023-05-05 02:44:48 +03:00
Assert.That(mesh.GetMaxTiles(), Is.EqualTo(128));
Assert.That(mesh.GetParams().maxPolys, Is.EqualTo(0x8000));
Assert.That(mesh.GetParams().tileWidth, Is.EqualTo(9.6f).Within(0.001f));
List<DtMeshTile> tiles = mesh.GetTilesAt(6, 9);
2023-03-14 08:02:43 +03:00
Assert.That(tiles.Count, Is.EqualTo(1));
Assert.That(tiles[0].data.polys.Length, Is.EqualTo(2));
Assert.That(tiles[0].data.verts.Length, Is.EqualTo(7 * 3));
2023-05-05 02:44:48 +03:00
tiles = mesh.GetTilesAt(2, 9);
2023-03-14 08:02:43 +03:00
Assert.That(tiles.Count, Is.EqualTo(1));
Assert.That(tiles[0].data.polys.Length, Is.EqualTo(2));
Assert.That(tiles[0].data.verts.Length, Is.EqualTo(9 * 3));
2023-05-05 02:44:48 +03:00
tiles = mesh.GetTilesAt(4, 3);
2023-03-14 08:02:43 +03:00
Assert.That(tiles.Count, Is.EqualTo(1));
Assert.That(tiles[0].data.polys.Length, Is.EqualTo(3));
Assert.That(tiles[0].data.verts.Length, Is.EqualTo(6 * 3));
2023-05-05 02:44:48 +03:00
tiles = mesh.GetTilesAt(2, 8);
2023-03-14 08:02:43 +03:00
Assert.That(tiles.Count, Is.EqualTo(1));
Assert.That(tiles[0].data.polys.Length, Is.EqualTo(5));
Assert.That(tiles[0].data.verts.Length, Is.EqualTo(17 * 3));
}
2023-06-01 16:25:15 +03:00
}