BsSphere.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsVector3.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Math
  7. * @{
  8. */
  9. /** A sphere represented by a center point and a radius. */
  10. class BS_UTILITY_EXPORT Sphere
  11. {
  12. public:
  13. /** Default constructor. Creates a unit sphere around the origin. */
  14. Sphere() : mRadius(1.0), mCenter(Vector3::ZERO)
  15. { }
  16. Sphere(const Vector3& center, float radius)
  17. :mRadius(radius), mCenter(center)
  18. { }
  19. /** Returns the radius of the sphere. */
  20. float getRadius(void) const { return mRadius; }
  21. /** Sets the radius of the sphere. */
  22. void setRadius(float radius) { mRadius = radius; }
  23. /** Returns the center point of the sphere. */
  24. const Vector3& getCenter(void) const { return mCenter; }
  25. /** Sets the center point of the sphere. */
  26. void setCenter(const Vector3& center) { mCenter = center; }
  27. /** Merges the two spheres, creating a new sphere that encapsulates them both. */
  28. void merge(const Sphere& rhs);
  29. /** Expands the sphere so it includes the provided point. */
  30. void merge(const Vector3& point);
  31. /** Transforms the sphere by the given matrix. */
  32. void transform(const Matrix4& matrix);
  33. /** Returns whether or not this sphere contains the provided point. */
  34. bool contains(const Vector3& v) const
  35. {
  36. return ((v - mCenter).squaredLength() <= Math::sqr(mRadius));
  37. }
  38. /** Returns whether or not this sphere intersects another sphere. */
  39. bool intersects(const Sphere& s) const
  40. {
  41. return (s.mCenter - mCenter).squaredLength() <=
  42. Math::sqr(s.mRadius + mRadius);
  43. }
  44. /** Returns whether or not this sphere intersects a box. */
  45. bool intersects(const AABox& box) const;
  46. /** Returns whether or not this sphere intersects a plane. */
  47. bool intersects(const Plane& plane) const;
  48. /**
  49. * Ray/sphere intersection, returns boolean result and distance to nearest intersection.
  50. *
  51. * @param[in] discardInside (optional) If true the intersection will be discarded if ray origin
  52. * is located within the sphere.
  53. */
  54. std::pair<bool, float> intersects(const Ray& ray, bool discardInside = true) const;
  55. private:
  56. float mRadius;
  57. Vector3 mCenter;
  58. };
  59. /** @} */
  60. /** @cond SPECIALIZATIONS */
  61. BS_ALLOW_MEMCPY_SERIALIZATION(Sphere);
  62. /** @endcond */
  63. }