/src/ogre/OgreMain/include/OgreSphere.h
Line | Count | Source |
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-2014 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 "OgreVector.h" |
35 | | #include "OgrePlane.h" |
36 | | |
37 | | namespace Ogre { |
38 | | |
39 | | |
40 | | /** \addtogroup Core |
41 | | * @{ |
42 | | */ |
43 | | /** \addtogroup Math |
44 | | * @{ |
45 | | */ |
46 | | /** A sphere primitive, mostly used for bounds checking. |
47 | | |
48 | | A sphere in math texts is normally represented by the function |
49 | | x^2 + y^2 + z^2 = r^2 (for sphere's centered on the origin). Ogre stores spheres |
50 | | simply as a center point and a radius. |
51 | | */ |
52 | | class _OgreExport Sphere |
53 | | { |
54 | | private: |
55 | | Real mRadius; |
56 | | Vector3 mCenter; |
57 | | public: |
58 | | /** Standard constructor - creates a unit sphere around the origin.*/ |
59 | 0 | Sphere() : mRadius(1.0), mCenter(Vector3::ZERO) {} |
60 | | /** Constructor allowing arbitrary spheres. |
61 | | @param center The center point of the sphere. |
62 | | @param radius The radius of the sphere. |
63 | | */ |
64 | | Sphere(const Vector3& center, Real radius) |
65 | 0 | : mRadius(radius), mCenter(center) {} |
66 | | |
67 | | /** Returns the radius of the sphere. */ |
68 | 0 | Real getRadius(void) const { return mRadius; } |
69 | | |
70 | | /** Sets the radius of the sphere. */ |
71 | 0 | void setRadius(Real radius) { mRadius = radius; } |
72 | | |
73 | | /** Returns the center point of the sphere. */ |
74 | 0 | const Vector3& getCenter(void) const { return mCenter; } |
75 | | |
76 | | /** Sets the center point of the sphere. */ |
77 | 0 | void setCenter(const Vector3& center) { mCenter = center; } |
78 | | |
79 | | /** Returns whether or not this sphere intersects another sphere. */ |
80 | | bool intersects(const Sphere& s) const |
81 | 0 | { |
82 | 0 | return (s.mCenter - mCenter).squaredLength() <= |
83 | 0 | Math::Sqr(s.mRadius + mRadius); |
84 | 0 | } |
85 | | /** Returns whether or not this sphere intersects a box. */ |
86 | | bool intersects(const AxisAlignedBox& box) const |
87 | 0 | { |
88 | 0 | return Math::intersects(*this, box); |
89 | 0 | } |
90 | | /** Returns whether or not this sphere intersects a plane. */ |
91 | | bool intersects(const Plane& plane) const |
92 | 0 | { |
93 | 0 | return Math::Abs(plane.getDistance(getCenter())) <= getRadius(); |
94 | 0 | } |
95 | | /** Returns whether or not this sphere intersects a point. */ |
96 | | bool intersects(const Vector3& v) const |
97 | 0 | { |
98 | 0 | return ((v - mCenter).squaredLength() <= Math::Sqr(mRadius)); |
99 | 0 | } |
100 | | /** Merges another Sphere into the current sphere */ |
101 | | void merge(const Sphere& oth) |
102 | 0 | { |
103 | 0 | Vector3 diff = oth.getCenter() - mCenter; |
104 | 0 | Real lengthSq = diff.squaredLength(); |
105 | 0 | Real radiusDiff = oth.getRadius() - mRadius; |
106 | 0 | |
107 | 0 | // Early-out |
108 | 0 | if (Math::Sqr(radiusDiff) >= lengthSq) |
109 | 0 | { |
110 | 0 | // One fully contains the other |
111 | 0 | if (radiusDiff <= 0.0f) |
112 | 0 | return; // no change |
113 | 0 | else |
114 | 0 | { |
115 | 0 | mCenter = oth.getCenter(); |
116 | 0 | mRadius = oth.getRadius(); |
117 | 0 | return; |
118 | 0 | } |
119 | 0 | } |
120 | 0 |
|
121 | 0 | Real length = Math::Sqrt(lengthSq); |
122 | 0 | Real t = (length + radiusDiff) / (2.0f * length); |
123 | 0 | mCenter = mCenter + diff * t; |
124 | 0 | mRadius = 0.5f * (length + mRadius + oth.getRadius()); |
125 | 0 | } |
126 | | }; |
127 | | |
128 | | inline bool Math::intersects(const Sphere& sphere, const Plane& plane) |
129 | 0 | { |
130 | 0 | return sphere.intersects(plane); |
131 | 0 | } |
132 | | /** @} */ |
133 | | /** @} */ |
134 | | |
135 | | } |
136 | | |
137 | | #endif |
138 | | |