BoundingBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 std::vector<Vector3>& vertices)
  26. {
  27. defined_ = false;
  28. for (std::vector<Vector3>::const_iterator i = vertices.begin(); i != vertices.end(); ++i)
  29. Merge(*i);
  30. }
  31. void BoundingBox::Define(const Vector3* vertices, unsigned count)
  32. {
  33. if (!count)
  34. return;
  35. defined_ = false;
  36. Merge(vertices, count);
  37. }
  38. void BoundingBox::Define(const Frustum& frustum)
  39. {
  40. Define(frustum.GetVertices(), NUM_FRUSTUM_VERTICES);
  41. }
  42. void BoundingBox::Define(const Sphere& sphere)
  43. {
  44. const Vector3& center = sphere.center_;
  45. float radius = sphere.radius_;
  46. min_ = center + Vector3(-radius, -radius, -radius);
  47. max_ = center + Vector3(radius, radius, radius);
  48. defined_ = true;
  49. }
  50. void BoundingBox::Merge(const std::vector<Vector3>& vertices)
  51. {
  52. for (std::vector<Vector3>::const_iterator i = vertices.begin(); i != vertices.end(); ++i)
  53. Merge(*i);
  54. }
  55. void BoundingBox::Merge(const Vector3* vertices, unsigned count)
  56. {
  57. while (count--)
  58. {
  59. Merge(*vertices);
  60. ++vertices;
  61. }
  62. }
  63. void BoundingBox::Merge(const Frustum& frustum)
  64. {
  65. Merge(frustum.GetVertices(), NUM_FRUSTUM_VERTICES);
  66. }
  67. void BoundingBox::Merge(const Sphere& sphere)
  68. {
  69. const Vector3& center = sphere.center_;
  70. float radius = sphere.radius_;
  71. Merge(center + Vector3(radius, radius, radius));
  72. Merge(center + Vector3(-radius, -radius, -radius));
  73. }
  74. void BoundingBox::Intersect(const BoundingBox& box)
  75. {
  76. if (box.min_.x_ > min_.x_)
  77. min_.x_ = box.min_.x_;
  78. if (box.max_.x_ < max_.x_)
  79. max_.x_ = box.max_.x_;
  80. if (box.min_.y_ > min_.y_)
  81. min_.y_ = box.min_.y_;
  82. if (box.max_.y_ < max_.y_)
  83. max_.y_ = box.max_.y_;
  84. if (box.min_.z_ > min_.z_)
  85. min_.z_ = box.min_.z_;
  86. if (box.max_.z_ < max_.z_)
  87. max_.z_ = box.max_.z_;
  88. if (min_.x_ > max_.x_)
  89. std::swap(min_.x_, max_.x_);
  90. if (min_.y_ > max_.y_)
  91. std::swap(min_.y_, max_.y_);
  92. if (min_.z_ > max_.z_)
  93. std::swap(min_.z_, max_.z_);
  94. }
  95. void BoundingBox::Transform(const Matrix3& transform)
  96. {
  97. Vector3 newCenter = transform * GetCenter();
  98. Vector3 oldEdge = GetSize() * 0.5;
  99. Vector3 newEdge = Vector3(
  100. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  101. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  102. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  103. );
  104. min_ = newCenter - newEdge;
  105. max_ = newCenter + newEdge;
  106. }
  107. void BoundingBox::Transform(const Matrix4x3& transform)
  108. {
  109. Vector3 newCenter = transform * GetCenter();
  110. Vector3 oldEdge = GetSize() * 0.5;
  111. Vector3 newEdge = Vector3(
  112. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  113. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  114. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  115. );
  116. min_ = newCenter - newEdge;
  117. max_ = newCenter + newEdge;
  118. }
  119. BoundingBox BoundingBox::GetTransformed(const Matrix3& transform) const
  120. {
  121. Vector3 newCenter = transform * GetCenter();
  122. Vector3 oldEdge = GetSize() * 0.5;
  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. BoundingBox BoundingBox::GetTransformed(const Matrix4x3& transform) const
  131. {
  132. Vector3 newCenter = transform * GetCenter();
  133. Vector3 oldEdge = GetSize() * 0.5f;
  134. Vector3 newEdge = Vector3(
  135. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  136. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  137. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  138. );
  139. return BoundingBox(newCenter - newEdge, newCenter + newEdge);
  140. }
  141. Rect BoundingBox::GetProjected(const Matrix4& projection) const
  142. {
  143. Vector3 projMin = min_;
  144. Vector3 projMax = max_;
  145. if (projMin.z_ < M_MIN_NEARCLIP)
  146. projMin.z_ = M_MIN_NEARCLIP;
  147. if (projMax.z_ < M_MIN_NEARCLIP)
  148. projMax.z_ = M_MIN_NEARCLIP;
  149. Vector3 vertices[8];
  150. vertices[0] = projMin;
  151. vertices[1] = Vector3(projMax.x_, projMin.y_, projMin.z_);
  152. vertices[2] = Vector3(projMin.x_, projMax.y_, projMin.z_);
  153. vertices[3] = Vector3(projMax.x_, projMax.y_, projMin.z_);
  154. vertices[4] = Vector3(projMin.x_, projMin.y_, projMax.z_);
  155. vertices[5] = Vector3(projMax.x_, projMin.y_, projMax.z_);
  156. vertices[6] = Vector3(projMin.x_, projMax.y_, projMax.z_);
  157. vertices[7] = projMax;
  158. Rect rect;
  159. for (unsigned i = 0; i < 8; ++i)
  160. {
  161. Vector3 projected = projection * vertices[i];
  162. rect.Merge(Vector2(projected.x_, projected.y_));
  163. }
  164. return rect;
  165. }
  166. Intersection BoundingBox::IsInside(const Sphere& sphere) const
  167. {
  168. float distSquared = 0;
  169. float temp;
  170. const Vector3& center = sphere.center_;
  171. if (center.x_ < min_.x_)
  172. {
  173. temp = center.x_ - min_.x_;
  174. distSquared += temp * temp;
  175. }
  176. else if (center.x_ > max_.x_)
  177. {
  178. temp = center.x_ - max_.x_;
  179. distSquared += temp * temp;
  180. }
  181. if (center.y_ < min_.y_)
  182. {
  183. temp = center.y_ - min_.y_;
  184. distSquared += temp * temp;
  185. }
  186. else if (center.y_ > max_.y_)
  187. {
  188. temp = center.y_ - max_.y_;
  189. distSquared += temp * temp;
  190. }
  191. if (center.z_ < min_.z_)
  192. {
  193. temp = center.z_ - min_.z_;
  194. distSquared += temp * temp;
  195. }
  196. else if (center.z_ > max_.z_)
  197. {
  198. temp = center.z_ - max_.z_;
  199. distSquared += temp * temp;
  200. }
  201. float radius = sphere.radius_;
  202. if (distSquared >= radius * radius)
  203. return OUTSIDE;
  204. if ((center.x_ - radius < min_.x_) || (center.x_ + radius > max_.x_))
  205. return INTERSECTS;
  206. if ((center.y_ - radius < min_.y_) || (center.y_ + radius > max_.y_))
  207. return INTERSECTS;
  208. if ((center.z_ - radius < min_.z_) || (center.z_ + radius > max_.z_))
  209. return INTERSECTS;
  210. return INSIDE;
  211. }
  212. Intersection BoundingBox::IsInsideFast(const Sphere& sphere) const
  213. {
  214. float distSquared = 0;
  215. float temp;
  216. const Vector3& center = sphere.center_;
  217. if (center.x_ < min_.x_)
  218. {
  219. temp = center.x_ - min_.x_;
  220. distSquared += temp * temp;
  221. }
  222. else if (center.x_ > max_.x_)
  223. {
  224. temp = center.x_ - max_.x_;
  225. distSquared += temp * temp;
  226. }
  227. if (center.y_ < min_.y_)
  228. {
  229. temp = center.y_ - min_.y_;
  230. distSquared += temp * temp;
  231. }
  232. else if (center.y_ > max_.y_)
  233. {
  234. temp = center.y_ - max_.y_;
  235. distSquared += temp * temp;
  236. }
  237. if (center.z_ < min_.z_)
  238. {
  239. temp = center.z_ - min_.z_;
  240. distSquared += temp * temp;
  241. }
  242. else if (center.z_ > max_.z_)
  243. {
  244. temp = center.z_ - max_.z_;
  245. distSquared += temp * temp;
  246. }
  247. float radius = sphere.radius_;
  248. if (distSquared >= radius * radius)
  249. return OUTSIDE;
  250. return INSIDE;
  251. }
  252. float BoundingBox::GetDistance(const Ray& ray) const
  253. {
  254. // If undefined, no hit (infinite distance)
  255. if (!defined_)
  256. return M_INFINITY;
  257. // Check for ray origin being inside the box
  258. if (IsInside(ray.origin_))
  259. return 0.0f;
  260. float dist = M_INFINITY;
  261. // Check for intersecting in the X-direction
  262. if ((ray.origin_.x_ < min_.x_) && (ray.direction_.x_ > 0.0f))
  263. {
  264. float x = (min_.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. if ((ray.origin_.x_ > max_.x_) && (ray.direction_.x_ < 0.0f))
  274. {
  275. float x = (max_.x_ - ray.origin_.x_) / ray.direction_.x_;
  276. if (x < dist)
  277. {
  278. Vector3 point = ray.origin_ + x * ray.direction_;
  279. if ((point.y_ >= min_.y_) && (point.y_ <= max_.y_) &&
  280. (point.z_ >= min_.z_) && (point.z_ <= max_.z_))
  281. dist = x;
  282. }
  283. }
  284. // Check for intersecting in the Y-direction
  285. if ((ray.origin_.y_ < min_.y_) && (ray.direction_.y_ > 0.0f))
  286. {
  287. float x = (min_.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. if ((ray.origin_.y_ > max_.y_) && (ray.direction_.y_ < 0.0f))
  297. {
  298. float x = (max_.y_ - ray.origin_.y_) / ray.direction_.y_;
  299. if (x < dist)
  300. {
  301. Vector3 point = ray.origin_ + x * ray.direction_;
  302. if ((point.x_ >= min_.x_) && (point.x_ <= max_.x_) &&
  303. (point.z_ >= min_.z_) && (point.z_ <= max_.z_))
  304. dist = x;
  305. }
  306. }
  307. // Check for intersecting in the Z-direction
  308. if ((ray.origin_.z_ < min_.z_) && (ray.direction_.z_ > 0.0f))
  309. {
  310. float x = (min_.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. if ((ray.origin_.z_ > max_.z_) && (ray.direction_.z_ < 0.0f))
  320. {
  321. float x = (max_.z_ - ray.origin_.z_) / ray.direction_.z_;
  322. if (x < dist)
  323. {
  324. Vector3 point = ray.origin_ + x * ray.direction_;
  325. if ((point.x_ >= min_.x_) && (point.x_ <= max_.x_) &&
  326. (point.y_ >= min_.y_) && (point.y_ <= max_.y_))
  327. dist = x;
  328. }
  329. }
  330. return dist;
  331. }