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.cpp
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 #include <vlCore/AABB.hpp>
33 
34 using namespace vl;
35 
36 //-----------------------------------------------------------------------------
37 // AABB
38 //-----------------------------------------------------------------------------
40 {
41  setNull();
42 }
43 //-----------------------------------------------------------------------------
44 AABB::AABB( const vec3& center, real radius )
45 {
46  mMax = center + radius;
47  mMin = center - radius;
48 }
49 //-----------------------------------------------------------------------------
50 AABB::AABB( const vec3& pt1, const vec3& pt2, real displace)
51 {
52  mMax = mMin = pt1;
53  if ( mMax.x() < pt2.x() ) mMax.x() = pt2.x();
54  if ( mMax.y() < pt2.y() ) mMax.y() = pt2.y();
55  if ( mMax.z() < pt2.z() ) mMax.z() = pt2.z();
56  if ( mMin.x() > pt2.x() ) mMin.x() = pt2.x();
57  if ( mMin.y() > pt2.y() ) mMin.y() = pt2.y();
58  if ( mMin.z() > pt2.z() ) mMin.z() = pt2.z();
59 
60  mMax = mMax + displace;
61  mMin = mMin - displace;
62 }
63 //-----------------------------------------------------------------------------
64 void AABB::enlarge(real displace) {
65  if ( isNull() )
66  return;
67 
68  mMax = mMax + displace;
69  mMin = mMin - displace;
70 }
71 //-----------------------------------------------------------------------------
72 bool AABB::intersects(const AABB& bb) const
73 {
74  if (isNull() || bb.isNull())
75  return false;
76 
77  if (mMax.x() < bb.mMin.x())
78  return false;
79 
80  if (mMax.z() < bb.mMin.z())
81  return false;
82 
83  if (mMin.x() > bb.mMax.x())
84  return false;
85 
86  if (mMin.z() > bb.mMax.z())
87  return false;
88 
89  return true;
90 }
91 //-----------------------------------------------------------------------------
92 vec3 AABB::clip(const vec3& v, bool clipx, bool clipy, bool clipz) const
93 {
94  if (isNull())
95  return v;
96 
97  vec3 tmp = v;
98 
99  if (clipx) {
100  if (v.x() > mMax.x())
101  tmp.x() = mMax.x();
102  if (v.x() < mMin.x())
103  tmp.x() = mMin.x();
104  }
105 
106  if (clipy) {
107  if (v.y() > mMax.y())
108  tmp.y() = mMax.y();
109  if (v.y() < mMin.y())
110  tmp.y() = mMin.y();
111  }
112 
113  if (clipz) {
114  if (v.z() > mMax.z())
115  tmp.z() = mMax.z();
116  if (v.z() < mMin.z())
117  tmp.z() = mMin.z();
118  }
119  return tmp;
120 }
121 //-----------------------------------------------------------------------------
122 bool AABB::isInside(const vec3& v, bool clipx, bool clipy, bool clipz) const
123 {
124  vec3 t = v;
125  return v == clip(t, clipx, clipy, clipz);
126 }
127 //-----------------------------------------------------------------------------
128 bool AABB::isInside(const vec3& v) const
129 {
130  return v.x() >= minCorner().x() && v.x() <= maxCorner().x() &&
131  v.y() >= minCorner().y() && v.y() <= maxCorner().y() &&
132  v.z() >= minCorner().z() && v.z() <= maxCorner().z();
133 }
134 //-----------------------------------------------------------------------------
135 void AABB::addPoint(const vec3& v, real radius)
136 {
137  if (isNull())
138  {
139  mMax = v;
140  mMin = v;
141  }
142 
143  if ( mMax.x() < v.x() + radius) mMax.x() = v.x() + radius;
144  if ( mMax.y() < v.y() + radius) mMax.y() = v.y() + radius;
145  if ( mMax.z() < v.z() + radius) mMax.z() = v.z() + radius;
146  if ( mMin.x() > v.x() - radius) mMin.x() = v.x() - radius;
147  if ( mMin.y() > v.y() - radius) mMin.y() = v.y() - radius;
148  if ( mMin.z() > v.z() - radius) mMin.z() = v.z() - radius;
149 }
150 //-----------------------------------------------------------------------------
151 real AABB::width() const {
152  if (isNull())
153  return 0;
154  else
155  return mMax.x() - mMin.x();
156 }
157 //-----------------------------------------------------------------------------
158 real AABB::height() const {
159  if (isNull())
160  return 0;
161  else
162  return mMax.y() - mMin.y();
163 }
164 //-----------------------------------------------------------------------------
165 real AABB::depth() const {
166  if (isNull())
167  return 0;
168  else
169  return mMax.z() - mMin.z();
170 }
171 //-----------------------------------------------------------------------------
172 AABB AABB::operator+(const AABB& aabb) const
173 {
174  if(isNull())
175  return aabb;
176  if (aabb.isNull())
177  return *this;
178  AABB tmp = aabb;
179  tmp.addPoint( mMin );
180  tmp.addPoint( mMax );
181  return tmp;
182 }
183 //-----------------------------------------------------------------------------
185 {
186  vec3 c = (minCorner() + maxCorner()) / 2.0f;
187  return c;
188 }
189 //-----------------------------------------------------------------------------
AABB()
Constructs a null AABB.
Definition: AABB.cpp:39
bool isNull() const
Returns true if the AABB is null.
Definition: AABB.hpp:60
const T_Scalar & z() const
Definition: Vector3.hpp:91
vec3 center() const
Returns the center of the AABB.
Definition: AABB.cpp:184
void setNull()
Sets ths AABB as null, that is, empty.
Definition: AABB.hpp:57
void enlarge(real displace)
Enlarges the AABB in all directions by displace amount.
Definition: AABB.cpp:64
vec3 mMax
Definition: AABB.hpp:212
Visualization Library main namespace.
vec3 clip(const vec3 &p, bool clipx=true, bool clipy=true, bool clipz=true) const
Clips the position of the given p point to be inside an AABB.
Definition: AABB.cpp:92
vec3 mMin
Definition: AABB.hpp:211
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
real width() const
Returns the width of the AABB computed as max.x - min.x.
Definition: AABB.cpp:151
const vec3 & maxCorner() const
Returns the corner of the AABB with the maximum x y z coordinates.
Definition: AABB.hpp:193
bool intersects(const AABB &bb) const
Returns true if an AABB intersects with the given AABB.
Definition: AABB.cpp:72
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
real depth() const
Returns the depth of the AABB computed as max.z - min.z.
Definition: AABB.cpp:165
const vec3 & minCorner() const
Returns the corner of the AABB with the minimum x y z coordinates.
Definition: AABB.hpp:190
bool isInside(const vec3 &p, bool clipx, bool clipy, bool clipz) const
Returns true if the given point is inside the AABB.
Definition: AABB.cpp:122
const T_Scalar & x() const
Definition: Vector3.hpp:89
AABB operator+(const AABB &aabb) const
Returns an AABB which contains the two source AABB.
Definition: AABB.cpp:172
real height() const
Returns the height of the AABB computed as max.y - min.y.
Definition: AABB.cpp:158