Ray.cpp 8.6 KB

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