Ray.pkg 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. $#include "Ray.h"
  2. /// Infinite straight line in three-dimensional space.
  3. class Ray
  4. {
  5. public:
  6. /// Construct undefined.
  7. Ray()
  8. {
  9. }
  10. /// Construct from origin and direction. The direction must be normalized.
  11. Ray(const Vector3& origin, const Vector3& direction) :
  12. origin_(origin),
  13. direction_(direction)
  14. {
  15. }
  16. /// Copy-construct from another ray.
  17. Ray(const Ray& ray) :
  18. origin_(ray.origin_),
  19. direction_(ray.direction_)
  20. {
  21. }
  22. /// Check for equality with another ray.
  23. bool operator == (const Ray& rhs) const { return origin_ == rhs.origin_ && direction_ == rhs.direction_; }
  24. /// Define from origin and direction. The direction will be normalized.
  25. void Define(const Vector3& origin, const Vector3& direction)
  26. {
  27. origin_ = origin;
  28. direction_ = direction.Normalized();
  29. }
  30. /// Project a point on the ray.
  31. Vector3 Project(const Vector3& point) const;
  32. /// Return distance of a point from the ray
  33. float Distance(const Vector3& point) const;
  34. /// Return closest point to another ray.
  35. Vector3 ClosestPoint(const Ray& ray) const;
  36. /// Return hit distance to a plane, or infinity if no hit.
  37. float HitDistance(const Plane& plane) const;
  38. /// Return hit distance to a bounding box, or infinity if no hit.
  39. float HitDistance(const BoundingBox& box) const;
  40. /// 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.
  41. float HitDistance(const Frustum& frustum, bool solidInside = true) const;
  42. /// Return hit distance to a sphere, or infinity if no hit.
  43. float HitDistance(const Sphere& sphere) const;
  44. /// Return hit distance to a triangle, or infinity if no hit.
  45. float HitDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2) const;
  46. /// Return hit distance to non-indexed geometry data, or infinity if no hit.
  47. float HitDistance(const void* vertexData, unsigned vertexSize, unsigned vertexStart, unsigned vertexCount) const;
  48. /// Return hit distance to indexed geometry data, or infinity if no hit.
  49. float HitDistance(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const;
  50. /// Return whether ray is inside non-indexed geometry.
  51. bool InsideGeometry(const void* vertexData, unsigned vertexSize, unsigned vertexStart, unsigned vertexCount) const;
  52. /// Return whether ray is inside indexed geometry.
  53. bool InsideGeometry(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const;
  54. /// Ray origin.
  55. Vector3 origin_ @ origin;
  56. /// Ray direction.
  57. Vector3 direction_ @ direction;
  58. };