Ray.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Math/Vector3.h"
  24. #include "../Math/Matrix3x4.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. /// @allfloats
  33. class URHO3D_API Ray
  34. {
  35. public:
  36. /// Construct a degenerate ray with zero origin and direction.
  37. Ray() noexcept = default;
  38. /// Construct from origin and direction. The direction will be normalized.
  39. Ray(const Vector3& origin, const Vector3& direction) noexcept
  40. {
  41. Define(origin, direction);
  42. }
  43. /// Copy-construct from another ray.
  44. Ray(const Ray& ray) noexcept = default;
  45. /// Assign from another ray.
  46. Ray& operator =(const Ray& rhs) noexcept = default;
  47. /// Check for equality with another ray.
  48. bool operator ==(const Ray& rhs) const { return origin_ == rhs.origin_ && direction_ == rhs.direction_; }
  49. /// Check for inequality with another ray.
  50. bool operator !=(const Ray& rhs) const { return origin_ != rhs.origin_ || direction_ != rhs.direction_; }
  51. /// Define from origin and direction. The direction will be normalized.
  52. void Define(const Vector3& origin, const Vector3& direction)
  53. {
  54. origin_ = origin;
  55. direction_ = direction.Normalized();
  56. }
  57. /// Project a point on the ray.
  58. Vector3 Project(const Vector3& point) const
  59. {
  60. Vector3 offset = point - origin_;
  61. return origin_ + offset.DotProduct(direction_) * direction_;
  62. }
  63. /// Return distance of a point from the ray.
  64. float Distance(const Vector3& point) const
  65. {
  66. Vector3 projected = Project(point);
  67. return (point - projected).Length();
  68. }
  69. /// Return closest point to another ray.
  70. Vector3 ClosestPoint(const Ray& ray) const;
  71. /// Return hit distance to a plane, or infinity if no hit.
  72. float HitDistance(const Plane& plane) const;
  73. /// Return hit distance to a bounding box, or infinity if no hit.
  74. float HitDistance(const BoundingBox& box) const;
  75. /// Return hit distance to a frustum, or infinity if no hit. If solidInside parameter is true (default) rays originating from inside return zero distance, otherwise the distance to the closest plane.
  76. float HitDistance(const Frustum& frustum, bool solidInside = true) const;
  77. /// Return hit distance to a sphere, or infinity if no hit.
  78. float HitDistance(const Sphere& sphere) const;
  79. /// Return hit distance to a triangle, or infinity if no hit. Optionally return hit normal and hit barycentric coordinate at intersect point.
  80. float HitDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2, Vector3* outNormal = nullptr, Vector3* outBary = nullptr) const;
  81. /// Return hit distance to non-indexed geometry data, or infinity if no hit. Optionally return hit normal and hit uv coordinates at intersect point.
  82. float HitDistance
  83. (const void* vertexData, unsigned vertexStride, unsigned vertexStart, unsigned vertexCount, Vector3* outNormal = nullptr,
  84. Vector2* outUV = nullptr, unsigned uvOffset = 0) const;
  85. /// Return hit distance to indexed geometry data, or infinity if no hit. Optionally return hit normal and hit uv coordinates at intersect point.
  86. float HitDistance(const void* vertexData, unsigned vertexStride, const void* indexData, unsigned indexSize, unsigned indexStart,
  87. unsigned indexCount, Vector3* outNormal = nullptr, Vector2* outUV = nullptr, unsigned uvOffset = 0) const;
  88. /// Return whether ray is inside non-indexed geometry.
  89. bool InsideGeometry(const void* vertexData, unsigned vertexSize, unsigned vertexStart, unsigned vertexCount) const;
  90. /// Return whether ray is inside indexed geometry.
  91. bool InsideGeometry(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart,
  92. unsigned indexCount) const;
  93. /// Return transformed by a 3x4 matrix. This may result in a non-normalized direction.
  94. Ray Transformed(const Matrix3x4& transform) const;
  95. /// Ray origin.
  96. Vector3 origin_;
  97. /// Ray direction.
  98. Vector3 direction_;
  99. };
  100. }