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;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using DotRecast.Core;
|
|
|
|
using DotRecast.Recast.Geom;
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
namespace DotRecast.Recast.Test;
|
|
|
|
|
2023-04-25 17:22:44 +03:00
|
|
|
[Parallelizable]
|
2023-03-14 08:02:43 +03:00
|
|
|
public class RecastTileMeshTest
|
|
|
|
{
|
|
|
|
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;
|
2023-07-28 14:43:50 +03:00
|
|
|
private RcPartition m_partitionType = RcPartition.WATERSHED;
|
2023-03-14 08:02:43 +03:00
|
|
|
private const int m_tileSize = 32;
|
|
|
|
|
|
|
|
[Test]
|
2023-05-05 02:44:48 +03:00
|
|
|
public void TestDungeon()
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
TestBuild("dungeon.obj");
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public void TestBuild(string filename)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-09-20 17:41:18 +03:00
|
|
|
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load(filename));
|
2023-09-22 17:42:21 +03:00
|
|
|
RcBuilder builder = new RcBuilder();
|
2023-09-02 07:31:30 +03:00
|
|
|
RcConfig cfg = new RcConfig(
|
|
|
|
true, m_tileSize, m_tileSize, RcConfig.CalcBorder(m_agentRadius, m_cellSize),
|
|
|
|
m_partitionType,
|
|
|
|
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, geom.GetMeshBoundsMin(), geom.GetMeshBoundsMax(), 7, 8);
|
|
|
|
RcBuilderResult rcResult = builder.Build(geom, bcfg);
|
2023-05-05 02:44:48 +03:00
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(1));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(5));
|
2023-09-22 17:42:21 +03:00
|
|
|
bcfg = new RcBuilderConfig(cfg, geom.GetMeshBoundsMin(), geom.GetMeshBoundsMax(), 6, 9);
|
2023-05-05 02:44:48 +03:00
|
|
|
rcResult = builder.Build(geom, bcfg);
|
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(2));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(7));
|
2023-09-22 17:42:21 +03:00
|
|
|
bcfg = new RcBuilderConfig(cfg, geom.GetMeshBoundsMin(), geom.GetMeshBoundsMax(), 2, 9);
|
2023-05-05 02:44:48 +03:00
|
|
|
rcResult = builder.Build(geom, bcfg);
|
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(2));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(9));
|
2023-09-22 17:42:21 +03:00
|
|
|
bcfg = new RcBuilderConfig(cfg, geom.GetMeshBoundsMin(), geom.GetMeshBoundsMax(), 4, 3);
|
2023-05-05 02:44:48 +03:00
|
|
|
rcResult = builder.Build(geom, bcfg);
|
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(3));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(6));
|
2023-09-22 17:42:21 +03:00
|
|
|
bcfg = new RcBuilderConfig(cfg, geom.GetMeshBoundsMin(), geom.GetMeshBoundsMax(), 2, 8);
|
2023-05-05 02:44:48 +03:00
|
|
|
rcResult = builder.Build(geom, bcfg);
|
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(5));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(17));
|
2023-09-22 17:42:21 +03:00
|
|
|
bcfg = new RcBuilderConfig(cfg, geom.GetMeshBoundsMin(), geom.GetMeshBoundsMax(), 0, 8);
|
2023-05-05 02:44:48 +03:00
|
|
|
rcResult = builder.Build(geom, bcfg);
|
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(6));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(15));
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2023-05-05 02:44:48 +03:00
|
|
|
public void TestPerformance()
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-09-20 17:41:18 +03:00
|
|
|
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load("dungeon.obj"));
|
2023-09-22 17:42:21 +03:00
|
|
|
RcBuilder builder = new RcBuilder();
|
2023-09-02 07:31:30 +03:00
|
|
|
RcConfig cfg = new RcConfig(
|
|
|
|
true, m_tileSize, m_tileSize,
|
|
|
|
RcConfig.CalcBorder(m_agentRadius, m_cellSize),
|
|
|
|
m_partitionType,
|
|
|
|
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-03-14 08:02:43 +03:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
Build(geom, builder, cfg, 1, true);
|
|
|
|
Build(geom, builder, cfg, 4, true);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-06 05:44:57 +03:00
|
|
|
long t1 = RcFrequency.Ticks;
|
2023-03-14 08:02:43 +03:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
Build(geom, builder, cfg, 1, false);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-06 05:44:57 +03:00
|
|
|
long t2 = RcFrequency.Ticks;
|
2023-03-14 08:02:43 +03:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
Build(geom, builder, cfg, 4, false);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-06 05:44:57 +03:00
|
|
|
long t3 = RcFrequency.Ticks;
|
2023-03-24 04:58:11 +03:00
|
|
|
Console.WriteLine(" Time ST : " + (t2 - t1) / TimeSpan.TicksPerMillisecond);
|
|
|
|
Console.WriteLine(" Time MT : " + (t3 - t2) / TimeSpan.TicksPerMillisecond);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-09-22 17:42:21 +03:00
|
|
|
private void Build(IInputGeomProvider geom, RcBuilder builder, RcConfig cfg, int threads, bool validate)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
|
|
|
CancellationTokenSource cts = new CancellationTokenSource();
|
2023-09-22 17:42:21 +03:00
|
|
|
List<RcBuilderResult> tiles = new();
|
2023-05-05 02:44:48 +03:00
|
|
|
var task = builder.BuildTilesAsync(geom, cfg, threads, tiles, Task.Factory, cts.Token);
|
2023-03-14 08:02:43 +03:00
|
|
|
if (validate)
|
|
|
|
{
|
2023-09-22 17:42:21 +03:00
|
|
|
RcBuilderResult rcResult = GetTile(tiles, 7, 8);
|
2023-05-05 02:44:48 +03:00
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(1));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(5));
|
|
|
|
rcResult = GetTile(tiles, 6, 9);
|
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(2));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(7));
|
|
|
|
rcResult = GetTile(tiles, 2, 9);
|
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(2));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(9));
|
|
|
|
rcResult = GetTile(tiles, 4, 3);
|
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(3));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(6));
|
|
|
|
rcResult = GetTile(tiles, 2, 8);
|
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(5));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(17));
|
|
|
|
rcResult = GetTile(tiles, 0, 8);
|
|
|
|
Assert.That(rcResult.GetMesh().npolys, Is.EqualTo(6));
|
|
|
|
Assert.That(rcResult.GetMesh().nverts, Is.EqualTo(15));
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
cts.Cancel();
|
2023-05-05 02:44:48 +03:00
|
|
|
//executor.AwaitTermination(1000, TimeUnit.HOURS);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2023-03-16 19:09:10 +03:00
|
|
|
Console.WriteLine(e);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 17:42:21 +03:00
|
|
|
private RcBuilderResult GetTile(List<RcBuilderResult> tiles, int x, int z)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
|
|
|
return tiles.FirstOrDefault(tile => tile.tileX == x && tile.tileZ == z);
|
|
|
|
}
|
|
|
|
}
|