Ray.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Collision/Common.h>
  7. namespace anki {
  8. /// @addtogroup collision
  9. /// @{
  10. /// Ray.
  11. class Ray
  12. {
  13. public:
  14. static constexpr CollisionShapeType kClassType = CollisionShapeType::kRay;
  15. /// Will not initialize any memory, nothing.
  16. Ray()
  17. {
  18. // Do nothing.
  19. }
  20. Ray(const Vec4& origin, const Vec4& dir)
  21. : m_origin(origin)
  22. , m_dir(dir)
  23. {
  24. check();
  25. }
  26. Ray(const Vec3& origin, const Vec3& dir)
  27. : m_origin(origin.xyz0)
  28. , m_dir(dir.xyz0)
  29. {
  30. check();
  31. }
  32. /// Copy.
  33. Ray(const Ray& other)
  34. {
  35. operator=(other);
  36. }
  37. Ray& operator=(const Ray& other)
  38. {
  39. other.check();
  40. m_origin = other.m_origin;
  41. m_dir = other.m_dir;
  42. return *this;
  43. }
  44. void setOrigin(const Vec4& origin)
  45. {
  46. m_origin = origin;
  47. }
  48. void setOrigin(const Vec3& origin)
  49. {
  50. m_origin = origin.xyz0;
  51. }
  52. [[nodiscard]] const Vec4& getOrigin() const
  53. {
  54. check();
  55. return m_origin;
  56. }
  57. void setDirection(const Vec4& dir)
  58. {
  59. m_dir = dir;
  60. }
  61. void setDirection(const Vec3& dir)
  62. {
  63. m_dir = dir.xyz0;
  64. }
  65. [[nodiscard]] const Vec4& getDirection() const
  66. {
  67. check();
  68. return m_dir;
  69. }
  70. [[nodiscard]] Ray getTransformed(const Transform& trf) const
  71. {
  72. check();
  73. Ray out;
  74. out.m_origin = trf.transform(m_origin);
  75. out.m_dir = Vec4(trf.getRotation() * m_dir, 0.0f);
  76. return out;
  77. }
  78. private:
  79. Vec4 m_origin
  80. #if ANKI_ASSERTIONS_ENABLED
  81. = Vec4(kMaxF32)
  82. #endif
  83. ;
  84. Vec4 m_dir
  85. #if ANKI_ASSERTIONS_ENABLED
  86. = Vec4(kMaxF32)
  87. #endif
  88. ;
  89. void check() const
  90. {
  91. ANKI_ASSERT(m_origin.w == 0.0f && m_dir.w == 0.0f && isZero(m_dir.lengthSquared() - 1.0f, kEpsilonf * 100.0f));
  92. }
  93. };
  94. /// @}
  95. } // end namespace anki