Ray.cpp 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "Ray.h"
  25. Vector3 Ray::Project(const Vector3& point) const
  26. {
  27. Vector3 offset = point - origin_;
  28. return origin_ + offset.DotProduct(direction_) * direction_;
  29. }
  30. float Ray::Distance(const Vector3& v0, const Vector3& v1, const Vector3& v2) const
  31. {
  32. // Based on Fast, Minimum Storage Ray/Triangle Intersection by Möller & Trumbore
  33. // http://www.graphics.cornell.edu/pubs/1997/MT97.pdf
  34. // Calculate edge vectors
  35. Vector3 edge1 = v1 - v0;
  36. Vector3 edge2 = v2 - v0;
  37. // Calculate determinant & check backfacing
  38. Vector3 p = direction_.CrossProduct(edge2);
  39. float det = edge1.DotProduct(p);
  40. if (det >= M_EPSILON)
  41. {
  42. // Calculate u & v parameters and test
  43. Vector3 t = origin_ - v0;
  44. float u = t.DotProduct(p);
  45. if (u >= 0.0f && u <= det)
  46. {
  47. Vector3 q = t.CrossProduct(edge1);
  48. float v = direction_.DotProduct(q);
  49. if (v >= 0.0f && u + v <= det)
  50. {
  51. // There is an intersection, so calculate distance
  52. return edge2.DotProduct(q) / det;
  53. }
  54. }
  55. }
  56. return M_INFINITY;
  57. }
  58. float Ray::Distance(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const
  59. {
  60. float nearest = M_INFINITY;
  61. const unsigned char* vertices = (const unsigned char*)vertexData;
  62. // 16-bit indices
  63. if (indexSize == sizeof(unsigned short))
  64. {
  65. const unsigned short* indices = (const unsigned short*)indexData;
  66. for (unsigned i = indexStart; i < indexStart + indexCount; i += 3)
  67. {
  68. const Vector3& v0 = *((const Vector3*)(&vertices[indices[i] * vertexSize]));
  69. const Vector3& v1 = *((const Vector3*)(&vertices[indices[i + 1] * vertexSize]));
  70. const Vector3& v2 = *((const Vector3*)(&vertices[indices[i + 2] * vertexSize]));
  71. nearest = Min(nearest, Distance(v0, v1, v2));
  72. }
  73. }
  74. // 32-bit indices
  75. else
  76. {
  77. const unsigned* indices = (const unsigned*)indexData;
  78. for (unsigned i = indexStart; i < indexStart + indexCount; i += 3)
  79. {
  80. const Vector3& v0 = *((const Vector3*)(&vertices[indices[i] * vertexSize]));
  81. const Vector3& v1 = *((const Vector3*)(&vertices[indices[i + 1] * vertexSize]));
  82. const Vector3& v2 = *((const Vector3*)(&vertices[indices[i + 2] * vertexSize]));
  83. nearest = Min(nearest, Distance(v0, v1, v2));
  84. }
  85. }
  86. return nearest;
  87. }