Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Frustum.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Frustum_INCLUDE_ONCE
33 #define Frustum_INCLUDE_ONCE
34 
35 #include <vlCore/Plane.hpp>
36 #include <vlCore/AABB.hpp>
37 #include <vlCore/Sphere.hpp>
38 
39 namespace vl
40 {
41  //-----------------------------------------------------------------------------
42  // Frustum
43  //-----------------------------------------------------------------------------
49  class Frustum: public Object
50  {
52 
53  public:
54  std::vector<Plane>& planes() { return mPlanes; }
55  const std::vector<Plane>& planes() const { return mPlanes; }
56 
57  void setPlane(unsigned i, const Plane& plane) { VL_CHECK(i<mPlanes.size()); mPlanes[i] = plane; }
58  const Plane& plane(unsigned i) const { VL_CHECK(i<mPlanes.size()); return mPlanes[i]; }
59 
60  bool cull(const Sphere& sphere) const
61  {
62  // null spheres are always visible
63  if (sphere.isNull())
64  return false;
65  for(unsigned i=0; i<planes().size(); ++i)
66  {
67  if ( plane(i).distance(sphere.center()) > sphere.radius() )
68  return true;
69  }
70  return false;
71  }
72 
73  bool cull(const AABB& aabb) const
74  {
75  if (aabb.isNull())
76  return false;
77  for(unsigned i=0; i<planes().size(); ++i)
78  {
79  if ( plane(i).isOutside(aabb) )
80  return true;
81  }
82  return false;
83  }
84 
85  bool cull(const std::vector<fvec3>& points) const
86  {
87  for(unsigned i=0; i<planes().size(); ++i)
88  {
89  unsigned j=0;
90  for(; j<points.size(); ++j)
91  if ( plane(i).distance((vec3)points[j]) <= 0 )
92  break;
93  if(j == points.size())
94  return true;
95  }
96  return false;
97  }
98 
99  protected:
100  std::vector<Plane> mPlanes;
101  };
102 }
103 
104 #endif
bool cull(const AABB &aabb) const
Definition: Frustum.hpp:73
The Plane class defines a plane using a normal and an origin.
Definition: Plane.hpp:49
bool isNull() const
Returns true if the AABB is null.
Definition: AABB.hpp:60
void setPlane(unsigned i, const Plane &plane)
Definition: Frustum.hpp:57
A set of planes defining a frustum used for culling purposes (frustum culling).
Definition: Frustum.hpp:49
std::vector< Plane > & planes()
Definition: Frustum.hpp:54
const vec3 & center() const
Returns the center of the sphere.
Definition: Sphere.hpp:68
bool cull(const Sphere &sphere) const
Definition: Frustum.hpp:60
bool isOutside(const AABB &) const
Definition: Plane.cpp:45
const Plane & plane(unsigned i) const
Definition: Frustum.hpp:58
bool cull(const std::vector< fvec3 > &points) const
Definition: Frustum.hpp:85
real distance(const vec3 &v) const
Definition: Plane.cpp:40
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
Visualization Library main namespace.
real radius() const
Returns the radius of the sphere.
Definition: Sphere.hpp:74
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
The base class for all the reference counted objects.
Definition: Object.hpp:158
The Sphere class defines a sphere using a center and a radius using vl::real precision.
Definition: Sphere.hpp:43
std::vector< Plane > mPlanes
Definition: Frustum.hpp:100
const std::vector< Plane > & planes() const
Definition: Frustum.hpp:55
bool isNull() const
Returns true if the sphere is null, ie, if radius is < 0.
Definition: Sphere.hpp:59
#define VL_CHECK(expr)
Definition: checks.hpp:73