source: GTP/trunk/App/Demos/Geom/OgreStuff/include/OgrePlaneBoundedVolume.h @ 1812

Revision 1812, 4.5 KB checked in by gumbau, 18 years ago (diff)
Line 
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-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#ifndef __PlaneBoundedVolume_H_
26#define __PlaneBoundedVolume_H_
27
28// Precompiler options
29#include "OgrePrerequisites.h"
30#include "OgreAxisAlignedBox.h"
31#include "OgreSphere.h"
32#include "OgreMath.h"
33#include "OgrePlane.h"
34
35namespace Ogre {
36
37    /** Represents a convex volume bounded by planes.
38    */
39    class _OgreExport PlaneBoundedVolume
40    {
41    public:
42        typedef std::vector<Plane> PlaneList;
43        /// Publicly accessible plane list, you can modify this direct
44        PlaneList planes;
45        Plane::Side outside;
46
47        PlaneBoundedVolume() :outside(Plane::NEGATIVE_SIDE) {}
48        /** Constructor, determines which side is deemed to be 'outside' */
49        PlaneBoundedVolume(Plane::Side theOutside)
50            : outside(theOutside) {}
51
52        /** Intersection test with AABB
53        @remarks May return false positives but will never miss an intersection.
54        */
55        inline bool intersects(const AxisAlignedBox& box) const
56        {
57            if (box.isNull()) return false;
58            // If all points are on outside of any plane, we fail
59            const Vector3* points = box.getAllCorners();
60            PlaneList::const_iterator i, iend;
61            iend = planes.end();
62            for (i = planes.begin(); i != iend; ++i)
63            {
64                const Plane& plane = *i;
65
66                // Test which side of the plane the corners are
67                // Intersection fails when at all corners are on the
68                // outside of one plane
69                bool splittingPlane = true;
70                for (int corner = 0; corner < 8; ++corner)
71                {
72                    if (plane.getSide(points[corner]) != outside)
73                    {
74                        // this point is on the wrong side
75                        splittingPlane = false;
76                        break;
77                    }
78                }
79                if (splittingPlane)
80                {
81                    // Found a splitting plane therefore return not intersecting
82                    return false;
83                }
84            }
85
86            // couldn't find a splitting plane, assume intersecting
87            return true;
88
89        }
90        /** Intersection test with Sphere
91        @remarks May return false positives but will never miss an intersection.
92        */
93        inline bool intersects(const Sphere& sphere) const
94        {
95            PlaneList::const_iterator i, iend;
96            iend = planes.end();
97            for (i = planes.begin(); i != iend; ++i)
98            {
99                const Plane& plane = *i;
100
101                // Test which side of the plane the sphere is
102                Real d = plane.getDistance(sphere.getCenter());
103                // Negate d if planes point inwards
104                if (outside == Plane::NEGATIVE_SIDE) d = -d;
105
106                if ( (d - sphere.getRadius()) > 0)
107                    return false;
108            }
109
110            return true;
111
112        }
113
114        /** Intersection test with a Ray
115        @returns std::pair of hit (bool) and distance
116        @remarks May return false positives but will never miss an intersection.
117        */
118        inline std::pair<bool, Real> intersects(const Ray& ray)
119        {
120            return Math::intersects(ray, planes, outside == Plane::POSITIVE_SIDE);
121        }
122
123    };
124
125    typedef std::vector<PlaneBoundedVolume> PlaneBoundedVolumeList;
126
127
128}
129
130#endif
131
Note: See TracBrowser for help on using the repository browser.