BsPlane.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsVector3.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Math
  7. * @{
  8. */
  9. /** A plane represented by a normal and a distance. */
  10. class BS_UTILITY_EXPORT Plane
  11. {
  12. public:
  13. /**
  14. * The "positive side" of the plane is the half space to which the plane normal points. The "negative side" is the
  15. * other half 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. * 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. * 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. /** Returns the side where the sphere is. The flag BOTH_SIDE indicates an intersecting sphere. */
  43. Side getSide(const Sphere& sphere) const;
  44. /**
  45. * Returns a distance from point to plane.
  46. *
  47. * @note The sign of the return value is positive if the point is on the
  48. * positive side of the plane, negative if the point is on the negative
  49. * side, and zero if the point is on the plane.
  50. */
  51. float getDistance(const Vector3& point) const;
  52. /** Project a vector onto the plane. */
  53. Vector3 projectVector(const Vector3& v) const;
  54. /** Normalizes the plane's normal and the length scale of d. */
  55. float normalize();
  56. /** Box/plane intersection. */
  57. bool intersects(const AABox& box) const;
  58. /** Sphere/plane intersection. */
  59. bool intersects(const Sphere& sphere) const;
  60. /** Ray/plane intersection, returns boolean result and distance to intersection point. */
  61. std::pair<bool, float> intersects(const Ray& ray) const;
  62. bool operator==(const Plane& rhs) const
  63. {
  64. return (rhs.d == d && rhs.normal == normal);
  65. }
  66. bool operator!=(const Plane& rhs) const
  67. {
  68. return (rhs.d != d || rhs.normal != normal);
  69. }
  70. public:
  71. Vector3 normal;
  72. float d;
  73. };
  74. typedef Vector<Plane> PlaneList;
  75. /** @} */
  76. }