BsSphere.h 2.5 KB

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