DotRecastNetSim/src/DotRecast.Core/RcMath.cs

802 lines
26 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
2023-03-15 17:00:29 +03:00
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
2023-03-14 08:02:43 +03:00
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;
2023-03-25 09:43:20 +03:00
namespace DotRecast.Core
2023-03-16 19:09:10 +03:00
{
2023-05-10 16:44:51 +03:00
public static class RcMath
2023-03-16 19:48:49 +03:00
{
public const float EPS = 1e-4f;
2023-05-05 02:44:48 +03:00
private static readonly float EQUAL_THRESHOLD = Sqr(1.0f / 16384.0f);
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static float VDistSqr(Vector3f v1, float[] v2, int i)
2023-03-25 09:43:20 +03:00
{
2023-04-25 19:08:38 +03:00
float dx = v2[i] - v1.x;
float dy = v2[i + 1] - v1.y;
float dz = v2[i + 2] - v1.z;
2023-03-25 09:43:20 +03:00
return dx * dx + dy * dy + dz * dz;
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static float VDistSqr(Vector3f v1, Vector3f v2)
2023-03-31 18:28:54 +03:00
{
2023-04-25 19:08:38 +03:00
float dx = v2.x - v1.x;
float dy = v2.y - v1.y;
float dz = v2.z - v1.z;
2023-03-31 18:28:54 +03:00
return dx * dx + dy * dy + dz * dz;
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static float VDistSqr(float[] v, int i, int j)
2023-03-29 18:59:00 +03:00
{
2023-04-04 07:52:07 +03:00
float dx = v[i] - v[j];
float dy = v[i + 1] - v[j + 1];
float dz = v[i + 2] - v[j + 2];
2023-03-29 18:59:00 +03:00
return dx * dx + dy * dy + dz * dz;
}
2023-05-14 06:36:04 +03:00
2023-03-25 09:43:20 +03:00
2023-05-05 02:44:48 +03:00
public static float Sqr(float f)
2023-03-25 09:43:20 +03:00
{
return f * f;
}
2023-05-05 02:44:48 +03:00
public static float GetPathLen(float[] path, int npath)
2023-03-25 09:43:20 +03:00
{
float totd = 0;
for (int i = 0; i < npath - 1; ++i)
{
2023-05-05 02:44:48 +03:00
totd += (float)Math.Sqrt(VDistSqr(path, i * 3, (i + 1) * 3));
2023-03-25 09:43:20 +03:00
}
return totd;
}
2023-04-04 07:52:07 +03:00
2023-05-05 02:44:48 +03:00
public static float Step(float threshold, float v)
2023-03-25 09:43:20 +03:00
{
return v < threshold ? 0.0f : 1.0f;
}
2023-05-05 02:44:48 +03:00
public static float Clamp(float v, float min, float max)
2023-03-25 09:43:20 +03:00
{
return Math.Max(Math.Min(v, max), min);
}
2023-05-05 02:44:48 +03:00
public static int Clamp(int v, int min, int max)
2023-03-25 09:43:20 +03:00
{
return Math.Max(Math.Min(v, max), min);
}
2023-05-05 02:44:48 +03:00
public static float Lerp(float f, float g, float u)
2023-03-25 09:43:20 +03:00
{
return u * g + (1f - u) * f;
}
2023-05-14 06:36:04 +03:00
2023-03-16 19:48:49 +03:00
/// Performs a scaled vector addition. (@p v1 + (@p v2 * @p s))
/// @param[out] dest The result vector. [(x, y, z)]
/// @param[in] v1 The base vector. [(x, y, z)]
/// @param[in] v2 The vector to scale and add to @p v1. [(x, y, z)]
/// @param[in] s The amount to scale @p v2 by before adding to @p v1.
2023-05-05 02:44:48 +03:00
public static Vector3f VMad(Vector3f v1, Vector3f v2, float s)
2023-03-28 19:52:26 +03:00
{
Vector3f dest = new Vector3f();
2023-04-25 19:08:38 +03:00
dest.x = v1.x + v2.x * s;
dest.y = v1.y + v2.y * s;
dest.z = v1.z + v2.z * s;
2023-03-28 19:52:26 +03:00
return dest;
}
2023-03-16 19:09:10 +03:00
2023-03-16 19:48:49 +03:00
/// Performs a linear interpolation between two vectors. (@p v1 toward @p
/// v2)
/// @param[out] dest The result vector. [(x, y, x)]
/// @param[in] v1 The starting vector.
/// @param[in] v2 The destination vector.
/// @param[in] t The interpolation factor. [Limits: 0 <= value <= 1.0]
2023-05-05 02:44:48 +03:00
public static Vector3f VLerp(float[] verts, int v1, int v2, float t)
2023-03-16 19:48:49 +03:00
{
2023-05-14 06:36:04 +03:00
return new Vector3f(
verts[v1 + 0] + (verts[v2 + 0] - verts[v1 + 0]) * t,
verts[v1 + 1] + (verts[v2 + 1] - verts[v1 + 1]) * t,
verts[v1 + 2] + (verts[v2 + 2] - verts[v1 + 2]) * t
);
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
2023-03-28 19:52:26 +03:00
2023-05-05 02:44:48 +03:00
public static void VSet(ref Vector3f @out, float a, float b, float c)
2023-03-28 18:03:33 +03:00
{
@out.x = a;
@out.y = b;
@out.z = c;
}
2023-05-05 02:44:48 +03:00
public static void VCopy(float[] @out, Vector3f @in)
2023-03-29 18:59:00 +03:00
{
2023-04-25 19:08:38 +03:00
@out[0] = @in.x;
@out[1] = @in.y;
@out[2] = @in.z;
2023-03-29 18:59:00 +03:00
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static void VCopy(ref Vector3f @out, float[] @in)
2023-03-28 18:03:33 +03:00
{
@out.x = @in[0];
@out.y = @in[1];
@out.z = @in[2];
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static void VCopy(ref Vector3f @out, float[] @in, int i)
2023-03-28 18:03:33 +03:00
{
@out.x = @in[i];
@out.y = @in[i + 1];
@out.z = @in[i + 2];
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static void VMin(ref Vector3f @out, float[] @in, int i)
2023-03-28 18:03:33 +03:00
{
@out.x = Math.Min(@out.x, @in[i]);
@out.y = Math.Min(@out.y, @in[i + 1]);
@out.z = Math.Min(@out.z, @in[i + 2]);
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static void VMax(ref Vector3f @out, float[] @in, int i)
2023-03-28 18:03:33 +03:00
{
@out.x = Math.Max(@out.x, @in[i]);
@out.y = Math.Max(@out.y, @in[i + 1]);
@out.z = Math.Max(@out.z, @in[i + 2]);
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// Returns the distance between two points.
/// @param[in] v1 A point. [(x, y, z)]
/// @param[in] v2 A point. [(x, y, z)]
/// @return The distance between the two points.
2023-05-05 02:44:48 +03:00
public static float VDistSqr(float[] v1, float[] v2)
2023-03-16 19:48:49 +03:00
{
float dx = v2[0] - v1[0];
float dy = v2[1] - v1[1];
float dz = v2[2] - v1[2];
return dx * dx + dy * dy + dz * dz;
}
2023-03-28 18:03:33 +03:00
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// Derives the square of the scalar length of the vector. (len * len)
/// @param[in] v The vector. [(x, y, z)]
/// @return The square of the scalar length of the vector.
2023-05-05 02:44:48 +03:00
public static float VLenSqr(Vector3f v)
2023-03-28 19:52:26 +03:00
{
2023-04-25 19:08:38 +03:00
return v.x * v.x + v.y * v.y + v.z * v.z;
2023-03-28 19:52:26 +03:00
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// Derives the distance between the specified points on the xz-plane.
/// @param[in] v1 A point. [(x, y, z)]
/// @param[in] v2 A point. [(x, y, z)]
/// @return The distance between the point on the xz-plane.
///
/// The vectors are projected onto the xz-plane, so the y-values are
/// ignored.
2023-05-05 02:44:48 +03:00
public static float VDist2D(float[] v1, float[] v2)
2023-03-16 19:48:49 +03:00
{
float dx = v2[0] - v1[0];
float dz = v2[2] - v1[2];
return (float)Math.Sqrt(dx * dx + dz * dz);
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static float VDist2D(Vector3f v1, Vector3f v2)
2023-03-28 19:52:26 +03:00
{
2023-04-25 19:08:38 +03:00
float dx = v2.x - v1.x;
float dz = v2.z - v1.z;
2023-03-28 19:52:26 +03:00
return (float)Math.Sqrt(dx * dx + dz * dz);
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static float VDist2DSqr(float[] v1, float[] v2)
2023-03-16 19:48:49 +03:00
{
float dx = v2[0] - v1[0];
float dz = v2[2] - v1[2];
return dx * dx + dz * dz;
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static float VDist2DSqr(Vector3f v1, Vector3f v2)
2023-03-28 19:52:26 +03:00
{
2023-04-25 19:08:38 +03:00
float dx = v2.x - v1.x;
float dz = v2.z - v1.z;
2023-03-28 19:52:26 +03:00
return dx * dx + dz * dz;
}
2023-03-14 08:02:43 +03:00
2023-03-28 19:52:26 +03:00
2023-05-05 02:44:48 +03:00
public static float VDist2DSqr(Vector3f p, float[] verts, int i)
2023-03-16 19:48:49 +03:00
{
2023-04-25 19:08:38 +03:00
float dx = verts[i] - p.x;
float dz = verts[i + 2] - p.z;
2023-03-16 19:48:49 +03:00
return dx * dx + dz * dz;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// Normalizes the vector.
/// @param[in,out] v The vector to normalize. [(x, y, z)]
2023-05-05 02:44:48 +03:00
public static void VNormalize(float[] v)
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
float d = (float)(1.0f / Math.Sqrt(Sqr(v[0]) + Sqr(v[1]) + Sqr(v[2])));
2023-03-16 19:48:49 +03:00
if (d != 0)
{
v[0] *= d;
v[1] *= d;
v[2] *= d;
}
}
2023-05-14 06:36:04 +03:00
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// Performs a 'sloppy' colocation check of the specified points.
/// @param[in] p0 A point. [(x, y, z)]
/// @param[in] p1 A point. [(x, y, z)]
/// @return True if the points are considered to be at the same location.
///
/// Basically, this function will return true if the specified points are
/// close enough to eachother to be considered colocated.
2023-05-05 02:44:48 +03:00
public static bool VEqual(Vector3f p0, Vector3f p1)
2023-03-28 19:52:26 +03:00
{
2023-05-05 02:44:48 +03:00
return VEqual(p0, p1, EQUAL_THRESHOLD);
2023-03-28 19:52:26 +03:00
}
2023-05-05 02:44:48 +03:00
public static bool VEqual(Vector3f p0, Vector3f p1, float thresholdSqr)
2023-03-28 19:52:26 +03:00
{
2023-05-05 02:44:48 +03:00
float d = VDistSqr(p0, p1);
2023-03-28 19:52:26 +03:00
return d < thresholdSqr;
}
2023-03-16 19:48:49 +03:00
/// Derives the xz-plane 2D perp product of the two vectors. (uz*vx - ux*vz)
/// @param[in] u The LHV vector [(x, y, z)]
/// @param[in] v The RHV vector [(x, y, z)]
/// @return The dot product on the xz-plane.
///
/// The vectors are projected onto the xz-plane, so the y-values are
/// ignored.
2023-05-05 02:44:48 +03:00
public static float VPerp2D(float[] u, float[] v)
2023-03-16 19:48:49 +03:00
{
return u[2] * v[0] - u[0] * v[2];
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static float VPerp2D(Vector3f u, Vector3f v)
2023-03-28 19:52:26 +03:00
{
2023-04-25 19:08:38 +03:00
return u.z * v.x - u.x * v.z;
2023-03-28 19:52:26 +03:00
}
2023-05-14 06:36:04 +03:00
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// @}
/// @name Computational geometry helper functions.
/// @{
/// Derives the signed xz-plane area of the triangle ABC, or the
/// relationship of line AB to point C.
/// @param[in] a Vertex A. [(x, y, z)]
/// @param[in] b Vertex B. [(x, y, z)]
/// @param[in] c Vertex C. [(x, y, z)]
/// @return The signed xz-plane area of the triangle.
2023-05-05 02:44:48 +03:00
public static float TriArea2D(float[] verts, int a, int b, int c)
2023-03-16 19:48:49 +03:00
{
float abx = verts[b] - verts[a];
float abz = verts[b + 2] - verts[a + 2];
float acx = verts[c] - verts[a];
float acz = verts[c + 2] - verts[a + 2];
return acx * abz - abx * acz;
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static float TriArea2D(float[] a, float[] b, float[] c)
2023-03-16 19:48:49 +03:00
{
float abx = b[0] - a[0];
float abz = b[2] - a[2];
float acx = c[0] - a[0];
float acz = c[2] - a[2];
return acx * abz - abx * acz;
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static float TriArea2D(Vector3f a, Vector3f b, Vector3f c)
2023-03-28 18:03:33 +03:00
{
2023-04-23 08:19:49 +03:00
float abx = b.x - a.x;
float abz = b.z - a.z;
float acx = c.x - a.x;
float acz = c.z - a.z;
2023-03-28 18:03:33 +03:00
return acx * abz - abx * acz;
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static float TriArea2D(Vector3f a, float[] b, Vector3f c)
2023-03-31 18:28:54 +03:00
{
2023-04-25 19:08:38 +03:00
float abx = b[0] - a.x;
float abz = b[2] - a.z;
float acx = c.x - a.x;
float acz = c.z - a.z;
2023-03-31 18:28:54 +03:00
return acx * abz - abx * acz;
}
2023-03-28 18:03:33 +03:00
2023-03-16 19:48:49 +03:00
/// Determines if two axis-aligned bounding boxes overlap.
/// @param[in] amin Minimum bounds of box A. [(x, y, z)]
/// @param[in] amax Maximum bounds of box A. [(x, y, z)]
/// @param[in] bmin Minimum bounds of box B. [(x, y, z)]
/// @param[in] bmax Maximum bounds of box B. [(x, y, z)]
/// @return True if the two AABB's overlap.
/// @see dtOverlapBounds
2023-05-05 02:44:48 +03:00
public static bool OverlapQuantBounds(int[] amin, int[] amax, int[] bmin, int[] bmax)
2023-03-16 19:48:49 +03:00
{
bool overlap = true;
overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
return overlap;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// Determines if two axis-aligned bounding boxes overlap.
/// @param[in] amin Minimum bounds of box A. [(x, y, z)]
/// @param[in] amax Maximum bounds of box A. [(x, y, z)]
/// @param[in] bmin Minimum bounds of box B. [(x, y, z)]
/// @param[in] bmax Maximum bounds of box B. [(x, y, z)]
/// @return True if the two AABB's overlap.
/// @see dtOverlapQuantBounds
2023-05-05 02:44:48 +03:00
public static bool OverlapBounds(float[] amin, float[] amax, float[] bmin, float[] bmax)
2023-03-16 19:48:49 +03:00
{
bool overlap = true;
2023-05-14 06:36:04 +03:00
overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
2023-03-16 19:48:49 +03:00
overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
return overlap;
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static bool OverlapBounds(Vector3f amin, Vector3f amax, Vector3f bmin, Vector3f bmax)
2023-03-28 19:52:26 +03:00
{
bool overlap = true;
2023-04-25 19:08:38 +03:00
overlap = (amin.x > bmax.x || amax.x < bmin.x) ? false : overlap;
overlap = (amin.y > bmax.y || amax.y < bmin.y) ? false : overlap;
overlap = (amin.z > bmax.z || amax.z < bmin.z) ? false : overlap;
2023-03-28 19:52:26 +03:00
return overlap;
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static Tuple<float, float> DistancePtSegSqr2D(Vector3f pt, Vector3f p, Vector3f q)
2023-03-16 19:48:49 +03:00
{
2023-04-25 19:08:38 +03:00
float pqx = q.x - p.x;
float pqz = q.z - p.z;
float dx = pt.x - p.x;
float dz = pt.z - p.z;
2023-03-16 19:48:49 +03:00
float d = pqx * pqx + pqz * pqz;
float t = pqx * dx + pqz * dz;
if (d > 0)
{
t /= d;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
if (t < 0)
{
t = 0;
}
else if (t > 1)
{
t = 1;
}
2023-03-14 08:02:43 +03:00
2023-04-25 19:08:38 +03:00
dx = p.x + t * pqx - pt.x;
dz = p.z + t * pqz - pt.z;
2023-03-16 19:48:49 +03:00
return Tuple.Create(dx * dx + dz * dz, t);
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static float? ClosestHeightPointTriangle(Vector3f p, Vector3f a, Vector3f b, Vector3f c)
2023-03-16 19:48:49 +03:00
{
2023-05-14 10:57:57 +03:00
Vector3f v0 = c.Subtract(a);
Vector3f v1 = b.Subtract(a);
Vector3f v2 = p.Subtract(a);
2023-03-16 19:48:49 +03:00
// Compute scaled barycentric coordinates
2023-04-25 19:08:38 +03:00
float denom = v0.x * v1.z - v0.z * v1.x;
2023-03-16 19:48:49 +03:00
if (Math.Abs(denom) < EPS)
{
return null;
}
2023-03-14 08:02:43 +03:00
2023-04-25 19:08:38 +03:00
float u = v1.z * v2.x - v1.x * v2.z;
float v = v0.x * v2.z - v0.z * v2.x;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
if (denom < 0)
{
denom = -denom;
u = -u;
v = -v;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
// If point lies inside the triangle, return interpolated ycoord.
if (u >= 0.0f && v >= 0.0f && (u + v) <= denom)
{
2023-04-25 19:08:38 +03:00
float h = a.y + (v0.y * u + v1.y * v) / denom;
2023-03-16 19:48:49 +03:00
return h;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
return null;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
/// @par
///
/// All points are projected onto the xz-plane, so the y-values are ignored.
2023-05-05 02:44:48 +03:00
public static bool PointInPolygon(Vector3f pt, float[] verts, int nverts)
2023-03-16 19:48:49 +03:00
{
// TODO: Replace pnpoly with triArea2D tests?
int i, j;
bool c = false;
for (i = 0, j = nverts - 1; i < nverts; j = i++)
{
int vi = i * 3;
int vj = j * 3;
2023-04-25 19:08:38 +03:00
if (((verts[vi + 2] > pt.z) != (verts[vj + 2] > pt.z)) && (pt.x < (verts[vj + 0] - verts[vi + 0])
* (pt.z - verts[vi + 2]) / (verts[vj + 2] - verts[vi + 2]) + verts[vi + 0]))
2023-03-16 19:48:49 +03:00
{
c = !c;
}
}
return c;
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
public static bool DistancePtPolyEdgesSqr(Vector3f pt, float[] verts, int nverts, float[] ed, float[] et)
2023-03-16 19:48:49 +03:00
{
// TODO: Replace pnpoly with triArea2D tests?
int i, j;
bool c = false;
for (i = 0, j = nverts - 1; i < nverts; j = i++)
{
int vi = i * 3;
int vj = j * 3;
2023-04-25 19:08:38 +03:00
if (((verts[vi + 2] > pt.z) != (verts[vj + 2] > pt.z)) && (pt.x < (verts[vj + 0] - verts[vi + 0])
* (pt.z - verts[vi + 2]) / (verts[vj + 2] - verts[vi + 2]) + verts[vi + 0]))
2023-03-16 19:48:49 +03:00
{
c = !c;
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
Tuple<float, float> edet = DistancePtSegSqr2D(pt, verts, vj, vi);
2023-03-16 19:48:49 +03:00
ed[j] = edet.Item1;
et[j] = edet.Item2;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
return c;
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
public static float[] ProjectPoly(Vector3f axis, float[] poly, int npoly)
2023-03-16 19:48:49 +03:00
{
float rmin, rmax;
2023-05-14 11:09:37 +03:00
rmin = rmax = axis.Dot2D(poly, 0);
2023-03-16 19:48:49 +03:00
for (int i = 1; i < npoly; ++i)
{
2023-05-14 11:09:37 +03:00
float d = axis.Dot2D(poly, i * 3);
2023-03-16 19:48:49 +03:00
rmin = Math.Min(rmin, d);
rmax = Math.Max(rmax, d);
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
return new float[] { rmin, rmax };
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
public static bool OverlapRange(float amin, float amax, float bmin, float bmax, float eps)
2023-03-16 19:48:49 +03:00
{
return ((amin + eps) > bmax || (amax - eps) < bmin) ? false : true;
}
2023-03-14 08:02:43 +03:00
2023-03-28 18:03:33 +03:00
const float eps = 1e-4f;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// @par
///
/// All vertices are projected onto the xz-plane, so the y-values are ignored.
2023-05-05 02:44:48 +03:00
public static bool OverlapPolyPoly2D(float[] polya, int npolya, float[] polyb, int npolyb)
2023-03-16 19:48:49 +03:00
{
for (int i = 0, j = npolya - 1; i < npolya; j = i++)
{
int va = j * 3;
int vb = i * 3;
Vector3f n = Vector3f.Of(polya[vb + 2] - polya[va + 2], 0, -(polya[vb + 0] - polya[va + 0]));
2023-03-16 19:48:49 +03:00
2023-05-05 02:44:48 +03:00
float[] aminmax = ProjectPoly(n, polya, npolya);
float[] bminmax = ProjectPoly(n, polyb, npolyb);
if (!OverlapRange(aminmax[0], aminmax[1], bminmax[0], bminmax[1], eps))
2023-03-16 19:48:49 +03:00
{
// Found separating axis
return false;
}
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
for (int i = 0, j = npolyb - 1; i < npolyb; j = i++)
{
int va = j * 3;
int vb = i * 3;
2023-03-14 08:02:43 +03:00
Vector3f n = Vector3f.Of(polyb[vb + 2] - polyb[va + 2], 0, -(polyb[vb + 0] - polyb[va + 0]));
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
float[] aminmax = ProjectPoly(n, polya, npolya);
float[] bminmax = ProjectPoly(n, polyb, npolyb);
if (!OverlapRange(aminmax[0], aminmax[1], bminmax[0], bminmax[1], eps))
2023-03-16 19:48:49 +03:00
{
// Found separating axis
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
// Returns a random point in a convex polygon.
// Adapted from Graphics Gems article.
2023-05-05 02:44:48 +03:00
public static Vector3f RandomPointInConvexPoly(float[] pts, int npts, float[] areas, float s, float t)
2023-03-16 19:48:49 +03:00
{
// Calc triangle araes
float areasum = 0.0f;
for (int i = 2; i < npts; i++)
{
2023-05-05 02:44:48 +03:00
areas[i] = TriArea2D(pts, 0, (i - 1) * 3, i * 3);
2023-03-16 19:48:49 +03:00
areasum += Math.Max(0.001f, areas[i]);
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
// Find sub triangle weighted by area.
float thr = s * areasum;
float acc = 0.0f;
float u = 1.0f;
int tri = npts - 1;
for (int i = 2; i < npts; i++)
{
float dacc = areas[i];
if (thr >= acc && thr < (acc + dacc))
{
u = (thr - acc) / dacc;
tri = i;
break;
}
acc += dacc;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
float v = (float)Math.Sqrt(t);
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
float a = 1 - v;
float b = (1 - u) * v;
float c = u * v;
int pa = 0;
int pb = (tri - 1) * 3;
int pc = tri * 3;
2023-03-14 08:02:43 +03:00
2023-03-28 19:52:26 +03:00
return new Vector3f()
2023-03-16 19:48:49 +03:00
{
2023-03-28 19:52:26 +03:00
x = a * pts[pa] + b * pts[pb] + c * pts[pc],
y = a * pts[pa + 1] + b * pts[pb + 1] + c * pts[pc + 1],
z = a * pts[pa + 2] + b * pts[pb + 2] + c * pts[pc + 2]
2023-03-16 19:48:49 +03:00
};
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static int NextPow2(int v)
2023-03-16 19:48:49 +03:00
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static int Ilog2(int v)
2023-03-16 19:48:49 +03:00
{
int r;
int shift;
r = (v > 0xffff ? 1 : 0) << 4;
v >>= r;
shift = (v > 0xff ? 1 : 0) << 3;
v >>= shift;
r |= shift;
shift = (v > 0xf ? 1 : 0) << 2;
v >>= shift;
r |= shift;
shift = (v > 0x3 ? 1 : 0) << 1;
v >>= shift;
r |= shift;
r |= (v >> 1);
return r;
}
2023-03-14 08:02:43 +03:00
2023-05-07 12:10:20 +03:00
2023-05-05 02:44:48 +03:00
public static IntersectResult IntersectSegmentPoly2D(Vector3f p0, Vector3f p1, float[] verts, int nverts)
2023-03-16 19:48:49 +03:00
{
IntersectResult result = new IntersectResult();
float EPS = 0.000001f;
2023-05-14 10:57:57 +03:00
var dir = p1.Subtract(p0);
2023-03-16 19:48:49 +03:00
2023-03-28 19:52:26 +03:00
var p0v = p0;
2023-03-16 19:48:49 +03:00
for (int i = 0, j = nverts - 1; i < nverts; j = i++)
{
2023-05-13 06:46:18 +03:00
Vector3f vpj = Vector3f.Of(verts, j * 3);
Vector3f vpi = Vector3f.Of(verts, i * 3);
2023-05-14 10:57:57 +03:00
var edge = vpi.Subtract(vpj);
var diff = p0v.Subtract(vpj);
2023-05-05 02:44:48 +03:00
float n = VPerp2D(edge, diff);
float d = VPerp2D(dir, edge);
2023-03-16 19:48:49 +03:00
if (Math.Abs(d) < EPS)
{
// S is nearly parallel to this edge
if (n < 0)
{
2023-03-14 08:02:43 +03:00
return result;
}
2023-03-16 19:48:49 +03:00
else
{
continue;
}
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
float t = n / d;
if (d < 0)
{
// segment S is entering across this edge
if (t > result.tmin)
{
result.tmin = t;
result.segMin = j;
// S enters after leaving polygon
if (result.tmin > result.tmax)
{
return result;
}
}
}
else
{
// segment S is leaving across this edge
if (t < result.tmax)
{
result.tmax = t;
result.segMax = j;
// S leaves before entering polygon
if (result.tmax < result.tmin)
{
return result;
}
2023-03-14 08:02:43 +03:00
}
}
}
2023-03-16 19:48:49 +03:00
result.intersects = true;
return result;
2023-03-14 08:02:43 +03:00
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static Tuple<float, float> DistancePtSegSqr2D(Vector3f pt, SegmentVert verts, int p, int q)
2023-04-22 07:43:24 +03:00
{
float pqx = verts[q + 0] - verts[p + 0];
float pqz = verts[q + 2] - verts[p + 2];
2023-04-25 19:08:38 +03:00
float dx = pt.x - verts[p + 0];
float dz = pt.z - verts[p + 2];
2023-04-22 07:43:24 +03:00
float d = pqx * pqx + pqz * pqz;
float t = pqx * dx + pqz * dz;
if (d > 0)
{
t /= d;
}
if (t < 0)
{
t = 0;
}
else if (t > 1)
{
t = 1;
}
2023-04-25 19:08:38 +03:00
dx = verts[p + 0] + t * pqx - pt.x;
dz = verts[p + 2] + t * pqz - pt.z;
2023-04-22 07:43:24 +03:00
return Tuple.Create(dx * dx + dz * dz, t);
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static Tuple<float, float> DistancePtSegSqr2D(Vector3f pt, float[] verts, int p, int q)
2023-03-16 19:48:49 +03:00
{
float pqx = verts[q + 0] - verts[p + 0];
float pqz = verts[q + 2] - verts[p + 2];
2023-04-25 19:08:38 +03:00
float dx = pt.x - verts[p + 0];
float dz = pt.z - verts[p + 2];
2023-03-16 19:48:49 +03:00
float d = pqx * pqx + pqz * pqz;
float t = pqx * dx + pqz * dz;
if (d > 0)
{
t /= d;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
if (t < 0)
{
t = 0;
}
else if (t > 1)
{
t = 1;
}
2023-03-14 08:02:43 +03:00
2023-04-25 19:08:38 +03:00
dx = verts[p + 0] + t * pqx - pt.x;
dz = verts[p + 2] + t * pqz - pt.z;
2023-03-16 19:48:49 +03:00
return Tuple.Create(dx * dx + dz * dz, t);
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static int OppositeTile(int side)
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
return (side + 4) & 0x7;
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
public static float VperpXZ(float[] a, float[] b)
2023-03-16 19:48:49 +03:00
{
return a[0] * b[2] - a[2] * b[0];
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static float VperpXZ(Vector3f a, Vector3f b)
2023-03-28 19:52:26 +03:00
{
2023-04-25 19:08:38 +03:00
return a.x * b.z - a.z * b.x;
2023-03-28 19:52:26 +03:00
}
2023-05-18 02:30:38 +03:00
public static Tuple<float, float> IntersectSegSeg2D(Vector3f ap, Vector3f aq, Vector3f bp, Vector3f bq)
2023-03-28 18:03:33 +03:00
{
2023-05-14 10:57:57 +03:00
Vector3f u = aq.Subtract(ap);
Vector3f v = bq.Subtract(bp);
Vector3f w = ap.Subtract(bp);
2023-05-05 02:44:48 +03:00
float d = VperpXZ(u, v);
2023-03-28 18:03:33 +03:00
if (Math.Abs(d) < 1e-6f)
{
return null;
}
2023-05-05 02:44:48 +03:00
float s = VperpXZ(v, w) / d;
float t = VperpXZ(u, w) / d;
2023-03-28 18:03:33 +03:00
return Tuple.Create(s, t);
}
2023-05-05 02:44:48 +03:00
public static Vector3f VScale(Vector3f @in, float scale)
2023-03-28 19:52:26 +03:00
{
var @out = new Vector3f();
2023-04-25 19:08:38 +03:00
@out.x = @in.x * scale;
@out.y = @in.y * scale;
@out.z = @in.z * scale;
2023-03-28 19:52:26 +03:00
return @out;
}
2023-03-16 19:09:10 +03:00
2023-03-16 19:48:49 +03:00
/// Checks that the specified vector's components are all finite.
/// @param[in] v A point. [(x, y, z)]
/// @return True if all of the point's components are finite, i.e. not NaN
/// or any of the infinities.
2023-05-05 02:44:48 +03:00
public static bool VIsFinite(float[] v)
2023-03-16 19:48:49 +03:00
{
return float.IsFinite(v[0]) && float.IsFinite(v[1]) && float.IsFinite(v[2]);
}
2023-05-14 06:36:04 +03:00
2023-05-05 02:44:48 +03:00
public static bool VIsFinite(Vector3f v)
2023-03-28 19:52:26 +03:00
{
2023-04-25 19:08:38 +03:00
return float.IsFinite(v.x) && float.IsFinite(v.y) && float.IsFinite(v.z);
2023-03-28 19:52:26 +03:00
}
2023-03-16 19:48:49 +03:00
/// Checks that the specified vector's 2D components are finite.
/// @param[in] v A point. [(x, y, z)]
2023-05-05 02:44:48 +03:00
public static bool VIsFinite2D(float[] v)
2023-03-16 19:48:49 +03:00
{
return float.IsFinite(v[0]) && float.IsFinite(v[2]);
}
2023-03-28 19:52:26 +03:00
2023-05-05 02:44:48 +03:00
public static bool VIsFinite2D(Vector3f v)
2023-03-28 19:52:26 +03:00
{
2023-04-25 19:08:38 +03:00
return float.IsFinite(v.x) && float.IsFinite(v.z);
2023-03-28 19:52:26 +03:00
}
2023-03-16 19:48:49 +03:00
}
2023-05-14 06:36:04 +03:00
}