OgreSphere.h
Go to the documentation of this file.
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2013 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#ifndef __Sphere_H_
#define __Sphere_H_
// Precompiler options
#include "OgrePrerequisites.h"
#include "OgreVector3.h"
namespace Ogre {
class _OgreExport Sphere
{
protected:
Real mRadius;
Vector3 mCenter;
public:
Sphere() : mRadius(1.0), mCenter(Vector3::ZERO) {}
Sphere(const Vector3& center, Real radius)
: mRadius(radius), mCenter(center) {}
Real getRadius(void) const { return mRadius; }
void setRadius(Real radius) { mRadius = radius; }
const Vector3& getCenter(void) const { return mCenter; }
void setCenter(const Vector3& center) { mCenter = center; }
bool intersects(const Sphere& s) const
{
return (s.mCenter - mCenter).squaredLength() <=
Math::Sqr(s.mRadius + mRadius);
}
bool intersects(const AxisAlignedBox& box) const
{
return Math::intersects(*this, box);
}
bool intersects(const Plane& plane) const
{
return Math::intersects(*this, plane);
}
bool intersects(const Vector3& v) const
{
return ((v - mCenter).squaredLength() <= Math::Sqr(mRadius));
}
void merge(const Sphere& oth)
{
Vector3 diff = oth.getCenter() - mCenter;
Real lengthSq = diff.squaredLength();
Real radiusDiff = oth.getRadius() - mRadius;
// Early-out
if (Math::Sqr(radiusDiff) >= lengthSq)
{
// One fully contains the other
if (radiusDiff <= 0.0f)
return; // no change
else
{
mCenter = oth.getCenter();
mRadius = oth.getRadius();
return;
}
}
Real length = Math::Sqrt(lengthSq);
Real t = (length + radiusDiff) / (2.0f * length);
mCenter = mCenter + diff * t;
mRadius = 0.5f * (length + mRadius + oth.getRadius());
}
};
}
#endif
Ogre::Vector3::squaredLength
Real squaredLength() const
Returns the square of the length(magnitude) of the vector.
Definition: OgreVector3.h:371
Ogre::Math::Sqr
static Real Sqr(Real fValue)
Squared function.
Definition: OgreMath.h:399
Ogre::Sphere::getCenter
const Vector3 & getCenter(void) const
Returns the center point of the sphere.
Definition: OgreSphere.h:73
OgreVector3.h
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
Ogre::Sphere
A sphere primitive, mostly used for bounds checking.
Definition: OgreSphere.h:51
Ogre::Sphere::merge
void merge(const Sphere &oth)
Merges another Sphere into the current sphere.
Definition: OgreSphere.h:100
Ogre::Real
float Real
Software floating point type.
Definition: OgrePrerequisites.h:70
Ogre::Sphere::intersects
bool intersects(const AxisAlignedBox &box) const
Returns whether or not this sphere intersects a box.
Definition: OgreSphere.h:85
Ogre::Sphere::mRadius
Real mRadius
Definition: OgreSphere.h:54
Ogre::Sphere::Sphere
Sphere(const Vector3 &center, Real radius)
Constructor allowing arbitrary spheres.
Definition: OgreSphere.h:63
Ogre::Sphere::intersects
bool intersects(const Sphere &s) const
Returns whether or not this sphere intersects another sphere.
Definition: OgreSphere.h:79
Ogre::Sphere::intersects
bool intersects(const Vector3 &v) const
Returns whether or not this sphere intersects a point.
Definition: OgreSphere.h:95
Ogre::Plane
Defines a plane in 3D space.
Definition: OgrePlane.h:61
Ogre::Vector3
Standard 3-dimensional vector.
Definition: OgreVector3.h:51
OgrePrerequisites.h
Ogre::Sphere::setRadius
void setRadius(Real radius)
Sets the radius of the sphere.
Definition: OgreSphere.h:70
Ogre::Math::Sqrt
static Real Sqrt(Real fValue)
Square root function.
Definition: OgreMath.h:405
Ogre::Sphere::setCenter
void setCenter(const Vector3 &center)
Sets the center point of the sphere.
Definition: OgreSphere.h:76
Ogre::Sphere::intersects
bool intersects(const Plane &plane) const
Returns whether or not this sphere intersects a plane.
Definition: OgreSphere.h:90
Ogre::Sphere::Sphere
Sphere()
Standard constructor - creates a unit sphere around the origin.
Definition: OgreSphere.h:58
Ogre
Definition: OgreAndroidLogListener.h:34
Ogre::Math::intersects
static std::pair< bool, Real > intersects(const Ray &ray, const Plane &plane)
Ray / plane intersection, returns boolean result and distance.
Ogre::Sphere::getRadius
Real getRadius(void) const
Returns the radius of the sphere.
Definition: OgreSphere.h:67
Ogre::Sphere::mCenter
Vector3 mCenter
Definition: OgreSphere.h:55
Ogre::AxisAlignedBox
A 3D box aligned with the x/y/z axes.
Definition: OgreAxisAlignedBox.h:54

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.