Ray.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2009-2021, 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. {
  9. /// @addtogroup collision
  10. /// @{
  11. /// Ray.
  12. class Ray
  13. {
  14. public:
  15. static constexpr CollisionShapeType CLASS_TYPE = CollisionShapeType::RAY;
  16. /// Will not initialize any memory, nothing.
  17. Ray()
  18. {
  19. // Do nothing.
  20. }
  21. Ray(const Vec4& origin, const Vec4& dir)
  22. : m_origin(origin)
  23. , m_dir(dir)
  24. {
  25. check();
  26. }
  27. Ray(const Vec3& origin, const Vec3& dir)
  28. : m_origin(origin.xyz0())
  29. , m_dir(dir.xyz0())
  30. {
  31. check();
  32. }
  33. /// Copy.
  34. Ray(const Ray& other)
  35. {
  36. operator=(other);
  37. }
  38. Ray& operator=(const Ray& other)
  39. {
  40. other.check();
  41. m_origin = other.m_origin;
  42. m_dir = other.m_dir;
  43. return *this;
  44. }
  45. void setOrigin(const Vec4& origin)
  46. {
  47. m_origin = origin;
  48. }
  49. void setOrigin(const Vec3& origin)
  50. {
  51. m_origin = origin.xyz0();
  52. }
  53. ANKI_USE_RESULT const Vec4& getOrigin() const
  54. {
  55. check();
  56. return m_origin;
  57. }
  58. void setDirection(const Vec4& dir)
  59. {
  60. m_dir = dir;
  61. }
  62. void setDirection(const Vec3& dir)
  63. {
  64. m_dir = dir.xyz0();
  65. }
  66. ANKI_USE_RESULT const Vec4& getDirection() const
  67. {
  68. check();
  69. return m_dir;
  70. }
  71. ANKI_USE_RESULT Ray getTransformed(const Transform& trf) const
  72. {
  73. check();
  74. Ray out;
  75. out.m_origin = trf.transform(m_origin);
  76. out.m_dir = Vec4(trf.getRotation() * m_dir, 0.0f);
  77. return out;
  78. }
  79. private:
  80. Vec4 m_origin
  81. #if ANKI_ENABLE_ASSERTIONS
  82. = Vec4(MAX_F32)
  83. #endif
  84. ;
  85. Vec4 m_dir
  86. #if ANKI_ENABLE_ASSERTIONS
  87. = Vec4(MAX_F32)
  88. #endif
  89. ;
  90. void check() const
  91. {
  92. ANKI_ASSERT(m_origin.w() == 0.0f && m_dir.w() == 0.0f
  93. && isZero(m_dir.getLengthSquared() - 1.0f, EPSILON * 100.0f));
  94. }
  95. };
  96. /// @}
  97. } // end namespace anki