Ray.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Vector.h>
  9. // The ray is represented as P+t*D, where P is the ray origin, D is a
  10. // unit-length direction vector, and t >= 0. The user must ensure that D is
  11. // unit length.
  12. namespace WwiseGTE
  13. {
  14. template <int N, typename Real>
  15. class Ray
  16. {
  17. public:
  18. // Construction and destruction. The default constructor sets the
  19. // origin to (0,...,0) and the ray direction to (1,0,...,0).
  20. Ray()
  21. {
  22. origin.MakeZero();
  23. direction.MakeUnit(0);
  24. }
  25. Ray(Vector<N, Real> const& inOrigin, Vector<N, Real> const& inDirection)
  26. :
  27. origin(inOrigin),
  28. direction(inDirection)
  29. {
  30. }
  31. // Public member access. The direction must be unit length.
  32. Vector<N, Real> origin, direction;
  33. public:
  34. // Comparisons to support sorted containers.
  35. bool operator==(Ray const& ray) const
  36. {
  37. return origin == ray.origin && direction == ray.direction;
  38. }
  39. bool operator!=(Ray const& ray) const
  40. {
  41. return !operator==(ray);
  42. }
  43. bool operator< (Ray const& ray) const
  44. {
  45. if (origin < ray.origin)
  46. {
  47. return true;
  48. }
  49. if (origin > ray.origin)
  50. {
  51. return false;
  52. }
  53. return direction < ray.direction;
  54. }
  55. bool operator<=(Ray const& ray) const
  56. {
  57. return !ray.operator<(*this);
  58. }
  59. bool operator> (Ray const& ray) const
  60. {
  61. return ray.operator<(*this);
  62. }
  63. bool operator>=(Ray const& ray) const
  64. {
  65. return !operator<(ray);
  66. }
  67. };
  68. // Template aliases for convenience.
  69. template <typename Real>
  70. using Ray2 = Ray<2, Real>;
  71. template <typename Real>
  72. using Ray3 = Ray<3, Real>;
  73. }