Bounds.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../precompiled.h"
  22. idBounds bounds_zero( vec3_zero, vec3_zero );
  23. idBounds bounds_zeroOneCube( idVec3( 0.0f ), idVec3( 1.0f ) );
  24. idBounds bounds_unitCube( idVec3( -1.0f ), idVec3( 1.0f ) );
  25. /*
  26. ============
  27. idBounds::GetRadius
  28. ============
  29. */
  30. float idBounds::GetRadius() const {
  31. int i;
  32. float total, b0, b1;
  33. total = 0.0f;
  34. for ( i = 0; i < 3; i++ ) {
  35. b0 = (float)idMath::Fabs( b[0][i] );
  36. b1 = (float)idMath::Fabs( b[1][i] );
  37. if ( b0 > b1 ) {
  38. total += b0 * b0;
  39. } else {
  40. total += b1 * b1;
  41. }
  42. }
  43. return idMath::Sqrt( total );
  44. }
  45. /*
  46. ============
  47. idBounds::GetRadius
  48. ============
  49. */
  50. float idBounds::GetRadius( const idVec3 &center ) const {
  51. int i;
  52. float total, b0, b1;
  53. total = 0.0f;
  54. for ( i = 0; i < 3; i++ ) {
  55. b0 = (float)idMath::Fabs( center[i] - b[0][i] );
  56. b1 = (float)idMath::Fabs( b[1][i] - center[i] );
  57. if ( b0 > b1 ) {
  58. total += b0 * b0;
  59. } else {
  60. total += b1 * b1;
  61. }
  62. }
  63. return idMath::Sqrt( total );
  64. }
  65. /*
  66. ================
  67. idBounds::PlaneDistance
  68. ================
  69. */
  70. float idBounds::PlaneDistance( const idPlane &plane ) const {
  71. idVec3 center;
  72. float d1, d2;
  73. center = ( b[0] + b[1] ) * 0.5f;
  74. d1 = plane.Distance( center );
  75. d2 = idMath::Fabs( ( b[1][0] - center[0] ) * plane.Normal()[0] ) +
  76. idMath::Fabs( ( b[1][1] - center[1] ) * plane.Normal()[1] ) +
  77. idMath::Fabs( ( b[1][2] - center[2] ) * plane.Normal()[2] );
  78. if ( d1 - d2 > 0.0f ) {
  79. return d1 - d2;
  80. }
  81. if ( d1 + d2 < 0.0f ) {
  82. return d1 + d2;
  83. }
  84. return 0.0f;
  85. }
  86. /*
  87. ================
  88. idBounds::PlaneSide
  89. ================
  90. */
  91. int idBounds::PlaneSide( const idPlane &plane, const float epsilon ) const {
  92. idVec3 center;
  93. float d1, d2;
  94. center = ( b[0] + b[1] ) * 0.5f;
  95. d1 = plane.Distance( center );
  96. d2 = idMath::Fabs( ( b[1][0] - center[0] ) * plane.Normal()[0] ) +
  97. idMath::Fabs( ( b[1][1] - center[1] ) * plane.Normal()[1] ) +
  98. idMath::Fabs( ( b[1][2] - center[2] ) * plane.Normal()[2] );
  99. if ( d1 - d2 > epsilon ) {
  100. return PLANESIDE_FRONT;
  101. }
  102. if ( d1 + d2 < -epsilon ) {
  103. return PLANESIDE_BACK;
  104. }
  105. return PLANESIDE_CROSS;
  106. }
  107. /*
  108. ============
  109. idBounds::LineIntersection
  110. Returns true if the line intersects the bounds between the start and end point.
  111. ============
  112. */
  113. bool idBounds::LineIntersection( const idVec3 &start, const idVec3 &end ) const {
  114. const idVec3 center = ( b[0] + b[1] ) * 0.5f;
  115. const idVec3 extents = b[1] - center;
  116. const idVec3 lineDir = 0.5f * ( end - start );
  117. const idVec3 lineCenter = start + lineDir;
  118. const idVec3 dir = lineCenter - center;
  119. const float ld0 = idMath::Fabs( lineDir[0] );
  120. if ( idMath::Fabs( dir[0] ) > extents[0] + ld0 ) {
  121. return false;
  122. }
  123. const float ld1 = idMath::Fabs( lineDir[1] );
  124. if ( idMath::Fabs( dir[1] ) > extents[1] + ld1 ) {
  125. return false;
  126. }
  127. const float ld2 = idMath::Fabs( lineDir[2] );
  128. if ( idMath::Fabs( dir[2] ) > extents[2] + ld2 ) {
  129. return false;
  130. }
  131. const idVec3 cross = lineDir.Cross( dir );
  132. if ( idMath::Fabs( cross[0] ) > extents[1] * ld2 + extents[2] * ld1 ) {
  133. return false;
  134. }
  135. if ( idMath::Fabs( cross[1] ) > extents[0] * ld2 + extents[2] * ld0 ) {
  136. return false;
  137. }
  138. if ( idMath::Fabs( cross[2] ) > extents[0] * ld1 + extents[1] * ld0 ) {
  139. return false;
  140. }
  141. return true;
  142. }
  143. /*
  144. ============
  145. idBounds::RayIntersection
  146. Returns true if the ray intersects the bounds.
  147. The ray can intersect the bounds in both directions from the start point.
  148. If start is inside the bounds it is considered an intersection with scale = 0
  149. ============
  150. */
  151. bool idBounds::RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale ) const {
  152. int i, ax0, ax1, ax2, side, inside;
  153. float f;
  154. idVec3 hit;
  155. ax0 = -1;
  156. inside = 0;
  157. for ( i = 0; i < 3; i++ ) {
  158. if ( start[i] < b[0][i] ) {
  159. side = 0;
  160. }
  161. else if ( start[i] > b[1][i] ) {
  162. side = 1;
  163. }
  164. else {
  165. inside++;
  166. continue;
  167. }
  168. if ( dir[i] == 0.0f ) {
  169. continue;
  170. }
  171. f = ( start[i] - b[side][i] );
  172. if ( ax0 < 0 || idMath::Fabs( f ) > idMath::Fabs( scale * dir[i] ) ) {
  173. scale = - ( f / dir[i] );
  174. ax0 = i;
  175. }
  176. }
  177. if ( ax0 < 0 ) {
  178. scale = 0.0f;
  179. // return true if the start point is inside the bounds
  180. return ( inside == 3 );
  181. }
  182. ax1 = (ax0+1)%3;
  183. ax2 = (ax0+2)%3;
  184. hit[ax1] = start[ax1] + scale * dir[ax1];
  185. hit[ax2] = start[ax2] + scale * dir[ax2];
  186. return ( hit[ax1] >= b[0][ax1] && hit[ax1] <= b[1][ax1] &&
  187. hit[ax2] >= b[0][ax2] && hit[ax2] <= b[1][ax2] );
  188. }
  189. /*
  190. ============
  191. idBounds::FromTransformedBounds
  192. ============
  193. */
  194. void idBounds::FromTransformedBounds( const idBounds &bounds, const idVec3 &origin, const idMat3 &axis ) {
  195. int i;
  196. idVec3 center, extents, rotatedExtents;
  197. center = (bounds[0] + bounds[1]) * 0.5f;
  198. extents = bounds[1] - center;
  199. for ( i = 0; i < 3; i++ ) {
  200. rotatedExtents[i] = idMath::Fabs( extents[0] * axis[0][i] ) +
  201. idMath::Fabs( extents[1] * axis[1][i] ) +
  202. idMath::Fabs( extents[2] * axis[2][i] );
  203. }
  204. center = origin + center * axis;
  205. b[0] = center - rotatedExtents;
  206. b[1] = center + rotatedExtents;
  207. }
  208. /*
  209. ============
  210. idBounds::FromPoints
  211. Most tight bounds for a point set.
  212. ============
  213. */
  214. void idBounds::FromPoints( const idVec3 *points, const int numPoints ) {
  215. SIMDProcessor->MinMax( b[0], b[1], points, numPoints );
  216. }
  217. /*
  218. ============
  219. idBounds::FromPointTranslation
  220. Most tight bounds for the translational movement of the given point.
  221. ============
  222. */
  223. void idBounds::FromPointTranslation( const idVec3 &point, const idVec3 &translation ) {
  224. int i;
  225. for ( i = 0; i < 3; i++ ) {
  226. if ( translation[i] < 0.0f ) {
  227. b[0][i] = point[i] + translation[i];
  228. b[1][i] = point[i];
  229. }
  230. else {
  231. b[0][i] = point[i];
  232. b[1][i] = point[i] + translation[i];
  233. }
  234. }
  235. }
  236. /*
  237. ============
  238. idBounds::FromBoundsTranslation
  239. Most tight bounds for the translational movement of the given bounds.
  240. ============
  241. */
  242. void idBounds::FromBoundsTranslation( const idBounds &bounds, const idVec3 &origin, const idMat3 &axis, const idVec3 &translation ) {
  243. int i;
  244. if ( axis.IsRotated() ) {
  245. FromTransformedBounds( bounds, origin, axis );
  246. }
  247. else {
  248. b[0] = bounds[0] + origin;
  249. b[1] = bounds[1] + origin;
  250. }
  251. for ( i = 0; i < 3; i++ ) {
  252. if ( translation[i] < 0.0f ) {
  253. b[0][i] += translation[i];
  254. }
  255. else {
  256. b[1][i] += translation[i];
  257. }
  258. }
  259. }
  260. /*
  261. ================
  262. BoundsForPointRotation
  263. only for rotations < 180 degrees
  264. ================
  265. */
  266. idBounds BoundsForPointRotation( const idVec3 &start, const idRotation &rotation ) {
  267. int i;
  268. float radiusSqr;
  269. idVec3 v1, v2;
  270. idVec3 origin, axis, end;
  271. idBounds bounds;
  272. end = start * rotation;
  273. axis = rotation.GetVec();
  274. origin = rotation.GetOrigin() + axis * ( axis * ( start - rotation.GetOrigin() ) );
  275. radiusSqr = ( start - origin ).LengthSqr();
  276. v1 = ( start - origin ).Cross( axis );
  277. v2 = ( end - origin ).Cross( axis );
  278. for ( i = 0; i < 3; i++ ) {
  279. // if the derivative changes sign along this axis during the rotation from start to end
  280. if ( ( v1[i] > 0.0f && v2[i] < 0.0f ) || ( v1[i] < 0.0f && v2[i] > 0.0f ) ) {
  281. if ( ( 0.5f * (start[i] + end[i]) - origin[i] ) > 0.0f ) {
  282. bounds[0][i] = Min( start[i], end[i] );
  283. bounds[1][i] = origin[i] + idMath::Sqrt( radiusSqr * ( 1.0f - axis[i] * axis[i] ) );
  284. }
  285. else {
  286. bounds[0][i] = origin[i] - idMath::Sqrt( radiusSqr * ( 1.0f - axis[i] * axis[i] ) );
  287. bounds[1][i] = Max( start[i], end[i] );
  288. }
  289. }
  290. else if ( start[i] > end[i] ) {
  291. bounds[0][i] = end[i];
  292. bounds[1][i] = start[i];
  293. }
  294. else {
  295. bounds[0][i] = start[i];
  296. bounds[1][i] = end[i];
  297. }
  298. }
  299. return bounds;
  300. }
  301. /*
  302. ============
  303. idBounds::FromPointRotation
  304. Most tight bounds for the rotational movement of the given point.
  305. ============
  306. */
  307. void idBounds::FromPointRotation( const idVec3 &point, const idRotation &rotation ) {
  308. float radius;
  309. if ( idMath::Fabs( rotation.GetAngle() ) < 180.0f ) {
  310. (*this) = BoundsForPointRotation( point, rotation );
  311. }
  312. else {
  313. radius = ( point - rotation.GetOrigin() ).Length();
  314. // FIXME: these bounds are usually way larger
  315. b[0].Set( -radius, -radius, -radius );
  316. b[1].Set( radius, radius, radius );
  317. }
  318. }
  319. /*
  320. ============
  321. idBounds::FromBoundsRotation
  322. Most tight bounds for the rotational movement of the given bounds.
  323. ============
  324. */
  325. void idBounds::FromBoundsRotation( const idBounds &bounds, const idVec3 &origin, const idMat3 &axis, const idRotation &rotation ) {
  326. int i;
  327. float radius;
  328. idVec3 point;
  329. idBounds rBounds;
  330. if ( idMath::Fabs( rotation.GetAngle() ) < 180.0f ) {
  331. (*this) = BoundsForPointRotation( bounds[0] * axis + origin, rotation );
  332. for ( i = 1; i < 8; i++ ) {
  333. point[0] = bounds[(i^(i>>1))&1][0];
  334. point[1] = bounds[(i>>1)&1][1];
  335. point[2] = bounds[(i>>2)&1][2];
  336. (*this) += BoundsForPointRotation( point * axis + origin, rotation );
  337. }
  338. }
  339. else {
  340. point = (bounds[1] - bounds[0]) * 0.5f;
  341. radius = (bounds[1] - point).Length() + (point - rotation.GetOrigin()).Length();
  342. // FIXME: these bounds are usually way larger
  343. b[0].Set( -radius, -radius, -radius );
  344. b[1].Set( radius, radius, radius );
  345. }
  346. }
  347. /*
  348. ============
  349. idBounds::ToPoints
  350. ============
  351. */
  352. void idBounds::ToPoints( idVec3 points[8] ) const {
  353. for ( int i = 0; i < 8; i++ ) {
  354. points[i][0] = b[(i^(i>>1))&1][0];
  355. points[i][1] = b[(i>>1)&1][1];
  356. points[i][2] = b[(i>>2)&1][2];
  357. }
  358. }