GeomUtils.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using UnityEngine;
  3. public class GeomUtils {
  4. public static void test() {
  5. float ti;
  6. int times = 2;
  7. Boolean temp;
  8. int i;
  9. // Moller-Trumbore method
  10. ti = Time.realtimeSinceStartup;
  11. for (i = 0; i < times; i++) {
  12. temp = rayIntersectsTriangleMT(new Vector3(2, 2, 0), new Vector3(0, 0, 20), new Vector3(0, 0, 10), new Vector3(0, 10, 10), new Vector3(10, 0, 10), true);
  13. if (i == 0) D.Log ("==> [true, ff=true] = " + temp);
  14. temp = rayIntersectsTriangleMT(new Vector3(2, 2, 0), new Vector3(0, 0, 20), new Vector3(0, 0, 10), new Vector3(10, 0, 10), new Vector3(0, 10, 10), true);
  15. if (i == 0) D.Log ("==> [true, ff=false] = " + temp);
  16. temp = rayIntersectsTriangleMT(new Vector3(-2, 2, 0), new Vector3(0, 0, 20), new Vector3(0, 0, 10), new Vector3(10, 0, 10), new Vector3(0, 10, 10), true);
  17. if (i == 0) D.Log ("==> [false] = " + temp);
  18. temp = rayIntersectsTriangleMT(new Vector3(-2, 2, 10), new Vector3(10, 0, 10), new Vector3(0, 0, 10), new Vector3(10, 0, 10), new Vector3(0, 10, 10), true);
  19. if (i == 0) D.Log ("==> [false] = " + temp);
  20. temp = rayIntersectsTriangleMT(new Vector3(-492.3f, 375.0f, 1.0f), new Vector3(0.0f, 0.0f, -1.0f), new Vector3(-553.8f, 250.0f, 0.0f), new Vector3(-615.4f, 375.0f, 0.0f), new Vector3(-492.3f, 375.0f, 0.0f), true);
  21. if (i == 0) D.Log ("==> [true] = " + temp);
  22. temp = rayIntersectsTriangleMT(new Vector3(-492.3f, 375.0f, 1.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(-553.8f, 250.0f, 0.0f), new Vector3(-615.4f, 375.0f, 0.0f), new Vector3(-492.3f, 375.0f, 0.0f), true);
  23. if (i == 0) D.Log ("==> [false] = " + temp);
  24. }
  25. D.Log("[MT] Time for " + times + " calls: " + (Time.realtimeSinceStartup - ti) + "s");
  26. // Segura-Feito method method
  27. ti = Time.realtimeSinceStartup;
  28. for (i = 0; i < times; i++) {
  29. temp = rayIntersectsTriangleSF(new Vector3(2, 2, 0), new Vector3(0, 0, 20), new Vector3(0, 0, 10), new Vector3(0, 10, 10), new Vector3(10, 0, 10));
  30. if (i == 0) D.Log ("==> [true, hit ccw] = " + temp);
  31. temp = rayIntersectsTriangleSF(new Vector3(2, 2, 0), new Vector3(0, 0, 20), new Vector3(0, 0, 10), new Vector3(10, 0, 10), new Vector3(0, 10, 10));
  32. if (i == 0) D.Log ("==> [true, hit cw] = " + temp);
  33. temp = rayIntersectsTriangleSF(new Vector3(-2, 2, 0), new Vector3(0, 0, 20), new Vector3(0, 0, 10), new Vector3(10, 0, 10), new Vector3(0, 10, 10));
  34. if (i == 0) D.Log ("==> [false] = " + temp);
  35. temp = rayIntersectsTriangleSF(new Vector3(-2, 2, 10), new Vector3(10, 0, 10), new Vector3(0, 0, 10), new Vector3(10, 0, 10), new Vector3(0, 10, 10));
  36. if (i == 0) D.Log ("==> [false] = " + temp);
  37. temp = rayIntersectsTriangleSF(new Vector3(-492.3f, 375.0f, 1.0f), new Vector3(0.0f, 0.0f, -1.0f), new Vector3(-553.8f, 250.0f, 0.0f), new Vector3(-615.4f, 375.0f, 0.0f), new Vector3(-492.3f, 375.0f, 0.0f));
  38. if (i == 0) D.Log ("==> [true] = " + temp);
  39. temp = rayIntersectsTriangleSF(new Vector3(-492.3f, 375.0f, 1.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(-553.8f, 250.0f, 0.0f), new Vector3(-615.4f, 375.0f, 0.0f), new Vector3(-492.3f, 375.0f, 0.0f));
  40. if (i == 0) D.Log ("==> [false] = " + temp);
  41. }
  42. D.Log("[SF] Time for " + times + " calls: " + (Time.realtimeSinceStartup - ti) + "s");
  43. // Segura-Feito method method with precalculation
  44. ti = Time.realtimeSinceStartup;
  45. Vector3 v1xv2 = Vector3.Cross(new Vector3(0, 0, 10) - new Vector3(-2, -2, 0), new Vector3(10, 0, 10)- new Vector3(-2, -2, 0));
  46. Vector3 v2xv3 = Vector3.Cross(new Vector3(10, 0, 10)- new Vector3(-2, -2, 0), new Vector3(0, 10, 10)- new Vector3(-2, -2, 0));
  47. Vector3 v3xv1 = Vector3.Cross(new Vector3(0, 10, 10)- new Vector3(-2, -2, 0), new Vector3(0, 0, 10)- new Vector3(-2, -2, 0));
  48. for (i = 0; i < times; i++) {
  49. temp = rayIntersectsTriangleSF_precalculated(new Vector3(2, 2, 0), new Vector3(0, 0, 20), new Vector3(0, 0, 10), new Vector3(0, 10, 10), new Vector3(10, 0, 10));
  50. if (i == 0) D.Log ("==> [true, hit ccw] = " + temp);
  51. temp = rayIntersectsTriangleSF_precalculated(new Vector3(2, 2, 0), new Vector3(0, 0, 20), v1xv2, v2xv3, v3xv1);
  52. if (i == 0) D.Log ("==> [true, hit cw] = " + temp);
  53. temp = rayIntersectsTriangleSF_precalculated(new Vector3(-2, 2, 0), new Vector3(0, 0, 20), v1xv2, v2xv3, v3xv1);
  54. if (i == 0) D.Log ("==> [false] = " + temp);
  55. temp = rayIntersectsTriangleSF_precalculated(new Vector3(-2, 2, 10), new Vector3(10, 0, 10), v1xv2, v2xv3, v3xv1);
  56. if (i == 0) D.Log ("==> [false] = " + temp);
  57. temp = rayIntersectsTriangleSF(new Vector3(-492.3f, 375.0f, 1.0f), new Vector3(0.0f, 0.0f, -1.0f), new Vector3(-553.8f, 250.0f, 0.0f), new Vector3(-615.4f, 375.0f, 0.0f), new Vector3(-492.3f, 375.0f, 0.0f));
  58. if (i == 0) D.Log ("==> [true] = " + temp);
  59. temp = rayIntersectsTriangleSF(new Vector3(-492.3f, 375.0f, 1.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(-553.8f, 250.0f, 0.0f), new Vector3(-615.4f, 375.0f, 0.0f), new Vector3(-492.3f, 375.0f, 0.0f));
  60. if (i == 0) D.Log ("==> [false] = " + temp);
  61. }
  62. D.Log("[SF PRECALC] Time for " + times + " calls: " + (Time.realtimeSinceStartup - ti) + "s");
  63. //D.Log("Intersect 3 ==> " + rayIntersectsTriangle(new Vector3(2, 2, 100), new Vector3(0, 0, -200), new Vector3(-1000, -1000, 10), new Vector3(1000, -1000, 10), new Vector3(0, 500, 10)));
  64. //D.Log("Intersect 4 ==> " + rayIntersectsTriangle(new Vector3(2, 2, 100), new Vector3(0, 0, -200), new Vector3(-1000, -1000, 10), new Vector3(0, 500, 10), new Vector3(1000, -1000, 10)));
  65. //D.Log("Intersect 3 ==> " + segmentIntersectsTriangle(new Vector3(2, 2, 0), new Vector3(2, 2, 20), new Vector3(0, 0, 10), new Vector3(0, 10, 10), new Vector3(10, 0, 10)));
  66. //D.Log("Intersect 4 ==> " + segmentIntersectsTriangle(new Vector3(2, 2, 0), new Vector3(2, 2, 20), new Vector3(0, 0, 10), new Vector3(10, 0, 10), new Vector3(0, 10, 10)));
  67. }
  68. public static bool rayIntersectsTriangleMT(Vector3 __origin, Vector3 __direction, Vector3 __v1, Vector3 __v2, Vector3 __v3, bool __allowSegmentOnly) {
  69. // Moller-Trumbore method
  70. // http://www.hugi.scene.org/online/coding/hugi%2025%20-%20coding%20corner%20graphics,%20sound%20&%20synchronization%20ken%20ray-triangle%20intersection%20tests%20for%20dummies.htm
  71. // Returns: float u, float v, float t, bool intersect, bool frontfacing
  72. float eps = 0.000000001f; // eps is the machine fpu epsilon (precision), or a very small number :)
  73. Vector3 e2 = __v3 - __v1; // second edge
  74. Vector3 e1 = __v2 - __v1; // first edge
  75. Vector3 r = Vector3.Cross(__direction, e2); // (d X e2) is used two times in the formula so we store it in an appropriate vector
  76. Vector3 s = __origin - __v1; // translated ray origin
  77. float a = Vector3.Dot(e1, r); // a=(d X e2)*e1
  78. Vector3 q = Vector3.Cross(s, e1);
  79. float u = Vector3.Dot(s, r);
  80. //bool frontfacing = true;
  81. float v;
  82. if (a > eps) {
  83. // Front facing triangle...
  84. if ((u < 0) || (u > a)) {
  85. //D.Log ("u = " + 0 + ", v = " + 0 + ", t = " + 0 + ", intersect = false, frontfacing = " + frontfacing);
  86. return false;
  87. }
  88. v = Vector3.Dot(__direction, q);
  89. if ((v < 0) || (u+v > a)) {
  90. //D.Log ("u = " + 0 + ", v = " + 0 + ", t = " + 0 + ", intersect = false, frontfacing = " + frontfacing);
  91. return false;
  92. }
  93. } else if (a < -eps) {
  94. // Back facing triangle...
  95. //frontfacing = false;
  96. if ((u > 0) || (u < a)) {
  97. //D.Log ("u = " + 0 + ", v = " + 0 + ", t = " + 0 + ", intersect = false, frontfacing = " + frontfacing);
  98. return false;
  99. }
  100. v = Vector3.Dot(__direction, q);
  101. if ((v > 0) || (u+v < a)) {
  102. //D.Log ("u = " + 0 + ", v = " + 0 + ", t = " + 0 + ", intersect = false, frontfacing = " + frontfacing);
  103. return false;
  104. }
  105. } else {
  106. // Ray parallel to triangle plane
  107. //D.Log ("u = " + 0 + ", v = " + 0 + ", t = " + 0 + ", intersect = false, frontfacing = false [PARALLEL]");
  108. return false;
  109. }
  110. float f = 1f / a; // slow division*
  111. float t = f * Vector3.Dot(e2, q);
  112. //u = u * f;
  113. //v = v * f;
  114. //D.Log ("u = " + u + ", v = " + v + ", t = " + t + ", intersect = true, frontfacing = " + frontfacing);
  115. return t > 0 && (!__allowSegmentOnly || t <= 1);
  116. //return true;
  117. }
  118. public static bool rayIntersectsTriangleSF_old(Vector3 __origin, Vector3 __direction, Vector3 __v1, Vector3 __v2, Vector3 __v3) {
  119. // Segura-Feito method
  120. /*
  121. __v1 -= __origin;
  122. __v2 -= __origin;
  123. __v3 -= __origin;
  124. Vector3 tmp = Vector3.Cross(__direction, __v1);
  125. float i = Mathf.Sign(Vector3.Dot(__v3, tmp));
  126. if (i<=0) return false;
  127. i = Mathf.Sign(Vector3.Dot(__v2, tmp));
  128. if (i<=0) return false;
  129. i = Mathf.Sign(Vector3.Dot(__v3, Vector3.Cross(__direction, __v2)));
  130. if (i<=0) return false;
  131. */
  132. float i = Mathf.Sign(Vector3.Dot(__direction, Vector3.Cross(__v1 - __origin, __v2 - __origin)));
  133. if (i<=0) return false;
  134. i = Mathf.Sign(Vector3.Dot(__direction, Vector3.Cross(__v2 - __origin, __v3 - __origin)));
  135. if (i<=0) return false;
  136. i = Mathf.Sign(Vector3.Dot(__direction, Vector3.Cross(__v3 - __origin, __v1 - __origin)));
  137. if (i<=0) return false;
  138. return true;
  139. }
  140. public static bool rayIntersectsTriangleSF(Vector3 __origin, Vector3 __direction, Vector3 __v1, Vector3 __v2, Vector3 __v3) {
  141. return rayIntersectsTriangleSF_precalculated(__origin, __direction, Vector3.Cross(__v1 - __origin, __v2 - __origin), Vector3.Cross(__v2 - __origin, __v3 - __origin), Vector3.Cross(__v3 - __origin, __v1 - __origin));
  142. }
  143. public static bool rayIntersectsTriangleSF_precalculated(Vector3 __origin, Vector3 __direction, Vector3 __v1xv2, Vector3 __v2xv3, Vector3 __v3xv1) {
  144. if (Mathf.Sign(Vector3.Dot(__direction, __v1xv2)) <= 0) return false;
  145. if (Mathf.Sign(Vector3.Dot(__direction, __v2xv3)) <= 0) return false;
  146. if (Mathf.Sign(Vector3.Dot(__direction, __v3xv1)) <= 0) return false;
  147. return true;
  148. }
  149. public static bool rayIntersectsTriangleSF2(Vector3 __start, Vector3 __end, Vector3 __v1, Vector3 __v2, Vector3 __v3, bool __someBool) {
  150. // http://www.hugi.scene.org/online/coding/hugi%2025%20-%20coding%20corner%20graphics,%20sound%20&%20synchronization%20ken%20ray-triangle%20intersection%20tests%20for%20dummies.htm
  151. // http://iason.zcu.cz/wscg2001/Papers_2001/R75.pdf
  152. __end += __start;
  153. //D.Log (" ==> " + areaSign(__start, __end, __v1, __v3));
  154. //D.Log (" ==> " + areaSign(__start, __end, __v2, __v3));
  155. //D.Log (" ==> " + areaSign(__start, __end, __v1, __v2));
  156. if (areaSign(__start, __end, __v1, __v3) <= 0) return false;
  157. if (areaSign(__start, __end, __v2, __v3) <= 0) return false;
  158. if (areaSign(__start, __end, __v1, __v2) <= 0) return false;
  159. return true;
  160. }
  161. public static float areaSign(Vector3 rs, Vector3 re, Vector3 v1, Vector3 v2) {
  162. return Mathf.Sign(v2.z * (re.x * v1.y - v1.x * re.y) + v2.y * (v1.z * re.x - re.z * v1.x) + v2.x * (v1.y * re.z - re.y * v1.z));
  163. //return Mathf.Sign(Vector3.Dot(re, Vector3.Cross(v1, v2)));
  164. }
  165. /*
  166. public bool rayIntersectsTriangle(Vector3 __origin, Vector3 __direction, Vector3 __v1, Vector3 __v2, Vector3 __v3) {
  167. __v1 -= __origin;
  168. __v2 -= __origin;
  169. __v3 -= __origin;
  170. Vector3 tmp = Vector3.Cross(__direction, __v1);
  171. D.Log (" ==> " + Mathf.Sign(Vector3.Dot(__v3, tmp)));
  172. D.Log (" ==> " + Mathf.Sign(Vector3.Dot(__v2, tmp)));
  173. D.Log (" ==> " + Mathf.Sign(Vector3.Dot(__v3, Vector3.Cross(__direction, __v2))));
  174. float i = Mathf.Sign(Vector3.Dot(__v3, tmp));
  175. if (i<=0) return false;
  176. i = Mathf.Sign(Vector3.Dot(__v2, tmp));
  177. if (i<=0) return false;
  178. i = Mathf.Sign(Vector3.Dot(__v3, Vector3.Cross(__direction, __v2)));
  179. if (i<=0) return false;
  180. return true;
  181. }
  182. */
  183. }