DotRecastNetSim/test/DotRecast.Detour.Dynamic.Test/VoxelQueryTest.cs

110 lines
3.8 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
recast4j copyright (c) 2021 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.IO;
using System.Threading.Tasks;
using System.Collections.Generic;
using DotRecast.Core;
using DotRecast.Detour.Dynamic.Io;
2023-08-05 16:39:18 +03:00
using DotRecast.Detour.Dynamic.Test.Io;
2023-03-14 08:02:43 +03:00
using DotRecast.Recast;
using Moq;
using NUnit.Framework;
namespace DotRecast.Detour.Dynamic.Test;
2023-04-25 17:22:44 +03:00
[Parallelizable]
2023-03-16 19:48:49 +03:00
public class VoxelQueryTest
{
2023-03-14 08:02:43 +03:00
private const int TILE_WIDTH = 100;
private const int TILE_DEPTH = 90;
private static readonly RcVec3f ORIGIN = RcVec3f.Of(50, 10, 40);
2023-03-16 19:48:49 +03:00
2023-03-14 08:02:43 +03:00
[Test]
2023-05-05 02:44:48 +03:00
public void ShouldTraverseTiles()
2023-03-14 08:02:43 +03:00
{
var hfProvider = new Mock<Func<int, int, RcHeightfield>>();
2023-03-16 19:48:49 +03:00
2023-03-14 08:02:43 +03:00
// Given
List<int> captorX = new();
List<int> captorZ = new();
hfProvider
.Setup(e => e.Invoke(It.IsAny<int>(), It.IsAny<int>()))
.Returns((RcHeightfield)null)
2023-03-14 08:02:43 +03:00
.Callback<int, int>((x, z) =>
{
captorX.Add(x);
captorZ.Add(z);
});
2023-03-16 19:48:49 +03:00
2023-03-14 08:02:43 +03:00
VoxelQuery query = new VoxelQuery(ORIGIN, TILE_WIDTH, TILE_DEPTH, hfProvider.Object);
RcVec3f start = RcVec3f.Of(120, 10, 365);
RcVec3f end = RcVec3f.Of(320, 10, 57);
2023-03-16 19:48:49 +03:00
2023-03-14 08:02:43 +03:00
// When
query.Raycast(start, end, out var hit);
2023-03-14 08:02:43 +03:00
// Then
hfProvider.Verify(mock => mock.Invoke(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(6));
2023-03-16 19:48:49 +03:00
Assert.That(captorX, Is.EqualTo(new[] { 0, 1, 1, 1, 2, 2 }));
Assert.That(captorZ, Is.EqualTo(new[] { 3, 3, 2, 1, 1, 0 }));
2023-03-14 08:02:43 +03:00
}
[Test]
2023-05-05 02:44:48 +03:00
public void ShouldHandleRaycastWithoutObstacles()
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
DynamicNavMesh mesh = CreateDynaMesh();
VoxelQuery query = mesh.VoxelQuery();
RcVec3f start = RcVec3f.Of(7.4f, 0.5f, -64.8f);
RcVec3f end = RcVec3f.Of(31.2f, 0.5f, -75.3f);
bool isHit = query.Raycast(start, end, out var hit);
Assert.That(isHit, Is.EqualTo(false));
2023-03-14 08:02:43 +03:00
}
[Test]
2023-05-05 02:44:48 +03:00
public void ShouldHandleRaycastWithObstacles()
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
DynamicNavMesh mesh = CreateDynaMesh();
VoxelQuery query = mesh.VoxelQuery();
RcVec3f start = RcVec3f.Of(32.3f, 0.5f, 47.9f);
RcVec3f end = RcVec3f.Of(-31.2f, 0.5f, -29.8f);
bool isHit = query.Raycast(start, end, out var hit);
Assert.That(isHit, Is.EqualTo(true));
Assert.That(hit, Is.EqualTo(0.5263836f).Within(1e-7f));
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
private DynamicNavMesh CreateDynaMesh()
2023-03-16 19:48:49 +03:00
{
2023-03-14 08:02:43 +03:00
var bytes = Loader.ToBytes("test_tiles.voxels");
using var ms = new MemoryStream(bytes);
2023-08-19 09:45:05 +03:00
using var br = new BinaryReader(ms);
2023-03-16 19:48:49 +03:00
2023-03-14 08:02:43 +03:00
// load voxels from file
2023-08-05 16:39:18 +03:00
VoxelFileReader reader = new VoxelFileReader(DtVoxelTileLZ4ForTestCompressor.Shared);
2023-08-19 09:45:05 +03:00
VoxelFile f = reader.Read(br);
2023-03-14 08:02:43 +03:00
// create dynamic navmesh
var mesh = new DynamicNavMesh(f);
// build navmesh asynchronously using multiple threads
2023-05-05 02:44:48 +03:00
Task<bool> future = mesh.Build(Task.Factory);
2023-03-14 08:02:43 +03:00
// wait for build to complete
var _ = future.Result;
return mesh;
}
2023-03-16 19:48:49 +03:00
}