CmRay.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include "CmVector3.h"
  4. namespace CamelotFramework
  5. {
  6. /**
  7. * @brief A ray in 3D space represented with an origin and direction.
  8. */
  9. class CM_UTILITY_EXPORT Ray
  10. {
  11. public:
  12. Ray()
  13. :mOrigin(Vector3::ZERO), mDirection(Vector3::UNIT_Z)
  14. { }
  15. Ray(const Vector3& origin, const Vector3& direction)
  16. :mOrigin(origin), mDirection(direction)
  17. { }
  18. void setOrigin(const Vector3& origin) { mOrigin = origin; }
  19. const Vector3& getOrigin(void) const { return mOrigin; }
  20. void setDirection(const Vector3& dir) { mDirection = dir; }
  21. const Vector3& getDirection(void) const {return mDirection;}
  22. /**
  23. * @brief Gets the position of a point t units along the ray.
  24. */
  25. Vector3 getPoint(float t) const
  26. {
  27. return Vector3(mOrigin + (mDirection * t));
  28. }
  29. /**
  30. * @brief Gets the position of a point t units along the ray.
  31. */
  32. Vector3 operator*(float t) const
  33. {
  34. return getPoint(t);
  35. }
  36. /**
  37. * @brief Ray/plane intersection, returns boolean result and distance to intersection point.
  38. */
  39. std::pair<bool, float> intersects(const Plane& p) const;
  40. /**
  41. * @brief Ray/sphere intersection, returns boolean result and distance to nearest intersection point.
  42. */
  43. std::pair<bool, float> intersects(const Sphere& s) const;
  44. /**
  45. * @brief Ray/axis aligned box intersection, returns boolean result and distance to nearest intersection point.
  46. */
  47. std::pair<bool, float> intersects(const AABox& box) const;
  48. /**
  49. * @brief Ray/triangle intersection, returns boolean result and distance to intersection point.
  50. *
  51. * @param a Triangle first vertex.
  52. * @param b Triangle second vertex.
  53. * @param c Triangle third vertex.
  54. * @param normal The normal of the triangle. Doesn't need to be normalized.
  55. * @param positiveSide (optional) Should intersections with the positive side (normal facing) count.
  56. * @param negativeSide (optional) Should intersections with the negative side (opposite of normal facing) count.
  57. *
  58. * @return Boolean result if intersection happened and distance to intersection point.
  59. */
  60. std::pair<bool, float> intersects(const Vector3& a, const Vector3& b, const Vector3& c,
  61. const Vector3& normal, bool positiveSide = true, bool negativeSide = true) const;
  62. protected:
  63. Vector3 mOrigin;
  64. Vector3 mDirection;
  65. };
  66. }