BsPlane.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #include "Math/BsVector3.h"
  6. namespace bs
  7. {
  8. /** @addtogroup Math
  9. * @{
  10. */
  11. /** A plane represented by a normal and a distance. */
  12. class BS_UTILITY_EXPORT Plane
  13. {
  14. public:
  15. /**
  16. * The "positive side" of the plane is the half space to which the plane normal points. The "negative side" is the
  17. * other half space. The flag "no side" indicates the plane itself.
  18. */
  19. enum Side
  20. {
  21. NO_SIDE,
  22. POSITIVE_SIDE,
  23. NEGATIVE_SIDE,
  24. BOTH_SIDE
  25. };
  26. public:
  27. Plane();
  28. Plane(const Plane& copy);
  29. Plane(const Vector3& normal, float d);
  30. Plane(float a, float b, float c, float d);
  31. Plane(const Vector3& normal, const Vector3& point);
  32. Plane(const Vector3& point0, const Vector3& point1, const Vector3& point2);
  33. /**
  34. * Returns the side of the plane where the point is located on.
  35. *
  36. * @note NO_SIDE signifies the point is on the plane.
  37. */
  38. Side getSide(const Vector3& point) const;
  39. /**
  40. * Returns the side where the alignedBox is. The flag BOTH_SIDE indicates an intersecting box.
  41. * One corner ON the plane is sufficient to consider the box and the plane intersecting.
  42. */
  43. Side getSide(const AABox& box) const;
  44. /** Returns the side where the sphere is. The flag BOTH_SIDE indicates an intersecting sphere. */
  45. Side getSide(const Sphere& sphere) const;
  46. /**
  47. * 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. /** Project a vector onto the plane. */
  55. Vector3 projectVector(const Vector3& v) const;
  56. /** Normalizes the plane's normal and the length scale of d. */
  57. float normalize();
  58. /** Box/plane intersection. */
  59. bool intersects(const AABox& box) const;
  60. /** Sphere/plane intersection. */
  61. bool intersects(const Sphere& sphere) const;
  62. /** Ray/plane intersection, returns boolean result and distance to intersection point. */
  63. std::pair<bool, float> intersects(const Ray& ray) const;
  64. bool operator==(const Plane& rhs) const
  65. {
  66. return (rhs.d == d && rhs.normal == normal);
  67. }
  68. bool operator!=(const Plane& rhs) const
  69. {
  70. return (rhs.d != d || rhs.normal != normal);
  71. }
  72. public:
  73. Vector3 normal;
  74. float d;
  75. };
  76. typedef Vector<Plane> PlaneList;
  77. /** @} */
  78. }