[1812] | 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 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | #ifndef __Sphere_H_
|
---|
| 26 | #define __Sphere_H_
|
---|
| 27 |
|
---|
| 28 | // Precompiler options
|
---|
| 29 | #include "OgrePrerequisites.h"
|
---|
| 30 |
|
---|
| 31 | #include "OgreVector3.h"
|
---|
| 32 |
|
---|
| 33 | namespace Ogre {
|
---|
| 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). Ogre stores spheres
|
---|
| 40 | simply as a center point and a radius.
|
---|
| 41 | */
|
---|
| 42 | class _OgreExport Sphere
|
---|
| 43 | {
|
---|
| 44 | protected:
|
---|
| 45 | Real 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, Real radius)
|
---|
| 55 | : mRadius(radius), mCenter(center) {}
|
---|
| 56 |
|
---|
| 57 | /** Returns the radius of the sphere. */
|
---|
| 58 | Real getRadius(void) const { return mRadius; }
|
---|
| 59 |
|
---|
| 60 | /** Sets the radius of the sphere. */
|
---|
| 61 | void setRadius(Real radius) { mRadius = radius; }
|
---|
| 62 |
|
---|
| 63 | /** Returns the center point of the sphere. */
|
---|
| 64 | const Vector3& getCenter(void) const { return mCenter; }
|
---|
| 65 |
|
---|
| 66 | /** Sets the center point of the sphere. */
|
---|
| 67 | void setCenter(const Vector3& center) { mCenter = center; }
|
---|
| 68 |
|
---|
| 69 | /** Returns whether or not this sphere interects another sphere. */
|
---|
| 70 | bool intersects(const Sphere& s) const
|
---|
| 71 | {
|
---|
| 72 | return (s.mCenter - mCenter).squaredLength() <=
|
---|
| 73 | Math::Sqr(s.mRadius + mRadius);
|
---|
| 74 | }
|
---|
| 75 | /** Returns whether or not this sphere interects a box. */
|
---|
| 76 | bool intersects(const AxisAlignedBox& box) const
|
---|
| 77 | {
|
---|
| 78 | return Math::intersects(*this, box);
|
---|
| 79 | }
|
---|
| 80 | /** Returns whether or not this sphere interects a plane. */
|
---|
| 81 | bool intersects(const Plane& plane) const
|
---|
| 82 | {
|
---|
| 83 | return Math::intersects(*this, plane);
|
---|
| 84 | }
|
---|
| 85 | /** Returns whether or not this sphere interects a point. */
|
---|
| 86 | bool intersects(const Vector3& v) const
|
---|
| 87 | {
|
---|
| 88 | return ((v - mCenter).squaredLength() <= Math::Sqr(mRadius));
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | #endif
|
---|
| 97 |
|
---|