CmSphere.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include "CmVector3.h"
  4. namespace CamelotFramework
  5. {
  6. /**
  7. * @brief A sphere represented by a center point and a radius.
  8. */
  9. class CM_UTILITY_EXPORT Sphere
  10. {
  11. public:
  12. /**
  13. * @brief Default constructor. Creates a unit sphere around the origin.
  14. */
  15. Sphere() : mRadius(1.0), mCenter(Vector3::ZERO)
  16. { }
  17. Sphere(const Vector3& center, float radius)
  18. :mRadius(radius), mCenter(center)
  19. { }
  20. /**
  21. * @brief Returns the radius of the sphere.
  22. */
  23. float getRadius(void) const { return mRadius; }
  24. /**
  25. * @brief Sets the radius of the sphere.
  26. */
  27. void setRadius(float radius) { mRadius = radius; }
  28. /**
  29. * @brief Returns the center point of the sphere.
  30. */
  31. const Vector3& getCenter(void) const { return mCenter; }
  32. /**
  33. * @brief Sets the center point of the sphere.
  34. */
  35. void setCenter(const Vector3& center) { mCenter = center; }
  36. /**
  37. * @brief Returns whether or not this sphere contains the provided point.
  38. */
  39. bool contains(const Vector3& v) const
  40. {
  41. return ((v - mCenter).squaredLength() <= Math::sqr(mRadius));
  42. }
  43. /**
  44. * @brief Returns whether or not this sphere intersects another sphere.
  45. */
  46. bool intersects(const Sphere& s) const
  47. {
  48. return (s.mCenter - mCenter).squaredLength() <=
  49. Math::sqr(s.mRadius + mRadius);
  50. }
  51. /**
  52. * @brief Returns whether or not this sphere intersects a box.
  53. */
  54. bool intersects(const AABox& box) const;
  55. /**
  56. * @brief Returns whether or not this sphere intersects a plane.
  57. */
  58. bool intersects(const Plane& plane) const;
  59. /**
  60. * @brief Ray/sphere intersection, returns boolean result and distance to nearest intersection.
  61. *
  62. * @param discardInside (optional) If true the intersection will be discarded if ray origin
  63. * is located within the sphere.
  64. */
  65. std::pair<bool, float> intersects(const Ray& ray, bool discardInside = true) const;
  66. private:
  67. float mRadius;
  68. Vector3 mCenter;
  69. };
  70. }