Ray.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. #pragma once
  24. #include "Vector3.h"
  25. class BoundingBox;
  26. class Frustum;
  27. class Plane;
  28. class Sphere;
  29. /// Infinite straight line in three-dimensional space.
  30. class Ray
  31. {
  32. public:
  33. /// Construct undefined.
  34. Ray()
  35. {
  36. }
  37. /// Construct from origin and direction. The direction must be normalized.
  38. Ray(const Vector3& origin, const Vector3& direction) :
  39. origin_(origin),
  40. direction_(direction)
  41. {
  42. }
  43. /// Copy-construct from another ray.
  44. Ray(const Ray& ray) :
  45. origin_(ray.origin_),
  46. direction_(ray.direction_)
  47. {
  48. }
  49. /// Assign from another ray.
  50. Ray& operator = (const Ray& rhs)
  51. {
  52. origin_ = rhs.origin_;
  53. direction_ = rhs.direction_;
  54. return *this;
  55. }
  56. /// Check for equality with another ray.
  57. bool operator == (const Ray& rhs) const { return origin_ == rhs.origin_ && direction_ == rhs.direction_; }
  58. /// Check for inequality with another ray.
  59. bool operator != (const Ray& rhs) const { return origin_ != rhs.origin_ || direction_ != rhs.direction_; }
  60. /// Define from origin and direction. The direction will be normalized.
  61. void Define(const Vector3& origin, const Vector3& direction)
  62. {
  63. origin_ = origin;
  64. direction_ = direction.Normalized();
  65. }
  66. /// Project a point on the ray.
  67. Vector3 Project(const Vector3& point) const;
  68. /// Return distance of a point from the ray
  69. float Distance(const Vector3& point) const;
  70. /// Return closest point to another ray.
  71. Vector3 ClosestPoint(const Ray& ray) const;
  72. /// Return hit distance to a plane, or infinity if no hit.
  73. float HitDistance(const Plane& plane) const;
  74. /// Return hit distance to a bounding box, or infinity if no hit.
  75. float HitDistance(const BoundingBox& box) const;
  76. /// Return hit distance to a frustum, or infinity if no hit.
  77. float HitDistance(const Frustum& frustum) const;
  78. /// Return hit distance to a sphere, or infinity if no hit.
  79. float HitDistance(const Sphere& sphere) const;
  80. /// Return hit distance to a triangle, or infinity if no hit.
  81. float HitDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2) const;
  82. /// Return hit distance to a triangle mesh defined by vertex and index data, or infinity if no hit.
  83. float HitDistance(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const;
  84. /// Ray origin.
  85. Vector3 origin_;
  86. /// Ray direction.
  87. Vector3 direction_;
  88. };