/ADAPTPlanning.UnityTest/PlannerGizmoAdaptive-gizmos.cs

https://bitbucket.org/kainino/constraint-aware-navigation · C# · 248 lines · 196 code · 28 blank · 24 comment · 66 complexity · 4a69a945cbe35894649ff84258e0c79c MD5 · raw file

  1. #define ADAPTIVE
  2. namespace ADAPTPlanning.UnityTest
  3. {
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using ADAPTPlanning.Planners;
  7. #if ADAPTIVE
  8. using TS = ADAPTPlanning.Domains.AdaptiveHighwayState;
  9. #else
  10. using TS = State;
  11. #endif
  12. public partial class PlannerGizmoAdaptive
  13. {
  14. /// <summary>
  15. /// Whether to show the resulting plan gizmos.
  16. /// </summary>
  17. public bool ShowPlan = true;
  18. /// <summary>
  19. /// Whether to show the endpoint state gizmos.
  20. /// </summary>
  21. public bool ShowEndpoints = true;
  22. /// <summary>
  23. /// Whether to show the open state transitions.
  24. /// </summary>
  25. public bool ShowOpen = true;
  26. /// <summary>
  27. /// Whether to show the inconsistent state transitions.
  28. /// </summary>
  29. public bool ShowIncons = true;
  30. /// <summary>
  31. /// Whether to show the invalid state transitions.
  32. /// </summary>
  33. public bool ShowInvalid = true;
  34. /// <summary>
  35. /// Whether to show the visited state transitions.
  36. /// </summary>
  37. public bool ShowVisited = true;
  38. /// <summary>
  39. /// Whether to show cost debug text.
  40. /// </summary>
  41. public bool ShowText;
  42. /// <summary>
  43. /// Draw all of the gizmos that show the current state of the planner.
  44. /// </summary>
  45. void OnDrawGizmos()
  46. {
  47. var cubesize = new UnityEngine.Vector3(0.5f, 0.5f, 0.5f);
  48. if (Planner != null) {
  49. DrawVisited();
  50. DrawIncons();
  51. DrawOpen(cubesize);
  52. DrawInvalid(cubesize);
  53. DrawText();
  54. DrawPlan();
  55. }
  56. DrawEndpoints(cubesize);
  57. }
  58. void DrawVisited()
  59. {
  60. var visited = Planner.Visited;
  61. if (!ShowVisited || visited == null) {
  62. return;
  63. }
  64. Gizmos.color = Color.magenta;
  65. foreach (var t in visited) {
  66. if (t == null) {
  67. continue;
  68. }
  69. Gizmos.DrawLine(t.From.Position.ToUnity(),
  70. t.To.Position.ToUnity());
  71. }
  72. var arastar = Planner as ARAStar<TS>;
  73. if (arastar != null) {
  74. Gizmos.color = Color.red;
  75. foreach (var n in arastar.VisitedNodes) {
  76. var t = n.Transition;
  77. if (t != null && n.Closed) {
  78. Gizmos.DrawLine(t.From.Position.ToUnity(),
  79. t.To.Position.ToUnity());
  80. }
  81. }
  82. }
  83. var dijkstra = Planner as Dijkstra<TS>;
  84. if (dijkstra != null) {
  85. Gizmos.color = Color.red;
  86. foreach (var n in dijkstra.VisitedNodes) {
  87. var t = n.Transition;
  88. if (t != null && dijkstra.Closed.Contains(n)) {
  89. Gizmos.DrawLine(t.From.Position.ToUnity(),
  90. t.To.Position.ToUnity());
  91. }
  92. }
  93. }
  94. var adstar = Planner as ADStar<TS>;
  95. if (adstar != null) {
  96. Gizmos.color = Color.red;
  97. foreach (var n in adstar.VisitedNodes) {
  98. var t = n.Transition;
  99. if (t != null && adstar.Closed.Contains(n)) {
  100. Gizmos.DrawLine(t.From.Position.ToUnity(),
  101. t.To.Position.ToUnity());
  102. }
  103. }
  104. }
  105. }
  106. void DrawIncons()
  107. {
  108. if (!ShowIncons) {
  109. return;
  110. }
  111. var arastar = Planner as ARAStar<TS>;
  112. if (arastar != null && arastar.InconsNodes != null) {
  113. Gizmos.color = Color.yellow;
  114. foreach (var n in arastar.InconsNodes) {
  115. var t = n.Transition;
  116. Gizmos.DrawLine(t.From.Position.ToUnity(),
  117. t.To.Position.ToUnity());
  118. }
  119. }
  120. var adstar = Planner as ADStar<TS>;
  121. if (adstar != null && adstar.Incons != null && adstar.Incons.Count != 0) {
  122. Gizmos.color = Color.yellow;
  123. foreach (var n in adstar.Incons) {
  124. var t = n.Transition;
  125. Gizmos.DrawLine(t.From.Position.ToUnity(),
  126. t.To.Position.ToUnity());
  127. }
  128. }
  129. }
  130. void DrawInvalid(UnityEngine.Vector3 cubesize)
  131. {
  132. if (!ShowInvalid) {
  133. return;
  134. }
  135. var pl = Planner as PlannerDynamic<Domain<TS>, TS>;
  136. if (pl != null && pl.InvalidStates != null && pl.InvalidStates.Count != 0) {
  137. Gizmos.color = Color.gray;
  138. foreach (var s in pl.InvalidStates) {
  139. Gizmos.DrawCube(s.Position.ToUnity(), cubesize);
  140. }
  141. }
  142. }
  143. void DrawOpen(UnityEngine.Vector3 cubesize)
  144. {
  145. var open = Planner.Open;
  146. if (!ShowOpen || open == null) {
  147. return;
  148. }
  149. Gizmos.color = Color.green;
  150. foreach (var s in open) {
  151. Gizmos.DrawCube(s.Position.ToUnity(), cubesize);
  152. }
  153. }
  154. void DrawText()
  155. {
  156. if (!(ShowText && ShowVisited)) {
  157. return;
  158. }
  159. var dijkstra = Planner as Dijkstra<TS>;
  160. if (dijkstra != null && dijkstra.VisitedNodes != null) {
  161. foreach (var n in dijkstra.VisitedNodes) {
  162. var ce = float.IsInfinity(n.CostEstimate) ?
  163. "" : n.CostEstimate.ToString("00.0");
  164. TextGizmo.Draw(n.State.Position.ToUnity(), ce);
  165. }
  166. return;
  167. }
  168. var astar = Planner as AStar<TS>;
  169. if (astar != null && astar.VisitedNodes != null) {
  170. foreach (var n in astar.VisitedNodes) {
  171. var pc = float.IsInfinity(n.PastCost) ?
  172. "" : n.PastCost.ToString("00.0");
  173. var ce = float.IsInfinity(n.CostEstimate) ?
  174. "" : n.CostEstimate.ToString("00.0");
  175. var s = string.Format("{0}\n{1}", pc, ce);
  176. TextGizmo.Draw(n.State.Position.ToUnity(), s);
  177. }
  178. return;
  179. }
  180. var adstar = Planner as ADStar<TS>;
  181. if (adstar != null && adstar.VisitedNodes != null) {
  182. foreach (var n in adstar.VisitedNodes) {
  183. var pc = float.IsInfinity(n.PastCost) ?
  184. "" : n.PastCost.ToString("00.0");
  185. var ce = float.IsInfinity(n.OneStepLookaheadCost) ?
  186. "" : n.OneStepLookaheadCost.ToString("00.0");
  187. var s = string.Format("{0}\n{1}", pc, ce);
  188. TextGizmo.Draw(n.State.Position.ToUnity(), s);
  189. }
  190. return;
  191. }
  192. }
  193. void DrawPlan()
  194. {
  195. var plan = Planner.Plan;
  196. if (!ShowPlan || plan == null) {
  197. return;
  198. }
  199. Gizmos.color = Planner.Status.Path == Status.PathStatus.Optimal ?
  200. Color.blue : Color.cyan;
  201. foreach (var t in plan) {
  202. Gizmos.DrawLine(t.From.Position.ToUnity(),
  203. t.To.Position.ToUnity());
  204. }
  205. }
  206. void DrawEndpoints(UnityEngine.Vector3 cubesize)
  207. {
  208. if (!ShowEndpoints) {
  209. return;
  210. }
  211. Gizmos.color = Color.blue;
  212. if (S1 != null) {
  213. Gizmos.DrawCube(S1.Position.ToUnity(), cubesize);
  214. }
  215. if (S2 != null) {
  216. Gizmos.DrawCube(S2.Position.ToUnity(), cubesize);
  217. }
  218. }
  219. }
  220. }