Helpers.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using AdditionalMathf;
  6. using UnityEngine;
  7. public class Helpers
  8. {
  9. public static void DrawPlane(Vector3 position, Vector3 normal)
  10. {
  11. Vector3 v3;
  12. if (normal.normalized != Vector3.forward)
  13. v3 = Vector3.Cross(normal, Vector3.forward).normalized * normal.magnitude;
  14. else
  15. v3 = Vector3.Cross(normal, Vector3.up).normalized * normal.magnitude;
  16. ;
  17. var corner0 = position + v3;
  18. var corner2 = position - v3;
  19. var q = Quaternion.AngleAxis(90.0f, normal);
  20. v3 = q * v3;
  21. var corner1 = position + v3;
  22. var corner3 = position - v3;
  23. Debug.DrawLine(corner0, corner2, Color.green);
  24. Debug.DrawLine(corner1, corner3, Color.green);
  25. Debug.DrawLine(corner0, corner1, Color.green);
  26. Debug.DrawLine(corner1, corner2, Color.green);
  27. Debug.DrawLine(corner2, corner3, Color.green);
  28. Debug.DrawLine(corner3, corner0, Color.green);
  29. Debug.DrawRay(position, normal, Color.red);
  30. }
  31. public static void DrawLine(Vector3 p1, Vector3 p2, float width)
  32. {
  33. int count = 1 + Mathf.CeilToInt(width); // how many lines are needed.
  34. if (count == 1)
  35. {
  36. Gizmos.DrawLine(p1, p2);
  37. }
  38. else
  39. {
  40. Camera c = Camera.current;
  41. if (c == null)
  42. {
  43. Debug.LogError("Camera.current is null");
  44. return;
  45. }
  46. var scp1 = c.WorldToScreenPoint(p1);
  47. var scp2 = c.WorldToScreenPoint(p2);
  48. Vector3 v1 = (scp2 - scp1).normalized; // line direction
  49. Vector3 n = Vector3.Cross(v1, Vector3.forward); // normal vector
  50. for (int i = 0; i < count; i++)
  51. {
  52. Vector3 o = 0.99f * n * width * ((float) i / (count - 1) - 0.5f);
  53. Vector3 origin = c.ScreenToWorldPoint(scp1 + o);
  54. Vector3 destiny = c.ScreenToWorldPoint(scp2 + o);
  55. Gizmos.DrawLine(origin, destiny);
  56. }
  57. }
  58. }
  59. public static float GetMinComponent(Vector3 vector)
  60. {
  61. if (vector.x <= vector.y && vector.x <= vector.z) return vector.x;
  62. if (vector.y <= vector.x && vector.y <= vector.z) return vector.y;
  63. return vector.z;
  64. }
  65. public static float GetMaxComponent(Vector3 vector)
  66. {
  67. if (vector.x > vector.y && vector.x > vector.z) return vector.x;
  68. if (vector.y > vector.x && vector.y > vector.z) return vector.y;
  69. return vector.z;
  70. }
  71. public static long RoundToLong(float f)
  72. {
  73. return (long) Mathf.Round(f);
  74. }
  75. public static Vector3 Vector3Abs(Vector3 value) =>
  76. new Vector3(Mathf.Abs(value.x), Mathf.Abs(value.y), Mathf.Abs(value.z));
  77. /// <summary>
  78. /// Remove outliers using the interquartile range method
  79. /// </summary>
  80. /// <param name="collection">A collection of floats, from which outliers are to be removed</param>
  81. /// <returns>A collection without the outliers</returns>
  82. public static IEnumerable<float> RemoveOutliers(IList<float> collection)
  83. {
  84. if (collection.Count < 4) return collection;
  85. var quartiles = new Quartiles(collection);
  86. var range = 1.5f * quartiles.Iqr;
  87. // see https://www.statology.org/outliers-excel/
  88. return collection.Where((v) => v >= quartiles.Q1 - range && v <= quartiles.Q3 + range);
  89. }
  90. public static IPAddress GetIPAddress()
  91. {
  92. IPAddress usedIp = null;
  93. foreach (var ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
  94. if (ip.AddressFamily == AddressFamily.InterNetwork)
  95. if (usedIp == null || ip.ToString().StartsWith("192.168.1."))
  96. usedIp = ip;
  97. return usedIp;
  98. }
  99. }