Ray.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Math/Vector3.h"
  5. #include "../Math/Matrix3x4.h"
  6. namespace Urho3D
  7. {
  8. class BoundingBox;
  9. class Frustum;
  10. class Plane;
  11. class Sphere;
  12. /// Infinite straight line in three-dimensional space.
  13. /// @allfloats
  14. class URHO3D_API Ray
  15. {
  16. public:
  17. /// Construct a degenerate ray with zero origin and direction.
  18. Ray() noexcept = default;
  19. /// Construct from origin and direction. The direction will be normalized.
  20. Ray(const Vector3& origin, const Vector3& direction) noexcept
  21. {
  22. Define(origin, direction);
  23. }
  24. /// Copy-construct from another ray.
  25. Ray(const Ray& ray) noexcept = default;
  26. /// Assign from another ray.
  27. Ray& operator =(const Ray& rhs) noexcept = default;
  28. /// Check for equality with another ray.
  29. bool operator ==(const Ray& rhs) const { return origin_ == rhs.origin_ && direction_ == rhs.direction_; }
  30. /// Check for inequality with another ray.
  31. bool operator !=(const Ray& rhs) const { return origin_ != rhs.origin_ || direction_ != rhs.direction_; }
  32. /// Define from origin and direction. The direction will be normalized.
  33. void Define(const Vector3& origin, const Vector3& direction)
  34. {
  35. origin_ = origin;
  36. direction_ = direction.Normalized();
  37. }
  38. /// Project a point on the ray.
  39. Vector3 Project(const Vector3& point) const
  40. {
  41. Vector3 offset = point - origin_;
  42. return origin_ + offset.DotProduct(direction_) * direction_;
  43. }
  44. /// Return distance of a point from the ray.
  45. float Distance(const Vector3& point) const
  46. {
  47. Vector3 projected = Project(point);
  48. return (point - projected).Length();
  49. }
  50. /// Return closest point to another ray.
  51. Vector3 ClosestPoint(const Ray& ray) const;
  52. /// Return hit distance to a plane, or infinity if no hit.
  53. float HitDistance(const Plane& plane) const;
  54. /// Return hit distance to a bounding box, or infinity if no hit.
  55. float HitDistance(const BoundingBox& box) const;
  56. /// 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.
  57. float HitDistance(const Frustum& frustum, bool solidInside = true) const;
  58. /// Return hit distance to a sphere, or infinity if no hit.
  59. float HitDistance(const Sphere& sphere) const;
  60. /// Return hit distance to a triangle, or infinity if no hit. Optionally return hit normal and hit barycentric coordinate at intersect point.
  61. float HitDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2, Vector3* outNormal = nullptr, Vector3* outBary = nullptr) const;
  62. /// Return hit distance to non-indexed geometry data, or infinity if no hit. Optionally return hit normal and hit uv coordinates at intersect point.
  63. float HitDistance
  64. (const void* vertexData, i32 vertexStride, i32 vertexStart, i32 vertexCount, Vector3* outNormal = nullptr,
  65. Vector2* outUV = nullptr, i32 uvOffset = 0) const;
  66. /// Return hit distance to indexed geometry data, or infinity if no hit. Optionally return hit normal and hit uv coordinates at intersect point.
  67. float HitDistance(const void* vertexData, i32 vertexStride, const void* indexData, i32 indexSize, i32 indexStart,
  68. i32 indexCount, Vector3* outNormal = nullptr, Vector2* outUV = nullptr, i32 uvOffset = 0) const;
  69. /// Return whether ray is inside non-indexed geometry.
  70. bool InsideGeometry(const void* vertexData, i32 vertexSize, i32 vertexStart, i32 vertexCount) const;
  71. /// Return whether ray is inside indexed geometry.
  72. bool InsideGeometry(const void* vertexData, i32 vertexSize, const void* indexData, i32 indexSize, i32 indexStart,
  73. i32 indexCount) const;
  74. /// Return transformed by a 3x4 matrix. This may result in a non-normalized direction.
  75. Ray Transformed(const Matrix3x4& transform) const;
  76. /// Ray origin.
  77. Vector3 origin_;
  78. /// Ray direction.
  79. Vector3 direction_;
  80. };
  81. }