BsSphere.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisitesUtil.h"
  5. #include "BsVector3.h"
  6. namespace BansheeEngine
  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(void) 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(void) 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] discardInside (optional) If true the intersection will be discarded if ray origin
  47. * is located within the sphere.
  48. */
  49. std::pair<bool, float> intersects(const Ray& ray, bool discardInside = true) const;
  50. private:
  51. float mRadius;
  52. Vector3 mCenter;
  53. };
  54. /** @} */
  55. /** @cond SPECIALIZATIONS */
  56. BS_ALLOW_MEMCPY_SERIALIZATION(Sphere);
  57. /** @endcond */
  58. }