hpp-fcl  3.0.0
HPP fork of FCL -- The Flexible Collision Library
AABB.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2015, Open Source Robotics Foundation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Open Source Robotics Foundation nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
38 #ifndef HPP_FCL_AABB_H
39 #define HPP_FCL_AABB_H
40 
41 #include "hpp/fcl/data_types.h"
42 
43 namespace hpp {
44 namespace fcl {
45 
46 struct CollisionRequest;
47 class Plane;
48 class Halfspace;
49 
53 
57  public:
62 
64  AABB();
65 
67  AABB(const Vec3f& v) : min_(v), max_(v) {}
68 
70  AABB(const Vec3f& a, const Vec3f& b)
71  : min_(a.cwiseMin(b)), max_(a.cwiseMax(b)) {}
72 
74  AABB(const AABB& core, const Vec3f& delta)
75  : min_(core.min_ - delta), max_(core.max_ + delta) {}
76 
78  AABB(const Vec3f& a, const Vec3f& b, const Vec3f& c)
79  : min_(a.cwiseMin(b).cwiseMin(c)), max_(a.cwiseMax(b).cwiseMax(c)) {}
80 
81  AABB(const AABB& other) = default;
82 
83  AABB& operator=(const AABB& other) = default;
84 
85  AABB& update(const Vec3f& a, const Vec3f& b) {
86  min_ = a.cwiseMin(b);
87  max_ = a.cwiseMax(b);
88  return *this;
89  }
90 
92  bool operator==(const AABB& other) const {
93  return min_ == other.min_ && max_ == other.max_;
94  }
95 
96  bool operator!=(const AABB& other) const { return !(*this == other); }
97 
101 
103  inline bool contain(const Vec3f& p) const {
104  if (p[0] < min_[0] || p[0] > max_[0]) return false;
105  if (p[1] < min_[1] || p[1] > max_[1]) return false;
106  if (p[2] < min_[2] || p[2] > max_[2]) return false;
107 
108  return true;
109  }
110 
112  inline bool overlap(const AABB& other) const {
113  if (min_[0] > other.max_[0]) return false;
114  if (min_[1] > other.max_[1]) return false;
115  if (min_[2] > other.max_[2]) return false;
116 
117  if (max_[0] < other.min_[0]) return false;
118  if (max_[1] < other.min_[1]) return false;
119  if (max_[2] < other.min_[2]) return false;
120 
121  return true;
122  }
123 
125  bool overlap(const Plane& p) const;
126 
128  bool overlap(const Halfspace& hs) const;
129 
131  bool overlap(const AABB& other, const CollisionRequest& request,
132  FCL_REAL& sqrDistLowerBound) const;
133 
135  FCL_REAL distance(const AABB& other) const;
136 
139  FCL_REAL distance(const AABB& other, Vec3f* P, Vec3f* Q) const;
140 
142  inline AABB& operator+=(const Vec3f& p) {
143  min_ = min_.cwiseMin(p);
144  max_ = max_.cwiseMax(p);
145  return *this;
146  }
147 
149  inline AABB& operator+=(const AABB& other) {
150  min_ = min_.cwiseMin(other.min_);
151  max_ = max_.cwiseMax(other.max_);
152  return *this;
153  }
154 
156  inline AABB operator+(const AABB& other) const {
157  AABB res(*this);
158  return res += other;
159  }
160 
162  inline FCL_REAL size() const { return (max_ - min_).squaredNorm(); }
163 
165  inline Vec3f center() const { return (min_ + max_) * 0.5; }
166 
168  inline FCL_REAL width() const { return max_[0] - min_[0]; }
169 
171  inline FCL_REAL height() const { return max_[1] - min_[1]; }
172 
174  inline FCL_REAL depth() const { return max_[2] - min_[2]; }
175 
177  inline FCL_REAL volume() const { return width() * height() * depth(); }
178 
180 
182  inline bool contain(const AABB& other) const {
183  return (other.min_[0] >= min_[0]) && (other.max_[0] <= max_[0]) &&
184  (other.min_[1] >= min_[1]) && (other.max_[1] <= max_[1]) &&
185  (other.min_[2] >= min_[2]) && (other.max_[2] <= max_[2]);
186  }
187 
189  inline bool overlap(const AABB& other, AABB& overlap_part) const {
190  if (!overlap(other)) {
191  return false;
192  }
193 
194  overlap_part.min_ = min_.cwiseMax(other.min_);
195  overlap_part.max_ = max_.cwiseMin(other.max_);
196  return true;
197  }
198 
200  inline bool axisOverlap(const AABB& other, int axis_id) const {
201  if (min_[axis_id] > other.max_[axis_id]) return false;
202  if (max_[axis_id] < other.min_[axis_id]) return false;
203 
204  return true;
205  }
206 
209  inline AABB& expand(const Vec3f& delta) {
210  min_ -= delta;
211  max_ += delta;
212  return *this;
213  }
214 
217  inline AABB& expand(const FCL_REAL delta) {
218  min_.array() -= delta;
219  max_.array() += delta;
220  return *this;
221  }
222 
224  inline AABB& expand(const AABB& core, FCL_REAL ratio) {
225  min_ = min_ * ratio - core.min_;
226  max_ = max_ * ratio - core.max_;
227  return *this;
228  }
229 
230  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
231 };
232 
234 static inline AABB translate(const AABB& aabb, const Vec3f& t) {
235  AABB res(aabb);
236  res.min_ += t;
237  res.max_ += t;
238  return res;
239 }
240 
241 static inline AABB rotate(const AABB& aabb, const Matrix3f& R) {
242  AABB res(R * aabb.min_);
243  Vec3f corner(aabb.min_);
244  const Eigen::DenseIndex bit[3] = {1, 2, 4};
245  for (Eigen::DenseIndex ic = 1; ic < 8;
246  ++ic) { // ic = 0 corresponds to aabb.min_. Skip it.
247  for (Eigen::DenseIndex i = 0; i < 3; ++i) {
248  corner[i] = (ic & bit[i]) ? aabb.max_[i] : aabb.min_[i];
249  }
250  res += R * corner;
251  }
252  return res;
253 }
254 
257 HPP_FCL_DLLAPI bool overlap(const Matrix3f& R0, const Vec3f& T0, const AABB& b1,
258  const AABB& b2);
259 
262 HPP_FCL_DLLAPI bool overlap(const Matrix3f& R0, const Vec3f& T0, const AABB& b1,
263  const AABB& b2, const CollisionRequest& request,
264  FCL_REAL& sqrDistLowerBound);
265 } // namespace fcl
266 
267 } // namespace hpp
268 
269 #endif
A class describing the AABB collision structure, which is a box in 3D space determined by two diagona...
Definition: AABB.h:56
Half Space: this is equivalent to the Plane in ODE. A Half space has a priviledged direction: the dir...
Definition: geometric_shapes.h:885
Infinite plane. A plane can be viewed as two half spaces; it has no priviledged direction....
Definition: geometric_shapes.h:976
#define HPP_FCL_DLLAPI
Definition: config.hh:88
Vec3f max_
The max point in the AABB.
Definition: AABB.h:61
bool overlap(const AABB &other, const CollisionRequest &request, FCL_REAL &sqrDistLowerBound) const
Check whether two AABB are overlap.
AABB(const Vec3f &v)
Creating an AABB at position v with zero size.
Definition: AABB.h:67
bool contain(const AABB &other) const
Check whether the AABB contains another AABB.
Definition: AABB.h:182
AABB & operator+=(const AABB &other)
Merge the AABB and another AABB.
Definition: AABB.h:149
bool operator!=(const AABB &other) const
Definition: AABB.h:96
FCL_REAL depth() const
Depth of the AABB.
Definition: AABB.h:174
FCL_REAL volume() const
Volume of the AABB.
Definition: AABB.h:177
bool overlap(const Halfspace &hs) const
Check whether AABB overlaps a halfspace.
AABB operator+(const AABB &other) const
Return the merged AABB of current AABB and the other one.
Definition: AABB.h:156
FCL_REAL distance(const AABB &other) const
Distance between two AABBs.
AABB(const AABB &core, const Vec3f &delta)
Creating an AABB centered as core and is of half-dimension delta.
Definition: AABB.h:74
bool overlap(const AABB &other, AABB &overlap_part) const
Check whether two AABB are overlap and return the overlap part.
Definition: AABB.h:189
FCL_REAL width() const
Width of the AABB.
Definition: AABB.h:168
bool operator==(const AABB &other) const
Comparison operator.
Definition: AABB.h:92
bool overlap(const AABB &other) const
Check whether two AABB are overlap.
Definition: AABB.h:112
AABB & operator+=(const Vec3f &p)
Merge the AABB and a point.
Definition: AABB.h:142
AABB(const Vec3f &a, const Vec3f &b)
Creating an AABB with two endpoints a and b.
Definition: AABB.h:70
Vec3f min_
The min point in the AABB.
Definition: AABB.h:59
AABB & expand(const FCL_REAL delta)
expand the half size of the AABB by a scalar delta, and keep the center unchanged.
Definition: AABB.h:217
bool overlap(const Matrix3f &R0, const Vec3f &T0, const AABB &b1, const AABB &b2)
Check collision between two aabbs, b1 is in configuration (R0, T0) and b2 is in identity.
bool axisOverlap(const AABB &other, int axis_id) const
Check whether two AABB are overlapped along specific axis.
Definition: AABB.h:200
AABB & update(const Vec3f &a, const Vec3f &b)
Definition: AABB.h:85
AABB()
Creating an AABB with zero size (low bound +inf, upper bound -inf)
AABB & operator=(const AABB &other)=default
AABB & expand(const AABB &core, FCL_REAL ratio)
expand the aabb by increase the thickness of the plate by a ratio
Definition: AABB.h:224
AABB(const Vec3f &a, const Vec3f &b, const Vec3f &c)
Creating an AABB contains three points.
Definition: AABB.h:78
bool contain(const Vec3f &p) const
Check whether the AABB contains a point.
Definition: AABB.h:103
AABB & expand(const Vec3f &delta)
expand the half size of the AABB by delta, and keep the center unchanged.
Definition: AABB.h:209
FCL_REAL height() const
Height of the AABB.
Definition: AABB.h:171
FCL_REAL distance(const AABB &other, Vec3f *P, Vec3f *Q) const
Distance between two AABBs; P and Q, should not be NULL, return the nearest points.
AABB(const AABB &other)=default
Vec3f center() const
Center of the AABB.
Definition: AABB.h:165
bool overlap(const Plane &p) const
Check whether AABB overlaps a plane.
FCL_REAL size() const
Size of the AABB (used in BV_Splitter to order two AABBs)
Definition: AABB.h:162
Eigen::Matrix< FCL_REAL, 3, 3 > Matrix3f
Definition: data_types.h:71
Eigen::Matrix< FCL_REAL, 3, 1 > Vec3f
Definition: data_types.h:67
double FCL_REAL
Definition: data_types.h:66
Main namespace.
Definition: broadphase_bruteforce.h:44
request to the collision algorithm
Definition: collision_data.h:312