Ray.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. //! Project a point on the ray
  70. Vector3 project(const Vector3& point) const;
  71. //! Return minimum distance to a triangle, or infinity if no hit
  72. float getDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2) const;
  73. //! Return minimum distance to a triangle mesh defined by vertex and index data
  74. float getDistance(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const;
  75. //! Ray origin
  76. Vector3 mOrigin;
  77. //! Ray direction
  78. Vector3 mDirection;
  79. };
  80. #endif // MATH_RAY_H