Ray.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef MATH_RAY_H
  24. #define MATH_RAY_H
  25. #include "Vector3.h"
  26. //! An infinite straight line in three-dimensional space
  27. class Ray
  28. {
  29. public:
  30. //! Construct an undefined ray
  31. Ray()
  32. {
  33. }
  34. //! Construct from origin and direction. The direction must be normalized
  35. Ray(const Vector3& origin, const Vector3& direction) :
  36. mOrigin(origin),
  37. mDirection(direction)
  38. {
  39. }
  40. //! Copy-construct from another ray
  41. Ray(const Ray& ray) :
  42. mOrigin(ray.mOrigin),
  43. mDirection(ray.mDirection)
  44. {
  45. }
  46. //! Assign from another ray
  47. Ray& operator = (const Ray& rhs)
  48. {
  49. mOrigin = rhs.mOrigin;
  50. mDirection = rhs.mDirection;
  51. return *this;
  52. }
  53. //! Check for equality with another ray
  54. bool operator == (const Ray& rhs) const
  55. {
  56. return (mOrigin == rhs.mOrigin) && (mDirection == rhs.mDirection);
  57. }
  58. //! Check for inequality with another ray
  59. bool operator != (const Ray& rhs) const
  60. {
  61. return (mOrigin != rhs.mOrigin) || (mDirection != rhs.mDirection);
  62. }
  63. //! Define from origin and direction. The direction will be normalized
  64. void define(const Vector3& origin, const Vector3& direction)
  65. {
  66. mOrigin = origin;
  67. mDirection = direction.getNormalized();
  68. }
  69. //! Return minimum distance to a triangle, or infinity if no hit
  70. float getDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2) const;
  71. //! Return minimum distance to a triangle mesh defined by vertex and index data
  72. float getDistance(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const;
  73. //! Ray origin
  74. Vector3 mOrigin;
  75. //! Ray direction
  76. Vector3 mDirection;
  77. };
  78. #endif // MATH_RAY_H