1
0

Helpers.cs 3.8 KB

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