DtLocalBoundary.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
  4. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using DotRecast.Core;
  22. namespace DotRecast.Detour.Crowd
  23. {
  24. using static RcMath;
  25. public class DtLocalBoundary
  26. {
  27. public const int MAX_LOCAL_SEGS = 8;
  28. private RcVec3f m_center = new RcVec3f();
  29. private List<DtSegment> m_segs = new List<DtSegment>();
  30. private List<long> m_polys = new List<long>();
  31. private List<long> m_parents = new List<long>();
  32. public DtLocalBoundary()
  33. {
  34. m_center.x = m_center.y = m_center.z = float.MaxValue;
  35. }
  36. public void Reset()
  37. {
  38. m_center.x = m_center.y = m_center.z = float.MaxValue;
  39. m_polys.Clear();
  40. m_segs.Clear();
  41. }
  42. protected void AddSegment(float dist, RcSegmentVert s)
  43. {
  44. // Insert neighbour based on the distance.
  45. DtSegment seg = new DtSegment();
  46. seg.s[0] = s.vmin;
  47. seg.s[1] = s.vmax;
  48. //Array.Copy(s, seg.s, 6);
  49. seg.d = dist;
  50. if (0 == m_segs.Count)
  51. {
  52. m_segs.Add(seg);
  53. }
  54. else if (dist >= m_segs[m_segs.Count - 1].d)
  55. {
  56. if (m_segs.Count >= MAX_LOCAL_SEGS)
  57. {
  58. return;
  59. }
  60. m_segs.Add(seg);
  61. }
  62. else
  63. {
  64. // Insert inbetween.
  65. int i;
  66. for (i = 0; i < m_segs.Count; ++i)
  67. {
  68. if (dist <= m_segs[i].d)
  69. {
  70. break;
  71. }
  72. }
  73. m_segs.Insert(i, seg);
  74. }
  75. while (m_segs.Count > MAX_LOCAL_SEGS)
  76. {
  77. m_segs.RemoveAt(m_segs.Count - 1);
  78. }
  79. }
  80. public void Update(long startRef, RcVec3f pos, float collisionQueryRange, DtNavMeshQuery navquery, IDtQueryFilter filter)
  81. {
  82. if (startRef == 0)
  83. {
  84. Reset();
  85. return;
  86. }
  87. m_center = pos;
  88. // First query non-overlapping polygons.
  89. var status = navquery.FindLocalNeighbourhood(startRef, pos, collisionQueryRange, filter, ref m_polys, ref m_parents);
  90. if (status.Succeeded())
  91. {
  92. // Secondly, store all polygon edges.
  93. m_segs.Clear();
  94. var segmentVerts = new List<RcSegmentVert>();
  95. var segmentRefs = new List<long>();
  96. for (int j = 0; j < m_polys.Count; ++j)
  97. {
  98. var result = navquery.GetPolyWallSegments(m_polys[j], false, filter, ref segmentVerts, ref segmentRefs);
  99. if (result.Succeeded())
  100. {
  101. for (int k = 0; k < segmentRefs.Count; ++k)
  102. {
  103. RcSegmentVert s = segmentVerts[k];
  104. var s0 = s.vmin;
  105. var s3 = s.vmax;
  106. // Skip too distant segments.
  107. var distSqr = DtUtils.DistancePtSegSqr2D(pos, s0, s3, out var tseg);
  108. if (distSqr > Sqr(collisionQueryRange))
  109. {
  110. continue;
  111. }
  112. AddSegment(distSqr, s);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. public bool IsValid(DtNavMeshQuery navquery, IDtQueryFilter filter)
  119. {
  120. if (m_polys.Count == 0)
  121. {
  122. return false;
  123. }
  124. // Check that all polygons still pass query filter.
  125. foreach (long refs in m_polys)
  126. {
  127. if (!navquery.IsValidPolyRef(refs, filter))
  128. {
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. public RcVec3f GetCenter()
  135. {
  136. return m_center;
  137. }
  138. public RcVec3f[] GetSegment(int j)
  139. {
  140. return m_segs[j].s;
  141. }
  142. public int GetSegmentCount()
  143. {
  144. return m_segs.Count;
  145. }
  146. }
  147. }