inline Vector3f

This commit is contained in:
ikpil 2023-05-25 23:12:23 +09:00
parent 425e1887f6
commit 3f72b8e916
9 changed files with 70 additions and 86 deletions

View File

@ -64,34 +64,6 @@ namespace DotRecast.Core
return u * g + (1f - u) * f; return u * g + (1f - u) * f;
} }
/// 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.
public static float VLenSqr(Vector3f v)
{
return v.x * v.x + v.y * v.y + v.z * v.z;
}
/// Normalizes the vector.
/// @param[in,out] v The vector to normalize. [(x, y, z)]
public static void VNormalize(float[] v)
{
float d = (float)(1.0f / Math.Sqrt(Sqr(v[0]) + Sqr(v[1]) + Sqr(v[2])));
if (d != 0)
{
v[0] *= d;
v[1] *= d;
v[2] *= d;
}
}
/// Performs a 'sloppy' colocation check of the specified points. /// Performs a 'sloppy' colocation check of the specified points.
/// @param[in] p0 A point. [(x, y, z)] /// @param[in] p0 A point. [(x, y, z)]
/// @param[in] p1 A point. [(x, y, z)] /// @param[in] p1 A point. [(x, y, z)]
@ -111,22 +83,7 @@ namespace DotRecast.Core
} }
/// 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.
public static float VPerp2D(float[] u, float[] v)
{
return u[2] * v[0] - u[0] * v[2];
}
public static float VPerp2D(Vector3f u, Vector3f v)
{
return u.z * v.x - u.x * v.z;
}
/// @} /// @}
@ -474,8 +431,8 @@ namespace DotRecast.Core
Vector3f vpi = Vector3f.Of(verts, i * 3); Vector3f vpi = Vector3f.Of(verts, i * 3);
var edge = vpi.Subtract(vpj); var edge = vpi.Subtract(vpj);
var diff = p0v.Subtract(vpj); var diff = p0v.Subtract(vpj);
float n = VPerp2D(edge, diff); float n = Vector3f.Perp2D(edge, diff);
float d = VPerp2D(dir, edge); float d = Vector3f.Perp2D(dir, edge);
if (Math.Abs(d) < EPS) if (Math.Abs(d) < EPS)
{ {
// S is nearly parallel to this edge // S is nearly parallel to this edge

View File

@ -206,6 +206,8 @@ namespace DotRecast.Core
return hash; return hash;
} }
/// Normalizes the vector.
/// @param[in,out] v The vector to normalize. [(x, y, z)]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Normalize() public void Normalize()
{ {
@ -446,5 +448,29 @@ namespace DotRecast.Core
float dz = verts[i + 2] - p.z; float dz = verts[i + 2] - p.z;
return dx * dx + dz * dz; return dx * dx + dz * dz;
} }
/// 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.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Perp2D(Vector3f u, Vector3f v)
{
return u.z * v.x - u.x * v.z;
}
/// 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.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float LenSqr(Vector3f v)
{
return v.x * v.x + v.y * v.y + v.z * v.z;
}
} }
} }

View File

@ -924,7 +924,7 @@ namespace DotRecast.Detour.Crowd
} }
diff.y = 0; diff.y = 0;
float distSqr = VLenSqr(diff); float distSqr = Vector3f.LenSqr(diff);
if (distSqr > Sqr(range)) if (distSqr > Sqr(range))
{ {
continue; continue;
@ -1094,7 +1094,7 @@ namespace DotRecast.Detour.Crowd
Vector3f diff = ag.npos.Subtract(nei.npos); Vector3f diff = ag.npos.Subtract(nei.npos);
diff.y = 0; diff.y = 0;
float distSqr = VLenSqr(diff); float distSqr = Vector3f.LenSqr(diff);
if (distSqr < 0.00001f) if (distSqr < 0.00001f)
{ {
continue; continue;
@ -1117,7 +1117,7 @@ namespace DotRecast.Detour.Crowd
// Adjust desired velocity. // Adjust desired velocity.
dvel = Vector3f.Mad(dvel, disp, 1.0f / w); dvel = Vector3f.Mad(dvel, disp, 1.0f / w);
// Clamp desired velocity to desired speed. // Clamp desired velocity to desired speed.
float speedSqr = VLenSqr(dvel); float speedSqr = Vector3f.LenSqr(dvel);
float desiredSqr = Sqr(ag.desiredSpeed); float desiredSqr = Sqr(ag.desiredSpeed);
if (speedSqr > desiredSqr) if (speedSqr > desiredSqr)
{ {
@ -1248,7 +1248,7 @@ namespace DotRecast.Detour.Crowd
Vector3f diff = ag.npos.Subtract(nei.npos); Vector3f diff = ag.npos.Subtract(nei.npos);
diff.y = 0; diff.y = 0;
float dist = VLenSqr(diff); float dist = Vector3f.LenSqr(diff);
if (dist > Sqr(ag.option.radius + nei.option.radius)) if (dist > Sqr(ag.option.radius + nei.option.radius))
{ {
continue; continue;

View File

@ -179,16 +179,16 @@ namespace DotRecast.Detour.Crowd
{ {
Vector3f v = bq.Subtract(bp); Vector3f v = bq.Subtract(bp);
Vector3f w = ap.Subtract(bp); Vector3f w = ap.Subtract(bp);
float d = VPerp2D(u, v); float d = Vector3f.Perp2D(u, v);
if (Math.Abs(d) < 1e-6f) if (Math.Abs(d) < 1e-6f)
return new IsectRaySegResult(false, 0f); return new IsectRaySegResult(false, 0f);
d = 1.0f / d; d = 1.0f / d;
float t = VPerp2D(v, w) * d; float t = Vector3f.Perp2D(v, w) * d;
if (t < 0 || t > 1) if (t < 0 || t > 1)
return new IsectRaySegResult(false, 0f); return new IsectRaySegResult(false, 0f);
float s = VPerp2D(u, w) * d; float s = Vector3f.Perp2D(u, w) * d;
if (s < 0 || s > 1) if (s < 0 || s > 1)
return new IsectRaySegResult(false, 0f); return new IsectRaySegResult(false, 0f);

View File

@ -41,7 +41,7 @@ namespace DotRecast.Detour
} }
else else
{ {
d = VLenSqr(diff); d = Vector3f.LenSqr(diff);
} }
if (d < nearestDistanceSqr) if (d < nearestDistanceSqr)

View File

@ -1382,7 +1382,7 @@ namespace DotRecast.Detour
} }
else else
{ {
d = VLenSqr(diff); d = Vector3f.LenSqr(diff);
} }
if (d < nearestDistanceSqr) if (d < nearestDistanceSqr)

View File

@ -54,7 +54,7 @@ public class RecastDemo
private IInputContext _input; private IInputContext _input;
private ImGuiController _imgui; private ImGuiController _imgui;
private RecastDemoCanvas _canvas; private RecastDemoCanvas _canvas;
private int width = 1000; private int width = 1000;
private int height = 900; private int height = 900;
@ -485,28 +485,29 @@ public class RecastDemo
} }
else if (settingsUI.IsNavMeshInputTrigerred()) else if (settingsUI.IsNavMeshInputTrigerred())
{ {
// try (MemoryStack stack = StackPush()) { // try (MemoryStack stack = StackPush()) {
// PointerBuffer aFilterPatterns = stack.MallocPointer(4); // PointerBuffer aFilterPatterns = stack.MallocPointer(4);
// aFilterPatterns.Put(stack.UTF8("*.bin")); // aFilterPatterns.Put(stack.UTF8("*.bin"));
// aFilterPatterns.Put(stack.UTF8("*.zip")); // aFilterPatterns.Put(stack.UTF8("*.zip"));
// aFilterPatterns.Put(stack.UTF8("*.bytes")); // aFilterPatterns.Put(stack.UTF8("*.bytes"));
// aFilterPatterns.Put(stack.UTF8("*.navmesh")); // aFilterPatterns.Put(stack.UTF8("*.navmesh"));
// aFilterPatterns.Flip(); // aFilterPatterns.Flip();
// string filename = TinyFileDialogs.Tinyfd_openFileDialog("Open Nav Mesh File", "", aFilterPatterns, // string filename = TinyFileDialogs.Tinyfd_openFileDialog("Open Nav Mesh File", "", aFilterPatterns,
// "Nav Mesh File", false); // "Nav Mesh File", false);
// if (filename != null) { // if (filename != null) {
// File file = new File(filename); // File file = new File(filename);
// if (file.Exists()) { // if (file.Exists()) {
// try { // try {
// LoadNavMesh(file, filename); // LoadNavMesh(file, filename);
// geom = null; // geom = null;
// } catch (Exception e) { // } catch (Exception e) {
// Console.WriteLine(e); // Console.WriteLine(e);
// } // }
// } // }
// } // }
// } // }
} }
if (settingsUI.IsBuildTriggered() && sample.GetInputGeom() != null) if (settingsUI.IsBuildTriggered() && sample.GetInputGeom() != null)
{ {
if (!building) if (!building)
@ -597,9 +598,9 @@ public class RecastDemo
hit = PolyMeshRaycast.Raycast(sample.GetRecastResults(), rayStart, rayEnd); hit = PolyMeshRaycast.Raycast(sample.GetRecastResults(), rayStart, rayEnd);
} }
float[] rayDir = new float[] { rayEnd.x - rayStart.x, rayEnd.y - rayStart.y, rayEnd.z - rayStart.z }; Vector3f rayDir = Vector3f.Of(rayEnd.x - rayStart.x, rayEnd.y - rayStart.y, rayEnd.z - rayStart.z);
Tool rayTool = toolsUI.GetTool(); Tool rayTool = toolsUI.GetTool();
VNormalize(rayDir); rayDir.Normalize();
if (rayTool != null) if (rayTool != null)
{ {
rayTool.HandleClickRay(rayStart, rayDir, processHitTestShift); rayTool.HandleClickRay(rayStart, rayDir, processHitTestShift);
@ -737,4 +738,4 @@ public class RecastDemo
window.SwapBuffers(); window.SwapBuffers();
} }
} }

View File

@ -229,14 +229,14 @@ public class DynamicUpdateTool : Tool
private Tuple<ICollider, IColliderGizmo> CylinderCollider(Vector3f p) private Tuple<ICollider, IColliderGizmo> CylinderCollider(Vector3f p)
{ {
float radius = 0.7f + (float)random.NextDouble() * 4f; float radius = 0.7f + (float)random.NextDouble() * 4f;
float[] a = new float[] { (1f - 2 * (float)random.NextDouble()), 0.01f + (float)random.NextDouble(), (1f - 2 * (float)random.NextDouble()) }; Vector3f a = Vector3f.Of(1f - 2 * (float)random.NextDouble(), 0.01f + (float)random.NextDouble(), 1f - 2 * (float)random.NextDouble());
VNormalize(a); a.Normalize();
float len = 2f + (float)random.NextDouble() * 20f; float len = 2f + (float)random.NextDouble() * 20f;
a[0] *= len; a[0] *= len;
a[1] *= len; a[1] *= len;
a[2] *= len; a[2] *= len;
Vector3f start = Vector3f.Of(p.x, p.y, p.z); Vector3f start = Vector3f.Of(p.x, p.y, p.z);
Vector3f end = Vector3f.Of(p.x + a[0], p.y + a[1], p.z + a[2]); Vector3f end = Vector3f.Of(p.x + a.x, p.y + a.y, p.z + a.z);
return Tuple.Create<ICollider, IColliderGizmo>(new CylinderCollider(start, end, radius, SampleAreaModifications.SAMPLE_POLYAREA_TYPE_WATER, return Tuple.Create<ICollider, IColliderGizmo>(new CylinderCollider(start, end, radius, SampleAreaModifications.SAMPLE_POLYAREA_TYPE_WATER,
dynaMesh.config.walkableClimb), GizmoFactory.Cylinder(start, end, radius)); dynaMesh.config.walkableClimb), GizmoFactory.Cylinder(start, end, radius));
} }
@ -349,7 +349,7 @@ public class DynamicUpdateTool : Tool
} }
public override void HandleClickRay(Vector3f start, float[] dir, bool shift) public override void HandleClickRay(Vector3f start, Vector3f dir, bool shift)
{ {
if (mode == DynamicUpdateToolMode.COLLIDERS) if (mode == DynamicUpdateToolMode.COLLIDERS)
{ {
@ -369,7 +369,7 @@ public class DynamicUpdateTool : Tool
} }
} }
private bool Hit(Vector3f point, float[] dir, float[] bounds) private bool Hit(Vector3f point, Vector3f dir, float[] bounds)
{ {
float cx = 0.5f * (bounds[0] + bounds[3]); float cx = 0.5f * (bounds[0] + bounds[3]);
float cy = 0.5f * (bounds[1] + bounds[4]); float cy = 0.5f * (bounds[1] + bounds[4]);
@ -387,7 +387,7 @@ public class DynamicUpdateTool : Tool
return true; return true;
} }
float b = mx * dir[0] + my * dir[1] + mz * dir[2]; float b = mx * dir.x + my * dir.y + mz * dir.z;
if (b > 0.0f) if (b > 0.0f)
{ {
return false; return false;

View File

@ -38,7 +38,7 @@ public abstract class Tool
public abstract string GetName(); public abstract string GetName();
public virtual void HandleClickRay(Vector3f start, float[] direction, bool shift) public virtual void HandleClickRay(Vector3f start, Vector3f direction, bool shift)
{ {
// ... // ...
} }