Bounds.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. #ifndef __BV_BOUNDS_H__
  21. #define __BV_BOUNDS_H__
  22. /*
  23. ===============================================================================
  24. Axis Aligned Bounding Box
  25. ===============================================================================
  26. */
  27. class idBounds {
  28. public:
  29. idBounds( void );
  30. explicit idBounds( const idVec3 &mins, const idVec3 &maxs );
  31. explicit idBounds( const idVec3 &point );
  32. const idVec3 & operator[]( const int index ) const;
  33. idVec3 & operator[]( const int index );
  34. idBounds operator+( const idVec3 &t ) const; // returns translated bounds
  35. idBounds & operator+=( const idVec3 &t ); // translate the bounds
  36. idBounds operator*( const idMat3 &r ) const; // returns rotated bounds
  37. idBounds & operator*=( const idMat3 &r ); // rotate the bounds
  38. idBounds operator+( const idBounds &a ) const;
  39. idBounds & operator+=( const idBounds &a );
  40. idBounds operator-( const idBounds &a ) const;
  41. idBounds & operator-=( const idBounds &a );
  42. bool Compare( const idBounds &a ) const; // exact compare, no epsilon
  43. bool Compare( const idBounds &a, const float epsilon ) const; // compare with epsilon
  44. bool operator==( const idBounds &a ) const; // exact compare, no epsilon
  45. bool operator!=( const idBounds &a ) const; // exact compare, no epsilon
  46. void Clear( void ); // inside out bounds
  47. void Zero( void ); // single point at origin
  48. idVec3 GetCenter( void ) const; // returns center of bounds
  49. float GetRadius( void ) const; // returns the radius relative to the bounds origin
  50. float GetRadius( const idVec3 &center ) const; // returns the radius relative to the given center
  51. float GetVolume( void ) const; // returns the volume of the bounds
  52. bool IsCleared( void ) const; // returns true if bounds are inside out
  53. bool AddPoint( const idVec3 &v ); // add the point, returns true if the bounds expanded
  54. bool AddBounds( const idBounds &a ); // add the bounds, returns true if the bounds expanded
  55. idBounds Intersect( const idBounds &a ) const; // return intersection of this bounds with the given bounds
  56. idBounds & IntersectSelf( const idBounds &a ); // intersect this bounds with the given bounds
  57. idBounds Expand( const float d ) const; // return bounds expanded in all directions with the given value
  58. idBounds & ExpandSelf( const float d ); // expand bounds in all directions with the given value
  59. idBounds Translate( const idVec3 &translation ) const; // return translated bounds
  60. idBounds & TranslateSelf( const idVec3 &translation ); // translate this bounds
  61. idBounds Rotate( const idMat3 &rotation ) const; // return rotated bounds
  62. idBounds & RotateSelf( const idMat3 &rotation ); // rotate this bounds
  63. float PlaneDistance( const idPlane &plane ) const;
  64. int PlaneSide( const idPlane &plane, const float epsilon = ON_EPSILON ) const;
  65. bool ContainsPoint( const idVec3 &p ) const; // includes touching
  66. bool IntersectsBounds( const idBounds &a ) const; // includes touching
  67. bool LineIntersection( const idVec3 &start, const idVec3 &end ) const;
  68. // intersection point is start + dir * scale
  69. bool RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale ) const;
  70. // most tight bounds for the given transformed bounds
  71. void FromTransformedBounds( const idBounds &bounds, const idVec3 &origin, const idMat3 &axis );
  72. // most tight bounds for a point set
  73. void FromPoints( const idVec3 *points, const int numPoints );
  74. // most tight bounds for a translation
  75. void FromPointTranslation( const idVec3 &point, const idVec3 &translation );
  76. void FromBoundsTranslation( const idBounds &bounds, const idVec3 &origin, const idMat3 &axis, const idVec3 &translation );
  77. // most tight bounds for a rotation
  78. void FromPointRotation( const idVec3 &point, const idRotation &rotation );
  79. void FromBoundsRotation( const idBounds &bounds, const idVec3 &origin, const idMat3 &axis, const idRotation &rotation );
  80. void ToPoints( idVec3 points[8] ) const;
  81. idSphere ToSphere( void ) const;
  82. void AxisProjection( const idVec3 &dir, float &min, float &max ) const;
  83. void AxisProjection( const idVec3 &origin, const idMat3 &axis, const idVec3 &dir, float &min, float &max ) const;
  84. private:
  85. idVec3 b[2];
  86. };
  87. extern idBounds bounds_zero;
  88. ID_INLINE idBounds::idBounds( void ) {
  89. }
  90. ID_INLINE idBounds::idBounds( const idVec3 &mins, const idVec3 &maxs ) {
  91. b[0] = mins;
  92. b[1] = maxs;
  93. }
  94. ID_INLINE idBounds::idBounds( const idVec3 &point ) {
  95. b[0] = point;
  96. b[1] = point;
  97. }
  98. ID_INLINE const idVec3 &idBounds::operator[]( const int index ) const {
  99. return b[index];
  100. }
  101. ID_INLINE idVec3 &idBounds::operator[]( const int index ) {
  102. return b[index];
  103. }
  104. ID_INLINE idBounds idBounds::operator+( const idVec3 &t ) const {
  105. return idBounds( b[0] + t, b[1] + t );
  106. }
  107. ID_INLINE idBounds &idBounds::operator+=( const idVec3 &t ) {
  108. b[0] += t;
  109. b[1] += t;
  110. return *this;
  111. }
  112. ID_INLINE idBounds idBounds::operator*( const idMat3 &r ) const {
  113. idBounds bounds;
  114. bounds.FromTransformedBounds( *this, vec3_origin, r );
  115. return bounds;
  116. }
  117. ID_INLINE idBounds &idBounds::operator*=( const idMat3 &r ) {
  118. this->FromTransformedBounds( *this, vec3_origin, r );
  119. return *this;
  120. }
  121. ID_INLINE idBounds idBounds::operator+( const idBounds &a ) const {
  122. idBounds newBounds;
  123. newBounds = *this;
  124. newBounds.AddBounds( a );
  125. return newBounds;
  126. }
  127. ID_INLINE idBounds &idBounds::operator+=( const idBounds &a ) {
  128. idBounds::AddBounds( a );
  129. return *this;
  130. }
  131. ID_INLINE idBounds idBounds::operator-( const idBounds &a ) const {
  132. assert( b[1][0] - b[0][0] > a.b[1][0] - a.b[0][0] &&
  133. b[1][1] - b[0][1] > a.b[1][1] - a.b[0][1] &&
  134. b[1][2] - b[0][2] > a.b[1][2] - a.b[0][2] );
  135. return idBounds( idVec3( b[0][0] + a.b[1][0], b[0][1] + a.b[1][1], b[0][2] + a.b[1][2] ),
  136. idVec3( b[1][0] + a.b[0][0], b[1][1] + a.b[0][1], b[1][2] + a.b[0][2] ) );
  137. }
  138. ID_INLINE idBounds &idBounds::operator-=( const idBounds &a ) {
  139. assert( b[1][0] - b[0][0] > a.b[1][0] - a.b[0][0] &&
  140. b[1][1] - b[0][1] > a.b[1][1] - a.b[0][1] &&
  141. b[1][2] - b[0][2] > a.b[1][2] - a.b[0][2] );
  142. b[0] += a.b[1];
  143. b[1] += a.b[0];
  144. return *this;
  145. }
  146. ID_INLINE bool idBounds::Compare( const idBounds &a ) const {
  147. return ( b[0].Compare( a.b[0] ) && b[1].Compare( a.b[1] ) );
  148. }
  149. ID_INLINE bool idBounds::Compare( const idBounds &a, const float epsilon ) const {
  150. return ( b[0].Compare( a.b[0], epsilon ) && b[1].Compare( a.b[1], epsilon ) );
  151. }
  152. ID_INLINE bool idBounds::operator==( const idBounds &a ) const {
  153. return Compare( a );
  154. }
  155. ID_INLINE bool idBounds::operator!=( const idBounds &a ) const {
  156. return !Compare( a );
  157. }
  158. ID_INLINE void idBounds::Clear( void ) {
  159. b[0][0] = b[0][1] = b[0][2] = idMath::INFINITY;
  160. b[1][0] = b[1][1] = b[1][2] = -idMath::INFINITY;
  161. }
  162. ID_INLINE void idBounds::Zero( void ) {
  163. b[0][0] = b[0][1] = b[0][2] =
  164. b[1][0] = b[1][1] = b[1][2] = 0;
  165. }
  166. ID_INLINE idVec3 idBounds::GetCenter( void ) const {
  167. return idVec3( ( b[1][0] + b[0][0] ) * 0.5f, ( b[1][1] + b[0][1] ) * 0.5f, ( b[1][2] + b[0][2] ) * 0.5f );
  168. }
  169. ID_INLINE float idBounds::GetVolume( void ) const {
  170. if ( b[0][0] >= b[1][0] || b[0][1] >= b[1][1] || b[0][2] >= b[1][2] ) {
  171. return 0.0f;
  172. }
  173. return ( ( b[1][0] - b[0][0] ) * ( b[1][1] - b[0][1] ) * ( b[1][2] - b[0][2] ) );
  174. }
  175. ID_INLINE bool idBounds::IsCleared( void ) const {
  176. return b[0][0] > b[1][0];
  177. }
  178. ID_INLINE bool idBounds::AddPoint( const idVec3 &v ) {
  179. bool expanded = false;
  180. if ( v[0] < b[0][0]) {
  181. b[0][0] = v[0];
  182. expanded = true;
  183. }
  184. if ( v[0] > b[1][0]) {
  185. b[1][0] = v[0];
  186. expanded = true;
  187. }
  188. if ( v[1] < b[0][1] ) {
  189. b[0][1] = v[1];
  190. expanded = true;
  191. }
  192. if ( v[1] > b[1][1]) {
  193. b[1][1] = v[1];
  194. expanded = true;
  195. }
  196. if ( v[2] < b[0][2] ) {
  197. b[0][2] = v[2];
  198. expanded = true;
  199. }
  200. if ( v[2] > b[1][2]) {
  201. b[1][2] = v[2];
  202. expanded = true;
  203. }
  204. return expanded;
  205. }
  206. ID_INLINE bool idBounds::AddBounds( const idBounds &a ) {
  207. bool expanded = false;
  208. if ( a.b[0][0] < b[0][0] ) {
  209. b[0][0] = a.b[0][0];
  210. expanded = true;
  211. }
  212. if ( a.b[0][1] < b[0][1] ) {
  213. b[0][1] = a.b[0][1];
  214. expanded = true;
  215. }
  216. if ( a.b[0][2] < b[0][2] ) {
  217. b[0][2] = a.b[0][2];
  218. expanded = true;
  219. }
  220. if ( a.b[1][0] > b[1][0] ) {
  221. b[1][0] = a.b[1][0];
  222. expanded = true;
  223. }
  224. if ( a.b[1][1] > b[1][1] ) {
  225. b[1][1] = a.b[1][1];
  226. expanded = true;
  227. }
  228. if ( a.b[1][2] > b[1][2] ) {
  229. b[1][2] = a.b[1][2];
  230. expanded = true;
  231. }
  232. return expanded;
  233. }
  234. ID_INLINE idBounds idBounds::Intersect( const idBounds &a ) const {
  235. idBounds n;
  236. n.b[0][0] = ( a.b[0][0] > b[0][0] ) ? a.b[0][0] : b[0][0];
  237. n.b[0][1] = ( a.b[0][1] > b[0][1] ) ? a.b[0][1] : b[0][1];
  238. n.b[0][2] = ( a.b[0][2] > b[0][2] ) ? a.b[0][2] : b[0][2];
  239. n.b[1][0] = ( a.b[1][0] < b[1][0] ) ? a.b[1][0] : b[1][0];
  240. n.b[1][1] = ( a.b[1][1] < b[1][1] ) ? a.b[1][1] : b[1][1];
  241. n.b[1][2] = ( a.b[1][2] < b[1][2] ) ? a.b[1][2] : b[1][2];
  242. return n;
  243. }
  244. ID_INLINE idBounds &idBounds::IntersectSelf( const idBounds &a ) {
  245. if ( a.b[0][0] > b[0][0] ) {
  246. b[0][0] = a.b[0][0];
  247. }
  248. if ( a.b[0][1] > b[0][1] ) {
  249. b[0][1] = a.b[0][1];
  250. }
  251. if ( a.b[0][2] > b[0][2] ) {
  252. b[0][2] = a.b[0][2];
  253. }
  254. if ( a.b[1][0] < b[1][0] ) {
  255. b[1][0] = a.b[1][0];
  256. }
  257. if ( a.b[1][1] < b[1][1] ) {
  258. b[1][1] = a.b[1][1];
  259. }
  260. if ( a.b[1][2] < b[1][2] ) {
  261. b[1][2] = a.b[1][2];
  262. }
  263. return *this;
  264. }
  265. ID_INLINE idBounds idBounds::Expand( const float d ) const {
  266. return idBounds( idVec3( b[0][0] - d, b[0][1] - d, b[0][2] - d ),
  267. idVec3( b[1][0] + d, b[1][1] + d, b[1][2] + d ) );
  268. }
  269. ID_INLINE idBounds &idBounds::ExpandSelf( const float d ) {
  270. b[0][0] -= d;
  271. b[0][1] -= d;
  272. b[0][2] -= d;
  273. b[1][0] += d;
  274. b[1][1] += d;
  275. b[1][2] += d;
  276. return *this;
  277. }
  278. ID_INLINE idBounds idBounds::Translate( const idVec3 &translation ) const {
  279. return idBounds( b[0] + translation, b[1] + translation );
  280. }
  281. ID_INLINE idBounds &idBounds::TranslateSelf( const idVec3 &translation ) {
  282. b[0] += translation;
  283. b[1] += translation;
  284. return *this;
  285. }
  286. ID_INLINE idBounds idBounds::Rotate( const idMat3 &rotation ) const {
  287. idBounds bounds;
  288. bounds.FromTransformedBounds( *this, vec3_origin, rotation );
  289. return bounds;
  290. }
  291. ID_INLINE idBounds &idBounds::RotateSelf( const idMat3 &rotation ) {
  292. FromTransformedBounds( *this, vec3_origin, rotation );
  293. return *this;
  294. }
  295. ID_INLINE bool idBounds::ContainsPoint( const idVec3 &p ) const {
  296. if ( p[0] < b[0][0] || p[1] < b[0][1] || p[2] < b[0][2]
  297. || p[0] > b[1][0] || p[1] > b[1][1] || p[2] > b[1][2] ) {
  298. return false;
  299. }
  300. return true;
  301. }
  302. ID_INLINE bool idBounds::IntersectsBounds( const idBounds &a ) const {
  303. if ( a.b[1][0] < b[0][0] || a.b[1][1] < b[0][1] || a.b[1][2] < b[0][2]
  304. || a.b[0][0] > b[1][0] || a.b[0][1] > b[1][1] || a.b[0][2] > b[1][2] ) {
  305. return false;
  306. }
  307. return true;
  308. }
  309. ID_INLINE idSphere idBounds::ToSphere( void ) const {
  310. idSphere sphere;
  311. sphere.SetOrigin( ( b[0] + b[1] ) * 0.5f );
  312. sphere.SetRadius( ( b[1] - sphere.GetOrigin() ).Length() );
  313. return sphere;
  314. }
  315. ID_INLINE void idBounds::AxisProjection( const idVec3 &dir, float &min, float &max ) const {
  316. float d1, d2;
  317. idVec3 center, extents;
  318. center = ( b[0] + b[1] ) * 0.5f;
  319. extents = b[1] - center;
  320. d1 = dir * center;
  321. d2 = idMath::Fabs( extents[0] * dir[0] ) +
  322. idMath::Fabs( extents[1] * dir[1] ) +
  323. idMath::Fabs( extents[2] * dir[2] );
  324. min = d1 - d2;
  325. max = d1 + d2;
  326. }
  327. ID_INLINE void idBounds::AxisProjection( const idVec3 &origin, const idMat3 &axis, const idVec3 &dir, float &min, float &max ) const {
  328. float d1, d2;
  329. idVec3 center, extents;
  330. center = ( b[0] + b[1] ) * 0.5f;
  331. extents = b[1] - center;
  332. center = origin + center * axis;
  333. d1 = dir * center;
  334. d2 = idMath::Fabs( extents[0] * ( dir * axis[0] ) ) +
  335. idMath::Fabs( extents[1] * ( dir * axis[1] ) ) +
  336. idMath::Fabs( extents[2] * ( dir * axis[2] ) );
  337. min = d1 - d2;
  338. max = d1 + d2;
  339. }
  340. #endif /* !__BV_BOUNDS_H__ */