obbox.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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/obbox.h $*
  25. * *
  26. * Org Author:: Greg_h *
  27. * *
  28. * Author : Kenny Mitchell *
  29. * *
  30. * $Modtime:: 06/26/02 4:04p $*
  31. * *
  32. * $Revision:: 24 $*
  33. * *
  34. * 06/26/02 KM Matrix name change to avoid MAX conflicts *
  35. *---------------------------------------------------------------------------------------------*
  36. * Functions: *
  37. * OBBoxClass::Transform -- transform an oriented box *
  38. * OBBoxClass::Project_To_Axis -- compute projection onto the given axis *
  39. * OBBoxClass::Compute_Point -- computes position of a parametricly defined point *
  40. * OBBoxClass::Compute_Axis_Aligned_Extent -- computes extent of an AABox enclosing this box *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #if defined(_MSC_VER)
  43. #pragma once
  44. #endif
  45. #ifndef OBBOX_H
  46. #define OBBOX_H
  47. #include "always.h"
  48. #include "vector3.h"
  49. #include "matrix3.h"
  50. #include "matrix3d.h"
  51. #include "wwmath.h"
  52. #include "castres.h"
  53. class TriClass;
  54. class AABoxClass;
  55. class PlaneClass;
  56. /*
  57. ** OBBoxClass
  58. **
  59. ** Oriented-Bounding-Box Class.
  60. ** This is a collision box in world space.
  61. ** Center - position of the center of the box
  62. ** Extents - size of the box
  63. ** Basis - rotation matrix defining the orientation of the box
  64. **
  65. ** To find the world space coordinates of the "+x,+y,+z" corner of
  66. ** the bounding box you could use this equation:
  67. ** Vector3 corner = Center + Basis * Extent;
  68. */
  69. class OBBoxClass
  70. {
  71. public:
  72. OBBoxClass(void) { }
  73. OBBoxClass(const OBBoxClass & that) :
  74. Basis(that.Basis),
  75. Center(that.Center),
  76. Extent(that.Extent)
  77. { }
  78. OBBoxClass(const Vector3 & center,const Vector3 & extent) :
  79. Basis(1),
  80. Center(center),
  81. Extent(extent)
  82. { }
  83. OBBoxClass(const Vector3 & center,const Vector3 & extent,const Matrix3x3 & basis) :
  84. Basis(basis),
  85. Center(center),
  86. Extent(extent)
  87. { }
  88. OBBoxClass(const Vector3 * points, int num_points);
  89. bool operator== (const OBBoxClass &src);
  90. bool operator!= (const OBBoxClass &src);
  91. void Init_From_Box_Points(Vector3 * points,int num_points);
  92. void Init_Random(float min_extent = 0.5f,float max_extent = 1.0f);
  93. float Project_To_Axis(const Vector3 & axis) const;
  94. float Volume(void) const { return 2.0*Extent.X * 2.0*Extent.Y * 2.0*Extent.Z; }
  95. void Compute_Point(float params[3],Vector3 * set_point) const;
  96. void Compute_Axis_Aligned_Extent(Vector3 * set_extent) const;
  97. Matrix3x3 Basis;
  98. Vector3 Center;
  99. Vector3 Extent;
  100. static void Transform(const Matrix3D & tm,const OBBoxClass & in,OBBoxClass * out);
  101. };
  102. // Test functions: slow, easy to understand version of box intersection code :)
  103. bool Oriented_Boxes_Intersect(const OBBoxClass & box0,const OBBoxClass & box1);
  104. bool Oriented_Boxes_Collide(const OBBoxClass & box0,const Vector3 & v0,const OBBoxClass & box1,const Vector3 & v1,float dt);
  105. bool Oriented_Box_Intersects_Tri(const OBBoxClass & box,const TriClass & tri);
  106. /***********************************************************************************************
  107. * OBBoxClass::Project_To_Axis -- compute projection onto the given axis *
  108. * *
  109. * INPUT: *
  110. * *
  111. * OUTPUT: *
  112. * *
  113. * WARNINGS: *
  114. * *
  115. * HISTORY: *
  116. * 2/24/98 GTH : Created. *
  117. *=============================================================================================*/
  118. inline float OBBoxClass::Project_To_Axis(const Vector3 & axis) const
  119. {
  120. float x = Extent[0] * Vector3::Dot_Product(axis,Vector3(Basis[0][0],Basis[1][0],Basis[2][0]));
  121. float y = Extent[1] * Vector3::Dot_Product(axis,Vector3(Basis[0][1],Basis[1][1],Basis[2][1]));
  122. float z = Extent[2] * Vector3::Dot_Product(axis,Vector3(Basis[0][2],Basis[1][2],Basis[2][2]));
  123. // projection is the sum of the absolute values of the projections of the three extents
  124. return (WWMath::Fabs(x) + WWMath::Fabs(y) + WWMath::Fabs(z));
  125. }
  126. /***********************************************************************************************
  127. * OBBoxClass::Transform -- transform an oriented box *
  128. * *
  129. * INPUT: *
  130. * *
  131. * OUTPUT: *
  132. * *
  133. * WARNINGS: *
  134. * *
  135. * HISTORY: *
  136. * 2/24/98 GTH : Created. *
  137. *=============================================================================================*/
  138. inline void OBBoxClass::Transform
  139. (
  140. const Matrix3D & tm,
  141. const OBBoxClass & in,
  142. OBBoxClass * out
  143. )
  144. {
  145. WWASSERT(out);
  146. WWASSERT(out!=&in);
  147. out->Extent = in.Extent;
  148. Matrix3D::Transform_Vector(tm,in.Center,&(out->Center));
  149. Matrix3x3::Multiply(tm,in.Basis,&(out->Basis));
  150. }
  151. /***********************************************************************************************
  152. * OBBoxClass::Compute_Point -- computes position of a parametricly defined point *
  153. * *
  154. * set_point = Center + params[0]*A0 + params[1]*A1 + params[2]*A2 *
  155. * *
  156. * INPUT: *
  157. * params - parametric description of a point in the box. -1 < params[i] < 1 *
  158. * set_point - pointer to a Vector3 to set. *
  159. * *
  160. * OUTPUT: *
  161. * *
  162. * WARNINGS: *
  163. * *
  164. * HISTORY: *
  165. * 4/2/99 GTH : Created. *
  166. *=============================================================================================*/
  167. inline void OBBoxClass::Compute_Point(float params[3],Vector3 * set_point) const
  168. {
  169. Vector3 point = Extent;
  170. point.X *= params[0];
  171. point.Y *= params[1];
  172. point.Z *= params[2];
  173. Matrix3x3::Rotate_Vector(Basis,point,set_point);
  174. Vector3::Add(Center,*set_point,set_point);
  175. }
  176. /***********************************************************************************************
  177. * OBBoxClass::Compute_Axis_Aligned_Extent -- computes extent of an AABox enclosing this box *
  178. * *
  179. * INPUT: *
  180. * set_extent - pointer to a Vector3 to put the result into *
  181. * *
  182. * OUTPUT: *
  183. * *
  184. * WARNINGS: *
  185. * *
  186. * HISTORY: *
  187. * 11/15/99 gth : Created. *
  188. *=============================================================================================*/
  189. inline void OBBoxClass::Compute_Axis_Aligned_Extent(Vector3 * set_extent) const
  190. {
  191. WWASSERT(set_extent != NULL);
  192. // x extent is the box projected onto the x axis
  193. set_extent->X = WWMath::Fabs(Extent[0] * Basis[0][0]) +
  194. WWMath::Fabs(Extent[1] * Basis[0][1]) +
  195. WWMath::Fabs(Extent[2] * Basis[0][2]);
  196. set_extent->Y = WWMath::Fabs(Extent[0] * Basis[1][0]) +
  197. WWMath::Fabs(Extent[1] * Basis[1][1]) +
  198. WWMath::Fabs(Extent[2] * Basis[1][2]);
  199. set_extent->Z = WWMath::Fabs(Extent[0] * Basis[2][0]) +
  200. WWMath::Fabs(Extent[1] * Basis[2][1]) +
  201. WWMath::Fabs(Extent[2] * Basis[2][2]);
  202. }
  203. /***********************************************************************************************
  204. * OBBoxClass::operator== -- Comparison operator *
  205. * *
  206. * INPUT: *
  207. * *
  208. * OUTPUT: *
  209. * *
  210. * WARNINGS: *
  211. * *
  212. * HISTORY: *
  213. * 6/21/00 PDS : Created. *
  214. *=============================================================================================*/
  215. inline bool OBBoxClass::operator== (const OBBoxClass &src)
  216. {
  217. return (Center == src.Center) && (Extent == src.Extent) && (Basis == src.Basis);
  218. }
  219. /***********************************************************************************************
  220. * OBBoxClass::operator!= -- Comparison operator *
  221. * *
  222. * INPUT: *
  223. * *
  224. * OUTPUT: *
  225. * *
  226. * WARNINGS: *
  227. * *
  228. * HISTORY: *
  229. * 6/21/00 PDS : Created. *
  230. *=============================================================================================*/
  231. inline bool OBBoxClass::operator!= (const OBBoxClass &src)
  232. {
  233. return (Center != src.Center) || (Extent != src.Extent) && (Basis == src.Basis);
  234. }
  235. #endif