From cd39cbbd36b5915e8b289d0434539249337de6a1 Mon Sep 17 00:00:00 2001 From: ikpil Date: Wed, 3 May 2023 00:11:45 +0900 Subject: [PATCH] heap memory optimization --- .../IsectRaySegResult.cs | 14 ++++++++++ .../ObstacleAvoidanceQuery.cs | 26 +++++++++++-------- .../SweepCircleCircleResult.cs | 2 +- 3 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 src/DotRecast.Detour.Crowd/IsectRaySegResult.cs diff --git a/src/DotRecast.Detour.Crowd/IsectRaySegResult.cs b/src/DotRecast.Detour.Crowd/IsectRaySegResult.cs new file mode 100644 index 0000000..de269f3 --- /dev/null +++ b/src/DotRecast.Detour.Crowd/IsectRaySegResult.cs @@ -0,0 +1,14 @@ +namespace DotRecast.Detour.Crowd +{ + public struct IsectRaySegResult + { + public readonly bool result; + public readonly float htmin; + + public IsectRaySegResult(bool result, float htmin) + { + this.result = result; + this.htmin = htmin; + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Detour.Crowd/ObstacleAvoidanceQuery.cs b/src/DotRecast.Detour.Crowd/ObstacleAvoidanceQuery.cs index 3e4b06c..33a16c6 100644 --- a/src/DotRecast.Detour.Crowd/ObstacleAvoidanceQuery.cs +++ b/src/DotRecast.Detour.Crowd/ObstacleAvoidanceQuery.cs @@ -65,7 +65,7 @@ namespace DotRecast.Detour.Crowd public bool touch; } - + private ObstacleAvoidanceParams m_params; private float m_invHorizTime; private float m_vmax; @@ -120,7 +120,7 @@ namespace DotRecast.Detour.Crowd { if (m_nsegments >= m_maxSegments) return; - + ObstacleSegment seg = m_segments[m_nsegments++]; seg.p = p; seg.q = q; @@ -207,21 +207,24 @@ namespace DotRecast.Detour.Crowd return new SweepCircleCircleResult(true, (b - rd) * a, (b + rd) * a); } - Tuple isectRaySeg(Vector3f ap, Vector3f u, Vector3f bp, Vector3f bq) + IsectRaySegResult isectRaySeg(Vector3f ap, Vector3f u, Vector3f bp, Vector3f bq) { Vector3f v = vSub(bq, bp); Vector3f w = vSub(ap, bp); float d = vPerp2D(u, v); if (Math.Abs(d) < 1e-6f) - return Tuple.Create(false, 0f); + return new IsectRaySegResult(false, 0f); + d = 1.0f / d; float t = vPerp2D(v, w) * d; if (t < 0 || t > 1) - return Tuple.Create(false, 0f); + return new IsectRaySegResult(false, 0f); + float s = vPerp2D(u, w) * d; if (s < 0 || s > 1) - return Tuple.Create(false, 0f); - return Tuple.Create(true, t); + return new IsectRaySegResult(false, 0f); + + return new IsectRaySegResult(true, t); } /** @@ -310,10 +313,11 @@ namespace DotRecast.Detour.Crowd } else { - Tuple ires = isectRaySeg(pos, vcand, seg.p, seg.q); - if (!ires.Item1) + var ires = isectRaySeg(pos, vcand, seg.p, seg.q); + if (!ires.result) continue; - htmin = ires.Item2; + + htmin = ires.htmin; } // Avoid less when facing walls. @@ -523,4 +527,4 @@ namespace DotRecast.Detour.Crowd return Tuple.Create(ns, nvel); } } -} +} \ No newline at end of file diff --git a/src/DotRecast.Detour.Crowd/SweepCircleCircleResult.cs b/src/DotRecast.Detour.Crowd/SweepCircleCircleResult.cs index 0c1bf51..5bce8e5 100644 --- a/src/DotRecast.Detour.Crowd/SweepCircleCircleResult.cs +++ b/src/DotRecast.Detour.Crowd/SweepCircleCircleResult.cs @@ -20,7 +20,7 @@ freely, subject to the following restrictions: namespace DotRecast.Detour.Crowd { - public class SweepCircleCircleResult + public struct SweepCircleCircleResult { public readonly bool intersection; public readonly float htmin;