Ray.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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 "BoundingBox.h"
  25. #include "Frustum.h"
  26. #include "Plane.h"
  27. #include "Ray.h"
  28. #include "Sphere.h"
  29. namespace Urho3D
  30. {
  31. Vector3 Ray::Project(const Vector3& point) const
  32. {
  33. Vector3 offset = point - origin_;
  34. return origin_ + offset.DotProduct(direction_) * direction_;
  35. }
  36. float Ray::Distance(const Vector3& point) const
  37. {
  38. Vector3 projected = Project(point);
  39. return (point - projected).Length();
  40. }
  41. Vector3 Ray::ClosestPoint(const Ray& ray) const
  42. {
  43. // Algorithm based on http://paulbourke.net/geometry/lineline3d/
  44. Vector3 p13 = origin_ - ray.origin_;
  45. Vector3 p43 = ray.direction_;
  46. Vector3 p21 = direction_;
  47. float d1343 = p13.DotProduct(p43);
  48. float d4321 = p43.DotProduct(p21);
  49. float d1321 = p13.DotProduct(p21);
  50. float d4343 = p43.DotProduct(p43);
  51. float d2121 = p21.DotProduct(p21);
  52. float d = d2121 * d4343 - d4321 * d4321;
  53. if (Abs(d) < M_EPSILON)
  54. return origin_;
  55. float n = d1343 * d4321 - d1321 * d4343;
  56. float a = n / d;
  57. return origin_ + a * direction_;
  58. }
  59. float Ray::HitDistance(const Plane& plane) const
  60. {
  61. float d = plane.normal_.DotProduct(direction_);
  62. if (Abs(d) >= M_EPSILON)
  63. {
  64. float t = -(plane.normal_.DotProduct(origin_) - plane.intercept_) / d;
  65. if (t >= 0.0f)
  66. return t;
  67. else
  68. return M_INFINITY;
  69. }
  70. else
  71. return M_INFINITY;
  72. }
  73. float Ray::HitDistance(const BoundingBox& box) const
  74. {
  75. // If undefined, no hit (infinite distance)
  76. if (!box.defined_)
  77. return M_INFINITY;
  78. // Check for ray origin being inside the box
  79. if (box.IsInside(origin_))
  80. return 0.0f;
  81. float dist = M_INFINITY;
  82. // Check for intersecting in the X-direction
  83. if (origin_.x_ < box.min_.x_ && direction_.x_ > 0.0f)
  84. {
  85. float x = (box.min_.x_ - origin_.x_) / direction_.x_;
  86. if (x < dist)
  87. {
  88. Vector3 point = origin_ + x * direction_;
  89. if (point.y_ >= box.min_.y_ && point.y_ <= box.max_.y_ && point.z_ >= box.min_.z_ && point.z_ <= box.max_.z_)
  90. dist = x;
  91. }
  92. }
  93. if (origin_.x_ > box.max_.x_ && direction_.x_ < 0.0f)
  94. {
  95. float x = (box.max_.x_ - origin_.x_) / direction_.x_;
  96. if (x < dist)
  97. {
  98. Vector3 point = origin_ + x * direction_;
  99. if (point.y_ >= box.min_.y_ && point.y_ <= box.max_.y_ && point.z_ >= box.min_.z_ && point.z_ <= box.max_.z_)
  100. dist = x;
  101. }
  102. }
  103. // Check for intersecting in the Y-direction
  104. if (origin_.y_ < box.min_.y_ && direction_.y_ > 0.0f)
  105. {
  106. float x = (box.min_.y_ - origin_.y_) / direction_.y_;
  107. if (x < dist)
  108. {
  109. Vector3 point = origin_ + x * direction_;
  110. if (point.x_ >= box.min_.x_ && point.x_ <= box.max_.x_ && point.z_ >= box.min_.z_ && point.z_ <= box.max_.z_)
  111. dist = x;
  112. }
  113. }
  114. if (origin_.y_ > box.max_.y_ && direction_.y_ < 0.0f)
  115. {
  116. float x = (box.max_.y_ - origin_.y_) / direction_.y_;
  117. if (x < dist)
  118. {
  119. Vector3 point = origin_ + x * direction_;
  120. if (point.x_ >= box.min_.x_ && point.x_ <= box.max_.x_ && point.z_ >= box.min_.z_ && point.z_ <= box.max_.z_)
  121. dist = x;
  122. }
  123. }
  124. // Check for intersecting in the Z-direction
  125. if (origin_.z_ < box.min_.z_ && direction_.z_ > 0.0f)
  126. {
  127. float x = (box.min_.z_ - origin_.z_) / direction_.z_;
  128. if (x < dist)
  129. {
  130. Vector3 point = origin_ + x * direction_;
  131. if (point.x_ >= box.min_.x_ && point.x_ <= box.max_.x_ && point.y_ >= box.min_.y_ && point.y_ <= box.max_.y_)
  132. dist = x;
  133. }
  134. }
  135. if (origin_.z_ > box.max_.z_ && direction_.z_ < 0.0f)
  136. {
  137. float x = (box.max_.z_ - origin_.z_) / direction_.z_;
  138. if (x < dist)
  139. {
  140. Vector3 point = origin_ + x * direction_;
  141. if (point.x_ >= box.min_.x_ && point.x_ <= box.max_.x_ && point.y_ >= box.min_.y_ && point.y_ <= box.max_.y_)
  142. dist = x;
  143. }
  144. }
  145. return dist;
  146. }
  147. float Ray::HitDistance(const Frustum& frustum) const
  148. {
  149. float maxOutside = 0.0f;
  150. float minInside = M_INFINITY;
  151. bool allInside = true;
  152. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  153. {
  154. const Plane& plane = frustum.planes_[i];
  155. float distance = HitDistance(frustum.planes_[i]);
  156. if (plane.Distance(origin_) < 0.0f)
  157. {
  158. maxOutside = Max(maxOutside, distance);
  159. allInside = false;
  160. }
  161. else
  162. minInside = Min(minInside, distance);
  163. }
  164. if (allInside)
  165. return 0.0f;
  166. else if (maxOutside <= minInside)
  167. return maxOutside;
  168. else
  169. return M_INFINITY;
  170. }
  171. float Ray::HitDistance(const Sphere& sphere) const
  172. {
  173. Vector3 centeredOrigin = origin_ - sphere.center_;
  174. float squaredRadius = sphere.radius_ * sphere.radius_;
  175. // Check if ray originates inside the sphere
  176. if (centeredOrigin.LengthSquared() <= squaredRadius)
  177. return 0.0f;
  178. // Calculate intersection by quadratic equation
  179. float a = direction_.DotProduct(direction_);
  180. float b = 2.0f * centeredOrigin.DotProduct(direction_);
  181. float c = centeredOrigin.DotProduct(centeredOrigin) - squaredRadius;
  182. float d = b * b - 4.0f * a * c;
  183. // No solution
  184. if (d < 0.0f)
  185. return M_INFINITY;
  186. // Get the nearer solution
  187. float dSqrt = sqrtf(d);
  188. float dist = (-b - dSqrt) / (2.0f * a);
  189. if (dist >= 0.0f)
  190. return dist;
  191. else
  192. return (-b + dSqrt) / (2.0f * a);
  193. }
  194. float Ray::HitDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2) const
  195. {
  196. // Based on Fast, Minimum Storage Ray/Triangle Intersection by Möller & Trumbore
  197. // http://www.graphics.cornell.edu/pubs/1997/MT97.pdf
  198. // Calculate edge vectors
  199. Vector3 edge1(v1 - v0);
  200. Vector3 edge2(v2 - v0);
  201. // Calculate determinant & check backfacing
  202. Vector3 p(direction_.CrossProduct(edge2));
  203. float det = edge1.DotProduct(p);
  204. if (det >= M_EPSILON)
  205. {
  206. // Calculate u & v parameters and test
  207. Vector3 t(origin_ - v0);
  208. float u = t.DotProduct(p);
  209. if (u >= 0.0f && u <= det)
  210. {
  211. Vector3 q(t.CrossProduct(edge1));
  212. float v = direction_.DotProduct(q);
  213. if (v >= 0.0f && u + v <= det)
  214. {
  215. // There is an intersection, so calculate distance
  216. return edge2.DotProduct(q) / det;
  217. }
  218. }
  219. }
  220. return M_INFINITY;
  221. }
  222. float Ray::HitDistance(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const
  223. {
  224. float nearest = M_INFINITY;
  225. const unsigned char* vertices = (const unsigned char*)vertexData;
  226. // 16-bit indices
  227. if (indexSize == sizeof(unsigned short))
  228. {
  229. const unsigned short* indices = ((const unsigned short*)indexData) + indexStart;
  230. const unsigned short* indicesEnd = indices + indexCount;
  231. while (indices < indicesEnd)
  232. {
  233. const Vector3& v0 = *((const Vector3*)(&vertices[indices[0] * vertexSize]));
  234. const Vector3& v1 = *((const Vector3*)(&vertices[indices[1] * vertexSize]));
  235. const Vector3& v2 = *((const Vector3*)(&vertices[indices[2] * vertexSize]));
  236. nearest = Min(nearest, HitDistance(v0, v1, v2));
  237. indices += 3;
  238. }
  239. }
  240. // 32-bit indices
  241. else
  242. {
  243. const unsigned* indices = ((const unsigned*)indexData) + indexStart;
  244. const unsigned* indicesEnd = indices + indexCount;
  245. while (indices < indicesEnd)
  246. {
  247. const Vector3& v0 = *((const Vector3*)(&vertices[indices[0] * vertexSize]));
  248. const Vector3& v1 = *((const Vector3*)(&vertices[indices[1] * vertexSize]));
  249. const Vector3& v2 = *((const Vector3*)(&vertices[indices[2] * vertexSize]));
  250. nearest = Min(nearest, HitDistance(v0, v1, v2));
  251. indices += 3;
  252. }
  253. }
  254. return nearest;
  255. }
  256. }