mSphere.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MSPHERE_H_
  23. #define _MSPHERE_H_
  24. //Includes
  25. #ifndef _MPOINT_H_
  26. #include "math/mPoint.h"
  27. #endif
  28. class SphereF
  29. {
  30. public:
  31. Point3F center;
  32. F32 radius;
  33. public:
  34. SphereF() { }
  35. SphereF(const Point3F& in_rPosition,
  36. const F32 in_rRadius)
  37. : center(in_rPosition),
  38. radius(in_rRadius)
  39. {
  40. if (radius < 0.0f)
  41. radius = 0.0f;
  42. }
  43. bool isContained(const Point3F& in_rContain) const;
  44. bool isContained(const SphereF& in_rContain) const;
  45. bool isIntersecting(const SphereF& in_rIntersect) const;
  46. };
  47. //-------------------------------------- INLINES
  48. //
  49. inline bool
  50. SphereF::isContained(const Point3F& in_rContain) const
  51. {
  52. F32 distSq = (center - in_rContain).lenSquared();
  53. return (distSq <= (radius * radius));
  54. }
  55. inline bool
  56. SphereF::isContained(const SphereF& in_rContain) const
  57. {
  58. if (radius < in_rContain.radius)
  59. return false;
  60. // Since our radius is guaranteed to be >= other's, we
  61. // can dodge the sqrt() here.
  62. //
  63. F32 dist = (in_rContain.center - center).lenSquared();
  64. return (dist <= ((radius - in_rContain.radius) *
  65. (radius - in_rContain.radius)));
  66. }
  67. inline bool
  68. SphereF::isIntersecting(const SphereF& in_rIntersect) const
  69. {
  70. F32 distSq = (in_rIntersect.center - center).lenSquared();
  71. return (distSq <= ((in_rIntersect.radius + radius) *
  72. (in_rIntersect.radius + radius)));
  73. }
  74. #endif //_SPHERE_H_