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 | |
---|
39 | /** \addtogroup Core |
---|
40 | * @{ |
---|
41 | */ |
---|
42 | /** \addtogroup Math |
---|
43 | * @{ |
---|
44 | */ |
---|
45 | /** A sphere primitive, mostly used for bounds checking. |
---|
46 | @remarks |
---|
47 | A sphere in math texts is normally represented by the function |
---|
48 | x^2 + y^2 + z^2 = r^2 (for sphere's centered on the origin). Ogre stores spheres |
---|
49 | simply as a center point and a radius. |
---|
50 | */ |
---|
51 | class _OgreExport Sphere |
---|
52 | { |
---|
53 | protected: |
---|
54 | Real mRadius; |
---|
55 | Vector3 mCenter; |
---|
56 | public: |
---|
57 | /** Standard constructor - creates a unit sphere around the origin.*/ |
---|
58 | Sphere() : mRadius(1.0), mCenter(Vector3::ZERO) {} |
---|
59 | /** Constructor allowing arbitrary spheres. |
---|
60 | @param center The center point of the sphere. |
---|
61 | @param radius The radius of the sphere. |
---|
62 | */ |
---|
63 | Sphere(const Vector3& center, Real radius) |
---|
64 | : mRadius(radius), mCenter(center) {} |
---|
65 | |
---|
66 | /** Returns the radius of the sphere. */ |
---|
67 | Real getRadius(void) const { return mRadius; } |
---|
68 | |
---|
69 | /** Sets the radius of the sphere. */ |
---|
70 | void setRadius(Real radius) { mRadius = radius; } |
---|
71 | |
---|
72 | /** Returns the center point of the sphere. */ |
---|
73 | const Vector3& getCenter(void) const { return mCenter; } |
---|
74 | |
---|
75 | /** Sets the center point of the sphere. */ |
---|
76 | void setCenter(const Vector3& center) { mCenter = center; } |
---|
77 | |
---|
78 | /** Returns whether or not this sphere intersects another sphere. */ |
---|
79 | bool intersects(const Sphere& s) const |
---|
80 | { |
---|
81 | return (s.mCenter - mCenter).squaredLength() <= |
---|
82 | Math::Sqr(s.mRadius + mRadius); |
---|
83 | } |
---|
84 | /** Returns whether or not this sphere intersects a box. */ |
---|
85 | bool intersects(const AxisAlignedBox& box) const |
---|
86 | { |
---|
87 | return Math::intersects(*this, box); |
---|
88 | } |
---|
89 | /** Returns whether or not this sphere intersects a plane. */ |
---|
90 | bool intersects(const Plane& plane) const |
---|
91 | { |
---|
92 | return Math::intersects(*this, plane); |
---|
93 | } |
---|
94 | /** Returns whether or not this sphere intersects a point. */ |
---|
95 | bool intersects(const Vector3& v) const |
---|
96 | { |
---|
97 | return ((v - mCenter).squaredLength() <= Math::Sqr(mRadius)); |
---|
98 | } |
---|
99 | /** Merges another Sphere into the current sphere */ |
---|
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 | }; |
---|
128 | /** @} */ |
---|
129 | /** @} */ |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | #endif |
---|
134 | |
---|