BsPlane.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsVector3.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief A plane represented by a normal and a distance.
  8. */
  9. class BS_UTILITY_EXPORT Plane
  10. {
  11. public:
  12. /**
  13. * @brief The "positive side" of the plane is the half space to which the
  14. * plane normal points. The "negative side" is the other half
  15. * space. The flag "no side" indicates the plane itself.
  16. */
  17. enum Side
  18. {
  19. NO_SIDE,
  20. POSITIVE_SIDE,
  21. NEGATIVE_SIDE,
  22. BOTH_SIDE
  23. };
  24. public:
  25. Plane();
  26. Plane(const Plane& copy);
  27. Plane(const Vector3& normal, float d);
  28. Plane(float a, float b, float c, float d);
  29. Plane(const Vector3& normal, const Vector3& point);
  30. Plane(const Vector3& point0, const Vector3& point1, const Vector3& point2);
  31. /**
  32. * @brief Returns the side of the plane where the point is located on.
  33. *
  34. * @note NO_SIDE signifies the point is on the plane.
  35. */
  36. Side getSide(const Vector3& point) const;
  37. /**
  38. * @brief Returns the side where the alignedBox is. The flag BOTH_SIDE indicates an intersecting box.
  39. * One corner ON the plane is sufficient to consider the box and the plane intersecting.
  40. */
  41. Side getSide(const AABox& box) const;
  42. /**
  43. * @brief Returns the side where the sphere is. The flag BOTH_SIDE indicates an intersecting sphere.
  44. */
  45. Side getSide(const Sphere& sphere) const;
  46. /**
  47. * @brief Returns a distance from point to plane.
  48. *
  49. * @note The sign of the return value is positive if the point is on the
  50. * positive side of the plane, negative if the point is on the negative
  51. * side, and zero if the point is on the plane.
  52. */
  53. float getDistance(const Vector3& point) const;
  54. /**
  55. * @brief Project a vector onto the plane.
  56. */
  57. Vector3 projectVector(const Vector3& v) const;
  58. /**
  59. * @brief Normalizes the plane's normal and the length scale of d
  60. * is as well.
  61. */
  62. float normalize();
  63. /**
  64. * @brief Box/plane intersection.
  65. */
  66. bool intersects(const AABox& box) const;
  67. /**
  68. * @brief Sphere/plane intersection.
  69. */
  70. bool intersects(const Sphere& sphere) const;
  71. /**
  72. * @brief Ray/plane intersection, returns boolean result and distance to intersection point.
  73. */
  74. std::pair<bool, float> intersects(const Ray& ray) const;
  75. bool operator==(const Plane& rhs) const
  76. {
  77. return (rhs.d == d && rhs.normal == normal);
  78. }
  79. bool operator!=(const Plane& rhs) const
  80. {
  81. return (rhs.d != d || rhs.normal != normal);
  82. }
  83. public:
  84. Vector3 normal;
  85. float d;
  86. };
  87. typedef Vector<Plane> PlaneList;
  88. }