LineSegment.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include <AnKi/Math.h>
  8. namespace anki
  9. {
  10. /// @addtogroup collision
  11. /// @{
  12. /// Line segment. Line from a point to a point. P0 = origin and P1 = direction + origin
  13. class LineSegment
  14. {
  15. public:
  16. static constexpr CollisionShapeType CLASS_TYPE = CollisionShapeType::LINE_SEGMENT;
  17. /// Will not initialize any memory, nothing.
  18. LineSegment()
  19. {
  20. }
  21. LineSegment(const Vec4& origin, const Vec4& direction)
  22. : m_origin(origin)
  23. , m_dir(direction)
  24. {
  25. check();
  26. }
  27. LineSegment(const LineSegment& b)
  28. {
  29. operator=(b);
  30. }
  31. LineSegment& operator=(const LineSegment& b)
  32. {
  33. b.check();
  34. m_origin = b.m_origin;
  35. m_dir = b.m_dir;
  36. return *this;
  37. }
  38. const Vec4& getOrigin() const
  39. {
  40. check();
  41. return m_origin;
  42. }
  43. void setOrigin(const Vec4& origin)
  44. {
  45. m_origin = origin;
  46. }
  47. const Vec4& getDirection() const
  48. {
  49. check();
  50. return m_dir;
  51. }
  52. void setDirection(const Vec4& dir)
  53. {
  54. m_dir = dir;
  55. }
  56. LineSegment getTransformed(const Transform& trf) const
  57. {
  58. check();
  59. LineSegment out;
  60. out.m_origin = trf.transform(m_origin);
  61. out.m_dir = Vec4(trf.getRotation() * (m_dir * trf.getScale()), 0.0f);
  62. return out;
  63. }
  64. private:
  65. Vec4 m_origin ///< P0
  66. #if ANKI_ENABLE_ASSERTIONS
  67. = Vec4(MAX_F32)
  68. #endif
  69. ;
  70. Vec4 m_dir ///< P1 = origin+dir so dir = P1-origin
  71. #if ANKI_ENABLE_ASSERTIONS
  72. = Vec4(MAX_F32)
  73. #endif
  74. ;
  75. void check() const
  76. {
  77. ANKI_ASSERT(m_origin.w() != 0.0f && m_dir.w() != 0.0f);
  78. }
  79. };
  80. /// @}
  81. } // end namespace anki