hpp-fcl: include/hpp/fcl/octree.h Source File

hpp-fcl 1.7.5 HPP fork of FCL -- The Flexible Collision Library

octree.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
39 #ifndef HPP_FCL_OCTREE_H
40 #define HPP_FCL_OCTREE_H
41
42
43 #include <boost/array.hpp>
44
45 #include <octomap/octomap.h>
46 #include <hpp/fcl/fwd.hh>
47 #include <hpp/fcl/BV/AABB.h>
48 #include <hpp/fcl/collision_object.h>
49
50 namespace hpp
51 {
52 namespace fcl
53 {
54
56 class HPP_FCL_DLLAPI OcTree : public CollisionGeometry
57 {
58 private:
59 shared_ptr<const octomap::OcTree> tree;
60
61 FCL_REAL default_occupancy;
62
63 FCL_REAL occupancy_threshold;
64 FCL_REAL free_threshold;
65
66 public:
67
68 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
69
70 typedef octomap::OcTreeNode OcTreeNode;
71
73 OcTree(FCL_REAL resolution) : tree(shared_ptr<const octomap::OcTree>(new octomap::OcTree(resolution)))
74 {
75 default_occupancy = tree->getOccupancyThres();
76
77 // default occupancy/free threshold is consistent with default setting from octomap
78 occupancy_threshold = tree->getOccupancyThres();
79 free_threshold = 0;
80 }
81
83 OcTree(const shared_ptr<const octomap::OcTree>& tree_) : tree(tree_)
84 {
85 default_occupancy = tree->getOccupancyThres();
86
87 // default occupancy/free threshold is consistent with default setting from octomap
88 occupancy_threshold = tree->getOccupancyThres();
89 free_threshold = 0;
90 }
91
92 OcTree(const OcTree & other)
93 : CollisionGeometry(other)
94 , tree(other.tree)
95 , default_occupancy(other.default_occupancy)
96 , occupancy_threshold(other.occupancy_threshold)
97 , free_threshold(other.free_threshold)
98 {
99 }
100
101 OcTree * clone() const { return new OcTree(*this); }
102
104 void computeLocalAABB()
105 {
106 aabb_local = getRootBV();
107 aabb_center = aabb_local.center();
108 aabb_radius = (aabb_local.min_ - aabb_center).norm();
109 }
110
112 AABB getRootBV() const
113 {
114 FCL_REAL delta = (1 << tree->getTreeDepth()) * tree->getResolution() / 2;
115
116 // std::cout << "octree size " << delta << std::endl;
117 return AABB(Vec3f(-delta, -delta, -delta), Vec3f(delta, delta, delta));
118 }
119
121 unsigned int getTreeDepth() const
122 {
123 return tree->getTreeDepth();
124 }
125
127 OcTreeNode* getRoot() const
128 {
129 return tree->getRoot();
130 }
131
133 bool isNodeOccupied(const OcTreeNode* node) const
134 {
135 // return tree->isNodeOccupied(node);
136 return node->getOccupancy() >= occupancy_threshold;
137 }
138
140 bool isNodeFree(const OcTreeNode* node) const
141 {
142 // return false; // default no definitely free node
143 return node->getOccupancy() <= free_threshold;
144 }
145
147 bool isNodeUncertain(const OcTreeNode* node) const
148 {
149 return (!isNodeOccupied(node)) && (!isNodeFree(node));
150 }
151
154 std::vector<boost::array<FCL_REAL, 6> > toBoxes() const
155 {
156 std::vector<boost::array<FCL_REAL, 6> > boxes;
157 boxes.reserve(tree->size() / 2);
158 for(octomap::OcTree::iterator it =
159 tree->begin((unsigned char) tree->getTreeDepth()), end = tree->end();
160 it != end;
161 ++it)
162 {
163 // if(tree->isNodeOccupied(*it))
164 if(isNodeOccupied(&*it))
165 {
166 FCL_REAL size = it.getSize();
167 FCL_REAL x = it.getX();
168 FCL_REAL y = it.getY();
169 FCL_REAL z = it.getZ();
170 FCL_REAL c = (*it).getOccupancy();
171 FCL_REAL t = tree->getOccupancyThres();
172
173 boost::array<FCL_REAL, 6> box = {{x, y, z, size, c, t}};
174 boxes.push_back(box);
175 }
176 }
177 return boxes;
178 }
179
181 FCL_REAL getOccupancyThres() const
182 {
183 return occupancy_threshold;
184 }
185
187 FCL_REAL getFreeThres() const
188 {
189 return free_threshold;
190 }
191
192 FCL_REAL getDefaultOccupancy() const
193 {
194 return default_occupancy;
195 }
196
197 void setCellDefaultOccupancy(FCL_REAL d)
198 {
199 default_occupancy = d;
200 }
201
202 void setOccupancyThres(FCL_REAL d)
203 {
204 occupancy_threshold = d;
205 }
206
207 void setFreeThres(FCL_REAL d)
208 {
209 free_threshold = d;
210 }
211
213 OcTreeNode* getNodeChild(OcTreeNode* node, unsigned int childIdx)
214 {
215 #if OCTOMAP_VERSION_AT_LEAST(1,8,0)
216 return tree->getNodeChild(node, childIdx);
217 #else
218 return node->getChild(childIdx);
219 #endif
220 }
221
223 const OcTreeNode* getNodeChild(const OcTreeNode* node, unsigned int childIdx) const
224 {
225 #if OCTOMAP_VERSION_AT_LEAST(1,8,0)
226 return tree->getNodeChild(node, childIdx);
227 #else
228 return node->getChild(childIdx);
229 #endif
230 }
231
233 bool nodeChildExists(const OcTreeNode* node, unsigned int childIdx) const
234 {
235 #if OCTOMAP_VERSION_AT_LEAST(1,8,0)
236 return tree->nodeChildExists(node, childIdx);
237 #else
238 return node->childExists(childIdx);
239 #endif
240 }
241
243 bool nodeHasChildren(const OcTreeNode* node) const
244 {
245 #if OCTOMAP_VERSION_AT_LEAST(1,8,0)
246 return tree->nodeHasChildren(node);
247 #else
248 return node->hasChildren();
249 #endif
250 }
251
253 OBJECT_TYPE getObjectType() const { return OT_OCTREE; }
254
256 NODE_TYPE getNodeType() const { return GEOM_OCTREE; }
257 };
258
260 static inline void computeChildBV(const AABB& root_bv, unsigned int i, AABB& child_bv)
261 {
262 if(i&1)
263 {
264 child_bv.min_[0] = (root_bv.min_[0] + root_bv.max_[0]) * 0.5;
265 child_bv.max_[0] = root_bv.max_[0];
266 }
267 else
268 {
269 child_bv.min_[0] = root_bv.min_[0];
270 child_bv.max_[0] = (root_bv.min_[0] + root_bv.max_[0]) * 0.5;
271 }
272
273 if(i&2)
274 {
275 child_bv.min_[1] = (root_bv.min_[1] + root_bv.max_[1]) * 0.5;
276 child_bv.max_[1] = root_bv.max_[1];
277 }
278 else
279 {
280 child_bv.min_[1] = root_bv.min_[1];
281 child_bv.max_[1] = (root_bv.min_[1] + root_bv.max_[1]) * 0.5;
282 }
283
284 if(i&4)
285 {
286 child_bv.min_[2] = (root_bv.min_[2] + root_bv.max_[2]) * 0.5;
287 child_bv.max_[2] = root_bv.max_[2];
288 }
289 else
290 {
291 child_bv.min_[2] = root_bv.min_[2];
292 child_bv.max_[2] = (root_bv.min_[2] + root_bv.max_[2]) * 0.5;
293 }
294 }
295
304 HPP_FCL_DLLAPI OcTree makeOctree(const Eigen::Matrix<FCL_REAL,Eigen::Dynamic,3> & point_cloud,
305 const FCL_REAL & resolution);
306
307 }
308
309 } // namespace hpp
310
311 #endif
AABB.h
hpp::fcl::AABB
A class describing the AABB collision structure, which is a box in 3D space determined by two diagona...
Definition: AABB.h:56
hpp::fcl::CollisionGeometry
The geometry for the object for collision or distance computation.
Definition: collision_object.h:66
hpp::fcl::OcTree
Octree is one type of collision geometry which can encode uncertainty information in the sensor data.
Definition: octree.h:57
hpp::fcl::OcTree::getRootBV
AABB getRootBV() const
get the bounding volume for the root
Definition: octree.h:112
hpp::fcl::OcTree::OcTreeNode
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef octomap::OcTreeNode OcTreeNode
Definition: octree.h:70
hpp::fcl::OcTree::OcTree
OcTree(FCL_REAL resolution)
construct octree with a given resolution
Definition: octree.h:73
hpp::fcl::OcTree::getObjectType
OBJECT_TYPE getObjectType() const
return object type, it is an octree
Definition: octree.h:253
hpp::fcl::OcTree::isNodeOccupied
bool isNodeOccupied(const OcTreeNode *node) const
whether one node is completely occupied
Definition: octree.h:133
hpp::fcl::OcTree::getFreeThres
FCL_REAL getFreeThres() const
the threshold used to decide whether one node is free, this is NOT the octree free_threshold
Definition: octree.h:187
hpp::fcl::OcTree::getDefaultOccupancy
FCL_REAL getDefaultOccupancy() const
Definition: octree.h:192
hpp::fcl::OcTree::OcTree
OcTree(const OcTree &other)
Definition: octree.h:92
hpp::fcl::OcTree::setCellDefaultOccupancy
void setCellDefaultOccupancy(FCL_REAL d)
Definition: octree.h:197
hpp::fcl::OcTree::getNodeChild
OcTreeNode * getNodeChild(OcTreeNode *node, unsigned int childIdx)
Definition: octree.h:213
hpp::fcl::OcTree::getRoot
OcTreeNode * getRoot() const
get the root node of the octree
Definition: octree.h:127
hpp::fcl::OcTree::toBoxes
std::vector< boost::array< FCL_REAL, 6 > > toBoxes() const
transform the octree into a bunch of boxes; uncertainty information is kept in the boxes....
Definition: octree.h:154
hpp::fcl::OcTree::getTreeDepth
unsigned int getTreeDepth() const
Returns the depth of octree.
Definition: octree.h:121
hpp::fcl::OcTree::setFreeThres
void setFreeThres(FCL_REAL d)
Definition: octree.h:207
hpp::fcl::OcTree::isNodeUncertain
bool isNodeUncertain(const OcTreeNode *node) const
whether one node is uncertain
Definition: octree.h:147
hpp::fcl::OcTree::clone
OcTree * clone() const
Clone *this into a new CollisionGeometry.
Definition: octree.h:101
hpp::fcl::OcTree::setOccupancyThres
void setOccupancyThres(FCL_REAL d)
Definition: octree.h:202
hpp::fcl::OcTree::nodeChildExists
bool nodeChildExists(const OcTreeNode *node, unsigned int childIdx) const
return true if the child at childIdx exists
Definition: octree.h:233
hpp::fcl::OcTree::getOccupancyThres
FCL_REAL getOccupancyThres() const
the threshold used to decide whether one node is occupied, this is NOT the octree occupied_thresold
Definition: octree.h:181
hpp::fcl::OcTree::getNodeChild
const OcTreeNode * getNodeChild(const OcTreeNode *node, unsigned int childIdx) const
Definition: octree.h:223
hpp::fcl::OcTree::computeLocalAABB
void computeLocalAABB()
compute the AABB for the octree in its local coordinate system
Definition: octree.h:104
hpp::fcl::OcTree::nodeHasChildren
bool nodeHasChildren(const OcTreeNode *node) const
return true if node has at least one child
Definition: octree.h:243
hpp::fcl::OcTree::OcTree
OcTree(const shared_ptr< const octomap::OcTree > &tree_)
construct octree from octomap
Definition: octree.h:83
hpp::fcl::OcTree::getNodeType
NODE_TYPE getNodeType() const
return node type, it is an octree
Definition: octree.h:256
hpp::fcl::OcTree::isNodeFree
bool isNodeFree(const OcTreeNode *node) const
whether one node is completely free
Definition: octree.h:140
collision_object.h
fwd.hh
hpp::fcl::OT_OCTREE
@ OT_OCTREE
Definition: collision_object.h:55
hpp::fcl::GEOM_OCTREE
@ GEOM_OCTREE
Definition: collision_object.h:59
hpp::fcl::Vec3f
Eigen::Matrix< FCL_REAL, 3, 1 > Vec3f
Definition: data_types.h:67
hpp::fcl::makeOctree
OcTree makeOctree(const Eigen::Matrix< FCL_REAL, Eigen::Dynamic, 3 > &point_cloud, const FCL_REAL &resolution)
Build an OcTree from a point cloud and a given resolution.
hpp::fcl::OBJECT_TYPE
OBJECT_TYPE
object type: BVH (mesh, points), basic geometry, octree
Definition: collision_object.h:55
hpp::fcl::NODE_TYPE
NODE_TYPE
traversal node type: bounding volume (AABB, OBB, RSS, kIOS, OBBRSS, KDOP16, KDOP18,...
Definition: collision_object.h:58
hpp::fcl::FCL_REAL
double FCL_REAL
Definition: data_types.h:66
hpp
Main namespace.
Definition: AABB.h:44