Ray.h 3.7 KB

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