OgreSphere.h
Go to the documentation of this file.
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-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __Sphere_H_
29 #define __Sphere_H_
30 
31 // Precompiler options
32 #include "OgrePrerequisites.h"
33 
34 #include "OgreVector3.h"
35 
36 namespace Ogre {
37 
38 
52  {
53  protected:
56  public:
58  Sphere() : mRadius(1.0), mCenter(Vector3::ZERO) {}
63  Sphere(const Vector3& center, Real radius)
64  : mRadius(radius), mCenter(center) {}
65 
67  Real getRadius(void) const { return mRadius; }
68 
70  void setRadius(Real radius) { mRadius = radius; }
71 
73  const Vector3& getCenter(void) const { return mCenter; }
74 
76  void setCenter(const Vector3& center) { mCenter = center; }
77 
79  bool intersects(const Sphere& s) const
80  {
81  return (s.mCenter - mCenter).squaredLength() <=
82  Math::Sqr(s.mRadius + mRadius);
83  }
85  bool intersects(const AxisAlignedBox& box) const
86  {
87  return Math::intersects(*this, box);
88  }
90  bool intersects(const Plane& plane) const
91  {
92  return Math::intersects(*this, plane);
93  }
95  bool intersects(const Vector3& v) const
96  {
97  return ((v - mCenter).squaredLength() <= Math::Sqr(mRadius));
98  }
100  void merge(const Sphere& oth)
101  {
102  Vector3 diff = oth.getCenter() - mCenter;
103  Real lengthSq = diff.squaredLength();
104  Real radiusDiff = oth.getRadius() - mRadius;
105 
106  // Early-out
107  if (Math::Sqr(radiusDiff) >= lengthSq)
108  {
109  // One fully contains the other
110  if (radiusDiff <= 0.0f)
111  return; // no change
112  else
113  {
114  mCenter = oth.getCenter();
115  mRadius = oth.getRadius();
116  return;
117  }
118  }
119 
120  Real length = Math::Sqrt(lengthSq);
121  Real t = (length + radiusDiff) / (2.0f * length);
122  mCenter = mCenter + diff * t;
123  mRadius = 0.5f * (length + mRadius + oth.getRadius());
124  }
125 
126 
127  };
131 }
132 
133 #endif
134 
Ogre::Sphere::intersects
bool intersects(const Plane &plane) const
Returns whether or not this sphere intersects a plane.
Definition: OgreSphere.h:90
Ogre
Definition: OgreAndroidLogListener.h:35
Ogre::Plane
Defines a plane in 3D space.
Definition: OgrePlane.h:62
Ogre::Sphere::getRadius
Real getRadius(void) const
Returns the radius of the sphere.
Definition: OgreSphere.h:67
Ogre::Sphere::setRadius
void setRadius(Real radius)
Sets the radius of the sphere.
Definition: OgreSphere.h:70
Ogre::Sphere::Sphere
Sphere(const Vector3 &center, Real radius)
Constructor allowing arbitrary spheres.
Definition: OgreSphere.h:63
Ogre::Sphere::setCenter
void setCenter(const Vector3 &center)
Sets the center point of the sphere.
Definition: OgreSphere.h:76
Ogre::Math::Sqrt
static Real Sqrt(Real fValue)
Square root function.
Definition: OgreMath.h:405
Ogre::Sphere::intersects
bool intersects(const Vector3 &v) const
Returns whether or not this sphere intersects a point.
Definition: OgreSphere.h:95
OgrePrerequisites.h
Ogre::Sphere::getCenter
const Vector3 & getCenter(void) const
Returns the center point of the sphere.
Definition: OgreSphere.h:73
Ogre::AxisAlignedBox
A 3D box aligned with the x/y/z axes.
Definition: OgreAxisAlignedBox.h:55
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
Ogre::Sphere
A sphere primitive, mostly used for bounds checking.
Definition: OgreSphere.h:52
Ogre::Sphere::mCenter
Vector3 mCenter
Definition: OgreSphere.h:55
Ogre::Vector3::squaredLength
Real squaredLength() const
Returns the square of the length(magnitude) of the vector.
Definition: OgreVector3.h:371
Ogre::Sphere::merge
void merge(const Sphere &oth)
Merges another Sphere into the current sphere.
Definition: OgreSphere.h:100
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::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::Real
float Real
Software floating point type.
Definition: OgrePrerequisites.h:70
Ogre::Sphere::Sphere
Sphere()
Standard constructor - creates a unit sphere around the origin.
Definition: OgreSphere.h:58
OgreVector3.h
Ogre::Vector3
Standard 3-dimensional vector.
Definition: OgreVector3.h:52
Ogre::Math::Sqr
static Real Sqr(Real fValue)
Squared function.
Definition: OgreMath.h:399
Ogre::Sphere::intersects
bool intersects(const Sphere &s) const
Returns whether or not this sphere intersects another sphere.
Definition: OgreSphere.h:79

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