CmSphere.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifndef __Sphere_H_
  25. #define __Sphere_H_
  26. // Precompiler options
  27. #include "CmPrerequisitesUtil.h"
  28. #include "CmVector3.h"
  29. namespace CamelotFramework {
  30. /** \addtogroup Core
  31. * @{
  32. */
  33. /** \addtogroup Math
  34. * @{
  35. */
  36. /** A sphere primitive, mostly used for bounds checking.
  37. @remarks
  38. A sphere in math texts is normally represented by the function
  39. x^2 + y^2 + z^2 = r^2 (for sphere's centered on the origin). Engine stores spheres
  40. simply as a center point and a radius.
  41. */
  42. class CM_UTILITY_EXPORT Sphere
  43. {
  44. protected:
  45. float mRadius;
  46. Vector3 mCenter;
  47. public:
  48. /** Standard constructor - creates a unit sphere around the origin.*/
  49. Sphere() : mRadius(1.0), mCenter(Vector3::ZERO) {}
  50. /** Constructor allowing arbitrary spheres.
  51. @param center The center point of the sphere.
  52. @param radius The radius of the sphere.
  53. */
  54. Sphere(const Vector3& center, float radius)
  55. : mRadius(radius), mCenter(center) {}
  56. /** Returns the radius of the sphere. */
  57. float getRadius(void) const { return mRadius; }
  58. /** Sets the radius of the sphere. */
  59. void setRadius(float radius) { mRadius = radius; }
  60. /** Returns the center point of the sphere. */
  61. const Vector3& getCenter(void) const { return mCenter; }
  62. /** Sets the center point of the sphere. */
  63. void setCenter(const Vector3& center) { mCenter = center; }
  64. /** Returns whether or not this sphere intersects another sphere. */
  65. bool intersects(const Sphere& s) const
  66. {
  67. return (s.mCenter - mCenter).squaredLength() <=
  68. Math::Sqr(s.mRadius + mRadius);
  69. }
  70. /** Returns whether or not this sphere intersects a box. */
  71. bool intersects(const AABox& box) const
  72. {
  73. return Math::intersects(*this, box);
  74. }
  75. /** Returns whether or not this sphere intersects a plane. */
  76. bool intersects(const Plane& plane) const
  77. {
  78. return Math::intersects(*this, plane);
  79. }
  80. /** Returns whether or not this sphere intersects a point. */
  81. bool intersects(const Vector3& v) const
  82. {
  83. return ((v - mCenter).squaredLength() <= Math::Sqr(mRadius));
  84. }
  85. };
  86. /** @} */
  87. /** @} */
  88. }
  89. #endif