afxTriBoxCheck2D_T3D.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //
  26. // Adapted to a 2D test for intersecting atlas triangles with zodiacs.
  27. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  28. //-----------------------------------------------------------------------------
  29. // AABB-triangle overlap test code originally by Tomas Akenine-Möller
  30. // Assisted by Pierre Terdiman and David Hunt
  31. // http://www.cs.lth.se/home/Tomas_Akenine_Moller/code/
  32. // Ported to TSE by BJG, 2005-4-14
  33. //-----------------------------------------------------------------------------
  34. #include "afx/arcaneFX.h"
  35. #include "afx/util/afxTriBoxCheck2D_T3D.h"
  36. #define FINDMINMAX(x0,x1,x2,min,max) \
  37. min = max = x0; \
  38. if(x1<min) min=x1;\
  39. if(x1>max) max=x1;\
  40. if(x2<min) min=x2;\
  41. if(x2>max) max=x2;
  42. /*======================== Z-tests ========================*/
  43. #define AXISTEST_Z12(a, b, fa, fb) \
  44. p1 = a*v1.x - b*v1.y; \
  45. p2 = a*v2.x - b*v2.y; \
  46. if(p2<p1) {min=p2; max=p1;} else {min=p1; max=p2;} \
  47. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  48. if(min>rad || max<-rad) return false;
  49. #define AXISTEST_Z0(a, b, fa, fb) \
  50. p0 = a*v0.x - b*v0.y; \
  51. p1 = a*v1.x - b*v1.y; \
  52. if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;} \
  53. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  54. if(min>rad || max<-rad) return false;
  55. bool afxTriBoxOverlap2D(const Point3F& boxcenter, const Point3F& boxhalfsize, const Point3F& a, const Point3F& b, const Point3F& c)
  56. {
  57. /* use separating axis theorem to test overlap between triangle and box */
  58. /* need to test for overlap in these directions: */
  59. /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
  60. /* we do not even need to test these) */
  61. /* 2) normal of the triangle */
  62. /* 3) crossproduct(edge from tri, {x,y,z}-directin) */
  63. /* this gives 3x3=9 more tests */
  64. F32 min,max,p0,p1,p2,rad;
  65. /* move everything so that the boxcenter is in (0,0,0) */
  66. Point3F v0 = a - boxcenter;
  67. Point3F v1 = b - boxcenter;
  68. Point3F v2 = c - boxcenter;
  69. /* compute triangle edges */
  70. Point3F e0 = v1 - v0; /* tri edge 0 */
  71. Point3F e1 = v2 - v1; /* tri edge 1 */
  72. Point3F e2 = v0 - v2; /* tri edge 2 */
  73. /* Bullet 3: */
  74. /* test the 3 tests first */
  75. F32 fex = mFabs(e0.x);
  76. F32 fey = mFabs(e0.y);
  77. AXISTEST_Z12(e0.y, e0.x, fey, fex);
  78. fex = mFabs(e1.x);
  79. fey = mFabs(e1.y);
  80. AXISTEST_Z0(e1.y, e1.x, fey, fex);
  81. fex = mFabs(e2.x);
  82. fey = mFabs(e2.y);
  83. AXISTEST_Z12(e2.y, e2.x, fey, fex);
  84. /* Bullet 1: */
  85. /* first test overlap in the {x,y,z}-directions */
  86. /* find min, max of the triangle each direction, and test for overlap in */
  87. /* that direction -- this is equivalent to testing a minimal AABB around */
  88. /* the triangle against the AABB */
  89. /* test in X-direction */
  90. FINDMINMAX(v0.x,v1.x,v2.x,min,max);
  91. if(min>boxhalfsize.x || max<-boxhalfsize.x) return false;
  92. /* test in Y-direction */
  93. FINDMINMAX(v0.y,v1.y,v2.y,min,max);
  94. if(min>boxhalfsize.y || max<-boxhalfsize.y) return false;
  95. return true; /* box and triangle overlaps */
  96. }