Plane.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /// Plane collision shape
  11. class Plane
  12. {
  13. public:
  14. static constexpr CollisionShapeType kClassType = CollisionShapeType::kPlane;
  15. /// Will not initialize any memory, nothing.
  16. Plane()
  17. {
  18. }
  19. /// Copy constructor
  20. Plane(const Plane& b)
  21. {
  22. operator=(b);
  23. }
  24. /// Constructor
  25. Plane(const Vec4& normal, F32 offset)
  26. : m_normal(normal)
  27. , m_offset(offset)
  28. {
  29. check();
  30. }
  31. /// @see setFrom3Points
  32. Plane(const Vec4& p0, const Vec4& p1, const Vec4& p2)
  33. {
  34. setFrom3Points(p0, p1, p2);
  35. }
  36. /// @see setFromPlaneEquation
  37. Plane(F32 a, F32 b, F32 c, F32 d)
  38. {
  39. setFromPlaneEquation(a, b, c, d);
  40. }
  41. Plane& operator=(const Plane& b)
  42. {
  43. b.check();
  44. m_normal = b.m_normal;
  45. m_offset = b.m_offset;
  46. return *this;
  47. }
  48. const Vec4& getNormal() const
  49. {
  50. check();
  51. return m_normal;
  52. }
  53. void setNormal(const Vec4& x)
  54. {
  55. m_normal = x;
  56. }
  57. F32 getOffset() const
  58. {
  59. check();
  60. return m_offset;
  61. }
  62. void setOffset(const F32 x)
  63. {
  64. m_offset = x;
  65. }
  66. /// Set the plane from 3 points
  67. void setFrom3Points(const Vec4& p0, const Vec4& p1, const Vec4& p2);
  68. /// Set from plane equation is ax+by+cz+d
  69. void setFromPlaneEquation(F32 a, F32 b, F32 c, F32 d);
  70. // Set the plane from a point in plane and a direction
  71. void setFromRay(Vec3 rayOrigin, Vec3 rayDir)
  72. {
  73. m_normal = rayDir.xyz0;
  74. m_offset = rayDir.dot(rayOrigin);
  75. }
  76. /// Return the transformed
  77. Plane getTransformed(const Transform& trf) const;
  78. private:
  79. Vec4 m_normal
  80. #if ANKI_ASSERTIONS_ENABLED
  81. = Vec4(kMaxF32)
  82. #endif
  83. ;
  84. F32 m_offset
  85. #if ANKI_ASSERTIONS_ENABLED
  86. = kMaxF32
  87. #endif
  88. ;
  89. void check() const
  90. {
  91. ANKI_ASSERT(m_normal.w == 0.0f);
  92. ANKI_ASSERT(m_offset != kMaxF32);
  93. }
  94. };
  95. /// @}
  96. } // end namespace anki