Ray.h 5.3 KB

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