Visualization Library 2.0.0-b3

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
AABB.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, 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 AABB_INCLUDE_ONCE
33 #define AABB_INCLUDE_ONCE
34 
35 #include <vlCore/Vector3.hpp>
36 #include <vlCore/Matrix4.hpp>
37 
38 namespace vl
39 {
40  //-----------------------------------------------------------------------------
41  // AABB
42  //-----------------------------------------------------------------------------
45  {
46  public:
48  AABB();
49 
51  AABB( const vec3& center, real radius );
52 
54  AABB( const vec3& pt1, const vec3& pt2, real displace=0);
55 
57  void setNull() { mMin = 1; mMax = -1; }
58 
60  bool isNull() const { return mMin.x() > mMax.x() || mMin.y() > mMax.y() || mMin.z() > mMax.z(); }
61 
63  bool isPoint() const { return mMin == mMax; }
64 
67  void enlarge(real displace);
68 
70  bool intersects(const AABB & bb) const;
71 
73  vec3 clip(const vec3& p, bool clipx=true, bool clipy=true, bool clipz=true) const;
74 
77  bool isInside(const vec3& p, bool clipx, bool clipy, bool clipz) const;
78 
80  bool isInside(const vec3& p) const;
81 
83  real width() const;
84 
86  real height() const;
87 
89  real depth() const;
90 
92  bool operator==(const AABB& aabb) const
93  {
94  return mMin == aabb.mMin && mMax == aabb.mMax;
95  }
96 
98  bool operator!=(const AABB& aabb) const
99  {
100  return !operator==(aabb);
101  }
102 
104  AABB operator+(const AABB& aabb) const;
105 
107  AABB& operator+=(const AABB& other)
108  {
109  *this = *this + other;
110  return *this;
111  }
112 
114  AABB operator+(const vec3& p)
115  {
116  AABB aabb = *this;
117  aabb += p;
118  return aabb;
119  }
120 
122  const AABB& operator+=(const vec3& p)
123  {
124  addPoint(p);
125  return *this;
126  }
127 
129  vec3 center() const;
130 
132  real longestSideLength() const
133  {
134  real side = width();
135  if (height() > side)
136  side = height();
137  if (depth() > side)
138  side = depth();
139  return side;
140  }
141 
144  void addPoint(const vec3& p, real radius);
145 
147  void addPoint(const vec3& p)
148  {
149  if (isNull())
150  {
151  mMax = p;
152  mMin = p;
153  return;
154  }
155 
156  if ( mMax.x() < p.x() ) mMax.x() = p.x();
157  if ( mMax.y() < p.y() ) mMax.y() = p.y();
158  if ( mMax.z() < p.z() ) mMax.z() = p.z();
159  if ( mMin.x() > p.x() ) mMin.x() = p.x();
160  if ( mMin.y() > p.y() ) mMin.y() = p.y();
161  if ( mMin.z() > p.z() ) mMin.z() = p.z();
162  }
163 
165  void transformed(AABB& out, const mat4& mat) const
166  {
167  out.setNull();
168  if ( !isNull() )
169  {
170  out.addPoint( mat * vec3(minCorner().x(), minCorner().y(), minCorner().z()) );
171  out.addPoint( mat * vec3(minCorner().x(), maxCorner().y(), minCorner().z()) );
172  out.addPoint( mat * vec3(maxCorner().x(), maxCorner().y(), minCorner().z()) );
173  out.addPoint( mat * vec3(maxCorner().x(), minCorner().y(), minCorner().z()) );
174  out.addPoint( mat * vec3(minCorner().x(), minCorner().y(), maxCorner().z()) );
175  out.addPoint( mat * vec3(minCorner().x(), maxCorner().y(), maxCorner().z()) );
176  out.addPoint( mat * vec3(maxCorner().x(), maxCorner().y(), maxCorner().z()) );
177  out.addPoint( mat * vec3(maxCorner().x(), minCorner().y(), maxCorner().z()) );
178  }
179  }
180 
182  AABB transformed(const mat4& mat) const
183  {
184  AABB aabb;
185  transformed(aabb, mat);
186  return aabb;
187  }
188 
190  const vec3& minCorner() const { return mMin; }
191 
193  const vec3& maxCorner() const { return mMax; }
194 
196  void setMinCorner(real x, real y, real z) { mMin.x() = x; mMin.y() = y; mMin.z() = z; }
197 
199  void setMinCorner(const vec3& v) { mMin = v; }
200 
202  void setMaxCorner(real x, real y, real z) { mMax.x() = x; mMax.y() = y; mMax.z() = z; }
203 
205  void setMaxCorner(const vec3& v) { mMax = v; }
206 
208  real volume() const { return width() * height() * depth(); }
209 
210  protected:
213  };
214 }
215 
216 #endif
AABB transformed(const mat4 &mat) const
Returns the AABB transformed by the given matrix.
Definition: AABB.hpp:182
bool operator==(const AABB &aabb) const
Returns true if two AABB are identical.
Definition: AABB.hpp:92
float operator+(float a, const half &b)
Definition: half.hpp:510
real longestSideLength() const
Returns the longest dimension of the AABB.
Definition: AABB.hpp:132
void setMaxCorner(const vec3 &v)
Sets the corner of the AABB with the maximum x y z coordinates.
Definition: AABB.hpp:205
bool isNull() const
Returns true if the AABB is null.
Definition: AABB.hpp:60
bool isPoint() const
Returns true if the AABB contains a single point, that is, if the min and max corners of the AABB are...
Definition: AABB.hpp:63
const T_Scalar & z() const
Definition: Vector3.hpp:91
void setMinCorner(const vec3 &v)
Sets the corner of the AABB with the minimum x y z coordinates.
Definition: AABB.hpp:199
AABB & operator+=(const AABB &other)
Enlarges (if necessary) an AABB so that it contains the given AABB.
Definition: AABB.hpp:107
void setNull()
Sets ths AABB as null, that is, empty.
Definition: AABB.hpp:57
vec3 mMax
Definition: AABB.hpp:212
const AABB & operator+=(const vec3 &p)
Enlarges (if necessary) an AABB to contain the given point.
Definition: AABB.hpp:122
Visualization Library main namespace.
vec3 mMin
Definition: AABB.hpp:211
void transformed(AABB &out, const mat4 &mat) const
Transforms an AABB by the given matrix and returns it into the out parameter.
Definition: AABB.hpp:165
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
AABB operator+(const vec3 &p)
Returns an AABB which contains the source AABB and the given point.
Definition: AABB.hpp:114
const vec3 & maxCorner() const
Returns the corner of the AABB with the maximum x y z coordinates.
Definition: AABB.hpp:193
void setMinCorner(real x, real y, real z)
Sets the corner of the AABB with the minimum x y z coordinates.
Definition: AABB.hpp:196
void setMaxCorner(real x, real y, real z)
Sets the corner of the AABB with the maximum x y z coordinates.
Definition: AABB.hpp:202
void addPoint(const vec3 &p, real radius)
Updates the AABB to contain the given point.
Definition: AABB.cpp:135
const T_Scalar & y() const
Definition: Vector3.hpp:90
void addPoint(const vec3 &p)
Updates the AABB to contain the given point.
Definition: AABB.hpp:147
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
bool operator==(const ref< T1 > &o1, const ref< T2 > &o2)
Definition: Object.hpp:144
const T_Scalar & x() const
Definition: Vector3.hpp:89
real volume() const
Returns the volume of the AABB.
Definition: AABB.hpp:208
bool operator!=(const AABB &aabb) const
Returns true if two AABB are not identical.
Definition: AABB.hpp:98