coal  3.0.1
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
collision_object.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 COAL_COLLISION_OBJECT_BASE_H
39 #define COAL_COLLISION_OBJECT_BASE_H
40 
41 #include <limits>
42 #include <typeinfo>
43 
44 #include "coal/deprecated.hh"
45 #include "coal/fwd.hh"
46 #include "coal/BV/AABB.h"
47 #include "coal/math/transform.h"
48 
49 namespace coal {
50 
58  OT_COUNT
59 };
60 
64 enum NODE_TYPE {
90 };
91 
94 
97  public:
99  : aabb_center(Vec3s::Constant((std::numeric_limits<Scalar>::max)())),
100  aabb_radius(-1),
101  cost_density(1),
102  threshold_occupied(1),
103  threshold_free(0) {}
104 
106  CollisionGeometry(const CollisionGeometry& other) = default;
107 
108  virtual ~CollisionGeometry() {}
109 
111  virtual CollisionGeometry* clone() const = 0;
112 
114  bool operator==(const CollisionGeometry& other) const {
115  return cost_density == other.cost_density &&
116  threshold_occupied == other.threshold_occupied &&
117  threshold_free == other.threshold_free &&
118  aabb_center == other.aabb_center &&
119  aabb_radius == other.aabb_radius && aabb_local == other.aabb_local &&
120  isEqual(other);
121  }
122 
124  bool operator!=(const CollisionGeometry& other) const {
125  return isNotEqual(other);
126  }
127 
129  virtual OBJECT_TYPE getObjectType() const { return OT_UNKNOWN; }
130 
132  virtual NODE_TYPE getNodeType() const { return BV_UNKNOWN; }
133 
135  virtual void computeLocalAABB() = 0;
136 
138  void* getUserData() const { return user_data; }
139 
141  void setUserData(void* data) { user_data = data; }
142 
144  inline bool isOccupied() const { return cost_density >= threshold_occupied; }
145 
147  inline bool isFree() const { return cost_density <= threshold_free; }
148 
150  bool isUncertain() const;
151 
154 
157 
161 
163  void* user_data;
164 
167 
170 
173 
175  virtual Vec3s computeCOM() const { return Vec3s::Zero(); }
176 
179  return Matrix3s::Constant(NAN);
180  }
181 
183  virtual Scalar computeVolume() const { return 0; }
184 
187  Matrix3s C = computeMomentofInertia();
188  Vec3s com = computeCOM();
189  Scalar V = computeVolume();
190 
191  return (Matrix3s() << C(0, 0) - V * (com[1] * com[1] + com[2] * com[2]),
192  C(0, 1) + V * com[0] * com[1], C(0, 2) + V * com[0] * com[2],
193  C(1, 0) + V * com[1] * com[0],
194  C(1, 1) - V * (com[0] * com[0] + com[2] * com[2]),
195  C(1, 2) + V * com[1] * com[2], C(2, 0) + V * com[2] * com[0],
196  C(2, 1) + V * com[2] * com[1],
197  C(2, 2) - V * (com[0] * com[0] + com[1] * com[1]))
198  .finished();
199  }
200 
201  private:
203  virtual bool isEqual(const CollisionGeometry& other) const = 0;
204 
206  virtual bool isNotEqual(const CollisionGeometry& other) const {
207  return !(*this == other);
208  }
209 
210  public:
211  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
212 };
213 
217  public:
218  CollisionObject(const shared_ptr<CollisionGeometry>& cgeom_,
219  bool compute_local_aabb = true)
220  : cgeom(cgeom_), user_data(nullptr) {
221  init(compute_local_aabb);
222  }
223 
224  CollisionObject(const shared_ptr<CollisionGeometry>& cgeom_,
225  const Transform3s& tf, bool compute_local_aabb = true)
226  : cgeom(cgeom_), t(tf), user_data(nullptr) {
227  init(compute_local_aabb);
228  }
229 
230  CollisionObject(const shared_ptr<CollisionGeometry>& cgeom_,
231  const Matrix3s& R, const Vec3s& T,
232  bool compute_local_aabb = true)
233  : cgeom(cgeom_), t(R, T), user_data(nullptr) {
234  init(compute_local_aabb);
235  }
236 
237  bool operator==(const CollisionObject& other) const {
238  return cgeom == other.cgeom && t == other.t && user_data == other.user_data;
239  }
240 
241  bool operator!=(const CollisionObject& other) const {
242  return !(*this == other);
243  }
244 
246 
248  OBJECT_TYPE getObjectType() const { return cgeom->getObjectType(); }
249 
251  NODE_TYPE getNodeType() const { return cgeom->getNodeType(); }
252 
254  const AABB& getAABB() const { return aabb; }
255 
257  AABB& getAABB() { return aabb; }
258 
260  void computeAABB() {
261  if (t.getRotation().isIdentity()) {
262  aabb = translate(cgeom->aabb_local, t.getTranslation());
263  } else {
264  aabb.min_ = aabb.max_ = t.getTranslation();
265 
266  Vec3s min_world, max_world;
267  for (int k = 0; k < 3; ++k) {
268  min_world.array() = t.getRotation().row(k).array() *
269  cgeom->aabb_local.min_.transpose().array();
270  max_world.array() = t.getRotation().row(k).array() *
271  cgeom->aabb_local.max_.transpose().array();
272 
273  aabb.min_[k] += (min_world.array().min)(max_world.array()).sum();
274  aabb.max_[k] += (min_world.array().max)(max_world.array()).sum();
275  }
276  }
277  }
278 
280  void* getUserData() const { return user_data; }
281 
283  void setUserData(void* data) { user_data = data; }
284 
286  inline const Vec3s& getTranslation() const { return t.getTranslation(); }
287 
289  inline const Matrix3s& getRotation() const { return t.getRotation(); }
290 
292  inline const Transform3s& getTransform() const { return t; }
293 
295  void setRotation(const Matrix3s& R) { t.setRotation(R); }
296 
298  void setTranslation(const Vec3s& T) { t.setTranslation(T); }
299 
301  void setTransform(const Matrix3s& R, const Vec3s& T) { t.setTransform(R, T); }
302 
304  void setTransform(const Transform3s& tf) { t = tf; }
305 
307  bool isIdentityTransform() const { return t.isIdentity(); }
308 
310  void setIdentityTransform() { t.setIdentity(); }
311 
313  const shared_ptr<const CollisionGeometry> collisionGeometry() const {
314  return cgeom;
315  }
316 
318  const shared_ptr<CollisionGeometry>& collisionGeometry() { return cgeom; }
319 
321  const CollisionGeometry* collisionGeometryPtr() const { return cgeom.get(); }
322 
324  CollisionGeometry* collisionGeometryPtr() { return cgeom.get(); }
325 
333  const shared_ptr<CollisionGeometry>& collision_geometry,
334  bool compute_local_aabb = true) {
335  if (collision_geometry.get() != cgeom.get()) {
336  cgeom = collision_geometry;
337  init(compute_local_aabb);
338  }
339  }
340 
341  protected:
342  void init(bool compute_local_aabb = true) {
343  if (cgeom) {
344  if (compute_local_aabb) cgeom->computeLocalAABB();
345  computeAABB();
346  }
347  }
348 
349  shared_ptr<CollisionGeometry> cgeom;
350 
352 
354  mutable AABB aabb;
355 
357  void* user_data;
358 };
359 
360 } // namespace coal
361 
362 #endif
A class describing the AABB collision structure, which is a box in 3D space determined by two diagona...
Definition: AABB.h:55
The geometry for the object for collision or distance computation.
Definition: collision_object.h:96
the object for collision or distance computation, contains the geometry and the transform information
Definition: collision_object.h:216
Simple transform class used locally by InterpMotion.
Definition: transform.h:55
#define COAL_DLLAPI
Definition: config.hh:88
void * user_data
pointer to user defined data specific to this object
Definition: collision_object.h:163
void setTransform(const Transform3s &tf)
set object's transform
Definition: collision_object.h:304
const shared_ptr< CollisionGeometry > & collisionGeometry()
get shared pointer to collision geometry of the object instance
Definition: collision_object.h:318
void setIdentityTransform()
set the object in local coordinate
Definition: collision_object.h:310
void setUserData(void *data)
set user data in geometry
Definition: collision_object.h:141
void setTransform(const Matrix3s &R, const Vec3s &T)
set object's transform
Definition: collision_object.h:301
void init(bool compute_local_aabb=true)
Definition: collision_object.h:342
AABB aabb
AABB in global coordinate.
Definition: collision_object.h:354
virtual OBJECT_TYPE getObjectType() const
get the type of the object
Definition: collision_object.h:129
virtual Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition: collision_object.h:178
void * user_data
pointer to user defined data specific to this object
Definition: collision_object.h:357
AABB aabb_local
AABB in local coordinate, used for tight AABB when only translation transform.
Definition: collision_object.h:160
const CollisionGeometry * collisionGeometryPtr() const
get raw pointer to collision geometry of the object instance
Definition: collision_object.h:321
virtual CollisionGeometry * clone() const =0
Clone *this into a new CollisionGeometry.
virtual Scalar computeVolume() const
compute the volume
Definition: collision_object.h:183
void setTranslation(const Vec3s &T)
set object's translation
Definition: collision_object.h:298
Scalar cost_density
collision cost for unit volume
Definition: collision_object.h:166
virtual NODE_TYPE getNodeType() const
get the node type
Definition: collision_object.h:132
bool isFree() const
whether the object is completely free
Definition: collision_object.h:147
const Transform3s & getTransform() const
get object's transform
Definition: collision_object.h:292
shared_ptr< CollisionGeometry > cgeom
Definition: collision_object.h:349
virtual Matrix3s computeMomentofInertiaRelatedToCOM() const
compute the inertia matrix, related to the com
Definition: collision_object.h:186
void * getUserData() const
get user data in geometry
Definition: collision_object.h:138
CollisionObject(const shared_ptr< CollisionGeometry > &cgeom_, const Transform3s &tf, bool compute_local_aabb=true)
Definition: collision_object.h:224
@ GEOM_CONE
Definition: collision_object.h:77
@ BV_KDOP24
Definition: collision_object.h:73
@ GEOM_TRIANGLE
Definition: collision_object.h:84
@ BV_KDOP18
Definition: collision_object.h:72
@ BV_RSS
Definition: collision_object.h:68
@ GEOM_CONVEX32
Definition: collision_object.h:80
@ GEOM_BOX
Definition: collision_object.h:74
@ GEOM_SPHERE
Definition: collision_object.h:75
@ BV_KDOP16
Definition: collision_object.h:71
@ NODE_COUNT
Definition: collision_object.h:89
@ BV_kIOS
Definition: collision_object.h:69
@ GEOM_CYLINDER
Definition: collision_object.h:78
@ GEOM_CAPSULE
Definition: collision_object.h:76
@ HF_OBBRSS
Definition: collision_object.h:88
@ BV_UNKNOWN
Definition: collision_object.h:65
@ BV_AABB
Definition: collision_object.h:66
@ GEOM_CONVEX16
Definition: collision_object.h:79
@ BV_OBBRSS
Definition: collision_object.h:70
@ GEOM_ELLIPSOID
Definition: collision_object.h:86
@ GEOM_HALFSPACE
Definition: collision_object.h:83
@ BV_OBB
Definition: collision_object.h:67
@ GEOM_PLANE
Definition: collision_object.h:82
@ HF_AABB
Definition: collision_object.h:87
@ GEOM_CONVEX
Definition: collision_object.h:81
@ GEOM_OCTREE
Definition: collision_object.h:85
bool isOccupied() const
whether the object is completely occupied
Definition: collision_object.h:144
bool isIdentityTransform() const
whether the object is in local coordinate
Definition: collision_object.h:307
CollisionGeometry(const CollisionGeometry &other)=default
Copy constructor.
AABB & getAABB()
get the AABB in world space
Definition: collision_object.h:257
Scalar threshold_occupied
threshold for occupied ( >= is occupied)
Definition: collision_object.h:169
void setRotation(const Matrix3s &R)
set object's rotation matrix
Definition: collision_object.h:295
~CollisionObject()
Definition: collision_object.h:245
const shared_ptr< const CollisionGeometry > collisionGeometry() const
get shared pointer to collision geometry of the object instance
Definition: collision_object.h:313
const Matrix3s & getRotation() const
get matrix rotation of the object
Definition: collision_object.h:289
NODE_TYPE getNodeType() const
get the node type
Definition: collision_object.h:251
bool operator!=(const CollisionObject &other) const
Definition: collision_object.h:241
virtual Vec3s computeCOM() const
compute center of mass
Definition: collision_object.h:175
virtual void computeLocalAABB()=0
compute the AABB for object in local coordinate
bool operator==(const CollisionObject &other) const
Definition: collision_object.h:237
Scalar threshold_free
threshold for free (<= is free)
Definition: collision_object.h:172
bool isUncertain() const
whether the object has some uncertainty
const Vec3s & getTranslation() const
get translation of the object
Definition: collision_object.h:286
bool operator==(const CollisionGeometry &other) const
Equality operator.
Definition: collision_object.h:114
CollisionObject(const shared_ptr< CollisionGeometry > &cgeom_, bool compute_local_aabb=true)
Definition: collision_object.h:218
virtual ~CollisionGeometry()
Definition: collision_object.h:108
Transform3s t
Definition: collision_object.h:351
CollisionGeometry * collisionGeometryPtr()
get raw pointer to collision geometry of the object instance
Definition: collision_object.h:324
@ OT_BVH
Definition: collision_object.h:54
@ OT_GEOM
Definition: collision_object.h:55
@ OT_COUNT
Definition: collision_object.h:58
@ OT_OCTREE
Definition: collision_object.h:56
@ OT_HFIELD
Definition: collision_object.h:57
@ OT_UNKNOWN
Definition: collision_object.h:53
CollisionObject(const shared_ptr< CollisionGeometry > &cgeom_, const Matrix3s &R, const Vec3s &T, bool compute_local_aabb=true)
Definition: collision_object.h:230
OBJECT_TYPE getObjectType() const
get the type of the object
Definition: collision_object.h:248
bool operator!=(const CollisionGeometry &other) const
Difference operator.
Definition: collision_object.h:124
void setUserData(void *data)
set user data in object
Definition: collision_object.h:283
Vec3s aabb_center
AABB center in local coordinate.
Definition: collision_object.h:153
CollisionGeometry()
Definition: collision_object.h:98
void setCollisionGeometry(const shared_ptr< CollisionGeometry > &collision_geometry, bool compute_local_aabb=true)
Associate a new CollisionGeometry.
Definition: collision_object.h:332
void * getUserData() const
get user data in object
Definition: collision_object.h:280
const AABB & getAABB() const
get the AABB in world space
Definition: collision_object.h:254
void computeAABB()
compute the AABB in world space
Definition: collision_object.h:260
Scalar aabb_radius
AABB radius.
Definition: collision_object.h:156
Main namespace.
Definition: broadphase_bruteforce.h:44
NODE_TYPE
traversal node type: bounding volume (AABB, OBB, RSS, kIOS, OBBRSS, KDOP16, KDOP18,...
Definition: collision_object.h:64
KDOP< N > translate(const KDOP< N > &bv, const Vec3s &t)
translate the KDOP BV
Eigen::Matrix< Scalar, 3, 1 > Vec3s
Definition: data_types.h:70
double Scalar
Definition: data_types.h:68
OBJECT_TYPE
object type: BVH (mesh, points), basic geometry, octree
Definition: collision_object.h:52
Eigen::Matrix< Scalar, 3, 3 > Matrix3s
Definition: data_types.h:74
bool isEqual(const Eigen::MatrixBase< Derived > &lhs, const Eigen::MatrixBase< OtherDerived > &rhs, const Scalar tol=std::numeric_limits< Scalar >::epsilon() *100)
Definition: tools.h:204