tri.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWMath *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/tri.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 3/12/02 10:21a $*
  29. * *
  30. * $Revision:: 10 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * TriClass::Find_Dominant_Plane -- returns indices of the axes of the dominant plane *
  35. * TriClass::Contains_Point -- performs 2D point-in-triangle test. *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "tri.h"
  38. #include "vector2.h"
  39. static inline void find_dominant_plane(const TriClass & tri, int * axis1,int * axis2,int * axis3)
  40. {
  41. /*
  42. ** Find the largest component of the normal
  43. */
  44. int ni = 0;
  45. float x = WWMath::Fabs(tri.N->X);
  46. float y = WWMath::Fabs(tri.N->Y);
  47. float z = WWMath::Fabs(tri.N->Z);
  48. float val = x;
  49. if (y > val) {
  50. ni = 1;
  51. val = y;
  52. }
  53. if (z > val) {
  54. ni = 2;
  55. }
  56. /*
  57. ** return the indices of the two axes perpendicular
  58. */
  59. switch (ni)
  60. {
  61. case 0:
  62. // Dominant is the X axis
  63. *axis1 = 1;
  64. *axis2 = 2;
  65. *axis3 = 0;
  66. break;
  67. case 1:
  68. // Dominant is the Y axis
  69. *axis1 = 0;
  70. *axis2 = 2;
  71. *axis3 = 1;
  72. break;
  73. case 2:
  74. // Dominant is the Z axis
  75. *axis1 = 0;
  76. *axis2 = 1;
  77. *axis3 = 2;
  78. break;
  79. }
  80. }
  81. /***********************************************************************************************
  82. * TriClass::Find_Dominant_Plane -- returns indices of the axes of the dominant plane *
  83. * *
  84. * INPUT: *
  85. * *
  86. * OUTPUT: *
  87. * *
  88. * WARNINGS: *
  89. * *
  90. * HISTORY: *
  91. * 3/24/99 GTH : Created. *
  92. *=============================================================================================*/
  93. void TriClass::Find_Dominant_Plane(int * axis1,int * axis2) const
  94. {
  95. /*
  96. ** Find the largest component of the normal
  97. */
  98. int ni = 0;
  99. float x = WWMath::Fabs(N->X);
  100. float y = WWMath::Fabs(N->Y);
  101. float z = WWMath::Fabs(N->Z);
  102. float val = x;
  103. if (y > val) {
  104. ni = 1;
  105. val = y;
  106. }
  107. if (z > val) {
  108. ni = 2;
  109. }
  110. /*
  111. ** return the indices of the two axes perpendicular
  112. */
  113. switch (ni)
  114. {
  115. case 0:
  116. // Dominant is the X axis
  117. *axis1 = 1;
  118. *axis2 = 2;
  119. break;
  120. case 1:
  121. // Dominant is the Y axis
  122. *axis1 = 0;
  123. *axis2 = 2;
  124. break;
  125. case 2:
  126. // Dominant is the Z axis
  127. *axis1 = 0;
  128. *axis2 = 1;
  129. break;
  130. }
  131. }
  132. /***********************************************************************************************
  133. * TriClass::Contains_Point -- performs 2D point-in-triangle test. *
  134. * *
  135. * INPUT: *
  136. * *
  137. * OUTPUT: *
  138. * *
  139. * WARNINGS: *
  140. * Assumes that the point is in the plane of the triangle... use this after you've intersected *
  141. * a ray with the plane of the triangle. *
  142. * *
  143. * HISTORY: *
  144. * 3/24/99 GTH : Created. *
  145. *=============================================================================================*/
  146. bool TriClass::Contains_Point(const Vector3 & ipoint) const
  147. {
  148. #if 0
  149. /*
  150. ** Perform the test in 2d on the plane which the normal
  151. ** is most perpendicular to. (copied from E.Cosky's intersection code)
  152. */
  153. int axis1 = 0;
  154. int axis2 = 0;
  155. Find_Dominant_Plane(&axis1,&axis2);
  156. #if 1
  157. unsigned char flags; // dummy variable passed into function and not used here
  158. return Point_In_Triangle_2D(*V[0], *V[1], *V[2], ipoint, axis1, axis2, flags);
  159. #else
  160. float u0 = ipoint[axis1] - (*V[0])[axis1];
  161. float v0 = ipoint[axis2] - (*V[0])[axis2];
  162. /*
  163. ** determine the 2d vectors on the dominant plane from the first vertex to the other two
  164. */
  165. float u1 = (*V[1])[axis1] - (*V[0])[axis1];
  166. float v1 = (*V[1])[axis2] - (*V[0])[axis2];
  167. float u2 = (*V[2])[axis1] - (*V[0])[axis1];
  168. float v2 = (*V[2])[axis2] - (*V[0])[axis2];
  169. float alpha, beta;
  170. bool intersect = false;
  171. // calculate alpha and beta as normalized (0..1) percentages across the 2d projected triangle
  172. // and do bounds checking (sum <= 1) to determine whether or not the triangle intersection occurs.
  173. if (u1 == 0) {
  174. beta = u0 / u2;
  175. if ((beta >= 0) && (beta <= 1)) {
  176. alpha = (v0 - beta * v2) / v1;
  177. intersect = ((alpha >= 0) && ((alpha + beta) <= 1 + WWMATH_EPSILON));
  178. }
  179. } else {
  180. beta = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1);
  181. if ((beta >= 0) && (beta <= 1)) {
  182. alpha = (u0 - beta * u2) / u1;
  183. intersect = ((alpha >= 0) && ((alpha + beta) <= 1 + WWMATH_EPSILON));
  184. }
  185. }
  186. return intersect;
  187. #endif
  188. #endif
  189. /*
  190. ** New cross-product based point-containment
  191. */
  192. #if 0
  193. int vi;
  194. int axis3 = 0;
  195. for (vi=0; vi<3; vi++) {
  196. if ((axis1 != vi) && (axis2 != vi)) axis3 = vi;
  197. }
  198. Vector3 test_point = ipoint;
  199. test_point[axis3] = 0.0f;
  200. Vector3 points[3];
  201. for (vi=0; vi<3; vi++) {
  202. points[vi] = *(V[vi]);
  203. points[vi][axis3] = 0.0f;
  204. }
  205. bool side[3];
  206. Vector3 edge;
  207. Vector3 cross;
  208. Vector3 dp;
  209. for (vi=0; vi<3; vi++) {
  210. edge = points[(vi+1)%3] - points[vi];
  211. dp = test_point - points[vi];
  212. Vector3::Cross_Product(dp,edge,&cross);
  213. side[vi] = (cross[axis3] > 0.0f);
  214. }
  215. bool my_intersect = ((side[0] == side[1]) && (side[1] == side[2]));
  216. return my_intersect;
  217. #endif
  218. /*
  219. ** "Optimized" version
  220. */
  221. #if 1
  222. int vi;
  223. int axis1 = 0;
  224. int axis2 = 0;
  225. int axis3 = 0;
  226. find_dominant_plane(*this,&axis1,&axis2,&axis3);
  227. int side_mask = 0;
  228. const int POS = 0x01;
  229. const int NEG = 0x02;
  230. /*
  231. ** Compute the 2D cross product of edge0 with a vector to the point
  232. */
  233. Vector2 edge;
  234. Vector2 dp;
  235. for (vi=0; vi<3; vi++) {
  236. int va=vi;
  237. int vb=(vi+1)%3;
  238. edge.Set((*V[vb])[axis1] - (*V[va])[axis1] , (*V[vb])[axis2] - (*V[va])[axis2]);
  239. dp.Set(ipoint[axis1] - (*V[va])[axis1] , ipoint[axis2] - (*V[va])[axis2]);
  240. float cross = edge.X * dp.Y - edge.Y * dp.X;
  241. if (cross > WWMATH_EPSILON) {
  242. side_mask |= POS;
  243. }
  244. if (cross < -WWMATH_EPSILON) {
  245. side_mask |= NEG;
  246. }
  247. }
  248. bool my_intersect = (side_mask != (POS | NEG));
  249. return my_intersect;
  250. #endif
  251. }