Cry_Geo.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : Common structures for geometry computations
  9. #pragma once
  10. #include "Cry_Math.h"
  11. ///////////////////////////////////////////////////////////////////////////////
  12. // Forward declarations //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. struct AABB;
  15. template <typename F>
  16. struct OBB_tpl;
  17. ///////////////////////////////////////////////////////////////////////////////
  18. ///////////////////////////////////////////////////////////////////////////////
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // struct AABB
  21. ///////////////////////////////////////////////////////////////////////////////
  22. ///////////////////////////////////////////////////////////////////////////////
  23. ///////////////////////////////////////////////////////////////////////////////
  24. struct AABB
  25. {
  26. Vec3 min;
  27. Vec3 max;
  28. /// default AABB constructor (without initialisation)
  29. ILINE AABB()
  30. {}
  31. enum type_reset
  32. {
  33. RESET
  34. };
  35. // AABB aabb(RESET) generates a reset aabb
  36. ILINE AABB(type_reset)
  37. { Reset(); }
  38. ILINE explicit AABB(float radius)
  39. { max = Vec3(radius); min = -max; }
  40. ILINE explicit AABB(const Vec3& v)
  41. { min = max = v; }
  42. ILINE AABB(const Vec3& v, float radius)
  43. { Vec3 ext(radius); min = v - ext; max = v + ext; }
  44. ILINE AABB(const Vec3& vmin, const Vec3& vmax)
  45. { min = vmin; max = vmax; }
  46. ILINE AABB(const AABB& aabb)
  47. {
  48. min.x = aabb.min.x;
  49. min.y = aabb.min.y;
  50. min.z = aabb.min.z;
  51. max.x = aabb.max.x;
  52. max.y = aabb.max.y;
  53. max.z = aabb.max.z;
  54. }
  55. inline AABB(const Vec3* points, int num)
  56. {
  57. Reset();
  58. for (int i = 0; i < num; i++)
  59. {
  60. Add(points[i]);
  61. }
  62. }
  63. //! Reset Bounding box before calculating bounds.
  64. //! These values ensure that Add() functions work correctly for Reset bbs, without additional comparisons.
  65. ILINE void Reset()
  66. { min = Vec3(1e15f); max = Vec3(-1e15f); }
  67. ILINE bool IsReset() const
  68. { return min.x > max.x; }
  69. ILINE float IsResetSel(float ifReset, float ifNotReset) const
  70. { return (float)fsel(max.x - min.x, ifNotReset, ifReset); }
  71. //! Check if bounding box is empty (Zero volume).
  72. ILINE bool IsEmpty() const
  73. { return min == max; }
  74. //! Check if bounding box has valid, non zero volume
  75. ILINE bool IsNonZero() const
  76. { return min.x < max.x && min.y < max.y && min.z < max.z; }
  77. ILINE Vec3 GetCenter() const
  78. { return (min + max) * 0.5f; }
  79. ILINE Vec3 GetSize() const
  80. { return (max - min) * IsResetSel(0.0f, 1.0f); }
  81. ILINE float GetRadius() const
  82. { return IsResetSel(0.0f, (max - min).GetLengthFloat() * 0.5f); }
  83. ILINE void Add(const Vec3& v)
  84. {
  85. min.CheckMin(v);
  86. max.CheckMax(v);
  87. }
  88. inline void Add(const Vec3& v, float radius)
  89. {
  90. Vec3 ext(radius);
  91. min.CheckMin(v - ext);
  92. max.CheckMax(v + ext);
  93. }
  94. ILINE void Add(const AABB& bb)
  95. {
  96. min.CheckMin(bb.min);
  97. max.CheckMax(bb.max);
  98. }
  99. //! Check if this bounding box contains a point within itself.
  100. bool IsContainPoint(const Vec3& pos) const
  101. {
  102. assert(min.IsValid());
  103. assert(max.IsValid());
  104. assert(pos.IsValid());
  105. if (pos.x < min.x)
  106. {
  107. return false;
  108. }
  109. if (pos.y < min.y)
  110. {
  111. return false;
  112. }
  113. if (pos.z < min.z)
  114. {
  115. return false;
  116. }
  117. if (pos.x > max.x)
  118. {
  119. return false;
  120. }
  121. if (pos.y > max.y)
  122. {
  123. return false;
  124. }
  125. if (pos.z > max.z)
  126. {
  127. return false;
  128. }
  129. return true;
  130. }
  131. float GetDistanceSqr(Vec3 const& v) const
  132. {
  133. Vec3 vNear = v;
  134. vNear.CheckMax(min);
  135. vNear.CheckMin(max);
  136. return vNear.GetSquaredDistance(v);
  137. }
  138. float GetDistance(Vec3 const& v) const
  139. {
  140. return sqrt(GetDistanceSqr(v));
  141. }
  142. // Check two bounding boxes for intersection.
  143. inline bool IsIntersectBox(const AABB& b) const
  144. {
  145. assert(min.IsValid());
  146. assert(max.IsValid());
  147. assert(b.min.IsValid());
  148. assert(b.max.IsValid());
  149. // Check for intersection on X axis.
  150. if ((min.x > b.max.x) || (b.min.x > max.x))
  151. {
  152. return false;
  153. }
  154. // Check for intersection on Y axis.
  155. if ((min.y > b.max.y) || (b.min.y > max.y))
  156. {
  157. return false;
  158. }
  159. // Check for intersection on Z axis.
  160. if ((min.z > b.max.z) || (b.min.z > max.z))
  161. {
  162. return false;
  163. }
  164. // Boxes overlap in all 3 axises.
  165. return true;
  166. }
  167. /*!
  168. * calculate the new bounds of a transformed AABB
  169. *
  170. * Example:
  171. * AABB aabb = AABB::CreateAABBfromOBB(m34,aabb);
  172. *
  173. * return values:
  174. * expanded AABB in world-space
  175. */
  176. ILINE void SetTransformedAABB(const Matrix34& m34, const AABB& aabb)
  177. {
  178. if (aabb.IsReset())
  179. {
  180. Reset();
  181. }
  182. else
  183. {
  184. Matrix33 m33;
  185. m33.m00 = fabs_tpl(m34.m00);
  186. m33.m01 = fabs_tpl(m34.m01);
  187. m33.m02 = fabs_tpl(m34.m02);
  188. m33.m10 = fabs_tpl(m34.m10);
  189. m33.m11 = fabs_tpl(m34.m11);
  190. m33.m12 = fabs_tpl(m34.m12);
  191. m33.m20 = fabs_tpl(m34.m20);
  192. m33.m21 = fabs_tpl(m34.m21);
  193. m33.m22 = fabs_tpl(m34.m22);
  194. Vec3 sz = m33 * ((aabb.max - aabb.min) * 0.5f);
  195. Vec3 pos = m34 * ((aabb.max + aabb.min) * 0.5f);
  196. min = pos - sz;
  197. max = pos + sz;
  198. }
  199. }
  200. };
  201. ILINE bool IsEquivalent(const AABB& a, const AABB& b, float epsilon = VEC_EPSILON)
  202. {
  203. return IsEquivalent(a.min, b.min, epsilon) && IsEquivalent(a.max, b.max, epsilon);
  204. }
  205. ///////////////////////////////////////////////////////////////////////////////
  206. ///////////////////////////////////////////////////////////////////////////////
  207. ///////////////////////////////////////////////////////////////////////////////
  208. // struct OBB
  209. ///////////////////////////////////////////////////////////////////////////////
  210. ///////////////////////////////////////////////////////////////////////////////
  211. ///////////////////////////////////////////////////////////////////////////////
  212. template <typename F>
  213. struct OBB_tpl
  214. {
  215. Matrix33 m33; //orientation vectors
  216. Vec3 h; //half-length-vector
  217. Vec3 c; //center of obb
  218. //default OBB constructor (without initialisation)
  219. inline OBB_tpl() {}
  220. ILINE void SetOBB(const Matrix33& matrix, const Vec3& hlv, const Vec3& center) { m33 = matrix; h = hlv; c = center; }
  221. ILINE static OBB_tpl<F> CreateOBB(const Matrix33& m33, const Vec3& hlv, const Vec3& center) { OBB_tpl<f32> obb; obb.m33 = m33; obb.h = hlv; obb.c = center; return obb; }
  222. ILINE void SetOBBfromAABB(const Matrix33& mat33, const AABB& aabb)
  223. {
  224. m33 = mat33;
  225. h = (aabb.max - aabb.min) * 0.5f; //calculate the half-length-vectors
  226. c = (aabb.max + aabb.min) * 0.5f; //the center is relative to the PIVOT
  227. }
  228. ILINE void SetOBBfromAABB(const Quat& q, const AABB& aabb)
  229. {
  230. m33 = Matrix33(q);
  231. h = (aabb.max - aabb.min) * 0.5f; //calculate the half-length-vectors
  232. c = (aabb.max + aabb.min) * 0.5f; //the center is relative to the PIVOT
  233. }
  234. ILINE static OBB_tpl<F> CreateOBBfromAABB(const Matrix33& m33, const AABB& aabb) { OBB_tpl<f32> obb; obb.SetOBBfromAABB(m33, aabb); return obb; }
  235. ILINE static OBB_tpl<F> CreateOBBfromAABB(const Quat& q, const AABB& aabb) { OBB_tpl<f32> obb; obb.SetOBBfromAABB(q, aabb); return obb; }
  236. ~OBB_tpl(void) {};
  237. };
  238. typedef OBB_tpl<f32> OBB;