OGRE  1.9.0
OgreSphere.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE 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
36namespace 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
#define _OgreExport
Definition: OgrePlatform.h:260
A 3D box aligned with the x/y/z axes.
Defines a plane in 3D space.
Definition: OgrePlane.h:62
A sphere primitive, mostly used for bounds checking.
Definition: OgreSphere.h:52
bool intersects(const Sphere &s) const
Returns whether or not this sphere intersects another sphere.
Definition: OgreSphere.h:79
bool intersects(const Vector3 &v) const
Returns whether or not this sphere intersects a point.
Definition: OgreSphere.h:95
Vector3 mCenter
Definition: OgreSphere.h:55
Real getRadius(void) const
Returns the radius of the sphere.
Definition: OgreSphere.h:67
bool intersects(const Plane &plane) const
Returns whether or not this sphere intersects a plane.
Definition: OgreSphere.h:90
bool intersects(const AxisAlignedBox &box) const
Returns whether or not this sphere intersects a box.
Definition: OgreSphere.h:85
Real mRadius
Definition: OgreSphere.h:54
Sphere()
Standard constructor - creates a unit sphere around the origin.
Definition: OgreSphere.h:58
void merge(const Sphere &oth)
Merges another Sphere into the current sphere.
Definition: OgreSphere.h:100
void setRadius(Real radius)
Sets the radius of the sphere.
Definition: OgreSphere.h:70
Sphere(const Vector3 &center, Real radius)
Constructor allowing arbitrary spheres.
Definition: OgreSphere.h:63
void setCenter(const Vector3 &center)
Sets the center point of the sphere.
Definition: OgreSphere.h:76
const Vector3 & getCenter(void) const
Returns the center point of the sphere.
Definition: OgreSphere.h:73
Standard 3-dimensional vector.
Definition: OgreVector3.h:52
Real squaredLength() const
Returns the square of the length(magnitude) of the vector.
Definition: OgreVector3.h:371
float Real
Software floating point type.