Visualization Library 2.0.0

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Plane.cpp
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 #include <vlCore/Plane.hpp>
33 #include <vlCore/AABB.hpp>
34 
35 using namespace vl;
36 
37 //-----------------------------------------------------------------------------
38 // Plane
39 //-----------------------------------------------------------------------------
40 real Plane::distance(const vec3 &v) const
41 {
42  return dot(v, mNormal) - mOrigin;
43 }
44 //-----------------------------------------------------------------------------
45 bool Plane::isOutside(const AABB& aabb) const
46 {
47  vec3 pt;
48  pt.x() = mNormal.x() >= 0 ? aabb.minCorner().x() : aabb.maxCorner().x();
49  pt.y() = mNormal.y() >= 0 ? aabb.minCorner().y() : aabb.maxCorner().y();
50  pt.z() = mNormal.z() >= 0 ? aabb.minCorner().z() : aabb.maxCorner().z();
51  return distance(pt) >= 0;
52 }
53 //-----------------------------------------------------------------------------
54 int Plane::classify(const AABB& aabb) const
55 {
56  vec3 corner[] =
57  {
58  vec3(aabb.minCorner().x(), aabb.minCorner().y(), aabb.minCorner().z()),
59  vec3(aabb.minCorner().x(), aabb.minCorner().y(), aabb.maxCorner().z()),
60  vec3(aabb.minCorner().x(), aabb.maxCorner().y(), aabb.minCorner().z()),
61  vec3(aabb.minCorner().x(), aabb.maxCorner().y(), aabb.maxCorner().z()),
62  vec3(aabb.maxCorner().x(), aabb.minCorner().y(), aabb.minCorner().z()),
63  vec3(aabb.maxCorner().x(), aabb.minCorner().y(), aabb.maxCorner().z()),
64  vec3(aabb.maxCorner().x(), aabb.maxCorner().y(), aabb.minCorner().z()),
65  vec3(aabb.maxCorner().x(), aabb.maxCorner().y(), aabb.maxCorner().z())
66  };
67 
68  int left = 0;
69  int right = 0;
70  real const NEPS = -0.0001f;
71  real const PEPS = +0.0001f;
72 
73  for(int i=0;i<8; ++i)
74  {
75  if ( distance(corner[i]) < NEPS )
76  left++;
77  else
78  if ( distance(corner[i]) > PEPS )
79  right++;
80  // else -> we don't count the points on the plane
81 
82  if(left && right) // its clearly intersecting the plane
83  return 0;
84  }
85 
86  if (left)
87  return -1;
88  else
89  if (right)
90  return +1;
91  else // all the points were on the plane
92  return 0;
93 }
94 //-----------------------------------------------------------------------------
const T_Scalar & z() const
Definition: Vector3.hpp:91
vec3 mNormal
Definition: Plane.hpp:83
bool isOutside(const AABB &) const
Definition: Plane.cpp:45
real distance(const vec3 &v) const
Definition: Plane.cpp:40
Visualization Library main namespace.
int classify(const AABB &) const
returns 0 if the AABB intersects the plane, 1 if it&#39;s in the positive side, -1 if it&#39;s in the negativ...
Definition: Plane.cpp:54
float dot(float a, float b)
Definition: glsl_math.hpp:1111
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
const vec3 & maxCorner() const
Returns the corner of the AABB with the maximum x y z coordinates.
Definition: AABB.hpp:193
const T_Scalar & y() const
Definition: Vector3.hpp:90
real mOrigin
Definition: Plane.hpp:84
const vec3 & minCorner() const
Returns the corner of the AABB with the minimum x y z coordinates.
Definition: AABB.hpp:190
fvec3 vec3
Defined as: &#39;typedef fvec3 vec3&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector3.hpp:269
const T_Scalar & x() const
Definition: Vector3.hpp:89