inttest.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/inttest.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 3/14/01 9:19a $*
  31. * *
  32. * $Revision:: 2 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef INTTEST_H
  41. #define INTTEST_H
  42. #include "always.h"
  43. #include "aabox.h"
  44. #include "obbox.h"
  45. #include "tri.h"
  46. #include "colmath.h"
  47. #include "coltype.h"
  48. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. // IntersectionTestClass
  50. //
  51. // This is the base class for all of the 'Intersection' functions. The intersection tests are
  52. // purely boolean tests. The base class only contains the CollisionType of the test.
  53. //
  54. // Every IntersectionTestClass should have the following functions:
  55. //
  56. // bool Cull(const Vector3 & min,const Vector3 & max);
  57. // bool Cull(const AABoxClass & box);
  58. // bool Intersect_Triangle(const TriClass & tri);
  59. //
  60. // These are not virtual because I don't want to pay the price of virtual function
  61. // calls at the point in the code where these are used. It may be possible to
  62. // write template functions if we use these exact function prototpyes for all
  63. // collision test classes though.
  64. //
  65. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. class IntersectionTestClass
  67. {
  68. public:
  69. IntersectionTestClass(int collision_type) : CollisionType(collision_type) { }
  70. IntersectionTestClass(const IntersectionTestClass & that) : CollisionType(that.CollisionType) { }
  71. public:
  72. int CollisionType;
  73. };
  74. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. // AABoxIntersectionTestClass
  76. //
  77. // This is an intersection test which uses an Axis-Aligned Box
  78. //
  79. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  80. class AABoxIntersectionTestClass : public IntersectionTestClass
  81. {
  82. public:
  83. AABoxIntersectionTestClass(const AABoxClass & box,int collision_type) :
  84. IntersectionTestClass(collision_type),
  85. Box(box)
  86. {
  87. }
  88. AABoxIntersectionTestClass(const AABoxIntersectionTestClass & that) :
  89. IntersectionTestClass(that),
  90. Box(that.Box)
  91. {
  92. }
  93. bool Cull(const Vector3 & cull_min,const Vector3 & cull_max);
  94. bool Cull(const AABoxClass & cull_box);
  95. bool Intersect_Triangle(const TriClass & tri);
  96. public:
  97. AABoxClass Box; // world space aabox that we want to test with
  98. };
  99. inline bool AABoxIntersectionTestClass::Cull(const Vector3 & cull_min,const Vector3 & cull_max)
  100. {
  101. Vector3 box_min;
  102. Vector3::Subtract(Box.Center,Box.Extent,&box_min);
  103. Vector3 box_max;
  104. Vector3::Add(Box.Center,Box.Extent,&box_max);
  105. if ((box_min.X > cull_max.X) || (box_max.X < cull_min.X)) return true;
  106. if ((box_min.Y > cull_max.Y) || (box_max.Y < cull_min.Y)) return true;
  107. if ((box_min.Z > cull_max.Z) || (box_max.Z < cull_min.Z)) return true;
  108. }
  109. inline bool AABoxIntersectionTestClass::Cull(const AABoxClass & cull_box)
  110. {
  111. Vector3 dc;
  112. Vector3 r;
  113. Vector3::Subtract(cull_box.Center,Box.Center,&dc);
  114. Vector3::Add(cull_box.Extent,Box.Extent,&r);
  115. if (WWMath::Fabs(dc.X) > r.X) return true;
  116. if (WWMath::Fabs(dc.Y) > r.Y) return true;
  117. if (WWMath::Fabs(dc.Z) > r.Z) return true;
  118. }
  119. inline bool AABoxIntersectionTestClass::Intersect_Triangle(const TriClass & tri)
  120. {
  121. return CollisionMath::Intersection_Test(Box,tri);
  122. }
  123. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  124. // OBBoxIntersectionTestClass
  125. //
  126. // This is an intersection test which uses an Axis-Aligned Box
  127. //
  128. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  129. class OBBoxIntersectionTestClass : public IntersectionTestClass
  130. {
  131. public:
  132. OBBoxIntersectionTestClass(const OBBoxClass & box,int collision_type);
  133. OBBoxIntersectionTestClass(const OBBoxIntersectionTestClass & that);
  134. OBBoxIntersectionTestClass(const OBBoxIntersectionTestClass & that,const Matrix3D & tm);
  135. OBBoxIntersectionTestClass(const AABoxIntersectionTestClass & that,const Matrix3D & tm);
  136. bool Cull(const Vector3 & min,const Vector3 & max);
  137. bool Cull(const AABoxClass & box);
  138. bool Intersect_Triangle(const TriClass & tri);
  139. protected:
  140. void update_bounding_box(void);
  141. public:
  142. OBBoxClass Box; // world space obbox that we want to test with
  143. AABoxClass BoundingBox; // axis aligned w-s bounding box
  144. };
  145. inline OBBoxIntersectionTestClass::OBBoxIntersectionTestClass(const OBBoxClass & box,int collision_type) :
  146. IntersectionTestClass(collision_type),
  147. Box(box)
  148. {
  149. update_bounding_box();
  150. }
  151. inline OBBoxIntersectionTestClass::OBBoxIntersectionTestClass(const OBBoxIntersectionTestClass & that) :
  152. IntersectionTestClass(that),
  153. Box(that.Box)
  154. {
  155. update_bounding_box();
  156. }
  157. inline OBBoxIntersectionTestClass::OBBoxIntersectionTestClass
  158. (
  159. const OBBoxIntersectionTestClass & that,
  160. const Matrix3D & tm
  161. ) :
  162. IntersectionTestClass(that)
  163. {
  164. OBBoxClass::Transform(tm,that.Box,&Box);
  165. update_bounding_box();
  166. }
  167. inline OBBoxIntersectionTestClass::OBBoxIntersectionTestClass
  168. (
  169. const AABoxIntersectionTestClass & that,
  170. const Matrix3D & tm
  171. ) :
  172. IntersectionTestClass(that)
  173. {
  174. Matrix3D::Transform_Vector(tm,that.Box.Center,&(Box.Center));
  175. Box.Extent = that.Box.Extent;
  176. Box.Basis = tm; // copies the 3x3 rotation portion of the transform
  177. update_bounding_box();
  178. }
  179. inline bool OBBoxIntersectionTestClass::Cull(const Vector3 & cull_min,const Vector3 & cull_max)
  180. {
  181. Vector3 box_min;
  182. Vector3::Subtract(BoundingBox.Center,BoundingBox.Extent,&box_min);
  183. Vector3 box_max;
  184. Vector3::Add(BoundingBox.Center,BoundingBox.Extent,&box_max);
  185. if ((box_min.X > cull_max.X) || (box_max.X < cull_min.X)) return true;
  186. if ((box_min.Y > cull_max.Y) || (box_max.Y < cull_min.Y)) return true;
  187. if ((box_min.Z > cull_max.Z) || (box_max.Z < cull_min.Z)) return true;
  188. return false;
  189. }
  190. inline bool OBBoxIntersectionTestClass::Cull(const AABoxClass & cull_box)
  191. {
  192. Vector3 dc;
  193. Vector3 r;
  194. Vector3::Subtract(cull_box.Center,BoundingBox.Center,&dc);
  195. Vector3::Add(cull_box.Extent,BoundingBox.Extent,&r);
  196. if (WWMath::Fabs(dc.X) > r.X) return true;
  197. if (WWMath::Fabs(dc.Y) > r.Y) return true;
  198. if (WWMath::Fabs(dc.Z) > r.Z) return true;
  199. return false;
  200. }
  201. inline bool OBBoxIntersectionTestClass::Intersect_Triangle(const TriClass & tri)
  202. {
  203. return CollisionMath::Intersection_Test(Box,tri);
  204. }
  205. inline void OBBoxIntersectionTestClass::update_bounding_box(void)
  206. {
  207. BoundingBox.Center = Box.Center;
  208. Box.Basis.Rotate_AABox_Extent(Box.Extent,&BoundingBox.Extent);
  209. }
  210. #endif