FCL  0.6.0
Flexible Collision Library
octree-inl.h
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 FCL_OCTREE_INL_H
39 #define FCL_OCTREE_INL_H
40 
41 #include "fcl/geometry/octree/octree.h"
42 
43 #include "fcl/config.h"
44 
45 #if FCL_HAVE_OCTOMAP
46 
47 namespace fcl
48 {
49 
50 //==============================================================================
51 extern template
52 class OcTree<double>;
53 
54 //==============================================================================
55 extern template
56 void computeChildBV(const AABB<double>& root_bv, unsigned int i, AABB<double>& child_bv);
57 
58 //==============================================================================
59 template <typename S>
60 OcTree<S>::OcTree(S resolution)
61  : tree(std::shared_ptr<const octomap::OcTree>(new octomap::OcTree(resolution)))
62 {
63  default_occupancy = tree->getOccupancyThres();
64 
65  // default occupancy/free threshold is consistent with default setting from octomap
66  occupancy_threshold = tree->getOccupancyThres();
67  free_threshold = 0;
68 }
69 
70 //==============================================================================
71 template <typename S>
72 OcTree<S>::OcTree(const std::shared_ptr<const octomap::OcTree>& tree_)
73  : tree(tree_)
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 
82 //==============================================================================
83 template <typename S>
84 void OcTree<S>::computeLocalAABB()
85 {
86  this->aabb_local = getRootBV();
87  this->aabb_center = this->aabb_local.center();
88  this->aabb_radius = (this->aabb_local.min_ - this->aabb_center).norm();
89 }
90 
91 //==============================================================================
92 template <typename S>
93 AABB<S> OcTree<S>::getRootBV() const
94 {
95  S delta = (1 << tree->getTreeDepth()) * tree->getResolution() / 2;
96 
97  // std::cout << "octree size " << delta << std::endl;
98  return AABB<S>(Vector3<S>(-delta, -delta, -delta), Vector3<S>(delta, delta, delta));
99 }
100 
101 //==============================================================================
102 template <typename S>
103 typename OcTree<S>::OcTreeNode* OcTree<S>::getRoot() const
104 {
105  return tree->getRoot();
106 }
107 
108 //==============================================================================
109 template <typename S>
110 bool OcTree<S>::isNodeOccupied(const OcTree<S>::OcTreeNode* node) const
111 {
112  // return tree->isNodeOccupied(node);
113  return node->getOccupancy() >= occupancy_threshold;
114 }
115 
116 //==============================================================================
117 template <typename S>
118 bool OcTree<S>::isNodeFree(const OcTree<S>::OcTreeNode* node) const
119 {
120  // return false; // default no definitely free node
121  return node->getOccupancy() <= free_threshold;
122 }
123 
124 //==============================================================================
125 template <typename S>
126 bool OcTree<S>::isNodeUncertain(const OcTree<S>::OcTreeNode* node) const
127 {
128  return (!isNodeOccupied(node)) && (!isNodeFree(node));
129 }
130 
131 //==============================================================================
132 template <typename S>
133 S OcTree<S>::getOccupancyThres() const
134 {
135  return occupancy_threshold;
136 }
137 
138 //==============================================================================
139 template <typename S>
140 S OcTree<S>::getFreeThres() const
141 {
142  return free_threshold;
143 }
144 
145 //==============================================================================
146 template <typename S>
147 S OcTree<S>::getDefaultOccupancy() const
148 {
149  return default_occupancy;
150 }
151 
152 //==============================================================================
153 template <typename S>
154 void OcTree<S>::setCellDefaultOccupancy(S d)
155 {
156  default_occupancy = d;
157 }
158 
159 //==============================================================================
160 template <typename S>
161 void OcTree<S>::setOccupancyThres(S d)
162 {
163  occupancy_threshold = d;
164 }
165 
166 //==============================================================================
167 template <typename S>
168 void OcTree<S>::setFreeThres(S d)
169 {
170  free_threshold = d;
171 }
172 
173 //==============================================================================
174 template <typename S>
175 typename OcTree<S>::OcTreeNode* OcTree<S>::getNodeChild(
176  typename OcTree<S>::OcTreeNode* node, unsigned int childIdx)
177 {
178 #if OCTOMAP_VERSION_AT_LEAST(1,8,0)
179  return tree->getNodeChild(node, childIdx);
180 #else
181  return node->getChild(childIdx);
182 #endif
183 }
184 
185 //==============================================================================
186 template <typename S>
187 const typename OcTree<S>::OcTreeNode* OcTree<S>::getNodeChild(
188  const typename OcTree<S>::OcTreeNode* node, unsigned int childIdx) const
189 {
190 #if OCTOMAP_VERSION_AT_LEAST(1,8,0)
191  return tree->getNodeChild(node, childIdx);
192 #else
193  return node->getChild(childIdx);
194 #endif
195 }
196 
197 //==============================================================================
198 template <typename S>
199 bool OcTree<S>::nodeChildExists(
200  const OcTree<S>::OcTreeNode* node, unsigned int childIdx) const
201 {
202 #if OCTOMAP_VERSION_AT_LEAST(1,8,0)
203  return tree->nodeChildExists(node, childIdx);
204 #else
205  return node->childExists(childIdx);
206 #endif
207 }
208 
209 //==============================================================================
210 template <typename S>
211 bool OcTree<S>::nodeHasChildren(const OcTree<S>::OcTreeNode* node) const
212 {
213 #if OCTOMAP_VERSION_AT_LEAST(1,8,0)
214  return tree->nodeHasChildren(node);
215 #else
216  return node->hasChildren();
217 #endif
218 }
219 
220 //==============================================================================
221 template <typename S>
222 OBJECT_TYPE OcTree<S>::getObjectType() const
223 {
224  return OT_OCTREE;
225 }
226 
227 //==============================================================================
228 template <typename S>
229 NODE_TYPE OcTree<S>::getNodeType() const
230 {
231  return GEOM_OCTREE;
232 }
233 
234 //==============================================================================
235 template <typename S>
236 std::vector<std::array<S, 6>> OcTree<S>::toBoxes() const
237 {
238  std::vector<std::array<S, 6>> boxes;
239  boxes.reserve(tree->size() / 2);
240  for(auto it = tree->begin(tree->getTreeDepth()), end = tree->end();
241  it != end;
242  ++it)
243  {
244  // if(tree->isNodeOccupied(*it))
245  if(isNodeOccupied(&*it))
246  {
247  S size = it.getSize();
248  S x = it.getX();
249  S y = it.getY();
250  S z = it.getZ();
251  S c = (*it).getOccupancy();
252  S t = tree->getOccupancyThres();
253 
254  std::array<S, 6> box = {{x, y, z, size, c, t}};
255  boxes.push_back(box);
256  }
257  }
258  return boxes;
259 }
260 
261 //==============================================================================
262 template <typename S>
263 void computeChildBV(const AABB<S>& root_bv, unsigned int i, AABB<S>& child_bv)
264 {
265  if(i&1)
266  {
267  child_bv.min_[0] = (root_bv.min_[0] + root_bv.max_[0]) * 0.5;
268  child_bv.max_[0] = root_bv.max_[0];
269  }
270  else
271  {
272  child_bv.min_[0] = root_bv.min_[0];
273  child_bv.max_[0] = (root_bv.min_[0] + root_bv.max_[0]) * 0.5;
274  }
275 
276  if(i&2)
277  {
278  child_bv.min_[1] = (root_bv.min_[1] + root_bv.max_[1]) * 0.5;
279  child_bv.max_[1] = root_bv.max_[1];
280  }
281  else
282  {
283  child_bv.min_[1] = root_bv.min_[1];
284  child_bv.max_[1] = (root_bv.min_[1] + root_bv.max_[1]) * 0.5;
285  }
286 
287  if(i&4)
288  {
289  child_bv.min_[2] = (root_bv.min_[2] + root_bv.max_[2]) * 0.5;
290  child_bv.max_[2] = root_bv.max_[2];
291  }
292  else
293  {
294  child_bv.min_[2] = root_bv.min_[2];
295  child_bv.max_[2] = (root_bv.min_[2] + root_bv.max_[2]) * 0.5;
296  }
297 }
298 
299 } // namespace fcl
300 
301 #endif
302 
303 #endif
NODE_TYPE
traversal node type: bounding volume (AABB, OBB, RSS, kIOS, OBBRSS, KDOP16, KDOP18, kDOP24), basic shape (box, sphere, ellipsoid, capsule, cone, cylinder, convex, plane, halfspace, triangle), and octree
Definition: collision_geometry.h:54
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
OBJECT_TYPE
object type: BVH (mesh, points), basic geometry, octree
Definition: collision_geometry.h:51