Ray.cpp 7.7 KB

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