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.
|
|
|
|
*/
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-16 19:09:10 +03:00
|
|
|
namespace DotRecast.Detour.Extras
|
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
public class PolyUtils
|
|
|
|
{
|
|
|
|
/**
|
2023-03-14 08:02:43 +03:00
|
|
|
* Find edge shared by 2 polygons within the same tile
|
|
|
|
*/
|
2023-03-16 19:48:49 +03:00
|
|
|
public static int findEdge(Poly node, Poly neighbour, MeshData tile, MeshData neighbourTile)
|
|
|
|
{
|
|
|
|
// Compare indices first assuming there are no duplicate vertices
|
|
|
|
for (int i = 0; i < node.vertCount; i++)
|
|
|
|
{
|
|
|
|
int j = (i + 1) % node.vertCount;
|
|
|
|
for (int k = 0; k < neighbour.vertCount; k++)
|
|
|
|
{
|
|
|
|
int l = (k + 1) % neighbour.vertCount;
|
|
|
|
if ((node.verts[i] == neighbour.verts[l] && node.verts[j] == neighbour.verts[k])
|
|
|
|
|| (node.verts[i] == neighbour.verts[k] && node.verts[j] == neighbour.verts[l]))
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
// Fall back to comparing actual positions in case of duplicate vertices
|
|
|
|
for (int i = 0; i < node.vertCount; i++)
|
|
|
|
{
|
|
|
|
int j = (i + 1) % node.vertCount;
|
|
|
|
for (int k = 0; k < neighbour.vertCount; k++)
|
|
|
|
{
|
|
|
|
int l = (k + 1) % neighbour.vertCount;
|
|
|
|
if ((samePosition(tile.verts, node.verts[i], neighbourTile.verts, neighbour.verts[l])
|
|
|
|
&& samePosition(tile.verts, node.verts[j], neighbourTile.verts, neighbour.verts[k]))
|
2023-03-14 08:02:43 +03:00
|
|
|
|| (samePosition(tile.verts, node.verts[i], neighbourTile.verts, neighbour.verts[k])
|
2023-03-16 19:48:49 +03:00
|
|
|
&& samePosition(tile.verts, node.verts[j], neighbourTile.verts, neighbour.verts[l])))
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
return -1;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
private static bool samePosition(float[] verts, int v, float[] verts2, int v2)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
if (verts[3 * v + i] != verts2[3 * v2 + 1])
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
return true;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/**
|
2023-03-14 08:02:43 +03:00
|
|
|
* Find edge closest to the given coordinate
|
|
|
|
*/
|
2023-03-16 19:48:49 +03:00
|
|
|
public static int findEdge(Poly node, MeshData tile, float value, int comp)
|
|
|
|
{
|
|
|
|
float error = float.MaxValue;
|
|
|
|
int edge = 0;
|
|
|
|
for (int i = 0; i < node.vertCount; i++)
|
|
|
|
{
|
|
|
|
int j = (i + 1) % node.vertCount;
|
|
|
|
float v1 = tile.verts[3 * node.verts[i] + comp] - value;
|
|
|
|
float v2 = tile.verts[3 * node.verts[j] + comp] - value;
|
|
|
|
float d = v1 * v1 + v2 * v2;
|
|
|
|
if (d < error)
|
|
|
|
{
|
|
|
|
error = d;
|
|
|
|
edge = i;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
return edge;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 19:09:10 +03:00
|
|
|
}
|