CmSphere.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #pragma once
  25. #include "CmPrerequisitesUtil.h"
  26. #include "CmVector3.h"
  27. namespace CamelotFramework
  28. {
  29. class CM_UTILITY_EXPORT Sphere
  30. {
  31. public:
  32. /**
  33. * @brief Default constructor. Creates a unit sphere around the origin.
  34. */
  35. Sphere() : mRadius(1.0), mCenter(Vector3::ZERO)
  36. { }
  37. Sphere(const Vector3& center, float radius)
  38. :mRadius(radius), mCenter(center)
  39. { }
  40. /**
  41. * @brief Returns the radius of the sphere.
  42. */
  43. float getRadius(void) const { return mRadius; }
  44. /**
  45. * @brief Sets the radius of the sphere.
  46. */
  47. void setRadius(float radius) { mRadius = radius; }
  48. /**
  49. * @brief Returns the center point of the sphere.
  50. */
  51. const Vector3& getCenter(void) const { return mCenter; }
  52. /**
  53. * @brief Sets the center point of the sphere.
  54. */
  55. void setCenter(const Vector3& center) { mCenter = center; }
  56. /**
  57. * @brief Returns whether or not this sphere intersects another sphere.
  58. */
  59. bool intersects(const Sphere& s) const
  60. {
  61. return (s.mCenter - mCenter).squaredLength() <=
  62. Math::sqr(s.mRadius + mRadius);
  63. }
  64. /**
  65. * @brief Returns whether or not this sphere intersects a box.
  66. */
  67. bool intersects(const AABox& box) const;
  68. /**
  69. * @brief Returns whether or not this sphere intersects a plane.
  70. */
  71. bool intersects(const Plane& plane) const;
  72. /**
  73. * @brief Returns whether or not this sphere intersects a point.
  74. */
  75. bool intersects(const Vector3& v) const
  76. {
  77. return ((v - mCenter).squaredLength() <= Math::sqr(mRadius));
  78. }
  79. /**
  80. * @brief Ray / sphere intersection, returns boolean result and distance.
  81. */
  82. std::pair<bool, float> intersects(const Ray& ray, bool discardInside = true) const;
  83. private:
  84. float mRadius;
  85. Vector3 mCenter;
  86. };
  87. }