/src/ogre/OgreMain/include/OgrePlaneBoundedVolume.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 __PlaneBoundedVolume_H_ |
29 | | #define __PlaneBoundedVolume_H_ |
30 | | |
31 | | // Precompiler options |
32 | | #include "OgrePrerequisites.h" |
33 | | #include "OgreAxisAlignedBox.h" |
34 | | #include "OgreSphere.h" |
35 | | #include "OgreMath.h" |
36 | | #include "OgrePlane.h" |
37 | | #include "OgreHeaderPrefix.h" |
38 | | |
39 | | namespace Ogre { |
40 | | |
41 | | /** \addtogroup Core |
42 | | * @{ |
43 | | */ |
44 | | /** \addtogroup Math |
45 | | * @{ |
46 | | */ |
47 | | /** Represents a convex volume bounded by planes. |
48 | | */ |
49 | | class _OgreExport PlaneBoundedVolume |
50 | | { |
51 | | public: |
52 | | typedef std::vector<Plane> PlaneList; |
53 | | /// Publicly accessible plane list, you can modify this direct |
54 | | PlaneList planes; |
55 | | Plane::Side outside; |
56 | | |
57 | 0 | PlaneBoundedVolume() :outside(Plane::NEGATIVE_SIDE) {} |
58 | | /** Constructor, determines which side is deemed to be 'outside' */ |
59 | | PlaneBoundedVolume(Plane::Side theOutside) |
60 | 0 | : outside(theOutside) {} |
61 | | |
62 | | /** Intersection test with AABB |
63 | | @remarks May return false positives but will never miss an intersection. |
64 | | */ |
65 | | inline bool intersects(const AxisAlignedBox& box) const |
66 | 0 | { |
67 | 0 | if (box.isNull()) return false; |
68 | 0 | if (box.isInfinite()) return true; |
69 | | |
70 | | // Get centre of the box |
71 | 0 | Vector3 centre = box.getCenter(); |
72 | | // Get the half-size of the box |
73 | 0 | Vector3 halfSize = box.getHalfSize(); |
74 | | |
75 | 0 | PlaneList::const_iterator i, iend; |
76 | 0 | iend = planes.end(); |
77 | 0 | for (i = planes.begin(); i != iend; ++i) |
78 | 0 | { |
79 | 0 | const Plane& plane = *i; |
80 | |
|
81 | 0 | Plane::Side side = plane.getSide(centre, halfSize); |
82 | 0 | if (side == outside) |
83 | 0 | { |
84 | | // Found a splitting plane therefore return not intersecting |
85 | 0 | return false; |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | // couldn't find a splitting plane, assume intersecting |
90 | 0 | return true; |
91 | |
|
92 | 0 | } |
93 | | /** Intersection test with Sphere |
94 | | @remarks May return false positives but will never miss an intersection. |
95 | | */ |
96 | | inline bool intersects(const Sphere& sphere) const |
97 | 0 | { |
98 | 0 | PlaneList::const_iterator i, iend; |
99 | 0 | iend = planes.end(); |
100 | 0 | for (i = planes.begin(); i != iend; ++i) |
101 | 0 | { |
102 | 0 | const Plane& plane = *i; |
103 | 0 |
|
104 | 0 | // Test which side of the plane the sphere is |
105 | 0 | Real d = plane.getDistance(sphere.getCenter()); |
106 | 0 | // Negate d if planes point inwards |
107 | 0 | if (outside == Plane::NEGATIVE_SIDE) d = -d; |
108 | 0 |
|
109 | 0 | if ( (d - sphere.getRadius()) > 0) |
110 | 0 | return false; |
111 | 0 | } |
112 | 0 |
|
113 | 0 | return true; |
114 | 0 |
|
115 | 0 | } |
116 | | |
117 | | /** Intersection test with a Ray |
118 | | @return std::pair of hit (bool) and distance |
119 | | @remarks May return false positives but will never miss an intersection. |
120 | | */ |
121 | | inline std::pair<bool, Real> intersects(const Ray& ray) |
122 | 0 | { |
123 | 0 | return Math::intersects(ray, planes, outside == Plane::POSITIVE_SIDE); |
124 | 0 | } |
125 | | |
126 | | }; |
127 | | |
128 | | typedef std::vector<PlaneBoundedVolume> PlaneBoundedVolumeList; |
129 | | |
130 | | /** @} */ |
131 | | /** @} */ |
132 | | |
133 | | } |
134 | | |
135 | | #include "OgreHeaderSuffix.h" |
136 | | |
137 | | #endif |
138 | | |