FCL  0.6.0
Flexible Collision Library
BVH_utility-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_BVH_UTILITY_INL_H
39 #define FCL_BVH_UTILITY_INL_H
40 
41 #include "fcl/geometry/bvh/BVH_utility.h"
42 
43 #include "fcl/math/bv/utility.h"
44 
45 namespace fcl
46 {
47 
48 //==============================================================================
49 extern template
50 void BVHExpand(
51  BVHModel<OBB<double>>& model, const Variance3<double>* ucs, double r);
52 
53 //==============================================================================
54 extern template
55 void BVHExpand(
56  BVHModel<RSS<double>>& model, const Variance3<double>* ucs, double r);
57 
58 //==============================================================================
59 template <typename S, typename BV>
60 void BVHExpand(BVHModel<BV>& model, const Variance3<S>* ucs, S r)
61 {
62  for(int i = 0; i < model.num_bvs; ++i)
63  {
64  BVNode<BV>& bvnode = model.getBV(i);
65 
66  BV bv;
67  for(int j = 0; j < bvnode.num_primitives; ++j)
68  {
69  int v_id = bvnode.first_primitive + j;
70  const Variance3<S>& uc = ucs[v_id];
71 
72  Vector3<S>& v = model.vertices[bvnode.first_primitive + j];
73 
74  for(int k = 0; k < 3; ++k)
75  {
76  bv += (v + uc.axis.col(k) * (r * uc.sigma[k]));
77  bv += (v - uc.axis.col(k) * (r * uc.sigma[k]));
78  }
79  }
80 
81  bvnode.bv = bv;
82  }
83 }
84 
85 //==============================================================================
86 template <typename S>
87 void BVHExpand(
88  BVHModel<OBB<S>>& model,
89  const Variance3<S>* ucs,
90  S r)
91 {
92  for(int i = 0; i < model.getNumBVs(); ++i)
93  {
94  BVNode<OBB<S>>& bvnode = model.getBV(i);
95 
96  Vector3<S>* vs = new Vector3<S>[bvnode.num_primitives * 6];
97  // TODO(JS): We could use one std::vector outside of the outter for-loop,
98  // and reuse it rather than creating and destructing the array every
99  // iteration.
100 
101  for(int j = 0; j < bvnode.num_primitives; ++j)
102  {
103  int v_id = bvnode.first_primitive + j;
104  const Variance3<S>& uc = ucs[v_id];
105 
106  Vector3<S>&v = model.vertices[bvnode.first_primitive + j];
107 
108  for(int k = 0; k < 3; ++k)
109  {
110  const auto index1 = 6 * j + 2 * k;
111  const auto index2 = index1 + 1;
112  vs[index1] = v;
113  vs[index1].noalias() += uc.axis.col(k) * (r * uc.sigma[k]);
114  vs[index2] = v;
115  vs[index2].noalias() -= uc.axis.col(k) * (r * uc.sigma[k]);
116  }
117  }
118 
119  OBB<S> bv;
120  fit(vs, bvnode.num_primitives * 6, bv);
121 
122  delete [] vs;
123 
124  bvnode.bv = bv;
125  }
126 }
127 
128 //==============================================================================
129 template <typename S>
130 void BVHExpand(
131  BVHModel<RSS<S>>& model,
132  const Variance3<S>* ucs,
133  S r)
134 {
135  for(int i = 0; i < model.getNumBVs(); ++i)
136  {
137  BVNode<RSS<S>>& bvnode = model.getBV(i);
138 
139  Vector3<S>* vs = new Vector3<S>[bvnode.num_primitives * 6];
140  // TODO(JS): We could use one std::vector outside of the outter for-loop,
141  // and reuse it rather than creating and destructing the array every
142  // iteration.
143 
144  for(int j = 0; j < bvnode.num_primitives; ++j)
145  {
146  int v_id = bvnode.first_primitive + j;
147  const Variance3<S>& uc = ucs[v_id];
148 
149  Vector3<S>&v = model.vertices[bvnode.first_primitive + j];
150 
151  for(int k = 0; k < 3; ++k)
152  {
153  vs[6 * j + 2 * k] = v + uc.axis.col(k) * (r * uc.sigma[k]);
154  vs[6 * j + 2 * k + 1] = v - uc.axis.col(k) * (r * uc.sigma[k]);
155  }
156  }
157 
158  RSS<S> bv;
159  fit(vs, bvnode.num_primitives * 6, bv);
160 
161  delete [] vs;
162 
163  bvnode.bv = bv;
164  }
165 }
166 
167 } // namespace fcl
168 
169 #endif
Vector3< S > sigma
Variations along the eign axes.
Definition: variance3.h:58
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
const BVNode< BV > & getBV(int id) const
We provide getBV() and getNumBVs() because BVH may be compressed (in future), so we must provide some...
Definition: BVH_model-inl.h:160
Vector3< S > * vertices
Geometry point data.
Definition: BVH_model.h:162
Matrix3< S > axis
Matrix whose columns are eigenvectors of Sigma.
Definition: variance3.h:61
void fit(Vector3< typename BV::S > *ps, int n, BV &bv)
Compute a bounding volume that fits a set of n points.
Definition: utility-inl.h:647
int num_primitives
The number of primitives belonging to the current node.
Definition: BV_node_base.h:63
Class for variance matrix in 3d.
Definition: variance3.h:51
A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as ...
Definition: BVH_model.h:57
BV bv
bounding volume storing the geometry
Definition: BV_node.h:57
A class describing a bounding volume node. It includes the tree structure providing in BVNodeBase and...
Definition: BV_node.h:52
int first_primitive
The start id the primitive belonging to the current node. The index is referred to the primitive_indi...
Definition: BV_node_base.h:60