TriangleTest.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. //---------------------------------------------------------------------------------------------------------------------
  2. // TriangleTest.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //---------------------------------------------------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. namespace CollisionSample
  10. {
  11. /// <summary>
  12. /// Represents a simple triangle by the vertices at each corner.
  13. /// </summary>
  14. public struct Triangle
  15. {
  16. public Vector3 V0;
  17. public Vector3 V1;
  18. public Vector3 V2;
  19. public Triangle(Vector3 v0, Vector3 v1, Vector3 v2)
  20. {
  21. V0 = v0;
  22. V1 = v1;
  23. V2 = v2;
  24. }
  25. }
  26. /// <summary>
  27. /// Triangle-based collision tests
  28. /// </summary>
  29. public static class TriangleTest
  30. {
  31. const float EPSILON = 1e-20F;
  32. #region Triangle-BoundingBox
  33. /// <summary>
  34. /// Returns true if the given box intersects the triangle (v0,v1,v2).
  35. /// </summary>
  36. public static bool Intersects(ref BoundingBox box, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  37. {
  38. Vector3 boxCenter = (box.Max + box.Min) * 0.5f;
  39. Vector3 boxHalfExtent = (box.Max - box.Min) * 0.5f;
  40. // Transform the triangle into the local space with the box center at the origin
  41. Triangle localTri = new Triangle();
  42. Vector3.Subtract(ref v0, ref boxCenter, out localTri.V0);
  43. Vector3.Subtract(ref v1, ref boxCenter, out localTri.V1);
  44. Vector3.Subtract(ref v2, ref boxCenter, out localTri.V2);
  45. return OriginBoxContains(ref boxHalfExtent, ref localTri) != ContainmentType.Disjoint;
  46. }
  47. /// <summary>
  48. /// Tests whether the given box contains, intersects, or is disjoint from the triangle (v0,v1,v2).
  49. /// </summary>
  50. public static ContainmentType Contains(ref BoundingBox box, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  51. {
  52. Vector3 boxCenter = (box.Max + box.Min) * 0.5f;
  53. Vector3 boxHalfExtent = (box.Max - box.Min) * 0.5f;
  54. // Transform the triangle into the local space with the box center at the origin
  55. Triangle localTri;
  56. Vector3.Subtract(ref v0, ref boxCenter, out localTri.V0);
  57. Vector3.Subtract(ref v1, ref boxCenter, out localTri.V1);
  58. Vector3.Subtract(ref v2, ref boxCenter, out localTri.V2);
  59. return OriginBoxContains(ref boxHalfExtent, ref localTri);
  60. }
  61. /// <summary>
  62. /// Tests whether the given box contains, intersects, or is disjoint from the given triangle.
  63. /// </summary>
  64. public static ContainmentType Contains(ref BoundingBox box, ref Triangle triangle)
  65. {
  66. return Contains(ref box, ref triangle.V0, ref triangle.V1, ref triangle.V2);
  67. }
  68. #endregion
  69. #region Triangle-BoundingOrientedBox
  70. /// <summary>
  71. /// Returns true if the given BoundingOrientedBox intersects the triangle (v0,v1,v2)
  72. /// </summary>
  73. public static bool Intersects(ref BoundingOrientedBox obox, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  74. {
  75. // Transform the triangle into the local space of the box, so we can use a
  76. // faster axis-aligned box test.
  77. // Note than when transforming more than one point, using an intermediate matrix
  78. // is faster than doing multiple quaternion transforms directly.
  79. Quaternion qinv;
  80. Quaternion.Conjugate(ref obox.Orientation, out qinv);
  81. Matrix minv = Matrix.CreateFromQuaternion(qinv);
  82. Triangle localTri = new Triangle();
  83. localTri.V0 = Vector3.TransformNormal(v0 - obox.Center, minv);
  84. localTri.V1 = Vector3.TransformNormal(v1 - obox.Center, minv);
  85. localTri.V2 = Vector3.TransformNormal(v2 - obox.Center, minv);
  86. return OriginBoxContains(ref obox.HalfExtent, ref localTri) != ContainmentType.Disjoint;
  87. }
  88. /// <summary>
  89. /// Determines whether the given BoundingOrientedBox contains/intersects/is disjoint from the triangle
  90. /// (v0,v1,v2)
  91. /// </summary>
  92. public static ContainmentType Contains(ref BoundingOrientedBox obox, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  93. {
  94. // Transform the triangle into the local space of the box, so we can use a
  95. // faster axis-aligned box test.
  96. // Note than when transforming more than one point, using an intermediate matrix
  97. // is faster than doing multiple quaternion transforms directly.
  98. Quaternion qinv;
  99. Quaternion.Conjugate(ref obox.Orientation, out qinv);
  100. Matrix minv;
  101. Matrix.CreateFromQuaternion(ref qinv, out minv);
  102. Triangle localTri = new Triangle();
  103. localTri.V0 = Vector3.TransformNormal(v0 - obox.Center, minv);
  104. localTri.V1 = Vector3.TransformNormal(v1 - obox.Center, minv);
  105. localTri.V2 = Vector3.TransformNormal(v2 - obox.Center, minv);
  106. return OriginBoxContains(ref obox.HalfExtent, ref localTri);
  107. }
  108. /// <summary>
  109. /// Determines whether the given BoundingOrientedBox contains/intersects/is disjoint from the
  110. /// given triangle.
  111. /// </summary>
  112. public static ContainmentType Contains(ref BoundingOrientedBox obox, ref Triangle triangle)
  113. {
  114. return Contains(ref obox, ref triangle.V0, ref triangle.V1, ref triangle.V2);
  115. }
  116. #endregion
  117. #region Triangle-Sphere
  118. /// <summary>
  119. /// Returns true if the given sphere intersects the triangle (v0,v1,v2).
  120. /// </summary>
  121. public static bool Intersects(ref BoundingSphere sphere, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  122. {
  123. Vector3 p = NearestPointOnTriangle(ref sphere.Center, ref v0, ref v1, ref v2);
  124. return Vector3.DistanceSquared(sphere.Center, p) < sphere.Radius * sphere.Radius;
  125. }
  126. /// <summary>
  127. /// Returns true if the given sphere intersects the given triangle.
  128. /// </summary>
  129. public static bool Intersects(ref BoundingSphere sphere, ref Triangle t)
  130. {
  131. Vector3 p = NearestPointOnTriangle(ref sphere.Center, ref t.V0, ref t.V1, ref t.V2);
  132. return Vector3.DistanceSquared(sphere.Center, p) < sphere.Radius * sphere.Radius;
  133. }
  134. /// <summary>
  135. /// Determines whether the given sphere contains/intersects/is disjoint from the triangle
  136. /// (v0,v1,v2)
  137. /// </summary>
  138. public static ContainmentType Contains(ref BoundingSphere sphere, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  139. {
  140. float r2 = sphere.Radius * sphere.Radius;
  141. if (Vector3.DistanceSquared(v0, sphere.Center) <= r2 &&
  142. Vector3.DistanceSquared(v1, sphere.Center) <= r2 &&
  143. Vector3.DistanceSquared(v2, sphere.Center) <= r2)
  144. return ContainmentType.Contains;
  145. return Intersects(ref sphere, ref v0, ref v1, ref v2)
  146. ? ContainmentType.Intersects : ContainmentType.Disjoint;
  147. }
  148. /// <summary>
  149. /// Determines whether the given sphere contains/intersects/is disjoint from the
  150. /// given triangle.
  151. /// </summary>
  152. public static ContainmentType Contains(ref BoundingSphere sphere, ref Triangle triangle)
  153. {
  154. return Contains(ref sphere, ref triangle.V0, ref triangle.V1, ref triangle.V2);
  155. }
  156. #endregion
  157. #region Triangle-Frustum
  158. /// <summary>
  159. /// Returns true if the given frustum intersects the triangle (v0,v1,v2).
  160. /// </summary>
  161. public static bool Intersects(BoundingFrustum frustum, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  162. {
  163. // A BoundingFrustum is defined by a matrix that projects the frustum shape
  164. // into the box from (-1,-1,0) to (1,1,1). We will project the triangle
  165. // through this matrix, and then do a simpler box-triangle test.
  166. Matrix m = frustum.Matrix;
  167. Triangle localTri;
  168. GeomUtil.PerspectiveTransform(ref v0, ref m, out localTri.V0);
  169. GeomUtil.PerspectiveTransform(ref v1, ref m, out localTri.V1);
  170. GeomUtil.PerspectiveTransform(ref v2, ref m, out localTri.V2);
  171. BoundingBox box;
  172. box.Min = new Vector3(-1, -1, 0);
  173. box.Max = new Vector3(1, 1, 1);
  174. return Intersects(ref box, ref localTri.V0, ref localTri.V1, ref localTri.V2);
  175. }
  176. /// <summary>
  177. /// Determines whether the given frustum contains/intersects/is disjoint from the triangle
  178. /// (v0,v1,v2)
  179. /// </summary>
  180. public static ContainmentType Contains(BoundingFrustum frustum, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  181. {
  182. // A BoundingFrustum is defined by a matrix that projects the frustum shape
  183. // into the box from (-1,-1,0) to (1,1,1). We will project the triangle
  184. // through this matrix, and then do a simpler box-triangle test.
  185. Matrix m = frustum.Matrix;
  186. Triangle localTri;
  187. GeomUtil.PerspectiveTransform(ref v0, ref m, out localTri.V0);
  188. GeomUtil.PerspectiveTransform(ref v1, ref m, out localTri.V1);
  189. GeomUtil.PerspectiveTransform(ref v2, ref m, out localTri.V2);
  190. // Center the projected box at the origin
  191. Vector3 halfExtent = new Vector3(1, 1, 0.5f);
  192. localTri.V0.Z -= 0.5f;
  193. localTri.V1.Z -= 0.5f;
  194. localTri.V2.Z -= 0.5f;
  195. return OriginBoxContains(ref halfExtent, ref localTri);
  196. }
  197. /// <summary>
  198. /// Determines whether the given frustum contains/intersects/is disjoint from the
  199. /// given triangle.
  200. /// </summary>
  201. public static ContainmentType Contains(BoundingFrustum frustum, ref Triangle triangle)
  202. {
  203. return Contains(frustum, ref triangle.V0, ref triangle.V1, ref triangle.V2);
  204. }
  205. #endregion
  206. #region Triangle-Plane
  207. /// <summary>
  208. /// Classify the triangle (v0,v1,v2) with respect to the given plane.
  209. /// </summary>
  210. public static PlaneIntersectionType Intersects(ref Plane plane, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  211. {
  212. float dV0 = plane.DotCoordinate(v0);
  213. float dV1 = plane.DotCoordinate(v1);
  214. float dV2 = plane.DotCoordinate(v2);
  215. if (Math.Min(dV0, Math.Min(dV1, dV2)) >= 0)
  216. {
  217. return PlaneIntersectionType.Front;
  218. }
  219. if (Math.Max(dV0, Math.Max(dV1, dV2)) <= 0)
  220. {
  221. return PlaneIntersectionType.Back;
  222. }
  223. return PlaneIntersectionType.Intersecting;
  224. }
  225. #endregion
  226. #region Triangle-Ray
  227. /// <summary>
  228. /// Determine whether the triangle (v0,v1,v2) intersects the given ray. If there is intersection,
  229. /// returns the parametric value of the intersection point on the ray. Otherwise returns null.
  230. /// </summary>
  231. public static float? Intersects(ref Ray ray, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  232. {
  233. // The algorithm is based on Moller, Tomas and Trumbore, "Fast, Minimum Storage
  234. // Ray-Triangle Intersection", Journal of Graphics Tools, vol. 2, no. 1,
  235. // pp 21-28, 1997.
  236. Vector3 e1 = v1 - v0;
  237. Vector3 e2 = v2 - v0;
  238. Vector3 p = Vector3.Cross(ray.Direction, e2);
  239. float det = Vector3.Dot(e1, p);
  240. float t;
  241. if (det >= EPSILON)
  242. {
  243. // Determinate is positive (front side of the triangle).
  244. Vector3 s = ray.Position - v0;
  245. float u = Vector3.Dot(s, p);
  246. if (u < 0 || u > det)
  247. return null;
  248. Vector3 q = Vector3.Cross(s, e1);
  249. float v = Vector3.Dot(ray.Direction, q);
  250. if (v < 0 || ((u + v) > det))
  251. return null;
  252. t = Vector3.Dot(e2, q);
  253. if (t < 0)
  254. return null;
  255. }
  256. else if (det <= -EPSILON)
  257. {
  258. // Determinate is negative (back side of the triangle).
  259. Vector3 s = ray.Position - v0;
  260. float u = Vector3.Dot(s, p);
  261. if (u > 0 || u < det)
  262. return null;
  263. Vector3 q = Vector3.Cross(s, e1);
  264. float v = Vector3.Dot(ray.Direction, q);
  265. if (v > 0 || ((u + v) < det))
  266. return null;
  267. t = Vector3.Dot(e2, q);
  268. if (t > 0)
  269. return null;
  270. }
  271. else
  272. {
  273. // Parallel ray.
  274. return null;
  275. }
  276. return t / det;
  277. }
  278. /// <summary>
  279. /// Determine whether the given triangle intersects the given ray. If there is intersection,
  280. /// returns the parametric value of the intersection point on the ray. Otherwise returns null.
  281. /// </summary>
  282. public static float? Intersects(ref Ray ray, ref Triangle tri)
  283. {
  284. return Intersects(ref ray, ref tri.V0, ref tri.V1, ref tri.V2);
  285. }
  286. #endregion
  287. #region Common utility methods
  288. /// <summary>
  289. /// Return the point on triangle (v0,v1,v2) closest to point p.
  290. /// </summary>
  291. public static Vector3 NearestPointOnTriangle(ref Vector3 p, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2)
  292. {
  293. // We'll work in a space where v0 is the origin.
  294. // Let D=p-v0 be the local position of p, E1=v1-v0 and E2=v2-v0 be the
  295. // local positions of v1 and v2.
  296. //
  297. // Points on the triangle are defined by
  298. // P=v0 + s*E1 + t*E2
  299. // for s >= 0, t >= 0, s+t <= 1
  300. //
  301. // To compute (s,t) for p, note that s=the ratio of the components of d and e1 which
  302. // are perpendicular to e2 in the plane of the triangle.
  303. //
  304. // s = project(perp(D,E2),E1) / project(perp(E1,E2),E1)
  305. // where project(A,B) = B*(A . B)/(B . B)
  306. // perp(A,B) = A - project(A,B)
  307. //
  308. // expanding and rearranging terms a bit gives:
  309. //
  310. // (D . E1)*(E2 . E2) - (D . E2)*(E1 . E2)
  311. // s = ---------------------------------------
  312. // (E1 . E1)*(E2 . E2) - (E1 . E2)^2
  313. //
  314. // t = [same thing with E1/E2 swapped]
  315. //
  316. // Note that the denominator is the same for s and t, so we only need to compute it
  317. // once, and that the denominator is never negative. So we can compute the numerator
  318. // and denominator separately, and only do the division in case we actually need to
  319. // produce s and/or t.
  320. //
  321. // We also need the parametric projections of p onto each edge:
  322. // u1 onto E1, u2 onto E2, u12 onto the (v2-v1) edge.
  323. // u1 = (D . E1)/(E1 . E1)
  324. // u2 = (D . E2)/(E2 . E2)
  325. Vector3 D = p - v0;
  326. Vector3 E1 = (v1 - v0);
  327. Vector3 E2 = (v2 - v0);
  328. float dot11 = E1.LengthSquared();
  329. float dot12 = Vector3.Dot(E1, E2);
  330. float dot22 = E2.LengthSquared();
  331. float dot1d = Vector3.Dot(E1, D);
  332. float dot2d = Vector3.Dot(E2, D);
  333. float dotdd = D.LengthSquared();
  334. float s = dot1d * dot22 - dot2d * dot12;
  335. float t = dot2d * dot11 - dot1d * dot12;
  336. float d = dot11 * dot22 - dot12 * dot12;
  337. if (dot1d <= 0 && dot2d <= 0)
  338. {
  339. // nearest point is V0
  340. return v0;
  341. }
  342. if (s <= 0 && dot2d >= 0 && dot2d <= dot22)
  343. {
  344. // nearest point is on E2
  345. return v0 + E2 * (dot2d / dot22);
  346. }
  347. if (t <= 0 && dot1d >= 0 && dot1d <= dot11)
  348. {
  349. // nearest point is on E1
  350. return v0 + E1 * (dot1d / dot11);
  351. }
  352. if (s >= 0 && t >= 0 && s + t <= d)
  353. {
  354. // nearest point is inside the triangle
  355. float dr = 1.0f / d;
  356. return v0 + (s * dr) * E1 + (t * dr) * E2;
  357. }
  358. // we need to compute u12. This is hairier than
  359. // u1 or u2 because we're not in a convenient
  360. // basis any more.
  361. float u12_num = dot2d - dot1d - dot12 + dot11;
  362. float u12_den = dot22 + dot11 - 2 * dot12;
  363. if (u12_num <= 0)
  364. {
  365. return v1;
  366. }
  367. if (u12_num >= u12_den)
  368. {
  369. return v2;
  370. }
  371. return v1 + (v2 - v1) * (u12_num / u12_den);
  372. }
  373. /// <summary>
  374. /// Check if an origin-centered, axis-aligned box with the given half extents contains,
  375. /// intersects, or is disjoint from the given triangle. This is used for the box and
  376. /// frustum vs. triangle tests.
  377. /// </summary>
  378. public static ContainmentType OriginBoxContains(ref Vector3 halfExtent, ref Triangle tri)
  379. {
  380. BoundingBox triBounds = new BoundingBox(); // 'new' to work around NetCF bug
  381. triBounds.Min.X = Math.Min(tri.V0.X, Math.Min(tri.V1.X, tri.V2.X));
  382. triBounds.Min.Y = Math.Min(tri.V0.Y, Math.Min(tri.V1.Y, tri.V2.Y));
  383. triBounds.Min.Z = Math.Min(tri.V0.Z, Math.Min(tri.V1.Z, tri.V2.Z));
  384. triBounds.Max.X = Math.Max(tri.V0.X, Math.Max(tri.V1.X, tri.V2.X));
  385. triBounds.Max.Y = Math.Max(tri.V0.Y, Math.Max(tri.V1.Y, tri.V2.Y));
  386. triBounds.Max.Z = Math.Max(tri.V0.Z, Math.Max(tri.V1.Z, tri.V2.Z));
  387. Vector3 triBoundhalfExtent;
  388. triBoundhalfExtent.X = (triBounds.Max.X - triBounds.Min.X) * 0.5f;
  389. triBoundhalfExtent.Y = (triBounds.Max.Y - triBounds.Min.Y) * 0.5f;
  390. triBoundhalfExtent.Z = (triBounds.Max.Z - triBounds.Min.Z) * 0.5f;
  391. Vector3 triBoundCenter;
  392. triBoundCenter.X = (triBounds.Max.X + triBounds.Min.X) * 0.5f;
  393. triBoundCenter.Y = (triBounds.Max.Y + triBounds.Min.Y) * 0.5f;
  394. triBoundCenter.Z = (triBounds.Max.Z + triBounds.Min.Z) * 0.5f;
  395. if (triBoundhalfExtent.X + halfExtent.X <= Math.Abs(triBoundCenter.X) ||
  396. triBoundhalfExtent.Y + halfExtent.Y <= Math.Abs(triBoundCenter.Y) ||
  397. triBoundhalfExtent.Z + halfExtent.Z <= Math.Abs(triBoundCenter.Z))
  398. {
  399. return ContainmentType.Disjoint;
  400. }
  401. if (triBoundhalfExtent.X + Math.Abs(triBoundCenter.X) <= halfExtent.X &&
  402. triBoundhalfExtent.Y + Math.Abs(triBoundCenter.Y) <= halfExtent.Y &&
  403. triBoundhalfExtent.Z + Math.Abs(triBoundCenter.Z) <= halfExtent.Z)
  404. {
  405. return ContainmentType.Contains;
  406. }
  407. Vector3 edge1, edge2, edge3;
  408. Vector3.Subtract(ref tri.V1, ref tri.V0, out edge1);
  409. Vector3.Subtract(ref tri.V2, ref tri.V0, out edge2);
  410. Vector3 normal;
  411. Vector3.Cross(ref edge1, ref edge2, out normal);
  412. float triangleDist = Vector3.Dot(tri.V0, normal);
  413. if(Math.Abs(normal.X*halfExtent.X) + Math.Abs(normal.Y*halfExtent.Y) + Math.Abs(normal.Z*halfExtent.Z) <= Math.Abs(triangleDist))
  414. {
  415. return ContainmentType.Disjoint;
  416. }
  417. // Worst case: we need to check all 9 possible separating planes
  418. // defined by Cross(box edge,triangle edge)
  419. // Check for separation in plane containing an axis of box A and and axis of box B
  420. //
  421. // We need to compute all 9 cross products to find them, but a lot of terms drop out
  422. // since we're working in A's local space. Also, since each such plane is parallel
  423. // to the defining axis in each box, we know those dot products will be 0 and can
  424. // omit them.
  425. Vector3.Subtract(ref tri.V1, ref tri.V2, out edge3);
  426. float dv0, dv1, dv2, dhalf;
  427. // a.X ^ b.X = (1,0,0) ^ edge1
  428. // axis = Vector3(0, -edge1.Z, edge1.Y);
  429. dv0 = tri.V0.Z * edge1.Y - tri.V0.Y * edge1.Z;
  430. dv1 = tri.V1.Z * edge1.Y - tri.V1.Y * edge1.Z;
  431. dv2 = tri.V2.Z * edge1.Y - tri.V2.Y * edge1.Z;
  432. dhalf = Math.Abs(halfExtent.Y * edge1.Z) + Math.Abs(halfExtent.Z * edge1.Y);
  433. if (Math.Min(dv0, Math.Min(dv1, dv2)) >= dhalf || Math.Max(dv0, Math.Max(dv1, dv2)) <= -dhalf)
  434. return ContainmentType.Disjoint;
  435. // a.X ^ b.Y = (1,0,0) ^ edge2
  436. // axis = Vector3(0, -edge2.Z, edge2.Y);
  437. dv0 = tri.V0.Z * edge2.Y - tri.V0.Y * edge2.Z;
  438. dv1 = tri.V1.Z * edge2.Y - tri.V1.Y * edge2.Z;
  439. dv2 = tri.V2.Z * edge2.Y - tri.V2.Y * edge2.Z;
  440. dhalf = Math.Abs(halfExtent.Y * edge2.Z) + Math.Abs(halfExtent.Z * edge2.Y);
  441. if (Math.Min(dv0, Math.Min(dv1, dv2)) >= dhalf || Math.Max(dv0, Math.Max(dv1, dv2)) <= -dhalf)
  442. return ContainmentType.Disjoint;
  443. // a.X ^ b.Y = (1,0,0) ^ edge3
  444. // axis = Vector3(0, -edge3.Z, edge3.Y);
  445. dv0 = tri.V0.Z * edge3.Y - tri.V0.Y * edge3.Z;
  446. dv1 = tri.V1.Z * edge3.Y - tri.V1.Y * edge3.Z;
  447. dv2 = tri.V2.Z * edge3.Y - tri.V2.Y * edge3.Z;
  448. dhalf = Math.Abs(halfExtent.Y * edge3.Z) + Math.Abs(halfExtent.Z * edge3.Y);
  449. if (Math.Min(dv0, Math.Min(dv1, dv2)) >= dhalf || Math.Max(dv0, Math.Max(dv1, dv2)) <= -dhalf)
  450. return ContainmentType.Disjoint;
  451. // a.Y ^ b.X = (0,1,0) ^ edge1
  452. // axis = Vector3(edge1.Z, 0, -edge1.X);
  453. dv0 = tri.V0.X * edge1.Z - tri.V0.Z * edge1.X;
  454. dv1 = tri.V1.X * edge1.Z - tri.V1.Z * edge1.X;
  455. dv2 = tri.V2.X * edge1.Z - tri.V2.Z * edge1.X;
  456. dhalf = Math.Abs(halfExtent.X * edge1.Z) + Math.Abs(halfExtent.Z * edge1.X);
  457. if (Math.Min(dv0, Math.Min(dv1, dv2)) >= dhalf || Math.Max(dv0, Math.Max(dv1, dv2)) <= -dhalf)
  458. return ContainmentType.Disjoint;
  459. // a.Y ^ b.X = (0,1,0) ^ edge2
  460. // axis = Vector3(edge2.Z, 0, -edge2.X);
  461. dv0 = tri.V0.X * edge2.Z - tri.V0.Z * edge2.X;
  462. dv1 = tri.V1.X * edge2.Z - tri.V1.Z * edge2.X;
  463. dv2 = tri.V2.X * edge2.Z - tri.V2.Z * edge2.X;
  464. dhalf = Math.Abs(halfExtent.X * edge2.Z) + Math.Abs(halfExtent.Z * edge2.X);
  465. if (Math.Min(dv0, Math.Min(dv1, dv2)) >= dhalf || Math.Max(dv0, Math.Max(dv1, dv2)) <= -dhalf)
  466. return ContainmentType.Disjoint;
  467. // a.Y ^ b.X = (0,1,0) ^ bX
  468. // axis = Vector3(edge3.Z, 0, -edge3.X);
  469. dv0 = tri.V0.X * edge3.Z - tri.V0.Z * edge3.X;
  470. dv1 = tri.V1.X * edge3.Z - tri.V1.Z * edge3.X;
  471. dv2 = tri.V2.X * edge3.Z - tri.V2.Z * edge3.X;
  472. dhalf = Math.Abs(halfExtent.X * edge3.Z) + Math.Abs(halfExtent.Z * edge3.X);
  473. if (Math.Min(dv0, Math.Min(dv1, dv2)) >= dhalf || Math.Max(dv0, Math.Max(dv1, dv2)) <= -dhalf)
  474. return ContainmentType.Disjoint;
  475. // a.Y ^ b.X = (0,0,1) ^ edge1
  476. // axis = Vector3(-edge1.Y, edge1.X, 0);
  477. dv0 = tri.V0.Y * edge1.X - tri.V0.X * edge1.Y;
  478. dv1 = tri.V1.Y * edge1.X - tri.V1.X * edge1.Y;
  479. dv2 = tri.V2.Y * edge1.X - tri.V2.X * edge1.Y;
  480. dhalf = Math.Abs(halfExtent.Y * edge1.X) + Math.Abs(halfExtent.X * edge1.Y);
  481. if (Math.Min(dv0, Math.Min(dv1, dv2)) >= dhalf || Math.Max(dv0, Math.Max(dv1, dv2)) <= -dhalf)
  482. return ContainmentType.Disjoint;
  483. // a.Y ^ b.X = (0,0,1) ^ edge2
  484. // axis = Vector3(-edge2.Y, edge2.X, 0);
  485. dv0 = tri.V0.Y * edge2.X - tri.V0.X * edge2.Y;
  486. dv1 = tri.V1.Y * edge2.X - tri.V1.X * edge2.Y;
  487. dv2 = tri.V2.Y * edge2.X - tri.V2.X * edge2.Y;
  488. dhalf = Math.Abs(halfExtent.Y * edge2.X) + Math.Abs(halfExtent.X * edge2.Y);
  489. if (Math.Min(dv0, Math.Min(dv1, dv2)) >= dhalf || Math.Max(dv0, Math.Max(dv1, dv2)) <= -dhalf)
  490. return ContainmentType.Disjoint;
  491. // a.Y ^ b.X = (0,0,1) ^ edge3
  492. // axis = Vector3(-edge3.Y, edge3.X, 0);
  493. dv0 = tri.V0.Y * edge3.X - tri.V0.X * edge3.Y;
  494. dv1 = tri.V1.Y * edge3.X - tri.V1.X * edge3.Y;
  495. dv2 = tri.V2.Y * edge3.X - tri.V2.X * edge3.Y;
  496. dhalf = Math.Abs(halfExtent.Y * edge3.X) + Math.Abs(halfExtent.X * edge3.Y);
  497. if (Math.Min(dv0, Math.Min(dv1, dv2)) >= dhalf || Math.Max(dv0, Math.Max(dv1, dv2)) <= -dhalf)
  498. return ContainmentType.Disjoint;
  499. return ContainmentType.Intersects;
  500. }
  501. #endregion
  502. }
  503. }