Ray.cpp 3.6 KB

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