2023-03-14 08:02:43 +03:00
|
|
|
/*
|
|
|
|
recast4j Copyright (c) 2015-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.Diagnostics;
|
2023-04-06 12:48:36 +03:00
|
|
|
using DotRecast.Core;
|
2023-06-23 01:54:28 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
using NUnit.Framework;
|
2023-09-23 01:30:47 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
namespace DotRecast.Detour.Test;
|
|
|
|
|
2023-04-25 17:22:44 +03:00
|
|
|
[Parallelizable]
|
2023-03-16 19:48:49 +03:00
|
|
|
public class RandomPointTest : AbstractDetourTest
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
[Test]
|
2023-05-05 02:44:48 +03:00
|
|
|
public void TestRandom()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-10-05 17:35:18 +03:00
|
|
|
RcRand f = new RcRand(1);
|
2023-06-08 15:38:02 +03:00
|
|
|
IDtQueryFilter filter = new DtQueryDefaultFilter();
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
{
|
2023-06-10 15:57:39 +03:00
|
|
|
var status = query.FindRandomPoint(filter, f, out var randomRef, out var randomPt);
|
|
|
|
Assert.That(status.Succeeded(), Is.True);
|
2023-06-10 19:15:44 +03:00
|
|
|
|
|
|
|
status = navmesh.GetTileAndPolyByRef(randomRef, out var tile, out var poly);
|
2023-03-14 08:02:43 +03:00
|
|
|
float[] bmin = new float[2];
|
|
|
|
float[] bmax = new float[2];
|
2023-06-10 19:15:44 +03:00
|
|
|
for (int j = 0; j < poly.vertCount; j++)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-06-10 19:15:44 +03:00
|
|
|
int v = poly.verts[j] * 3;
|
|
|
|
bmin[0] = j == 0 ? tile.data.verts[v] : Math.Min(bmin[0], tile.data.verts[v]);
|
|
|
|
bmax[0] = j == 0 ? tile.data.verts[v] : Math.Max(bmax[0], tile.data.verts[v]);
|
|
|
|
bmin[1] = j == 0 ? tile.data.verts[v + 2] : Math.Min(bmin[1], tile.data.verts[v + 2]);
|
|
|
|
bmax[1] = j == 0 ? tile.data.verts[v + 2] : Math.Max(bmax[1], tile.data.verts[v + 2]);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-10-12 17:04:29 +03:00
|
|
|
Assert.That(randomPt.X >= bmin[0], Is.True);
|
|
|
|
Assert.That(randomPt.X <= bmax[0], Is.True);
|
|
|
|
Assert.That(randomPt.Z >= bmin[1], Is.True);
|
|
|
|
Assert.That(randomPt.Z <= bmax[1], Is.True);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2023-05-05 02:44:48 +03:00
|
|
|
public void TestRandomAroundCircle()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-10-05 17:35:18 +03:00
|
|
|
RcRand f = new RcRand(1);
|
2023-06-08 15:38:02 +03:00
|
|
|
IDtQueryFilter filter = new DtQueryDefaultFilter();
|
2023-06-10 15:57:39 +03:00
|
|
|
query.FindRandomPoint(filter, f, out var randomRef, out var randomPt);
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
{
|
2023-06-10 16:20:56 +03:00
|
|
|
var status = query.FindRandomPointAroundCircle(randomRef, randomPt, 5f, filter, f, out var nextRandomRef, out var nextRandomPt);
|
|
|
|
Assert.That(status.Failed(), Is.False);
|
2023-06-10 15:57:39 +03:00
|
|
|
|
2023-06-10 16:20:56 +03:00
|
|
|
randomRef = nextRandomRef;
|
|
|
|
randomPt = nextRandomPt;
|
2023-06-10 19:15:44 +03:00
|
|
|
|
|
|
|
status = navmesh.GetTileAndPolyByRef(randomRef, out var tile, out var poly);
|
2023-06-10 16:20:56 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
float[] bmin = new float[2];
|
|
|
|
float[] bmax = new float[2];
|
2023-06-10 19:15:44 +03:00
|
|
|
for (int j = 0; j < poly.vertCount; j++)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-06-10 19:15:44 +03:00
|
|
|
int v = poly.verts[j] * 3;
|
|
|
|
bmin[0] = j == 0 ? tile.data.verts[v] : Math.Min(bmin[0], tile.data.verts[v]);
|
|
|
|
bmax[0] = j == 0 ? tile.data.verts[v] : Math.Max(bmax[0], tile.data.verts[v]);
|
|
|
|
bmin[1] = j == 0 ? tile.data.verts[v + 2] : Math.Min(bmin[1], tile.data.verts[v + 2]);
|
|
|
|
bmax[1] = j == 0 ? tile.data.verts[v + 2] : Math.Max(bmax[1], tile.data.verts[v + 2]);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-10-12 17:04:29 +03:00
|
|
|
Assert.That(randomPt.X >= bmin[0], Is.True);
|
|
|
|
Assert.That(randomPt.X <= bmax[0], Is.True);
|
|
|
|
Assert.That(randomPt.Z >= bmin[1], Is.True);
|
|
|
|
Assert.That(randomPt.Z <= bmax[1], Is.True);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2023-05-05 02:44:48 +03:00
|
|
|
public void TestRandomWithinCircle()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-10-05 17:35:18 +03:00
|
|
|
RcRand f = new RcRand(1);
|
2023-06-08 15:38:02 +03:00
|
|
|
IDtQueryFilter filter = new DtQueryDefaultFilter();
|
2023-06-10 15:57:39 +03:00
|
|
|
query.FindRandomPoint(filter, f, out var randomRef, out var randomPt);
|
2023-03-14 08:02:43 +03:00
|
|
|
float radius = 5f;
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
{
|
2023-06-10 16:20:56 +03:00
|
|
|
var status = query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f, out var nextRandomRef, out var nextRandomPt);
|
|
|
|
Assert.That(status.Failed(), Is.False);
|
2023-06-10 15:57:39 +03:00
|
|
|
|
2023-06-10 16:20:56 +03:00
|
|
|
float distance = RcVec3f.Dist2D(randomPt, nextRandomPt);
|
2023-03-14 08:02:43 +03:00
|
|
|
Assert.That(distance <= radius, Is.True);
|
2023-06-10 15:57:39 +03:00
|
|
|
|
2023-06-10 16:20:56 +03:00
|
|
|
randomRef = nextRandomRef;
|
|
|
|
randomPt = nextRandomPt;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2023-05-05 02:44:48 +03:00
|
|
|
public void TestPerformance()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-10-05 17:35:18 +03:00
|
|
|
RcRand f = new RcRand(1);
|
2023-06-08 15:38:02 +03:00
|
|
|
IDtQueryFilter filter = new DtQueryDefaultFilter();
|
2023-06-10 15:57:39 +03:00
|
|
|
query.FindRandomPoint(filter, f, out var randomRef, out var randomPt);
|
2023-06-10 16:20:56 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
float radius = 5f;
|
|
|
|
// jvm warmup
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
{
|
2023-06-10 16:20:56 +03:00
|
|
|
query.FindRandomPointAroundCircle(randomRef, randomPt, radius, filter, f, out var _, out var _);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
{
|
2023-06-10 16:20:56 +03:00
|
|
|
query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f, out var _, out var _);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-06 05:44:57 +03:00
|
|
|
long t1 = RcFrequency.Ticks;
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < 10000; i++)
|
|
|
|
{
|
2023-06-10 16:20:56 +03:00
|
|
|
query.FindRandomPointAroundCircle(randomRef, randomPt, radius, filter, f, out var _, out var _);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-05-06 05:44:57 +03:00
|
|
|
long t2 = RcFrequency.Ticks;
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < 10000; i++)
|
|
|
|
{
|
2023-06-10 16:20:56 +03:00
|
|
|
query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f, out var _, out var _);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-05-06 05:44:57 +03:00
|
|
|
long t3 = RcFrequency.Ticks;
|
2023-03-24 04:58:11 +03:00
|
|
|
Console.WriteLine("Random point around circle: " + (t2 - t1) / TimeSpan.TicksPerMillisecond + "ms");
|
|
|
|
Console.WriteLine("Random point within circle: " + (t3 - t2) / TimeSpan.TicksPerMillisecond + "ms");
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-06-10 15:57:39 +03:00
|
|
|
}
|