UnityVectorExtensionTests.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Runtime.Serialization.Json;
  3. using UnityEngine;
  4. using NUnit.Framework;
  5. using Cinemachine.Utility;
  6. using UnityEditor;
  7. using UnityEngine.TestTools.Utils;
  8. //using Assert = UnityEngine.Assertions.Assert;
  9. namespace Tests.Editor
  10. {
  11. public class UnityVectorExtensionTests
  12. {
  13. public enum IntersectionResult
  14. {
  15. Zero,
  16. Infinity,
  17. l1_p1,
  18. l1_p2,
  19. l2_p1,
  20. l2_p2
  21. }
  22. private static object[] IntersectionTestCases =
  23. {
  24. // l1_p1, l1_p2, l2_p1, l2_p2, expectedIntersectionType, expectedIntersectionResult
  25. new object[] {new Vector2(0, 1), new Vector2(0, -1), new Vector2(-1, 0), new Vector2(1, 0), 2, IntersectionResult.Zero},
  26. new object[] {new Vector2(0, 1), new Vector2(0, 0), new Vector2(-1, 0), new Vector2(1, 0), 2, IntersectionResult.Zero},
  27. new object[] {new Vector2(0, 2), new Vector2(0, 1), new Vector2(-1, 0), new Vector2(1, 0), 1, IntersectionResult.Zero},
  28. new object[] {new Vector2(0, 2), new Vector2(0, 1), new Vector2(1, 2), new Vector2(1, 1), 0, IntersectionResult.Infinity},
  29. new object[] {new Vector2(1, 2), new Vector2(1, 1), new Vector2(1, -2), new Vector2(1, -1), 3, IntersectionResult.Infinity},
  30. new object[] {new Vector2(1, 2), new Vector2(1, -2), new Vector2(1, 3), new Vector2(1, 1), 4, IntersectionResult.Infinity},
  31. new object[] {new Vector2(1, 2), new Vector2(1, -2), new Vector2(1, 2), new Vector2(1, -2), 4, IntersectionResult.Infinity},
  32. new object[] {new Vector2(1, 2), new Vector2(1, -2), new Vector2(1, -2), new Vector2(1, 2), 4, IntersectionResult.Infinity},
  33. new object[] {new Vector2(0, 1), new Vector2(0, 1), new Vector2(1, 0), new Vector2(1, 0), 4, IntersectionResult.Infinity},
  34. new object[] {new Vector2(0, 0), new Vector2(2, 0), new Vector2(0, 1), new Vector2(1, 0), 2, IntersectionResult.l2_p2},
  35. new object[] {new Vector2(0, 0), new Vector2(2, 0), new Vector2(1, 0), new Vector2(0, 1), 2, IntersectionResult.l2_p1},
  36. // Parallel segments touching at one point
  37. new object[] {new Vector2(0, 3), new Vector2(0, 5), new Vector2(0, 5), new Vector2(0, 9), 4, IntersectionResult.l2_p1},
  38. new object[] {new Vector2(0, 5), new Vector2(0, 3), new Vector2(0, 5), new Vector2(0, 9), 4, IntersectionResult.l2_p1},
  39. new object[] {new Vector2(0, 3), new Vector2(0, 5), new Vector2(0, 9), new Vector2(0, 5), 4, IntersectionResult.l2_p2},
  40. new object[] {new Vector2(0, 5), new Vector2(0, 3), new Vector2(0, 9), new Vector2(0, 5), 4, IntersectionResult.l2_p2}
  41. };
  42. [Test, TestCaseSource(nameof(IntersectionTestCases))]
  43. public void FindIntersectionTest(Vector2 l1_p1, Vector2 l1_p2, Vector2 l2_p1, Vector2 l2_p2,
  44. int expectedIntersectionType, IntersectionResult expectedIntersectionResult)
  45. {
  46. int intersectionType = UnityVectorExtensions.FindIntersection(l1_p1, l1_p2, l2_p1, l2_p2,
  47. out Vector2 intersection);
  48. Assert.That(intersectionType, Is.EqualTo(expectedIntersectionType));
  49. switch (expectedIntersectionResult)
  50. {
  51. case IntersectionResult.Zero:
  52. Assert.That(intersection, Is.EqualTo(Vector2.zero).Using(Vector2EqualityComparer.Instance));
  53. break;
  54. case IntersectionResult.Infinity:
  55. Assert.That(float.IsInfinity(intersection.x), Is.True);
  56. Assert.That(float.IsInfinity(intersection.y), Is.True);
  57. break;
  58. case IntersectionResult.l1_p1:
  59. Assert.That(intersection, Is.EqualTo(l1_p1).Using(Vector2EqualityComparer.Instance));
  60. break;
  61. case IntersectionResult.l1_p2:
  62. Assert.That(intersection, Is.EqualTo(l1_p2).Using(Vector2EqualityComparer.Instance));
  63. break;
  64. case IntersectionResult.l2_p1:
  65. Assert.That(intersection, Is.EqualTo(l2_p1).Using(Vector2EqualityComparer.Instance));
  66. break;
  67. case IntersectionResult.l2_p2:
  68. Assert.That(intersection, Is.EqualTo(l2_p2).Using(Vector2EqualityComparer.Instance));
  69. break;
  70. }
  71. }
  72. private static object[] AngleTestCases =
  73. {
  74. new object[] {Vector2.left, 90f, true},
  75. new object[] {Vector2.right, 90f, true},
  76. new object[] {new Vector2(-0.0001f, 1f), 0.00572958f, false},
  77. new object[] {new Vector2(0.0001f, 1f), 0.00572958f, false}
  78. };
  79. [Test, TestCaseSource(nameof(AngleTestCases))]
  80. public void TestAngle(Vector2 v2, float expectedAngle, bool compareWithBuiltIn)
  81. {
  82. Vector3 v1 = Vector3.up;
  83. float angle = UnityVectorExtensions.Angle(v1, v2);
  84. Assert.That(angle, Is.EqualTo(expectedAngle).Within(UnityVectorExtensions.Epsilon));
  85. if (compareWithBuiltIn)
  86. {
  87. float angle2 = Vector2.Angle(v1, v2);
  88. Assert.That(angle2, Is.EqualTo(angle).Within(UnityVectorExtensions.Epsilon));
  89. }
  90. }
  91. private static object[] SignedAngleTestCases =
  92. {
  93. new object[] {Vector2.left, 90f, true},
  94. new object[] {Vector2.right, -90f, true},
  95. new object[] {new Vector2(-0.0001f, 1f), 0.00572958f, false},
  96. new object[] {new Vector2(0.0001f, 1f), -0.00572958f, false}
  97. };
  98. [Test, TestCaseSource(nameof(SignedAngleTestCases))]
  99. public void TestSignedAngle(Vector2 v2, float expectedAngle, bool compareWithBuiltIn)
  100. {
  101. Vector3 v1 = Vector3.up;
  102. float angle = UnityVectorExtensions.SignedAngle(v1, v2, Vector3.forward);
  103. Assert.That(angle, Is.EqualTo(expectedAngle).Within(UnityVectorExtensions.Epsilon));
  104. if (compareWithBuiltIn)
  105. {
  106. float angle2 = Vector2.SignedAngle(v1, v2);
  107. Assert.That(angle2, Is.EqualTo(angle).Within(UnityVectorExtensions.Epsilon));
  108. }
  109. float angle3 = UnityVectorExtensions.SignedAngle(v1, v2, Vector3.back);
  110. Assert.That(angle3, Is.EqualTo(-angle).Within(UnityVectorExtensions.Epsilon));
  111. }
  112. }
  113. }