Ray.cpp 9.4 KB

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