obbox.h 14 KB

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