Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/include/OgrePlaneBoundedVolume.h @ 5

Last change on this file since 5 was 5, checked in by anonymous, 17 years ago

=hoffentlich gehts jetzt

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