coal 3.0.1
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
Loading...
Searching...
No Matches
broadphase_dynamic_AABB_tree-inl.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-2016, 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_BROAD_PHASE_DYNAMIC_AABB_TREE_INL_H
39#define COAL_BROAD_PHASE_DYNAMIC_AABB_TREE_INL_H
40
42
43#include <limits>
44
45#if COAL_HAVE_OCTOMAP
46#include "coal/octree.h"
47#endif
48
49#include "coal/BV/BV.h"
51
52namespace coal {
53namespace detail {
54
55namespace dynamic_AABB_tree {
56
57#if COAL_HAVE_OCTOMAP
58
59//==============================================================================
60template <typename Derived>
61bool collisionRecurse_(DynamicAABBTreeCollisionManager::DynamicAABBNode* root1,
62 const OcTree* tree2, const OcTree::OcTreeNode* root2,
63 const AABB& root2_bv,
64 const Eigen::MatrixBase<Derived>& translation2,
65 CollisionCallBackBase* callback) {
66 if (!root2) {
67 if (root1->isLeaf()) {
68 CollisionObject* obj1 = static_cast<CollisionObject*>(root1->data);
69
70 if (!obj1->collisionGeometry()->isFree()) {
71 const AABB& root2_bv_t = translate(root2_bv, translation2);
72 if (root1->bv.overlap(root2_bv_t)) {
73 Box* box = new Box();
74 Transform3s box_tf;
76 tf2.translation() = translation2;
77 constructBox(root2_bv, tf2, *box, box_tf);
78
79 box->cost_density =
80 tree2->getOccupancyThres(); // thresholds are 0, 1, so uncertain
81
82 CollisionObject obj2(shared_ptr<CollisionGeometry>(box), box_tf);
83 return (*callback)(obj1, &obj2);
84 }
85 }
86 } else {
87 if (collisionRecurse_(root1->children[0], tree2, nullptr, root2_bv,
88 translation2, callback))
89 return true;
90 if (collisionRecurse_(root1->children[1], tree2, nullptr, root2_bv,
91 translation2, callback))
92 return true;
93 }
94
95 return false;
96 } else if (root1->isLeaf() && !tree2->nodeHasChildren(root2)) {
97 CollisionObject* obj1 = static_cast<CollisionObject*>(root1->data);
98
99 if (!tree2->isNodeFree(root2) && !obj1->collisionGeometry()->isFree()) {
100 const AABB& root2_bv_t = translate(root2_bv, translation2);
101 if (root1->bv.overlap(root2_bv_t)) {
102 Box* box = new Box();
103 Transform3s box_tf;
105 tf2.translation() = translation2;
106 constructBox(root2_bv, tf2, *box, box_tf);
107
108 box->cost_density = Scalar(root2->getOccupancy());
110
111 CollisionObject obj2(shared_ptr<CollisionGeometry>(box), box_tf);
112 return (*callback)(obj1, &obj2);
113 } else
114 return false;
115 } else
116 return false;
117 }
118
119 const AABB& root2_bv_t = translate(root2_bv, translation2);
120 if (tree2->isNodeFree(root2) || !root1->bv.overlap(root2_bv_t)) return false;
121
122 if (!tree2->nodeHasChildren(root2) ||
123 (!root1->isLeaf() && (root1->bv.size() > root2_bv.size()))) {
124 if (collisionRecurse_(root1->children[0], tree2, root2, root2_bv,
125 translation2, callback))
126 return true;
127 if (collisionRecurse_(root1->children[1], tree2, root2, root2_bv,
128 translation2, callback))
129 return true;
130 } else {
131 for (unsigned int i = 0; i < 8; ++i) {
132 if (tree2->nodeChildExists(root2, i)) {
133 const OcTree::OcTreeNode* child = tree2->getNodeChild(root2, i);
134 AABB child_bv;
135 computeChildBV(root2_bv, i, child_bv);
136
137 if (collisionRecurse_(root1, tree2, child, child_bv, translation2,
138 callback))
139 return true;
140 } else {
141 AABB child_bv;
142 computeChildBV(root2_bv, i, child_bv);
143 if (collisionRecurse_(root1, tree2, nullptr, child_bv, translation2,
144 callback))
145 return true;
146 }
147 }
148 }
149 return false;
150}
151
152//==============================================================================
153template <typename Derived>
154bool distanceRecurse_(DynamicAABBTreeCollisionManager::DynamicAABBNode* root1,
155 const OcTree* tree2, const OcTree::OcTreeNode* root2,
156 const AABB& root2_bv,
157 const Eigen::MatrixBase<Derived>& translation2,
158 DistanceCallBackBase* callback, Scalar& min_dist) {
159 if (root1->isLeaf() && !tree2->nodeHasChildren(root2)) {
160 if (tree2->isNodeOccupied(root2)) {
161 Box* box = new Box();
162 Transform3s box_tf;
164 tf2.translation() = translation2;
165 constructBox(root2_bv, tf2, *box, box_tf);
166 CollisionObject obj(shared_ptr<CollisionGeometry>(box), box_tf);
167 return (*callback)(static_cast<CollisionObject*>(root1->data), &obj,
168 min_dist);
169 } else
170 return false;
171 }
172
173 if (!tree2->isNodeOccupied(root2)) return false;
174
175 if (!tree2->nodeHasChildren(root2) ||
176 (!root1->isLeaf() && (root1->bv.size() > root2_bv.size()))) {
177 const AABB& aabb2 = translate(root2_bv, translation2);
178 Scalar d1 = aabb2.distance(root1->children[0]->bv);
179 Scalar d2 = aabb2.distance(root1->children[1]->bv);
180
181 if (d2 < d1) {
182 if (d2 < min_dist) {
183 if (distanceRecurse_(root1->children[1], tree2, root2, root2_bv,
184 translation2, callback, min_dist))
185 return true;
186 }
187
188 if (d1 < min_dist) {
189 if (distanceRecurse_(root1->children[0], tree2, root2, root2_bv,
190 translation2, callback, min_dist))
191 return true;
192 }
193 } else {
194 if (d1 < min_dist) {
195 if (distanceRecurse_(root1->children[0], tree2, root2, root2_bv,
196 translation2, callback, min_dist))
197 return true;
198 }
199
200 if (d2 < min_dist) {
201 if (distanceRecurse_(root1->children[1], tree2, root2, root2_bv,
202 translation2, callback, min_dist))
203 return true;
204 }
205 }
206 } else {
207 for (unsigned int i = 0; i < 8; ++i) {
208 if (tree2->nodeChildExists(root2, i)) {
209 const OcTree::OcTreeNode* child = tree2->getNodeChild(root2, i);
210 AABB child_bv;
211 computeChildBV(root2_bv, i, child_bv);
212 const AABB& aabb2 = translate(child_bv, translation2);
213
214 Scalar d = root1->bv.distance(aabb2);
215
216 if (d < min_dist) {
217 if (distanceRecurse_(root1, tree2, child, child_bv, translation2,
218 callback, min_dist))
219 return true;
220 }
221 }
222 }
223 }
224
225 return false;
226}
227
228#endif
229
230} // namespace dynamic_AABB_tree
231} // namespace detail
232} // namespace coal
233
234#endif
A class describing the AABB collision structure, which is a box in 3D space determined by two diagona...
Definition AABB.h:55
Scalar distance(const AABB &other) const
Distance between two AABBs.
Scalar size() const
Size of the AABB (used in BV_Splitter to order two AABBs)
Definition AABB.h:161
Center at zero point, axis aligned box.
Definition geometric_shapes.h:164
the object for collision or distance computation, contains the geometry and the transform information
Definition collision_object.h:216
Octree is one type of collision geometry which can encode uncertainty information in the sensor data.
Definition octree.h:53
bool nodeChildExists(const OcTreeNode *node, unsigned int childIdx) const
return true if the child at childIdx exists
Definition octree.h:259
Scalar getOccupancyThres() const
the threshold used to decide whether one node is occupied, this is NOT the octree occupied_thresold
Definition octree.h:225
OcTreeNode * getNodeChild(OcTreeNode *node, unsigned int childIdx)
Definition octree.h:240
bool nodeHasChildren(const OcTreeNode *node) const
return true if node has at least one child
Definition octree.h:268
octomap::OcTreeNode OcTreeNode
Definition octree.h:63
bool isNodeFree(const OcTreeNode *node) const
whether one node is completely free
Definition octree.h:165
bool isNodeOccupied(const OcTreeNode *node) const
whether one node is completely occupied
Definition octree.h:159
Simple transform class used locally by InterpMotion.
Definition transform.h:55
const Vec3s & translation() const
get translation
Definition transform.h:104
static Transform3s Identity()
Definition transform.h:68
Scalar cost_density
collision cost for unit volume
Definition collision_object.h:166
Scalar threshold_occupied
threshold for occupied ( >= is occupied)
Definition collision_object.h:169
const shared_ptr< const CollisionGeometry > collisionGeometry() const
get shared pointer to collision geometry of the object instance
Definition collision_object.h:313
Main namespace.
Definition broadphase_bruteforce.h:44
void constructBox(const AABB &bv, Box &box, Transform3s &tf)
construct a box shape (with a configuration) from a given bounding volume
KDOP< N > translate(const KDOP< N > &bv, const Vec3s &t)
translate the KDOP BV
double Scalar
Definition data_types.h:68
Base callback class for collision queries. This class can be supersed by child classes to provide des...
Definition broadphase_callbacks.h:49
Base callback class for distance queries. This class can be supersed by child classes to provide desi...
Definition broadphase_callbacks.h:72
dynamic AABB tree node
Definition node_base.h:49
NodeBase< BV > * children[2]
for leaf node, children nodes
Definition node_base.h:64
void * data
Definition node_base.h:65
bool isLeaf() const
whether is a leaf
Definition node_base-inl.h:54
BV bv
the bounding volume for the node
Definition node_base.h:51