Bounds.cpp 10 KB

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