BoundingBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Frustum.h"
  25. void BoundingBox::Define(const Vector3* vertices, unsigned count)
  26. {
  27. if (!count)
  28. return;
  29. defined_ = false;
  30. Merge(vertices, count);
  31. }
  32. void BoundingBox::Define(const Frustum& frustum)
  33. {
  34. Define(frustum.GetVertices(), NUM_FRUSTUM_VERTICES);
  35. }
  36. void BoundingBox::Define(const Sphere& sphere)
  37. {
  38. const Vector3& center = sphere.center_;
  39. float radius = sphere.radius_;
  40. min_ = center + Vector3(-radius, -radius, -radius);
  41. max_ = center + Vector3(radius, radius, radius);
  42. defined_ = true;
  43. }
  44. void BoundingBox::Merge(const Vector3* vertices, unsigned count)
  45. {
  46. while (count--)
  47. {
  48. Merge(*vertices);
  49. ++vertices;
  50. }
  51. }
  52. void BoundingBox::Merge(const Frustum& frustum)
  53. {
  54. Merge(frustum.GetVertices(), NUM_FRUSTUM_VERTICES);
  55. }
  56. void BoundingBox::Merge(const Sphere& sphere)
  57. {
  58. const Vector3& center = sphere.center_;
  59. float radius = sphere.radius_;
  60. Merge(center + Vector3(radius, radius, radius));
  61. Merge(center + Vector3(-radius, -radius, -radius));
  62. }
  63. void BoundingBox::Intersect(const BoundingBox& box)
  64. {
  65. if (box.min_.x_ > min_.x_)
  66. min_.x_ = box.min_.x_;
  67. if (box.max_.x_ < max_.x_)
  68. max_.x_ = box.max_.x_;
  69. if (box.min_.y_ > min_.y_)
  70. min_.y_ = box.min_.y_;
  71. if (box.max_.y_ < max_.y_)
  72. max_.y_ = box.max_.y_;
  73. if (box.min_.z_ > min_.z_)
  74. min_.z_ = box.min_.z_;
  75. if (box.max_.z_ < max_.z_)
  76. max_.z_ = box.max_.z_;
  77. if (min_.x_ > max_.x_)
  78. Swap(min_.x_, max_.x_);
  79. if (min_.y_ > max_.y_)
  80. Swap(min_.y_, max_.y_);
  81. if (min_.z_ > max_.z_)
  82. Swap(min_.z_, max_.z_);
  83. }
  84. void BoundingBox::Transform(const Matrix3& transform)
  85. {
  86. Vector3 newCenter = transform * GetCenter();
  87. Vector3 oldEdge = GetSize() * 0.5;
  88. Vector3 newEdge = Vector3(
  89. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  90. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  91. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  92. );
  93. min_ = newCenter - newEdge;
  94. max_ = newCenter + newEdge;
  95. }
  96. void BoundingBox::Transform(const Matrix4x3& transform)
  97. {
  98. Vector3 newCenter = transform * GetCenter();
  99. Vector3 oldEdge = GetSize() * 0.5;
  100. Vector3 newEdge = Vector3(
  101. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  102. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  103. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  104. );
  105. min_ = newCenter - newEdge;
  106. max_ = newCenter + newEdge;
  107. }
  108. BoundingBox BoundingBox::GetTransformed(const Matrix3& transform) const
  109. {
  110. Vector3 newCenter = transform * GetCenter();
  111. Vector3 oldEdge = GetSize() * 0.5;
  112. Vector3 newEdge = Vector3(
  113. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  114. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  115. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  116. );
  117. return BoundingBox(newCenter - newEdge, newCenter + newEdge);
  118. }
  119. BoundingBox BoundingBox::GetTransformed(const Matrix4x3& transform) const
  120. {
  121. Vector3 newCenter = transform * GetCenter();
  122. Vector3 oldEdge = GetSize() * 0.5f;
  123. Vector3 newEdge = Vector3(
  124. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  125. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  126. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  127. );
  128. return BoundingBox(newCenter - newEdge, newCenter + newEdge);
  129. }
  130. Rect BoundingBox::GetProjected(const Matrix4& projection) const
  131. {
  132. Vector3 projMin = min_;
  133. Vector3 projMax = max_;
  134. if (projMin.z_ < M_MIN_NEARCLIP)
  135. projMin.z_ = M_MIN_NEARCLIP;
  136. if (projMax.z_ < M_MIN_NEARCLIP)
  137. projMax.z_ = M_MIN_NEARCLIP;
  138. Vector3 vertices[8];
  139. vertices[0] = projMin;
  140. vertices[1] = Vector3(projMax.x_, projMin.y_, projMin.z_);
  141. vertices[2] = Vector3(projMin.x_, projMax.y_, projMin.z_);
  142. vertices[3] = Vector3(projMax.x_, projMax.y_, projMin.z_);
  143. vertices[4] = Vector3(projMin.x_, projMin.y_, projMax.z_);
  144. vertices[5] = Vector3(projMax.x_, projMin.y_, projMax.z_);
  145. vertices[6] = Vector3(projMin.x_, projMax.y_, projMax.z_);
  146. vertices[7] = projMax;
  147. Rect rect;
  148. for (unsigned i = 0; i < 8; ++i)
  149. {
  150. Vector3 projected = projection * vertices[i];
  151. rect.Merge(Vector2(projected.x_, projected.y_));
  152. }
  153. return rect;
  154. }
  155. Intersection BoundingBox::IsInside(const Sphere& sphere) const
  156. {
  157. float distSquared = 0;
  158. float temp;
  159. const Vector3& center = sphere.center_;
  160. if (center.x_ < min_.x_)
  161. {
  162. temp = center.x_ - min_.x_;
  163. distSquared += temp * temp;
  164. }
  165. else if (center.x_ > max_.x_)
  166. {
  167. temp = center.x_ - max_.x_;
  168. distSquared += temp * temp;
  169. }
  170. if (center.y_ < min_.y_)
  171. {
  172. temp = center.y_ - min_.y_;
  173. distSquared += temp * temp;
  174. }
  175. else if (center.y_ > max_.y_)
  176. {
  177. temp = center.y_ - max_.y_;
  178. distSquared += temp * temp;
  179. }
  180. if (center.z_ < min_.z_)
  181. {
  182. temp = center.z_ - min_.z_;
  183. distSquared += temp * temp;
  184. }
  185. else if (center.z_ > max_.z_)
  186. {
  187. temp = center.z_ - max_.z_;
  188. distSquared += temp * temp;
  189. }
  190. float radius = sphere.radius_;
  191. if (distSquared >= radius * radius)
  192. return OUTSIDE;
  193. if ((center.x_ - radius < min_.x_) || (center.x_ + radius > max_.x_))
  194. return INTERSECTS;
  195. if ((center.y_ - radius < min_.y_) || (center.y_ + radius > max_.y_))
  196. return INTERSECTS;
  197. if ((center.z_ - radius < min_.z_) || (center.z_ + radius > max_.z_))
  198. return INTERSECTS;
  199. return INSIDE;
  200. }
  201. Intersection BoundingBox::IsInsideFast(const Sphere& sphere) const
  202. {
  203. float distSquared = 0;
  204. float temp;
  205. const Vector3& center = sphere.center_;
  206. if (center.x_ < min_.x_)
  207. {
  208. temp = center.x_ - min_.x_;
  209. distSquared += temp * temp;
  210. }
  211. else if (center.x_ > max_.x_)
  212. {
  213. temp = center.x_ - max_.x_;
  214. distSquared += temp * temp;
  215. }
  216. if (center.y_ < min_.y_)
  217. {
  218. temp = center.y_ - min_.y_;
  219. distSquared += temp * temp;
  220. }
  221. else if (center.y_ > max_.y_)
  222. {
  223. temp = center.y_ - max_.y_;
  224. distSquared += temp * temp;
  225. }
  226. if (center.z_ < min_.z_)
  227. {
  228. temp = center.z_ - min_.z_;
  229. distSquared += temp * temp;
  230. }
  231. else if (center.z_ > max_.z_)
  232. {
  233. temp = center.z_ - max_.z_;
  234. distSquared += temp * temp;
  235. }
  236. float radius = sphere.radius_;
  237. if (distSquared >= radius * radius)
  238. return OUTSIDE;
  239. return INSIDE;
  240. }
  241. float BoundingBox::GetDistance(const Ray& ray) const
  242. {
  243. // If undefined, no hit (infinite distance)
  244. if (!defined_)
  245. return M_INFINITY;
  246. // Check for ray origin being inside the box
  247. if (IsInside(ray.origin_))
  248. return 0.0f;
  249. float dist = M_INFINITY;
  250. // Check for intersecting in the X-direction
  251. if ((ray.origin_.x_ < min_.x_) && (ray.direction_.x_ > 0.0f))
  252. {
  253. float x = (min_.x_ - ray.origin_.x_) / ray.direction_.x_;
  254. if (x < dist)
  255. {
  256. Vector3 point = ray.origin_ + x * ray.direction_;
  257. if ((point.y_ >= min_.y_) && (point.y_ <= max_.y_) &&
  258. (point.z_ >= min_.z_) && (point.z_ <= max_.z_))
  259. dist = x;
  260. }
  261. }
  262. if ((ray.origin_.x_ > max_.x_) && (ray.direction_.x_ < 0.0f))
  263. {
  264. float x = (max_.x_ - ray.origin_.x_) / ray.direction_.x_;
  265. if (x < dist)
  266. {
  267. Vector3 point = ray.origin_ + x * ray.direction_;
  268. if ((point.y_ >= min_.y_) && (point.y_ <= max_.y_) &&
  269. (point.z_ >= min_.z_) && (point.z_ <= max_.z_))
  270. dist = x;
  271. }
  272. }
  273. // Check for intersecting in the Y-direction
  274. if ((ray.origin_.y_ < min_.y_) && (ray.direction_.y_ > 0.0f))
  275. {
  276. float x = (min_.y_ - ray.origin_.y_) / ray.direction_.y_;
  277. if (x < dist)
  278. {
  279. Vector3 point = ray.origin_ + x * ray.direction_;
  280. if ((point.x_ >= min_.x_) && (point.x_ <= max_.x_) &&
  281. (point.z_ >= min_.z_) && (point.z_ <= max_.z_))
  282. dist = x;
  283. }
  284. }
  285. if ((ray.origin_.y_ > max_.y_) && (ray.direction_.y_ < 0.0f))
  286. {
  287. float x = (max_.y_ - ray.origin_.y_) / ray.direction_.y_;
  288. if (x < dist)
  289. {
  290. Vector3 point = ray.origin_ + x * ray.direction_;
  291. if ((point.x_ >= min_.x_) && (point.x_ <= max_.x_) &&
  292. (point.z_ >= min_.z_) && (point.z_ <= max_.z_))
  293. dist = x;
  294. }
  295. }
  296. // Check for intersecting in the Z-direction
  297. if ((ray.origin_.z_ < min_.z_) && (ray.direction_.z_ > 0.0f))
  298. {
  299. float x = (min_.z_ - ray.origin_.z_) / ray.direction_.z_;
  300. if (x < dist)
  301. {
  302. Vector3 point = ray.origin_ + x * ray.direction_;
  303. if ((point.x_ >= min_.x_) && (point.x_ <= max_.x_) &&
  304. (point.y_ >= min_.y_) && (point.y_ <= max_.y_))
  305. dist = x;
  306. }
  307. }
  308. if ((ray.origin_.z_ > max_.z_) && (ray.direction_.z_ < 0.0f))
  309. {
  310. float x = (max_.z_ - ray.origin_.z_) / ray.direction_.z_;
  311. if (x < dist)
  312. {
  313. Vector3 point = ray.origin_ + x * ray.direction_;
  314. if ((point.x_ >= min_.x_) && (point.x_ <= max_.x_) &&
  315. (point.y_ >= min_.y_) && (point.y_ <= max_.y_))
  316. dist = x;
  317. }
  318. }
  319. return dist;
  320. }