Ray.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. class URHO3D_API Ray
  33. {
  34. public:
  35. /// Construct a degenerate ray with zero origin and direction.
  36. Ray() noexcept = default;
  37. /// Construct from origin and direction. The direction will be normalized.
  38. Ray(const Vector3& origin, const Vector3& direction) noexcept
  39. {
  40. Define(origin, direction);
  41. }
  42. /// Copy-construct from another ray.
  43. Ray(const Ray& ray) noexcept = default;
  44. /// Assign from another ray.
  45. Ray& operator =(const Ray& rhs) noexcept = default;
  46. /// Check for equality with another ray.
  47. bool operator ==(const Ray& rhs) const { return origin_ == rhs.origin_ && direction_ == rhs.direction_; }
  48. /// Check for inequality with another ray.
  49. bool operator !=(const Ray& rhs) const { return origin_ != rhs.origin_ || direction_ != rhs.direction_; }
  50. /// Define from origin and direction. The direction will be normalized.
  51. void Define(const Vector3& origin, const Vector3& direction)
  52. {
  53. origin_ = origin;
  54. direction_ = direction.Normalized();
  55. }
  56. /// Project a point on the ray.
  57. Vector3 Project(const Vector3& point) const
  58. {
  59. Vector3 offset = point - origin_;
  60. return origin_ + offset.DotProduct(direction_) * direction_;
  61. }
  62. /// Return distance of a point from the ray.
  63. float Distance(const Vector3& point) const
  64. {
  65. Vector3 projected = Project(point);
  66. return (point - projected).Length();
  67. }
  68. /// Return closest point to another ray.
  69. Vector3 ClosestPoint(const Ray& ray) const;
  70. /// Return hit distance to a plane, or infinity if no hit.
  71. float HitDistance(const Plane& plane) const;
  72. /// Return hit distance to a bounding box, or infinity if no hit.
  73. float HitDistance(const BoundingBox& box) const;
  74. /// 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.
  75. float HitDistance(const Frustum& frustum, bool solidInside = true) const;
  76. /// Return hit distance to a sphere, or infinity if no hit.
  77. float HitDistance(const Sphere& sphere) const;
  78. /// Return hit distance to a triangle, or infinity if no hit. Optionally return hit normal and hit barycentric coordinate at intersect point.
  79. float HitDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2, Vector3* outNormal = nullptr, Vector3* outBary = nullptr) const;
  80. /// Return hit distance to non-indexed geometry data, or infinity if no hit. Optionally return hit normal and hit uv coordinates at intersect point.
  81. float HitDistance
  82. (const void* vertexData, unsigned vertexStride, unsigned vertexStart, unsigned vertexCount, Vector3* outNormal = nullptr,
  83. Vector2* outUV = nullptr, unsigned uvOffset = 0) const;
  84. /// Return hit distance to indexed geometry data, or infinity if no hit. Optionally return hit normal and hit uv coordinates at intersect point.
  85. float HitDistance(const void* vertexData, unsigned vertexStride, const void* indexData, unsigned indexSize, unsigned indexStart,
  86. unsigned indexCount, Vector3* outNormal = nullptr, Vector2* outUV = nullptr, unsigned uvOffset = 0) const;
  87. /// Return whether ray is inside non-indexed geometry.
  88. bool InsideGeometry(const void* vertexData, unsigned vertexSize, unsigned vertexStart, unsigned vertexCount) const;
  89. /// Return whether ray is inside indexed geometry.
  90. bool InsideGeometry(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart,
  91. unsigned indexCount) const;
  92. /// Return transformed by a 3x4 matrix. This may result in a non-normalized direction.
  93. Ray Transformed(const Matrix3x4& transform) const;
  94. /// Ray origin.
  95. Vector3 origin_;
  96. /// Ray direction.
  97. Vector3 direction_;
  98. };
  99. }